aboutsummaryrefslogtreecommitdiff
path: root/dep/src
diff options
context:
space:
mode:
Diffstat (limited to 'dep/src')
-rw-r--r--dep/src/libmpq/common.c222
-rw-r--r--dep/src/libmpq/explode.c602
-rw-r--r--dep/src/libmpq/extract.c361
-rw-r--r--dep/src/libmpq/huffman.c1101
-rw-r--r--dep/src/libmpq/mpq.c1033
-rw-r--r--dep/src/libmpq/wave.c250
6 files changed, 3569 insertions, 0 deletions
diff --git a/dep/src/libmpq/common.c b/dep/src/libmpq/common.c
new file mode 100644
index 00000000000..91b259f5515
--- /dev/null
+++ b/dep/src/libmpq/common.c
@@ -0,0 +1,222 @@
+/*
+ * common.c -- shared functions used by mpq-tools.
+ *
+ * Copyright (c) 2003-2008 Maik Broemme <mbroemme@plusserver.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* generic includes. */
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#if HAVE_UNISTD_H
+ #include <unistd.h>
+#endif
+
+/* libmpq main includes. */
+#include "mpq.h"
+#include "mpq-internal.h"
+
+/* libmpq generic includes. */
+#include "extract.h"
+
+#include "common.h"
+
+/* the global shared decryption buffer. it's a static array compiled into the
+ * library, and can be re-created by compiling and running crypt_buf_gen.c
+ */
+#include "crypt_buf.h"
+
+/* function to return the hash to a given string. */
+uint32_t libmpq__hash_string(const char *key, uint32_t offset) {
+
+ /* some common variables. */
+ uint32_t seed1 = 0x7FED7FED;
+ uint32_t seed2 = 0xEEEEEEEE;
+
+ /* one key character. */
+ uint32_t ch;
+
+ /* prepare seeds. */
+ while (*key != 0) {
+ ch = toupper(*key++);
+ seed1 = crypt_buf[offset + ch] ^ (seed1 + seed2);
+ seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3;
+ }
+
+ return seed1;
+}
+
+/* function to encrypt a block. */
+int32_t libmpq__encrypt_block(uint32_t *in_buf, uint32_t in_size, uint32_t seed) {
+
+ /* some common variables. */
+ uint32_t seed2 = 0xEEEEEEEE;
+ uint32_t ch;
+
+ /* we're processing the data 4 bytes at a time. */
+ for (; in_size >= 4; in_size -= 4) {
+ seed2 += crypt_buf[0x400 + (seed & 0xFF)];
+ ch = *in_buf ^ (seed + seed2);
+ seed = ((~seed << 0x15) + 0x11111111) | (seed >> 0x0B);
+ seed2 = *in_buf + seed2 + (seed2 << 5) + 3;
+ *in_buf++ = ch;
+ }
+
+ /* if no error was found, return decrypted bytes. */
+ return LIBMPQ_SUCCESS;
+}
+
+
+/* function to decrypt a block. */
+int32_t libmpq__decrypt_block(uint32_t *in_buf, uint32_t in_size, uint32_t seed) {
+
+ /* some common variables. */
+ uint32_t seed2 = 0xEEEEEEEE;
+ uint32_t ch;
+
+ /* we're processing the data 4 bytes at a time. */
+ for (; in_size >= 4; in_size -= 4) {
+ seed2 += crypt_buf[0x400 + (seed & 0xFF)];
+ ch = *in_buf ^ (seed + seed2);
+ seed = ((~seed << 0x15) + 0x11111111) | (seed >> 0x0B);
+ seed2 = ch + seed2 + (seed2 << 5) + 3;
+ *in_buf++ = ch;
+ }
+
+ /* if no error was found, return decrypted bytes. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* function to detect decryption key. */
+int32_t libmpq__decrypt_key(uint8_t *in_buf, uint32_t in_size, uint32_t block_size) {
+
+ /* some common variables. */
+ uint32_t saveseed1;
+
+ /* temp = seed1 + seed2 */
+ uint32_t temp;
+ uint32_t i = 0;
+
+ /* temp = seed1 + buffer[0x400 + (seed1 & 0xFF)] */
+ temp = (*(uint32_t *)in_buf ^ in_size) - 0xEEEEEEEE;
+
+ /* try all 255 possibilities. */
+ for (i = 0; i < 0x100; i++) {
+
+ /* some common variables. */
+ uint32_t seed1;
+ uint32_t seed2 = 0xEEEEEEEE;
+ uint32_t ch;
+ uint32_t ch2;
+
+ /* try the first uint32_t's (we exactly know the value). */
+ seed1 = temp - crypt_buf[0x400 + i];
+ seed2 += crypt_buf[0x400 + (seed1 & 0xFF)];
+ ch = ((uint32_t *)in_buf)[0] ^ (seed1 + seed2);
+
+ if (ch != in_size) {
+ continue;
+ }
+
+ /* add one because we are decrypting block positions. */
+ saveseed1 = seed1 + 1;
+ ch2 = ch;
+
+ /*
+ * if ok, continue and test the second value. we don't know exactly the value,
+ * but we know that the second one has lower 16 bits set to zero (no compressed
+ * block is larger than 0xFFFF bytes)
+ */
+ seed1 = ((~seed1 << 0x15) + 0x11111111) | (seed1 >> 0x0B);
+ seed2 = ch + seed2 + (seed2 << 5) + 3;
+ seed2 += crypt_buf[0x400 + (seed1 & 0xFF)];
+ ch = ((uint32_t *)in_buf)[1] ^ (seed1 + seed2);
+
+ /* check if we found the file seed. */
+ if ((ch - ch2) <= block_size) {
+
+ /* file seed found, so return it. */
+ return saveseed1;
+ }
+ }
+
+ /* if no file seed was found return with error. */
+ return LIBMPQ_ERROR_DECRYPT;
+}
+
+/* function to decompress or explode a block from mpq archive. */
+int32_t libmpq__decompress_block(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size, uint32_t compression_type) {
+
+ /* some common variables. */
+ int32_t tb = 0;
+
+ /* check if buffer is not compressed. */
+ if (compression_type == LIBMPQ_FLAG_COMPRESS_NONE) {
+
+ /* no compressed data, so copy input buffer to output buffer. */
+ memcpy(out_buf, in_buf, out_size);
+
+ /* store number of bytes copied. */
+ tb = out_size;
+ }
+
+ /* check if one compression mode is used. */
+ else if (compression_type == LIBMPQ_FLAG_COMPRESS_PKZIP ||
+ compression_type == LIBMPQ_FLAG_COMPRESS_MULTI) {
+
+ /* check if block is really compressed, some blocks have set the compression flag, but are not compressed. */
+ if (in_size < out_size) {
+
+ /* check if we are using pkzip compression algorithm. */
+ if (compression_type == LIBMPQ_FLAG_COMPRESS_PKZIP) {
+
+ /* decompress using pkzip. */
+ if ((tb = libmpq__decompress_pkzip(in_buf, in_size, out_buf, out_size)) < 0) {
+
+ /* something on decompression failed. */
+ return tb;
+ }
+ }
+
+ /* check if we are using multiple compression algorithm. */
+ else if (compression_type == LIBMPQ_FLAG_COMPRESS_MULTI) {
+
+ /*
+ * check if it is a file compressed by blizzard's multiple compression, note that storm.dll
+ * version 1.0.9 distributed with warcraft 3 passes the full path name of the opened archive
+ * as the new last parameter.
+ */
+ if ((tb = libmpq__decompress_multi(in_buf, in_size, out_buf, out_size)) < 0) {
+
+ /* something on decompression failed. */
+ return tb;
+ }
+ }
+ } else {
+
+ /* block has set compression flag, but is not compressed, so copy data to output buffer. */
+ memcpy(out_buf, in_buf, out_size);
+
+ /* save the number of transferred bytes. */
+ tb = in_size;
+ }
+ }
+
+ /* if no error was found, return transferred bytes. */
+ return tb;
+}
diff --git a/dep/src/libmpq/explode.c b/dep/src/libmpq/explode.c
new file mode 100644
index 00000000000..2d778d25c39
--- /dev/null
+++ b/dep/src/libmpq/explode.c
@@ -0,0 +1,602 @@
+/*
+ * explode.c -- explode function of pkware data compression library.
+ *
+ * Copyright (c) 2003-2008 Maik Broemme <mbroemme@plusserver.de>
+ *
+ * This source was adepted from the C++ version of pkware.cpp included
+ * in stormlib. The C++ version belongs to the following authors:
+ *
+ * Ladislav Zezula <ladik@zezula.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* generic includes. */
+#include <string.h>
+
+/* libmpq main includes. */
+#include "mpq.h"
+
+/* libmpq generic includes. */
+#include "explode.h"
+
+/* tables used for data extraction. */
+static const uint8_t pkzip_dist_bits[] = {
+ 0x02, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
+ 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+ 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
+ 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
+};
+
+/* tables used for data extraction. */
+static const uint8_t pkzip_dist_code[] = {
+ 0x03, 0x0D, 0x05, 0x19, 0x09, 0x11, 0x01, 0x3E, 0x1E, 0x2E, 0x0E, 0x36, 0x16, 0x26, 0x06, 0x3A,
+ 0x1A, 0x2A, 0x0A, 0x32, 0x12, 0x22, 0x42, 0x02, 0x7C, 0x3C, 0x5C, 0x1C, 0x6C, 0x2C, 0x4C, 0x0C,
+ 0x74, 0x34, 0x54, 0x14, 0x64, 0x24, 0x44, 0x04, 0x78, 0x38, 0x58, 0x18, 0x68, 0x28, 0x48, 0x08,
+ 0xF0, 0x70, 0xB0, 0x30, 0xD0, 0x50, 0x90, 0x10, 0xE0, 0x60, 0xA0, 0x20, 0xC0, 0x40, 0x80, 0x00
+};
+
+/* tables used for data extraction. */
+static const uint8_t pkzip_clen_bits[] = {
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
+};
+
+/* tables used for data extraction. */
+static const uint16_t pkzip_len_base[] = {
+ 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
+ 0x0008, 0x000A, 0x000E, 0x0016, 0x0026, 0x0046, 0x0086, 0x0106
+};
+
+/* tables used for data extraction. */
+static const uint8_t pkzip_slen_bits[] = {
+ 0x03, 0x02, 0x03, 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x07, 0x07
+};
+
+/* tables used for data extraction. */
+static const uint8_t pkzip_len_code[] = {
+ 0x05, 0x03, 0x01, 0x06, 0x0A, 0x02, 0x0C, 0x14, 0x04, 0x18, 0x08, 0x30, 0x10, 0x20, 0x40, 0x00
+};
+
+/* tables used for data extraction. */
+static const uint8_t pkzip_bits_asc[] = {
+ 0x0B, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x08, 0x07, 0x0C, 0x0C, 0x07, 0x0C, 0x0C,
+ 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
+ 0x04, 0x0A, 0x08, 0x0C, 0x0A, 0x0C, 0x0A, 0x08, 0x07, 0x07, 0x08, 0x09, 0x07, 0x06, 0x07, 0x08,
+ 0x07, 0x06, 0x07, 0x07, 0x07, 0x07, 0x08, 0x07, 0x07, 0x08, 0x08, 0x0C, 0x0B, 0x07, 0x09, 0x0B,
+ 0x0C, 0x06, 0x07, 0x06, 0x06, 0x05, 0x07, 0x08, 0x08, 0x06, 0x0B, 0x09, 0x06, 0x07, 0x06, 0x06,
+ 0x07, 0x0B, 0x06, 0x06, 0x06, 0x07, 0x09, 0x08, 0x09, 0x09, 0x0B, 0x08, 0x0B, 0x09, 0x0C, 0x08,
+ 0x0C, 0x05, 0x06, 0x06, 0x06, 0x05, 0x06, 0x06, 0x06, 0x05, 0x0B, 0x07, 0x05, 0x06, 0x05, 0x05,
+ 0x06, 0x0A, 0x05, 0x05, 0x05, 0x05, 0x08, 0x07, 0x08, 0x08, 0x0A, 0x0B, 0x0B, 0x0C, 0x0C, 0x0C,
+ 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D,
+ 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D,
+ 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D,
+ 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
+ 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
+ 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
+ 0x0D, 0x0C, 0x0D, 0x0D, 0x0D, 0x0C, 0x0D, 0x0D, 0x0D, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, 0x0C, 0x0D,
+ 0x0D, 0x0D, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D
+};
+
+/* tables used for data extraction. */
+static const uint16_t pkzip_code_asc[] = {
+ 0x0490, 0x0FE0, 0x07E0, 0x0BE0, 0x03E0, 0x0DE0, 0x05E0, 0x09E0,
+ 0x01E0, 0x00B8, 0x0062, 0x0EE0, 0x06E0, 0x0022, 0x0AE0, 0x02E0,
+ 0x0CE0, 0x04E0, 0x08E0, 0x00E0, 0x0F60, 0x0760, 0x0B60, 0x0360,
+ 0x0D60, 0x0560, 0x1240, 0x0960, 0x0160, 0x0E60, 0x0660, 0x0A60,
+ 0x000F, 0x0250, 0x0038, 0x0260, 0x0050, 0x0C60, 0x0390, 0x00D8,
+ 0x0042, 0x0002, 0x0058, 0x01B0, 0x007C, 0x0029, 0x003C, 0x0098,
+ 0x005C, 0x0009, 0x001C, 0x006C, 0x002C, 0x004C, 0x0018, 0x000C,
+ 0x0074, 0x00E8, 0x0068, 0x0460, 0x0090, 0x0034, 0x00B0, 0x0710,
+ 0x0860, 0x0031, 0x0054, 0x0011, 0x0021, 0x0017, 0x0014, 0x00A8,
+ 0x0028, 0x0001, 0x0310, 0x0130, 0x003E, 0x0064, 0x001E, 0x002E,
+ 0x0024, 0x0510, 0x000E, 0x0036, 0x0016, 0x0044, 0x0030, 0x00C8,
+ 0x01D0, 0x00D0, 0x0110, 0x0048, 0x0610, 0x0150, 0x0060, 0x0088,
+ 0x0FA0, 0x0007, 0x0026, 0x0006, 0x003A, 0x001B, 0x001A, 0x002A,
+ 0x000A, 0x000B, 0x0210, 0x0004, 0x0013, 0x0032, 0x0003, 0x001D,
+ 0x0012, 0x0190, 0x000D, 0x0015, 0x0005, 0x0019, 0x0008, 0x0078,
+ 0x00F0, 0x0070, 0x0290, 0x0410, 0x0010, 0x07A0, 0x0BA0, 0x03A0,
+ 0x0240, 0x1C40, 0x0C40, 0x1440, 0x0440, 0x1840, 0x0840, 0x1040,
+ 0x0040, 0x1F80, 0x0F80, 0x1780, 0x0780, 0x1B80, 0x0B80, 0x1380,
+ 0x0380, 0x1D80, 0x0D80, 0x1580, 0x0580, 0x1980, 0x0980, 0x1180,
+ 0x0180, 0x1E80, 0x0E80, 0x1680, 0x0680, 0x1A80, 0x0A80, 0x1280,
+ 0x0280, 0x1C80, 0x0C80, 0x1480, 0x0480, 0x1880, 0x0880, 0x1080,
+ 0x0080, 0x1F00, 0x0F00, 0x1700, 0x0700, 0x1B00, 0x0B00, 0x1300,
+ 0x0DA0, 0x05A0, 0x09A0, 0x01A0, 0x0EA0, 0x06A0, 0x0AA0, 0x02A0,
+ 0x0CA0, 0x04A0, 0x08A0, 0x00A0, 0x0F20, 0x0720, 0x0B20, 0x0320,
+ 0x0D20, 0x0520, 0x0920, 0x0120, 0x0E20, 0x0620, 0x0A20, 0x0220,
+ 0x0C20, 0x0420, 0x0820, 0x0020, 0x0FC0, 0x07C0, 0x0BC0, 0x03C0,
+ 0x0DC0, 0x05C0, 0x09C0, 0x01C0, 0x0EC0, 0x06C0, 0x0AC0, 0x02C0,
+ 0x0CC0, 0x04C0, 0x08C0, 0x00C0, 0x0F40, 0x0740, 0x0B40, 0x0340,
+ 0x0300, 0x0D40, 0x1D00, 0x0D00, 0x1500, 0x0540, 0x0500, 0x1900,
+ 0x0900, 0x0940, 0x1100, 0x0100, 0x1E00, 0x0E00, 0x0140, 0x1600,
+ 0x0600, 0x1A00, 0x0E40, 0x0640, 0x0A40, 0x0A00, 0x1200, 0x0200,
+ 0x1C00, 0x0C00, 0x1400, 0x0400, 0x1800, 0x0800, 0x1000, 0x0000
+};
+
+/* local unused variables. */
+char pkware_copyright[] = "PKWARE Data Compression Library for Win32\r\n"
+ "Copyright 1989-1995 PKWARE Inc. All Rights Reserved\r\n"
+ "Patent No. 5,051,745\r\n"
+ "PKWARE Data Compression Library Reg. U.S. Pat. and Tm. Off.\r\n"
+ "Version 1.11\r\n";
+
+/* skips given number of bits. */
+static int32_t skip_bit(pkzip_cmp_s *mpq_pkzip, uint32_t bits) {
+
+ /* check if number of bits required is less than number of bits in the buffer. */
+ if (bits <= mpq_pkzip->extra_bits) {
+ mpq_pkzip->extra_bits -= bits;
+ mpq_pkzip->bit_buf >>= bits;
+ return 0;
+ }
+
+ /* load input buffer if necessary. */
+ mpq_pkzip->bit_buf >>= mpq_pkzip->extra_bits;
+ if (mpq_pkzip->in_pos == mpq_pkzip->in_bytes) {
+ mpq_pkzip->in_pos = sizeof(mpq_pkzip->in_buf);
+ if ((mpq_pkzip->in_bytes = mpq_pkzip->read_buf((char *)mpq_pkzip->in_buf, &mpq_pkzip->in_pos, mpq_pkzip->param)) == 0) {
+ return 1;
+ }
+ mpq_pkzip->in_pos = 0;
+ }
+
+ /* update bit buffer. */
+ mpq_pkzip->bit_buf |= (mpq_pkzip->in_buf[mpq_pkzip->in_pos++] << 8);
+ mpq_pkzip->bit_buf >>= (bits - mpq_pkzip->extra_bits);
+ mpq_pkzip->extra_bits = (mpq_pkzip->extra_bits - bits) + 8;
+
+ /* if no error was found, return zero. */
+ return 0;
+}
+
+/* this function generate the decode tables used for decryption. */
+static void generate_tables_decode(int32_t count, uint8_t *bits, const uint8_t *code, uint8_t *buf2) {
+
+ /* some common variables. */
+ int32_t i;
+
+ /* EBX - count */
+ for (i = count-1; i >= 0; i--) {
+
+ /* some common variables. */
+ uint32_t idx1 = code[i];
+ uint32_t idx2 = 1 << bits[i];
+
+ /* loop until table is ready. */
+ do {
+ buf2[idx1] = (uint8_t)i;
+ idx1 += idx2;
+ } while (idx1 < 0x100);
+ }
+}
+
+/* this function generate the tables for ascii decompression. */
+static void generate_tables_ascii(pkzip_cmp_s *mpq_pkzip) {
+
+ /* some common variables. */
+ const uint16_t *code_asc = &pkzip_code_asc[0xFF];
+ uint32_t acc;
+ uint32_t add;
+ uint16_t count;
+
+ /* loop through ascii table. */
+ for (count = 0x00FF; code_asc >= pkzip_code_asc; code_asc--, count--) {
+ uint8_t *bits_asc = mpq_pkzip->bits_asc + count;
+ uint8_t bits_tmp = *bits_asc;
+
+ /* check if byte is finished. */
+ if (bits_tmp <= 8) {
+ add = (1 << bits_tmp);
+ acc = *code_asc;
+ do {
+ mpq_pkzip->offs_2c34[acc] = (uint8_t)count;
+ acc += add;
+ } while (acc < 0x100);
+ } else {
+ if ((acc = (*code_asc & 0xFF)) != 0) {
+ mpq_pkzip->offs_2c34[acc] = 0xFF;
+ if (*code_asc & 0x3F) {
+
+ /* decrease bit by four. */
+ bits_tmp -= 4;
+ *bits_asc = bits_tmp;
+ add = (1 << bits_tmp);
+ acc = *code_asc >> 4;
+ do {
+ mpq_pkzip->offs_2d34[acc] = (uint8_t)count;
+ acc += add;
+ } while (acc < 0x100);
+ } else {
+
+ /* decrease bit by six. */
+ bits_tmp -= 6;
+ *bits_asc = bits_tmp;
+ add = (1 << bits_tmp);
+ acc = *code_asc >> 6;
+ do {
+ mpq_pkzip->offs_2e34[acc] = (uint8_t)count;
+ acc += add;
+ } while (acc < 0x80);
+ }
+ } else {
+
+ /* decrease bit by eight. (one byte) */
+ bits_tmp -= 8;
+ *bits_asc = bits_tmp;
+ add = (1 << bits_tmp);
+ acc = *code_asc >> 8;
+ do {
+ mpq_pkzip->offs_2eb4[acc] = (uint8_t)count;
+ acc += add;
+ } while (acc < 0x100);
+ }
+ }
+ }
+}
+
+/*
+ * decompress the imploded data using coded literals.
+ *
+ * returns: 0x000 - 0x0FF : one byte from compressed file.
+ * 0x100 - 0x305 : copy previous block. (0x100 = 1 byte)
+ * 0x306 : out of buffer?
+ */
+static uint32_t decode_literal(pkzip_cmp_s *mpq_pkzip) {
+
+ /* number of bits to skip. */
+ uint32_t bits;
+
+ /* position in buffers. */
+ uint32_t value;
+
+ /* check if bit the current buffer is set, if not return the next byte. */
+ if (mpq_pkzip->bit_buf & 1) {
+
+ /* skip current bit in the buffer. */
+ if (skip_bit(mpq_pkzip, 1)) {
+ return 0x306;
+ }
+
+ /* the next bits are position in buffers. */
+ value = mpq_pkzip->pos2[(mpq_pkzip->bit_buf & 0xFF)];
+
+ /* get number of bits to skip. */
+ if (skip_bit(mpq_pkzip, mpq_pkzip->slen_bits[value])) {
+ return 0x306;
+ }
+
+ /* check bits. */
+ if ((bits = mpq_pkzip->clen_bits[value]) != 0) {
+
+ /* some common variables. */
+ uint32_t val2 = mpq_pkzip->bit_buf & ((1 << bits) - 1);
+
+ /* check if we should skip one bit. */
+ if (skip_bit(mpq_pkzip, bits)) {
+
+ /* check position if we should skip the bit. */
+ if ((value + val2) != 0x10E) {
+ return 0x306;
+ }
+ }
+
+ /* fill values. */
+ value = mpq_pkzip->len_base[value] + val2;
+ }
+
+ /* return number of bytes to repeat. */
+ return value + 0x100;
+ }
+
+ /* skip one bit. */
+ if (skip_bit(mpq_pkzip, 1)) {
+ return 0x306;
+ }
+
+ /* check the binary compression type, read 8 bits and return them as one byte. */
+ if (mpq_pkzip->cmp_type == LIBMPQ_PKZIP_CMP_BINARY) {
+
+ /* fill values. */
+ value = mpq_pkzip->bit_buf & 0xFF;
+
+ /* check if we should skip one bit. */
+ if (skip_bit(mpq_pkzip, 8)) {
+ return 0x306;
+ }
+
+ /* return value from bit buffer. */
+ return value;
+ }
+
+ /* check if ascii compression is used. */
+ if (mpq_pkzip->bit_buf & 0xFF) {
+
+ /* fill values. */
+ value = mpq_pkzip->offs_2c34[mpq_pkzip->bit_buf & 0xFF];
+
+ /* check value. */
+ if (value == 0xFF) {
+ if (mpq_pkzip->bit_buf & 0x3F) {
+
+ /* check if four bits are in bit buffer for skipping. */
+ if (skip_bit(mpq_pkzip, 4)) {
+ return 0x306;
+ }
+
+ /* fill values. */
+ value = mpq_pkzip->offs_2d34[mpq_pkzip->bit_buf & 0xFF];
+ } else {
+
+ /* check if six bits are in bit buffer for skipping. */
+ if (skip_bit(mpq_pkzip, 6)) {
+ return 0x306;
+ }
+
+ /* fill values. */
+ value = mpq_pkzip->offs_2e34[mpq_pkzip->bit_buf & 0x7F];
+ }
+ }
+ } else {
+
+ /* check if eight bits are in bit buffer for skipping. */
+ if (skip_bit(mpq_pkzip, 8)) {
+ return 0x306;
+ }
+
+ /* fill values. */
+ value = mpq_pkzip->offs_2eb4[mpq_pkzip->bit_buf & 0xFF];
+ }
+
+ /* return out of buffer error (0x306) or position in buffer. */
+ return skip_bit(mpq_pkzip, mpq_pkzip->bits_asc[value]) ? 0x306 : value;
+}
+
+/* this function retrieves the number of bytes to move back. */
+static uint32_t decode_distance(pkzip_cmp_s *mpq_pkzip, uint32_t length) {
+
+ /* some common variables. */
+ uint32_t pos = mpq_pkzip->pos1[(mpq_pkzip->bit_buf & 0xFF)];
+
+ /* number of bits to skip. */
+ uint32_t skip = mpq_pkzip->dist_bits[pos];
+
+ /* skip the appropriate number of bits. */
+ if (skip_bit(mpq_pkzip, skip) == 1) {
+ return 0;
+ }
+
+ /* check if length is two. */
+ if (length == 2) {
+ pos = (pos << 2) | (mpq_pkzip->bit_buf & 0x03);
+
+ /* skip the bits. */
+ if (skip_bit(mpq_pkzip, 2) == 1) {
+ return 0;
+ }
+ } else {
+ pos = (pos << mpq_pkzip->dsize_bits) | (mpq_pkzip->bit_buf & mpq_pkzip->dsize_mask);
+
+ /* skip the bits */
+ if (skip_bit(mpq_pkzip, mpq_pkzip->dsize_bits) == 1) {
+ return 0;
+ }
+ }
+
+ /* return the bytes to move back. */
+ return pos + 1;
+}
+
+/*
+ * function loads data from the input buffer used by mpq_pkzip
+ * "implode" and "explode" function as user defined callback and
+ * returns number of bytes loaded.
+ *
+ * char *buf - pointer to a buffer where to store loaded data.
+ * uint32_t *size - maximum number of bytes to read.
+ * void *param - custom pointer, parameter of implode/explode.
+ */
+static uint32_t data_read_input(char *buf, uint32_t *size, void *param) {
+
+ /* some common variables. */
+ pkzip_data_s *info = (pkzip_data_s *)param;
+ uint32_t max_avail = (info->in_bytes - info->in_pos);
+ uint32_t to_read = *size;
+
+ /* check the case when not enough data available. */
+ if (to_read > max_avail) {
+ to_read = max_avail;
+ }
+
+ /* load data and increment offsets. */
+ memcpy(buf, info->in_buf + info->in_pos, to_read);
+ info->in_pos += to_read;
+
+ /* return bytes read. */
+ return to_read;
+}
+
+/*
+ * function for store output data used by mpq_pkzip "implode" and
+ * "explode" as userdefined callback.
+ *
+ * char *buf - pointer to data to be written.
+ * uint32_t *size - number of bytes to write.
+ * void *param - custom pointer, parameter of implode/explode.
+ */
+static void data_write_output(char *buf, uint32_t *size, void *param) {
+
+ /* some common variables. */
+ pkzip_data_s *info = (pkzip_data_s *)param;
+ uint32_t max_write = (info->max_out - info->out_pos);
+ uint32_t to_write = *size;
+
+ /* check the case when not enough space in the output buffer. */
+ if (to_write > max_write) {
+ to_write = max_write;
+ }
+
+ /* write output data and increments offsets. */
+ memcpy(info->out_buf + info->out_pos, buf, to_write);
+ info->out_pos += to_write;
+}
+
+/* this function extract the data from input stream. */
+static uint32_t expand(pkzip_cmp_s *mpq_pkzip) {
+
+ /* number of bytes to copy. */
+ uint32_t copy_bytes;
+
+ /* one byte from compressed file. */
+ uint32_t one_byte;
+
+ /* some common variables. */
+ uint32_t result;
+
+ /* initialize output buffer position. */
+ mpq_pkzip->out_pos = 0x1000;
+
+ /* check if end of data or error, so terminate decompress. */
+ while ((result = one_byte = decode_literal(mpq_pkzip)) < 0x305) {
+
+ /* check if one byte is greater than 0x100, which means 'repeat n - 0xFE bytes'. */
+ if (one_byte >= 0x100) {
+
+ /* ECX */
+ uint8_t *source;
+
+ /* EDX */
+ uint8_t *target;
+
+ /* some common variables. */
+ uint32_t copy_length = one_byte - 0xFE;
+ uint32_t move_back;
+
+ /* get length of data to copy. */
+ if ((move_back = decode_distance(mpq_pkzip, copy_length)) == 0) {
+ result = 0x306;
+ break;
+ }
+
+ /* target and source pointer. */
+ target = &mpq_pkzip->out_buf[mpq_pkzip->out_pos];
+ source = target - move_back;
+ mpq_pkzip->out_pos += copy_length;
+
+ /* copy until nothing left. */
+ while (copy_length-- > 0) {
+ *target++ = *source++;
+ }
+ } else {
+
+ /* byte is 0x100 great, so add one byte. */
+ mpq_pkzip->out_buf[mpq_pkzip->out_pos++] = (uint8_t)one_byte;
+ }
+
+ /* check if number of extracted bytes has reached 1/2 of output buffer, so flush output buffer. */
+ if (mpq_pkzip->out_pos >= 0x2000) {
+
+ /* copy decompressed data into user buffer. */
+ copy_bytes = 0x1000;
+ mpq_pkzip->write_buf((char *)&mpq_pkzip->out_buf[0x1000], &copy_bytes, mpq_pkzip->param);
+
+ /* check if there are some data left, keep them alive. */
+ memcpy(mpq_pkzip->out_buf, &mpq_pkzip->out_buf[0x1000], mpq_pkzip->out_pos - 0x1000);
+ mpq_pkzip->out_pos -= 0x1000;
+ }
+ }
+
+ /* copy the rest. */
+ copy_bytes = mpq_pkzip->out_pos - 0x1000;
+ mpq_pkzip->write_buf((char *)&mpq_pkzip->out_buf[0x1000], &copy_bytes, mpq_pkzip->param);
+
+ /* return copied bytes. */
+ return result;
+}
+
+/* this function explode the data stream. */
+uint32_t libmpq__do_decompress_pkzip(uint8_t *work_buf, void *param) {
+
+ /* some common variables. */
+ pkzip_cmp_s *mpq_pkzip = (pkzip_cmp_s *)work_buf;
+
+ /* set the whole work buffer to zeros. */
+ memset(mpq_pkzip, 0, sizeof(pkzip_cmp_s));
+
+ /* initialize work struct and load compressed data. */
+ mpq_pkzip->read_buf = data_read_input;
+ mpq_pkzip->write_buf = data_write_output;
+ mpq_pkzip->param = param;
+ mpq_pkzip->in_pos = sizeof(mpq_pkzip->in_buf);
+ mpq_pkzip->in_bytes = mpq_pkzip->read_buf((char *)mpq_pkzip->in_buf, &mpq_pkzip->in_pos, mpq_pkzip->param);
+
+ /* check if we have pkzip data. */
+ if (mpq_pkzip->in_bytes <= 4) {
+ return LIBMPQ_PKZIP_CMP_BAD_DATA;
+ }
+
+ /* get the compression type. */
+ mpq_pkzip->cmp_type = mpq_pkzip->in_buf[0];
+
+ /* get the dictionary size. */
+ mpq_pkzip->dsize_bits = mpq_pkzip->in_buf[1];
+
+ /* initialize 16-bit bit buffer. */
+ mpq_pkzip->bit_buf = mpq_pkzip->in_buf[2];
+
+ /* extra (over 8) bits. */
+ mpq_pkzip->extra_bits = 0;
+
+ /* position in input buffer. */
+ mpq_pkzip->in_pos = 3;
+
+ /* check if valid dictionary size. */
+ if (4 > mpq_pkzip->dsize_bits || mpq_pkzip->dsize_bits > 6) {
+ return LIBMPQ_PKZIP_CMP_INV_DICTSIZE;
+ }
+
+ /* shifted by 'sar' instruction. */
+ mpq_pkzip->dsize_mask = 0xFFFF >> (0x10 - mpq_pkzip->dsize_bits);
+
+ /* check if we are using binary compression. */
+ if (mpq_pkzip->cmp_type != LIBMPQ_PKZIP_CMP_BINARY) {
+
+ /* check if we are using ascii compression. */
+ if (mpq_pkzip->cmp_type != LIBMPQ_PKZIP_CMP_ASCII) {
+ return LIBMPQ_PKZIP_CMP_INV_MODE;
+ }
+
+ /* create ascii buffer. */
+ memcpy(mpq_pkzip->bits_asc, pkzip_bits_asc, sizeof(mpq_pkzip->bits_asc));
+ generate_tables_ascii(mpq_pkzip);
+ }
+
+ /* create the tables for decode. */
+ memcpy(mpq_pkzip->slen_bits, pkzip_slen_bits, sizeof(mpq_pkzip->slen_bits));
+ generate_tables_decode(0x10, mpq_pkzip->slen_bits, pkzip_len_code, mpq_pkzip->pos2);
+
+ /* create the tables for decode. */
+ memcpy(mpq_pkzip->clen_bits, pkzip_clen_bits, sizeof(mpq_pkzip->clen_bits));
+ memcpy(mpq_pkzip->len_base, pkzip_len_base, sizeof(mpq_pkzip->len_base));
+ memcpy(mpq_pkzip->dist_bits, pkzip_dist_bits, sizeof(mpq_pkzip->dist_bits));
+ generate_tables_decode(0x40, mpq_pkzip->dist_bits, pkzip_dist_code, mpq_pkzip->pos1);
+
+ /* check if data extraction works. */
+ if (expand(mpq_pkzip) != 0x306) {
+ return LIBMPQ_PKZIP_CMP_NO_ERROR;
+ }
+
+ /* something failed, so return error. */
+ return LIBMPQ_PKZIP_CMP_ABORT;
+}
diff --git a/dep/src/libmpq/extract.c b/dep/src/libmpq/extract.c
new file mode 100644
index 00000000000..11de1071683
--- /dev/null
+++ b/dep/src/libmpq/extract.c
@@ -0,0 +1,361 @@
+/*
+ * extract.c -- global extracting function for all known file compressions
+ * in a mpq archive.
+ *
+ * Copyright (c) 2003-2008 Maik Broemme <mbroemme@plusserver.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* generic includes. */
+#include <stdlib.h>
+#include <string.h>
+
+/* zlib includes. */
+#include <zlib.h>
+#include <bzlib.h>
+
+/* libmpq main includes. */
+#include "mpq.h"
+
+/* libmpq generic includes. */
+#include "explode.h"
+#include "extract.h"
+#include "huffman.h"
+#include "wave.h"
+
+/* table with decompression bits and functions. */
+static decompress_table_s dcmp_table[] = {
+ {LIBMPQ_COMPRESSION_HUFFMAN, libmpq__decompress_huffman}, /* decompression using huffman trees. */
+ {LIBMPQ_COMPRESSION_ZLIB, libmpq__decompress_zlib}, /* decompression with the zlib library. */
+ {LIBMPQ_COMPRESSION_PKZIP, libmpq__decompress_pkzip}, /* decompression with pkware data compression library. */
+ {LIBMPQ_COMPRESSION_BZIP2, libmpq__decompress_bzip2}, /* decompression with bzip2 library. */
+ {LIBMPQ_COMPRESSION_WAVE_MONO, libmpq__decompress_wave_mono}, /* decompression for mono waves. */
+ {LIBMPQ_COMPRESSION_WAVE_STEREO, libmpq__decompress_wave_stereo} /* decompression for stereo waves. */
+};
+
+/* this function decompress a stream using huffman algorithm. */
+int32_t libmpq__decompress_huffman(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) {
+
+ /* TODO: make typdefs of this structs? */
+ /* some common variables. */
+ int32_t tb = 0;
+ struct huffman_tree_s *ht;
+ struct huffman_input_stream_s *is;
+
+ /* allocate memory for the huffman tree. */
+ if ((ht = malloc(sizeof(struct huffman_tree_s))) == NULL ||
+ (is = malloc(sizeof(struct huffman_input_stream_s))) == NULL) {
+
+ /* memory allocation problem. */
+ return LIBMPQ_ERROR_MALLOC;
+ }
+
+ /* cleanup structures. */
+ memset(ht, 0, sizeof(struct huffman_tree_s));
+ memset(is, 0, sizeof(struct huffman_input_stream_s));
+
+ /* initialize input stream. */
+ is->bit_buf = *(uint32_t *)in_buf;
+ in_buf += sizeof(int32_t);
+ is->in_buf = (uint8_t *)in_buf;
+ is->bits = 32;
+
+// TODO: add all the mallocs to init function and add function libmpq__huffman_tree_free() */
+// if ((result = libmpq__huffman_tree_init(ht, LIBMPQ_HUFF_DECOMPRESS)) < 0) {
+//
+// /* something on zlib initialization failed. */
+// return LIBMPQ_ERROR_UNPACK;
+// }
+
+ /* initialize the huffman tree for decompression. */
+ libmpq__huffman_tree_init(ht, LIBMPQ_HUFF_DECOMPRESS);
+
+ /* save the number of copied bytes. */
+ tb = libmpq__do_decompress_huffman(ht, is, out_buf, out_size);
+
+ /* free structures. */
+ free(is);
+ free(ht);
+
+ /* return transferred bytes. */
+ return tb;
+}
+
+/* this function decompress a stream using zlib algorithm. */
+int32_t libmpq__decompress_zlib(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) {
+
+ /* some common variables. */
+ int32_t result = 0;
+ int32_t tb = 0;
+ z_stream z;
+
+ /* fill the stream structure for zlib. */
+ z.next_in = (Bytef *)in_buf;
+ z.avail_in = (uInt)in_size;
+ z.total_in = in_size;
+ z.next_out = (Bytef *)out_buf;
+ z.avail_out = (uInt)out_size;
+ z.total_out = 0;
+ z.zalloc = NULL;
+ z.zfree = NULL;
+
+ /* initialize the decompression structure, storm.dll uses zlib version 1.1.3. */
+ if ((result = inflateInit(&z)) != Z_OK) {
+
+ /* something on zlib initialization failed. */
+ return result;
+ }
+
+ /* call zlib to decompress the data. */
+ if ((result = inflate(&z, Z_FINISH)) != Z_STREAM_END) {
+
+ /* something on zlib decompression failed. */
+ return result;
+ }
+
+ /* save transferred bytes. */
+ tb = z.total_out;
+
+ /* cleanup zlib. */
+ if ((result = inflateEnd(&z)) != Z_OK) {
+
+ /* something on zlib finalization failed. */
+ return result;
+ }
+
+ /* return transferred bytes. */
+ return tb;
+}
+
+/* this function decompress a stream using pkzip algorithm. */
+int32_t libmpq__decompress_pkzip(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) {
+
+ /* some common variables. */
+ int32_t tb = 0;
+ uint8_t *work_buf;
+ pkzip_data_s info;
+
+ /* allocate memory for pkzip data structure. */
+ if ((work_buf = malloc(sizeof(pkzip_cmp_s))) == NULL) {
+
+ /* memory allocation problem. */
+ return LIBMPQ_ERROR_MALLOC;
+ }
+
+ /* cleanup. */
+ memset(work_buf, 0, sizeof(pkzip_cmp_s));
+
+ /* fill data information structure. */
+ info.in_buf = in_buf;
+ info.in_pos = 0;
+ info.in_bytes = in_size;
+ info.out_buf = out_buf;
+ info.out_pos = 0;
+ info.max_out = out_size;
+
+ /* do the decompression. */
+ if ((tb = libmpq__do_decompress_pkzip(work_buf, &info)) < 0) {
+
+ /* free working buffer. */
+ free(work_buf);
+
+ /* something failed on pkzip decompression. */
+ return tb;
+ }
+
+ /* save transferred bytes. */
+ tb = info.out_pos;
+
+ /* free working buffer. */
+ free(work_buf);
+
+ /* return transferred bytes. */
+ return tb;
+}
+
+/* this function decompress a stream using bzip2 library. */
+int32_t libmpq__decompress_bzip2(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) {
+
+ /* some common variables. */
+ int32_t result = 0;
+ int32_t tb = 0;
+ bz_stream strm;
+
+ /* initialize the bzlib decompression. */
+ strm.bzalloc = NULL;
+ strm.bzfree = NULL;
+
+ /* initialize the structure. */
+ if ((result = BZ2_bzDecompressInit(&strm, 0, 0)) != BZ_OK) {
+
+ /* something on bzlib initialization failed. */
+ return result;
+ }
+
+ /* fill the stream structure for bzlib. */
+ strm.next_in = (char *)in_buf;
+ strm.avail_in = in_size;
+ strm.next_out = (char *)out_buf;
+ strm.avail_out = out_size;
+
+ /* do the decompression. */
+ while (BZ2_bzDecompress(&strm) != BZ_STREAM_END);
+
+ /* save transferred bytes. */
+ tb = strm.total_out_lo32;
+
+ /* cleanup of bzip stream. */
+ BZ2_bzDecompressEnd(&strm);
+
+ /* return transferred bytes. */
+ return tb;
+}
+
+/* this function decompress a stream using wave algorithm. (1 channel) */
+int32_t libmpq__decompress_wave_mono(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) {
+
+ /* some common variables. */
+ int32_t tb = 0;
+
+ /* save the number of copied bytes. */
+ if ((tb = libmpq__do_decompress_wave(out_buf, out_size, in_buf, in_size, 1)) < 0) {
+
+ /* something on wave decompression failed. */
+ return tb;
+ }
+
+ /* return transferred bytes. */
+ return tb;
+}
+
+/* this function decompress a stream using wave algorithm. (2 channels) */
+int32_t libmpq__decompress_wave_stereo(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) {
+
+ /* some common variables. */
+ int32_t tb = 0;
+
+ /* save the number of copied bytes. */
+ if ((tb = libmpq__do_decompress_wave(out_buf, out_size, in_buf, in_size, 2)) < 0) {
+
+ /* something on wave decompression failed. */
+ return tb;
+ }
+
+ /* return transferred bytes. */
+ return tb;
+}
+
+/* this function decompress a stream using a combination of the other compression algorithm. */
+int32_t libmpq__decompress_multi(uint8_t *in_buf, uint32_t in_size, uint8_t *out_buf, uint32_t out_size) {
+
+ /* some common variables. */
+ int32_t tb = 0;
+ uint32_t count = 0;
+ uint32_t entries = (sizeof(dcmp_table) / sizeof(decompress_table_s));
+ uint8_t *temp_buf = NULL;
+ uint8_t *work_buf = 0;
+ uint8_t decompress_flag, decompress_unsupp;
+ uint32_t i;
+
+ /* get applied compression types. */
+ decompress_flag = decompress_unsupp = *in_buf++;
+
+ /* decrement data size. */
+ in_size--;
+
+ /* search decompression table type and get all types of compression. */
+ for (i = 0; i < entries; i++) {
+
+ /* check if have to apply this decompression. */
+ if (decompress_flag & dcmp_table[i].mask) {
+
+ /* increase counter for used compression algorithms. */
+ count++;
+ /* this algorithm is supported, remove from unsupp mask */
+ decompress_unsupp &= ~dcmp_table[i].mask;
+ }
+ }
+
+ /* check if there is some method unhandled. (e.g. compressed by future versions) */
+ if (decompress_unsupp) {
+
+ /* compression type is unknown and we need to implement it. :) */
+ return LIBMPQ_ERROR_UNPACK;
+ }
+
+ /* if multiple decompressions should be made, we need temporary buffer for the data. */
+ if (count > 1) {
+
+ /* allocate memory for temporary buffer. */
+ if ((temp_buf = malloc(out_size)) == NULL) {
+
+ /* memory allocation problem. */
+ return LIBMPQ_ERROR_MALLOC;
+ }
+
+ /* cleanup. */
+ memset(temp_buf, 0, out_size);
+ }
+
+ /* apply all decompressions. */
+ for (i = 0, count = 0; i < entries; i++) {
+
+ /* check if not used this kind of compression. */
+ if (decompress_flag & dcmp_table[i].mask) {
+
+ /* if multiple decompressions should be made, we need temporary buffer for the data. */
+ if (count == 0) {
+
+ /* use output buffer as working buffer. */
+ work_buf = out_buf;
+ } else {
+
+ /* use temporary buffer as working buffer. */
+ work_buf = temp_buf;
+ }
+
+ /* decompress buffer using corresponding function. */
+ if ((tb = dcmp_table[i].decompress(in_buf, in_size, work_buf, out_size)) < 0) {
+
+ /* free temporary buffer. */
+ free(temp_buf);
+
+ /* something on decompression failed. */
+ return tb;
+ }
+
+ /* move output size to source size for next compression. */
+ in_size = out_size;
+ in_buf = work_buf;
+
+ /* increase counter. */
+ count++;
+ }
+ }
+
+ /* if output buffer is not the same like target buffer, we have to copy data (this will happen on multiple decompressions). */
+ if (work_buf != out_buf) {
+
+ /* copy buffer. */
+ memcpy(out_buf, in_buf, out_size);
+ }
+
+ /* free temporary buffer. */
+ free(temp_buf);
+
+ /* return transferred bytes. */
+ return tb;
+}
diff --git a/dep/src/libmpq/huffman.c b/dep/src/libmpq/huffman.c
new file mode 100644
index 00000000000..8fc87be2f60
--- /dev/null
+++ b/dep/src/libmpq/huffman.c
@@ -0,0 +1,1101 @@
+/*
+ * huffman.c -- functions do decompress files in mpq files which
+ * uses a modified huffman version.
+ *
+ * Copyright (c) 2003-2008 Maik Broemme <mbroemme@plusserver.de>
+ *
+ * Differences between C++ and C version:
+ *
+ * - Removed the object oriented stuff.
+ * - Replaced the goto things with some better C code.
+ *
+ * This source was adepted from the C++ version of huffman.cpp included
+ * in stormlib. The C++ version belongs to the following authors:
+ *
+ * Ladislav Zezula <ladik@zezula.net>
+ * ShadowFlare <BlakFlare@hotmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* generic includes. */
+#include <stdlib.h>
+#include <string.h>
+
+/* libmpq main includes. */
+#include "mpq.h"
+#include "mpq-internal.h"
+
+/* libmpq generic includes. */
+#include "huffman.h"
+
+/* tables for huffman tree. */
+static const uint8_t table_1502A630[] = {
+
+ /* data for compression type 0x00. */
+ 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
+ 0x00, 0x00,
+
+ /* data for compression type 0x01. */
+ 0x54, 0x16, 0x16, 0x0D, 0x0C, 0x08, 0x06, 0x05, 0x06, 0x05, 0x06, 0x03, 0x04, 0x04, 0x03, 0x05,
+ 0x0E, 0x0B, 0x14, 0x13, 0x13, 0x09, 0x0B, 0x06, 0x05, 0x04, 0x03, 0x02, 0x03, 0x02, 0x02, 0x02,
+ 0x0D, 0x07, 0x09, 0x06, 0x06, 0x04, 0x03, 0x02, 0x04, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02,
+ 0x09, 0x06, 0x04, 0x04, 0x04, 0x04, 0x03, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x04,
+ 0x08, 0x03, 0x04, 0x07, 0x09, 0x05, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
+ 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02,
+ 0x06, 0x0A, 0x08, 0x08, 0x06, 0x07, 0x04, 0x03, 0x04, 0x04, 0x02, 0x02, 0x04, 0x02, 0x03, 0x03,
+ 0x04, 0x03, 0x07, 0x07, 0x09, 0x06, 0x04, 0x03, 0x03, 0x02, 0x01, 0x02, 0x02, 0x02, 0x02, 0x02,
+ 0x0A, 0x02, 0x02, 0x03, 0x02, 0x02, 0x01, 0x01, 0x02, 0x02, 0x02, 0x06, 0x03, 0x05, 0x02, 0x03,
+ 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x01, 0x01, 0x01,
+ 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x04, 0x04, 0x04, 0x07, 0x09, 0x08, 0x0C, 0x02,
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x03,
+ 0x04, 0x01, 0x02, 0x04, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
+ 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x01, 0x02, 0x02, 0x02, 0x06, 0x4B,
+ 0x00, 0x00,
+
+ /* data for compression type 0x02. */
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x27, 0x00, 0x00, 0x23, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xFF, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x01, 0x01, 0x06, 0x0E, 0x10, 0x04,
+ 0x06, 0x08, 0x05, 0x04, 0x04, 0x03, 0x03, 0x02, 0x02, 0x03, 0x03, 0x01, 0x01, 0x02, 0x01, 0x01,
+ 0x01, 0x04, 0x02, 0x04, 0x02, 0x02, 0x02, 0x01, 0x01, 0x04, 0x01, 0x01, 0x02, 0x03, 0x03, 0x02,
+ 0x03, 0x01, 0x03, 0x06, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x01, 0x01,
+ 0x01, 0x29, 0x07, 0x16, 0x12, 0x40, 0x0A, 0x0A, 0x11, 0x25, 0x01, 0x03, 0x17, 0x10, 0x26, 0x2A,
+ 0x10, 0x01, 0x23, 0x23, 0x2F, 0x10, 0x06, 0x07, 0x02, 0x09, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00,
+
+ /* data for compression type 0x03. */
+ 0xFF, 0x0B, 0x07, 0x05, 0x0B, 0x02, 0x02, 0x02, 0x06, 0x02, 0x02, 0x01, 0x04, 0x02, 0x01, 0x03,
+ 0x09, 0x01, 0x01, 0x01, 0x03, 0x04, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
+ 0x05, 0x01, 0x01, 0x01, 0x0D, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x02, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01,
+ 0x0A, 0x04, 0x02, 0x01, 0x06, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x03, 0x01, 0x01, 0x01,
+ 0x05, 0x02, 0x03, 0x04, 0x03, 0x03, 0x03, 0x02, 0x01, 0x01, 0x01, 0x02, 0x01, 0x02, 0x03, 0x03,
+ 0x01, 0x03, 0x01, 0x01, 0x02, 0x05, 0x01, 0x01, 0x04, 0x03, 0x05, 0x01, 0x03, 0x01, 0x03, 0x03,
+ 0x02, 0x01, 0x04, 0x03, 0x0A, 0x06, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x02, 0x02, 0x01, 0x0A, 0x02, 0x05, 0x01, 0x01, 0x02, 0x07, 0x02, 0x17, 0x01, 0x05, 0x01, 0x01,
+ 0x0E, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x06, 0x02, 0x01, 0x04, 0x05, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01,
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
+ 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x07, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01,
+ 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x11,
+ 0x00, 0x00,
+
+ /* data for compression type 0x04. */
+ 0xFF, 0xFB, 0x98, 0x9A, 0x84, 0x85, 0x63, 0x64, 0x3E, 0x3E, 0x22, 0x22, 0x13, 0x13, 0x18, 0x17,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00,
+
+ /* data for compression type 0x05. */
+ 0xFF, 0xF1, 0x9D, 0x9E, 0x9A, 0x9B, 0x9A, 0x97, 0x93, 0x93, 0x8C, 0x8E, 0x86, 0x88, 0x80, 0x82,
+ 0x7C, 0x7C, 0x72, 0x73, 0x69, 0x6B, 0x5F, 0x60, 0x55, 0x56, 0x4A, 0x4B, 0x40, 0x41, 0x37, 0x37,
+ 0x2F, 0x2F, 0x27, 0x27, 0x21, 0x21, 0x1B, 0x1C, 0x17, 0x17, 0x13, 0x13, 0x10, 0x10, 0x0D, 0x0D,
+ 0x0B, 0x0B, 0x09, 0x09, 0x08, 0x08, 0x07, 0x07, 0x06, 0x05, 0x05, 0x04, 0x04, 0x04, 0x19, 0x18,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00,
+
+ /* data for compression type 0x06. */
+ 0xC3, 0xCB, 0xF5, 0x41, 0xFF, 0x7B, 0xF7, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xBF, 0xCC, 0xF2, 0x40, 0xFD, 0x7C, 0xF7, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x7A, 0x46, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00,
+
+ /* data for compression type 0x07. */
+ 0xC3, 0xD9, 0xEF, 0x3D, 0xF9, 0x7C, 0xE9, 0x1E, 0xFD, 0xAB, 0xF1, 0x2C, 0xFC, 0x5B, 0xFE, 0x17,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xBD, 0xD9, 0xEC, 0x3D, 0xF5, 0x7D, 0xE8, 0x1D, 0xFB, 0xAE, 0xF0, 0x2C, 0xFB, 0x5C, 0xFF, 0x18,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x70, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00,
+
+ /* data for compression type 0x08. */
+ 0xBA, 0xC5, 0xDA, 0x33, 0xE3, 0x6D, 0xD8, 0x18, 0xE5, 0x94, 0xDA, 0x23, 0xDF, 0x4A, 0xD1, 0x10,
+ 0xEE, 0xAF, 0xE4, 0x2C, 0xEA, 0x5A, 0xDE, 0x15, 0xF4, 0x87, 0xE9, 0x21, 0xF6, 0x43, 0xFC, 0x12,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0xB0, 0xC7, 0xD8, 0x33, 0xE3, 0x6B, 0xD6, 0x18, 0xE7, 0x95, 0xD8, 0x23, 0xDB, 0x49, 0xD0, 0x11,
+ 0xE9, 0xB2, 0xE2, 0x2B, 0xE8, 0x5C, 0xDD, 0x15, 0xF1, 0x87, 0xE7, 0x20, 0xF7, 0x44, 0xFF, 0x13,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x5F, 0x9E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00
+};
+
+/* this function insert an item to a huffman tree. */
+void libmpq__huffman_insert_item(struct huffman_tree_item_s **p_item, struct huffman_tree_item_s *item, uint32_t where, struct huffman_tree_item_s *item2) {
+
+ /* EDI - next to the first item. */
+ struct huffman_tree_item_s *next = item->next;
+
+ /* ESI - prev to the first item. */
+ struct huffman_tree_item_s *prev = item->prev;
+
+ /* pointer to previous item. */
+ struct huffman_tree_item_s *prev2;
+
+ /* pointer to next item. */
+ long next2;
+
+ /* check the first item already has next one. */
+ if (next != 0) {
+
+ /* check if previous item exist. */
+ if (PTR_INT(prev) < 0) {
+
+ /* return previous item. */
+ prev = PTR_NOT(prev);
+ } else {
+
+ /* add item. */
+ prev += (item - next->prev);
+ }
+
+ /* 150083C1 - remove the item from the tree. */
+ prev->next = next;
+ next->prev = prev;
+
+ /* invalidate prev and next pointer. */
+ item->next = 0;
+ item->prev = 0;
+ }
+
+ /* EDX - check if the second item is not entered. */
+ if (item2 == NULL) {
+
+ /* take the first tree item. */
+ item2 = PTR_PTR(&p_item[1]);
+ }
+
+ /* check if items should be switched or new one inserted. */
+ switch (where) {
+ case SWITCH_ITEMS:
+
+ /* item2->next (pointer to pointer to first). */
+ item->next = item2->next;
+ item->prev = item2->next->prev;
+ item2->next->prev = item;
+
+ /* set the first item. */
+ item2->next = item;
+
+ /* return from function. */
+ return;
+ case INSERT_ITEM:
+
+ /* set next item (or pointer to pointer to first item) - insert as last item. */
+ item->next = item2;
+
+ /* set previous item (or last item in the tree). */
+ item->prev = item2->prev;
+
+ /* usually NULL. */
+ next2 = PTR_INT(p_item[0]);
+
+ /* previous item to the second (or last tree item). */
+ prev2 = item2->prev;
+
+ /* check if previous item is a valid pointer. */
+ if (PTR_INT(prev2) < 0) {
+
+ /* set values. */
+ prev2 = PTR_NOT(prev);
+ prev2->next = item;
+
+ /* next after last item. */
+ item2->prev = item;
+
+ /* return from function. */
+ return;
+ }
+
+ /* check if next item is empty. */
+ if (next2 < 0) {
+
+ /* set next item. */
+ next2 = item2 - item2->next->prev;
+ }
+
+ /* add next item to previous one. */
+ prev2 += next2;
+ prev2->next = item;
+
+ /* set the next and last item. */
+ item2->prev = item;
+
+ /* return from function. */
+ return;
+ default:
+
+ /* nothing to do, so return from function. */
+ return;
+ }
+}
+
+/* 1500BC90 - remove item from huffman tree.*/
+void libmpq__huffman_remove_item(struct huffman_tree_item_s *hi) {
+
+ /* EDX - some common variables. */
+ struct huffman_tree_item_s *temp;
+
+ /* check if next item is not empty. */
+ if (hi->next != NULL) {
+
+ /* fetch previous item. */
+ temp = hi->prev;
+
+ /* check if previous item is a pointer. */
+ if (PTR_INT(temp) <= 0) {
+ temp = PTR_NOT(temp);
+ } else {
+ temp += (hi - hi->next->prev);
+ }
+
+ /* reorganize tree. */
+ temp->next = hi->next;
+ hi->next->prev = hi->prev;
+ hi->next = hi->prev = NULL;
+ }
+}
+
+/* get previous huffman tree item. */
+struct huffman_tree_item_s *libmpq__huffman_previous_item(struct huffman_tree_item_s *hi, long value) {
+
+ /* check if previous item exist. */
+ if (PTR_INT(hi->prev) < 0) {
+
+ /* return previous item. */
+ return PTR_NOT(hi->prev);
+ }
+
+ /* check if something else should returned. */
+ if (value < 0) {
+
+ /* fetch previous item of next item. */
+ value = hi - hi->next->prev;
+ }
+
+ /* return previous item with value. */
+ return hi->prev + value;
+}
+
+/* get one bit from input stream. */
+uint32_t libmpq__huffman_get_1bit(struct huffman_input_stream_s *is) {
+
+ /* some common variables. */
+ uint32_t bit = (is->bit_buf & 1);
+
+ /* shift bit right by one. */
+ is->bit_buf >>= 1;
+
+ /* check if we should extract bits. */
+ if (--is->bits == 0) {
+ is->bit_buf = *(uint32_t *)is->in_buf;
+ is->in_buf += sizeof(int32_t);
+ is->bits = 32;
+ }
+
+ /* return the bit. */
+ return bit;
+}
+
+/* get 7 bits from the input stream. */
+uint32_t libmpq__huffman_get_7bit(struct huffman_input_stream_s *is) {
+
+ /* check if we should extract bits. */
+ if (is->bits <= 7) {
+ is->bit_buf |= *(uint16_t *)is->in_buf << is->bits;
+ is->in_buf += sizeof(int16_t);
+ is->bits += 16;
+ }
+
+ /* get 7 bits from input stream. */
+ return (is->bit_buf & 0x7F);
+}
+
+/* get the whole byte from the input stream. */
+uint32_t libmpq__huffman_get_8bit(struct huffman_input_stream_s *is) {
+
+ /* some common variables. */
+ uint32_t one_byte;
+
+ /* check if we should extract bits. */
+ if (is->bits <= 8) {
+ is->bit_buf |= *(uint16_t *)is->in_buf << is->bits;
+ is->in_buf += sizeof(int16_t);
+ is->bits += 16;
+ }
+
+ /* fill values. */
+ one_byte = (is->bit_buf & 0xFF);
+ is->bit_buf >>= 8;
+ is->bits -= 8;
+
+ /* return the 8 bits. */
+ return one_byte;
+}
+
+/* return struct for 1500E740. */
+struct huffman_tree_item_s *libmpq__huffman_call_1500E740(struct huffman_tree_s *ht) {
+
+ /* EDX */
+ struct huffman_tree_item_s *p_item1 = ht->item3058;
+
+ /* EAX */
+ struct huffman_tree_item_s *p_item2;
+
+ /* some common variables. */
+ struct huffman_tree_item_s *p_next;
+ struct huffman_tree_item_s *p_prev;
+ struct huffman_tree_item_s **pp_item;
+
+ /* check if item is empty. */
+ if (PTR_INT(p_item1) <= 0 || (p_item2 = p_item1) == NULL) {
+
+ /* check if item is not empty. */
+ if ((p_item2 = &ht->items0008[ht->items++]) != NULL) {
+ p_item1 = p_item2;
+ } else {
+ p_item1 = ht->first;
+ }
+ } else {
+ p_item1 = p_item2;
+ }
+
+ /* set next item. */
+ p_next = p_item1->next;
+
+ /* check if next item is not empty. */
+ if (p_next != NULL) {
+
+ /* set previous item. */
+ p_prev = p_item1->prev;
+
+ /* check if previous item is a valid pointer. */
+ if (PTR_INT(p_prev) <= 0) {
+ p_prev = PTR_NOT(p_prev);
+ } else {
+ p_prev += (p_item1 - p_item1->next->prev);
+ }
+
+ /* fill values. */
+ p_prev->next = p_next;
+ p_next->prev = p_prev;
+ p_item1->next = NULL;
+ p_item1->prev = NULL;
+ }
+
+ /* ESI */
+ pp_item = &ht->first;
+ p_item1->next = (struct huffman_tree_item_s *)pp_item;
+ p_item1->prev = pp_item[1];
+
+ /* EDI = ht->item305C - ECX */
+ p_prev = pp_item[1];
+
+ /* check if previous pointer is valid. */
+ if (p_prev <= 0) {
+
+ /* fill values. */
+ p_prev = PTR_NOT(p_prev);
+ p_prev->next = p_item1;
+ p_prev->prev = p_item2;
+ p_item2->parent = NULL;
+ p_item2->child = NULL;
+ } else {
+
+ /* check if pointer is valid. */
+ if (PTR_INT(ht->item305C) < 0) {
+ p_prev += (struct huffman_tree_item_s *)pp_item - (*pp_item)->prev;
+ } else {
+ p_prev += PTR_INT(ht->item305C);
+ }
+
+ /* fill values. */
+ p_prev->next = p_item1;
+ pp_item[1] = p_item2;
+ p_item2->parent = NULL;
+ p_item2->child = NULL;
+ }
+
+ /* return item. */
+ return p_item2;
+}
+
+/* return struct for 1500E820. */
+void libmpq__huffman_call_1500E820(struct huffman_tree_s *ht, struct huffman_tree_item_s *p_item) {
+
+ /* EDI */
+ struct huffman_tree_item_s *p_item1;
+
+ /* EAX */
+ struct huffman_tree_item_s *p_item2 = NULL;
+
+ /* EDX */
+ struct huffman_tree_item_s *p_item3;
+
+ /* EBX */
+ struct huffman_tree_item_s *p_prev;
+
+ /* loop through parent items. */
+ for (; p_item != NULL; p_item = p_item->parent) {
+
+ /* increase byte counter. */
+ p_item->byte_value++;
+
+ /* loop through previous items. */
+ for (p_item1 = p_item; ; p_item1 = p_prev) {
+
+ /* set previous item. */
+ p_prev = p_item1->prev;
+
+ /* check if pointer is valid. */
+ if (PTR_INT(p_prev) <= 0) {
+ p_prev = NULL;
+ break;
+ }
+
+ /* check if byte value of previous item is higher than actual item. */
+ if (p_prev->byte_value >= p_item->byte_value) {
+ break;
+ }
+ }
+
+ /* check if previous item is same like actual item. */
+ if (p_item1 == p_item) {
+ continue;
+ }
+
+ /* check if next item is not empty, */
+ if (p_item1->next != NULL) {
+
+ /* fill values. */
+ p_item2 = libmpq__huffman_previous_item(p_item1, -1);
+ p_item2->next = p_item1->next;
+ p_item1->next->prev = p_item1->prev;
+ p_item1->next = NULL;
+ p_item1->prev = NULL;
+ }
+
+ /* fill values. */
+ p_item2 = p_item->next;
+ p_item1->next = p_item2;
+ p_item1->prev = p_item2->prev;
+ p_item2->prev = p_item1;
+ p_item->next = p_item1;
+
+ /* check if both items are not empty. */
+ if ((p_item2 = p_item1) != NULL) {
+
+ /* fill values. */
+ p_item2 = libmpq__huffman_previous_item(p_item, -1);
+ p_item2->next = p_item->next;
+ p_item->next->prev = p_item->prev;
+ p_item->next = NULL;
+ p_item->prev = NULL;
+ }
+
+ /* check if previous item is empty. */
+ if (p_prev == NULL) {
+ p_prev = PTR_PTR(&ht->first);
+ }
+
+ /* fill values. */
+ p_item2 = p_prev->next;
+ p_item->next = p_item2;
+ p_item->prev = p_item2->prev;
+ p_item2->prev = p_item;
+ p_prev->next = p_item;
+ p_item3 = p_item1->parent->child;
+ p_item2 = p_item->parent;
+
+ /* check if child item and parent item match. */
+ if (p_item2->child == p_item) {
+ p_item2->child = p_item1;
+ }
+
+ /* check if items match. */
+ if (p_item3 == p_item1) {
+ p_item1->parent->child = p_item;
+ }
+
+ /* fill values. */
+ p_item2 = p_item->parent;
+ p_item->parent = p_item1->parent;
+ p_item1->parent = p_item2;
+
+ /* increase counter. */
+ ht->offs0004++;
+ }
+}
+
+/* this function initialize a huffman tree. */
+void libmpq__huffman_tree_init(struct huffman_tree_s *ht, uint32_t cmp) {
+
+ /* some common variables. */
+ uint32_t count;
+ struct huffman_tree_item_s *hi;
+
+ /* clear links for all the items in the tree. */
+ for (hi = ht->items0008, count = 0x203; count != 0; hi++, count--) {
+ hi->next = hi->prev = NULL;
+ }
+
+ /* fill values. */
+ ht->item3050 = NULL;
+ ht->item3054 = PTR_PTR(&ht->item3054);
+ ht->item3058 = PTR_NOT(ht->item3054);
+ ht->item305C = NULL;
+ ht->first = PTR_PTR(&ht->first);
+ ht->last = PTR_NOT(ht->first);
+ ht->offs0004 = 1;
+ ht->items = 0;
+
+ /* clear all huffman decompress items, do this only if preparing for decompression. */
+ if (cmp == LIBMPQ_HUFF_DECOMPRESS) {
+ for (count = 0; count < sizeof(ht->qd3474) / sizeof(struct huffman_decompress_s); count++) {
+ ht->qd3474[count].offs00 = 0;
+ }
+ }
+}
+
+/* this function build a huffman tree, called with the first 8 bits loaded from input stream. */
+void libmpq__huffman_tree_build(struct huffman_tree_s *ht, uint32_t cmp_type) {
+
+ /* [ESP+10] - the greatest character found in table. */
+ uint32_t max_byte;
+
+ /* [ESP+1C] - pointer to uint8_t in table_1502A630. */
+ const uint8_t *byte_array;
+
+ /* thats needed to replace the goto stuff from original source. :) */
+ uint32_t found;
+
+ /* [ESP+14] - Pointer to Huffman tree item pointer array. */
+ struct huffman_tree_item_s **p_item;
+ struct huffman_tree_item_s *child1;
+
+ /* some common variables. */
+ uint32_t i;
+
+ /* ESI - loop while pointer has a negative value (last entry). */
+ while (PTR_INT(ht->last) > 0) {
+
+ /* EAX */
+ struct huffman_tree_item_s *temp;
+
+ /* ESI->next */
+ if (ht->last->next != NULL) {
+ libmpq__huffman_remove_item(ht->last);
+ }
+
+ /* [EDI+4] */
+ ht->item3058 = PTR_PTR(&ht->item3054);
+
+ /* EAX */
+ ht->last->prev = ht->item3058;
+ temp = libmpq__huffman_previous_item(PTR_PTR(&ht->item3054), PTR_INT(&ht->item3050));
+ temp->next = ht->last;
+ ht->item3054 = ht->last;
+ }
+
+ /* clear all pointers in huffman tree item array. */
+ memset(ht->items306C, 0, sizeof(ht->items306C));
+
+ /* greatest character found init to zero. */
+ max_byte = 0;
+
+ /* pointer to current entry in huffman tree item pointer array. */
+ p_item = (struct huffman_tree_item_s **)&ht->items306C;
+
+ /* ensure we have low 8 bits only. */
+ cmp_type &= 0xFF;
+
+ /* EDI also. */
+ byte_array = table_1502A630 + cmp_type * 258;
+
+ /* loop to build huffman tree. */
+ for (i = 0; i < 0x100; i++, p_item++) {
+
+ /* item to be created. */
+ struct huffman_tree_item_s *item = ht->item3058;
+ struct huffman_tree_item_s *p_item3 = ht->item3058;
+ uint8_t one_byte = byte_array[i];
+
+ /* skip all the bytes which are zero. */
+ if (byte_array[i] == 0) {
+ continue;
+ }
+
+ /* if not valid pointer, take the first available item in the array. */
+ if (PTR_INT(item) <= 0) {
+ item = &ht->items0008[ht->items++];
+ }
+
+ /* insert this item as the top of the tree. */
+ libmpq__huffman_insert_item(&ht->item305C, item, SWITCH_ITEMS, NULL);
+
+ /* invalidate child and parent. */
+ item->parent = NULL;
+ item->child = NULL;
+
+ /* store pointer into pointer array. */
+ *p_item = item;
+
+ /* store counter. */
+ item->dcmp_byte = i;
+
+ /* store byte value. */
+ item->byte_value = one_byte;
+
+ /* check if byte is to big. */
+ if (one_byte >= max_byte) {
+
+ /* set max byte to highest value. */
+ max_byte = one_byte;
+
+ /* continue loop. */
+ continue;
+ }
+
+ /* find the first item which has byte value greater than current one byte. */
+ found = 0;
+
+ /* EDI - Pointer to the last item. */
+ if (PTR_INT((p_item3 = ht->last)) > 0) {
+
+ /* 15006AF7 */
+ if (p_item3 != NULL) {
+
+ /* 15006AFB */
+ do {
+
+ /* check if we found item. */
+ if (p_item3->byte_value >= one_byte) {
+ found = 1;
+ break;
+ }
+
+ /* switch to previous item. */
+ p_item3 = p_item3->prev;
+ } while (PTR_INT(p_item3) > 0);
+ }
+ }
+
+ /* check if item was not found. */
+ if (found == 0) {
+ p_item3 = NULL;
+ }
+
+ /* 15006B09 */
+ if (item->next != NULL) {
+ libmpq__huffman_remove_item(item);
+ }
+
+ /* 15006B15 */
+ if (p_item3 == NULL) {
+ p_item3 = PTR_PTR(&ht->first);
+ }
+
+ /* 15006B1F */
+ item->next = p_item3->next;
+ item->prev = p_item3->next->prev;
+ p_item3->next->prev = item;
+ p_item3->next = item;
+ }
+
+ /* 15006B4A */
+ for (; i < 0x102; i++) {
+
+ /* EDI */
+ struct huffman_tree_item_s **p_item2 = &ht->items306C[i];
+
+ /* 15006B59 - ESI */
+ struct huffman_tree_item_s *item2 = ht->item3058;
+
+ /* check if item is a valid pointer. */
+ if (PTR_INT(item2) <= 0) {
+ item2 = &ht->items0008[ht->items++];
+ }
+
+ /* insert the item into tree. */
+ libmpq__huffman_insert_item(&ht->item305C, item2, INSERT_ITEM, NULL);
+
+ /* 15006B89 */
+ item2->dcmp_byte = i;
+ item2->byte_value = 1;
+ item2->parent = NULL;
+ item2->child = NULL;
+ *p_item2++ = item2;
+ }
+
+ /* 15006BAA - EDI - last item (first child to item). */
+ if (PTR_INT((child1 = ht->last)) > 0) {
+
+ /* EBP */
+ struct huffman_tree_item_s *child2;
+
+ /* ESI */
+ struct huffman_tree_item_s *item;
+
+ /* 15006BB8 */
+ while (PTR_INT((child2 = child1->prev)) > 0) {
+ if (PTR_INT((item = ht->item3058)) <= 0) {
+ item = &ht->items0008[ht->items++];
+ }
+
+ /* 15006BE3 */
+ libmpq__huffman_insert_item(&ht->item305C, item, SWITCH_ITEMS, NULL);
+
+ /* 15006BF3 */
+ item->parent = NULL;
+ item->child = NULL;
+
+ /*
+ * EDX = child2->byte_value + child1->byte_value;
+ * EAX = child1->byte_value;
+ * ECX = max_byte; (the greatest character (0xFF usually))
+ * item->byte_value (0x02 usually)
+ */
+ item->byte_value = child1->byte_value + child2->byte_value;
+
+ /* previous item in the tree. */
+ item->child = child1;
+ child1->parent = item;
+ child2->parent = item;
+
+ /* EAX = item->byte_value */
+ if (item->byte_value >= max_byte) {
+ max_byte = item->byte_value;
+ } else {
+
+ /* EDI */
+ struct huffman_tree_item_s *p_item2 = child2->prev;
+ found = 0;
+
+ /* check if item is a valid pointer. */
+ if (PTR_INT(p_item2) > 0) {
+
+ /* 15006C2D */
+ do {
+
+ /* check if we found item. */
+ if (p_item2->byte_value >= item->byte_value) {
+ found = 1;
+ break;
+ }
+
+ /* switch to previous item. */
+ p_item2 = p_item2->prev;
+ } while (PTR_INT(p_item2) > 0);
+ }
+
+ /* check if item was not found. */
+ if (found == 0) {
+ p_item2 = NULL;
+ }
+
+ /* check if next item exist. */
+ if (item->next != 0) {
+
+ /* some common variables. */
+ struct huffman_tree_item_s *temp4 = libmpq__huffman_previous_item(item, -1);
+
+ /* zhe first item changed. */
+ temp4->next = item->next;
+
+ /* first->prev changed to negative value. */
+ item->next->prev = item->prev;
+ item->next = NULL;
+ item->prev = NULL;
+ }
+
+ /* 15006C62 */
+ if (p_item2 == NULL) {
+ p_item2 = PTR_PTR(&ht->first);
+ }
+
+ /* set item with 0x100 byte value. */
+ item->next = p_item2->next;
+
+ /* set item with 0x17 byte value. */
+ item->prev = p_item2->next->prev;
+
+ /* changed prev of item with. */
+ p_item2->next->prev = item;
+ p_item2->next = item;
+ }
+
+ /* 15006C7B */
+ if (PTR_INT((child1 = child2->prev)) <= 0) {
+ break;
+ }
+ }
+ }
+
+ /* 15006C88 */
+ ht->offs0004 = 1;
+}
+
+/* this function did the real decompression. */
+int32_t libmpq__do_decompress_huffman(struct huffman_tree_s *ht, struct huffman_input_stream_s *is, uint8_t *out_buf, uint32_t out_length) {
+
+ /* some common variables. */
+ uint32_t dcmp_byte = 0;
+ uint8_t *out_pos = out_buf;
+ uint32_t bit_count;
+ struct huffman_decompress_s *qd;
+ struct huffman_tree_item_s *p_item1;
+ struct huffman_tree_item_s *p_item2;
+
+ /* 8 bits loaded from input stream. */
+ uint32_t n8bits;
+
+ /* 7 bits loaded from input stream. */
+ uint32_t n7bits;
+
+ /* thats needed to replace the goto stuff from original source. :) */
+ uint32_t found;
+
+ /* can we use quick decompression */
+ uint32_t has_qd;
+
+ /* test the output length, must not be non zero. */
+ if (out_length == 0) {
+ return 0;
+ }
+
+ /* get the compression type from the input stream. */
+ n8bits = libmpq__huffman_get_8bit(is);
+
+ /* build the Huffman tree. */
+ libmpq__huffman_tree_build(ht, n8bits);
+
+ /* compression 8 bit or not? */
+ ht->cmp0 = (n8bits == 0) ? TRUE : FALSE;
+
+ /* loop until break. */
+ for(;;) {
+
+ /* get 7 bits from input stream. */
+ n7bits = libmpq__huffman_get_7bit(is);
+
+ /* try to use quick decompression, check huffman decompress struct for corresponding item. */
+ qd = &ht->qd3474[n7bits];
+
+ /* if there is a quick-pass possible (ebx). */
+ has_qd = (qd->offs00 >= ht->offs0004) ? TRUE : FALSE;
+
+ /* if we can use quick decompress, use it. */
+ if (has_qd) {
+ found = 0;
+ if (qd->bits > 7) {
+ is->bit_buf >>= 7;
+ is->bits -= 7;
+ p_item1 = qd->p_item;
+ found = 1;
+ }
+ if (found == 0) {
+ is->bit_buf >>= qd->bits;
+ is->bits -= qd->bits;
+ dcmp_byte = qd->dcmp_byte;
+ }
+ } else {
+ found = 1;
+ p_item1 = ht->first->next->prev;
+ if (PTR_INT(p_item1) <= 0) {
+ p_item1 = NULL;
+ }
+ }
+
+ /* check if item was found. */
+ if (found == 1) {
+ bit_count = 0;
+ p_item2 = NULL;
+
+ /* loop until tree has no deeper level. */
+ do {
+
+ /* move down by one level. */
+ p_item1 = p_item1->child;
+
+ /* check if current bit is set, move to previous. */
+ if (libmpq__huffman_get_1bit(is)) {
+ p_item1 = p_item1->prev;
+ }
+
+ /* check if we are at 7th bit, save current huffman tree item. */
+ if (++bit_count == 7) {
+ p_item2 = p_item1;
+ }
+ } while (p_item1->child != NULL);
+
+ /* no quick decompression. :( */
+ if (has_qd == FALSE) {
+
+ /* check bit counter. */
+ if (bit_count > 7) {
+ qd->offs00 = ht->offs0004;
+ qd->bits = bit_count;
+ qd->p_item = p_item2;
+ } else {
+ uint32_t index = n7bits & (0xFFFFFFFF >> (32 - bit_count));
+ uint32_t add = (1 << bit_count);
+
+ /* loop through compression. */
+ for (qd = &ht->qd3474[index]; index <= 0x7F; index += add, qd += add) {
+ qd->offs00 = ht->offs0004;
+ qd->bits = bit_count;
+ qd->dcmp_byte = p_item1->dcmp_byte;
+ }
+ }
+ }
+
+ /* set compression byte. */
+ dcmp_byte = p_item1->dcmp_byte;
+ }
+
+ /* check if huffman tree needs to be modified. */
+ if (dcmp_byte == 0x101) {
+
+ /* fill values. */
+ n8bits = libmpq__huffman_get_8bit(is);
+ p_item1 = (ht->last <= 0) ? NULL : ht->last;
+ p_item2 = libmpq__huffman_call_1500E740(ht);
+ p_item2->parent = p_item1;
+ p_item2->dcmp_byte = p_item1->dcmp_byte;
+ p_item2->byte_value = p_item1->byte_value;
+ ht->items306C[p_item2->dcmp_byte] = p_item2;
+ p_item2 = libmpq__huffman_call_1500E740(ht);
+ p_item2->parent = p_item1;
+ p_item2->dcmp_byte = n8bits;
+ p_item2->byte_value = 0;
+ ht->items306C[p_item2->dcmp_byte] = p_item2;
+ p_item1->child = p_item2;
+
+ /* call 1500E820. */
+ libmpq__huffman_call_1500E820(ht, p_item2);
+
+ /* check if compression is not set. */
+ if (ht->cmp0 == 0) {
+ libmpq__huffman_call_1500E820(ht, ht->items306C[n8bits]);
+ }
+
+ /* set compression byte. */
+ dcmp_byte = n8bits;
+ }
+
+ /* check for compression. */
+ if (dcmp_byte == 0x100) {
+ break;
+ }
+
+ /* increase position by compression byte. */
+ *out_pos++ = (uint8_t)dcmp_byte;
+ if (--out_length == 0) {
+ break;
+ }
+
+ /* check if compression is not set. */
+ if (ht->cmp0) {
+ libmpq__huffman_call_1500E820(ht, ht->items306C[dcmp_byte]);
+ }
+ }
+
+ /* return copied bytes. */
+ return (out_pos - out_buf);
+}
diff --git a/dep/src/libmpq/mpq.c b/dep/src/libmpq/mpq.c
new file mode 100644
index 00000000000..4e3d7db0b4d
--- /dev/null
+++ b/dep/src/libmpq/mpq.c
@@ -0,0 +1,1033 @@
+/*
+ * mpq.c -- functions for developers using libmpq.
+ *
+ * Copyright (c) 2003-2008 Maik Broemme <mbroemme@plusserver.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* mpq-tools configuration includes. */
+#include "config.h"
+
+/* libmpq main includes. */
+#include "mpq.h"
+#include "mpq-internal.h"
+
+/* libmpq generic includes. */
+#include "common.h"
+
+/* generic includes. */
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#if HAVE_UNISTD_H
+ #include <unistd.h>
+#endif
+
+#ifdef _MSC_VER
+ #define fseeko _fseeki64
+#endif
+
+/* this function returns the library version information. */
+const char *libmpq__version(void) {
+
+ /* return version information. */
+ return VERSION;
+}
+
+/* this function read a file and verify if it is a valid mpq archive, then it read and decrypt the hash table. */
+int32_t libmpq__archive_open(mpq_archive_s **mpq_archive, const char *mpq_filename, libmpq__off_t archive_offset) {
+
+ /* some common variables. */
+ uint32_t rb = 0;
+ uint32_t i = 0;
+ uint32_t count = 0;
+ int32_t result = 0;
+ uint32_t header_search = FALSE;
+
+ if (archive_offset == -1) {
+ archive_offset = 0;
+ header_search = TRUE;
+ }
+
+ if ((*mpq_archive = calloc(1, sizeof(mpq_archive_s))) == NULL) {
+
+ /* archive struct could not be allocated */
+ return LIBMPQ_ERROR_MALLOC;
+ }
+
+ /* check if file exists and is readable */
+ if (((*mpq_archive)->fp = fopen(mpq_filename, "rb")) == NULL) {
+
+ /* file could not be opened. */
+ result = LIBMPQ_ERROR_OPEN;
+ goto error;
+ }
+
+ /* assign some default values. */
+ (*mpq_archive)->mpq_header.mpq_magic = 0;
+ (*mpq_archive)->files = 0;
+
+ /* loop through file and search for mpq signature. */
+ while (TRUE) {
+
+ /* reset header values. */
+ (*mpq_archive)->mpq_header.mpq_magic = 0;
+
+ /* seek in file. */
+ if (fseeko((*mpq_archive)->fp, archive_offset, SEEK_SET) < 0) {
+
+ /* seek in file failed. */
+ result = LIBMPQ_ERROR_SEEK;
+ goto error;
+ }
+
+ /* read header from file. */
+ if ((rb = fread(&(*mpq_archive)->mpq_header, 1, sizeof(mpq_header_s), (*mpq_archive)->fp)) != sizeof(mpq_header_s)) {
+
+ /* no valid mpq archive. */
+ result = LIBMPQ_ERROR_FORMAT;
+ goto error;
+ }
+
+ /* check if we found a valid mpq header. */
+ if ((*mpq_archive)->mpq_header.mpq_magic == LIBMPQ_HEADER) {
+
+ /* check if we process old mpq archive version. */
+ if ((*mpq_archive)->mpq_header.version == LIBMPQ_ARCHIVE_VERSION_ONE) {
+
+ /* check if the archive is protected. */
+ if ((*mpq_archive)->mpq_header.header_size != sizeof(mpq_header_s)) {
+
+ /* correct header size. */
+ (*mpq_archive)->mpq_header.header_size = sizeof(mpq_header_s);
+ }
+ }
+
+ /* check if we process new mpq archive version. */
+ if ((*mpq_archive)->mpq_header.version == LIBMPQ_ARCHIVE_VERSION_TWO) {
+
+ /* check if the archive is protected. */
+ if ((*mpq_archive)->mpq_header.header_size != sizeof(mpq_header_s) + sizeof(mpq_header_ex_s)) {
+
+ /* correct header size. */
+ (*mpq_archive)->mpq_header.header_size = sizeof(mpq_header_s) + sizeof(mpq_header_ex_s);
+ }
+ }
+
+ /* break the loop, because header was found. */
+ break;
+ }
+
+ /* move to the next possible offset. */
+ if (!header_search) {
+
+ /* no valid mpq archive. */
+ result = LIBMPQ_ERROR_FORMAT;
+ goto error;
+ }
+ archive_offset += 512;
+ }
+
+ /* store block size for later use. */
+ (*mpq_archive)->block_size = 512 << (*mpq_archive)->mpq_header.block_size;
+
+ /* store archive offset and size for later use. */
+ (*mpq_archive)->archive_offset = archive_offset;
+
+ /* check if we process new mpq archive version. */
+ if ((*mpq_archive)->mpq_header.version == LIBMPQ_ARCHIVE_VERSION_TWO) {
+
+ /* seek in file. */
+ if (fseeko((*mpq_archive)->fp, sizeof(mpq_header_s) + archive_offset, SEEK_SET) < 0) {
+
+ /* seek in file failed. */
+ result = LIBMPQ_ERROR_SEEK;
+ goto error;
+ }
+
+ /* read header from file. */
+ if ((rb = fread(&(*mpq_archive)->mpq_header_ex, 1, sizeof(mpq_header_ex_s), (*mpq_archive)->fp)) != sizeof(mpq_header_ex_s)) {
+
+ /* no valid mpq archive. */
+ result = LIBMPQ_ERROR_FORMAT;
+ goto error;
+ }
+ }
+
+ /* allocate memory for the block table, hash table, file and block table to file mapping. */
+ if (((*mpq_archive)->mpq_block = calloc((*mpq_archive)->mpq_header.block_table_count, sizeof(mpq_block_s))) == NULL ||
+ ((*mpq_archive)->mpq_block_ex = calloc((*mpq_archive)->mpq_header.block_table_count, sizeof(mpq_block_ex_s))) == NULL ||
+ ((*mpq_archive)->mpq_hash = calloc((*mpq_archive)->mpq_header.hash_table_count, sizeof(mpq_hash_s))) == NULL ||
+ ((*mpq_archive)->mpq_file = calloc((*mpq_archive)->mpq_header.block_table_count, sizeof(mpq_file_s))) == NULL ||
+ ((*mpq_archive)->mpq_map = calloc((*mpq_archive)->mpq_header.block_table_count, sizeof(mpq_map_s))) == NULL) {
+
+ /* memory allocation problem. */
+ result = LIBMPQ_ERROR_MALLOC;
+ goto error;
+ }
+
+ /* seek in file. */
+ if (fseeko((*mpq_archive)->fp, (*mpq_archive)->mpq_header.hash_table_offset + (((long long)((*mpq_archive)->mpq_header_ex.hash_table_offset_high)) << 32) + (*mpq_archive)->archive_offset, SEEK_SET) < 0) {
+
+ /* seek in file failed. */
+ result = LIBMPQ_ERROR_SEEK;
+ goto error;
+ }
+
+ /* read the hash table into the buffer. */
+ if ((rb = fread((*mpq_archive)->mpq_hash, 1, (*mpq_archive)->mpq_header.hash_table_count * sizeof(mpq_hash_s), (*mpq_archive)->fp)) < 0) {
+
+ /* something on read failed. */
+ result = LIBMPQ_ERROR_READ;
+ goto error;
+ }
+
+ /* decrypt the hashtable. */
+ libmpq__decrypt_block((uint32_t *)((*mpq_archive)->mpq_hash), (*mpq_archive)->mpq_header.hash_table_count * sizeof(mpq_hash_s), libmpq__hash_string("(hash table)", 0x300));
+
+ /* seek in file. */
+ if (fseeko((*mpq_archive)->fp, (*mpq_archive)->mpq_header.block_table_offset + (((long long)((*mpq_archive)->mpq_header_ex.block_table_offset_high)) << 32) + (*mpq_archive)->archive_offset, SEEK_SET) < 0) {
+
+ /* seek in file failed. */
+ result = LIBMPQ_ERROR_SEEK;
+ goto error;
+ }
+
+ /* read the block table into the buffer. */
+ if ((rb = fread((*mpq_archive)->mpq_block, 1, (*mpq_archive)->mpq_header.block_table_count * sizeof(mpq_block_s), (*mpq_archive)->fp)) < 0) {
+
+ /* something on read failed. */
+ result = LIBMPQ_ERROR_READ;
+ goto error;
+ }
+
+ /* decrypt block table. */
+ libmpq__decrypt_block((uint32_t *)((*mpq_archive)->mpq_block), (*mpq_archive)->mpq_header.block_table_count * sizeof(mpq_block_s), libmpq__hash_string("(block table)", 0x300));
+
+ /* check if extended block table is present, regardless of version 2 it is only present in archives > 4GB. */
+ if ((*mpq_archive)->mpq_header_ex.extended_offset > 0) {
+
+ /* seek in file. */
+ if (fseeko((*mpq_archive)->fp, (*mpq_archive)->mpq_header_ex.extended_offset + archive_offset, SEEK_SET) < 0) {
+
+ /* seek in file failed. */
+ result = LIBMPQ_ERROR_SEEK;
+ goto error;
+ }
+
+ /* read header from file. */
+ if ((rb = fread((*mpq_archive)->mpq_block_ex, 1, (*mpq_archive)->mpq_header.block_table_count * sizeof(mpq_block_ex_s), (*mpq_archive)->fp)) < 0) {
+
+ /* no valid mpq archive. */
+ result = LIBMPQ_ERROR_FORMAT;
+ goto error;
+ }
+ }
+
+ /* loop through all files in mpq archive and check if they are valid. */
+ for (i = 0; i < (*mpq_archive)->mpq_header.block_table_count; i++) {
+
+ /* save block difference between valid and invalid blocks. */
+ (*mpq_archive)->mpq_map[i].block_table_diff = i - count;
+
+ /* check if file exists, sizes and offsets are correct. */
+ if (((*mpq_archive)->mpq_block[i].flags & LIBMPQ_FLAG_EXISTS) == 0) {
+
+ /* file does not exist, so nothing to do with that block. */
+ continue;
+ }
+
+ /* create final indices tables. */
+ (*mpq_archive)->mpq_map[count].block_table_indices = i;
+
+ /* increase file counter. */
+ count++;
+ }
+
+ /* save the number of files. */
+ (*mpq_archive)->files = count;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+
+error:
+ if ((*mpq_archive)->fp)
+ fclose((*mpq_archive)->fp);
+
+ free((*mpq_archive)->mpq_map);
+ free((*mpq_archive)->mpq_file);
+ free((*mpq_archive)->mpq_hash);
+ free((*mpq_archive)->mpq_block);
+ free((*mpq_archive)->mpq_block_ex);
+ free(*mpq_archive);
+
+ *mpq_archive = NULL;
+
+ return result;
+}
+
+/* this function close the file descriptor, free the decryption buffer and the file list. */
+int32_t libmpq__archive_close(mpq_archive_s *mpq_archive) {
+
+ /* try to close the file */
+ if ((fclose(mpq_archive->fp)) < 0) {
+
+ /* don't free anything here, so the caller can try calling us
+ * again.
+ */
+ return LIBMPQ_ERROR_CLOSE;
+ }
+
+ /* free header, tables and list. */
+ free(mpq_archive->mpq_map);
+ free(mpq_archive->mpq_file);
+ free(mpq_archive->mpq_hash);
+ free(mpq_archive->mpq_block);
+ free(mpq_archive->mpq_block_ex);
+ free(mpq_archive);
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the packed size of all files in the archive. */
+int32_t libmpq__archive_packed_size(mpq_archive_s *mpq_archive, libmpq__off_t *packed_size) {
+
+ /* some common variables. */
+ uint32_t i;
+
+ /* loop through all files in archive and count packed size. */
+ for (i = 0; i < mpq_archive->files; i++) {
+ *packed_size += mpq_archive->mpq_block[mpq_archive->mpq_map[i].block_table_indices].packed_size;
+ }
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the unpacked size of all files in the archive. */
+int32_t libmpq__archive_unpacked_size(mpq_archive_s *mpq_archive, libmpq__off_t *unpacked_size) {
+
+ /* some common variables. */
+ uint32_t i;
+
+ /* loop through all files in archive and count unpacked size. */
+ for (i = 0; i < mpq_archive->files; i++) {
+ *unpacked_size += mpq_archive->mpq_block[mpq_archive->mpq_map[i].block_table_indices].unpacked_size;
+ }
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the archive offset (beginning of archive in file). */
+int32_t libmpq__archive_offset(mpq_archive_s *mpq_archive, libmpq__off_t *offset) {
+
+ /* return archive offset. */
+ *offset = mpq_archive->archive_offset;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the archive offset. */
+int32_t libmpq__archive_version(mpq_archive_s *mpq_archive, uint32_t *version) {
+
+ /* return archive version. */
+ *version = mpq_archive->mpq_header.version + 1;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the number of valid files in archive. */
+int32_t libmpq__archive_files(mpq_archive_s *mpq_archive, uint32_t *files) {
+
+ /* return archive version. */
+ *files = mpq_archive->files;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the packed size of the given files in the archive. */
+int32_t libmpq__file_packed_size(mpq_archive_s *mpq_archive, uint32_t file_number, libmpq__off_t *packed_size) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* get the packed size of file. */
+ *packed_size = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].packed_size;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the unpacked size of the given file in the archive. */
+int32_t libmpq__file_unpacked_size(mpq_archive_s *mpq_archive, uint32_t file_number, libmpq__off_t *unpacked_size) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* get the unpacked size of file. */
+ *unpacked_size = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the file offset (beginning of file in archive). */
+int32_t libmpq__file_offset(mpq_archive_s *mpq_archive, uint32_t file_number, libmpq__off_t *offset) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* return file offset relative to archive start. */
+ *offset = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].offset + (((long long)mpq_archive->mpq_block_ex[mpq_archive->mpq_map[file_number].block_table_indices].offset_high) << 32);
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the number of blocks for the given file in the archive. */
+int32_t libmpq__file_blocks(mpq_archive_s *mpq_archive, uint32_t file_number, uint32_t *blocks) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* return the number of blocks for the given file. */
+ *blocks = (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) != 0 ? 1 : (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return if the file is encrypted or not. */
+int32_t libmpq__file_encrypted(mpq_archive_s *mpq_archive, uint32_t file_number, uint32_t *encrypted) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* return the encryption status of file. */
+ *encrypted = (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_ENCRYPTED) != 0 ? TRUE : FALSE;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return if the file is compressed or not. */
+int32_t libmpq__file_compressed(mpq_archive_s *mpq_archive, uint32_t file_number, uint32_t *compressed) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* return the compression status of file. */
+ *compressed = (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_COMPRESS_MULTI) != 0 ? TRUE : FALSE;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return if the file is imploded or not. */
+int32_t libmpq__file_imploded(mpq_archive_s *mpq_archive, uint32_t file_number, uint32_t *imploded) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* return the implosion status of file. */
+ *imploded = (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_COMPRESS_PKZIP) != 0 ? TRUE : FALSE;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return filenumber by the given name. */
+int32_t libmpq__file_number(mpq_archive_s *mpq_archive, const char *filename, uint32_t *number) {
+
+ /* some common variables. */
+ uint32_t i, hash1, hash2, hash3, ht_count;
+
+ /* if the list of file names doesn't include this one, we'll have
+ * to figure out the file number the "hard" way.
+ */
+ ht_count = mpq_archive->mpq_header.hash_table_count;
+
+ hash1 = libmpq__hash_string (filename, 0x0) & (ht_count - 1);
+ hash2 = libmpq__hash_string (filename, 0x100);
+ hash3 = libmpq__hash_string (filename, 0x200);
+
+ /* loop through all files in mpq archive.
+ * hash1 gives us a clue about the starting position of this
+ * search.
+ */
+ for (i = hash1; mpq_archive->mpq_hash[i].block_table_index != LIBMPQ_HASH_FREE; i = (i + 1) & (ht_count - 1)) {
+
+ /* if the other two hashes match, we found our file number. */
+ if (mpq_archive->mpq_hash[i].hash_a == hash2 &&
+ mpq_archive->mpq_hash[i].hash_b == hash3) {
+
+ /* return the file number. */
+ *number = mpq_archive->mpq_hash[i].block_table_index - mpq_archive->mpq_map[mpq_archive->mpq_hash[i].block_table_index].block_table_diff;
+
+ /* we found our file, return zero. */
+ return LIBMPQ_SUCCESS;
+ }
+
+ /* check if we have cycled through the whole hash table */
+ if (((i + 1) & (ht_count - 1)) == hash1) {
+ break;
+ }
+ }
+
+ /* if no matching entry found, so return error. */
+ return LIBMPQ_ERROR_EXIST;
+}
+
+/* this function read the given file from archive into a buffer. */
+int32_t libmpq__file_read(mpq_archive_s *mpq_archive, uint32_t file_number, uint8_t *out_buf, libmpq__off_t out_size, libmpq__off_t *transferred) {
+
+ /* some common variables. */
+ uint32_t i;
+ uint32_t blocks = 0;
+ int32_t result = 0;
+ libmpq__off_t file_offset = 0;
+ libmpq__off_t unpacked_size = 0;
+ libmpq__off_t transferred_block = 0;
+ libmpq__off_t transferred_total = 0;
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* get target size of block. */
+ libmpq__file_unpacked_size(mpq_archive, file_number, &unpacked_size);
+
+ /* check if target buffer is to small. */
+ if (unpacked_size > out_size) {
+
+ /* output buffer size is to small or block size is unknown. */
+ return LIBMPQ_ERROR_SIZE;
+ }
+
+ /* fetch file offset. */
+ libmpq__file_offset(mpq_archive, file_number, &file_offset);
+
+ /* get block count for file. */
+ libmpq__file_blocks(mpq_archive, file_number, &blocks);
+
+ /* open the packed block offset table. */
+ if ((result = libmpq__block_open_offset(mpq_archive, file_number)) < 0) {
+
+ /* something on opening packed block offset table failed. */
+ return result;
+ }
+
+ /* loop through all blocks. */
+ for (i = 0; i < blocks; i++) {
+
+ /* cleanup size variable. */
+ unpacked_size = 0;
+
+ /* get unpacked block size. */
+ libmpq__block_unpacked_size(mpq_archive, file_number, i, &unpacked_size);
+
+ /* read block. */
+ if ((result = libmpq__block_read(mpq_archive, file_number, i, out_buf + transferred_total, unpacked_size, &transferred_block)) < 0) {
+
+ /* close the packed block offset table. */
+ libmpq__block_close_offset(mpq_archive, file_number);
+
+ /* something on reading block failed. */
+ return result;
+ }
+
+ transferred_total += transferred_block;
+
+ }
+
+ /* close the packed block offset table. */
+ libmpq__block_close_offset(mpq_archive, file_number);
+
+ /* check for null pointer. */
+ if (transferred != NULL) {
+
+ /* store transferred bytes. */
+ *transferred = transferred_total;
+ }
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function open a file in the given archive and caches the block offset information. */
+int32_t libmpq__block_open_offset(mpq_archive_s *mpq_archive, uint32_t file_number) {
+
+ /* some common variables. */
+ uint32_t i;
+ uint32_t packed_size;
+ int32_t rb = 0;
+ int32_t result = 0;
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ if (mpq_archive->mpq_file[file_number]) {
+
+ /* file already opened, so increment counter */
+ mpq_archive->mpq_file[file_number]->open_count++;
+ return LIBMPQ_SUCCESS;
+ }
+
+ /* check if file is not stored in a single sector. */
+ if ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) == 0) {
+
+ /* get packed size based on block size and block count. */
+ packed_size = sizeof(uint32_t) * (((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size) + 1);
+ } else {
+
+ /* file is stored in single sector and we need only two entries for the packed block offset table. */
+ packed_size = sizeof(uint32_t) * 2;
+ }
+
+ /* check if data has one extra entry. */
+ if ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_EXTRA) != 0) {
+
+ /* add one uint32_t. */
+ packed_size += sizeof(uint32_t);
+ }
+
+ /* allocate memory for the file. */
+ if ((mpq_archive->mpq_file[file_number] = calloc(1, sizeof(mpq_file_s))) == NULL) {
+
+ /* memory allocation problem. */
+ result = LIBMPQ_ERROR_MALLOC;
+ goto error;
+ }
+
+ /* allocate memory for the packed block offset table. */
+ if ((mpq_archive->mpq_file[file_number]->packed_offset = calloc(1, packed_size)) == NULL) {
+
+ /* memory allocation problem. */
+ result = LIBMPQ_ERROR_MALLOC;
+ goto error;
+ }
+
+ /* initialize counter to one opening */
+ mpq_archive->mpq_file[file_number]->open_count = 1;
+
+ /* check if we need to load the packed block offset table, we will maintain this table for unpacked files too. */
+ if ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_COMPRESSED) != 0 &&
+ (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) == 0) {
+
+ /* seek to block position. */
+ if (fseeko(mpq_archive->fp, mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].offset + (((long long)mpq_archive->mpq_block_ex[mpq_archive->mpq_map[file_number].block_table_indices].offset_high) << 32) + mpq_archive->archive_offset, SEEK_SET) < 0) {
+
+ /* seek in file failed. */
+ result = LIBMPQ_ERROR_SEEK;
+ goto error;
+ }
+
+ /* read block positions from begin of file. */
+ if ((rb = fread(mpq_archive->mpq_file[file_number]->packed_offset, 1, packed_size, mpq_archive->fp)) < 0) {
+
+ /* something on read from archive failed. */
+ result = LIBMPQ_ERROR_READ;
+ goto error;
+ }
+
+ /* check if the archive is protected some way, sometimes the file appears not to be encrypted, but it is. */
+ if (mpq_archive->mpq_file[file_number]->packed_offset[0] != rb) {
+
+ /* file is encrypted. */
+ mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags |= LIBMPQ_FLAG_ENCRYPTED;
+ }
+
+ /* check if packed offset block is encrypted, we have to decrypt it. */
+ if (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_ENCRYPTED) {
+
+ /* check if we don't know the file seed, try to find it. */
+ if ((mpq_archive->mpq_file[file_number]->seed = libmpq__decrypt_key((uint8_t *)mpq_archive->mpq_file[file_number]->packed_offset, packed_size, mpq_archive->block_size)) < 0) {
+
+ /* sorry without seed, we cannot extract file. */
+ result = LIBMPQ_ERROR_DECRYPT;
+ goto error;
+ }
+
+ /* decrypt block in input buffer. */
+ if (libmpq__decrypt_block(mpq_archive->mpq_file[file_number]->packed_offset, packed_size, mpq_archive->mpq_file[file_number]->seed - 1) < 0 ) {
+
+ /* something on decrypt failed. */
+ result = LIBMPQ_ERROR_DECRYPT;
+ goto error;
+ }
+
+ /* check if the block positions are correctly decrypted. */
+ if (mpq_archive->mpq_file[file_number]->packed_offset[0] != packed_size) {
+
+ /* sorry without seed, we cannot extract file. */
+ result = LIBMPQ_ERROR_DECRYPT;
+ goto error;
+ }
+ }
+ } else {
+
+ /* check if file is not stored in a single sector. */
+ if ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) == 0) {
+
+ /* loop through all blocks and create packed block offset table based on block size. */
+ for (i = 0; i < ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size + 1); i++) {
+
+ /* check if we process the last block. */
+ if (i == ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size)) {
+
+ /* store size of last block. */
+ mpq_archive->mpq_file[file_number]->packed_offset[i] = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size;
+ } else {
+
+ /* store default block size. */
+ mpq_archive->mpq_file[file_number]->packed_offset[i] = i * mpq_archive->block_size;
+ }
+ }
+ } else {
+
+ /* store offsets. */
+ mpq_archive->mpq_file[file_number]->packed_offset[0] = 0;
+ mpq_archive->mpq_file[file_number]->packed_offset[1] = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].packed_size;
+ }
+ }
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+
+error:
+
+ /* free packed block offset table and file pointer. */
+ free(mpq_archive->mpq_file[file_number]->packed_offset);
+ free(mpq_archive->mpq_file[file_number]);
+
+ /* return error constant. */
+ return result;
+}
+
+/* this function free the file pointer to the opened file in archive. */
+int32_t libmpq__block_close_offset(mpq_archive_s *mpq_archive, uint32_t file_number) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ if (mpq_archive->mpq_file[file_number] == NULL) {
+
+ /* packed block offset table is not opened. */
+ return LIBMPQ_ERROR_OPEN;
+ }
+
+ mpq_archive->mpq_file[file_number]->open_count--;
+
+ if (mpq_archive->mpq_file[file_number]->open_count != 0) {
+
+ /* still in use */
+ return LIBMPQ_SUCCESS;
+ }
+
+ /* free packed block offset table and file pointer. */
+ free(mpq_archive->mpq_file[file_number]->packed_offset);
+ free(mpq_archive->mpq_file[file_number]);
+
+ /* mark it as unopened - libmpq__block_open_offset checks for this to decide whether to increment the counter */
+ mpq_archive->mpq_file[file_number] = NULL;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the unpacked size of the given file and block in the archive. */
+int32_t libmpq__block_unpacked_size(mpq_archive_s *mpq_archive, uint32_t file_number, uint32_t block_number, libmpq__off_t *unpacked_size) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* check if given block number is not out of range. */
+ if (block_number < 0 || block_number >= ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) != 0 ? 1 : (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size)) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* check if packed block offset table is opened. */
+ if (mpq_archive->mpq_file[file_number] == NULL ||
+ mpq_archive->mpq_file[file_number]->packed_offset == NULL) {
+
+ /* packed block offset table is not opened. */
+ return LIBMPQ_ERROR_OPEN;
+ }
+
+ /* check if block is stored as single sector. */
+ if ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) != 0) {
+
+ /* return the unpacked size of the block in the mpq archive. */
+ *unpacked_size = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size;
+ }
+
+ /* check if block is not stored as single sector. */
+ if ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) == 0) {
+
+ /* check if we not process the last block. */
+ if (block_number < ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size) - 1) {
+
+ /* return the block size as unpacked size. */
+ *unpacked_size = mpq_archive->block_size;
+ } else {
+
+ /* return the unpacked size of the last block in the mpq archive. */
+ *unpacked_size = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size - mpq_archive->block_size * block_number;
+ }
+ }
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function return the decryption seed for the given file and block. */
+int32_t libmpq__block_seed(mpq_archive_s *mpq_archive, uint32_t file_number, uint32_t block_number, uint32_t *seed) {
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* check if given block number is not out of range. */
+ if (block_number < 0 || block_number >= ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) != 0 ? 1 : (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size)) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* check if packed block offset table is opened. */
+ if (mpq_archive->mpq_file[file_number] == NULL ||
+ mpq_archive->mpq_file[file_number]->packed_offset == NULL) {
+
+ /* packed block offset table is not opened. */
+ return LIBMPQ_ERROR_OPEN;
+ }
+
+ /* return the decryption key. */
+ *seed = mpq_archive->mpq_file[file_number]->seed + block_number;
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
+
+/* this function read the given block from archive into a buffer. */
+int32_t libmpq__block_read(mpq_archive_s *mpq_archive, uint32_t file_number, uint32_t block_number, uint8_t *out_buf, libmpq__off_t out_size, libmpq__off_t *transferred) {
+
+ /* some common variables. */
+ uint8_t *in_buf;
+ uint32_t seed = 0;
+ uint32_t encrypted = 0;
+ uint32_t compressed = 0;
+ uint32_t imploded = 0;
+ int32_t tb = 0;
+ libmpq__off_t block_offset = 0;
+ off_t in_size = 0;
+ libmpq__off_t unpacked_size = 0;
+
+ /* check if given file number is not out of range. */
+ if (file_number < 0 || file_number > mpq_archive->files - 1) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* check if given block number is not out of range. */
+ if (block_number < 0 || block_number >= ((mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].flags & LIBMPQ_FLAG_SINGLE) != 0 ? 1 : (mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].unpacked_size + mpq_archive->block_size - 1) / mpq_archive->block_size)) {
+
+ /* file number is out of range. */
+ return LIBMPQ_ERROR_EXIST;
+ }
+
+ /* check if packed block offset table is opened. */
+ if (mpq_archive->mpq_file[file_number] == NULL ||
+ mpq_archive->mpq_file[file_number]->packed_offset == NULL) {
+
+ /* packed block offset table is not opened. */
+ return LIBMPQ_ERROR_OPEN;
+ }
+
+ /* get target size of block. */
+ libmpq__block_unpacked_size(mpq_archive, file_number, block_number, &unpacked_size);
+
+ /* check if target buffer is to small. */
+ if (unpacked_size > out_size) {
+
+ /* output buffer size is to small or block size is unknown. */
+ return LIBMPQ_ERROR_SIZE;
+ }
+
+ /* fetch some required values like input buffer size and block offset. */
+ block_offset = mpq_archive->mpq_block[mpq_archive->mpq_map[file_number].block_table_indices].offset + (((long long)mpq_archive->mpq_block_ex[mpq_archive->mpq_map[file_number].block_table_indices].offset_high) << 32) + mpq_archive->mpq_file[file_number]->packed_offset[block_number];
+ in_size = mpq_archive->mpq_file[file_number]->packed_offset[block_number + 1] - mpq_archive->mpq_file[file_number]->packed_offset[block_number];
+
+ /* seek in file. */
+ if (fseeko(mpq_archive->fp, block_offset + mpq_archive->archive_offset, SEEK_SET) < 0) {
+
+ /* something with seek in file failed. */
+ return LIBMPQ_ERROR_SEEK;
+ }
+
+ /* allocate memory for the read buffer. */
+ if ((in_buf = calloc(1, in_size)) == NULL) {
+
+ /* memory allocation problem. */
+ return LIBMPQ_ERROR_MALLOC;
+ }
+
+ /* read block from file. */
+ if (fread(in_buf, 1, in_size, mpq_archive->fp) < 0) {
+
+ /* free buffers. */
+ free(in_buf);
+
+ /* something on reading block failed. */
+ return LIBMPQ_ERROR_READ;
+ }
+
+ /* get encryption status. */
+ libmpq__file_encrypted(mpq_archive, file_number, &encrypted);
+
+ /* check if file is encrypted. */
+ if (encrypted == 1) {
+
+ /* get decryption key. */
+ libmpq__block_seed(mpq_archive, file_number, block_number, &seed);
+
+ /* decrypt block. */
+ if (libmpq__decrypt_block((uint32_t *)in_buf, in_size, seed) < 0) {
+
+ /* free buffers. */
+ free(in_buf);
+
+ /* something on decrypting block failed. */
+ return LIBMPQ_ERROR_DECRYPT;
+ }
+ }
+
+ /* get compression status. */
+ libmpq__file_compressed(mpq_archive, file_number, &compressed);
+
+ /* check if file is compressed. */
+ if (compressed == 1) {
+
+ /* decompress block. */
+ if ((tb = libmpq__decompress_block(in_buf, in_size, out_buf, out_size, LIBMPQ_FLAG_COMPRESS_MULTI)) < 0) {
+
+ /* free temporary buffer. */
+ free(in_buf);
+
+ /* something on decompressing block failed. */
+ return LIBMPQ_ERROR_UNPACK;
+ }
+ }
+
+ /* get implosion status. */
+ libmpq__file_imploded(mpq_archive, file_number, &imploded);
+
+ /* check if file is imploded. */
+ if (imploded == 1) {
+
+ /* explode block. */
+ if ((tb = libmpq__decompress_block(in_buf, in_size, out_buf, out_size, LIBMPQ_FLAG_COMPRESS_PKZIP)) < 0) {
+
+ /* free temporary buffer. */
+ free(in_buf);
+
+ /* something on decompressing block failed. */
+ return LIBMPQ_ERROR_UNPACK;
+ }
+ }
+
+ /* check if file is neither compressed nor imploded. */
+ if (compressed == 0 && imploded == 0) {
+
+ /* copy block. */
+ if ((tb = libmpq__decompress_block(in_buf, in_size, out_buf, out_size, LIBMPQ_FLAG_COMPRESS_NONE)) < 0) {
+
+ /* free temporary buffer. */
+ free(in_buf);
+
+ /* something on decompressing block failed. */
+ return LIBMPQ_ERROR_UNPACK;
+ }
+ }
+
+ /* free read buffer. */
+ free(in_buf);
+
+ /* check for null pointer. */
+ if (transferred != NULL) {
+
+ /* store transferred bytes. */
+ *transferred = tb;
+ }
+
+ /* if no error was found, return zero. */
+ return LIBMPQ_SUCCESS;
+}
diff --git a/dep/src/libmpq/wave.c b/dep/src/libmpq/wave.c
new file mode 100644
index 00000000000..4f2b73ba27e
--- /dev/null
+++ b/dep/src/libmpq/wave.c
@@ -0,0 +1,250 @@
+/*
+ * wave.c -- this file contains decompression methods used by mpq-tools
+ * to decompress wave files.
+ *
+ * Copyright (c) 2003-2007 Maik Broemme <mbroemme@plusserver.de>
+ *
+ * This source was adepted from the C++ version of wave.cpp included
+ * in stormlib. The C++ version belongs to the following authors:
+ *
+ * Ladislav Zezula <ladik@zezula.net>
+ * Tom Amigo <tomamigo@apexmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/* generic includes. */
+#include <stdint.h>
+
+/* libmpq generic includes. */
+#include "wave.h"
+
+/* table necessary dor decompression. */
+static const uint32_t wave_table_1503f120[] = {
+ 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF, 0x00000004, 0xFFFFFFFF, 0x00000002, 0xFFFFFFFF, 0x00000006,
+ 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0x00000005, 0xFFFFFFFF, 0x00000003, 0xFFFFFFFF, 0x00000007,
+ 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF, 0x00000005, 0xFFFFFFFF, 0x00000003, 0xFFFFFFFF, 0x00000007,
+ 0xFFFFFFFF, 0x00000002, 0xFFFFFFFF, 0x00000004, 0xFFFFFFFF, 0x00000006, 0xFFFFFFFF, 0x00000008
+};
+
+/* table necessary dor decompression. */
+static const uint32_t wave_table_1503f1a0[] = {
+ 0x00000007, 0x00000008, 0x00000009, 0x0000000A, 0x0000000B, 0x0000000C, 0x0000000D, 0x0000000E,
+ 0x00000010, 0x00000011, 0x00000013, 0x00000015, 0x00000017, 0x00000019, 0x0000001C, 0x0000001F,
+ 0x00000022, 0x00000025, 0x00000029, 0x0000002D, 0x00000032, 0x00000037, 0x0000003C, 0x00000042,
+ 0x00000049, 0x00000050, 0x00000058, 0x00000061, 0x0000006B, 0x00000076, 0x00000082, 0x0000008F,
+ 0x0000009D, 0x000000AD, 0x000000BE, 0x000000D1, 0x000000E6, 0x000000FD, 0x00000117, 0x00000133,
+ 0x00000151, 0x00000173, 0x00000198, 0x000001C1, 0x000001EE, 0x00000220, 0x00000256, 0x00000292,
+ 0x000002D4, 0x0000031C, 0x0000036C, 0x000003C3, 0x00000424, 0x0000048E, 0x00000502, 0x00000583,
+ 0x00000610, 0x000006AB, 0x00000756, 0x00000812, 0x000008E0, 0x000009C3, 0x00000ABD, 0x00000BD0,
+ 0x00000CFF, 0x00000E4C, 0x00000FBA, 0x0000114C, 0x00001307, 0x000014EE, 0x00001706, 0x00001954,
+ 0x00001BDC, 0x00001EA5, 0x000021B6, 0x00002515, 0x000028CA, 0x00002CDF, 0x0000315B, 0x0000364B,
+ 0x00003BB9, 0x000041B2, 0x00004844, 0x00004F7E, 0x00005771, 0x0000602F, 0x000069CE, 0x00007462,
+ 0x00007FFF
+};
+
+/* this function decompress a wave file, mono or stereo, 1500F230 offset. */
+int32_t libmpq__do_decompress_wave(uint8_t *out_buf, int32_t out_length, uint8_t *in_buf, int32_t in_length, int32_t channels) {
+
+ /* some common variables. */
+ byte_and_int16_t out;
+ byte_and_int16_t in;
+ uint32_t index;
+ int32_t nr_array1[2];
+ int32_t nr_array2[2];
+ uint32_t count = 0;
+
+ /* end on input buffer. */
+ uint8_t *in_end = in_buf + in_length;
+
+ /* assign default values. */
+ out.pb = out_buf;
+ in.pb = in_buf;
+ nr_array1[0] = 0x2C;
+ nr_array1[1] = 0x2C;
+
+ /* increase. */
+ in.pw++;
+
+ /* 15007AD7 */
+ for (count = 0; count < channels; count++) {
+
+ /* some common variables. */
+ int32_t temp;
+
+ /* save pointer. */
+ temp = *(int16_t *)in.pw++;
+ nr_array2[count] = temp;
+
+ /* check if should break. */
+ if (out_length < 2) {
+ return out.pb - out_buf;
+ }
+
+ /* return values. */
+ *out.pw++ = (uint16_t)temp;
+ out_length -= 2;
+ }
+
+ /* decrease channels. */
+ index = channels - 1;
+
+ /* loop through input buffer until end reached. */
+ while (in.pb < in_end) {
+
+ /* save the byte. */
+ uint8_t one_byte = *in.pb++;
+
+ /* check how many channels and set index. */
+ if (channels == 2) {
+ index = (index == 0) ? 1 : 0;
+ }
+
+ /* 15007B25 - get one byte from input buffer. */
+ if (one_byte & 0x80) {
+
+ /* 15007B32 */
+ switch (one_byte & 0x7F) {
+ case 0:
+
+ /* 15007B8E */
+ if (nr_array1[index] != 0) {
+ nr_array1[index]--;
+ }
+
+ /* check if should break. */
+ if (out_length < 2) {
+ break;
+ }
+
+ /* return values. */
+ *out.pw++ = (uint16_t)nr_array2[index];
+ out_length -= 2;
+
+ /* continue loop. */
+ continue;
+ case 1:
+ /* 15007B72 and EBX. */
+ nr_array1[index] += 8;
+
+ /* check index. */
+ if (nr_array1[index] > 0x58) {
+ nr_array1[index] = 0x58;
+ }
+
+ /* check how many channels and set index. */
+ if (channels == 2) {
+ index = (index == 0) ? 1 : 0;
+ }
+
+ /* continue loop. */
+ continue;
+ case 2:
+
+ /* nothing todo, so continue. */
+ continue;
+ default:
+
+ /* decrease index. */
+ nr_array1[index] -= 8;
+
+ /* check index. */
+ if (nr_array1[index] < 0) {
+ nr_array1[index] = 0;
+ }
+
+ /* check if two channels left. */
+ if (channels != 2) {
+ continue;
+ }
+ index = (index == 0) ? 1 : 0;
+
+ /* continue loop. */
+ continue;
+ }
+ } else {
+
+ /* EDI */
+ uint32_t temp1 = wave_table_1503f1a0[nr_array1[index]];
+
+ /* ESI */
+ uint32_t temp2 = temp1 >> in_buf[1];
+
+ /* ECX */
+ int32_t temp3 = nr_array2[index];
+
+ /* EBX = one byte. */
+ if (one_byte & 0x01) {
+ temp2 += (temp1 >> 0);
+ }
+ if (one_byte & 0x02) {
+ temp2 += (temp1 >> 1);
+ }
+ if (one_byte & 0x04) {
+ temp2 += (temp1 >> 2);
+ }
+ if (one_byte & 0x08) {
+ temp2 += (temp1 >> 3);
+ }
+ if (one_byte & 0x10) {
+ temp2 += (temp1 >> 4);
+ }
+ if (one_byte & 0x20) {
+ temp2 += (temp1 >> 5);
+ }
+ if (one_byte & 0x40) {
+ temp3 -= temp2;
+ if (temp3 <= (int32_t)0xFFFF8000) {
+ temp3 = (int32_t)0xFFFF8000;
+ }
+ } else {
+ temp3 += temp2;
+ if (temp3 >= 0x7FFF) {
+ temp3 = 0x7FFF;
+ }
+ }
+
+ /* restore index. */
+ nr_array2[index] = temp3;
+
+ /* check if should break. */
+ if (out_length < 2) {
+ break;
+ }
+
+ /* assign values. */
+ temp2 = nr_array1[index];
+ one_byte &= 0x1F;
+ *out.pw++ = (uint16_t)temp3;
+ out_length -= 2;
+ temp2 += wave_table_1503f120[one_byte];
+ nr_array1[index] = temp2;
+
+ /* check index. */
+ if (nr_array1[index] < 0) {
+ nr_array1[index] = 0;
+ } else {
+
+ /* check index. */
+ if (nr_array1[index] > 0x58) {
+ nr_array1[index] = 0x58;
+ }
+ }
+ }
+ }
+
+ /* return copied bytes. */
+ return (out.pb - out_buf);
+}