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
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
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 */
40 extern jmp_buf j2k_error;
43 * Number of bytes written.
47 return bio_bp - bio_start;
54 * len : Output buffer length
56 void bio_init_enc(unsigned char *bp, int len)
69 * len : Input buffer length
71 void bio_init_dec(unsigned char *bp, int len)
81 * Write byte. --> function modified to eliminate longjmp !!!
86 bio_buf = (bio_buf << 8) & 0xffff;
87 bio_ct = bio_buf == 0xff00 ? 7 : 8;
88 if (bio_bp >= bio_end)
90 *bio_bp++ = bio_buf >> 8;
95 * Read byte. --> function modified to eliminate longjmp !!
100 bio_buf = (bio_buf << 8) & 0xffff;
101 bio_ct = bio_buf == 0xff00 ? 7 : 8;
102 if (bio_bp >= bio_end)
104 bio_buf |= *bio_bp++;
111 * b : Bit to write (0 or 1)
113 void bio_putbit(int b)
119 bio_buf |= b << bio_ct;
132 return (bio_buf >> bio_ct) & 1;
139 * n : Number of bits to write
141 void bio_write(int v, int n)
144 for (i = n - 1; i >= 0; i--) {
145 bio_putbit((v >> i) & 1);
152 * n : Number of bits to read
158 for (i = n - 1; i >= 0; i--) {
159 v += bio_getbit() << i;
165 * Flush bits. Modified to eliminate longjmp !!
183 * Passes the ending bits (coming from flushing)
188 if ((bio_buf & 0xff) == 0xff) {