mksourceid.c 23 KB

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