… | |
… | |
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 |
… | |
… | |
364 | int toklen, i; |
383 | int toklen, i; |
365 | time_t tstamp = timestamp ? timestamp : time (0); |
384 | time_t tstamp = timestamp ? timestamp : time (0); |
366 | struct tm *tm = gmtime (&tstamp); |
385 | struct tm *tm = gmtime (&tstamp); |
367 | |
386 | |
368 | New (0, token, |
387 | New (0, token, |
369 | 1 + 1 // version |
388 | 1 + 1 // version |
370 | + 12 + 1 // time field sans century |
389 | + 12 + 1 // time field sans century |
371 | + strlen (resource) + 1 // ressource |
390 | + strlen (resource) + 1 // ressource |
372 | + strlen (trial) + extrarand + 8 + 1 // trial |
391 | + strlen (trial) + extrarand + 8 + 1 // trial |
373 | + 1, |
392 | + 1, |
374 | char); |
393 | char); |
375 | |
394 | |
376 | if (!token) |
395 | if (!token) |
377 | croak ("out of memory"); |
396 | croak ("out of memory"); |
378 | |
397 | |
379 | if (size > 64) |
398 | if (size > 64) |