fixed cmake support for openjpip
[openjpeg.git] / testing / OPJ_Validate.c
1 /*\r
2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium\r
3 * Copyright (c) 2002-2007, Professor Benoit Macq\r
4 * Copyright (c) 2003-2007, Francois-Olivier Devaux \r
5 * All rights reserved.\r
6 *\r
7 * Redistribution and use in source and binary forms, with or without\r
8 * modification, are permitted provided that the following conditions\r
9 * are met:\r
10 * 1. Redistributions of source code must retain the above copyright\r
11 *    notice, this list of conditions and the following disclaimer.\r
12 * 2. Redistributions in binary form must reproduce the above copyright\r
13 *    notice, this list of conditions and the following disclaimer in the\r
14 *    documentation and/or other materials provided with the distribution.\r
15 *\r
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'\r
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
26 * POSSIBILITY OF SUCH DAMAGE.\r
27 */\r
28 \r
29 #ifdef _WIN32\r
30 #include <windows.h>\r
31 #endif /* _WIN32 */\r
32 #include <stdio.h>\r
33 #include <string.h>\r
34 #include "md5.h"\r
35 \r
36 #define OPJ_Bin_Dir "OPJ_Binaries"\r
37 \r
38 int doprocess(char programme[4096],char command_line[4096]) {\r
39 \r
40 #ifdef _WIN32\r
41         \r
42         int exit=STILL_ACTIVE;\r
43         STARTUPINFO siStartupInfo;\r
44         PROCESS_INFORMATION piProcessInfo;\r
45         \r
46         memset(&siStartupInfo, 0, sizeof(siStartupInfo));\r
47         memset(&piProcessInfo, 0, sizeof(piProcessInfo));\r
48         siStartupInfo.cb = sizeof(siStartupInfo);\r
49         \r
50         if(CreateProcess(programme, // Application name\r
51                 command_line, // Application arguments\r
52                 0,\r
53                 0,\r
54                 FALSE,\r
55                 CREATE_DEFAULT_ERROR_MODE,\r
56                 0,\r
57                 0, // Working directory\r
58                 &siStartupInfo,\r
59                 &piProcessInfo) == FALSE)       \r
60                 return 1;\r
61         \r
62         exit=STILL_ACTIVE;\r
63         while(exit==STILL_ACTIVE) {\r
64                 Sleep(1000);\r
65                 GetExitCodeProcess(piProcessInfo.hProcess,&exit);\r
66         }\r
67         \r
68         return 0;\r
69 \r
70 #else /* !_WIN32 */\r
71         printf("\n%s\n", command_line);\r
72         system(command_line);\r
73         return 0;\r
74 \r
75 #endif /* _WIN32 */\r
76         \r
77 }\r
78 \r
79 char MD5_process(char *input_filename, char *md5_filename) {\r
80         MD5_CTX mdContext;\r
81         int bytes;\r
82   unsigned char data[1024];\r
83         FILE *input_file, *md5_file;\r
84         \r
85         input_file = fopen(input_filename, "rb");\r
86         if (!input_file) {\r
87                 printf("Error opening file %s\n", input_filename);\r
88                 return 1;\r
89         }\r
90         \r
91         md5_file = fopen(md5_filename, "wb");\r
92         if (!md5_file) {\r
93                 printf("Error opening file %s\n", md5_filename);\r
94                 return 1;\r
95         }\r
96         \r
97         MD5Init (&mdContext);\r
98   while ((bytes = fread (data, 1, 1024, input_file)) != 0)\r
99     MD5Update (&mdContext, data, bytes);\r
100   MD5Final (&mdContext);\r
101         \r
102         fwrite(mdContext.digest,16,1,md5_file);\r
103         \r
104         fclose(input_file);\r
105         fclose(md5_file);\r
106         \r
107         return 0;\r
108 }\r
109 \r
110 char fcompare(char *input_filename, char *output_filename) {\r
111         FILE *input_file, *output_file;\r
112         unsigned char input_buffer[17], output_buffer[17];\r
113         char comparison;\r
114         \r
115         input_file = fopen(input_filename, "rb");\r
116         if (!input_file) {\r
117                 printf("Error opening file %s\n", input_filename);\r
118                 return -1;\r
119         }\r
120         \r
121         output_file = fopen(output_filename, "rb");\r
122         if (!output_file) {\r
123                 printf("Error opening file %s\n", output_filename);\r
124                 return -1;\r
125         }\r
126         \r
127         fread(input_buffer,16,1,input_file);\r
128         fread(output_buffer,16,1,output_file);\r
129         fclose(input_file);\r
130         fclose(output_file);\r
131         input_buffer[16] = 0;\r
132         output_buffer[16] = 0;\r
133         \r
134         comparison = strcmp(input_buffer, output_buffer);\r
135         \r
136         if (comparison)\r
137                 return 1;\r
138         return 0;\r
139 }\r
140 \r
141 int main(int argc, char* argv[]) {\r
142         FILE *param_file, *md5_file;\r
143         FILE *report_file;\r
144         char line[4096];\r
145         char md5_filename[4096], tempmd5_filename[4096], temp[4096], report_filename[4096];\r
146         char output_filename[4096];\r
147         char input_cmdline[4096];\r
148         char command_line[4096], exefile[4096];\r
149         int task_counter = 0, word_counter;\r
150         char bin_dir[4096];\r
151         unsigned int word_pointer;\r
152         char ch[4096];                          \r
153         char comparison;\r
154         int num_failed = 0;\r
155         int num_inexistant = 0;\r
156         int num_passed = 0;\r
157                 \r
158         if (argc != 3) {\r
159                 printf("Error with command line. \nExpected: OPJ_Validate parameter_filename bin_directory\n Example: OPJ_Validate parameters_01.txt version1.1.a\n\n");\r
160                 return 1;\r
161         }\r
162         \r
163         param_file = fopen(argv[1],"rb");\r
164         if (!param_file) {\r
165                 printf("Error opening parameter file %s\n",argv[1]);\r
166                 return 1;\r
167         }       \r
168         \r
169         sprintf(bin_dir,"%s/%s",OPJ_Bin_Dir,argv[2]);\r
170         sprintf(tempmd5_filename,"temp/tempmd5.txt");\r
171         sprintf(report_filename,"%s/report.txt",bin_dir);\r
172         report_file = fopen(report_filename, "wb");\r
173         if (!report_file) {\r
174                 printf("Unable to open report file %s", report_filename);\r
175                 return 1;\r
176         }\r
177         \r
178         while (fgets(line, 4096, param_file) != NULL) {\r
179                 \r
180                 if (line[0] != '#' && line[0] != 0x0d) {        // If not a comment line\r
181                         sscanf(line, "%s", temp);\r
182                         word_pointer = 0;\r
183                         sprintf(input_cmdline,"");      \r
184                         sscanf(line+word_pointer,"%s",ch);\r
185                         sprintf(exefile,"%s/%s",bin_dir,ch);                            \r
186                         word_counter = 0;\r
187                         while (sscanf(line+word_pointer,"%s",ch) > 0) {\r
188                                 if (word_counter == 4) \r
189                                         strcpy(output_filename, ch);\r
190                                 word_pointer += strlen(ch)+1;\r
191                                 sprintf(input_cmdline,"%s%s ",input_cmdline, ch);                               \r
192                                 word_counter++;\r
193                         }                       \r
194                         sprintf(md5_filename,"%s.md5",output_filename);\r
195                         task_counter++;\r
196                         sprintf(command_line,"%s/%s",bin_dir,input_cmdline);\r
197                         printf("Task %d\nMD5 file: %s\nCommand line: \"%s\"\n",task_counter, md5_filename,command_line);\r
198                         fprintf(report_file,"Task %d\n   MD5 file: %s\n   Command line: \"%s\"\n",task_counter, md5_filename,command_line);\r
199                         \r
200                         if (doprocess(exefile,command_line)) {\r
201                                 printf("Error executing: \"%s\" \n", command_line);\r
202                                 fprintf(report_file,"Task %d failed because command line is not valid.\n\n", task_counter);\r
203                         }\r
204                         else {\r
205                                 \r
206                                 // Check if MD5 reference exists\r
207                                 md5_file = fopen(md5_filename,"rb");\r
208                                 if (md5_file) {\r
209                                         fclose(md5_file);\r
210                                         if (MD5_process(output_filename, tempmd5_filename)) \r
211                                                 return 1;\r
212                                         \r
213                                         comparison = fcompare(tempmd5_filename, md5_filename);\r
214                                         if (comparison == -1)\r
215                                                 return 1;\r
216                                         else if (comparison) {\r
217                                                 printf("ERROR: %s and %s are different.\nThe codec seems to behave differently.\n\n", tempmd5_filename, md5_filename);\r
218                                                 fprintf(report_file,"ERROR: %s and %s are different.\nThe codec seems to behave differently.\n\n", tempmd5_filename, md5_filename);\r
219                                                 num_failed++;\r
220                                         }\r
221                                         else {\r
222                                                 printf("%s and %s are the same.\nTask %d OK\n\n",tempmd5_filename, md5_filename, task_counter);\r
223                                                 fprintf(report_file,"   %s and %s are the same.\nTask %d OK\n\n",tempmd5_filename, md5_filename, task_counter);\r
224                                                 num_passed++;\r
225                                         }\r
226                                         remove(tempmd5_filename);\r
227                                 }       \r
228                                 else {\r
229                                         if (MD5_process(output_filename, md5_filename))\r
230                                                 return 1;\r
231                                         printf("...  MD5 of %s was inexistant. It has been created\n\n", output_filename);\r
232                                         fprintf(report_file,"MD5 of %s was inexistant. It has been created\n\n", output_filename);\r
233                                         num_inexistant++;\r
234                                 }\r
235                         }\r
236                 }\r
237         }               \r
238 \r
239         printf("\nREPORT;\n%d tests num_passed\n%d tests num_failed\n%d MD5 were num_inexistant\n", num_passed, num_failed, num_inexistant);\r
240         fprintf(report_file,"\nREPORT;\n%d tests num_passed\n%d tests num_failed\n%d MD5 were num_inexistant\n", num_passed, num_failed, num_inexistant);\r
241         fclose(param_file);\r
242         fclose(report_file);\r
243                 \r
244 }\r