-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.cpp
715 lines (601 loc) · 18.5 KB
/
parser.cpp
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
#include <cstdio>
#include <cstdlib>
#include <vector>
#include "parser.h"
using namespace std;
// returns the parsed message size which might be less or equal to the buffer size
// Throws need_more_data exception if parse failed but you could try a greater maxlength of at least the need_more_data.howmuch()
size_t field::parse(const string &msgbuf)
{
return parse(msgbuf.data(), msgbuf.length());
}
size_t field::parse(const char *s, size_t n)
{
field message(firstfrm);
if(!s)
throw invalid_argument("Empty input\n");
size_t parsedlength=message.parse_field(s, n);
moveFrom(message);
return parsedlength;
}
size_t field::parse_field_length(const char *buf, size_t maxlength)
{
size_t lenlen=0;
size_t newlength=0;
string lengthbuf;
lenlen=frm->lengthLength;
if(lenlen > maxlength)
throw need_more_data(lenlen, "Buffer less than length size");
switch(frm->lengthFormat)
{
case fldformat::fll_bin:
if(lenlen>4)
for(size_t i=0; i < lenlen-4; i++)
if(buf[i]!='\0')
throw invalid_argument("Length is too big");
for(size_t i=0; i<(lenlen>4?4:lenlen); i++)
((char *)(&newlength))[i]=buf[(lenlen>4?4:lenlen)-i-1];
break;
case fldformat::fll_ber:
if((unsigned char)buf[0]>127)
{
lenlen=2;
if(lenlen > maxlength)
throw need_more_data(lenlen, "Buffer less than length size");
}
else
lenlen=1;
((char *)(&newlength))[0]=buf[lenlen-1];
break;
case fldformat::fll_bcd:
if(lenlen>3)
{
for(size_t i=0; i < lenlen-3; i++)
if(buf[i]!='\0')
throw invalid_argument("Length is too big");
parse_hex(buf + lenlen - 3, lengthbuf, 6, '0');
}
else
parse_hex(buf, lengthbuf, lenlen*2, '0');
newlength=atoi(lengthbuf.c_str());
break;
case fldformat::fll_ascii:
if(lenlen>6)
for(size_t i=0; i < lenlen-6; i++)
if(buf[i]!='0')
throw invalid_argument("Length is too big");
lengthbuf.assign(buf, lenlen);
newlength=atoi(lengthbuf.c_str());
break;
case fldformat::fll_ebcdic:
if(lenlen>6)
for(size_t i=0; i < lenlen-6; i++)
if(buf[i]!=(char)0xF0)
throw invalid_argument("Length is too big");
if(lenlen>6)
parse_ebcdic(buf + lenlen - 6, lengthbuf, 6);
else
parse_ebcdic(buf, lengthbuf, lenlen);
newlength=atoi(lengthbuf.c_str());
break;
case fldformat::fll_unknown:
newlength=maxlength < frm->maxLength ? maxlength : frm->maxLength;
break;
case fldformat::fll_fixed:
newlength=frm->maxLength;
break;
default:
if(frm->dataFormat!=fldformat::fld_isobitmap)
throw invalid_argument("Unknown length format");
}
if(frm->addLength>0 && newlength<(size_t)frm->addLength)
throw invalid_argument("Negative length");
newlength-=frm->addLength;
if((frm->dataFormat==fldformat::fld_bcdsf || frm->dataFormat==fldformat::fld_hex) && frm->lengthFormat!=fldformat::fll_fixed)
newlength*=2;
flength=newlength;
return newlength;
}
// returns the parsed message size which might be less or equal to the buffer size
// Throws need_more_data exception if parse failed but you could try a greater maxlength of at least the need_more_data.howmuch()
size_t field::parse_field(const char *buf, size_t maxlength)
{
int newlength;
size_t minlength=0;
if(!maxlength)
throw need_more_data(1, "Zero buffer length");
if(blength && frm->altformat)
{
clear();
try
{
switch_altformat();
}
catch(const exception &e)
{
throw invalid_argument("Field was already parsed but unable to switch to altformat");
}
}
else
reset_altformat();
while(true)
{
if(debug && altformat)
printf("Info: parse_field: Retrying with alternate format \"%s\"\n", frm->get_description().c_str());
try
{
newlength=parse_field_alt(buf, maxlength);
return newlength;
}
catch(const need_more_data& e)
{
if(debug)
printf("Please retry with length %lu: %s\n", (unsigned long)e.howmuch(), e.what());
if(minlength==0 || e.howmuch()<minlength)
minlength=e.howmuch();
}
catch(const exception& e)
{
if(debug)
printf("Parsing of field failed: %s\n", e.what());
}
clear();
try
{
switch_altformat();
}
catch(const exception &e)
{
break;
}
}
if(!minlength)
throw invalid_argument("Parsing failed using all altformats");
else
throw need_more_data(minlength, "Parsing failed using all altformats, but you can try greater length");
}
size_t field::parse_field_alt(const char *buf, size_t maxlength)
{
size_t lenlen=0;
size_t newblength=0;
string lengthbuf;
int bitmap_start=-1;
int bitmap_end=0;
int parse_failed;
fldformat::const_iterator cursf;
const fldformat *curfrm;
size_t pos;
long int sflen;
size_t minlength=0;
size_t taglength=0;
fldformat tmpfrm;
size_t oldlenlen;
int curnum;
vector<int> fieldstack;
if(!buf)
throw invalid_argument("No buf");
lenlen=frm->lengthLength;
if(frm->lengthFormat==fldformat::fll_ber)
{
if((unsigned char)buf[0]>127)
lenlen=2;
else
lenlen=1;
}
if(frm->dataFormat!=fldformat::fld_isobitmap)
{
sflen=parse_field_length(buf, maxlength);
if(flength > frm->maxLength)
throw invalid_argument("field length exceeds max");
// flength=frm->maxLength;
switch(frm->dataFormat)
{
case fldformat::fld_bitmap:
case fldformat::fld_bitstr:
newblength=(flength+7)/8;
break;
case fldformat::fld_hex:
case fldformat::fld_bcdsf:
case fldformat::fld_bcdl:
case fldformat::fld_bcdr:
newblength=(flength+1)/2;
break;
default:
newblength=flength;
}
if(lenlen + newblength > maxlength)
throw need_more_data(lenlen+newblength, string("Field is bigger than buffer (") + to_string(lenlen+newblength) + ">" + to_string(maxlength) + ")");
// else if(lenlen + newblength < maxlength)
// throw invalid_argument("Field is smaller than buffer");
}
//Now we know the length except for ISOBITMAP
// printf("Length is %lu for %s\n", flength, frm->get_description().c_str());
switch(frm->dataFormat)
{
case fldformat::fld_bcdsf:
parse_bcdl(buf+lenlen, data, flength, frm->fillChar);
buf=data.data();
oldlenlen=lenlen;
lenlen=0;
maxlength=data.length();
// no break intentionally
case fldformat::fld_subfields:
case fldformat::fld_tlv:
parse_failed=1;
if(frm->dataFormat==fldformat::fld_tlv)
taglength=frm->tagLength;
else
cursf=frm->begin();
pos=lenlen;
while(parse_failed)
{
parse_failed=0; //TODO: use try{} block instead of parse_failed
if(frm->subfields.empty())
throw invalid_argument("No subformats loaded");
for(; pos!=maxlength;)
{
if(pos==flength+lenlen) // Some subfields are missing or canceled by bitmap, but we are precisely on the boundary of the field length
break;
if(frm->dataFormat==fldformat::fld_tlv) // subfield number is encoded as a tag name
{
if(frm->tagFormat==fldformat::flt_ber)
{
if((buf[pos]&0x1F)==0x1F)
taglength=2;
else
taglength=1;
}
if(pos+taglength>=maxlength)
throw need_more_data(pos+taglength, "Not enough length for TLV tag"); //TODO: check the value
lengthbuf.clear();
switch(frm->tagFormat)
{
case fldformat::flt_ebcdic:
parse_ebcdic(buf+pos, lengthbuf, taglength);
curnum=atoi(lengthbuf.c_str());
break;
case fldformat::flt_bcd:
parse_bcdl(buf+pos, lengthbuf, taglength*2, '0');
curnum=atoi(lengthbuf.c_str());
break;
case fldformat::flt_ber:
case fldformat::flt_bin:
curnum=0;
for(size_t i=0; i<taglength; i++)
*(((char*)(&curnum))+taglength-1-i)=buf[pos+i];
break;
case fldformat::flt_ascii:
lengthbuf.assign(buf+pos, taglength);
curnum=atoi(lengthbuf.c_str());
break;
default:
throw invalid_argument("Unknown tag format");
}
if(!frm->sfexist(curnum))
throw invalid_argument("No format for TLV tag");
curfrm=&frm->sf(curnum);
pos+=taglength;
}
else // subfield number is defined by its position
{
if(cursf==frm->end())
{
if(debug)
printf("Field length is greater than formats available (%s)\n", frm->get_description().c_str());
parse_failed=1;
break;
}
curfrm=&cursf->second;
curnum=cursf->first;
++cursf;
}
fieldstack.push_back(curnum);
sflen=0; //TODO: Remove
if(bitmap_start!=-1 && sf(bitmap_start).frm->dataFormat==fldformat::fld_isobitmap && bitmap_end < curnum)
break;
if(bitmap_start==-1 || (bitmap_end > curnum-1 && sf(bitmap_start).data[curnum-bitmap_start-1]=='1'))
{
if(sfexist(curnum))
{
sflen=sf(curnum).blength;
sf(curnum).clear();
sf(curnum).blength=sflen;
}
if(curfrm->lengthFormat==fldformat::fll_unknown && curfrm->dataFormat!=fldformat::fld_isobitmap) //for unknown length, search for the smallest
{
sflen=-1; // TODO: Optimize for some cases when sf length can be known more precise
for(size_t i=sf(curnum).blength+1; i<flength+lenlen-pos+1; i=-sflen)
{
if(debug)
printf("trying pos %lu length %lu/%lu for %s\n", (unsigned long)pos, (unsigned long)i, (unsigned long)(flength+lenlen-pos), curfrm->get_description().c_str()); //TODO: remove curfrm
sf(curnum).blength=0;
try
{
sflen=sf(curnum).parse_field(buf+pos, i);
if((size_t)sflen<i)
{
if(debug)
printf("Parsed successfully but with less length of %ld/%lu\n", sflen, (unsigned long)i);
sflen=0;
}
break;
}
catch(const need_more_data& e)
{
sflen=-e.howmuch();
}
catch(const exception& e)
{
sflen=0;
break;
}
sf(curnum).reset_altformat(); //restore frm that might be replaced with altformat by parse_field
if((size_t)-sflen<=i)
sflen=-(i+1);
}
}
else
{
try
{
sflen=sf(curnum).parse_field(buf+pos, flength+lenlen-pos);
}
catch(const need_more_data& e)
{
sflen=-e.howmuch();
}
catch(const exception& e)
{
sflen=0;
}
}
if(sflen==0 && sf(curnum).frm->maxLength==0)
{
if(debug)
printf("Optional subfield %d (%s) skipped\n", curnum, sf(curnum).frm->get_description().c_str());
sf(curnum).clear();
continue;
}
if(sflen<=0)
{
if(debug)
printf("Error: unable to parse subfield %d (%s/%s, %ld)\n", curnum, get_description().c_str(), sf(curnum).frm->get_description().c_str(), sflen);
sf(curnum).clear();
parse_failed=1;
break;
}
if(frm->dataFormat!=fldformat::fld_tlv && (sf(curnum).frm->dataFormat==fldformat::fld_bitmap || sf(curnum).frm->dataFormat==fldformat::fld_isobitmap))
{
if(debug && bitmap_start!=-1)
printf("Warning: Only one bitmap per subfield allowed\n");
bitmap_start=curnum;
bitmap_end=bitmap_start+sf(bitmap_start).data.length();
for(int i=0; i<bitmap_end-bitmap_start; i++)
if(sf(bitmap_start).data[i]=='1' && !frm->sfexist(bitmap_start+1+i))
{
if(debug)
printf("Error: No format for subfield %d which is present in bitmap\n", bitmap_start+1+i);
sf(curnum).clear();
bitmap_start=-1;
parse_failed=1;
break;
}
}
sf(curnum).start=pos-taglength;
sf(curnum).blength+=taglength;
pos+=sflen;
}
}
if(!parse_failed)
{
if(pos!=flength+lenlen)
{
if(debug)
printf("Error: Not enough subfield formats (%lu, %lu) for %s\n", (unsigned long)pos, (unsigned long)flength, frm->get_description().c_str());
parse_failed=1;
}
else if(frm->lengthFormat==fldformat::fll_unknown && frm->hasBitmap!=-1)
{
if(bitmap_start==-1)
throw need_more_data(pos+1, "This field has a bitmap subfield which we hasn't read yet");
//Check if parsed all bitmapped fields
for(size_t i=0; i<sf(bitmap_start).data.length(); i++)
if(sf(bitmap_start).data[i]=='1' && bitmap_start+i+1>(size_t)curnum)
{
if(debug)
printf("Field %lu was not parsed yet\n", (unsigned long)(bitmap_start+i+1));
throw need_more_data(pos+1, "The bitmap defines a field that we hasn't read yet");
}
}
}
if(parse_failed)
{
if(sflen<0 && (minlength==0 || pos+(size_t)(-sflen)<minlength))
minlength=pos-sflen;
while(!fieldstack.empty())
{
if(frm->dataFormat!=fldformat::fld_tlv)
--cursf;
curnum=fieldstack.back();
fieldstack.pop_back();
if(curnum==bitmap_start)
bitmap_start=-1;
if(sfexist(curnum) && !sf(curnum).empty() && ((sf(curnum).frm->lengthFormat==fldformat::fll_unknown && sf(curnum).frm->dataFormat!=fldformat::fld_isobitmap && sf(curnum).blength < flength+lenlen-sf(curnum).start) || sf(curnum).frm->altformat))
{
if(debug)
printf("Come back to sf %d of %s (%s)\n", curnum, frm->get_description().c_str(), sf(curnum).frm->get_description().c_str());
pos=sf(curnum).start;
parse_failed=0;
break;
}
else if(sfexist(curnum))
sf(curnum).clear();
}
if(parse_failed)
{
if(debug)
printf("Not comming back (from %s)\n", frm->get_description().c_str());
break;
}
parse_failed=1;
}
}
if(parse_failed)
{
if(minlength==0)
throw invalid_argument("Error parsing of a subfield");
else
throw need_more_data(minlength, "Not enough data to parse all subfields");
}
if(frm->dataFormat==fldformat::fld_bcdsf)
lenlen=oldlenlen;
break;
case fldformat::fld_isobitmap:
for(size_t i=0; i==0 || buf[i*8-8]>>7; i++)
{
flength=i*64+63;
newblength=i*8+8;
if((i+1)*8>maxlength)
throw need_more_data((i+1)*8, "Field is too long"); //TODO: //check this
if(i!=0)
data.push_back('0');
for(unsigned char j=1; j<64; j++)
data.push_back((buf[i*8+j/8] & (1<<(7-j%8))) ? '1':'0');
}
break;
case fldformat::fld_bitmap:
case fldformat::fld_bitstr:
data.clear();
for(size_t i=0; i<flength;i++)
data.push_back((buf[lenlen + i/8] & (1<<(7-i%8))) ? '1':'0');
break;
case fldformat::fld_ascii:
data.assign(buf+lenlen, flength);
break;
case fldformat::fld_hex:
parse_hex(buf+lenlen, data, flength, '0');
break;
case fldformat::fld_bcdl:
parse_bcdl(buf+lenlen, data, flength, frm->fillChar);
break;
case fldformat::fld_bcdr:
parse_bcdr(buf+lenlen, data, flength, frm->fillChar);
break;
case fldformat::fld_ebcdic:
parse_ebcdic(buf+lenlen, data, newblength);
break;
default:
throw invalid_argument("Unknown data format");
}
if(!frm->data.empty() && !data.empty() && data!=frm->data)
{
data.clear();
throw invalid_argument("Format mandatory data does not match field data");
}
blength=lenlen+newblength;
if(debug && !data.empty() && frm->dataFormat!=fldformat::fld_subfields)
printf("%s\t(%lu/%lu): \"%s\"\n", frm->get_description().c_str(), (unsigned long)flength, (unsigned long)blength, data.c_str());
return lenlen+newblength;
}
size_t field::parse_ebcdic(const char *from, string &to, size_t len)
{
// const string ebcdic2ascii("\0\x001\x002\x003 \t \x07F \x00B\x00C\x00D\x00E\x00F\x010\x011\x012\x013 \n\x008 \x018\x019 \x01C\x01D\x01E\x01F\x080 \x01C \x00A\x017\x01B \x005\x006\x007 \x016 \x004 \x014\x015 \x01A âäàáãåçñ¢.<(+|&éêëèíîïìß!$*);¬-/ÂÄÀÁÃÅÇÑ\x0A6,%_>?øÉÊËÈÍÎÏÌ`:#@'=\"Øabcdefghi ý \x010jklmnopqr Æ æ ~stuvwxyz Ý ^£¥ [] {ABCDEFGHI ôöòóõ}JKLMNOPQR ûüùúÿ\\ STUVWXYZ ÔÖÒÓÕ0123456789 ÛÜÙÚŸ", 256);
const string ebcdic2ascii(" \x001\x002\x003 \t \x07F \x00B\x00C\x00D\x00E\x00F\x010\x011\x012\x013 \n\x008 \x018\x019 \x01C\x01D\x01E\x01F\x080 \x01C \x00A\x017\x01B \x005\x006\x007 \x016 \x004 \x014\x015 \x01A .<(+|& !$*); -/ \x0A6,%_>? `:#@'=\" abcdefghi \x010jklmnopqr ~stuvwxyz ^ [] {ABCDEFGHI }JKLMNOPQR \\ STUVWXYZ 0123456789 ", 256);
for(size_t i=0; i<len; i++)
to.push_back(ebcdic2ascii[(unsigned char)from[i]]);
return len;
}
size_t field::parse_bcdr(const char *from, string &to, size_t len, char fillChar)
{
for(size_t i=0; i<(len+1)/2; i++)
{
unsigned char t;
const bool u=(len/2*2==len);
size_t separator_found=0;
if(i || u)
{
t=((unsigned char)from[i]) >> 4;
if(17<len && len<38 && !separator_found && t==0xD) //making one exception for track2
{
separator_found=1;
to.push_back('^');
}
else if (t > 9)
throw invalid_argument("The string is not BCD");
else
to.push_back('0'+t);
}
else if(((unsigned char)from[i])>>4!=(fillChar=='F'?0xF:0))
throw invalid_argument("First 4 bits don't match fillChar");
t=((unsigned char)from[i]) & 0x0F;
if(17<len && len<38 && !separator_found && t==0xD) //making one exception for track2
{
separator_found=1;
to.push_back('^');
}
else if (t > 9)
throw invalid_argument("The string is not BCD");
else
to.push_back('0'+t);
}
return len;
}
size_t field::parse_bcdl(const char *from, string &to, size_t len, char fillChar)
{
for(size_t i=0; i<(len+1)/2; i++)
{
unsigned char t;
const bool u=(len/2*2==len);
size_t separator_found=0;
t=((unsigned char)from[i]) >> 4;
if(17<len && len<38 && !separator_found && t==0xD) //making one exception for track2
{
separator_found=1;
to.push_back('^');
}
else if (t > 9)
throw invalid_argument("The string is not BCD");
else
to.push_back('0'+t);
if(u || i!=(len+1)/2-1)
{
t=((unsigned char)from[i]) & 0x0F;
if(17<len && len<38 && !separator_found && t==0xD) //making one exception for track2
{
separator_found=1;
to.push_back('^');
}
else if (t > 9)
throw invalid_argument("The string is not BCD");
else
to.push_back('0'+t);
}
else if((((unsigned char)from[i]) & 0x0F)!=(fillChar=='F'?0xF:0))
throw invalid_argument("Last 4 bits don't match fillChar");
}
return len;
}
size_t field::parse_hex(const char *from, string &to, size_t len, char fillChar)
{
for(size_t i=0; i<(len+1)/2; i++)
{
unsigned char t;
const bool u=(len/2*2==len);
if(i || u)
{
t=((unsigned char)from[i]) >> 4;
if (t > 9)
to.push_back('A'+t-10);
else
to.push_back('0'+t);
}
else if(((unsigned char)from[i])>>4!=(fillChar=='F'?0xF:0))
throw invalid_argument("First 4 bits don't match fillChar");
t=((unsigned char)from[i]) & 0x0F;
if (t > 9)
to.push_back('A'+t-10);
else
to.push_back('0'+t);
}
return len;
}