From c579f3458775ca45bd86dcb9b8ef09e7d2a03bdc Mon Sep 17 00:00:00 2001 From: Pixel Date: Wed, 5 Aug 2009 11:36:56 -0700 Subject: MPQCryptography.c doesn't really have anything to do anymore with its original piece of code; cleaning it up and renaming it. --- MPQCryptography.c | 179 ---------------------------------------------------- MPQCryptography.h | 30 --------- MSVC8/mpqlib.vcproj | 15 ++--- MSVC9/mpqlib.vcproj | 16 ++--- Makefile | 2 +- Makefile.mingw32 | 3 +- mpq-bios.c | 2 +- mpq-crypto.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++ mpq-crypto.h | 21 ++++++ mpq-misc.c | 2 +- 10 files changed, 204 insertions(+), 230 deletions(-) delete mode 100644 MPQCryptography.c delete mode 100644 MPQCryptography.h create mode 100644 mpq-crypto.c create mode 100644 mpq-crypto.h diff --git a/MPQCryptography.c b/MPQCryptography.c deleted file mode 100644 index e18e279..0000000 --- a/MPQCryptography.c +++ /dev/null @@ -1,179 +0,0 @@ -/* - * MPQCryptography.c - * MPQKit - * - * Created by Jean-Francois Roy on Sat Oct 05 2002. - * Copyright (c) 2002-2007 MacStorm. All rights reserved. - * - */ - -/* - * Ripped for mpqlib - all endinaness stuff has been killed. - * Also, all the unnecessary code has been ripped out. - * Other than that, this is the exact same code. - */ - -#include -#include - -#include "MPQCryptography.h" - -static int crypt_table_initialized = 0; -static uint32_t crypt_table[0x500]; - -/****TODO****/ -/* Re-implement various endianess fixes. */ - -const uint32_t *__mpqlib_get_cryptography_table() -{ - assert(crypt_table_initialized); - return crypt_table; -} - -void __mpqlib_init_cryptography() -{ - // Prepare crypt_table - uint32_t seed = 0x00100001; - uint32_t index1 = 0; - uint32_t index2 = 0; - int32_t i; - - if (!crypt_table_initialized) { - crypt_table_initialized = 1; - - for (index1 = 0; index1 < 0x100; index1++) { - for (index2 = index1, i = 0; i < 5; i++, index2 += 0x100) { - uint32_t temp1, temp2; - - seed = (seed * 125 + 3) % 0x2AAAAB; - temp1 = (seed & 0xFFFF) << 0x10; - - seed = (seed * 125 + 3) % 0x2AAAAB; - temp2 = (seed & 0xFFFF); - - crypt_table[index2] = (temp1 | temp2); - } - } - } -} - -void __mpqlib_encrypt(void *_data, uint32_t length, uint32_t key, char disable_input_swapping) -{ - char * data = (char *) _data; - uint32_t *buffer32 = (uint32_t *) data; - uint32_t seed = 0xEEEEEEEE; - uint32_t ch; - - assert(crypt_table_initialized); - assert(data); - - // Round to 4 bytes - length = length / 4; - - // We duplicate the loop to avoid costly branches - if (disable_input_swapping) { - while (length-- > 0) { - seed += crypt_table[0x400 + (key & 0xFF)]; - ch = *buffer32 ^ (key + seed); - - key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); - seed = *buffer32 + seed + (seed << 5) + 3; - - *buffer32++ = ch; - } - } else { - while (length-- > 0) { - seed += crypt_table[0x400 + (key & 0xFF)]; - ch = *buffer32 ^ (key + seed); - - key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); - seed = *buffer32 + seed + (seed << 5) + 3; - - *buffer32++ = ch; - } - } -} - -void __mpqlib_decrypt(void *_data, uint32_t length, uint32_t key, char disable_output_swapping) -{ - char * data = (char *) _data; - uint32_t *buffer32 = (uint32_t *) data; - uint32_t seed = 0xEEEEEEEE; - uint32_t ch; - - assert(crypt_table_initialized); - assert(data); - - // Round to 4 bytes - length = length / 4; - - if (disable_output_swapping) { - while (length-- > 0) { - ch = *buffer32; - - seed += crypt_table[0x400 + (key & 0xFF)]; - ch = ch ^ (key + seed); - - key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); - seed = ch + seed + (seed << 5) + 3; - - *buffer32++ = ch; - } - - } else { - while (length-- > 0) { - ch = *buffer32; - - seed += crypt_table[0x400 + (key & 0xFF)]; - ch = ch ^ (key + seed); - - key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); - seed = ch + seed + (seed << 5) + 3; - - *buffer32++ = ch; - } - } -} - -uint32_t __mpqlib_hash_cstring(const char *string, uint32_t type) -{ - uint32_t seed1 = 0x7FED7FED; - uint32_t seed2 = 0xEEEEEEEE; - uint32_t shifted_type = (type << 8); - int32_t ch; - - assert(crypt_table_initialized); - assert(string); - - while (*string != 0) { - ch = *string++; - if (ch > 0x60 && ch < 0x7b) - ch -= 0x20; - - seed1 = crypt_table[shifted_type + ch] ^ (seed1 + seed2); - seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3; - } - - return seed1; -} - -uint32_t __mpqlib_hash_data(const char *data, uint32_t length, uint32_t type) -{ - uint32_t seed1 = 0x7FED7FED; - uint32_t seed2 = 0xEEEEEEEE; - uint32_t shifted_type = (type << 8); - int32_t ch; - - assert(crypt_table_initialized); - assert(data); - - while (length > 0) { - ch = *data++; - - seed1 = crypt_table[shifted_type + ch] ^ (seed1 + seed2); - seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3; - length--; - } - - return seed1; -} diff --git a/MPQCryptography.h b/MPQCryptography.h deleted file mode 100644 index 53bac82..0000000 --- a/MPQCryptography.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * MPQCryptography.h - * MPQKit - * - * Created by Jean-Francois Roy on Sat Oct 05 2002. - * Copyright (c) 2002-2007 MacStorm. All rights reserved. - * - */ - -#include "mpqlib-stdint.h" - -#if defined(__cplusplus) -extern "C" { -#endif - - void __mpqlib_init_cryptography(void); - - const uint32_t *__mpqlib_get_cryptography_table(void); - - void __mpqlib_encrypt(void *data, uint32_t length, uint32_t key, - char disable_input_swapping); - void __mpqlib_decrypt(void *data, uint32_t length, uint32_t key, - char disable_output_swapping); - - uint32_t __mpqlib_hash_cstring(const char *string, uint32_t type); - uint32_t __mpqlib_hash_data(const char *data, uint32_t length, uint32_t type); - -#if defined(__cplusplus) -} -#endif diff --git a/MSVC8/mpqlib.vcproj b/MSVC8/mpqlib.vcproj index 6721b2c..de4c75f 100644 --- a/MSVC8/mpqlib.vcproj +++ b/MSVC8/mpqlib.vcproj @@ -164,6 +164,10 @@ RelativePath="..\mpq-bios.c" > + + @@ -180,10 +184,6 @@ RelativePath="..\mpq-misc.c" > - - @@ -218,6 +218,9 @@ RelativePath="..\mpq-bios.h" > + @@ -234,10 +237,6 @@ RelativePath="..\mpq-misc.h" > - - diff --git a/MSVC9/mpqlib.vcproj b/MSVC9/mpqlib.vcproj index 873b84b..db04aea 100644 --- a/MSVC9/mpqlib.vcproj +++ b/MSVC9/mpqlib.vcproj @@ -165,6 +165,10 @@ RelativePath=".\mpq-bios.c" > + + @@ -181,10 +185,6 @@ RelativePath=".\mpq-misc.c" > - - @@ -215,6 +215,10 @@ RelativePath=".\mpq-bios.h" > + + @@ -231,10 +235,6 @@ RelativePath=".\mpq-misc.h" > - - diff --git a/Makefile b/Makefile index 667b867..c41f319 100644 --- a/Makefile +++ b/Makefile @@ -6,12 +6,12 @@ CPPFLAGS = -g -Wall -Werror -D_FILE_OFFSET_BITS=64 -I. LDFLAGS = -g -lz SOURCE_LIST = \ -MPQCryptography.c \ errors.c \ lookupa.c \ hashtab.c \ recycle.c \ mpq-bios.c \ +mpq-crypto.c \ mpq-errors.c \ mpq-file.c \ mpq-fs.c \ diff --git a/Makefile.mingw32 b/Makefile.mingw32 index 4c17789..67081d1 100644 --- a/Makefile.mingw32 +++ b/Makefile.mingw32 @@ -6,13 +6,12 @@ CPPFLAGS = -g -Wall -Werror -D_FILE_OFFSET_BITS=64 -I. -I ../gnuwin32/include LDFLAGS = -g -L../gnuwin32/lib SOURCE_LIST = \ -MPQCryptography.c \ errors.c \ -extract.c \ lookupa.c \ hashtab.c \ recycle.c \ mpq-bios.c \ +mpq-crypto.c \ mpq-errors.c \ mpq-file.c \ mpq-fs.c \ diff --git a/mpq-bios.c b/mpq-bios.c index cf9f581..35f657c 100644 --- a/mpq-bios.c +++ b/mpq-bios.c @@ -22,7 +22,7 @@ #include #include -#include "MPQCryptography.h" +#include "mpq-crypto.h" #include "mpq-bios.h" #include "mpq-errors.h" #include "mpq-misc.h" diff --git a/mpq-crypto.c b/mpq-crypto.c new file mode 100644 index 0000000..7ddbad5 --- /dev/null +++ b/mpq-crypto.c @@ -0,0 +1,164 @@ +#include +#include + +#include "mpq-crypto.h" + +static int crypt_table_initialized = 0; +static uint32_t crypt_table[0x500]; + +/****TODO****/ +/* Re-implement various endianess fixes. */ + +const uint32_t *__mpqlib_get_cryptography_table() +{ + assert(crypt_table_initialized); + return crypt_table; +} + +void __mpqlib_init_cryptography() +{ + // Prepare crypt_table + uint32_t seed = 0x00100001; + uint32_t index1 = 0; + uint32_t index2 = 0; + int32_t i; + + if (!crypt_table_initialized) { + crypt_table_initialized = 1; + + for (index1 = 0; index1 < 0x100; index1++) { + for (index2 = index1, i = 0; i < 5; i++, index2 += 0x100) { + uint32_t temp1, temp2; + + seed = (seed * 125 + 3) % 0x2AAAAB; + temp1 = (seed & 0xFFFF) << 0x10; + + seed = (seed * 125 + 3) % 0x2AAAAB; + temp2 = (seed & 0xFFFF); + + crypt_table[index2] = (temp1 | temp2); + } + } + } +} + +void __mpqlib_encrypt(void *_data, uint32_t length, uint32_t key, char disable_input_swapping) +{ + char * data = (char *) _data; + uint32_t *buffer32 = (uint32_t *) data; + uint32_t seed = 0xEEEEEEEE; + uint32_t ch; + + assert(crypt_table_initialized); + assert(data); + + // Round to 4 bytes + length = length / 4; + + // We duplicate the loop to avoid costly branches + if (disable_input_swapping) { + while (length-- > 0) { + seed += crypt_table[0x400 + (key & 0xFF)]; + ch = *buffer32 ^ (key + seed); + + key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); + seed = *buffer32 + seed + (seed << 5) + 3; + + *buffer32++ = ch; + } + } else { + while (length-- > 0) { + seed += crypt_table[0x400 + (key & 0xFF)]; + ch = *buffer32 ^ (key + seed); + + key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); + seed = *buffer32 + seed + (seed << 5) + 3; + + *buffer32++ = ch; + } + } +} + +void __mpqlib_decrypt(void *_data, uint32_t length, uint32_t key, char disable_output_swapping) +{ + char * data = (char *) _data; + uint32_t *buffer32 = (uint32_t *) data; + uint32_t seed = 0xEEEEEEEE; + uint32_t ch; + + assert(crypt_table_initialized); + assert(data); + + // Round to 4 bytes + length = length / 4; + + if (disable_output_swapping) { + while (length-- > 0) { + ch = *buffer32; + + seed += crypt_table[0x400 + (key & 0xFF)]; + ch = ch ^ (key + seed); + + key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); + seed = ch + seed + (seed << 5) + 3; + + *buffer32++ = ch; + } + + } else { + while (length-- > 0) { + ch = *buffer32; + + seed += crypt_table[0x400 + (key & 0xFF)]; + ch = ch ^ (key + seed); + + key = ((~key << 0x15) + 0x11111111) | (key >> 0x0B); + seed = ch + seed + (seed << 5) + 3; + + *buffer32++ = ch; + } + } +} + +uint32_t __mpqlib_hash_cstring(const char *string, uint32_t type) +{ + uint32_t seed1 = 0x7FED7FED; + uint32_t seed2 = 0xEEEEEEEE; + uint32_t shifted_type = (type << 8); + int32_t ch; + + assert(crypt_table_initialized); + assert(string); + + while (*string != 0) { + ch = *string++; + if (ch > 0x60 && ch < 0x7b) + ch -= 0x20; + + seed1 = crypt_table[shifted_type + ch] ^ (seed1 + seed2); + seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3; + } + + return seed1; +} + +uint32_t __mpqlib_hash_data(const char *data, uint32_t length, uint32_t type) +{ + uint32_t seed1 = 0x7FED7FED; + uint32_t seed2 = 0xEEEEEEEE; + uint32_t shifted_type = (type << 8); + int32_t ch; + + assert(crypt_table_initialized); + assert(data); + + while (length > 0) { + ch = *data++; + + seed1 = crypt_table[shifted_type + ch] ^ (seed1 + seed2); + seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3; + length--; + } + + return seed1; +} diff --git a/mpq-crypto.h b/mpq-crypto.h new file mode 100644 index 0000000..bc29509 --- /dev/null +++ b/mpq-crypto.h @@ -0,0 +1,21 @@ +#include "mpqlib-stdint.h" + +#if defined(__cplusplus) +extern "C" { +#endif + + void __mpqlib_init_cryptography(void); + + const uint32_t *__mpqlib_get_cryptography_table(void); + + void __mpqlib_encrypt(void *data, uint32_t length, uint32_t key, + char disable_input_swapping); + void __mpqlib_decrypt(void *data, uint32_t length, uint32_t key, + char disable_output_swapping); + + uint32_t __mpqlib_hash_cstring(const char *string, uint32_t type); + uint32_t __mpqlib_hash_data(const char *data, uint32_t length, uint32_t type); + +#if defined(__cplusplus) +} +#endif diff --git a/mpq-misc.c b/mpq-misc.c index aa2d958..8aacfce 100644 --- a/mpq-misc.c +++ b/mpq-misc.c @@ -1,5 +1,5 @@ #include "mpq-misc.h" -#include "MPQCryptography.h" +#include "mpq-crypto.h" /* * The various string hashing function. -- cgit v1.2.3