bb5729bac935ba549cc6370961b55b6878f3a498
[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,
644                                                                   const OPJ_BYTE * p_buffer,
645                                                                   OPJ_SIZE_T p_size, 
646                                                                   opj_event_mgr_t * p_event_mgr)
647 {
648         OPJ_SIZE_T l_remaining_bytes = 0;
649         OPJ_SIZE_T l_write_nb_bytes = 0;
650
651         if (p_stream->m_status & opj_stream_e_error) {
652                 return (OPJ_SIZE_T)-1;
653         }
654
655         while(1) {
656                 l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
657                 
658                 /* we have more memory than required */
659                 if (l_remaining_bytes >= p_size) {
660                         memcpy(p_stream->m_current_data, p_buffer, p_size);
661                         
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                         
667                         return l_write_nb_bytes;
668                 }
669
670                 /* we copy data and then do an actual read on the stream */
671                 if (l_remaining_bytes) {
672                         l_write_nb_bytes += l_remaining_bytes;
673                         
674                         memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes);
675                         
676                         p_stream->m_current_data = p_stream->m_stored_data;
677                         
678                         p_buffer += l_remaining_bytes;
679                         p_size -= l_remaining_bytes;
680                         p_stream->m_bytes_in_buffer += l_remaining_bytes;
681                         p_stream->m_byte_offset += (OPJ_OFF_T)l_remaining_bytes;
682                 }
683
684                 if (opj_stream_flush(p_stream, p_event_mgr) == EXIT_FAILURE) {
685                         return (OPJ_SIZE_T)-1;
686                 }
687         }
688
689 }
690
691 /**
692  * Writes the content of the stream buffer to the stream.
693  * @param               p_stream        the stream to write data to.
694  * @param               p_event_mgr     the user event manager to be notified of special events.
695  * @return              the number of bytes written, or -1 if an error occured.
696  */
697 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
698 {
699         /* the number of bytes written on the media. */
700         OPJ_SIZE_T l_current_write_nb_bytes = 0;
701
702         p_stream->m_current_data = p_stream->m_stored_data;
703
704         while (p_stream->m_bytes_in_buffer) {
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,
707                                                                                                                 p_stream->m_bytes_in_buffer,
708                                                                                                                 p_stream->m_user_data);
709                 
710                 if (l_current_write_nb_bytes == (OPJ_SIZE_T)-1) {
711                         p_stream->m_status |= opj_stream_e_error;
712                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Error on writting stream!\n");
713
714                         return EXIT_FAILURE;
715                 }
716
717                 p_stream->m_current_data += l_current_write_nb_bytes;
718                 p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
719         }
720
721         p_stream->m_current_data = p_stream->m_stored_data;
722         
723         return EXIT_SUCCESS;
724 }
725
726 /**
727  * Skips a number of bytes from the stream.
728  * @param               p_stream        the stream to skip data from.
729  * @param               p_size          the number of bytes to skip.
730  * @param               p_event_mgr     the user event manager to be notified of special events.
731  * @return              the number of bytes skipped, or -1 if an error occured.
732  */
733 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)
734 {
735         OPJ_OFF_T l_skip_nb_bytes = 0;
736         OPJ_OFF_T l_current_skip_nb_bytes = 0;
737
738   assert( p_size >= 0 );
739         if
740                 (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size)
741         {
742                 p_stream->m_current_data += p_size;
743     /* it is safe to cast p_size to OPJ_SIZE_T since it is <= m_bytes_in_buffer
744        which is of type OPJ_SIZE_T */
745                 p_stream->m_bytes_in_buffer -= (OPJ_SIZE_T)p_size;
746                 l_skip_nb_bytes += p_size;
747                 p_stream->m_byte_offset += l_skip_nb_bytes;
748                 return l_skip_nb_bytes;
749         }
750
751         /* we are now in the case when the remaining data if not sufficient */
752         if
753                 (p_stream->m_status & opj_stream_e_end)
754         {
755                 l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
756                 p_stream->m_current_data += p_stream->m_bytes_in_buffer;
757                 p_stream->m_bytes_in_buffer = 0;
758                 p_stream->m_byte_offset += l_skip_nb_bytes;
759                 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
760         }
761
762         /* the flag is not set, we copy data and then do an actual skip on the stream */
763         if
764                 (p_stream->m_bytes_in_buffer)
765         {
766                 l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
767                 p_stream->m_current_data = p_stream->m_stored_data;
768                 p_size -= (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
769                 p_stream->m_bytes_in_buffer = 0;
770         }
771
772         while
773                 (p_size > 0)
774         {
775                 /* we should do an actual skip on the media */
776                 l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
777                 if
778                         (l_current_skip_nb_bytes == (OPJ_OFF_T) -1)
779                 {
780                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
781
782                         p_stream->m_status |= opj_stream_e_end;
783                         p_stream->m_byte_offset += l_skip_nb_bytes;
784                         /* end if stream */
785                         return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
786                 }
787                 p_size -= l_current_skip_nb_bytes;
788                 l_skip_nb_bytes += l_current_skip_nb_bytes;
789         }
790         p_stream->m_byte_offset += l_skip_nb_bytes;
791         return l_skip_nb_bytes;
792 }
793
794 /**
795  * Skips a number of bytes from the stream.
796  * @param               p_stream        the stream to skip data from.
797  * @param               p_size          the number of bytes to skip.
798  * @param               p_event_mgr     the user event manager to be notified of special events.
799  * @return              the number of bytes skipped, or -1 if an error occured.
800  */
801 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)
802 {
803         opj_bool l_is_written = 0;
804         OPJ_OFF_T l_current_skip_nb_bytes = 0;
805         OPJ_OFF_T l_skip_nb_bytes = 0;
806
807         if
808                 (p_stream->m_status & opj_stream_e_error)
809         {
810                 return (OPJ_OFF_T) -1;
811         }
812
813         /* we should flush data */
814         l_is_written = opj_stream_flush (p_stream, p_event_mgr);
815         if
816                 (! l_is_written)
817         {
818                 p_stream->m_status |= opj_stream_e_error;
819                 p_stream->m_bytes_in_buffer = 0;
820                 p_stream->m_current_data = p_stream->m_current_data;
821                 return (OPJ_OFF_T) -1;
822         }
823         /* then skip */
824
825         while
826                 (p_size > 0)
827         {
828                 /* we should do an actual skip on the media */
829                 l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
830                 if
831                         (l_current_skip_nb_bytes == (OPJ_OFF_T)-1)
832                 {
833                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream error!\n");
834
835                         p_stream->m_status |= opj_stream_e_error;
836                         p_stream->m_byte_offset += l_skip_nb_bytes;
837                         /* end if stream */
838                         return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T)-1;
839                 }
840                 p_size -= l_current_skip_nb_bytes;
841                 l_skip_nb_bytes += l_current_skip_nb_bytes;
842         }
843         p_stream->m_byte_offset += l_skip_nb_bytes;
844         return l_skip_nb_bytes;
845 }
846
847 /**
848  * Tells the byte offset on the stream (similar to ftell).
849  *
850  * @param               p_stream        the stream to get the information from.
851  *
852  * @return              the current position of the stream.
853  */
854 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream)
855 {
856         return p_stream->m_byte_offset;
857 }
858
859
860 /**
861  * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
862  *
863  * @param               p_stream        the stream to get the information from.
864  *
865  * @return              Number of bytes left before the end of the stream.
866  */
867 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream)
868 {
869   assert( p_stream->m_byte_offset >= 0 );
870   assert( p_stream->m_user_data_length >= (OPJ_UINT64)p_stream->m_byte_offset);
871   return p_stream->m_user_data_length ?
872                                 (OPJ_OFF_T)(p_stream->m_user_data_length) - p_stream->m_byte_offset :
873                                 0;
874 }
875
876 /**
877  * Skips a number of bytes from the stream.
878  * @param               p_stream        the stream to skip data from.
879  * @param               p_size          the number of bytes to skip.
880  * @param               p_event_mgr     the user event manager to be notified of special events.
881  * @return              the number of bytes skipped, or -1 if an error occured.
882  */
883 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
884 {
885         assert(p_size >= 0);
886         return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
887 }
888
889
890 /**
891  * Skips a number of bytes from the stream.
892  * @param               p_stream        the stream to skip data from.
893  * @param               p_size          the number of bytes to skip.
894  * @param               p_event_mgr     the user event manager to be notified of special events.
895  * @return              the number of bytes skipped, or -1 if an error occured.
896  */
897 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
898 {
899         OPJ_ARG_NOT_USED(p_event_mgr);
900         p_stream->m_current_data = p_stream->m_stored_data;
901         p_stream->m_bytes_in_buffer = 0;
902
903         if( p_stream->m_seek_fn(p_size,p_stream->m_user_data)) {
904                 p_stream->m_status |= opj_stream_e_end;
905                 return EXIT_FAILURE;
906         }
907         else {
908                 /* reset stream status */
909                 p_stream->m_status &= (~opj_stream_e_end);
910                 p_stream->m_byte_offset = p_size;
911
912         }
913
914         return EXIT_SUCCESS;
915 }
916
917 /**
918  * Skips a number of bytes from the stream.
919  * @param               p_stream        the stream to skip data from.
920  * @param               p_size          the number of bytes to skip.
921  * @param               p_event_mgr     the user event manager to be notified of special events.
922  * @return              the number of bytes skipped, or -1 if an error occured.
923  */
924 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
925 {
926         if
927                 (! opj_stream_flush(p_stream,p_event_mgr))
928         {
929                 p_stream->m_status |= opj_stream_e_error;
930                 return EXIT_FAILURE;
931         }
932
933         p_stream->m_current_data = p_stream->m_stored_data;
934         p_stream->m_bytes_in_buffer = 0;
935
936         if
937                 (! p_stream->m_seek_fn(p_size,p_stream->m_user_data))
938         {
939                 p_stream->m_status |= opj_stream_e_error;
940                 return EXIT_FAILURE;
941         }
942         else
943         {
944                 p_stream->m_byte_offset = p_size;
945         }
946         return EXIT_SUCCESS;
947 }
948
949
950 /**
951  * Seeks a number of bytes from the stream.
952  * @param               p_stream        the stream to skip data from.
953  * @param               p_size          the number of bytes to skip.
954  * @param               p_event_mgr     the user event manager to be notified of special events.
955  * @return              true if the stream is seekable.
956  */
957 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
958 {
959         assert(p_size >= 0);
960         return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
961 }
962
963 /**
964  * Tells if the given stream is seekable.
965  */
966 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream)
967 {
968         return p_stream->m_seek_fn != opj_stream_default_seek;
969 }
970
971 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
972 {
973         OPJ_ARG_NOT_USED(p_buffer);
974         OPJ_ARG_NOT_USED(p_nb_bytes);
975         OPJ_ARG_NOT_USED(p_user_data);
976         return (OPJ_SIZE_T) -1;
977 }
978
979 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
980 {
981         OPJ_ARG_NOT_USED(p_buffer);
982         OPJ_ARG_NOT_USED(p_nb_bytes);
983         OPJ_ARG_NOT_USED(p_user_data);
984         return (OPJ_SIZE_T) -1;
985 }
986
987 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data)
988 {
989         OPJ_ARG_NOT_USED(p_nb_bytes);
990         OPJ_ARG_NOT_USED(p_user_data);
991         return (OPJ_OFF_T) -1;
992 }
993
994 opj_bool opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
995 {
996         OPJ_ARG_NOT_USED(p_nb_bytes);
997         OPJ_ARG_NOT_USED(p_user_data);
998         return EXIT_FAILURE;
999 }