11da177e4SLinus Torvalds #include <netinet/in.h> 21da177e4SLinus Torvalds #ifdef __sun__ 31da177e4SLinus Torvalds #include <inttypes.h> 41da177e4SLinus Torvalds #else 51da177e4SLinus Torvalds #include <stdint.h> 61da177e4SLinus Torvalds #endif 71da177e4SLinus Torvalds #include <ctype.h> 81da177e4SLinus Torvalds #include <errno.h> 91da177e4SLinus Torvalds #include <string.h> 10fc31c771SMike Frysinger #include <limits.h> 111da177e4SLinus Torvalds #include "modpost.h" 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds /* 141da177e4SLinus Torvalds * Stolen form Cryptographic API. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * MD4 Message Digest Algorithm (RFC1320). 171da177e4SLinus Torvalds * 181da177e4SLinus Torvalds * Implementation derived from Andrew Tridgell and Steve French's 191da177e4SLinus Torvalds * CIFS MD4 implementation, and the cryptoapi implementation 201da177e4SLinus Torvalds * originally based on the public domain implementation written 211da177e4SLinus Torvalds * by Colin Plumb in 1993. 221da177e4SLinus Torvalds * 231da177e4SLinus Torvalds * Copyright (c) Andrew Tridgell 1997-1998. 241da177e4SLinus Torvalds * Modified by Steve French (sfrench@us.ibm.com) 2002 251da177e4SLinus Torvalds * Copyright (c) Cryptoapi developers. 261da177e4SLinus Torvalds * Copyright (c) 2002 David S. Miller (davem@redhat.com) 271da177e4SLinus Torvalds * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 301da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 311da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 321da177e4SLinus Torvalds * (at your option) any later version. 331da177e4SLinus Torvalds * 341da177e4SLinus Torvalds */ 351da177e4SLinus Torvalds #define MD4_DIGEST_SIZE 16 361da177e4SLinus Torvalds #define MD4_HMAC_BLOCK_SIZE 64 371da177e4SLinus Torvalds #define MD4_BLOCK_WORDS 16 381da177e4SLinus Torvalds #define MD4_HASH_WORDS 4 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds struct md4_ctx { 411da177e4SLinus Torvalds uint32_t hash[MD4_HASH_WORDS]; 421da177e4SLinus Torvalds uint32_t block[MD4_BLOCK_WORDS]; 431da177e4SLinus Torvalds uint64_t byte_count; 441da177e4SLinus Torvalds }; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds static inline uint32_t lshift(uint32_t x, unsigned int s) 471da177e4SLinus Torvalds { 481da177e4SLinus Torvalds x &= 0xFFFFFFFF; 491da177e4SLinus Torvalds return ((x << s) & 0xFFFFFFFF) | (x >> (32 - s)); 501da177e4SLinus Torvalds } 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static inline uint32_t F(uint32_t x, uint32_t y, uint32_t z) 531da177e4SLinus Torvalds { 541da177e4SLinus Torvalds return (x & y) | ((~x) & z); 551da177e4SLinus Torvalds } 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds static inline uint32_t G(uint32_t x, uint32_t y, uint32_t z) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds return (x & y) | (x & z) | (y & z); 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds static inline uint32_t H(uint32_t x, uint32_t y, uint32_t z) 631da177e4SLinus Torvalds { 641da177e4SLinus Torvalds return x ^ y ^ z; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds #define ROUND1(a,b,c,d,k,s) (a = lshift(a + F(b,c,d) + k, s)) 681da177e4SLinus Torvalds #define ROUND2(a,b,c,d,k,s) (a = lshift(a + G(b,c,d) + k + (uint32_t)0x5A827999,s)) 691da177e4SLinus Torvalds #define ROUND3(a,b,c,d,k,s) (a = lshift(a + H(b,c,d) + k + (uint32_t)0x6ED9EBA1,s)) 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds /* XXX: this stuff can be optimized */ 721da177e4SLinus Torvalds static inline void le32_to_cpu_array(uint32_t *buf, unsigned int words) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds while (words--) { 751da177e4SLinus Torvalds *buf = ntohl(*buf); 761da177e4SLinus Torvalds buf++; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds static inline void cpu_to_le32_array(uint32_t *buf, unsigned int words) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds while (words--) { 831da177e4SLinus Torvalds *buf = htonl(*buf); 841da177e4SLinus Torvalds buf++; 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds } 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds static void md4_transform(uint32_t *hash, uint32_t const *in) 891da177e4SLinus Torvalds { 901da177e4SLinus Torvalds uint32_t a, b, c, d; 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds a = hash[0]; 931da177e4SLinus Torvalds b = hash[1]; 941da177e4SLinus Torvalds c = hash[2]; 951da177e4SLinus Torvalds d = hash[3]; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds ROUND1(a, b, c, d, in[0], 3); 981da177e4SLinus Torvalds ROUND1(d, a, b, c, in[1], 7); 991da177e4SLinus Torvalds ROUND1(c, d, a, b, in[2], 11); 1001da177e4SLinus Torvalds ROUND1(b, c, d, a, in[3], 19); 1011da177e4SLinus Torvalds ROUND1(a, b, c, d, in[4], 3); 1021da177e4SLinus Torvalds ROUND1(d, a, b, c, in[5], 7); 1031da177e4SLinus Torvalds ROUND1(c, d, a, b, in[6], 11); 1041da177e4SLinus Torvalds ROUND1(b, c, d, a, in[7], 19); 1051da177e4SLinus Torvalds ROUND1(a, b, c, d, in[8], 3); 1061da177e4SLinus Torvalds ROUND1(d, a, b, c, in[9], 7); 1071da177e4SLinus Torvalds ROUND1(c, d, a, b, in[10], 11); 1081da177e4SLinus Torvalds ROUND1(b, c, d, a, in[11], 19); 1091da177e4SLinus Torvalds ROUND1(a, b, c, d, in[12], 3); 1101da177e4SLinus Torvalds ROUND1(d, a, b, c, in[13], 7); 1111da177e4SLinus Torvalds ROUND1(c, d, a, b, in[14], 11); 1121da177e4SLinus Torvalds ROUND1(b, c, d, a, in[15], 19); 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds ROUND2(a, b, c, d,in[ 0], 3); 1151da177e4SLinus Torvalds ROUND2(d, a, b, c, in[4], 5); 1161da177e4SLinus Torvalds ROUND2(c, d, a, b, in[8], 9); 1171da177e4SLinus Torvalds ROUND2(b, c, d, a, in[12], 13); 1181da177e4SLinus Torvalds ROUND2(a, b, c, d, in[1], 3); 1191da177e4SLinus Torvalds ROUND2(d, a, b, c, in[5], 5); 1201da177e4SLinus Torvalds ROUND2(c, d, a, b, in[9], 9); 1211da177e4SLinus Torvalds ROUND2(b, c, d, a, in[13], 13); 1221da177e4SLinus Torvalds ROUND2(a, b, c, d, in[2], 3); 1231da177e4SLinus Torvalds ROUND2(d, a, b, c, in[6], 5); 1241da177e4SLinus Torvalds ROUND2(c, d, a, b, in[10], 9); 1251da177e4SLinus Torvalds ROUND2(b, c, d, a, in[14], 13); 1261da177e4SLinus Torvalds ROUND2(a, b, c, d, in[3], 3); 1271da177e4SLinus Torvalds ROUND2(d, a, b, c, in[7], 5); 1281da177e4SLinus Torvalds ROUND2(c, d, a, b, in[11], 9); 1291da177e4SLinus Torvalds ROUND2(b, c, d, a, in[15], 13); 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds ROUND3(a, b, c, d,in[ 0], 3); 1321da177e4SLinus Torvalds ROUND3(d, a, b, c, in[8], 9); 1331da177e4SLinus Torvalds ROUND3(c, d, a, b, in[4], 11); 1341da177e4SLinus Torvalds ROUND3(b, c, d, a, in[12], 15); 1351da177e4SLinus Torvalds ROUND3(a, b, c, d, in[2], 3); 1361da177e4SLinus Torvalds ROUND3(d, a, b, c, in[10], 9); 1371da177e4SLinus Torvalds ROUND3(c, d, a, b, in[6], 11); 1381da177e4SLinus Torvalds ROUND3(b, c, d, a, in[14], 15); 1391da177e4SLinus Torvalds ROUND3(a, b, c, d, in[1], 3); 1401da177e4SLinus Torvalds ROUND3(d, a, b, c, in[9], 9); 1411da177e4SLinus Torvalds ROUND3(c, d, a, b, in[5], 11); 1421da177e4SLinus Torvalds ROUND3(b, c, d, a, in[13], 15); 1431da177e4SLinus Torvalds ROUND3(a, b, c, d, in[3], 3); 1441da177e4SLinus Torvalds ROUND3(d, a, b, c, in[11], 9); 1451da177e4SLinus Torvalds ROUND3(c, d, a, b, in[7], 11); 1461da177e4SLinus Torvalds ROUND3(b, c, d, a, in[15], 15); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds hash[0] += a; 1491da177e4SLinus Torvalds hash[1] += b; 1501da177e4SLinus Torvalds hash[2] += c; 1511da177e4SLinus Torvalds hash[3] += d; 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds static inline void md4_transform_helper(struct md4_ctx *ctx) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(uint32_t)); 1571da177e4SLinus Torvalds md4_transform(ctx->hash, ctx->block); 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static void md4_init(struct md4_ctx *mctx) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds mctx->hash[0] = 0x67452301; 1631da177e4SLinus Torvalds mctx->hash[1] = 0xefcdab89; 1641da177e4SLinus Torvalds mctx->hash[2] = 0x98badcfe; 1651da177e4SLinus Torvalds mctx->hash[3] = 0x10325476; 1661da177e4SLinus Torvalds mctx->byte_count = 0; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds static void md4_update(struct md4_ctx *mctx, 1701da177e4SLinus Torvalds const unsigned char *data, unsigned int len) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds mctx->byte_count += len; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds if (avail > len) { 1771da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 1781da177e4SLinus Torvalds data, len); 1791da177e4SLinus Torvalds return; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), 1831da177e4SLinus Torvalds data, avail); 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds md4_transform_helper(mctx); 1861da177e4SLinus Torvalds data += avail; 1871da177e4SLinus Torvalds len -= avail; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds while (len >= sizeof(mctx->block)) { 1901da177e4SLinus Torvalds memcpy(mctx->block, data, sizeof(mctx->block)); 1911da177e4SLinus Torvalds md4_transform_helper(mctx); 1921da177e4SLinus Torvalds data += sizeof(mctx->block); 1931da177e4SLinus Torvalds len -= sizeof(mctx->block); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds memcpy(mctx->block, data, len); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds static void md4_final_ascii(struct md4_ctx *mctx, char *out, unsigned int len) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds const unsigned int offset = mctx->byte_count & 0x3f; 2021da177e4SLinus Torvalds char *p = (char *)mctx->block + offset; 2031da177e4SLinus Torvalds int padding = 56 - (offset + 1); 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds *p++ = 0x80; 2061da177e4SLinus Torvalds if (padding < 0) { 2071da177e4SLinus Torvalds memset(p, 0x00, padding + sizeof (uint64_t)); 2081da177e4SLinus Torvalds md4_transform_helper(mctx); 2091da177e4SLinus Torvalds p = (char *)mctx->block; 2101da177e4SLinus Torvalds padding = 56; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds memset(p, 0, padding); 2141da177e4SLinus Torvalds mctx->block[14] = mctx->byte_count << 3; 2151da177e4SLinus Torvalds mctx->block[15] = mctx->byte_count >> 29; 2161da177e4SLinus Torvalds le32_to_cpu_array(mctx->block, (sizeof(mctx->block) - 2171da177e4SLinus Torvalds sizeof(uint64_t)) / sizeof(uint32_t)); 2181da177e4SLinus Torvalds md4_transform(mctx->hash, mctx->block); 2191da177e4SLinus Torvalds cpu_to_le32_array(mctx->hash, sizeof(mctx->hash) / sizeof(uint32_t)); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds snprintf(out, len, "%08X%08X%08X%08X", 2221da177e4SLinus Torvalds mctx->hash[0], mctx->hash[1], mctx->hash[2], mctx->hash[3]); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds static inline void add_char(unsigned char c, struct md4_ctx *md) 2261da177e4SLinus Torvalds { 2271da177e4SLinus Torvalds md4_update(md, &c, 1); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static int parse_string(const char *file, unsigned long len, 2311da177e4SLinus Torvalds struct md4_ctx *md) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds unsigned long i; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds add_char(file[0], md); 2361da177e4SLinus Torvalds for (i = 1; i < len; i++) { 2371da177e4SLinus Torvalds add_char(file[i], md); 2381da177e4SLinus Torvalds if (file[i] == '"' && file[i-1] != '\\') 2391da177e4SLinus Torvalds break; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds return i; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds static int parse_comment(const char *file, unsigned long len) 2451da177e4SLinus Torvalds { 2461da177e4SLinus Torvalds unsigned long i; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds for (i = 2; i < len; i++) { 2491da177e4SLinus Torvalds if (file[i-1] == '*' && file[i] == '/') 2501da177e4SLinus Torvalds break; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds return i; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /* FIXME: Handle .s files differently (eg. # starts comments) --RR */ 25661d9cdf2SJ.A. Magallon static int parse_file(const char *fname, struct md4_ctx *md) 2571da177e4SLinus Torvalds { 25861d9cdf2SJ.A. Magallon char *file; 2591da177e4SLinus Torvalds unsigned long i, len; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds file = grab_file(fname, &len); 2621da177e4SLinus Torvalds if (!file) 2631da177e4SLinus Torvalds return 0; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds for (i = 0; i < len; i++) { 2661da177e4SLinus Torvalds /* Collapse and ignore \ and CR. */ 2671da177e4SLinus Torvalds if (file[i] == '\\' && (i+1 < len) && file[i+1] == '\n') { 2681da177e4SLinus Torvalds i++; 2691da177e4SLinus Torvalds continue; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds /* Ignore whitespace */ 2731da177e4SLinus Torvalds if (isspace(file[i])) 2741da177e4SLinus Torvalds continue; 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* Handle strings as whole units */ 2771da177e4SLinus Torvalds if (file[i] == '"') { 2781da177e4SLinus Torvalds i += parse_string(file+i, len - i, md); 2791da177e4SLinus Torvalds continue; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* Comments: ignore */ 2831da177e4SLinus Torvalds if (file[i] == '/' && file[i+1] == '*') { 2841da177e4SLinus Torvalds i += parse_comment(file+i, len - i); 2851da177e4SLinus Torvalds continue; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds add_char(file[i], md); 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds release_file(file, len); 2911da177e4SLinus Torvalds return 1; 2921da177e4SLinus Torvalds } 293*e6e6733eSAshutosh Naik /* Check whether the file is a static library or not */ 294*e6e6733eSAshutosh Naik static int is_static_library(const char *objfile) 295*e6e6733eSAshutosh Naik { 296*e6e6733eSAshutosh Naik int len = strlen(objfile); 297*e6e6733eSAshutosh Naik if (objfile[len - 2] == '.' && objfile[len - 1] == 'a') 298*e6e6733eSAshutosh Naik return 1; 299*e6e6733eSAshutosh Naik else 300*e6e6733eSAshutosh Naik return 0; 301*e6e6733eSAshutosh Naik } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds /* We have dir/file.o. Open dir/.file.o.cmd, look for deps_ line to 3041da177e4SLinus Torvalds * figure out source file. */ 3051da177e4SLinus Torvalds static int parse_source_files(const char *objfile, struct md4_ctx *md) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds char *cmd, *file, *line, *dir; 3081da177e4SLinus Torvalds const char *base; 3091da177e4SLinus Torvalds unsigned long flen, pos = 0; 3101da177e4SLinus Torvalds int dirlen, ret = 0, check_files = 0; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds cmd = NOFAIL(malloc(strlen(objfile) + sizeof("..cmd"))); 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds base = strrchr(objfile, '/'); 3151da177e4SLinus Torvalds if (base) { 3161da177e4SLinus Torvalds base++; 3171da177e4SLinus Torvalds dirlen = base - objfile; 3181da177e4SLinus Torvalds sprintf(cmd, "%.*s.%s.cmd", dirlen, objfile, base); 3191da177e4SLinus Torvalds } else { 3201da177e4SLinus Torvalds dirlen = 0; 3211da177e4SLinus Torvalds sprintf(cmd, ".%s.cmd", objfile); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds dir = NOFAIL(malloc(dirlen + 1)); 3241da177e4SLinus Torvalds strncpy(dir, objfile, dirlen); 3251da177e4SLinus Torvalds dir[dirlen] = '\0'; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds file = grab_file(cmd, &flen); 3281da177e4SLinus Torvalds if (!file) { 329cb80514dSSam Ravnborg warn("could not find %s for %s\n", cmd, objfile); 3301da177e4SLinus Torvalds goto out; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds /* There will be a line like so: 3341da177e4SLinus Torvalds deps_drivers/net/dummy.o := \ 3351da177e4SLinus Torvalds drivers/net/dummy.c \ 3361da177e4SLinus Torvalds $(wildcard include/config/net/fastroute.h) \ 3371da177e4SLinus Torvalds include/linux/config.h \ 3381da177e4SLinus Torvalds $(wildcard include/config/h.h) \ 3391da177e4SLinus Torvalds include/linux/module.h \ 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds Sum all files in the same dir or subdirs. 3421da177e4SLinus Torvalds */ 3431da177e4SLinus Torvalds while ((line = get_next_line(&pos, file, flen)) != NULL) { 34461d9cdf2SJ.A. Magallon char* p = line; 3451da177e4SLinus Torvalds if (strncmp(line, "deps_", sizeof("deps_")-1) == 0) { 3461da177e4SLinus Torvalds check_files = 1; 3471da177e4SLinus Torvalds continue; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds if (!check_files) 3501da177e4SLinus Torvalds continue; 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds /* Continue until line does not end with '\' */ 3531da177e4SLinus Torvalds if ( *(p + strlen(p)-1) != '\\') 3541da177e4SLinus Torvalds break; 3551da177e4SLinus Torvalds /* Terminate line at first space, to get rid of final ' \' */ 3561da177e4SLinus Torvalds while (*p) { 3571da177e4SLinus Torvalds if (isspace(*p)) { 3581da177e4SLinus Torvalds *p = '\0'; 3591da177e4SLinus Torvalds break; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds p++; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds /* Check if this file is in same dir as objfile */ 3651da177e4SLinus Torvalds if ((strstr(line, dir)+strlen(dir)-1) == strrchr(line, '/')) { 3661da177e4SLinus Torvalds if (!parse_file(line, md)) { 367cb80514dSSam Ravnborg warn("could not open %s: %s\n", 3681da177e4SLinus Torvalds line, strerror(errno)); 3691da177e4SLinus Torvalds goto out_file; 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds /* Everyone parsed OK */ 3771da177e4SLinus Torvalds ret = 1; 3781da177e4SLinus Torvalds out_file: 3791da177e4SLinus Torvalds release_file(file, flen); 3801da177e4SLinus Torvalds out: 3811da177e4SLinus Torvalds free(dir); 3821da177e4SLinus Torvalds free(cmd); 3831da177e4SLinus Torvalds return ret; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds /* Calc and record src checksum. */ 3871da177e4SLinus Torvalds void get_src_version(const char *modname, char sum[], unsigned sumlen) 3881da177e4SLinus Torvalds { 3891da177e4SLinus Torvalds void *file; 3901da177e4SLinus Torvalds unsigned long len; 3911da177e4SLinus Torvalds struct md4_ctx md; 3921da177e4SLinus Torvalds char *sources, *end, *fname; 3931da177e4SLinus Torvalds const char *basename; 39441370d3bSSam Ravnborg char filelist[PATH_MAX + 1]; 39541370d3bSSam Ravnborg char *modverdir = getenv("MODVERDIR"); 39641370d3bSSam Ravnborg 39741370d3bSSam Ravnborg if (!modverdir) 39841370d3bSSam Ravnborg modverdir = "."; 3991da177e4SLinus Torvalds 4001da177e4SLinus Torvalds /* Source files for module are in .tmp_versions/modname.mod, 4011da177e4SLinus Torvalds after the first line. */ 4021da177e4SLinus Torvalds if (strrchr(modname, '/')) 4031da177e4SLinus Torvalds basename = strrchr(modname, '/') + 1; 4041da177e4SLinus Torvalds else 4051da177e4SLinus Torvalds basename = modname; 40641370d3bSSam Ravnborg sprintf(filelist, "%s/%.*s.mod", modverdir, 4071da177e4SLinus Torvalds (int) strlen(basename) - 2, basename); 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds file = grab_file(filelist, &len); 4104be40e22SSam Ravnborg if (!file) 4114be40e22SSam Ravnborg /* not a module or .mod file missing - ignore */ 4121da177e4SLinus Torvalds return; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds sources = strchr(file, '\n'); 4151da177e4SLinus Torvalds if (!sources) { 416cb80514dSSam Ravnborg warn("malformed versions file for %s\n", modname); 4171da177e4SLinus Torvalds goto release; 4181da177e4SLinus Torvalds } 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds sources++; 4211da177e4SLinus Torvalds end = strchr(sources, '\n'); 4221da177e4SLinus Torvalds if (!end) { 423cb80514dSSam Ravnborg warn("bad ending versions file for %s\n", modname); 4241da177e4SLinus Torvalds goto release; 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds *end = '\0'; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds md4_init(&md); 4291da177e4SLinus Torvalds while ((fname = strsep(&sources, " ")) != NULL) { 4301da177e4SLinus Torvalds if (!*fname) 4311da177e4SLinus Torvalds continue; 432*e6e6733eSAshutosh Naik if (!(is_static_library(fname)) && 433*e6e6733eSAshutosh Naik !parse_source_files(fname, &md)) 4341da177e4SLinus Torvalds goto release; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds md4_final_ascii(&md, sum, sumlen); 4381da177e4SLinus Torvalds release: 4391da177e4SLinus Torvalds release_file(file, len); 4401da177e4SLinus Torvalds } 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds static void write_version(const char *filename, const char *sum, 4431da177e4SLinus Torvalds unsigned long offset) 4441da177e4SLinus Torvalds { 4451da177e4SLinus Torvalds int fd; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds fd = open(filename, O_RDWR); 4481da177e4SLinus Torvalds if (fd < 0) { 449cb80514dSSam Ravnborg warn("changing sum in %s failed: %s\n", 4501da177e4SLinus Torvalds filename, strerror(errno)); 4511da177e4SLinus Torvalds return; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds if (lseek(fd, offset, SEEK_SET) == (off_t)-1) { 455cb80514dSSam Ravnborg warn("changing sum in %s:%lu failed: %s\n", 4561da177e4SLinus Torvalds filename, offset, strerror(errno)); 4571da177e4SLinus Torvalds goto out; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds if (write(fd, sum, strlen(sum)+1) != strlen(sum)+1) { 461cb80514dSSam Ravnborg warn("writing sum in %s failed: %s\n", 4621da177e4SLinus Torvalds filename, strerror(errno)); 4631da177e4SLinus Torvalds goto out; 4641da177e4SLinus Torvalds } 4651da177e4SLinus Torvalds out: 4661da177e4SLinus Torvalds close(fd); 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds 46961d9cdf2SJ.A. Magallon static int strip_rcs_crap(char *version) 4701da177e4SLinus Torvalds { 4711da177e4SLinus Torvalds unsigned int len, full_len; 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds if (strncmp(version, "$Revision", strlen("$Revision")) != 0) 4741da177e4SLinus Torvalds return 0; 4751da177e4SLinus Torvalds 4761da177e4SLinus Torvalds /* Space for version string follows. */ 4771da177e4SLinus Torvalds full_len = strlen(version) + strlen(version + strlen(version) + 1) + 2; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds /* Move string to start with version number: prefix will be 4801da177e4SLinus Torvalds * $Revision$ or $Revision: */ 4811da177e4SLinus Torvalds len = strlen("$Revision"); 4821da177e4SLinus Torvalds if (version[len] == ':' || version[len] == '$') 4831da177e4SLinus Torvalds len++; 4841da177e4SLinus Torvalds while (isspace(version[len])) 4851da177e4SLinus Torvalds len++; 4861da177e4SLinus Torvalds memmove(version, version+len, full_len-len); 4871da177e4SLinus Torvalds full_len -= len; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds /* Preserve up to next whitespace. */ 4901da177e4SLinus Torvalds len = 0; 4911da177e4SLinus Torvalds while (version[len] && !isspace(version[len])) 4921da177e4SLinus Torvalds len++; 4931da177e4SLinus Torvalds memmove(version + len, version + strlen(version), 4941da177e4SLinus Torvalds full_len - strlen(version)); 4951da177e4SLinus Torvalds return 1; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds /* Clean up RCS-style version numbers. */ 4991da177e4SLinus Torvalds void maybe_frob_rcs_version(const char *modfilename, 5001da177e4SLinus Torvalds char *version, 5011da177e4SLinus Torvalds void *modinfo, 5021da177e4SLinus Torvalds unsigned long version_offset) 5031da177e4SLinus Torvalds { 5041da177e4SLinus Torvalds if (strip_rcs_crap(version)) 5051da177e4SLinus Torvalds write_version(modfilename, version, version_offset); 5061da177e4SLinus Torvalds } 507