base_d/features/
detection.rs1use crate::core::config::{DictionaryRegistry, EncodingMode};
2use crate::core::dictionary::Dictionary;
3use crate::decode;
4use std::collections::HashSet;
5
6#[derive(Debug, Clone)]
8pub struct DictionaryMatch {
9 pub name: String,
11 pub confidence: f64,
13 pub dictionary: Dictionary,
15}
16
17pub struct DictionaryDetector {
19 dictionaries: Vec<(String, Dictionary)>,
20}
21
22impl DictionaryDetector {
23 pub fn new(config: &DictionaryRegistry) -> Result<Self, Box<dyn std::error::Error>> {
25 let mut dictionaries = Vec::new();
26
27 for (name, dict_config) in &config.dictionaries {
28 let dictionary = match dict_config.mode {
29 EncodingMode::ByteRange => {
30 let start = dict_config
31 .start_codepoint
32 .ok_or("ByteRange mode requires start_codepoint")?;
33 Dictionary::new_with_mode_and_range(
34 Vec::new(),
35 dict_config.mode.clone(),
36 None,
37 Some(start),
38 )?
39 }
40 _ => {
41 let chars: Vec<char> = dict_config.chars.chars().collect();
42 let padding = dict_config.padding.as_ref().and_then(|s| s.chars().next());
43 Dictionary::new_with_mode(chars, dict_config.mode.clone(), padding)?
44 }
45 };
46 dictionaries.push((name.clone(), dictionary));
47 }
48
49 Ok(DictionaryDetector { dictionaries })
50 }
51
52 pub fn detect(&self, input: &str) -> Vec<DictionaryMatch> {
55 let input = input.trim();
56 if input.is_empty() {
57 return Vec::new();
58 }
59
60 let mut matches = Vec::new();
61
62 for (name, dict) in &self.dictionaries {
63 if let Some(confidence) = self.score_dictionary(input, dict) {
64 matches.push(DictionaryMatch {
65 name: name.clone(),
66 confidence,
67 dictionary: dict.clone(),
68 });
69 }
70 }
71
72 matches.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());
74
75 matches
76 }
77
78 fn score_dictionary(&self, input: &str, dict: &Dictionary) -> Option<f64> {
81 let mut score = 0.0;
82 let mut weight_sum = 0.0;
83
84 const CHARSET_WEIGHT: f64 = 0.25;
86 const SPECIFICITY_WEIGHT: f64 = 0.20; const PADDING_WEIGHT: f64 = 0.30; const LENGTH_WEIGHT: f64 = 0.15;
89 const DECODE_WEIGHT: f64 = 0.10;
90
91 let charset_score = self.score_charset(input, dict);
93 score += charset_score * CHARSET_WEIGHT;
94 weight_sum += CHARSET_WEIGHT;
95
96 if charset_score < 0.5 {
98 return None;
99 }
100
101 let specificity_score = self.score_specificity(input, dict);
103 score += specificity_score * SPECIFICITY_WEIGHT;
104 weight_sum += SPECIFICITY_WEIGHT;
105
106 if let Some(padding_score) = self.score_padding(input, dict) {
108 score += padding_score * PADDING_WEIGHT;
109 weight_sum += PADDING_WEIGHT;
110 }
111
112 let length_score = self.score_length(input, dict);
114 score += length_score * LENGTH_WEIGHT;
115 weight_sum += LENGTH_WEIGHT;
116
117 if let Some(decode_score) = self.score_decode(input, dict) {
119 score += decode_score * DECODE_WEIGHT;
120 weight_sum += DECODE_WEIGHT;
121 }
122
123 if weight_sum > 0.0 {
125 Some(score / weight_sum)
126 } else {
127 None
128 }
129 }
130
131 fn score_charset(&self, input: &str, dict: &Dictionary) -> f64 {
133 let input_chars: HashSet<char> = input
135 .chars()
136 .filter(|c| !c.is_whitespace() && Some(*c) != dict.padding())
137 .collect();
138
139 if input_chars.is_empty() {
140 return 0.0;
141 }
142
143 if let Some(start) = dict.start_codepoint() {
145 let in_range = input_chars
146 .iter()
147 .filter(|&&c| {
148 let code = c as u32;
149 code >= start && code < start + 256
150 })
151 .count();
152 return in_range as f64 / input_chars.len() as f64;
153 }
154
155 let mut valid_count = 0;
157 for c in &input_chars {
158 if dict.decode_char(*c).is_some() {
159 valid_count += 1;
160 }
161 }
162
163 if valid_count < input_chars.len() {
164 return 0.0;
166 }
167
168 let dict_size = dict.base();
170 let input_unique = input_chars.len();
171
172 let usage_ratio = input_unique as f64 / dict_size as f64;
174
175 if usage_ratio > 0.7 {
178 1.0
180 } else if usage_ratio > 0.5 {
181 0.85
183 } else if usage_ratio > 0.3 {
184 0.7
186 } else {
187 0.5
190 }
191 }
192
193 fn score_specificity(&self, _input: &str, dict: &Dictionary) -> f64 {
196 let dict_size = dict.base();
197
198 match dict_size {
201 16 => 1.0, 32 => 0.95, 58 => 0.90, 62 => 0.88, 64 => 0.92, 85 => 0.70, 256 => 0.60, _ if dict_size < 64 => 0.85,
209 _ if dict_size < 128 => 0.75,
210 _ => 0.65,
211 }
212 }
213
214 fn score_padding(&self, input: &str, dict: &Dictionary) -> Option<f64> {
216 let padding = dict.padding()?;
217
218 if *dict.mode() == EncodingMode::Chunked {
220 let has_padding = input.ends_with(padding);
221 let padding_count = input.chars().filter(|c| *c == padding).count();
222
223 if has_padding {
224 let trimmed = input.trim_end_matches(padding);
226 let internal_padding = trimmed.chars().any(|c| c == padding);
227
228 if internal_padding {
229 Some(0.5) } else if padding_count <= 3 {
231 Some(1.0) } else {
233 Some(0.3) }
235 } else {
236 Some(0.8)
238 }
239 } else {
240 None
241 }
242 }
243
244 fn score_length(&self, input: &str, dict: &Dictionary) -> f64 {
246 let length = input.trim().len();
247
248 match dict.mode() {
249 EncodingMode::Chunked => {
250 let base = dict.base();
252
253 let trimmed = if let Some(pad) = dict.padding() {
255 input.trim_end_matches(pad)
256 } else {
257 input
258 };
259
260 let expected_multiple = match base {
264 64 => 4,
265 32 => 8,
266 16 => 2,
267 _ => return 0.5, };
269
270 if trimmed.len() % expected_multiple == 0 {
271 1.0
272 } else {
273 0.3
274 }
275 }
276 EncodingMode::ByteRange => {
277 1.0
279 }
280 EncodingMode::BaseConversion => {
281 if length > 0 {
283 1.0
284 } else {
285 0.0
286 }
287 }
288 }
289 }
290
291 fn score_decode(&self, input: &str, dict: &Dictionary) -> Option<f64> {
293 match decode(input, dict) {
294 Ok(decoded) => {
295 if decoded.is_empty() {
296 Some(0.5)
297 } else {
298 Some(1.0)
300 }
301 }
302 Err(_) => {
303 Some(0.0)
305 }
306 }
307 }
308}
309
310pub fn detect_dictionary(input: &str) -> Result<Vec<DictionaryMatch>, Box<dyn std::error::Error>> {
312 let config = DictionaryRegistry::load_with_overrides()?;
313 let detector = DictionaryDetector::new(&config)?;
314 Ok(detector.detect(input))
315}
316
317#[cfg(test)]
318mod tests {
319 use super::*;
320 use crate::encode;
321
322 #[test]
323 fn test_detect_base64() {
324 let config = DictionaryRegistry::load_default().unwrap();
325 let detector = DictionaryDetector::new(&config).unwrap();
326
327 let matches = detector.detect("SGVsbG8sIFdvcmxkIQ==");
329 assert!(!matches.is_empty());
330 assert!(matches[0].name == "base64" || matches[0].name == "base64url");
332 assert!(matches[0].confidence > 0.7);
333 }
334
335 #[test]
336 fn test_detect_base32() {
337 let config = DictionaryRegistry::load_default().unwrap();
338 let detector = DictionaryDetector::new(&config).unwrap();
339
340 let matches = detector.detect("JBSWY3DPEBLW64TMMQ======");
341 assert!(!matches.is_empty());
342 let base32_found = matches.iter().take(5).any(|m| m.name.starts_with("base32"));
344 assert!(base32_found, "base32 should be in top 5 candidates");
345 }
346
347 #[test]
348 fn test_detect_hex() {
349 let config = DictionaryRegistry::load_default().unwrap();
350 let detector = DictionaryDetector::new(&config).unwrap();
351
352 let matches = detector.detect("48656c6c6f");
353 assert!(!matches.is_empty());
354 assert!(matches[0].name == "hex" || matches[0].name == "hex_math");
356 assert!(matches[0].confidence > 0.8);
357 }
358
359 #[test]
360 fn test_detect_from_encoded() {
361 let config = DictionaryRegistry::load_default().unwrap();
362
363 let dict_config = config.get_dictionary("base64").unwrap();
365 let chars: Vec<char> = dict_config.chars.chars().collect();
366 let padding = dict_config.padding.as_ref().and_then(|s| s.chars().next());
367 let dict = Dictionary::new_with_mode(chars, dict_config.mode.clone(), padding).unwrap();
368
369 let data = b"Hello, World!";
370 let encoded = encode(data, &dict);
371
372 let detector = DictionaryDetector::new(&config).unwrap();
373 let matches = detector.detect(&encoded);
374
375 assert!(!matches.is_empty());
376 assert!(matches[0].name == "base64" || matches[0].name == "base64url");
378 }
379
380 #[test]
381 fn test_detect_empty_input() {
382 let config = DictionaryRegistry::load_default().unwrap();
383 let detector = DictionaryDetector::new(&config).unwrap();
384
385 let matches = detector.detect("");
386 assert!(matches.is_empty());
387 }
388
389 #[test]
390 fn test_detect_invalid_input() {
391 let config = DictionaryRegistry::load_default().unwrap();
392 let detector = DictionaryDetector::new(&config).unwrap();
393
394 let matches = detector.detect("こんにちは世界");
396 if !matches.is_empty() {
398 assert!(matches[0].confidence < 0.5);
399 }
400 }
401}