- opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
-
- if (!l_stream) {
- return;
- }
- l_stream->m_seek_fn = p_function;
-}
-
-void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function)
-{
- opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
-
- if ((!l_stream )|| (! (l_stream->m_status & OPJ_STREAM_STATUS_OUTPUT))) {
- return;
- }
-
- l_stream->m_write_fn = p_function;
-}
-
-void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function)
-{
- opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
-
- if (! l_stream) {
- return;
- }
-
- l_stream->m_skip_fn = p_function;
-}
-
-void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data, opj_stream_free_user_data_fn p_function)
-{
- opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
- if (!l_stream)
- return;
- l_stream->m_user_data = p_data;
- l_stream->m_free_user_data_fn = p_function;
-}
-
-void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length)
-{
- opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
- if (!l_stream)
- return;
- l_stream->m_user_data_length = data_length;
-}
-
-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)
-{
- OPJ_SIZE_T l_read_nb_bytes = 0;
- if (p_stream->m_bytes_in_buffer >= p_size) {
- memcpy(p_buffer,p_stream->m_current_data,p_size);
- p_stream->m_current_data += p_size;
- p_stream->m_bytes_in_buffer -= p_size;
- l_read_nb_bytes += p_size;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
- return l_read_nb_bytes;
- }
-
- /* we are now in the case when the remaining data if not sufficient */
- if (p_stream->m_status & OPJ_STREAM_STATUS_END) {
- l_read_nb_bytes += p_stream->m_bytes_in_buffer;
- memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
- p_stream->m_current_data += p_stream->m_bytes_in_buffer;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_bytes_in_buffer = 0;
- return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
- }
-
- /* the flag is not set, we copy data and then do an actual read on the stream */
- if (p_stream->m_bytes_in_buffer) {
- l_read_nb_bytes += p_stream->m_bytes_in_buffer;
- memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
- p_stream->m_current_data = p_stream->m_stored_data;
- p_buffer += p_stream->m_bytes_in_buffer;
- p_size -= p_stream->m_bytes_in_buffer;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_bytes_in_buffer = 0;
- }
- else {
- /* case where we are already at the end of the buffer
- so reset the m_current_data to point to the start of the
- stored buffer to get ready to read from disk*/
- p_stream->m_current_data = p_stream->m_stored_data;
- }
-
- for (;;) {
- /* we should read less than a chunk -> read a chunk */
- if (p_size < p_stream->m_buffer_size) {
- /* we should do an actual read on the media */
- 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);
-
- if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
- /* end of stream */
- opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
-
- p_stream->m_bytes_in_buffer = 0;
- p_stream->m_status |= OPJ_STREAM_STATUS_END;
- /* end of stream */
- return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
- }
- else if (p_stream->m_bytes_in_buffer < p_size) {
- /* not enough data */
- l_read_nb_bytes += p_stream->m_bytes_in_buffer;
- memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
- p_stream->m_current_data = p_stream->m_stored_data;
- p_buffer += p_stream->m_bytes_in_buffer;
- p_size -= p_stream->m_bytes_in_buffer;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_bytes_in_buffer = 0;
- }
- else {
- l_read_nb_bytes += p_size;
- memcpy(p_buffer,p_stream->m_current_data,p_size);
- p_stream->m_current_data += p_size;
- p_stream->m_bytes_in_buffer -= p_size;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
- return l_read_nb_bytes;
- }
- }
- else {
- /* direct read on the dest buffer */
- p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_buffer,p_size,p_stream->m_user_data);
-
- if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
- /* end of stream */
- opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
-
- p_stream->m_bytes_in_buffer = 0;
- p_stream->m_status |= OPJ_STREAM_STATUS_END;
- /* end of stream */
- return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
- }
- else if (p_stream->m_bytes_in_buffer < p_size) {
- /* not enough data */
- l_read_nb_bytes += p_stream->m_bytes_in_buffer;
- p_stream->m_current_data = p_stream->m_stored_data;
- p_buffer += p_stream->m_bytes_in_buffer;
- p_size -= p_stream->m_bytes_in_buffer;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_bytes_in_buffer = 0;
- }
- else {
- /* we have read the exact size */
- l_read_nb_bytes += p_stream->m_bytes_in_buffer;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_current_data = p_stream->m_stored_data;
- p_stream->m_bytes_in_buffer = 0;
- return l_read_nb_bytes;
- }
- }
- }
-}
-
-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)
-{
- OPJ_SIZE_T l_remaining_bytes = 0;
- OPJ_SIZE_T l_write_nb_bytes = 0;
-
- if (p_stream->m_status & OPJ_STREAM_STATUS_ERROR) {
- return (OPJ_SIZE_T)-1;
- }
-
- for (;;) {
- l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
-
- /* we have more memory than required */
- if (l_remaining_bytes >= p_size) {
- memcpy(p_stream->m_current_data, p_buffer, p_size);
-
- p_stream->m_current_data += p_size;
- p_stream->m_bytes_in_buffer += p_size;
- l_write_nb_bytes += p_size;
- p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
-
- return l_write_nb_bytes;
- }
-
- /* we copy data and then do an actual read on the stream */
- if (l_remaining_bytes) {
- l_write_nb_bytes += l_remaining_bytes;
-
- memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes);
-
- p_stream->m_current_data = p_stream->m_stored_data;
-
- p_buffer += l_remaining_bytes;
- p_size -= l_remaining_bytes;
- p_stream->m_bytes_in_buffer += l_remaining_bytes;
- p_stream->m_byte_offset += (OPJ_OFF_T)l_remaining_bytes;
- }
-
- if (! opj_stream_flush(p_stream, p_event_mgr)) {
- return (OPJ_SIZE_T)-1;
- }
- }
-
-}
-
-OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
-{
- /* the number of bytes written on the media. */
- OPJ_SIZE_T l_current_write_nb_bytes = 0;
-
- p_stream->m_current_data = p_stream->m_stored_data;
-
- while (p_stream->m_bytes_in_buffer) {
- /* we should do an actual write on the media */
- 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);
-
- if (l_current_write_nb_bytes == (OPJ_SIZE_T)-1) {
- p_stream->m_status |= OPJ_STREAM_STATUS_ERROR;
- opj_event_msg(p_event_mgr, EVT_INFO, "Error on writing stream!\n");
-
- return OPJ_FALSE;
- }
-
- p_stream->m_current_data += l_current_write_nb_bytes;
- p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
- }
-
- p_stream->m_current_data = p_stream->m_stored_data;
-
- return OPJ_TRUE;
-}
-
-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)
-{
- OPJ_OFF_T l_skip_nb_bytes = 0;
- OPJ_OFF_T l_current_skip_nb_bytes = 0;
-
- assert( p_size >= 0 );
-
- if (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size) {
- p_stream->m_current_data += p_size;
- /* it is safe to cast p_size to OPJ_SIZE_T since it is <= m_bytes_in_buffer
- which is of type OPJ_SIZE_T */
- p_stream->m_bytes_in_buffer -= (OPJ_SIZE_T)p_size;
- l_skip_nb_bytes += p_size;
- p_stream->m_byte_offset += l_skip_nb_bytes;
- return l_skip_nb_bytes;
- }
-
- /* we are now in the case when the remaining data if not sufficient */
- if (p_stream->m_status & OPJ_STREAM_STATUS_END) {
- l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_current_data += p_stream->m_bytes_in_buffer;
- p_stream->m_bytes_in_buffer = 0;
- p_stream->m_byte_offset += l_skip_nb_bytes;
- return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
- }
-
- /* the flag is not set, we copy data and then do an actual skip on the stream */
- if (p_stream->m_bytes_in_buffer) {
- l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_current_data = p_stream->m_stored_data;
- p_size -= (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
- p_stream->m_bytes_in_buffer = 0;
- }
-
- while (p_size > 0) {
- /* we should do an actual skip on the media */
- l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
- if (l_current_skip_nb_bytes == (OPJ_OFF_T) -1) {
- opj_event_msg(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
-
- p_stream->m_status |= OPJ_STREAM_STATUS_END;
- p_stream->m_byte_offset += l_skip_nb_bytes;
- /* end if stream */
- return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
- }
- p_size -= l_current_skip_nb_bytes;
- l_skip_nb_bytes += l_current_skip_nb_bytes;
- }
-
- p_stream->m_byte_offset += l_skip_nb_bytes;
-
- return l_skip_nb_bytes;
-}
-
-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)
-{
- OPJ_BOOL l_is_written = 0;
- OPJ_OFF_T l_current_skip_nb_bytes = 0;
- OPJ_OFF_T l_skip_nb_bytes = 0;