opj_free(cio);
return NULL;
}
- cio->length = (unsigned int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
- cio->buffer = (unsigned char *)opj_malloc(cio->length);
+ cio->length = (int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
+ assert(cio->length >= 0);
+ cio->buffer = (unsigned char *)opj_malloc((OPJ_SIZE_T)cio->length);
if(!cio->buffer) {
opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
opj_free(cio);
/*
* Get position in byte stream.
*/
-int OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
+OPJ_OFF_T OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
return cio->bp - cio->start;
}
/*
* Number of bytes left before the end of the stream.
*/
-int cio_numbytesleft(opj_cio_t *cio) {
- return cio->end - cio->bp;
+OPJ_SIZE_T cio_numbytesleft(opj_cio_t *cio) {
+ const ptrdiff_t diff = cio->end - cio->bp;
+ assert( diff >= 0 );
+ return (OPJ_SIZE_T)diff;
}
/*
if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
return 0;
}
- return n;
+ assert( n >= 0 );
+ return (unsigned int)n;
}
/*
*/
unsigned int cio_read(opj_cio_t *cio, int n) {
int i;
- unsigned int v;
- v = 0;
+ unsigned int v = 0;
for (i = n - 1; i >= 0; i--) {
- v += cio_bytein(cio) << (i << 3);
+ const unsigned int c = cio_bytein(cio);
+ v += c << (i << 3);
}
return v;
}
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 += p_size;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
return l_read_nb_bytes;
}
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 += 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 : -1;
+ 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 */
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 += 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 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 == -1) {
+ if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
/* end of stream */
opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
p_stream->m_bytes_in_buffer = 0;
p_stream->m_status |= opj_stream_e_end;
/* end of stream */
- return l_read_nb_bytes ? l_read_nb_bytes : -1;
+ 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 */
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 += 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 {
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 += p_size;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
return l_read_nb_bytes;
}
}
/* 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 == -1) {
+ if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
/* end of stream */
opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
p_stream->m_bytes_in_buffer = 0;
p_stream->m_status |= opj_stream_e_end;
/* end of stream */
- return l_read_nb_bytes ? l_read_nb_bytes : -1;
+ 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 */
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 += 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 += 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;
if
(p_stream->m_status & opj_stream_e_error)
{
- return -1;
+ return (OPJ_SIZE_T)-1;
}
while(1)
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 += p_size;
+ p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
return l_write_nb_bytes;
}
p_buffer += l_remaining_bytes;
p_size -= l_remaining_bytes;
p_stream->m_bytes_in_buffer += l_remaining_bytes;
- p_stream->m_byte_offset += l_remaining_bytes;
+ p_stream->m_byte_offset += (OPJ_OFF_T)l_remaining_bytes;
}
if
(! opj_stream_flush(p_stream, p_event_mgr))
{
- return -1;
+ 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.
+ /* 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
+ /* 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 == -1)
+ (l_current_write_nb_bytes == (OPJ_SIZE_T)-1)
{
p_stream->m_status |= opj_stream_e_error;
opj_event_msg_v2(p_event_mgr, EVT_INFO, "Error on writting stream!\n");
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 >= p_size)
+ (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
+ /* 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
+ /* we are now in the case when the remaining data if not sufficient */
if
(p_stream->m_status & opj_stream_e_end)
{
- l_skip_nb_bytes += 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_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
+ /* 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 += 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 -= p_stream->m_bytes_in_buffer;
+ 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
+ /* 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)
p_stream->m_status |= opj_stream_e_end;
p_stream->m_byte_offset += l_skip_nb_bytes;
- // end if stream
+ /* end if stream */
return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
}
p_size -= l_current_skip_nb_bytes;
return (OPJ_OFF_T) -1;
}
- // we should flush data
+ /* we should flush data */
l_is_written = opj_stream_flush (p_stream, p_event_mgr);
if
(! l_is_written)
p_stream->m_current_data = p_stream->m_current_data;
return (OPJ_OFF_T) -1;
}
- // then skip
+ /* then skip */
while
(p_size > 0)
{
- // we should do an actual skip on the media
+ /* 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)
p_stream->m_status |= opj_stream_e_error;
p_stream->m_byte_offset += l_skip_nb_bytes;
- // end if stream
+ /* end if stream */
return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T)-1;
}
p_size -= l_current_skip_nb_bytes;
*/
OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream)
{
- return p_stream->m_user_data_length ?
- p_stream->m_user_data_length - p_stream->m_byte_offset :
+ assert( p_stream->m_byte_offset >= 0 );
+ assert( p_stream->m_user_data_length >= (OPJ_UINT64)p_stream->m_byte_offset);
+ return p_stream->m_user_data_length ?
+ (OPJ_OFF_T)(p_stream->m_user_data_length) - p_stream->m_byte_offset :
0;
}
return EXIT_FAILURE;
}
else {
- // reset stream status
+ /* reset stream status */
p_stream->m_status &= (~opj_stream_e_end);
p_stream->m_byte_offset = p_size;