-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOK.h
124 lines (107 loc) · 3.67 KB
/
OK.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef LIBRDX_OK_H
#define LIBRDX_OK_H
#include <errno.h>
#include <stdint.h>
#include "$.h"
#include "01.h"
typedef uint64_t ok64;
typedef ok64 ok;
#define OK 0
#define FAIL 0xffffffffffffffffUL
typedef u64 u60;
#define u60max ((1UL << 60) - 1)
con ok64 FAILsanity = 0x3ca495de5cade3d;
con ok64 notimplyet = 0xcb3e2dc74c3da78;
con ok64 OKnoroom = 0x614cb3db3cf1;
con ok64 OKbadtext = 0x18526968e29f38;
con ok64 noroom = 0xcb3db3cf1;
con ok64 badarg = 0x9a5a25dab;
con ok64 faileq = 0xaa5b70a75;
con ok64 FAILeq = 0x3ca495a75;
con char *_base_ron64 =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~";
con u8 BASEron64rev[256] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0xff, 0xff, 0xff, 0xff, 0x24,
0xff, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x3c, 0x3d, 0x3e, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
};
// todo thread local
static char _ok64_tmp[16];
fun ok64 RONfeed64(u8 **into, ok64 val) {
u8 tmp[11];
u8 *e = tmp + 11, *t = e;
do {
*--t = _base_ron64[val & 63];
val >>= 6;
} while (val);
size_t l = e - t;
if ($len(into) < l) return $noroom;
memcpy(*into, t, l);
*into += l;
return OK;
}
fun ok64 OKprint(ok64 o, uint8_t **into) {
if (o == 0) {
if (into[1] < into[0] + 2) return OKnoroom;
**into = 'O';
++*into;
**into = 'K';
++*into;
return OK;
} else {
return RONfeed64(into, o);
}
}
fun ok64 RONdrain64(ok64 *o, u8c **from) {
ok64 res = 0;
for (uint8_t const *p = from[0]; p < from[1]; ++p) {
uint64_t v = BASEron64rev[*p];
if (v == 0xff) return OKbadtext;
res = (res << 6) | v;
}
*o = res;
return OK;
}
fun ok64 OKscan(ok64 *o, uint8_t const **from) {
ok64 oo = RONdrain64(o, from);
if (oo == OK && *o == 0x518) *o = 0;
return oo;
}
fun const char *ok64str(ok64 o) {
char *tmp[2] = {_ok64_tmp, _ok64_tmp + sizeof(_ok64_tmp)};
OKprint(o, (uint8_t **)tmp);
**tmp = 0;
return _ok64_tmp;
}
fun const char *okstr(ok64 o) { return ok64str(o); }
fun int ok64is(ok64 val, ok64 root) {
u8 bits = 64 - clz64(root);
u64 mask = (1UL << bits) - 1;
return (val & mask) == root;
}
fun ok64 errnok() {
ok64 e = errno;
e &= 63;
e <<= 54;
return e;
}
#endif