… | |
… | |
17 | /* pcg: I was tempted to just rip this code off, after all, if you don't |
17 | /* pcg: I was tempted to just rip this code off, after all, if you don't |
18 | * demand anything I am inclined not to give anything. *Sigh* something |
18 | * demand anything I am inclined not to give anything. *Sigh* something |
19 | * kept me from doing it, so here's the truth: I took this code from the |
19 | * kept me from doing it, so here's the truth: I took this code from the |
20 | * SHA1 perl module, since it looked reasonably well-crafted. I modified |
20 | * SHA1 perl module, since it looked reasonably well-crafted. I modified |
21 | * it here and there, though. |
21 | * it here and there, though. |
|
|
22 | */ |
|
|
23 | |
|
|
24 | /* |
|
|
25 | * we have lots of micro-optimizations here, this is just for toying |
|
|
26 | * around... |
22 | */ |
27 | */ |
23 | |
28 | |
24 | /* don't expect _too_ much from compilers for now. */ |
29 | /* don't expect _too_ much from compilers for now. */ |
25 | #if __GNUC__ > 2 |
30 | #if __GNUC__ > 2 |
26 | # define restrict __restrict__ |
31 | # define restrict __restrict__ |
… | |
… | |
31 | #elif __STDC_VERSION__ < 199900 |
36 | #elif __STDC_VERSION__ < 199900 |
32 | # define restrict |
37 | # define restrict |
33 | # define inline |
38 | # define inline |
34 | #endif |
39 | #endif |
35 | |
40 | |
|
|
41 | #if __GNUC__ < 2 |
|
|
42 | # define __attribute__(x) |
|
|
43 | #endif |
|
|
44 | |
|
|
45 | #ifdef __i386 |
|
|
46 | # define a_regparm(n) __attribute__((__regparm__(n))) |
|
|
47 | #else |
|
|
48 | # define a_regparm(n) |
|
|
49 | #endif |
|
|
50 | |
|
|
51 | #define a_const __attribute__((__const__)) |
|
|
52 | |
36 | /* Useful defines & typedefs */ |
53 | /* Useful defines & typedefs */ |
37 | |
54 | |
38 | #if defined(U64TYPE) && (defined(USE_64_BIT_INT) || ((BYTEORDER != 0x1234) && (BYTEORDER != 0x4321))) |
55 | #if defined(U64TYPE) && (defined(USE_64_BIT_INT) || ((BYTEORDER != 0x1234) && (BYTEORDER != 0x4321))) |
39 | typedef U64TYPE ULONG; |
56 | typedef U64TYPE ULONG; |
40 | # if BYTEORDER == 0x1234 |
57 | # if BYTEORDER == 0x1234 |
41 | # undef BYTEORDER |
58 | # undef BYTEORDER |
42 | # define BYTEORDER 0x12345678 |
59 | # define BYTEORDER 0x12345678 |
43 | # elif BYTEORDER == 0x4321 |
60 | # elif BYTEORDER == 0x4321 |
44 | # undef BYTEORDER |
61 | # undef BYTEORDER |
45 | # define BYTEORDER 0x87654321 |
62 | # define BYTEORDER 0x87654321 |
46 | # endif |
63 | # endif |
47 | #else |
64 | #else |
48 | typedef uint_fast32_t ULONG; /* 32-or-more-bit quantity */ |
65 | typedef uint_fast32_t ULONG; /* 32-or-more-bit quantity */ |
49 | #endif |
66 | #endif |
50 | |
67 | |
51 | #if GCCX86ASM |
68 | #if GCCX86ASM |
52 | # define zprefix(n) ({ int _r; __asm__ ("bsrl %1, %0" : "=r" (_r) : "r" (n)); 31 - _r ; }) |
69 | # define zprefix(n) ({ int _r; __asm__ ("bsrl %1, %0" : "=r" (_r) : "r" (n)); 31 - _r ; }) |
|
|
70 | #elif __GNUC__ > 2 && __GNUC_MINOR__ > 3 |
|
|
71 | # define zprefix(n) (__extension__ ({ uint32_t n__ = (n); n ? __builtin_clz (n) : 32; })) |
53 | #else |
72 | #else |
54 | static int zprefix (ULONG n) |
73 | static int a_const zprefix (ULONG n) |
55 | { |
74 | { |
56 | static char zp[256] = |
75 | static char zp[256] = |
57 | { |
76 | { |
58 | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
77 | 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, |
59 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
78 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
… | |
… | |
127 | B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30) |
146 | B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30) |
128 | |
147 | |
129 | #define FT(n) \ |
148 | #define FT(n) \ |
130 | A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30) |
149 | A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30) |
131 | |
150 | |
132 | static void sha_transform(SHA_INFO *restrict sha_info) |
151 | static void a_regparm(1) sha_transform(SHA_INFO *restrict sha_info) |
133 | { |
152 | { |
134 | int i; |
153 | int i; |
135 | U8 *dp; |
154 | U8 *restrict dp; |
136 | ULONG T, A, B, C, D, E, W[80], *restrict WP; |
155 | ULONG T, A, B, C, D, E, W[80], *restrict WP; |
137 | |
156 | |
138 | dp = sha_info->data; |
157 | dp = sha_info->data; |
139 | |
158 | |
140 | #if BYTEORDER == 0x1234 |
159 | #if BYTEORDER == 0x1234 |
… | |
… | |
324 | |
343 | |
325 | # could be improved quite a bit in accuracy |
344 | # could be improved quite a bit in accuracy |
326 | NV |
345 | NV |
327 | _estimate_rounds () |
346 | _estimate_rounds () |
328 | CODE: |
347 | CODE: |
|
|
348 | { |
329 | char data[40]; |
349 | char data[40]; |
330 | NVTime nvtime = get_nvtime (); |
350 | NVTime nvtime = get_nvtime (); |
331 | NV t1, t2, t; |
351 | NV t1, t2, t; |
332 | int count = 0; |
352 | int count = 0; |
333 | SHA_INFO ctx; |
353 | SHA_INFO ctx; |
… | |
… | |
348 | t2 = nvtime (); |
368 | t2 = nvtime (); |
349 | |
369 | |
350 | } while (t == t2); |
370 | } while (t == t2); |
351 | |
371 | |
352 | RETVAL = (NV)count / (t2 - t1); |
372 | RETVAL = (NV)count / (t2 - t1); |
|
|
373 | } |
353 | OUTPUT: |
374 | OUTPUT: |
354 | RETVAL |
375 | RETVAL |
355 | |
376 | |
356 | SV * |
377 | SV * |
357 | _gentoken (int size, IV timestamp, char *resource, char *trial = "", int extrarand = 0) |
378 | _gentoken (int size, IV timestamp, char *resource, char *trial = "", int extrarand = 0) |
358 | CODE: |
379 | CODE: |
|
|
380 | { |
359 | SHA_INFO ctx1, ctx; |
381 | SHA_INFO ctx1, ctx; |
360 | char *token, *seq, *s; |
382 | char *token, *seq, *s; |
361 | int toklen, i; |
383 | int toklen, i; |
362 | time_t tstamp = timestamp ? timestamp : time (0); |
384 | time_t tstamp = timestamp ? timestamp : time (0); |
363 | struct tm *tm = gmtime (&tstamp); |
385 | struct tm *tm = gmtime (&tstamp); |
… | |
… | |
410 | *s = nextenc [*s]; |
432 | *s = nextenc [*s]; |
411 | } while (*s++ == 'a'); |
433 | } while (*s++ == 'a'); |
412 | } |
434 | } |
413 | |
435 | |
414 | RETVAL = newSVpvn (token, toklen); |
436 | RETVAL = newSVpvn (token, toklen); |
|
|
437 | } |
415 | OUTPUT: |
438 | OUTPUT: |
416 | RETVAL |
439 | RETVAL |
417 | |
440 | |
418 | int |
441 | int |
419 | _prefixlen (SV *tok) |
442 | _prefixlen (SV *tok) |
420 | CODE: |
443 | CODE: |
|
|
444 | { |
421 | STRLEN toklen; |
445 | STRLEN toklen; |
422 | char *token = SvPV (tok, toklen); |
446 | char *token = SvPV (tok, toklen); |
423 | SHA_INFO ctx; |
447 | SHA_INFO ctx; |
424 | |
448 | |
425 | sha_init (&ctx); |
449 | sha_init (&ctx); |
426 | sha_update (&ctx, token, toklen); |
450 | sha_update (&ctx, token, toklen); |
427 | RETVAL = sha_final (&ctx); |
451 | RETVAL = sha_final (&ctx); |
|
|
452 | } |
428 | OUTPUT: |
453 | OUTPUT: |
429 | RETVAL |
454 | RETVAL |
430 | |
455 | |
431 | |
456 | |