bio.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
  3. * Copyright (c) 2002-2007, Professor Benoit Macq
  4. * Copyright (c) 2001-2003, David Janssens
  5. * Copyright (c) 2002-2003, Yannick Verschueren
  6. * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
  7. * Copyright (c) 2005, Herve Drolon, FreeImage Team
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "opj_includes.h"
  32. /** @defgroup BIO BIO - Individual bit input-output stream */
  33. /*@{*/
  34. /** @name Local static functions */
  35. /*@{*/
  36. /**
  37. Write a bit
  38. @param bio BIO handle
  39. @param b Bit to write (0 or 1)
  40. */
  41. static void bio_putbit(opj_bio_t *bio, int b);
  42. /**
  43. Read a bit
  44. @param bio BIO handle
  45. @return Returns the read bit
  46. */
  47. static int bio_getbit(opj_bio_t *bio);
  48. /**
  49. Write a byte
  50. @param bio BIO handle
  51. @return Returns 0 if successful, returns 1 otherwise
  52. */
  53. static int bio_byteout(opj_bio_t *bio);
  54. /**
  55. Read a byte
  56. @param bio BIO handle
  57. @return Returns 0 if successful, returns 1 otherwise
  58. */
  59. static int bio_bytein(opj_bio_t *bio);
  60. /*@}*/
  61. /*@}*/
  62. /*
  63. ==========================================================
  64. local functions
  65. ==========================================================
  66. */
  67. static int bio_byteout(opj_bio_t *bio) {
  68. bio->buf = (bio->buf << 8) & 0xffff;
  69. bio->ct = bio->buf == 0xff00 ? 7 : 8;
  70. if (bio->bp >= bio->end) {
  71. return 1;
  72. }
  73. *bio->bp++ = bio->buf >> 8;
  74. return 0;
  75. }
  76. static int bio_bytein(opj_bio_t *bio) {
  77. bio->buf = (bio->buf << 8) & 0xffff;
  78. bio->ct = bio->buf == 0xff00 ? 7 : 8;
  79. if (bio->bp >= bio->end) {
  80. return 1;
  81. }
  82. bio->buf |= *bio->bp++;
  83. return 0;
  84. }
  85. static void bio_putbit(opj_bio_t *bio, int b) {
  86. if (bio->ct == 0) {
  87. bio_byteout(bio);
  88. }
  89. bio->ct--;
  90. bio->buf |= b << bio->ct;
  91. }
  92. static int bio_getbit(opj_bio_t *bio) {
  93. if (bio->ct == 0) {
  94. bio_bytein(bio);
  95. }
  96. bio->ct--;
  97. return (bio->buf >> bio->ct) & 1;
  98. }
  99. /*
  100. ==========================================================
  101. Bit Input/Output interface
  102. ==========================================================
  103. */
  104. opj_bio_t* bio_create(void) {
  105. opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
  106. return bio;
  107. }
  108. void bio_destroy(opj_bio_t *bio) {
  109. if(bio) {
  110. opj_free(bio);
  111. }
  112. }
  113. int bio_numbytes(opj_bio_t *bio) {
  114. return (int)(bio->bp - bio->start);
  115. }
  116. void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len) {
  117. bio->start = bp;
  118. bio->end = bp + len;
  119. bio->bp = bp;
  120. bio->buf = 0;
  121. bio->ct = 8;
  122. }
  123. void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len) {
  124. bio->start = bp;
  125. bio->end = bp + len;
  126. bio->bp = bp;
  127. bio->buf = 0;
  128. bio->ct = 0;
  129. }
  130. void bio_write(opj_bio_t *bio, int v, int n) {
  131. int i;
  132. for (i = n - 1; i >= 0; i--) {
  133. bio_putbit(bio, (v >> i) & 1);
  134. }
  135. }
  136. int bio_read(opj_bio_t *bio, int n) {
  137. int i, v;
  138. v = 0;
  139. for (i = n - 1; i >= 0; i--) {
  140. v += bio_getbit(bio) << i;
  141. }
  142. return v;
  143. }
  144. int bio_flush(opj_bio_t *bio) {
  145. bio->ct = 0;
  146. if (bio_byteout(bio)) {
  147. return 1;
  148. }
  149. if (bio->ct == 7) {
  150. bio->ct = 0;
  151. if (bio_byteout(bio)) {
  152. return 1;
  153. }
  154. }
  155. return 0;
  156. }
  157. int bio_inalign(opj_bio_t *bio) {
  158. bio->ct = 0;
  159. if ((bio->buf & 0xff) == 0xff) {
  160. if (bio_bytein(bio)) {
  161. return 1;
  162. }
  163. bio->ct = 0;
  164. }
  165. return 0;
  166. }