use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub const MAX_COMPILED_REGEX_BYTES: usize = 32 * 1024 * 1024;
pub const MAX_UDP_PROBE_BYTES: usize = 512;
pub fn unescape(payload: &str) -> Vec<u8> {
let mut out = Vec::with_capacity(payload.len());
let mut chars = payload.chars();
while let Some(c) = chars.next() {
if c != '\\' {
out.extend_from_slice(c.encode_utf8(&mut [0u8; 4]).as_bytes());
continue;
}
match chars.next() {
Some('r') => out.push(b'\r'),
Some('n') => out.push(b'\n'),
Some('t') => out.push(b'\t'),
Some('0') => out.push(0),
Some('\\') => out.push(b'\\'),
Some('x') => {
let hi = chars.next().and_then(|h| h.to_digit(16));
let lo = chars.next().and_then(|l| l.to_digit(16));
match (hi, lo) {
(Some(hi), Some(lo)) => out.push((hi * 16 + lo) as u8),
_ => out.extend_from_slice(b"\\x"),
}
}
Some(other) => {
out.push(b'\\');
out.extend_from_slice(other.encode_utf8(&mut [0u8; 4]).as_bytes());
}
None => out.push(b'\\'),
}
}
out
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceSignature {
pub name: String,
pub default_ports: Vec<u16>,
pub description: Option<String>,
pub attribution: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Probe {
pub name: Option<String>,
pub payload: String,
pub protocol: String,
#[serde(default)]
pub rarity: u8,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MatchRule {
pub name: Option<String>,
pub pattern: String,
pub version_group: Option<u8>,
pub vendor: Option<String>,
pub product: Option<String>,
pub context: Option<String>,
pub example: Option<String>,
pub metadata: Option<HashMap<String, String>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceDefinition {
pub service: ServiceSignature,
#[serde(default)]
pub probe: Vec<Probe>,
#[serde(default)]
pub r#match: Vec<MatchRule>,
}