Fix a set of warnings reported by gcc in cio.c. This patch impact the API directly...
[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 = (int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
62                 assert(cio->length >= 0);
63                 cio->buffer = (unsigned char *)opj_malloc((OPJ_SIZE_T)cio->length);
64                 if(!cio->buffer) {
65                         opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
66                         opj_free(cio);
67                         return NULL;
68                 }
69         }
70         else {
71                 opj_free(cio);
72                 return NULL;
73         }
74
75         /* Initialize byte IO */
76         cio->start = cio->buffer;
77         cio->end = cio->buffer + cio->length;
78         cio->bp = cio->buffer;
79
80         return cio;
81 }
82
83 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
84         if(cio) {
85                 if(cio->openmode == OPJ_STREAM_WRITE) {
86                         /* destroy the allocated buffer */
87                         opj_free(cio->buffer);
88                 }
89                 /* destroy the cio */
90                 opj_free(cio);
91         }
92 }
93
94
95 /* ----------------------------------------------------------------------- */
96
97 /*
98  * Get position in byte stream.
99  */
100 OPJ_OFF_T OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
101         return cio->bp - cio->start;
102 }
103
104 /*
105  * Set position in byte stream.
106  *
107  * pos : position, in number of bytes, from the beginning of the stream
108  */
109 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
110         cio->bp = cio->start + pos;
111 }
112
113 /*
114  * Number of bytes left before the end of the stream.
115  */
116 OPJ_SIZE_T cio_numbytesleft(opj_cio_t *cio) {
117         const ptrdiff_t diff = cio->end - cio->bp;
118   assert( diff >= 0 );
119   return (OPJ_SIZE_T)diff;
120 }
121
122 /*
123  * Get pointer to the current position in the stream.
124  */
125 unsigned char *cio_getbp(opj_cio_t *cio) {
126         return cio->bp;
127 }
128
129 /*
130  * Write a byte.
131  */
132 opj_bool cio_byteout(opj_cio_t *cio, unsigned char v) {
133         if (cio->bp >= cio->end) {
134                 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
135                 return OPJ_FALSE;
136         }
137         *cio->bp++ = v;
138         return OPJ_TRUE;
139 }
140
141 /*
142  * Read a byte.
143  */
144 unsigned char cio_bytein(opj_cio_t *cio) {
145         if (cio->bp >= cio->end) {
146                 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);
147                 return 0;
148         }
149         return *cio->bp++;
150 }
151
152 /*
153  * Write some bytes.
154  *
155  * v : value to write
156  * n : number of bytes to write
157  */
158 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n) {
159         int i;
160         for (i = n - 1; i >= 0; i--) {
161                 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
162                         return 0;
163         }
164   assert( n >= 0 );
165         return (unsigned int)n;
166 }
167
168 /*
169  * Read some bytes.
170  *
171  * n : number of bytes to read
172  *
173  * return : value of the n bytes read
174  */
175 unsigned int cio_read(opj_cio_t *cio, int n) {
176         int i;
177         unsigned int v = 0;
178         for (i = n - 1; i >= 0; i--) {
179     const unsigned int c = cio_bytein(cio);
180                 v += c << (i << 3);
181         }
182         return v;
183 }
184
185 /* 
186  * Skip some bytes.
187  *
188  * n : number of bytes to skip
189  */
190 void cio_skip(opj_cio_t *cio, int n) {
191         cio->bp += n;
192 }
193
194
195 /* ----------------------------------------------------------------------- */
196
197
198 /**
199  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
200  * @param p_buffer              pointer the data buffer to write data to.
201  * @param p_value               the value to write
202  * @param p_nb_bytes    the number of bytes to write
203 */
204 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
205 {
206         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes;
207
208         assert(p_nb_bytes > 0 && p_nb_bytes <=  sizeof(OPJ_UINT32));
209
210         memcpy(p_buffer,l_data_ptr,p_nb_bytes);
211 }
212
213 /**
214  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
215  * @param p_buffer              pointer the data buffer to write data to.
216  * @param p_value               the value to write
217  * @param p_nb_bytes    the number of bytes to write
218  * @return                              the number of bytes written or -1 if an error occured
219 */
220 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
221 {
222         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes - 1;
223         OPJ_UINT32 i;
224
225         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
226
227         for     (i=0;i<p_nb_bytes;++i) {
228                 *(p_buffer++) = *(l_data_ptr--);
229         }
230 }
231
232 /**
233  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
234  * @param p_buffer              pointer the data buffer to read data from.
235  * @param p_value               pointer to the value that will store the data.
236  * @param p_nb_bytes    the nb bytes to read.
237  * @return                              the number of bytes read or -1 if an error occured.
238  */
239 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
240 {
241         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
242
243         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
244
245         *p_value = 0;
246         memcpy(l_data_ptr+4-p_nb_bytes,p_buffer,p_nb_bytes);
247 }
248
249 /**
250  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
251  * @param p_buffer              pointer the data buffer to read data from.
252  * @param p_value               pointer to the value that will store the data.
253  * @param p_nb_bytes    the nb bytes to read.
254  * @return                              the number of bytes read or -1 if an error occured.
255  */
256 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
257 {
258         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + p_nb_bytes-1;
259         OPJ_UINT32 i;
260
261         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
262
263         *p_value = 0;
264         for (i=0;i<p_nb_bytes;++i) {
265                 *(l_data_ptr--) = *(p_buffer++);
266         }
267 }
268
269 /**
270  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
271  * @param p_buffer              pointer the data buffer to write data to.
272  * @param p_value               the value to write
273  * @return                              the number of bytes written or -1 if an error occured
274  */
275 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
276 {
277         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
278         memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT64));
279 }
280
281 /**
282  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
283  * @param p_buffer              pointer the data buffer to write data to.
284  * @param p_value               the value to write
285  */
286 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
287 {
288         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT64) - 1;
289         OPJ_UINT32 i;
290         for     (i=0;i<sizeof(OPJ_FLOAT64);++i) {
291                 *(p_buffer++) = *(l_data_ptr--);
292         }
293 }
294
295 /**
296  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
297  * @param p_buffer              pointer the data buffer to read data from.
298  * @param p_value               pointer to the value that will store the data.
299  */
300 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
301 {
302         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
303         memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT64));
304 }
305
306
307 /**
308  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
309  * @param p_buffer              pointer the data buffer to read data from.
310  * @param p_value               pointer to the value that will store the data.
311  */
312 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
313 {
314         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT64)-1;
315         OPJ_UINT32 i;
316         for (i=0;i<sizeof(OPJ_FLOAT64);++i) {
317                 *(l_data_ptr--) = *(p_buffer++);
318         }
319 }
320
321 /**
322  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
323  * @param p_buffer              pointer the data buffer to write data to.
324  * @param p_value               the value to write
325  * @return                              the number of bytes written or -1 if an error occured
326  */
327 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
328 {
329         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
330         memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT32));
331 }
332
333 /**
334  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
335  * @param p_buffer              pointer the data buffer to write data to.
336  * @param p_value               the value to write
337  */
338 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
339 {
340         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT32) - 1;
341         OPJ_UINT32 i;
342         for     (i=0;i<sizeof(OPJ_FLOAT32);++i) {
343                 *(p_buffer++) = *(l_data_ptr--);
344         }
345 }
346
347 /**
348  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
349  * @param p_buffer              pointer the data buffer to read data from.
350  * @param p_value               pointer to the value that will store the data.
351  */
352 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
353 {
354         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
355         memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT32));
356 }
357
358
359 /**
360  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
361  * @param p_buffer              pointer the data buffer to read data from.
362  * @param p_value               pointer to the value that will store the data.
363  */
364 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
365 {
366         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT32)-1;
367         OPJ_UINT32 i;
368         for     (i=0;i<sizeof(OPJ_FLOAT32);++i) {
369                 *(l_data_ptr--) = *(p_buffer++);
370         }
371 }
372
373
374 /**
375  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
376  * @return a stream object.
377 */
378 opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,opj_bool l_is_input)
379 {
380         opj_stream_private_t * l_stream = 00;
381         l_stream = (opj_stream_private_t*) opj_malloc(sizeof(opj_stream_private_t));
382         if (! l_stream) {
383                 return 00;
384         }
385
386         memset(l_stream,0,sizeof(opj_stream_private_t));
387         l_stream->m_buffer_size = p_buffer_size;
388         l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_buffer_size);
389         if (! l_stream->m_stored_data) {
390                 opj_free(l_stream);
391                 return 00;
392         }
393
394         l_stream->m_current_data = l_stream->m_stored_data;
395
396         if (l_is_input) {
397                 l_stream->m_status |= opj_stream_e_input;
398                 l_stream->m_opj_skip = opj_stream_read_skip;
399                 l_stream->m_opj_seek = opj_stream_read_seek;
400         }
401         else {
402                 l_stream->m_status |= opj_stream_e_output;
403                 l_stream->m_opj_skip = opj_stream_write_skip;
404                 l_stream->m_opj_seek = opj_stream_write_seek;
405         }
406
407         l_stream->m_read_fn = opj_stream_default_read;
408         l_stream->m_write_fn = opj_stream_default_write;
409         l_stream->m_skip_fn = opj_stream_default_skip;
410         l_stream->m_seek_fn = opj_stream_default_seek;
411
412         return (opj_stream_t *) l_stream;
413 }
414
415 /**
416  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
417  * @return a stream object.
418 */
419 opj_stream_t* OPJ_CALLCONV opj_stream_default_create(opj_bool l_is_input)
420 {
421         return opj_stream_create(J2K_STREAM_CHUNK_SIZE,l_is_input);
422 }
423
424 /**
425  * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must
426  * close its own implementation of the stream.
427  */
428 OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream)
429 {
430         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
431         if (l_stream) {
432                 opj_free(l_stream->m_stored_data);
433                 l_stream->m_stored_data = 00;
434                 opj_free(l_stream);
435         }
436 }
437
438 /**
439  * Sets the given function to be used as a read function.
440  * @param               p_stream        the stream to modify
441  * @param               p_function      the function to use a read function.
442 */
443 OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function)
444 {
445         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
446
447         if ((!l_stream) || (! (l_stream->m_status & opj_stream_e_input))) {
448                 return;
449         }
450
451         l_stream->m_read_fn = p_function;
452 }
453
454 OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function)
455 {
456         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
457         if
458                 (!l_stream)
459         {
460                 return;
461         }
462         l_stream->m_seek_fn = p_function;
463 }
464
465 /**
466  * Sets the given function to be used as a write function.
467  * @param               p_stream        the stream to modify
468  * @param               p_function      the function to use a write function.
469 */
470 OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function)
471 {
472         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
473         if
474                 ((!l_stream )|| (! (l_stream->m_status & opj_stream_e_output)))
475         {
476                 return;
477         }
478         l_stream->m_write_fn = p_function;
479 }
480
481 /**
482  * Sets the given function to be used as a skip function.
483  * @param               p_stream        the stream to modify
484  * @param               p_function      the function to use a skip function.
485 */
486 OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function)
487 {
488         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
489         if
490                 (! l_stream)
491         {
492                 return;
493         }
494         l_stream->m_skip_fn = p_function;
495 }
496
497 /**
498  * Sets the given data to be used as a user data for the stream.
499  * @param               p_stream        the stream to modify
500  * @param               p_data          the data to set.
501 */
502 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data)
503 {
504         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
505         l_stream->m_user_data = p_data;
506 }
507
508 /**
509  * Sets the given data to be used as a user data for the stream.
510  * @param               p_stream        the stream to modify
511  * @param               p_data          the data to set.
512 */
513 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length)
514 {
515         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
516         l_stream->m_user_data_length = data_length;
517 }
518
519 /**
520  * Reads some bytes from the stream.
521  * @param               p_stream        the stream to read data from.
522  * @param               p_buffer        pointer to the data buffer that will receive the data.
523  * @param               p_size          number of bytes to read.
524  * @param               p_event_mgr     the user event manager to be notified of special events.
525  * @return              the number of bytes read, or -1 if an error occured or if the stream is at the end.
526  */
527 OPJ_SIZE_T opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
528 {
529         OPJ_SIZE_T l_read_nb_bytes = 0;
530         if (p_stream->m_bytes_in_buffer >= p_size) {
531                 memcpy(p_buffer,p_stream->m_current_data,p_size);
532                 p_stream->m_current_data += p_size;
533                 p_stream->m_bytes_in_buffer -= p_size;
534                 l_read_nb_bytes += p_size;
535                 p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
536                 return l_read_nb_bytes;
537         }
538
539         /* we are now in the case when the remaining data if not sufficient */
540         if (p_stream->m_status & opj_stream_e_end) {
541                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
542                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
543                 p_stream->m_current_data += p_stream->m_bytes_in_buffer;
544                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
545                 p_stream->m_bytes_in_buffer = 0;
546                 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
547         }
548
549         /* the flag is not set, we copy data and then do an actual read on the stream */
550         if (p_stream->m_bytes_in_buffer) {
551                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
552                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
553                 p_stream->m_current_data = p_stream->m_stored_data;
554                 p_buffer += p_stream->m_bytes_in_buffer;
555                 p_size -= p_stream->m_bytes_in_buffer;
556                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
557                 p_stream->m_bytes_in_buffer = 0;
558         }
559         else {
560     /* case where we are already at the end of the buffer
561        so reset the m_current_data to point to the start of the
562        stored buffer to get ready to read from disk*/
563                 p_stream->m_current_data = p_stream->m_stored_data;
564         }
565
566
567         while(1){
568                 /* we should read less than a chunk -> read a chunk */
569                 if (p_size < p_stream->m_buffer_size) {
570                         /* we should do an actual read on the media */
571                         p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_stream->m_stored_data,p_stream->m_buffer_size,p_stream->m_user_data);
572
573                         if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
574                                 /* end of stream */
575                                 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
576
577                                 p_stream->m_bytes_in_buffer = 0;
578                                 p_stream->m_status |= opj_stream_e_end;
579                                 /* end of stream */
580                                 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
581                         }
582                         else if (p_stream->m_bytes_in_buffer < p_size) {
583                                 /* not enough data */
584                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
585                                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
586                                 p_stream->m_current_data = p_stream->m_stored_data;
587                                 p_buffer += p_stream->m_bytes_in_buffer;
588                                 p_size -= p_stream->m_bytes_in_buffer;
589                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
590                                 p_stream->m_bytes_in_buffer = 0;
591                         }
592                         else {
593                                 l_read_nb_bytes += p_size;
594                                 memcpy(p_buffer,p_stream->m_current_data,p_size);
595                                 p_stream->m_current_data += p_size;
596                                 p_stream->m_bytes_in_buffer -= p_size;
597                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
598                                 return l_read_nb_bytes;
599                         }
600                 }
601                 else {
602                         /* direct read on the dest buffer */
603                         p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_buffer,p_size,p_stream->m_user_data);
604
605                         if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
606                                 /*  end of stream */
607                                 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
608
609                                 p_stream->m_bytes_in_buffer = 0;
610                                 p_stream->m_status |= opj_stream_e_end;
611                                 /* end of stream */
612                                 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
613                         }
614                         else if (p_stream->m_bytes_in_buffer < p_size) {
615                                 /* not enough data */
616                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
617                                 p_stream->m_current_data = p_stream->m_stored_data;
618                                 p_buffer += p_stream->m_bytes_in_buffer;
619                                 p_size -= p_stream->m_bytes_in_buffer;
620                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
621                                 p_stream->m_bytes_in_buffer = 0;
622                         }
623                         else {
624                                 /* we have read the exact size */
625                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
626                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
627                                 p_stream->m_current_data = p_stream->m_stored_data;
628                                 p_stream->m_bytes_in_buffer = 0;
629                                 return l_read_nb_bytes;
630                         }
631                 }
632         }
633 }
634
635 /**
636  * Writes some bytes from the stream.
637  * @param               p_stream        the stream to write data to.
638  * @param               p_buffer        pointer to the data buffer holds the data to be writtent.
639  * @param               p_size          number of bytes to write.
640  * @param               p_event_mgr     the user event manager to be notified of special events.
641  * @return              the number of bytes writtent, or -1 if an error occured.
642  */
643 OPJ_SIZE_T opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer,OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
644 {
645         OPJ_SIZE_T l_remaining_bytes = 0;
646         OPJ_SIZE_T l_write_nb_bytes = 0;
647
648         if
649                 (p_stream->m_status & opj_stream_e_error)
650         {
651                 return (OPJ_SIZE_T)-1;
652         }
653
654         while(1)
655         {
656                 l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
657                 /* we have more memory than required */
658                 if
659                         (l_remaining_bytes >= p_size)
660                 {
661                         memcpy(p_stream->m_current_data,p_buffer,p_size);
662                         p_stream->m_current_data += p_size;
663                         p_stream->m_bytes_in_buffer += p_size;
664                         l_write_nb_bytes += p_size;
665                         p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
666                         return l_write_nb_bytes;
667                 }
668
669                 /* we copy data and then do an actual read on the stream */
670                 if
671                         (l_remaining_bytes)
672                 {
673                         l_write_nb_bytes += l_remaining_bytes;
674                         memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes);
675                         p_stream->m_current_data = p_stream->m_stored_data;
676                         p_buffer += l_remaining_bytes;
677                         p_size -= l_remaining_bytes;
678                         p_stream->m_bytes_in_buffer += l_remaining_bytes;
679                         p_stream->m_byte_offset += (OPJ_OFF_T)l_remaining_bytes;
680                 }
681                 if
682                         (! opj_stream_flush(p_stream, p_event_mgr))
683                 {
684                         return (OPJ_SIZE_T)-1;
685                 }
686         }
687
688 }
689
690 /**
691  * Writes the content of the stream buffer to the stream.
692  * @param               p_stream        the stream to write data to.
693  * @param               p_event_mgr     the user event manager to be notified of special events.
694  * @return              the number of bytes written, or -1 if an error occured.
695  */
696 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
697 {
698         /* the number of bytes written on the media. */
699         OPJ_SIZE_T l_current_write_nb_bytes = 0;
700         p_stream->m_current_data = p_stream->m_stored_data;
701
702         while
703                 (p_stream->m_bytes_in_buffer)
704         {
705                 /* we should do an actual write on the media */
706                 l_current_write_nb_bytes = p_stream->m_write_fn(p_stream->m_current_data,p_stream->m_bytes_in_buffer,p_stream->m_user_data);
707                 if
708                         (l_current_write_nb_bytes == (OPJ_SIZE_T)-1)
709                 {
710                         p_stream->m_status |= opj_stream_e_error;
711                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Error on writting stream!\n");
712
713                         return EXIT_FAILURE;
714                 }
715                 p_stream->m_current_data += l_current_write_nb_bytes;
716                 p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
717         }
718         p_stream->m_current_data = p_stream->m_stored_data;
719         return EXIT_SUCCESS;
720 }
721
722 /**
723  * Skips a number of bytes from the stream.
724  * @param               p_stream        the stream to skip data from.
725  * @param               p_size          the number of bytes to skip.
726  * @param               p_event_mgr     the user event manager to be notified of special events.
727  * @return              the number of bytes skipped, or -1 if an error occured.
728  */
729 OPJ_OFF_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
730 {
731         OPJ_OFF_T l_skip_nb_bytes = 0;
732         OPJ_OFF_T l_current_skip_nb_bytes = 0;
733
734   assert( p_size >= 0 );
735         if
736                 (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size)
737         {
738                 p_stream->m_current_data += p_size;
739     /* it is safe to cast p_size to OPJ_SIZE_T since it is <= m_bytes_in_buffer
740        which is of type OPJ_SIZE_T */
741                 p_stream->m_bytes_in_buffer -= (OPJ_SIZE_T)p_size;
742                 l_skip_nb_bytes += p_size;
743                 p_stream->m_byte_offset += l_skip_nb_bytes;
744                 return l_skip_nb_bytes;
745         }
746
747         /* we are now in the case when the remaining data if not sufficient */
748         if
749                 (p_stream->m_status & opj_stream_e_end)
750         {
751                 l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
752                 p_stream->m_current_data += p_stream->m_bytes_in_buffer;
753                 p_stream->m_bytes_in_buffer = 0;
754                 p_stream->m_byte_offset += l_skip_nb_bytes;
755                 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
756         }
757
758         /* the flag is not set, we copy data and then do an actual skip on the stream */
759         if
760                 (p_stream->m_bytes_in_buffer)
761         {
762                 l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
763                 p_stream->m_current_data = p_stream->m_stored_data;
764                 p_size -= (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
765                 p_stream->m_bytes_in_buffer = 0;
766         }
767
768         while
769                 (p_size > 0)
770         {
771                 /* we should do an actual skip on the media */
772                 l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
773                 if
774                         (l_current_skip_nb_bytes == (OPJ_OFF_T) -1)
775                 {
776                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
777
778                         p_stream->m_status |= opj_stream_e_end;
779                         p_stream->m_byte_offset += l_skip_nb_bytes;
780                         /* end if stream */
781                         return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
782                 }
783                 p_size -= l_current_skip_nb_bytes;
784                 l_skip_nb_bytes += l_current_skip_nb_bytes;
785         }
786         p_stream->m_byte_offset += l_skip_nb_bytes;
787         return l_skip_nb_bytes;
788 }
789
790 /**
791  * Skips a number of bytes from the stream.
792  * @param               p_stream        the stream to skip data from.
793  * @param               p_size          the number of bytes to skip.
794  * @param               p_event_mgr     the user event manager to be notified of special events.
795  * @return              the number of bytes skipped, or -1 if an error occured.
796  */
797 OPJ_OFF_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
798 {
799         opj_bool l_is_written = 0;
800         OPJ_OFF_T l_current_skip_nb_bytes = 0;
801         OPJ_OFF_T l_skip_nb_bytes = 0;
802
803         if
804                 (p_stream->m_status & opj_stream_e_error)
805         {
806                 return (OPJ_OFF_T) -1;
807         }
808
809         /* we should flush data */
810         l_is_written = opj_stream_flush (p_stream, p_event_mgr);
811         if
812                 (! l_is_written)
813         {
814                 p_stream->m_status |= opj_stream_e_error;
815                 p_stream->m_bytes_in_buffer = 0;
816                 p_stream->m_current_data = p_stream->m_current_data;
817                 return (OPJ_OFF_T) -1;
818         }
819         /* then skip */
820
821         while
822                 (p_size > 0)
823         {
824                 /* we should do an actual skip on the media */
825                 l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
826                 if
827                         (l_current_skip_nb_bytes == (OPJ_OFF_T)-1)
828                 {
829                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream error!\n");
830
831                         p_stream->m_status |= opj_stream_e_error;
832                         p_stream->m_byte_offset += l_skip_nb_bytes;
833                         /* end if stream */
834                         return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T)-1;
835                 }
836                 p_size -= l_current_skip_nb_bytes;
837                 l_skip_nb_bytes += l_current_skip_nb_bytes;
838         }
839         p_stream->m_byte_offset += l_skip_nb_bytes;
840         return l_skip_nb_bytes;
841 }
842
843 /**
844  * Tells the byte offset on the stream (similar to ftell).
845  *
846  * @param               p_stream        the stream to get the information from.
847  *
848  * @return              the current position of the stream.
849  */
850 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream)
851 {
852         return p_stream->m_byte_offset;
853 }
854
855
856 /**
857  * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
858  *
859  * @param               p_stream        the stream to get the information from.
860  *
861  * @return              Number of bytes left before the end of the stream.
862  */
863 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream)
864 {
865   assert( p_stream->m_byte_offset >= 0 );
866   assert( p_stream->m_user_data_length >= (OPJ_UINT64)p_stream->m_byte_offset);
867   return p_stream->m_user_data_length ?
868                                 (OPJ_OFF_T)(p_stream->m_user_data_length) - p_stream->m_byte_offset :
869                                 0;
870 }
871
872 /**
873  * Skips a number of bytes from the stream.
874  * @param               p_stream        the stream to skip data from.
875  * @param               p_size          the number of bytes to skip.
876  * @param               p_event_mgr     the user event manager to be notified of special events.
877  * @return              the number of bytes skipped, or -1 if an error occured.
878  */
879 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
880 {
881         assert(p_size >= 0);
882         return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
883 }
884
885
886 /**
887  * Skips a number of bytes from the stream.
888  * @param               p_stream        the stream to skip data from.
889  * @param               p_size          the number of bytes to skip.
890  * @param               p_event_mgr     the user event manager to be notified of special events.
891  * @return              the number of bytes skipped, or -1 if an error occured.
892  */
893 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
894 {
895         OPJ_ARG_NOT_USED(p_event_mgr);
896         p_stream->m_current_data = p_stream->m_stored_data;
897         p_stream->m_bytes_in_buffer = 0;
898
899         if( p_stream->m_seek_fn(p_size,p_stream->m_user_data)) {
900                 p_stream->m_status |= opj_stream_e_end;
901                 return EXIT_FAILURE;
902         }
903         else {
904                 /* reset stream status */
905                 p_stream->m_status &= (~opj_stream_e_end);
906                 p_stream->m_byte_offset = p_size;
907
908         }
909
910         return EXIT_SUCCESS;
911 }
912
913 /**
914  * Skips a number of bytes from the stream.
915  * @param               p_stream        the stream to skip data from.
916  * @param               p_size          the number of bytes to skip.
917  * @param               p_event_mgr     the user event manager to be notified of special events.
918  * @return              the number of bytes skipped, or -1 if an error occured.
919  */
920 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
921 {
922         if
923                 (! opj_stream_flush(p_stream,p_event_mgr))
924         {
925                 p_stream->m_status |= opj_stream_e_error;
926                 return EXIT_FAILURE;
927         }
928
929         p_stream->m_current_data = p_stream->m_stored_data;
930         p_stream->m_bytes_in_buffer = 0;
931
932         if
933                 (! p_stream->m_seek_fn(p_size,p_stream->m_user_data))
934         {
935                 p_stream->m_status |= opj_stream_e_error;
936                 return EXIT_FAILURE;
937         }
938         else
939         {
940                 p_stream->m_byte_offset = p_size;
941         }
942         return EXIT_SUCCESS;
943 }
944
945
946 /**
947  * Seeks a number of bytes from the stream.
948  * @param               p_stream        the stream to skip data from.
949  * @param               p_size          the number of bytes to skip.
950  * @param               p_event_mgr     the user event manager to be notified of special events.
951  * @return              true if the stream is seekable.
952  */
953 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
954 {
955         assert(p_size >= 0);
956         return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
957 }
958
959 /**
960  * Tells if the given stream is seekable.
961  */
962 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream)
963 {
964         return p_stream->m_seek_fn != opj_stream_default_seek;
965 }
966
967 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
968 {
969         OPJ_ARG_NOT_USED(p_buffer);
970         OPJ_ARG_NOT_USED(p_nb_bytes);
971         OPJ_ARG_NOT_USED(p_user_data);
972         return (OPJ_SIZE_T) -1;
973 }
974
975 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
976 {
977         OPJ_ARG_NOT_USED(p_buffer);
978         OPJ_ARG_NOT_USED(p_nb_bytes);
979         OPJ_ARG_NOT_USED(p_user_data);
980         return (OPJ_SIZE_T) -1;
981 }
982
983 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data)
984 {
985         OPJ_ARG_NOT_USED(p_nb_bytes);
986         OPJ_ARG_NOT_USED(p_user_data);
987         return (OPJ_OFF_T) -1;
988 }
989
990 opj_bool opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
991 {
992         OPJ_ARG_NOT_USED(p_nb_bytes);
993         OPJ_ARG_NOT_USED(p_user_data);
994         return EXIT_FAILURE;
995 }