From aeb420d9f20f0973d428b5de0bd19f83c684bf54 Mon Sep 17 00:00:00 2001 From: pixel Date: Tue, 29 Jan 2008 10:13:13 +0000 Subject: Smoothing up win32 fixes. --- MPQCryptography.c | 101 +++--------------------------------------------------- MPQCryptography.h | 32 ++--------------- Makefile | 2 +- extract.c | 4 +-- hashtab.c | 5 --- hashtab.h | 6 +--- lookupa.h | 6 +--- mpq-bios.c | 22 ++++++------ mpq-bios.h | 6 +--- mpq-file.h | 6 +--- mpq-misc.h | 6 +--- mpqlib-stdint.h | 14 ++++++++ mpqlib.vcproj | 2 +- recycle.c | 6 +--- stdint.h | 10 ------ 15 files changed, 41 insertions(+), 187 deletions(-) create mode 100644 mpqlib-stdint.h delete mode 100644 stdint.h diff --git a/MPQCryptography.c b/MPQCryptography.c index 6c33e55..e18e279 100644 --- a/MPQCryptography.c +++ b/MPQCryptography.c @@ -9,48 +9,21 @@ /* * 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 - -#ifdef USE_SSL -#include -#include -#include -#include -#include -#endif #include "MPQCryptography.h" -#ifdef WIN32 -#include "stdint.h" -#else -#include "inttypes.h" -#endif - static int crypt_table_initialized = 0; static uint32_t crypt_table[0x500]; /****TODO****/ /* Re-implement various endianess fixes. */ -#ifdef USE_SSL -static void memrev(unsigned char *buf, size_t count) -{ - unsigned char *r; - - for (r = buf + count - 1; buf < r; buf++, r--) { - *buf ^= *r; - *r ^= *buf; - *buf ^= *r; - } -} -#endif - const uint32_t *__mpqlib_get_cryptography_table() { assert(crypt_table_initialized); @@ -82,18 +55,11 @@ void __mpqlib_init_cryptography() } } } - // Load up OpenSSL -#ifdef USE_SSL - OpenSSL_add_all_digests(); - OpenSSL_add_all_algorithms(); - OpenSSL_add_all_ciphers(); - ERR_load_crypto_strings(); -#endif } -void __mpqlib_encrypt(void *_data, uint32_t length, uint32_t key, bool disable_input_swapping) +void __mpqlib_encrypt(void *_data, uint32_t length, uint32_t key, char disable_input_swapping) { - char * data = (char *) _data; + char * data = (char *) _data; uint32_t *buffer32 = (uint32_t *) data; uint32_t seed = 0xEEEEEEEE; uint32_t ch; @@ -128,7 +94,7 @@ void __mpqlib_encrypt(void *_data, uint32_t length, uint32_t key, bool disable_i } } -void __mpqlib_decrypt(void *_data, uint32_t length, uint32_t key, bool disable_output_swapping) +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; @@ -211,62 +177,3 @@ uint32_t __mpqlib_hash_data(const char *data, uint32_t length, uint32_t type) return seed1; } - -void __mpqlib_crc32(const unsigned char *buffer, uint32_t length, uint32_t * crc, uint32_t flags) -{ - uint32_t local_crc = 0; - const uint32_t *crc_table = (uint32_t *) get_crc_table(); - const unsigned char *buffer_end = buffer + length; - - if (crc) - local_crc = *crc; - if (flags & __MPQLIB_CRC_INIT) - local_crc = 0xFFFFFFFF; - - if (flags & __MPQLIB_CRC_UPDATE) { - while (buffer < buffer_end) { - local_crc = ((local_crc >> 8) & 0x00FFFFFF) ^ crc_table[(local_crc ^ *buffer) & 0xFF]; - buffer++; - } - } - - if (flags & __MPQLIB_CRC_FINALIZE) - local_crc = local_crc ^ 0xFFFFFFFF; - if (crc) - *crc = local_crc; -} - -#ifdef USE_SSL -int __mpqlib_verify_weak_signature(RSA * public_key, const unsigned char *signature, const unsigned char *digest) -{ - unsigned char reversed_signature[__MPQLIB_WEAK_SIGNATURE_SIZE]; - - memcpy(reversed_signature, signature + 8, __MPQLIB_WEAK_SIGNATURE_SIZE); - memrev(reversed_signature, __MPQLIB_WEAK_SIGNATURE_SIZE); - - return RSA_verify(NID_md5, digest, MD5_DIGEST_LENGTH, reversed_signature, __MPQLIB_WEAK_SIGNATURE_SIZE, public_key); -} - -int __mpqlib_verify_strong_signature(RSA * public_key, const unsigned char *signature, const unsigned char *digest) -{ - unsigned char reversed_signature[__MPQLIB_STRONG_SIGNATURE_SIZE]; - - memcpy(reversed_signature, signature + 4, __MPQLIB_STRONG_SIGNATURE_SIZE); - memrev(reversed_signature, __MPQLIB_STRONG_SIGNATURE_SIZE); - - unsigned char real_digest[__MPQLIB_STRONG_SIGNATURE_SIZE]; - - memset(real_digest, 0xbb, sizeof(real_digest)); - real_digest[0] = 0x0b; - - uint32_t digest_offset = sizeof(real_digest) - SHA_DIGEST_LENGTH; - - memcpy(real_digest + digest_offset, digest, SHA_DIGEST_LENGTH); - memrev(real_digest + digest_offset, SHA_DIGEST_LENGTH); - - RSA_public_decrypt(__MPQLIB_STRONG_SIGNATURE_SIZE, reversed_signature, reversed_signature, public_key, RSA_NO_PADDING); - unsigned long error = ERR_get_error(); - - return (!error && memcmp(reversed_signature, real_digest, __MPQLIB_STRONG_SIGNATURE_SIZE) == 0); -} -#endif diff --git a/MPQCryptography.h b/MPQCryptography.h index 01aa418..53bac82 100644 --- a/MPQCryptography.h +++ b/MPQCryptography.h @@ -7,26 +7,10 @@ * */ -#ifdef WIN32 -#include "stdint.h" -#include -#define bool char -#else -#include -#include -#include -#endif +#include "mpqlib-stdint.h" #if defined(__cplusplus) extern "C" { -#endif - -#if !defined(__MPQLIB_WEAK_SIGNATURE_SIZE) -#define __MPQLIB_WEAK_SIGNATURE_SIZE 64 -#endif - -#if !defined(__MPQLIB_STRONG_SIGNATURE_SIZE) -#define __MPQLIB_STRONG_SIGNATURE_SIZE 256 #endif void __mpqlib_init_cryptography(void); @@ -34,23 +18,13 @@ extern "C" { const uint32_t *__mpqlib_get_cryptography_table(void); void __mpqlib_encrypt(void *data, uint32_t length, uint32_t key, - bool disable_input_swapping); + char disable_input_swapping); void __mpqlib_decrypt(void *data, uint32_t length, uint32_t key, - bool disable_output_swapping); + 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); -#define __MPQLIB_CRC_INIT 0x1 -#define __MPQLIB_CRC_UPDATE 0x2 -#define __MPQLIB_CRC_FINALIZE 0x4 - void __mpqlib_crc32(const unsigned char *buffer, uint32_t length, uint32_t * crc, uint32_t flags); - -#ifdef USE_SSL - int __mpqlib_verify_weak_signature(RSA * public_key, const unsigned char *signature, const unsigned char *digest); - int __mpqlib_verify_strong_signature(RSA * public_key, const unsigned char *signature, const unsigned char *digest); -#endif - #if defined(__cplusplus) } #endif diff --git a/Makefile b/Makefile index 5971ac3..86355d7 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ LD = gcc AR = ar rcs CPPFLAGS = -g -Wall -Werror -D_FILE_OFFSET_BITS=64 -I. -LDFLAGS = -g +LDFLAGS = -g -lz SOURCE_LIST = \ MPQCryptography.c \ diff --git a/extract.c b/extract.c index 035f2d2..07035b1 100644 --- a/extract.c +++ b/extract.c @@ -1242,7 +1242,7 @@ static void huff_init_tree(struct huffman_tree *ht, struct huffman_tree_item *hi /* * Compression structure (size: 12596 bytes on x86-32) */ -#ifdef WIN32 +#ifdef _MSC_VER #pragma pack(1) #endif typedef struct { @@ -1276,7 +1276,7 @@ typedef struct { unsigned char clen_bits[0x10]; /* 3104 - Number of valid bits for copied block */ unsigned short len_base[0x10]; /* 3114 - Buffer for */ } -#ifndef WIN32 +#ifndef _MSC_VER __attribute__ ((packed)) #endif pkzip_data_cmp; diff --git a/hashtab.c b/hashtab.c index 1ddab62..bfe4a41 100644 --- a/hashtab.c +++ b/hashtab.c @@ -28,11 +28,6 @@ This implements a hash table. -------------------------------------------------------------------- */ -#ifdef WIN32 -#include "stdint.h" -#else -#include -#endif #include #include "lookupa.h" diff --git a/hashtab.h b/hashtab.h index 45ad923..2eaee12 100644 --- a/hashtab.h +++ b/hashtab.h @@ -31,11 +31,7 @@ This implements a hash table. #ifndef HASHTAB #define HASHTAB -#ifdef WIN32 -#include "stdint.h" -#else -#include -#endif +#include "mpqlib-stdint.h" #include /* PRIVATE TYPES AND DEFINITIONS */ diff --git a/lookupa.h b/lookupa.h index 8ac120d..ffb5738 100644 --- a/lookupa.h +++ b/lookupa.h @@ -10,11 +10,7 @@ Source is http://burtleburtle.net/bob/c/lookupa.h #ifndef LOOKUPA #define LOOKUPA -#ifdef WIN32 -#include "stdint.h" -#else -#include -#endif +#include "mpqlib-stdint.h" #define CHECKSTATE 8 #define hashsize(n) ((uint32_t)1<<(n)) diff --git a/mpq-bios.c b/mpq-bios.c index 35173d6..d2c96f7 100644 --- a/mpq-bios.c +++ b/mpq-bios.c @@ -1,10 +1,13 @@ #define _LARGEFILE64_SOURCE -#ifdef WIN32 #include +#ifdef WIN32 #include #include #define lseek64 _lseeki64 +#define O_RDONLY _O_RDONLY +#define O_BINARY _O_BINARY +#define O_LARGEFILE 0 #else #include #endif @@ -25,10 +28,9 @@ /* * MPQ header. */ -#ifdef WIN32 +#ifdef _MSC_VER #pragma pack(1) #endif - typedef struct { /* basic version of the header. */ char magic[4]; @@ -47,7 +49,7 @@ typedef struct { uint16_t hash_table_offset_high; uint16_t block_table_offset_high; } -#ifndef WIN32 +#ifndef _MSC_VER __attribute__ ((packed)) #endif mpq_header_t; @@ -63,7 +65,7 @@ typedef struct { uint16_t platform; uint32_t file_block_index; } -#ifndef WIN32 +#ifndef _MSC_VER __attribute__ ((packed)) #endif mpq_hash_t; @@ -78,7 +80,7 @@ typedef struct { uint32_t file_size; uint32_t flags; } -#ifndef WIN32 +#ifndef _MSC_VER __attribute__ ((packed)) #endif mpq_block_t; @@ -104,7 +106,7 @@ typedef struct { } block_t; struct mpq_archive_t { - int fd; + int fd; int closeit; uint32_t header_size; @@ -131,11 +133,7 @@ struct mpq_archive_t * mpqlib_open_archive(const char * fname) { int fd; struct mpq_archive_t * r; -#ifdef WIN32 - if ((fd = open(fname, _O_RDONLY | _O_BINARY)) == -1) { -#else - if ((fd = open(fname, O_RDONLY | O_LARGEFILE | O_BINARY)) == -1) { -#endif + if ((fd = open(fname, O_RDONLY | O_LARGEFILE | O_BINARY)) == -1) { __mpqlib_errno = MPQLIB_ERROR_OPEN; return NULL; } diff --git a/mpq-bios.h b/mpq-bios.h index 61e2020..c046f53 100644 --- a/mpq-bios.h +++ b/mpq-bios.h @@ -1,11 +1,7 @@ #ifndef __MPQ_BIOS_H__ #define __MPQ_BIOS_H__ -#ifdef WIN32 -#include "stdint.h" -#else -#include -#endif +#include "mpqlib-stdint.h" struct mpq_archive_t; diff --git a/mpq-file.h b/mpq-file.h index 7557758..aea19cd 100644 --- a/mpq-file.h +++ b/mpq-file.h @@ -1,11 +1,7 @@ #ifndef __MPQ_FILE_H__ #define __MPQ_FILE_H__ -#ifdef WIN32 -#include "stdint.h" -#else -#include -#endif +#include "mpqlib-stdint.h" struct mpq_file_t; enum mpqlib_file_seek_t { diff --git a/mpq-misc.h b/mpq-misc.h index 72565de..a08436d 100644 --- a/mpq-misc.h +++ b/mpq-misc.h @@ -1,11 +1,7 @@ #ifndef __MPQ_MISC_H__ #define __MPQ_MISC_H__ -#ifdef WIN32 -typedef unsigned long int uint32_t; -#else -#include -#endif +#include "mpqlib-stdint.h" #ifdef __cplusplus extern "C" { diff --git a/mpqlib-stdint.h b/mpqlib-stdint.h new file mode 100644 index 0000000..1dc4e2d --- /dev/null +++ b/mpqlib-stdint.h @@ -0,0 +1,14 @@ +#ifndef __STD_INT_H_ +#define __STD_INT_H_ + +#ifdef WIN32 +typedef unsigned char uint8_t; +typedef unsigned short int uint16_t; +typedef signed long int int32_t; +typedef unsigned long int uint32_t; +typedef unsigned __int64 uint64_t; +#else +#include +#endif + +#endif diff --git a/mpqlib.vcproj b/mpqlib.vcproj index c6a6ff4..ad7a08b 100644 --- a/mpqlib.vcproj +++ b/mpqlib.vcproj @@ -243,7 +243,7 @@ > diff --git a/recycle.c b/recycle.c index ff823d1..084854b 100644 --- a/recycle.c +++ b/recycle.c @@ -13,11 +13,7 @@ This also decreases memory fragmentation, and freeing structures -------------------------------------------------------------------- */ -#ifdef WIN32 -#include "stdint.h" -#else -#include -#endif +#include "mpqlib-stdint.h" #include #include #include "recycle.h" diff --git a/stdint.h b/stdint.h deleted file mode 100644 index dd9c546..0000000 --- a/stdint.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __STD_INT_H_ -#define __STD_INT_H_ - -typedef unsigned char uint8_t; -typedef unsigned short int uint16_t; -typedef signed long int int32_t; -typedef unsigned long int uint32_t; -typedef unsigned __int64 uint64_t; - -#endif \ No newline at end of file -- cgit v1.2.3