interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / protocol / omnet / om / codec / test / driver.om

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