libinjectionrs 0.1.1

Rust port of libinjection - SQL/XSS injection detection library
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
// SQL tokenizer implementation matching libinjection C version

use crate::sqli::{SqliFlags, sqli_data};

// Token type constants matching C version
const TYPE_NONE: u8 = 0;
const TYPE_KEYWORD: u8 = b'k';
const TYPE_UNION: u8 = b'U';
const TYPE_GROUP: u8 = b'B';
const TYPE_EXPRESSION: u8 = b'E';
const TYPE_SQLTYPE: u8 = b't';
const TYPE_FUNCTION: u8 = b'f';
const TYPE_BAREWORD: u8 = b'n';
const TYPE_NUMBER: u8 = b'1';
const TYPE_VARIABLE: u8 = b'v';
const TYPE_STRING: u8 = b's';
const TYPE_OPERATOR: u8 = b'o';
const TYPE_LOGIC_OPERATOR: u8 = b'&';
const TYPE_COMMENT: u8 = b'c';
const TYPE_COLLATE: u8 = b'A';
const TYPE_LEFTPARENS: u8 = b'(';
const TYPE_RIGHTPARENS: u8 = b')';
const TYPE_LEFTBRACE: u8 = b'{';
const TYPE_RIGHTBRACE: u8 = b'}';
const TYPE_DOT: u8 = b'.';
const TYPE_COMMA: u8 = b',';
const TYPE_COLON: u8 = b':';
const TYPE_SEMICOLON: u8 = b';';
const TYPE_TSQL: u8 = b'T';
const TYPE_UNKNOWN: u8 = b'?';
const TYPE_EVIL: u8 = b'X';
const TYPE_FINGERPRINT: u8 = b'F';
const TYPE_BACKSLASH: u8 = b'\\';

// Character constants
pub(crate) const CHAR_NULL: u8 = b'\0';
const CHAR_SINGLE: u8 = b'\'';
const CHAR_DOUBLE: u8 = b'"';
const CHAR_TICK: u8 = b'`';

// SQL injection limits
const LIBINJECTION_SQLI_TOKEN_SIZE: usize = 32;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TokenType {
    None,
    Keyword,
    Union,
    Group,
    Expression,
    SqlType,
    Function,
    Bareword,
    Number,
    Variable,
    String,
    Operator,
    LogicOperator,
    Comment,
    Collate,
    LeftParenthesis,
    RightParenthesis,
    LeftBrace,
    RightBrace,
    Dot,
    Comma,
    Colon,
    Semicolon,
    Tsql,
    Unknown,
    Evil,
    Fingerprint,
    Backslash,
}

impl TokenType {
    pub fn to_char(&self) -> char {
        match self {
            TokenType::None => '\0',
            TokenType::Keyword => 'k',
            TokenType::Union => 'U',
            TokenType::Group => 'B',
            TokenType::Expression => 'E',
            TokenType::SqlType => 't',
            TokenType::Function => 'f',
            TokenType::Bareword => 'n',
            TokenType::Number => '1',
            TokenType::Variable => 'v',
            TokenType::String => 's',
            TokenType::Operator => 'o',
            TokenType::LogicOperator => '&',
            TokenType::Comment => 'c',
            TokenType::Collate => 'A',
            TokenType::LeftParenthesis => '(',
            TokenType::RightParenthesis => ')',
            TokenType::LeftBrace => '{',
            TokenType::RightBrace => '}',
            TokenType::Dot => '.',
            TokenType::Comma => ',',
            TokenType::Colon => ':',
            TokenType::Semicolon => ';',
            TokenType::Tsql => 'T',
            TokenType::Unknown => '?',
            TokenType::Evil => 'X',
            TokenType::Fingerprint => 'F',
            TokenType::Backslash => '\\',
        }
    }
}

#[derive(Debug, Clone)]
pub struct Token {
    pub token_type: TokenType,
    pub pos: usize,
    pub len: usize,
    pub val: [u8; 32],
    pub str_open: u8,
    pub str_close: u8,
    pub count: i32,
}

impl Token {
    pub fn new() -> Self {
        Self {
            token_type: TokenType::None,
            pos: 0,
            len: 0,
            val: [0; 32],
            str_open: CHAR_NULL,
            str_close: CHAR_NULL,
            count: 0,
        }
    }
    
    pub fn value_as_str(&self) -> &str {
        let end = self.len.min(32);
        // For display purposes, show lossy conversion to handle 0xFF bytes
        // This won't affect tokenization logic, only debugging output
        std::str::from_utf8(&self.val[..end]).unwrap_or("<binary>")
    }
    
    pub fn clear(&mut self) {
        *self = Self::new();
    }
    
    pub fn assign_char(&mut self, token_type: u8, pos: usize, value: u8) {
        self.token_type = byte_to_token_type(token_type);
        self.pos = pos;
        self.len = 1;
        self.val[0] = value;
        self.val[1] = CHAR_NULL;
        // Note: str_open, str_close, and count are NOT reset to preserve variable info like C st_assign_char()
    }
    
    pub fn assign(&mut self, token_type: u8, pos: usize, len: usize, value: &[u8]) {
        let copy_len = len.min(LIBINJECTION_SQLI_TOKEN_SIZE - 1);
        let actual_copy_len = copy_len.min(value.len());
        self.token_type = byte_to_token_type(token_type);
        self.pos = pos;
        self.len = actual_copy_len;
        
        // Clear the value array first
        self.val = [0; 32];
        
        // Copy the value
        for i in 0..actual_copy_len {
            self.val[i] = value[i];
        }
        
        self.val[actual_copy_len] = CHAR_NULL;
        // Note: str_open, str_close, and count are NOT reset to preserve variable info like C st_assign()
    }
    
    pub fn copy_from(&mut self, other: &Token) {
        *self = other.clone();
    }
}

fn byte_to_token_type(b: u8) -> TokenType {
    match b {
        TYPE_KEYWORD => TokenType::Keyword,
        TYPE_UNION => TokenType::Union,
        TYPE_GROUP => TokenType::Group,
        TYPE_EXPRESSION => TokenType::Expression,
        TYPE_SQLTYPE => TokenType::SqlType,
        TYPE_FUNCTION => TokenType::Function,
        TYPE_BAREWORD => TokenType::Bareword,
        TYPE_NUMBER => TokenType::Number,
        TYPE_VARIABLE => TokenType::Variable,
        TYPE_STRING => TokenType::String,
        TYPE_OPERATOR => TokenType::Operator,
        TYPE_LOGIC_OPERATOR => TokenType::LogicOperator,
        TYPE_COMMENT => TokenType::Comment,
        TYPE_COLLATE => TokenType::Collate,
        TYPE_LEFTPARENS => TokenType::LeftParenthesis,
        TYPE_RIGHTPARENS => TokenType::RightParenthesis,
        TYPE_LEFTBRACE => TokenType::LeftBrace,
        TYPE_RIGHTBRACE => TokenType::RightBrace,
        TYPE_DOT => TokenType::Dot,
        TYPE_COMMA => TokenType::Comma,
        TYPE_COLON => TokenType::Colon,
        TYPE_SEMICOLON => TokenType::Semicolon,
        TYPE_TSQL => TokenType::Tsql,
        TYPE_UNKNOWN => TokenType::Unknown,
        TYPE_EVIL => TokenType::Evil,
        TYPE_FINGERPRINT => TokenType::Fingerprint,
        TYPE_BACKSLASH => TokenType::Backslash,
        _ => TokenType::None,
    }
}

fn token_type_to_byte(t: TokenType) -> u8 {
    match t {
        TokenType::Keyword => TYPE_KEYWORD,
        TokenType::Union => TYPE_UNION,
        TokenType::Group => TYPE_GROUP,
        TokenType::Expression => TYPE_EXPRESSION,
        TokenType::SqlType => TYPE_SQLTYPE,
        TokenType::Function => TYPE_FUNCTION,
        TokenType::Bareword => TYPE_BAREWORD,
        TokenType::Number => TYPE_NUMBER,
        TokenType::Variable => TYPE_VARIABLE,
        TokenType::String => TYPE_STRING,
        TokenType::Operator => TYPE_OPERATOR,
        TokenType::LogicOperator => TYPE_LOGIC_OPERATOR,
        TokenType::Comment => TYPE_COMMENT,
        TokenType::Collate => TYPE_COLLATE,
        TokenType::LeftParenthesis => TYPE_LEFTPARENS,
        TokenType::RightParenthesis => TYPE_RIGHTPARENS,
        TokenType::LeftBrace => TYPE_LEFTBRACE,
        TokenType::RightBrace => TYPE_RIGHTBRACE,
        TokenType::Dot => TYPE_DOT,
        TokenType::Comma => TYPE_COMMA,
        TokenType::Colon => TYPE_COLON,
        TokenType::Semicolon => TYPE_SEMICOLON,
        TokenType::Tsql => TYPE_TSQL,
        TokenType::Unknown => TYPE_UNKNOWN,
        TokenType::Evil => TYPE_EVIL,
        TokenType::Fingerprint => TYPE_FINGERPRINT,
        TokenType::Backslash => TYPE_BACKSLASH,
        _ => TYPE_NONE,
    }
}

// Lookup function type
type LookupFn = dyn Fn(&str) -> TokenType;

pub struct SqliTokenizer<'a> {
    input: &'a [u8],
    flags: SqliFlags,
    pos: usize,
    current: Token,
    lookup_fn: Option<&'a LookupFn>,
    pub stats_comment_c: i32,
    pub stats_comment_ddw: i32,
    pub stats_comment_ddx: i32,
    pub stats_comment_hash: i32,
}

impl<'a> SqliTokenizer<'a> {
    pub fn new(input: &'a [u8], flags: SqliFlags) -> Self {
        Self {
            input,
            flags,
            pos: 0,
            current: Token::new(),
            lookup_fn: None,
            stats_comment_c: 0,
            stats_comment_ddw: 0,
            stats_comment_ddx: 0,
            stats_comment_hash: 0,
        }
    }
    
    pub fn with_lookup_fn(mut self, lookup_fn: &'a LookupFn) -> Self {
        self.lookup_fn = Some(lookup_fn);
        self
    }
    
    fn lookup_word(&self, word: &str) -> TokenType {
        if let Some(lookup_fn) = self.lookup_fn {
            lookup_fn(word)
        } else {
            sqli_data::lookup_word(word)
        }
    }
    
    // Main tokenization function - matches libinjection_sqli_tokenize
    pub fn next_token(&mut self) -> Option<Token> {
        if self.input.is_empty() || self.pos >= self.input.len() {
            return None;
        }
        
        self.current.clear();
        
        // Handle quote context at start of string - matches C behavior
        let quote_context = self.flags.quote_context();
        if self.pos == 0 && quote_context != b'\0' {
            // FIXED: Parse only first token as string like C does with parse_string_core
            return self.parse_first_token_with_quote_context(quote_context);
        }
        
        while self.pos < self.input.len() {
            let ch = self.input[self.pos];
            let new_pos = self.dispatch_char_parser(ch);
            self.pos = new_pos;
            
            if self.current.token_type != TokenType::None {
                return Some(self.current.clone());
            }
        }
        
        None
    }
    
    fn parse_first_token_with_quote_context(&mut self, quote_char: u8) -> Option<Token> {
        // FIXED: Implements exact C behavior from libinjection_sqli.c parse_string_core function
        // Called from libinjection_sqli.c:1216: parse_string_core(s, slen, 0, current, flag2delim(sf->flags), 0)
        // This handles FLAG_QUOTE_DOUBLE context where input is treated as if starting with a quote
        
        let start_pos = self.pos; // Should be 0 for first token
        let len = self.input.len();
        let pos = start_pos; // 0 for first token
        let offset = 0; // simulated quote (libinjection_sqli.c:624 comment explains offset parameter)
        
        // Step 1: Find first quote occurrence
        // From libinjection_sqli.c:627-628: memchr(cs + pos + offset, delim, len - pos - offset)
        let search_start = pos + offset;
        let search_len = len - pos - offset;
        
        let mut qpos_idx = None;
        for i in search_start..(search_start + search_len) {
            if self.input[i] == quote_char {
                qpos_idx = Some(i);
                break;
            }
        }
        
        // From libinjection_sqli.c:638-643: Set str_open based on offset
        // offset = 0 means "simulated quote", so str_open = CHAR_NULL
        self.current.str_open = CHAR_NULL;
        
        // Main parsing loop from libinjection_sqli.c:645-672
        while let Some(qpos) = qpos_idx {
            // Check backslash escape - libinjection_sqli.c:655
            // C: is_backslash_escaped(qpos - 1, cs + pos + offset)
            if qpos > pos + offset {
                let check_pos = qpos - 1;
                let start_pos_for_escape = pos + offset;
                
                // Implement libinjection_sqli.c:586-606 is_backslash_escaped function exactly
                let mut backslash_count = 0;
                let mut ptr = check_pos;
                
                // libinjection_sqli.c:600-604: Count consecutive backslashes backwards
                loop {
                    if ptr < start_pos_for_escape || self.input[ptr] != b'\\' {
                        break;
                    }
                    backslash_count += 1;
                    if ptr == 0 {
                        break;
                    }
                    ptr -= 1;
                }
                
                // libinjection_sqli.c:605-606: If odd number of backslashes, it's escaped
                if backslash_count & 1 == 1 {
                    // libinjection_sqli.c:656-659: Continue search from qpos + 1
                    qpos_idx = None;
                    for i in (qpos + 1)..len {
                        if self.input[i] == quote_char {
                            qpos_idx = Some(i);
                            break;
                        }
                    }
                    continue;
                }
            }
            
            // Check double delimiter escape - libinjection_sqli.c:660
            // C: is_double_delim_escaped(qpos, cs + len)
            // Implements libinjection_sqli.c:610-612: ((cur + 1) < end) && *(cur + 1) == *cur
            if qpos + 1 < len && self.input[qpos + 1] == quote_char {
                // libinjection_sqli.c:661-664: Continue search from qpos + 2
                qpos_idx = None;
                for i in (qpos + 2)..len {
                    if self.input[i] == quote_char {
                        qpos_idx = Some(i);
                        break;
                    }
                }
                continue;
            }
            
            // Found unescaped quote - libinjection_sqli.c:666-670
            // C: st_assign(st, TYPE_STRING, pos + offset, qpos - (cs + pos + offset), cs + pos + offset)
            let content_start = pos + offset;
            let content_len = qpos - content_start;
            let content = &self.input[content_start..(content_start + content_len)];
            
            self.current.assign(TYPE_STRING, content_start, content_len, content);
            self.current.str_close = quote_char; // libinjection_sqli.c:669
            
            // libinjection_sqli.c:670: return (size_t)(qpos - cs + 1)
            self.pos = qpos + 1;
            return Some(self.current.clone());
        }
        
        // No closing quote found - libinjection_sqli.c:646-654
        // C: st_assign(st, TYPE_STRING, pos + offset, len - pos - offset, cs + pos + offset)
        let content = &self.input[start_pos..];
        self.current.assign(TYPE_STRING, start_pos, self.input.len() - start_pos, content);
        self.current.str_close = CHAR_NULL; // libinjection_sqli.c:653
        self.pos = self.input.len(); // libinjection_sqli.c:654: return len
        
        Some(self.current.clone())
    }
    
    // Character dispatch function - matches char_parse_map in C
    fn dispatch_char_parser(&mut self, ch: u8) -> usize {
        use crate::sqli::sqli_data::{get_char_type, CharType};
        
        // Use the generated lookup table - same as C implementation
        match get_char_type(ch) {
            CharType::White => self.parse_white(),
            CharType::Bang => self.parse_operator2(),
            CharType::String => self.parse_string(),
            CharType::Hash => self.parse_hash(),
            CharType::Money => self.parse_money(),
            CharType::Op1 | CharType::Unary => self.parse_operator1(),
            CharType::Op2 => self.parse_operator2(),
            CharType::LeftParens | CharType::RightParens | CharType::Comma | 
            CharType::Semicolon | CharType::LeftBrace | CharType::RightBrace => self.parse_char(),
            CharType::Dash => self.parse_dash(),
            CharType::Number => self.parse_number(),
            CharType::Slash => self.parse_slash(),
            CharType::Variable => self.parse_var(),
            CharType::Word => self.parse_word(),     // This now handles UTF-8 bytes 128-255!
            CharType::BString => self.parse_bstring(),
            CharType::EString => self.parse_estring(),
            CharType::NQString => self.parse_nqstring(),
            CharType::QString => self.parse_qstring(),
            CharType::UString => self.parse_ustring(),
            CharType::XString => self.parse_xstring(),
            CharType::BWord => self.parse_bword(),
            CharType::Backslash => self.parse_backslash(),
            CharType::Tick => self.parse_tick(),
            CharType::Other => self.parse_other(),
        }
    }
    
    // Parser implementations matching C version exactly
    
    fn parse_white(&mut self) -> usize {
        self.pos + 1
    }
    
    fn parse_operator1(&mut self) -> usize {
        let ch = self.input[self.pos];
        self.current.assign_char(TYPE_OPERATOR, self.pos, ch);
        self.pos + 1
    }
    
    fn parse_operator2(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // Boundary check - if not enough characters for 2-char operator, fallback
        if pos + 1 >= slen {
            return self.parse_operator1();
        }
        
        // First check for 3-character operator <=> (special case in C)
        if pos + 2 < slen && 
           self.input[pos] == b'<' && self.input[pos + 1] == b'=' && self.input[pos + 2] == b'>' {
            let op = [b'<', b'=', b'>'];
            self.current.assign(TYPE_OPERATOR, pos, 3, &op);
            return pos + 3;
        }
        
        // Try 2-character operator lookup using the comprehensive table
        // Only attempt lookup if bytes form valid UTF-8, since operators are ASCII-only
        let two_char = &self.input[pos..pos + 2];
        if let Ok(two_char_str) = std::str::from_utf8(two_char) {
            let token_type = self.lookup_word(two_char_str);
            if token_type != TokenType::None && token_type != TokenType::Bareword {
                // Found a 2-character operator in the lookup table
                let type_byte = match token_type {
                    TokenType::Operator => TYPE_OPERATOR,
                    TokenType::LogicOperator => TYPE_LOGIC_OPERATOR,
                    _ => TYPE_OPERATOR, // Default fallback
                };
                self.current.assign(type_byte, pos, 2, two_char);
                return pos + 2;
            }
        }
        
        // No 2-character operator found, check for special single character cases
        let ch = self.input[pos];
        if ch == b':' {
            // Special case: ':' is not an operator, it's TYPE_COLON
            self.current.assign_char(TYPE_COLON, pos, ch);
            return pos + 1;
        } else {
            // Must be a single char operator - delegate to parse_operator1
            return self.parse_operator1();
        }
    }
    
    fn parse_other(&mut self) -> usize {
        let ch = self.input[self.pos];
        self.current.assign_char(TYPE_UNKNOWN, self.pos, ch);
        self.pos + 1
    }
    
    fn parse_char(&mut self) -> usize {
        let ch = self.input[self.pos];
        self.current.assign_char(ch, self.pos, ch);
        self.pos + 1
    }
    
    fn parse_hash(&mut self) -> usize {
        self.stats_comment_hash += 1;
        if self.flags.is_mysql() {
            // C version has a bug that increments stats_comment_hash twice in MySQL mode
            // We need to match this behavior exactly
            self.stats_comment_hash += 1;
            self.parse_eol_comment()
        } else {
            self.current.assign_char(TYPE_OPERATOR, self.pos, b'#');
            self.pos + 1
        }
    }
    
    fn parse_dash(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        if pos + 1 < slen && self.input[pos + 1] == b'-' {
            if pos + 2 >= slen || self.is_white_char(self.input[pos + 2]) {
                // "--" followed by whitespace or end: SQL comment
                self.stats_comment_ddw += 1;
                return self.parse_eol_comment();
            } else {
                // "--" followed by non-whitespace: depends on SQL mode
                self.stats_comment_ddx += 1;
                if self.flags.is_ansi() {
                    return self.parse_eol_comment();
                } else {
                    // MySQL treats as two unary operators
                    self.current.assign_char(TYPE_OPERATOR, pos, b'-');
                    return pos + 1;
                }
            }
        } else {
            // Single dash: operator
            self.current.assign_char(TYPE_OPERATOR, pos, b'-');
            pos + 1
        }
    }
    
    fn parse_slash(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // Match C logic exactly: if (pos1 == slen || cs[pos1] != '*')
        if pos + 1 == slen || self.input[pos + 1] != b'*' {
            // Regular operator
            self.current.assign_char(TYPE_OPERATOR, pos, b'/');
            pos + 1
        } else {
            // C-style comment /* ... */
            self.stats_comment_c += 1;
            self.parse_c_comment()
        }
    }
    
    fn parse_backslash(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // Weird MySQL alias for NULL, "\N" (capital N only)
        if pos + 1 < slen && self.input[pos + 1] == b'N' {
            let content = &self.input[pos..pos + 2];
            self.current.assign(TYPE_NUMBER, pos, 2, content);
            pos + 2
        } else {
            let ch = self.input[pos];
            self.current.assign_char(TYPE_BACKSLASH, pos, ch);
            pos + 1
        }
    }
    
    fn parse_eol_comment(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // Find end of line or end of string
        let mut end_pos = pos;
        while end_pos < slen && self.input[end_pos] != b'\n' {
            end_pos += 1;
        }
        
        let comment_slice = &self.input[pos..end_pos];
        self.current.assign(TYPE_COMMENT, pos, end_pos - pos, comment_slice);
        
        if end_pos < slen {
            end_pos + 1 // Skip the newline
        } else {
            slen
        }
    }
    
    // Helper function equivalent to C memchr2: finds two consecutive characters
    fn memchr2(&self, haystack: &[u8], c0: u8, c1: u8) -> Option<usize> {
        if haystack.len() < 2 {
            return None;
        }
        
        for i in 0..haystack.len() - 1 {
            if haystack[i] == c0 && haystack[i + 1] == c1 {
                return Some(i);
            }
        }
        None
    }
    
    // Helper function equivalent to C is_mysql_comment
    fn is_mysql_comment(&self, pos: usize) -> bool {
        let slen = self.input.len();
        
        // Need at least 3 chars: /*!
        if pos + 2 >= slen {
            return false;
        }
        
        // Check if it's /*!
        self.input[pos] == b'/' && 
        self.input[pos + 1] == b'*' && 
        self.input[pos + 2] == b'!'
    }
    
    fn parse_c_comment(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // This implements the exact same logic as C parse_slash function
        
        // Search for */ in the part after /*
        let search_slice = if pos + 2 < slen {
            &self.input[pos + 2..]
        } else {
            &[]
        };
        
        let clen = if let Some(close_pos) = self.memchr2(search_slice, b'*', b'/') {
            // Found */: include everything from /* to */
            (pos + 2) + close_pos + 2 - pos  // +2 to include the */
        } else {
            // No */ found: include everything to end of string
            slen - pos
        };
        
        let mut ctype = TYPE_COMMENT;
        
        // Check for nested comments - matching C logic exactly, including the off-by-one bug
        // C code: memchr2(cur + 2, (size_t)(ptr - (cur + 1)), '/', '*')
        // where ptr points to the '*' in the closing '*/'
        if let Some(close_pos) = self.memchr2(search_slice, b'*', b'/') {
            // In C terms:
            // - cur = pos (start of /*)
            // - ptr = pos + 2 + close_pos (points to * in */)
            // - Search start: cur + 2 = pos + 2 
            // - Search length: ptr - (cur + 1) = (pos + 2 + close_pos) - (pos + 1) = close_pos + 1
            // The bug: should be ptr - (cur + 2) = close_pos, but it's close_pos + 1
            
            // So we search 1 byte more than we should
            let buggy_search_length = close_pos + 1;
            
            // Make sure we don't go out of bounds
            let actual_search_length = buggy_search_length.min(search_slice.len());
            
            if actual_search_length > 0 {
                let buggy_search_region = &search_slice[..actual_search_length];
                if self.memchr2(buggy_search_region, b'/', b'*').is_some() {
                    ctype = TYPE_EVIL;
                }
            }
        }
        
        // Check for MySQL conditional comments /*!
        if self.is_mysql_comment(pos) {
            ctype = TYPE_EVIL;
        }
        
        let comment_slice = &self.input[pos..pos + clen];
        self.current.assign(ctype, pos, clen, comment_slice);
        pos + clen
    }
    
    fn parse_string(&mut self) -> usize {
        let pos = self.pos;
        let delim = self.input[pos];
        self.parse_string_core(pos, delim, 1)
    }
    
    fn parse_string_core(&mut self, pos: usize, delim: u8, offset: usize) -> usize {
        let slen = self.input.len();
        let start_pos = pos + offset;
        let mut end_pos = start_pos;
        
        // Look for closing delimiter
        while end_pos < slen {
            if let Some(found_pos) = self.memchr(delim, &self.input[end_pos..]) {
                let actual_pos = end_pos + found_pos;
                
                // Check for escape sequences
                if actual_pos > 0 && self.is_backslash_escaped(actual_pos - 1) {
                    end_pos = actual_pos + 1;
                    continue;
                } else if self.is_double_delim_escaped(actual_pos) {
                    end_pos = actual_pos + 2;
                    continue;
                } else {
                    // Found unescaped closing delimiter
                    let content = &self.input[start_pos..actual_pos];
                    self.current.assign(TYPE_STRING, start_pos, actual_pos - start_pos, content);
                    self.current.str_open = delim;
                    self.current.str_close = delim;
                    return actual_pos + 1;
                }
            } else {
                // No closing delimiter found
                let content = &self.input[start_pos..];
                self.current.assign(TYPE_STRING, start_pos, slen - start_pos, content);
                self.current.str_open = delim;
                self.current.str_close = CHAR_NULL;
                return slen;
            }
        }
        
        // Handle unterminated string at end of input (like C does)
        let content = &self.input[start_pos..];
        self.current.assign(TYPE_STRING, start_pos, slen - start_pos, content);
        self.current.str_open = delim;
        self.current.str_close = CHAR_NULL;
        
        slen
    }
    
    fn parse_estring(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        if pos + 2 >= slen || self.input[pos + 1] != CHAR_SINGLE {
            return self.parse_word();
        }
        
        self.parse_string_core(pos, CHAR_SINGLE, 2)
    }
    
    fn parse_ustring(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        if pos + 2 < slen && self.input[pos + 1] == b'&' && self.input[pos + 2] == b'\'' {
            let _old_pos = self.pos;
            self.pos += 2;
            let result = self.parse_string();
            self.current.str_open = b'u';
            if self.current.str_close == b'\'' {
                self.current.str_close = b'u';
            }
            result
        } else {
            self.parse_word()
        }
    }
    
    fn parse_qstring(&mut self) -> usize {
        self.parse_qstring_core(0)
    }
    
    fn parse_nqstring(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        if pos + 2 < slen && self.input[pos + 1] == CHAR_SINGLE {
            return self.parse_estring();
        }
        
        self.parse_qstring_core(1)
    }
    
    fn parse_qstring_core(&mut self, offset: usize) -> usize {
        let pos = self.pos + offset;
        let slen = self.input.len();
        
        // Oracle Q-string: Q'<delimiter>content<delimiter>'
        if pos >= slen || (self.input[pos] != b'q' && self.input[pos] != b'Q') ||
           pos + 2 >= slen || self.input[pos + 1] != b'\'' {
            return self.parse_word();
        }
        
        let start_delim = self.input[pos + 2];
        // Match C logic exactly: libinjection_sqli.c:747-748
        // C uses signed char, so bytes 128-255 become negative values
        // The condition "ch < 33" catches both low ASCII and high bytes (128-255)
        let start_delim_signed = start_delim as i8;
        if start_delim_signed < 33 {
            return self.parse_word();
        }
        
        // Map opening to closing delimiter
        let end_delim = match start_delim {
            b'(' => b')',
            b'[' => b']',
            b'{' => b'}',
            b'<' => b'>',
            _ => start_delim,
        };
        
        // Find ending pattern
        let content_start = pos + 3;
        if let Some(end_pos) = self.find_qstring_end(content_start, end_delim) {
            let content = &self.input[content_start..end_pos];
            self.current.assign(TYPE_STRING, content_start, end_pos - content_start, content);
            self.current.str_open = b'q';
            self.current.str_close = b'q';
            end_pos + 2
        } else {
            let content = &self.input[content_start..];
            self.current.assign(TYPE_STRING, content_start, slen - content_start, content);
            self.current.str_open = b'q';
            self.current.str_close = CHAR_NULL;
            slen
        }
    }
    
    fn parse_bstring(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // Binary string: B'01011'
        if pos + 2 >= slen || self.input[pos + 1] != b'\'' {
            return self.parse_word();
        }
        
        let content_start = pos + 2;
        let mut content_end = content_start;
        
        // Only allow 0 and 1
        while content_end < slen && (self.input[content_end] == b'0' || self.input[content_end] == b'1') {
            content_end += 1;
        }
        
        if content_end >= slen || self.input[content_end] != b'\'' {
            return self.parse_word();
        }
        
        let full_token = &self.input[pos..content_end + 1];
        self.current.assign(TYPE_NUMBER, pos, content_end + 1 - pos, full_token);
        content_end + 1
    }
    
    fn parse_xstring(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // Hex string: X'deadbeef'
        if pos + 2 >= slen || self.input[pos + 1] != b'\'' {
            return self.parse_word();
        }
        
        let content_start = pos + 2;
        let mut content_end = content_start;
        
        // Only allow hex digits
        while content_end < slen {
            match self.input[content_end] {
                b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f' => content_end += 1,
                _ => break,
            }
        }
        
        if content_end >= slen || self.input[content_end] != b'\'' {
            return self.parse_word();
        }
        
        let full_token = &self.input[pos..content_end + 1];
        self.current.assign(TYPE_NUMBER, pos, content_end + 1 - pos, full_token);
        content_end + 1
    }
    
    fn parse_bword(&mut self) -> usize {
        let pos = self.pos;
        
        // SQL Server bracket words: [column name]
        if let Some(end_pos) = self.memchr(b']', &self.input[pos..]) {
            let actual_end = pos + end_pos;
            let content = &self.input[pos..=actual_end];
            self.current.assign(TYPE_BAREWORD, pos, content.len(), content);
            actual_end + 1
        } else {
            let content = &self.input[pos..];
            self.current.assign(TYPE_BAREWORD, pos, content.len(), content);
            self.input.len()
        }
    }
    
    fn parse_word(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // Find word boundary - matches C version's strlencspn character set
        let word_chars = b" []{}<>:\\?=@!#~+-*/&|^%(),';	\n\x0B\x0C\r\"\xA0\x00";
        let mut end_pos = pos;
        
        while end_pos < slen && !word_chars.contains(&self.input[end_pos]) {
            end_pos += 1;
        }
        
        let word_len = end_pos - pos;
        let word_slice = &self.input[pos..end_pos];
        
        self.current.assign(TYPE_BAREWORD, pos, word_len, word_slice);
        
        // Check for special delimiters within word
        for (i, &byte) in word_slice.iter().enumerate() {
            if byte == b'.' || byte == b'`' {
                // For delimiter detection, we only need to check if the first i bytes
                // form a valid keyword. Since libinjection keywords are ASCII-only,
                // if the slice contains any non-UTF8 bytes (like 0xFF), it can't be 
                // a keyword anyway, so we can safely skip the lookup.
                if let Ok(partial_word) = std::str::from_utf8(&word_slice[..i]) {
                    let token_type = self.lookup_word(partial_word);
                    if token_type != TokenType::None && token_type != TokenType::Bareword {
                        self.current.clear();
                        let type_byte = token_type_to_byte(token_type);
                        self.current.assign(type_byte, pos, i, &word_slice[..i]);
                        return pos + i;
                    }
                }
                // If UTF-8 conversion fails, continue - can't be a keyword
            }
        }
        
        // Do full word lookup - only for valid UTF-8 sequences since keywords are ASCII-only
        if word_len < LIBINJECTION_SQLI_TOKEN_SIZE {
            if let Ok(word_str) = std::str::from_utf8(word_slice) {
                let token_type = self.lookup_word(word_str);
                if token_type != TokenType::None {
                    self.current.token_type = token_type;
                }
            }
            // If UTF-8 conversion fails, leave as BAREWORD - bytes with 0xFF can't be keywords
        }
        
        end_pos
    }
    
    fn parse_tick(&mut self) -> usize {
        // MySQL backticks
        let pos = self.parse_string_core(self.pos, CHAR_TICK, 1);
        
        // Check if backtick content is a keyword/function - only for valid UTF-8
        if let Ok(word_str) = std::str::from_utf8(&self.current.val[..self.current.len]) {
            let token_type = self.lookup_word(word_str);
            if token_type == TokenType::Function {
                self.current.token_type = TokenType::Function;
            } else {
                self.current.token_type = TokenType::Bareword;
            }
        } else {
            // If UTF-8 conversion fails, default to bareword - 0xFF bytes can't be keywords
            self.current.token_type = TokenType::Bareword;
        }
        
        pos
    }
    
    fn parse_var(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        let mut new_pos = pos + 1;
        
        // Count @ symbols
        let mut at_count = 1;
        if new_pos < slen && self.input[new_pos] == b'@' {
            new_pos += 1;
            at_count = 2;
        }
        
        self.current.count = at_count;
        
        // Handle special cases like @@`version`
        if new_pos < slen {
            if self.input[new_pos] == b'`' {
                self.pos = new_pos;
                let result = self.parse_tick();
                
                // Store only the backtick content (without @ symbols) like C
                // The backtick content is already parsed correctly by parse_tick
                self.current.token_type = TokenType::Variable;
                return result;
            } else if self.input[new_pos] == CHAR_SINGLE || self.input[new_pos] == CHAR_DOUBLE {
                self.pos = new_pos;
                let result = self.parse_string();
                
                // Store only the string content (without @ symbols) like C
                // The string content is already parsed correctly by parse_string
                self.current.token_type = TokenType::Variable;
                return result;
            }
        }
        
        // Regular variable name - must exactly match C implementation
        // C: " <>:\\?=@!#~+-*/&|^%(),';\t\n\v\f\r'`\""
        let var_chars = b" <>:\\?=@!#~+-*/&|^%(),;'\t\n\x0B\x0C\r'`\"";
        let mut end_pos = new_pos;
        
        while end_pos < slen && !var_chars.contains(&self.input[end_pos]) {
            end_pos += 1;
        }
        
        if end_pos == new_pos {
            // Empty variable name (just @ or @@ symbols)
            // Store the @ symbols like C implementation 
            let var_slice = &self.input[self.pos..new_pos]; // Include @ symbols
            self.current.assign(TYPE_VARIABLE, self.pos, new_pos - self.pos, var_slice);
            new_pos
        } else {
            // Non-empty variable - store the @ symbols + name like C
            let var_slice = &self.input[self.pos..end_pos]; // Include @ symbols
            self.current.assign(TYPE_VARIABLE, self.pos, end_pos - self.pos, var_slice);
            end_pos
        }
    }
    
    fn parse_money(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        if pos + 1 == slen {
            self.current.assign_char(TYPE_BAREWORD, pos, b'$');
            return slen;
        }
        
        let next_char = self.input[pos + 1];
        
        // Check for $1,000.00 format
        let money_chars = b"0123456789.,";
        let mut end_pos = pos + 1;
        
        while end_pos < slen && money_chars.contains(&self.input[end_pos]) {
            end_pos += 1;
        }
        
        if end_pos > pos + 1 {
            // Check for special case: $. should be parsed as word
            if end_pos == pos + 2 && self.input[pos + 1] == b'.' {
                return self.parse_word();
            }
            // Found numeric content
            let money_slice = &self.input[pos..end_pos];
            self.current.assign(TYPE_NUMBER, pos, end_pos - pos, money_slice);
            return end_pos;
        }
        
        // Check for PostgreSQL $$ strings
        if next_char == b'$' {
            return self.parse_dollar_string();
        }
        
        // Check for PostgreSQL $tag$ strings
        let tag_chars = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        let mut tag_end = pos + 1;
        
        while tag_end < slen && tag_chars.contains(&self.input[tag_end]) {
            tag_end += 1;
        }
        
        if tag_end == pos + 1 {
            // Just $ followed by non-alphanumeric
            self.current.assign_char(TYPE_BAREWORD, pos, b'$');
            pos + 1
        } else if tag_end < slen && self.input[tag_end] == b'$' {
            // Found $tag$ pattern
            self.parse_tagged_dollar_string(tag_end)
        } else {
            // $ followed by letters but no closing $
            self.current.assign_char(TYPE_BAREWORD, pos, b'$');
            pos + 1
        }
    }
    
    fn parse_number(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        let mut end_pos = pos;
        let mut have_e = false;
        let mut have_exp = false;
        
        // Handle special prefixes 0x, 0X, 0b, 0B
        if end_pos < slen && self.input[end_pos] == b'0' && end_pos + 1 < slen {
            match self.input[end_pos + 1] {
                b'X' | b'x' => {
                    end_pos += 2;
                    while end_pos < slen {
                        match self.input[end_pos] {
                            b'0'..=b'9' | b'A'..=b'F' | b'a'..=b'f' => end_pos += 1,
                            _ => break,
                        }
                    }
                    
                    if end_pos == pos + 2 {
                        // No hex digits after 0x
                        let token = &self.input[pos..pos + 2];
                        self.current.assign(TYPE_BAREWORD, pos, 2, token);
                        return pos + 2;
                    } else {
                        let token = &self.input[pos..end_pos];
                        self.current.assign(TYPE_NUMBER, pos, end_pos - pos, token);
                        return end_pos;
                    }
                }
                b'B' | b'b' => {
                    end_pos += 2;
                    while end_pos < slen && (self.input[end_pos] == b'0' || self.input[end_pos] == b'1') {
                        end_pos += 1;
                    }
                    
                    if end_pos == pos + 2 {
                        // No binary digits after 0b
                        let token = &self.input[pos..pos + 2];
                        self.current.assign(TYPE_BAREWORD, pos, 2, token);
                        return pos + 2;
                    } else {
                        let token = &self.input[pos..end_pos];
                        self.current.assign(TYPE_NUMBER, pos, end_pos - pos, token);
                        return end_pos;
                    }
                }
                _ => {} // Continue with normal number parsing
            }
        }
        
        let start_pos = end_pos;
        
        // Parse integer part
        while end_pos < slen && self.input[end_pos].is_ascii_digit() {
            end_pos += 1;
        }
        
        // Parse decimal part
        if end_pos < slen && self.input[end_pos] == b'.' {
            end_pos += 1;
            while end_pos < slen && self.input[end_pos].is_ascii_digit() {
                end_pos += 1;
            }
            
            if end_pos - start_pos == 1 {
                // Only read '.', this is a dot token
                self.current.assign_char(TYPE_DOT, start_pos, b'.');
                return end_pos;
            }
        }
        
        // Parse exponent
        if end_pos < slen && (self.input[end_pos] == b'E' || self.input[end_pos] == b'e') {
            have_e = true;
            end_pos += 1;
            
            if end_pos < slen && (self.input[end_pos] == b'+' || self.input[end_pos] == b'-') {
                end_pos += 1;
            }
            
            while end_pos < slen && self.input[end_pos].is_ascii_digit() {
                have_exp = true;
                end_pos += 1;
            }
        }
        
        // Oracle float/double suffix
        if end_pos < slen {
            match self.input[end_pos] {
                b'd' | b'D' | b'f' | b'F' => {
                    if end_pos + 1 == slen {
                        end_pos += 1;
                    } else if self.is_white_char(self.input[end_pos + 1]) || self.input[end_pos + 1] == b';' {
                        end_pos += 1;
                    } else if end_pos + 1 < slen && (self.input[end_pos + 1] == b'u' || self.input[end_pos + 1] == b'U') {
                        // Handle "1fUNION" -> "1f" "UNION"
                        end_pos += 1;
                    }
                }
                _ => {}
            }
        }
        
        // Check for invalid exponential format
        if have_e && !have_exp {
            let token = &self.input[start_pos..end_pos];
            self.current.assign(TYPE_BAREWORD, start_pos, end_pos - start_pos, token);
        } else {
            let token = &self.input[start_pos..end_pos];
            self.current.assign(TYPE_NUMBER, start_pos, end_pos - start_pos, token);
        }
        
        end_pos
    }
    
    // Helper functions
    
    fn is_white_char(&self, ch: u8) -> bool {
        // Match C implementation exactly - libinjection_sqli.c lines 194-205 (char_is_white function)
        // C code: return strchr(" \t\n\v\f\r\240\000", ch) != NULL;
        // C comment:
        // /* ' '  space is 0x32
        //    '\t  0x09 \011 horizontal tab
        //    '\n' 0x0a \012 new line
        //    '\v' 0x0b \013 vertical tab
        //    '\f' 0x0c \014 new page
        //    '\r' 0x0d \015 carriage return
        //         0x00 \000 null (oracle)
        //         0xa0 \240 is Latin-1
        // */
        matches!(ch, b' ' | b'\t' | b'\n' | 0x0B | 0x0C | b'\r' | 0xA0 | 0x00)
    }
    
    fn memchr(&self, needle: u8, haystack: &[u8]) -> Option<usize> {
        haystack.iter().position(|&x| x == needle)
    }
    
    fn is_backslash_escaped(&self, pos: usize) -> bool {
        let mut backslash_count = 0;
        let mut current_pos = pos;
        
        // Count consecutive backslashes backwards from pos (including pos itself)
        while current_pos < self.input.len() && self.input[current_pos] == b'\\' {
            backslash_count += 1;
            if current_pos == 0 {
                break;
            }
            current_pos -= 1;
        }
        
        // If odd number of backslashes, the character after them is escaped
        backslash_count & 1 == 1
    }
    
    fn is_double_delim_escaped(&self, pos: usize) -> bool {
        pos + 1 < self.input.len() && self.input[pos] == self.input[pos + 1]
    }
    
    fn find_qstring_end(&self, start: usize, end_delim: u8) -> Option<usize> {
        let mut pos = start;
        
        while pos + 1 < self.input.len() {
            if self.input[pos] == end_delim && self.input[pos + 1] == b'\'' {
                return Some(pos);
            }
            pos += 1;
        }
        
        None
    }
    
    fn parse_dollar_string(&mut self) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        
        // $$ string $$
        let content_start = pos + 2;
        
        // Find ending $$
        let mut end_pos = content_start;
        while end_pos + 1 < slen {
            if self.input[end_pos] == b'$' && self.input[end_pos + 1] == b'$' {
                let content = &self.input[content_start..end_pos];
                self.current.assign(TYPE_STRING, content_start, end_pos - content_start, content);
                self.current.str_open = b'$';
                self.current.str_close = b'$';
                return end_pos + 2;
            }
            end_pos += 1;
        }
        
        // No closing $$ found
        let content = &self.input[content_start..];
        self.current.assign(TYPE_STRING, content_start, slen - content_start, content);
        self.current.str_open = b'$';
        self.current.str_close = CHAR_NULL;
        slen
    }
    
    fn parse_tagged_dollar_string(&mut self, tag_end: usize) -> usize {
        let pos = self.pos;
        let slen = self.input.len();
        let tag = &self.input[pos..=tag_end];
        let content_start = tag_end + 1;
        
        // Find matching end tag
        let mut search_pos = content_start;
        while search_pos + tag.len() <= slen {
            if &self.input[search_pos..search_pos + tag.len()] == tag {
                let content = &self.input[content_start..search_pos];
                self.current.assign(TYPE_STRING, content_start, search_pos - content_start, content);
                self.current.str_open = b'$';
                self.current.str_close = b'$';
                return search_pos + tag.len();
            }
            search_pos += 1;
        }
        
        // No matching end tag
        let content = &self.input[content_start..];
        self.current.assign(TYPE_STRING, content_start, slen - content_start, content);
        self.current.str_open = b'$';
        self.current.str_close = CHAR_NULL;
        slen
    }
}