2c5dec9f77ec7c40ab6e53bcc4a1f6ceef789be1
[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 /* ----------------------------------------------------------------------- */
192
193
194 /**
195  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
196  * @param p_buffer              pointer the data buffer to write data to.
197  * @param p_value               the value to write
198  * @param p_nb_bytes    the number of bytes to write
199 */
200 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
201 {
202         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes;
203
204         assert(p_nb_bytes > 0 && p_nb_bytes <=  sizeof(OPJ_UINT32));
205
206         memcpy(p_buffer,l_data_ptr,p_nb_bytes);
207 }
208
209 /**
210  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
211  * @param p_buffer              pointer the data buffer to write data to.
212  * @param p_value               the value to write
213  * @param p_nb_bytes    the number of bytes to write
214  * @return                              the number of bytes written or -1 if an error occured
215 */
216 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
217 {
218         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes - 1;
219         OPJ_UINT32 i;
220
221         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
222
223         for     (i=0;i<p_nb_bytes;++i) {
224                 *(p_buffer++) = *(l_data_ptr--);
225         }
226 }
227
228 /**
229  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
230  * @param p_buffer              pointer the data buffer to read data from.
231  * @param p_value               pointer to the value that will store the data.
232  * @param p_nb_bytes    the nb bytes to read.
233  * @return                              the number of bytes read or -1 if an error occured.
234  */
235 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
236 {
237         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
238
239         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
240
241         *p_value = 0;
242         memcpy(l_data_ptr+4-p_nb_bytes,p_buffer,p_nb_bytes);
243 }
244
245 /**
246  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
247  * @param p_buffer              pointer the data buffer to read data from.
248  * @param p_value               pointer to the value that will store the data.
249  * @param p_nb_bytes    the nb bytes to read.
250  * @return                              the number of bytes read or -1 if an error occured.
251  */
252 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
253 {
254         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + p_nb_bytes-1;
255         OPJ_UINT32 i;
256
257         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
258
259         *p_value = 0;
260         for (i=0;i<p_nb_bytes;++i) {
261                 *(l_data_ptr--) = *(p_buffer++);
262         }
263 }
264
265 /**
266  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
267  * @param p_buffer              pointer the data buffer to write data to.
268  * @param p_value               the value to write
269  * @return                              the number of bytes written or -1 if an error occured
270  */
271 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
272 {
273         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
274         memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT64));
275 }
276
277 /**
278  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
279  * @param p_buffer              pointer the data buffer to write data to.
280  * @param p_value               the value to write
281  */
282 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
283 {
284         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT64) - 1;
285         OPJ_UINT32 i;
286         for     (i=0;i<sizeof(OPJ_FLOAT64);++i) {
287                 *(p_buffer++) = *(l_data_ptr--);
288         }
289 }
290
291 /**
292  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
293  * @param p_buffer              pointer the data buffer to read data from.
294  * @param p_value               pointer to the value that will store the data.
295  */
296 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
297 {
298         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
299         memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT64));
300 }
301
302
303 /**
304  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
305  * @param p_buffer              pointer the data buffer to read data from.
306  * @param p_value               pointer to the value that will store the data.
307  */
308 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
309 {
310         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT64)-1;
311         OPJ_UINT32 i;
312         for (i=0;i<sizeof(OPJ_FLOAT64);++i) {
313                 *(l_data_ptr--) = *(p_buffer++);
314         }
315 }
316
317 /**
318  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
319  * @param p_buffer              pointer the data buffer to write data to.
320  * @param p_value               the value to write
321  * @return                              the number of bytes written or -1 if an error occured
322  */
323 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
324 {
325         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
326         memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT32));
327 }
328
329 /**
330  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
331  * @param p_buffer              pointer the data buffer to write data to.
332  * @param p_value               the value to write
333  */
334 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
335 {
336         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT32) - 1;
337         OPJ_UINT32 i;
338         for     (i=0;i<sizeof(OPJ_FLOAT32);++i) {
339                 *(p_buffer++) = *(l_data_ptr--);
340         }
341 }
342
343 /**
344  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
345  * @param p_buffer              pointer the data buffer to read data from.
346  * @param p_value               pointer to the value that will store the data.
347  */
348 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
349 {
350         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
351         memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT32));
352 }
353
354
355 /**
356  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
357  * @param p_buffer              pointer the data buffer to read data from.
358  * @param p_value               pointer to the value that will store the data.
359  */
360 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
361 {
362         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT32)-1;
363         OPJ_UINT32 i;
364         for     (i=0;i<sizeof(OPJ_FLOAT32);++i) {
365                 *(l_data_ptr--) = *(p_buffer++);
366         }
367 }
368
369
370 /**
371  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
372  * @return a stream object.
373 */
374 opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_UINT32 p_size,opj_bool l_is_input)
375 {
376         opj_stream_private_t * l_stream = 00;
377         l_stream = (opj_stream_private_t*) opj_malloc(sizeof(opj_stream_private_t));
378         if (! l_stream) {
379                 return 00;
380         }
381
382         memset(l_stream,0,sizeof(opj_stream_private_t));
383         l_stream->m_buffer_size = p_size;
384         l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_size);
385         if (! l_stream->m_stored_data) {
386                 opj_free(l_stream);
387                 return 00;
388         }
389
390         l_stream->m_current_data = l_stream->m_stored_data;
391
392         if (l_is_input) {
393                 l_stream->m_status |= opj_stream_e_input;
394                 l_stream->m_opj_skip = opj_stream_read_skip;
395                 l_stream->m_opj_seek = opj_stream_read_seek;
396         }
397         else {
398                 l_stream->m_status |= opj_stream_e_output;
399                 l_stream->m_opj_skip = opj_stream_write_skip;
400                 l_stream->m_opj_seek = opj_stream_write_seek;
401         }
402
403         l_stream->m_read_fn = opj_stream_default_read;
404         l_stream->m_write_fn = opj_stream_default_write;
405         l_stream->m_skip_fn = opj_stream_default_skip;
406         l_stream->m_seek_fn = opj_stream_default_seek;
407
408         return (opj_stream_t *) l_stream;
409 }
410
411 /**
412  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
413  * @return a stream object.
414 */
415 opj_stream_t* OPJ_CALLCONV opj_stream_default_create(opj_bool l_is_input)
416 {
417         return opj_stream_create(J2K_STREAM_CHUNK_SIZE,l_is_input);
418 }
419
420 /**
421  * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must
422  * close its own implementation of the stream.
423  */
424 OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream)
425 {
426         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
427         if (l_stream) {
428                 opj_free(l_stream->m_stored_data);
429                 l_stream->m_stored_data = 00;
430                 opj_free(l_stream);
431         }
432 }
433
434 /**
435  * Sets the given function to be used as a read function.
436  * @param               p_stream        the stream to modify
437  * @param               p_function      the function to use a read function.
438 */
439 OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function)
440 {
441         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
442         if
443                 ((!l_stream) || (! (l_stream->m_status & opj_stream_e_input)))
444         {
445                 return;
446         }
447         l_stream->m_read_fn = p_function;
448 }
449
450 OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function)
451 {
452         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
453         if
454                 (!l_stream)
455         {
456                 return;
457         }
458         l_stream->m_seek_fn = p_function;
459 }
460
461 /**
462  * Sets the given function to be used as a write function.
463  * @param               p_stream        the stream to modify
464  * @param               p_function      the function to use a write function.
465 */
466 OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function)
467 {
468         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
469         if
470                 ((!l_stream )|| (! (l_stream->m_status & opj_stream_e_output)))
471         {
472                 return;
473         }
474         l_stream->m_write_fn = p_function;
475 }
476
477 /**
478  * Sets the given function to be used as a skip function.
479  * @param               p_stream        the stream to modify
480  * @param               p_function      the function to use a skip function.
481 */
482 OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function)
483 {
484         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
485         if
486                 (! l_stream)
487         {
488                 return;
489         }
490         l_stream->m_skip_fn = p_function;
491 }
492
493 /**
494  * Sets the given data to be used as a user data for the stream.
495  * @param               p_stream        the stream to modify
496  * @param               p_data          the data to set.
497 */
498 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data)
499 {
500         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
501         l_stream->m_user_data = p_data;
502 }
503
504 /**
505  * Reads some bytes from the stream.
506  * @param               p_stream        the stream to read data from.
507  * @param               p_buffer        pointer to the data buffer that will receive the data.
508  * @param               p_size          number of bytes to read.
509  * @param               p_event_mgr     the user event manager to be notified of special events.
510  * @return              the number of bytes read, or -1 if an error occured or if the stream is at the end.
511  */
512 OPJ_UINT32 opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, opj_event_mgr_t * p_event_mgr)
513 {
514         OPJ_UINT32 l_read_nb_bytes = 0;
515         if
516                 (p_stream->m_bytes_in_buffer >= p_size)
517         {
518                 memcpy(p_buffer,p_stream->m_current_data,p_size);
519                 p_stream->m_current_data += p_size;
520                 p_stream->m_bytes_in_buffer -= p_size;
521                 l_read_nb_bytes += p_size;
522                 p_stream->m_byte_offset += p_size;
523                 return l_read_nb_bytes;
524         }
525
526         // we are now in the case when the remaining data if not sufficient
527         if
528                 (p_stream->m_status & opj_stream_e_end)
529         {
530                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
531                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
532                 p_stream->m_current_data += p_stream->m_bytes_in_buffer;
533                 p_stream->m_byte_offset += p_stream->m_bytes_in_buffer;
534                 p_stream->m_bytes_in_buffer = 0;
535                 return l_read_nb_bytes ? l_read_nb_bytes : -1;
536         }
537
538         // the flag is not set, we copy data and then do an actual read on the stream
539         if
540                 (p_stream->m_bytes_in_buffer)
541         {
542                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
543                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
544                 p_stream->m_current_data = p_stream->m_stored_data;
545                 p_buffer += p_stream->m_bytes_in_buffer;
546                 p_size -= p_stream->m_bytes_in_buffer;
547                 p_stream->m_byte_offset += p_stream->m_bytes_in_buffer;
548                 p_stream->m_bytes_in_buffer = 0;
549         }
550   else
551   {
552     /* case where we are already at the end of the buffer
553        so reset the m_current_data to point to the start of the
554        stored buffer to get ready to read from disk*/
555     p_stream->m_current_data = p_stream->m_stored_data;
556   }
557
558
559         while(1){
560                 // we should read less than a chunk -> read a chunk
561                 if
562                         (p_size < p_stream->m_buffer_size)
563                 {
564                         // we should do an actual read on the media
565                         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);
566                         if
567                                 (p_stream->m_bytes_in_buffer == -1)
568                         {
569                                 // end of stream
570 #ifdef TODO_MSD
571                                 opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
572 #endif
573                                 p_stream->m_bytes_in_buffer = 0;
574                                 p_stream->m_status |= opj_stream_e_end;
575                                 // end of stream
576                                 return l_read_nb_bytes ? l_read_nb_bytes : -1;
577                         }
578                         else if
579                                 (p_stream->m_bytes_in_buffer < p_size)
580                         {
581                                 // not enough data
582                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
583                                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
584                                 p_stream->m_current_data = p_stream->m_stored_data;
585                                 p_buffer += p_stream->m_bytes_in_buffer;
586                                 p_size -= p_stream->m_bytes_in_buffer;
587                                 p_stream->m_byte_offset += p_stream->m_bytes_in_buffer;
588                                 p_stream->m_bytes_in_buffer = 0;
589                         }
590                         else
591                         {
592                                 l_read_nb_bytes += p_size;
593                                 memcpy(p_buffer,p_stream->m_current_data,p_size);
594                                 p_stream->m_current_data += p_size;
595                                 p_stream->m_bytes_in_buffer -= p_size;
596                                 p_stream->m_byte_offset += p_size;
597                                 return l_read_nb_bytes;
598                         }
599                 }
600                 else
601                 {
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                         if
605                                 (p_stream->m_bytes_in_buffer == -1)
606                         {
607                                 // end of stream
608 #ifdef TODO_MSD
609                                 opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
610 #endif
611                                 p_stream->m_bytes_in_buffer = 0;
612                                 p_stream->m_status |= opj_stream_e_end;
613                                 // end of stream
614                                 return l_read_nb_bytes ? l_read_nb_bytes : -1;
615                         }
616                         else if
617                                 (p_stream->m_bytes_in_buffer < p_size)
618                         {
619                                 // not enough data
620                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
621                                 p_stream->m_current_data = p_stream->m_stored_data;
622                                 p_buffer += p_stream->m_bytes_in_buffer;
623                                 p_size -= p_stream->m_bytes_in_buffer;
624                                 p_stream->m_byte_offset += p_stream->m_bytes_in_buffer;
625                                 p_stream->m_bytes_in_buffer = 0;
626                         }
627                         else
628                         {
629                                 // we have read the exact size
630                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
631                                 p_stream->m_byte_offset += p_stream->m_bytes_in_buffer;
632                                 p_stream->m_current_data = p_stream->m_stored_data;
633                                 p_stream->m_bytes_in_buffer = 0;
634                                 return l_read_nb_bytes;
635                         }
636                 }
637         }
638 }
639
640 /**
641  * Writes some bytes from the stream.
642  * @param               p_stream        the stream to write data to.
643  * @param               p_buffer        pointer to the data buffer holds the data to be writtent.
644  * @param               p_size          number of bytes to write.
645  * @param               p_event_mgr     the user event manager to be notified of special events.
646  * @return              the number of bytes writtent, or -1 if an error occured.
647  */
648 OPJ_UINT32 opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer,OPJ_UINT32 p_size, opj_event_mgr_t * p_event_mgr)
649 {
650         OPJ_UINT32 l_remaining_bytes = 0;
651         OPJ_UINT32 l_write_nb_bytes = 0;
652
653         if
654                 (p_stream->m_status & opj_stream_e_error)
655         {
656                 return -1;
657         }
658
659         while(1)
660         {
661                 l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
662                 // we have more memory than required
663                 if
664                         (l_remaining_bytes >= p_size)
665                 {
666                         memcpy(p_stream->m_current_data,p_buffer,p_size);
667                         p_stream->m_current_data += p_size;
668                         p_stream->m_bytes_in_buffer += p_size;
669                         l_write_nb_bytes += p_size;
670                         p_stream->m_byte_offset += p_size;
671                         return l_write_nb_bytes;
672                 }
673
674                 // we copy data and then do an actual read on the stream
675                 if
676                         (l_remaining_bytes)
677                 {
678                         l_write_nb_bytes += l_remaining_bytes;
679                         memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes);
680                         p_stream->m_current_data = p_stream->m_stored_data;
681                         p_buffer += l_remaining_bytes;
682                         p_size -= l_remaining_bytes;
683                         p_stream->m_bytes_in_buffer += l_remaining_bytes;
684                         p_stream->m_byte_offset += l_remaining_bytes;
685                 }
686                 if
687                         (! opj_stream_flush(p_stream, p_event_mgr))
688                 {
689                         return -1;
690                 }
691         }
692
693 }
694
695 /**
696  * Writes the content of the stream buffer to the stream.
697  * @param               p_stream        the stream to write data to.
698  * @param               p_event_mgr     the user event manager to be notified of special events.
699  * @return              the number of bytes written, or -1 if an error occured.
700  */
701 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
702 {
703         // the number of bytes written on the media.
704         OPJ_UINT32 l_current_write_nb_bytes = 0;
705         p_stream->m_current_data = p_stream->m_stored_data;
706
707         while
708                 (p_stream->m_bytes_in_buffer)
709         {
710                 // we should do an actual write on the media
711                 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);
712                 if
713                         (l_current_write_nb_bytes == -1)
714                 {
715                         p_stream->m_status |= opj_stream_e_error;
716 #ifdef TODO_MSD
717                         opj_event_msg(p_event_mgr, EVT_INFO, "Error on writting stream!\n");
718 #endif
719                         return EXIT_FAILURE;
720                 }
721                 p_stream->m_current_data += l_current_write_nb_bytes;
722                 p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
723         }
724         p_stream->m_current_data = p_stream->m_stored_data;
725         return EXIT_SUCCESS;
726 }
727
728 /**
729  * Skips a number of bytes from the stream.
730  * @param               p_stream        the stream to skip data from.
731  * @param               p_size          the number of bytes to skip.
732  * @param               p_event_mgr     the user event manager to be notified of special events.
733  * @return              the number of bytes skipped, or -1 if an error occured.
734  */
735 OPJ_SIZE_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
736 {
737         OPJ_SIZE_T l_skip_nb_bytes = 0;
738         OPJ_SIZE_T l_current_skip_nb_bytes = 0;
739
740         if
741                 (p_stream->m_bytes_in_buffer >= p_size)
742         {
743                 p_stream->m_current_data += p_size;
744                 p_stream->m_bytes_in_buffer -= p_size;
745                 l_skip_nb_bytes += p_size;
746                 p_stream->m_byte_offset += l_skip_nb_bytes;
747                 return l_skip_nb_bytes;
748         }
749
750         // we are now in the case when the remaining data if not sufficient
751         if
752                 (p_stream->m_status & opj_stream_e_end)
753         {
754                 l_skip_nb_bytes += p_stream->m_bytes_in_buffer;
755                 p_stream->m_current_data += p_stream->m_bytes_in_buffer;
756                 p_stream->m_bytes_in_buffer = 0;
757                 p_stream->m_byte_offset += l_skip_nb_bytes;
758                 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T) -1;
759         }
760
761         // the flag is not set, we copy data and then do an actual skip on the stream
762         if
763                 (p_stream->m_bytes_in_buffer)
764         {
765                 l_skip_nb_bytes += p_stream->m_bytes_in_buffer;
766                 p_stream->m_current_data = p_stream->m_stored_data;
767                 p_size -= p_stream->m_bytes_in_buffer;
768                 p_stream->m_bytes_in_buffer = 0;
769         }
770
771         while
772                 (p_size > 0)
773         {
774                 // we should do an actual skip on the media
775                 l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
776                 if
777                         (l_current_skip_nb_bytes == (OPJ_SIZE_T) -1)
778                 {
779 #ifdef TODO_MSD
780                         opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
781 # endif
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_SIZE_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_SIZE_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
802 {
803         opj_bool l_is_written = 0;
804         OPJ_SIZE_T l_current_skip_nb_bytes = 0;
805         OPJ_SIZE_T l_skip_nb_bytes = 0;
806
807         if
808                 (p_stream->m_status & opj_stream_e_error)
809         {
810                 return (OPJ_SIZE_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_SIZE_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_SIZE_T)-1)
832                 {
833 #ifdef TODO_MSD
834                         opj_event_msg(p_event_mgr, EVT_INFO, "Stream error!\n");
835 #endif
836                         p_stream->m_status |= opj_stream_e_error;
837                         p_stream->m_byte_offset += l_skip_nb_bytes;
838                         // end if stream
839                         return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_SIZE_T)-1;
840                 }
841                 p_size -= l_current_skip_nb_bytes;
842                 l_skip_nb_bytes += l_current_skip_nb_bytes;
843         }
844         p_stream->m_byte_offset += l_skip_nb_bytes;
845         return l_skip_nb_bytes;
846 }
847
848 /**
849  * Tells the byte offset on the stream (similar to ftell).
850  *
851  * @param               p_stream        the stream to get the information from.
852  *
853  * @return              the current position of the stream.
854  */
855 OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream)
856 {
857         return p_stream->m_byte_offset;
858 }
859
860 /**
861  * Skips a number of bytes from the stream.
862  * @param               p_stream        the stream to skip data from.
863  * @param               p_size          the number of bytes to skip.
864  * @param               p_event_mgr     the user event manager to be notified of special events.
865  * @return              the number of bytes skipped, or -1 if an error occured.
866  */
867 OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
868 {
869         return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
870 }
871
872
873 /**
874  * Skips a number of bytes from the stream.
875  * @param               p_stream        the stream to skip data from.
876  * @param               p_size          the number of bytes to skip.
877  * @param               p_event_mgr     the user event manager to be notified of special events.
878  * @return              the number of bytes skipped, or -1 if an error occured.
879  */
880 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
881 {
882         p_stream->m_current_data = p_stream->m_stored_data;
883         p_stream->m_bytes_in_buffer = 0;
884         if
885                 (! p_stream->m_seek_fn(p_size,p_stream->m_user_data))
886         {
887                 p_stream->m_status |= opj_stream_e_end;
888                 return EXIT_FAILURE;
889         }
890         else
891         {
892                 // reset stream status
893                 p_stream->m_status &= (~opj_stream_e_end);
894                 p_stream->m_byte_offset = p_size;
895
896         }
897         return EXIT_SUCCESS;
898 }
899
900 /**
901  * Skips a number of bytes from the stream.
902  * @param               p_stream        the stream to skip data from.
903  * @param               p_size          the number of bytes to skip.
904  * @param               p_event_mgr     the user event manager to be notified of special events.
905  * @return              the number of bytes skipped, or -1 if an error occured.
906  */
907 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
908 {
909         if
910                 (! opj_stream_flush(p_stream,p_event_mgr))
911         {
912                 p_stream->m_status |= opj_stream_e_error;
913                 return EXIT_FAILURE;
914         }
915
916         p_stream->m_current_data = p_stream->m_stored_data;
917         p_stream->m_bytes_in_buffer = 0;
918
919         if
920                 (! p_stream->m_seek_fn(p_size,p_stream->m_user_data))
921         {
922                 p_stream->m_status |= opj_stream_e_error;
923                 return EXIT_FAILURE;
924         }
925         else
926         {
927                 p_stream->m_byte_offset = p_size;
928         }
929         return EXIT_SUCCESS;
930 }
931
932
933 /**
934  * Seeks a number of bytes from the stream.
935  * @param               p_stream        the stream to skip data from.
936  * @param               p_size          the number of bytes to skip.
937  * @param               p_event_mgr     the user event manager to be notified of special events.
938  * @return              true if the stream is seekable.
939  */
940 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr)
941 {
942         return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
943 }
944
945 /**
946  * Tells if the given stream is seekable.
947  */
948 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream)
949 {
950         return p_stream->m_seek_fn != opj_stream_default_seek;
951 }
952
953
954
955
956
957 OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data)
958 {
959         return (OPJ_UINT32) -1;
960 }
961 OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data)
962 {
963         return (OPJ_UINT32) -1;
964 }
965 OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data)
966 {
967         return (OPJ_SIZE_T) -1;
968 }
969
970 opj_bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data)
971 {
972         return EXIT_FAILURE;
973 }
974
975
976
977