Update tests in returning1.test to account for [c7896e88].
[sqlite.git] / tool / mksourceid.c
blobdd153997df273359bb9f737c27ad3141bfe28bbe
1 /*
2 ** Run this program with a single argument which is the name of the
3 ** Fossil "manifest" file for a project, and this program will emit on
4 ** standard output the "source id" for for the program.
5 **
6 ** (1) The "source id" is the date of check-in together with the
7 ** SHA3 hash of the manifest file.
8 **
9 ** (2) All individual file hashes in the manifest are verified. If any
10 ** source file has changed, the SHA3 hash ends with "modified".
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <ctype.h>
19 /* Portable 64-bit unsigned integers */
20 #if defined(_MSC_VER) || defined(__BORLANDC__)
21 typedef unsigned __int64 u64;
22 #else
23 typedef unsigned long long int u64;
24 #endif
28 ** Macros to determine whether the machine is big or little endian,
29 ** and whether or not that determination is run-time or compile-time.
31 ** For best performance, an attempt is made to guess at the byte-order
32 ** using C-preprocessor macros. If that is unsuccessful, or if
33 ** -DBYTEORDER=0 is set, then byte-order is determined
34 ** at run-time.
36 #ifndef BYTEORDER
37 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
38 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
39 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
40 defined(__arm__)
41 # define BYTEORDER 1234
42 # elif defined(sparc) || defined(__ppc__)
43 # define BYTEORDER 4321
44 # else
45 # define BYTEORDER 0
46 # endif
47 #endif
52 ** State structure for a SHA3 hash in progress
54 typedef struct SHA3Context SHA3Context;
55 struct SHA3Context {
56 union {
57 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
58 unsigned char x[1600]; /* ... or 1600 bytes */
59 } u;
60 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
61 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
62 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
66 ** A single step of the Keccak mixing function for a 1600-bit state
68 static void KeccakF1600Step(SHA3Context *p){
69 int i;
70 u64 B0, B1, B2, B3, B4;
71 u64 C0, C1, C2, C3, C4;
72 u64 D0, D1, D2, D3, D4;
73 static const u64 RC[] = {
74 0x0000000000000001ULL, 0x0000000000008082ULL,
75 0x800000000000808aULL, 0x8000000080008000ULL,
76 0x000000000000808bULL, 0x0000000080000001ULL,
77 0x8000000080008081ULL, 0x8000000000008009ULL,
78 0x000000000000008aULL, 0x0000000000000088ULL,
79 0x0000000080008009ULL, 0x000000008000000aULL,
80 0x000000008000808bULL, 0x800000000000008bULL,
81 0x8000000000008089ULL, 0x8000000000008003ULL,
82 0x8000000000008002ULL, 0x8000000000000080ULL,
83 0x000000000000800aULL, 0x800000008000000aULL,
84 0x8000000080008081ULL, 0x8000000000008080ULL,
85 0x0000000080000001ULL, 0x8000000080008008ULL
87 # define A00 (p->u.s[0])
88 # define A01 (p->u.s[1])
89 # define A02 (p->u.s[2])
90 # define A03 (p->u.s[3])
91 # define A04 (p->u.s[4])
92 # define A10 (p->u.s[5])
93 # define A11 (p->u.s[6])
94 # define A12 (p->u.s[7])
95 # define A13 (p->u.s[8])
96 # define A14 (p->u.s[9])
97 # define A20 (p->u.s[10])
98 # define A21 (p->u.s[11])
99 # define A22 (p->u.s[12])
100 # define A23 (p->u.s[13])
101 # define A24 (p->u.s[14])
102 # define A30 (p->u.s[15])
103 # define A31 (p->u.s[16])
104 # define A32 (p->u.s[17])
105 # define A33 (p->u.s[18])
106 # define A34 (p->u.s[19])
107 # define A40 (p->u.s[20])
108 # define A41 (p->u.s[21])
109 # define A42 (p->u.s[22])
110 # define A43 (p->u.s[23])
111 # define A44 (p->u.s[24])
112 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
114 for(i=0; i<24; i+=4){
115 C0 = A00^A10^A20^A30^A40;
116 C1 = A01^A11^A21^A31^A41;
117 C2 = A02^A12^A22^A32^A42;
118 C3 = A03^A13^A23^A33^A43;
119 C4 = A04^A14^A24^A34^A44;
120 D0 = C4^ROL64(C1, 1);
121 D1 = C0^ROL64(C2, 1);
122 D2 = C1^ROL64(C3, 1);
123 D3 = C2^ROL64(C4, 1);
124 D4 = C3^ROL64(C0, 1);
126 B0 = (A00^D0);
127 B1 = ROL64((A11^D1), 44);
128 B2 = ROL64((A22^D2), 43);
129 B3 = ROL64((A33^D3), 21);
130 B4 = ROL64((A44^D4), 14);
131 A00 = B0 ^((~B1)& B2 );
132 A00 ^= RC[i];
133 A11 = B1 ^((~B2)& B3 );
134 A22 = B2 ^((~B3)& B4 );
135 A33 = B3 ^((~B4)& B0 );
136 A44 = B4 ^((~B0)& B1 );
138 B2 = ROL64((A20^D0), 3);
139 B3 = ROL64((A31^D1), 45);
140 B4 = ROL64((A42^D2), 61);
141 B0 = ROL64((A03^D3), 28);
142 B1 = ROL64((A14^D4), 20);
143 A20 = B0 ^((~B1)& B2 );
144 A31 = B1 ^((~B2)& B3 );
145 A42 = B2 ^((~B3)& B4 );
146 A03 = B3 ^((~B4)& B0 );
147 A14 = B4 ^((~B0)& B1 );
149 B4 = ROL64((A40^D0), 18);
150 B0 = ROL64((A01^D1), 1);
151 B1 = ROL64((A12^D2), 6);
152 B2 = ROL64((A23^D3), 25);
153 B3 = ROL64((A34^D4), 8);
154 A40 = B0 ^((~B1)& B2 );
155 A01 = B1 ^((~B2)& B3 );
156 A12 = B2 ^((~B3)& B4 );
157 A23 = B3 ^((~B4)& B0 );
158 A34 = B4 ^((~B0)& B1 );
160 B1 = ROL64((A10^D0), 36);
161 B2 = ROL64((A21^D1), 10);
162 B3 = ROL64((A32^D2), 15);
163 B4 = ROL64((A43^D3), 56);
164 B0 = ROL64((A04^D4), 27);
165 A10 = B0 ^((~B1)& B2 );
166 A21 = B1 ^((~B2)& B3 );
167 A32 = B2 ^((~B3)& B4 );
168 A43 = B3 ^((~B4)& B0 );
169 A04 = B4 ^((~B0)& B1 );
171 B3 = ROL64((A30^D0), 41);
172 B4 = ROL64((A41^D1), 2);
173 B0 = ROL64((A02^D2), 62);
174 B1 = ROL64((A13^D3), 55);
175 B2 = ROL64((A24^D4), 39);
176 A30 = B0 ^((~B1)& B2 );
177 A41 = B1 ^((~B2)& B3 );
178 A02 = B2 ^((~B3)& B4 );
179 A13 = B3 ^((~B4)& B0 );
180 A24 = B4 ^((~B0)& B1 );
182 C0 = A00^A20^A40^A10^A30;
183 C1 = A11^A31^A01^A21^A41;
184 C2 = A22^A42^A12^A32^A02;
185 C3 = A33^A03^A23^A43^A13;
186 C4 = A44^A14^A34^A04^A24;
187 D0 = C4^ROL64(C1, 1);
188 D1 = C0^ROL64(C2, 1);
189 D2 = C1^ROL64(C3, 1);
190 D3 = C2^ROL64(C4, 1);
191 D4 = C3^ROL64(C0, 1);
193 B0 = (A00^D0);
194 B1 = ROL64((A31^D1), 44);
195 B2 = ROL64((A12^D2), 43);
196 B3 = ROL64((A43^D3), 21);
197 B4 = ROL64((A24^D4), 14);
198 A00 = B0 ^((~B1)& B2 );
199 A00 ^= RC[i+1];
200 A31 = B1 ^((~B2)& B3 );
201 A12 = B2 ^((~B3)& B4 );
202 A43 = B3 ^((~B4)& B0 );
203 A24 = B4 ^((~B0)& B1 );
205 B2 = ROL64((A40^D0), 3);
206 B3 = ROL64((A21^D1), 45);
207 B4 = ROL64((A02^D2), 61);
208 B0 = ROL64((A33^D3), 28);
209 B1 = ROL64((A14^D4), 20);
210 A40 = B0 ^((~B1)& B2 );
211 A21 = B1 ^((~B2)& B3 );
212 A02 = B2 ^((~B3)& B4 );
213 A33 = B3 ^((~B4)& B0 );
214 A14 = B4 ^((~B0)& B1 );
216 B4 = ROL64((A30^D0), 18);
217 B0 = ROL64((A11^D1), 1);
218 B1 = ROL64((A42^D2), 6);
219 B2 = ROL64((A23^D3), 25);
220 B3 = ROL64((A04^D4), 8);
221 A30 = B0 ^((~B1)& B2 );
222 A11 = B1 ^((~B2)& B3 );
223 A42 = B2 ^((~B3)& B4 );
224 A23 = B3 ^((~B4)& B0 );
225 A04 = B4 ^((~B0)& B1 );
227 B1 = ROL64((A20^D0), 36);
228 B2 = ROL64((A01^D1), 10);
229 B3 = ROL64((A32^D2), 15);
230 B4 = ROL64((A13^D3), 56);
231 B0 = ROL64((A44^D4), 27);
232 A20 = B0 ^((~B1)& B2 );
233 A01 = B1 ^((~B2)& B3 );
234 A32 = B2 ^((~B3)& B4 );
235 A13 = B3 ^((~B4)& B0 );
236 A44 = B4 ^((~B0)& B1 );
238 B3 = ROL64((A10^D0), 41);
239 B4 = ROL64((A41^D1), 2);
240 B0 = ROL64((A22^D2), 62);
241 B1 = ROL64((A03^D3), 55);
242 B2 = ROL64((A34^D4), 39);
243 A10 = B0 ^((~B1)& B2 );
244 A41 = B1 ^((~B2)& B3 );
245 A22 = B2 ^((~B3)& B4 );
246 A03 = B3 ^((~B4)& B0 );
247 A34 = B4 ^((~B0)& B1 );
249 C0 = A00^A40^A30^A20^A10;
250 C1 = A31^A21^A11^A01^A41;
251 C2 = A12^A02^A42^A32^A22;
252 C3 = A43^A33^A23^A13^A03;
253 C4 = A24^A14^A04^A44^A34;
254 D0 = C4^ROL64(C1, 1);
255 D1 = C0^ROL64(C2, 1);
256 D2 = C1^ROL64(C3, 1);
257 D3 = C2^ROL64(C4, 1);
258 D4 = C3^ROL64(C0, 1);
260 B0 = (A00^D0);
261 B1 = ROL64((A21^D1), 44);
262 B2 = ROL64((A42^D2), 43);
263 B3 = ROL64((A13^D3), 21);
264 B4 = ROL64((A34^D4), 14);
265 A00 = B0 ^((~B1)& B2 );
266 A00 ^= RC[i+2];
267 A21 = B1 ^((~B2)& B3 );
268 A42 = B2 ^((~B3)& B4 );
269 A13 = B3 ^((~B4)& B0 );
270 A34 = B4 ^((~B0)& B1 );
272 B2 = ROL64((A30^D0), 3);
273 B3 = ROL64((A01^D1), 45);
274 B4 = ROL64((A22^D2), 61);
275 B0 = ROL64((A43^D3), 28);
276 B1 = ROL64((A14^D4), 20);
277 A30 = B0 ^((~B1)& B2 );
278 A01 = B1 ^((~B2)& B3 );
279 A22 = B2 ^((~B3)& B4 );
280 A43 = B3 ^((~B4)& B0 );
281 A14 = B4 ^((~B0)& B1 );
283 B4 = ROL64((A10^D0), 18);
284 B0 = ROL64((A31^D1), 1);
285 B1 = ROL64((A02^D2), 6);
286 B2 = ROL64((A23^D3), 25);
287 B3 = ROL64((A44^D4), 8);
288 A10 = B0 ^((~B1)& B2 );
289 A31 = B1 ^((~B2)& B3 );
290 A02 = B2 ^((~B3)& B4 );
291 A23 = B3 ^((~B4)& B0 );
292 A44 = B4 ^((~B0)& B1 );
294 B1 = ROL64((A40^D0), 36);
295 B2 = ROL64((A11^D1), 10);
296 B3 = ROL64((A32^D2), 15);
297 B4 = ROL64((A03^D3), 56);
298 B0 = ROL64((A24^D4), 27);
299 A40 = B0 ^((~B1)& B2 );
300 A11 = B1 ^((~B2)& B3 );
301 A32 = B2 ^((~B3)& B4 );
302 A03 = B3 ^((~B4)& B0 );
303 A24 = B4 ^((~B0)& B1 );
305 B3 = ROL64((A20^D0), 41);
306 B4 = ROL64((A41^D1), 2);
307 B0 = ROL64((A12^D2), 62);
308 B1 = ROL64((A33^D3), 55);
309 B2 = ROL64((A04^D4), 39);
310 A20 = B0 ^((~B1)& B2 );
311 A41 = B1 ^((~B2)& B3 );
312 A12 = B2 ^((~B3)& B4 );
313 A33 = B3 ^((~B4)& B0 );
314 A04 = B4 ^((~B0)& B1 );
316 C0 = A00^A30^A10^A40^A20;
317 C1 = A21^A01^A31^A11^A41;
318 C2 = A42^A22^A02^A32^A12;
319 C3 = A13^A43^A23^A03^A33;
320 C4 = A34^A14^A44^A24^A04;
321 D0 = C4^ROL64(C1, 1);
322 D1 = C0^ROL64(C2, 1);
323 D2 = C1^ROL64(C3, 1);
324 D3 = C2^ROL64(C4, 1);
325 D4 = C3^ROL64(C0, 1);
327 B0 = (A00^D0);
328 B1 = ROL64((A01^D1), 44);
329 B2 = ROL64((A02^D2), 43);
330 B3 = ROL64((A03^D3), 21);
331 B4 = ROL64((A04^D4), 14);
332 A00 = B0 ^((~B1)& B2 );
333 A00 ^= RC[i+3];
334 A01 = B1 ^((~B2)& B3 );
335 A02 = B2 ^((~B3)& B4 );
336 A03 = B3 ^((~B4)& B0 );
337 A04 = B4 ^((~B0)& B1 );
339 B2 = ROL64((A10^D0), 3);
340 B3 = ROL64((A11^D1), 45);
341 B4 = ROL64((A12^D2), 61);
342 B0 = ROL64((A13^D3), 28);
343 B1 = ROL64((A14^D4), 20);
344 A10 = B0 ^((~B1)& B2 );
345 A11 = B1 ^((~B2)& B3 );
346 A12 = B2 ^((~B3)& B4 );
347 A13 = B3 ^((~B4)& B0 );
348 A14 = B4 ^((~B0)& B1 );
350 B4 = ROL64((A20^D0), 18);
351 B0 = ROL64((A21^D1), 1);
352 B1 = ROL64((A22^D2), 6);
353 B2 = ROL64((A23^D3), 25);
354 B3 = ROL64((A24^D4), 8);
355 A20 = B0 ^((~B1)& B2 );
356 A21 = B1 ^((~B2)& B3 );
357 A22 = B2 ^((~B3)& B4 );
358 A23 = B3 ^((~B4)& B0 );
359 A24 = B4 ^((~B0)& B1 );
361 B1 = ROL64((A30^D0), 36);
362 B2 = ROL64((A31^D1), 10);
363 B3 = ROL64((A32^D2), 15);
364 B4 = ROL64((A33^D3), 56);
365 B0 = ROL64((A34^D4), 27);
366 A30 = B0 ^((~B1)& B2 );
367 A31 = B1 ^((~B2)& B3 );
368 A32 = B2 ^((~B3)& B4 );
369 A33 = B3 ^((~B4)& B0 );
370 A34 = B4 ^((~B0)& B1 );
372 B3 = ROL64((A40^D0), 41);
373 B4 = ROL64((A41^D1), 2);
374 B0 = ROL64((A42^D2), 62);
375 B1 = ROL64((A43^D3), 55);
376 B2 = ROL64((A44^D4), 39);
377 A40 = B0 ^((~B1)& B2 );
378 A41 = B1 ^((~B2)& B3 );
379 A42 = B2 ^((~B3)& B4 );
380 A43 = B3 ^((~B4)& B0 );
381 A44 = B4 ^((~B0)& B1 );
386 ** Initialize a new hash. iSize determines the size of the hash
387 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
388 ** can be zero to use the default hash size of 256 bits.
390 static void SHA3Init(SHA3Context *p, int iSize){
391 memset(p, 0, sizeof(*p));
392 if( iSize>=128 && iSize<=512 ){
393 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
394 }else{
395 p->nRate = (1600 - 2*256)/8;
397 #if BYTEORDER==1234
398 /* Known to be little-endian at compile-time. No-op */
399 #elif BYTEORDER==4321
400 p->ixMask = 7; /* Big-endian */
401 #else
403 static unsigned int one = 1;
404 if( 1==*(unsigned char*)&one ){
405 /* Little endian. No byte swapping. */
406 p->ixMask = 0;
407 }else{
408 /* Big endian. Byte swap. */
409 p->ixMask = 7;
412 #endif
416 ** Make consecutive calls to the SHA3Update function to add new content
417 ** to the hash
419 static void SHA3Update(
420 SHA3Context *p,
421 const unsigned char *aData,
422 unsigned int nData
424 unsigned int i = 0;
425 #if BYTEORDER==1234
426 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
427 for(; i+7<nData; i+=8){
428 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
429 p->nLoaded += 8;
430 if( p->nLoaded>=p->nRate ){
431 KeccakF1600Step(p);
432 p->nLoaded = 0;
436 #endif
437 for(; i<nData; i++){
438 #if BYTEORDER==1234
439 p->u.x[p->nLoaded] ^= aData[i];
440 #elif BYTEORDER==4321
441 p->u.x[p->nLoaded^0x07] ^= aData[i];
442 #else
443 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
444 #endif
445 p->nLoaded++;
446 if( p->nLoaded==p->nRate ){
447 KeccakF1600Step(p);
448 p->nLoaded = 0;
454 ** After all content has been added, invoke SHA3Final() to compute
455 ** the final hash. The function returns a pointer to the binary
456 ** hash value.
458 static unsigned char *SHA3Final(SHA3Context *p){
459 unsigned int i;
460 if( p->nLoaded==p->nRate-1 ){
461 const unsigned char c1 = 0x86;
462 SHA3Update(p, &c1, 1);
463 }else{
464 const unsigned char c2 = 0x06;
465 const unsigned char c3 = 0x80;
466 SHA3Update(p, &c2, 1);
467 p->nLoaded = p->nRate - 1;
468 SHA3Update(p, &c3, 1);
470 for(i=0; i<p->nRate; i++){
471 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
473 return &p->u.x[p->nRate];
477 ** Convert a digest into base-16. digest should be declared as
478 ** "unsigned char digest[20]" in the calling function. The SHA3
479 ** digest is stored in the first 20 bytes. zBuf should
480 ** be "char zBuf[41]".
482 static void DigestToBase16(unsigned char *digest, char *zBuf, int nByte){
483 static const char zEncode[] = "0123456789abcdef";
484 int ix;
486 for(ix=0; ix<nByte; ix++){
487 *zBuf++ = zEncode[(*digest>>4)&0xf];
488 *zBuf++ = zEncode[*digest++ & 0xf];
490 *zBuf = '\0';
495 ** Compute the SHA3 checksum of a file on disk. Store the resulting
496 ** checksum in the blob pCksum. pCksum is assumed to be initialized.
498 ** Return the number of errors.
500 static int sha3sum_file(const char *zFilename, int iSize, char *pCksum){
501 FILE *in;
502 SHA3Context ctx;
503 char zBuf[10240];
505 in = fopen(zFilename,"rb");
506 if( in==0 ){
507 return 1;
509 SHA3Init(&ctx, iSize);
510 for(;;){
511 int n = (int)fread(zBuf, 1, sizeof(zBuf), in);
512 if( n<=0 ) break;
513 SHA3Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
515 fclose(in);
516 DigestToBase16(SHA3Final(&ctx), pCksum, iSize/8);
517 return 0;
521 ** The SHA1 implementation below is adapted from:
523 ** $NetBSD: sha1.c,v 1.6 2009/11/06 20:31:18 joerg Exp $
524 ** $OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $
526 ** SHA-1 in C
527 ** By Steve Reid <steve@edmweb.com>
528 ** 100% Public Domain
530 typedef struct SHA1Context SHA1Context;
531 struct SHA1Context {
532 unsigned int state[5];
533 unsigned int count[2];
534 unsigned char buffer[64];
538 * blk0() and blk() perform the initial expand.
539 * I got the idea of expanding during the round function from SSLeay
541 * blk0le() for little-endian and blk0be() for big-endian.
543 #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
544 #define rol(x,k) SHA_ROT(x,k,32-(k))
545 #define ror(x,k) SHA_ROT(x,32-(k),k)
547 #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
548 |(rol(block[i],8)&0x00FF00FF))
549 #define blk0be(i) block[i]
550 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
551 ^block[(i+2)&15]^block[i&15],1))
554 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
556 * Rl0() for little-endian and Rb0() for big-endian. Endianness is
557 * determined at run-time.
559 #define Rl0(v,w,x,y,z,i) \
560 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
561 #define Rb0(v,w,x,y,z,i) \
562 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
563 #define R1(v,w,x,y,z,i) \
564 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
565 #define R2(v,w,x,y,z,i) \
566 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
567 #define R3(v,w,x,y,z,i) \
568 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
569 #define R4(v,w,x,y,z,i) \
570 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
573 * Hash a single 512-bit block. This is the core of the algorithm.
575 #define a qq[0]
576 #define b qq[1]
577 #define c qq[2]
578 #define d qq[3]
579 #define e qq[4]
581 static void SHA1Transform(
582 unsigned int state[5],
583 const unsigned char buffer[64]
585 unsigned int qq[5]; /* a, b, c, d, e; */
586 static int one = 1;
587 unsigned int block[16];
588 memcpy(block, buffer, 64);
589 memcpy(qq,state,5*sizeof(unsigned int));
591 /* Copy context->state[] to working vars */
593 a = state[0];
594 b = state[1];
595 c = state[2];
596 d = state[3];
597 e = state[4];
600 /* 4 rounds of 20 operations each. Loop unrolled. */
601 if( 1 == *(unsigned char*)&one ){
602 Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
603 Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
604 Rl0(c,d,e,a,b, 8); Rl0(b,c,d,e,a, 9); Rl0(a,b,c,d,e,10); Rl0(e,a,b,c,d,11);
605 Rl0(d,e,a,b,c,12); Rl0(c,d,e,a,b,13); Rl0(b,c,d,e,a,14); Rl0(a,b,c,d,e,15);
606 }else{
607 Rb0(a,b,c,d,e, 0); Rb0(e,a,b,c,d, 1); Rb0(d,e,a,b,c, 2); Rb0(c,d,e,a,b, 3);
608 Rb0(b,c,d,e,a, 4); Rb0(a,b,c,d,e, 5); Rb0(e,a,b,c,d, 6); Rb0(d,e,a,b,c, 7);
609 Rb0(c,d,e,a,b, 8); Rb0(b,c,d,e,a, 9); Rb0(a,b,c,d,e,10); Rb0(e,a,b,c,d,11);
610 Rb0(d,e,a,b,c,12); Rb0(c,d,e,a,b,13); Rb0(b,c,d,e,a,14); Rb0(a,b,c,d,e,15);
612 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
613 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
614 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
615 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
616 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
617 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
618 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
619 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
620 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
621 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
622 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
623 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
624 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
625 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
626 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
627 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
629 /* Add the working vars back into context.state[] */
630 state[0] += a;
631 state[1] += b;
632 state[2] += c;
633 state[3] += d;
634 state[4] += e;
639 * SHA1Init - Initialize new context
641 static void SHA1Init(SHA1Context *context){
642 /* SHA1 initialization constants */
643 context->state[0] = 0x67452301;
644 context->state[1] = 0xEFCDAB89;
645 context->state[2] = 0x98BADCFE;
646 context->state[3] = 0x10325476;
647 context->state[4] = 0xC3D2E1F0;
648 context->count[0] = context->count[1] = 0;
653 * Run your data through this.
655 static void SHA1Update(
656 SHA1Context *context,
657 const unsigned char *data,
658 unsigned int len
660 unsigned int i, j;
662 j = context->count[0];
663 if ((context->count[0] += len << 3) < j)
664 context->count[1] += (len>>29)+1;
665 j = (j >> 3) & 63;
666 if ((j + len) > 63) {
667 (void)memcpy(&context->buffer[j], data, (i = 64-j));
668 SHA1Transform(context->state, context->buffer);
669 for ( ; i + 63 < len; i += 64)
670 SHA1Transform(context->state, &data[i]);
671 j = 0;
672 } else {
673 i = 0;
675 (void)memcpy(&context->buffer[j], &data[i], len - i);
680 * Add padding and return the message digest.
682 static void SHA1Final(unsigned char *digest, SHA1Context *context){
683 unsigned int i;
684 unsigned char finalcount[8];
686 for (i = 0; i < 8; i++) {
687 finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
688 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
690 SHA1Update(context, (const unsigned char *)"\200", 1);
691 while ((context->count[0] & 504) != 448)
692 SHA1Update(context, (const unsigned char *)"\0", 1);
693 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
695 if (digest) {
696 for (i = 0; i < 20; i++)
697 digest[i] = (unsigned char)
698 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
704 ** Compute the SHA1 checksum of a file on disk. Store the resulting
705 ** checksum in the blob pCksum. pCksum is assumed to be initialized.
707 ** Return the number of errors.
709 static int sha1sum_file(const char *zFilename, char *pCksum){
710 FILE *in;
711 SHA1Context ctx;
712 unsigned char zResult[20];
713 char zBuf[10240];
715 in = fopen(zFilename,"rb");
716 if( in==0 ){
717 return 1;
719 SHA1Init(&ctx);
720 for(;;){
721 int n = (int)fread(zBuf, 1, sizeof(zBuf), in);
722 if( n<=0 ) break;
723 SHA1Update(&ctx, (unsigned char*)zBuf, (unsigned)n);
725 fclose(in);
726 SHA1Final(zResult, &ctx);
727 DigestToBase16(zResult, pCksum, 20);
728 return 0;
732 ** Print a usage comment and quit.
734 static void usage(const char *argv0){
735 fprintf(stderr,
736 "Usage: %s manifest\n"
737 "Options:\n"
738 " -v Diagnostic output\n"
739 , argv0);
740 exit(1);
744 ** Find the first whitespace character in a string. Set that whitespace
745 ** to a \000 terminator and return a pointer to the next character.
747 static char *nextToken(char *z){
748 while( *z && !isspace(*z) ) z++;
749 if( *z==0 ) return z;
750 *z = 0;
751 return &z[1];
755 int main(int argc, char **argv){
756 const char *zManifest = 0;
757 int i;
758 int bVerbose = 0;
759 FILE *in;
760 int allValid = 1;
761 int rc;
762 SHA3Context ctx;
763 char zDate[50];
764 char zHash[100];
765 char zLine[20000];
767 for(i=1; i<argc; i++){
768 const char *z = argv[i];
769 if( z[0]=='-' ){
770 if( z[1]=='-' ) z++;
771 if( strcmp(z, "-v")==0 ){
772 bVerbose = 1;
773 }else
775 fprintf(stderr, "unknown option \"%s\"", argv[i]);
776 exit(1);
778 }else if( zManifest!=0 ){
779 usage(argv[0]);
780 }else{
781 zManifest = z;
784 if( zManifest==0 ) usage(argv[0]);
785 zDate[0] = 0;
786 in = fopen(zManifest, "rb");
787 if( in==0 ){
788 fprintf(stderr, "cannot open \"%s\" for reading\n", zManifest);
789 exit(1);
791 SHA3Init(&ctx, 256);
792 while( fgets(zLine, sizeof(zLine), in) ){
793 if( strncmp(zLine,"# Remove this line", 18)!=0 ){
794 SHA3Update(&ctx, (unsigned char*)zLine, (unsigned)strlen(zLine));
796 if( strncmp(zLine, "D 20", 4)==0 ){
797 memcpy(zDate, &zLine[2], 10);
798 zDate[10] = ' ';
799 memcpy(&zDate[11], &zLine[13], 8);
800 zDate[19] = 0;
801 continue;
803 if( strncmp(zLine, "F ", 2)==0 ){
804 char *zFilename = &zLine[2];
805 char *zMHash = nextToken(zFilename);
806 nextToken(zMHash);
807 if( strlen(zMHash)==40 ){
808 rc = sha1sum_file(zFilename, zHash);
809 }else{
810 rc = sha3sum_file(zFilename, 256, zHash);
812 if( rc ){
813 allValid = 0;
814 if( bVerbose ){
815 printf("hash failed: %s\n", zFilename);
817 }else if( strcmp(zHash, zMHash)!=0 ){
818 allValid = 0;
819 if( bVerbose ){
820 printf("wrong hash: %s\n", zFilename);
821 printf("... expected: %s\n", zMHash);
822 printf("... got: %s\n", zHash);
827 fclose(in);
828 DigestToBase16(SHA3Final(&ctx), zHash, 256/8);
829 if( !allValid ){
830 printf("%s %.60salt1\n", zDate, zHash);
831 }else{
832 printf("%s %s\n", zDate, zHash);
834 return 0;