bio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. Write a bit
  44. @param bio BIO handle
  45. @param b Bit to write (0 or 1)
  46. */
  47. static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b);
  48. /**
  49. Read a bit
  50. @param bio BIO handle
  51. @return Returns the read bit
  52. */
  53. static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio);
  54. /**
  55. Write 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_byteout(opj_bio_t *bio);
  60. /**
  61. Read a byte
  62. @param bio BIO handle
  63. @return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
  64. */
  65. static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio);
  66. /*@}*/
  67. /*@}*/
  68. /*
  69. ==========================================================
  70. local functions
  71. ==========================================================
  72. */
  73. static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio)
  74. {
  75. bio->buf = (bio->buf << 8) & 0xffff;
  76. bio->ct = bio->buf == 0xff00 ? 7 : 8;
  77. if ((OPJ_SIZE_T)bio->bp >= (OPJ_SIZE_T)bio->end) {
  78. return OPJ_FALSE;
  79. }
  80. *bio->bp++ = (OPJ_BYTE)(bio->buf >> 8);
  81. return OPJ_TRUE;
  82. }
  83. static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio)
  84. {
  85. bio->buf = (bio->buf << 8) & 0xffff;
  86. bio->ct = bio->buf == 0xff00 ? 7 : 8;
  87. if ((OPJ_SIZE_T)bio->bp >= (OPJ_SIZE_T)bio->end) {
  88. return OPJ_FALSE;
  89. }
  90. bio->buf |= *bio->bp++;
  91. return OPJ_TRUE;
  92. }
  93. static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b)
  94. {
  95. if (bio->ct == 0) {
  96. opj_bio_byteout(
  97. bio); /* MSD: why not check the return value of this function ? */
  98. }
  99. bio->ct--;
  100. bio->buf |= b << bio->ct;
  101. }
  102. static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio)
  103. {
  104. if (bio->ct == 0) {
  105. opj_bio_bytein(
  106. bio); /* MSD: why not check the return value of this function ? */
  107. }
  108. bio->ct--;
  109. return (bio->buf >> bio->ct) & 1;
  110. }
  111. /*
  112. ==========================================================
  113. Bit Input/Output interface
  114. ==========================================================
  115. */
  116. opj_bio_t* opj_bio_create(void)
  117. {
  118. opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
  119. return bio;
  120. }
  121. void opj_bio_destroy(opj_bio_t *bio)
  122. {
  123. if (bio) {
  124. opj_free(bio);
  125. }
  126. }
  127. ptrdiff_t opj_bio_numbytes(opj_bio_t *bio)
  128. {
  129. return (bio->bp - bio->start);
  130. }
  131. void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len)
  132. {
  133. bio->start = bp;
  134. bio->end = bp + len;
  135. bio->bp = bp;
  136. bio->buf = 0;
  137. bio->ct = 8;
  138. }
  139. void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len)
  140. {
  141. bio->start = bp;
  142. bio->end = bp + len;
  143. bio->bp = bp;
  144. bio->buf = 0;
  145. bio->ct = 0;
  146. }
  147. void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n)
  148. {
  149. OPJ_INT32 i;
  150. assert((n > 0U) && (n <= 32U));
  151. for (i = (OPJ_INT32)n - 1; i >= 0; i--) {
  152. opj_bio_putbit(bio, (v >> i) & 1);
  153. }
  154. }
  155. OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n)
  156. {
  157. OPJ_INT32 i;
  158. OPJ_UINT32 v;
  159. assert((n > 0U) /* && (n <= 32U)*/);
  160. #ifdef OPJ_UBSAN_BUILD
  161. /* This assert fails for some corrupted images which are gracefully rejected */
  162. /* Add this assert only for ubsan build. */
  163. /* This is the condition for overflow not to occur below which is needed because of OPJ_NOSANITIZE */
  164. assert(n <= 32U);
  165. #endif
  166. v = 0U;
  167. for (i = (OPJ_INT32)n - 1; i >= 0; i--) {
  168. v |= opj_bio_getbit(bio) <<
  169. i; /* can't overflow, opj_bio_getbit returns 0 or 1 */
  170. }
  171. return v;
  172. }
  173. OPJ_BOOL opj_bio_flush(opj_bio_t *bio)
  174. {
  175. if (! opj_bio_byteout(bio)) {
  176. return OPJ_FALSE;
  177. }
  178. if (bio->ct == 7) {
  179. if (! opj_bio_byteout(bio)) {
  180. return OPJ_FALSE;
  181. }
  182. }
  183. return OPJ_TRUE;
  184. }
  185. OPJ_BOOL opj_bio_inalign(opj_bio_t *bio)
  186. {
  187. if ((bio->buf & 0xff) == 0xff) {
  188. if (! opj_bio_bytein(bio)) {
  189. return OPJ_FALSE;
  190. }
  191. }
  192. bio->ct = 0;
  193. return OPJ_TRUE;
  194. }