| 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 |
* this module implements a recursive descent parser |
| 8 |
*/ |
| 9 |
#include <assert.h> |
| 10 |
#include <ctype.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include <stddef.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
typedef struct { |
| 18 |
int ctx; |
| 19 |
size_t consumed; |
| 20 |
char * buff; |
| 21 |
size_t size; |
| 22 |
} input_t; |
| 23 |
|
| 24 |
const char eol='\n'; |
| 25 |
|
| 26 |
static input_t * dup_input(input_t * after) { |
| 27 |
input_t * dup = alloca( sizeof(input_t) ); |
| 28 |
dup->consumed = after->consumed; |
| 29 |
dup->size = after->size; |
| 30 |
dup->ctx = after->ctx; |
| 31 |
dup->buff = after->buff; |
| 32 |
return dup; |
| 33 |
} |
| 34 |
|
| 35 |
static char * take (const input_t * before, const input_t * after) { |
| 36 |
ssize_t len = after->consumed - before->consumed; |
| 37 |
assert(len >= 0); |
| 38 |
char *newstr = calloc(sizeof(char), len + 1); |
| 39 |
if (NULL == newstr) { |
| 40 |
perror("could not allocate memory for string\n"); |
| 41 |
exit(EXIT_FAILURE); |
| 42 |
} |
| 43 |
if (len > 0) { |
| 44 |
strncpy(newstr, before->buff + before->consumed, len); |
| 45 |
} |
| 46 |
return newstr; |
| 47 |
} |
| 48 |
|
| 49 |
static bool preview_if_ch(const input_t * input, const char ch) { |
| 50 |
if (input->size > 0 && input->consumed < input->size) { |
| 51 |
if (input->buff[input->consumed] == ch) { |
| 52 |
return true; |
| 53 |
} |
| 54 |
} |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
static bool eat_if_ch(input_t * input, const char ch) { |
| 60 |
if (preview_if_ch(input, ch)) { |
| 61 |
input->consumed++; |
| 62 |
return true; |
| 63 |
} |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
static bool preview_if(input_t * input, const char * string) { |
| 68 |
if (input->size > 0) { |
| 69 |
if (NULL != string) { |
| 70 |
size_t consumed = input->consumed; |
| 71 |
while ( |
| 72 |
(consumed < input->size) |
| 73 |
&& ( string[consumed - input->consumed] != '\0' ) |
| 74 |
&& (input->buff[consumed] == string[consumed - input->consumed]) |
| 75 |
) { |
| 76 |
consumed++; |
| 77 |
} |
| 78 |
if (consumed == strlen(string)) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
return false; |
| 84 |
} |
| 85 |
static bool eat_if(input_t * input, const char * string) { |
| 86 |
if (input->size > 0) { |
| 87 |
if (NULL != string) { |
| 88 |
size_t consumed = input->consumed; |
| 89 |
while ( |
| 90 |
(consumed < input->size) |
| 91 |
&& ( string[consumed - input->consumed] != '\0' ) |
| 92 |
&& (input->buff[consumed] == string[consumed - input->consumed]) |
| 93 |
) { |
| 94 |
consumed++; |
| 95 |
} |
| 96 |
if (consumed == strlen(string)) { |
| 97 |
input->consumed = consumed; |
| 98 |
return true; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
static bool preview_until(input_t * input, const char disgusting) { |
| 106 |
for (size_t i=input->consumed; i<input->size; i++) { |
| 107 |
if (input->buff[i] == disgusting) { |
| 108 |
return true; |
| 109 |
} |
| 110 |
} |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
static bool eat_until(input_t * input, const char disgusting) { |
| 115 |
for (size_t i=input->consumed; i<input->size; i++) { |
| 116 |
if (input->buff[i] == disgusting) { |
| 117 |
if (i > input->consumed) { |
| 118 |
input->consumed=i-1; |
| 119 |
} |
| 120 |
return true; |
| 121 |
} |
| 122 |
} |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
static bool prs_commentline(input_t * input) { |
| 127 |
if (eat_if_ch(input, eol)) { |
| 128 |
eat_until(input, eol); |
| 129 |
return true; |
| 130 |
} |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
static bool prs_path(input_t * input) { |
| 135 |
/* |
| 136 |
[A-Z] |
| 137 |
[a-z] |
| 138 |
[0-9] |
| 139 |
'.' |
| 140 |
'_' |
| 141 |
'-' |
| 142 |
SlashorBackslash |
| 143 |
*/ |
| 144 |
for (size_t i=input->consumed; i<input->size; i++) { |
| 145 |
char ch = input->buff[i]; |
| 146 |
if (!isalnum(ch) && ch != '.' && ch != '_' && ch != '-' && ch != '/' && ch != '\\') { |
| 147 |
if (i > input->consumed) { |
| 148 |
input->consumed=i-1; |
| 149 |
} |
| 150 |
return true; |
| 151 |
} |
| 152 |
} |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
static bool prs_includeline(input_t * input) { |
| 158 |
if ( eat_if(input, "include(") ) { |
| 159 |
input_t * before = dup_input(input); |
| 160 |
if (prs_path(input)) { |
| 161 |
input_t * after = dup_input(input); |
| 162 |
if( eat_if_ch(input, ')') ) { |
| 163 |
/* set_include PATH */ |
| 164 |
char * path = take(before, after); |
| 165 |
printf("INCLUDE PATH:'%s'\n", path); |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
static bool prs_tagline(input_t * input) { |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
static bool prs_linetype() { |
| 178 |
return ( |
| 179 |
prs_tagline() |
| 180 |
|| prs_modeline() |
| 181 |
|| prs_includeline() |
| 182 |
|| prs_commentline() |
| 183 |
|| prs_spaceline() |
| 184 |
); |
| 185 |
} |
| 186 |
|
| 187 |
static bool prs_line() { |
| 188 |
return ( prs_line_type() && prs_eol() ) |
| 189 |
|| prs_error(); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
void * prs_start() { |
| 194 |
while(prs_line() ); |
| 195 |
prs_eof() || prs_error(); |
| 196 |
} |