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, *bio_end, *bio_bp;
35 static unsigned int bio_buf;
38 extern jmp_buf j2k_error;
41 /* Number of bytes written. */
45 return bio_bp - bio_start;
51 /* <param name="bp">Output buffer</param> */
52 /* <param name="len">Output buffer length</param> */
53 void bio_init_enc(unsigned char *bp, int len)
65 /* <param name="bp">Input buffer</param> */
66 /* <param name="len">Input buffer length</param> */
67 void bio_init_dec(unsigned char *bp, int len)
77 /* Write byte. --> function modified to eliminate longjmp !!! */
81 bio_buf = (bio_buf << 8) & 0xffff;
82 bio_ct = bio_buf == 0xff00 ? 7 : 8;
83 if (bio_bp >= bio_end)
85 *bio_bp++ = bio_buf >> 8;
90 /* Read byte. --> function modified to eliminate longjmp !! */
94 bio_buf = (bio_buf << 8) & 0xffff;
95 bio_ct = bio_buf == 0xff00 ? 7 : 8;
96 if (bio_bp >= bio_end)
105 /* <param name="b">Bit to write (0 or 1)</param> */
106 void bio_putbit(int b)
112 bio_buf |= b << bio_ct;
124 return (bio_buf >> bio_ct) & 1;
130 /* <param name="v">Value of bits</param> */
131 /* <param name="n">Number of bits to write</param> */
132 void bio_write(int v, int n)
135 for (i = n - 1; i >= 0; i--) {
136 bio_putbit((v >> i) & 1);
143 /* <param name="n">Number of bits to read</param> */
148 for (i = n - 1; i >= 0; i--) {
149 v += bio_getbit() << i;
155 /* Flush bits. MOdified to eliminate longjmp !! */
176 if ((bio_buf & 0xff) == 0xff) {