Merge pull request #750 from mayeut/issue-729-2
[openjpeg.git] / src / bin / jp2 / opj_dump.c
index 7a841d536383b39aa88a9c01c25b0a2c331271fe..bd608c2b7efa8f7f55a2117f4bc174e9e81117dc 100644 (file)
@@ -57,6 +57,7 @@
 #include "index.h"
 
 #include "format_defs.h"
+#include "opj_string.h"
 
 typedef struct dircnt{
        /** Buffer for holding images read from Directory*/
@@ -91,19 +92,14 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param
 
 /* -------------------------------------------------------------------------- */
 static void decode_help_display(void) {
-       fprintf(stdout,"HELP for opj_dump\n----\n\n");
-       fprintf(stdout,"- the -h option displays this help information on screen\n\n");
-
-/* UniPG>> */
-       fprintf(stdout,"List of parameters for the JPEG 2000 "
-#ifdef USE_JPWL
-               "+ JPWL "
-#endif /* USE_JPWL */
-               "decoder:\n");
-/* <<UniPG */
-       fprintf(stdout,"\n");
-       fprintf(stdout,"\n");
-       fprintf(stdout,"  -ImgDir \n");
+    fprintf(stdout,"\nThis is the opj_dump utility from the OpenJPEG project.\n"
+            "It dumps JPEG 2000 codestream info to stdout or a given file.\n"
+            "It has been compiled against openjp2 library v%s.\n\n",opj_version());
+
+    fprintf(stdout,"Parameters:\n");
+    fprintf(stdout,"-----------\n");
+    fprintf(stdout,"\n");
+    fprintf(stdout,"  -ImgDir <directory>\n");
        fprintf(stdout,"        Image file Directory path \n");
        fprintf(stdout,"  -i <compressed file>\n");
        fprintf(stdout,"    REQUIRED only if an Input image directory not specified\n");
@@ -113,10 +109,10 @@ static void decode_help_display(void) {
        fprintf(stdout,"    OPTIONAL\n");
        fprintf(stdout,"    Output file where file info will be dump.\n");
        fprintf(stdout,"    By default it will be in the stdout.\n");
-       fprintf(stdout,"  -v "); /* FIXME WIP_MSD */
+    fprintf(stdout,"  -v "); /* FIXME WIP_MSD */
        fprintf(stdout,"    OPTIONAL\n");
-       fprintf(stdout,"    Activate or not the verbose mode (display info and warning message)\n");
-       fprintf(stdout,"    By default verbose mode is off.\n");
+    fprintf(stdout,"    Enable informative messages\n");
+    fprintf(stdout,"    By default verbose mode is off.\n");
        fprintf(stdout,"\n");
 }
 
@@ -139,6 +135,7 @@ static int get_num_images(char *imgdirpath){
                        continue;
                num_images++;
        }
+       closedir(dir);
        return num_images;
 }
 
@@ -165,6 +162,7 @@ static int load_images(dircnt_t *dirptr, char *imgdirpath){
                strcpy(dirptr->filename[i],content->d_name);
                i++;
        }
+       closedir(dir);
        return 0;       
 }
 
@@ -173,7 +171,7 @@ static int get_file_format(const char *filename) {
        unsigned int i;
        static const char *extension[] = {"pgx", "pnm", "pgm", "ppm", "bmp","tif", "raw", "tga", "png", "j2k", "jp2", "jpt", "j2c", "jpc"  };
        static const int format[] = { PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, RAW_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, JPT_CFMT, J2K_CFMT, J2K_CFMT };
-       char * ext = strrchr(filename, '.');
+       const char *ext = strrchr(filename, '.');
        if (ext == NULL)
                return -1;
        ext++;
@@ -199,7 +197,9 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_d
        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,"."));
@@ -209,7 +209,9 @@ static char get_next_file(int imageno,dircnt_t *dirptr,img_fol_t *img_fol, opj_d
        }
        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;
 }
@@ -277,9 +279,9 @@ static int infile_format(const char *fname)
 static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *parameters,img_fol_t *img_fol) {
        int totlen, c;
        opj_option_t long_option[]={
-               {"ImgDir",REQ_ARG, NULL ,'y'},
+        {"ImgDir",REQ_ARG, NULL ,'y'}
        };
-       const char optlist[] = "i:o:f:hv";
+    const char optlist[] = "i:o:f:hv";
 
        totlen=sizeof(long_option);
        img_fol->set_out_format = 0;
@@ -306,7 +308,10 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param
                             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;
 
@@ -314,8 +319,10 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param
 
                        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;
                                
@@ -341,38 +348,40 @@ static int parse_cmdline_decoder(int argc, char **argv, opj_dparameters_t *param
 
                        /* ----------------------------------------------------- */
 
-                       case 'v':               /* Verbose mode */
+            case 'v':                  /* Verbose mode */
                        {
-                               parameters->m_verbose = 1;
+                parameters->m_verbose = 1;
                        }
                        break;
                        
                                /* ----------------------------------------------------- */
-                       default:
-                               fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c, opj_optarg);
-                               break;
-               }
+        default:
+            fprintf(stderr, "[WARNING] An invalid option has been ignored.\n");
+            break;
+        }
        }while(c != -1);
 
        /* check for possible errors */
        if(img_fol->set_imgdir==1){
                if(!(parameters->infile[0]==0)){
-                       fprintf(stderr, "Error: options -ImgDir and -i cannot be used together !!\n");
+            fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n");
                        return 1;
                }
                if(img_fol->set_out_format == 0){
-                       fprintf(stderr, "Error: When -ImgDir is used, -OutFor <FORMAT> must be used !!\n");
-                       fprintf(stderr, "Only one format allowed! Valid format PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA!!\n");
+            fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n");
+            fprintf(stderr, "Only one format allowed.\n"
+                            "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n");
                        return 1;
                }
                if(!(parameters->outfile[0] == 0)){
-                       fprintf(stderr, "Error: options -ImgDir and -o cannot be used together !!\n");
+            fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together\n");
                        return 1;
                }
        }else{
                if(parameters->infile[0] == 0) {
+            fprintf(stderr, "[ERROR] Required parameter is missing\n");
                        fprintf(stderr, "Example: %s -i image.j2k\n",argv[0]);
-                       fprintf(stderr, "    Try: %s -h\n",argv[0]);
+            fprintf(stderr, "   Help: %s -h\n",argv[0]);
                        return 1;
                }
        }
@@ -424,12 +433,6 @@ int main(int argc, char *argv[])
        img_fol_t img_fol;
        dircnt_t *dirptr = NULL;
 
-#ifdef MSD
-       OPJ_BOOL l_go_on = OPJ_TRUE;
-       OPJ_UINT32 l_max_data_size = 1000;
-       OPJ_BYTE * l_data = (OPJ_BYTE *) malloc(1000);
-#endif
-
        /* Set decoding parameters to default values */
        opj_set_default_decoder_parameters(&parameters);