2 * Copyright (c) 2001-2002, David Janssens
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
31 static unsigned char *cio_start; /* pointer to the start of the stream */
32 static unsigned char *cio_end; /* pointer to the end of the stream */
33 static unsigned char *cio_bp; /* pointer to the present position */
35 extern jmp_buf j2k_error;
38 * Number of bytes written.
42 return cio_bp - cio_start;
46 * Get position in byte stream.
50 return cio_bp - cio_start;
54 * Set position in byte stream.
56 * pos : position, in number of bytes, from the beginning of the stream
58 void cio_seek(int pos)
60 cio_bp = cio_start + pos;
64 * Number of bytes left before the end of the stream.
66 int cio_numbytesleft()
68 return cio_end - cio_bp;
72 * Get pointer to the current position in the stream.
74 unsigned char *cio_getbp()
82 * bp : destination/source stream
83 * len : length of the stream
85 void cio_init(unsigned char *bp, int len)
95 void cio_byteout(unsigned char v)
97 if (cio_bp >= cio_end)
98 longjmp(j2k_error, 1);
106 unsigned char cio_bytein()
108 if (cio_bp >= cio_end)
109 longjmp(j2k_error, 1);
117 * n : number of bytes to write
119 void cio_write(unsigned int v, int n)
122 for (i = n - 1; i >= 0; i--) {
123 cio_byteout((unsigned char) ((v >> (i << 3)) & 0xff));
130 * n : number of bytes to read
132 * return : value of the n bytes read
134 unsigned int cio_read(int n)
139 for (i = n - 1; i >= 0; i--) {
140 v += cio_bytein() << (i << 3);
148 * n : number of bytes to skip
156 * Read n bytes, copy to buffer
158 * n : number of bytes to transfer
160 void cio_read_to_buf(unsigned char* src_buf, int n)/* Glenn adds */
162 if (cio_bp + n > cio_end)
163 longjmp(j2k_error, 1);
164 memcpy(cio_bp, src_buf, n);
169 * Write n bytes, copy from buffer
171 * n : number of bytes to transfer
173 void cio_write_from_buf(unsigned char* dest_buf, int n)/* Glenn adds */
175 if (cio_bp + n > cio_end)
176 longjmp(j2k_error, 1);
177 memcpy(dest_buf, cio_bp, n);