bio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * The copyright in this software is being made available under the 2-clauses
  3. * BSD License, included below. This software may be subject to other third
  4. * party and contributor rights, including patent rights, and no such rights
  5. * are granted under this license.
  6. *
  7. * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
  8. * Copyright (c) 2002-2014, Professor Benoit Macq
  9. * Copyright (c) 2001-2003, David Janssens
  10. * Copyright (c) 2002-2003, Yannick Verschueren
  11. * Copyright (c) 2003-2007, Francois-Olivier Devaux
  12. * Copyright (c) 2003-2014, Antonin Descampe
  13. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions
  18. * are met:
  19. * 1. Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright
  22. * notice, this list of conditions and the following disclaimer in the
  23. * documentation and/or other materials provided with the distribution.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "opj_includes.h"
  38. /** @defgroup BIO BIO - Individual bit input-output stream */
  39. /*@{*/
  40. /** @name Local static functions */
  41. /*@{*/
  42. /**
  43. Read a bit
  44. @param bio BIO handle
  45. @return Returns the read bit
  46. */
  47. static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio);
  48. /**
  49. Write a byte
  50. @param bio BIO handle
  51. @return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
  52. */
  53. static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio);
  54. /**
  55. Read a byte
  56. @param bio BIO handle
  57. @return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
  58. */
  59. static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio);
  60. /*@}*/
  61. /*@}*/
  62. /*
  63. ==========================================================
  64. local functions
  65. ==========================================================
  66. */
  67. static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio)
  68. {
  69. bio->buf = (bio->buf << 8) & 0xffff;
  70. bio->ct = bio->buf == 0xff00 ? 7 : 8;
  71. if ((OPJ_SIZE_T)bio->bp >= (OPJ_SIZE_T)bio->end) {
  72. return OPJ_FALSE;
  73. }
  74. *bio->bp++ = (OPJ_BYTE)(bio->buf >> 8);
  75. return OPJ_TRUE;
  76. }
  77. static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio)
  78. {
  79. bio->buf = (bio->buf << 8) & 0xffff;
  80. bio->ct = bio->buf == 0xff00 ? 7 : 8;
  81. if ((OPJ_SIZE_T)bio->bp >= (OPJ_SIZE_T)bio->end) {
  82. return OPJ_FALSE;
  83. }
  84. bio->buf |= *bio->bp++;
  85. return OPJ_TRUE;
  86. }
  87. static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio)
  88. {
  89. if (bio->ct == 0) {
  90. opj_bio_bytein(
  91. bio); /* MSD: why not check the return value of this function ? */
  92. }
  93. bio->ct--;
  94. return (bio->buf >> bio->ct) & 1;
  95. }
  96. /*
  97. ==========================================================
  98. Bit Input/Output interface
  99. ==========================================================
  100. */
  101. opj_bio_t* opj_bio_create(void)
  102. {
  103. opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
  104. return bio;
  105. }
  106. void opj_bio_destroy(opj_bio_t *bio)
  107. {
  108. if (bio) {
  109. opj_free(bio);
  110. }
  111. }
  112. ptrdiff_t opj_bio_numbytes(opj_bio_t *bio)
  113. {
  114. return (bio->bp - bio->start);
  115. }
  116. void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len)
  117. {
  118. bio->start = bp;
  119. bio->end = bp + len;
  120. bio->bp = bp;
  121. bio->buf = 0;
  122. bio->ct = 8;
  123. }
  124. void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len)
  125. {
  126. bio->start = bp;
  127. bio->end = bp + len;
  128. bio->bp = bp;
  129. bio->buf = 0;
  130. bio->ct = 0;
  131. }
  132. void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b)
  133. {
  134. if (bio->ct == 0) {
  135. opj_bio_byteout(
  136. bio); /* MSD: why not check the return value of this function ? */
  137. }
  138. bio->ct--;
  139. bio->buf |= b << bio->ct;
  140. }
  141. void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n)
  142. {
  143. OPJ_INT32 i;
  144. assert((n > 0U) && (n <= 32U));
  145. for (i = (OPJ_INT32)n - 1; i >= 0; i--) {
  146. opj_bio_putbit(bio, (v >> i) & 1);
  147. }
  148. }
  149. OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n)
  150. {
  151. OPJ_INT32 i;
  152. OPJ_UINT32 v;
  153. assert((n > 0U) /* && (n <= 32U)*/);
  154. #ifdef OPJ_UBSAN_BUILD
  155. /* This assert fails for some corrupted images which are gracefully rejected */
  156. /* Add this assert only for ubsan build. */
  157. /* This is the condition for overflow not to occur below which is needed because of OPJ_NOSANITIZE */
  158. assert(n <= 32U);
  159. #endif
  160. v = 0U;
  161. for (i = (OPJ_INT32)n - 1; i >= 0; i--) {
  162. v |= opj_bio_getbit(bio) <<
  163. i; /* can't overflow, opj_bio_getbit returns 0 or 1 */
  164. }
  165. return v;
  166. }
  167. OPJ_BOOL opj_bio_flush(opj_bio_t *bio)
  168. {
  169. if (! opj_bio_byteout(bio)) {
  170. return OPJ_FALSE;
  171. }
  172. if (bio->ct == 7) {
  173. if (! opj_bio_byteout(bio)) {
  174. return OPJ_FALSE;
  175. }
  176. }
  177. return OPJ_TRUE;
  178. }
  179. OPJ_BOOL opj_bio_inalign(opj_bio_t *bio)
  180. {
  181. if ((bio->buf & 0xff) == 0xff) {
  182. if (! opj_bio_bytein(bio)) {
  183. return OPJ_FALSE;
  184. }
  185. }
  186. bio->ct = 0;
  187. return OPJ_TRUE;
  188. }