Fix several memory and resource leaks
[openjpeg.git] / tests / compare_raw_files.c
1 /*
2  * Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France 
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * compare_raw_files.c
29  *
30  *  Created on: 31 August 2011
31  *      Author: mickael
32  *
33  * This is equivalent to the UNIX `cmp` command
34  */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <ctype.h>
40
41 #include "opj_getopt.h"
42
43 typedef struct test_cmp_parameters
44 {
45   /**  */
46   char* base_filename;
47   /**  */
48   char* test_filename;
49 } test_cmp_parameters;
50
51 /*******************************************************************************
52  * Command line help function
53  *******************************************************************************/
54 static void compare_raw_files_help_display(void) {
55   fprintf(stdout,"\nList of parameters for the compare_raw_files function  \n");
56   fprintf(stdout,"\n");
57   fprintf(stdout,"  -b \t REQUIRED \t filename to the reference/baseline RAW image \n");
58   fprintf(stdout,"  -t \t REQUIRED \t filename to the test RAW image\n");
59   fprintf(stdout,"\n");
60 }
61
62 /*******************************************************************************
63  * Parse command line
64  *******************************************************************************/
65 static int parse_cmdline_cmp(int argc, char **argv, test_cmp_parameters* param)
66 {
67   size_t sizemembasefile, sizememtestfile;
68   int index;
69   const char optlist[] = "b:t:";
70   int c;
71
72   /* Init parameters*/
73   param->base_filename = NULL;
74   param->test_filename = NULL;
75
76   opj_opterr = 0;
77   while ((c = opj_getopt(argc, argv, optlist)) != -1)
78     switch (c)
79       {
80     case 'b':
81       sizemembasefile = strlen(opj_optarg)+1;
82       free(param->base_filename); /* handle dup option */
83       param->base_filename = (char*) malloc(sizemembasefile);
84       strcpy(param->base_filename, opj_optarg);
85       /*printf("param->base_filename = %s [%d / %d]\n", param->base_filename, strlen(param->base_filename), sizemembasefile );*/
86       break;
87     case 't':
88       sizememtestfile = strlen(opj_optarg) + 1;
89       free(param->test_filename); /* handle dup option */
90       param->test_filename = (char*) malloc(sizememtestfile);
91       strcpy(param->test_filename, opj_optarg);
92       /*printf("param->test_filename = %s [%d / %d]\n", param->test_filename, strlen(param->test_filename), sizememtestfile);*/
93       break;
94     case '?':
95       if ((opj_optopt == 'b') || (opj_optopt == 't'))
96         fprintf(stderr, "Option -%c requires an argument.\n", opj_optopt);
97       else
98         if (isprint(opj_optopt))        fprintf(stderr, "Unknown option `-%c'.\n", opj_optopt);
99         else    fprintf(stderr, "Unknown option character `\\x%x'.\n", opj_optopt);
100       return 1;
101     default:
102       fprintf(stderr, "WARNING -> this option is not valid \"-%c %s\"\n", c, opj_optarg);
103       break;
104       }
105
106   if (opj_optind != argc) {
107     for (index = opj_optind; index < argc; index++)
108       fprintf(stderr,"Non-option argument %s\n", argv[index]);
109     return 1;
110   }
111
112   return 0;
113 }
114
115 /*******************************************************************************
116  * MAIN
117  *******************************************************************************/
118 int main(int argc, char **argv)
119 {
120   int pos = 0;
121   test_cmp_parameters inParam;
122   FILE *file_test=NULL, *file_base=NULL;
123   unsigned char equal = 0U; /* returns error by default */
124
125   /* Get parameters from command line*/
126   if (parse_cmdline_cmp(argc, argv, &inParam))
127   {
128     compare_raw_files_help_display();
129     goto cleanup;
130   }
131
132   file_test = fopen(inParam.test_filename, "rb");
133   if (!file_test) {
134     fprintf(stderr, "Failed to open %s for reading !!\n", inParam.test_filename);
135     goto cleanup;
136   }
137
138   file_base = fopen(inParam.base_filename, "rb");
139   if (!file_base) {
140     fprintf(stderr, "Failed to open %s for reading !!\n", inParam.base_filename);
141     goto cleanup;
142   }
143
144   /* Read simultaneously the two files*/
145   equal = 1U;
146   while (equal)
147   {
148     unsigned char value_test = 0;
149     unsigned char eof_test = 0;
150     unsigned char value_base = 0;
151     unsigned char eof_base = 0;
152
153     /* Read one byte*/
154     if (!fread(&value_test, 1, 1, file_test)) {
155       eof_test = 1;
156     }
157
158     /* Read one byte*/
159     if (!fread(&value_base, 1, 1, file_base)) {
160       eof_base = 1;
161     }
162
163     /* End of file reached by the two files?*/
164     if (eof_test && eof_base)
165       break;
166
167     /* End of file reached only by one file?*/
168     if (eof_test || eof_base)
169     {
170       fprintf(stdout,"Files have different sizes.\n");
171       equal = 0;
172     }
173
174     /* Binary values are equal?*/
175     if (value_test != value_base)
176     {
177       fprintf(stdout,"Binary values read in the file are different %x vs %x at position %d.\n", value_test, value_base, pos);
178       equal = 0;
179     }
180     pos++;
181   }
182
183   if(equal) fprintf(stdout,"---- TEST SUCCEED: Files are equal ----\n");
184 cleanup:
185   if(file_test) fclose(file_test);
186   if(file_base) fclose(file_base);
187
188   /* Free Memory */
189   free(inParam.base_filename);
190   free(inParam.test_filename);
191
192   return equal ? EXIT_SUCCESS : EXIT_FAILURE;
193 }