omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / cbor / test / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: program-mode test driver for om/cbor/cbor.om. Concatenated after |
| 3 | note om/blake2s/blake2s.om and om/cbor/cbor.om. |
| 4 | note |
| 5 | note driver reads one case per line from stdin: the input bytes as hex (either case, even digit |
| 6 | note count, at most 65536 bytes) or "-" for the empty input. For each case it writes one line: |
| 7 | note e<code> cbrdec rejected the input with that code |
| 8 | note ok NTOK DUP UTF8 K1 CANON HASH cbrdec accepted it: |
| 9 | note NTOK token count (cbrcount) |
| 10 | note DUP 1 when the root is a map with duplicate keys (cbrdupk), else 0 |
| 11 | note UTF8 1 when every text string is valid UTF-8 (cbrutf8), else 0 |
| 12 | note K1 major:argument of the value at unsigned key 1 of a root map (cbrmfind), or - |
| 13 | note CANON cbrenc output as lowercase hex, or - when empty (never: an item is >= 1 byte) |
| 14 | note HASH BLAKE2s-256 of CANON (b2shash), lowercase hex |
| 15 | note Exit 0 at end of input, 2 on a malformed line. |
| 16 | |
| 17 | global ib uchar 65536 |
| 18 | global ist ulong 2 |
| 19 | |
| 20 | note next input byte, -1 at end of input |
| 21 | fn getc to int |
| 22 | if eq get ist 0 get ist 1 |
| 23 | r = read 0 ib 0 65536 |
| 24 | if le r 0 |
| 25 | ret -1 |
| 26 | end |
| 27 | put ist 0 0 |
| 28 | put ist 1 ulong r |
| 29 | end |
| 30 | p = get ist 0 |
| 31 | put ist 0 add p 1 |
| 32 | ret int get ib p |
| 33 | end |
| 34 | |
| 35 | fn hexv c int to int |
| 36 | if and ge c 48 le c 57 |
| 37 | ret sub c 48 |
| 38 | end |
| 39 | if and ge c 97 le c 102 |
| 40 | ret sub c 87 |
| 41 | end |
| 42 | if and ge c 65 le c 70 |
| 43 | ret sub c 55 |
| 44 | end |
| 45 | ret -1 |
| 46 | end |
| 47 | |
| 48 | fn hexd v ulong to uchar |
| 49 | if lt v 10 |
| 50 | ret uchar add v 48 |
| 51 | end |
| 52 | ret uchar add v 87 |
| 53 | end |
| 54 | |
| 55 | note one line of hex into a: the byte count; -2 at end of input before any byte; -1 when malformed |
| 56 | fn rdline a edit uchar 65536 to long |
| 57 | c = getc |
| 58 | if eq c -1 |
| 59 | ret -2 |
| 60 | end |
| 61 | if eq c 45 |
| 62 | if eq getc 10 |
| 63 | ret 0 |
| 64 | end |
| 65 | ret -1 |
| 66 | end |
| 67 | t = ulong 0 |
| 68 | while ne c 10 bound 999999 |
| 69 | if ge t 65536 |
| 70 | ret -1 |
| 71 | end |
| 72 | hi = hexv c |
| 73 | lo = hexv getc |
| 74 | if or lt hi 0 lt lo 0 |
| 75 | ret -1 |
| 76 | end |
| 77 | put a t uchar add mul hi 16 lo |
| 78 | t = add t 1 |
| 79 | c = getc |
| 80 | end |
| 81 | ret long t |
| 82 | end |
| 83 | |
| 84 | note write a[0..n) as hex into h at offset at; returns the new offset |
| 85 | fn hexout h edit uchar 131200 at ulong a view uchar 65536 n ulong to ulong |
| 86 | for i ulong n |
| 87 | v = ulong get a i |
| 88 | put h add at mul i 2 hexd shr v 4 |
| 89 | put h add at add mul i 2 1 hexd and v 15 |
| 90 | end |
| 91 | ret add at mul n 2 |
| 92 | end |
| 93 | |
| 94 | fn hex32 h edit uchar 131200 at ulong a view uchar 32 to ulong |
| 95 | for i ulong 32 |
| 96 | v = ulong get a i |
| 97 | put h add at mul i 2 hexd shr v 4 |
| 98 | put h add at add mul i 2 1 hexd and v 15 |
| 99 | end |
| 100 | ret add at 64 |
| 101 | end |
| 102 | |
| 103 | note decimal v into h at offset at; returns the new offset |
| 104 | fn decout h edit uchar 131200 at ulong v ulong to ulong |
| 105 | g = new uchar 24 0 |
| 106 | n = ulong 0 |
| 107 | x = v |
| 108 | while true bound 24 |
| 109 | put g n uchar add rem x 10 48 |
| 110 | n = add n 1 |
| 111 | x = div x 10 |
| 112 | if eq x 0 |
| 113 | break |
| 114 | end |
| 115 | end |
| 116 | for i ulong n |
| 117 | put h add at i get g sub sub n i 1 |
| 118 | end |
| 119 | ret add at n |
| 120 | end |
| 121 | |
| 122 | fn putw h edit uchar 131200 at ulong c ulong to ulong |
| 123 | put h at uchar c |
| 124 | ret add at 1 |
| 125 | end |
| 126 | |
| 127 | note 1 when every text token of the table is valid UTF-8 |
| 128 | fn allutf8 d view uchar 65536 t view ulong 131074 to ulong |
| 129 | for i ulong cbrcount t |
| 130 | if eq cbrmaj t i 3 |
| 131 | if eq cbrutf8 d cbrpoff t i cbrval t i 0 |
| 132 | ret 0 |
| 133 | end |
| 134 | end |
| 135 | end |
| 136 | ret 1 |
| 137 | end |
| 138 | |
| 139 | fn rootdup d view uchar 65536 t view ulong 131074 to ulong |
| 140 | if eq cbrmaj t 0 5 |
| 141 | ret cbrdupk d t 0 |
| 142 | end |
| 143 | ret 0 |
| 144 | end |
| 145 | |
| 146 | note K1 field: major:argument of the value at key 1 of a root map, or - |
| 147 | fn k1out h edit uchar 131200 at ulong t view ulong 131074 to ulong |
| 148 | if ne cbrmaj t 0 5 |
| 149 | ret putw h at 45 |
| 150 | end |
| 151 | v = cbrmfind t 0 1 |
| 152 | if eq v 0 |
| 153 | ret putw h at 45 |
| 154 | end |
| 155 | q = decout h at cbrmaj t v |
| 156 | q = putw h q 58 |
| 157 | ret decout h q cbrval t v |
| 158 | end |
| 159 | |
| 160 | note the ok line after a successful decode of d into t |
| 161 | fn okline h edit uchar 131200 d view uchar 65536 t view ulong 131074 o edit uchar 65536 to ulong |
| 162 | md = new uchar 32 0 |
| 163 | k = new uchar 32 0 |
| 164 | n = cbrenc d t o |
| 165 | q = putw h 0 111 |
| 166 | q = putw h q 107 |
| 167 | q = putw h q 32 |
| 168 | q = decout h q cbrcount t |
| 169 | q = putw h q 32 |
| 170 | q = decout h q rootdup d t |
| 171 | q = putw h q 32 |
| 172 | q = decout h q allutf8 d t |
| 173 | q = putw h q 32 |
| 174 | q = k1out h q t |
| 175 | q = putw h q 32 |
| 176 | q = hexout h q o n |
| 177 | q = putw h q 32 |
| 178 | b2shash o n k 0 32 md |
| 179 | q = hex32 h q md |
| 180 | ret putw h q 10 |
| 181 | end |
| 182 | |
| 183 | fn cases to int |
| 184 | d = new uchar 65536 0 |
| 185 | t = new ulong 131074 0 |
| 186 | o = new uchar 65536 0 |
| 187 | h = new uchar 131200 0 |
| 188 | while true bound 999999 |
| 189 | n = rdline d |
| 190 | if eq n -2 |
| 191 | ret 0 |
| 192 | end |
| 193 | if lt n 0 |
| 194 | ret 2 |
| 195 | end |
| 196 | e = cbrdec d ulong n t |
| 197 | q = ulong 0 |
| 198 | if eq e 0 |
| 199 | q = okline h d t o |
| 200 | else |
| 201 | q = putw h 0 101 |
| 202 | q = decout h q e |
| 203 | q = putw h q 10 |
| 204 | end |
| 205 | w = write 1 h 0 q |
| 206 | if ne w long q |
| 207 | trap 1 |
| 208 | end |
| 209 | end |
| 210 | ret 2 |
| 211 | end |
| 212 | |
| 213 | fn main to int |
| 214 | ret cases |
| 215 | end |