interplane.Get Omega
Read in your language

agent-archive

aien-dev/agent-archive

Brought by drakestapleton

branch main Branches Tags History

agent-archive / src / pb.rs

86 lines · 2879 bytes Raw Blame History
1//! Minimal protobuf wire-format walker (no schema). Used for Gemini/Antigravity step blobs.
2
3pub enum Val<'a> {
4 Varint(u64),
5 Bytes(&'a [u8]),
6 Fixed(#[allow(dead_code)] u64),
7}
8
9pub fn varint(b: &[u8], i: &mut usize) -> Option<u64> {
10 let mut v = 0u64;
11 let mut shift = 0;
12 while *i < b.len() {
13 let c = b[*i];
14 *i += 1;
15 v |= ((c & 0x7f) as u64) << shift;
16 if c & 0x80 == 0 {
17 return Some(v);
18 }
19 shift += 7;
20 if shift > 63 {
21 return None;
22 }
23 }
24 None
25}
26
27/// Parse one message level. Returns None if the bytes are not a clean message.
28pub fn fields(b: &[u8]) -> Option<Vec<(u32, Val<'_>)>> {
29 let mut i = 0;
30 let mut out = Vec::new();
31 while i < b.len() {
32 let tag = varint(b, &mut i)?;
33 let f = (tag >> 3) as u32;
34 if f == 0 {
35 return None;
36 }
37 match tag & 7 {
38 0 => out.push((f, Val::Varint(varint(b, &mut i)?))),
39 1 => {
40 let e = i.checked_add(8)?;
41 if e > b.len() { return None; }
42 out.push((f, Val::Fixed(u64::from_le_bytes(b[i..e].try_into().ok()?))));
43 i = e;
44 }
45 2 => {
46 let n = varint(b, &mut i)? as usize;
47 let e = i.checked_add(n)?;
48 if e > b.len() { return None; }
49 out.push((f, Val::Bytes(&b[i..e])));
50 i = e;
51 }
52 5 => {
53 let e = i.checked_add(4)?;
54 if e > b.len() { return None; }
55 out.push((f, Val::Fixed(u32::from_le_bytes(b[i..e].try_into().ok()?) as u64)));
56 i = e;
57 }
58 _ => return None,
59 }
60 }
61 Some(out)
62}
63
64pub fn dump(b: &[u8], depth: usize, path: &str, out: &mut String) {
65 if let Some(fs) = fields(b) {
66 for (f, v) in fs {
67 let p = format!("{path}.{f}");
68 match v {
69 Val::Varint(n) => out.push_str(&format!("{p} = {n}\n")),
70 Val::Fixed(n) => out.push_str(&format!("{p} = fixed {n}\n")),
71 Val::Bytes(x) => {
72 if depth < 8 && !x.is_empty() && fields(x).is_some() && std::str::from_utf8(x).map(|s| s.chars().any(|c| c.is_control() && c != '\n')).unwrap_or(true) {
73 out.push_str(&format!("{p} {{\n"));
74 dump(x, depth + 1, &p, out);
75 out.push_str("}\n");
76 } else if let Ok(s) = std::str::from_utf8(x) {
77 let t: String = s.chars().take(100).collect();
78 out.push_str(&format!("{p} = {:?}\n", t));
79 } else {
80 out.push_str(&format!("{p} = <{} bytes>\n", x.len()));
81 }
82 }
83 }
84 }
85 }
86}