--- /dev/null
+/*
+ * The copyright in this software is being made available under the 2-clauses
+ * BSD License, included below. This software may be subject to other third
+ * party and contributor rights, including patent rights, and no such rights
+ * are granted under this license.
+ *
+ * Copyright (c) 2015, Matthieu Darbois
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef OPJ_STRING_H
+#define OPJ_STRING_H
+
+#include <errno.h>
+#include <string.h>
+
+/* strnlen is not standard, strlen_s is C11... */
+/* keep in mind there still is a buffer read overflow possible */
+static size_t opj_strnlen_s(const char *src, size_t max_len)
+{
+ size_t len;
+
+ if (src == NULL) {
+ return 0U;
+ }
+ for (len = 0U; (*src != '\0') && (len < max_len); src++, len++);
+ return len;
+}
+
+/* should be equivalent to C11 function except for the handler */
+/* keep in mind there still is a buffer read overflow possible */
+static int opj_strcpy_s(char* dst, size_t dst_size, const char* src)
+{
+ size_t src_len = 0U;
+ if ((dst == NULL) || (dst_size == 0U)) {
+ return EINVAL;
+ }
+ if (src == NULL) {
+ dst[0] = '\0';
+ return EINVAL;
+ }
+ src_len = opj_strnlen_s(src, dst_size);
+ if (src_len >= dst_size) {
+ return ERANGE;
+ }
+ memcpy(dst, src, src_len);
+ dst[src_len] = '\0';
+ return 0;
+}
+
+#endif /* OPJ_STRING_H */
${OPENJPEG_SOURCE_DIR}/src/bin/common/color.h
${OPENJPEG_SOURCE_DIR}/src/bin/common/opj_getopt.c
${OPENJPEG_SOURCE_DIR}/src/bin/common/opj_getopt.h
+ ${OPENJPEG_SOURCE_DIR}/src/bin/common/opj_string.h
)
if(OPJ_HAVE_LIBTIFF)
return 0;
}
- if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image))
+ if (!tga_readheader(f, &pixel_bit_depth, &image_width, &image_height, &flip_image)) {
+ fclose(f);
return NULL;
+ }
/* We currently only support 24 & 32 bit tga's ... */
- if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32)))
+ if (!((pixel_bit_depth == 24) || (pixel_bit_depth == 32))) {
+ fclose(f);
return NULL;
+ }
/* initialize image components */
memset(&cmptparm[0], 0, 4 * sizeof(opj_image_cmptparm_t));
/* create the image */
image = opj_image_create((OPJ_UINT32)numcomps, &cmptparm[0], color_space);
- if (!image)
+ if (!image) {
+ fclose(f);
return NULL;
+ }
+
/* set image offset and reference grid */
image->x0 = (OPJ_UINT32)parameters->image_offset_x0;
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&g, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&r, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&g, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&r, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if ( !fread(&a, 1, 1, f) )
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(f);
return NULL;
}
fprintf(stderr, "Currently unsupported bit depth : %s\n", filename);
}
}
+ fclose(f);
return image;
}
if ((image->comps[0].dx != image->comps[i+1].dx)
||(image->comps[0].dy != image->comps[i+1].dy)
||(image->comps[0].prec != image->comps[i+1].prec)) {
+ fclose(fdest);
fprintf(stderr, "Unable to create a tga file with such J2K image charateristics.");
return 1;
}
fseek(f, 0, SEEK_SET);
if( fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h) != 9){
+ fclose(f);
fprintf(stderr, "ERROR: Failed to read the right number of element from the fscanf() function!\n");
return NULL;
}
} else if (endian2=='M' && endian1=='L') {
bigendian = 0;
} else {
+ fclose(f);
fprintf(stderr, "Bad pgx header, please check input file\n");
return NULL;
}
FILE *fdest = NULL;
for (compno = 0; compno < image->numcomps; compno++)
- {
+ {
opj_image_comp_t *comp = &image->comps[compno];
char bname[256]; /* buffer for name */
char *name = bname; /* pointer */
const size_t total = dotpos + 1 + 1 + 4; /* '-' + '[1-3]' + '.pgx' */
if( outfile[dotpos] != '.' )
- {
+ {
/* `pgx` was recognized but there is no dot at expected position */
fprintf(stderr, "ERROR -> Impossible happen." );
goto fin;
- }
+ }
if( total > 256 )
- {
+ {
name = (char*)malloc(total+1);
- }
+ if (name == NULL) {
+ goto fin;
+ }
+ }
strncpy(name, outfile, dotpos);
sprintf(name+dotpos, "_%d.pgx", compno);
fdest = fopen(name, "wb");
/* dont need name anymore */
- if( total > 256 ) free(name);
+
if (!fdest)
- {
+ {
+
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
+ if( total > 256 ) free(name);
goto fin;
- }
+ }
w = (int)image->comps[compno].w;
h = (int)image->comps[compno].h;
nbytes = 4;
for (i = 0; i < w * h; i++)
- {
+ {
/* FIXME: clamp func is being called within a loop */
const int val = clamp(image->comps[compno].data[i],
(int)comp->prec, (int)comp->sgnd);
for (j = nbytes - 1; j >= 0; j--)
- {
+ {
int v = (int)(val >> (j * 8));
unsigned char byte = (unsigned char)v;
res = fwrite(&byte, 1, 1, fdest);
if( res < 1 )
- {
+ {
fprintf(stderr, "failed to write 1 byte for %s\n", name);
+ if( total > 256 ) free(name);
goto fin;
- }
- }
- }
+ }
+ }
+ }
+ if( total > 256 ) free(name);
fclose(fdest); fdest = NULL;
- }
+ }
fails = 0;
fin:
if(fdest) fclose(fdest);
{
fprintf(stderr, "\nError: fread return a number of element different from the expected.\n");
opj_image_destroy(image);
+ fclose(fp);
return NULL;
}
if(one)
if (!cmptparm) {
fprintf(stderr, "Failed to allocate image components parameters !!\n");
fprintf(stderr,"Aborting\n");
+ fclose(f);
return NULL;
}
/* initialize image components */
for (i = 0; i < nloop; i++) {
if (!fread(&value, 1, 1, f)) {
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
image->comps[compno].data[i] = raw_cp->rawSigned?(char)value:value;
unsigned char temp2;
if (!fread(&temp1, 1, 1, f)) {
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if (!fread(&temp2, 1, 1, f)) {
fprintf(stderr,"Error reading raw file. End of file probably reached.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
if( big_endian )
}
else {
fprintf(stderr,"OpenJPEG cannot encode raw components with bit depth higher than 16 bits.\n");
+ opj_image_destroy(image);
+ fclose(f);
return NULL;
}
image = opj_image_create(numcmpts, &cmptparm[0], (numcmpts == 1U) ? OPJ_CLRSPC_GRAY : OPJ_CLRSPC_SRGB);
if(!image) {
fclose(IN);
+ free(pData);
return NULL;
}
if (numcmpts == 4U) {
<<-- <<-- <<-- <<-- */
fdest = fopen(outfile, "wb");
+ if (!fdest) {
+ fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
+ return 1;
+ }
w = (int)image->comps[0].w;
h = (int)image->comps[0].h;
}
fclose(writer);
- if(fails) remove(write_idf);
+ if(fails) (void)remove(write_idf); /* ignore return value */
return fails;
}/* imagetopng() */
#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;
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) {
}
}
}
- 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");
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"
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 */
/* 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) {
+ if(parse_cmdline_encoder(argc, argv, ¶meters,&img_fol, &raw_cp, indexfilename, sizeof(indexfilename)) == 1) {
return 1;
}
#include "color.h"
#include "format_defs.h"
+#include "opj_string.h"
typedef struct dircnt{
/** Buffer for holding images read from Directory*/
int decod_format;
/** output file format 0: PGX, 1: PxM, 2: BMP */
int cod_format;
+ /** index file name */
+ char indexfilename[OPJ_PATH_LEN];
/** Decoding area left boundary */
OPJ_UINT32 DA_x0;
char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_decompress_parameters *parameters);
static int infile_format(const char *fname);
-int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol, char *indexfilename);
+int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol);
int parse_DA_values( char* inArg, unsigned int *DA_x0, unsigned int *DA_y0, unsigned int *DA_x1, unsigned int *DA_y1);
static opj_image_t* convert_gray_to_rgb(opj_image_t* original);
continue;
num_images++;
}
+ closedir(dir);
return num_images;
}
strcpy(dirptr->filename[i],content->d_name);
i++;
}
+ closedir(dir);
return 0;
}
parameters->decod_format = infile_format(infilename);
if (parameters->decod_format == -1)
return 1;
- 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,strtok(image_filename,"."));
}
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;
}
* Parse the command line
*/
/* -------------------------------------------------------------------------- */
-int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol, char *indexfilename) {
+int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol) {
/* parse the command line */
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) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
}
break;
fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile);
return 1;
}
- strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), outfile) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
}
break;
case 'd': /* Input decode ROI */
{
- int size_optarg = (int)strlen(opj_optarg) + 1;
- char *ROI_values = (char*) malloc((size_t)size_optarg);
+ size_t size_optarg = (size_t)strlen(opj_optarg) + 1U;
+ char *ROI_values = (char*) malloc(size_optarg);
+ if (ROI_values == NULL) {
+ fprintf(stderr, "[ERROR] Couldn't allocate memory\n");
+ return 1;
+ }
ROI_values[0] = '\0';
- strncpy(ROI_values, opj_optarg, strlen(opj_optarg));
- ROI_values[strlen(opj_optarg)] = '\0';
+ memcpy(ROI_values, opj_optarg, size_optarg);
/*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */
parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1);
case 'x': /* Creation of index file */
{
- char *index = opj_optarg;
- strncpy(indexfilename, index, OPJ_PATH_LEN);
+ if (opj_strcpy_s(parameters->indexfilename, sizeof(parameters->indexfilename), opj_optarg) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
}
break;
opj_codec_t* l_codec = NULL; /* Handle to a decompressor */
opj_codestream_index_t* cstr_index = NULL;
- char indexfilename[OPJ_PATH_LEN]; /* index file name */
-
OPJ_INT32 num_images, imageno;
img_fol_t img_fol;
dircnt_t *dirptr = NULL;
/* set decoding parameters to default values */
set_default_parameters(¶meters);
- /* FIXME Initialize indexfilename and img_fol */
- *indexfilename = 0;
-
/* Initialize img_fol */
memset(&img_fol,0,sizeof(img_fol_t));
/* parse input and get user encoding parameters */
- if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol, indexfilename) == 1) {
+ if(parse_cmdline_decoder(argc, argv, ¶meters,&img_fol) == 1) {
destroy_parameters(¶meters);
return EXIT_FAILURE;
}
/* destroy the codestream index */
opj_destroy_cstr_index(&cstr_index);
- if(failed) remove(parameters.outfile);
+ if(failed) (void)remove(parameters.outfile); /* ignore return value */
}
destroy_parameters(¶meters);
if (numDecompressedImages) {
#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,strtok(image_filename,"."));
}
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;
}
infile);
return 1;
}
- strncpy(parameters->infile, infile, sizeof(parameters->infile)-1);
+ if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
}
break;
case 'o': /* output file */
{
- char *outfile = opj_optarg;
- strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1);
+ if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile), opj_optarg) != 0) {
+ fprintf(stderr, "[ERROR] Path is too long\n");
+ return 1;
+ }
}
break;