interplane.Get Omega
Read in your language

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

branch main Branches Tags History

mailhand / tests / bench-lib.om

938 lines · 20065 bytes Raw Blame History
1note ======== tests/bench-lib.om: synthetic Maildir generator and exact rate printing (GateBuilder) ========
2note Shared by tests/gen/mkmaildir.om and tests/bench.om; listed after src/core.om and src/sha.om in
3note tests/units/mkmaildir.txt and tests/BENCH_UNITS. Uses core paths (gp, p_*), store S_AUX (the
4note Maildir root path) and core output (o_b, o_raw, o_digits, o_flush to stdout with gv VMODE 0).
5note
6note Maildir written by mk_gen N SEED (root = the path held in store S_AUX):
7note ROOT/cur ROOT/new ROOT/tmp (mode 0700 before umask; ROOT must not exist, its parent must).
8note Message i (0..N-1): file ROOT/new/T.MiPSEED.example when i rem 3 is 0 (unread), else
9note ROOT/cur/T.MiPSEED.example:2,S (seen) (i and SEED in decimal); mode 0644 before umask; LF line
10note ends, no CR.
11note T = 1735689600 (2025-01-01T00:00:00Z) + 600 i + (mix(SMIX + 2i + 1) rem 600), so T grows with i.
12note Kind i rem 5: 0 plain (no MIME headers), 1 multipart/mixed: text/plain part + base64
13note application/octet-stream attachment of 64..16447 random bytes (76-character lines), 2 text/plain
14note utf-8 quoted-printable body with UTF-8 letters and "=" escaped and soft breaks (lines <= 76),
15note 3 RFC 2047 encoded words: Subject B- or Q-encoded (UTF-8 letter at the end), From display name
16note Q-encoded, 4 thread reply: "Re: " + the subject of an earlier message j (0 <= j < i),
17note In-Reply-To and References <mj.sSEED@example.org>.
18note Every message: Date (matches T, UTC), From "Name Name <word+digit@example.org>",
19note To "Bench Reader <bench@example.org>", Cc one example.org address in about 1 of 4,
20note Subject, Message-ID <mi.sSEED@example.org>. Only example.org addresses appear.
21note Contents come only from SEED and i (splitmix64: SMIX = mix(SEED + 0x9e3779b97f4a7c15),
22note message i body stream state mix(SMIX + 2i + 1), subject stream of message j mix(SMIX + 2j)), so
23note the same N and SEED give byte-identical files; the first N of a larger N are the same files.
24note No array grows with the output: bytes go through the 64 KiB window mk_w to the open file; a
25note message has no size limit. Fixed local arrays below hold generator-chosen words (at most 9
26note letters) and subjects (at most 53 bytes), never input. Files are not fsynced (bench data).
27
28const MK_WIN ulong 65536
29const MK_FMODE int 420
30note mk_g cells: 0 random state, 1 bytes waiting in mk_w, 2 descriptor+1, 3 failed (1), 4 errno,
31note 5 bytes of the current file, 6 total bytes, 7 seed, 8 column (base64/QP), 9 base64 group,
32note 10 bytes in the group, 11 files in new, 12 files in cur, 13..17 messages of kind 0..4,
33note 18 SMIX, 19 epoch of the current message, 20 N, 30 bad argument (mk_argnum)
34const MKG_RNG ulong 0
35const MKG_WN ulong 1
36const MKG_FD ulong 2
37const MKG_FAIL ulong 3
38const MKG_ERRNO ulong 4
39const MKG_FB ulong 5
40const MKG_TB ulong 6
41const MKG_SEED ulong 7
42const MKG_COL ulong 8
43const MKG_ACC ulong 9
44const MKG_NACC ulong 10
45const MKG_NEW ulong 11
46const MKG_CUR ulong 12
47const MKG_KIND ulong 13
48const MKG_SMIX ulong 18
49const MKG_T ulong 19
50const MKG_N ulong 20
51const MKG_BAD ulong 30
52global mk_w uchar 65536
53global mk_g ulong 32
54
55text mkt_cons "bcdfghjklmnprstvwz"
56text mkt_vow "aeiou"
57text mkt_b64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
58text mkt_hexu "0123456789ABCDEF"
59text mkt_wd "SunMonTueWedThuFriSat"
60text mkt_mon "JanFebMarAprMayJunJulAugSepOctNovDec"
61text mkt_nl "\n"
62text mkt_hdate "Date: "
63text mkt_comsp ", "
64text mkt_utc " +0000\n"
65text mkt_hfrom "From: "
66text mkt_lt " <"
67text mkt_atgt "@example.org>\n"
68text mkt_hto "To: Bench Reader <bench@example.org>\n"
69text mkt_hcc "Cc: "
70text mkt_atnl "@example.org\n"
71text mkt_hsubj "Subject: "
72text mkt_re "Re: "
73text mkt_ewb "=?UTF-8?B?"
74text mkt_ewq "=?UTF-8?Q?"
75text mkt_ewe "?="
76text mkt_hmid "Message-ID: <m"
77text mkt_hirt "In-Reply-To: <m"
78text mkt_hrefs "References: <m"
79text mkt_mids ".s"
80text mkt_mime "MIME-Version: 1.0\n"
81text mkt_ctplain "Content-Type: text/plain; charset=us-ascii\n"
82text mkt_ctutf "Content-Type: text/plain; charset=utf-8\n"
83text mkt_cteqp "Content-Transfer-Encoding: quoted-printable\n"
84text mkt_ctmix "Content-Type: multipart/mixed; boundary=\""
85text mkt_qnl "\"\n"
86text mkt_pre "This is a multi-part message in MIME format.\n"
87text mkt_bnd "bnd"
88text mkt_ctoct "Content-Type: application/octet-stream; name=\"data-"
89text mkt_cdatt "Content-Disposition: attachment; filename=\"data-"
90text mkt_bin ".bin\"\n"
91text mkt_cteb64 "Content-Transfer-Encoding: base64\n"
92text mkt_cur "/cur"
93text mkt_new "/new"
94text mkt_tmp "/tmp"
95text mkt_m ".M"
96text mkt_host ".example"
97text mkt_flag ":2,S"
98text mkt_epath "Maildir path is empty, longer than 4095 bytes or holds a NUL byte"
99text mkt_eroot "cannot create the Maildir directory (it must not exist; its parent must), errno "
100text mkt_ecreate "cannot create a message file, errno "
101text mkt_ewrite "writing a message file failed (that file removed), errno "
102text mkt_colsp ": "
103text mkt_kn "N="
104text mkt_kseed " seed="
105text mkt_kbytes " bytes="
106text mkt_knew " new="
107text mkt_kcur " cur="
108text mkt_kplain " plain="
109text mkt_kmulti " multipart="
110text mkt_kqp " qp="
111text mkt_kenc " encoded_subject="
112text mkt_kreply " reply="
113text mkt_na "n/a"
114
115note ---- random: splitmix64 (constants 0x9e3779b97f4a7c15, 0xbf58476d1ce4e5b9, 0x94d049bb133111eb) ----
116fn mk_mix z0 ulong to ulong
117z = xor z0 shr z0 30
118z = mul z 13787848793156543929
119z = xor z shr z 27
120z = mul z 10723151780598845931
121ret xor z shr z 31
122end
123
124fn mk_rnd to ulong
125s = add get mk_g MKG_RNG 11400714819323198485
126put mk_g MKG_RNG s
127ret mk_mix s
128end
129
130note mk_rn: a value 0..n-1 (n > 0)
131fn mk_rn n ulong to ulong
132ret rem mk_rnd n
133end
134
135note ---- output window ----
136note mk_fail: remember the first failure; r is the host word result (-errno, or 0 for no progress)
137fn mk_fail r long to void
138if ne get mk_g MKG_FAIL 0
139ret
140end
141e = long 5
142if lt r 0
143e = neg r
144end
145put mk_g MKG_FAIL 1
146put mk_g MKG_ERRNO ulong e
147end
148
149note mk_flush: write the waiting bytes of mk_w to the current file; false after any failure (sticky)
150fn mk_flush to bool
151if ne get mk_g MKG_FAIL 0
152ret false
153end
154n = get mk_g MKG_WN
155fd = int sub get mk_g MKG_FD 1
156p = ulong 0
157while lt p n bound 9223372036854775807
158r = write fd mk_w p sub n p
159if eq r -4
160continue
161end
162if le r 0
163mk_fail r
164ret false
165end
166p = add p ulong r
167end
168put mk_g MKG_WN 0
169ret true
170end
171
172fn mk_b c uchar to void
173n = get mk_g MKG_WN
174if ge n MK_WIN
175z = mk_flush
176put mk_g MKG_WN 0
177n = ulong 0
178end
179put mk_w n c
180put mk_g MKG_WN add n 1
181put mk_g MKG_FB add get mk_g MKG_FB 1
182end
183
184fn mk_t t view uchar * to void
185for i ulong len t bound 9223372036854775807
186mk_b get t i
187end
188end
189
190note mk_u: decimal digits of v; mk_u2: two digits, zero padded (v < 100)
191fn mk_u v ulong to void
192b = new uchar 24 0
193x = v
194i = ulong 24
195while true bound 24
196i = sub i 1
197put b i uchar add 48 rem x 10
198x = div x 10
199if eq x 0
200break
201end
202end
203for j ulong sub 24 i bound 24
204mk_b get b add i j
205end
206end
207
208fn mk_u2 v ulong to void
209mk_b uchar add 48 div v 10
210mk_b uchar add 48 rem v 10
211end
212
213note ---- words ----
214note mk_wordb: a pseudo-word of 2..9 letters (consonant, vowel, ...) into b at n; cap: capital first
215note letter; returns the new length
216fn mk_wordb b edit uchar * n ulong cap bool to ulong
217k = add 2 mk_rn 8
218p = n
219for i ulong k bound 16
220c = get mkt_cons mk_rn len mkt_cons
221if eq rem i 2 1
222c = get mkt_vow mk_rn len mkt_vow
223end
224if and cap eq i 0
225c = sub c 32
226end
227put b p c
228p = add p 1
229end
230ret p
231end
232
233fn mk_word cap bool to void
234b = new uchar 32 0
235n = mk_wordb b 0 cap
236mk_t part b 0 n
237end
238
239note mk_u8: append one UTF-8 letter to b at n: 0 e-acute, 1 u-umlaut, 2 sharp s, 3 euro sign
240fn mk_u8 b edit uchar * n ulong k ulong to ulong
241if eq k 3
242put b n 226
243put b add n 1 130
244put b add n 2 172
245ret add n 3
246end
247c = uchar 169
248if eq k 1
249c = 188
250end
251if eq k 2
252c = 159
253end
254put b n 195
255put b add n 1 c
256ret add n 2
257end
258
259note mk_subj: the plain ASCII subject of message j into b (from j's own subject stream, so a reply
260note repeats it): 2..5 words, or 1..2 words when j is of kind 3 (its encoded word stays <= 75
261note characters); returns the length (at most 49)
262fn mk_subj b edit uchar * j ulong to ulong
263keep = get mk_g MKG_RNG
264put mk_g MKG_RNG mk_mix add get mk_g MKG_SMIX mul j 2
265w = add 2 mk_rn 4
266if eq rem j 5 3
267w = add 1 mk_rn 2
268end
269n = ulong 0
270for i ulong w bound 8
271if gt i 0
272put b n 32
273n = add n 1
274end
275n = mk_wordb b n eq i 0
276end
277put mk_g MKG_RNG keep
278ret n
279end
280
281note ---- encoders ----
282note mk_b64q: the 4 characters of the 24-bit group v that holds k (1..3) input bytes, "=" padded
283fn mk_b64q v ulong k ulong to void
284mk_b get mkt_b64 and shr v 18 63
285mk_b get mkt_b64 and shr v 12 63
286if gt k 1
287mk_b get mkt_b64 and shr v 6 63
288else
289mk_b 61
290end
291if gt k 2
292mk_b get mkt_b64 and v 63
293else
294mk_b 61
295end
296end
297
298note mk_b64s: one byte into the streaming base64 body encoder (lines of 76 characters)
299fn mk_b64s c uchar to void
300a = or shl get mk_g MKG_ACC 8 ulong c
301k = add get mk_g MKG_NACC 1
302if lt k 3
303put mk_g MKG_ACC a
304put mk_g MKG_NACC k
305ret
306end
307mk_b64q a 3
308put mk_g MKG_ACC 0
309put mk_g MKG_NACC 0
310col = add get mk_g MKG_COL 4
311if ge col 76
312mk_b 10
313col = ulong 0
314end
315put mk_g MKG_COL col
316end
317
318note mk_b64e: end of a base64 body: the last group padded, the last line ended
319fn mk_b64e to void
320k = get mk_g MKG_NACC
321if gt k 0
322a = shl get mk_g MKG_ACC mul sub 3 k 8
323mk_b64q a k
324put mk_g MKG_COL add get mk_g MKG_COL 4
325end
326if gt get mk_g MKG_COL 0
327mk_b 10
328end
329put mk_g MKG_ACC 0
330put mk_g MKG_NACC 0
331put mk_g MKG_COL 0
332end
333
334note mk_b64v: base64 of b[0, n) with no line breaks (an RFC 2047 B encoded word)
335fn mk_b64v b view uchar * n ulong to void
336i = ulong 0
337while lt i n bound 9223372036854775807
338k = sub n i
339if gt k 3
340k = 3
341end
342v = ulong 0
343for j ulong 3
344v = shl v 8
345if lt j k
346v = or v ulong get b add i j
347end
348end
349mk_b64q v k
350i = add i k
351end
352end
353
354fn mk_alnum c uchar to bool
355if and ge c 97 le c 122
356ret true
357end
358if and ge c 65 le c 90
359ret true
360end
361ret and ge c 48 le c 57
362end
363
364note mk_hex3: "=XX" for byte c
365fn mk_hex3 c uchar to void
366mk_b 61
367mk_b get mkt_hexu ulong shr c 4
368mk_b get mkt_hexu ulong and c 15
369end
370
371note mk_qwc: one byte of an RFC 2047 Q encoded word: space "_", letters and digits as is, else "=XX"
372fn mk_qwc c uchar to void
373if eq c 32
374mk_b 95
375ret
376end
377if mk_alnum c
378mk_b c
379ret
380end
381mk_hex3 c
382end
383
384note mk_qpc: one byte of a quoted-printable body: "=", bytes >= 127 and control bytes other than LF
385note become "=XX"; LF is a hard line break; a soft break "=" LF keeps every line at most 76 characters
386fn mk_qpc c uchar to void
387if eq c 10
388mk_b 10
389put mk_g MKG_COL 0
390ret
391end
392w = ulong 1
393if or or eq c 61 ge c 127 lt c 32
394w = 3
395end
396col = get mk_g MKG_COL
397if gt add col w 75
398mk_b 61
399mk_b 10
400col = ulong 0
401end
402if eq w 3
403mk_hex3 c
404else
405mk_b c
406end
407put mk_g MKG_COL add col w
408end
409
410fn mk_bq c uchar qp bool to void
411if qp
412mk_qpc c
413ret
414end
415mk_b c
416end
417
418note ---- bodies ----
419note mk_wordo: one body word; in a QP body about 1 word in 6 gets a UTF-8 letter, 1 in 20 an "="
420fn mk_wordo qp bool to void
421b = new uchar 32 0
422n = mk_wordb b 0 false
423if qp
424if eq mk_rn 6 0
425n = mk_u8 b n mk_rn 4
426end
427if eq mk_rn 20 0
428put b n 61
429n = add n 1
430end
431end
432for i ulong n bound 32
433mk_bq get b i qp
434end
435end
436
437note mk_line: 4..12 words separated by spaces, then LF
438fn mk_line qp bool to void
439w = add 4 mk_rn 9
440for x ulong w bound 16
441if gt x 0
442mk_bq 32 qp
443end
444mk_wordo qp
445end
446mk_bq 10 qp
447end
448
449note mk_text: 2..31 lines
450fn mk_text qp bool to void
451put mk_g MKG_COL 0
452n = add 2 mk_rn 30
453for x ulong n bound 64
454mk_line qp
455end
456end
457
458fn mk_bnd i ulong to void
459mk_t mkt_bnd
460mk_u i
461mk_b 120
462mk_u get mk_g MKG_SEED
463end
464
465fn mk_delim i ulong last bool to void
466mk_b 45
467mk_b 45
468mk_bnd i
469if last
470mk_b 45
471mk_b 45
472end
473mk_b 10
474end
475
476note mk_multi: headers from MIME-Version on, and the body of a kind 1 message
477fn mk_multi i ulong to void
478mk_t mkt_mime
479mk_t mkt_ctmix
480mk_bnd i
481mk_t mkt_qnl
482mk_b 10
483mk_t mkt_pre
484mk_delim i false
485mk_t mkt_ctplain
486mk_b 10
487mk_text false
488mk_delim i false
489mk_t mkt_ctoct
490mk_u i
491mk_t mkt_bin
492mk_t mkt_cdatt
493mk_u i
494mk_t mkt_bin
495mk_t mkt_cteb64
496mk_b 10
497put mk_g MKG_COL 0
498n = add 64 mk_rn 16384
499for x ulong n bound 9223372036854775807
500mk_b64s uchar and mk_rnd 255
501end
502mk_b64e
503mk_delim i true
504end
505
506fn mk_body i ulong k ulong to void
507if eq k 1
508mk_multi i
509ret
510end
511if eq k 0
512mk_b 10
513mk_text false
514ret
515end
516mk_t mkt_mime
517if eq k 2
518mk_t mkt_ctutf
519mk_t mkt_cteqp
520mk_b 10
521mk_text true
522ret
523end
524if eq k 3
525mk_t mkt_ctutf
526else
527mk_t mkt_ctplain
528end
529mk_b 10
530mk_text false
531end
532
533note ---- headers ----
534note mk_date: "Date: Www, DD Mon YYYY HH:MM:SS +0000" LF for epoch seconds t (UTC; 1970-01-01 a Thursday)
535fn mk_date t ulong to void
536days = div t 86400
537sec = rem t 86400
538c = ulong civil long days
539y = div c 10000
540m = rem div c 100 100
541d = rem c 100
542w = rem add days 4 7
543mk_t mkt_hdate
544for i ulong 3
545mk_b get mkt_wd add mul w 3 i
546end
547mk_t mkt_comsp
548mk_u2 d
549mk_b 32
550for i ulong 3
551mk_b get mkt_mon add mul sub m 1 3 i
552end
553mk_b 32
554mk_u y
555mk_b 32
556mk_u2 div sec 3600
557mk_b 58
558mk_u2 rem div sec 60 60
559mk_b 58
560mk_u2 rem sec 60
561mk_t mkt_utc
562end
563
564note mk_from: "From: Name Name <wordD@example.org>"; enc: the display name has a UTF-8 letter and is
565note one Q encoded word
566fn mk_from enc bool to void
567nb = new uchar 128 0
568n = mk_wordb nb 0 true
569if enc
570n = mk_u8 nb n 0
571end
572put nb n 32
573n = mk_wordb nb add n 1 true
574mk_t mkt_hfrom
575if enc
576mk_t mkt_ewq
577for x ulong n bound 128
578mk_qwc get nb x
579end
580mk_t mkt_ewe
581else
582mk_t part nb 0 n
583end
584mk_t mkt_lt
585mk_word false
586mk_b uchar add 48 mk_rn 10
587mk_t mkt_atgt
588end
589
590fn mk_cc to void
591mk_t mkt_hcc
592mk_word false
593mk_t mkt_atnl
594end
595
596note mk_hsubj: the Subject line of message i of kind k (j: the parent of a reply)
597fn mk_hsubj i ulong k ulong j ulong to void
598sb = new uchar 256 0
599mk_t mkt_hsubj
600if eq k 4
601mk_t mkt_re
602n1 = mk_subj sb j
603mk_t part sb 0 n1
604mk_b 10
605ret
606end
607n = mk_subj sb i
608if ne k 3
609mk_t part sb 0 n
610mk_b 10
611ret
612end
613put sb n 32
614n = mk_u8 sb add n 1 mk_rn 4
615if eq mk_rn 2 0
616mk_t mkt_ewb
617mk_b64v sb n
618else
619mk_t mkt_ewq
620for x ulong n bound 256
621mk_qwc get sb x
622end
623end
624mk_t mkt_ewe
625mk_b 10
626end
627
628note mk_mid: header text t, then "i.sSEED@example.org>" LF
629fn mk_mid t view uchar * i ulong to void
630mk_t t
631mk_u i
632mk_t mkt_mids
633mk_u get mk_g MKG_SEED
634mk_t mkt_atgt
635end
636
637note mk_msg: the bytes of message i through mk_b
638fn mk_msg i ulong to void
639k = rem i 5
640smix = get mk_g MKG_SMIX
641put mk_g MKG_RNG mk_mix add smix add mul i 2 1
642j = ulong 0
643if eq k 4
644j = mk_rn i
645end
646mk_date get mk_g MKG_T
647mk_from eq k 3
648mk_t mkt_hto
649if eq mk_rn 4 0
650mk_cc
651end
652mk_hsubj i k j
653mk_mid mkt_hmid i
654if eq k 4
655mk_mid mkt_hirt j
656mk_mid mkt_hrefs j
657end
658mk_body i k
659end
660
661note ---- files ----
662note mk_errno: one stderr line: t, the decimal of -e, ": ", the path in gp
663fn mk_errno t view uchar * e long to void
664b = new uchar 24 0
665x = ulong 0
666if lt e 0
667x = ulong neg e
668end
669i = ulong 24
670while true bound 24
671i = sub i 1
672put b i uchar add 48 rem x 10
673x = div x 10
674if eq x 0
675break
676end
677end
678r = write 2 t 0 len t
679r = write 2 b i sub 24 i
680r = write 2 mkt_colsp 0 len mkt_colsp
681r = write 2 gp 0 get gv VPL
682r = write 2 mkt_nl 0 1
683end
684
685fn mk_err t view uchar * to void
686r = write 2 t 0 len t
687r = write 2 mkt_nl 0 1
688end
689
690fn mk_proot to void
691p_reset
692p_s S_AUX 0 s_len S_AUX
693end
694
695note mk_mkroot: ROOT, ROOT/cur, ROOT/new, ROOT/tmp; 0, 1 (unusable path or ROOT exists), 6 (I/O)
696fn mk_mkroot to int
697mk_proot
698if not p_ok
699mk_err mkt_epath
700ret 1
701end
702r = p_mkdir
703if lt r 0
704mk_errno mkt_eroot r
705if eq r -17
706ret 1
707end
708ret 6
709end
710for k ulong 3
711mk_proot
712if eq k 0
713p_t mkt_cur
714end
715if eq k 1
716p_t mkt_new
717end
718if eq k 2
719p_t mkt_tmp
720end
721r = p_mkdir
722if lt r 0
723mk_errno mkt_eroot r
724ret 6
725end
726end
727ret 0
728end
729
730note mk_open: create (exclusively, never through a symlink) the file of message i with epoch t
731fn mk_open i ulong t ulong to long
732mk_proot
733if eq rem i 3 0
734p_t mkt_new
735else
736p_t mkt_cur
737end
738p_c 47
739p_u t
740p_t mkt_m
741p_u i
742p_c 80
743p_u get mk_g MKG_SEED
744p_t mkt_host
745if ne rem i 3 0
746p_t mkt_flag
747end
748if not p_ok
749ret -36
750end
751ret fopen gp 0 get gv VPL OEXCL MK_FMODE
752end
753
754note mk_one: write message i; 0 or 6 (I/O, stderr names errno and path; a partial file is removed)
755fn mk_one i ulong to int
756x = mk_mix add get mk_g MKG_SMIX add mul i 2 1
757t = add add 1735689600 mul i 600 rem x 600
758fd = mk_open i t
759if lt fd 0
760mk_errno mkt_ecreate fd
761ret 6
762end
763put mk_g MKG_FD add ulong fd 1
764put mk_g MKG_WN 0
765put mk_g MKG_FB 0
766put mk_g MKG_T t
767mk_msg i
768z = mk_flush
769c = fclose int fd
770put mk_g MKG_FD 0
771if lt c 0
772mk_fail c
773end
774if ne get mk_g MKG_FAIL 0
775zu = unlink gp 0 get gv VPL
776mk_errno mkt_ewrite neg long get mk_g MKG_ERRNO
777ret 6
778end
779put mk_g MKG_TB add get mk_g MKG_TB get mk_g MKG_FB
780k = add MKG_KIND rem i 5
781put mk_g k add get mk_g k 1
782if eq rem i 3 0
783put mk_g MKG_NEW add get mk_g MKG_NEW 1
784else
785put mk_g MKG_CUR add get mk_g MKG_CUR 1
786end
787ret 0
788end
789
790note mk_gen: the Maildir at the path in store S_AUX with n messages from seed (layout at the top);
791note 0, 1 (unusable path or the root exists), 6 (I/O). Totals stay in mk_g for mk_osum.
792fn mk_gen n ulong seed ulong to int
793for x ulong 32
794put mk_g x 0
795end
796put mk_g MKG_SEED seed
797put mk_g MKG_N n
798put mk_g MKG_SMIX mk_mix add seed 11400714819323198485
799r = mk_mkroot
800if ne r 0
801ret r
802end
803for i ulong n bound 9223372036854775807
804r = mk_one i
805if ne r 0
806ret r
807end
808end
809ret 0
810end
811
812note mk_bytes: total bytes of the messages written by the last mk_gen
813fn mk_bytes to ulong
814ret get mk_g MKG_TB
815end
816
817note ---- report ----
818fn mk_okv t view uchar * v ulong to void
819o_raw t
820o_digits v
821end
822
823note mk_osum: "N=.. seed=.. bytes=.. new=.. cur=.. plain=.. multipart=.. qp=.. encoded_subject=.. reply=.."
824fn mk_osum to void
825mk_okv mkt_kn get mk_g MKG_N
826mk_okv mkt_kseed get mk_g MKG_SEED
827mk_okv mkt_kbytes get mk_g MKG_TB
828mk_okv mkt_knew get mk_g MKG_NEW
829mk_okv mkt_kcur get mk_g MKG_CUR
830mk_okv mkt_kplain get mk_g MKG_KIND
831mk_okv mkt_kmulti get mk_g add MKG_KIND 1
832mk_okv mkt_kqp get mk_g add MKG_KIND 2
833mk_okv mkt_kenc get mk_g add MKG_KIND 3
834mk_okv mkt_kreply get mk_g add MKG_KIND 4
835end
836
837note ---- arguments ----
838note mk_argnum: argument idx as a decimal ulong; mk_g cell 30 becomes 1 when it is empty, holds a
839note byte other than 0-9, or is above 18446744073709551615 (the ulong range, a machine boundary)
840fn mk_argnum idx ulong to ulong
841b = new uchar 131072 0
842k = argread idx b 0 131072
843if le k 0
844put mk_g MKG_BAD 1
845ret 0
846end
847v = ulong 0
848for i ulong ulong k bound 131072
849c = get b i
850if or lt c 48 gt c 57
851put mk_g MKG_BAD 1
852ret 0
853end
854dg = ulong sub c 48
855if gt v 1844674407370955161
856put mk_g MKG_BAD 1
857ret 0
858end
859if eq v 1844674407370955161
860if gt dg 5
861put mk_g MKG_BAD 1
862ret 0
863end
864end
865v = add mul v 10 dg
866end
867ret v
868end
869
870note ---- exact rates ----
871note mk_digits: the decimal digits (values 0..9, most significant first) of a into da; the count
872fn mk_digits a ulong da edit uchar 24 to ulong
873t = new uchar 24 0
874x = a
875n = ulong 0
876while true bound 24
877put t n uchar rem x 10
878x = div x 10
879n = add n 1
880if eq x 0
881break
882end
883end
884for i ulong n bound 24
885put da i get t sub sub n i 1
886end
887ret n
888end
889
890note bn_ratio: a * 10^k / b printed with d decimals, truncated (not rounded). Exact schoolbook long
891note division over the digits of a followed by k + d zeros: the remainder r < b, so each step's
892note r*10 + 9 <= 10b - 1 fits a ulong when b <= 1844674407370955161 = floor((2^64-1)/10) (ns: 58 years).
893note b = 0 or b above that prints "n/a" (a machine boundary of 64-bit arithmetic, never a wrong number).
894note messages/s = N * 10^9 / wall_ns (k 9); decimal MB/s = bytes * 10^3 / wall_ns (k 3), MB = 10^6 bytes.
895fn bn_ratio a ulong k ulong b ulong d ulong to void
896if or eq b 0 gt b 1844674407370955161
897o_raw mkt_na
898ret
899end
900da = new uchar 24 0
901na = mk_digits a da
902ip = add na k
903bn_ldiv da na ip add ip d b
904end
905
906note bn_ldiv: the quotient digits of (digits da[0, na) then zeros) / b, positions 0..tot-1; a "."
907note before position ip; leading zeros of the integer part dropped (one "0" kept)
908fn bn_ldiv da view uchar 24 na ulong ip ulong tot ulong b ulong to void
909r = ulong 0
910lead = true
911for i ulong tot bound 9223372036854775807
912if eq i ip
913if lead
914o_b 48
915lead = false
916end
917o_b 46
918end
919dg = ulong 0
920if lt i na
921dg = ulong get da i
922end
923r = add mul r 10 dg
924q = div r b
925r = rem r b
926if lt i ip
927if or ne q 0 not lead
928o_b uchar add 48 q
929lead = false
930end
931else
932o_b uchar add 48 q
933end
934end
935if lead
936o_b 48
937end
938end