-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathts.c
457 lines (382 loc) · 9.36 KB
/
ts.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void chunkdone(long);
/*
* Internal chunkifier data.
*/
typedef struct Chunker {
/* Chunkification parameters */
uint32_t mu; /* Desired mean chunk length */
uint32_t p; /* Modulus */
uint32_t pp; /* - p^(-1) mod 2^32 */
uint32_t ar; /* alpha * 2^32 mod p */
uint32_t * cm; /* Coefficient map modulo p */
uint32_t htlen; /* Size of hash table in 2-word entries */
uint32_t blen; /* Length of buf[] */
uint32_t w; /* Minimum substring length; size of b[] */
/* Current state */
uint32_t k; /* Number of bytes in chunk so far */
uint32_t r; /* floor(sqrt(4 * k - mu)) */
uint32_t rs; /* (r + 1)^2 - (4 * k - mu) */
uint32_t akr; /* a^k * 2^32 mod p */
uint32_t yka; /* Power series truncated before x^k term */
/* evaluated at a mod p */
uint32_t * b; /* Circular buffer of values waiting to */
/* be added to the hash table. */
uint32_t * ht; /* Hash table; pairs of the form (yka, k). */
uint8_t * buf; /* Buffer of bytes processed */
} Chunker;
static int isprime(uint32_t n);
static uint32_t nextprime(uint32_t n);
static uint32_t mmul(uint32_t a, uint32_t b, uint32_t p, uint32_t pp);
static int minorder(uint32_t ar, uint32_t ord, uint32_t p, uint32_t pp);
static uint32_t isqrt(uint32_t x);
static void chunkstart(Chunker * c);
/* Returns nonzero iff n is prime. */
static int
isprime(uint32_t n)
{
uint32_t x;
for (x = 2; (x * x <= n) && (x < 65536); x++)
if ((n % x) == 0)
return (0);
return (n > 1);
}
/*
* Returns the smallest prime satisfying n <= p < 2^32, or 0 if none exist.
*/
static uint32_t
nextprime(uint32_t n)
{
volatile uint32_t p;
for (p = n; p != 0; p++)
if (isprime(p))
break;
return (p);
}
/**
* Compute $(a * b + (a * b * pp \bmod 2^{32}) * p) / 2^{32}$.
* Note that for $b \leq p$ this is at most $p * (1 + a / 2^{32})$.
*/
static uint32_t
mmul(uint32_t a, uint32_t b, uint32_t p, uint32_t pp)
{
uint64_t ab;
uint32_t abpp;
ab = (uint64_t)(a) * (uint64_t)(b);
abpp = (uint32_t)(ab) * pp;
ab += (uint64_t)(abpp) * (uint64_t)(p);
return (ab >> 32);
}
/*
* Returns nonzero if (ar / 2^32) has multiplicative order at least ord mod p.
*/
static int
minorder(uint32_t ar, uint32_t ord, uint32_t p, uint32_t pp)
{
uint32_t akr;
uint32_t akr0;
uint32_t k;
akr = akr0 = (- p) % p;
for (k = 0; k < ord; k++) {
akr = mmul(akr, ar, p, pp) % p;
if (akr == akr0)
return (0);
}
return (1);
}
/*
* Return the greatest y such that y^2 <= x.
*/
static uint32_t
isqrt(uint32_t x)
{
uint32_t y;
for (y = 1; y < 65536; y++)
if (y * y > x)
break;
return (y - 1);
}
/*
* Prepare the Chunker for input.
*/
static void
chunkstart(Chunker * c)
{
uint32_t i;
/* No entries in the hash table. */
for (i = 0; i < 2 * c->htlen; i++)
c->ht[i] = - c->htlen;
/* Nothing in the queue waiting to be added to the table, either. */
for (i = 0; i < c->w; i++)
c->b[i] = c->p;
/* No bytes input yet. */
c->akr = (- c->p) % c->p;
c->yka = 0;
c->k = 0;
c->r = 0;
c->rs = 1 + c->mu;
}
void
rand32(uint8_t *buf)
{
int i;
for (i = 0; i < 32; i += 4) {
*(uint32_t*)&buf[i] = rand();
}
}
Chunker *
chunkinit(uint32_t meanlen, uint32_t maxlen)
{
Chunker * c;
uint8_t hbuf[32]; /* HMAC of something */
uint32_t pmin;
uint32_t i;
/*
* Parameter verification.
*/
if ((meanlen > 1262226) || (maxlen <= meanlen)) {
return (NULL);
}
/*
* Allocate memory for Chunker structure.
*/
c = malloc(sizeof(*c));
if (c == NULL)
return (NULL);
/*
* Initialize fixed chunkifier parameters.
*/
c->mu = meanlen;
c->blen = maxlen;
c->w = 32;
/*-
* Compute the necessary hash table size. At any given time, there
* are sqrt(4 k - mu) entries and up to sqrt(4 k - mu) tombstones in
* the hash table, and we want table inserts and lookups to be fast,
* so we want these to use up no more than 50% of the table. We also
* want the table size to be a power of 2.
*
* Consequently, the table size should be the least power of 2 in
* excess of 4 * sqrt(4 maxlen - mu) = 8 * sqrt(maxlen - mu / 4).
*/
c->htlen = 8;
for (i = c->blen - c->mu / 4; i > 0; i >>= 2)
c->htlen <<= 1;
/*
* Allocate memory for buffers.
*/
c->cm = c->b = c->ht = NULL;
c->buf = NULL;
c->cm = malloc(256 * sizeof(c->cm[0]));
c->b = malloc(c->w * sizeof(c->b[0]));
c->ht = malloc(c->htlen * 2 * sizeof(c->ht[0]));
c->buf = malloc((c->blen) * sizeof(c->buf[0]));
if ((c->cm == NULL) || (c->b == NULL) ||
(c->ht == NULL) || (c->buf == NULL))
goto err;
/* Generate parameter values by computing HMACs. */
/* p is generated from HMAC('p\0'). */
rand32(hbuf);
memcpy(&c->p, hbuf, sizeof(c->p));
/* alpha is generated from HMAC('a\0'). */
rand32(hbuf);
memcpy(&c->ar, hbuf, sizeof(c->ar));
/* cm[i] is generated from HMAC('x' . i). */
for (i = 0; i < 256; i++) {
rand32(hbuf);
memcpy(&c->cm[i], hbuf, sizeof(c->cm[i]));
}
/*
* Using the generated pseudorandom values, actually generate
* the parameters we want.
*/
/*
* We want p to be approximately mu^(3/2) * 1.009677744. Compute p
* to be at least floor(mu*floor(sqrt(mu))*1.01) and no more than
* floor(sqrt(mu)) - 1 more than that.
*/
pmin = c->mu * isqrt(c->mu);
pmin += pmin / 100;
c->p = nextprime(pmin + (c->p % isqrt(c->mu)));
/* c->p <= 1431655739 < 1431655765 = floor(2^32 / 3) */
/* Compute pp = - p^(-1) mod 2^32. */
c->pp = ((2 * c->p + 4) & 8) - c->p; /* pp = - 1/p mod 2^4 */
c->pp *= 2 + c->p * c->pp; /* pp = - 1/p mod 2^8 */
c->pp *= 2 + c->p * c->pp; /* pp = - 1/p mod 2^16 */
c->pp *= 2 + c->p * c->pp; /* pp = - 1/p mod 2^32 */
/*
* We want to have 1 < ar < p - 1 and the multiplicative order of
* alpha mod p greater than mu.
*/
c->ar = 2 + (c->ar % (c->p - 3));
while (! minorder(c->ar, c->mu, c->p, c->pp)) {
c->ar = c->ar + 1;
if (c->ar == c->p)
c->ar = 2;
}
/*
* Prepare for incoming data.
*/
chunkstart(c);
/*
* Return initialized Chunker.
*/
return (c);
err:
/* free(NULL) is safe, so it doesn't matter where we jumped from. */
free(c->buf);
free(c->ht);
free(c->b);
free(c->cm);
free(c);
return (NULL);
}
int
chunkend(Chunker * c)
{
/* If we haven't started the chunk yet, don't end it either. */
if (c->k == 0)
return (0);
/* Process the chunk. */
chunkdone(c->k);
/* Prepare for more input. */
chunkstart(c);
return (0);
}
int
chunkwrite(Chunker * c, uint8_t * buf, size_t buflen)
{
uint32_t htpos;
uint32_t yka_tmp;
size_t i;
int rc;
for (i = 0; i < (size_t)buflen; i++) {
/* Add byte to buffer. */
c->buf[c->k] = buf[i];
/* k := k + 1 */
c->k++;
while (c->rs <= 4) {
c->rs += 2 * c->r + 1;
c->r += 1;
}
c->rs -= 4;
/*
* If k = blen, then we've filled the buffer and we
* automatically have the end of the chunk.
*/
if (c->k == c->blen)
goto endofchunk;
/*
* Don't waste time on arithmetic if we don't have enough
* data yet for a permitted loop to ever occur.
*/
if (c->r == 0)
continue;
/*
* Update state to add new character.
*/
/* y_k(a) := y_k(a) + a^k * x_k mod p */
/* yka <= p * (2 + p / (2^32 - p)) <= p * 2.5 < 2^31 + p */
c->yka += mmul(c->akr, c->cm[buf[i]], c->p, c->pp);
/* Each step reduces yka by p iff yka >= p. */
c->yka -= c->p & (((c->yka - c->p) >> 31) - 1);
c->yka -= c->p & (((c->yka - c->p) >> 31) - 1);
/* a^k := a^k * alpha mod p */
/* akr <= p * 2^32 / (2^32 - p) */
c->akr = mmul(c->akr, c->ar, c->p, c->pp);
/*
* Check if yka is in the hash table.
*/
htpos = c->yka & (c->htlen - 1);
do {
/* Have we found yka? */
if (c->ht[2 * htpos + 1] == c->yka) {
/* Recent enough to be a valid entry? */
if (c->k - c->ht[2 * htpos] - 1 < c->r)
goto endofchunk;
}
/* Have we found an empty space? */
if (c->k - c->ht[2 * htpos] - 1 >= 2 * c->r)
break;
/* Move to the next position in the table. */
htpos = (htpos + 1) & (c->htlen - 1);
} while (1);
/*
* Insert queued value into table.
*/
yka_tmp = c->b[c->k & (c->w - 1)];
htpos = yka_tmp & (c->htlen - 1);
do {
/* Have we found an empty space or tombstone? */
if (c->k - c->ht[2 * htpos] - 1 >= c->r) {
c->ht[2 * htpos] = c->k;
c->ht[2 * htpos + 1] = yka_tmp;
break;
}
/* Move to the next position in the table. */
htpos = (htpos + 1) & (c->htlen - 1);
} while (1);
/*
* Add current value into queue.
*/
c->b[c->k & (c->w - 1)] = c->yka;
/*
* Move on to next byte.
*/
continue;
endofchunk:
/*
* We've reached the end of a chunk.
*/
rc = chunkend(c);
if (rc)
return (rc);
}
return (0);
}
void
chunkdone(long n)
{
printf("%ld\n", n);
}
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
static char iobuf[65536];
struct Chunker *chunker;
long rd, tot;
int fd;
if (argc < 2)
return 1;
if ((fd = open(argv[1], O_RDONLY)) == -1)
return 1;
srand(0);
chunker = chunkinit(AVGBLK, MAXBLK);
tot = 0;
for (;;) {
rd = read(fd, iobuf, sizeof(iobuf));
tot += rd;
if (!rd)
break;
chunkwrite(chunker, (uint8_t*)iobuf, rd);
}
chunkend(chunker);
fprintf(stderr, "munched ");
if (tot / 1000000000)
fprintf(stderr, "%.2fGb\n", (float)tot / 1000000000);
else if (tot / 1000000)
fprintf(stderr, "%.2fMb\n", (float)tot / 1000000);
else if (tot / 1000)
fprintf(stderr, "%.2fKb\n", (float)tot / 1000);
else
fprintf(stderr, "%ldb\n", tot);
}