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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
32 #include "opj_includes.h"
34 /* ----------------------------------------------------------------------- */
36 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
38 opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
41 if(buffer && length) {
42 /* wrap a user buffer containing the encoded image */
43 cio->openmode = OPJ_STREAM_READ;
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) {
52 cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
55 cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
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);
65 opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
75 /* Initialize byte IO */
76 cio->start = cio->buffer;
77 cio->end = cio->buffer + cio->length;
78 cio->bp = cio->buffer;
83 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
85 if(cio->openmode == OPJ_STREAM_WRITE) {
86 /* destroy the allocated buffer */
87 opj_free(cio->buffer);
95 /* ----------------------------------------------------------------------- */
98 * Get position in byte stream.
100 OPJ_OFF_T OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
101 return cio->bp - cio->start;
105 * Set position in byte stream.
107 * pos : position, in number of bytes, from the beginning of the stream
109 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
110 cio->bp = cio->start + pos;
114 * Number of bytes left before the end of the stream.
116 OPJ_SIZE_T cio_numbytesleft(opj_cio_t *cio) {
117 const ptrdiff_t diff = cio->end - cio->bp;
119 return (OPJ_SIZE_T)diff;
123 * Get pointer to the current position in the stream.
125 unsigned char *cio_getbp(opj_cio_t *cio) {
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");
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);
156 * n : number of bytes to write
158 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n) {
160 for (i = n - 1; i >= 0; i--) {
161 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
165 return (unsigned int)n;
171 * n : number of bytes to read
173 * return : value of the n bytes read
175 unsigned int cio_read(opj_cio_t *cio, int n) {
178 for (i = n - 1; i >= 0; i--) {
179 const unsigned int c = cio_bytein(cio);
188 * n : number of bytes to skip
190 void cio_skip(opj_cio_t *cio, int n) {
195 /* ----------------------------------------------------------------------- */
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
204 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
206 const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes;
208 assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
210 memcpy(p_buffer,l_data_ptr,p_nb_bytes);
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
220 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
222 const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes - 1;
225 assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
227 for (i=0;i<p_nb_bytes;++i) {
228 *(p_buffer++) = *(l_data_ptr--);
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.
239 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
241 OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
243 assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
246 memcpy(l_data_ptr+4-p_nb_bytes,p_buffer,p_nb_bytes);
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.
256 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
258 OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + p_nb_bytes-1;
261 assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
264 for (i=0;i<p_nb_bytes;++i) {
265 *(l_data_ptr--) = *(p_buffer++);
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
275 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
277 const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
278 memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT64));
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
286 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
288 const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT64) - 1;
290 for (i=0;i<sizeof(OPJ_FLOAT64);++i) {
291 *(p_buffer++) = *(l_data_ptr--);
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.
300 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
302 OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
303 memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT64));
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.
312 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
314 OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT64)-1;
316 for (i=0;i<sizeof(OPJ_FLOAT64);++i) {
317 *(l_data_ptr--) = *(p_buffer++);
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
327 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
329 const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
330 memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT32));
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
338 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
340 const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT32) - 1;
342 for (i=0;i<sizeof(OPJ_FLOAT32);++i) {
343 *(p_buffer++) = *(l_data_ptr--);
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.
352 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
354 OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
355 memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT32));
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.
364 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
366 OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT32)-1;
368 for (i=0;i<sizeof(OPJ_FLOAT32);++i) {
369 *(l_data_ptr--) = *(p_buffer++);
375 * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
376 * @return a stream object.
378 opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,opj_bool l_is_input)
380 opj_stream_private_t * l_stream = 00;
381 l_stream = (opj_stream_private_t*) opj_malloc(sizeof(opj_stream_private_t));
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) {
394 l_stream->m_current_data = l_stream->m_stored_data;
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;
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;
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;
412 return (opj_stream_t *) l_stream;
416 * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
417 * @return a stream object.
419 opj_stream_t* OPJ_CALLCONV opj_stream_default_create(opj_bool l_is_input)
421 return opj_stream_create(J2K_STREAM_CHUNK_SIZE,l_is_input);
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.
428 OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream)
430 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
432 opj_free(l_stream->m_stored_data);
433 l_stream->m_stored_data = 00;
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.
443 OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function)
445 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
447 if ((!l_stream) || (! (l_stream->m_status & opj_stream_e_input))) {
451 l_stream->m_read_fn = p_function;
454 OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function)
456 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
462 l_stream->m_seek_fn = p_function;
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.
470 OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function)
472 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
474 ((!l_stream )|| (! (l_stream->m_status & opj_stream_e_output)))
478 l_stream->m_write_fn = p_function;
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.
486 OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function)
488 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
494 l_stream->m_skip_fn = p_function;
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.
502 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data)
504 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
505 l_stream->m_user_data = p_data;
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.
513 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length)
515 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
516 l_stream->m_user_data_length = data_length;
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.
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)
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;
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;
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;
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;
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);
573 if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
575 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
577 p_stream->m_bytes_in_buffer = 0;
578 p_stream->m_status |= opj_stream_e_end;
580 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
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;
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;
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);
605 if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
607 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
609 p_stream->m_bytes_in_buffer = 0;
610 p_stream->m_status |= opj_stream_e_end;
612 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
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;
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;
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.
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)
645 OPJ_SIZE_T l_remaining_bytes = 0;
646 OPJ_SIZE_T l_write_nb_bytes = 0;
649 (p_stream->m_status & opj_stream_e_error)
651 return (OPJ_SIZE_T)-1;
656 l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
657 /* we have more memory than required */
659 (l_remaining_bytes >= p_size)
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;
669 /* we copy data and then do an actual read on the stream */
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;
682 (! opj_stream_flush(p_stream, p_event_mgr))
684 return (OPJ_SIZE_T)-1;
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.
696 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
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;
703 (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,p_stream->m_bytes_in_buffer,p_stream->m_user_data);
708 (l_current_write_nb_bytes == (OPJ_SIZE_T)-1)
710 p_stream->m_status |= opj_stream_e_error;
711 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Error on writting stream!\n");
715 p_stream->m_current_data += l_current_write_nb_bytes;
716 p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
718 p_stream->m_current_data = p_stream->m_stored_data;
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.
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)
731 OPJ_OFF_T l_skip_nb_bytes = 0;
732 OPJ_OFF_T l_current_skip_nb_bytes = 0;
734 assert( p_size >= 0 );
736 (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size)
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;
747 /* we are now in the case when the remaining data if not sufficient */
749 (p_stream->m_status & opj_stream_e_end)
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;
758 /* the flag is not set, we copy data and then do an actual skip on the stream */
760 (p_stream->m_bytes_in_buffer)
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;
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);
774 (l_current_skip_nb_bytes == (OPJ_OFF_T) -1)
776 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
778 p_stream->m_status |= opj_stream_e_end;
779 p_stream->m_byte_offset += l_skip_nb_bytes;
781 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
783 p_size -= l_current_skip_nb_bytes;
784 l_skip_nb_bytes += l_current_skip_nb_bytes;
786 p_stream->m_byte_offset += l_skip_nb_bytes;
787 return l_skip_nb_bytes;
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.
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)
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;
804 (p_stream->m_status & opj_stream_e_error)
806 return (OPJ_OFF_T) -1;
809 /* we should flush data */
810 l_is_written = opj_stream_flush (p_stream, p_event_mgr);
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;
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);
827 (l_current_skip_nb_bytes == (OPJ_OFF_T)-1)
829 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream error!\n");
831 p_stream->m_status |= opj_stream_e_error;
832 p_stream->m_byte_offset += l_skip_nb_bytes;
834 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T)-1;
836 p_size -= l_current_skip_nb_bytes;
837 l_skip_nb_bytes += l_current_skip_nb_bytes;
839 p_stream->m_byte_offset += l_skip_nb_bytes;
840 return l_skip_nb_bytes;
844 * Tells the byte offset on the stream (similar to ftell).
846 * @param p_stream the stream to get the information from.
848 * @return the current position of the stream.
850 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream)
852 return p_stream->m_byte_offset;
857 * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
859 * @param p_stream the stream to get the information from.
861 * @return Number of bytes left before the end of the stream.
863 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream)
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 :
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.
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)
882 return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
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.
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)
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;
899 if( p_stream->m_seek_fn(p_size,p_stream->m_user_data)) {
900 p_stream->m_status |= opj_stream_e_end;
904 /* reset stream status */
905 p_stream->m_status &= (~opj_stream_e_end);
906 p_stream->m_byte_offset = p_size;
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.
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)
923 (! opj_stream_flush(p_stream,p_event_mgr))
925 p_stream->m_status |= opj_stream_e_error;
929 p_stream->m_current_data = p_stream->m_stored_data;
930 p_stream->m_bytes_in_buffer = 0;
933 (! p_stream->m_seek_fn(p_size,p_stream->m_user_data))
935 p_stream->m_status |= opj_stream_e_error;
940 p_stream->m_byte_offset = p_size;
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.
953 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
956 return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
960 * Tells if the given stream is seekable.
962 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream)
964 return p_stream->m_seek_fn != opj_stream_default_seek;
967 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
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;
975 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
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;
983 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data)
985 OPJ_ARG_NOT_USED(p_nb_bytes);
986 OPJ_ARG_NOT_USED(p_user_data);
987 return (OPJ_OFF_T) -1;
990 opj_bool opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
992 OPJ_ARG_NOT_USED(p_nb_bytes);
993 OPJ_ARG_NOT_USED(p_user_data);