Debian Codegraph @

src/checkit_tiff.c

raw
1 /* 'checkit_tiff' is a conformance checker for baseline TIFFs
2 *
3 * author: Andreas Romeyke, 2015-2022
4 * licensed under conditions of libtiff
5 * (see http://libtiff.maptools.org/misc.html)
6 *
7 */
8
9
10 #include "config_parser.h"
11 #include "check_helper.h"
12 #include "check_renderer.h"
13 #include "strcopy.h"
14 #include <assert.h>
15 #include <dirent.h>
16 #include <ctype.h>
17 #include <stdio.h>
18 //#include <bits/getopt_core.h>
19 #ifdef __unix__
20 #include <sys/stat.h>
21 #else
22 /* #include <sys\stat.h> */
23 #include <sys/stat.h>
24 #endif
25 #ifdef HAVE_IccProfLib
26 #include "IccProfLibVer.h"
27 #endif
28
29
30 #define STRINGIZE(x) #x
31
32
33 int validate_dir(int are_valid, int flag_use_memorymapped_io, int flag_scan_recursively, const char *cfg_file,
34 const char *tiff_file_or_dir);
35
36 static void print_if_defined(FILE * filehandle, const char * s) {
37 if (NULL != s && s[0] != '\0') {
38 fprintf(filehandle, " %s", s);
39 }
40 }
41
42
43
44 static int check_specific_tiff_file( const char * tiff_file, int use_memmapped) {
45 GET_EMPTY_RET(res)
46 /* init render pipeline */
47 retmsg_t * render_pipeline = malloc( sizeof( retmsg_t) );
48 if (NULL == render_pipeline) {
49 exit (could_not_allocate_memory);
50 }
51 retmsg_t * actual_render = render_pipeline;
52 actual_render->rm_type = rm_file;
53 actual_render->next=NULL;
54 assert(NULL != tiff_file);
55 actual_render->rm_msg = str( tiff_file);
56 /* parse TIFF file */
57 ctiff_t * ctif = initialize_ctif( tiff_file, use_memmapped?is_memmap:is_filep );
58 res = parse_header_and_endianess( ctif );
59 if (res.returncode != is_valid) {
60 add_to_render_pipeline(&actual_render, res.value_found, rm_hard_error);
61 add_to_render_pipeline(&actual_render, empty_str(), rm_endtiff);
62 //printf("res.val='%s'\n", res.value_found);
63 goto renderer_exit;
64 }
65 uint32 offset;
66 res=get_first_IFD(ctif, &offset);
67 if (res.returncode != is_valid) {
68 add_to_render_pipeline(&actual_render, res.value_found, rm_hard_error);
69 add_to_render_pipeline(&actual_render, empty_str(), rm_endtiff);
70 goto renderer_exit;
71 }
72 execute_plan(ctif);
73 res = print_plan_results( actual_render);
74 renderer_exit:
75 {
76 stringbuf_t render_result_string = renderer( render_pipeline );
77 for (size_t i=0; i<render_result_string.pos; i++) {
78 if (strlen_of_str(render_result_string.strings[i])>0) {
79 printf("%s", cstr_of_str(render_result_string.strings[i]));
80 }
81 }
82 printf("\n");
83 /* free all entries of render pipeline */
84 clean_render_pipeline(&render_pipeline);
85 clean_stringbuf( &render_result_string);
86 }
87 free_ctif( ctif );
88 res.value_found=empty_str();
89 return res.returncode;
90 }
91
92 static void print_program_header (FILE * filehandle, const char * programname) {
93 fprintf( filehandle, "'%s' version: %s\n", programname, VERSION);
94 fprintf( filehandle, "\trevision: %s\n", REPO_REVISION);
95 fprintf( filehandle, "licensed under conditions of libtiff (see http://libtiff.maptools.org/misc.html)\n");
96 #ifdef HAVE_IccProfLib
97 fprintf( filehandle, "(enabled full ICC profile checking using IccProfLib by color.org, version " ICCPROFLIBVER "\n");
98 fprintf( filehandle, " the ICCProflib is licensed under ICC Software License, version 0.2\n");
99 fprintf( filehandle, " copyright (c) 2003 The International Color Consortium. All rights reserved.)\n");
100 #else
101 fprintf( filehandle, "(disabled full ICC profile checking, but using rudimentary ICC profile checks)\n");
102 #endif
103 fprintf( filehandle, "(compiled with flags: ");
104 #ifdef ALLOW_CMMTYPE_LINO
105 print_if_defined( filehandle, STRINGIZE(ALLOW_CMMTYPE_LINO));
106 #endif
107 #ifdef ALLOW_ICC1_2001_04
108 print_if_defined( filehandle, STRINGIZE(ALLOW_ICC1_2001_04));
109 #endif
110 #ifdef COMPAT_NEED_REALLOCARRAY
111 print_if_defined( filehandle, STRINGIZE(COMPAT_NEED_REALLOCARRAY));
112 #endif
113 #ifdef DEBUG
114 print_if_defined( filehandle, STRINGIZE(DEBUG));
115 #endif
116 #ifdef HARDEN
117 print_if_defined( filehandle, STRINGIZE(HARDEN));
118 #endif
119 #ifdef HAVE_STRNDUP
120 print_if_defined( filehandle, STRINGIZE(HAVE_STRNDUP));
121 #endif
122 #ifdef PCRE2_STATIC
123 print_if_defined( filehandle, STRINGIZE(PCRE2_STATIC));
124 #endif
125 #ifdef WITH_STRONG_ICC
126 print_if_defined( filehandle, STRINGIZE(WITH_STRONG_ICC));
127 #endif
128 #ifdef HAVE_IccProfLib
129 print_if_defined( filehandle, STRINGIZE(HAVE_IccProfLib));
130 #endif
131 #ifdef HAVE_MMAP
132 print_if_defined( filehandle, STRINGIZE(HAVE_MMAP));
133 #endif
134 #ifdef HAVE_MUNMAP
135 print_if_defined( filehandle, STRINGIZE(HAVE_MUNMAP));
136 #endif
137 #ifdef __WIN32__
138 print_if_defined( filehandle, STRINGIZE(__WIN32__));
139 #endif
140 fprintf( filehandle, ")\n");
141 }
142
143 /** help function */
144 static void help (FILE * filehandle, const char * programname) {
145 print_program_header(filehandle, programname);
146 printf ("call it with:\n");
147 printf ("\tcheckit_tiff [[-c|-t]|-h|-m|-d|-D|-q] <configfile> <tifffile> [<tifffile> ...]\n");
148 printf ("\nwhere <tifffile> is the tiff file (or directory) to be validated\n");
149 printf ("and <configfile> is the file name of the validation profile\n");
150 printf ("\t-h this help\n");
151 printf ("\t-c colorized output using ANSI escape sequences\n");
152 printf ("\t-m uses memmapped I/O (faster validation, but needs more RAM. Not available on all operating systems.)\n");
153 printf ("\t-d check all files in that directory (non-recursively)\n");
154 printf ("\t-D check all files in that directory (recursively)\n");
155 printf ("\t-q suppresses the output of all valid tags\n");
156 printf ("\t-t CSV output\n");
157 printf ("example:\n\tcheckit_tiff example_configs/baseline_minimal.cfg tiffs_should_pass/minimal_valid.tiff \n");
158 printf ("\n");
159 }
160
161 /** iterate through all files of given directory */
162 int validate_dir(int are_valid, int flag_use_memorymapped_io, int flag_scan_recursively, const char *cfg_file,
163 const char *tiff_file_or_dir) {
164 size_t len = strlen(tiff_file_or_dir);
165 char tiff_dir[len + 1];
166 cit_strcopy(tiff_dir, sizeof(tiff_dir), tiff_file_or_dir, len);
167 /* remove trailing / */
168 char const *dirsuffix = strrchr(tiff_dir, '/');
169 if (dirsuffix != NULL) { /* found a / */
170 if (0 == strcmp(dirsuffix, "/")) { /* ok, ends with / */
171 /* remove last / */
172 assert(len >= 1); // or whatever you want to do with short strings
173 tiff_dir[len - 1] = 0;
174 }
175 }
176 /* iterate through all files in given dir */
177 DIR *dir;
178 if ((dir = opendir(tiff_file_or_dir)) != NULL) {
179 /* print all the files and directories within directory */
180 struct dirent *ent;
181 while ((ent = readdir(dir)) != NULL) {
182 if (0 == strcmp(ent->d_name, "..") ) {
183 //printf ("PARENT DETECTED (%s)\n", ent->d_name);
184 continue;
185 }
186 if (0 == strcmp(ent->d_name, ".") ) {
187 //printf ("CURRENT DETECTED (%s)\n", ent->d_name);
188 continue;
189 }
190 struct stat attribute;
191 len = strlen(tiff_dir) + strlen(ent->d_name) + 2;
192 char fqname[len];
193 snprintf(fqname, len, "%s/%s", tiff_dir, ent->d_name);
194 if (stat(fqname, &attribute) == -1) {
195 fprintf(stderr, "could not stat on file '%s' in directory '%s' (%s)\n", ent->d_name, tiff_dir,
196 fqname);
197 exit(EXIT_FAILURE);
198 } else if (attribute.st_mode & S_IFREG) {
199 //printf ("%s\n", fqname);
200 parse_plan_via_file(cfg_file);
201 are_valid += check_specific_tiff_file(fqname, flag_use_memorymapped_io);
202 clean_plan();
203 printf("\n");
204 } else if (flag_scan_recursively) {
205 are_valid += validate_dir(are_valid, flag_use_memorymapped_io, flag_scan_recursively, cfg_file,
206 fqname);
207 }
208 }
209 closedir(dir);
210 } else {
211 /* could not open directory */
212 fprintf(stderr, "directory '%s' could not be opened\n", tiff_file_or_dir);
213 exit(EXIT_FAILURE);
214 }
215 return are_valid;
216 }
217
218 /** main */
219 int main (int argc, char * argv[]) {
220 FILE *default_filehandle = stdout;
221 int c;
222 int flag_check_directory = UNFLAGGED;
223 int flag_check_directory_recursively = UNFLAGGED;
224 int flag_use_memorymapped_io = UNFLAGGED;
225 while ((c = getopt(argc, argv, "chmdDx:qt")) != -1) {
226 switch (c) {
227 case 'h': /* help */
228 help(default_filehandle, argv[0]);
229 exit(0);
230 case 'c': /* colorize output */
231 set_renderer_to_ansi();
232 break;
233 case 'd': /* check directory */
234 flag_check_directory = FLAGGED;
235 flag_check_directory_recursively = UNFLAGGED;
236 // printf("\nCheck all files in given directory\n");
237 break;
238 case 'D': /* check directory */
239 flag_check_directory = FLAGGED;
240 flag_check_directory_recursively = FLAGGED;
241 // printf("\nCheck all files in given directory, recursively\n");
242 break;
243 case 'm': /* use memory mapped I/O */
244 flag_use_memorymapped_io = FLAGGED;
245 break;
246 case 'q': /* suppresses output of valid rules/tags */
247 set_renderer_to_quiet();
248 break;
249 case 't':
250 default_filehandle = stderr;
251 set_renderer_to_csv();
252 break;
253 case '?': /* something goes wrong */
254 /*
255 if (optopt == 'r') {
256 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
257 return (-1);
258 }
259 else*/
260 if (isprint(optopt)) {
261 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
262 return (-1);
263 } else if (0 != optopt) {
264 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
265 return (-1);
266 }
267 break;
268 default:
269 abort();
270 }
271 }
272 if (argc - optind < 2) {
273 help(default_filehandle, argv[0]);
274 fprintf(stderr,
275 "%s needs at least two arguments, first should be the config-file, second the TIFF-file\nexample:\n\t %s example_configs/baseline_minimal.cfg tiffs_should_pass/minimal_valid.tiff\n",
276 argv[0], argv[0]);
277 exit(EXIT_FAILURE);
278 }
279 const char *cfg_file = argv[optind];
280 print_program_header(default_filehandle, argv[0]);
281 fprintf(default_filehandle, "cfg_file=%s\n", cfg_file);
282 int are_valid = 0;
283 int index_of_tiff_file_or_dir = optind + 1;
284 do {
285 const char *tiff_file_or_dir = argv[index_of_tiff_file_or_dir++];
286 fprintf(default_filehandle, "tiff file/dir=%s\n", tiff_file_or_dir);
287
288
289 if (flag_check_directory == FLAGGED) {
290 are_valid = validate_dir(are_valid, flag_use_memorymapped_io, flag_check_directory_recursively, cfg_file, tiff_file_or_dir);
291 } else { /* a file */
292 /* use tiff_file_or_dir */
293 parse_plan_via_file(cfg_file);
294 are_valid += check_specific_tiff_file(tiff_file_or_dir, flag_use_memorymapped_io);
295 clean_plan();
296 }
297 } while (index_of_tiff_file_or_dir < argc);
298 if (0 == are_valid) {
299 exit(EXIT_SUCCESS);
300 } else {
301 exit(EXIT_FAILURE);
302 }
303 }
304
305
306 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/