psi_detector 0.1.3

Protocol detection and upgrade framework inspired by Yuri's PSI Detector
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
//! x86_64 SIMD优化实现
//!
//! 利用SSE2、SSE4.1、AVX2等指令集加速协议探测。

#[cfg(target_arch = "x86_64")]
use std::arch::x86_64::*;
use crate::core::protocol::ProtocolType;
use crate::error::{DetectorError, Result};
use crate::simd::{SimdDetectionResult, SimdDetector, SimdInstructionSet};
use std::time::Instant;

/// 检测x86_64特性
fn detect_x86_features() -> (bool, bool, bool, bool) {
    let has_sse2 = is_x86_feature_detected!("sse2");
    let has_sse41 = is_x86_feature_detected!("sse4.1");
    let has_avx2 = is_x86_feature_detected!("avx2");
    let has_avx512 = is_x86_feature_detected!("avx512f");
    
    (has_sse2, has_sse41, has_avx2, has_avx512)
}

/// AVX512 SIMD探测器
pub struct Avx512Detector {
    instruction_set: SimdInstructionSet,
}

impl Avx512Detector {
    /// 创建新的AVX512 SIMD探测器
    pub fn new() -> Self {
        Self {
            instruction_set: SimdInstructionSet::AVX512,
        }
    }
}

impl SimdDetector for Avx512Detector {
    fn detect_http2(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        // 使用GenericSimdDetector作为基础实现
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_http2(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_quic(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_quic(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_grpc(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_grpc(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_websocket(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_websocket(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_tls(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_tls(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_multiple(&self, data: &[u8], protocols: &[ProtocolType]) -> Result<Vec<SimdDetectionResult>> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let results = detector.detect_multiple(data, protocols)?;
        
        Ok(results.into_iter().map(|result| {
            SimdDetectionResult {
                protocol: result.protocol,
                confidence: result.confidence,
                match_positions: result.match_positions,
                instruction_set: self.instruction_set,
            }
        }).collect())
    }
    
    fn instruction_set(&self) -> SimdInstructionSet {
        self.instruction_set
    }
    
    fn supports_protocol(&self, _protocol: ProtocolType) -> bool {
        true
    }
}

/// AVX2 SIMD探测器
pub struct Avx2Detector {
    instruction_set: SimdInstructionSet,
}

impl Avx2Detector {
    /// 创建新的AVX2 SIMD探测器
    pub fn new() -> Self {
        Self {
            instruction_set: SimdInstructionSet::AVX2,
        }
    }
}

impl SimdDetector for Avx2Detector {
    fn detect_http2(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_http2(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_quic(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_quic(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_grpc(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_grpc(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_websocket(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_websocket(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_tls(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_tls(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_multiple(&self, data: &[u8], protocols: &[ProtocolType]) -> Result<Vec<SimdDetectionResult>> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let results = detector.detect_multiple(data, protocols)?;
        
        Ok(results.into_iter().map(|result| {
            SimdDetectionResult {
                protocol: result.protocol,
                confidence: result.confidence,
                match_positions: result.match_positions,
                instruction_set: self.instruction_set,
            }
        }).collect())
    }
    
    fn instruction_set(&self) -> SimdInstructionSet {
        self.instruction_set
    }
    
    fn supports_protocol(&self, _protocol: ProtocolType) -> bool {
        true
    }
}

/// SSE2 SIMD探测器
pub struct Sse2Detector {
    instruction_set: SimdInstructionSet,
}

impl Sse2Detector {
    /// 创建新的SSE2 SIMD探测器
    pub fn new() -> Self {
        Self {
            instruction_set: SimdInstructionSet::SSE2,
        }
    }
}

impl SimdDetector for Sse2Detector {
    fn detect_http2(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_http2(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_quic(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_quic(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_grpc(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_grpc(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_websocket(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_websocket(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_tls(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let result = detector.detect_tls(data)?;
        
        Ok(SimdDetectionResult {
            protocol: result.protocol,
            confidence: result.confidence,
            match_positions: result.match_positions,
            instruction_set: self.instruction_set,
        })
    }
    
    fn detect_multiple(&self, data: &[u8], protocols: &[ProtocolType]) -> Result<Vec<SimdDetectionResult>> {
        let detector = crate::simd::detector::GenericSimdDetector::new();
        let results = detector.detect_multiple(data, protocols)?;
        
        Ok(results.into_iter().map(|result| {
            SimdDetectionResult {
                protocol: result.protocol,
                confidence: result.confidence,
                match_positions: result.match_positions,
                instruction_set: self.instruction_set,
            }
        }).collect())
    }
    
    fn instruction_set(&self) -> SimdInstructionSet {
        self.instruction_set
    }
    
    fn supports_protocol(&self, _protocol: ProtocolType) -> bool {
        true
    }
}

/// x86_64 SIMD探测器
pub struct X86_64SimdDetector {
    instruction_set: SimdInstructionSet,
    has_sse2: bool,
    has_sse41: bool,
    has_avx2: bool,
}

impl X86_64SimdDetector {
    /// 创建新的x86_64 SIMD探测器
    pub fn new() -> Self {
        let (has_sse2, has_sse41, has_avx2, has_avx512) = detect_x86_features();
        
        let instruction_set = if has_avx512 {
            SimdInstructionSet::AVX512
        } else if has_avx2 {
            SimdInstructionSet::AVX2
        } else if has_sse41 {
            SimdInstructionSet::SSE41
        } else if has_sse2 {
            SimdInstructionSet::SSE2
        } else {
            SimdInstructionSet::None
        };
        
        Self {
            instruction_set,
            has_sse2,
            has_sse41,
            has_avx2,
        }
    }
    
    /// 使用AVX2进行快速模式匹配
    #[cfg(target_arch = "x86_64")]
    unsafe fn avx2_pattern_match(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
        if !self.has_avx2 || needle.is_empty() || haystack.len() < needle.len() {
            return None;
        }
        
        if needle.len() == 1 {
            return self.avx2_find_byte(haystack, needle[0]);
        }
        
        // 对于较长的模式,使用滑动窗口
        let first_byte = needle[0];
        let mut pos = 0;
        let max_iterations = haystack.len() * 2; // 防止无限循环
        let mut iteration_count = 0;
        
        while pos <= haystack.len() - needle.len() && iteration_count < max_iterations {
            iteration_count += 1;
            
            if let Some(candidate) = self.avx2_find_byte(&haystack[pos..], first_byte) {
                let actual_pos = pos + candidate;
                if actual_pos + needle.len() <= haystack.len() {
                    if haystack[actual_pos..actual_pos + needle.len()] == *needle {
                        return Some(actual_pos);
                    }
                }
                // 确保位置总是向前推进
                pos = actual_pos + 1;
                if pos <= actual_pos {
                    pos = actual_pos + 1;
                }
            } else {
                break;
            }
        }
        
        None
    }
    
    /// 使用AVX2查找单个字节
    #[cfg(target_arch = "x86_64")]
    unsafe fn avx2_find_byte(&self, data: &[u8], byte: u8) -> Option<usize> {
        if !self.has_avx2 || data.is_empty() {
            return None;
        }
        
        let needle = _mm256_set1_epi8(byte as i8);
        let mut pos = 0;
        
        // 处理32字节对齐的块
        while pos + 32 <= data.len() {
            let chunk = _mm256_loadu_si256(data.as_ptr().add(pos) as *const __m256i);
            let cmp = _mm256_cmpeq_epi8(chunk, needle);
            let mask = _mm256_movemask_epi8(cmp) as u32;
            
            if mask != 0 {
                return Some(pos + mask.trailing_zeros() as usize);
            }
            
            pos += 32;
        }
        
        // 处理剩余字节
        for i in pos..data.len() {
            if data[i] == byte {
                return Some(i);
            }
        }
        
        None
    }
    
    /// 使用SSE4.1进行模式匹配
    #[cfg(target_arch = "x86_64")]
    unsafe fn sse41_pattern_match(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
        if !self.has_sse41 || needle.is_empty() || haystack.len() < needle.len() {
            return None;
        }
        
        if needle.len() == 1 {
            return self.sse41_find_byte(haystack, needle[0]);
        }
        
        // 对于较长的模式,使用滑动窗口
        let first_byte = needle[0];
        let mut pos = 0;
        let max_iterations = haystack.len() * 2; // 防止无限循环
        let mut iteration_count = 0;
        
        while pos <= haystack.len() - needle.len() && iteration_count < max_iterations {
            iteration_count += 1;
            
            if let Some(candidate) = self.sse41_find_byte(&haystack[pos..], first_byte) {
                let actual_pos = pos + candidate;
                if actual_pos + needle.len() <= haystack.len() {
                    if haystack[actual_pos..actual_pos + needle.len()] == *needle {
                        return Some(actual_pos);
                    }
                }
                // 确保位置总是向前推进
                pos = actual_pos + 1;
                if pos <= actual_pos {
                    pos = actual_pos + 1;
                }
            } else {
                break;
            }
        }
        
        None
    }
    
    /// 使用SSE4.1查找单个字节
    #[cfg(target_arch = "x86_64")]
    unsafe fn sse41_find_byte(&self, data: &[u8], byte: u8) -> Option<usize> {
        if !self.has_sse41 || data.is_empty() {
            return None;
        }
        
        let needle = _mm_set1_epi8(byte as i8);
        let mut pos = 0;
        
        // 处理16字节对齐的块
        while pos + 16 <= data.len() {
            let chunk = _mm_loadu_si128(data.as_ptr().add(pos) as *const __m128i);
            let cmp = _mm_cmpeq_epi8(chunk, needle);
            let mask = _mm_movemask_epi8(cmp) as u16;
            
            if mask != 0 {
                return Some(pos + mask.trailing_zeros() as usize);
            }
            
            pos += 16;
        }
        
        // 处理剩余字节
        for i in pos..data.len() {
            if data[i] == byte {
                return Some(i);
            }
        }
        
        None
    }
    
    /// 快速模式匹配(根据可用指令集选择最佳实现)
    fn fast_pattern_match(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
        #[cfg(target_arch = "x86_64")]
        unsafe {
            if self.has_avx2 {
                return self.avx2_pattern_match(haystack, needle);
            } else if self.has_sse41 {
                return self.sse41_pattern_match(haystack, needle);
            }
        }
        
        // 回退到标准实现
        self.fallback_pattern_match(haystack, needle)
    }
    
    /// 回退模式匹配实现
    fn fallback_pattern_match(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
        if needle.is_empty() || haystack.len() < needle.len() {
            return None;
        }
        
        for i in 0..=haystack.len() - needle.len() {
            if haystack[i..i + needle.len()] == *needle {
                return Some(i);
            }
        }
        
        None
    }
}

/// 使用AVX2指令集计算字节出现次数
#[cfg(target_arch = "x86_64")]
pub unsafe fn avx2_count_bytes(data: &[u8], byte: u8) -> usize {
    if !is_x86_feature_detected!("avx2") || data.is_empty() {
        return data.iter().filter(|&&b| b == byte).count();
    }
    
    let needle = _mm256_set1_epi8(byte as i8);
    let mut count = 0;
    let mut pos = 0;
    
    // 处理32字节对齐的块
    while pos + 32 <= data.len() {
        let chunk = _mm256_loadu_si256(data.as_ptr().add(pos) as *const __m256i);
        let cmp = _mm256_cmpeq_epi8(chunk, needle);
        let mask = _mm256_movemask_epi8(cmp) as u32;
        count += mask.count_ones() as usize;
        pos += 32;
    }
    
    // 处理剩余字节
    for i in pos..data.len() {
        if data[i] == byte {
            count += 1;
        }
    }
    
    count
}

/// 使用SSE2指令集计算字节出现次数
#[cfg(target_arch = "x86_64")]
pub unsafe fn sse2_count_bytes(data: &[u8], byte: u8) -> usize {
    if !is_x86_feature_detected!("sse2") || data.is_empty() {
        return data.iter().filter(|&&b| b == byte).count();
    }
    
    let needle = _mm_set1_epi8(byte as i8);
    let mut count = 0;
    let mut pos = 0;
    
    // 处理16字节对齐的块
    while pos + 16 <= data.len() {
        let chunk = _mm_loadu_si128(data.as_ptr().add(pos) as *const __m128i);
        let cmp = _mm_cmpeq_epi8(chunk, needle);
        let mask = _mm_movemask_epi8(cmp) as u16;
        count += mask.count_ones() as usize;
        pos += 16;
    }
    
    // 处理剩余字节
    for i in pos..data.len() {
        if data[i] == byte {
            count += 1;
        }
    }
    
    count
}

/// 使用AVX2指令集查找字节
#[cfg(target_arch = "x86_64")]
pub unsafe fn avx2_find_byte(data: &[u8], byte: u8) -> Option<usize> {
    if !is_x86_feature_detected!("avx2") || data.is_empty() {
        return data.iter().position(|&b| b == byte);
    }
    
    let needle = _mm256_set1_epi8(byte as i8);
    let mut pos = 0;
    
    // 处理32字节对齐的块
    while pos + 32 <= data.len() {
        let chunk = _mm256_loadu_si256(data.as_ptr().add(pos) as *const __m256i);
        let cmp = _mm256_cmpeq_epi8(chunk, needle);
        let mask = _mm256_movemask_epi8(cmp) as u32;
        
        if mask != 0 {
            return Some(pos + mask.trailing_zeros() as usize);
        }
        
        pos += 32;
    }
    
    // 处理剩余字节
    for i in pos..data.len() {
        if data[i] == byte {
            return Some(i);
        }
    }
    
    None
}

/// 使用SSE2指令集查找字节
#[cfg(target_arch = "x86_64")]
pub unsafe fn sse2_find_byte(data: &[u8], byte: u8) -> Option<usize> {
    if !is_x86_feature_detected!("sse2") || data.is_empty() {
        return data.iter().position(|&b| b == byte);
    }
    
    let needle = _mm_set1_epi8(byte as i8);
    let mut pos = 0;
    
    // 处理16字节对齐的块
    while pos + 16 <= data.len() {
        let chunk = _mm_loadu_si128(data.as_ptr().add(pos) as *const __m128i);
        let cmp = _mm_cmpeq_epi8(chunk, needle);
        let mask = _mm_movemask_epi8(cmp) as u16;
        
        if mask != 0 {
            return Some(pos + mask.trailing_zeros() as usize);
        }
        
        pos += 16;
    }
    
    // 处理剩余字节
    for i in pos..data.len() {
        if data[i] == byte {
            return Some(i);
        }
    }
    
    None
}

impl SimdDetector for X86_64SimdDetector {
    fn detect_http2(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let start = Instant::now();
        
        // HTTP/2 连接前言
        let http2_preface = b"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
        
        if let Some(_) = self.fast_pattern_match(data, http2_preface) {
            return Ok(SimdDetectionResult {
                protocol: ProtocolType::HTTP2,
                confidence: 1.0,
                match_positions: vec![0],
                instruction_set: self.instruction_set,
            });
        }
        
        // 检查HTTP/2帧头
        if data.len() >= 9 {
            let frame_type = data[3];
            
            match frame_type {
                0x0 => { // DATA帧
                    return Ok(SimdDetectionResult {
                        protocol: ProtocolType::HTTP2,
                        confidence: 0.8,
                        match_positions: vec![3],
                        instruction_set: self.instruction_set,
                    });
                }
                0x1 => { // HEADERS帧
                    return Ok(SimdDetectionResult {
                        protocol: ProtocolType::HTTP2,
                        confidence: 0.9,
                        match_positions: vec![3],
                        instruction_set: self.instruction_set,
                    });
                }
                0x4 => { // SETTINGS帧
                    return Ok(SimdDetectionResult {
                        protocol: ProtocolType::HTTP2,
                        confidence: 0.95,
                        match_positions: vec![3],
                        instruction_set: self.instruction_set,
                    });
                }
                _ => {}
            }
        }
        
        Err(DetectorError::detection_failed("No HTTP/2 patterns found"))
    }
    
    fn detect_quic(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        if data.is_empty() {
            return Err(DetectorError::detection_failed("Empty data"));
        }
        
        let first_byte = data[0];
        
        // QUIC长头部格式检查
        if (first_byte & 0x80) != 0 {
            if data.len() >= 5 {
                let version = u32::from_be_bytes([
                    data[1], data[2], data[3], data[4]
                ]);
                
                if version == 0x00000001 {
                    return Ok(SimdDetectionResult {
                        protocol: ProtocolType::QUIC,
                        confidence: 0.95,
                        match_positions: vec![0],
                        instruction_set: self.instruction_set,
                    });
                }
                
                if version == 0x00000000 {
                    return Ok(SimdDetectionResult {
                        protocol: ProtocolType::QUIC,
                        confidence: 0.9,
                        match_positions: vec![0],
                        instruction_set: self.instruction_set,
                    });
                }
            }
        } else {
            return Ok(SimdDetectionResult {
                protocol: ProtocolType::QUIC,
                confidence: 0.7,
                match_positions: vec![0],
                instruction_set: self.instruction_set,
            });
        }
        
        Err(DetectorError::detection_failed("No QUIC patterns found"))
    }
    
    fn detect_grpc(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let grpc_content_type = b"application/grpc";
        
        if let Some(pos) = self.fast_pattern_match(data, grpc_content_type) {
            return Ok(SimdDetectionResult {
                protocol: ProtocolType::GRPC,
                confidence: 0.9,
                match_positions: vec![pos],
                instruction_set: self.instruction_set,
            });
        }
        
        let grpc_web = b"application/grpc-web";
        if let Some(pos) = self.fast_pattern_match(data, grpc_web) {
            return Ok(SimdDetectionResult {
                protocol: ProtocolType::GRPC,
                confidence: 0.85,
                match_positions: vec![pos],
                instruction_set: self.instruction_set,
            });
        }
        
        // 检查gRPC帧格式
        if data.len() >= 5 {
            let compression_flag = data[0];
            if compression_flag <= 1 {
                let message_length = u32::from_be_bytes([
                    data[1], data[2], data[3], data[4]
                ]) as usize;
                
                if message_length > 0 && data.len() >= 5 + message_length {
                    return Ok(SimdDetectionResult {
                        protocol: ProtocolType::GRPC,
                        confidence: 0.7,
                        match_positions: vec![0],
                        instruction_set: self.instruction_set,
                    });
                }
            }
        }
        
        Err(DetectorError::detection_failed("No gRPC patterns found"))
    }
    
    fn detect_websocket(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        let mut positions = Vec::new();
        let mut confidence = 0.0;
        
        // 使用SIMD加速的模式匹配
        let upgrade_header = b"Upgrade: websocket";
        if let Some(pos) = self.fast_pattern_match(data, upgrade_header) {
            positions.push(pos);
            confidence += 0.4;
        }
        
        let connection_header = b"Connection: Upgrade";
        if let Some(pos) = self.fast_pattern_match(data, connection_header) {
            positions.push(pos);
            confidence += 0.3;
        }
        
        let websocket_key = b"Sec-WebSocket-Key:";
        if let Some(pos) = self.fast_pattern_match(data, websocket_key) {
            positions.push(pos);
            confidence += 0.3;
        }
        
        // WebSocket帧格式检查
        if data.len() >= 2 {
            let first_byte = data[0];
            let opcode = first_byte & 0x0F;
            
            if matches!(opcode, 0x0 | 0x1 | 0x2 | 0x8 | 0x9 | 0xA) {
                confidence += 0.2;
                if positions.is_empty() {
                    positions.push(0);
                }
            }
        }
        
        if confidence > 0.5 {
            Ok(SimdDetectionResult {
                protocol: ProtocolType::WebSocket,
                confidence,
                match_positions: positions,
                instruction_set: self.instruction_set,
            })
        } else {
            Err(DetectorError::detection_failed("No WebSocket patterns found"))
        }
    }
    
    fn detect_tls(&self, data: &[u8]) -> Result<SimdDetectionResult> {
        if data.len() < 5 {
            return Err(DetectorError::detection_failed("Data too short for TLS"));
        }
        
        let content_type = data[0];
        let version_major = data[1];
        let version_minor = data[2];
        
        let valid_content_type = matches!(content_type, 0x14 | 0x15 | 0x16 | 0x17);
        let valid_version = matches!((version_major, version_minor), 
            (0x03, 0x00) | (0x03, 0x01) | (0x03, 0x02) | (0x03, 0x03) | (0x03, 0x04));
        
        if valid_content_type && valid_version {
            let length = u16::from_be_bytes([data[3], data[4]]) as usize;
            
            if length > 0 && length <= 16384 && data.len() >= 5 + length {
                let confidence = match content_type {
                    0x16 => 0.95, // Handshake
                    0x17 => 0.9,  // Application Data
                    0x15 => 0.85, // Alert
                    0x14 => 0.8,  // Change Cipher Spec
                    _ => 0.7,
                };
                
                return Ok(SimdDetectionResult {
                    protocol: ProtocolType::TLS,
                    confidence,
                    match_positions: vec![0],
                    instruction_set: self.instruction_set,
                });
            }
        }
        
        Err(DetectorError::detection_failed("No TLS patterns found"))
    }
    
    fn detect_multiple(&self, data: &[u8], protocols: &[ProtocolType]) -> Result<Vec<SimdDetectionResult>> {
        let mut results = Vec::new();
        
        for &protocol in protocols {
            let result = match protocol {
                ProtocolType::HTTP2 => self.detect_http2(data),
                ProtocolType::QUIC => self.detect_quic(data),
                ProtocolType::GRPC => self.detect_grpc(data),
                ProtocolType::WebSocket => self.detect_websocket(data),
                ProtocolType::TLS => self.detect_tls(data),
                _ => continue,
            };
            
            if let Ok(detection) = result {
                results.push(detection);
            }
        }
        
        results.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap_or(std::cmp::Ordering::Equal));
        Ok(results)
    }
    
    fn instruction_set(&self) -> SimdInstructionSet {
        self.instruction_set
    }
    
    fn supports_protocol(&self, protocol: ProtocolType) -> bool {
        matches!(
            protocol,
            ProtocolType::HTTP2
                | ProtocolType::QUIC
                | ProtocolType::GRPC
                | ProtocolType::WebSocket
                | ProtocolType::TLS
                | ProtocolType::UDP
        )
    }
}