summaryrefslogtreecommitdiff
path: root/depends/md5
diff options
context:
space:
mode:
authorPetr Mrázek2012-03-13 14:46:48 +0100
committerPetr Mrázek2012-03-13 14:46:48 +0100
commiteb4757043b12764f20c6bd1a6edc12201f74b2ce (patch)
treecfb41b761fa691651b88ce330fde59e57862a384 /depends/md5
parentb3f6bccdf6ba559cfbff462029ea350f5d367171 (diff)
downloaddfhack-eb4757043b12764f20c6bd1a6edc12201f74b2ce.tar.gz
dfhack-eb4757043b12764f20c6bd1a6edc12201f74b2ce.tar.bz2
dfhack-eb4757043b12764f20c6bd1a6edc12201f74b2ce.tar.xz
Move depends out of main library, make them (static) libraries.
Diffstat (limited to 'depends/md5')
-rw-r--r--depends/md5/CMakeLists.txt3
-rw-r--r--depends/md5/md5.cpp240
-rw-r--r--depends/md5/md5.h36
-rw-r--r--depends/md5/md5wrapper.cpp167
-rw-r--r--depends/md5/md5wrapper.h69
5 files changed, 515 insertions, 0 deletions
diff --git a/depends/md5/CMakeLists.txt b/depends/md5/CMakeLists.txt
new file mode 100644
index 00000000..69e0cf0b
--- /dev/null
+++ b/depends/md5/CMakeLists.txt
@@ -0,0 +1,3 @@
+project(dfhack-md5)
+ADD_LIBRARY(dfhack-md5 STATIC EXCLUDE_FROM_ALL md5.cpp md5wrapper.cpp)
+IDE_FOLDER(dfhack-md5 "Depends") \ No newline at end of file
diff --git a/depends/md5/md5.cpp b/depends/md5/md5.cpp
new file mode 100644
index 00000000..5ee4fb45
--- /dev/null
+++ b/depends/md5/md5.cpp
@@ -0,0 +1,240 @@
+/*
+ * This code implements the MD5 message-digest algorithm.
+ * The algorithm is due to Ron Rivest. This code was
+ * written by Colin Plumb in 1993, no copyright is claimed.
+ * This code is in the public domain; do with it what you wish.
+ */
+
+#include <string.h>
+#include "md5.h"
+
+#ifndef HIGHFIRST
+#define byteReverse(buf, len) /* Nothing */
+#else
+/*
+ * Note: this code is harmless on little-endian machines.
+ */
+void byteReverse(unsigned char *buf, unsigned longs)
+{
+ uint32_t t;
+ do
+ {
+ t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
+ ((unsigned) buf[1] << 8 | buf[0]);
+ *(uint32_t *) buf = t;
+ buf += 4;
+ } while (--longs);
+}
+#endif
+
+/*
+ * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
+ * initialization constants.
+ */
+void MD5Init(MD5Context *ctx)
+{
+ ctx->buf[0] = 0x67452301;
+ ctx->buf[1] = 0xefcdab89;
+ ctx->buf[2] = 0x98badcfe;
+ ctx->buf[3] = 0x10325476;
+
+ ctx->bits[0] = 0;
+ ctx->bits[1] = 0;
+}
+
+/*
+ * Update context to reflect the concatenation of another buffer full
+ * of bytes.
+ */
+void MD5Update( MD5Context *ctx, unsigned char *buf, unsigned len)
+{
+ uint32_t t;
+
+ /* Update bitcount */
+
+ t = ctx->bits[0];
+ if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
+ ctx->bits[1]++; /* Carry from low to high */
+ ctx->bits[1] += len >> 29;
+
+ t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
+
+ /* Handle any leading odd-sized chunks */
+ if (t)
+ {
+ unsigned char *p = (unsigned char *) ctx->in + t;
+
+ t = 64 - t;
+ if (len < t)
+ {
+ memcpy(p, buf, len);
+ return;
+ }
+ memcpy(p, buf, t);
+ byteReverse(ctx->in, 16);
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ buf += t;
+ len -= t;
+ }
+ /* Process data in 64-byte chunks */
+ while (len >= 64)
+ {
+ memcpy(ctx->in, buf, 64);
+ byteReverse(ctx->in, 16);
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ buf += 64;
+ len -= 64;
+ }
+
+ /* Handle any remaining bytes of data. */
+ memcpy(ctx->in, buf, len);
+}
+
+/*
+ * Final wrapup - pad to 64-byte boundary with the bit pattern
+ * 1 0* (64-bit count of bits processed, MSB-first)
+ */
+void MD5Final(unsigned char digest[16], MD5Context *ctx)
+{
+ unsigned count;
+ unsigned char *p;
+
+ /* Compute number of bytes mod 64 */
+ count = (ctx->bits[0] >> 3) & 0x3F;
+
+ /* Set the first char of padding to 0x80. This is safe since there is
+ always at least one byte free */
+ p = ctx->in + count;
+ *p++ = 0x80;
+
+ /* Bytes of padding needed to make 64 bytes */
+ count = 64 - 1 - count;
+
+ /* Pad out to 56 mod 64 */
+ if (count < 8)
+ {
+ /* Two lots of padding: Pad the first block to 64 bytes */
+ memset(p, 0, count);
+ byteReverse(ctx->in, 16);
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+
+ /* Now fill the next block with 56 bytes */
+ memset(ctx->in, 0, 56);
+ }
+ else
+ {
+ /* Pad block to 56 bytes */
+ memset(p, 0, count - 8);
+ }
+ byteReverse(ctx->in, 14);
+
+ /* Append length in bits and transform */
+ ((uint32_t *) ctx->in)[14] = ctx->bits[0];
+ ((uint32_t *) ctx->in)[15] = ctx->bits[1];
+
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ byteReverse((unsigned char *) ctx->buf, 4);
+ memcpy(digest, ctx->buf, 16);
+ memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
+}
+
+
+/* The four core functions - F1 is optimized somewhat */
+
+/* #define F1(x, y, z) (x & y | ~x & z) */
+#define F1(x, y, z) (z ^ (x & (y ^ z)))
+#define F2(x, y, z) F1(z, x, y)
+#define F3(x, y, z) (x ^ y ^ z)
+#define F4(x, y, z) (y ^ (x | ~z))
+
+/* This is the central step in the MD5 algorithm. */
+#define MD5STEP(f, w, x, y, z, data, s) \
+ ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
+
+/*
+ * The core of the MD5 algorithm, this alters an existing MD5 hash to
+ * reflect the addition of 16 longwords of new data. MD5Update blocks
+ * the data and converts bytes into longwords for this routine.
+ */
+void MD5Transform(uint32_t buf[4], uint32_t in[16])
+{
+ register uint32_t a, b, c, d;
+
+ a = buf[0];
+ b = buf[1];
+ c = buf[2];
+ d = buf[3];
+
+ MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
+ MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
+ MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
+ MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
+ MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
+ MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
+ MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
+ MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
+ MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
+ MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
+ MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
+ MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
+ MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
+ MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
+ MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
+ MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
+
+ MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
+ MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
+ MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
+ MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
+ MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
+ MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
+ MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
+ MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
+ MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
+ MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
+ MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
+ MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
+ MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
+ MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
+ MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
+ MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
+
+ MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
+ MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
+ MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
+ MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
+ MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
+ MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
+ MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
+ MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
+ MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
+ MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
+ MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
+ MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
+ MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
+ MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
+ MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
+ MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
+
+ MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
+ MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
+ MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
+ MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
+ MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
+ MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
+ MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
+ MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
+ MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
+ MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
+ MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
+ MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
+ MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
+ MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
+ MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
+ MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
+
+ buf[0] += a;
+ buf[1] += b;
+ buf[2] += c;
+ buf[3] += d;
+}
diff --git a/depends/md5/md5.h b/depends/md5/md5.h
new file mode 100644
index 00000000..0a04051b
--- /dev/null
+++ b/depends/md5/md5.h
@@ -0,0 +1,36 @@
+#pragma once
+
+/* The following tests optimise behaviour on little-endian
+ machines, where there is no need to reverse the byte order
+ of 32 bit words in the MD5 computation. By default,
+ HIGHFIRST is defined, which indicates we're running on a
+ big-endian (most significant byte first) machine, on which
+ the byteReverse function in md5.c must be invoked. However,
+ byteReverse is coded in such a way that it is an identity
+ function when run on a little-endian machine, so calling it
+ on such a platform causes no harm apart from wasting time.
+ If the platform is known to be little-endian, we speed
+ things up by undefining HIGHFIRST, which defines
+ byteReverse as a null macro. Doing things in this manner
+ insures we work on new platforms regardless of their byte
+ order. */
+
+#define HIGHFIRST
+
+#ifdef __i386__
+#undef HIGHFIRST
+#endif
+
+#include <stdint.h>
+
+struct MD5Context
+{
+ uint32_t buf[4];
+ uint32_t bits[2];
+ unsigned char in[64];
+};
+
+extern void MD5Init( MD5Context *ctx);
+extern void MD5Update( MD5Context *ctx, unsigned char *buf, unsigned len);
+extern void MD5Final(unsigned char digest[16], MD5Context *ctx);
+extern void MD5Transform(uint32_t buf[4], uint32_t in[16]);
diff --git a/depends/md5/md5wrapper.cpp b/depends/md5/md5wrapper.cpp
new file mode 100644
index 00000000..e12b6578
--- /dev/null
+++ b/depends/md5/md5wrapper.cpp
@@ -0,0 +1,167 @@
+/*
+ * This is part of my wrapper-class to create
+ * a MD5 Hash from a string and a file.
+ *
+ * This code is completly free, you
+ * can copy it, modify it, or do
+ * what ever you want with it.
+ *
+ * Feb. 2005
+ * Benjamin Grüdelbach
+ */
+
+/*
+ * Changed unsigned long int types into uint32_t to make this work on 64bit systems.
+ * Sep. 5. 2009
+ * Petr Mrázek
+ */
+
+#if defined(_MSC_VER) && _MSC_VER >= 1400
+#define _CRT_SECURE_NO_WARNINGS
+#endif
+
+//----------------------------------------------------------------------
+//basic includes
+#include <fstream>
+#include <iostream>
+#include <errno.h>
+#include <string.h>
+//my includes
+#include "md5wrapper.h"
+#include "md5.h"
+
+//---------privates--------------------------
+
+/*
+ * internal hash function, calling
+ * the basic methods from md5.h
+ */
+std::string md5wrapper::hashit(std::string text)
+{
+ MD5Context ctx;
+
+ //init md5
+ MD5Init(&ctx);
+ //update with our string
+ MD5Update(&ctx,
+ (unsigned char*)text.c_str(),
+ text.length());
+
+ //create the hash
+ unsigned char buff[16] = "";
+ MD5Final((unsigned char*)buff,&ctx);
+
+ //converte the hash to a string and return it
+ return convToString(buff);
+}
+
+/*
+ * converts the numeric hash to
+ * a valid std::string.
+ * (based on Jim Howard's code;
+ * http://www.codeproject.com/cpp/cmd5.asp)
+ */
+std::string md5wrapper::convToString(unsigned char *bytes)
+{
+ char asciihash[33];
+
+ int p = 0;
+ for(int i=0; i<16; i++)
+ {
+ ::sprintf(&asciihash[p],"%02x",bytes[i]);
+ p += 2;
+ }
+ asciihash[32] = '\0';
+ return std::string(asciihash);
+}
+
+//---------publics--------------------------
+
+//constructor
+md5wrapper::md5wrapper()
+{
+}
+
+
+//destructor
+md5wrapper::~md5wrapper()
+{
+}
+
+/*
+ * creates a MD5 hash from
+ * "text" and returns it as
+ * string
+ */
+std::string md5wrapper::getHashFromString(std::string text)
+{
+ return this->hashit(text);
+}
+
+
+/*
+ * creates a MD5 hash from
+ * a file specified in "filename" and
+ * returns it as string
+ * (based on Ronald L. Rivest's code
+ * from RFC1321 "The MD5 Message-Digest Algorithm")
+ */
+std::string md5wrapper::getHashFromFile(std::string filename, uint32_t & length, char * first_kb)
+{
+ FILE *file;
+ MD5Context context;
+
+ int len;
+ int saved = 0;
+ unsigned char buffer[1024], digest[16];
+
+ //open file
+ if ((file = fopen (filename.c_str(), "rb")) == NULL)
+ {
+ return "file unreadable.";
+ }
+ length = 0;
+ //init md5
+ MD5Init (&context);
+
+ //read the filecontent
+ while (1)
+ {
+ errno = 0;
+ len = fread (buffer, 1, 1024, file);
+ if(saved < 1024 && first_kb)
+ {
+ memcpy(first_kb + saved, buffer, std::min (len, 1024 - saved));
+ saved += len;
+ }
+ length += len;
+ if(len != 1024)
+ {
+ int err = ferror(file);
+ //FIXME: check errno here.
+ if(err)
+ {
+ fclose(file);
+ return strerror(err);
+ }
+ if(feof(file))
+ {
+ MD5Update (&context, buffer, len);
+ break;
+ }
+ }
+ MD5Update (&context, buffer, len);
+ }
+
+ /*
+ generate hash, close the file and return the
+ hash as std::string
+ */
+ MD5Final (digest, &context);
+ fclose (file);
+ return convToString(digest);
+ }
+
+/*
+ * EOF
+ */
diff --git a/depends/md5/md5wrapper.h b/depends/md5/md5wrapper.h
new file mode 100644
index 00000000..1a41192a
--- /dev/null
+++ b/depends/md5/md5wrapper.h
@@ -0,0 +1,69 @@
+/*
+ * This is my wrapper-class to create
+ * a MD5 Hash from a string and a file.
+ *
+ * This code is completly free, you
+ * can copy it, modify it, or do
+ * what ever you want with it.
+ *
+ * Feb. 2005
+ * Benjamin Grüdelbach
+ */
+
+/*
+ * Changed unsigned long int types into uint32_t to make this work on 64bit systems.
+ * Sep. 5. 2009
+ * Petr Mrázek
+ */
+
+//include protection
+#ifndef MD5WRAPPER_H
+#define MD5WRAPPER_H
+
+//basic includes
+#include <string>
+#include <stdint.h>
+
+class md5wrapper
+{
+ private:
+ /*
+ * internal hash function, calling
+ * the basic methods from md5.h
+ */
+ std::string hashit(std::string text);
+
+ /*
+ * converts the numeric giets to
+ * a valid std::string
+ */
+ std::string convToString(unsigned char *bytes);
+ public:
+ //constructor
+ md5wrapper();
+
+ //destructor
+ ~md5wrapper();
+
+ /*
+ * creates a MD5 hash from
+ * "text" and returns it as
+ * string
+ */
+ std::string getHashFromString(std::string text);
+
+ /*
+ * creates a MD5 hash from
+ * a file specified in "filename" and
+ * returns it as string
+ */
+ std::string getHashFromFile(const std::string filename, uint32_t & length, char * first_kb = NULL);
+};
+
+
+//include protection
+#endif
+
+/*
+ * EOF
+ */