/
exparser.c
750 lines (661 loc) · 15 KB
/
exparser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
/*****************************************************************************
Expression Parser
Copyright (c) 2010,2011,2012 RoyalAnarchy.com, All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//
// Original algorithm/code from Bjarne Stroustrup (bs@research.att.com)
// The original published work appears to be free of any copy restrictions.
// http://www2.research.att.com/~bs/dc_command_line.c
//
// Revision History:
// - Modified to use unsigned long long instead of double
// - Added: %, >>, <<, ~, ==, !=, >=, <=, >, <, &&, ||, !
// - Added: abs()
// - Added: mask()
// - Added: network to/from unsigned long long conversions
//
// program:
// END // END is end-of-input
// expr_list END
//
// expr_list:
// expression PRINT // PRINT is semicolon
// expression PRINT expr_list
//
// expression:
// expression + term
// expression - term
// term
//
// term:
// term / primary
// term * primary
// term & primary
// term | primary
// term % primary
// term >> primary
// term << primary
// term ^ primary
// term > primary
// term >= primary
// term < primary
// term <= primary
// term == primary
// term != primary
// term && primary
// term || primary
// primary
//
// primary:
// NUMBER
// NAME
// NAME = expression
// - primary
// ~ primary
// ! primary
// ( expression )
//
// Notes:
// - This code has only been tested on LITTLE_ENDIAN (Intel) machines.
// - If you get unexpected results, consider using more parenthesis.
//
//*****************************************************************************
#define PARSER_LIBRARY_VERSION "0.9.10"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "exparser.h"
static unsigned long long s_expression(exp_pstat_t* pstat, int get);
static Token_value_t s_get_token(exp_pstat_t* pstat);
char*
exp_get_library_version(void)
{
return PARSER_LIBRARY_VERSION;
}
unsigned long long
exp_get_mapped_value(exp_pstat_t* pstat, char* name)
{
int i;
for(i=0; i<EXP_MAX_MAPPED_SYMBOLS; i++)
{
if(pstat->map[i].name[0] != 0)
{
if((strcmp(name, pstat->map[i].name)) == 0)
{
return pstat->map[i].value;
}
}
}
return 0;
}
int
exp_set_mapped_value(exp_pstat_t* pstat, char* name, unsigned long long value)
{
int i;
for(i=0; i<EXP_MAX_MAPPED_SYMBOLS; i++)
{
if(pstat->map[i].name[0] != 0)
{
if((strcmp(name, pstat->map[i].name)) == 0)
{
pstat->map[i].value = value;
return 0;
}
}
}
for(i=0; i<EXP_MAX_MAPPED_SYMBOLS; i++)
{
if(pstat->map[i].name[0] == 0)
{
strcpy(pstat->map[i].name, name);
pstat->map[i].value = value;
return 0;
}
}
return -1;
}
static unsigned long long
s_function_abs(exp_pstat_t* pstat)
{
unsigned long long v = 0;
long long sv = 0;
if(s_get_token(pstat) != LP)
{
fprintf(stderr, "( expected: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
return 0;
}
v = s_expression(pstat, 1);
if(pstat->curr_tok != RP)
{
fprintf(stderr, ") expected: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
return 0;
}
s_get_token(pstat);
sv = (long long)v;
if(sv < 0)
{
return -sv;
}
return v;
}
static unsigned long long
s_function_mask(exp_pstat_t* pstat)
{
long long startbit = 0;
long long endbit = 0;
long long tmpbit = 0;
unsigned long long value = 0;
unsigned char* vp = 0;
int byte = 0;
int shift = 0;
int mask = 0;
if(s_get_token(pstat) != LP)
{
fprintf(stderr, "( expected: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
return 0;
}
startbit = s_expression(pstat, 1);
if(pstat->curr_tok != COMMA)
{
fprintf(stderr, ", expected: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
return 0;
}
endbit = s_expression(pstat, 1);
if(pstat->curr_tok != RP)
{
fprintf(stderr, ") expected: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
return 0;
}
s_get_token(pstat);
value = 0;
vp = (unsigned char*)&value+7;
if(startbit > 63)
{
startbit = 63;
}
if(endbit < 0)
{
endbit = 0;
}
if(endbit > startbit)
{
tmpbit = endbit;
endbit = startbit;
startbit = tmpbit;
}
vp = (unsigned char*)&value+7;
for(byte=7; byte>= 0; byte--)
{
if((startbit >= (byte*8)) && (startbit < (byte+1)*8))
{
shift = (((byte+1)*8)-1) - startbit;
mask = ((0xffU >> (shift)) & 0xffU);
*vp = mask;
}
else if((startbit > (byte*8)))
{
*vp = 0xff;
}
if((endbit >= (byte*8)) && (endbit < (byte+1)*8))
{
shift = (((byte+1)*8)) - endbit;
mask = ((0xffU >> (shift)) & 0xffU);
*vp &= ~mask;
break;
}
vp--;
}
return value;
}
static Token_value_t
s_get_token(exp_pstat_t* pstat)
{
char ch = 0;
//Skip white space
while(1)
{
if(*pstat->input == 0)
{
return pstat->curr_tok = END;
}
if(!(isspace(*pstat->input)))
{
break;
}
pstat->input++;
}
ch = *pstat->input;
switch(ch)
{
case ';':
case '\n':
pstat->input++;
return pstat->curr_tok = PRINT;
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '%':
case '^':
case '~':
case ',':
pstat->input++;
return pstat->curr_tok = (Token_value_t)ch;
case '&':
pstat->input++;
switch(*pstat->input)
{
case '&':
pstat->input++;
return pstat->curr_tok = LAND;
default:
return pstat->curr_tok = AND;
}
case '|':
pstat->input++;
switch(*pstat->input)
{
case '|':
pstat->input++;
return pstat->curr_tok = LOR;
default:
return pstat->curr_tok = OR;
}
case '!':
pstat->input++;
switch(*pstat->input)
{
case '=':
pstat->input++;
return pstat->curr_tok = CNE;
default:
return pstat->curr_tok = LNOT;
}
case '=':
pstat->input++;
switch(*pstat->input)
{
case '=':
pstat->input++;
return pstat->curr_tok = CE;
default:
return pstat->curr_tok = ASSIGN;
}
case '>':
pstat->input++;
switch(*pstat->input)
{
case '=':
pstat->input++;
return pstat->curr_tok = CGE;
case '>':
pstat->input++;
return pstat->curr_tok = SR;
default:
return pstat->curr_tok = CGT;
}
case '<':
pstat->input++;
switch(*pstat->input)
{
case '=':
pstat->input++;
return pstat->curr_tok = CLE;
case '<':
pstat->input++;
return pstat->curr_tok = SL;
default:
return pstat->curr_tok = CLT;
}
case '0' ... '9':
pstat->number_value = strtoull(pstat->input, &pstat->input, 0);
return pstat->curr_tok = NUMBER;
default:
if((isalpha(ch)) || (ch == '$') || (ch == '.'))
{
int i = 0;
pstat->string_value[i] = 0;
while((*pstat->input != 0) && ((isalnum(*pstat->input)) ||
(*pstat->input == '$') || (*pstat->input == '.')))
{
pstat->string_value[i] = *pstat->input;
pstat->input++;
i++;
}
pstat->string_value[i] = 0;
return pstat->curr_tok = NAME;
}
fprintf(stderr, "Bad token: %d(0x%x)", ch, ch);
pstat->errors++;
pstat->input++;
return pstat->curr_tok = PRINT;
}
}
static unsigned long long
s_primary(exp_pstat_t* pstat, int get)
{
unsigned long long v = 0;
char string_save[256];
if(get)
{
s_get_token(pstat);
}
switch(pstat->curr_tok)
{
case NUMBER: // Unsigned unsigned long long constant
v = pstat->number_value;
s_get_token(pstat);
return v;
case NAME:
// abs() function?
if((strcasecmp(pstat->string_value, EXP_FUNCTION_ABS)) == 0)
{
return s_function_abs(pstat);
}
// mask() function?
if((strcasecmp(pstat->string_value, EXP_FUNCTION_MASK)) == 0)
{
return s_function_mask(pstat);
}
if(s_get_token(pstat) == ASSIGN)
{
strcpy(string_save, pstat->string_value);
v = s_expression(pstat, 1);
exp_set_mapped_value(pstat, string_save, v);
}
else
{
v = exp_get_mapped_value(pstat, pstat->string_value);
}
return v;
case MINUS:
return -s_primary(pstat, 1);
case PLUS:
return s_primary(pstat, 1);
case NOT:
return ~s_primary(pstat, 1);
case LNOT:
v = s_primary(pstat, 1);
if(v)
{
return 0;
}
return 1;
case LP:
v = s_expression(pstat, 1);
if(pstat->curr_tok != RP)
{
fprintf(stderr, ") expected: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
pstat->errors++;
return 0;
}
s_get_token(pstat);
return v;
default:
fprintf(stderr, "primary expected: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
pstat->errors++;
return 0;
}
}
static unsigned long long
s_term(exp_pstat_t* pstat, int get)
{
unsigned long long left = s_primary(pstat, get);
unsigned long long right = 0;
while(1)
{
switch(pstat->curr_tok)
{
case AND:
left &= s_primary(pstat, 1);
break;
case OR:
left |= s_primary(pstat, 1);
break;
case MUL:
left *= s_primary(pstat, 1);
break;
case DIV:
right = s_primary(pstat, 1);
if(right != 0)
{
left /= right;
break;
}
fprintf(stderr, "Divide by zero: %d(0x%x)", pstat->curr_tok, pstat->curr_tok);
pstat->errors++;
left = 0;
break;
case SR:
left >>= s_primary(pstat, 1);
break;
case SL:
left <<= s_primary(pstat, 1);
break;
case MOD:
left %= s_primary(pstat, 1);
break;
case XOR:
left ^= s_primary(pstat, 1);
break;
case CE:
right = s_primary(pstat, 1);
if(left == right)
{
left = 1;
}
else
{
left = 0;
}
break;
case CNE:
right = s_primary(pstat, 1);
if(left != right)
{
left = 1;
}
else
{
left = 0;
}
break;
case CGT:
right = s_primary(pstat, 1);
if(left > right)
{
left = 1;
}
else
{
left = 0;
}
break;
case CLT:
right = s_primary(pstat, 1);
if(left < right)
{
left = 1;
}
else
{
left = 0;
}
break;
case CGE:
right = s_primary(pstat, 1);
if(left >= right)
{
left = 1;
}
else
{
left = 0;
}
break;
case CLE:
right = s_primary(pstat, 1);
if(left <= right)
{
left = 1;
}
else
{
left = 0;
}
break;
case LOR:
right = s_primary(pstat, 1);
left = left || right;
break;
case LAND:
right = s_primary(pstat, 1);
left = left && right;
break;
default:
return left;
}
}
}
static unsigned long long
s_expression(exp_pstat_t* pstat, int get)
{
unsigned long long left = s_term(pstat, get);
while(1)
{
switch(pstat->curr_tok)
{
case PLUS:
left += s_term(pstat, 1);
break;
case MINUS:
left -= s_term(pstat, 1);
break;
default:
return left;
}
}
}
int
exp_parse_expression(exp_pstat_t* pstat, char* expr, unsigned long long* result)
{
pstat->input = expr;
while(*pstat->input)
{
s_get_token(pstat);
if(pstat->curr_tok == END)
{
break;
}
if(pstat->curr_tok == PRINT)
{
continue;
}
*result = s_expression(pstat, 0);
}
return 0;
}
unsigned long long
exp_ipv6_prefix_to_ull(struct in6_addr* ipv6)
{
int offset = 0;
unsigned long long temp = 0;
unsigned char* dst = (unsigned char*)&temp+7;
unsigned char* src = (unsigned char*)ipv6;
for(offset=0; offset<8; offset++, dst--, src++)
{
*dst = *src;
}
return temp;
}
unsigned long long
exp_ipv6_host_to_ull(struct in6_addr* ipv6)
{
int offset = 0;
unsigned long long temp = 0;
unsigned char* dst = (unsigned char*)&temp+7;
unsigned char* src = (unsigned char*)ipv6+8;
for(offset=0; offset<8; offset++, dst--, src++)
{
*dst = *src;
}
return temp;
}
void
exp_pull_hull_to_ipv6(unsigned long long prefix, unsigned long long host, struct in6_addr* ipv6)
{
int offset = 0;
unsigned char* src = (unsigned char*)&prefix;
unsigned char* dst = (unsigned char*)ipv6+7;
for(offset=0; offset<8; offset++, dst--, src++)
{
*dst = *src;
}
src = (unsigned char*)&host;
dst = (unsigned char*)ipv6+15;
for(offset=0; offset<8; offset++, dst--, src++)
{
*dst = *src;
}
return;
}
unsigned long long
exp_ipv4_address_to_ull(struct in_addr* ipv4)
{
unsigned long long temp = ntohl(ipv4->s_addr);
return temp;
}
void
exp_ull_to_ipv4_address(unsigned long long ull, struct in_addr* ipv4)
{
unsigned long temp = ull & 0xffffffff;
temp = htonl(temp);
memcpy(ipv4, &temp, 4);
return;
}
void
exp_ipv6_string_to_addr(char* ipv6_str, struct in6_addr* ipv6)
{
memset(ipv6, 0, sizeof(struct in6_addr));
inet_pton(AF_INET6, ipv6_str, ipv6);
}
void
exp_ipv6_addr_to_string(struct in6_addr* ipv6, char* ipv6_str, int length)
{
memset(ipv6_str, 0, length);
inet_ntop(AF_INET6, ipv6, ipv6_str, (socklen_t)length);
}
void
exp_ipv4_string_to_addr(char* ipv4_str, struct in_addr* ipv4)
{
memset(ipv4, 0, sizeof(struct in_addr));
inet_pton(AF_INET, ipv4_str, ipv4);
}
void
exp_ipv4_addr_to_string(struct in_addr* ipv4, char* ipv4_str, int length)
{
memset(ipv4_str, 0, length);
inet_ntop(AF_INET, ipv4, ipv4_str, (socklen_t)length);
}