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/omega-mv/blob/main/protocol/omnet/om/codec/test/driver.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / protocol / omnet / om / codec / test / driver.om` 257 lines ยท 4704 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note driver.om: test driver for om/codec (program mode), concatenated after codec.om. 3 note Reads cases on stdin, one per line: OP ARG 4 note OP b64e b64d b32e b32d hexe hexd aume aumd nkye nkyd 5 note ARG the input bytes as hex (either case, even digit count), or - for empty. For a decoder 6 note these are the bytes of the text to decode, so CR, LF, 255 and any other byte can appear. 7 note Writes one line per case: 8 note encoders "ok TEXT" (the encoded characters, - when empty) 9 note decoders "ok HEX" (decoded bytes as lowercase hex, - when empty) or "err" 10 note aume and nkye take exactly 32 bytes. Exit 0 at end of input, 65 on a malformed line. 11 note With one argument N (1..6) the driver instead calls one coder with a length one past its 12 note capacity, which must trap 3 (exit 103): 1 b64e 49153, 2 b64d 65537, 3 b32e 40961, 13 note 4 b32d 65537, 5 hexe 32769, 6 hexd 65537. 14 15 text tbad "driver: bad input\n" 16 text tok "ok " 17 text terr "err\n" 18 text tdash "-" 19 text tnl "\n" 20 21 global dvin uchar `16777216` 22 global dvlen ulong 1 23 global dvbuf uchar 131080 24 global dvarg uchar 8 25 26 fn dvdie to void 27 r = write 2 tbad 0 len tbad 28 exit 65 29 end 30 31 fn dvnib c ulong to ulong 32 if and ge c 48 le c 57 33 ret sub c 48 34 end 35 if and ge c 97 le c 102 36 ret sub c 87 37 end 38 if and ge c 65 le c 70 39 ret sub c 55 40 end 41 dvdie 42 ret 0 43 end 44 45 note hex digits dvin[o..e) into a; returns the byte count 46 fn dvhex a edit uchar 65536 o ulong e ulong to ulong 47 h = sub e o 48 if and eq h 1 eq get dvin o 45 49 ret 0 50 end 51 if or ne and h 1 0 gt h 131072 52 dvdie 53 end 54 c = div h 2 55 for i ulong c 56 hi = dvnib ulong get dvin add o mul i 2 57 lo = dvnib ulong get dvin add o add mul i 2 1 58 put a i uchar or shl hi 4 lo 59 end 60 ret c 61 end 62 63 note "ok " then o[0..n) as text (hx 0) or as lowercase hex (hx 1), "-" when n is 0 64 fn dvok o view uchar 65536 n ulong hx ulong to void 65 r = write 1 tok 0 len tok 66 if eq n 0 67 r = write 1 tdash 0 1 68 r = write 1 tnl 0 1 69 ret 70 end 71 m = n 72 if eq hx 1 73 m = mul n 2 74 for i ulong n 75 v = ulong get o i 76 put dvbuf mul i 2 cdhexc shr v 4 77 put dvbuf add mul i 2 1 cdhexc and v 15 78 end 79 else 80 for i ulong n 81 put dvbuf i get o i 82 end 83 end 84 put dvbuf m 10 85 r = write 1 dvbuf 0 add m 1 86 end 87 88 note a decoder result r over o: cdbad -> err, else ok HEX 89 fn dvdec o view uchar 65536 r ulong to void 90 if eq r cdbad 91 w = write 1 terr 0 len terr 92 ret 93 end 94 dvok o r 1 95 end 96 97 note a fixed 32-byte decoder result: 1 -> ok HEX of k, 0 -> err (k must then be all zero) 98 fn dvfix k view uchar 32 r ulong o edit uchar 65536 to void 99 if eq r 0 100 for i ulong 32 101 if ne get k i 0 102 dvdie 103 end 104 end 105 w = write 1 terr 0 len terr 106 ret 107 end 108 for i ulong 32 109 put o i get k i 110 end 111 dvok o 32 1 112 end 113 114 fn dvk32 a view uchar 65536 n ulong k edit uchar 32 to void 115 if ne n 32 116 dvdie 117 end 118 for i ulong 32 119 put k i get a i 120 end 121 end 122 123 note run one case: op code x, input a[0..n) 124 fn dvcase x ulong a view uchar 65536 n ulong o edit uchar 65536 k edit uchar 32 to void 125 note b64e b64d b32e b32d hexe hexd as 4 big-endian bytes 126 if eq x `1647719525` 127 dvok o cdb64enc a n o 0 128 ret 129 end 130 if eq x `1647719524` 131 dvdec o `cdb64dec` a n o 132 ret 133 end 134 if eq x `1647522405` 135 dvok o cdb32enc a n o 0 136 ret 137 end 138 if eq x `1647522404` 139 dvdec o `cdb32dec` a n o 140 ret 141 end 142 if eq x `1751480421` 143 dvok o cdhexenc a n o 0 144 ret 145 end 146 if eq x `1751480420` 147 dvdec o cdhexdec a n o 148 ret 149 end 150 dvcase2 x a n o k 151 end 152 153 note aume aumd nkye nkyd as 4 big-endian bytes 154 fn dvcase2 x ulong a view uchar 65536 n ulong o edit uchar 65536 k edit uchar 32 to void 155 if eq x `1635085669` 156 dvk32 a n k 157 dvok o cdaumenc k o 0 158 ret 159 end 160 if eq x `1635085668` 161 r = cdaumdec a n k 162 dvfix k r o 163 ret 164 end 165 if eq x `1852537189` 166 dvk32 a n k 167 dvok o cdnkenc k o 0 168 ret 169 end 170 if eq x `1852537188` 171 r = cdnkdec a n k 172 dvfix k r o 173 ret 174 end 175 dvdie 176 end 177 178 fn dvslurp to void 179 t = ulong 0 180 while true bound 999999 181 room = sub len dvin t 182 if eq room 0 183 dvdie 184 end 185 r = read 0 dvin t room 186 if lt r 0 187 dvdie 188 end 189 if eq r 0 190 break 191 end 192 t = add t ulong r 193 end 194 put dvlen 0 t 195 end 196 197 note over-capacity calls; each must trap 3 before it returns 198 fn dvtrap a view uchar 65536 o edit uchar 65536 to void 199 l = argread 1 dvarg 0 8 200 if ne l 1 201 dvdie 202 end 203 c = get dvarg 0 204 r = ulong 0 205 if eq c 49 206 r = cdb64enc a 49153 o 207 end 208 if eq c 50 209 r = `cdb64dec` a 65537 o 210 end 211 if eq c 51 212 r = cdb32enc a 40961 o 213 end 214 if eq c 52 215 r = `cdb32dec` a 65537 o 216 end 217 if eq c 53 218 r = cdhexenc a 32769 o 219 end 220 if eq c 54 221 r = cdhexdec a 65537 o 222 end 223 dvdie 224 end 225 226 fn main to int 227 a = new uchar 65536 0 228 o = new uchar 65536 0 229 k = new uchar 32 0 230 if ge argcnt 2 231 dvtrap a o 232 ret 1 233 end 234 dvslurp 235 tot = get dvlen 0 236 p = ulong 0 237 while lt p tot bound 999999 238 e = p 239 while ne get dvin e 10 bound 999999 240 e = add e 1 241 end 242 if lt sub e p 6 243 dvdie 244 end 245 if ne get dvin add p 4 32 246 dvdie 247 end 248 x = ulong 0 249 for i ulong 4 250 x = or shl x 8 ulong get dvin add p i 251 end 252 n = dvhex a add p 5 e 253 dvcase x a n o k 254 p = add e 1 255 end 256 ret 0 257 end