Files
marqdo/src/host/encoding.rs
T
cflmy 8b0ea9ed7a refactor: 精简三处重复 — as_text×18、catalog 助手×4、view 路由 34 臂
按审计结论清理冗余(不改变行为):
1. as_text:17 个 host 模块各自复制同一函数(两种措辞),合并为
   crate::host::as_text(统一 "{label} needs text",无测试依赖旧措辞)
2. catalog.rs:extract_fm_field / extract_nested_by / extract_fm_list /
   yaml_escape 与 knowledge::util 逐字相同,改为直接引用
3. view/mod.rs:handle 29 臂 + handle_debug 5 臂折叠成两张路由表
   (Notebook/Debug/Action 三种处理器),每臂原本重复同一 api_debug_post 包装

实测:
- cargo build 零警告;cargo test --release 605 passed / 0 failed
- view 服务实测:GET / → 200;POST /api/debug/continue → 200(真实响应
  {"error":"unknown session"});POST /api/nope → 405

行数:view/mod.rs -99(168 删 69 增);src 合计由 53,276 降至约 53,000
(as_text 18 文件各约 6 行、catalog 4 函数约 70 行)

期间发现并修正一处真实风险:路由臂分布在两个 handler(handle / handle_debug),
首次只替换了一个导致 debug 路由 405;已按 handler 分别建表并由实测覆盖。
2026-10-22 15:39:18 +08:00

224 lines
6.8 KiB
Rust

//! Base64 / hex encoding for Mid M1 (`lib/encoding`).
//! No third-party crates — standard alphabet + padding.
use crate::host::as_text;
use crate::value::Value;
const B64: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn base64_encode(text: &Value) -> Result<Value, String> {
let s = as_text(text, "text")?;
Ok(Value::Text(encode_base64(s.as_bytes())))
}
pub fn base64_decode(text: &Value) -> Result<Value, String> {
let s = as_text(text, "text")?;
let bytes = decode_base64(s)?;
let out = String::from_utf8(bytes).map_err(|e| format!("base64_decode: invalid utf-8: {e}"))?;
Ok(Value::Text(out))
}
pub fn hex_encode(text: &Value) -> Result<Value, String> {
let s = as_text(text, "text")?;
let mut out = String::with_capacity(s.len() * 2);
for b in s.as_bytes() {
out.push_str(&format!("{b:02x}"));
}
Ok(Value::Text(out))
}
pub fn hex_decode(text: &Value) -> Result<Value, String> {
let s = as_text(text, "text")?;
if s.len() % 2 != 0 {
return Err("hex_decode: length must be even".into());
}
let mut bytes = Vec::with_capacity(s.len() / 2);
// Require ASCII hex digits only (byte-oriented).
let raw = s.as_bytes();
if raw.len() != s.len() {
return Err("hex_decode: needs ASCII hex".into());
}
for i in (0..raw.len()).step_by(2) {
let hi = from_hex(raw[i])?;
let lo = from_hex(raw[i + 1])?;
bytes.push((hi << 4) | lo);
}
let out = String::from_utf8(bytes).map_err(|e| format!("hex_decode: invalid utf-8: {e}"))?;
Ok(Value::Text(out))
}
const B32: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
pub fn base32_encode(text: &Value) -> Result<Value, String> {
let s = as_text(text, "text")?;
Ok(Value::Text(encode_base32(s.as_bytes())))
}
pub fn base32_decode(text: &Value) -> Result<Value, String> {
let s = as_text(text, "text")?;
let bytes = decode_base32(s)?;
let out = String::from_utf8(bytes).map_err(|e| format!("base32_decode: invalid utf-8: {e}"))?;
Ok(Value::Text(out))
}
fn encode_base32(input: &[u8]) -> String {
let mut out = String::new();
let mut buffer: u64 = 0;
let mut bits = 0u32;
for &b in input {
buffer = (buffer << 8) | (b as u64);
bits += 8;
while bits >= 5 {
bits -= 5;
let idx = ((buffer >> bits) & 31) as usize;
out.push(B32[idx] as char);
}
}
if bits > 0 {
let idx = ((buffer << (5 - bits)) & 31) as usize;
out.push(B32[idx] as char);
}
while out.len() % 8 != 0 {
out.push('=');
}
out
}
fn decode_base32(input: &str) -> Result<Vec<u8>, String> {
let cleaned: String = input
.chars()
.filter(|c| !c.is_whitespace())
.map(|c| c.to_ascii_uppercase())
.collect();
if cleaned.len() % 8 != 0 {
return Err("base32_decode: length must be multiple of 8".into());
}
let mut out = Vec::new();
let mut buffer: u64 = 0;
let mut bits = 0u32;
for c in cleaned.bytes() {
if c == b'=' {
break;
}
let v = match c {
b'A'..=b'Z' => c - b'A',
b'2'..=b'7' => c - b'2' + 26,
_ => return Err(format!("base32_decode: invalid character {:?}", c as char)),
} as u64;
buffer = (buffer << 5) | v;
bits += 5;
if bits >= 8 {
bits -= 8;
out.push(((buffer >> bits) & 0xff) as u8);
}
}
Ok(out)
}
fn from_hex(b: u8) -> Result<u8, String> {
match b {
b'0'..=b'9' => Ok(b - b'0'),
b'a'..=b'f' => Ok(b - b'a' + 10),
b'A'..=b'F' => Ok(b - b'A' + 10),
_ => Err(format!("hex_decode: invalid digit {:?}", b as char)),
}
}
fn encode_base64(input: &[u8]) -> String {
let mut out = String::with_capacity((input.len() + 2) / 3 * 4);
let mut i = 0;
while i + 3 <= input.len() {
let n = ((input[i] as u32) << 16) | ((input[i + 1] as u32) << 8) | (input[i + 2] as u32);
out.push(B64[((n >> 18) & 63) as usize] as char);
out.push(B64[((n >> 12) & 63) as usize] as char);
out.push(B64[((n >> 6) & 63) as usize] as char);
out.push(B64[(n & 63) as usize] as char);
i += 3;
}
let rem = input.len() - i;
if rem == 1 {
let n = (input[i] as u32) << 16;
out.push(B64[((n >> 18) & 63) as usize] as char);
out.push(B64[((n >> 12) & 63) as usize] as char);
out.push('=');
out.push('=');
} else if rem == 2 {
let n = ((input[i] as u32) << 16) | ((input[i + 1] as u32) << 8);
out.push(B64[((n >> 18) & 63) as usize] as char);
out.push(B64[((n >> 12) & 63) as usize] as char);
out.push(B64[((n >> 6) & 63) as usize] as char);
out.push('=');
}
out
}
fn decode_base64(input: &str) -> Result<Vec<u8>, String> {
let raw = input.as_bytes();
if raw.len() % 4 != 0 {
return Err("base64_decode: length must be multiple of 4".into());
}
let mut out = Vec::with_capacity(raw.len() / 4 * 3);
let mut i = 0;
while i < raw.len() {
let a = decode_b64_char(raw[i])?;
let b = decode_b64_char(raw[i + 1])?;
let c = raw[i + 2];
let d = raw[i + 3];
let c_val = if c == b'=' {
0
} else {
decode_b64_char(c)?
};
let d_val = if d == b'=' {
0
} else {
decode_b64_char(d)?
};
let n = (a << 18) | (b << 12) | (c_val << 6) | d_val;
out.push(((n >> 16) & 0xff) as u8);
if c != b'=' {
out.push(((n >> 8) & 0xff) as u8);
}
if d != b'=' {
out.push((n & 0xff) as u8);
}
if c == b'=' && d != b'=' {
return Err("base64_decode: invalid padding".into());
}
i += 4;
}
Ok(out)
}
fn decode_b64_char(b: u8) -> Result<u32, String> {
let v = match b {
b'A'..=b'Z' => b - b'A',
b'a'..=b'z' => b - b'a' + 26,
b'0'..=b'9' => b - b'0' + 52,
b'+' => 62,
b'/' => 63,
_ => return Err(format!("base64_decode: invalid character {:?}", b as char)),
};
Ok(v as u32)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip_hello() {
let enc = base64_encode(&Value::Text("hello".into())).unwrap();
assert_eq!(enc, Value::Text("aGVsbG8=".into()));
let dec = base64_decode(&enc).unwrap();
assert_eq!(dec, Value::Text("hello".into()));
let hx = hex_encode(&Value::Text("hi".into())).unwrap();
assert_eq!(hx, Value::Text("6869".into()));
assert_eq!(hex_decode(&hx).unwrap(), Value::Text("hi".into()));
let b32 = base32_encode(&Value::Text("hello".into())).unwrap();
assert_eq!(b32, Value::Text("NBSWY3DP".into()));
assert_eq!(base32_decode(&b32).unwrap(), Value::Text("hello".into()));
}
}