clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name apply.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/tmp/build/foma/foma-0.10.0+g279~a2d32b38 -resource-dir /usr/lib/llvm-16/lib/clang/16 -D _GNU_SOURCE -D foma_shared_EXPORTS -I /tmp/build/foma/foma-0.10.0+g279~a2d32b38 -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-missing-field-initializers -Wno-deprecated -Wno-unused-parameter -std=c18 -fdebug-compilation-dir=/tmp/build/foma/foma-0.10.0+g279~a2d32b38 -ferror-limit 19 -fvisibility=hidden -fgnuc-version=4.2.1 -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/build/foma/scan-build/2024-09-11-155945-2678-1 -x c /tmp/build/foma/foma-0.10.0+g279~a2d32b38/apply.c
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | #include <stdio.h> |
19 | #include <stdlib.h> |
20 | #include <time.h> |
21 | #include <string.h> |
22 | #include <limits.h> |
23 | #include "foma.h" |
24 | |
25 | #define RANDOM 1 |
26 | #define ENUMERATE 2 |
27 | #define MATCH 4 |
28 | #define UP 8 |
29 | #define DOWN 16 |
30 | #define LOWER 32 |
31 | #define UPPER 64 |
32 | #define SPACE 128 |
33 | |
34 | #define FAIL 0 |
35 | #define SUCCEED 1 |
36 | |
37 | #define DEFAULT_OUTSTRING_SIZE 4096 |
38 | #define DEFAULT_STACK_SIZE 128 |
39 | |
40 | #define APPLY_BINSEARCH_THRESHOLD 10 |
41 | |
42 | #define BITMASK(b) (1 << ((b) & 7)) |
43 | #define BITSLOT(b) ((b) >> 3) |
44 | #define BITSET(a,b) ((a)[BITSLOT(b)] |= BITMASK(b)) |
45 | #define BITCLEAR(a,b) ((a)[BITSLOT(b)] &= ~BITMASK(b)) |
46 | #define BITTEST(a,b) ((a)[BITSLOT(b)] & BITMASK(b)) |
47 | #define BITNSLOTS(nb) ((nb + CHAR_BIT - 1) / CHAR_BIT) |
48 | |
49 | |
50 | |
51 | static int apply_append(struct apply_handle *h, int cptr, int sym); |
52 | static char *apply_net(struct apply_handle *h); |
53 | static void apply_create_statemap(struct apply_handle *h,struct fsm *net); |
54 | static void apply_create_sigarray(struct apply_handle *h,struct fsm *net); |
55 | static void apply_create_sigmatch(struct apply_handle *h); |
56 | int apply_match_length(struct apply_handle *h, int symbol); |
57 | static int apply_match_str(struct apply_handle *h,int symbol, int position); |
58 | static void apply_add_flag(struct apply_handle *h,char *name); |
59 | static int apply_check_flag(struct apply_handle *h,int type, char *name, char *value); |
60 | static void apply_clear_flags(struct apply_handle *h); |
61 | void apply_set_iptr(struct apply_handle *h); |
62 | void apply_mark_flagstates(struct apply_handle *h); |
63 | void apply_clear_index(struct apply_handle *h); |
64 | |
65 | static void apply_stack_clear(struct apply_handle *h); |
66 | static int apply_stack_isempty(struct apply_handle *h); |
67 | static void apply_stack_pop (struct apply_handle *h); |
68 | static void apply_stack_push (struct apply_handle *h, int vmark, char *sflagname, char *sflagvalue, int sflagneg); |
69 | static void apply_force_clear_stack(struct apply_handle *h); |
70 | |
71 | |
72 | void apply_set_obey_flags(struct apply_handle *h, int value) { |
73 | h->obey_flags = value; |
74 | } |
75 | |
76 | void apply_set_show_flags(struct apply_handle *h, int value) { |
77 | h->show_flags = value; |
78 | } |
79 | |
80 | void apply_set_print_space(struct apply_handle *h, int value) { |
81 | h->print_space = value; |
82 | h->space_symbol = strdup(" "); |
83 | } |
84 | |
85 | void apply_set_separator(struct apply_handle *h, char *symbol) { |
86 | h->separator = strdup(symbol); |
87 | } |
88 | |
89 | void apply_set_epsilon(struct apply_handle *h, char *symbol) { |
90 | free(h->epsilon_symbol); |
91 | h->epsilon_symbol = strdup(symbol); |
92 | (h->sigs+EPSILON)->symbol = h->epsilon_symbol; |
93 | (h->sigs+EPSILON)->length = strlen(h->epsilon_symbol); |
94 | } |
95 | |
96 | void apply_set_space_symbol(struct apply_handle *h, char *space) { |
97 | h->space_symbol = strdup(space); |
98 | h->print_space = 1; |
99 | } |
100 | |
101 | void apply_set_print_pairs(struct apply_handle *h, int value) { |
102 | h->print_pairs = value; |
103 | } |
104 | |
105 | static void apply_force_clear_stack(struct apply_handle *h) { |
106 | |
107 | if (!apply_stack_isempty(h)) { |
108 | *(h->marks+(h->gstates+h->ptr)->state_no) = 0; |
109 | while (!apply_stack_isempty(h)) { |
110 | apply_stack_pop(h); |
111 | *(h->marks+(h->gstates+h->ptr)->state_no) = 0; |
112 | } |
113 | h->iterator = 0; |
114 | h->iterate_old = 0; |
115 | apply_stack_clear(h); |
116 | } |
117 | } |
118 | |
119 | char *apply_enumerate(struct apply_handle *h) { |
120 | |
121 | char *result = NULL; |
122 | |
123 | if (h->last_net == NULL || h->last_net->finalcount == 0) { |
124 | return (NULL); |
125 | } |
126 | h->binsearch = 0; |
127 | if (h->iterator == 0) { |
128 | h->iterate_old = 0; |
129 | apply_force_clear_stack(h); |
130 | result = apply_net(h); |
131 | if ((h->mode & RANDOM) != RANDOM) |
132 | (h->iterator)++; |
133 | } else { |
134 | h->iterate_old = 1; |
135 | result = apply_net(h); |
136 | } |
137 | return(result); |
138 | } |
139 | |
140 | char *apply_words(struct apply_handle *h) { |
141 | h->mode = DOWN + ENUMERATE + LOWER + UPPER; |
142 | return(apply_enumerate(h)); |
143 | } |
144 | |
145 | char *apply_upper_words(struct apply_handle *h) { |
146 | h->mode = DOWN + ENUMERATE + UPPER; |
147 | return(apply_enumerate(h)); |
148 | } |
149 | |
150 | char *apply_lower_words(struct apply_handle *h) { |
151 | h->mode = DOWN + ENUMERATE + LOWER; |
152 | return(apply_enumerate(h)); |
153 | } |
154 | |
155 | char *apply_random_words(struct apply_handle *h) { |
156 | apply_clear_flags(h); |
157 | h->mode = DOWN + ENUMERATE + LOWER + UPPER + RANDOM; |
158 | return(apply_enumerate(h)); |
159 | } |
160 | |
161 | char *apply_random_lower(struct apply_handle *h) { |
162 | apply_clear_flags(h); |
163 | h->mode = DOWN + ENUMERATE + LOWER + RANDOM; |
164 | return(apply_enumerate(h)); |
165 | } |
166 | |
167 | char *apply_random_upper(struct apply_handle *h) { |
168 | apply_clear_flags(h); |
169 | h->mode = DOWN + ENUMERATE + UPPER + RANDOM; |
170 | return(apply_enumerate(h)); |
171 | } |
172 | |
173 | |
174 | void apply_clear(struct apply_handle *h) { |
175 | struct sigma_trie_arrays *sta, *stap; |
176 | for (sta = h->sigma_trie_arrays; sta != NULL; ) { |
177 | stap = sta; |
178 | free(sta->arr); |
179 | sta = sta->next; |
180 | free(stap); |
181 | } |
182 | h->sigma_trie_arrays = NULL; |
183 | if (h->statemap != NULL) { |
184 | free(h->statemap); |
185 | h->statemap = NULL; |
186 | } |
187 | if (h->numlines != NULL) { |
188 | free(h->numlines); |
189 | h->numlines = NULL; |
190 | } |
191 | if (h->marks != NULL) { |
192 | free(h->marks); |
193 | h->marks = NULL; |
194 | } |
195 | if (h->searchstack != NULL) { |
196 | free(h->searchstack); |
197 | h->searchstack = NULL; |
198 | } |
199 | if (h->sigs != NULL) { |
200 | free(h->sigs); |
201 | h->sigs = NULL; |
202 | } |
203 | if (h->flag_lookup != NULL) { |
204 | free(h->flag_lookup); |
205 | h->flag_lookup = NULL; |
206 | } |
207 | if (h->sigmatch_array != NULL) { |
208 | free(h->sigmatch_array); |
209 | h->sigmatch_array = NULL; |
210 | } |
211 | if (h->flagstates != NULL) { |
212 | free(h->flagstates); |
213 | h->flagstates = NULL; |
214 | } |
215 | apply_clear_index(h); |
216 | h->last_net = NULL; |
217 | h->iterator = 0; |
218 | free(h->outstring); |
219 | free(h->separator); |
220 | free(h->epsilon_symbol); |
221 | free(h); |
222 | } |
223 | |
224 | char *apply_updown(struct apply_handle *h, char *word) { |
225 | |
226 | char *result = NULL; |
227 | |
228 | if (h->last_net == NULL || h->last_net->finalcount == 0) |
229 | return (NULL); |
230 | |
231 | if (word == NULL) { |
232 | h->iterate_old = 1; |
233 | result = apply_net(h); |
234 | } |
235 | else if (word != NULL) { |
236 | h->iterate_old = 0; |
237 | h->instring = word; |
238 | apply_create_sigmatch(h); |
239 | |
240 | |
241 | apply_force_clear_stack(h); |
242 | result = apply_net(h); |
243 | } |
244 | return(result); |
245 | } |
246 | |
247 | char *apply_down(struct apply_handle *h, char *word) { |
248 | |
249 | h->mode = DOWN; |
250 | if (h->index_in) { |
251 | h->indexed = 1; |
252 | } else { |
253 | h->indexed = 0; |
254 | } |
255 | h->binsearch = (h->last_net->arcs_sorted_in == 1) ? 1 : 0; |
256 | return(apply_updown(h, word)); |
257 | } |
258 | |
259 | char *apply_up(struct apply_handle *h, char *word) { |
260 | |
261 | h->mode = UP; |
262 | if (h->index_out) { |
263 | h->indexed = 1; |
264 | } else { |
265 | h->indexed = 0; |
266 | } |
267 | h->binsearch = (h->last_net->arcs_sorted_out == 1) ? 1 : 0; |
268 | return(apply_updown(h, word)); |
269 | } |
270 | |
271 | struct apply_handle *apply_init(struct fsm *net) { |
272 | struct apply_handle *h; |
273 | |
274 | srand((unsigned int) time(NULL)); |
275 | h = calloc(1,sizeof(struct apply_handle)); |
276 | |
277 | |
278 | h->iterate_old = 0; |
279 | h->iterator = 0; |
280 | h->instring = NULL; |
281 | h->flag_list = NULL; |
282 | h->flag_lookup = NULL; |
283 | h->obey_flags = 1; |
284 | h->show_flags = 0; |
285 | h->print_space = 0; |
286 | h->print_pairs = 0; |
287 | h->separator = strdup(":"); |
288 | h->epsilon_symbol = strdup("0"); |
289 | h->last_net = net; |
290 | h->outstring = malloc(sizeof(char)*DEFAULT_OUTSTRING_SIZE); |
291 | h->outstringtop = DEFAULT_OUTSTRING_SIZE; |
292 | *(h->outstring) = '\0'; |
293 | h->gstates = net->states; |
294 | h->gsigma = net->sigma; |
295 | h->printcount = 1; |
296 | apply_create_statemap(h, net); |
297 | h->searchstack = malloc(sizeof(struct searchstack) * DEFAULT_STACK_SIZE); |
298 | h->apply_stack_top = DEFAULT_STACK_SIZE; |
299 | apply_stack_clear(h); |
300 | apply_create_sigarray(h, net); |
301 | return(h); |
302 | } |
303 | |
304 | int apply_stack_isempty (struct apply_handle *h) { |
305 | if (h->apply_stack_ptr == 0) { |
306 | return 1; |
307 | } |
308 | return 0; |
309 | } |
310 | |
311 | void apply_stack_clear (struct apply_handle *h) { |
312 | h->apply_stack_ptr = 0; |
313 | } |
314 | |
315 | void apply_stack_pop (struct apply_handle *h) { |
316 | struct flag_list *flist; |
317 | struct searchstack *ss; |
318 | (h->apply_stack_ptr)--; |
319 | ss = h->searchstack+h->apply_stack_ptr; |
320 | |
321 | h->iptr = ss->iptr; |
322 | h->ptr = ss->offset; |
323 | h->ipos = ss->ipos; |
324 | h->opos = ss->opos; |
325 | h->state_has_index = ss->state_has_index; |
326 | |
327 | *(h->marks+(h->gstates+h->ptr)->state_no) = ss->visitmark; |
328 | |
329 | if (h->has_flags && ss->flagname != NULL) { |
330 | |
331 | for (flist = h->flag_list; flist != NULL; flist = flist->next) { |
332 | if (strcmp(flist->name, ss->flagname) == 0) { |
333 | break; |
334 | } |
335 | } |
336 | if (flist == NULL) |
337 | perror("***Nothing to pop\n"); |
338 | flist->value = ss->flagvalue; |
339 | flist->neg = ss->flagneg; |
340 | } |
341 | } |
342 | |
343 | static void apply_stack_push (struct apply_handle *h, int vmark, char *sflagname, char *sflagvalue, int sflagneg) { |
344 | struct searchstack *ss; |
345 | if (h->apply_stack_ptr == h->apply_stack_top) { |
346 | h->searchstack = realloc(h->searchstack, sizeof(struct searchstack)* ((h->apply_stack_top)*2)); |
347 | if (h->searchstack == NULL) { |
348 | perror("Apply stack full!!!\n"); |
349 | exit(0); |
350 | } |
351 | h->apply_stack_top *= 2; |
352 | } |
353 | ss = h->searchstack+h->apply_stack_ptr; |
354 | ss->offset = h->curr_ptr; |
355 | ss->ipos = h->ipos; |
356 | ss->opos = h->opos; |
357 | ss->visitmark = vmark; |
358 | ss->iptr = h->iptr; |
359 | ss->state_has_index = h->state_has_index; |
360 | if (h->has_flags) { |
361 | ss->flagname = sflagname; |
362 | ss->flagvalue = sflagvalue; |
363 | ss->flagneg = sflagneg; |
364 | } |
365 | (h->apply_stack_ptr)++; |
366 | } |
367 | |
368 | void apply_reset_enumerator(struct apply_handle *h) { |
369 | int statecount, i; |
370 | statecount = h->last_net->statecount; |
371 | for (i=0; i < statecount; i++) { |
372 | *(h->marks+i) = 0; |
373 | } |
374 | h->iterator = 0; |
375 | h->iterate_old = 0; |
376 | } |
377 | |
378 | void apply_clear_index_list(struct apply_handle *h, struct apply_state_index **index) { |
379 | int i, j, statecount; |
380 | struct apply_state_index *iptr, *iptr_tmp, *iptr_zero; |
381 | if (index == NULL) |
382 | return; |
383 | statecount = h->last_net->statecount; |
384 | for (i = 0; i < statecount; i++) { |
385 | iptr = *(index+i); |
386 | if (iptr == NULL) { |
387 | continue; |
388 | } |
389 | iptr_zero = *(index+i); |
390 | for (j = h->sigma_size - 1 ; j >= 0; j--) { |
391 | iptr = *(index+i) + j; |
392 | for (iptr = iptr->next ; iptr != NULL && iptr != iptr_zero; iptr = iptr_tmp) { |
393 | iptr_tmp = iptr->next; |
394 | free(iptr); |
395 | } |
396 | } |
397 | free(*(index+i)); |
398 | } |
399 | } |
400 | |
401 | void apply_clear_index(struct apply_handle *h) { |
402 | if (h->index_in) { |
403 | apply_clear_index_list(h, h->index_in); |
404 | free(h->index_in); |
405 | h->index_in = NULL; |
406 | } |
407 | if (h->index_out) { |
408 | apply_clear_index_list(h, h->index_out); |
409 | free(h->index_out); |
410 | h->index_out = NULL; |
411 | } |
412 | } |
413 | |
414 | void apply_index(struct apply_handle *h, int inout, int densitycutoff, int mem_limit, int flags_only) { |
415 | struct fsm_state *fsm; |
416 | unsigned int cnt = 0; |
417 | int i, j, maxtrans, numtrans, laststate, sym; |
418 | fsm = h->gstates; |
419 | |
420 | struct apply_state_index **indexptr, *iptr, *tempiptr; |
421 | |
422 | struct pre_index { |
423 | int state_no; |
424 | struct pre_index *next; |
425 | } *pre_index, *tp, *tpp; |
426 | if (flags_only && !h->has_flags) { |
| 1 | Assuming 'flags_only' is not equal to 0 | |
|
| 2 | | Assuming field 'has_flags' is not equal to 0 | |
|
| |
427 | return; |
428 | } |
429 | |
430 | for (i=0, laststate = 0, maxtrans = 0, numtrans = 0; (fsm+i)->state_no != -1; i++) { |
| 4 | | Assuming the condition is true | |
|
| 5 | | Loop condition is true. Entering loop body | |
|
| 11 | | Assuming the condition is false | |
|
| 12 | | Loop condition is false. Execution continues on line 441 | |
|
431 | if ((fsm+i)->state_no != laststate) { |
| 6 | | Assuming 'laststate' is not equal to field 'state_no' | |
|
432 | maxtrans = numtrans > maxtrans ? numtrans : maxtrans; |
| |
| |
433 | numtrans = 0; |
434 | } |
435 | if ((fsm+i)->target != -1) { |
| 9 | | Assuming the condition is false | |
|
| |
436 | numtrans++; |
437 | } |
438 | laststate = (fsm+i)->state_no; |
439 | } |
440 | |
441 | pre_index = calloc(maxtrans+1, sizeof(struct pre_index)); |
442 | for (i = 0; i <= maxtrans; i++) { |
| 13 | | Loop condition is true. Entering loop body | |
|
| 14 | | Loop condition is false. Execution continues on line 450 | |
|
443 | (pre_index+i)->state_no = -1; |
444 | } |
445 | |
446 | |
447 | |
448 | |
449 | |
450 | for (i = 0, laststate = 0, maxtrans = 0, numtrans = 0; (fsm+i)->state_no != -1; i++) { |
| 15 | | Loop condition is true. Entering loop body | |
|
| 20 | | Loop condition is false. Execution continues on line 468 | |
|
451 | if ((fsm+i)->state_no != laststate) { |
| |
452 | if ((pre_index+numtrans)->state_no == -1) { |
| |
453 | (pre_index+numtrans)->state_no = laststate; |
454 | } else { |
455 | tp = calloc(1, sizeof(struct pre_index)); |
456 | tp->state_no = laststate; |
457 | tp->next = (pre_index+numtrans)->next; |
458 | (pre_index+numtrans)->next = tp; |
459 | } |
460 | maxtrans = numtrans > maxtrans ? numtrans : maxtrans; |
| |
461 | numtrans = 0; |
462 | } |
463 | if ((fsm+i)->target != -1) { |
| |
464 | numtrans++; |
465 | } |
466 | laststate = (fsm+i)->state_no; |
467 | } |
468 | indexptr = NULL; |
469 | cnt += round_up_to_power_of_two(h->last_net->statecount*sizeof(struct apply_state_index *)); |
470 | |
471 | if (cnt > mem_limit) { |
| 21 | | Assuming 'cnt' is <= 'mem_limit' | |
|
| |
472 | cnt -= round_up_to_power_of_two(h->last_net->statecount*sizeof(struct apply_state_index *)); |
473 | goto memlimitnoindex; |
474 | } |
475 | |
476 | indexptr = calloc(h->last_net->statecount, sizeof(struct apply_state_index *)); |
477 | |
478 | if (h->has_flags && flags_only) { |
| |
479 | |
480 | if (!(h->flagstates)) { |
| 24 | | Assuming field 'flagstates' is null | |
|
| |
481 | apply_mark_flagstates(h); |
482 | } |
483 | } |
484 | |
485 | for (i = maxtrans; i >= 0; i--) { |
| 26 | | Loop condition is true. Entering loop body | |
|
486 | for (tp = pre_index+i; tp != NULL; tp = tp->next) { |
| 27 | | Loop condition is true. Entering loop body | |
|
487 | if (tp->state_no >= 0) { |
| |
488 | if (i < densitycutoff) { |
| 29 | | Assuming 'i' is < 'densitycutoff' | |
|
489 | if (!(h->has_flags && flags_only && BITTEST(h->flagstates, tp->state_no))) { |
| 30 | | Array access (via field 'flagstates') results in a null pointer dereference |
|
490 | continue; |
491 | } |
492 | } |
493 | cnt += round_up_to_power_of_two(h->sigma_size*sizeof(struct apply_state_index)); |
494 | if (cnt > mem_limit) { |
495 | cnt -= round_up_to_power_of_two(h->sigma_size*sizeof(struct apply_state_index)); |
496 | goto memlimit; |
497 | } |
498 | *(indexptr + tp->state_no) = malloc(h->sigma_size*sizeof(struct apply_state_index)); |
499 | |
500 | |
501 | |
502 | |
503 | |
504 | for (j = 0; j < h->sigma_size; j++) { |
505 | (*(indexptr + tp->state_no) + j)->fsmptr = -1; |
506 | if (j == EPSILON) |
507 | (*(indexptr + tp->state_no) + j)->next = NULL; |
508 | else |
509 | (*(indexptr + tp->state_no) + j)->next = (*(indexptr + tp->state_no)); |
510 | } |
511 | } |
512 | } |
513 | } |
514 | |
515 | memlimit: |
516 | |
517 | for (i=0; (fsm+i)->state_no != -1; i++) { |
518 | iptr = *(indexptr + (fsm+i)->state_no); |
519 | if (iptr == NULL || (fsm+i)->target == -1) { |
520 | continue; |
521 | } |
522 | sym = inout == APPLY_INDEX_INPUT ? (fsm+i)->in : (fsm+i)->out; |
523 | |
524 | if (h->has_flags && (h->flag_lookup+sym)->type) { |
525 | sym = EPSILON; |
526 | } |
527 | if (sym == UNKNOWN) { |
528 | sym = IDENTITY; |
529 | } |
530 | if ((iptr+sym)->fsmptr == -1) { |
531 | (iptr+sym)->fsmptr = i; |
532 | } else { |
533 | cnt += round_up_to_power_of_two(sizeof(struct apply_state_index)); |
534 | tempiptr = calloc(1, sizeof(struct apply_state_index)); |
535 | |
536 | tempiptr->next = (iptr+sym)->next; |
537 | tempiptr->fsmptr = i; |
538 | (iptr+sym)->next = tempiptr; |
539 | } |
540 | } |
541 | |
542 | |
543 | |
544 | memlimitnoindex: |
545 | |
546 | for (i = maxtrans; i >= 0; i--) { |
547 | for (tp = (pre_index+i)->next; tp != NULL; tp = tpp) { |
548 | tpp = tp->next; |
549 | free(tp); |
550 | } |
551 | } |
552 | free(pre_index); |
553 | |
554 | if (inout == APPLY_INDEX_INPUT) { |
555 | h->index_in = indexptr; |
556 | } else { |
557 | h->index_out = indexptr; |
558 | } |
559 | } |
560 | |
561 | int apply_binarysearch(struct apply_handle *h) { |
562 | int thisstate, nextsym, seeksym, thisptr, lastptr, midptr; |
563 | |
564 | thisptr = h->curr_ptr = h->ptr; |
565 | nextsym = (((h->mode) & DOWN) == DOWN) ? (h->gstates+h->curr_ptr)->in : (h->gstates+h->curr_ptr)->out; |
566 | if (nextsym == EPSILON) |
567 | return 1; |
568 | if (nextsym == -1) |
569 | return 0; |
570 | if (h->ipos >= h->current_instring_length) { |
571 | return 0; |
572 | } |
573 | seeksym = (h->sigmatch_array+h->ipos)->signumber; |
574 | if (seeksym == nextsym || (nextsym == UNKNOWN && seeksym == IDENTITY)) |
575 | return 1; |
576 | |
577 | thisstate = (h->gstates+thisptr)->state_no; |
578 | lastptr = *(h->statemap+thisstate)+*(h->numlines+thisstate)-1; |
579 | thisptr++; |
580 | |
581 | if (seeksym == IDENTITY || lastptr - thisptr < APPLY_BINSEARCH_THRESHOLD) { |
582 | for ( ; thisptr <= lastptr; thisptr++) { |
583 | nextsym = (((h->mode) & DOWN) == DOWN) ? (h->gstates+thisptr)->in : (h->gstates+thisptr)->out; |
584 | if ((nextsym == seeksym) || (nextsym == UNKNOWN && seeksym == IDENTITY)) { |
585 | h->curr_ptr = thisptr; |
586 | return 1; |
587 | } |
588 | if (nextsym > seeksym || nextsym == -1) { |
589 | return 0; |
590 | } |
591 | } |
592 | return 0; |
593 | } |
594 | |
595 | for (;;) { |
596 | if (thisptr > lastptr) { return 0; } |
597 | midptr = (thisptr+lastptr)/2; |
598 | nextsym = (((h->mode) & DOWN) == DOWN) ? (h->gstates+midptr)->in : (h->gstates+midptr)->out; |
599 | if (seeksym < nextsym) { |
600 | lastptr = midptr - 1; |
601 | continue; |
602 | } else if (seeksym > nextsym) { |
603 | thisptr = midptr + 1; |
604 | continue; |
605 | } else { |
606 | |
607 | while (((((h->mode) & DOWN) == DOWN) ? (h->gstates+(midptr-1))->in : (h->gstates+(midptr-1))->out) == seeksym) { |
608 | midptr--; |
609 | } |
610 | h->curr_ptr = midptr; |
611 | return 1; |
612 | } |
613 | } |
614 | } |
615 | |
616 | int apply_follow_next_arc(struct apply_handle *h) { |
617 | char *fname, *fvalue; |
618 | int eatupi, eatupo, symin, symout, fneg; |
619 | int vcount, marksource, marktarget; |
620 | |
621 | |
622 | |
623 | |
624 | |
625 | |
626 | |
627 | |
628 | |
629 | if (h->state_has_index) { |
630 | for ( ; h->iptr != NULL && h->iptr->fsmptr != -1; ) { |
631 | |
632 | h->ptr = h->curr_ptr = h->iptr->fsmptr; |
633 | if (((h->mode) & DOWN) == DOWN) { |
634 | symin = (h->gstates+h->curr_ptr)->in; |
635 | symout = (h->gstates+h->curr_ptr)->out; |
636 | } else { |
637 | symin = (h->gstates+h->curr_ptr)->out; |
638 | symout = (h->gstates+h->curr_ptr)->in; |
639 | } |
640 | |
641 | marksource = *(h->marks+(h->gstates+h->ptr)->state_no); |
642 | marktarget = *(h->marks+(h->gstates+(*(h->statemap+(h->gstates+h->curr_ptr)->target)))->state_no); |
643 | eatupi = apply_match_length(h, symin); |
644 | if (!(eatupi == -1 || -1-(h->ipos)-eatupi == marktarget)) { |
645 | if ((eatupi = apply_match_str(h, symin, h->ipos)) != -1) { |
646 | eatupo = apply_append(h, h->curr_ptr, symout); |
647 | if (h->obey_flags && h->has_flags && ((h->flag_lookup+symin)->type & (FLAG_UNIFY|FLAG_CLEAR|FLAG_POSITIVE|FLAG_NEGATIVE))) { |
648 | fname = (h->flag_lookup+symin)->name; |
649 | fvalue = h->oldflagvalue; |
650 | fneg = h->oldflagneg; |
651 | } else { |
652 | fname = fvalue = NULL; |
653 | fneg = 0; |
654 | } |
655 | |
656 | apply_stack_push(h, marksource, fname, fvalue, fneg); |
657 | h->ptr = *(h->statemap+(h->gstates+h->curr_ptr)->target); |
658 | h->ipos += eatupi; |
659 | h->opos += eatupo; |
660 | apply_set_iptr(h); |
661 | return 1; |
662 | } |
663 | } |
664 | h->iptr = h->iptr->next; |
665 | } |
666 | return 0; |
667 | } else if ((h->binsearch && !(h->has_flags)) || (h->binsearch && !(BITTEST(h->flagstates, (h->gstates+h->ptr)->state_no)))) { |
668 | for (;;) { |
669 | if (apply_binarysearch(h)) { |
670 | if (((h->mode) & DOWN) == DOWN) { |
671 | symin = (h->gstates+h->curr_ptr)->in; |
672 | symout = (h->gstates+h->curr_ptr)->out; |
673 | } else { |
674 | symin = (h->gstates+h->curr_ptr)->out; |
675 | symout = (h->gstates+h->curr_ptr)->in; |
676 | } |
677 | |
678 | marksource = *(h->marks+(h->gstates+h->ptr)->state_no); |
679 | marktarget = *(h->marks+(h->gstates+(*(h->statemap+(h->gstates+h->curr_ptr)->target)))->state_no); |
680 | |
681 | eatupi = apply_match_length(h, symin); |
682 | if (eatupi != -1 && -1-(h->ipos)-eatupi != marktarget) { |
683 | if ((eatupi = apply_match_str(h, symin, h->ipos)) != -1) { |
684 | eatupo = apply_append(h, h->curr_ptr, symout); |
685 | |
686 | |
687 | apply_stack_push(h, marksource, NULL, NULL, 0); |
688 | |
689 | |
690 | h->ptr = *(h->statemap+(h->gstates+h->curr_ptr)->target); |
691 | h->ipos += eatupi; |
692 | h->opos += eatupo; |
693 | apply_set_iptr(h); |
694 | return 1; |
695 | } |
696 | } |
697 | if ((h->gstates+h->curr_ptr)->state_no == (h->gstates+h->curr_ptr+1)->state_no) { |
698 | h->curr_ptr++; |
699 | h->ptr = h->curr_ptr; |
700 | if ((h->gstates+h->curr_ptr)-> target == -1) { |
701 | return 0; |
702 | } |
703 | continue; |
704 | } |
705 | } |
706 | return 0; |
707 | } |
708 | } else { |
709 | for (h->curr_ptr = h->ptr; (h->gstates+h->curr_ptr)->state_no == (h->gstates+h->ptr)->state_no && (h->gstates+h->curr_ptr)-> in != -1; (h->curr_ptr)++) { |
710 | |
711 | |
712 | if ((h->mode & RANDOM) == RANDOM) { |
713 | vcount = 0; |
714 | for (h->curr_ptr = h->ptr; (h->gstates+h->curr_ptr)->state_no == (h->gstates+h->ptr)->state_no && (h->gstates+h->curr_ptr)-> in != -1; (h->curr_ptr)++) { |
715 | vcount++; |
716 | } |
717 | if (vcount > 0) { |
718 | h->curr_ptr = h->ptr + (rand() % vcount); |
719 | } else { |
720 | h->curr_ptr = h->ptr; |
721 | } |
722 | } |
723 | |
724 | if (((h->mode) & DOWN) == DOWN) { |
725 | symin = (h->gstates+h->curr_ptr)->in; |
726 | symout = (h->gstates+h->curr_ptr)->out; |
727 | } else { |
728 | symin = (h->gstates+h->curr_ptr)->out; |
729 | symout = (h->gstates+h->curr_ptr)->in; |
730 | } |
731 | |
732 | marksource = *(h->marks+(h->gstates+h->ptr)->state_no); |
733 | marktarget = *(h->marks+(h->gstates+(*(h->statemap+(h->gstates+h->curr_ptr)->target)))->state_no); |
734 | |
735 | eatupi = apply_match_length(h, symin); |
736 | |
737 | if (eatupi == -1 || -1-(h->ipos)-eatupi == marktarget) { continue; } |
738 | if ((eatupi = apply_match_str(h, symin, h->ipos)) != -1) { |
739 | eatupo = apply_append(h, h->curr_ptr, symout); |
740 | if (h->obey_flags && h->has_flags && ((h->flag_lookup+symin)->type & (FLAG_UNIFY|FLAG_CLEAR|FLAG_POSITIVE|FLAG_NEGATIVE))) { |
741 | |
742 | fname = (h->flag_lookup+symin)->name; |
743 | fvalue = h->oldflagvalue; |
744 | fneg = h->oldflagneg; |
745 | } else { |
746 | fname = fvalue = NULL; |
747 | fneg = 0; |
748 | } |
749 | |
750 | |
751 | apply_stack_push(h, marksource, fname, fvalue, fneg); |
752 | |
753 | |
754 | h->ptr = *(h->statemap+(h->gstates+h->curr_ptr)->target); |
755 | h->ipos += eatupi; |
756 | h->opos += eatupo; |
757 | apply_set_iptr(h); |
758 | return(1); |
759 | } |
760 | } |
761 | return(0); |
762 | } |
763 | } |
764 | |
765 | char *apply_return_string(struct apply_handle *h) { |
766 | |
767 | *(h->outstring+h->opos) = '\0'; |
768 | if (((h->mode) & RANDOM) == RANDOM) { |
769 | |
770 | if (!(rand() % 2)) { |
771 | apply_stack_clear(h); |
772 | h->iterator = 0; |
773 | h->iterate_old = 0; |
774 | return(h->outstring); |
775 | } |
776 | } else { |
777 | return(h->outstring); |
778 | } |
779 | return(NULL); |
780 | } |
781 | |
782 | void apply_mark_state(struct apply_handle *h) { |
783 | |
784 | |
785 | |
786 | |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | if ((h->mode & RANDOM) != RANDOM) { |
794 | if (*(h->marks+(h->gstates+h->ptr)->state_no) == h->ipos+1) { |
795 | *(h->marks+(h->gstates+h->ptr)->state_no) = -(h->ipos+1); |
796 | } else { |
797 | *(h->marks+(h->gstates+h->ptr)->state_no) = h->ipos+1; |
798 | } |
799 | } |
800 | } |
801 | |
802 | void apply_skip_this_arc(struct apply_handle *h) { |
803 | |
804 | if (h->iptr) { |
805 | h->ptr = h->iptr->fsmptr; |
806 | h->iptr = h->iptr->next; |
807 | |
808 | } else { |
809 | (h->ptr)++; |
810 | } |
811 | } |
812 | |
813 | int apply_at_last_arc(struct apply_handle *h) { |
814 | int seeksym, nextsym; |
815 | if (h->state_has_index) { |
816 | if (h->iptr->next == NULL || h->iptr->next->fsmptr == -1) { |
817 | return 1; |
818 | } |
819 | } else { |
820 | if ((h->binsearch && !(h->has_flags)) || (h->binsearch && !(BITTEST(h->flagstates, (h->gstates+h->ptr)->state_no)))) { |
821 | if ((h->gstates+h->ptr)->state_no != (h->gstates+h->ptr+1)->state_no) { |
822 | return 1; |
823 | } |
824 | seeksym = (h->sigmatch_array+h->ipos)->signumber; |
825 | nextsym = (((h->mode) & DOWN) == DOWN) ? (h->gstates+h->ptr)->in : (h->gstates+h->ptr)->out; |
826 | if (nextsym == -1 || seeksym < nextsym) { |
827 | return 1; |
828 | } |
829 | } else { |
830 | if ((h->gstates+h->ptr)->state_no != (h->gstates+h->ptr+1)->state_no) { |
831 | return 1; |
832 | } |
833 | } |
834 | } |
835 | return 0; |
836 | } |
837 | |
838 | |
839 | void apply_set_iptr(struct apply_handle *h) { |
840 | struct apply_state_index **idx, *sidx; |
841 | int stateno, seeksym; |
842 | |
843 | if ((idx = ((h->mode) & DOWN) == DOWN ? (h->index_in) : (h->index_out)) == NULL) { |
844 | return; |
845 | } |
846 | |
847 | h->iptr = NULL; |
848 | h->state_has_index = 0; |
849 | stateno = (h->gstates+h->ptr)->state_no; |
850 | if (stateno < 0) { |
851 | return; |
852 | } |
853 | |
854 | sidx = *(idx + stateno); |
855 | if (sidx == NULL) { return; } |
856 | seeksym = (h->sigmatch_array+h->ipos)->signumber; |
857 | h->state_has_index = 1; |
858 | sidx = sidx + seeksym; |
859 | if (sidx->fsmptr == -1) { |
860 | if (sidx->next == NULL) { |
861 | return; |
862 | } else { |
863 | sidx = sidx->next; |
864 | } |
865 | } |
866 | h->iptr = sidx; |
867 | if (sidx->fsmptr == -1) { |
868 | h->iptr = NULL; |
869 | } |
870 | h->state_has_index = 1; |
871 | } |
872 | |
873 | char *apply_net(struct apply_handle *h) { |
874 | |
875 | |
876 | |
877 | |
878 | |
879 | |
880 | |
881 | |
882 | |
883 | |
884 | |
885 | |
886 | |
887 | |
888 | |
889 | |
890 | |
891 | |
892 | |
893 | char *returnstring; |
894 | |
895 | if (h->iterate_old == 1) { |
896 | goto resume; |
897 | } |
898 | |
899 | h->iptr = NULL; h->ptr = 0; h->ipos = 0; h->opos = 0; |
900 | apply_set_iptr(h); |
901 | |
902 | apply_stack_clear(h); |
903 | |
904 | if (h->has_flags) { |
905 | apply_clear_flags(h); |
906 | } |
907 | |
908 | |
909 | |
910 | |
911 | goto L2; |
912 | |
913 | while(!apply_stack_isempty(h)) { |
914 | apply_stack_pop(h); |
915 | |
916 | if (apply_at_last_arc(h)) { |
917 | *(h->marks+(h->gstates+h->ptr)->state_no) = 0; |
918 | continue; |
919 | } |
920 | apply_skip_this_arc(h); |
921 | L1: |
922 | if (!apply_follow_next_arc(h)) { |
923 | *(h->marks+(h->gstates+h->ptr)->state_no) = 0; |
924 | continue; |
925 | } |
926 | L2: |
927 | |
928 | if ((h->gstates+h->ptr)->final_state == 1 && (h->ipos == h->current_instring_length || ((h->mode) & ENUMERATE) == ENUMERATE)) { |
929 | if ((returnstring = (apply_return_string(h))) != NULL) { |
930 | return(returnstring); |
931 | } |
932 | } |
933 | |
934 | resume: |
935 | apply_mark_state(h); |
936 | goto L1; |
937 | } |
938 | if ((h->mode & RANDOM) == RANDOM) { |
939 | apply_stack_clear(h); |
940 | h->iterator = 0; |
941 | h->iterate_old = 0; |
942 | return(h->outstring); |
943 | } |
944 | apply_stack_clear(h); |
945 | return NULL; |
946 | } |
947 | |
948 | int apply_append(struct apply_handle *h, int cptr, int sym) { |
949 | |
950 | char *astring, *bstring, *pstring; |
951 | int symin, symout, len, alen, blen, idlen; |
952 | |
953 | symin = (h->gstates+cptr)->in; |
954 | symout = (h->gstates+cptr)->out; |
955 | astring = ((h->sigs)+symin)->symbol; |
956 | alen = ((h->sigs)+symin)->length; |
957 | bstring = ((h->sigs)+symout)->symbol; |
958 | blen = ((h->sigs)+symout)->length; |
959 | |
960 | while (alen + blen + h->opos + 2 + strlen(h->separator) >= h->outstringtop) { |
961 | |
962 | h->outstring = realloc(h->outstring, sizeof(char) * ((h->outstringtop) * 2)); |
963 | (h->outstringtop) *= 2; |
964 | } |
965 | |
966 | if ((h->has_flags) && !h->show_flags && (h->flag_lookup+symin)->type) { |
967 | astring = ""; alen = 0; |
968 | } |
969 | if (h->has_flags && !h->show_flags && (h->flag_lookup+symout)->type) { |
970 | bstring = ""; blen = 0; |
971 | } |
972 | if (((h->mode) & ENUMERATE) == ENUMERATE) { |
973 | |
974 | |
975 | if (((h->mode) & (UPPER | LOWER)) == (UPPER|LOWER)) { |
976 | |
977 | if (astring == bstring) { |
978 | strcpy(h->outstring+h->opos, astring); |
979 | len = alen; |
980 | } else { |
981 | strcpy(h->outstring+h->opos, astring); |
982 | |
983 | strcpy(h->outstring+h->opos+alen,h->separator); |
984 | |
985 | strcpy(h->outstring+h->opos+alen+strlen(h->separator),bstring); |
986 | |
987 | len = alen+blen+strlen(h->separator); |
988 | } |
989 | } |
990 | |
991 | |
992 | if (((h->mode) & (UPPER|LOWER)) != (UPPER|LOWER)) { |
993 | |
994 | if (symin == EPSILON) { |
995 | astring = ""; alen = 0; |
996 | } |
997 | if (symout == EPSILON) { |
998 | bstring = ""; blen = 0; |
999 | } |
1000 | if (((h->mode) & (UPPER|LOWER)) == UPPER) { |
1001 | pstring = astring; |
1002 | len = alen; |
1003 | } else { |
1004 | pstring = bstring; |
1005 | len = blen; |
1006 | } |
1007 | |
1008 | memcpy(h->outstring+h->opos, pstring, len); |
1009 | } |
1010 | } |
1011 | if (((h->mode) & ENUMERATE) != ENUMERATE) { |
1012 | |
1013 | if (h->print_pairs && (symin != symout)) { |
1014 | |
1015 | if (symin == UNKNOWN && ((h->mode) & DOWN) == DOWN) |
1016 | strncpy(astring, h->instring+h->ipos, 1); |
1017 | if (symout == UNKNOWN && ((h->mode) & UP) == UP) |
1018 | strncpy(bstring, h->instring+h->ipos, 1); |
1019 | strcpy(h->outstring+h->opos, "<"); |
1020 | strcpy(h->outstring+h->opos+1, astring); |
1021 | |
1022 | strcpy(h->outstring+h->opos+alen+1,h->separator); |
1023 | |
1024 | strcpy(h->outstring+h->opos+alen+1+strlen(h->separator), bstring); |
1025 | |
1026 | strcpy(h->outstring+h->opos+alen+blen+1+strlen(h->separator),">"); |
1027 | |
1028 | len = alen+blen+2+strlen(h->separator); |
1029 | } |
1030 | |
1031 | else if (sym == IDENTITY) { |
1032 | |
1033 | |
1034 | idlen = (h->sigmatch_array+h->ipos)->consumes; |
1035 | strncpy(h->outstring+h->opos, h->instring+h->ipos, idlen); |
1036 | strncpy(h->outstring+h->opos+idlen,"", 1); |
1037 | len = idlen; |
1038 | } else if (sym == EPSILON) { |
1039 | return(0); |
1040 | } else { |
1041 | if (((h->mode) & DOWN) == DOWN) { |
1042 | pstring = bstring; |
1043 | len = blen; |
1044 | } else { |
1045 | pstring = astring; |
1046 | len = alen; |
1047 | } |
1048 | memcpy(h->outstring+h->opos, pstring, len); |
1049 | } |
1050 | } |
1051 | if (h->print_space && len > 0) { |
1052 | strcpy(h->outstring+h->opos+len, h->space_symbol); |
1053 | len++; |
1054 | } |
1055 | return(len); |
1056 | } |
1057 | |
1058 | int apply_match_length(struct apply_handle *h, int symbol) { |
1059 | if (symbol == EPSILON) { |
1060 | return 0; |
1061 | } |
1062 | if (h->has_flags && (h->flag_lookup+symbol)->type) { |
1063 | return 0; |
1064 | } |
1065 | if (((h->mode) & ENUMERATE) == ENUMERATE) { |
1066 | return 0; |
1067 | } |
1068 | if (h->ipos >= h->current_instring_length) { |
1069 | return -1; |
1070 | } |
1071 | if ((h->sigmatch_array+(h->ipos))->signumber == symbol) { |
1072 | return((h->sigmatch_array+(h->ipos))->consumes); |
1073 | } |
1074 | if ((symbol == IDENTITY) || (symbol == UNKNOWN)) { |
1075 | if ((h->sigmatch_array+h->ipos)->signumber == IDENTITY) { |
1076 | return((h->sigmatch_array+(h->ipos))->consumes); |
1077 | } |
1078 | } |
1079 | return -1; |
1080 | } |
1081 | |
1082 | |
1083 | |
1084 | |
1085 | |
1086 | int apply_match_str(struct apply_handle *h, int symbol, int position) { |
1087 | if (((h->mode) & ENUMERATE) == ENUMERATE) { |
1088 | if (h->has_flags && (h->flag_lookup+symbol)->type) { |
1089 | if (!h->obey_flags) { |
1090 | return 0; |
1091 | } |
1092 | if (apply_check_flag(h,(h->flag_lookup+symbol)->type, (h->flag_lookup+symbol)->name, (h->flag_lookup+symbol)->value) == SUCCEED) { |
1093 | return 0; |
1094 | } else { |
1095 | return -1; |
1096 | } |
1097 | } |
1098 | return(0); |
1099 | } |
1100 | |
1101 | |
1102 | if (symbol == EPSILON) { |
1103 | return 0; |
1104 | } |
1105 | |
1106 | |
1107 | if (h->has_flags && (h->flag_lookup+symbol)->type) { |
1108 | if (!h->obey_flags) { |
1109 | return 0; |
1110 | } |
1111 | if (apply_check_flag(h,(h->flag_lookup+symbol)->type, (h->flag_lookup+symbol)->name, (h->flag_lookup+symbol)->value) == SUCCEED) { |
1112 | return 0; |
1113 | } else { |
1114 | return -1; |
1115 | } |
1116 | } |
1117 | |
1118 | if (position >= h->current_instring_length) { |
1119 | return -1; |
1120 | } |
1121 | if ((h->sigmatch_array+position)->signumber == symbol) { |
1122 | return((h->sigmatch_array+position)->consumes); |
1123 | } |
1124 | if ((symbol == IDENTITY) || (symbol == UNKNOWN)) { |
1125 | if ((h->sigmatch_array+position)->signumber == IDENTITY) { |
1126 | return((h->sigmatch_array+position)->consumes); |
1127 | } |
1128 | } |
1129 | return -1; |
1130 | } |
1131 | |
1132 | void apply_create_statemap(struct apply_handle *h, struct fsm *net) { |
1133 | int i; |
1134 | struct fsm_state *fsm; |
1135 | fsm = net->states; |
1136 | h->statemap = malloc(sizeof(int)*net->statecount); |
1137 | h->marks = malloc(sizeof(int)*net->statecount); |
1138 | h->numlines = malloc(sizeof(int)*net->statecount); |
1139 | |
1140 | for (i=0; i < net->statecount; i++) { |
1141 | *(h->numlines+i) = 0; |
1142 | *(h->statemap+i) = -1; |
1143 | *(h->marks+i) = 0; |
1144 | } |
1145 | for (i=0; (fsm+i)->state_no != -1; i++) { |
1146 | *(h->numlines+(fsm+i)->state_no) = *(h->numlines+(fsm+i)->state_no)+1; |
1147 | if (*(h->statemap+(fsm+i)->state_no) == -1) { |
1148 | *(h->statemap+(fsm+i)->state_no) = i; |
1149 | } |
1150 | } |
1151 | } |
1152 | |
1153 | void apply_add_sigma_trie(struct apply_handle *h, int number, char *symbol, int len) { |
1154 | |
1155 | |
1156 | |
1157 | |
1158 | |
1159 | |
1160 | int i; |
1161 | struct sigma_trie *st; |
1162 | struct sigma_trie_arrays *sta; |
1163 | |
1164 | st = h->sigma_trie; |
1165 | for (i = 0; i < len; i++) { |
1166 | st = st+(unsigned char)*(symbol+i); |
1167 | if (i == (len-1)) { |
1168 | st->signum = number; |
1169 | } else { |
1170 | if (st->next == NULL) { |
1171 | st->next = calloc(256,sizeof(struct sigma_trie)); |
1172 | st = st->next; |
1173 | |
1174 | sta = malloc(sizeof(struct sigma_trie_arrays)); |
1175 | sta->arr = st; |
1176 | sta->next = h->sigma_trie_arrays; |
1177 | h->sigma_trie_arrays = sta; |
1178 | } else { |
1179 | st = st->next; |
1180 | } |
1181 | } |
1182 | } |
1183 | } |
1184 | |
1185 | void apply_mark_flagstates(struct apply_handle *h) { |
1186 | int i; |
1187 | struct fsm_state *fsm; |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | if (!h->has_flags || h->flag_lookup == NULL) { |
1193 | return; |
1194 | } |
1195 | if (h->flagstates) { |
1196 | free(h->flagstates); |
1197 | } |
1198 | h->flagstates = calloc(BITNSLOTS(h->last_net->statecount), sizeof(uint8_t)); |
1199 | fsm = h->last_net->states; |
1200 | for (i=0; (fsm+i)->state_no != -1; i++) { |
1201 | if ((fsm+i)->target == -1) { |
1202 | continue; |
1203 | } |
1204 | if ((h->flag_lookup+(fsm+i)->in)->type) { |
1205 | BITSET(h->flagstates,(fsm+i)->state_no); |
1206 | } |
1207 | if ((h->flag_lookup+(fsm+i)->out)->type) { |
1208 | BITSET(h->flagstates,(fsm+i)->state_no); |
1209 | } |
1210 | } |
1211 | } |
1212 | |
1213 | void apply_create_sigarray(struct apply_handle *h, struct fsm *net) { |
1214 | struct sigma *sig; |
1215 | int i, maxsigma; |
1216 | |
1217 | maxsigma = sigma_max(net->sigma); |
1218 | h->sigma_size = maxsigma+1; |
1219 | |
1220 | h->sigmatch_array = calloc(1024,sizeof(struct sigmatch_array)); |
1221 | h->sigmatch_array_size = 1024; |
1222 | |
1223 | h->sigs = malloc(sizeof(struct sigs)*(maxsigma+1)); |
1224 | h->has_flags = 0; |
1225 | h->flag_list = NULL; |
1226 | |
1227 | |
1228 | |
1229 | |
1230 | h->sigma_trie = calloc(256,sizeof(struct sigma_trie)); |
1231 | h->sigma_trie_arrays = malloc(sizeof(struct sigma_trie_arrays)); |
1232 | h->sigma_trie_arrays->arr = h->sigma_trie; |
1233 | h->sigma_trie_arrays->next = NULL; |
1234 | |
1235 | for (i=0;i<256;i++) |
1236 | (h->sigma_trie+i)->next = NULL; |
1237 | for (sig = h->gsigma; sig != NULL && sig->number != -1; sig = sig->next) { |
1238 | if (flag_check(sig->symbol)) { |
1239 | h->has_flags = 1; |
1240 | apply_add_flag(h, flag_get_name(sig->symbol)); |
1241 | } |
1242 | (h->sigs+(sig->number))->symbol = sig->symbol; |
1243 | (h->sigs+(sig->number))->length = strlen(sig->symbol); |
1244 | |
1245 | if (sig->number > IDENTITY) { |
1246 | apply_add_sigma_trie(h, sig->number, sig->symbol, (h->sigs+(sig->number))->length); |
1247 | } |
1248 | } |
1249 | if (maxsigma >= IDENTITY) { |
1250 | (h->sigs+EPSILON)->symbol = h->epsilon_symbol; |
1251 | (h->sigs+EPSILON)->length = strlen(h->epsilon_symbol); |
1252 | (h->sigs+UNKNOWN)->symbol = "?"; |
1253 | (h->sigs+UNKNOWN)->length = 1; |
1254 | (h->sigs+IDENTITY)->symbol = "@"; |
1255 | (h->sigs+IDENTITY)->length = 1; |
1256 | } |
1257 | if (h->has_flags) { |
1258 | |
1259 | h->flag_lookup = malloc(sizeof(struct flag_lookup)*(maxsigma+1)); |
1260 | for (i=0; i <= maxsigma; i++) { |
1261 | (h->flag_lookup+i)->type = 0; |
1262 | (h->flag_lookup+i)->name = NULL; |
1263 | (h->flag_lookup+i)->value = NULL; |
1264 | } |
1265 | for (sig = h->gsigma; sig != NULL ; sig = sig->next) { |
1266 | if (flag_check(sig->symbol)) { |
1267 | (h->flag_lookup+sig->number)->type = flag_get_type(sig->symbol); |
1268 | (h->flag_lookup+sig->number)->name = flag_get_name(sig->symbol); |
1269 | (h->flag_lookup+sig->number)->value = flag_get_value(sig->symbol); |
1270 | } |
1271 | } |
1272 | apply_mark_flagstates(h); |
1273 | } |
1274 | } |
1275 | |
1276 | |
1277 | |
1278 | |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | |
1284 | |
1285 | void apply_create_sigmatch(struct apply_handle *h) { |
1286 | char *symbol; |
1287 | struct sigma_trie *st; |
1288 | int i, j, inlen, lastmatch, consumes, cons; |
1289 | |
1290 | if (((h->mode) & ENUMERATE) == ENUMERATE) { |
1291 | return; |
1292 | } |
1293 | symbol = h->instring; |
1294 | inlen = strlen(symbol); |
1295 | h->current_instring_length = inlen; |
1296 | if (inlen >= h->sigmatch_array_size) { |
1297 | free(h->sigmatch_array); |
1298 | h->sigmatch_array = malloc(sizeof(struct sigmatch_array)*(inlen)); |
1299 | h->sigmatch_array_size = inlen; |
1300 | } |
1301 | |
1302 | |
1303 | for (i=0; i < inlen; i += consumes ) { |
1304 | st = h->sigma_trie; |
1305 | for (j=0, lastmatch = 0; ; j++) { |
1306 | if (*(symbol+i+j) == '\0') { |
1307 | break; |
1308 | } |
1309 | st = st+(unsigned char)*(symbol+i+j); |
1310 | if (st->signum != 0) { |
1311 | lastmatch = st->signum; |
1312 | if (st->next == NULL) |
1313 | break; |
1314 | st = st->next; |
1315 | } else if (st->next != NULL) { |
1316 | st = st->next; |
1317 | } else { |
1318 | break; |
1319 | } |
1320 | } |
1321 | if (lastmatch != 0) { |
1322 | (h->sigmatch_array+i)->signumber = lastmatch; |
1323 | consumes = (h->sigs+lastmatch)->length; |
1324 | } else { |
1325 | |
1326 | (h->sigmatch_array+i)->signumber = IDENTITY; |
1327 | consumes = utf8skip(symbol+i)+1; |
1328 | } |
1329 | |
1330 | |
1331 | |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | |
1338 | |
1339 | |
1340 | |
1341 | for ( ; (cons = utf8iscombining((unsigned char *)(symbol+i+consumes))); consumes += cons) { |
1342 | (h->sigmatch_array+i)->signumber = IDENTITY; |
1343 | } |
1344 | (h->sigmatch_array+i)->consumes = consumes; |
1345 | } |
1346 | } |
1347 | |
1348 | void apply_add_flag(struct apply_handle *h, char *name) { |
1349 | struct flag_list *flist, *flist_prev; |
1350 | if (h->flag_list == NULL) { |
1351 | flist = h->flag_list = malloc(sizeof(struct flag_list)); |
1352 | } else { |
1353 | for (flist = h->flag_list; flist != NULL; flist_prev = flist, flist = flist->next) { |
1354 | if (strcmp(flist->name, name) == 0) { |
1355 | return; |
1356 | } |
1357 | } |
1358 | flist = malloc(sizeof(struct flag_list)); |
1359 | flist_prev->next = flist; |
1360 | } |
1361 | flist->name = name; |
1362 | flist->value = NULL; |
1363 | flist->neg = 0; |
1364 | flist->next = NULL; |
1365 | return; |
1366 | } |
1367 | |
1368 | void apply_clear_flags(struct apply_handle *h) { |
1369 | struct flag_list *flist; |
1370 | for (flist = h->flag_list; flist != NULL; flist = flist->next) { |
1371 | flist->value = NULL; |
1372 | flist->neg = 0; |
1373 | } |
1374 | return; |
1375 | } |
1376 | |
1377 | |
1378 | |
1379 | |
1380 | int apply_check_flag(struct apply_handle *h, int type, char *name, char *value) { |
1381 | struct flag_list *flist, *flist2; |
1382 | for (flist = h->flag_list; flist != NULL; flist = flist->next) { |
1383 | if (strcmp(flist->name, name) == 0) { |
1384 | break; |
1385 | } |
1386 | } |
1387 | h->oldflagvalue = flist->value; |
1388 | h->oldflagneg = flist->neg; |
1389 | |
1390 | if (type == FLAG_UNIFY) { |
1391 | if (flist->value == NULL) { |
1392 | flist->value = strdup(value); |
1393 | return SUCCEED; |
1394 | } |
1395 | else if (strcmp(value, flist->value) == 0 && flist->neg == 0) { |
1396 | return SUCCEED; |
1397 | } else if (strcmp(value, flist->value) != 0 && flist->neg == 1) { |
1398 | flist->value = strdup(value); |
1399 | flist->neg = 0; |
1400 | return SUCCEED; |
1401 | } |
1402 | return FAIL; |
1403 | } |
1404 | |
1405 | if (type == FLAG_CLEAR) { |
1406 | flist->value = NULL; |
1407 | flist->neg = 0; |
1408 | return SUCCEED; |
1409 | } |
1410 | |
1411 | if (type == FLAG_DISALLOW) { |
1412 | if (flist->value == NULL) { |
1413 | return SUCCEED; |
1414 | } |
1415 | if (value == NULL && flist->value != NULL) { |
1416 | return FAIL; |
1417 | } |
1418 | if (strcmp(value, flist->value) != 0) { |
1419 | if (flist->neg == 1) |
1420 | return FAIL; |
1421 | return SUCCEED; |
1422 | } |
1423 | if (strcmp(value, flist->value) == 0 && flist->neg == 1) { |
1424 | return SUCCEED; |
1425 | } |
1426 | return FAIL; |
1427 | } |
1428 | |
1429 | if (type == FLAG_NEGATIVE) { |
1430 | flist->value = value; |
1431 | flist->neg = 1; |
1432 | return SUCCEED; |
1433 | } |
1434 | |
1435 | if (type == FLAG_POSITIVE) { |
1436 | flist->value = value; |
1437 | flist->neg = 0; |
1438 | return SUCCEED; |
1439 | } |
1440 | |
1441 | if (type == FLAG_REQUIRE) { |
1442 | |
1443 | if (value == NULL) { |
1444 | if (flist->value == NULL) { |
1445 | return FAIL; |
1446 | } else { |
1447 | return SUCCEED; |
1448 | } |
1449 | } else { |
1450 | if (flist->value == NULL) { |
1451 | return FAIL; |
1452 | } |
1453 | if (strcmp(value, flist->value) != 0) { |
1454 | return FAIL; |
1455 | } else { |
1456 | if (flist->neg == 1) { |
1457 | return FAIL; |
1458 | } |
1459 | return SUCCEED; |
1460 | } |
1461 | } |
1462 | } |
1463 | |
1464 | if (type == FLAG_EQUAL) { |
1465 | for (flist2 = h->flag_list; flist2 != NULL; flist2 = flist2->next) { |
1466 | if (strcmp(flist2->name, value) == 0) { |
1467 | break; |
1468 | } |
1469 | } |
1470 | |
1471 | if (flist2 == NULL && flist->value != NULL) |
1472 | return FAIL; |
1473 | if (flist2 == NULL && flist->value == NULL) { |
1474 | return SUCCEED; |
1475 | } |
1476 | if (flist2->value == NULL || flist->value == NULL) { |
1477 | if (flist2->value == NULL && flist->value == NULL && flist->neg == flist2->neg) { |
1478 | return SUCCEED; |
1479 | } else { |
1480 | return FAIL; |
1481 | } |
1482 | } else if (strcmp(flist2->value, flist->value) == 0 && flist->neg == flist2->neg) { |
1483 | return SUCCEED; |
1484 | } |
1485 | return FAIL; |
1486 | } |
1487 | fprintf(stderr,"***Don't know what do with flag [%i][%s][%s]\n", type, name, value); |
1488 | return FAIL; |
1489 | } |