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 
32 #include "opj_includes.h"
33 
34 /** @defgroup BIO BIO - Individual bit input-output stream */
35 /*@{*/
36 
37 /** @name Local static functions */
38 /*@{*/
39 
40 /**
41 Write a bit
42 @param bio BIO handle
43 @param b Bit to write (0 or 1)
44 */
45 static void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b);
46 /**
47 Read a bit
48 @param bio BIO handle
49 @return Returns the read bit
50 */
51 static OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio);
52 /**
53 Write a byte
54 @param bio BIO handle
55 @return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
56 */
57 static OPJ_BOOL opj_bio_byteout(opj_bio_t *bio);
58 /**
59 Read a byte
60 @param bio BIO handle
61 @return Returns OPJ_TRUE if successful, returns OPJ_FALSE otherwise
62 */
63 static OPJ_BOOL opj_bio_bytein(opj_bio_t *bio);
64 
65 /*@}*/
66 
67 /*@}*/
68 
69 /*
70 ==========================================================
71    local functions
72 ==========================================================
73 */
74 
opj_bio_byteout(opj_bio_t * bio)75 OPJ_BOOL opj_bio_byteout(opj_bio_t *bio) {
76 	bio->buf = (bio->buf << 8) & 0xffff;
77 	bio->ct = bio->buf == 0xff00 ? 7 : 8;
78 	if (bio->bp >= bio->end) {
79 		return OPJ_FALSE;
80 	}
81 	*bio->bp++ = (OPJ_BYTE)(bio->buf >> 8);
82 	return OPJ_TRUE;
83 }
84 
opj_bio_bytein(opj_bio_t * bio)85 OPJ_BOOL opj_bio_bytein(opj_bio_t *bio) {
86 	bio->buf = (bio->buf << 8) & 0xffff;
87 	bio->ct = bio->buf == 0xff00 ? 7 : 8;
88 	if (bio->bp >= bio->end) {
89 		return OPJ_FALSE;
90 	}
91 	bio->buf |= *bio->bp++;
92 	return OPJ_TRUE;
93 }
94 
opj_bio_putbit(opj_bio_t * bio,OPJ_UINT32 b)95 void opj_bio_putbit(opj_bio_t *bio, OPJ_UINT32 b) {
96 	if (bio->ct == 0) {
97 		opj_bio_byteout(bio); /* MSD: why not check the return value of this function ? */
98 	}
99 	bio->ct--;
100 	bio->buf |= b << bio->ct;
101 }
102 
opj_bio_getbit(opj_bio_t * bio)103 OPJ_UINT32 opj_bio_getbit(opj_bio_t *bio) {
104 	if (bio->ct == 0) {
105 		opj_bio_bytein(bio); /* MSD: why not check the return value of this function ? */
106 	}
107 	bio->ct--;
108 	return (bio->buf >> bio->ct) & 1;
109 }
110 
111 /*
112 ==========================================================
113    Bit Input/Output interface
114 ==========================================================
115 */
116 
opj_bio_create(void)117 opj_bio_t* opj_bio_create(void) {
118 	opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
119 	return bio;
120 }
121 
opj_bio_destroy(opj_bio_t * bio)122 void opj_bio_destroy(opj_bio_t *bio) {
123 	if(bio) {
124 		opj_free(bio);
125 	}
126 }
127 
opj_bio_numbytes(opj_bio_t * bio)128 ptrdiff_t opj_bio_numbytes(opj_bio_t *bio) {
129 	return (bio->bp - bio->start);
130 }
131 
opj_bio_init_enc(opj_bio_t * bio,OPJ_BYTE * bp,OPJ_UINT32 len)132 void opj_bio_init_enc(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) {
133 	bio->start = bp;
134 	bio->end = bp + len;
135 	bio->bp = bp;
136 	bio->buf = 0;
137 	bio->ct = 8;
138 }
139 
opj_bio_init_dec(opj_bio_t * bio,OPJ_BYTE * bp,OPJ_UINT32 len)140 void opj_bio_init_dec(opj_bio_t *bio, OPJ_BYTE *bp, OPJ_UINT32 len) {
141 	bio->start = bp;
142 	bio->end = bp + len;
143 	bio->bp = bp;
144 	bio->buf = 0;
145 	bio->ct = 0;
146 }
147 
opj_bio_write(opj_bio_t * bio,OPJ_UINT32 v,OPJ_UINT32 n)148 void opj_bio_write(opj_bio_t *bio, OPJ_UINT32 v, OPJ_UINT32 n) {
149 	OPJ_UINT32 i;
150 	for (i = n - 1; i < n; i--) {
151 		opj_bio_putbit(bio, (v >> i) & 1);
152 	}
153 }
154 
opj_bio_read(opj_bio_t * bio,OPJ_UINT32 n)155 OPJ_UINT32 opj_bio_read(opj_bio_t *bio, OPJ_UINT32 n) {
156 	OPJ_UINT32 i;
157     OPJ_UINT32 v;
158 	v = 0;
159 	for (i = n - 1; i < n; i--) {
160 		v += opj_bio_getbit(bio) << i;
161 	}
162 	return v;
163 }
164 
opj_bio_flush(opj_bio_t * bio)165 OPJ_BOOL opj_bio_flush(opj_bio_t *bio) {
166 	bio->ct = 0;
167 	if (! opj_bio_byteout(bio)) {
168 		return OPJ_FALSE;
169 	}
170 	if (bio->ct == 7) {
171 		bio->ct = 0;
172 		if (! opj_bio_byteout(bio)) {
173 			return OPJ_FALSE;
174 		}
175 	}
176 	return OPJ_TRUE;
177 }
178 
opj_bio_inalign(opj_bio_t * bio)179 OPJ_BOOL opj_bio_inalign(opj_bio_t *bio) {
180 	bio->ct = 0;
181 	if ((bio->buf & 0xff) == 0xff) {
182 		if (! opj_bio_bytein(bio)) {
183 			return OPJ_FALSE;
184 		}
185 		bio->ct = 0;
186 	}
187 	return OPJ_TRUE;
188 }
189