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,
644 const OPJ_BYTE * p_buffer,
646 opj_event_mgr_t * p_event_mgr)
648 OPJ_SIZE_T l_remaining_bytes = 0;
649 OPJ_SIZE_T l_write_nb_bytes = 0;
651 if (p_stream->m_status & opj_stream_e_error) {
652 return (OPJ_SIZE_T)-1;
656 l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
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);
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;
667 return l_write_nb_bytes;
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;
674 memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes);
676 p_stream->m_current_data = p_stream->m_stored_data;
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;
684 if (opj_stream_flush(p_stream, p_event_mgr) == EXIT_FAILURE) {
685 return (OPJ_SIZE_T)-1;
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.
697 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
699 /* the number of bytes written on the media. */
700 OPJ_SIZE_T l_current_write_nb_bytes = 0;
702 p_stream->m_current_data = p_stream->m_stored_data;
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);
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");
717 p_stream->m_current_data += l_current_write_nb_bytes;
718 p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
721 p_stream->m_current_data = p_stream->m_stored_data;
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.
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)
735 OPJ_OFF_T l_skip_nb_bytes = 0;
736 OPJ_OFF_T l_current_skip_nb_bytes = 0;
738 assert( p_size >= 0 );
740 (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size)
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;
751 /* we are now in the case when the remaining data if not sufficient */
753 (p_stream->m_status & opj_stream_e_end)
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;
762 /* the flag is not set, we copy data and then do an actual skip on the stream */
764 (p_stream->m_bytes_in_buffer)
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;
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);
778 (l_current_skip_nb_bytes == (OPJ_OFF_T) -1)
780 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
782 p_stream->m_status |= opj_stream_e_end;
783 p_stream->m_byte_offset += l_skip_nb_bytes;
785 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
787 p_size -= l_current_skip_nb_bytes;
788 l_skip_nb_bytes += l_current_skip_nb_bytes;
790 p_stream->m_byte_offset += l_skip_nb_bytes;
791 return l_skip_nb_bytes;
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.
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)
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;
808 (p_stream->m_status & opj_stream_e_error)
810 return (OPJ_OFF_T) -1;
813 /* we should flush data */
814 l_is_written = opj_stream_flush (p_stream, p_event_mgr);
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;
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);
831 (l_current_skip_nb_bytes == (OPJ_OFF_T)-1)
833 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream error!\n");
835 p_stream->m_status |= opj_stream_e_error;
836 p_stream->m_byte_offset += l_skip_nb_bytes;
838 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T)-1;
840 p_size -= l_current_skip_nb_bytes;
841 l_skip_nb_bytes += l_current_skip_nb_bytes;
843 p_stream->m_byte_offset += l_skip_nb_bytes;
844 return l_skip_nb_bytes;
848 * Tells the byte offset on the stream (similar to ftell).
850 * @param p_stream the stream to get the information from.
852 * @return the current position of the stream.
854 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream)
856 return p_stream->m_byte_offset;
861 * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
863 * @param p_stream the stream to get the information from.
865 * @return Number of bytes left before the end of the stream.
867 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream)
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 :
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.
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)
886 return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
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.
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)
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;
903 if( p_stream->m_seek_fn(p_size,p_stream->m_user_data)) {
904 p_stream->m_status |= opj_stream_e_end;
908 /* reset stream status */
909 p_stream->m_status &= (~opj_stream_e_end);
910 p_stream->m_byte_offset = p_size;
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.
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)
927 (! opj_stream_flush(p_stream,p_event_mgr))
929 p_stream->m_status |= opj_stream_e_error;
933 p_stream->m_current_data = p_stream->m_stored_data;
934 p_stream->m_bytes_in_buffer = 0;
937 (! p_stream->m_seek_fn(p_size,p_stream->m_user_data))
939 p_stream->m_status |= opj_stream_e_error;
944 p_stream->m_byte_offset = p_size;
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.
957 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
960 return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
964 * Tells if the given stream is seekable.
966 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream)
968 return p_stream->m_seek_fn != opj_stream_default_seek;
971 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
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;
979 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
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;
987 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data)
989 OPJ_ARG_NOT_USED(p_nb_bytes);
990 OPJ_ARG_NOT_USED(p_user_data);
991 return (OPJ_OFF_T) -1;
994 opj_bool opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
996 OPJ_ARG_NOT_USED(p_nb_bytes);
997 OPJ_ARG_NOT_USED(p_user_data);