initial commit with v2.6.9
[linux-2.6.9-moxart.git] / scripts / mod / sumversion.c
blob86ea6a34d7919c2cb6dbfd2d85ca4065d1692e61
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 "modpost.h"
12 /* Parse tag=value strings from .modinfo section */
13 static char *next_string(char *string, unsigned long *secsize)
15 /* Skip non-zero chars */
16 while (string[0]) {
17 string++;
18 if ((*secsize)-- <= 1)
19 return NULL;
22 /* Skip any zero padding. */
23 while (!string[0]) {
24 string++;
25 if ((*secsize)-- <= 1)
26 return NULL;
28 return string;
31 static char *get_modinfo(void *modinfo, unsigned long modinfo_len,
32 const char *tag)
34 char *p;
35 unsigned int taglen = strlen(tag);
36 unsigned long size = modinfo_len;
38 for (p = modinfo; p; p = next_string(p, &size)) {
39 if (strncmp(p, tag, taglen) == 0 && p[taglen] == '=')
40 return p + taglen + 1;
42 return NULL;
46 * Stolen form Cryptographic API.
48 * MD4 Message Digest Algorithm (RFC1320).
50 * Implementation derived from Andrew Tridgell and Steve French's
51 * CIFS MD4 implementation, and the cryptoapi implementation
52 * originally based on the public domain implementation written
53 * by Colin Plumb in 1993.
55 * Copyright (c) Andrew Tridgell 1997-1998.
56 * Modified by Steve French (sfrench@us.ibm.com) 2002
57 * Copyright (c) Cryptoapi developers.
58 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
59 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
61 * This program is free software; you can redistribute it and/or modify
62 * it under the terms of the GNU General Public License as published by
63 * the Free Software Foundation; either version 2 of the License, or
64 * (at your option) any later version.
67 #define MD4_DIGEST_SIZE 16
68 #define MD4_HMAC_BLOCK_SIZE 64
69 #define MD4_BLOCK_WORDS 16
70 #define MD4_HASH_WORDS 4
72 struct md4_ctx {
73 uint32_t hash[MD4_HASH_WORDS];
74 uint32_t block[MD4_BLOCK_WORDS];
75 uint64_t byte_count;
78 static inline uint32_t lshift(uint32_t x, unsigned int s)
80 x &= 0xFFFFFFFF;
81 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
84 static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
86 return (x & y) | ((~x) & z);
89 static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
91 return (x & y) | (x & z) | (y & z);
94 static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
96 return x ^ y ^ z;
99 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
100 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
101 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
103 /* XXX: this stuff can be optimized */
104 static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
106 while (words--) {
107 *buf = ntohl(*buf);
108 buf++;
112 static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
114 while (words--) {
115 *buf = htonl(*buf);
116 buf++;
120 static void md4_transform(uint32_t *hash, uint32_t const *in)
122 uint32_t a, b, c, d;
124 a = hash[0];
125 b = hash[1];
126 c = hash[2];
127 d = hash[3];
129 ROUND1(a, b, c, d, in[0], 3);
130 ROUND1(d, a, b, c, in[1], 7);
131 ROUND1(c, d, a, b, in[2], 11);
132 ROUND1(b, c, d, a, in[3], 19);
133 ROUND1(a, b, c, d, in[4], 3);
134 ROUND1(d, a, b, c, in[5], 7);
135 ROUND1(c, d, a, b, in[6], 11);
136 ROUND1(b, c, d, a, in[7], 19);
137 ROUND1(a, b, c, d, in[8], 3);
138 ROUND1(d, a, b, c, in[9], 7);
139 ROUND1(c, d, a, b, in[10], 11);
140 ROUND1(b, c, d, a, in[11], 19);
141 ROUND1(a, b, c, d, in[12], 3);
142 ROUND1(d, a, b, c, in[13], 7);
143 ROUND1(c, d, a, b, in[14], 11);
144 ROUND1(b, c, d, a, in[15], 19);
146 ROUND2(a, b, c, d,in[ 0], 3);
147 ROUND2(d, a, b, c, in[4], 5);
148 ROUND2(c, d, a, b, in[8], 9);
149 ROUND2(b, c, d, a, in[12], 13);
150 ROUND2(a, b, c, d, in[1], 3);
151 ROUND2(d, a, b, c, in[5], 5);
152 ROUND2(c, d, a, b, in[9], 9);
153 ROUND2(b, c, d, a, in[13], 13);
154 ROUND2(a, b, c, d, in[2], 3);
155 ROUND2(d, a, b, c, in[6], 5);
156 ROUND2(c, d, a, b, in[10], 9);
157 ROUND2(b, c, d, a, in[14], 13);
158 ROUND2(a, b, c, d, in[3], 3);
159 ROUND2(d, a, b, c, in[7], 5);
160 ROUND2(c, d, a, b, in[11], 9);
161 ROUND2(b, c, d, a, in[15], 13);
163 ROUND3(a, b, c, d,in[ 0], 3);
164 ROUND3(d, a, b, c, in[8], 9);
165 ROUND3(c, d, a, b, in[4], 11);
166 ROUND3(b, c, d, a, in[12], 15);
167 ROUND3(a, b, c, d, in[2], 3);
168 ROUND3(d, a, b, c, in[10], 9);
169 ROUND3(c, d, a, b, in[6], 11);
170 ROUND3(b, c, d, a, in[14], 15);
171 ROUND3(a, b, c, d, in[1], 3);
172 ROUND3(d, a, b, c, in[9], 9);
173 ROUND3(c, d, a, b, in[5], 11);
174 ROUND3(b, c, d, a, in[13], 15);
175 ROUND3(a, b, c, d, in[3], 3);
176 ROUND3(d, a, b, c, in[11], 9);
177 ROUND3(c, d, a, b, in[7], 11);
178 ROUND3(b, c, d, a, in[15], 15);
180 hash[0] += a;
181 hash[1] += b;
182 hash[2] += c;
183 hash[3] += d;
186 static inline void md4_transform_helper(struct md4_ctx *ctx)
188 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
189 md4_transform(ctx->hash, ctx->block);
192 static void md4_init(struct md4_ctx *mctx)
194 mctx->hash[0] = 0x67452301;
195 mctx->hash[1] = 0xefcdab89;
196 mctx->hash[2] = 0x98badcfe;
197 mctx->hash[3] = 0x10325476;
198 mctx->byte_count = 0;
201 static void md4_update(struct md4_ctx *mctx,
202 const unsigned char *data, unsigned int len)
204 const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
206 mctx->byte_count += len;
208 if (avail > len) {
209 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
210 data, len);
211 return;
214 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
215 data, avail);
217 md4_transform_helper(mctx);
218 data += avail;
219 len -= avail;
221 while (len >= sizeof(mctx->block)) {
222 memcpy(mctx->block, data, sizeof(mctx->block));
223 md4_transform_helper(mctx);
224 data += sizeof(mctx->block);
225 len -= sizeof(mctx->block);
228 memcpy(mctx->block, data, len);
231 static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
233 const unsigned int offset = mctx->byte_count & 0x3f;
234 char *p = (char *)mctx->block + offset;
235 int padding = 56 - (offset + 1);
237 *p++ = 0x80;
238 if (padding < 0) {
239 memset(p, 0x00, padding + sizeof (uint64_t));
240 md4_transform_helper(mctx);
241 p = (char *)mctx->block;
242 padding = 56;
245 memset(p, 0, padding);
246 mctx->block[14] = mctx->byte_count << 3;
247 mctx->block[15] = mctx->byte_count >> 29;
248 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
249 sizeof(uint64_t)) / sizeof(uint32_t));
250 md4_transform(mctx->hash, mctx->block);
251 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
253 snprintf(out, len, "%08X%08X%08X%08X",
254 mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
257 static inline void add_char(unsigned char c, struct md4_ctx *md)
259 md4_update(md, &c, 1);
262 static int parse_string(const char *file, unsigned long len,
263 struct md4_ctx *md)
265 unsigned long i;
267 add_char(file[0], md);
268 for (i = 1; i < len; i++) {
269 add_char(file[i], md);
270 if (file[i] == '"' && file[i-1] != '\\')
271 break;
273 return i;
276 static int parse_comment(const char *file, unsigned long len)
278 unsigned long i;
280 for (i = 2; i < len; i++) {
281 if (file[i-1] == '*' && file[i] == '/')
282 break;
284 return i;
287 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
288 static int parse_file(const char *fname, struct md4_ctx *md)
290 char *file;
291 unsigned long i, len;
293 file = grab_file(fname, &len);
294 if (!file)
295 return 0;
297 for (i = 0; i < len; i++) {
298 /* Collapse and ignore \ and CR. */
299 if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
300 i++;
301 continue;
304 /* Ignore whitespace */
305 if (isspace(file[i]))
306 continue;
308 /* Handle strings as whole units */
309 if (file[i] == '"') {
310 i += parse_string(file+i, len - i, md);
311 continue;
314 /* Comments: ignore */
315 if (file[i] == '/' && file[i+1] == '*') {
316 i += parse_comment(file+i, len - i);
317 continue;
320 add_char(file[i], md);
322 release_file(file, len);
323 return 1;
326 /* We have dir/file.o. Open dir/.file.o.cmd, look for deps_ line to
327 * figure out source file. */
328 static int parse_source_files(const char *objfile, struct md4_ctx *md)
330 char *cmd, *file, *line, *dir;
331 const char *base;
332 unsigned long flen, pos = 0;
333 int dirlen, ret = 0, check_files = 0;
335 cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd")));
337 base = strrchr(objfile, '/');
338 if (base) {
339 base++;
340 dirlen = base - objfile;
341 sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
342 } else {
343 dirlen = 0;
344 sprintf(cmd, ".%s.cmd", objfile);
346 dir = NOFAIL(malloc(dirlen + 1));
347 strncpy(dir, objfile, dirlen);
348 dir[dirlen] = '\0';
350 file = grab_file(cmd, &flen);
351 if (!file) {
352 fprintf(stderr, "Warning: could not find %s for %s\n",
353 cmd, objfile);
354 goto out;
357 /* There will be a line like so:
358 deps_drivers/net/dummy.o := \
359 drivers/net/dummy.c \
360 $(wildcard include/config/net/fastroute.h) \
361 include/linux/config.h \
362 $(wildcard include/config/h.h) \
363 include/linux/module.h \
365 Sum all files in the same dir or subdirs.
367 while ((line = get_next_line(&pos, file, flen)) != NULL) {
368 char* p = line;
369 if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
370 check_files = 1;
371 continue;
373 if (!check_files)
374 continue;
376 /* Continue until line does not end with '\' */
377 if ( *(p + strlen(p)-1) != '\\')
378 break;
379 /* Terminate line at first space, to get rid of final ' \' */
380 while (*p) {
381 if (isspace(*p)) {
382 *p = '\0';
383 break;
385 p++;
388 /* Check if this file is in same dir as objfile */
389 if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
390 if (!parse_file(line, md)) {
391 fprintf(stderr,
392 "Warning: could not open %s: %s\n",
393 line, strerror(errno));
394 goto out_file;
401 /* Everyone parsed OK */
402 ret = 1;
403 out_file:
404 release_file(file, flen);
405 out:
406 free(dir);
407 free(cmd);
408 return ret;
411 static int get_version(const char *modname, char sum[])
413 void *file;
414 unsigned long len;
415 int ret = 0;
416 struct md4_ctx md;
417 char *sources, *end, *fname;
418 const char *basename;
419 char filelist[sizeof(".tmp_versions/%s.mod") + strlen(modname)];
421 /* Source files for module are in .tmp_versions/modname.mod,
422 after the first line. */
423 if (strrchr(modname, '/'))
424 basename = strrchr(modname, '/') + 1;
425 else
426 basename = modname;
427 sprintf(filelist, ".tmp_versions/%s", basename);
428 /* Truncate .o, add .mod */
429 strcpy(filelist + strlen(filelist)-2, ".mod");
431 file = grab_file(filelist, &len);
432 if (!file) {
433 fprintf(stderr, "Warning: could not find versions for %s\n",
434 filelist);
435 return 0;
438 sources = strchr(file, '\n');
439 if (!sources) {
440 fprintf(stderr, "Warning: malformed versions file for %s\n",
441 modname);
442 goto release;
445 sources++;
446 end = strchr(sources, '\n');
447 if (!end) {
448 fprintf(stderr, "Warning: bad ending versions file for %s\n",
449 modname);
450 goto release;
452 *end = '\0';
454 md4_init(&md);
455 for (fname = strtok(sources, " "); fname; fname = strtok(NULL, " ")) {
456 if (!parse_source_files(fname, &md))
457 goto release;
460 /* sum is of form \0<padding>. */
461 md4_final_ascii(&md, sum, 1 + strlen(sum+1));
462 ret = 1;
463 release:
464 release_file(file, len);
465 return ret;
468 static void write_version(const char *filename, const char *sum,
469 unsigned long offset)
471 int fd;
473 fd = open(filename, O_RDWR);
474 if (fd < 0) {
475 fprintf(stderr, "Warning: changing sum in %s failed: %s\n",
476 filename, strerror(errno));
477 return;
480 if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
481 fprintf(stderr, "Warning: changing sum in %s:%lu failed: %s\n",
482 filename, offset, strerror(errno));
483 goto out;
486 if (write(fd, sum, strlen(sum)+1) != strlen(sum)+1) {
487 fprintf(stderr, "Warning: writing sum in %s failed: %s\n",
488 filename, strerror(errno));
489 goto out;
491 out:
492 close(fd);
495 void strip_rcs_crap(char *version)
497 unsigned int len, full_len;
499 if (strncmp(version, "$Revision", strlen("$Revision")) != 0)
500 return;
502 /* Space for version string follows. */
503 full_len = strlen(version) + strlen(version + strlen(version) + 1) + 2;
505 /* Move string to start with version number: prefix will be
506 * $Revision$ or $Revision: */
507 len = strlen("$Revision");
508 if (version[len] == ':' || version[len] == '$')
509 len++;
510 while (isspace(version[len]))
511 len++;
512 memmove(version, version+len, full_len-len);
513 full_len -= len;
515 /* Preserve up to next whitespace. */
516 len = 0;
517 while (version[len] && !isspace(version[len]))
518 len++;
519 memmove(version + len, version + strlen(version),
520 full_len - strlen(version));
523 /* If the modinfo contains a "version" value, then set this. */
524 void maybe_frob_version(const char *modfilename,
525 void *modinfo,
526 unsigned long modinfo_len,
527 unsigned long modinfo_offset)
529 char *version, *csum;
531 version = get_modinfo(modinfo, modinfo_len, "version");
532 if (!version)
533 return;
535 /* RCS $Revision gets stripped out. */
536 strip_rcs_crap(version);
538 /* Check against double sumversion */
539 if (strchr(version, ' '))
540 return;
542 /* Version contains embedded NUL: second half has space for checksum */
543 csum = version + strlen(version);
544 *(csum++) = ' ';
545 if (get_version(modfilename, csum))
546 write_version(modfilename, version,
547 modinfo_offset + (version - (char *)modinfo));