omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / net / tests / parse.om
| 1 | note seed/net/tests/parse.om -- memory-mode parser driver for the corpus run by check.py. |
| 2 | note Compose: { printf 'om2\n'; cat seed/net/http.om seed/net/tests/parse.om; } | omc |
| 3 | note stdin: records `C <mode> <len>` LF, then len message bytes, then LF. mode q = request, |
| 4 | note s = response, h = response to a HEAD request. Caps used here: head 2048 bytes, 16 header |
| 5 | note fields, body 4096 bytes, chunk framing allowance 1024 bytes (README "Checks"). |
| 6 | note stdout, one line per record, idx counting from 0: |
| 7 | note idx ok status version keep bodymode fields headhash bodylen bodyhash |
| 8 | note idx err CODE (CODE = minus the result, e.g. 400, 431, 499) |
| 9 | note idx eof (a request stream that ended before any byte) |
| 10 | note headhash = FNV-1a 64 over [method SP target LF for requests] then, per field, |
| 11 | note lowercase(name) ':' value LF; bodyhash = FNV-1a 64 of the decoded body. Both 16 hex digits. |
| 12 | note Exit 0 after the last record; 2..8 = malformed corpus or I/O failure. |
| 13 | fn fnvb h ulong b uchar to ulong |
| 14 | ret mul xor h ulong b 1099511628211 |
| 15 | end |
| 16 | fn fnvs h ulong a view uchar * to ulong |
| 17 | x = h |
| 18 | for i ulong len a |
| 19 | x = fnvb x get a i |
| 20 | end |
| 21 | ret x |
| 22 | end |
| 23 | fn fnvl h ulong a view uchar * to ulong |
| 24 | x = h |
| 25 | for i ulong len a |
| 26 | x = fnvb x hlow get a i |
| 27 | end |
| 28 | ret x |
| 29 | end |
| 30 | fn hdhash cs edit ulong 320 io edit uchar 65536 to ulong |
| 31 | h = ulong 14695981039346656037 |
| 32 | if eq get cs HSERVER 1 |
| 33 | h = fnvs h view part io get cs HMOFF get cs HMLEN |
| 34 | h = fnvb h 32 |
| 35 | h = fnvs h view part io get cs HTOFF get cs HTLEN |
| 36 | h = fnvb h 10 |
| 37 | end |
| 38 | for i ulong get cs HHN |
| 39 | h = fnvl h view part io hhnoff cs i hhnlen cs i |
| 40 | h = fnvb h 58 |
| 41 | h = fnvs h view part io hhvoff cs i hhvlen cs i |
| 42 | h = fnvb h 10 |
| 43 | end |
| 44 | ret h |
| 45 | end |
| 46 | note the output line is built in aux[65536, 65792) |
| 47 | fn obch aux edit uchar 65792 p ulong c uchar to ulong |
| 48 | put aux p c |
| 49 | ret add p 1 |
| 50 | end |
| 51 | fn obdec aux edit uchar 65792 p ulong v ulong to ulong |
| 52 | d = new uchar 20 0 |
| 53 | i = ulong 20 |
| 54 | x = v |
| 55 | for it ulong 20 |
| 56 | i = sub i 1 |
| 57 | put d i uchar add rem x 10 48 |
| 58 | x = div x 10 |
| 59 | if eq x 0 |
| 60 | break |
| 61 | end |
| 62 | end |
| 63 | q = p |
| 64 | for k ulong sub 20 i |
| 65 | put aux q get d add i k |
| 66 | q = add q 1 |
| 67 | end |
| 68 | ret q |
| 69 | end |
| 70 | fn obhex aux edit uchar 65792 p ulong v ulong to ulong |
| 71 | for k ulong 16 |
| 72 | put aux add p k hhexd and shr v mul sub 15 k 4 15 |
| 73 | end |
| 74 | ret add p 16 |
| 75 | end |
| 76 | fn wrall aux edit uchar 65792 s ulong e ulong to long |
| 77 | p = s |
| 78 | for it ulong 4096 |
| 79 | if ge p e |
| 80 | ret 0 |
| 81 | end |
| 82 | w = write 1 aux p sub e p |
| 83 | if le w 0 |
| 84 | ret 7 |
| 85 | end |
| 86 | p = add p ulong w |
| 87 | end |
| 88 | ret 7 |
| 89 | end |
| 90 | fn prerr aux edit uchar 65792 idx ulong r long to long |
| 91 | p = obdec aux 65536 idx |
| 92 | p = obch aux p 32 |
| 93 | if eq r 0 |
| 94 | p = obch aux p 101 |
| 95 | p = obch aux p 111 |
| 96 | p = obch aux p 102 |
| 97 | else |
| 98 | p = obch aux p 101 |
| 99 | p = obch aux p 114 |
| 100 | p = obch aux p 114 |
| 101 | p = obch aux p 32 |
| 102 | p = obdec aux p ulong neg r |
| 103 | end |
| 104 | p = obch aux p 10 |
| 105 | ret wrall aux 65536 p |
| 106 | end |
| 107 | fn prok cs edit ulong 320 io edit uchar 65536 aux edit uchar 65792 idx ulong bl ulong to long |
| 108 | hh = hdhash cs io |
| 109 | bh = fnvs 14695981039346656037 view part aux 0 bl |
| 110 | p = obdec aux 65536 idx |
| 111 | p = obch aux p 32 |
| 112 | p = obch aux p 111 |
| 113 | p = obch aux p 107 |
| 114 | p = obch aux p 32 |
| 115 | p = obdec aux p get cs HSTAT |
| 116 | p = obch aux p 32 |
| 117 | p = obdec aux p get cs HVER |
| 118 | p = obch aux p 32 |
| 119 | p = obdec aux p get cs HKEEP |
| 120 | p = obch aux p 32 |
| 121 | p = obdec aux p get cs HBMODE |
| 122 | p = obch aux p 32 |
| 123 | p = obdec aux p get cs HHN |
| 124 | p = obch aux p 32 |
| 125 | p = obhex aux p hh |
| 126 | p = obch aux p 32 |
| 127 | p = obdec aux p bl |
| 128 | p = obch aux p 32 |
| 129 | p = obhex aux p bh |
| 130 | p = obch aux p 10 |
| 131 | ret wrall aux 65536 p |
| 132 | end |
| 133 | fn docase cs edit ulong 320 io edit uchar 65536 aux edit uchar 65792 md uchar idx ulong to long |
| 134 | put cs HHEADMX 2048 |
| 135 | put cs HHMAX 16 |
| 136 | put cs HBCAP 4096 |
| 137 | put cs HFRMAX 1024 |
| 138 | r = long 0 |
| 139 | if eq md 113 |
| 140 | r = hreqrd cs io |
| 141 | else |
| 142 | if eq md 104 |
| 143 | put cs HISHEAD 1 |
| 144 | end |
| 145 | r = hrsprd cs io |
| 146 | end |
| 147 | if le r 0 |
| 148 | ret prerr aux idx r |
| 149 | end |
| 150 | b = hbodyall cs io part aux 0 65536 |
| 151 | if lt b 0 |
| 152 | ret prerr aux idx b |
| 153 | end |
| 154 | ret prok cs io aux idx ulong b |
| 155 | end |
| 156 | note hdrq: index after the LF of the record header at p, 0 when malformed |
| 157 | fn hdrq inb view uchar 1048576 p ulong n ulong to ulong |
| 158 | if gt add p 6 n |
| 159 | ret 0 |
| 160 | end |
| 161 | if or ne get inb p 67 ne get inb add p 1 32 |
| 162 | ret 0 |
| 163 | end |
| 164 | if ne get inb add p 3 32 |
| 165 | ret 0 |
| 166 | end |
| 167 | q = add p 4 |
| 168 | for k ulong 30 |
| 169 | if ge q n |
| 170 | ret 0 |
| 171 | end |
| 172 | c = get inb q |
| 173 | if eq c 10 |
| 174 | break |
| 175 | end |
| 176 | if or lt c 48 gt c 57 |
| 177 | ret 0 |
| 178 | end |
| 179 | q = add q 1 |
| 180 | end |
| 181 | if ge q n |
| 182 | ret 0 |
| 183 | end |
| 184 | ret add q 1 |
| 185 | end |
| 186 | fn hdrlen inb view uchar 1048576 p ulong q ulong to ulong |
| 187 | v = ulong 0 |
| 188 | k = add p 4 |
| 189 | while lt k sub q 1 bound 30 |
| 190 | v = add mul v 10 ulong sub get inb k 48 |
| 191 | k = add k 1 |
| 192 | end |
| 193 | ret v |
| 194 | end |
| 195 | fn rolei md uchar to ulong |
| 196 | if eq md 113 |
| 197 | ret 1 |
| 198 | end |
| 199 | ret 0 |
| 200 | end |
| 201 | note onerec: the record at sb[0] (input end sb[1]); the index after it, or 0 on a failure |
| 202 | fn onerec inb view uchar 1048576 sb edit ulong 4 io edit uchar 65536 cs edit ulong 320 aux edit uchar 65792 idx ulong to ulong |
| 203 | p = get sb 0 |
| 204 | n = get sb 1 |
| 205 | q = hdrq inb p n |
| 206 | if eq q 0 |
| 207 | ret 0 |
| 208 | end |
| 209 | ln = hdrlen inb p q |
| 210 | if gt add q add ln 1 n |
| 211 | ret 0 |
| 212 | end |
| 213 | r = hinit cs io -1 rolei get inb add p 2 |
| 214 | if lt r 0 |
| 215 | ret 0 |
| 216 | end |
| 217 | r = hmemld cs io view part inb q ln |
| 218 | if lt r 0 |
| 219 | ret 0 |
| 220 | end |
| 221 | r = docase cs io aux get inb add p 2 idx |
| 222 | if ne r 0 |
| 223 | ret 0 |
| 224 | end |
| 225 | ret add add q ln 1 |
| 226 | end |
| 227 | note refill: keep inb[p, n) and read until the buffer is full or stdin ends. |
| 228 | note sb: 0 = p, 1 = n, 2 = end of input seen |
| 229 | fn refill inb edit uchar 1048576 sb edit ulong 4 to long |
| 230 | p = get sb 0 |
| 231 | n = get sb 1 |
| 232 | for i ulong sub n p |
| 233 | put inb i get inb add p i |
| 234 | end |
| 235 | n = sub n p |
| 236 | put sb 0 0 |
| 237 | for it ulong 1048576 |
| 238 | if ge n len inb |
| 239 | break |
| 240 | end |
| 241 | r = read 0 inb n sub len inb n |
| 242 | if lt r 0 |
| 243 | ret r |
| 244 | end |
| 245 | if eq r 0 |
| 246 | put sb 2 1 |
| 247 | break |
| 248 | end |
| 249 | n = add n ulong r |
| 250 | end |
| 251 | put sb 1 n |
| 252 | ret 0 |
| 253 | end |
| 254 | note runall: records stream through a 1 MiB buffer that always holds at least 65536 + 32 bytes |
| 255 | note ahead (or the rest of the input), more than one record (hmemld caps a message at 49152) |
| 256 | fn runall inb edit uchar 1048576 sb edit ulong 4 io edit uchar 65536 cs edit ulong 320 aux edit uchar 65792 to int |
| 257 | idx = ulong 0 |
| 258 | for it ulong 1000000 |
| 259 | if and eq get sb 2 0 lt sub get sb 1 get sb 0 65568 |
| 260 | if lt refill inb sb 0 |
| 261 | ret 2 |
| 262 | end |
| 263 | end |
| 264 | if ge get sb 0 get sb 1 |
| 265 | ret 0 |
| 266 | end |
| 267 | np = onerec inb sb io cs aux idx |
| 268 | if eq np 0 |
| 269 | ret 3 |
| 270 | end |
| 271 | put sb 0 np |
| 272 | idx = add idx 1 |
| 273 | end |
| 274 | ret 8 |
| 275 | end |
| 276 | fn main to int |
| 277 | inb = new uchar 1048576 0 |
| 278 | io = new uchar 65536 0 |
| 279 | cs = new ulong 320 0 |
| 280 | aux = new uchar 65792 0 |
| 281 | sb = new ulong 4 0 |
| 282 | ret runall inb sb io cs aux |
| 283 | end |