minor change for readability
[openjpeg.git] / jpwl / decoder / libopenjpeg / bio.c
1 /*
2  * Copyright (c) 2001-2002, David Janssens
3  * Copyright (c) 2003, Yannick Verschueren
4  * Copyright (c) 2003,  Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "bio.h"
31 #include <stdio.h>
32 #include <setjmp.h>
33
34 static unsigned char *bio_start;        /* pointer to the start of the buffer */
35 static unsigned char *bio_end;  /* pointer to the end of the buffer */
36 static unsigned char *bio_bp;   /* pointer to the present position in the buffer */
37 static unsigned int bio_buf;    /* temporary place where each byte is read or written */
38 static int bio_ct;              /* coder : number of bits free to write // decoder : number of bits read */
39
40 extern jmp_buf j2k_error;
41
42 /*
43  * Number of bytes written.
44  */
45 int bio_numbytes()
46 {
47   return bio_bp - bio_start;
48 }
49
50 /*
51  * Init encoder.
52  *
53  * bp  : Output buffer
54  * len : Output buffer length 
55  */
56 void bio_init_enc(unsigned char *bp, int len)
57 {
58   bio_start = bp;
59   bio_end = bp + len;
60   bio_bp = bp;
61   bio_buf = 0;
62   bio_ct = 8;
63 }
64
65 /*
66  * Init decoder.
67  * 
68  * bp  : Input buffer
69  * len : Input buffer length 
70  */
71 void bio_init_dec(unsigned char *bp, int len)
72 {
73   bio_start = bp;
74   bio_end = bp + len;
75   bio_bp = bp;
76   bio_buf = 0;
77   bio_ct = 0;
78 }
79
80 /*
81  * Write byte. --> function modified to eliminate longjmp !!! 
82  *
83  */
84 int bio_byteout()
85 {
86   bio_buf = (bio_buf << 8) & 0xffff;
87   bio_ct = bio_buf == 0xff00 ? 7 : 8;
88   if (bio_bp >= bio_end)
89     return 1;
90   *bio_bp++ = bio_buf >> 8;
91   return 0;
92 }
93
94 /*
95  * Read byte. --> function modified to eliminate longjmp !!
96  *
97  */
98 int bio_bytein()
99 {
100   bio_buf = (bio_buf << 8) & 0xffff;
101   bio_ct = bio_buf == 0xff00 ? 7 : 8;
102   if (bio_bp >= bio_end)
103     return 1;
104   bio_buf |= *bio_bp++;
105   return 0;
106 }
107
108 /*
109  * Write bit.
110  *
111  * b : Bit to write (0 or 1)
112  */
113 void bio_putbit(int b)
114 {
115   if (bio_ct == 0) {
116     bio_byteout();
117   }
118   bio_ct--;
119   bio_buf |= b << bio_ct;
120 }
121
122 /*
123  * Read bit.
124  *
125  */
126 int bio_getbit()
127 {
128   if (bio_ct == 0) {
129     bio_bytein();
130   }
131   bio_ct--;
132   return (bio_buf >> bio_ct) & 1;
133 }
134
135 /*
136  * Write bits.
137  *
138  * v : Value of bits
139  * n : Number of bits to write 
140  */
141 void bio_write(int v, int n)
142 {
143   int i;
144   for (i = n - 1; i >= 0; i--) {
145     bio_putbit((v >> i) & 1);
146   }
147 }
148
149 /*
150  * Read bits.
151  * 
152  * n : Number of bits to read
153  */
154 int bio_read(int n)
155 {
156   int i, v;
157   v = 0;
158   for (i = n - 1; i >= 0; i--) {
159     v += bio_getbit() << i;
160   }
161   return v;
162 }
163
164 /*
165  * Flush bits. Modified to eliminate longjmp !!
166  *
167  */
168 int bio_flush()
169 {
170   bio_ct = 0;
171   if (bio_byteout())
172     return 1;
173   if (bio_ct == 7) {
174     bio_ct = 0;
175
176     if (bio_byteout())
177       return 1;
178   }
179   return 0;
180 }
181
182 /*
183  * Passes the ending bits (coming from flushing)
184  */
185 int bio_inalign()
186 {
187   bio_ct = 0;
188   if ((bio_buf & 0xff) == 0xff) {
189     if (bio_bytein())
190       return 1;
191     bio_ct = 0;
192   }
193   return 0;
194 }