mailhand
mailhand: agents look after a person's email in seeded Omega. Maildir + MIME parsing, JSONL CLI and MCP, rule triage, policy-gated sending, hash-chained audit. Free (Apache-2.0).
Brought by drakestapleton
mailhand / tests / gen / mkbig.om
| 1 | note ======== tests/gen/mkbig.om: large synthetic MIME inputs for the parser tests ======== |
| 2 | note Usage: mkbig KIND OUTFILE, KIND one of body1m hdr1m hdrs100k nest50 nest5000 b64_64m. |
| 3 | note Bytes go straight to OUTFILE through the 64 KiB output window mb_w; a long repetition is |
| 4 | note written from a 64 KiB local window of whole pattern copies. No array grows with the output. |
| 5 | note Exit 0 written; 1 usage (argument count, unknown KIND, empty or unusable OUTFILE path); |
| 6 | note 6 I/O (open, write or close failed; a partly written OUTFILE is unlinked; stderr has -errno). |
| 7 | note OUTFILE is created or truncated (mode 0644 before umask); a symlink as the last path |
| 8 | note component is refused by O_NOFOLLOW (exit 6, errno 40). |
| 9 | note Layouts ("\n" is one LF byte; no CR anywhere; no final LF unless shown): |
| 10 | note body1m "Subject: big\n\n" + 1048576 "x" 1048590 bytes |
| 11 | note hdr1m "X-Big: " + 1048576 "x" + "\n\nbody" 1048589 bytes |
| 12 | note hdrs100k 100000 * "X-A: b\n" + "\nbody" 700005 bytes |
| 13 | note nestD "Content-Type: multipart/mixed; boundary=b0\n\n", then for k = 0..D-2 |
| 14 | note "--bk\nContent-Type: multipart/mixed; boundary=b(k+1)\n\n", then |
| 15 | note "--b(D-1)\nContent-Type: text/plain\n\ndeep", then for k = D-1 down to 0 |
| 16 | note "\n--bk--\n" (ends with LF). D counts multipart containers b0..b(D-1). |
| 17 | note nest50 2998 bytes, nest5000 326698 bytes |
| 18 | note b64_64m "Content-Type: application/octet-stream\nContent-Transfer-Encoding: base64\n\n" |
| 19 | note + 22369621 * "QUFB" + "QQ==", one line: decodes to 67108864 "A" 89478562 bytes |
| 20 | |
| 21 | const MB_WIN ulong 65536 |
| 22 | note open flags (Linux AArch64): O_WRONLY 1, O_CREAT 64, O_TRUNC 512, O_NOFOLLOW 32768, O_CLOEXEC 524288 |
| 23 | const MB_OW int 557633 |
| 24 | |
| 25 | note mb_w: output window; mb_v: 0 bytes waiting in mb_w, 1 descriptor, 2 failed (1), 3 errno |
| 26 | global mb_w uchar 65536 |
| 27 | global mb_v ulong 4 |
| 28 | |
| 29 | text mb_kus "usage: mkbig KIND OUTFILE (KIND: body1m hdr1m hdrs100k nest50 nest5000 b64_64m)" |
| 30 | text mb_kpath "mkbig: OUTFILE is empty, longer than 4095 bytes or holds a NUL byte" |
| 31 | text mb_kopen "mkbig: cannot open OUTFILE, errno " |
| 32 | text mb_kwr "mkbig: writing OUTFILE failed (partial file removed), errno " |
| 33 | |
| 34 | note mb_fail: remember the first failure; r is the host word result (-errno, or 0 for no progress) |
| 35 | fn mb_fail r long to void |
| 36 | if ne get mb_v 2 0 |
| 37 | ret |
| 38 | end |
| 39 | e = long 5 |
| 40 | if lt r 0 |
| 41 | e = neg r |
| 42 | end |
| 43 | put mb_v 2 1 |
| 44 | put mb_v 3 ulong e |
| 45 | end |
| 46 | |
| 47 | note mb_dnum: one stderr line, text t then the decimal of -e (e is -errno or 0) |
| 48 | fn mb_dnum t view uchar * e long to void |
| 49 | b = new uchar 24 0 |
| 50 | x = ulong 0 |
| 51 | if lt e 0 |
| 52 | x = ulong neg e |
| 53 | end |
| 54 | i = ulong 24 |
| 55 | while true bound 24 |
| 56 | i = sub i 1 |
| 57 | put b i uchar add 48 rem x 10 |
| 58 | x = div x 10 |
| 59 | if eq x 0 |
| 60 | break |
| 61 | end |
| 62 | end |
| 63 | diag2 t part b i sub 24 i |
| 64 | end |
| 65 | |
| 66 | note mb_flush: write the waiting bytes of mb_w; false after any failure (sticky) |
| 67 | fn mb_flush to bool |
| 68 | if ne get mb_v 2 0 |
| 69 | ret false |
| 70 | end |
| 71 | n = get mb_v 0 |
| 72 | fd = int get mb_v 1 |
| 73 | p = ulong 0 |
| 74 | while lt p n bound 9223372036854775807 |
| 75 | r = write fd mb_w p sub n p |
| 76 | if eq r -4 |
| 77 | continue |
| 78 | end |
| 79 | if le r 0 |
| 80 | mb_fail r |
| 81 | ret false |
| 82 | end |
| 83 | p = add p ulong r |
| 84 | end |
| 85 | put mb_v 0 0 |
| 86 | ret true |
| 87 | end |
| 88 | |
| 89 | note mb_wv: write all of t straight to the descriptor (mb_w must be flushed first) |
| 90 | fn mb_wv t view uchar * to bool |
| 91 | if ne get mb_v 2 0 |
| 92 | ret false |
| 93 | end |
| 94 | fd = int get mb_v 1 |
| 95 | n = len t |
| 96 | p = ulong 0 |
| 97 | while lt p n bound 9223372036854775807 |
| 98 | r = write fd t p sub n p |
| 99 | if eq r -4 |
| 100 | continue |
| 101 | end |
| 102 | if le r 0 |
| 103 | mb_fail r |
| 104 | ret false |
| 105 | end |
| 106 | p = add p ulong r |
| 107 | end |
| 108 | ret true |
| 109 | end |
| 110 | |
| 111 | fn mb_b c uchar to void |
| 112 | n = get mb_v 0 |
| 113 | if ge n MB_WIN |
| 114 | z = mb_flush |
| 115 | put mb_v 0 0 |
| 116 | n = ulong 0 |
| 117 | end |
| 118 | put mb_w n c |
| 119 | put mb_v 0 add n 1 |
| 120 | end |
| 121 | |
| 122 | fn mb_t t view uchar * to void |
| 123 | for i ulong len t bound 9223372036854775807 |
| 124 | mb_b get t i |
| 125 | end |
| 126 | end |
| 127 | |
| 128 | note mb_num: decimal digits of v |
| 129 | fn mb_num v ulong to void |
| 130 | b = new uchar 24 0 |
| 131 | x = v |
| 132 | i = ulong 24 |
| 133 | while true bound 24 |
| 134 | i = sub i 1 |
| 135 | put b i uchar add 48 rem x 10 |
| 136 | x = div x 10 |
| 137 | if eq x 0 |
| 138 | break |
| 139 | end |
| 140 | end |
| 141 | for j ulong sub 24 i bound 24 |
| 142 | mb_b get b add i j |
| 143 | end |
| 144 | end |
| 145 | |
| 146 | note mb_rep: cnt copies of t; whole windows of copies go straight to the descriptor |
| 147 | fn mb_rep t view uchar * cnt ulong to void |
| 148 | pl = len t |
| 149 | if eq pl 0 |
| 150 | ret |
| 151 | end |
| 152 | k = div MB_WIN pl |
| 153 | if eq k 0 |
| 154 | for q ulong cnt bound 9223372036854775807 |
| 155 | mb_t t |
| 156 | end |
| 157 | ret |
| 158 | end |
| 159 | if not mb_flush |
| 160 | ret |
| 161 | end |
| 162 | w = new uchar 65536 0 |
| 163 | wl = mul k pl |
| 164 | for i ulong wl bound 65536 |
| 165 | put w i get t rem i pl |
| 166 | end |
| 167 | full = div cnt k |
| 168 | for j ulong full bound 9223372036854775807 |
| 169 | if not mb_wv part w 0 wl |
| 170 | ret |
| 171 | end |
| 172 | end |
| 173 | for m ulong rem cnt k bound 65536 |
| 174 | mb_t t |
| 175 | end |
| 176 | end |
| 177 | |
| 178 | text mb_ks1 "Subject: big\n\n" |
| 179 | text mb_kx "x" |
| 180 | fn mb_body1m to void |
| 181 | mb_t mb_ks1 |
| 182 | mb_rep mb_kx 1048576 |
| 183 | end |
| 184 | |
| 185 | text mb_kh1 "X-Big: " |
| 186 | text mb_kh2 "\n\nbody" |
| 187 | fn mb_hdr1m to void |
| 188 | mb_t mb_kh1 |
| 189 | mb_rep mb_kx 1048576 |
| 190 | mb_t mb_kh2 |
| 191 | end |
| 192 | |
| 193 | text mb_ka "X-A: b\n" |
| 194 | text mb_kbody "\nbody" |
| 195 | fn mb_hdrs100k to void |
| 196 | mb_rep mb_ka 100000 |
| 197 | mb_t mb_kbody |
| 198 | end |
| 199 | |
| 200 | text mb_kct "Content-Type: multipart/mixed; boundary=b" |
| 201 | text mb_kdd "--b" |
| 202 | text mb_knn "\n\n" |
| 203 | text mb_knct "\nContent-Type: multipart/mixed; boundary=b" |
| 204 | text mb_kleaf "\nContent-Type: text/plain\n\ndeep" |
| 205 | text mb_kcl "\n--b" |
| 206 | text mb_kce "--\n" |
| 207 | note mb_nest: d >= 1 multipart containers b0..b(d-1), each opening the next, then a text/plain leaf |
| 208 | fn mb_nest d ulong to void |
| 209 | mb_t mb_kct |
| 210 | mb_num 0 |
| 211 | mb_t mb_knn |
| 212 | k = ulong 0 |
| 213 | while lt add k 1 d bound 9223372036854775807 |
| 214 | mb_t mb_kdd |
| 215 | mb_num k |
| 216 | mb_t mb_knct |
| 217 | mb_num add k 1 |
| 218 | mb_t mb_knn |
| 219 | k = add k 1 |
| 220 | end |
| 221 | mb_t mb_kdd |
| 222 | mb_num sub d 1 |
| 223 | mb_t mb_kleaf |
| 224 | k = d |
| 225 | while gt k 0 bound 9223372036854775807 |
| 226 | k = sub k 1 |
| 227 | mb_t mb_kcl |
| 228 | mb_num k |
| 229 | mb_t mb_kce |
| 230 | end |
| 231 | end |
| 232 | |
| 233 | text mb_kb64 "Content-Type: application/octet-stream\nContent-Transfer-Encoding: base64\n\n" |
| 234 | text mb_kq "QUFB" |
| 235 | text mb_kqt "QQ==" |
| 236 | note mb_b64: 67108864 = 3 * 22369621 + 1, so 22369621 "QUFB" ("AAA") and one "QQ==" ("A") |
| 237 | fn mb_b64 to void |
| 238 | mb_t mb_kb64 |
| 239 | mb_rep mb_kq 22369621 |
| 240 | mb_t mb_kqt |
| 241 | end |
| 242 | |
| 243 | fn mb_eq a view uchar * b view uchar * to bool |
| 244 | if ne len a len b |
| 245 | ret false |
| 246 | end |
| 247 | for i ulong len a bound 9223372036854775807 |
| 248 | if ne get a i get b i |
| 249 | ret false |
| 250 | end |
| 251 | end |
| 252 | ret true |
| 253 | end |
| 254 | |
| 255 | text mb_n1 "body1m" |
| 256 | text mb_n2 "hdr1m" |
| 257 | text mb_n3 "hdrs100k" |
| 258 | text mb_n4 "nest50" |
| 259 | text mb_n5 "nest5000" |
| 260 | text mb_n6 "b64_64m" |
| 261 | note mb_kind: 1..6 for the KIND names above, 0 for anything else |
| 262 | fn mb_kind a view uchar * to ulong |
| 263 | if mb_eq a mb_n1 |
| 264 | ret 1 |
| 265 | end |
| 266 | if mb_eq a mb_n2 |
| 267 | ret 2 |
| 268 | end |
| 269 | if mb_eq a mb_n3 |
| 270 | ret 3 |
| 271 | end |
| 272 | if mb_eq a mb_n4 |
| 273 | ret 4 |
| 274 | end |
| 275 | if mb_eq a mb_n5 |
| 276 | ret 5 |
| 277 | end |
| 278 | if mb_eq a mb_n6 |
| 279 | ret 6 |
| 280 | end |
| 281 | ret 0 |
| 282 | end |
| 283 | |
| 284 | fn mb_gen kd ulong to void |
| 285 | if eq kd 1 |
| 286 | mb_body1m |
| 287 | end |
| 288 | if eq kd 2 |
| 289 | mb_hdr1m |
| 290 | end |
| 291 | if eq kd 3 |
| 292 | mb_hdrs100k |
| 293 | end |
| 294 | if eq kd 4 |
| 295 | mb_nest 50 |
| 296 | end |
| 297 | if eq kd 5 |
| 298 | mb_nest 5000 |
| 299 | end |
| 300 | if eq kd 6 |
| 301 | mb_b64 |
| 302 | end |
| 303 | end |
| 304 | |
| 305 | note one argument is at most MAX_ARG_STRLEN 131072 (kernel boundary); OUTFILE at most PATH_MAX |
| 306 | fn main to int |
| 307 | if ne ulong argcnt 3 |
| 308 | diag mb_kus |
| 309 | ret EUSAGE |
| 310 | end |
| 311 | a = new uchar 131072 0 |
| 312 | r = argread 1 a 0 131072 |
| 313 | if lt r 0 |
| 314 | diag mb_kus |
| 315 | ret EUSAGE |
| 316 | end |
| 317 | kd = mb_kind part a 0 ulong r |
| 318 | if eq kd 0 |
| 319 | diag mb_kus |
| 320 | ret EUSAGE |
| 321 | end |
| 322 | r = argread 2 a 0 131072 |
| 323 | if lt r 1 |
| 324 | diag mb_kpath |
| 325 | ret EUSAGE |
| 326 | end |
| 327 | p_reset |
| 328 | p_t part a 0 ulong r |
| 329 | if not p_ok |
| 330 | diag mb_kpath |
| 331 | ret EUSAGE |
| 332 | end |
| 333 | fd = fopen gp 0 get gv VPL MB_OW 420 |
| 334 | if lt fd 0 |
| 335 | mb_dnum mb_kopen fd |
| 336 | ret EIO |
| 337 | end |
| 338 | put mb_v 0 0 |
| 339 | put mb_v 1 ulong fd |
| 340 | put mb_v 2 0 |
| 341 | put mb_v 3 0 |
| 342 | mb_gen kd |
| 343 | z = mb_flush |
| 344 | c = fclose int fd |
| 345 | if lt c 0 |
| 346 | mb_fail c |
| 347 | end |
| 348 | if ne get mb_v 2 0 |
| 349 | z2 = unlink gp 0 get gv VPL |
| 350 | mb_dnum mb_kwr neg long get mb_v 3 |
| 351 | ret EIO |
| 352 | end |
| 353 | ret EOK |
| 354 | end |