#include "index.h"
#include "format_defs.h"
+#include "opj_string.h"
typedef struct dircnt{
/** Buffer for holding images read from Directory*/
continue;
num_images++;
}
+ closedir(dir);
return num_images;
}
strcpy(dirptr->filename[i],content->d_name);
i++;
}
+ closedir(dir);
return 0;
}
if (parameters->decod_format == -1)
return 1;
sprintf(infilename,"%s/%s",img_fol->imgdirpath,image_filename);
- strncpy(parameters->infile, infilename, sizeof(infilename));
-
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infilename) != 0) {
+ return 1;
+ }
+
/*Set output file*/
strcpy(temp_ofname,get_file_name(image_filename));
while((temp_p = strtok(NULL,".")) != NULL){
}
if(img_fol->set_out_format==1){
sprintf(outfilename,"%s/%s.%s",img_fol->imgdirpath,temp_ofname,img_fol->out_format);
- strncpy(parameters->outfile, outfilename, sizeof(outfilename));
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfilename) != 0) {
+ return 1;
+ }
}
return 0;
}
/* ------------------------------------------------------------------------------------ */
static int parse_cmdline_encoder(int argc, char **argv, opj_cparameters_t *parameters,
- img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename) {
+ img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename, size_t indexfilename_size) {
OPJ_UINT32 i, j;
int totlen, c;
opj_option_t long_option[]={
infile);
return 1;
}
- strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
+ return 1;
+ }
}
break;
fprintf(stderr, "Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
return 1;
}
- strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) {
+ return 1;
+ }
}
break;
char signo;
int width,height,bitdepth,ncomp;
OPJ_UINT32 len;
- OPJ_BOOL raw_signed;
+ OPJ_BOOL raw_signed = OPJ_FALSE;
substr2 = strchr(opj_optarg,'@');
if (substr2 == NULL) {
len = (OPJ_UINT32) strlen(opj_optarg);
substr2++; /* skip '@' character */
}
substr1 = (char*) malloc((len+1)*sizeof(char));
+ if (substr1 == NULL) {
+ return 1;
+ }
memcpy(substr1,opj_optarg,len);
substr1[len] = '\0';
if (sscanf(substr1, "%d,%d,%d,%d,%c", &width, &height, &ncomp, &bitdepth, &signo) == 5) {
wrong = OPJ_TRUE;
}
if (!wrong) {
- int i;
+ int compno;
int lastdx = 1;
int lastdy = 1;
raw_cp->rawWidth = width;
raw_cp->rawBitDepth = bitdepth;
raw_cp->rawSigned = raw_signed;
raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(ncomp))*sizeof(raw_comp_cparameters_t));
- for (i = 0; i < ncomp && !wrong; i++) {
+ if(raw_cp->rawComps == NULL){
+ free(substr1);
+ return 1;
+ }
+ for (compno = 0; compno < ncomp && !wrong; compno++) {
if (substr2 == NULL) {
- raw_cp->rawComps[i].dx = lastdx;
- raw_cp->rawComps[i].dy = lastdy;
+ raw_cp->rawComps[compno].dx = lastdx;
+ raw_cp->rawComps[compno].dy = lastdy;
} else {
int dx,dy;
sep = strchr(substr2,':');
if (sscanf(substr2, "%dx%d", &dx, &dy) == 2) {
lastdx = dx;
lastdy = dy;
- raw_cp->rawComps[i].dx = dx;
- raw_cp->rawComps[i].dy = dy;
+ raw_cp->rawComps[compno].dx = dx;
+ raw_cp->rawComps[compno].dy = dy;
substr2 = NULL;
} else {
wrong = OPJ_TRUE;
}
} else {
if (sscanf(substr2, "%dx%d:%s", &dx, &dy, substr2) == 3) {
- raw_cp->rawComps[i].dx = dx;
- raw_cp->rawComps[i].dy = dy;
+ raw_cp->rawComps[compno].dx = dx;
+ raw_cp->rawComps[compno].dy = dy;
} else {
wrong = OPJ_TRUE;
}
}
}
}
- if (substr1) free(substr1);
+ free(substr1);
if (wrong) {
fprintf(stderr,"\nError: invalid raw image parameters\n");
fprintf(stderr,"Please use the Format option -F:\n");
numresolution = (OPJ_UINT32)parameters->numresolution;
matrix_width = numresolution * 3;
parameters->cp_matrice = (int *) malloc(numlayers * matrix_width * sizeof(int));
+ if(parameters->cp_matrice == NULL){
+ return 1;
+ }
s = s + 2;
for (i = 0; i < numlayers; i++) {
case 'x': /* creation of index file */
{
- char *index = opj_optarg;
- strncpy(indexfilename, index, OPJ_PATH_LEN);
+ if (opj_strcpy_s(indexfilename, indexfilename_size, opj_optarg) != 0) {
+ return 1;
+ }
/* FIXME ADE INDEX >> */
fprintf(stderr,
"[WARNING] Index file generation is currently broken.\n"
case 'z': /* Image Directory path */
{
img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1);
+ if(img_fol->imgdirpath == NULL){
+ return 1;
+ }
strcpy(img_fol->imgdirpath,opj_optarg);
img_fol->set_imgdir=1;
}
lStrLen = (size_t)ftell(lFile);
fseek(lFile,0,SEEK_SET);
lMatrix = (char *) malloc(lStrLen + 1);
+ if (lMatrix == NULL) {
+ fclose(lFile);
+ return 1;
+ }
lStrFread = fread(lMatrix, 1, lStrLen, lFile);
fclose(lFile);
- if( lStrLen != lStrFread ) return 1;
+ if( lStrLen != lStrFread ) {
+ free(lMatrix);
+ return 1;
+ }
lMatrix[lStrLen] = 0;
lCurrentPtr = lMatrix;
lMctComp = lNbComp * lNbComp;
lTotalComp = lMctComp + lNbComp;
lSpace = (float *) malloc((size_t)lTotalComp * sizeof(float));
+ if(lSpace == NULL) {
+ free(lMatrix);
+ return 1;
+ }
lCurrentDoublePtr = lSpace;
for (i2=0;i2<lMctComp;++i2) {
lStrLen = strlen(lCurrentPtr) + 1;
/* we need to enable indexing */
if (!indexfilename || !*indexfilename) {
- strncpy(indexfilename, JPWL_PRIVATEINDEX_NAME, OPJ_PATH_LEN);
+ if (opj_strcpy_s(indexfilename, indexfilename_size, JPWL_PRIVATEINDEX_NAME) != 0) {
+ return 1;
+ }
}
/* search for different protection methods */
}
return 0;
+
}
/* -------------------------------------------------------------------------- */
/* parse input and get user encoding parameters */
parameters.tcp_mct = (char) 255; /* This will be set later according to the input image or the provided option */
- if(parse_cmdline_encoder(argc, argv, ¶meters,&img_fol, &raw_cp, indexfilename) == 1) {
- return 1;
+ if(parse_cmdline_encoder(argc, argv, ¶meters,&img_fol, &raw_cp, indexfilename, sizeof(indexfilename)) == 1) {
+ goto fails;
}
/* Read directory if necessary */
OPJ_BYTE *l_data;
OPJ_UINT32 l_data_size = 512*512*3;
l_data = (OPJ_BYTE*) calloc( 1,l_data_size);
- assert( l_data );
+ if(l_data == NULL){
+ goto fails;
+ }
for (i=0;i<l_nb_tiles;++i) {
if (! opj_write_tile(l_codec,i,l_data,l_data_size,l_stream)) {
fprintf(stderr, "ERROR -> test_tile_encoder: failed to write the tile %d!\n",i);
}
return 0;
+
+fails:
+ if(parameters.cp_comment) free(parameters.cp_comment);
+ if(parameters.cp_matrice) free(parameters.cp_matrice);
+ if(raw_cp.rawComps) free(raw_cp.rawComps);
+ if(img_fol.imgdirpath) free(img_fol.imgdirpath);
+ if(dirptr){
+ if(dirptr->filename_buf) free(dirptr->filename_buf);
+ if(dirptr->filename) free(dirptr->filename);
+ free(dirptr);
+ }
+ return 1;
}