net: rds: use correct size for max unacked packets and bytes
[linux/fpc-iii.git] / scripts / mod / sumversion.c
blob944418da9fe3369efd596e44fd53ffaffc0f80bd
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"
14 * Stolen form Cryptographic API.
16 * MD4 Message Digest Algorithm (RFC1320).
18 * Implementation derived from Andrew Tridgell and Steve French's
19 * CIFS MD4 implementation, and the cryptoapi implementation
20 * originally based on the public domain implementation written
21 * by Colin Plumb in 1993.
23 * Copyright (c) Andrew Tridgell 1997-1998.
24 * Modified by Steve French (sfrench@us.ibm.com) 2002
25 * Copyright (c) Cryptoapi developers.
26 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
27 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
35 #define MD4_DIGEST_SIZE 16
36 #define MD4_HMAC_BLOCK_SIZE 64
37 #define MD4_BLOCK_WORDS 16
38 #define MD4_HASH_WORDS 4
40 struct md4_ctx {
41 uint32_t hash[MD4_HASH_WORDS];
42 uint32_t block[MD4_BLOCK_WORDS];
43 uint64_t byte_count;
46 static inline uint32_t lshift(uint32_t x, unsigned int s)
48 x &= 0xFFFFFFFF;
49 return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s));
52 static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z)
54 return (x & y) | ((~x) & z);
57 static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z)
59 return (x & y) | (x & z) | (y & z);
62 static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z)
64 return x ^ y ^ z;
67 #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s))
68 #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s))
69 #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s))
71 /* XXX: this stuff can be optimized */
72 static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words)
74 while (words--) {
75 *buf = ntohl(*buf);
76 buf++;
80 static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words)
82 while (words--) {
83 *buf = htonl(*buf);
84 buf++;
88 static void md4_transform(uint32_t *hash, uint32_t const *in)
90 uint32_t a, b, c, d;
92 a = hash[0];
93 b = hash[1];
94 c = hash[2];
95 d = hash[3];
97 ROUND1(a, b, c, d, in[0], 3);
98 ROUND1(d, a, b, c, in[1], 7);
99 ROUND1(c, d, a, b, in[2], 11);
100 ROUND1(b, c, d, a, in[3], 19);
101 ROUND1(a, b, c, d, in[4], 3);
102 ROUND1(d, a, b, c, in[5], 7);
103 ROUND1(c, d, a, b, in[6], 11);
104 ROUND1(b, c, d, a, in[7], 19);
105 ROUND1(a, b, c, d, in[8], 3);
106 ROUND1(d, a, b, c, in[9], 7);
107 ROUND1(c, d, a, b, in[10], 11);
108 ROUND1(b, c, d, a, in[11], 19);
109 ROUND1(a, b, c, d, in[12], 3);
110 ROUND1(d, a, b, c, in[13], 7);
111 ROUND1(c, d, a, b, in[14], 11);
112 ROUND1(b, c, d, a, in[15], 19);
114 ROUND2(a, b, c, d,in[ 0], 3);
115 ROUND2(d, a, b, c, in[4], 5);
116 ROUND2(c, d, a, b, in[8], 9);
117 ROUND2(b, c, d, a, in[12], 13);
118 ROUND2(a, b, c, d, in[1], 3);
119 ROUND2(d, a, b, c, in[5], 5);
120 ROUND2(c, d, a, b, in[9], 9);
121 ROUND2(b, c, d, a, in[13], 13);
122 ROUND2(a, b, c, d, in[2], 3);
123 ROUND2(d, a, b, c, in[6], 5);
124 ROUND2(c, d, a, b, in[10], 9);
125 ROUND2(b, c, d, a, in[14], 13);
126 ROUND2(a, b, c, d, in[3], 3);
127 ROUND2(d, a, b, c, in[7], 5);
128 ROUND2(c, d, a, b, in[11], 9);
129 ROUND2(b, c, d, a, in[15], 13);
131 ROUND3(a, b, c, d,in[ 0], 3);
132 ROUND3(d, a, b, c, in[8], 9);
133 ROUND3(c, d, a, b, in[4], 11);
134 ROUND3(b, c, d, a, in[12], 15);
135 ROUND3(a, b, c, d, in[2], 3);
136 ROUND3(d, a, b, c, in[10], 9);
137 ROUND3(c, d, a, b, in[6], 11);
138 ROUND3(b, c, d, a, in[14], 15);
139 ROUND3(a, b, c, d, in[1], 3);
140 ROUND3(d, a, b, c, in[9], 9);
141 ROUND3(c, d, a, b, in[5], 11);
142 ROUND3(b, c, d, a, in[13], 15);
143 ROUND3(a, b, c, d, in[3], 3);
144 ROUND3(d, a, b, c, in[11], 9);
145 ROUND3(c, d, a, b, in[7], 11);
146 ROUND3(b, c, d, a, in[15], 15);
148 hash[0] += a;
149 hash[1] += b;
150 hash[2] += c;
151 hash[3] += d;
154 static inline void md4_transform_helper(struct md4_ctx *ctx)
156 le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t));
157 md4_transform(ctx->hash, ctx->block);
160 static void md4_init(struct md4_ctx *mctx)
162 mctx->hash[0] = 0x67452301;
163 mctx->hash[1] = 0xefcdab89;
164 mctx->hash[2] = 0x98badcfe;
165 mctx->hash[3] = 0x10325476;
166 mctx->byte_count = 0;
169 static void md4_update(struct md4_ctx *mctx,
170 const unsigned char *data, unsigned int len)
172 const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
174 mctx->byte_count += len;
176 if (avail > len) {
177 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
178 data, len);
179 return;
182 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
183 data, avail);
185 md4_transform_helper(mctx);
186 data += avail;
187 len -= avail;
189 while (len >= sizeof(mctx->block)) {
190 memcpy(mctx->block, data, sizeof(mctx->block));
191 md4_transform_helper(mctx);
192 data += sizeof(mctx->block);
193 len -= sizeof(mctx->block);
196 memcpy(mctx->block, data, len);
199 static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len)
201 const unsigned int offset = mctx->byte_count & 0x3f;
202 char *p = (char *)mctx->block + offset;
203 int padding = 56 - (offset + 1);
205 *p++ = 0x80;
206 if (padding < 0) {
207 memset(p, 0x00, padding + sizeof (uint64_t));
208 md4_transform_helper(mctx);
209 p = (char *)mctx->block;
210 padding = 56;
213 memset(p, 0, padding);
214 mctx->block[14] = mctx->byte_count << 3;
215 mctx->block[15] = mctx->byte_count >> 29;
216 le32_to_cpu_array(mctx->block, (sizeof(mctx->block) -
217 sizeof(uint64_t)) / sizeof(uint32_t));
218 md4_transform(mctx->hash, mctx->block);
219 cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t));
221 snprintf(out, len, "%08X%08X%08X%08X",
222 mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]);
225 static inline void add_char(unsigned char c, struct md4_ctx *md)
227 md4_update(md, &c, 1);
230 static int parse_string(const char *file, unsigned long len,
231 struct md4_ctx *md)
233 unsigned long i;
235 add_char(file[0], md);
236 for (i = 1; i < len; i++) {
237 add_char(file[i], md);
238 if (file[i] == '"' && file[i-1] != '\\')
239 break;
241 return i;
244 static int parse_comment(const char *file, unsigned long len)
246 unsigned long i;
248 for (i = 2; i < len; i++) {
249 if (file[i-1] == '*' && file[i] == '/')
250 break;
252 return i;
255 /* FIXME: Handle .s files differently (eg. # starts comments) --RR */
256 static int parse_file(const char *fname, struct md4_ctx *md)
258 char *file;
259 unsigned long i, len;
261 file = grab_file(fname, &len);
262 if (!file)
263 return 0;
265 for (i = 0; i < len; i++) {
266 /* Collapse and ignore \ and CR. */
267 if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') {
268 i++;
269 continue;
272 /* Ignore whitespace */
273 if (isspace(file[i]))
274 continue;
276 /* Handle strings as whole units */
277 if (file[i] == '"') {
278 i += parse_string(file+i, len - i, md);
279 continue;
282 /* Comments: ignore */
283 if (file[i] == '/' && file[i+1] == '*') {
284 i += parse_comment(file+i, len - i);
285 continue;
288 add_char(file[i], md);
290 release_file(file, len);
291 return 1;
293 /* Check whether the file is a static library or not */
294 static int is_static_library(const char *objfile)
296 int len = strlen(objfile);
297 if (objfile[len - 2] == '.' && objfile[len - 1] == 'a')
298 return 1;
299 else
300 return 0;
303 /* We have dir/file.o. Open dir/.file.o.cmd, look for source_ and deps_ line
304 * to figure out source files. */
305 static int parse_source_files(const char *objfile, struct md4_ctx *md)
307 char *cmd, *file, *line, *dir;
308 const char *base;
309 unsigned long flen, pos = 0;
310 int dirlen, ret = 0, check_files = 0;
312 cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd")));
314 base = strrchr(objfile, '/');
315 if (base) {
316 base++;
317 dirlen = base - objfile;
318 sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base);
319 } else {
320 dirlen = 0;
321 sprintf(cmd, ".%s.cmd", objfile);
323 dir = NOFAIL(malloc(dirlen + 1));
324 strncpy(dir, objfile, dirlen);
325 dir[dirlen] = '\0';
327 file = grab_file(cmd, &flen);
328 if (!file) {
329 warn("could not find %s for %s\n", cmd, objfile);
330 goto out;
333 /* There will be a line like so:
334 deps_drivers/net/dummy.o := \
335 drivers/net/dummy.c \
336 $(wildcard include/config/net/fastroute.h) \
337 include/linux/module.h \
339 Sum all files in the same dir or subdirs.
341 while ((line = get_next_line(&pos, file, flen)) != NULL) {
342 char* p = line;
344 if (strncmp(line, "source_", sizeof("source_")-1) == 0) {
345 p = strrchr(line, ' ');
346 if (!p) {
347 warn("malformed line: %s\n", line);
348 goto out_file;
350 p++;
351 if (!parse_file(p, md)) {
352 warn("could not open %s: %s\n",
353 p, strerror(errno));
354 goto out_file;
356 continue;
358 if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) {
359 check_files = 1;
360 continue;
362 if (!check_files)
363 continue;
365 /* Continue until line does not end with '\' */
366 if ( *(p + strlen(p)-1) != '\\')
367 break;
368 /* Terminate line at first space, to get rid of final ' \' */
369 while (*p) {
370 if (isspace(*p)) {
371 *p = '\0';
372 break;
374 p++;
377 /* Check if this file is in same dir as objfile */
378 if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) {
379 if (!parse_file(line, md)) {
380 warn("could not open %s: %s\n",
381 line, strerror(errno));
382 goto out_file;
389 /* Everyone parsed OK */
390 ret = 1;
391 out_file:
392 release_file(file, flen);
393 out:
394 free(dir);
395 free(cmd);
396 return ret;
399 /* Calc and record src checksum. */
400 void get_src_version(const char *modname, char sum[], unsigned sumlen)
402 void *file;
403 unsigned long len;
404 struct md4_ctx md;
405 char *sources, *end, *fname;
406 const char *basename;
407 char filelist[PATH_MAX + 1];
408 char *modverdir = getenv("MODVERDIR");
410 if (!modverdir)
411 modverdir = ".";
413 /* Source files for module are in .tmp_versions/modname.mod,
414 after the first line. */
415 if (strrchr(modname, '/'))
416 basename = strrchr(modname, '/') + 1;
417 else
418 basename = modname;
419 snprintf(filelist, sizeof(filelist), "%s/%.*s.mod", modverdir,
420 (int) strlen(basename) - 2, basename);
422 file = grab_file(filelist, &len);
423 if (!file)
424 /* not a module or .mod file missing - ignore */
425 return;
427 sources = strchr(file, '\n');
428 if (!sources) {
429 warn("malformed versions file for %s\n", modname);
430 goto release;
433 sources++;
434 end = strchr(sources, '\n');
435 if (!end) {
436 warn("bad ending versions file for %s\n", modname);
437 goto release;
439 *end = '\0';
441 md4_init(&md);
442 while ((fname = strsep(&sources, " ")) != NULL) {
443 if (!*fname)
444 continue;
445 if (!(is_static_library(fname)) &&
446 !parse_source_files(fname, &md))
447 goto release;
450 md4_final_ascii(&md, sum, sumlen);
451 release:
452 release_file(file, len);
455 static void write_version(const char *filename, const char *sum,
456 unsigned long offset)
458 int fd;
460 fd = open(filename, O_RDWR);
461 if (fd < 0) {
462 warn("changing sum in %s failed: %s\n",
463 filename, strerror(errno));
464 return;
467 if (lseek(fd, offset, SEEK_SET) == (off_t)-1) {
468 warn("changing sum in %s:%lu failed: %s\n",
469 filename, offset, strerror(errno));
470 goto out;
473 if (write(fd, sum, strlen(sum)+1) != strlen(sum)+1) {
474 warn("writing sum in %s failed: %s\n",
475 filename, strerror(errno));
476 goto out;
478 out:
479 close(fd);
482 static int strip_rcs_crap(char *version)
484 unsigned int len, full_len;
486 if (strncmp(version, "$Revision", strlen("$Revision")) != 0)
487 return 0;
489 /* Space for version string follows. */
490 full_len = strlen(version) + strlen(version + strlen(version) + 1) + 2;
492 /* Move string to start with version number: prefix will be
493 * $Revision$ or $Revision: */
494 len = strlen("$Revision");
495 if (version[len] == ':' || version[len] == '$')
496 len++;
497 while (isspace(version[len]))
498 len++;
499 memmove(version, version+len, full_len-len);
500 full_len -= len;
502 /* Preserve up to next whitespace. */
503 len = 0;
504 while (version[len] && !isspace(version[len]))
505 len++;
506 memmove(version + len, version + strlen(version),
507 full_len - strlen(version));
508 return 1;
511 /* Clean up RCS-style version numbers. */
512 void maybe_frob_rcs_version(const char *modfilename,
513 char *version,
514 void *modinfo,
515 unsigned long version_offset)
517 if (strip_rcs_crap(version))
518 write_version(modfilename, version, version_offset);