* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
+ * Copyright (c) 2008;2011-2012, Centre National d'Etudes Spatiales (CNES), France
+ * Copyright (c) 2012, CS Systemes d'Information, France
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
/** @name Local static functions */
/*@{*/
-static void t2_putcommacode(opj_bio_t *bio, int n);
+static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n);
static OPJ_UINT32 opj_t2_getcommacode(opj_bio_t *bio);
/**
/* ----------------------------------------------------------------------- */
/* #define RESTART 0x04 */
-/* TODO MSD->LHE */
-static void t2_putcommacode(opj_bio_t *bio, int n) {
+static void opj_t2_putcommacode(opj_bio_t *bio, OPJ_INT32 n) {
while (--n >= 0) {
opj_bio_write(bio, 1, 1);
}
/* don't forget to release pi */
opj_pi_destroy(l_pi,l_nb_pocs);
- *p_data_read = l_current_data - p_src;
+ assert( l_current_data - p_src < UINT32_MAX );
+ *p_data_read = (OPJ_UINT32)(l_current_data - p_src);
return OPJ_TRUE;
}
opj_codestream_info_t *cstr_info)
{
OPJ_UINT32 bandno, cblkno;
- OPJ_BYTE *c = dest;
+ OPJ_BYTE* c = dest;
OPJ_UINT32 l_nb_bytes;
- OPJ_UINT32 compno = pi->compno; /* component value */
- OPJ_UINT32 resno = pi->resno; /* resolution level value */
- OPJ_UINT32 precno = pi->precno; /* precinct value */
- OPJ_UINT32 layno = pi->layno; /* quality layer value */
+ OPJ_UINT32 compno = pi->compno; /* component value */
+ OPJ_UINT32 resno = pi->resno; /* resolution level value */
+ OPJ_UINT32 precno = pi->precno; /* precinct value */
+ OPJ_UINT32 layno = pi->layno; /* quality layer value */
OPJ_UINT32 l_nb_blocks;
opj_tcd_band_t *band = 00;
opj_tcd_cblk_enc_t* cblk = 00;
c[1] = 145;
c[2] = 0;
c[3] = 4;
+#if 0
c[4] = (tile->packno % 65536) / 256;
c[5] = (tile->packno % 65536) % 256;
+#else
+ c[4] = (tile->packno >> 8) & 0xff; /* packno is uint32_t */
+ c[5] = tile->packno & 0xff;
+#endif
c += 6;
length -= 6;
}
++pass;
}
- t2_putcommacode(bio, increment);
+ opj_t2_putcommacode(bio, increment);
/* computation of the new Length indicator */
cblk->numlenbits += increment;
return OPJ_FALSE; /* modified to eliminate longjmp !! */
}
- l_nb_bytes = opj_bio_numbytes(bio);
+ l_nb_bytes = (OPJ_UINT32)opj_bio_numbytes(bio);
c += l_nb_bytes;
length -= l_nb_bytes;
++band;
}
- * p_data_written += (c - dest);
+ assert( c >= dest );
+ * p_data_written += (OPJ_UINT32)(c - dest);
return OPJ_TRUE;
}
else { /* Normal Case */
l_header_data_start = &(l_current_data);
l_header_data = *l_header_data_start;
- l_remaining_length = p_src_data+p_max_length-l_header_data;
+ l_remaining_length = (OPJ_UINT32)(p_src_data+p_max_length-l_header_data);
l_modified_length_ptr = &(l_remaining_length);
}
/* EPH markers */
if (p_tcp->csty & J2K_CP_CSTY_EPH) {
- if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
+ if (p_max_length < 2) {
+ fprintf(stderr, "Not enough space for expected EPH marker\n");
+ } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
printf("Error : expected EPH marker\n");
} else {
l_header_data += 2;
}
}
- l_header_length = (l_header_data - *l_header_data_start);
+ l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
*l_modified_length_ptr -= l_header_length;
*l_header_data_start += l_header_length;
/* INDEX >> */
* p_is_data_present = OPJ_FALSE;
- *p_data_read = l_current_data - p_src_data;
+ *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
return OPJ_TRUE;
}
/* EPH markers */
if (p_tcp->csty & J2K_CP_CSTY_EPH) {
- if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
+ if (p_max_length < 2) {
+ fprintf(stderr, "Not enough space for expected EPH marker\n");
+ } else if ((*l_header_data) != 0xff || (*(l_header_data + 1) != 0x92)) {
/* TODO opj_event_msg(t2->cinfo->event_mgr, EVT_ERROR, "Expected EPH marker\n"); */
} else {
l_header_data += 2;
}
}
- l_header_length = (l_header_data - *l_header_data_start);
+ l_header_length = (OPJ_UINT32)(l_header_data - *l_header_data_start);
*l_modified_length_ptr -= l_header_length;
*l_header_data_start += l_header_length;
/* INDEX >> */
*p_is_data_present = OPJ_TRUE;
- *p_data_read = l_current_data - p_src_data;
+ *p_data_read = (OPJ_UINT32)(l_current_data - p_src_data);
return OPJ_TRUE;
}
if (!l_cblk->numsegs) {
l_seg = l_cblk->segs;
++l_cblk->numsegs;
- l_cblk->len = 0;
+ l_cblk->data_current_size = 0;
}
else {
l_seg = &l_cblk->segs[l_cblk->numsegs - 1];
};
#endif /* USE_JPWL */
-
- if ((l_cblk->len + l_seg->newlen) > 8192) {
+ /* Check if the cblk->data have allocated enough memory */
+ if ((l_cblk->data_current_size + l_seg->newlen) > l_cblk->data_max_size) {
+ OPJ_BYTE* new_cblk_data = (OPJ_BYTE*) opj_realloc(l_cblk->data, l_cblk->data_current_size + l_seg->newlen);
+ if(! new_cblk_data) {
+ opj_free(l_cblk->data);
+ l_cblk->data_max_size = 0;
+ /* opj_event_msg(p_manager, EVT_ERROR, "Not enough memory to realloc code block cata!\n"); */
return OPJ_FALSE;
+ }
+ l_cblk->data_max_size = l_cblk->data_current_size + l_seg->newlen;
+ l_cblk->data = new_cblk_data;
}
- memcpy(l_cblk->data + l_cblk->len, l_current_data, l_seg->newlen);
+ memcpy(l_cblk->data + l_cblk->data_current_size, l_current_data, l_seg->newlen);
if (l_seg->numpasses == 0) {
l_seg->data = &l_cblk->data;
- l_seg->dataindex = l_cblk->len;
+ l_seg->dataindex = l_cblk->data_current_size;
}
l_current_data += l_seg->newlen;
l_cblk->numnewpasses -= l_seg->numnewpasses;
l_seg->real_num_passes = l_seg->numpasses;
- l_cblk->len += l_seg->newlen;
+ l_cblk->data_current_size += l_seg->newlen;
l_seg->len += l_seg->newlen;
if (l_cblk->numnewpasses > 0) {
l_cblk->real_num_segs = l_cblk->numsegs;
++l_cblk;
- }
+ } /* next code_block */
++l_band;
}
- *(p_data_read) = l_current_data - p_src_data;
+ *(p_data_read) = (OPJ_UINT32)(l_current_data - p_src_data);
return OPJ_TRUE;
}
if (!l_cblk->numsegs) {
l_seg = l_cblk->segs;
++l_cblk->numsegs;
- l_cblk->len = 0;
+ l_cblk->data_current_size = 0;
}
else {
l_seg = &l_cblk->segs[l_cblk->numsegs - 1];