j2k.c: remove hardcoded constants related to m_state, and useless FIXME
[openjpeg.git] / src / lib / openmj2 / bio.c
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
38 #include "opj_includes.h"
39
40 /** @defgroup BIO BIO - Individual bit input-output stream */
41 /*@{*/
42
43 /** @name Local static functions */
44 /*@{*/
45
46 /**
47 Write a bit
48 @param bio BIO handle
49 @param b Bit to write (0 or 1)
50 */
51 static void bio_putbit(opj_bio_t *bio, int b);
52 /**
53 Read a bit
54 @param bio BIO handle
55 @return Returns the read bit
56 */
57 static int bio_getbit(opj_bio_t *bio);
58 /**
59 Write a byte
60 @param bio BIO handle
61 @return Returns 0 if successful, returns 1 otherwise
62 */
63 static int bio_byteout(opj_bio_t *bio);
64 /**
65 Read a byte
66 @param bio BIO handle
67 @return Returns 0 if successful, returns 1 otherwise
68 */
69 static int bio_bytein(opj_bio_t *bio);
70
71 /*@}*/
72
73 /*@}*/
74
75 /*
76 ==========================================================
77    local functions
78 ==========================================================
79 */
80
81 static int bio_byteout(opj_bio_t *bio)
82 {
83     bio->buf = (bio->buf << 8) & 0xffff;
84     bio->ct = bio->buf == 0xff00 ? 7 : 8;
85     if (bio->bp >= bio->end) {
86         return 1;
87     }
88     *bio->bp++ = bio->buf >> 8;
89     return 0;
90 }
91
92 static int bio_bytein(opj_bio_t *bio)
93 {
94     bio->buf = (bio->buf << 8) & 0xffff;
95     bio->ct = bio->buf == 0xff00 ? 7 : 8;
96     if (bio->bp >= bio->end) {
97         return 1;
98     }
99     bio->buf |= *bio->bp++;
100     return 0;
101 }
102
103 static void bio_putbit(opj_bio_t *bio, int b)
104 {
105     if (bio->ct == 0) {
106         bio_byteout(bio);
107     }
108     bio->ct--;
109     bio->buf |= b << bio->ct;
110 }
111
112 static int bio_getbit(opj_bio_t *bio)
113 {
114     if (bio->ct == 0) {
115         bio_bytein(bio);
116     }
117     bio->ct--;
118     return (bio->buf >> bio->ct) & 1;
119 }
120
121 /*
122 ==========================================================
123    Bit Input/Output interface
124 ==========================================================
125 */
126
127 opj_bio_t* bio_create(void)
128 {
129     opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
130     return bio;
131 }
132
133 void bio_destroy(opj_bio_t *bio)
134 {
135     if (bio) {
136         opj_free(bio);
137     }
138 }
139
140 int bio_numbytes(opj_bio_t *bio)
141 {
142     return (bio->bp - bio->start);
143 }
144
145 void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len)
146 {
147     bio->start = bp;
148     bio->end = bp + len;
149     bio->bp = bp;
150     bio->buf = 0;
151     bio->ct = 8;
152 }
153
154 void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len)
155 {
156     bio->start = bp;
157     bio->end = bp + len;
158     bio->bp = bp;
159     bio->buf = 0;
160     bio->ct = 0;
161 }
162
163 void bio_write(opj_bio_t *bio, int v, int n)
164 {
165     int i;
166     for (i = n - 1; i >= 0; i--) {
167         bio_putbit(bio, (v >> i) & 1);
168     }
169 }
170
171 int bio_read(opj_bio_t *bio, int n)
172 {
173     int i, v;
174     v = 0;
175     for (i = n - 1; i >= 0; i--) {
176         v += bio_getbit(bio) << i;
177     }
178     return v;
179 }
180
181 int bio_flush(opj_bio_t *bio)
182 {
183     bio->ct = 0;
184     if (bio_byteout(bio)) {
185         return 1;
186     }
187     if (bio->ct == 7) {
188         bio->ct = 0;
189         if (bio_byteout(bio)) {
190             return 1;
191         }
192     }
193     return 0;
194 }
195
196 int bio_inalign(opj_bio_t *bio)
197 {
198     bio->ct = 0;
199     if ((bio->buf & 0xff) == 0xff) {
200         if (bio_bytein(bio)) {
201             return 1;
202         }
203         bio->ct = 0;
204     }
205     return 0;
206 }