pyscriptc 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyscriptc/__init__.py +3 -0
- pyscriptc/__main__.py +3 -0
- pyscriptc/_posthog_token.py +1 -0
- pyscriptc/builtins.py +156 -0
- pyscriptc/canonical.py +167 -0
- pyscriptc/check.py +49 -0
- pyscriptc/cli.py +271 -0
- pyscriptc/coverage.py +388 -0
- pyscriptc/debug.py +120 -0
- pyscriptc/diagnostics.py +179 -0
- pyscriptc/errors.py +56 -0
- pyscriptc/ir.py +365 -0
- pyscriptc/llvm.py +2129 -0
- pyscriptc/log.py +127 -0
- pyscriptc/lower/__init__.py +1071 -0
- pyscriptc/lower/_calls.py +1499 -0
- pyscriptc/lower/_classes.py +969 -0
- pyscriptc/lower/_core.py +301 -0
- pyscriptc/lower/_expr.py +886 -0
- pyscriptc/lower/_stdlib.py +491 -0
- pyscriptc/lower/_stdlib_shared.py +93 -0
- pyscriptc/lower/_stmt.py +1173 -0
- pyscriptc/package.py +249 -0
- pyscriptc/restlower.py +1008 -0
- pyscriptc/runtime/ps_argv.c +28 -0
- pyscriptc/runtime/ps_b64.c +168 -0
- pyscriptc/runtime/ps_coll.c +133 -0
- pyscriptc/runtime/ps_dt.c +388 -0
- pyscriptc/runtime/ps_hash.c +411 -0
- pyscriptc/runtime/ps_help.c +219 -0
- pyscriptc/runtime/ps_hmac.c +120 -0
- pyscriptc/runtime/ps_io.c +198 -0
- pyscriptc/runtime/ps_island.c +272 -0
- pyscriptc/runtime/ps_json.c +402 -0
- pyscriptc/runtime/ps_mix.c +47 -0
- pyscriptc/runtime/ps_more.c +1089 -0
- pyscriptc/runtime/ps_osmore.c +447 -0
- pyscriptc/runtime/ps_path.c +547 -0
- pyscriptc/runtime/ps_re.c +1102 -0
- pyscriptc/runtime/ps_rt.c +1175 -0
- pyscriptc/runtime/ps_rt.h +189 -0
- pyscriptc/runtime/ps_struct.c +291 -0
- pyscriptc/runtime/ps_time.c +207 -0
- pyscriptc/runtime/ps_unicode.c +1521 -0
- pyscriptc/runtime/ps_url.c +327 -0
- pyscriptc/stdlib_queue.py +191 -0
- pyscriptc/stdmore.py +1596 -0
- pyscriptc/telemetry.py +219 -0
- pyscriptc-0.1.0.dist-info/METADATA +187 -0
- pyscriptc-0.1.0.dist-info/RECORD +54 -0
- pyscriptc-0.1.0.dist-info/WHEEL +5 -0
- pyscriptc-0.1.0.dist-info/entry_points.txt +2 -0
- pyscriptc-0.1.0.dist-info/licenses/LICENSE +201 -0
- pyscriptc-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/* md5, sha1, sha256, sha384, and sha512. No OpenSSL. The result is an owned
|
|
2
|
+
digest buffer; hexdigest formats it. */
|
|
3
|
+
|
|
4
|
+
#include "ps_rt.h"
|
|
5
|
+
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
static uint32_t ps_rotr32(uint32_t x, int n) { return (x >> n) | (x << (32 - n)); }
|
|
10
|
+
static uint64_t ps_rotr64(uint64_t x, int n) { return (x >> n) | (x << (64 - n)); }
|
|
11
|
+
static uint32_t ps_rotl32(uint32_t x, int n) { return (x << n) | (x >> (32 - n)); }
|
|
12
|
+
|
|
13
|
+
static uint32_t ps_be32(const uint8_t *p) {
|
|
14
|
+
return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static uint32_t ps_le32(const uint8_t *p) {
|
|
18
|
+
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* ps_be64 needs the low half without recursion. */
|
|
22
|
+
static uint64_t ps_load_be64(const uint8_t *p) {
|
|
23
|
+
return ((uint64_t)ps_be32(p) << 32) | (uint64_t)ps_be32(p + 4);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static void ps_store_be32(uint8_t *p, uint32_t v) {
|
|
27
|
+
p[0] = (uint8_t)(v >> 24);
|
|
28
|
+
p[1] = (uint8_t)(v >> 16);
|
|
29
|
+
p[2] = (uint8_t)(v >> 8);
|
|
30
|
+
p[3] = (uint8_t)v;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static void ps_store_be64(uint8_t *p, uint64_t v) {
|
|
34
|
+
ps_store_be32(p, (uint32_t)(v >> 32));
|
|
35
|
+
ps_store_be32(p + 4, (uint32_t)v);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static void ps_store_le32(uint8_t *p, uint32_t v) {
|
|
39
|
+
p[0] = (uint8_t)v;
|
|
40
|
+
p[1] = (uint8_t)(v >> 8);
|
|
41
|
+
p[2] = (uint8_t)(v >> 16);
|
|
42
|
+
p[3] = (uint8_t)(v >> 24);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static PsBuf *ps_digest(const uint8_t *bytes, int64_t n) {
|
|
46
|
+
PsBuf *box = ps_alloc((int64_t)sizeof(PsBuf));
|
|
47
|
+
ps_buf_copy(box, (const char *)bytes, n);
|
|
48
|
+
return box;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
static void ps_md5_block(uint32_t h[4], const uint8_t block[64]) {
|
|
52
|
+
static const int s[64] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9,
|
|
53
|
+
14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
|
54
|
+
4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
|
|
55
|
+
uint32_t m[16];
|
|
56
|
+
for (int i = 0; i < 16; i++) {
|
|
57
|
+
m[i] = ps_le32(block + (size_t)i * 4);
|
|
58
|
+
}
|
|
59
|
+
uint32_t a = h[0];
|
|
60
|
+
uint32_t b = h[1];
|
|
61
|
+
uint32_t c = h[2];
|
|
62
|
+
uint32_t d = h[3];
|
|
63
|
+
for (int i = 0; i < 64; i++) {
|
|
64
|
+
uint32_t f;
|
|
65
|
+
int g;
|
|
66
|
+
if (i < 16) {
|
|
67
|
+
f = (b & c) | (~b & d);
|
|
68
|
+
g = i;
|
|
69
|
+
} else if (i < 32) {
|
|
70
|
+
f = (d & b) | (~d & c);
|
|
71
|
+
g = (5 * i + 1) % 16;
|
|
72
|
+
} else if (i < 48) {
|
|
73
|
+
f = b ^ c ^ d;
|
|
74
|
+
g = (3 * i + 5) % 16;
|
|
75
|
+
} else {
|
|
76
|
+
f = c ^ (b | ~d);
|
|
77
|
+
g = (7 * i) % 16;
|
|
78
|
+
}
|
|
79
|
+
double sine = __builtin_sin((double)(i + 1));
|
|
80
|
+
if (sine < 0) {
|
|
81
|
+
sine = -sine;
|
|
82
|
+
}
|
|
83
|
+
uint32_t t = (uint32_t)(sine * 4294967296.0);
|
|
84
|
+
uint32_t next = b + ps_rotl32(a + f + t + m[g], s[i]);
|
|
85
|
+
a = d;
|
|
86
|
+
d = c;
|
|
87
|
+
c = b;
|
|
88
|
+
b = next;
|
|
89
|
+
}
|
|
90
|
+
h[0] += a;
|
|
91
|
+
h[1] += b;
|
|
92
|
+
h[2] += c;
|
|
93
|
+
h[3] += d;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
static PsBuf *ps_md5_of(const char *p, int64_t n) {
|
|
97
|
+
uint32_t h[4] = {0x67452301u, 0xefcdab89u, 0x98badcfeu, 0x10325476u};
|
|
98
|
+
uint8_t block[64];
|
|
99
|
+
int64_t off = 0;
|
|
100
|
+
while (off + 64 <= n) {
|
|
101
|
+
ps_md5_block(h, (const uint8_t *)p + off);
|
|
102
|
+
off += 64;
|
|
103
|
+
}
|
|
104
|
+
int64_t rem = n - off;
|
|
105
|
+
memset(block, 0, 64);
|
|
106
|
+
if (rem > 0) {
|
|
107
|
+
memcpy(block, p + off, (size_t)rem);
|
|
108
|
+
}
|
|
109
|
+
block[rem] = 0x80;
|
|
110
|
+
if (rem >= 56) {
|
|
111
|
+
ps_md5_block(h, block);
|
|
112
|
+
memset(block, 0, 64);
|
|
113
|
+
}
|
|
114
|
+
uint64_t bits = (uint64_t)n * 8;
|
|
115
|
+
for (int i = 0; i < 8; i++) {
|
|
116
|
+
block[56 + i] = (uint8_t)(bits >> (8 * i));
|
|
117
|
+
}
|
|
118
|
+
ps_md5_block(h, block);
|
|
119
|
+
uint8_t out[16];
|
|
120
|
+
for (int i = 0; i < 4; i++) {
|
|
121
|
+
ps_store_le32(out + i * 4, h[i]);
|
|
122
|
+
}
|
|
123
|
+
return ps_digest(out, 16);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
static void ps_sha1_block(uint32_t h[5], const uint8_t block[64]) {
|
|
127
|
+
uint32_t w[80];
|
|
128
|
+
for (int i = 0; i < 16; i++) {
|
|
129
|
+
w[i] = ps_be32(block + (size_t)i * 4);
|
|
130
|
+
}
|
|
131
|
+
for (int i = 16; i < 80; i++) {
|
|
132
|
+
w[i] = ps_rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
|
|
133
|
+
}
|
|
134
|
+
uint32_t a = h[0];
|
|
135
|
+
uint32_t b = h[1];
|
|
136
|
+
uint32_t c = h[2];
|
|
137
|
+
uint32_t d = h[3];
|
|
138
|
+
uint32_t e = h[4];
|
|
139
|
+
for (int i = 0; i < 80; i++) {
|
|
140
|
+
uint32_t f;
|
|
141
|
+
uint32_t k;
|
|
142
|
+
if (i < 20) {
|
|
143
|
+
f = (b & c) | (~b & d);
|
|
144
|
+
k = 0x5a827999u;
|
|
145
|
+
} else if (i < 40) {
|
|
146
|
+
f = b ^ c ^ d;
|
|
147
|
+
k = 0x6ed9eba1u;
|
|
148
|
+
} else if (i < 60) {
|
|
149
|
+
f = (b & c) | (b & d) | (c & d);
|
|
150
|
+
k = 0x8f1bbcdcu;
|
|
151
|
+
} else {
|
|
152
|
+
f = b ^ c ^ d;
|
|
153
|
+
k = 0xca62c1d6u;
|
|
154
|
+
}
|
|
155
|
+
uint32_t temp = ps_rotl32(a, 5) + f + e + k + w[i];
|
|
156
|
+
e = d;
|
|
157
|
+
d = c;
|
|
158
|
+
c = ps_rotl32(b, 30);
|
|
159
|
+
b = a;
|
|
160
|
+
a = temp;
|
|
161
|
+
}
|
|
162
|
+
h[0] += a;
|
|
163
|
+
h[1] += b;
|
|
164
|
+
h[2] += c;
|
|
165
|
+
h[3] += d;
|
|
166
|
+
h[4] += e;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static PsBuf *ps_sha1_of(const char *p, int64_t n) {
|
|
170
|
+
uint32_t h[5] = {0x67452301u, 0xefcdab89u, 0x98badcfeu, 0x10325476u, 0xc3d2e1f0u};
|
|
171
|
+
uint8_t block[64];
|
|
172
|
+
int64_t off = 0;
|
|
173
|
+
while (off + 64 <= n) {
|
|
174
|
+
ps_sha1_block(h, (const uint8_t *)p + off);
|
|
175
|
+
off += 64;
|
|
176
|
+
}
|
|
177
|
+
int64_t rem = n - off;
|
|
178
|
+
memset(block, 0, 64);
|
|
179
|
+
if (rem > 0) {
|
|
180
|
+
memcpy(block, p + off, (size_t)rem);
|
|
181
|
+
}
|
|
182
|
+
block[rem] = 0x80;
|
|
183
|
+
if (rem >= 56) {
|
|
184
|
+
ps_sha1_block(h, block);
|
|
185
|
+
memset(block, 0, 64);
|
|
186
|
+
}
|
|
187
|
+
uint64_t bits = (uint64_t)n * 8;
|
|
188
|
+
for (int i = 0; i < 8; i++) {
|
|
189
|
+
block[56 + i] = (uint8_t)(bits >> (8 * (7 - i)));
|
|
190
|
+
}
|
|
191
|
+
ps_sha1_block(h, block);
|
|
192
|
+
uint8_t out[20];
|
|
193
|
+
for (int i = 0; i < 5; i++) {
|
|
194
|
+
ps_store_be32(out + i * 4, h[i]);
|
|
195
|
+
}
|
|
196
|
+
return ps_digest(out, 20);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
static const uint32_t PS_K256[64] = {
|
|
200
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
|
|
201
|
+
0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
|
202
|
+
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
|
|
203
|
+
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
204
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08,
|
|
205
|
+
0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
|
206
|
+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
|
|
207
|
+
|
|
208
|
+
static void ps_sha256_block(uint32_t st[8], const uint8_t block[64]) {
|
|
209
|
+
uint32_t w[64];
|
|
210
|
+
for (int i = 0; i < 16; i++) {
|
|
211
|
+
w[i] = ps_be32(block + (size_t)i * 4);
|
|
212
|
+
}
|
|
213
|
+
for (int i = 16; i < 64; i++) {
|
|
214
|
+
uint32_t s0 = ps_rotr32(w[i - 15], 7) ^ ps_rotr32(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
|
215
|
+
uint32_t s1 = ps_rotr32(w[i - 2], 17) ^ ps_rotr32(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
|
216
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
|
217
|
+
}
|
|
218
|
+
uint32_t a = st[0];
|
|
219
|
+
uint32_t b = st[1];
|
|
220
|
+
uint32_t c = st[2];
|
|
221
|
+
uint32_t d = st[3];
|
|
222
|
+
uint32_t e = st[4];
|
|
223
|
+
uint32_t f = st[5];
|
|
224
|
+
uint32_t g = st[6];
|
|
225
|
+
uint32_t hh = st[7];
|
|
226
|
+
for (int i = 0; i < 64; i++) {
|
|
227
|
+
uint32_t S1 = ps_rotr32(e, 6) ^ ps_rotr32(e, 11) ^ ps_rotr32(e, 25);
|
|
228
|
+
uint32_t ch = (e & f) ^ (~e & g);
|
|
229
|
+
uint32_t t1 = hh + S1 + ch + PS_K256[i] + w[i];
|
|
230
|
+
uint32_t S0 = ps_rotr32(a, 2) ^ ps_rotr32(a, 13) ^ ps_rotr32(a, 22);
|
|
231
|
+
uint32_t maj = (a & b) ^ (a & c) ^ (b & c);
|
|
232
|
+
uint32_t t2 = S0 + maj;
|
|
233
|
+
hh = g;
|
|
234
|
+
g = f;
|
|
235
|
+
f = e;
|
|
236
|
+
e = d + t1;
|
|
237
|
+
d = c;
|
|
238
|
+
c = b;
|
|
239
|
+
b = a;
|
|
240
|
+
a = t1 + t2;
|
|
241
|
+
}
|
|
242
|
+
st[0] += a;
|
|
243
|
+
st[1] += b;
|
|
244
|
+
st[2] += c;
|
|
245
|
+
st[3] += d;
|
|
246
|
+
st[4] += e;
|
|
247
|
+
st[5] += f;
|
|
248
|
+
st[6] += g;
|
|
249
|
+
st[7] += hh;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
static PsBuf *ps_sha256_of(const char *p, int64_t n) {
|
|
253
|
+
uint32_t st[8] = {0x6a09e667u, 0xbb67ae85u, 0x3c6ef372u, 0xa54ff53au, 0x510e527fu, 0x9b05688cu, 0x1f83d9abu, 0x5be0cd19u};
|
|
254
|
+
uint8_t block[64];
|
|
255
|
+
int64_t off = 0;
|
|
256
|
+
while (off + 64 <= n) {
|
|
257
|
+
ps_sha256_block(st, (const uint8_t *)p + off);
|
|
258
|
+
off += 64;
|
|
259
|
+
}
|
|
260
|
+
int64_t rem = n - off;
|
|
261
|
+
memset(block, 0, 64);
|
|
262
|
+
if (rem > 0) {
|
|
263
|
+
memcpy(block, p + off, (size_t)rem);
|
|
264
|
+
}
|
|
265
|
+
block[rem] = 0x80;
|
|
266
|
+
if (rem >= 56) {
|
|
267
|
+
ps_sha256_block(st, block);
|
|
268
|
+
memset(block, 0, 64);
|
|
269
|
+
}
|
|
270
|
+
uint64_t bits = (uint64_t)n * 8;
|
|
271
|
+
for (int i = 0; i < 8; i++) {
|
|
272
|
+
block[56 + i] = (uint8_t)(bits >> (8 * (7 - i)));
|
|
273
|
+
}
|
|
274
|
+
ps_sha256_block(st, block);
|
|
275
|
+
uint8_t out[32];
|
|
276
|
+
for (int i = 0; i < 8; i++) {
|
|
277
|
+
ps_store_be32(out + i * 4, st[i]);
|
|
278
|
+
}
|
|
279
|
+
return ps_digest(out, 32);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
static const uint64_t PS_K512[80] = {
|
|
283
|
+
0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL,
|
|
284
|
+
0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL,
|
|
285
|
+
0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL,
|
|
286
|
+
0xc19bf174cf692694ULL, 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
|
|
287
|
+
0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 0x983e5152ee66dfabULL,
|
|
288
|
+
0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL,
|
|
289
|
+
0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL,
|
|
290
|
+
0x53380d139d95b3dfULL, 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
|
|
291
|
+
0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL,
|
|
292
|
+
0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL,
|
|
293
|
+
0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL,
|
|
294
|
+
0x682e6ff3d6b2b8a3ULL, 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
|
|
295
|
+
0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, 0xca273eceea26619cULL,
|
|
296
|
+
0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL,
|
|
297
|
+
0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL,
|
|
298
|
+
0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL};
|
|
299
|
+
|
|
300
|
+
static void ps_sha512_block(uint64_t st[8], const uint8_t block[128]) {
|
|
301
|
+
uint64_t w[80];
|
|
302
|
+
for (int i = 0; i < 16; i++) {
|
|
303
|
+
w[i] = ps_load_be64(block + (size_t)i * 8);
|
|
304
|
+
}
|
|
305
|
+
for (int i = 16; i < 80; i++) {
|
|
306
|
+
uint64_t s0 = ps_rotr64(w[i - 15], 1) ^ ps_rotr64(w[i - 15], 8) ^ (w[i - 15] >> 7);
|
|
307
|
+
uint64_t s1 = ps_rotr64(w[i - 2], 19) ^ ps_rotr64(w[i - 2], 61) ^ (w[i - 2] >> 6);
|
|
308
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
|
309
|
+
}
|
|
310
|
+
uint64_t a = st[0];
|
|
311
|
+
uint64_t b = st[1];
|
|
312
|
+
uint64_t c = st[2];
|
|
313
|
+
uint64_t d = st[3];
|
|
314
|
+
uint64_t e = st[4];
|
|
315
|
+
uint64_t f = st[5];
|
|
316
|
+
uint64_t g = st[6];
|
|
317
|
+
uint64_t hh = st[7];
|
|
318
|
+
for (int i = 0; i < 80; i++) {
|
|
319
|
+
uint64_t S1 = ps_rotr64(e, 14) ^ ps_rotr64(e, 18) ^ ps_rotr64(e, 41);
|
|
320
|
+
uint64_t ch = (e & f) ^ (~e & g);
|
|
321
|
+
uint64_t t1 = hh + S1 + ch + PS_K512[i] + w[i];
|
|
322
|
+
uint64_t S0 = ps_rotr64(a, 28) ^ ps_rotr64(a, 34) ^ ps_rotr64(a, 39);
|
|
323
|
+
uint64_t maj = (a & b) ^ (a & c) ^ (b & c);
|
|
324
|
+
uint64_t t2 = S0 + maj;
|
|
325
|
+
hh = g;
|
|
326
|
+
g = f;
|
|
327
|
+
f = e;
|
|
328
|
+
e = d + t1;
|
|
329
|
+
d = c;
|
|
330
|
+
c = b;
|
|
331
|
+
b = a;
|
|
332
|
+
a = t1 + t2;
|
|
333
|
+
}
|
|
334
|
+
st[0] += a;
|
|
335
|
+
st[1] += b;
|
|
336
|
+
st[2] += c;
|
|
337
|
+
st[3] += d;
|
|
338
|
+
st[4] += e;
|
|
339
|
+
st[5] += f;
|
|
340
|
+
st[6] += g;
|
|
341
|
+
st[7] += hh;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
static void ps_sha512_pad(uint64_t st[8], const char *p, int64_t n) {
|
|
345
|
+
uint8_t block[128];
|
|
346
|
+
int64_t off = 0;
|
|
347
|
+
while (off + 128 <= n) {
|
|
348
|
+
ps_sha512_block(st, (const uint8_t *)p + off);
|
|
349
|
+
off += 128;
|
|
350
|
+
}
|
|
351
|
+
int64_t rem = n - off;
|
|
352
|
+
memset(block, 0, 128);
|
|
353
|
+
if (rem > 0) {
|
|
354
|
+
memcpy(block, p + off, (size_t)rem);
|
|
355
|
+
}
|
|
356
|
+
block[rem] = 0x80;
|
|
357
|
+
if (rem >= 112) {
|
|
358
|
+
ps_sha512_block(st, block);
|
|
359
|
+
memset(block, 0, 128);
|
|
360
|
+
}
|
|
361
|
+
uint64_t bits = (uint64_t)n * 8;
|
|
362
|
+
ps_store_be64(block + 112, 0);
|
|
363
|
+
ps_store_be64(block + 120, bits);
|
|
364
|
+
ps_sha512_block(st, block);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
static PsBuf *ps_sha512_of(const char *p, int64_t n) {
|
|
368
|
+
uint64_t st[8] = {0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
|
|
369
|
+
0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL};
|
|
370
|
+
ps_sha512_pad(st, p, n);
|
|
371
|
+
uint8_t out[64];
|
|
372
|
+
for (int i = 0; i < 8; i++) {
|
|
373
|
+
ps_store_be64(out + i * 8, st[i]);
|
|
374
|
+
}
|
|
375
|
+
return ps_digest(out, 64);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
static PsBuf *ps_sha384_of(const char *p, int64_t n) {
|
|
379
|
+
uint64_t st[8] = {0xcbbb9d5dc1059ed8ULL, 0x629a292a367cd507ULL, 0x9159015a3070dd17ULL, 0x152fecd8f70e5939ULL,
|
|
380
|
+
0x67332667ffc00b31ULL, 0x8eb44a8768581511ULL, 0xdb0c2e0d64f98fa7ULL, 0x47b5481dbefa4fa4ULL};
|
|
381
|
+
ps_sha512_pad(st, p, n);
|
|
382
|
+
uint8_t out[48];
|
|
383
|
+
for (int i = 0; i < 6; i++) {
|
|
384
|
+
ps_store_be64(out + i * 8, st[i]);
|
|
385
|
+
}
|
|
386
|
+
return ps_digest(out, 48);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
PsBuf *ps_hash_md5(int64_t n, char *p) { return ps_md5_of(p, n); }
|
|
390
|
+
PsBuf *ps_hash_sha1(int64_t n, char *p) { return ps_sha1_of(p, n); }
|
|
391
|
+
PsBuf *ps_hash_sha256(int64_t n, char *p) { return ps_sha256_of(p, n); }
|
|
392
|
+
PsBuf *ps_hash_sha384(int64_t n, char *p) { return ps_sha384_of(p, n); }
|
|
393
|
+
PsBuf *ps_hash_sha512(int64_t n, char *p) { return ps_sha512_of(p, n); }
|
|
394
|
+
|
|
395
|
+
void ps_hash_hex(PsBuf *h, PsBuf *out) {
|
|
396
|
+
static const char *hexd = "0123456789abcdef";
|
|
397
|
+
char *q = malloc((size_t)h->len * 2 + 1);
|
|
398
|
+
if (q == NULL) {
|
|
399
|
+
abort();
|
|
400
|
+
}
|
|
401
|
+
for (int64_t i = 0; i < h->len; i++) {
|
|
402
|
+
unsigned char c = (unsigned char)h->data[i];
|
|
403
|
+
q[i * 2] = hexd[c >> 4];
|
|
404
|
+
q[i * 2 + 1] = hexd[c & 15];
|
|
405
|
+
}
|
|
406
|
+
q[h->len * 2] = '\0';
|
|
407
|
+
out->len = h->len * 2;
|
|
408
|
+
out->data = q;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
void ps_hash_digest(PsBuf *h, PsBuf *out) { ps_buf_copy(out, h->data, h->len); }
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
/* Object helpers that are easier in C than in the LLVM text: abs, sum,
|
|
2
|
+
sorted, ord, chr, round, int(bytes), warnings.warn, and lexicographic i64
|
|
3
|
+
compare.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#include "ps_rt.h"
|
|
7
|
+
|
|
8
|
+
#include <math.h>
|
|
9
|
+
#include <stdio.h>
|
|
10
|
+
#include <stdlib.h>
|
|
11
|
+
#include <string.h>
|
|
12
|
+
|
|
13
|
+
int32_t ps_i64_lex(const int64_t *a, const int64_t *b, int64_t n) {
|
|
14
|
+
int64_t i;
|
|
15
|
+
for (i = 0; i < n; i++) {
|
|
16
|
+
if (a[i] < b[i]) {
|
|
17
|
+
return -1;
|
|
18
|
+
}
|
|
19
|
+
if (a[i] > b[i]) {
|
|
20
|
+
return 1;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
int64_t ps_abs_i64(int64_t x) {
|
|
27
|
+
if (x == INT64_MIN) {
|
|
28
|
+
__builtin_trap();
|
|
29
|
+
}
|
|
30
|
+
return x < 0 ? -x : x;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
double ps_abs_f64(double x) { return fabs(x); }
|
|
34
|
+
|
|
35
|
+
int64_t ps_sum_i64(PsSeq *s) {
|
|
36
|
+
int64_t acc = 0;
|
|
37
|
+
int64_t i;
|
|
38
|
+
for (i = 0; i < s->len; i++) {
|
|
39
|
+
int64_t item;
|
|
40
|
+
memcpy(&item, (char *)s->data + (size_t)i * (size_t)s->elem, sizeof item);
|
|
41
|
+
if (__builtin_add_overflow(acc, item, &acc)) {
|
|
42
|
+
__builtin_trap();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return acc;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
double ps_sum_f64(PsSeq *s) {
|
|
49
|
+
double acc = 0;
|
|
50
|
+
int64_t i;
|
|
51
|
+
for (i = 0; i < s->len; i++) {
|
|
52
|
+
double item;
|
|
53
|
+
memcpy(&item, (char *)s->data + (size_t)i * (size_t)s->elem, sizeof item);
|
|
54
|
+
acc += item;
|
|
55
|
+
}
|
|
56
|
+
return acc;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static int ps_cmp_i64(const void *a, const void *b) {
|
|
60
|
+
int64_t xa;
|
|
61
|
+
int64_t xb;
|
|
62
|
+
memcpy(&xa, a, sizeof xa);
|
|
63
|
+
memcpy(&xb, b, sizeof xb);
|
|
64
|
+
if (xa < xb) {
|
|
65
|
+
return -1;
|
|
66
|
+
}
|
|
67
|
+
if (xa > xb) {
|
|
68
|
+
return 1;
|
|
69
|
+
}
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static int ps_cmp_f64(const void *a, const void *b) {
|
|
74
|
+
double xa;
|
|
75
|
+
double xb;
|
|
76
|
+
memcpy(&xa, a, sizeof xa);
|
|
77
|
+
memcpy(&xb, b, sizeof xb);
|
|
78
|
+
if (isnan(xa) && isnan(xb)) {
|
|
79
|
+
return 0;
|
|
80
|
+
}
|
|
81
|
+
if (isnan(xa)) {
|
|
82
|
+
return 1;
|
|
83
|
+
}
|
|
84
|
+
if (isnan(xb)) {
|
|
85
|
+
return -1;
|
|
86
|
+
}
|
|
87
|
+
if (xa < xb) {
|
|
88
|
+
return -1;
|
|
89
|
+
}
|
|
90
|
+
if (xa > xb) {
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
return 0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
int32_t ps_str_cmp(int64_t an, char *ap, int64_t bn, char *bp);
|
|
97
|
+
|
|
98
|
+
static int ps_cmp_buf(const void *a, const void *b) {
|
|
99
|
+
const PsBuf *xa = a;
|
|
100
|
+
const PsBuf *xb = b;
|
|
101
|
+
return ps_str_cmp(xa->len, xa->data, xb->len, xb->data);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
static PsSeq *ps_sorted(PsSeq *src, int (*cmp)(const void *, const void *)) {
|
|
105
|
+
PsSeq *out = ps_seq_new(src->elem, src->len);
|
|
106
|
+
if (src->len > 0) {
|
|
107
|
+
memcpy(out->data, src->data, (size_t)src->len * (size_t)src->elem);
|
|
108
|
+
qsort(out->data, (size_t)src->len, (size_t)src->elem, cmp);
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
PsSeq *ps_sorted_i64(PsSeq *s) { return ps_sorted(s, ps_cmp_i64); }
|
|
114
|
+
|
|
115
|
+
PsSeq *ps_sorted_f64(PsSeq *s) { return ps_sorted(s, ps_cmp_f64); }
|
|
116
|
+
|
|
117
|
+
PsSeq *ps_sorted_buf(PsSeq *s) { return ps_sorted(s, ps_cmp_buf); }
|
|
118
|
+
|
|
119
|
+
int64_t ps_ord(int64_t n, const char *s) {
|
|
120
|
+
if (n != 1) {
|
|
121
|
+
ps_raise_cstr(PS_VALUE, "ord() expected a character");
|
|
122
|
+
}
|
|
123
|
+
return (unsigned char)s[0];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/* int(bytes): base 10, CPython's surrounding-whitespace strip, optional
|
|
127
|
+
sign, every remaining byte a digit, no underscore subset, i64 range only. */
|
|
128
|
+
int64_t ps_int_from_bytes(int64_t n, const char *s) {
|
|
129
|
+
int64_t i = 0;
|
|
130
|
+
while (i < n && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n' || s[i] == '\r' || s[i] == '\f' || s[i] == '\v')) {
|
|
131
|
+
i++;
|
|
132
|
+
}
|
|
133
|
+
int neg = 0;
|
|
134
|
+
if (i < n && (s[i] == '+' || s[i] == '-')) {
|
|
135
|
+
neg = s[i] == '-';
|
|
136
|
+
i++;
|
|
137
|
+
}
|
|
138
|
+
uint64_t mag = 0;
|
|
139
|
+
int64_t end = n;
|
|
140
|
+
while (end > i && (s[end - 1] == ' ' || s[end - 1] == '\t' || s[end - 1] == '\n' || s[end - 1] == '\r'
|
|
141
|
+
|| s[end - 1] == '\f' || s[end - 1] == '\v')) {
|
|
142
|
+
end--;
|
|
143
|
+
}
|
|
144
|
+
if (i >= end) {
|
|
145
|
+
ps_raise_cstr(PS_VALUE, "invalid literal for int() with base 10");
|
|
146
|
+
}
|
|
147
|
+
for (; i < end; i++) {
|
|
148
|
+
if (s[i] < '0' || s[i] > '9') {
|
|
149
|
+
ps_raise_cstr(PS_VALUE, "invalid literal for int() with base 10");
|
|
150
|
+
}
|
|
151
|
+
if (mag > (UINT64_MAX - (uint64_t)(s[i] - '0')) / 10) {
|
|
152
|
+
ps_raise_cstr(PS_VALUE, "int() literal does not fit in i64");
|
|
153
|
+
}
|
|
154
|
+
mag = mag * 10 + (uint64_t)(s[i] - '0');
|
|
155
|
+
}
|
|
156
|
+
if (!neg && mag > (uint64_t)INT64_MAX) {
|
|
157
|
+
ps_raise_cstr(PS_VALUE, "int() literal does not fit in i64");
|
|
158
|
+
}
|
|
159
|
+
return neg ? -(int64_t)(mag - 1) - 1 : (int64_t)mag;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
void ps_chr(int64_t c, PsBuf *out) {
|
|
163
|
+
char buf[1];
|
|
164
|
+
if (c < 0 || c > 127) {
|
|
165
|
+
ps_raise_cstr(PS_VALUE, "chr is ASCII in this subset");
|
|
166
|
+
}
|
|
167
|
+
buf[0] = (char)c;
|
|
168
|
+
ps_buf_copy(out, buf, 1);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static int64_t ps_half_even(double x) {
|
|
172
|
+
double base;
|
|
173
|
+
double frac;
|
|
174
|
+
double rounded;
|
|
175
|
+
if (!isfinite(x)) {
|
|
176
|
+
ps_raise_cstr(PS_OVERFLOW, "cannot round this float");
|
|
177
|
+
}
|
|
178
|
+
base = floor(x);
|
|
179
|
+
frac = x - base;
|
|
180
|
+
if (frac > 0.5) {
|
|
181
|
+
rounded = base + 1.0;
|
|
182
|
+
} else if (frac < 0.5) {
|
|
183
|
+
rounded = base;
|
|
184
|
+
} else if (fmod(base, 2.0) == 0.0) {
|
|
185
|
+
rounded = base;
|
|
186
|
+
} else {
|
|
187
|
+
rounded = base + 1.0;
|
|
188
|
+
}
|
|
189
|
+
if (rounded >= 9223372036854775808.0 || rounded < -9223372036854775808.0) {
|
|
190
|
+
ps_raise_cstr(PS_OVERFLOW, "cannot round this float");
|
|
191
|
+
}
|
|
192
|
+
return (int64_t)rounded;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
int64_t ps_round_i64(double x) { return ps_half_even(x); }
|
|
196
|
+
|
|
197
|
+
double ps_round_nd(double x, int64_t nd) {
|
|
198
|
+
double scale;
|
|
199
|
+
int64_t rounded;
|
|
200
|
+
if (nd < 0 || nd > 8) {
|
|
201
|
+
ps_raise_cstr(PS_VALUE, "round ndigits is out of the lowered range");
|
|
202
|
+
}
|
|
203
|
+
if (!isfinite(x)) {
|
|
204
|
+
ps_raise_cstr(PS_VALUE, "cannot round this float");
|
|
205
|
+
}
|
|
206
|
+
scale = pow(10.0, (double)nd);
|
|
207
|
+
rounded = ps_half_even(x * scale);
|
|
208
|
+
return (double)rounded / scale;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
void ps_warn(int64_t n, const char *s) {
|
|
212
|
+
if (n < 0) {
|
|
213
|
+
n = 0;
|
|
214
|
+
}
|
|
215
|
+
if (n > 1000000) {
|
|
216
|
+
n = 1000000;
|
|
217
|
+
}
|
|
218
|
+
fprintf(stderr, "UserWarning: %.*s\n", (int)n, s);
|
|
219
|
+
}
|