sumversion.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include <netinet/in.h>
  2. #ifdef __sun__
  3. #include <inttypes.h>
  4. #else
  5. #include <stdint.h>
  6. #endif
  7. #include <ctype.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #include <limits.h>
  11. #include "modpost.h"
  12. /*
  13. * Stolen form Cryptographic API.
  14. *
  15. * MD4 Message Digest Algorithm (RFC1320).
  16. *
  17. * Implementation derived from Andrew Tridgell and Steve French's
  18. * CIFS MD4 implementation, and the cryptoapi implementation
  19. * originally based on the public domain implementation written
  20. * by Colin Plumb in 1993.
  21. *
  22. * Copyright (c) Andrew Tridgell 1997-1998.
  23. * Modified by Steve French (sfrench@us.ibm.com) 2002
  24. * Copyright (c) Cryptoapi developers.
  25. * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  26. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  27. *
  28. * This program is free software; you can redistribute it and/or modify
  29. * it under the terms of the GNU General Public License as published by
  30. * the Free Software Foundation; either version 2 of the License, or
  31. * (at your option) any later version.
  32. *
  33. */
  34. #define MD4_DIGEST_SIZE 16
  35. #define MD4_HMAC_BLOCK_SIZE 64
  36. #define MD4_BLOCK_WORDS 16
  37. #define MD4_HASH_WORDS 4
  38. struct md4_ctx {
  39. uint32_t hash[MD4_HASH_WORDS];
  40. uint32_t block[MD4_BLOCK_WORDS];
  41. uint64_t byte_count;
  42. };
  43. static inline uint32_t lshift(uint32_t x, unsigned int s)
  44. {
  45. x &= 0xFFFFFFFF;
  46. return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
  47. }
  48. static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
  49. {
  50. return (x & y) | ((~x) & z);
  51. }
  52. static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
  53. {
  54. return (x & y) | (x & z) | (y & z);
  55. }
  56. static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
  57. {
  58. return x ^ y ^ z;
  59. }
  60. #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
  61. #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
  62. #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
  63. /* XXX: this stuff can be optimized */
  64. static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
  65. {
  66. while (words--) {
  67. *buf = ntohl(*buf);
  68. buf++;
  69. }
  70. }
  71. static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
  72. {
  73. while (words--) {
  74. *buf = htonl(*buf);
  75. buf++;
  76. }
  77. }
  78. static void md4_transform(uint32_t *hash, uint32_t const *in)
  79. {
  80. uint32_t a, b, c, d;
  81. a = hash[0];
  82. b = hash[1];
  83. c = hash[2];
  84. d = hash[3];
  85. ROUND1(a, b, c, d, in[0], 3);
  86. ROUND1(d, a, b, c, in[1], 7);
  87. ROUND1(c, d, a, b, in[2], 11);
  88. ROUND1(b, c, d, a, in[3], 19);
  89. ROUND1(a, b, c, d, in[4], 3);
  90. ROUND1(d, a, b, c, in[5], 7);
  91. ROUND1(c, d, a, b, in[6], 11);
  92. ROUND1(b, c, d, a, in[7], 19);
  93. ROUND1(a, b, c, d, in[8], 3);
  94. ROUND1(d, a, b, c, in[9], 7);
  95. ROUND1(c, d, a, b, in[10], 11);
  96. ROUND1(b, c, d, a, in[11], 19);
  97. ROUND1(a, b, c, d, in[12], 3);
  98. ROUND1(d, a, b, c, in[13], 7);
  99. ROUND1(c, d, a, b, in[14], 11);
  100. ROUND1(b, c, d, a, in[15], 19);
  101. ROUND2(a, b, c, d,in[ 0], 3);
  102. ROUND2(d, a, b, c, in[4], 5);
  103. ROUND2(c, d, a, b, in[8], 9);
  104. ROUND2(b, c, d, a, in[12], 13);
  105. ROUND2(a, b, c, d, in[1], 3);
  106. ROUND2(d, a, b, c, in[5], 5);
  107. ROUND2(c, d, a, b, in[9], 9);
  108. ROUND2(b, c, d, a, in[13], 13);
  109. ROUND2(a, b, c, d, in[2], 3);
  110. ROUND2(d, a, b, c, in[6], 5);
  111. ROUND2(c, d, a, b, in[10], 9);
  112. ROUND2(b, c, d, a, in[14], 13);
  113. ROUND2(a, b, c, d, in[3], 3);
  114. ROUND2(d, a, b, c, in[7], 5);
  115. ROUND2(c, d, a, b, in[11], 9);
  116. ROUND2(b, c, d, a, in[15], 13);
  117. ROUND3(a, b, c, d,in[ 0], 3);
  118. ROUND3(d, a, b, c, in[8], 9);
  119. ROUND3(c, d, a, b, in[4], 11);
  120. ROUND3(b, c, d, a, in[12], 15);
  121. ROUND3(a, b, c, d, in[2], 3);
  122. ROUND3(d, a, b, c, in[10], 9);
  123. ROUND3(c, d, a, b, in[6], 11);
  124. ROUND3(b, c, d, a, in[14], 15);
  125. ROUND3(a, b, c, d, in[1], 3);
  126. ROUND3(d, a, b, c, in[9], 9);
  127. ROUND3(c, d, a, b, in[5], 11);
  128. ROUND3(b, c, d, a, in[13], 15);
  129. ROUND3(a, b, c, d, in[3], 3);
  130. ROUND3(d, a, b, c, in[11], 9);
  131. ROUND3(c, d, a, b, in[7], 11);
  132. ROUND3(b, c, d, a, in[15], 15);
  133. hash[0] += a;
  134. hash[1] += b;
  135. hash[2] += c;
  136. hash[3] += d;
  137. }
  138. static inline void md4_transform_helper(struct md4_ctx *ctx)
  139. {
  140. le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
  141. md4_transform(ctx->hash, ctx->block);
  142. }
  143. static void md4_init(struct md4_ctx *mctx)
  144. {
  145. mctx->hash[0] = 0x67452301;
  146. mctx->hash[1] = 0xefcdab89;
  147. mctx->hash[2] = 0x98badcfe;
  148. mctx->hash[3] = 0x10325476;
  149. mctx->byte_count = 0;
  150. }
  151. static void md4_update(struct md4_ctx *mctx,
  152. const unsigned char *data, unsigned int len)
  153. {
  154. const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
  155. mctx->byte_count += len;
  156. if (avail > len) {
  157. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  158. data, len);
  159. return;
  160. }
  161. memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
  162. data, avail);
  163. md4_transform_helper(mctx);
  164. data += avail;
  165. len -= avail;
  166. while (len >= sizeof(mctx->block)) {
  167. memcpy(mctx->block, data, sizeof(mctx->block));
  168. md4_transform_helper(mctx);
  169. data += sizeof(mctx->block);
  170. len -= sizeof(mctx->block);
  171. }
  172. memcpy(mctx->block, data, len);
  173. }
  174. static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
  175. {
  176. const unsigned int offset = mctx->byte_count & 0x3f;
  177. char *p = (char *)mctx->block + offset;
  178. int padding = 56 - (offset + 1);
  179. *p++ = 0x80;
  180. if (padding < 0) {
  181. memset(p, 0x00, padding + sizeof (uint64_t));
  182. md4_transform_helper(mctx);
  183. p = (char *)mctx->block;
  184. padding = 56;
  185. }
  186. memset(p, 0, padding);
  187. mctx->block[14] = mctx->byte_count << 3;
  188. mctx->block[15] = mctx->byte_count >> 29;
  189. le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
  190. sizeof(uint64_t)) / sizeof(uint32_t));
  191. md4_transform(mctx->hash, mctx->block);
  192. cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
  193. snprintf(out, len, "%08X%08X%08X%08X",
  194. mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
  195. }
  196. static inline void add_char(unsigned char c, struct md4_ctx *md)
  197. {
  198. md4_update(md, &c, 1);
  199. }
  200. static int parse_string(const char *file, unsigned long len,
  201. struct md4_ctx *md)
  202. {
  203. unsigned long i;
  204. add_char(file[0], md);
  205. for (i = 1; i < len; i++) {
  206. add_char(file[i], md);
  207. if (file[i] == '"' && file[i-1] != '\\')
  208. break;
  209. }
  210. return i;
  211. }
  212. static int parse_comment(const char *file, unsigned long len)
  213. {
  214. unsigned long i;
  215. for (i = 2; i < len; i++) {
  216. if (file[i-1] == '*' && file[i] == '/')
  217. break;
  218. }
  219. return i;
  220. }
  221. /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
  222. static int parse_file(const char *fname, struct md4_ctx *md)
  223. {
  224. char *file;
  225. unsigned long i, len;
  226. file = grab_file(fname, &len);
  227. if (!file)
  228. return 0;
  229. for (i = 0; i < len; i++) {
  230. /* Collapse and ignore \ and CR. */
  231. if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
  232. i++;
  233. continue;
  234. }
  235. /* Ignore whitespace */
  236. if (isspace(file[i]))
  237. continue;
  238. /* Handle strings as whole units */
  239. if (file[i] == '"') {
  240. i += parse_string(file+i, len - i, md);
  241. continue;
  242. }
  243. /* Comments: ignore */
  244. if (file[i] == '/' && file[i+1] == '*') {
  245. i += parse_comment(file+i, len - i);
  246. continue;
  247. }
  248. add_char(file[i], md);
  249. }
  250. release_file(file, len);
  251. return 1;
  252. }
  253. /* Check whether the file is a static library or not */
  254. static int is_static_library(const char *objfile)
  255. {
  256. int len = strlen(objfile);
  257. if (objfile[len - 2] == '.' && objfile[len - 1] == 'a')
  258. return 1;
  259. else
  260. return 0;
  261. }
  262. /* We have dir/file.o. Open dir/.file.o.cmd, look for source_ and deps_ line
  263. * to figure out source files. */
  264. static int parse_source_files(const char *objfile, struct md4_ctx *md)
  265. {
  266. char *cmd, *file, *line, *dir;
  267. const char *base;
  268. unsigned long flen, pos = 0;
  269. int dirlen, ret = 0, check_files = 0;
  270. cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd")));
  271. base = strrchr(objfile, '/');
  272. if (base) {
  273. base++;
  274. dirlen = base - objfile;
  275. sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
  276. } else {
  277. dirlen = 0;
  278. sprintf(cmd, ".%s.cmd", objfile);
  279. }
  280. dir = NOFAIL(malloc(dirlen + 1));
  281. strncpy(dir, objfile, dirlen);
  282. dir[dirlen] = '\0';
  283. file = grab_file(cmd, &flen);
  284. if (!file) {
  285. warn("could not find %s for %s\n", cmd, objfile);
  286. goto out;
  287. }
  288. /* Sum all files in the same dir or subdirs. */
  289. while ((line = get_next_line(&pos, file, flen)) != NULL) {
  290. char* p = line;
  291. if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
  292. p = strrchr(line, ' ');
  293. if (!p) {
  294. warn("malformed line: %s\n", line);
  295. goto out_file;
  296. }
  297. p++;
  298. if (!parse_file(p, md)) {
  299. warn("could not open %s: %s\n",
  300. p, strerror(errno));
  301. goto out_file;
  302. }
  303. continue;
  304. }
  305. if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
  306. check_files = 1;
  307. continue;
  308. }
  309. if (!check_files)
  310. continue;
  311. /* Continue until line does not end with '\' */
  312. if ( *(p + strlen(p)-1) != '\\')
  313. break;
  314. /* Terminate line at first space, to get rid of final ' \' */
  315. while (*p) {
  316. if (isspace(*p)) {
  317. *p = '\0';
  318. break;
  319. }
  320. p++;
  321. }
  322. /* Check if this file is in same dir as objfile */
  323. if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
  324. if (!parse_file(line, md)) {
  325. warn("could not open %s: %s\n",
  326. line, strerror(errno));
  327. goto out_file;
  328. }
  329. }
  330. }
  331. /* Everyone parsed OK */
  332. ret = 1;
  333. out_file:
  334. release_file(file, flen);
  335. out:
  336. free(dir);
  337. free(cmd);
  338. return ret;
  339. }
  340. /* Calc and record src checksum. */
  341. void get_src_version(const char *modname, char sum[], unsigned sumlen)
  342. {
  343. void *file;
  344. unsigned long len;
  345. struct md4_ctx md;
  346. char *sources, *end, *fname;
  347. const char *basename;
  348. char filelist[PATH_MAX + 1];
  349. char *modverdir = getenv("MODVERDIR");
  350. if (!modverdir)
  351. modverdir = ".";
  352. /* Source files for module are in .tmp_versions/modname.mod,
  353. after the first line. */
  354. if (strrchr(modname, '/'))
  355. basename = strrchr(modname, '/') + 1;
  356. else
  357. basename = modname;
  358. snprintf(filelist, sizeof(filelist), "%s/%.*s.mod", modverdir,
  359. (int) strlen(basename) - 2, basename);
  360. file = grab_file(filelist, &len);
  361. if (!file)
  362. /* not a module or .mod file missing - ignore */
  363. return;
  364. sources = strchr(file, '\n');
  365. if (!sources) {
  366. warn("malformed versions file for %s\n", modname);
  367. goto release;
  368. }
  369. sources++;
  370. end = strchr(sources, '\n');
  371. if (!end) {
  372. warn("bad ending versions file for %s\n", modname);
  373. goto release;
  374. }
  375. *end = '\0';
  376. md4_init(&md);
  377. while ((fname = strsep(&sources, " ")) != NULL) {
  378. if (!*fname)
  379. continue;
  380. if (!(is_static_library(fname)) &&
  381. !parse_source_files(fname, &md))
  382. goto release;
  383. }
  384. md4_final_ascii(&md, sum, sumlen);
  385. release:
  386. release_file(file, len);
  387. }
  388. static void write_version(const char *filename, const char *sum,
  389. unsigned long offset)
  390. {
  391. int fd;
  392. fd = open(filename, O_RDWR);
  393. if (fd < 0) {
  394. warn("changing sum in %s failed: %s\n",
  395. filename, strerror(errno));
  396. return;
  397. }
  398. if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
  399. warn("changing sum in %s:%lu failed: %s\n",
  400. filename, offset, strerror(errno));
  401. goto out;
  402. }
  403. if (write(fd, sum, strlen(sum)+1) != strlen(sum)+1) {
  404. warn("writing sum in %s failed: %s\n",
  405. filename, strerror(errno));
  406. goto out;
  407. }
  408. out:
  409. close(fd);
  410. }
  411. static int strip_rcs_crap(char *version)
  412. {
  413. unsigned int len, full_len;
  414. if (strncmp(version, "$Revision", strlen("$Revision")) != 0)
  415. return 0;
  416. /* Space for version string follows. */
  417. full_len = strlen(version) + strlen(version + strlen(version) + 1) + 2;
  418. /* Move string to start with version number: prefix will be
  419. * $Revision$ or $Revision: */
  420. len = strlen("$Revision");
  421. if (version[len] == ':' || version[len] == '$')
  422. len++;
  423. while (isspace(version[len]))
  424. len++;
  425. memmove(version, version+len, full_len-len);
  426. full_len -= len;
  427. /* Preserve up to next whitespace. */
  428. len = 0;
  429. while (version[len] && !isspace(version[len]))
  430. len++;
  431. memmove(version + len, version + strlen(version),
  432. full_len - strlen(version));
  433. return 1;
  434. }
  435. /* Clean up RCS-style version numbers. */
  436. void maybe_frob_rcs_version(const char *modfilename,
  437. char *version,
  438. void *modinfo,
  439. unsigned long version_offset)
  440. {
  441. if (strip_rcs_crap(version))
  442. write_version(modfilename, version, version_offset);
  443. }