r312 - in trunk: . scripts
DONOTREPLY at icculus.org
DONOTREPLY at icculus.org
Sun May 27 11:56:12 EDT 2007
Author: icculus
Date: 2007-05-27 11:56:12 -0400 (Sun, 27 May 2007)
New Revision: 312
Added:
trunk/checksum_crc32.c
trunk/checksum_md5.c
trunk/checksum_sha1.c
Modified:
trunk/CMakeLists.txt
trunk/fileio.c
trunk/fileio.h
trunk/lua_glue.c
trunk/mojosetup.c
trunk/scripts/mojosetup_mainline.lua
trunk/universal.h
Log:
Added support for various types of checksums.
Modified: trunk/CMakeLists.txt
===================================================================
--- trunk/CMakeLists.txt 2007-05-27 06:01:08 UTC (rev 311)
+++ trunk/CMakeLists.txt 2007-05-27 15:56:12 UTC (rev 312)
@@ -178,6 +178,9 @@
fileio.c
archive_zip.c
archive_tar.c
+ checksum_crc32.c
+ checksum_md5.c
+ checksum_sha1.c
platform_unix.c
platform_beos.c
lua_glue.c
@@ -266,6 +269,27 @@
ENDIF(MOJOSETUP_LUALIB_PACKAGE)
+# Checksums...
+
+# BINARY SIZE += !!! FIXME: check this.
+OPTION(MOJOSETUP_CHECKSUM_CRC32 "Enable CRC-32 checksum support" TRUE)
+IF(MOJOSETUP_CHECKSUM_CRC32)
+ ADD_DEFINITIONS(-DSUPPORT_CRC32=1)
+ENDIF(MOJOSETUP_CHECKSUM_CRC32)
+
+# BINARY SIZE += !!! FIXME: check this.
+OPTION(MOJOSETUP_CHECKSUM_MD5 "Enable MD5 checksum support" TRUE)
+IF(MOJOSETUP_CHECKSUM_MD5)
+ ADD_DEFINITIONS(-DSUPPORT_MD5=1)
+ENDIF(MOJOSETUP_CHECKSUM_MD5)
+
+# BINARY SIZE += !!! FIXME: check this.
+OPTION(MOJOSETUP_CHECKSUM_SHA1 "Enable SHA-1 checksum support" TRUE)
+IF(MOJOSETUP_CHECKSUM_SHA1)
+ ADD_DEFINITIONS(-DSUPPORT_SHA1=1)
+ENDIF(MOJOSETUP_CHECKSUM_SHA1)
+
+
# GUI plugins...
# BINARY SIZE += 2.5
Added: trunk/checksum_crc32.c
===================================================================
--- trunk/checksum_crc32.c (rev 0)
+++ trunk/checksum_crc32.c 2007-05-27 15:56:12 UTC (rev 312)
@@ -0,0 +1,81 @@
+#include "universal.h"
+
+#if SUPPORT_CRC32
+
+void MojoCrc32_init(MojoCrc32 *context)
+{
+ *context = (MojoCrc32) 0xFFFFFFFF;
+} // MojoCrc32_init
+
+
+void MojoCrc32_append(MojoCrc32 *_crc, const uint8 *buf, uint32 len)
+{
+ uint32 crc = (uint32) *_crc;
+
+ int n;
+ for (n = 0; n < len; n++)
+ {
+ uint32 xorval = (uint32) ((crc ^ buf[n]) & 0xFF);
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ xorval = ((xorval & 1) ? (0xEDB88320 ^ (xorval >> 1)) : (xorval >> 1));
+ crc = xorval ^ (crc >> 8);
+ } // for
+
+ *_crc = (MojoCrc32) crc;
+} // MojoCrc32_append
+
+
+void MojoCrc32_finish(MojoCrc32 *context, uint32 *digest)
+{
+ *digest = (*context ^ 0xFFFFFFFF);
+} // MojoCrc32_finish
+
+
+#endif // SUPPORT_CRC32
+
+#if TEST_CRC32
+int main(int argc, char **argv)
+{
+ int i = 0;
+ for (i = 1; i < argc; i++)
+ {
+ FILE *in = NULL;
+ MojoCrc32 ctx;
+ MojoCrc32_init(&ctx);
+ in = fopen(argv[i], "rb");
+ if (!in)
+ perror("fopen");
+ else
+ {
+ uint32 digest = 0;
+ int err = 0;
+ while ( (!err) && (!feof(in)) )
+ {
+ uint8 buf[1024];
+ size_t rc = fread(buf, 1, sizeof (buf), in);
+ if (rc > 0)
+ MojoCrc32_append(&ctx, buf, rc);
+ err = ferror(in);
+ } // while
+
+ if (err)
+ perror("fread");
+ fclose(in);
+ MojoCrc32_finish(&ctx, &digest);
+ if (!err)
+ printf("%s: %X\n", argv[i], (unsigned int) digest);
+ } // else
+ } // for
+
+ return 0;
+} // main
+#endif
+
+// end of checksum_crc32.c ...
+
Added: trunk/checksum_md5.c
===================================================================
--- trunk/checksum_md5.c (rev 0)
+++ trunk/checksum_md5.c 2007-05-27 15:56:12 UTC (rev 312)
@@ -0,0 +1,441 @@
+#include "universal.h"
+
+#if SUPPORT_MD5
+
+// MD5 code originally from http://sourceforge.net/projects/libmd5-rfc/
+// License: zlib.
+// I cleaned it up a little for MojoSetup's specific purposes. --ryan.
+
+/*
+ Copyright (C) 1999, 2000, 2002 Aladdin Enterprises. All rights reserved.
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ L. Peter Deutsch
+ ghost at aladdin.com
+
+ */
+/* $Id: md5.c,v 1.6 2002/04/13 19:20:28 lpd Exp $ */
+/*
+ Independent implementation of MD5 (RFC 1321).
+
+ This code implements the MD5 Algorithm defined in RFC 1321, whose
+ text is available at
+ http://www.ietf.org/rfc/rfc1321.txt
+ The code is derived from the text of the RFC, including the test suite
+ (section A.5) but excluding the rest of Appendix A. It does not include
+ any code or documentation that is identified in the RFC as being
+ copyrighted.
+
+ The original and principal author of md5.c is L. Peter Deutsch
+ <ghost at aladdin.com>. Other authors are noted in the change history
+ that follows (in reverse chronological order):
+
+ 2002-04-13 lpd Clarified derivation from RFC 1321; now handles byte order
+ either statically or dynamically; added missing #include <string.h>
+ in library.
+ 2002-03-11 lpd Corrected argument list for main(), and added int return
+ type, in test program and T value program.
+ 2002-02-21 lpd Added missing #include <stdio.h> in test program.
+ 2000-07-03 lpd Patched to eliminate warnings about "constant is
+ unsigned in ANSI C, signed in traditional"; made test program
+ self-checking.
+ 1999-11-04 lpd Edited comments slightly for automatic TOC extraction.
+ 1999-10-18 lpd Fixed typo in header comment (ansi2knr rather than md5).
+ 1999-05-03 lpd Original version.
+ */
+
+#undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */
+//#ifdef ARCH_IS_BIG_ENDIAN
+//# define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1)
+//#else
+//# define BYTE_ORDER 0
+//#endif
+#if PLATFORM_BIGENDIAN
+# define BYTE_ORDER 1
+#else
+# define BYTE_ORDER -1
+#endif
+
+#define T_MASK ((uint32)~0)
+#define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87)
+#define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9)
+#define T3 0x242070db
+#define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111)
+#define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050)
+#define T6 0x4787c62a
+#define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec)
+#define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe)
+#define T9 0x698098d8
+#define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850)
+#define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e)
+#define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841)
+#define T13 0x6b901122
+#define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c)
+#define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71)
+#define T16 0x49b40821
+#define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d)
+#define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf)
+#define T19 0x265e5a51
+#define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855)
+#define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2)
+#define T22 0x02441453
+#define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e)
+#define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437)
+#define T25 0x21e1cde6
+#define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829)
+#define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278)
+#define T28 0x455a14ed
+#define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa)
+#define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07)
+#define T31 0x676f02d9
+#define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375)
+#define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd)
+#define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e)
+#define T35 0x6d9d6122
+#define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3)
+#define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb)
+#define T38 0x4bdecfa9
+#define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f)
+#define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f)
+#define T41 0x289b7ec6
+#define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805)
+#define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a)
+#define T44 0x04881d05
+#define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6)
+#define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a)
+#define T47 0x1fa27cf8
+#define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a)
+#define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb)
+#define T50 0x432aff97
+#define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58)
+#define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6)
+#define T53 0x655b59c3
+#define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d)
+#define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82)
+#define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e)
+#define T57 0x6fa87e4f
+#define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f)
+#define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb)
+#define T60 0x4e0811a1
+#define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d)
+#define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca)
+#define T63 0x2ad7d2bb
+#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e)
+
+
+static void
+MojoMd5_process(MojoMd5 *pms, const uint8 *data /*[64]*/)
+{
+ uint32
+ a = pms->abcd[0], b = pms->abcd[1],
+ c = pms->abcd[2], d = pms->abcd[3];
+ uint32 t;
+#if BYTE_ORDER > 0
+ /* Define storage only for big-endian CPUs. */
+ uint32 X[16];
+#else
+ /* Define storage for little-endian or both types of CPUs. */
+ uint32 xbuf[16];
+ const uint32 *X;
+#endif
+
+ {
+#if BYTE_ORDER == 0
+ /*
+ * Determine dynamically whether this is a big-endian or
+ * little-endian machine, since we can use a more efficient
+ * algorithm on the latter.
+ */
+ static const int w = 1;
+
+ if (*((const uint8 *)&w)) /* dynamic little-endian */
+#endif
+#if BYTE_ORDER <= 0 /* little-endian */
+ {
+ /*
+ * On little-endian machines, we can process properly aligned
+ * data without copying it.
+ */
+ if (!((data - (const uint8 *)0) & 3)) {
+ /* data are properly aligned */
+ X = (const uint32 *)data;
+ } else {
+ /* not aligned */
+ memcpy(xbuf, data, 64);
+ X = xbuf;
+ }
+ }
+#endif
+#if BYTE_ORDER == 0
+ else /* dynamic big-endian */
+#endif
+#if BYTE_ORDER >= 0 /* big-endian */
+ {
+ /*
+ * On big-endian machines, we must arrange the bytes in the
+ * right order.
+ */
+ const uint8 *xp = data;
+ int i;
+
+# if BYTE_ORDER == 0
+ X = xbuf; /* (dynamic only) */
+# else
+# define xbuf X /* (static only) */
+# endif
+ for (i = 0; i < 16; ++i, xp += 4)
+ xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24);
+ }
+#endif
+ }
+
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
+
+ /* Round 1. */
+ /* Let [abcd k s i] denote the operation
+ a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
+#define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + F(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 0, 7, T1);
+ SET(d, a, b, c, 1, 12, T2);
+ SET(c, d, a, b, 2, 17, T3);
+ SET(b, c, d, a, 3, 22, T4);
+ SET(a, b, c, d, 4, 7, T5);
+ SET(d, a, b, c, 5, 12, T6);
+ SET(c, d, a, b, 6, 17, T7);
+ SET(b, c, d, a, 7, 22, T8);
+ SET(a, b, c, d, 8, 7, T9);
+ SET(d, a, b, c, 9, 12, T10);
+ SET(c, d, a, b, 10, 17, T11);
+ SET(b, c, d, a, 11, 22, T12);
+ SET(a, b, c, d, 12, 7, T13);
+ SET(d, a, b, c, 13, 12, T14);
+ SET(c, d, a, b, 14, 17, T15);
+ SET(b, c, d, a, 15, 22, T16);
+#undef SET
+
+ /* Round 2. */
+ /* Let [abcd k s i] denote the operation
+ a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
+#define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + G(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 1, 5, T17);
+ SET(d, a, b, c, 6, 9, T18);
+ SET(c, d, a, b, 11, 14, T19);
+ SET(b, c, d, a, 0, 20, T20);
+ SET(a, b, c, d, 5, 5, T21);
+ SET(d, a, b, c, 10, 9, T22);
+ SET(c, d, a, b, 15, 14, T23);
+ SET(b, c, d, a, 4, 20, T24);
+ SET(a, b, c, d, 9, 5, T25);
+ SET(d, a, b, c, 14, 9, T26);
+ SET(c, d, a, b, 3, 14, T27);
+ SET(b, c, d, a, 8, 20, T28);
+ SET(a, b, c, d, 13, 5, T29);
+ SET(d, a, b, c, 2, 9, T30);
+ SET(c, d, a, b, 7, 14, T31);
+ SET(b, c, d, a, 12, 20, T32);
+#undef SET
+
+ /* Round 3. */
+ /* Let [abcd k s t] denote the operation
+ a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + H(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 5, 4, T33);
+ SET(d, a, b, c, 8, 11, T34);
+ SET(c, d, a, b, 11, 16, T35);
+ SET(b, c, d, a, 14, 23, T36);
+ SET(a, b, c, d, 1, 4, T37);
+ SET(d, a, b, c, 4, 11, T38);
+ SET(c, d, a, b, 7, 16, T39);
+ SET(b, c, d, a, 10, 23, T40);
+ SET(a, b, c, d, 13, 4, T41);
+ SET(d, a, b, c, 0, 11, T42);
+ SET(c, d, a, b, 3, 16, T43);
+ SET(b, c, d, a, 6, 23, T44);
+ SET(a, b, c, d, 9, 4, T45);
+ SET(d, a, b, c, 12, 11, T46);
+ SET(c, d, a, b, 15, 16, T47);
+ SET(b, c, d, a, 2, 23, T48);
+#undef SET
+
+ /* Round 4. */
+ /* Let [abcd k s t] denote the operation
+ a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
+#define I(x, y, z) ((y) ^ ((x) | ~(z)))
+#define SET(a, b, c, d, k, s, Ti)\
+ t = a + I(b,c,d) + X[k] + Ti;\
+ a = ROTATE_LEFT(t, s) + b
+ /* Do the following 16 operations. */
+ SET(a, b, c, d, 0, 6, T49);
+ SET(d, a, b, c, 7, 10, T50);
+ SET(c, d, a, b, 14, 15, T51);
+ SET(b, c, d, a, 5, 21, T52);
+ SET(a, b, c, d, 12, 6, T53);
+ SET(d, a, b, c, 3, 10, T54);
+ SET(c, d, a, b, 10, 15, T55);
+ SET(b, c, d, a, 1, 21, T56);
+ SET(a, b, c, d, 8, 6, T57);
+ SET(d, a, b, c, 15, 10, T58);
+ SET(c, d, a, b, 6, 15, T59);
+ SET(b, c, d, a, 13, 21, T60);
+ SET(a, b, c, d, 4, 6, T61);
+ SET(d, a, b, c, 11, 10, T62);
+ SET(c, d, a, b, 2, 15, T63);
+ SET(b, c, d, a, 9, 21, T64);
+#undef SET
+
+ /* Then perform the following additions. (That is increment each
+ of the four registers by the value it had before this block
+ was started.) */
+ pms->abcd[0] += a;
+ pms->abcd[1] += b;
+ pms->abcd[2] += c;
+ pms->abcd[3] += d;
+}
+
+void
+MojoMd5_init(MojoMd5 *pms)
+{
+ pms->count[0] = pms->count[1] = 0;
+ pms->abcd[0] = 0x67452301;
+ pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476;
+ pms->abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301;
+ pms->abcd[3] = 0x10325476;
+}
+
+void
+MojoMd5_append(MojoMd5 *pms, const uint8 *data, int nbytes)
+{
+ const uint8 *p = data;
+ int left = nbytes;
+ int offset = (pms->count[0] >> 3) & 63;
+ uint32 nbits = (uint32)(nbytes << 3);
+
+ if (nbytes <= 0)
+ return;
+
+ /* Update the message length. */
+ pms->count[1] += nbytes >> 29;
+ pms->count[0] += nbits;
+ if (pms->count[0] < nbits)
+ pms->count[1]++;
+
+ /* Process an initial partial block. */
+ if (offset) {
+ int copy = (offset + nbytes > 64 ? 64 - offset : nbytes);
+
+ memcpy(pms->buf + offset, p, copy);
+ if (offset + copy < 64)
+ return;
+ p += copy;
+ left -= copy;
+ MojoMd5_process(pms, pms->buf);
+ }
+
+ /* Process full blocks. */
+ for (; left >= 64; p += 64, left -= 64)
+ MojoMd5_process(pms, p);
+
+ /* Process a final partial block. */
+ if (left)
+ memcpy(pms->buf, p, left);
+}
+
+void
+MojoMd5_finish(MojoMd5 *pms, uint8 digest[16])
+{
+ const uint8 pad[64] = {
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+ };
+ uint8 data[8];
+ int i;
+
+ /* Save the length before padding. */
+ for (i = 0; i < 8; ++i)
+ data[i] = (uint8)(pms->count[i >> 2] >> ((i & 3) << 3));
+ /* Pad to 56 bytes mod 64. */
+ MojoMd5_append(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1);
+ /* Append the length. */
+ MojoMd5_append(pms, data, 8);
+ for (i = 0; i < 16; ++i)
+ digest[i] = (uint8)(pms->abcd[i >> 2] >> ((i & 3) << 3));
+}
+
+#endif // SUPPORT_MD5
+
+#if TEST_MD5
+int main(int argc, char **argv)
+{
+ int i = 0;
+ for (i = 1; i < argc; i++)
+ {
+ FILE *in = NULL;
+ MojoMd5 ctx;
+ MojoMd5_init(&ctx);
+ in = fopen(argv[i], "rb");
+ if (!in)
+ perror("fopen");
+ else
+ {
+ uint8 dig[16];
+ int err = 0;
+ while ( (!err) && (!feof(in)) )
+ {
+ uint8 buf[1024];
+ size_t rc = fread(buf, 1, sizeof (buf), in);
+ if (rc > 0)
+ MojoMd5_append(&ctx, buf, rc);
+ err = ferror(in);
+ } // while
+
+ if (err)
+ perror("fread");
+ fclose(in);
+ MojoMd5_finish(&ctx, dig);
+
+ if (!err)
+ {
+ printf("%s: %x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x\n", argv[i],
+ (int) dig[0], (int) dig[1], (int) dig[2], (int) dig[3],
+ (int) dig[4], (int) dig[5], (int) dig[6], (int) dig[7],
+ (int) dig[8], (int) dig[9], (int) dig[10], (int) dig[11],
+ (int) dig[12], (int) dig[13], (int) dig[14], (int) dig[15]);
+ } // if
+ } // else
+ } // for
+
+ return 0;
+} // main
+#endif
+
+// end of checksum_md5.c ...
+
Added: trunk/checksum_sha1.c
===================================================================
--- trunk/checksum_sha1.c (rev 0)
+++ trunk/checksum_sha1.c 2007-05-27 15:56:12 UTC (rev 312)
@@ -0,0 +1,210 @@
+#include "universal.h"
+
+#if SUPPORT_SHA1
+
+// SHA-1 code originally from ftp://ftp.funet.fi/pub/crypt/hash/sha/sha1.c
+// License: public domain.
+// I cleaned it up a little for MojoSetup's specific purposes. --ryan.
+
+/*
+SHA-1 in C
+By Steve Reid <steve at edmweb.com>
+100% Public Domain
+
+Test Vectors (from FIPS PUB 180-1)
+"abc"
+ A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
+"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
+ 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
+A million repetitions of "a"
+ 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
+*/
+
+#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
+
+/* blk0() and blk() perform the initial expand. */
+/* I got the idea of expanding during the round function from SSLeay */
+#if PLATFORM_LITTLEENDIAN
+#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
+ |(rol(block->l[i],8)&0x00FF00FF))
+#else
+#define blk0(i) block->l[i]
+#endif
+#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
+ ^block->l[(i+2)&15]^block->l[i&15],1))
+
+/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
+#define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
+#define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
+#define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
+#define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
+
+
+/* Hash a single 512-bit block. This is the core of the algorithm. */
+
+static void MojoSha1_transform(uint32_t state[5], const uint8_t buffer[64])
+{
+ uint32_t a, b, c, d, e;
+ typedef union {
+ uint8_t c[64];
+ uint32_t l[16];
+ } CHAR64LONG16;
+ CHAR64LONG16* block;
+ static uint8_t workspace[64];
+ block = (CHAR64LONG16*)workspace;
+ memcpy(block, buffer, 64);
+ /* Copy context->state[] to working vars */
+ a = state[0];
+ b = state[1];
+ c = state[2];
+ d = state[3];
+ e = state[4];
+ /* 4 rounds of 20 operations each. Loop unrolled. */
+ R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
+ R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
+ R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
+ R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ /* Add the working vars back into context.state[] */
+ state[0] += a;
+ state[1] += b;
+ state[2] += c;
+ state[3] += d;
+ state[4] += e;
+ /* Wipe variables */
+ a = b = c = d = e = 0;
+}
+
+
+/* MojoSha1_init - Initialize new context */
+
+void MojoSha1_init(MojoSha1 *context)
+{
+ /* SHA1 initialization constants */
+ context->state[0] = 0x67452301;
+ context->state[1] = 0xEFCDAB89;
+ context->state[2] = 0x98BADCFE;
+ context->state[3] = 0x10325476;
+ context->state[4] = 0xC3D2E1F0;
+ context->count[0] = context->count[1] = 0;
+}
+
+
+/* Run your data through this. */
+
+void MojoSha1_append(MojoSha1 *context, const uint8_t *data, uint32_t len)
+{
+ uint32_t i, j;
+
+ j = (context->count[0] >> 3) & 63;
+ if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++;
+ context->count[1] += (len >> 29);
+ if ((j + len) > 63) {
+ memcpy(&context->buffer[j], data, (i = 64-j));
+ MojoSha1_transform(context->state, context->buffer);
+ for ( ; i + 63 < len; i += 64) {
+ MojoSha1_transform(context->state, &data[i]);
+ }
+ j = 0;
+ }
+ else i = 0;
+ memcpy(&context->buffer[j], &data[i], len - i);
+}
+
+
+/* Add padding and return the message digest. */
+
+void MojoSha1_finish(MojoSha1 *context, uint8_t digest[20])
+{
+ uint32_t i, j;
+ uint8_t finalcount[8];
+
+ for (i = 0; i < 8; i++) {
+ finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
+ >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
+ }
+ MojoSha1_append(context, (uint8_t *)"\200", 1);
+ while ((context->count[0] & 504) != 448) {
+ MojoSha1_append(context, (uint8_t *)"\0", 1);
+ }
+ MojoSha1_append(context, finalcount, 8); /* Should cause a MojoSha1_transform() */
+ for (i = 0; i < 20; i++) {
+ digest[i] = (uint8_t)
+ ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
+ }
+ /* Wipe variables */
+ i = j = 0;
+ memset(context->buffer, 0, 64);
+ memset(context->state, 0, 20);
+ memset(context->count, 0, 8);
+ memset(&finalcount, 0, 8);
+ MojoSha1_transform(context->state, context->buffer);
+}
+
+#endif // SUPPORT_SHA1
+
+#if TEST_SHA1
+int main(int argc, char **argv)
+{
+ int i = 0;
+ for (i = 1; i < argc; i++)
+ {
+ FILE *in = NULL;
+ MojoSha1 ctx;
+ MojoSha1_init(&ctx);
+ in = fopen(argv[i], "rb");
+ if (!in)
+ perror("fopen");
+ else
+ {
+ uint8_t dig[20];
+ int err = 0;
+ while ( (!err) && (!feof(in)) )
+ {
+ uint8_t buf[1024];
+ size_t rc = fread(buf, 1, sizeof (buf), in);
+ if (rc > 0)
+ MojoSha1_append(&ctx, buf, rc);
+ err = ferror(in);
+ } // while
+
+ if (err)
+ perror("fread");
+ fclose(in);
+ MojoSha1_finish(&ctx, dig);
+
+ if (!err)
+ {
+ printf("%s: %x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x\n",
+ argv[i],
+ (int) dig[0], (int) dig[1], (int) dig[2], (int) dig[3],
+ (int) dig[4], (int) dig[5], (int) dig[6], (int) dig[7],
+ (int) dig[8], (int) dig[9], (int) dig[10], (int) dig[11],
+ (int) dig[12], (int) dig[13], (int) dig[14], (int) dig[15],
+ (int) dig[16], (int) dig[17], (int) dig[18], (int) dig[19]);
+ } // if
+ } // else
+ } // for
+
+ return 0;
+} // main
+#endif
+
+// end of checksum_sha1.c ...
+
Modified: trunk/fileio.c
===================================================================
--- trunk/fileio.c 2007-05-27 06:01:08 UTC (rev 311)
+++ trunk/fileio.c 2007-05-27 15:56:12 UTC (rev 312)
@@ -84,6 +84,7 @@
// !!! FIXME: I'd rather not use a callback here, but I can't see a cleaner
// !!! FIXME: way right now...
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
+ MojoChecksums *checksums,
MojoInput_FileCopyCallback cb, void *data)
{
boolean retval = false;
@@ -92,10 +93,17 @@
boolean iofailure = false;
int64 flen = 0;
int64 bw = 0;
+ MojoChecksumContext sumctx;
if (in == NULL)
return false;
+ if (checksums != NULL)
+ {
+ memset(checksums, '\0', sizeof (MojoChecksums));
+ MojoChecksum_init(&sumctx);
+ } // if
+
// Wait for a ready(), so length() can be meaningful on network streams.
while ((!in->ready(in)) && (!iofailure))
{
@@ -137,7 +145,11 @@
if (fwrite(scratchbuf_128k, br, 1, out) != 1)
iofailure = true;
else
+ {
+ if (checksums != NULL)
+ MojoChecksum_append(&sumctx, scratchbuf_128k, br);
bw += br;
+ } // else
} // else
} // else
@@ -156,6 +168,8 @@
else
{
MojoPlatform_chmod(fname, perms);
+ if (checksums != NULL)
+ MojoChecksum_finish(&sumctx, checksums);
retval = true;
} // else
} // if
Modified: trunk/fileio.h
===================================================================
--- trunk/fileio.h 2007-05-27 06:01:08 UTC (rev 311)
+++ trunk/fileio.h 2007-05-27 15:56:12 UTC (rev 312)
@@ -100,6 +100,7 @@
typedef boolean (*MojoInput_FileCopyCallback)(uint32 ticks, int64 justwrote,
int64 bw, int64 total, void *data);
boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
+ MojoChecksums *checksums,
MojoInput_FileCopyCallback cb, void *data);
MojoInput *MojoInput_fromURL(const char *url);
Modified: trunk/lua_glue.c
===================================================================
--- trunk/lua_glue.c 2007-05-27 06:01:08 UTC (rev 311)
+++ trunk/lua_glue.c 2007-05-27 15:56:12 UTC (rev 312)
@@ -152,6 +152,49 @@
} // retvalLightUserData
+static int retvalChecksums(lua_State *L, const MojoChecksums *sums)
+{
+ lua_newtable(L);
+
+ #if SUPPORT_CRC32
+ {
+ char buf[64];
+ snprintf(buf, sizeof (buf), "%X", (unsigned int) sums->crc32);
+ set_string(L, buf, "crc32");
+ }
+ #endif
+
+ #if SUPPORT_MD5
+ {
+ char buf[64];
+ const uint8 *dig = sums->md5;
+ snprintf(buf, sizeof (buf), "%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
+ (int) dig[0], (int) dig[1], (int) dig[2], (int) dig[3],
+ (int) dig[4], (int) dig[5], (int) dig[6], (int) dig[7],
+ (int) dig[8], (int) dig[9], (int) dig[10], (int) dig[11],
+ (int) dig[12], (int) dig[13], (int) dig[14], (int) dig[15]);
+ set_string(L, buf, "md5");
+ }
+ #endif
+
+ #if SUPPORT_SHA1
+ {
+ char buf[64];
+ const uint8 *dig = sums->sha1;
+ snprintf(buf, sizeof (buf), "%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X%X",
+ (int) dig[0], (int) dig[1], (int) dig[2], (int) dig[3],
+ (int) dig[4], (int) dig[5], (int) dig[6], (int) dig[7],
+ (int) dig[8], (int) dig[9], (int) dig[10], (int) dig[11],
+ (int) dig[12], (int) dig[13], (int) dig[14], (int) dig[15],
+ (int) dig[16], (int) dig[17], (int) dig[18], (int) dig[19]);
+ set_string(L, buf, "sha1");
+ }
+ #endif
+
+ return 1;
+} // retvalChecksums
+
+
static inline int snprintfcat(char **ptr, size_t *len, const char *fmt, ...)
{
int bw = 0;
@@ -673,8 +716,10 @@
{
MojoArchive *archive = (MojoArchive *) lua_touserdata(L, 1);
const char *path = luaL_checkstring(L, 2);
+ int retval = 0;
+ boolean rc = false;
uint16 perms = archive->prevEnum.perms;
- boolean retval = false;
+ MojoChecksums sums;
MojoInput *in = archive->openCurrentEntry(archive);
if (in != NULL)
{
@@ -686,10 +731,14 @@
if (!valid)
fatal(_("BUG: '%s' is not a valid permission string"), permstr);
} // if
- retval = MojoInput_toPhysicalFile(in, path, perms, writeCallback, L);
+ rc = MojoInput_toPhysicalFile(in, path, perms, &sums,
+ writeCallback, L);
} // if
- return retvalBoolean(L, retval);
+ retval += retvalBoolean(L, rc);
+ if (rc)
+ retval += retvalChecksums(L, &sums);
+ return retval;
} // luahook_writefile
@@ -706,14 +755,23 @@
static int luahook_download(lua_State *L)
{
- boolean retval = false;
const char *url = luaL_checkstring(L, 1);
const char *dst = luaL_checkstring(L, 2);
+ int retval = 0;
+ boolean rc = false;
+ MojoChecksums sums;
MojoInput *in = MojoInput_fromURL(url);
- // !!! FIXME: Unix-specific permissions thing here.
if (in != NULL)
- retval = MojoInput_toPhysicalFile(in, dst, 0644, writeCallback, L);
- return retvalBoolean(L, retval);
+ {
+ // !!! FIXME: Unix-specific permissions thing here.
+ rc = MojoInput_toPhysicalFile(in, dst, 0644, &sums,
+ writeCallback, L);
+ } // if
+
+ retval += retvalBoolean(L, rc);
+ if (rc)
+ retval += retvalChecksums(L, &sums);
+ return retval;
} // luahook_download
@@ -858,7 +916,7 @@
{
uint16 perms = 0;
MojoPlatform_perms(src, &perms);
- retval = MojoInput_toPhysicalFile(in, dst, perms, NULL, NULL);
+ retval = MojoInput_toPhysicalFile(in, dst, perms, NULL, NULL, NULL);
if (retval)
{
retval = MojoPlatform_unlink(src);
Modified: trunk/mojosetup.c
===================================================================
--- trunk/mojosetup.c 2007-05-27 06:01:08 UTC (rev 311)
+++ trunk/mojosetup.c 2007-05-27 15:56:12 UTC (rev 312)
@@ -159,6 +159,50 @@
} // deinitEverything
+void MojoChecksum_init(MojoChecksumContext *ctx)
+{
+ memset(ctx, '\0', sizeof (MojoChecksumContext));
+ #if SUPPORT_CRC32
+ MojoCrc32_init(&ctx->crc32);
+ #endif
+ #if SUPPORT_MD5
+ MojoMd5_init(&ctx->md5);
+ #endif
+ #if SUPPORT_SHA1
+ MojoSha1_init(&ctx->sha1);
+ #endif
+} // MojoChecksum_init
+
+
+void MojoChecksum_append(MojoChecksumContext *ctx, const uint8 *d, uint32 len)
+{
+ #if SUPPORT_CRC32
+ MojoCrc32_append(&ctx->crc32, d, len);
+ #endif
+ #if SUPPORT_MD5
+ MojoMd5_append(&ctx->md5, d, len);
+ #endif
+ #if SUPPORT_SHA1
+ MojoSha1_append(&ctx->sha1, d, len);
+ #endif
+} // MojoChecksum_append
+
+
+void MojoChecksum_finish(MojoChecksumContext *ctx, MojoChecksums *sums)
+{
+ memset(sums, '\0', sizeof (MojoChecksums));
+ #if SUPPORT_CRC32
+ MojoCrc32_finish(&ctx->crc32, &sums->crc32);
+ #endif
+ #if SUPPORT_MD5
+ MojoMd5_finish(&ctx->md5, sums->md5);
+ #endif
+ #if SUPPORT_SHA1
+ MojoSha1_finish(&ctx->sha1, sums->sha1);
+ #endif
+} // MojoChecksum_finish
+
+
boolean cmdline(const char *arg)
{
int argc = GArgc;
Modified: trunk/scripts/mojosetup_mainline.lua
===================================================================
--- trunk/scripts/mojosetup_mainline.lua 2007-05-27 06:01:08 UTC (rev 311)
+++ trunk/scripts/mojosetup_mainline.lua 2007-05-27 15:56:12 UTC (rev 312)
@@ -252,7 +252,8 @@
end
MojoSetup.installed_files[#MojoSetup.installed_files+1] = path
- if not MojoSetup.writefile(archive, path, perms, callback) then
+ local written, sums = MojoSetup.writefile(archive, path, perms, callback)
+ if not written then
-- !!! FIXME: formatting!
if not keepgoing then
MojoSetup.logerror("User cancelled install during file write.")
@@ -795,7 +796,8 @@
end
MojoSetup.loginfo("Download '" .. url .. "' to '" .. f .. "'")
- if not MojoSetup.download(url, f, callback) then
+ local downloaded, sums = MojoSetup.download(url, f, callback)
+ if not downloaded then
-- !!! FIXME: formatting!
MojoSetup.fatal(_("file download failed!"))
end
Modified: trunk/universal.h
===================================================================
--- trunk/universal.h 2007-05-27 06:01:08 UTC (rev 311)
+++ trunk/universal.h 2007-05-27 15:56:12 UTC (rev 312)
@@ -181,6 +181,59 @@
void logInfo(const char *fmt, ...) ISPRINTF(1,2);
void logDebug(const char *fmt, ...) ISPRINTF(1,2);
+
+// Checksums.
+
+typedef uint32 MojoCrc32;
+void MojoCrc32_init(MojoCrc32 *context);
+void MojoCrc32_append(MojoCrc32 *context, const uint8 *buf, uint32 len);
+void MojoCrc32_finish(MojoCrc32 *context, uint32 *digest);
+
+
+typedef struct MojoMd5
+{
+ uint32 count[2];
+ uint32 abcd[4];
+ uint8 buf[64];
+} MojoMd5;
+
+void MojoMd5_init(MojoMd5 *pms);
+void MojoMd5_append(MojoMd5 *pms, const uint8 *data, int nbytes);
+void MojoMd5_finish(MojoMd5 *pms, uint8 digest[16]);
+
+
+typedef struct
+{
+ uint32 state[5];
+ uint32 count[2];
+ uint8 buffer[64];
+} MojoSha1;
+
+void MojoSha1_init(MojoSha1 *context);
+void MojoSha1_append(MojoSha1 *context, const uint8 *data, uint32 len);
+void MojoSha1_finish(MojoSha1 *context, uint8 digest[20]);
+
+typedef struct MojoChecksumContext
+{
+ MojoCrc32 crc32;
+ MojoMd5 md5;
+ MojoSha1 sha1;
+} MojoChecksumContext;
+
+
+typedef struct MojoChecksums
+{
+ uint32 crc32;
+ uint8 md5[16];
+ uint8 sha1[20];
+} MojoChecksums;
+
+
+void MojoChecksum_init(MojoChecksumContext *ctx);
+void MojoChecksum_append(MojoChecksumContext *c, const uint8 *data, uint32 ln);
+void MojoChecksum_finish(MojoChecksumContext *c, MojoChecksums *sums);
+
+
// A pointer to this struct is passed to plugins, so they can access
// functionality in the base application. (Add to this as you need to.)
typedef struct MojoSetupEntryPoints
More information about the mojosetup-commits
mailing list