| 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 |
/* #define YY_DEBUG */ |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <assert.h> |
| 14 |
#include "config_parser.h" |
| 15 |
#include "check_helper.h" |
| 16 |
#include "msg_parser.h" |
| 17 |
#include "simple_string.h" |
| 18 |
|
| 19 |
#define PCRE2_CODE_UNIT_WIDTH 8 |
| 20 |
#include <pcre2.h> /* lib pcre2, see http://www.pcre.org/ */ |
| 21 |
#include <libgen.h> |
| 22 |
|
| 23 |
void print_st (int i, internal_stack_entry_t v) { |
| 24 |
switch (v.type) { |
| 25 |
case st_unsigned_int: |
| 26 |
printf("[%i] uint (%u)\n", i, v.value.i); |
| 27 |
break; |
| 28 |
case st_regex: |
| 29 |
printf("[%i] regex (%s)\n", i, v.value.regex); |
| 30 |
break; |
| 31 |
/* |
| 32 |
case st_val: |
| 33 |
printf("[%i] val (%i)\n", i,v.value.val); |
| 34 |
break; |
| 35 |
*/ |
| 36 |
case st_LOW_GUARD: |
| 37 |
printf("[%i] LOW GUARD!\n", i); |
| 38 |
break; |
| 39 |
case st_HIGH_GUARD: |
| 40 |
printf("[%i] HIGH_GUARD!\n", i); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
void print_res(int i, const full_res_t full_result) { |
| 45 |
printf ("returncode = %i\n", full_result.returncode); |
| 46 |
printf("i=%i lineno=%i tag=%hu func=%s (%i) returncode=%s (%i)\n", i, |
| 47 |
full_result.lineno, |
| 48 |
full_result.tag, |
| 49 |
get_parser_function_description(full_result.function), |
| 50 |
full_result.function, |
| 51 |
get_parser_error_description(full_result.returncode), |
| 52 |
full_result.returncode |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
void print_exe(int i, const exe_entry_t exe) { |
| 57 |
printf("/* lineno=%03i */ ", exe.lineno); |
| 58 |
if (exe.is_precondition) { printf("PRECOND: "); } |
| 59 |
else { printf("EXEC: "); } |
| 60 |
printf("%s tag=%i", get_parser_function_name(exe.function), |
| 61 |
exe.tag); |
| 62 |
if (!exe.is_precondition) { printf("\n"); } |
| 63 |
printf("\n"); |
| 64 |
} |
| 65 |
|
| 66 |
void print_val(int i, values_t v) { |
| 67 |
printf("[%i] val (%i)\n", i,v); |
| 68 |
} |
| 69 |
|
| 70 |
#define YY_CTX_LOCAL |
| 71 |
|
| 72 |
/* |
| 73 |
#define RULE_DEBUG 1 |
| 74 |
#define DEBUG 1 |
| 75 |
#define EXE_DEBUG 1 |
| 76 |
#define YY_DEBUG 1 |
| 77 |
*/ |
| 78 |
|
| 79 |
/* global vars */ |
| 80 |
static parser_state_t parser_state; |
| 81 |
|
| 82 |
/* redefined YY_INPUT to read from stream */ |
| 83 |
void |
| 84 |
summarize_results(size_t count_of_all_results, size_t count_of_valid_results, retmsg_t **actual_render, ret_t *res); |
| 85 |
|
| 86 |
#define YY_INPUT(yyctx, buf, result, max_size) \ |
| 87 |
{ \ |
| 88 |
int yyc= fgetc(parser_state.stream); \ |
| 89 |
(result)= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \ |
| 90 |
yyprintf((stderr, "<%c>", yyc)); \ |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/* prints a plan (list) of functions */ |
| 95 |
/* |
| 96 |
void print_plan () { |
| 97 |
printf("print plan:\n"); |
| 98 |
executionentry_t * this_exe_p = plan.start; |
| 99 |
while (NULL != this_exe_p) { |
| 100 |
printf("next action is: %s\n", this_exe_p->name); |
| 101 |
this_exe_p = this_exe_p->next; |
| 102 |
} |
| 103 |
} |
| 104 |
*/ |
| 105 |
|
| 106 |
static void result_push(const full_res_t r) { |
| 107 |
full_res_t_push( parser_state.result_stack, r); |
| 108 |
} |
| 109 |
|
| 110 |
static void result_push_back(const full_res_t r) { |
| 111 |
full_res_t_push_back( parser_state.result_stack, r); |
| 112 |
} |
| 113 |
static void result_printstack(void) { |
| 114 |
printf("=== BEGIN result_printstack\n"); |
| 115 |
full_res_t_print_top(parser_state.result_stack); |
| 116 |
printf("=== END result_printstack\n"); |
| 117 |
} |
| 118 |
|
| 119 |
static void i_push (unsigned int i) { |
| 120 |
#ifdef RULE_DEBUG |
| 121 |
printf("i_push, %u\n", i); |
| 122 |
#endif |
| 123 |
internal_stack_entry_t tmp; |
| 124 |
tmp.type=st_unsigned_int; |
| 125 |
tmp.value.i = i; |
| 126 |
internal_stack_entry_t_push(parser_state.intermediate_stack, tmp); |
| 127 |
} |
| 128 |
static unsigned int i_pop (void) { |
| 129 |
internal_stack_entry_t tmp = internal_stack_entry_t_pop(parser_state.intermediate_stack); |
| 130 |
assert(tmp.type == st_unsigned_int); |
| 131 |
return tmp.value.i; |
| 132 |
} |
| 133 |
static void v_push ( values_t v) { |
| 134 |
#ifdef RULE_DEBUG |
| 135 |
printf("v_push, %i\n", v); |
| 136 |
#endif |
| 137 |
values_t_push(parser_state.values_stack, v); |
| 138 |
} |
| 139 |
|
| 140 |
static values_t v_pop (void) { |
| 141 |
return values_t_pop(parser_state.values_stack); |
| 142 |
} |
| 143 |
|
| 144 |
static void r_push (const char * r) { |
| 145 |
#ifdef RULE_DEBUG |
| 146 |
printf("r_push, %s\n", r); |
| 147 |
#endif |
| 148 |
internal_stack_entry_t tmp; |
| 149 |
tmp.type=st_regex; |
| 150 |
tmp.value.regex = r; |
| 151 |
internal_stack_entry_t_push(parser_state.intermediate_stack, tmp); |
| 152 |
} |
| 153 |
|
| 154 |
static const char * r_pop (void) { |
| 155 |
internal_stack_entry_t tmp = internal_stack_entry_t_pop(parser_state.intermediate_stack); |
| 156 |
assert(tmp.type == st_regex); |
| 157 |
return tmp.value.regex; |
| 158 |
} |
| 159 |
|
| 160 |
static void exe_push (const exe_entry_t i) { exe_entry_t_push( parser_state.exe_stack, i);} |
| 161 |
static exe_entry_t exe_pop (void) { return exe_entry_t_pop( parser_state.exe_stack); } |
| 162 |
|
| 163 |
void exe_printstack_human_readable (void) { |
| 164 |
printf("\n/* the rules are in stack order, the top comes first */\n\n"); |
| 165 |
exe_entry_t_print_top(parser_state.exe_stack); |
| 166 |
} |
| 167 |
|
| 168 |
/* reduce results */ |
| 169 |
static void reduce_results(void) { |
| 170 |
full_res_t_stack_t * temporary_results = full_res_t_new(); |
| 171 |
//full_res_t_init(temporary_results, __FILE__, __LINE__); |
| 172 |
#ifdef DEBUG |
| 173 |
full_res_t_print_top( parser_state.result_stack); |
| 174 |
#endif |
| 175 |
while (!full_res_t_is_empty(parser_state.result_stack)) { |
| 176 |
full_res_t v = full_res_t_pop(parser_state.result_stack); |
| 177 |
if (v.returncode == parser_logical_combine_close) { |
| 178 |
/* check all values until parser_logical_close found */ |
| 179 |
bool one_result_is_ok = false; |
| 180 |
char * found_fails = calloc(512,sizeof(char)); |
| 181 |
while (!full_res_t_is_empty(parser_state.result_stack)) { |
| 182 |
full_res_t tmp = full_res_t_pop(parser_state.result_stack); |
| 183 |
if (tmp.returncode == parser_logical_combine_open) { |
| 184 |
/* push combined result to temporary_results */ |
| 185 |
if (one_result_is_ok == true) { |
| 186 |
tmp.returncode = is_valid; |
| 187 |
free( found_fails); |
| 188 |
found_fails = NULL; |
| 189 |
} else { |
| 190 |
tmp.found_value = str(found_fails); |
| 191 |
} |
| 192 |
full_res_t_push(temporary_results, tmp); |
| 193 |
//full_res_t_push(&temporary_results, tmp); |
| 194 |
break; /* exit inner loop */ |
| 195 |
} else { |
| 196 |
if (tmp.returncode != is_valid) { |
| 197 |
found_fails = strncat( found_fails, tmp.found_value.string, tmp.found_value.len); |
| 198 |
found_fails = strcat( found_fails, " "); |
| 199 |
} else { |
| 200 |
one_result_is_ok = true; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} else { |
| 205 |
full_res_t_push(temporary_results, v); |
| 206 |
} |
| 207 |
} |
| 208 |
/* now go through temporary results and push them back to result stack */ |
| 209 |
assert( full_res_t_is_empty(parser_state.result_stack) ); |
| 210 |
#ifdef DEBUG |
| 211 |
full_res_t_print_top( temporary_results); |
| 212 |
#endif |
| 213 |
while (!full_res_t_is_empty(temporary_results)) { |
| 214 |
full_res_t v = full_res_t_pop(temporary_results); |
| 215 |
full_res_t_push(parser_state.result_stack, v); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/* stack function for parser */ |
| 220 |
static void exe_i_push (exe_entry_t * ep, unsigned int i) { |
| 221 |
internal_stack_entry_t tmp; |
| 222 |
tmp.type=st_unsigned_int; |
| 223 |
tmp.value.i = i; |
| 224 |
internal_stack_entry_t_push(ep->stack, tmp); |
| 225 |
} |
| 226 |
static unsigned int exe_i_pop(exe_entry_t * ep) { |
| 227 |
internal_stack_entry_t tmp = internal_stack_entry_t_pop(ep->stack); |
| 228 |
assert(tmp.type == st_unsigned_int); |
| 229 |
return tmp.value.i; |
| 230 |
} |
| 231 |
/* stack function for parser */ |
| 232 |
static exe_entry_t * exe_regex_push (exe_entry_t * ep, const char * s) { |
| 233 |
#ifdef EXE_DEBUG |
| 234 |
printf("-------------------\n"); |
| 235 |
printf("\tpush REGEX='%s'\n", s); |
| 236 |
printf("-------------------\n"); |
| 237 |
#endif |
| 238 |
internal_stack_entry_t tmp; |
| 239 |
tmp.type = st_regex; |
| 240 |
tmp.value.regex = strdup(s); |
| 241 |
internal_stack_entry_t_push(ep->stack, tmp); |
| 242 |
#ifdef EXE_DEBUG |
| 243 |
printf("\ton top='%s'\n", ep->regex_stack[ (ep->regex_stackp)-1 ]); |
| 244 |
#endif |
| 245 |
return ep; |
| 246 |
} |
| 247 |
|
| 248 |
static const char * exe_regex_pop(exe_entry_t * ep) { |
| 249 |
internal_stack_entry_t tmp = internal_stack_entry_t_pop(ep->stack); |
| 250 |
assert(tmp.type == st_regex); |
| 251 |
const char * s = tmp.value.regex; |
| 252 |
#ifdef EXE_DEBUG |
| 253 |
printf("-------------------\n"); |
| 254 |
printf("\tpop REGEX='%s'\n", s); |
| 255 |
printf("-------------------\n"); |
| 256 |
printf("\ton top='%s'\n", ep->regex_stack[ (ep->regex_stackp) ]); |
| 257 |
#endif |
| 258 |
return s; |
| 259 |
} |
| 260 |
|
| 261 |
static ret_t call_exec_function(ctiff_t * ctif, ret_t * retp, const exe_entry_t * exep) { |
| 262 |
ret_t ret = *retp; |
| 263 |
exe_entry_t exe = *exep; |
| 264 |
switch (exe.function) { |
| 265 |
case fc_true: { ret.returncode=is_valid; break;} |
| 266 |
case fc_false: { ret.returncode=should_not_occur; break; } |
| 267 |
case fc_tag_has_some_of_these_values: { unsigned int count = exe_i_pop(&exe); |
| 268 |
unsigned int values[count]; |
| 269 |
for (unsigned int j=0; j<count; j++) {values[j]=exe_i_pop(&exe);} |
| 270 |
ret = check_tag_has_some_of_these_values(ctif, exe.tag, count, values); |
| 271 |
break; |
| 272 |
} |
| 273 |
case fc_tag_has_valuelist: { unsigned int count = exe_i_pop(&exe); |
| 274 |
unsigned int values[count]; /* values are in parser_state.valuelist! */ |
| 275 |
for (unsigned int j=0; j<count; j++) {values[j]=exe_i_pop(&exe);} |
| 276 |
ret = check_tag_has_valuelist(ctif, exe.tag, count, values); |
| 277 |
break; |
| 278 |
} |
| 279 |
case fc_tag_has_value_in_range: { unsigned int a = exe_i_pop(&exe); |
| 280 |
unsigned int b = exe_i_pop(&exe); |
| 281 |
ret = check_tag_has_value_in_range(ctif, exe.tag, a, b); |
| 282 |
break; |
| 283 |
} |
| 284 |
case fc_tag_has_value: { unsigned int a = exe_i_pop(&exe); |
| 285 |
ret = check_tag_has_value(ctif, exe.tag, a); |
| 286 |
break; |
| 287 |
} |
| 288 |
case fc_tag_has_value_quiet: { unsigned int a = exe_i_pop(&exe); |
| 289 |
ret = check_tag_has_value_quiet(ctif, exe.tag, a); |
| 290 |
break; |
| 291 |
} |
| 292 |
case fc_tag: { ret = check_tag(ctif, exe.tag); break;} |
| 293 |
case fc_tag_quiet: { returncode_t rc = check_tag_quiet(ctif, exe.tag); ret.returncode = rc; break;} |
| 294 |
case fc_notag: { |
| 295 |
ret = check_notag(ctif, exe.tag); |
| 296 |
break; |
| 297 |
} |
| 298 |
case fc_tag_has_valid_type: { ret = check_tag_has_valid_type(ctif, exe.tag); break;} |
| 299 |
case fc_datetime: { ret = check_datetime(ctif); break;} |
| 300 |
case fc_icc: { ret = check_icc(ctif); break;} |
| 301 |
case fc_has_only_one_ifd: { ret = check_has_only_one_ifd(ctif); break;} |
| 302 |
case fc_tagorder: { ret = check_tagorder(ctif); break;} |
| 303 |
case fc_tag_has_valid_asciivalue: { ret = check_tag_has_valid_asciivalue(ctif, exe.tag); break;} |
| 304 |
case fc_tag_has_value_matching_regex: { const char * regex_string = exe_regex_pop( &exe); |
| 305 |
ret = check_tag_has_value_matching_regex(ctif, exe.tag, regex_string); |
| 306 |
//free( regex_string ); |
| 307 |
break; |
| 308 |
} |
| 309 |
case fc_all_offsets_are_word_aligned: { ret = check_all_offsets_are_word_aligned(ctif); break;} |
| 310 |
case fc_all_offsets_are_used_once_only: { ret = check_all_offsets_are_used_once_only(ctif); break;} |
| 311 |
case fc_all_IFDs_are_word_aligned: { ret = check_all_IFDs_are_word_aligned(ctif); break;} |
| 312 |
case fc_internal_logic_combine_open: { ret.returncode = parser_logical_combine_open ; break; } |
| 313 |
case fc_internal_logic_combine_close: { ret.returncode = parser_logical_combine_close ; break; } |
| 314 |
case fc_all_offsets_are_not_zero: { ret = check_all_offsets_are_greater_zero(ctif); break;} |
| 315 |
case fc_all_offsets_within_filesize: { ret = check_all_offsets_within_filesize(ctif); break;} |
| 316 |
case fc_all_IFDs_have_tags: { ret = check_all_IFDs_have_tags(ctif); break;} |
| 317 |
case fc_all_geotiff_tags_have_same_count_of_values: { |
| 318 |
ret = check_all_geotiff_tags(ctif); break; |
| 319 |
} |
| 320 |
case fc_check_cardinality: { ret = check_cardinality_of_some_tags_are_equal(ctif); break;} |
| 321 |
|
| 322 |
default: { |
| 323 |
GET_EMPTY_RET(res) |
| 324 |
res.value_found = empty_str(); |
| 325 |
fprintf(stderr, "lineno=%i, stack entry tag %i", parser_state.lineno, exe.tag); |
| 326 |
exit(parser_error_wrong_function_found_in_parser_state_exe_stack); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
assert( ret.returncode != should_not_occur ); |
| 331 |
assert( ret.returncode != calling_error_count_size); |
| 332 |
*retp = ret; |
| 333 |
return ret; |
| 334 |
} |
| 335 |
|
| 336 |
/* ececute fc directly */ |
| 337 |
static ret_t execute_function_and_set_results_for_next_call (ctiff_t * ctif, const exe_entry_t * exep, const ret_t * retp, int * precondition_result_p, function_t * precondition_function_p ) { |
| 338 |
assert(NULL != ctif); |
| 339 |
assert(NULL != exep); |
| 340 |
assert(NULL != retp); |
| 341 |
exe_entry_t exe = *exep; |
| 342 |
ret_t ret = *retp; |
| 343 |
ret = call_exec_function(ctif, &ret, &exe ); |
| 344 |
#ifdef EXE_DEBUG |
| 345 |
printf("ret.returncode=%i, exe.is_precondition=%i is_valid=%i", ret.returncode, exe.is_precondition, is_valid); |
| 346 |
if (exe.function == fc_tag_has_valid_type) { |
| 347 |
printf("SSS tag %i has valid type: %i==%i (%s)\n", exe.tag, ret.returncode, is_valid, ret.value_found); |
| 348 |
} |
| 349 |
|
| 350 |
#endif |
| 351 |
if (false == exe.is_precondition) { /* no precondition */ |
| 352 |
if (exe.tag >= MINTAGS) { /* exe.tag is 16bit, therefore true: && exe.tag <= (MAXTAGS-1)) */ |
| 353 |
parser_state.called_tags[exe.tag]=true; /* mark tag that it has a rule */ |
| 354 |
} |
| 355 |
} |
| 356 |
/* combine results */ |
| 357 |
if ( |
| 358 |
( true == exe.is_precondition ) && |
| 359 |
|
| 360 |
( |
| 361 |
! ( |
| 362 |
exe.function == fc_tag_has_valid_type && |
| 363 |
( |
| 364 |
ret.returncode == tagerror_unexpected_type_found || |
| 365 |
ret.returncode == tag_does_not_exist |
| 366 |
) |
| 367 |
) |
| 368 |
|
| 369 |
) |
| 370 |
) { /* precondition, next run depends on this result */ |
| 371 |
*precondition_result_p = ret.returncode; |
| 372 |
ret.value_found = empty_str(); |
| 373 |
ret.expected_value = empty_str(); |
| 374 |
//if (NULL != expected_value) { |
| 375 |
// free(expected_value); |
| 376 |
// expected_value = NULL; |
| 377 |
//} |
| 378 |
} else { /* no precondition function */ |
| 379 |
/* if true, execute function */ |
| 380 |
full_res_t full; |
| 381 |
full.tag = exe.tag; |
| 382 |
full.function=exe.function; |
| 383 |
full.lineno=exe.lineno; |
| 384 |
full.returncode=ret.returncode; |
| 385 |
full.expected_value=empty_str(); |
| 386 |
full.found_value=empty_str(); |
| 387 |
if (ret.returncode == tag_does_not_exist) { |
| 388 |
full.found_value = const_str("Tag not exist"); |
| 389 |
} |
| 390 |
else if (ret.returncode != is_valid) { |
| 391 |
full.expected_value = ret.expected_value; |
| 392 |
full.found_value = ret.value_found; |
| 393 |
} |
| 394 |
else { |
| 395 |
ret.value_found= empty_str(); |
| 396 |
} |
| 397 |
result_push( full ); |
| 398 |
*precondition_result_p=is_valid; |
| 399 |
*precondition_function_p = exe.function; |
| 400 |
} |
| 401 |
#ifdef EXE_DEBUG |
| 402 |
printf(" precondition_result=%i\n", *precondition_result_p); |
| 403 |
#endif |
| 404 |
return ret; |
| 405 |
} |
| 406 |
|
| 407 |
static ret_t clean_logical_or_if_precondition_failed (const exe_entry_t * exep, const ret_t * retp) { |
| 408 |
assert(NULL != exep); |
| 409 |
assert(NULL != retp); |
| 410 |
exe_entry_t exe = *exep; |
| 411 |
ret_t ret = *retp; |
| 412 |
#ifdef EXE_DEBUG |
| 413 |
printf ( "\t\t\t\t\t\t\t####################################\n" ); |
| 414 |
printf ( "exe function: %s\n", get_parser_function_description ( exe.function ) ); |
| 415 |
#endif |
| 416 |
if ( exe.function == fc_internal_logic_combine_open ) { /* clean logical or if precondition fails */ |
| 417 |
if (!exe_entry_t_is_empty(parser_state.exe_stack)) { |
| 418 |
do { |
| 419 |
exe_entry_t l_exe = exe_pop(); |
| 420 |
#ifdef EXE_DEBUG |
| 421 |
printf ( "~~~ eliminating Logicals because PRE fails: %s\n", get_parser_function_description ( l_exe.function ) ); |
| 422 |
#endif |
| 423 |
|
| 424 |
if (l_exe.function == fc_internal_logic_combine_close) { break; } |
| 425 |
} while (!exe_entry_t_is_empty(parser_state.exe_stack)); |
| 426 |
} |
| 427 |
#ifdef EXE_DEBUG |
| 428 |
printf ( "\t\t\t\t\t\t\t# ende\n" ); |
| 429 |
#endif |
| 430 |
} |
| 431 |
ret.returncode = is_valid; |
| 432 |
return ret; |
| 433 |
} |
| 434 |
|
| 435 |
/* executes a plan (list) of functions, checks if predicate-function calls are |
| 436 |
* needed, too. The plan is a global variable. |
| 437 |
* |
| 438 |
* HINT: the order of stack is reversed, that means: |
| 439 |
* first, push an execute-function |
| 440 |
* second, push the corresponding precondition |
| 441 |
* |
| 442 |
* Example: "123; optional; only("1")" |
| 443 |
* push fc_tag_has_value 123 <--- test if tag 123 has only value 1, but only if precondition succeeds |
| 444 |
* push fc_tag_quiet 123 <--- precondition, checks if tag exists |
| 445 |
* |
| 446 |
* @param tif pointer to TIFF structure |
| 447 |
* @return return-code is 0 if all called functions are succeeds |
| 448 |
*/ |
| 449 |
void execute_plan (ctiff_t * ctif) { |
| 450 |
/* iterate other function-stack */ |
| 451 |
int precondition_result=is_valid; |
| 452 |
function_t precondition_function=fc_true; |
| 453 |
int * restrict called_tags = parser_state.called_tags; |
| 454 |
|
| 455 |
memset(&called_tags[MINTAGS], false, sizeof(int) * (MAXTAGS-MINTAGS)); |
| 456 |
if (!exe_entry_t_is_empty(parser_state.exe_stack)) { |
| 457 |
bool last_run_was_a_precondition=false; |
| 458 |
#ifdef EXE_DEBUG |
| 459 |
printf("------------------------------------\n"); |
| 460 |
//i_printstack(); |
| 461 |
//r_printstack(); |
| 462 |
exe_printstack(); |
| 463 |
printf("------------------------------------\n"); |
| 464 |
#endif |
| 465 |
do { /* parser_state.exe_stackp > 0 */ |
| 466 |
GET_EMPTY_RET(ret) |
| 467 |
exe_entry_t exe = exe_pop(); |
| 468 |
bool should_we_go_in_fc_call = |
| 469 |
( /* Precondition was valid */ |
| 470 |
(true == last_run_was_a_precondition) && |
| 471 |
(is_valid == precondition_result) |
| 472 |
) || |
| 473 |
( /* Precondition was valid, but has a type warning */ |
| 474 |
(true == last_run_was_a_precondition) && |
| 475 |
(tagwarn_type_of_unknown_tag_could_not_be_checked == precondition_result) |
| 476 |
) || |
| 477 |
|
| 478 |
( /* last was a normal function */ |
| 479 |
false == last_run_was_a_precondition |
| 480 |
); |
| 481 |
#ifdef EXE_DEBUG |
| 482 |
//i_printstack(); |
| 483 |
//exe_printstack(); |
| 484 |
printf("last run was a precondition? %s\n", last_run_was_a_precondition?"true":"false"); |
| 485 |
printf("last run was a precondition? (2) %s\n", (true==last_run_was_a_precondition)?"true":"false"); |
| 486 |
printf(".. precondition_result=%s\n", get_parser_error_description(precondition_result)); |
| 487 |
printf(".. precondition_function=%s\n", get_parser_function_description(precondition_function)); |
| 488 |
printf("should we go in fc call? %s\n", ( /* last was successful precondition */ |
| 489 |
should_we_go_in_fc_call)?"true":"false"); |
| 490 |
printf("parsing function %s (%i) (linecode=%i)\n", get_parser_function_description( exe.function), exe.function, exe.lineno); |
| 491 |
#endif |
| 492 |
if (should_we_go_in_fc_call) { /* if true, execute function */ |
| 493 |
ret = execute_function_and_set_results_for_next_call(ctif, &exe, &ret, &precondition_result, |
| 494 |
&precondition_function); |
| 495 |
} else { // if precondition fails, return precondition if they have unexpected value! |
| 496 |
if (last_run_was_a_precondition) { |
| 497 |
ret = clean_logical_or_if_precondition_failed(&exe, &ret); |
| 498 |
} |
| 499 |
} |
| 500 |
last_run_was_a_precondition = exe.is_precondition; |
| 501 |
assert(ret.returncode != should_not_occur); |
| 502 |
#ifdef EXE_DEBUG |
| 503 |
if (ret.returncode == is_valid) { |
| 504 |
printf("tmpresult = %s {returncode=%i, tag=%i}\n\nexe is precondition=%s\n", get_parser_error_description(ret.returncode), ret.returncode, exe.tag, exe.is_precondition==true?"true":"false");; |
| 505 |
}else{ |
| 506 |
printf("tmpresult = %s {returncode=%i, tag=%i}\n\nexe is precondition=%s\n", get_parser_error_description(ret.returncode), ret.returncode, exe.tag, exe.is_precondition==true?"true":"false");; |
| 507 |
} |
| 508 |
printf("==========\n"); |
| 509 |
#endif |
| 510 |
} while (!exe_entry_t_is_empty(parser_state.exe_stack)); |
| 511 |
} |
| 512 |
#ifdef EXE_DEBUG |
| 513 |
printf("all processed\n"); |
| 514 |
#endif |
| 515 |
#ifndef NOTAGCHECK |
| 516 |
for (unsigned int tag = MINTAGS; tag < MAXTAGS; tag++) { |
| 517 |
|
| 518 |
if ( |
| 519 |
(-1 != TIFFGetRawTagListIndex(ctif->ifd0, tag)) |
| 520 |
&& (false==called_tags[tag]) |
| 521 |
) { // tag found in tiff, but not called |
| 522 |
#ifdef EXE_DEBUG |
| 523 |
// printf("checking %u -> res=%s (%tag)\n", tag, get_parser_error_description(res.returncode), res.returncode); |
| 524 |
#endif |
| 525 |
full_res_t full; |
| 526 |
full.tag = (tag_t) tag; |
| 527 |
full.function = fc_notag; |
| 528 |
full.lineno = -1; |
| 529 |
full.returncode = tagerror_not_white_listed; |
| 530 |
full.expected_value = const_str("Tag should not exist."); |
| 531 |
full.found_value = empty_str(); |
| 532 |
result_push_back(full); |
| 533 |
} |
| 534 |
} |
| 535 |
#endif /* NOTAGCHECK */ |
| 536 |
|
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
/* function to clean an execution plan */ |
| 541 |
void clean_plan (void) { |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
/* helper function for parser */ |
| 547 |
static tag_t settag( tag_t tag) { |
| 548 |
#ifdef RULE_DEBUG |
| 549 |
printf("settag, %i\n", tag); |
| 550 |
#endif |
| 551 |
parser_state.tag=tag; |
| 552 |
return tag; |
| 553 |
} |
| 554 |
|
| 555 |
static tag_t settagref( tag_t tag) { |
| 556 |
#ifdef RULE_DEBUG |
| 557 |
printf("settagref, %i\n", tag); |
| 558 |
#endif |
| 559 |
parser_state.tagref=tag; |
| 560 |
return tag; |
| 561 |
} |
| 562 |
/* helper function for parser */ |
| 563 |
static tag_t gettag(void) { return parser_state.tag;} |
| 564 |
static int incrlineno(void) { |
| 565 |
parser_state.lineno++; |
| 566 |
#ifdef RULE_DEBUG |
| 567 |
printf("##lineno=%i\n", parser_state.lineno); |
| 568 |
#endif |
| 569 |
return parser_state.lineno; |
| 570 |
} |
| 571 |
|
| 572 |
/* helper function for parser */ |
| 573 |
static int getlineno(void) { return parser_state.lineno;} |
| 574 |
|
| 575 |
|
| 576 |
/* helper function for parser */ |
| 577 |
static void commentline(void) { |
| 578 |
#ifdef RULE_DEBUG |
| 579 |
printf("commentline, %i\n", parser_state.lineno); |
| 580 |
#endif |
| 581 |
} |
| 582 |
|
| 583 |
/* helper function for parser */ |
| 584 |
static void set_mandatory(void) { |
| 585 |
#ifdef RULE_DEBUG |
| 586 |
printf("tag '%u' is mandatory\n", gettag()); |
| 587 |
#endif |
| 588 |
parser_state.req=mandatory; |
| 589 |
} |
| 590 |
|
| 591 |
/* helper function for parser */ |
| 592 |
static void set_optional(void) { |
| 593 |
#ifdef RULE_DEBUG |
| 594 |
printf("tag '%u' is optional\n", gettag()); |
| 595 |
#endif |
| 596 |
parser_state.req=optional; |
| 597 |
parser_state.any_reference=any_ref; |
| 598 |
parser_state.tagref=gettag(); |
| 599 |
} |
| 600 |
|
| 601 |
/* helper function for parser */ |
| 602 |
static void set_ifdepends(void) { |
| 603 |
#ifdef RULE_DEBUG |
| 604 |
printf("tag '%u' is set if depends\n", gettag()); |
| 605 |
#endif |
| 606 |
parser_state.req=ifdepends; |
| 607 |
} |
| 608 |
|
| 609 |
/* helper function for parser */ |
| 610 |
static void set_optdepends(void) { |
| 611 |
#ifdef RULE_DEBUG |
| 612 |
printf("tag '%u' is set optional depends\n", gettag()); |
| 613 |
#endif |
| 614 |
parser_state.req=optdepends; |
| 615 |
} |
| 616 |
|
| 617 |
/* helper function for parser */ |
| 618 |
static void regex_push(char * regex_string) { |
| 619 |
PCRE2_SIZE erroffset; |
| 620 |
int errorcode; |
| 621 |
pcre2_code *re = pcre2_compile( |
| 622 |
(unsigned char *) regex_string, /* the pattern */ |
| 623 |
PCRE2_ZERO_TERMINATED, /* the pattern length in code units */ |
| 624 |
0, /* default options */ |
| 625 |
&errorcode, /* for error code */ |
| 626 |
&erroffset, /* for error offset */ |
| 627 |
NULL |
| 628 |
); /* no compile context */ |
| 629 |
if (NULL != re) { |
| 630 |
#ifdef DEBUG |
| 631 |
printf("regex found: '%s' in line %i\n", regex_string, parser_state.lineno); |
| 632 |
#endif |
| 633 |
pcre2_code_free(re); |
| 634 |
r_push(regex_string); |
| 635 |
} else { |
| 636 |
unsigned char errormsg[512]; |
| 637 |
pcre2_get_error_message(errorcode, errormsg, 511); |
| 638 |
fprintf(stderr, "regex compile error: %s at offset: %li in line %i\n", errormsg, erroffset, parser_state.lineno); |
| 639 |
exit(EXIT_FAILURE); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
/* helper function for parser */ |
| 644 |
static void reset_valuelist(void) { |
| 645 |
parser_state.valuelist = 0; |
| 646 |
} |
| 647 |
|
| 648 |
/* helper function for parser */ |
| 649 |
static void incr_values (void) { |
| 650 |
parser_state.valuelist++; |
| 651 |
} |
| 652 |
|
| 653 |
/* prepare functions for preconditions */ |
| 654 |
static exe_entry_t prepare_internal_entry(void) { |
| 655 |
exe_entry_t p; |
| 656 |
p.stack = internal_stack_entry_t_new(); |
| 657 |
p.lineno=getlineno(); |
| 658 |
p.is_precondition=true; |
| 659 |
p.tag=parser_state.tagref; |
| 660 |
p.function=fc_tag_quiet; |
| 661 |
switch (parser_state.any_reference) { |
| 662 |
case no_ref: |
| 663 |
case any_ref: |
| 664 |
p.function=fc_tag_quiet; |
| 665 |
break; |
| 666 |
case only_ref: |
| 667 |
p.function=fc_tag_has_value_quiet; |
| 668 |
exe_i_push(&p, i_pop() ); |
| 669 |
break; |
| 670 |
case range_ref: |
| 671 |
p.function=fc_tag_has_value_in_range; |
| 672 |
exe_i_push(&p, i_pop() ); |
| 673 |
exe_i_push(&p, i_pop() ); |
| 674 |
break; |
| 675 |
case ntupel_ref: |
| 676 |
{ |
| 677 |
unsigned int c = i_pop(); |
| 678 |
for (unsigned int i = 0; i<c; i++) { |
| 679 |
exe_i_push(&p, i_pop() ); |
| 680 |
} |
| 681 |
exe_i_push(&p, c); |
| 682 |
|
| 683 |
p.function=fc_tag_has_valuelist; |
| 684 |
break; |
| 685 |
} |
| 686 |
case regex_ref: |
| 687 |
p.function=fc_tag_has_value_matching_regex; |
| 688 |
exe_regex_push(&p, r_pop()); |
| 689 |
break; |
| 690 |
} |
| 691 |
return p; |
| 692 |
} |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
static void build_functional_structure__range(exe_entry_t * e_p) { |
| 697 |
#ifdef RULE_DEBUG |
| 698 |
printf("range found\n"); |
| 699 |
#endif |
| 700 |
exe_i_push(e_p, i_pop() ); |
| 701 |
exe_i_push(e_p, i_pop() ); |
| 702 |
e_p->function=fc_tag_has_value_in_range; |
| 703 |
} |
| 704 |
|
| 705 |
static void build_functional_structure__ntupel(exe_entry_t * e_p) { |
| 706 |
#ifdef RULE_DEBUG |
| 707 |
printf("ntupel found\n"); |
| 708 |
#endif |
| 709 |
unsigned int c = i_pop(); |
| 710 |
for (unsigned int i = 0; i<c; i++) { |
| 711 |
exe_i_push(e_p, i_pop() ); |
| 712 |
} |
| 713 |
exe_i_push(e_p, c); |
| 714 |
e_p->function=fc_tag_has_valuelist; |
| 715 |
} |
| 716 |
static void build_functional_structure__only(exe_entry_t * e_p) { |
| 717 |
#ifdef RULE_DEBUG |
| 718 |
printf("only found\n"); |
| 719 |
#endif |
| 720 |
exe_i_push(e_p, i_pop() ); |
| 721 |
e_p->function=fc_tag_has_value; |
| 722 |
} |
| 723 |
|
| 724 |
static void build_functional_structure__any(exe_entry_t * e_p) { |
| 725 |
#ifdef RULE_DEBUG |
| 726 |
printf("any found\n"); |
| 727 |
#endif |
| 728 |
e_p->function=fc_tag; |
| 729 |
} |
| 730 |
|
| 731 |
static void build_functional_structure__regex(exe_entry_t * e_p) { |
| 732 |
#ifdef RULE_DEBUG |
| 733 |
printf("regex found\n"); |
| 734 |
#endif |
| 735 |
exe_regex_push(e_p, r_pop()); |
| 736 |
e_p->function=fc_tag_has_value_matching_regex; |
| 737 |
} |
| 738 |
|
| 739 |
static void build_functional_structure__iccprofile(exe_entry_t * e_p) { |
| 740 |
#ifdef RULE_DEBUG |
| 741 |
printf("iccprofile found\n"); |
| 742 |
#endif |
| 743 |
e_p->function=fc_icc; |
| 744 |
} |
| 745 |
|
| 746 |
static void build_functional_structure__datetime(exe_entry_t * e_p) { |
| 747 |
#ifdef RULE_DEBUG |
| 748 |
printf("datetime found\n"); |
| 749 |
#endif |
| 750 |
e_p->function=fc_datetime; |
| 751 |
} |
| 752 |
|
| 753 |
static void build_functional_structure__printable_ascii(exe_entry_t * e_p) { |
| 754 |
#ifdef RULE_DEBUG |
| 755 |
printf("printable_ascii found\n"); |
| 756 |
#endif |
| 757 |
exe_regex_push(e_p, "^[[:print:]]*$"); |
| 758 |
e_p->function=fc_tag_has_value_matching_regex; |
| 759 |
} |
| 760 |
|
| 761 |
static void build_functional_structure__full_ascii(exe_entry_t * e_p) { |
| 762 |
#ifdef RULE_DEBUG |
| 763 |
printf("full_ascii found\n"); |
| 764 |
#endif |
| 765 |
exe_regex_push(e_p, "^[[:ascii:]]*$"); |
| 766 |
e_p->function=fc_tag_has_value_matching_regex; |
| 767 |
} |
| 768 |
/* builds an entry structure holding function and their values */ |
| 769 |
static void build_functional_structure(exe_entry_t * e_p, values_t val) { |
| 770 |
switch (val) { |
| 771 |
case range: |
| 772 |
build_functional_structure__range(e_p); |
| 773 |
break; |
| 774 |
case ntupel: |
| 775 |
build_functional_structure__ntupel(e_p); |
| 776 |
break; |
| 777 |
case only: |
| 778 |
build_functional_structure__only(e_p); |
| 779 |
break; |
| 780 |
case any: |
| 781 |
build_functional_structure__any(e_p); |
| 782 |
break; |
| 783 |
case regex: |
| 784 |
build_functional_structure__regex(e_p); |
| 785 |
break; |
| 786 |
case iccprofile: |
| 787 |
build_functional_structure__iccprofile(e_p); |
| 788 |
break; |
| 789 |
case datetime: |
| 790 |
build_functional_structure__datetime(e_p); |
| 791 |
break; |
| 792 |
|
| 793 |
case printable_ascii: |
| 794 |
build_functional_structure__printable_ascii(e_p); |
| 795 |
break; |
| 796 |
case full_ascii: |
| 797 |
build_functional_structure__full_ascii(e_p); |
| 798 |
break; |
| 799 |
/* TODO: |
| 800 |
case sbit: { |
| 801 |
#ifdef RULE_DEBUG |
| 802 |
printf("sbit found\n"); |
| 803 |
#endif |
| 804 |
exe_regex_push(e_p, r_pop()); |
| 805 |
e_p->function=fc_tag_has_value; |
| 806 |
break; |
| 807 |
} |
| 808 |
*/ |
| 809 |
default: |
| 810 |
fprintf(stderr, "unknown val %u, should not occur\n", val); |
| 811 |
exit(EXIT_FAILURE); |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
/* add preconditions to stack, first comes the real function, than the preconditions! */ |
| 816 |
static void evaluate_req_and_push_exe(requirements_t req, exe_entry_t e) { |
| 817 |
assert( e.function != fc_dummy); |
| 818 |
#ifdef RULE_DEBUG |
| 819 |
printf("eval e=%s\n", get_parser_function_description(e.function)); |
| 820 |
#endif |
| 821 |
|
| 822 |
if (e.function == fc_internal_logic_combine_close) { |
| 823 |
#ifdef RULE_DEBUG |
| 824 |
printf("\n### eval req logical close found\n"); |
| 825 |
#endif |
| 826 |
e.function = fc_internal_logic_combine_close; |
| 827 |
exe_push(e); // TODO: exe_push(e); /* <-- logical close */ |
| 828 |
// TODO: exe_push(e); /* <-- real rule(s) */ |
| 829 |
for (int i = 0; i< parser_state.logical_elements; i++) { |
| 830 |
values_t val = v_pop(); |
| 831 |
e.function = fc_dummy; |
| 832 |
build_functional_structure(&e, val); /* <-- this builds the tiff test */ |
| 833 |
#ifdef RULE_DEBUG |
| 834 |
printf("###### i=%i e=%s\n", i, get_parser_function_description(e.function)); |
| 835 |
#endif |
| 836 |
exe_push(e); |
| 837 |
}; |
| 838 |
e.function=fc_internal_logic_combine_open; |
| 839 |
} |
| 840 |
exe_push(e); |
| 841 |
switch (req) { |
| 842 |
case ifdepends: |
| 843 |
case optional: |
| 844 |
{ |
| 845 |
exe_entry_t p = prepare_internal_entry(); |
| 846 |
|
| 847 |
if (parser_state.mode & mode_enable_type_checks) { |
| 848 |
exe_entry_t z = e; |
| 849 |
z.is_precondition = true; |
| 850 |
z.function = fc_tag_has_valid_type; |
| 851 |
exe_push(z); |
| 852 |
} |
| 853 |
exe_push(p); |
| 854 |
break; |
| 855 |
} |
| 856 |
case mandatory: |
| 857 |
{ |
| 858 |
|
| 859 |
if (parser_state.mode & mode_enable_type_checks) { |
| 860 |
exe_entry_t z = e; |
| 861 |
z.is_precondition = true; |
| 862 |
z.function = fc_tag_has_valid_type; |
| 863 |
exe_push(z); |
| 864 |
} |
| 865 |
break; |
| 866 |
} |
| 867 |
case optdepends: |
| 868 |
{ |
| 869 |
exe_entry_t pp; |
| 870 |
pp.stack = internal_stack_entry_t_new(); |
| 871 |
pp.lineno = getlineno(); |
| 872 |
pp.is_precondition = true; |
| 873 |
pp.tag = e.tag; |
| 874 |
pp.function = fc_tag_quiet; |
| 875 |
|
| 876 |
exe_entry_t p = prepare_internal_entry(); |
| 877 |
|
| 878 |
if (parser_state.mode & mode_enable_type_checks) { |
| 879 |
exe_entry_t z = e; |
| 880 |
z.is_precondition = true; |
| 881 |
z.function = fc_tag_has_valid_type; |
| 882 |
exe_push(z); |
| 883 |
} |
| 884 |
exe_push(p); |
| 885 |
exe_push(pp); |
| 886 |
break; |
| 887 |
} |
| 888 |
default: |
| 889 |
{ |
| 890 |
fprintf(stderr, "unknown parserstate.req (%i), should not occur\n", parser_state.req); |
| 891 |
exit(EXIT_FAILURE); |
| 892 |
} |
| 893 |
|
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
static void set_rule_logical_open(void) { |
| 898 |
#ifdef RULE_DEBUG |
| 899 |
printf("rule_add_logical_config\n"); |
| 900 |
#endif |
| 901 |
parser_state.within_logical_or=true; |
| 902 |
} |
| 903 |
|
| 904 |
static void incr_logical_elements(void) { |
| 905 |
#ifdef RULE_DEBUG |
| 906 |
printf("incr logical elements to %i\n", parser_state.logical_elements+1); |
| 907 |
#endif |
| 908 |
|
| 909 |
parser_state.logical_elements++; |
| 910 |
} |
| 911 |
|
| 912 |
/* helper function for parser */ |
| 913 |
static void set_rule_logical_close(void) { |
| 914 |
#ifdef RULE_DEBUG |
| 915 |
printf("reset_logical_list\n"); |
| 916 |
#endif |
| 917 |
exe_entry_t e; |
| 918 |
e.tag = parser_state.tag; |
| 919 |
e.stack = internal_stack_entry_t_new(); |
| 920 |
e.lineno=getlineno(); |
| 921 |
e.is_precondition=false; |
| 922 |
e.function=fc_internal_logic_combine_close; |
| 923 |
//exe_push(e); |
| 924 |
evaluate_req_and_push_exe( parser_state.req, e); |
| 925 |
parser_state.within_logical_or=false; |
| 926 |
parser_state.logical_elements=0; |
| 927 |
} |
| 928 |
|
| 929 |
/* this adds the config of a tagline to execution plan |
| 930 |
* HINT: order of calling arguments from stacks is IMPORTANT! */ |
| 931 |
static void rule_addtag_config(void) { |
| 932 |
if(!values_t_is_empty(parser_state.values_stack)) { |
| 933 |
#ifdef RULE_DEBUG |
| 934 |
printf("rule_addtag_config\n"); |
| 935 |
#endif |
| 936 |
exe_entry_t e; |
| 937 |
e.stack =internal_stack_entry_t_new(); |
| 938 |
e.lineno=getlineno(); |
| 939 |
e.is_precondition=false; /* no precondition as default */ |
| 940 |
e.tag = parser_state.tag; |
| 941 |
|
| 942 |
#ifdef RULE_DEBUG |
| 943 |
printf( "try to match tagline at line %i\n", e.lineno); |
| 944 |
#endif |
| 945 |
/* HINT: order of evaluating last val and last req is IMPORTANT! */ |
| 946 |
/* stack should be organized as: |
| 947 |
* [ruleA] |
| 948 |
* [preconditionA] <- optional |
| 949 |
* [prepreconditionA] <- optional |
| 950 |
* [ruleB] |
| 951 |
* [preconditionB] <- optional |
| 952 |
* [prepreconditionB] <- optional |
| 953 |
*/ |
| 954 |
do { |
| 955 |
values_t val = v_pop(); |
| 956 |
e.function = fc_dummy; |
| 957 |
build_functional_structure(&e, val); /* <-- this builds the tiff test */ |
| 958 |
/* set predicate if and only if lastreq = depends */ |
| 959 |
/* HINT: order of evaluating last val and last req is IMPORTANT! */ |
| 960 |
/* HINT: order of calling arguments from stacks is IMPORTANT! */ |
| 961 |
evaluate_req_and_push_exe( parser_state.req, e); /* <-- this sets the predicates */ |
| 962 |
} while (!values_t_is_empty(parser_state.values_stack)); |
| 963 |
reset_valuelist(); |
| 964 |
parser_state.any_reference = 0; |
| 965 |
#ifdef RULE_DEBUG |
| 966 |
printf("ENDE rule_addtag\n"); |
| 967 |
printf("\n\n\n"); |
| 968 |
|
| 969 |
#endif |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
/* set_mode */ |
| 974 |
static void set_mode(modes_t mode) { |
| 975 |
#ifdef RULE_DEBUG |
| 976 |
printf("Mode=%i at line=%i (needs to be implemented)\n", mode, parser_state.lineno ); |
| 977 |
#endif |
| 978 |
exe_entry_t e; |
| 979 |
e.stack=internal_stack_entry_t_new(); |
| 980 |
e.lineno=getlineno(); |
| 981 |
e.is_precondition=false; /* no precondition as default */ |
| 982 |
e.tag = parser_state.tag; |
| 983 |
|
| 984 |
switch (mode) { |
| 985 |
case mode_enable_offset_checks: { |
| 986 |
e.function = fc_all_offsets_are_word_aligned; |
| 987 |
exe_push(e); |
| 988 |
e.function = fc_all_offsets_are_used_once_only; |
| 989 |
exe_push(e); |
| 990 |
e.function = fc_all_offsets_are_not_zero; |
| 991 |
exe_push(e); |
| 992 |
e.function = fc_all_offsets_within_filesize; |
| 993 |
exe_push(e); |
| 994 |
break; |
| 995 |
} |
| 996 |
case mode_enable_ifd_checks: { |
| 997 |
e.function = fc_all_IFDs_have_tags; |
| 998 |
exe_push(e); |
| 999 |
e.function = fc_all_IFDs_are_word_aligned; |
| 1000 |
exe_push(e); |
| 1001 |
e.function = fc_tagorder; |
| 1002 |
exe_push(e); |
| 1003 |
e.function = fc_check_cardinality; |
| 1004 |
exe_push(e); |
| 1005 |
break; |
| 1006 |
} |
| 1007 |
case mode_baseline: { |
| 1008 |
e.function = fc_has_only_one_ifd; |
| 1009 |
exe_push(e); |
| 1010 |
break; |
| 1011 |
} |
| 1012 |
case mode_enable_deep_geotiff_checks: { |
| 1013 |
e.function = fc_all_geotiff_tags_have_same_count_of_values; |
| 1014 |
exe_push(e); |
| 1015 |
break; |
| 1016 |
} |
| 1017 |
case mode_enable_type_checks: { /* nothing, because we must enable it only in rule_addtag_config */ |
| 1018 |
} |
| 1019 |
} |
| 1020 |
parser_state.mode |= mode; |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
/* reset the parser state */ |
| 1025 |
static void reset_parser_state(void) { |
| 1026 |
parser_state.any_reference=0; |
| 1027 |
parser_state.exe_stack = exe_entry_t_new(); |
| 1028 |
parser_state.values_stack = values_t_new(); |
| 1029 |
parser_state.includedepth=0; |
| 1030 |
parser_state.lineno=1; |
| 1031 |
parser_state.req=0; |
| 1032 |
parser_state.result_stack = full_res_t_new(); |
| 1033 |
parser_state.intermediate_stack = internal_stack_entry_t_new(); |
| 1034 |
parser_state.tag=0; |
| 1035 |
parser_state.tagref=0; |
| 1036 |
parser_state.valuelist=0; |
| 1037 |
parser_state.mode=0; |
| 1038 |
parser_state.within_logical_or=false; |
| 1039 |
parser_state.logical_elements=0; |
| 1040 |
/* |
| 1041 |
int * restrict called_tags = parser_state.called_tags; |
| 1042 |
for (register unsigned int i=0; i<MAXTAGS; i++) { |
| 1043 |
called_tags[i]= false; |
| 1044 |
} |
| 1045 |
*/ |
| 1046 |
memset(parser_state.called_tags, (int) false, sizeof(unsigned int)*MAXTAGS); |
| 1047 |
} |
| 1048 |
|
| 1049 |
/* include the PEG generated parser, see "man peg" for details */ |
| 1050 |
#include "config_dsl.grammar.c" /* yyparse() */ |
| 1051 |
|
| 1052 |
/* set_include */ |
| 1053 |
static void set_include( const char * include_file ) { |
| 1054 |
if (parser_state.includedepth >= 1) { |
| 1055 |
fprintf( stderr, "only include depth of %i is supported\n", MAXINCLUDEDEPTH); |
| 1056 |
exit (EXIT_FAILURE); |
| 1057 |
} |
| 1058 |
parser_state.includedepth++; |
| 1059 |
/* check if path is relative, then add current parent path */ |
| 1060 |
char * include_file_expanded = NULL; |
| 1061 |
const char * current_file = parser_state.cfg_filename; |
| 1062 |
|
| 1063 |
#ifndef __WIN32__ |
| 1064 |
if (include_file[0] != '/' && current_file != NULL) { |
| 1065 |
char * absolute_current = realpath(current_file, NULL); |
| 1066 |
const char * dirname_current = dirname( absolute_current ); |
| 1067 |
size_t dirname_len = strlen( dirname_current); |
| 1068 |
size_t include_len = strlen( include_file); |
| 1069 |
include_file_expanded = calloc((dirname_len + include_len + 2), sizeof (char)); /* 2 because EOS + "/" */ |
| 1070 |
strcat( include_file_expanded, dirname_current); |
| 1071 |
const char * delim = "/"; |
| 1072 |
strcat( include_file_expanded, delim); |
| 1073 |
strcat( include_file_expanded, include_file); |
| 1074 |
free( absolute_current ); |
| 1075 |
} else { |
| 1076 |
include_file_expanded = strdup(include_file); |
| 1077 |
} |
| 1078 |
#else |
| 1079 |
/* if anybody sends a patch to detect if absolute path under MS Windows is ued, the code could be included. |
| 1080 |
* Otherwise the config-file should ue absolute paths instead |
| 1081 |
*/ |
| 1082 |
include_file_expanded = strdup(include_file); |
| 1083 |
#endif |
| 1084 |
printf("Include='%s'\n", include_file_expanded); |
| 1085 |
FILE * cfg = fopen(include_file_expanded, "r"); |
| 1086 |
if (NULL == cfg) { |
| 1087 |
fprintf( stderr, "file '%s' could not be opened\n", include_file_expanded); |
| 1088 |
exit (EXIT_FAILURE); |
| 1089 |
}; |
| 1090 |
clean_plan(); |
| 1091 |
yycontext ctx2 = {0}; |
| 1092 |
FILE * old_stream = parser_state.stream; |
| 1093 |
int old_lineno = parser_state.lineno; |
| 1094 |
const char * old_file = parser_state.cfg_filename; |
| 1095 |
printf("At lineno %i, file '%s', switching to include file '%s' (expanded: '%s')\n", old_lineno, parser_state.cfg_filename, include_file, include_file_expanded); |
| 1096 |
parser_state.lineno=1; |
| 1097 |
parser_state.stream=cfg; |
| 1098 |
while (yyparse(&ctx2)) /* repeat until EOF */ |
| 1099 |
; |
| 1100 |
yyrelease(&ctx2); |
| 1101 |
parser_state.includedepth--; |
| 1102 |
printf("End of Include=%s\n", include_file); |
| 1103 |
parser_state.stream = old_stream; |
| 1104 |
parser_state.lineno = old_lineno; |
| 1105 |
parser_state.cfg_filename = old_file; |
| 1106 |
fclose(cfg); |
| 1107 |
free( include_file_expanded); |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
/* function to parse a config file from STDIN */ |
| 1112 |
void parse_plan (void) { |
| 1113 |
reset_parser_state(); |
| 1114 |
yycontext ctx = {0}; |
| 1115 |
parser_state.stream=stdin; |
| 1116 |
parser_state.cfg_filename=NULL; |
| 1117 |
while (yyparse(&ctx)) /* repeat until EOF */ |
| 1118 |
; |
| 1119 |
yyrelease(&ctx); |
| 1120 |
|
| 1121 |
} |
| 1122 |
|
| 1123 |
/* function to parse a config file from file stream */ |
| 1124 |
void parse_plan_via_stream( FILE * file, const char * optional_cfg_file ) { |
| 1125 |
reset_parser_state(); |
| 1126 |
yycontext ctx = {0}; |
| 1127 |
parser_state.stream=file; |
| 1128 |
parser_state.cfg_filename=optional_cfg_file; |
| 1129 |
while (yyparse(&ctx)) /* repeat until EOF */ |
| 1130 |
; |
| 1131 |
yyrelease(&ctx); |
| 1132 |
} |
| 1133 |
|
| 1134 |
/* function to parse a config file */ |
| 1135 |
void parse_plan_via_file( const char * cfg_file ) { |
| 1136 |
FILE * cfg = fopen(cfg_file, "r"); |
| 1137 |
if (NULL == cfg) { |
| 1138 |
fprintf( stderr, "file '%s' could not be opened\n", cfg_file); |
| 1139 |
exit (EXIT_FAILURE); |
| 1140 |
}; |
| 1141 |
clean_plan(); |
| 1142 |
parse_plan_via_stream( cfg, cfg_file ); |
| 1143 |
fclose(cfg); |
| 1144 |
} |
| 1145 |
|
| 1146 |
/* set parse error |
| 1147 |
* @param msg describes the details about what was going wrong |
| 1148 |
* @param yytext gives context of the error |
| 1149 |
*/ |
| 1150 |
static void set_parse_error(const char * msg, char * yytext) { |
| 1151 |
fprintf(stderr, "%s at line %i (error at '%s')\n", msg, parser_state.lineno, yytext); |
| 1152 |
exit(EXIT_FAILURE); |
| 1153 |
} |
| 1154 |
|
| 1155 |
static void clean_plan_results(void) { |
| 1156 |
full_res_t_delete(parser_state.result_stack); |
| 1157 |
// free(parser_state.result_stack); |
| 1158 |
parser_state.result_stack = full_res_t_new(); |
| 1159 |
} |
| 1160 |
|
| 1161 |
/* prints a plan (list) of functions and their results*/ |
| 1162 |
ret_t print_plan_results(retmsg_t * actual_render) { |
| 1163 |
GET_EMPTY_RET(res) |
| 1164 |
#ifdef DEBUG |
| 1165 |
printf("print plan results:\n"); |
| 1166 |
printf("####################(\n"); |
| 1167 |
full_res_t_print_top( parser_state.result_stack); |
| 1168 |
printf(")####################\n"); |
| 1169 |
printf("reduced\n"); |
| 1170 |
#endif |
| 1171 |
|
| 1172 |
#ifdef EXE_DEBUG |
| 1173 |
result_printstack(); |
| 1174 |
#endif |
| 1175 |
reduce_results(); |
| 1176 |
#ifdef EXE_DEBUG |
| 1177 |
result_printstack(); |
| 1178 |
#endif |
| 1179 |
size_t count_of_all_results = full_res_t_size(parser_state.result_stack); |
| 1180 |
size_t count_of_valid_results = 0; |
| 1181 |
while(!full_res_t_is_empty(parser_state.result_stack)) { |
| 1182 |
full_res_t parser_result = full_res_t_pop(parser_state.result_stack); |
| 1183 |
#ifdef DEBUG |
| 1184 |
full_res_t_print_top( parser_state.result_stack); |
| 1185 |
#endif |
| 1186 |
returncode_t returncode = parser_result.returncode; |
| 1187 |
/* fill render pipeline */ |
| 1188 |
uint16 tag = parser_result.tag; |
| 1189 |
/* fill with errorcodes */ |
| 1190 |
rm_type_t type= rm_default; |
| 1191 |
switch ( returncode ) { |
| 1192 |
/* add rm_type */ |
| 1193 |
case is_valid: type = rm_is_valid; count_of_valid_results++; break; |
| 1194 |
case parser_logicalor_error: type = rm_logicalor_error; break; |
| 1195 |
case could_not_allocate_memory: |
| 1196 |
case could_not_print: |
| 1197 |
case should_not_occur: assert ( 1 ); |
| 1198 |
case code_error_streampointer_empty: |
| 1199 |
case code_error_filedescriptor_empty: |
| 1200 |
case code_error_ctif_empty: |
| 1201 |
case pcre_compile_error: |
| 1202 |
case tiff_seek_error_header: |
| 1203 |
case tiff_read_error_header: |
| 1204 |
case tiff_seek_error_offset: |
| 1205 |
case tiff_read_error_offset: |
| 1206 |
type = rm_hard_error; break; |
| 1207 |
default: type = rm_error; break; |
| 1208 |
} |
| 1209 |
add_to_render_pipeline(&actual_render, empty_str(), type); |
| 1210 |
|
| 1211 |
/* fill with tag infos */ |
| 1212 |
if (tag > 0) { |
| 1213 |
char tagstr [VALUESTRLEN]; |
| 1214 |
snprintf(tagstr, sizeof(tagstr), "tag %i (%s)", tag, TIFFTagName(tag)); |
| 1215 |
add_to_render_pipeline(&actual_render, str( tagstr), rm_tag); |
| 1216 |
} else { |
| 1217 |
add_to_render_pipeline(&actual_render, const_str("general"), rm_mode); |
| 1218 |
} |
| 1219 |
|
| 1220 |
/* fill with rule infos */ |
| 1221 |
add_to_render_pipeline(&actual_render, str( get_parser_function_description(parser_result.function)), rm_rule); |
| 1222 |
add_to_render_pipeline(&actual_render, parser_result.expected_value, rm_expected); |
| 1223 |
/* fill with value found */ |
| 1224 |
if (returncode != is_valid) { |
| 1225 |
add_to_render_pipeline(&actual_render, str( get_parser_error_description(returncode)), rm_error_description); |
| 1226 |
add_to_render_pipeline(&actual_render, parser_result.found_value, rm_value); |
| 1227 |
} |
| 1228 |
/* fill with lineno */ |
| 1229 |
char msg[5]; |
| 1230 |
snprintf(msg, 5, "%i", parser_result.lineno); |
| 1231 |
add_to_render_pipeline(&actual_render, str(msg), rm_lineno); |
| 1232 |
|
| 1233 |
/* fill with newline */ |
| 1234 |
add_to_render_pipeline(&actual_render, empty_str(), rm_endrule); |
| 1235 |
if (type == rm_hard_error) { goto harderrors; } |
| 1236 |
|
| 1237 |
/* printf( "%s", renderer( parser_state.result_stack[i].result)); */ |
| 1238 |
} |
| 1239 |
harderrors: |
| 1240 |
summarize_results(count_of_all_results, count_of_valid_results, &actual_render, &res); |
| 1241 |
add_to_render_pipeline(&actual_render, empty_str(), rm_endtiff); |
| 1242 |
clean_plan_results(); |
| 1243 |
return res; |
| 1244 |
} |
| 1245 |
|
| 1246 |
void summarize_results(size_t count_of_all_results, size_t count_of_valid_results, retmsg_t **actual_render, ret_t *res) { |
| 1247 |
size_t errors = (count_of_all_results - count_of_valid_results); |
| 1248 |
char emsg[25]; |
| 1249 |
char msg[25]; |
| 1250 |
snprintf(msg, 25, "Found %zi valid results", count_of_valid_results); |
| 1251 |
snprintf(emsg, 25, "Found %zi errors", errors); |
| 1252 |
add_to_render_pipeline(actual_render, str(msg), rm_count_valid); |
| 1253 |
if (errors > 0) { |
| 1254 |
(*res).returncode = should_not_occur; |
| 1255 |
add_to_render_pipeline(actual_render, str(emsg), rm_count_invalid); |
| 1256 |
add_to_render_pipeline(actual_render, const_str("No, the given tif is not valid :("), |
| 1257 |
rm_summary_invalid); |
| 1258 |
} else { |
| 1259 |
(*res).returncode = is_valid; |
| 1260 |
add_to_render_pipeline(actual_render, const_str("Yes, the given tif is valid :)"), |
| 1261 |
rm_summary_valid); |
| 1262 |
} |
| 1263 |
} |
| 1264 |
|
| 1265 |
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 smarttab expandtab :*/ |