[1.5] fix issue #104
[openjpeg.git] / libopenjpeg / cio.c
1 /*
2  * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3  * Copyright (c) 2002-2007, Professor Benoit Macq
4  * Copyright (c) 2001-2003, David Janssens
5  * Copyright (c) 2002-2003, Yannick Verschueren
6  * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "opj_includes.h"
33
34 /* ----------------------------------------------------------------------- */
35
36 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
37         opj_cp_t *cp = NULL;
38         opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
39         if(!cio) return NULL;
40         cio->cinfo = cinfo;
41         if(buffer && length) {
42                 /* wrap a user buffer containing the encoded image */
43                 cio->openmode = OPJ_STREAM_READ;
44                 cio->buffer = buffer;
45                 cio->length = length;
46         }
47         else if(!buffer && !length && cinfo) {
48                 /* allocate a buffer for the encoded image */
49                 cio->openmode = OPJ_STREAM_WRITE;
50                 switch(cinfo->codec_format) {
51                         case CODEC_J2K:
52                                 cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
53                                 break;
54                         case CODEC_JP2:
55                                 cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
56                                 break;
57                         default:
58                                 opj_free(cio);
59                                 return NULL;
60                 }
61                 cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
62                 cio->buffer = (unsigned char *)opj_malloc(cio->length);
63                 if(!cio->buffer) {
64                         opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
65                         opj_free(cio);
66                         return NULL;
67                 }
68         }
69         else {
70                 opj_free(cio);
71                 return NULL;
72         }
73
74         /* Initialize byte IO */
75         cio->start = cio->buffer;
76         cio->end = cio->buffer + cio->length;
77         cio->bp = cio->buffer;
78
79         return cio;
80 }
81
82 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
83         if(cio) {
84                 if(cio->openmode == OPJ_STREAM_WRITE) {
85                         /* destroy the allocated buffer */
86                         opj_free(cio->buffer);
87                 }
88                 /* destroy the cio */
89                 opj_free(cio);
90         }
91 }
92
93
94 /* ----------------------------------------------------------------------- */
95
96 /*
97  * Get position in byte stream.
98  */
99 int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
100         return cio->bp - cio->start;
101 }
102
103 /*
104  * Set position in byte stream.
105  *
106  * pos : position, in number of bytes, from the beginning of the stream
107  */
108 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
109         cio->bp = cio->start + pos;
110 }
111
112 /*
113  * Number of bytes left before the end of the stream.
114  */
115 int cio_numbytesleft(opj_cio_t *cio) {
116         return cio->end - cio->bp;
117 }
118
119 /*
120  * Get pointer to the current position in the stream.
121  */
122 unsigned char *cio_getbp(opj_cio_t *cio) {
123         return cio->bp;
124 }
125
126 /*
127  * Write a byte.
128  */
129 opj_bool cio_byteout(opj_cio_t *cio, unsigned char v) {
130         if (cio->bp >= cio->end) {
131                 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
132                 return OPJ_FALSE;
133         }
134         *cio->bp++ = v;
135         return OPJ_TRUE;
136 }
137
138 /*
139  * Read a byte.
140  */
141 unsigned char cio_bytein(opj_cio_t *cio) {
142         if (cio->bp >= cio->end) {
143                 opj_event_msg(cio->cinfo, EVT_ERROR, "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
144                 return 0;
145         }
146         return *cio->bp++;
147 }
148
149 /*
150  * Write some bytes.
151  *
152  * v : value to write
153  * n : number of bytes to write
154  */
155 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n) {
156         int i;
157         for (i = n - 1; i >= 0; i--) {
158                 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
159                         return 0;
160         }
161         return n;
162 }
163
164 /*
165  * Read some bytes.
166  *
167  * n : number of bytes to read
168  *
169  * return : value of the n bytes read
170  */
171 unsigned int cio_read(opj_cio_t *cio, int n) {
172         int i;
173         unsigned int v;
174         v = 0;
175         for (i = n - 1; i >= 0; i--) {
176                 v += cio_bytein(cio) << (i << 3);
177         }
178         return v;
179 }
180
181 /* 
182  * Skip some bytes.
183  *
184  * n : number of bytes to skip
185  */
186 void cio_skip(opj_cio_t *cio, int n) {
187         cio->bp += n;
188 }
189
190
191