Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/allen-reflex-kernel/src/card.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / allen-reflex-kernel / src / card.rs` 242 lines ยท 8219 bytes Raw Blame History 1 //! The exam card file `\EFI\AIENOS\ALLEN.EXM` (QUALIFICATION ONLY input of the pi5-allen image). 2 //! 3 //! Layout: plain text, at most [`FILE_MAX`] bytes, LF line ends. 4 //! - line 1: exactly [`HEADER`] 5 //! - lines 2..: 1..[`CASES_MAX`] case lines `|` 6 //! - `id`: 1..[`ID_MAX`] bytes of `A-Z a-z 0-9 _ -`, unique in the file 7 //! - `turns`: 1..[`TURNS_MAX`] bytes, the exam's turns field (`user ~~ assistant ~~ user ...`) 8 //! - every byte is printable ASCII 0x20..0x7e or the LF that ends a line; the file ends with an LF; 9 //! no empty line, no CR, no tab, no NUL. 10 //! 11 //! The file carries no expected answers: labels stay on the host, which joins the decision lines 12 //! with them (`scripts/allen_pi_results.sh`). Only the turns reach the core. 13 14 use alloc::vec::Vec; 15 16 /// First line of the file. 17 pub const HEADER: &[u8] = b"AIENALX1 QUALIF-ALLEN-EXAM"; 18 /// Largest file accepted. 19 pub const FILE_MAX: usize = 131_072; 20 /// Most cases in one file. 21 pub const CASES_MAX: usize = 512; 22 /// Longest case id. 23 pub const ID_MAX: usize = 16; 24 /// Longest turns field. 25 pub const TURNS_MAX: usize = 4096; 26 27 /// One case: byte ranges into the file buffer. 28 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 29 pub struct CaseRef { 30 /// id bytes `[id.0, id.1)` 31 pub id: (usize, usize), 32 /// turns bytes `[turns.0, turns.1)` 33 pub turns: (usize, usize), 34 } 35 36 /// Parse and check the whole file. `Err` names the first failed check; nothing is kept then. 37 pub fn parse(buf: &[u8]) -> Result, &'static str> { 38 let n = buf.len(); 39 if n == 0 || n > FILE_MAX { 40 return Err("file size must be 1..131072 bytes"); 41 } 42 if !buf 43 .iter() 44 .all(|&b| b == b'\n' || (0x20..=0x7e).contains(&b)) 45 { 46 return Err("only printable ASCII and LF line ends are allowed"); 47 } 48 if buf[n - 1] != b'\n' { 49 return Err("file does not end with a line feed"); 50 } 51 let hl = HEADER.len(); 52 if n < hl + 1 || &buf[..hl] != HEADER || buf[hl] != b'\n' { 53 return Err("bad header line"); 54 } 55 let mut out: Vec = Vec::new(); 56 let mut at = hl + 1; 57 while at < n { 58 let mut e = at; 59 while buf[e] != b'\n' { 60 e += 1; 61 } 62 if e == at { 63 return Err("empty line"); 64 } 65 if out.len() >= CASES_MAX { 66 return Err("more than 512 cases"); 67 } 68 let bar = match buf[at..e].iter().position(|&b| b == b'|') { 69 Some(p) => at + p, 70 None => return Err("case line has no '|' after the id"), 71 }; 72 let id = &buf[at..bar]; 73 if id.is_empty() || id.len() > ID_MAX { 74 return Err("id must be 1..16 bytes"); 75 } 76 if !id 77 .iter() 78 .all(|&b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-') 79 { 80 return Err("id may hold only A-Z a-z 0-9 _ -"); 81 } 82 let tl = e - (bar + 1); 83 if tl == 0 || tl > TURNS_MAX { 84 return Err("turns must be 1..4096 bytes"); 85 } 86 if out.iter().any(|c| &buf[c.id.0..c.id.1] == id) { 87 return Err("duplicate case id"); 88 } 89 out.push(CaseRef { 90 id: (at, bar), 91 turns: (bar + 1, e), 92 }); 93 at = e + 1; 94 } 95 if out.is_empty() { 96 return Err("no case line"); 97 } 98 Ok(out) 99 } 100 101 /// First line of a route card (aienos#399): interactive requests, QUALIFICATION ONLY input. 102 pub const ROUTE_HEADER: &[u8] = b"AIENALR1 QUALIF-ALLEN-ROUTE"; 103 /// Largest session id accepted on a route card line. 104 pub const SESSION_MAX: u64 = 999_999_999; 105 106 /// Which kind of card file this is (by its first line). 107 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 108 pub enum Kind { 109 /// `ALLEN.EXM` exam card ([`HEADER`]) 110 Exam, 111 /// route card ([`ROUTE_HEADER`]) 112 Route, 113 } 114 115 /// The card kind from the header line, `None` when neither header. 116 pub fn kind(buf: &[u8]) -> Option { 117 for (h, k) in [(HEADER, Kind::Exam), (ROUTE_HEADER, Kind::Route)] { 118 if buf.len() > h.len() && &buf[..h.len()] == h && buf[h.len()] == b'\n' { 119 return Some(k); 120 } 121 } 122 None 123 } 124 125 /// One route card request. The card carries no expected answer. 126 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 127 pub struct RouteRef { 128 /// id bytes 129 pub id: (usize, usize), 130 /// owner (true) or guest (false), as the owner-access path of aienos#396 would say 131 pub owner: bool, 132 /// session id 133 pub session: u64, 134 /// owner memory consent recorded (flag `C`) 135 pub consent: bool, 136 /// the lifecycle has a pending item (flag `P`) 137 pub pending: bool, 138 /// turns bytes, `%HH` escaped (decoded by the runner) 139 pub turns: (usize, usize), 140 } 141 142 /// Parse a route card. Layout as the exam card, header [`ROUTE_HEADER`], request lines 143 /// `|:||`; 144 /// `turns` uses `%HH` escapes for any byte that is not printable ASCII (and `%25` for `%`). 145 pub fn parse_route(buf: &[u8]) -> Result, &'static str> { 146 let n = buf.len(); 147 if n == 0 || n > FILE_MAX { 148 return Err("file size must be 1..131072 bytes"); 149 } 150 if !buf 151 .iter() 152 .all(|&b| b == b'\n' || (0x20..=0x7e).contains(&b)) 153 { 154 return Err("only printable ASCII and LF line ends are allowed"); 155 } 156 if buf[n - 1] != b'\n' { 157 return Err("file does not end with a line feed"); 158 } 159 if kind(buf) != Some(Kind::Route) { 160 return Err("bad header line"); 161 } 162 let mut out: Vec = Vec::new(); 163 let mut at = ROUTE_HEADER.len() + 1; 164 while at < n { 165 let mut e = at; 166 while buf[e] != b'\n' { 167 e += 1; 168 } 169 if e == at { 170 return Err("empty line"); 171 } 172 if out.len() >= CASES_MAX { 173 return Err("more than 512 cases"); 174 } 175 let line = &buf[at..e]; 176 let mut bars = [0usize; 3]; 177 let mut k = 0; 178 for (i, &b) in line.iter().enumerate() { 179 if b == b'|' && k < 3 { 180 bars[k] = at + i; 181 k += 1; 182 } 183 } 184 if k != 3 { 185 return Err("route line needs id|speaker:session|flags|turns"); 186 } 187 let id = &buf[at..bars[0]]; 188 if id.is_empty() || id.len() > ID_MAX { 189 return Err("id must be 1..16 bytes"); 190 } 191 if !id 192 .iter() 193 .all(|&b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-') 194 { 195 return Err("id may hold only A-Z a-z 0-9 _ -"); 196 } 197 let who = &buf[bars[0] + 1..bars[1]]; 198 let (owner, rest) = if let Some(r) = who.strip_prefix(b"owner:") { 199 (true, r) 200 } else if let Some(r) = who.strip_prefix(b"guest:") { 201 (false, r) 202 } else { 203 return Err("speaker must be owner: or guest:"); 204 }; 205 if rest.is_empty() || rest.len() > 9 || !rest.iter().all(|b| b.is_ascii_digit()) { 206 return Err("session must be 1..`999999999`"); 207 } 208 let session = rest.iter().fold(0u64, |a, &b| a * 10 + (b - b'0') as u64); 209 if session == 0 || session > SESSION_MAX { 210 return Err("session must be 1..`999999999`"); 211 } 212 let flags = &buf[bars[1] + 1..bars[2]]; 213 let ok_flags = flags == b"-" 214 || (!flags.is_empty() 215 && flags.len() <= 2 216 && flags.iter().all(|b| *b == b'C' || *b == b'P') 217 && !(flags.len() == 2 && flags[0] == flags[1])); 218 if !ok_flags { 219 return Err("flags must be - or letters from CP"); 220 } 221 let tl = e - (bars[2] + 1); 222 if tl == 0 || tl > TURNS_MAX { 223 return Err("turns must be 1..4096 bytes"); 224 } 225 if out.iter().any(|c| &buf[c.id.0..c.id.1] == id) { 226 return Err("duplicate case id"); 227 } 228 out.push(RouteRef { 229 id: (at, bars[0]), 230 owner, 231 session, 232 consent: flags.contains(&b'C'), 233 pending: flags.contains(&b'P'), 234 turns: (bars[2] + 1, e), 235 }); 236 at = e + 1; 237 } 238 if out.is_empty() { 239 return Err("no case line"); 240 } 241 Ok(out) 242 }