forked from pig4210/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signaturematcher.cpp
2589 lines (2390 loc) · 72.6 KB
/
signaturematcher.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
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma warning(push)
#pragma warning(disable:4512) //SIG_STL无法生成赋值运算符
#include "signaturematcher.h"
#include "hex_str.h"
#include "ws_s.h"
#include "pe.h"
#include "syssnap.h"
#include <bitset>
using namespace std;
///////////////////////////调试信息输出设置///////////////////////////////
namespace signaturematcher
{
class smlog : public xmsg
{
public:
virtual ~smlog()
{
if(empty()) return;
if(func != nullptr)
{
append("\r\n");
func(c_str());
clear();
return;
}
#ifndef FOR_RING0
OutputDebugStringA(c_str());
#else
DbgPrint("%s\n", c_str());
#endif
clear();
}
public:
static xlog::levels log_level;
static log_out_func func;
};
xlog::levels smlog::log_level = xlog::lvl_error;
log_out_func smlog::func = nullptr;
#define smlog_do(v) if((v) <= log_level()) smlog()
#define smtrace smlog_do(xlog::lvl_trace)
#define smerr smlog_do(xlog::lvl_error)
}
////////////////////////////特征码字符串类////////////////////////////////
namespace signaturematcher
{
//! 特征码字符串类,用于操纵字符串的遍历
class Sign
{
public:
Sign(SIGNATURE sig)
:sign(sig), scaner(0)
{
;
}
//! 返回当前处理的字符
char ch() const
{
return sign[scaner];
}
//! 向后移动一个字符
void step()
{
++scaner;
}
private:
friend static xmsg& operator<<(xmsg& msg, const Sign& s);
private:
SIGNATURE sign; //!< 存放特征码字符串指针
intptr_t scaner; //!< 指示当前解析字符起始索引
};
#pragma warning(disable:4239)
//! 重载输出操作以输出当前处理信息
static xmsg& operator<<(xmsg& msg, const Sign& s)
{
intptr_t row = 1;
intptr_t col = 1;
const intptr_t lp = s.scaner;
for(intptr_t i = 0; i < lp; ++i)
{
switch(s.sign[i])
{
//注意只识别\n以兼容Unix
case '\n':
col = 0; ++row;
break;
case '\0':
return msg << "第" << row << "行" << col << "列[越界错误]";
default:;
}
++col;
}
return msg << "第" << row << "行" << col << "列:";
}
}
///////////////////////////////词法类型///////////////////////////////////
namespace signaturematcher
{
enum LexicalType : unsigned char
{
LT_Error, //!< 错误词法
LT_End, //!< end -> \0
//!< ws -> [ \t\n\r]* //空白词法不生成类型
//!< note -> #.*\n|\0 //注释词法不生成类型
//!< 以下词法不单独成词
//!< hex -> [0-9A-Fa-f]
//!< range_value -> {hex}({ws}{hex}){1,7}//x64下为{hex}({ws}{hex}){0,15}
//!< range_delimiter -> [,-~:\|]
//!< range -> {ws}([\*\+\?]) | (\{{ws}{range_value}?{ws}{range_delimiter}?{ws}{range_value}?{ws}\})
//!< hexhex -> ({hex}{ws}{hex}) | (\:[^\0])
LT_String, //!< string -> [L|l]?{ws}\'[^\0]+\'
LT_Quote, //!< quote -> [L|l]?{ws}\"[^\0]+\"
LT_Dot, //!< dot -> \.{range}?
LT_MarkRef, //!< markreg -> {hexhex}({ws}@{ws}L)?({ws}@{ws}R)?
//!< refreg -> {hexhex}({ws}${ws}[1-7]{ws}L)?({ws}${ws}[1-7]{ws}R)?
LT_Ucbit, //!< ucbit -> {hexhex}({ws}[\-\|\&]{ws}{hexhex})*{range}?
LT_ConstHex, //!< consthex -> {hexhex}{range}?
//!< collection -> \[\^?{hexhex}({ws}[\&\|\&]?{ws}{hexhex})*{ws}\]{range}?
LT_Record_Addr,
LT_Record_Offset,
LT_Record_Qword,
LT_Record_Dword,
LT_Record_Word,
LT_Record_Byte, //!< record -> \<{ws}\^?{ws}[AFQDWB]{ws}[^ \t\n\r\0>]*{ws}[^ \t\n\r\0]*{ws}\>
};
}
///////////////////////////range标识结构//////////////////////////////////
namespace signaturematcher
{
typedef intptr_t RangeType; //!< 范围类型
//! 范围指示结构
struct Range
{
RangeType N; //!< 最小匹配次数
RangeType M; //!< 最大匹配次数
//! 简单初始化
Range(const RangeType& n, const RangeType& m)
:N(n), M(m)
{
;
}
//! 指定固定范围
Range(const RangeType& n)
:N(n), M(n)
{
;
}
};
//! 用以标识错误或最大范围指示值
static const RangeType gk_max_range =
#ifdef _WIN64
0x7FFFFFFFFFFFFFFF;
#else
0x7FFFFFFF;
#endif
//! 用以标识错误的范围
static const Range gk_err_range = { -1, -1 };
//! 用以标识缺省的范围
static const Range gk_normal_range = { 1, 1 };
//! 用以标识空的范围
static const Range gk_none_range = { 0, 0 };
//! 比较范围是否相同
static bool operator==(const Range& ra, const Range& rb)
{
return (ra.N == rb.N) && (ra.M == rb.M);
}
//! 范围融合
static Range& operator+=(Range& ra, const Range& rb)
{
const RangeType N = ra.N + rb.N;
ra.N = (N < ra.N) ? gk_max_range : N;
const RangeType M = ra.M + rb.M;
ra.M = (M < ra.M) ? gk_max_range : M;
return ra;
}
}
///////////////////////////////词法基类///////////////////////////////////
namespace signaturematcher
{
class LexicalBase
{
public:
LexicalBase(LexicalType t = LT_Error,const Range& r = gk_normal_range)
:type(t), range(r), match_count(-1)
{
;
}
//! 空虚析构,必要存在,以使继承类能正确释放资源
virtual ~LexicalBase()
{
;
}
virtual const char* type_name() const
{
return "词法end/未命名/错误类型";
}
//! 用以指示是否有效词法
bool valid() const
{
return type != LT_Error;
}
//! 用以整合同类型,返回表示融合失败,融合成功后注意释放资源。默认不融合
virtual bool fusion(LexicalBase*)
{
return false;
}
//! 用以指示是否特征点,默认不是
virtual bool record() const
{
return false;
}
//! 指定内存范围和索引,进行匹配,匹配失败返回false,失败且lp > blk.size()时,彻底失败
bool match(const xblk& blk, RangeType& lp)
{
void* pp = (unsigned char*)blk.start() + lp;
if(match_count >= range.M)
{
smtrace << (void*)((unsigned char*)pp - match_count)
<< ' ' << type_name()
<< "^匹配范围达到最大值:" << (void*)range.M << ",回退";
lp -= match_count;
match_count = -1;
return false;
}
if(match_count < range.N)
{
if((RangeType)blk.size() < (lp + range.N))
{
smtrace << pp << ' ' << type_name()
<< "^无法实现最低" << (void*)range.N << "次匹配,失败!";
lp = gk_max_range;
return false;
}
smtrace << pp << ' ' << type_name()
<< "^正在进行最低" << (void*)range.N << "次匹配";
const xblk mem(pp, range.N);
if(!test(mem))
{
smtrace << pp << ' ' << type_name()
<< "^最低匹配失败,回退";
return false;
}
match_mem = pp;
lp += range.N;
match_count = range.N;
return true;
}
if((RangeType)blk.size() < (lp + 1))
{
smtrace << pp << ' ' << type_name() << "^已经无法递进匹配了,失败!";
lp = gk_max_range;
return false;
}
smtrace << pp << ' ' << type_name()
<< "^正在进行递进匹配";
const xblk mem(pp, 1);
if(!test(mem))
{
smtrace << (void*)((unsigned char*)pp - match_count)
<< ' ' << type_name() << "^递进匹配失败,回退";
lp -= match_count;
match_count = -1;
return false;
}
++lp;
++match_count;
return true;
}
//! 设计防止内存不可读产生异常
virtual bool chk(const xblk& blk) const
{
#ifdef FOR_RING0
const size_t base_page = 0x1000;
if(blk.size() == 0) return true;
size_t s = (size_t)blk.start() / base_page * base_page;
size_t e = ((size_t)blk.end() - 1) / base_page * base_page;
do
{
if(!MmIsAddressValid((PVOID)s)) return false;
s += base_page;
}while(s <= e);
return true;
#else
return FALSE == IsBadReadPtr(blk.start(), blk.size());
#endif
}
//! 用以给定内存段细节匹配,默认匹配。需要细节匹配则重载之
virtual bool test(const xblk& blk) const
{
return chk(blk);
}
//! 输出原子细节
virtual void to_atom(line&) const
{
return;
}
//! 用以输出原子
void create_atom(line& atom) const
{
atom << type << range;
to_atom(atom);
}
public:
LexicalType type; //!< 指示词法类型
Range range; //!< 指示词法匹配内存大小
RangeType match_count; //!< 指示词法在匹配过程中匹配的大小
void* match_mem; //!< 用以记录匹配位置
};
}
////////////////////词法end、ws、note、hex识别函数////////////////////////
namespace signaturematcher
{
//! 识别词法end,成功返回LT_End,否则返回nullptr
static LexicalBase* match_end(Sign& sig)
{
if(sig.ch() != '\0') return nullptr;
sig.step();
smtrace << "识别到词法end";
return new LexicalBase(LT_End);
}
//! 识别词法ws,只是跳过ws,无论成功与否都返回nullptr
static LexicalBase* match_ws(Sign& sig)
{
while(true)
{
switch(sig.ch())
{
case ' ': case '\t': case '\n': case '\r':
sig.step(); continue;
default:;
}
break;
};
return nullptr;
}
//! 识别词法note,只是跳过note,无论成功与否都返回nullptr
static LexicalBase* match_note(Sign& sig)
{
//建立循环是为了处理连继注释的情况
while(sig.ch() != '\0')
{
if(sig.ch() != '#') return nullptr;
sig.step();
while(sig.ch() != '\n' && sig.ch() != '\0')
sig.step();
smtrace << "跳过词法note";
match_ws(sig);
}
return nullptr;
}
//! 匹配hex词法,返回值 < 0表示非此词法
static char match_hex(Sign& sig)
{
switch(sig.ch())
{
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
case '8': case '9':
case 'A': case 'B': case 'C': case 'D':
case 'E': case 'F':
case 'a': case 'b': case 'c': case 'd':
case 'e': case 'f':
{
char hex = sig.ch() & 0x0F;
if(sig.ch() > '9') hex += 0x09;
sig.step();
return hex;
}
default:;
}
return -1;
}
}
///////////////////////////词法range提取函数//////////////////////////////
namespace signaturematcher
{
//! 匹配词法range_value,返回值 < 0表示非此词法
static RangeType match_range_value(Sign& sig)
{
RangeType r = -1;
char hex = match_hex(sig);
if(hex < 0) return r;
r = hex;
for(size_t i = 0; i < (2 * sizeof(RangeType) - 1); ++i)
{
match_ws(sig);
hex = match_hex(sig);
if(hex < 0) return r;
r = (r << 4) | hex;
}
return r;
}
//! 匹配词法range_delimiter
static bool match_range_delimiter(Sign& sig)
{
switch(sig.ch())
{
case ',': case '-': case '~': case '|': case ':':
sig.step(); return true;
default:;
}
return false;
}
//! 匹配词法range,返回值 == gk_err_range时,匹配错误。
static Range match_range(Sign& sig)
{
Range range = gk_normal_range;
match_ws(sig);
switch(sig.ch()) //先识别单一字符的范围指示
{
case '*':
smtrace << " 识别到范围*";
range.N = 0; range.M = gk_max_range;
sig.step();
return range;
case '+':
smtrace << " 识别到范围+";
range.N = 1; range.M = gk_max_range;
sig.step();
return range;
case '?':
smtrace << " 识别到范围?";
range.N = 0; range.M = 1;
sig.step();
return range;
case '{':
sig.step();
break;
default:
smtrace << " 无范围指示,返回默认范围{1,1}";
return gk_normal_range;
}
match_ws(sig);
range.N = match_range_value(sig);
bool needM = true;
if(range.N >= 0) //存在N值
{
smtrace << " 识别到范围N值:" << (void*)range.N;
match_ws(sig);
if(!match_range_delimiter(sig)) //存在N值,但没有分隔符{N}
{
smtrace << " 范围分隔符不存在,不再提取M值";
range.M = range.N;
needM = false; //没有分隔符的情况下,不能继续提取M值
}
//存在N值且有分隔符的情况下,可能是{N,} | {N,M}
}
else //不存在N值
{
smtrace << " 没有识别到范围N值";
match_ws(sig);
if(!match_range_delimiter(sig)) //不存在N值且无分隔符,非法{}
{
smerr << sig << "范围指示识别失败!";
return gk_err_range;
}
smtrace << " 存在分隔符,N = 0";
range.N = 0; //不存在N值但有分隔符,可能是{,} | {,M}
}
if(needM)
{
match_ws(sig);
range.M = match_range_value(sig);
if(range.M >= 0) //存在M值{N,M} | {,M}
{
smtrace << " 识别到范围M值:" << (void*)range.M;
}
else //不存在M值{N,} | {,}
{
smtrace << " 没有识别到范围M值,M为最大值";
range.M = gk_max_range;
}
}
match_ws(sig);
if(sig.ch() != '}')
{
smerr << sig << "范围指示缺少'}'结束/存在非法字符/超越最大允许位数!";
return gk_err_range;
}
sig.step();
if(range.N == 0 && range.M == 0)
{
smerr << sig << "非法的空范围,请检查范围指示!";
return gk_err_range;
}
seqswap(range.N, range.M);
smtrace << " 确定范围:{" << (void*)range.N << ',' << (void*)range.M << "}";
return range;
}
}
/////////////////////词法string、quote类及识别函数////////////////////////
namespace signaturematcher
{
class LexicalString : public LexicalBase
{
public:
LexicalString(const string& s)
:LexicalBase(LT_String, Range(s.size())), str(s)
{
;
}
virtual const char* type_name() const
{
return "词法string";
}
virtual bool fusion(LexicalBase* lb)
{
if(lb->type != LT_String) return false;
str.append(((LexicalString*)lb)->str);
range = Range(str.size()); //注意不是+=
return true;
}
virtual bool test(const xblk& blk) const
{
if(!chk(blk)) return false;
smtrace << " 匹配string\r\n"
<< hex2show(str.c_str(), str.size(), 6)
<< hex2show(blk.start(), blk.size(), 6);
if(blk.size() != str.size()) return false;
return memcmp(str.c_str(), blk.start(), str.size()) == 0;
}
virtual void to_atom(line& nline) const
{
nline << str.size();
nline.append((const unsigned char*)str.c_str(), str.size());
}
public:
string str;
};
class LexicalQuote : public LexicalBase
{
public:
LexicalQuote(const string& s)
:LexicalBase(LT_Quote, Range(sizeof(void*))), str(s)
{
;
}
virtual const char* type_name() const
{
return "词法quote";
}
//继承funcsion、test
virtual void to_atom(line& nline) const
{
nline << str.size();
nline.append((const unsigned char*)str.c_str(), str.size());
}
public:
string str;
};
//! 识别词法string、quote,成功返回LT_String或LT_Quote,非词法返回nullptr,失败返回LT_Error
static LexicalBase* match_string_quote(Sign& sig)
{
bool unicode = false;
if(sig.ch() == 'L' || sig.ch() == 'l')
{
sig.step();
unicode = true;
match_ws(sig);
smtrace << "识别到Unicode标识";
}
const char ch = sig.ch();
if(ch != '\"' && ch != '\'' || ch == '\0')
{
if(!unicode) return nullptr;
smerr << sig << "不允许单独的Unicode标识'L'";
return new LexicalBase(LT_Error);
}
sig.step();
string str;
while(sig.ch() != '\0')
{
if(sig.ch() != ch)
{
//简单处理\'或\"的情况
if(sig.ch() == '\\')
{
sig.step();
if(sig.ch() == ch)
{
str.append(1, ch);
sig.step();
}
else
{
str.append(1, '\\');
}
continue;
}
str.append(1, sig.ch());
sig.step();
continue;
}
if(str.empty())
{
smerr << sig << "字符串/引用串不可以为空";
return new LexicalBase(LT_Error);
}
sig.step();
smtrace << "字符串提取完成:" << str;
const string s(escape(str));
smtrace << "字符串转换完成\r\n" << hex2show(s.c_str(), s.size(), 2);
if(unicode)
{
const wstring ws(s2ws(s));
str.assign((const char*)ws.c_str(), ws.size()* sizeof(wchar_t));
smtrace << "字符串转换为Unicode\r\n"
<< hex2show(str.c_str(), str.size(), 2, HC_UNICODE);
}
else
{
str = s;
}
if(ch == '\"') return new LexicalQuote(str);
return new LexicalString(str);
}
smerr << sig << "缺少" << ch << "来界定结束";
return new LexicalBase(LT_Error);
}
}
////////////////////////词法dot类及识别函数///////////////////////////////
namespace signaturematcher
{
class LexicalDot : public LexicalBase
{
public:
LexicalDot(const Range& range)
:LexicalBase(LT_Dot, range)
{
;
}
virtual const char* type_name() const
{
return "词法dot";
}
virtual bool fusion(LexicalBase* lb)
{
if(lb->type != LT_Dot) return false;
range += ((LexicalDot*)lb)->range;
return true;
}
};
//! 识别词法dot,成功返回LT_Dot,非词法返回nullptr,失败返回LT_Error
static LexicalBase* match_dot(Sign& sig)
{
if(sig.ch() != '.') return nullptr;
sig.step();
smtrace << "识别到词法dot";
Range range = match_range(sig);
if(range == gk_err_range) return new LexicalBase(LT_Error);
return new LexicalDot(range);
}
}
//////////////////////////////ucbit类/////////////////////////////////////
namespace signaturematcher
{
class ucbit : public bitset<0x100>
{
public:
//! 添加模糊匹配组
ucbit& set_obfus(const unsigned char TA, const unsigned char TB)
{
for(size_t i = 0; i < size(); ++i)
{
if(!((TA^i) & (TB^i)))
set(i);
}
return *this;
}
//! 添加连续组
ucbit& set_queue(unsigned char TA, unsigned char TB)
{
seqswap(TA, TB);
for(size_t i = TA; i <= TB; ++i)
set(i);
return *this;
}
};
}
/////////////////词法ucbit、consthex、markreg、refreg类///////////////////
namespace signaturematcher
{
class LexicalUcbit : public LexicalBase
{
public:
LexicalUcbit(const ucbit& u, const Range& range = gk_normal_range)
:LexicalBase(LT_Ucbit, range), uc(u)
{
}
virtual const char* type_name() const
{
return "词法ucbit";
}
virtual bool fusion(LexicalBase* lb)
{
if(lb->type != LT_Ucbit) return false;
if(uc != ((LexicalUcbit*)lb)->uc) return false;
range += ((LexicalUcbit*)lb)->range;
return true;
}
virtual bool test(const xblk& blk) const
{
if(!chk(blk)) return false;
const unsigned char* lp = (const unsigned char*)blk.start();
smtrace << " 匹配ucbit\r\n" << hex2show(lp, blk.size(), 6);
for(size_t i = 0; i < blk.size(); ++i)
{
if(!uc.test(lp[i])) return false;
}
return true;
}
virtual void to_atom(line& nline) const
{
nline << uc;
}
public:
ucbit uc;
};
class LexicalConstHex : public LexicalBase
{
public:
LexicalConstHex(const unsigned char h, const Range& range = gk_normal_range)
:LexicalBase(LT_ConstHex, range), hex(h)
{
}
virtual const char* type_name() const
{
return "词法consthex";
}
virtual bool fusion(LexicalBase* lb)
{
if(lb->type != LT_ConstHex) return false;
if(hex != ((LexicalConstHex*)lb)->hex) return false;
range += ((LexicalConstHex*)lb)->range;
return true;
}
virtual bool test(const xblk& blk) const
{
if(!chk(blk)) return false;
const char* lp = (const char*)blk.start();
smtrace << " 匹配consthex : " << hex << "\r\n"
<< hex2show(lp, blk.size(), 6);
for(size_t i = 0; i < blk.size(); ++i)
{
if(lp[i] != hex) return false;
}
return true;
}
virtual void to_atom(line& nline) const
{
nline << hex;
}
public:
unsigned char hex;
};
struct MarkRef
{
bool mark_right : 1;
bool mark_left : 1;
char ref_right : 4;
char ref_left : 4;
};
static const MarkRef gk_none_mark_ref = { false, false, 0, 0 };
static const MarkRef gk_err_mark_ref = { true, true, 1, 1 };
static bool operator==(const MarkRef& mra, const MarkRef& mrb)
{
return (mra.ref_left == mrb.ref_left) &&
(mra.mark_left == mrb.mark_left) &&
(mra.ref_right == mrb.ref_right) &&
(mra.mark_right == mrb.mark_right);
}
static bool operator!=(const MarkRef& mra, const MarkRef& mrb)
{
return !(mra == mrb);
}
class LexicalMarkRef : public LexicalBase
{
public:
LexicalMarkRef(const ucbit& u, const MarkRef& mr)
:LexicalBase(LT_MarkRef, gk_normal_range), uc(u), mark_ref(mr)
{
;
}
virtual const char* type_name() const
{
return "词法markref";
}
virtual bool test(const xblk& blk) const
{
if(!chk(blk)) return false;
const unsigned char* lp = (const unsigned char*)blk.start();
smtrace << " 匹配markref\r\n" << hex2show(lp, blk.size(), 6);
for(size_t i = 0; i < blk.size(); ++i)
{
if(!uc.test(lp[i])) return false;
}
return true;
}
virtual void to_atom(line& nline) const
{
nline << uc << mark_ref;
}
public:
ucbit uc;
MarkRef mark_ref;
};
}
//////////词法hexhex、ucbit、consthex、markreg、refreg识别函数////////////
namespace signaturematcher
{
//! 识别hexhex,成功返回string包含一个字符,非词法为空,错误则包含不只一个字符
static string match_hexhex(Sign& sig)
{
const char hhex = match_hex(sig);
if(hhex < 0)
{
if(sig.ch() != ':') return string();
sig.step();
char hex = sig.ch();
if(hex == '\0')
{
smerr << sig << "\":\"后提前结束";
return string("err");
}
sig.step();
return string(1, hex);
}
match_ws(sig);
const char lhex = match_hex(sig);
if(lhex < 0)
{
smerr << sig << "hexhex配对失败";
return string("err");
}
return string(1, hhex << 4 | lhex);
}
//! 识别markreg和refreg的后缀词法,返回gk_err_mark_ref、gk_none_mark_ref或结果
static MarkRef match_markref(Sign& sig)
{
match_ws(sig);
MarkRef mr = gk_none_mark_ref;
while(sig.ch() != '\0')
{
switch(sig.ch())
{
case '@':
{
sig.step();
match_ws(sig);
switch(sig.ch())
{
case 'L': case 'l':
if(mr.mark_left == true)
{
smerr << sig << "重复标识左寄存器";
return gk_err_mark_ref;
}
if(mr.ref_left != 0)
{
smerr << sig << "已引用左寄存器,不能继续标识,请指明其一";
return gk_err_mark_ref;
}
mr.mark_left = true;
smtrace << " 识别到mark_left";
break;
case 'R': case 'r':
if(mr.mark_right == true)
{
smerr << sig << "重复标识右寄存器";
return gk_err_mark_ref;
}
if(mr.ref_right != 0)
{
smerr << sig << "已引用右寄存器,不能继续标识,请指明其一";
return gk_err_mark_ref;
}
mr.mark_right = true;
smtrace << " 识别到mark_right";
break;
default:
smerr << sig << "未知的标记符:" << sig.ch() << ",请指定[LR]";
return gk_err_mark_ref;
}
sig.step();
}
break;
case '$':
{
sig.step();
match_ws(sig);
char hex = match_hex(sig);
if(hex <= 0)
{
hex = 1;
}
match_ws(sig);
switch(sig.ch())
{
case 'L': case 'l':
if(mr.mark_left == true)
{
smerr << sig << "已标识左寄存器,不能引用,请指明其一";
return gk_err_mark_ref;
}
if(mr.ref_left != 0)
{
smerr << sig << "已引用左寄存器";
return gk_err_mark_ref;
}
mr.ref_left = hex;
smtrace << " 识别到ref_left:" << (unsigned char)hex;
break;
case 'R': case 'r':
if(mr.mark_right == true)
{
smerr << sig << "已标识右寄存器,不能引用,请指明其一";
return gk_err_mark_ref;
}
if(mr.ref_right != 0)
{
smerr << sig << "已引用右寄存器";
return gk_err_mark_ref;
}
mr.ref_right = hex;
smtrace << " 识别到ref_right:" << (unsigned char)hex;
break;
default:
smerr << sig << "未知的引用符或索引:" << sig.ch() << ",请指定[1-F][LR]";
return gk_err_mark_ref;
}
sig.step();
}
break;
default: return mr;
}
}
return mr;
}
/*!
优化识别结果
当非模糊匹配时,如果范围固定,转化为LT_String,否则转化为LT_ConstHex
当模糊匹配全部时,转化为LT_Dot,否则为LT_Ucbit
*/
static LexicalBase* ucbit_optimization(const ucbit& uc, const Range& range)
{