From 1385b5811b5b3cffb9bed8e7fb59494ecbb77d8e Mon Sep 17 00:00:00 2001 From: drof <40671101+cryotb@users.noreply.github.com> Date: Sat, 21 Sep 2024 04:49:43 +0200 Subject: [PATCH] Add the loader component. --- loader/build.sh | 29 + loader/setup.sh | 102 + loader/src/entry.cpp | 13 + loader/src/hde.cpp | 336 ++ loader/src/hdepp.cpp | 68 + loader/src/hwid.cpp | 238 + loader/src/inc/console.h | 47 + loader/src/inc/defs.h | 75 + loader/src/inc/elfimage.h | 152 + loader/src/inc/hde.h | 11 + loader/src/inc/hde64.h | 94 + loader/src/inc/hwid.h | 33 + loader/src/inc/include.h | 196 + loader/src/inc/integrity.h | 24 + loader/src/inc/libpmap.h | 73 + loader/src/inc/loader.h | 16 + loader/src/inc/log.h | 20 + loader/src/inc/lzmapp.h | 15 + loader/src/inc/mapper.h | 132 + loader/src/inc/memory.h | 108 + loader/src/inc/mm.h | 64 + loader/src/inc/murmur.h | 45 + loader/src/inc/netsdk.h | 37 + loader/src/inc/ntos.h | 150 + loader/src/inc/offsets.h | 110 + loader/src/inc/packet-handlers.h | 33 + loader/src/inc/pattern.h | 120 + loader/src/inc/pe.h | 187 + loader/src/inc/pocketlzma.h | 7498 ++++++++++++++++++++++++++++++ loader/src/inc/pump.h | 46 + loader/src/inc/respawn.h | 6 + loader/src/inc/rotate.h | 84 + loader/src/inc/sockets-wrapper.h | 98 + loader/src/inc/string-table.h | 30 + loader/src/inc/table64.h | 69 + loader/src/inc/tools.h | 69 + loader/src/inc/vmt.h | 48 + loader/src/inc/wptrace.h | 125 + loader/src/inc/xorstr.h | 4 + loader/src/integrity.cpp | 253 + loader/src/libpmap.cpp | 147 + loader/src/loader.cpp | 349 ++ loader/src/log.cpp | 28 + loader/src/lzmapp.cpp | 43 + loader/src/mapper.cpp | 1008 ++++ loader/src/mm.cpp | 87 + loader/src/netsdk.cpp | 250 + loader/src/packet-handlers.cpp | 89 + loader/src/pe.cpp | 194 + loader/src/pump.cpp | 92 + loader/src/respawn.cpp | 229 + loader/src/string-table.cpp | 85 + loader/src/tools.cpp | 207 + loader/src/vmt.cpp | 97 + loader/src/wptrace.cpp | 88 + 55 files changed, 13851 insertions(+) create mode 100644 loader/build.sh create mode 100644 loader/setup.sh create mode 100644 loader/src/entry.cpp create mode 100644 loader/src/hde.cpp create mode 100644 loader/src/hdepp.cpp create mode 100644 loader/src/hwid.cpp create mode 100644 loader/src/inc/console.h create mode 100644 loader/src/inc/defs.h create mode 100644 loader/src/inc/elfimage.h create mode 100644 loader/src/inc/hde.h create mode 100644 loader/src/inc/hde64.h create mode 100644 loader/src/inc/hwid.h create mode 100644 loader/src/inc/include.h create mode 100644 loader/src/inc/integrity.h create mode 100644 loader/src/inc/libpmap.h create mode 100644 loader/src/inc/loader.h create mode 100644 loader/src/inc/log.h create mode 100644 loader/src/inc/lzmapp.h create mode 100644 loader/src/inc/mapper.h create mode 100644 loader/src/inc/memory.h create mode 100644 loader/src/inc/mm.h create mode 100644 loader/src/inc/murmur.h create mode 100644 loader/src/inc/netsdk.h create mode 100644 loader/src/inc/ntos.h create mode 100644 loader/src/inc/offsets.h create mode 100644 loader/src/inc/packet-handlers.h create mode 100644 loader/src/inc/pattern.h create mode 100644 loader/src/inc/pe.h create mode 100644 loader/src/inc/pocketlzma.h create mode 100644 loader/src/inc/pump.h create mode 100644 loader/src/inc/respawn.h create mode 100644 loader/src/inc/rotate.h create mode 100644 loader/src/inc/sockets-wrapper.h create mode 100644 loader/src/inc/string-table.h create mode 100644 loader/src/inc/table64.h create mode 100644 loader/src/inc/tools.h create mode 100644 loader/src/inc/vmt.h create mode 100644 loader/src/inc/wptrace.h create mode 100644 loader/src/inc/xorstr.h create mode 100644 loader/src/integrity.cpp create mode 100644 loader/src/libpmap.cpp create mode 100644 loader/src/loader.cpp create mode 100644 loader/src/log.cpp create mode 100644 loader/src/lzmapp.cpp create mode 100644 loader/src/mapper.cpp create mode 100644 loader/src/mm.cpp create mode 100644 loader/src/netsdk.cpp create mode 100644 loader/src/packet-handlers.cpp create mode 100644 loader/src/pe.cpp create mode 100644 loader/src/pump.cpp create mode 100644 loader/src/respawn.cpp create mode 100644 loader/src/string-table.cpp create mode 100644 loader/src/tools.cpp create mode 100644 loader/src/vmt.cpp create mode 100644 loader/src/wptrace.cpp diff --git a/loader/build.sh b/loader/build.sh new file mode 100644 index 0000000..51fc93c --- /dev/null +++ b/loader/build.sh @@ -0,0 +1,29 @@ +rm debug + +if [ -n "$RANDOM_NUMBER_OVERRIDE" ]; then + random_number="$RANDOM_NUMBER_OVERRIDE" +else + random_number=$(hexdump -n 16 -e '16/1 "%02X" "\n"' /dev/urandom) +fi + +loader_user_email="$LOADER_USER_EMAIL" +loader_user_access_token="$LOADER_USER_ACCESS_TOKEN" +build_timestamp=$(date +%Y-%m-%d_%H-%M-%S) + +echo "SEED: $random_number" +echo "LOADER USER EMAIL: $loader_user_email" +echo "LOADER USER ACCESS TOKEN: $loader_user_access_token" +echo "BUILDING ON $build_timestamp" + +/src/obfuscator-drause-llvm11/build/bin/clang++ \ +-m64 -std=c++17 src/*.cpp -lm -O0 -no-pie -Wno-multichar -s -fno-rtti -Wl,-allow-multiple-definition \ +-lssl -lcrypto -lelf -l:libsodium.a -llzma \ +-msse4.2 -o debug \ +-DR5I_PROD \ +-mllvm -aesSeed="$random_number" -mllvm -csobf -mllvm -epobf -mllvm -sub -mllvm -sub_loop=3 \ +-D_LOADER_COMPILATION_AES_KEY="$random_number" \ +-D_LOADER_USER_EMAIL="$loader_user_email" \ +-D_LOADER_USER_ACCESS_TOKEN="$loader_user_access_token" \ +-D_BUILD_TIMESTAMP=$build_timestamp \ +-I/home/drof/CLionProjects/ -Iprojects/ -I/src/obfuscator-drause-llvm11/build/lib/clang/11.0.0/include/ +/src/obfuscator-drause-llvm11/build/bin/llvm-strip debug \ No newline at end of file diff --git a/loader/setup.sh b/loader/setup.sh new file mode 100644 index 0000000..66508c5 --- /dev/null +++ b/loader/setup.sh @@ -0,0 +1,102 @@ +echo "[SETUP] ------( CLEANUP )------" +rm -rf /src/hiredis/ +rm -rf /src/redis-plus-plus/ +rm -rf /src/libsodium-stable/ + +echo "[SETUP] ------( PACKAGE MANAGER EZ DEPENDENCIES )------" +apt install liblzma-dev || exit 1 +apt install libelf-dev || exit 1 +apt install libssl-dev | exit 1 + +echo "[SETUP] (SUCCESS) PACKAGE MANAGER EZ DEPENDENCIES" + +############################################################################## + +echo "[SETUP] ------( HIREDIS )------" +cd /src || exit 1 + +echo "[SETUP] cloning..." +git clone --recursive https://github.com/redis/hiredis +if [ $? -ne 0 ]; then + echo "[SETUP] (ERROR) cloning failed. aborting!" + exit 1 +fi + +cd hiredis ||exit 1 + +echo "[SETUP] making..." +make -j 6 +if [ $? -ne 0 ]; then + echo "[SETUP] (error) make failed. Aborting." + exit 1 +fi + + +echo "[SETUP] installing..." +make install +if [ $? -ne 0 ]; then + echo "[SETUP] (error) installation failed. Aborting." + exit 1 +fi + +echo "[SETUP] (SUCCESS) HIREDIS"; +cd .. || exit 1 + +############################################################################# + +echo "[SETUP] ------( REDIS++ )------"; + +git clone https://github.com/sewenew/redis-plus-plus.git +if [ $? -ne 0 ]; then + echo "[SETUP] (ERROR) cloning failed. aborting!" + exit 1 +fi + +cd redis-plus-plus || exit 1 + +mkdir build || exit 1 +cd build || exit 1 +cmake .. +if [ $? -ne 0 ]; then + echo "[SETUP] (ERROR) cmake call has failed. aborting!" + exit 1 +fi + +make -j 6 +if [ $? -ne 0 ]; then + echo "[SETUP] (error) make failed. Aborting." + exit 1 +fi + +make install +if [ $? -ne 0 ]; then + echo "[SETUP] (error) installation failed. Aborting." + exit 1 +fi + +cd .. || exit 1 + +echo "[SETUP] (SUCCESS) REDIS++"; +cd .. || exit 1 + +############################################################################# + +echo "[SETUP] ------( LIBSODIUM )------" + +wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz || exit 1 +tar -xzf libsodium-1.0.18-stable.tar.gz || exit 1 +rm libsodium-1.0.18-stable.tar.gz || exit 1 +cd libsodium-stable || exit 1 +./configure +make -j 6 + +make check + +make install + +echo "[SETUP] (SUCCESS) LIBSODIUM" +cd .. + +############################################################################# + +echo "[SETUP] done." diff --git a/loader/src/entry.cpp b/loader/src/entry.cpp new file mode 100644 index 0000000..c57eda1 --- /dev/null +++ b/loader/src/entry.cpp @@ -0,0 +1,13 @@ +#include "inc/include.h" + +Context *gctx = nullptr; + +int main(int argc, char *argv[]) +{ + msg("<------ DrauseHook ------>"); + + gLoader = new Loader(); + gLoader->start(argc, argv); + + return 0; +} diff --git a/loader/src/hde.cpp b/loader/src/hde.cpp new file mode 100644 index 0000000..717a9b2 --- /dev/null +++ b/loader/src/hde.cpp @@ -0,0 +1,336 @@ +#include "inc/include.h" + +unsigned int hde64_disasm(const void* code, hde64s* hs) +{ + uint8_t x, c, * p = (uint8_t*)code, cflags, opcode, pref = 0; + uint8_t* ht = hde64_table, m_mod, m_reg, m_rm, disp_size = 0; + uint8_t op64 = 0; + + memset(hs, 0, sizeof(hde64s)); + + for (x = 16; x; x--) + switch (c = *p++) { + case 0xf3: + hs->p_rep = c; + pref |= PRE_F3; + break; + case 0xf2: + hs->p_rep = c; + pref |= PRE_F2; + break; + case 0xf0: + hs->p_lock = c; + pref |= PRE_LOCK; + break; + case 0x26: case 0x2e: case 0x36: + case 0x3e: case 0x64: case 0x65: + hs->p_seg = c; + pref |= PRE_SEG; + break; + case 0x66: + hs->p_66 = c; + pref |= PRE_66; + break; + case 0x67: + hs->p_67 = c; + pref |= PRE_67; + break; + default: + goto pref_done; + } +pref_done: + + hs->flags = (uint32_t)pref << 23; + + if (!pref) + pref |= PRE_NONE; + + if ((c & 0xf0) == 0x40) { + hs->flags |= F_PREFIX_REX; + if ((hs->rex_w = (c & 0xf) >> 3) && (*p & 0xf8) == 0xb8) + op64++; + hs->rex_r = (c & 7) >> 2; + hs->rex_x = (c & 3) >> 1; + hs->rex_b = c & 1; + if (((c = *p++) & 0xf0) == 0x40) { + opcode = c; + goto error_opcode; + } + } + + if ((hs->opcode = c) == 0x0f) { + hs->opcode2 = c = *p++; + ht += DELTA_OPCODES; + } + else if (c >= 0xa0 && c <= 0xa3) { + op64++; + if (pref & PRE_67) + pref |= PRE_66; + else + pref &= ~PRE_66; + } + + opcode = c; + cflags = ht[ht[opcode / 4] + (opcode % 4)]; + + if (cflags == C_ERROR) { + error_opcode: + hs->flags |= F_ERROR | F_ERROR_OPCODE; + cflags = 0; + if ((opcode & -3) == 0x24) + cflags++; + } + + x = 0; + if (cflags & C_GROUP) { + uint16_t t; + t = *(uint16_t*)(ht + (cflags & 0x7f)); + cflags = (uint8_t)t; + x = (uint8_t)(t >> 8); + } + + if (hs->opcode2) { + ht = hde64_table + DELTA_PREFIXES; + if (ht[ht[opcode / 4] + (opcode % 4)] & pref) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (cflags & C_MODRM) { + hs->flags |= F_MODRM; + hs->modrm = c = *p++; + hs->modrm_mod = m_mod = c >> 6; + hs->modrm_rm = m_rm = c & 7; + hs->modrm_reg = m_reg = (c & 0x3f) >> 3; + + if (x && ((x << m_reg) & 0x80)) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + + if (!hs->opcode2 && opcode >= 0xd9 && opcode <= 0xdf) { + uint8_t t = opcode - 0xd9; + if (m_mod == 3) { + ht = hde64_table + DELTA_FPU_MODRM + t * 8; + t = ht[m_reg] << m_rm; + } + else { + ht = hde64_table + DELTA_FPU_REG; + t = ht[t] << m_reg; + } + if (t & 0x80) + hs->flags |= F_ERROR | F_ERROR_OPCODE; + } + + if (pref & PRE_LOCK) { + if (m_mod == 3) { + hs->flags |= F_ERROR | F_ERROR_LOCK; + } + else { + uint8_t* table_end, op = opcode; + if (hs->opcode2) { + ht = hde64_table + DELTA_OP2_LOCK_OK; + table_end = ht + DELTA_OP_ONLY_MEM - DELTA_OP2_LOCK_OK; + } + else { + ht = hde64_table + DELTA_OP_LOCK_OK; + table_end = ht + DELTA_OP2_LOCK_OK - DELTA_OP_LOCK_OK; + op &= -2; + } + for (; ht != table_end; ht++) + if (*ht++ == op) { + if (!((*ht << m_reg) & 0x80)) + goto no_lock_error; + else + break; + } + hs->flags |= F_ERROR | F_ERROR_LOCK; + no_lock_error: + ; + } + } + + if (hs->opcode2) { + switch (opcode) { + case 0x20: case 0x22: + m_mod = 3; + if (m_reg > 4 || m_reg == 1) + goto error_operand; + else + goto no_error_operand; + case 0x21: case 0x23: + m_mod = 3; + if (m_reg == 4 || m_reg == 5) + goto error_operand; + else + goto no_error_operand; + } + } + else { + switch (opcode) { + case 0x8c: + if (m_reg > 5) + goto error_operand; + else + goto no_error_operand; + case 0x8e: + if (m_reg == 1 || m_reg > 5) + goto error_operand; + else + goto no_error_operand; + } + } + + if (m_mod == 3) { + uint8_t* table_end; + if (hs->opcode2) { + ht = hde64_table + DELTA_OP2_ONLY_MEM; + table_end = ht + sizeof(hde64_table) - DELTA_OP2_ONLY_MEM; + } + else { + ht = hde64_table + DELTA_OP_ONLY_MEM; + table_end = ht + DELTA_OP2_ONLY_MEM - DELTA_OP_ONLY_MEM; + } + for (; ht != table_end; ht += 2) + if (*ht++ == opcode) { + if (*ht++ & pref && !((*ht << m_reg) & 0x80)) + goto error_operand; + else + break; + } + goto no_error_operand; + } + else if (hs->opcode2) { + switch (opcode) { + case 0x50: case 0xd7: case 0xf7: + if (pref & (PRE_NONE | PRE_66)) + goto error_operand; + break; + case 0xd6: + if (pref & (PRE_F2 | PRE_F3)) + goto error_operand; + break; + case 0xc5: + goto error_operand; + } + goto no_error_operand; + } + else + goto no_error_operand; + + error_operand: + hs->flags |= F_ERROR | F_ERROR_OPERAND; + no_error_operand: + + c = *p++; + if (m_reg <= 1) { + if (opcode == 0xf6) + cflags |= C_IMM8; + else if (opcode == 0xf7) + cflags |= C_IMM_P66; + } + + switch (m_mod) { + case 0: + if (pref & PRE_67) { + if (m_rm == 6) + disp_size = 2; + } + else + if (m_rm == 5) + disp_size = 4; + break; + case 1: + disp_size = 1; + break; + case 2: + disp_size = 2; + if (!(pref & PRE_67)) + disp_size <<= 1; + } + + if (m_mod != 3 && m_rm == 4) { + hs->flags |= F_SIB; + p++; + hs->sib = c; + hs->sib_scale = c >> 6; + hs->sib_index = (c & 0x3f) >> 3; + if ((hs->sib_base = c & 7) == 5 && !(m_mod & 1)) + disp_size = 4; + } + + p--; + switch (disp_size) { + case 1: + hs->flags |= F_DISP8; + hs->disp.disp8 = *p; + break; + case 2: + hs->flags |= F_DISP16; + hs->disp.disp16 = *(uint16_t*)p; + break; + case 4: + hs->flags |= F_DISP32; + hs->disp.disp32 = *(uint32_t*)p; + } + p += disp_size; + } + else if (pref & PRE_LOCK) + hs->flags |= F_ERROR | F_ERROR_LOCK; + + if (cflags & C_IMM_P66) { + if (cflags & C_REL32) { + if (pref & PRE_66) { + hs->flags |= F_IMM16 | F_RELATIVE; + hs->imm.imm16 = *(uint16_t*)p; + p += 2; + goto disasm_done; + } + goto rel32_ok; + } + if (op64) { + hs->flags |= F_IMM64; + hs->imm.imm64 = *(uint64_t*)p; + p += 8; + } + else if (!(pref & PRE_66)) { + hs->flags |= F_IMM32; + hs->imm.imm32 = *(uint32_t*)p; + p += 4; + } + else + goto imm16_ok; + } + + + if (cflags & C_IMM16) { + imm16_ok: + hs->flags |= F_IMM16; + hs->imm.imm16 = *(uint16_t*)p; + p += 2; + } + if (cflags & C_IMM8) { + hs->flags |= F_IMM8; + hs->imm.imm8 = *p++; + } + + if (cflags & C_REL32) { + rel32_ok: + hs->flags |= F_IMM32 | F_RELATIVE; + hs->imm.imm32 = *(uint32_t*)p; + p += 4; + } + else if (cflags & C_REL8) { + hs->flags |= F_IMM8 | F_RELATIVE; + hs->imm.imm8 = *p++; + } + +disasm_done: + + if ((hs->len = (uint8_t)(p - (uint8_t*)code)) > 15) { + hs->flags |= F_ERROR | F_ERROR_LENGTH; + hs->len = 15; + } + + return (unsigned int)hs->len; +} + + + diff --git a/loader/src/hdepp.cpp b/loader/src/hdepp.cpp new file mode 100644 index 0000000..387e664 --- /dev/null +++ b/loader/src/hdepp.cpp @@ -0,0 +1,68 @@ +#include "inc/include.h" + +namespace hde +{ + size_t disasm_buffer(void *data, size_t length, hde64s *list, size_t list_length) + { + size_t left = length; + size_t count = 0; + size_t offset = 0; + hde64s info{}; + + do + { + if (mem::addr(data).Add(offset).Base() > + mem::addr(data).Add(length).Base()) + break; + + if (!hde64_disasm(mem::addr(data).Add(offset).Ptr(), &info)) + break; + + const auto delta = left - info.len; + + if (static_cast(delta) <= 0) + break; + + memcpy(&list[count], &info, sizeof(hde64s)); + + count++; + left -= info.len; + offset += info.len; + } while (left > 0 && (count + 1) < list_length); + + return count; + } + + std::vector disasm_buffer_ex(void *data, size_t length) + { + auto result = std::vector(); + + size_t left = length; + size_t count = 0; + size_t offset = 0; + hde64s info{}; + + do + { + if (mem::addr(data).Add(offset).Base() > + mem::addr(data).Add(length).Base()) + break; + + if (!hde64_disasm(mem::addr(data).Add(offset).Ptr(), &info)) + break; + + const auto delta = left - info.len; + + if (static_cast(delta) <= 0) + break; + + result.push_back(info); + + count++; + left -= info.len; + offset += info.len; + } while (left > 0); + + return result; + } +} diff --git a/loader/src/hwid.cpp b/loader/src/hwid.cpp new file mode 100644 index 0000000..ca07b97 --- /dev/null +++ b/loader/src/hwid.cpp @@ -0,0 +1,238 @@ +#include "inc/include.h" + +#if defined(R5I_PROD) +Hwid *gHwid = new Hwid(); +#else +Hwid *gHwid = nullptr; +#endif + +class File_Descriptor_Guard +{ +public: + File_Descriptor_Guard() = delete; + File_Descriptor_Guard(int fd) + { + m_fd = fd; + } + ~File_Descriptor_Guard() + { + close(m_fd); + } + +private: + int m_fd; +}; + +Hwid::Hwid() +{ + m_available = collect(); +} + +bool Hwid::collect() +{ + if (!query_all_disks()) + return false; + + if(!query_arp()) + return false; + + if(this->m_vec_device_macs.empty()) + { + error("HWE001"); + return false; + } + + if(this->m_vec_disks.empty()) + { + error("HWE002"); + return false; + } + + return true; +} + +bool Hwid::query_all_disks() +{ + const char *prefix_ssd = "/dev/sd"; + const char* prefix_nvme_device = "/dev/nvme"; + + const char letters[] = + { + 'a', + 'b', + 'c', + 'd', + 'e', + 'f', + }; + + for (int i = 0; i < 6; i++) + { + char fmt[32]; + memset(fmt, 0, sizeof(fmt)); + sprintf(fmt, "%s%c", prefix_ssd, letters[i]); + + hd_driveid identity; + memset(&identity, 0, sizeof(identity)); + + if (!query_disk_identity(fmt, identity)) + { + continue; + } + + Disk_t rec{}; + rec.model_name.resize(sizeof(identity.model)); + rec.serial_number.resize(sizeof(identity.serial_no)); + sprintf(rec.model_name.data(), "%.40s", identity.model); + sprintf(rec.serial_number.data(), "%.20s", identity.serial_no); + m_vec_disks.push_back(rec); + } + + for (int i = 0; i < 6; i++) + { + char fmt[32]; + memset(fmt, 0, sizeof(fmt)); + sprintf(fmt, "%s%in1", prefix_nvme_device, i); + + Disk_t rec{}; + + if (!query_nvme_identity(fmt, rec)) + { + continue; + } + m_vec_disks.push_back(rec); + } + + return true; +} + +bool Hwid::query_arp() +{ + char line[512]; + char ip_address[256]; + int hw_type; + int flags; + char mac_address[64]; + char mask[64]; + char device[64]; + + FILE *fp = fopen("/proc/net/arp", "r"); + if (fp) + { + fgets(line, sizeof(line), fp); + while (fgets(line, sizeof(line), fp)) + { + sscanf(line, "%s 0x%x 0x%x %s %s %s\n", + ip_address, + &hw_type, + &flags, + mac_address, + mask, + device); + + ArpParticipant_t record; + record.device = ip_address; + record.mac_address_permanent = mac_address; + m_vec_device_macs.push_back(record); + } + } else + { + return false; + } + + fclose(fp); + return true; +} + +bool Hwid::query_disk_identity(const char *dev, hd_driveid &buffer) +{ + static struct hd_driveid hd; + int fd; + + if ((fd = open(dev, O_RDONLY | O_NONBLOCK)) < 0) + { + return false; + } + + File_Descriptor_Guard _(fd); + + if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) + { + memcpy(&buffer, &hd, sizeof(buffer)); + } + else + { + return false; + } + + return true; +} + +bool Hwid::query_nvme_identity(const char *dev, Disk_t &buffer) +{ + int fd = open(dev, O_RDWR); + if (fd == 0 || fd < 0) + { + return false; + } + + File_Descriptor_Guard _(fd); + + char buf[4096] = {0}; + struct nvme_admin_cmd mib = {0}; + mib.opcode = 0x06; // identify + mib.nsid = 0; + mib.addr = (__u64)buf; + mib.data_len = sizeof(buf); + mib.cdw10 = 1; // controller + + int ret = ioctl(fd, NVME_IOCTL_ADMIN_CMD, &mib); + if (ret) + { + return false; + } + + buffer.serial_number.resize(21); + snprintf(buffer.serial_number.data(), 21, "%.20s", &buf[4]); + + buffer.model_name.resize(41); + snprintf(buffer.model_name.data(), 41, "%.40s", &buf[24]); + + return true; +} + +#define SMBIOS_START_ADDRESS 0xE0000 // Start address of SMBIOS data in physical memory +#define SMBIOS_END_ADDRESS 0xFFFFF // End address of SMBIOS data in physical memory +#define BUFFER_SIZE 16 // Size of buffer for reading from /dev/mem +std::vector hwid::dump_smbios_data() +{ + std::vector smbios_data; + unsigned char buffer[BUFFER_SIZE]; + + // Open /dev/mem for reading + int fd = open("/dev/mem", O_RDONLY); + if (fd < 0) { + return smbios_data; + } + + // Seek to the start of SMBIOS data + if (lseek(fd, SMBIOS_START_ADDRESS, SEEK_SET) < 0) { + return smbios_data; + } + + // Read and dump SMBIOS data until SMBIOS_END_ADDRESS is reached + while (lseek(fd, 0, SEEK_CUR) < SMBIOS_END_ADDRESS) { + ssize_t bytesRead = read(fd, buffer, BUFFER_SIZE); + if (bytesRead < 0) { + return smbios_data; + } + + // Append the read buffer to the vector + smbios_data.insert(smbios_data.end(), buffer, buffer + bytesRead); + } + + // Close /dev/mem + close(fd); + + return smbios_data; +} diff --git a/loader/src/inc/console.h b/loader/src/inc/console.h new file mode 100644 index 0000000..161301c --- /dev/null +++ b/loader/src/inc/console.h @@ -0,0 +1,47 @@ +#pragma once + +class console_spinner +{ +public: + console_spinner() = delete; + console_spinner(const char* fmt, ...) { + va_list args; + va_start(args, fmt); + + int size = vsnprintf(NULL, 0, fmt, args) + 1; + char* buf = new char[size]; + vsnprintf(buf, size, fmt, args); + va_end(args); + + _text = buf; + delete[] buf; + _active = true; + std::thread(&console_spinner::th_think, this).detach(); + } + + ~console_spinner() { + stop(); + } + + void stop() { + _active = false; + } + + void th_think() { + const char spinner[] = "|/-\\"; + int i = 0; + + while (_active) { + printf(" \r%c %s", spinner[i], _text.c_str()); + fflush(stdout); + i = (i + 1) % 4; + usleep(100000); + } + + printf("\n"); + } + +private: + bool _active; + std::string _text; +}; diff --git a/loader/src/inc/defs.h b/loader/src/inc/defs.h new file mode 100644 index 0000000..346caa5 --- /dev/null +++ b/loader/src/inc/defs.h @@ -0,0 +1,75 @@ +#pragma once + +#define USEC_IN_MS(X) (X * 1000) +#define FORWARD_DECLARE_CLASS(X) class X + +#define BASE_OF(X) ((uint64_t)X) +#define PTR_OF(X) ((void *)X) + +typedef unsigned char u8; +typedef unsigned short u16; +typedef uint32_t u32; +typedef unsigned long u64; +typedef u64 uptr; + +typedef signed char n8; +typedef signed short n16; +typedef signed long n32; +typedef signed long long n64; + +#define ZERO_MEM(dst, len) memset(dst, 0, len) +#define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) + +#define MAX_INSTRUCTION_LEN 15 + +#define OBFUS_FUNC_DISABLE __attribute__((__annotate__(("nobcf")), __annotate__(("nofla")), \ + __annotate__(("nosub")), __annotate__(("nosplit")))) + +#define MAPPER_USE_COMPSTUI + +#if defined(R5I_PROD) +#define CloudLog_Msg(X, ...) gNetSDK->push_message_to_cloud_log(X, ##__VA_ARGS__) +#endif + +inline double bytesToKB(long long bytes) +{ + return static_cast(bytes) / 1024.0; +} + +inline double bytesToMB(long long bytes) +{ + return bytesToKB(bytes) / 1024.0; +} + +__attribute__((always_inline)) inline void terminate_safe() +{ + auto pretaddr = reinterpret_cast(BASE_OF((__builtin_return_address(0)))); + + for (int i = 0; i < 12; i++) + { + *pretaddr = u64{}; + ++pretaddr; + } + + asm volatile( + "xorq %%rax, %%rax\n\t" + "movq %%rax, %%rbx\n\t" + "movq %%rax, %%rdi\n\t" + "movq %%rax, %%rbp\n\t" + "movq %%rax, %%rdx\n\t" + "movq %%rax, %%r8\n\t" + "movq %%rax, %%r9\n\t" + "movq %%rax, %%r10\n\t" + "movq %%rax, %%r11\n\t" + "movq %%rax, %%r12\n\t" + "movq %%rax, %%r13\n\t" + "movq %%rax, %%r14\n\t" + "movq %%rax, %%r15\n\t" + "jmpq *%%rax" + : + : + : "%rax", "%rbx", "%rdi", "%rbp", "%rdx", "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15"); +} + +#define QUOTE(str) #str +#define STRINGIFIED_DEF(str) QUOTE(str) diff --git a/loader/src/inc/elfimage.h b/loader/src/inc/elfimage.h new file mode 100644 index 0000000..1a495bf --- /dev/null +++ b/loader/src/inc/elfimage.h @@ -0,0 +1,152 @@ +#pragma once + +struct ELFSection +{ + ELFSection() = delete; + ELFSection(const std::string& name, uptr addr, u32 len) + { + this->name = name; + this->addr = addr; + this->length = len; + } + + std::string name; + uptr addr; + u32 length; +}; + +struct ELFSymbol +{ + ELFSymbol() = delete; + ELFSymbol(const std::string &name, uptr addr) + { + this->name = name; + this->addr = addr; + } + + std::string name; + uptr addr; +}; + +class ELFImage +{ +public: + ELFImage() = delete; + ELFImage(const char *path) + { + _image = tools::aob_read(path); + + populate_sections(); + populate_symbols(); + } + + ELFImage(const std::vector& vec_contents) + { + _image = vec_contents; + + populate_sections(); + populate_symbols(); + } + + void populate_sections() + { + Elf *elf; + Elf_Scn *scn = nullptr; + GElf_Shdr shdr; + + elf_version(EV_CURRENT); + elf = elf_memory(reinterpret_cast(_image.data()), _image.size()); + + size_t shstrndx; + elf_getshdrstrndx(elf, &shstrndx); // Get the section string table index + + while ((scn = elf_nextscn(elf, scn)) != nullptr) + { + gelf_getshdr(scn, &shdr); + + if (shdr.sh_type == SHT_PROGBITS && shdr.sh_flags == (SHF_ALLOC | SHF_EXECINSTR)) + { + const char *section_name = elf_strptr(elf, shstrndx, shdr.sh_name); + if (strcmp(section_name, ".text") == 0) + { + // Obtain the address and size of the .text section + Elf64_Addr section_address = shdr.sh_addr; + Elf64_Xword section_size = shdr.sh_size; + // Use the text_section_address and text_section_size as needed + _sections.push_back( ELFSection(section_name, section_address, section_size) ); + break; + } + } + } + + elf_end(elf); + } + + void populate_symbols() + { + Elf *elf; + Elf_Scn *scn = nullptr; + Elf_Data *data = nullptr; + GElf_Sym sym; + size_t num_symbols, i; + + elf_version(EV_CURRENT); + elf = elf_memory(reinterpret_cast(_image.data()), _image.size()); + + while ((scn = elf_nextscn(elf, scn)) != nullptr) + { + GElf_Shdr shdr; + gelf_getshdr(scn, &shdr); + + if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) + { + data = elf_getdata(scn, data); + num_symbols = shdr.sh_size / shdr.sh_entsize; + + for (i = 0; i < num_symbols; i++) + { + gelf_getsym(data, i, &sym); + + const char *symbol_name = elf_strptr(elf, shdr.sh_link, sym.st_name); + Elf64_Addr symbol_address = sym.st_value; + _symbols.push_back(ELFSymbol(symbol_name, symbol_address)); + } + } + } + + elf_end(elf); + } + + uintptr_t find_symbol(const char *name) + { + for (const auto &sym : _symbols) + { + if (sym.name == name) + { + return sym.addr; + } + } + + return 0; + } + + auto find_section(const char* name) -> const ELFSection* const + { + for(const auto& scn : _sections) + { + if(scn.name == name) + { + return &scn; + } + } + + return nullptr; + } + + auto buf() { return _image.data(); } +private: + std::vector _image; + std::vector _sections; + std::vector _symbols; +}; + diff --git a/loader/src/inc/hde.h b/loader/src/inc/hde.h new file mode 100644 index 0000000..ca6b3c6 --- /dev/null +++ b/loader/src/inc/hde.h @@ -0,0 +1,11 @@ +#pragma once + +#include "table64.h" +#include "hde64.h" + +namespace hde +{ + size_t disasm_buffer(void *data, size_t length, hde64s *list, size_t list_length); + std::vector disasm_buffer_ex(void *data, size_t length); +} + diff --git a/loader/src/inc/hde64.h b/loader/src/inc/hde64.h new file mode 100644 index 0000000..14ccbf1 --- /dev/null +++ b/loader/src/inc/hde64.h @@ -0,0 +1,94 @@ +#pragma once + +#ifndef _HDE64_H_ +#define _HDE64_H_ + +#define F_MODRM 0x00000001 +#define F_SIB 0x00000002 +#define F_IMM8 0x00000004 +#define F_IMM16 0x00000008 +#define F_IMM32 0x00000010 +#define F_IMM64 0x00000020 +#define F_DISP8 0x00000040 +#define F_DISP16 0x00000080 +#define F_DISP32 0x00000100 +#define F_RELATIVE 0x00000200 +#define F_ERROR 0x00001000 +#define F_ERROR_OPCODE 0x00002000 +#define F_ERROR_LENGTH 0x00004000 +#define F_ERROR_LOCK 0x00008000 +#define F_ERROR_OPERAND 0x00010000 +#define F_PREFIX_REPNZ 0x01000000 +#define F_PREFIX_REPX 0x02000000 +#define F_PREFIX_REP 0x03000000 +#define F_PREFIX_66 0x04000000 +#define F_PREFIX_67 0x08000000 +#define F_PREFIX_LOCK 0x10000000 +#define F_PREFIX_SEG 0x20000000 +#define F_PREFIX_REX 0x40000000 +#define F_PREFIX_ANY 0x7f000000 + +#define PREFIX_SEGMENT_CS 0x2e +#define PREFIX_SEGMENT_SS 0x36 +#define PREFIX_SEGMENT_DS 0x3e +#define PREFIX_SEGMENT_ES 0x26 +#define PREFIX_SEGMENT_FS 0x64 +#define PREFIX_SEGMENT_GS 0x65 +#define PREFIX_LOCK 0xf0 +#define PREFIX_REPNZ 0xf2 +#define PREFIX_REPX 0xf3 +#define PREFIX_OPERAND_SIZE 0x66 +#define PREFIX_ADDRESS_SIZE 0x67 + +#pragma pack(push,1) + +typedef struct { + uint8_t len; + uint8_t p_rep; + uint8_t p_lock; + uint8_t p_seg; + uint8_t p_66; + uint8_t p_67; + uint8_t rex; + uint8_t rex_w; + uint8_t rex_r; + uint8_t rex_x; + uint8_t rex_b; + uint8_t opcode; + uint8_t opcode2; + uint8_t modrm; + uint8_t modrm_mod; + uint8_t modrm_reg; + uint8_t modrm_rm; + uint8_t sib; + uint8_t sib_scale; + uint8_t sib_index; + uint8_t sib_base; + union { + uint8_t imm8; + uint16_t imm16; + uint32_t imm32; + uint64_t imm64; + } imm; + union { + uint8_t disp8; + uint16_t disp16; + uint32_t disp32; + } disp; + uint32_t flags; +} hde64s; + +#pragma pack(pop) + +#ifdef __cplusplus +extern "C" { +#endif + + /* __cdecl */ + unsigned int hde64_disasm(const void* code, hde64s* hs); + +#ifdef __cplusplus +} +#endif + +#endif /* _HDE64_H_ */ \ No newline at end of file diff --git a/loader/src/inc/hwid.h b/loader/src/inc/hwid.h new file mode 100644 index 0000000..972c88d --- /dev/null +++ b/loader/src/inc/hwid.h @@ -0,0 +1,33 @@ +#pragma once + +class Hwid +{ +public: + struct Disk_t + { + std::string model_name; + std::string serial_number; + }; + + struct ArpParticipant_t + { + std::string mac_address_permanent; + std::string device; + }; + +public: + Hwid(); + bool collect(); + bool query_all_disks(); + bool query_arp(); + + bool query_disk_identity(const char *dev, hd_driveid &buffer); + bool query_nvme_identity(const char* dev, Disk_t& buffer); + std::vector dump_smbios_data(); + + bool m_available; + std::vector m_vec_disks; + std::vector m_vec_device_macs; +}; + +extern Hwid *gHwid; \ No newline at end of file diff --git a/loader/src/inc/include.h b/loader/src/inc/include.h new file mode 100644 index 0000000..6c42c84 --- /dev/null +++ b/loader/src/inc/include.h @@ -0,0 +1,196 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std::chrono_literals; + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include // Include this for O_RDONLY +#include // Include this for open and close +#include +#include +#include +#include +#include +#include +#include + +#include "defs.h" +#include "sockets-wrapper.h" +#include "lzmapp.h" +#include "log.h" +#include +#include +#include +#include +#include "murmur.h" +#include "string-table.h" +#include "netsdk.h" +#include "memory.h" +#include "libpmap.h" +#include "hde.h" +#include "xorstr.h" +#include "rotate.h" +#include "ntos.h" +#include "mm.h" +#include "pattern.h" +#include "offsets.h" +#include "pe.h" +#include "wptrace.h" +#include "vmt.h" +#include "tools.h" +#include "elfimage.h" +#include "mapper.h" +#include "console.h" +#include "pump.h" +#include "integrity.h" +#include "hwid.h" + +namespace pe +{ + struct Section + { + Section() + { + begin = 0; + length = 0; + end = 0; + name = "NA"; + } + + Section(u64 b, u64 l, const std::string &n) + { + begin = b; + length = l; + end = (begin + length); + name = n; + } + + u64 begin; + u64 length; + u64 end; + std::string name; + }; +} + +struct Context +{ + int m_pid; + u64 m_game_base; + u64 m_game_len; + u64 m_shell_base; + u64 m_ctx_base; + u64 m_strtbl_base; + u64 m_libc_base; + u64 m_kernel32_base; + u64 m_ntdll_base; + u32 m_libc_syscall_gadget_offset; + + std::vector m_snap_game; + + struct + { + std::vector table; + + std::vector snap_cs; + + inline auto get_sec_hdr(const char* sname) + { + for(const auto& shdr : table) + { + auto name = (const char*)&shdr.Name; + + if(!strcmp(name, sname)) + { + return shdr; + } + } + + return IMAGE_SECTION_HEADER{ }; + } + } m_game_secs; + + LoaderContext m_lctx; + + struct Options + { + bool payload_debug; + bool debug_sigs; + bool dump_game_image; + bool analysis_from_dump; + bool use_custom_payload_path; + bool use_unsafe_memory; + std::string analysis_from_dump_path; + std::string custom_payload_path; + bool analysis_from_dump_vec; + std::vector vec_analysis_from_dump; + } m_opts; + + struct Offsets + { + u32 iface_base_client; + } m_offsets; + + struct Iface + { + u64 base_client; + } m_ix; + + struct Hooks + { + u64 fn_init; + } m_hooks; + + struct VftsRemote + { + u64 base_client; + } m_vfts_remote; + + struct + { + uptr base_client; + } m_vft_orig_ptrs; + + struct Payload + { + u64 length; + u64 entry; + std::vector sections; + u64 ptr_new_loader_ctx; + } m_payload; + + string_table::String_table* m_strtbl; +}; + +extern Context *gctx; + +#include "respawn.h" + +#include "loader.h" +#include "packet-handlers.h" diff --git a/loader/src/inc/integrity.h b/loader/src/inc/integrity.h new file mode 100644 index 0000000..8f3e440 --- /dev/null +++ b/loader/src/inc/integrity.h @@ -0,0 +1,24 @@ +#pragma once + +class Integrity +{ +public: + Integrity(); + + virtual void check1_thread(); + virtual void check2_thread(); + + virtual bool is_process_being_traced(); + virtual bool is_process_being_patched(); + + virtual bool check_debugger_presence(); + virtual bool check_blacklisted_program_presence(); + + virtual void push_violation(proto::msg::integrity_event::ids id, proto::msg::integrity_event::contents_t contents); + + virtual u32 calc_self_code_checksum(); +private: + u32 m_checksum_self_seg_text; +}; + +extern Integrity* gInteg; diff --git a/loader/src/inc/libpmap.h b/loader/src/inc/libpmap.h new file mode 100644 index 0000000..5eea32c --- /dev/null +++ b/loader/src/inc/libpmap.h @@ -0,0 +1,73 @@ +/** + * @file procmaps.h + * @brief Header file for lib procmaps + * @author Lilian VERLHAC + * @version 1.0 + * @date 2020-04-18 + */ + +#ifndef PROCMAPS_H_ +#define PROCMAPS_H_ + +typedef struct dev_major_minor_s dev_major_minor_t; + +/** + * @brief Hold the major minor id for the map's device + */ +struct dev_major_minor_s { + int major; + int minor; +}; + +typedef struct procmaps_row_s procmaps_row_t; + +/** + * @brief Hold all the information of a map + */ +struct procmaps_row_s { + unsigned long long addr_begin; + unsigned long long addr_end; + unsigned char perms; + unsigned long long offset; + dev_major_minor_t dev; + int inode; + char *pathname; +}; + +/** + * @brief Permissions associated to a map + */ +enum MAPS_PERMS { + PERMS_READ = 1 << 0, + PERMS_WRITE = 1 << 1, + PERMS_EXECUTE = 1 << 2, + PERMS_PRIVATE = 1 << 3, + PERMS_SHARED = 1 << 4 +}; + +typedef procmaps_row_t hr_procmaps; + +#define MAX_PROCPATH_LEN 100 +#define PROCMAPS_ARRAY_BASE_SIZE 32 + +/** + * @brief Parse /proc/pid/maps to create an array of struct ptr which + * hold of the information + * + * @param pid Process pid, if pid <= 0 use self process + * + * @return Array of filled procmaps_row_t * + */ +hr_procmaps **contruct_procmaps(int pid); + +/** + * @brief Destroy hr_procmaps object, free all the allocated memory + * + * @param procmaps Array of procmaps_row_t * + */ +void destroy_procmaps(hr_procmaps **procmaps); + +void procmaps_begin_transaction(); +void procmaps_end_transaction(); + +#endif // PROCMAPS_H_ \ No newline at end of file diff --git a/loader/src/inc/loader.h b/loader/src/inc/loader.h new file mode 100644 index 0000000..2ed5d6a --- /dev/null +++ b/loader/src/inc/loader.h @@ -0,0 +1,16 @@ +#pragma once + +class Loader +{ +public: + virtual void start(int argc, char* argv[]); + virtual bool setup_netsdk(); + + virtual std::string get_argument_param(const std::string &str); + virtual void parse_argument(const std::string &str); + + virtual bool authenticate(); +private: +}; + +extern Loader* gLoader; diff --git a/loader/src/inc/log.h b/loader/src/inc/log.h new file mode 100644 index 0000000..482eb1b --- /dev/null +++ b/loader/src/inc/log.h @@ -0,0 +1,20 @@ +#pragma once + +namespace xlog +{ + void __msg(const char* fmt, ...); + void __error(const char* fmt, ...); + void __safeLog_msg(const char* fmt, ...); +} + +#define msg(X, ...) xlog::__msg(_XS(X), ##__VA_ARGS__) +#define error(X, ...) xlog::__error(_XS(X), ##__VA_ARGS__) + +// TODO: implement! +#define dbg(X, ...) while(0){ } + +#if defined(R5I_PROD) +#define safeLog_msg(X, ...) CloudLog_Msg(_XS(X), ##__VA_ARGS__) +#else +#define safeLog_msg msg +#endif diff --git a/loader/src/inc/lzmapp.h b/loader/src/inc/lzmapp.h new file mode 100644 index 0000000..e8542fd --- /dev/null +++ b/loader/src/inc/lzmapp.h @@ -0,0 +1,15 @@ +// +// Created by drof on 27.01.24. +// + +#ifndef BACKEND_LZMAPP_H +#define BACKEND_LZMAPP_H + +namespace lzma +{ + extern std::vector compress(const std::vector& data); + extern std::vector decompress(const std::vector& data); +} + + +#endif //BACKEND_LZMAPP_H diff --git a/loader/src/inc/mapper.h b/loader/src/inc/mapper.h new file mode 100644 index 0000000..1eb9f2a --- /dev/null +++ b/loader/src/inc/mapper.h @@ -0,0 +1,132 @@ +#pragma once + +#define TARGET_NAME _XS("r5apex") // R5Apex +#define MAPPING_NAME _XS("r5apex.exe") // r5apex.exe + +struct relocation_info_t +{ + uint16_t m_type; + uintptr_t m_address; +}; +using relocation_list = std::vector; + +#pragma pack(push, 1) +struct LoaderContext +{ + unsigned int magic; + u8 inited; + /* + * Keep this here and if you change anything above, + * then ENSURE to update our trampoline offsets! + */ + struct Origs + { + u64 init; + } origs; + u64 game_base; + u64 game_len; + u64 game_rdata_base; + u64 game_rdata_len; + u64 game_cs_base; + u64 game_cs_len; + u64 game_data_base; + u64 game_data_len; + u64 libc_base; + u64 libm_base; + string_table::String_table* strtbl; + u64 strtbl_decryption_key; + + struct + { + uint32_t fopen; + uint32_t fputs; + uint32_t fclose; + uint32_t sprintf; + uint32_t sleep; + } linuxcrt_cpp; + + struct + { + uint32_t acosf; + uint32_t atan2f; + uint32_t fabsf; + uint32_t sinf; + uint32_t cosf; + } linuxcrt_math; + + struct + { + uptr create_file; + uptr get_file_size; + uptr close_handle; + uptr read_file; + uptr capture_stack_back_trace; + } winapis; + + struct Offsets + { + uint32_t entity_list; + uint32_t signon_state; + uint32_t input_system; + uint32_t mp_gamemode; + uint32_t global_vars; + uint32_t game_movement; + uint32_t map_name; + uint32_t name_list; + uint32_t tag_list; + uint32_t client_class_head; + uint32_t mat_system_surface; + uint32_t base_client; + uint32_t view_render; + uint32_t client_state; + uint32_t client_mode; + uint32_t kwewed_render_ctx; + uint32_t rs_cvar_gamepad_checks_enabled; + uint32_t rs_gamepad_checks_last; + uint32_t string_dict_user_names; + + struct + { + uint32_t view_offset; + uint32_t view_angle; + } player; + + struct + { + uint32_t get_mem_alloc; + uint32_t mss_draw_filled_rect; + uint32_t math_ang2vec; + uint32_t math_vec2ang; + } fns; + + struct + { + uint32_t generic_1; + } vft_caves; + + struct + { + u64 mkrctx_unk1; + u64 create_move; + u64 override_mouse; + } ret_addrs; + } offsets; + + struct + { + u64 base_client; + } vft_orig_ptrs; +}; +#pragma pack(pop) + +namespace mapper +{ + extern relocation_list get_relocation_list(u64 image_base, PIMAGE_NT_HEADERS64 nt_header); + + extern bool resolve_linux_crt(); + + extern bool setup_pre(); + extern bool setup(int pid); + extern bool fetch_image(std::vector& buf); + extern bool load(std::vector &buf); +} diff --git a/loader/src/inc/memory.h b/loader/src/inc/memory.h new file mode 100644 index 0000000..c301334 --- /dev/null +++ b/loader/src/inc/memory.h @@ -0,0 +1,108 @@ +#pragma once + +namespace mem +{ + struct range { + uptr m_begin{}; + uptr m_end{}; + uptr m_size{}; + }; + + class addr + { + public: + addr() = default; + ~addr() = default; + + bool operator==(const uptr& other) const + { + return (m_base == other); + } + + bool operator==(const uptr& lhs) + { + return (lhs == m_base); + } + + addr(const uptr base) + { + m_base = base; + } + + addr(void* const ptr) + { + m_base = reinterpret_cast(ptr); + } + + [[nodiscard]] auto Get() const + { + return addr(m_base); + } + + [[nodiscard]] auto Base() const + { + return m_base; + } + + [[nodiscard]] auto Valid() const + { + return !(m_base == 0); + } + + [[nodiscard]] auto Add(const uptr t) const + { + return addr(m_base + t); + } + + [[nodiscard]] auto Sub(const uptr t) const + { + return addr(m_base - t); + } + + [[nodiscard]] auto Deref() const + { + return addr(*reinterpret_cast(m_base)); + } + + [[nodiscard]] auto Ptr() const + { + return reinterpret_cast(m_base); + } + + template < class T > + [[nodiscard]] auto As() + { + return reinterpret_cast(m_base); + } + + template < class T > + [[nodiscard]] auto Retrieve_as() + { + return *reinterpret_cast(m_base); + } + + template < class T > + void Set(const T& value) + { + *As() = value; + } + private: + uptr m_base; + }; + + inline uptr resolve_operandof_lea(const u8 *rip) + { + u8 offset = 0; + u8 skip = 3; + + if (rip[0] == 0x48) + { + /* REX.W PREFIX */ + offset = 1; + --skip; + } + + auto disp = *(int32_t *)(rip + offset + skip); + return reinterpret_cast(rip + 7 + disp); + } +} diff --git a/loader/src/inc/mm.h b/loader/src/inc/mm.h new file mode 100644 index 0000000..d07421f --- /dev/null +++ b/loader/src/inc/mm.h @@ -0,0 +1,64 @@ +#pragma once + +namespace mm::detail +{ + char vm_read(int pid, void* dst, u64 src, u64 len); + char vm_write(int pid, u64 dst, const void* src, u64 len); + + u64 aob_search_buf_single(void *pHayStack, void *pNeedle, size_t uHayStackLength, size_t uNeedleLength); +} + +namespace mm +{ + inline u64 aob_search_buf_single_u64(void *pHayStack, size_t uHayStackLength, u64 uNeedle) + { + return detail::aob_search_buf_single(pHayStack, &uNeedle, uHayStackLength, sizeof(uNeedle)); + } +} + +class MemoryManager +{ +public: + MemoryManager() = delete; + MemoryManager(int pid) + { + m_pid = pid; + } + + bool read(void* dst, u64 src, size_t len); + bool write(u64 dst, const void* src, size_t len); + + bool fill(u64 dst, u8 ch, size_t len) + { + auto tmp = malloc(len); + if(tmp) + { + memset(tmp, ch, len); + auto rs = write(dst, tmp, len); + free(tmp); + return rs; + } + + return false; + } + + template < typename T > + T tread(u64 src) + { + T result; + memset(&result, 0, sizeof(result)); + + read(&result, src, sizeof(result)); + return result; + } + + template < typename T > + bool twrite(u64 dst, const T& src) + { + return write(dst, &src, sizeof(src)); + } +private: + int m_pid; +}; + +extern MemoryManager* gmm; diff --git a/loader/src/inc/murmur.h b/loader/src/inc/murmur.h new file mode 100644 index 0000000..1bac294 --- /dev/null +++ b/loader/src/inc/murmur.h @@ -0,0 +1,45 @@ +#pragma once + +inline unsigned int mmh_32(const void* key, int len, unsigned int seed) +{ + const unsigned int m = 0x5bd1e995; + const int r = 24; + + unsigned int h = seed ^ len; + + const unsigned char* data = (const unsigned char*)key; + + while (len >= 4) + { + unsigned int k = *(unsigned int*)data; + + k *= m; + k ^= k >> r; + k *= m; + + h *= m; + h ^= k; + + data += 4; + len -= 4; + } + + switch (len) + { + case 3: h ^= data[2] << 16; + case 2: h ^= data[1] << 8; + case 1: h ^= data[0]; + h *= m; + }; + + h ^= h >> 13; + h *= m; + h ^= h >> 15; + + return h; +} + +inline int string_hash(const char* text, size_t length) +{ + return mmh_32((void*)text, static_cast(length), 6); +} diff --git a/loader/src/inc/netsdk.h b/loader/src/inc/netsdk.h new file mode 100644 index 0000000..c05a64f --- /dev/null +++ b/loader/src/inc/netsdk.h @@ -0,0 +1,37 @@ +#pragma once + +FORWARD_DECLARE_CLASS(Pump); + +class NetSDK +{ +public: + NetSDK() = delete; + NetSDK(const std::string_view& hostname, int port) + { + m_hostname = hostname; + m_port = port; + } + + bool start(); + bool handshake(); + bool wait_for_activation(); + bool ping(); + bool start_pump(); + bool authenticate(const std::string& email, const std::string& password); + bool request_dlc(u32 id); + bool request_asset(u32 id); + + void push_message_to_cloud_log(const char* contents, ...); + + auto pump() { return m_pump; } +private: + int m_sck; + std::string m_hostname; + int m_port; + unsigned char m_client_pk[crypto_kx_PUBLICKEYBYTES], m_client_sk[crypto_kx_SECRETKEYBYTES]; + unsigned char m_client_rx[crypto_kx_SESSIONKEYBYTES], m_client_tx[crypto_kx_SESSIONKEYBYTES]; + unsigned char m_server_pk[crypto_kx_PUBLICKEYBYTES]; + Pump* m_pump; +}; + +extern NetSDK* gNetSDK; diff --git a/loader/src/inc/ntos.h b/loader/src/inc/ntos.h new file mode 100644 index 0000000..03f4d90 --- /dev/null +++ b/loader/src/inc/ntos.h @@ -0,0 +1,150 @@ +#pragma once + +#define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16 +#define IMAGE_DOS_SIGNATURE 0x5A4D +#define IMAGE_NT_SIGNATURE 0x4550 +#define IMAGE_SIZEOF_SHORT_NAME 8 + +typedef unsigned char BYTE; +typedef unsigned short WORD; +typedef int32_t LONG; +typedef uint32_t DWORD; +typedef unsigned long long ULONGLONG; +typedef unsigned short int USHORT; + +typedef struct _IMAGE_BASE_RELOCATION +{ + DWORD VirtualAddress; + DWORD SizeOfBlock; + // WORD TypeOffset[1]; +} __attribute__((aligned(1), packed)) IMAGE_BASE_RELOCATION; +typedef IMAGE_BASE_RELOCATION UNALIGNED, *PIMAGE_BASE_RELOCATION; + +typedef struct BASE_RELOCATION_ENTRY +{ + USHORT Offset : 12; + USHORT Type : 4; +} __attribute__((aligned(1), packed)) BASE_RELOCATION_ENTRY, *PBASE_RELOCATION_ENTRY; + +typedef struct _IMAGE_DOS_HEADER +{ + WORD e_magic; + WORD e_cblp; + WORD e_cp; + WORD e_crlc; + WORD e_cparhdr; + WORD e_minalloc; + WORD e_maxalloc; + WORD e_ss; + WORD e_sp; + WORD e_csum; + WORD e_ip; + WORD e_cs; + WORD e_lfarlc; + WORD e_ovno; + WORD e_res[4]; + WORD e_oemid; + WORD e_oeminfo; + WORD e_res2[10]; + LONG e_lfanew; +} __attribute__((aligned(1), packed)) IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER; + +typedef struct _IMAGE_FILE_HEADER +{ + WORD Machine; + WORD NumberOfSections; + DWORD TimeDateStamp; + DWORD PointerToSymbolTable; + DWORD NumberOfSymbols; + WORD SizeOfOptionalHeader; + WORD Characteristics; +} __attribute__((aligned(1), packed)) IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER; + +typedef struct _IMAGE_DATA_DIRECTORY +{ + DWORD VirtualAddress; // RVA of the data + DWORD Size; // Size of the data +} __attribute__((aligned(1), packed)) IMAGE_DATA_DIRECTORY; + +typedef struct _IMAGE_DEBUG_DIRECTORY { + DWORD Characteristics; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD Type; + DWORD SizeOfData; + DWORD AddressOfRawData; + DWORD PointerToRawData; +} __attribute__((aligned(1), packed)) IMAGE_DEBUG_DIRECTORY, *PIMAGE_DEBUG_DIRECTORY; + +typedef struct _IMAGE_OPTIONAL_HEADER64 +{ + WORD Magic; + BYTE MajorLinkerVersion; + BYTE MinorLinkerVersion; + DWORD SizeOfCode; + DWORD SizeOfInitializedData; + DWORD SizeOfUninitializedData; + DWORD AddressOfEntryPoint; + DWORD BaseOfCode; + ULONGLONG ImageBase; + DWORD SectionAlignment; + DWORD FileAlignment; + WORD MajorOperatingSystemVersion; + WORD MinorOperatingSystemVersion; + WORD MajorImageVersion; + WORD MinorImageVersion; + WORD MajorSubsystemVersion; + WORD MinorSubsystemVersion; + DWORD Win32VersionValue; + DWORD SizeOfImage; + DWORD SizeOfHeaders; + DWORD CheckSum; + WORD Subsystem; + WORD DllCharacteristics; + ULONGLONG SizeOfStackReserve; + ULONGLONG SizeOfStackCommit; + ULONGLONG SizeOfHeapReserve; + ULONGLONG SizeOfHeapCommit; + DWORD LoaderFlags; + DWORD NumberOfRvaAndSizes; + IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; +} __attribute__((aligned(1), packed)) IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64; + +typedef struct _IMAGE_NT_HEADERS_GENERIC +{ + DWORD Signature; + IMAGE_FILE_HEADER FileHeader; +} __attribute__((aligned(1), packed)) IMAGE_NT_HEADERS_GENERIC, *PIMAGE_NT_HEADERS_GENERIC; + +typedef struct _IMAGE_NT_HEADERS64 +{ + DWORD Signature; + IMAGE_FILE_HEADER FileHeader; + IMAGE_OPTIONAL_HEADER64 OptionalHeader; +} __attribute__((aligned(1), packed)) IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64; + +typedef struct _IMAGE_SECTION_HEADER +{ + BYTE Name[IMAGE_SIZEOF_SHORT_NAME]; + union + { + DWORD PhysicalAddress; + DWORD VirtualSize; + } Misc; + DWORD VirtualAddress; + DWORD SizeOfRawData; + DWORD PointerToRawData; + DWORD PointerToRelocations; + DWORD PointerToLinenumbers; + WORD NumberOfRelocations; + WORD NumberOfLinenumbers; + DWORD Characteristics; +} __attribute__((aligned(1), packed)) IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER; + +#define FIELD_OFFSET(CLASS, FIELD) offsetof(CLASS, FIELD) +#define IMAGE_FIRST_SECTION64(ntheader) ((PIMAGE_SECTION_HEADER)((u64)(ntheader) + \ + FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader) + \ + ((ntheader))->FileHeader.SizeOfOptionalHeader)) + +#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 diff --git a/loader/src/inc/offsets.h b/loader/src/inc/offsets.h new file mode 100644 index 0000000..d29dc6d --- /dev/null +++ b/loader/src/inc/offsets.h @@ -0,0 +1,110 @@ +#pragma once + +#define resolve_rva(addr, instr_off, instr_len) ((addr + *reinterpret_cast(addr + instr_off)) + instr_len) + +namespace offsets +{ + namespace detail + { + inline uptr find_ptr(uptr start, uptr len, const char* pattern, u16 ioff, u16 ilen) + { + auto rs = find_pattern(start, len, pattern); + if(rs) + { + return resolve_rva(rs, ioff, ilen); + } + return 0; + } + } + + inline u64 fix_scn_off(void *image, u32 sec_base, uptr off) + { + //msg("fix_scn_off(%p, %x, %p)", image, sec_base, off); + return ((BASE_OF(image)) + sec_base + off); + } + + inline u64 find(u64 base, u64 start, u64 len, const char *name, const char *pattern) + { + auto initial_addr = find_pattern(start, len, pattern); + if (!initial_addr) + { + safeLog_msg("!! couldn't find an offset (pattern) -> %s", name); + exit(0); + } + + auto corrected = initial_addr - base; + safeLog_msg("** pattern %s (final) -> %lx", name, corrected); + + return corrected; + } + + inline u64 find_ptr(u64 base, u64 start, u64 len, const char *name, const char *pattern, u64 ioff, u64 ilen) + { + auto initial_addr = find_pattern(start, len, pattern); + if (!initial_addr) + { + safeLog_msg("!! couldn't find an offset (pattern/PTR) -> %s", name); + exit(0); + } + + auto corrected = resolve_rva(initial_addr, ioff, ilen); + if (!corrected) + { + safeLog_msg("!! couldn't correct an offset (pattern/PTR) -> %s", name); + exit(0); + } + + corrected -= base; + safeLog_msg("** pattern_PTR %s (final) -> %lx", name, corrected); + + return corrected; + } + + inline u64 find_local_ptr_adj_remote(u64 start, u64 len, u64 remote_base, + u64 remote_scn_base, const char *name, const char *pattern, u64 ioff, u64 ilen, bool fix = true) + { + auto initial_addr = find_pattern(start, len, pattern); + if (!initial_addr) + { + safeLog_msg("!! couldn't find an offset (pattern/PTR) -> %s", name); + exit(0); + } + + auto corrected = resolve_rva(initial_addr, ioff, ilen); + if (!corrected) + { + safeLog_msg("!! couldn't correct an offset (pattern/PTR) -> %s", name); + exit(0); + } + + auto rs = fix ? (fix_scn_off(PTR_OF(remote_base), remote_scn_base, (corrected - start)) - remote_base) : corrected; + if(!fix) rs -= remote_base; + safeLog_msg("** pattern_PTR %s (final) -> %lx", name, rs); + + return rs; + } + + inline u64 find_local_adj_remote(u64 start, u64 len, u64 remote_base, u64 remote_scn_base, const char *name, const char *pattern, bool fix = true) + { + auto initial_addr = find_pattern(start, len, pattern); + if (!initial_addr) + { + safeLog_msg("!! couldn't find an offset (pattern) -> %s", name); + exit(0); + } + + auto rs = fix ? (fix_scn_off(PTR_OF(remote_base), remote_scn_base, (initial_addr - start)) - remote_base) : initial_addr; + if(!fix) rs -= remote_base; + safeLog_msg("** pattern %s (final) -> %lx", name, rs); + + return rs; + } +} + +#define FIND_SIG(BASE, START, LEN, NAME, PAT) offsets::find(BASE, START, LEN, _XS(NAME), _XS(PAT)) +#define FIND_PTR(BASE, START, LEN, NAME, PAT, IOFF, ILEN) offsets::find_ptr(BASE, START, LEN, _XS(NAME), _XS(PAT), IOFF, ILEN) + +// GIVES AN RVA ONLY, WITH IMAGE BASE SUBTRACTED. +#define FIND_OFF_LAJR(START, LEN, REMOTE_BASE, REMOTE_SCN_BASE, NAME, PAT, FIX) offsets::find_local_adj_remote(START, LEN, REMOTE_BASE, REMOTE_SCN_BASE, _XS(NAME), _XS(PAT), FIX) +#define FIND_PTR_LAJR(START, LEN, REMOTE_BASE, REMOTE_SCN_BASE, NAME, PAT, IOFF, ILEN, FIX) offsets::find_local_ptr_adj_remote(START, LEN, \ +REMOTE_BASE, REMOTE_SCN_BASE, _XS(NAME), _XS(PAT), IOFF, ILEN, FIX) diff --git a/loader/src/inc/packet-handlers.h b/loader/src/inc/packet-handlers.h new file mode 100644 index 0000000..2bdf8ff --- /dev/null +++ b/loader/src/inc/packet-handlers.h @@ -0,0 +1,33 @@ +#pragma once + +struct NetworkSessionContext_t +{ + bool received_ping; + bool has_server_acknowledged_ping; + + bool received_authentication_result; + proto::AUTH_STATES value_authentication_result; + + u32 requested_asset_id; + + bool received_requested_asset; + std::vector vec_asset_contents; + + u32 requested_dlc_id; + + bool received_request_dlc_response; + bool will_server_send_requested_dlc; + + bool received_dlc; + std::vector vec_dlc_contents; +}; +extern NetworkSessionContext_t* gNetSsnContext; + +namespace packet_handlers +{ + extern void handle_response_to_ping(int sck, proto::Packet_header_t hdr, std::vector vec_contents); + extern void handle_response_to_auth(int sck, proto::Packet_header_t hdr, std::vector vec_contents); + extern void handle_response_to_request_dlc(int sck, proto::Packet_header_t hdr, std::vector vec_contents); + extern void handle_server_drop_client(int sck, proto::Packet_header_t hdr, std::vector vec_contents); + extern void handle_server_data_transfer(int sck, proto::Packet_header_t hdr, std::vector vec_contents); +} diff --git a/loader/src/inc/pattern.h b/loader/src/inc/pattern.h new file mode 100644 index 0000000..a130f3c --- /dev/null +++ b/loader/src/inc/pattern.h @@ -0,0 +1,120 @@ +#pragma once + +#define INRANGE(x,a,b) (x >= a && x <= b) +#define getBits( x ) (INRANGE(x,'0','9') ? (x - '0') : ((x&(~0x20)) - 'A' + 0xa)) +#define getByte( x ) (getBits(x[0]) << 4 | getBits(x[1])) + +namespace sig +{ + OBFUS_FUNC_DISABLE inline + bool isMatch(const u8* addr, const u8* pat, const u8* msk) + { + size_t n = 0; + while (addr[n] == pat[n] || msk[n] == (u8)'?') { + if (!msk[++n]) { + return true; + } + } + return false; + } + + OBFUS_FUNC_DISABLE inline const u8* find_pattern(const u8* rangeStart, u64 len, const char* pattern) + { + size_t l = strlen(pattern); + u8* patt_base = static_cast(alloca(l >> 1)); + u8* msk_base = static_cast(alloca(l >> 1)); + u8* pat = patt_base; + u8* msk = msk_base; + l = 0; + while (*pattern) { + if (*pattern == ' ') + pattern++; + if (!*pattern) + break; + if (*(u8*)pattern == (u8)'\?') { + *pat++ = 0; + *msk++ = '?'; + pattern += ((*(u16*)pattern == (u16)'\?\?') ? 2 : 1); + } + else { + *pat++ = getByte(pattern); + *msk++ = 'x'; + pattern += 2; + } + l++; + } + *msk = 0; + pat = patt_base; + msk = msk_base; + for (u32 n = 0; n < (len - l); ++n) + { + if (isMatch(rangeStart + n, patt_base, msk_base)) { + return rangeStart + n; + } + } + return NULL; + } + + OBFUS_FUNC_DISABLE inline auto get_pattern_byte_length(const char* pattern) + { + size_t l = strlen(pattern); + u8* patt_base = static_cast(alloca(l >> 1)); + u8* msk_base = static_cast(alloca(l >> 1)); + u8* pat = patt_base; + u8* msk = msk_base; + u64 result = 0; + + int fields = 0; + for (u64 i = 0; i < l; i++) + { + if (pattern[i] == ' ') + continue; + + if (fields < 1) + fields++; + else + { + result++; + fields = 0; + } + } + + return result; + } + + OBFUS_FUNC_DISABLE inline auto find_pattern_rc(const u8* rangeStart, u64 len, const char* pattern) + { + auto result = std::vector(); + auto pat_len = get_pattern_byte_length(pattern); + + if (pat_len > 0) + { + const u8* last_hit = nullptr; + + for (auto curr = rangeStart; (curr < (rangeStart + len)); ) + { + const u8* rs = nullptr; + + if (!last_hit) + rs = find_pattern(curr, len, pattern); + else + { + rs = find_pattern(curr, ((rangeStart + len) - last_hit - pat_len), pattern); + } + + if (rs) + { + result.push_back(BASE_OF(rs)); + curr = rs + pat_len; + last_hit = rs; + } + else + break; + } + } + + return result; + } +} + +#define find_pattern(base, length, pat) (u64)sig::find_pattern((u8*)base, length, pat) diff --git a/loader/src/inc/pe.h b/loader/src/inc/pe.h new file mode 100644 index 0000000..f846d07 --- /dev/null +++ b/loader/src/inc/pe.h @@ -0,0 +1,187 @@ +#pragma once + +typedef struct _IMAGE_EXPORT_DIRECTORY +{ + DWORD Characteristics; + DWORD TimeDateStamp; + WORD MajorVersion; + WORD MinorVersion; + DWORD Name; + DWORD Base; + DWORD NumberOfFunctions; + DWORD NumberOfNames; + DWORD AddressOfFunctions; // RVA from base of image + DWORD AddressOfNames; // RVA from base of image + DWORD AddressOfNameOrdinals; // RVA from base of image +} IMAGE_EXPORT_DIRECTORY, *PIMAGE_EXPORT_DIRECTORY; + +#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 +#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 +#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 + +bool pe_dump_image(int pid, u64 base, u64 *out_len); +void pe_dump_fix(void *image); + +namespace pe +{ + template + PIMAGE_SECTION_HEADER GetEnclosingSectionHeader( + DWORD rva, + T *pNTHeader) // 'T' == PIMAGE_NT_HEADERS + { + PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION64(pNTHeader); + unsigned i; + + for (i = 0; i < pNTHeader->FileHeader.NumberOfSections; i++, section++) + { + // This 3 line idiocy is because Watcom's linker actually sets the + // Misc.VirtualSize field to 0. (!!! - Retards....!!!) + DWORD size = section->Misc.VirtualSize; + if (0 == size) + size = section->SizeOfRawData; + + // Is the RVA within this section? + if ((rva >= section->VirtualAddress) && + (rva < (section->VirtualAddress + size))) + return section; + } + + return 0; + } + + template + void *get_ptr_from_va( + DWORD rva, + T *pNTHeader, + u8 *imageBase) // 'T' = PIMAGE_NT_HEADERS + { + PIMAGE_SECTION_HEADER pSectionHdr; + int delta; + + pSectionHdr = GetEnclosingSectionHeader(rva, pNTHeader); + if (!pSectionHdr) + return 0; + + delta = (int)(pSectionHdr->VirtualAddress - pSectionHdr->PointerToRawData); + return (void *)(imageBase + rva - delta); + } + + template + _pt *get_data_directory_ptr(uintptr_t base, _nth nt_hdr, int index) + { + const auto offset = + nt_hdr->OptionalHeader.DataDirectory[index].VirtualAddress; + + return reinterpret_cast<_pt *>((base + offset)); + } + + template + bool get_data_directory_info(uintptr_t base, _nth nt_hdr, int index, IMAGE_DATA_DIRECTORY &out) + { + out = nt_hdr->OptionalHeader.DataDirectory[index]; + + return true; + } + + extern PIMAGE_SECTION_HEADER find_section_base_virt(void *image, const char *name); + inline u64 find_export_address(uintptr_t base, PIMAGE_DOS_HEADER dos_hdr, PIMAGE_NT_HEADERS64 nt_hdr, const char *exp_name, bool file = true) + { + auto export_ddir = &nt_hdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; + if (!export_ddir->VirtualAddress && !export_ddir->Size) + return 0; + + auto export_descriptor = reinterpret_cast(get_ptr_from_va(export_ddir->VirtualAddress, nt_hdr, (u8 *)base)); + + const auto export_descriptor_rva_raw = reinterpret_cast(export_descriptor) - base; + const auto export_descriptor_rva = get_ptr_from_va(export_descriptor_rva_raw, nt_hdr, (u8 *)base); + + const uintptr_t names_rva = export_descriptor->AddressOfNames; + const uintptr_t addresses_rva = export_descriptor->AddressOfFunctions; + const uintptr_t ordinals_rva = export_descriptor->AddressOfNameOrdinals; + + const auto names_address = get_ptr_from_va(static_cast(names_rva), nt_hdr, (u8 *)base); + const auto addresses_address = get_ptr_from_va(static_cast(addresses_rva), nt_hdr, (u8 *)base); + const auto ordinals_address = get_ptr_from_va(static_cast(ordinals_rva), nt_hdr, (u8 *)base); + + auto *const names_array = reinterpret_cast(names_address); + auto *const addresses_array = reinterpret_cast(addresses_address); + auto *const ordinals_array = reinterpret_cast(ordinals_address); + + const auto export_descriptor_rva_begin = (u64)export_descriptor_rva; + const auto export_descriptor_rva_end = (u64)(BASE_OF(export_descriptor_rva) + export_ddir->Size); + + /*msg("export -> exportDescVA: %p", export_ddir->VirtualAddress); + msg("export -> exportDescLN: %p", export_ddir->Size); + + msg("export -> aNames: %p", names_array); + msg("export -> aAddrs: %p", addresses_array); + msg("export -> aOrdinals: %p", ordinals_array); + + msg("export-> nFuncs: %i", export_descriptor->NumberOfFunctions);*/ + + for (size_t i = 0; i < export_descriptor->NumberOfFunctions; i++) + { + auto address_virt = addresses_array[ordinals_array[i]]; + auto address = (u64)get_ptr_from_va(address_virt, nt_hdr, (u8 *)base); + auto name = (char *)get_ptr_from_va(names_array[i], nt_hdr, (u8 *)base); + + if (strcmp(name, exp_name) == 0) + { + if (file) + return address; + else + return base + address_virt; + } + } + + return 0; + } + + inline uptr find_export_address_virt(uptr base, PIMAGE_DOS_HEADER dos_hdr, PIMAGE_NT_HEADERS64 nt_hdr, const char *exp_name) + { + auto *const export_descriptor = + get_data_directory_ptr(base, nt_hdr, + IMAGE_DIRECTORY_ENTRY_EXPORT); + + IMAGE_DATA_DIRECTORY export_descriptor_info{}; + + if (export_descriptor == nullptr) + return 0; + if (!get_data_directory_info(base, nt_hdr, + IMAGE_DIRECTORY_ENTRY_EXPORT, export_descriptor_info)) + return 0; + const auto export_descriptor_rva = + (reinterpret_cast(export_descriptor) - base); + + const uptr names_rva = export_descriptor->AddressOfNames; + const uptr addresses_rva = export_descriptor->AddressOfFunctions; + const uptr ordinals_rva = export_descriptor->AddressOfNameOrdinals; + + const auto names_address = static_cast(base + names_rva); + const auto addresses_address = static_cast(base + addresses_rva); + const auto ordinals_address = static_cast(base + ordinals_rva); + + auto *const names_array = reinterpret_cast(names_address); + auto *const addresses_array = reinterpret_cast(addresses_address); + auto *const ordinals_array = reinterpret_cast(ordinals_address); + + const auto export_descriptor_rva_begin = export_descriptor_rva; + const auto export_descriptor_rva_end = (export_descriptor_rva + export_descriptor_info.Size); + + for (size_t i = 0; i < export_descriptor->NumberOfFunctions; i++) + { + const auto ordinal = ordinals_array[i]; + if (ordinal == 0) + { + } + + const auto address = static_cast(base + addresses_array[i]); + auto *const name = reinterpret_cast(base + names_array[i]); + + if(!strcmp(name, exp_name)) + return address; + } + + return 0; + } +} diff --git a/loader/src/inc/pocketlzma.h b/loader/src/inc/pocketlzma.h new file mode 100644 index 0000000..b5ba88b --- /dev/null +++ b/loader/src/inc/pocketlzma.h @@ -0,0 +1,7498 @@ +/*! + * BSD 2-Clause License + + Copyright (c) 2020, Robin Berg Pettersen + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + IMPORTANT - YOU MUST #define POCKETLZMA_LZMA_C_DEFINE exactly ONCE before the first time you include "pocketlzma.hpp" + The reason for this is that the base logic used by PocketLzma is written i C (by Igor Pavlov), thus this is required + to make sure the implementations are included only once. In other words: If pocketlzma.hpp is included several places, + the #define must not be included anywhere but in one of them. + + Example: + #define POCKETLZMA_LZMA_C_DEFINE + #include "pocketlzma.hpp" + + */ + +#ifndef POCKETLZMA_POCKETLZMA_H +#define POCKETLZMA_POCKETLZMA_H + +//LZMA C INCLUDES +#ifdef _WIN32 +#include +#endif + +#include +#include +#include +#include + + +/*** Start of inlined file: PocketLzmaConfig.h ***/ +#define POCKETLZMA_VERSION_MAJOR 1 +#define POCKETLZMA_VERSION_MINOR 0 +#define POCKETLZMA_VERSION_PATCH 0 + +/*** End of inlined file: PocketLzmaConfig.h ***/ + +namespace plz +{ + namespace c + { + + /*** Start of inlined file: 7zTypes.h ***/ +#ifndef __7Z_TYPES_H +#define __7Z_TYPES_H + +//#ifdef _WIN32 +///* #include */ +//#endif + +//#include + +#ifndef EXTERN_C_BEGIN +#ifdef __cplusplus +#define EXTERN_C_BEGIN extern "C" { +#define EXTERN_C_END } +#else + #define EXTERN_C_BEGIN +#define EXTERN_C_END +#endif +#endif + + EXTERN_C_BEGIN + +#define SZ_OK 0 + +#define SZ_ERROR_DATA 1 +#define SZ_ERROR_MEM 2 +#define SZ_ERROR_CRC 3 +#define SZ_ERROR_UNSUPPORTED 4 +#define SZ_ERROR_PARAM 5 +#define SZ_ERROR_INPUT_EOF 6 +#define SZ_ERROR_OUTPUT_EOF 7 +#define SZ_ERROR_READ 8 +#define SZ_ERROR_WRITE 9 +#define SZ_ERROR_PROGRESS 10 +#define SZ_ERROR_FAIL 11 +#define SZ_ERROR_THREAD 12 + +#define SZ_ERROR_ARCHIVE 16 +#define SZ_ERROR_NO_ARCHIVE 17 + + typedef int SRes; + +#ifdef _WIN32 + + /* typedef DWORD WRes; */ + typedef unsigned WRes; +#define MY_SRes_HRESULT_FROM_WRes(x) HRESULT_FROM_WIN32(x) + +#else + + typedef int WRes; +#define MY__FACILITY_WIN32 7 +#define MY__FACILITY__WRes MY__FACILITY_WIN32 +#define MY_SRes_HRESULT_FROM_WRes(x) ((HRESULT)(x) <= 0 ? ((HRESULT)(x)) : ((HRESULT) (((x) & 0x0000FFFF) | (MY__FACILITY__WRes << 16) | 0x80000000))) + +#endif + +#ifndef RINOK +#define RINOK(x) { int __result__ = (x); if (__result__ != 0) return __result__; } +#endif + + typedef unsigned char Byte; + typedef short Int16; + typedef unsigned short UInt16; + +#ifdef _LZMA_UINT32_IS_ULONG + typedef long Int32; + typedef unsigned long UInt32; +#else + typedef int Int32; + typedef unsigned int UInt32; +#endif + +#ifdef _SZ_NO_INT_64 + + /* define _SZ_NO_INT_64, if your compiler doesn't support 64-bit integers. + NOTES: Some code will work incorrectly in that case! */ + + typedef long Int64; + typedef unsigned long UInt64; + +#else + +#if defined(_MSC_VER) || defined(__BORLANDC__) + typedef __int64 Int64; + typedef unsigned __int64 UInt64; +#define UINT64_CONST(n) n +#else + typedef long long int Int64; + typedef unsigned long long int UInt64; +#define UINT64_CONST(n) n ## ULL +#endif + +#endif + +#ifdef _LZMA_NO_SYSTEM_SIZE_T + typedef UInt32 SizeT; +#else + typedef size_t SizeT; +#endif + + typedef int BoolInt; + /* typedef BoolInt Bool; */ +#define True 1 +#define False 0 + +#ifdef _WIN32 +#define MY_STD_CALL __stdcall +#else +#define MY_STD_CALL +#endif + +#ifdef _MSC_VER + + #if _MSC_VER >= 1300 +#define MY_NO_INLINE __declspec(noinline) +#else +#define MY_NO_INLINE +#endif + +#define MY_FORCE_INLINE __forceinline + +#define MY_CDECL __cdecl +#define MY_FAST_CALL __fastcall + +#else + +#define MY_NO_INLINE +#define MY_FORCE_INLINE +#define MY_CDECL +#define MY_FAST_CALL + +/* inline keyword : for C++ / C99 */ + +/* GCC, clang: */ +/* +#if defined (__GNUC__) && (__GNUC__ >= 4) +#define MY_FORCE_INLINE __attribute__((always_inline)) +#define MY_NO_INLINE __attribute__((noinline)) +#endif +*/ + +#endif + +/* The following interfaces use first parameter as pointer to structure */ + + typedef struct IByteIn IByteIn; + struct IByteIn + { + Byte(*Read)(const IByteIn* p); /* reads one byte, returns 0 in case of EOF or error */ + }; +#define IByteIn_Read(p) (p)->Read(p) + + typedef struct IByteOut IByteOut; + struct IByteOut + { + void (*Write)(const IByteOut* p, Byte b); + }; +#define IByteOut_Write(p, b) (p)->Write(p, b) + + typedef struct ISeqInStream ISeqInStream; + struct ISeqInStream + { + SRes(*Read)(const ISeqInStream* p, void* buf, size_t* size); + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. + (output(*size) < input(*size)) is allowed */ + }; +#define ISeqInStream_Read(p, buf, size) (p)->Read(p, buf, size) + + /* it can return SZ_ERROR_INPUT_EOF */ + SRes SeqInStream_Read(const ISeqInStream* stream, void* buf, size_t size); + SRes SeqInStream_Read2(const ISeqInStream* stream, void* buf, size_t size, SRes errorType); + SRes SeqInStream_ReadByte(const ISeqInStream* stream, Byte* buf); + + typedef struct ISeqOutStream ISeqOutStream; + struct ISeqOutStream + { + size_t(*Write)(const ISeqOutStream* p, const void* buf, size_t size); + /* Returns: result - the number of actually written bytes. + (result < size) means error */ + }; +#define ISeqOutStream_Write(p, buf, size) (p)->Write(p, buf, size) + + typedef enum + { + SZ_SEEK_SET = 0, + SZ_SEEK_CUR = 1, + SZ_SEEK_END = 2 + } ESzSeek; + + typedef struct ISeekInStream ISeekInStream; + struct ISeekInStream + { + SRes(*Read)(const ISeekInStream* p, void* buf, size_t* size); /* same as ISeqInStream::Read */ + SRes(*Seek)(const ISeekInStream* p, Int64* pos, ESzSeek origin); + }; +#define ISeekInStream_Read(p, buf, size) (p)->Read(p, buf, size) +#define ISeekInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) + + typedef struct ILookInStream ILookInStream; + struct ILookInStream + { + SRes(*Look)(const ILookInStream* p, const void** buf, size_t* size); + /* if (input(*size) != 0 && output(*size) == 0) means end_of_stream. + (output(*size) > input(*size)) is not allowed + (output(*size) < input(*size)) is allowed */ + SRes(*Skip)(const ILookInStream* p, size_t offset); + /* offset must be <= output(*size) of Look */ + + SRes(*Read)(const ILookInStream* p, void* buf, size_t* size); + /* reads directly (without buffer). It's same as ISeqInStream::Read */ + SRes(*Seek)(const ILookInStream* p, Int64* pos, ESzSeek origin); + }; + +#define ILookInStream_Look(p, buf, size) (p)->Look(p, buf, size) +#define ILookInStream_Skip(p, offset) (p)->Skip(p, offset) +#define ILookInStream_Read(p, buf, size) (p)->Read(p, buf, size) +#define ILookInStream_Seek(p, pos, origin) (p)->Seek(p, pos, origin) + + SRes LookInStream_LookRead(const ILookInStream* stream, void* buf, size_t* size); + SRes LookInStream_SeekTo(const ILookInStream* stream, UInt64 offset); + + /* reads via ILookInStream::Read */ + SRes LookInStream_Read2(const ILookInStream* stream, void* buf, size_t size, SRes errorType); + SRes LookInStream_Read(const ILookInStream* stream, void* buf, size_t size); + + typedef struct + { + ILookInStream vt; + const ISeekInStream* realStream; + + size_t pos; + size_t size; /* it's data size */ + + /* the following variables must be set outside */ + Byte* buf; + size_t bufSize; + } CLookToRead2; + + void LookToRead2_CreateVTable(CLookToRead2* p, int lookahead); + +#define LookToRead2_Init(p) { (p)->pos = (p)->size = 0; } + + typedef struct + { + ISeqInStream vt; + const ILookInStream* realStream; + } CSecToLook; + + void SecToLook_CreateVTable(CSecToLook* p); + + typedef struct + { + ISeqInStream vt; + const ILookInStream* realStream; + } CSecToRead; + + void SecToRead_CreateVTable(CSecToRead* p); + + typedef struct ICompressProgress ICompressProgress; + + struct ICompressProgress + { + SRes(*Progress)(const ICompressProgress* p, UInt64 inSize, UInt64 outSize); + /* Returns: result. (result != SZ_OK) means break. + Value (UInt64)(Int64)-1 for size means unknown value. */ + }; +#define ICompressProgress_Progress(p, inSize, outSize) (p)->Progress(p, inSize, outSize) + + typedef struct ISzAlloc ISzAlloc; + typedef const ISzAlloc* ISzAllocPtr; + + struct ISzAlloc + { + void* (*Alloc)(ISzAllocPtr p, size_t size); + void (*Free)(ISzAllocPtr p, void* address); /* address can be 0 */ + }; + +#define ISzAlloc_Alloc(p, size) (p)->Alloc(p, size) +#define ISzAlloc_Free(p, a) (p)->Free(p, a) + + /* deprecated */ +#define IAlloc_Alloc(p, size) ISzAlloc_Alloc(p, size) +#define IAlloc_Free(p, a) ISzAlloc_Free(p, a) + +#ifndef MY_offsetof +#ifdef offsetof +#define MY_offsetof(type, m) offsetof(type, m) +/* +#define MY_offsetof(type, m) FIELD_OFFSET(type, m) +*/ +#else +#define MY_offsetof(type, m) ((size_t)&(((type *)0)->m)) +#endif +#endif + +#ifndef MY_container_of + +/* +#define MY_container_of(ptr, type, m) container_of(ptr, type, m) +#define MY_container_of(ptr, type, m) CONTAINING_RECORD(ptr, type, m) +#define MY_container_of(ptr, type, m) ((type *)((char *)(ptr) - offsetof(type, m))) +#define MY_container_of(ptr, type, m) (&((type *)0)->m == (ptr), ((type *)(((char *)(ptr)) - MY_offsetof(type, m)))) +*/ + +/* + GCC shows warning: "perhaps the 'offsetof' macro was used incorrectly" + GCC 3.4.4 : classes with constructor + GCC 4.8.1 : classes with non-public variable members" +*/ + +#define MY_container_of(ptr, type, m) ((type *)((char *)(1 ? (ptr) : &((type *)0)->m) - MY_offsetof(type, m))) + +#endif + +#define CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) ((type *)(ptr)) + +/* +#define CONTAINER_FROM_VTBL(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) +*/ +#define CONTAINER_FROM_VTBL(ptr, type, m) MY_container_of(ptr, type, m) + +#define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL_SIMPLE(ptr, type, m) +/* +#define CONTAINER_FROM_VTBL_CLS(ptr, type, m) CONTAINER_FROM_VTBL(ptr, type, m) +*/ + +#ifdef _WIN32 + + #define CHAR_PATH_SEPARATOR '\\' +#define WCHAR_PATH_SEPARATOR L'\\' +#define STRING_PATH_SEPARATOR "\\" +#define WSTRING_PATH_SEPARATOR L"\\" + +#else + +#define CHAR_PATH_SEPARATOR '/' +#define WCHAR_PATH_SEPARATOR L'/' +#define STRING_PATH_SEPARATOR "/" +#define WSTRING_PATH_SEPARATOR L"/" + +#endif + + EXTERN_C_END + +#endif + + /*** End of inlined file: 7zTypes.h ***/ + + + + /*** Start of inlined file: Precomp.h ***/ +#ifndef __7Z_PRECOMP_H +#define __7Z_PRECOMP_H + + +/*** Start of inlined file: Compiler.h ***/ +#ifndef __7Z_COMPILER_H +#define __7Z_COMPILER_H + +#ifdef _MSC_VER + + #ifdef UNDER_CE +#define RPC_NO_WINDOWS_H +/* #pragma warning(disable : 4115) // '_RPC_ASYNC_STATE' : named type definition in parentheses */ +#pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union +#pragma warning(disable : 4214) // nonstandard extension used : bit field types other than int +#endif + +#if _MSC_VER >= 1300 +#pragma warning(disable : 4996) // This function or variable may be unsafe +#else +#pragma warning(disable : 4511) // copy constructor could not be generated +#pragma warning(disable : 4512) // assignment operator could not be generated +#pragma warning(disable : 4514) // unreferenced inline function has been removed +#pragma warning(disable : 4702) // unreachable code +#pragma warning(disable : 4710) // not inlined +#pragma warning(disable : 4714) // function marked as __forceinline not inlined +#pragma warning(disable : 4786) // identifier was truncated to '255' characters in the debug information +#endif + +#endif + +#define UNUSED_VAR(x) (void)x; +/* #define UNUSED_VAR(x) x=x; */ + +#endif + +/*** End of inlined file: Compiler.h ***/ + +/* #include "7zTypes.h" */ + +#endif + +/*** End of inlined file: Precomp.h ***/ + + +/*** Start of inlined file: Alloc.h ***/ +#ifndef __COMMON_ALLOC_H +#define __COMMON_ALLOC_H + + EXTERN_C_BEGIN + + void* MyAlloc(size_t size); + void MyFree(void* address); + +#ifdef _WIN32 + + void SetLargePageSize(); + + void* MidAlloc(size_t size); + void MidFree(void* address); + void* BigAlloc(size_t size); + void BigFree(void* address); + +#else + +#define MidAlloc(size) MyAlloc(size) +#define MidFree(address) MyFree(address) +#define BigAlloc(size) MyAlloc(size) +#define BigFree(address) MyFree(address) + +#endif + + extern const ISzAlloc g_Alloc; + extern const ISzAlloc g_BigAlloc; + extern const ISzAlloc g_MidAlloc; + extern const ISzAlloc g_AlignedAlloc; + + typedef struct + { + ISzAlloc vt; + ISzAllocPtr baseAlloc; + unsigned numAlignBits; /* ((1 << numAlignBits) >= sizeof(void *)) */ + size_t offset; /* (offset == (k * sizeof(void *)) && offset < (1 << numAlignBits) */ + } CAlignOffsetAlloc; + + void AlignOffsetAlloc_CreateVTable(CAlignOffsetAlloc* p); + + EXTERN_C_END + +#endif + + /*** End of inlined file: Alloc.h ***/ + + + /*** Start of inlined file: LzmaDec.h ***/ +#ifndef __LZMA_DEC_H +#define __LZMA_DEC_H + + EXTERN_C_BEGIN + + /* #define _LZMA_PROB32 */ + /* _LZMA_PROB32 can increase the speed on some CPUs, + but memory usage for CLzmaDec::probs will be doubled in that case */ + + typedef +#ifdef _LZMA_PROB32 + UInt32 +#else + UInt16 +#endif + CLzmaProb; + + /* ---------- LZMA Properties ---------- */ + +#define LZMA_PROPS_SIZE 5 + + typedef struct _CLzmaProps + { + Byte lc; + Byte lp; + Byte pb; + Byte _pad_; + UInt32 dicSize; + } CLzmaProps; + + /* LzmaProps_Decode - decodes properties + Returns: + SZ_OK + SZ_ERROR_UNSUPPORTED - Unsupported properties + */ + + SRes LzmaProps_Decode(CLzmaProps* p, const Byte* data, unsigned size); + + /* ---------- LZMA Decoder state ---------- */ + + /* LZMA_REQUIRED_INPUT_MAX = number of required input bytes for worst case. + Num bits = log2((2^11 / 31) ^ 22) + 26 < 134 + 26 = 160; */ + +#define LZMA_REQUIRED_INPUT_MAX 20 + + typedef struct + { + /* Don't change this structure. ASM code can use it. */ + CLzmaProps prop; + CLzmaProb* probs; + CLzmaProb* probs_1664; + Byte* dic; + SizeT dicBufSize; + SizeT dicPos; + const Byte* buf; + UInt32 range; + UInt32 code; + UInt32 processedPos; + UInt32 checkDicSize; + UInt32 reps[4]; + UInt32 state; + UInt32 remainLen; + + UInt32 numProbs; + unsigned tempBufSize; + Byte tempBuf[LZMA_REQUIRED_INPUT_MAX]; + } CLzmaDec; + +#define LzmaDec_Construct(p) { (p)->dic = NULL; (p)->probs = NULL; } + + void LzmaDec_Init(CLzmaDec* p); + + /* There are two types of LZMA streams: + - Stream with end mark. That end mark adds about 6 bytes to compressed size. + - Stream without end mark. You must know exact uncompressed size to decompress such stream. */ + + typedef enum + { + LZMA_FINISH_ANY, /* finish at any point */ + LZMA_FINISH_END /* block must be finished at the end */ + } ELzmaFinishMode; + + /* ELzmaFinishMode has meaning only if the decoding reaches output limit !!! + + You must use LZMA_FINISH_END, when you know that current output buffer + covers last bytes of block. In other cases you must use LZMA_FINISH_ANY. + + If LZMA decoder sees end marker before reaching output limit, it returns SZ_OK, + and output value of destLen will be less than output buffer size limit. + You can check status result also. + + You can use multiple checks to test data integrity after full decompression: + 1) Check Result and "status" variable. + 2) Check that output(destLen) = uncompressedSize, if you know real uncompressedSize. + 3) Check that output(srcLen) = compressedSize, if you know real compressedSize. + You must use correct finish mode in that case. */ + + typedef enum + { + LZMA_STATUS_NOT_SPECIFIED, /* use main error code instead */ + LZMA_STATUS_FINISHED_WITH_MARK, /* stream was finished with end mark. */ + LZMA_STATUS_NOT_FINISHED, /* stream was not finished */ + LZMA_STATUS_NEEDS_MORE_INPUT, /* you must provide more input bytes */ + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK /* there is probability that stream was finished without end mark */ + } ELzmaStatus; + + /* ELzmaStatus is used only as output value for function call */ + + /* ---------- Interfaces ---------- */ + + /* There are 3 levels of interfaces: + 1) Dictionary Interface + 2) Buffer Interface + 3) One Call Interface + You can select any of these interfaces, but don't mix functions from different + groups for same object. */ + + /* There are two variants to allocate state for Dictionary Interface: + 1) LzmaDec_Allocate / LzmaDec_Free + 2) LzmaDec_AllocateProbs / LzmaDec_FreeProbs + You can use variant 2, if you set dictionary buffer manually. + For Buffer Interface you must always use variant 1. + + LzmaDec_Allocate* can return: + SZ_OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - Unsupported properties + */ + + SRes LzmaDec_AllocateProbs(CLzmaDec* p, const Byte* props, unsigned propsSize, ISzAllocPtr alloc); + void LzmaDec_FreeProbs(CLzmaDec* p, ISzAllocPtr alloc); + + SRes LzmaDec_Allocate(CLzmaDec* p, const Byte* props, unsigned propsSize, ISzAllocPtr alloc); + void LzmaDec_Free(CLzmaDec* p, ISzAllocPtr alloc); + + /* ---------- Dictionary Interface ---------- */ + + /* You can use it, if you want to eliminate the overhead for data copying from + dictionary to some other external buffer. + You must work with CLzmaDec variables directly in this interface. + + STEPS: + LzmaDec_Construct() + LzmaDec_Allocate() + for (each new stream) + { + LzmaDec_Init() + while (it needs more decompression) + { + LzmaDec_DecodeToDic() + use data from CLzmaDec::dic and update CLzmaDec::dicPos + } + } + LzmaDec_Free() + */ + + /* LzmaDec_DecodeToDic + + The decoding to internal dictionary buffer (CLzmaDec::dic). + You must manually update CLzmaDec::dicPos, if it reaches CLzmaDec::dicBufSize !!! + + finishMode: + It has meaning only if the decoding reaches output limit (dicLimit). + LZMA_FINISH_ANY - Decode just dicLimit bytes. + LZMA_FINISH_END - Stream must be finished after dicLimit. + + Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + LZMA_STATUS_NEEDS_MORE_INPUT + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK + SZ_ERROR_DATA - Data error + */ + + SRes LzmaDec_DecodeToDic(CLzmaDec* p, SizeT dicLimit, + const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status); + + /* ---------- Buffer Interface ---------- */ + + /* It's zlib-like interface. + See LzmaDec_DecodeToDic description for information about STEPS and return results, + but you must use LzmaDec_DecodeToBuf instead of LzmaDec_DecodeToDic and you don't need + to work with CLzmaDec variables manually. + + finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + LZMA_FINISH_ANY - Decode just destLen bytes. + LZMA_FINISH_END - Stream must be finished after (*destLen). + */ + + SRes LzmaDec_DecodeToBuf(CLzmaDec* p, Byte* dest, SizeT* destLen, + const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status); + + /* ---------- One Call Interface ---------- */ + + /* LzmaDecode + + finishMode: + It has meaning only if the decoding reaches output limit (*destLen). + LZMA_FINISH_ANY - Decode just destLen bytes. + LZMA_FINISH_END - Stream must be finished after (*destLen). + + Returns: + SZ_OK + status: + LZMA_STATUS_FINISHED_WITH_MARK + LZMA_STATUS_NOT_FINISHED + LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK + SZ_ERROR_DATA - Data error + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_UNSUPPORTED - Unsupported properties + SZ_ERROR_INPUT_EOF - It needs more bytes in input buffer (src). + */ + + SRes LzmaDecode(Byte* dest, SizeT* destLen, const Byte* src, SizeT* srcLen, + const Byte* propData, unsigned propSize, ELzmaFinishMode finishMode, + ELzmaStatus* status, ISzAllocPtr alloc); + + EXTERN_C_END + +#endif + + /*** End of inlined file: LzmaDec.h ***/ + + + /*** Start of inlined file: LzmaEnc.h ***/ +#ifndef __LZMA_ENC_H +#define __LZMA_ENC_H + + EXTERN_C_BEGIN + +#define LZMA_PROPS_SIZE 5 + + typedef struct _CLzmaEncProps + { + int level; /* 0 <= level <= 9 */ + UInt32 dictSize; /* (1 << 12) <= dictSize <= (1 << 27) for 32-bit version + (1 << 12) <= dictSize <= (3 << 29) for 64-bit version + default = (1 << 24) */ + int lc; /* 0 <= lc <= 8, default = 3 */ + int lp; /* 0 <= lp <= 4, default = 0 */ + int pb; /* 0 <= pb <= 4, default = 2 */ + int algo; /* 0 - fast, 1 - normal, default = 1 */ + int fb; /* 5 <= fb <= 273, default = 32 */ + int btMode; /* 0 - hashChain Mode, 1 - binTree mode - normal, default = 1 */ + int numHashBytes; /* 2, 3 or 4, default = 4 */ + UInt32 mc; /* 1 <= mc <= (1 << 30), default = 32 */ + unsigned writeEndMark; /* 0 - do not write EOPM, 1 - write EOPM, default = 0 */ + int numThreads; /* 1 or 2, default = 2 */ + + UInt64 reduceSize; /* estimated size of data that will be compressed. default = (UInt64)(Int64)-1. + Encoder uses this value to reduce dictionary size */ + } CLzmaEncProps; + + void LzmaEncProps_Init(CLzmaEncProps* p); + void LzmaEncProps_Normalize(CLzmaEncProps* p); + UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps* props2); + + /* ---------- CLzmaEncHandle Interface ---------- */ + + /* LzmaEnc* functions can return the following exit codes: + SRes: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater in props + SZ_ERROR_WRITE - ISeqOutStream write callback error + SZ_ERROR_OUTPUT_EOF - output buffer overflow - version with (Byte *) output + SZ_ERROR_PROGRESS - some break from progress callback + SZ_ERROR_THREAD - error in multithreading functions (only for Mt version) + */ + + typedef void* CLzmaEncHandle; + + CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc); + void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig); + + SRes LzmaEnc_SetProps(CLzmaEncHandle p, const CLzmaEncProps* props); + void LzmaEnc_SetDataSize(CLzmaEncHandle p, UInt64 expectedDataSiize); + SRes LzmaEnc_WriteProperties(CLzmaEncHandle p, Byte* properties, SizeT* size); + unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle p); + + SRes LzmaEnc_Encode(CLzmaEncHandle p, ISeqOutStream* outStream, ISeqInStream* inStream, + ICompressProgress* progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); + SRes LzmaEnc_MemEncode(CLzmaEncHandle p, Byte* dest, SizeT* destLen, const Byte* src, SizeT srcLen, + int writeEndMark, ICompressProgress* progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); + + /* ---------- One Call Interface ---------- */ + + SRes LzmaEncode(Byte* dest, SizeT* destLen, const Byte* src, SizeT srcLen, + const CLzmaEncProps* props, Byte* propsEncoded, SizeT* propsSize, int writeEndMark, + ICompressProgress* progress, ISzAllocPtr alloc, ISzAllocPtr allocBig); + + EXTERN_C_END + +#endif + + /*** End of inlined file: LzmaEnc.h ***/ + + + /*** Start of inlined file: LzmaLib.h ***/ +#ifndef __LZMA_LIB_H +#define __LZMA_LIB_H + + EXTERN_C_BEGIN + +#define MY_STDAPI int MY_STD_CALL + +#define LZMA_PROPS_SIZE 5 + + /* + RAM requirements for LZMA: + for compression: (dictSize * 11.5 + 6 MB) + state_size + for decompression: dictSize + state_size + state_size = (4 + (1.5 << (lc + lp))) KB + by default (lc=3, lp=0), state_size = 16 KB. + + LZMA properties (5 bytes) format + Offset Size Description + 0 1 lc, lp and pb in encoded form. + 1 4 dictSize (little endian). + */ + + /* + LzmaCompress + ------------ + + outPropsSize - + In: the pointer to the size of outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. + Out: the pointer to the size of written properties in outProps buffer; *outPropsSize = LZMA_PROPS_SIZE = 5. + + LZMA Encoder will use defult values for any parameter, if it is + -1 for any from: level, loc, lp, pb, fb, numThreads + 0 for dictSize + + level - compression level: 0 <= level <= 9; + + level dictSize algo fb + 0: 16 KB 0 32 + 1: 64 KB 0 32 + 2: 256 KB 0 32 + 3: 1 MB 0 32 + 4: 4 MB 0 32 + 5: 16 MB 1 32 + 6: 32 MB 1 32 + 7+: 64 MB 1 64 + + The default value for "level" is 5. + + algo = 0 means fast method + algo = 1 means normal method + + dictSize - The dictionary size in bytes. The maximum value is + 128 MB = (1 << 27) bytes for 32-bit version + 1 GB = (1 << 30) bytes for 64-bit version + The default value is 16 MB = (1 << 24) bytes. + It's recommended to use the dictionary that is larger than 4 KB and + that can be calculated as (1 << N) or (3 << N) sizes. + + lc - The number of literal context bits (high bits of previous literal). + It can be in the range from 0 to 8. The default value is 3. + Sometimes lc=4 gives the gain for big files. + + lp - The number of literal pos bits (low bits of current position for literals). + It can be in the range from 0 to 4. The default value is 0. + The lp switch is intended for periodical data when the period is equal to 2^lp. + For example, for 32-bit (4 bytes) periodical data you can use lp=2. Often it's + better to set lc=0, if you change lp switch. + + pb - The number of pos bits (low bits of current position). + It can be in the range from 0 to 4. The default value is 2. + The pb switch is intended for periodical data when the period is equal 2^pb. + + fb - Word size (the number of fast bytes). + It can be in the range from 5 to 273. The default value is 32. + Usually, a big number gives a little bit better compression ratio and + slower compression process. + + numThreads - The number of thereads. 1 or 2. The default value is 2. + Fast mode (algo = 0) can use only 1 thread. + + Out: + destLen - processed output size + Returns: + SZ_OK - OK + SZ_ERROR_MEM - Memory allocation error + SZ_ERROR_PARAM - Incorrect paramater + SZ_ERROR_OUTPUT_EOF - output buffer overflow + SZ_ERROR_THREAD - errors in multithreading functions (only for Mt version) + */ + + MY_STDAPI LzmaCompress(unsigned char* dest, size_t* destLen, const unsigned char* src, size_t srcLen, + unsigned char* outProps, size_t* outPropsSize, /* *outPropsSize must be = 5 */ + int level, /* 0 <= level <= 9, default = 5 */ + unsigned dictSize, /* default = (1 << 24) */ + int lc, /* 0 <= lc <= 8, default = 3 */ + int lp, /* 0 <= lp <= 4, default = 0 */ + int pb, /* 0 <= pb <= 4, default = 2 */ + int fb, /* 5 <= fb <= 273, default = 32 */ + int numThreads /* 1 or 2, default = 2 */ + ); + + /* + LzmaUncompress + -------------- + In: + dest - output data + destLen - output data size + src - input data + srcLen - input data size + Out: + destLen - processed output size + srcLen - processed input size + Returns: + SZ_OK - OK + SZ_ERROR_DATA - Data error + SZ_ERROR_MEM - Memory allocation arror + SZ_ERROR_UNSUPPORTED - Unsupported properties + SZ_ERROR_INPUT_EOF - it needs more bytes in input buffer (src) + */ + + MY_STDAPI LzmaUncompress(unsigned char* dest, size_t* destLen, const unsigned char* src, SizeT* srcLen, + const unsigned char* props, size_t propsSize); + + EXTERN_C_END + +#endif + + /*** End of inlined file: LzmaLib.h ***/ + +#ifdef POCKETLZMA_LZMA_C_DEFINE + + /*** Start of inlined file: Alloc.c ***/ +//#include +// +//#ifdef _WIN32 +//#include +//#endif +//#include + +/* #define _SZ_ALLOC_DEBUG */ + +/* use _SZ_ALLOC_DEBUG to debug alloc/free operations */ +#ifdef _SZ_ALLOC_DEBUG + +#include +int g_allocCount = 0; + int g_allocCountMid = 0; + int g_allocCountBig = 0; + +#define CONVERT_INT_TO_STR(charType, tempSize) \ + unsigned char temp[tempSize]; unsigned i = 0; \ + while (val >= 10) { temp[i++] = (unsigned char)('0' + (unsigned)(val % 10)); val /= 10; } \ + *s++ = (charType)('0' + (unsigned)val); \ + while (i != 0) { i--; *s++ = temp[i]; } \ + *s = 0; + + static void ConvertUInt64ToString(UInt64 val, char* s) + { + CONVERT_INT_TO_STR(char, 24); + } + +#define GET_HEX_CHAR(t) ((char)(((t < 10) ? ('0' + t) : ('A' + (t - 10))))) + + static void ConvertUInt64ToHex(UInt64 val, char* s) + { + UInt64 v = val; + unsigned i; + for (i = 1;; i++) + { + v >>= 4; + if (v == 0) + break; + } + s[i] = 0; + do + { + unsigned t = (unsigned)(val & 0xF); + val >>= 4; + s[--i] = GET_HEX_CHAR(t); + } while (i); + } + +#define DEBUG_OUT_STREAM stderr + + static void Print(const char* s) + { + fputs(s, DEBUG_OUT_STREAM); + } + + static void PrintAligned(const char* s, size_t align) + { + size_t len = strlen(s); + for (;;) + { + fputc(' ', DEBUG_OUT_STREAM); + if (len >= align) + break; + ++len; + } + Print(s); + } + + static void PrintLn() + { + Print("\n"); + } + + static void PrintHex(UInt64 v, size_t align) + { + char s[32]; + ConvertUInt64ToHex(v, s); + PrintAligned(s, align); + } + + static void PrintDec(UInt64 v, size_t align) + { + char s[32]; + ConvertUInt64ToString(v, s); + PrintAligned(s, align); + } + + static void PrintAddr(void* p) + { + PrintHex((UInt64)(size_t)(ptrdiff_t)p, 12); + } + +#define PRINT_ALLOC(name, cnt, size, ptr) \ + Print(name " "); \ + PrintDec(cnt++, 10); \ + PrintHex(size, 10); \ + PrintAddr(ptr); \ + PrintLn(); + +#define PRINT_FREE(name, cnt, ptr) if (ptr) { \ + Print(name " "); \ + PrintDec(--cnt, 10); \ + PrintAddr(ptr); \ + PrintLn(); } + +#else + +#define PRINT_ALLOC(name, cnt, size, ptr) +#define PRINT_FREE(name, cnt, ptr) +#define Print(s) +#define PrintLn() +#define PrintHex(v, align) +#define PrintDec(v, align) +#define PrintAddr(p) + +#endif + + void* MyAlloc(size_t size) + { + if (size == 0) + return NULL; +#ifdef _SZ_ALLOC_DEBUG + { + void* p = malloc(size); + PRINT_ALLOC("Alloc ", g_allocCount, size, p); + return p; + } +#else + return malloc(size); +#endif + } + + void MyFree(void* address) + { + PRINT_FREE("Free ", g_allocCount, address); + + free(address); + } + +#ifdef _WIN32 + + void* MidAlloc(size_t size) + { + if (size == 0) + return NULL; + + PRINT_ALLOC("Alloc-Mid", g_allocCountMid, size, NULL); + + return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); + } + + void MidFree(void* address) + { + PRINT_FREE("Free-Mid", g_allocCountMid, address); + + if (!address) + return; + VirtualFree(address, 0, MEM_RELEASE); + } + +#ifndef MEM_LARGE_PAGES +#undef _7ZIP_LARGE_PAGES +#endif + +#ifdef _7ZIP_LARGE_PAGES + SIZE_T g_LargePageSize = 0; + typedef SIZE_T(WINAPI* GetLargePageMinimumP)(); +#endif + + void SetLargePageSize() + { +#ifdef _7ZIP_LARGE_PAGES + SIZE_T size; + GetLargePageMinimumP largePageMinimum = (GetLargePageMinimumP) + GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetLargePageMinimum"); + if (!largePageMinimum) + return; + size = largePageMinimum(); + if (size == 0 || (size & (size - 1)) != 0) + return; + g_LargePageSize = size; +#endif + } + + void* BigAlloc(size_t size) + { + if (size == 0) + return NULL; + + PRINT_ALLOC("Alloc-Big", g_allocCountBig, size, NULL); + +#ifdef _7ZIP_LARGE_PAGES + { + SIZE_T ps = g_LargePageSize; + if (ps != 0 && ps <= (1 << 30) && size > (ps / 2)) + { + size_t size2; + ps--; + size2 = (size + ps) & ~ps; + if (size2 >= size) + { + void* res = VirtualAlloc(NULL, size2, MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE); + if (res) + return res; + } + } + } +#endif + + return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); + } + + void BigFree(void* address) + { + PRINT_FREE("Free-Big", g_allocCountBig, address); + + if (!address) + return; + VirtualFree(address, 0, MEM_RELEASE); + } + +#endif + + static void* SzAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return MyAlloc(size); } + static void SzFree(ISzAllocPtr p, void* address) { UNUSED_VAR(p); MyFree(address); } + const ISzAlloc g_Alloc = { SzAlloc, SzFree }; + + static void* SzMidAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return MidAlloc(size); } + static void SzMidFree(ISzAllocPtr p, void* address) { UNUSED_VAR(p); MidFree(address); } + const ISzAlloc g_MidAlloc = { SzMidAlloc, SzMidFree }; + + static void* SzBigAlloc(ISzAllocPtr p, size_t size) { UNUSED_VAR(p); return BigAlloc(size); } + static void SzBigFree(ISzAllocPtr p, void* address) { UNUSED_VAR(p); BigFree(address); } + const ISzAlloc g_BigAlloc = { SzBigAlloc, SzBigFree }; + + /* + uintptr_t : C99 (optional) + : unsupported in VS6 + */ + +#ifdef _WIN32 + typedef UINT_PTR UIntPtr; +#else + /* + typedef uintptr_t UIntPtr; + */ + typedef ptrdiff_t UIntPtr; +#endif + +#define ADJUST_ALLOC_SIZE 0 + /* + #define ADJUST_ALLOC_SIZE (sizeof(void *) - 1) + */ + /* + Use (ADJUST_ALLOC_SIZE = (sizeof(void *) - 1)), if + MyAlloc() can return address that is NOT multiple of sizeof(void *). + */ + + /* + #define MY_ALIGN_PTR_DOWN(p, align) ((void *)((char *)(p) - ((size_t)(UIntPtr)(p) & ((align) - 1)))) + */ +#define MY_ALIGN_PTR_DOWN(p, align) ((void *)((((UIntPtr)(p)) & ~((UIntPtr)(align) - 1)))) + +#define MY_ALIGN_PTR_UP_PLUS(p, align) MY_ALIGN_PTR_DOWN(((char *)(p) + (align) + ADJUST_ALLOC_SIZE), align) + +#if (_POSIX_C_SOURCE >= 200112L) && !defined(_WIN32) +#define USE_posix_memalign +#endif + + /* + This posix_memalign() is for test purposes only. + We also need special Free() function instead of free(), + if this posix_memalign() is used. + */ + + /* + static int posix_memalign(void **ptr, size_t align, size_t size) + { + size_t newSize = size + align; + void *p; + void *pAligned; + *ptr = NULL; + if (newSize < size) + return 12; // ENOMEM + p = MyAlloc(newSize); + if (!p) + return 12; // ENOMEM + pAligned = MY_ALIGN_PTR_UP_PLUS(p, align); + ((void **)pAligned)[-1] = p; + *ptr = pAligned; + return 0; + } + */ + + /* + ALLOC_ALIGN_SIZE >= sizeof(void *) + ALLOC_ALIGN_SIZE >= cache_line_size + */ + +#define ALLOC_ALIGN_SIZE ((size_t)1 << 7) + + static void* SzAlignedAlloc(ISzAllocPtr pp, size_t size) + { +#ifndef USE_posix_memalign + + void* p; + void* pAligned; + size_t newSize; + UNUSED_VAR(pp); + + /* also we can allocate additional dummy ALLOC_ALIGN_SIZE bytes after aligned + block to prevent cache line sharing with another allocated blocks */ + + newSize = size + ALLOC_ALIGN_SIZE * 1 + ADJUST_ALLOC_SIZE; + if (newSize < size) + return NULL; + + p = MyAlloc(newSize); + + if (!p) + return NULL; + pAligned = MY_ALIGN_PTR_UP_PLUS(p, ALLOC_ALIGN_SIZE); + + Print(" size="); PrintHex(size, 8); + Print(" a_size="); PrintHex(newSize, 8); + Print(" ptr="); PrintAddr(p); + Print(" a_ptr="); PrintAddr(pAligned); + PrintLn(); + + ((void**)pAligned)[-1] = p; + + return pAligned; + +#else + + void* p; + UNUSED_VAR(pp); + if (posix_memalign(&p, ALLOC_ALIGN_SIZE, size)) + return NULL; + + Print(" posix_memalign="); PrintAddr(p); + PrintLn(); + + return p; + +#endif + } + + static void SzAlignedFree(ISzAllocPtr pp, void* address) + { + UNUSED_VAR(pp); +#ifndef USE_posix_memalign + if (address) + MyFree(((void**)address)[-1]); +#else + free(address); +#endif + } + + const ISzAlloc g_AlignedAlloc = { SzAlignedAlloc, SzAlignedFree }; + +#define MY_ALIGN_PTR_DOWN_1(p) MY_ALIGN_PTR_DOWN(p, sizeof(void *)) + + /* we align ptr to support cases where CAlignOffsetAlloc::offset is not multiply of sizeof(void *) */ +#define REAL_BLOCK_PTR_VAR(p) ((void **)MY_ALIGN_PTR_DOWN_1(p))[-1] +/* +#define REAL_BLOCK_PTR_VAR(p) ((void **)(p))[-1] +*/ + + static void* AlignOffsetAlloc_Alloc(ISzAllocPtr pp, size_t size) + { + CAlignOffsetAlloc* p = CONTAINER_FROM_VTBL(pp, CAlignOffsetAlloc, vt); + void* adr; + void* pAligned; + size_t newSize; + size_t extra; + size_t alignSize = (size_t)1 << p->numAlignBits; + + if (alignSize < sizeof(void*)) + alignSize = sizeof(void*); + + if (p->offset >= alignSize) + return NULL; + + /* also we can allocate additional dummy ALLOC_ALIGN_SIZE bytes after aligned + block to prevent cache line sharing with another allocated blocks */ + extra = p->offset & (sizeof(void*) - 1); + newSize = size + alignSize + extra + ADJUST_ALLOC_SIZE; + if (newSize < size) + return NULL; + + adr = ISzAlloc_Alloc(p->baseAlloc, newSize); + + if (!adr) + return NULL; + + pAligned = (char*)MY_ALIGN_PTR_DOWN((char*)adr + + alignSize - p->offset + extra + ADJUST_ALLOC_SIZE, alignSize) + p->offset; + + PrintLn(); + Print("- Aligned: "); + Print(" size="); PrintHex(size, 8); + Print(" a_size="); PrintHex(newSize, 8); + Print(" ptr="); PrintAddr(adr); + Print(" a_ptr="); PrintAddr(pAligned); + PrintLn(); + + REAL_BLOCK_PTR_VAR(pAligned) = adr; + + return pAligned; + } + + static void AlignOffsetAlloc_Free(ISzAllocPtr pp, void* address) + { + if (address) + { + CAlignOffsetAlloc* p = CONTAINER_FROM_VTBL(pp, CAlignOffsetAlloc, vt); + PrintLn(); + Print("- Aligned Free: "); + PrintLn(); + ISzAlloc_Free(p->baseAlloc, REAL_BLOCK_PTR_VAR(address)); + } + } + + void AlignOffsetAlloc_CreateVTable(CAlignOffsetAlloc* p) + { + p->vt.Alloc = AlignOffsetAlloc_Alloc; + p->vt.Free = AlignOffsetAlloc_Free; + } + + /*** End of inlined file: Alloc.c ***/ + + + + /*** Start of inlined file: LzmaDec.c ***/ + //#include + + /* #include "CpuArch.h" */ + +#define kNumTopBits 24 +#define kTopValue ((UInt32)1 << kNumTopBits) + +#define kNumBitModelTotalBits 11 +#define kBitModelTotal (1 << kNumBitModelTotalBits) +#define kNumMoveBits 5 + +#define RC_INIT_SIZE 5 + +#define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); } + +#define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound) +#define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); +#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); +#define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \ + { UPDATE_0(p); i = (i + i); A0; } else \ + { UPDATE_1(p); i = (i + i) + 1; A1; } + +#define TREE_GET_BIT(probs, i) { GET_BIT2(probs + i, i, ;, ;); } + +#define REV_BIT(p, i, A0, A1) IF_BIT_0(p + i) \ + { UPDATE_0(p + i); A0; } else \ + { UPDATE_1(p + i); A1; } +#define REV_BIT_VAR( p, i, m) REV_BIT(p, i, i += m; m += m, m += m; i += m; ) +#define REV_BIT_CONST(p, i, m) REV_BIT(p, i, i += m; , i += m * 2; ) +#define REV_BIT_LAST( p, i, m) REV_BIT(p, i, i -= m , ; ) + +#define TREE_DECODE(probs, limit, i) \ + { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; } + +/* #define _LZMA_SIZE_OPT */ + +#ifdef _LZMA_SIZE_OPT +#define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i) +#else +#define TREE_6_DECODE(probs, i) \ + { i = 1; \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + TREE_GET_BIT(probs, i); \ + i -= 0x40; } +#endif + +#define NORMAL_LITER_DEC TREE_GET_BIT(prob, symbol) +#define MATCHED_LITER_DEC \ + matchByte += matchByte; \ + bit = offs; \ + offs &= matchByte; \ + probLit = prob + (offs + bit + symbol); \ + GET_BIT2(probLit, symbol, offs ^= bit; , ;) + +#define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); } + +#define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * (UInt32)ttt; if (code < bound) +#define UPDATE_0_CHECK range = bound; +#define UPDATE_1_CHECK range -= bound; code -= bound; +#define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \ + { UPDATE_0_CHECK; i = (i + i); A0; } else \ + { UPDATE_1_CHECK; i = (i + i) + 1; A1; } +#define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;) +#define TREE_DECODE_CHECK(probs, limit, i) \ + { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; } + +#define REV_BIT_CHECK(p, i, m) IF_BIT_0_CHECK(p + i) \ + { UPDATE_0_CHECK; i += m; m += m; } else \ + { UPDATE_1_CHECK; m += m; i += m; } + +#define kNumPosBitsMax 4 +#define kNumPosStatesMax (1 << kNumPosBitsMax) + +#define kLenNumLowBits 3 +#define kLenNumLowSymbols (1 << kLenNumLowBits) +#define kLenNumHighBits 8 +#define kLenNumHighSymbols (1 << kLenNumHighBits) + +#define LenLow 0 +#define LenHigh (LenLow + 2 * (kNumPosStatesMax << kLenNumLowBits)) +#define kNumLenProbs (LenHigh + kLenNumHighSymbols) + +#define LenChoice LenLow +#define LenChoice2 (LenLow + (1 << kLenNumLowBits)) + +#define kNumStates 12 +#define kNumStates2 16 +#define kNumLitStates 7 + +#define kStartPosModelIndex 4 +#define kEndPosModelIndex 14 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) + +#define kNumPosSlotBits 6 +#define kNumLenToPosStates 4 + +#define kNumAlignBits 4 +#define kAlignTableSize (1 << kNumAlignBits) + +#define kMatchMinLen 2 +#define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols * 2 + kLenNumHighSymbols) + +/* External ASM code needs same CLzmaProb array layout. So don't change it. */ + +/* (probs_1664) is faster and better for code size at some platforms */ +/* +#ifdef MY_CPU_X86_OR_AMD64 +*/ +#define kStartOffset 1664 +#define GET_PROBS p->probs_1664 +/* +#define GET_PROBS p->probs + kStartOffset +#else +#define kStartOffset 0 +#define GET_PROBS p->probs +#endif +*/ + +#define SpecPos (-kStartOffset) +#define IsRep0Long (SpecPos + kNumFullDistances) +#define RepLenCoder (IsRep0Long + (kNumStates2 << kNumPosBitsMax)) +#define LenCoder (RepLenCoder + kNumLenProbs) +#define IsMatch (LenCoder + kNumLenProbs) +#define Align (IsMatch + (kNumStates2 << kNumPosBitsMax)) +#define IsRep (Align + kAlignTableSize) +#define IsRepG0 (IsRep + kNumStates) +#define IsRepG1 (IsRepG0 + kNumStates) +#define IsRepG2 (IsRepG1 + kNumStates) +#define PosSlot (IsRepG2 + kNumStates) +#define Literal (PosSlot + (kNumLenToPosStates << kNumPosSlotBits)) +#define NUM_BASE_PROBS (Literal + kStartOffset) + +#if Align != 0 && kStartOffset != 0 +#error Stop_Compiling_Bad_LZMA_kAlign +#endif + +#if NUM_BASE_PROBS != 1984 +#error Stop_Compiling_Bad_LZMA_PROBS +#endif + +#define LZMA_LIT_SIZE 0x300 + +#define LzmaProps_GetNumProbs(p) (NUM_BASE_PROBS + ((UInt32)LZMA_LIT_SIZE << ((p)->lc + (p)->lp))) + +#define CALC_POS_STATE(processedPos, pbMask) (((processedPos) & (pbMask)) << 4) +#define COMBINED_PS_STATE (posState + state) +#define GET_LEN_STATE (posState) + +#define LZMA_DIC_MIN (1 << 12) + +/* +p->remainLen : shows status of LZMA decoder: + < kMatchSpecLenStart : normal remain + = kMatchSpecLenStart : finished + = kMatchSpecLenStart + 1 : need init range coder + = kMatchSpecLenStart + 2 : need init range coder and state +*/ + +/* ---------- LZMA_DECODE_REAL ---------- */ +/* +LzmaDec_DecodeReal_3() can be implemented in external ASM file. +3 - is the code compatibility version of that function for check at link time. +*/ + +#define LZMA_DECODE_REAL LzmaDec_DecodeReal_3 + +/* +LZMA_DECODE_REAL() +In: + RangeCoder is normalized + if (p->dicPos == limit) + { + LzmaDec_TryDummy() was called before to exclude LITERAL and MATCH-REP cases. + So first symbol can be only MATCH-NON-REP. And if that MATCH-NON-REP symbol + is not END_OF_PAYALOAD_MARKER, then function returns error code. + } + +Processing: + first LZMA symbol will be decoded in any case + All checks for limits are at the end of main loop, + It will decode new LZMA-symbols while (p->buf < bufLimit && dicPos < limit), + RangeCoder is still without last normalization when (p->buf < bufLimit) is being checked. + +Out: + RangeCoder is normalized + Result: + SZ_OK - OK + SZ_ERROR_DATA - Error + p->remainLen: + < kMatchSpecLenStart : normal remain + = kMatchSpecLenStart : finished +*/ + +#ifdef _LZMA_DEC_OPT + + int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec* p, SizeT limit, const Byte* bufLimit); + +#else + + static + int MY_FAST_CALL LZMA_DECODE_REAL(CLzmaDec* p, SizeT limit, const Byte* bufLimit) + { + CLzmaProb* probs = GET_PROBS; + unsigned state = (unsigned)p->state; + UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3]; + unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1; + unsigned lc = p->prop.lc; + unsigned lpMask = ((unsigned)0x100 << p->prop.lp) - ((unsigned)0x100 >> lc); + + Byte* dic = p->dic; + SizeT dicBufSize = p->dicBufSize; + SizeT dicPos = p->dicPos; + + UInt32 processedPos = p->processedPos; + UInt32 checkDicSize = p->checkDicSize; + unsigned len = 0; + + const Byte* buf = p->buf; + UInt32 range = p->range; + UInt32 code = p->code; + + do + { + CLzmaProb* prob; + UInt32 bound; + unsigned ttt; + unsigned posState = CALC_POS_STATE(processedPos, pbMask); + + prob = probs + IsMatch + COMBINED_PS_STATE; + IF_BIT_0(prob) + { + unsigned symbol; + UPDATE_0(prob); + prob = probs + Literal; + if (processedPos != 0 || checkDicSize != 0) + prob += (UInt32)3 * ((((processedPos << 8) + dic[(dicPos == 0 ? dicBufSize : dicPos) - 1]) & lpMask) << lc); + processedPos++; + + if (state < kNumLitStates) + { + state -= (state < 4) ? state : 3; + symbol = 1; +#ifdef _LZMA_SIZE_OPT + do { NORMAL_LITER_DEC } while (symbol < 0x100); +#else + NORMAL_LITER_DEC + NORMAL_LITER_DEC + NORMAL_LITER_DEC + NORMAL_LITER_DEC + NORMAL_LITER_DEC + NORMAL_LITER_DEC + NORMAL_LITER_DEC + NORMAL_LITER_DEC +#endif + } + else + { + unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)]; + unsigned offs = 0x100; + state -= (state < 10) ? 3 : 6; + symbol = 1; +#ifdef _LZMA_SIZE_OPT + do + { + unsigned bit; + CLzmaProb* probLit; + MATCHED_LITER_DEC + } while (symbol < 0x100); +#else + { + unsigned bit; + CLzmaProb* probLit; + MATCHED_LITER_DEC + MATCHED_LITER_DEC + MATCHED_LITER_DEC + MATCHED_LITER_DEC + MATCHED_LITER_DEC + MATCHED_LITER_DEC + MATCHED_LITER_DEC + MATCHED_LITER_DEC + } +#endif + } + + dic[dicPos++] = (Byte)symbol; + continue; + } + + { + UPDATE_1(prob); + prob = probs + IsRep + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + state += kNumStates; + prob = probs + LenCoder; + } + else + { + UPDATE_1(prob); + /* + // that case was checked before with kBadRepCode + if (checkDicSize == 0 && processedPos == 0) + return SZ_ERROR_DATA; + */ + prob = probs + IsRepG0 + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + prob = probs + IsRep0Long + COMBINED_PS_STATE; + IF_BIT_0(prob) + { + UPDATE_0(prob); + dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)]; + dicPos++; + processedPos++; + state = state < kNumLitStates ? 9 : 11; + continue; + } + UPDATE_1(prob); + } + else + { + UInt32 distance; + UPDATE_1(prob); + prob = probs + IsRepG1 + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + distance = rep1; + } + else + { + UPDATE_1(prob); + prob = probs + IsRepG2 + state; + IF_BIT_0(prob) + { + UPDATE_0(prob); + distance = rep2; + } + else + { + UPDATE_1(prob); + distance = rep3; + rep3 = rep2; + } + rep2 = rep1; + } + rep1 = rep0; + rep0 = distance; + } + state = state < kNumLitStates ? 8 : 11; + prob = probs + RepLenCoder; + } + +#ifdef _LZMA_SIZE_OPT + { + unsigned lim, offset; + CLzmaProb* probLen = prob + LenChoice; + IF_BIT_0(probLen) + { + UPDATE_0(probLen); + probLen = prob + LenLow + GET_LEN_STATE; + offset = 0; + lim = (1 << kLenNumLowBits); + } + else + { + UPDATE_1(probLen); + probLen = prob + LenChoice2; + IF_BIT_0(probLen) + { + UPDATE_0(probLen); + probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); + offset = kLenNumLowSymbols; + lim = (1 << kLenNumLowBits); + } + else + { + UPDATE_1(probLen); + probLen = prob + LenHigh; + offset = kLenNumLowSymbols * 2; + lim = (1 << kLenNumHighBits); + } + } + TREE_DECODE(probLen, lim, len); + len += offset; + } +#else + { + CLzmaProb* probLen = prob + LenChoice; + IF_BIT_0(probLen) + { + UPDATE_0(probLen); + probLen = prob + LenLow + GET_LEN_STATE; + len = 1; + TREE_GET_BIT(probLen, len); + TREE_GET_BIT(probLen, len); + TREE_GET_BIT(probLen, len); + len -= 8; + } + else + { + UPDATE_1(probLen); + probLen = prob + LenChoice2; + IF_BIT_0(probLen) + { + UPDATE_0(probLen); + probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); + len = 1; + TREE_GET_BIT(probLen, len); + TREE_GET_BIT(probLen, len); + TREE_GET_BIT(probLen, len); + } + else + { + UPDATE_1(probLen); + probLen = prob + LenHigh; + TREE_DECODE(probLen, (1 << kLenNumHighBits), len); + len += kLenNumLowSymbols * 2; + } + } + } +#endif + + if (state >= kNumStates) + { + UInt32 distance; + prob = probs + PosSlot + + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits); + TREE_6_DECODE(prob, distance); + if (distance >= kStartPosModelIndex) + { + unsigned posSlot = (unsigned)distance; + unsigned numDirectBits = (unsigned)(((distance >> 1) - 1)); + distance = (2 | (distance & 1)); + if (posSlot < kEndPosModelIndex) + { + distance <<= numDirectBits; + prob = probs + SpecPos; + { + UInt32 m = 1; + distance++; + do + { + REV_BIT_VAR(prob, distance, m); + } while (--numDirectBits); + distance -= m; + } + } + else + { + numDirectBits -= kNumAlignBits; + do + { + NORMALIZE + range >>= 1; + + { + UInt32 t; + code -= range; + t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */ + distance = (distance << 1) + (t + 1); + code += range & t; + } + /* + distance <<= 1; + if (code >= range) + { + code -= range; + distance |= 1; + } + */ + } while (--numDirectBits); + prob = probs + Align; + distance <<= kNumAlignBits; + { + unsigned i = 1; + REV_BIT_CONST(prob, i, 1); + REV_BIT_CONST(prob, i, 2); + REV_BIT_CONST(prob, i, 4); + REV_BIT_LAST(prob, i, 8); + distance |= i; + } + if (distance == (UInt32)0xFFFFFFFF) + { + len = kMatchSpecLenStart; + state -= kNumStates; + break; + } + } + } + + rep3 = rep2; + rep2 = rep1; + rep1 = rep0; + rep0 = distance + 1; + state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3; + if (distance >= (checkDicSize == 0 ? processedPos : checkDicSize)) + { + p->dicPos = dicPos; + return SZ_ERROR_DATA; + } + } + + len += kMatchMinLen; + + { + SizeT rem; + unsigned curLen; + SizeT pos; + + if ((rem = limit - dicPos) == 0) + { + p->dicPos = dicPos; + return SZ_ERROR_DATA; + } + + curLen = ((rem < len) ? (unsigned)rem : len); + pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0); + + processedPos += (UInt32)curLen; + + len -= curLen; + if (curLen <= dicBufSize - pos) + { + Byte* dest = dic + dicPos; + ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos; + const Byte* lim = dest + curLen; + dicPos += (SizeT)curLen; + do + *(dest) = (Byte) * (dest + src); + while (++dest != lim); + } + else + { + do + { + dic[dicPos++] = dic[pos]; + if (++pos == dicBufSize) + pos = 0; + } while (--curLen != 0); + } + } + } + } while (dicPos < limit && buf < bufLimit); + + NORMALIZE; + + p->buf = buf; + p->range = range; + p->code = code; + p->remainLen = (UInt32)len; + p->dicPos = dicPos; + p->processedPos = processedPos; + p->reps[0] = rep0; + p->reps[1] = rep1; + p->reps[2] = rep2; + p->reps[3] = rep3; + p->state = (UInt32)state; + + return SZ_OK; + } +#endif + + static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec* p, SizeT limit) + { + if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart) + { + Byte* dic = p->dic; + SizeT dicPos = p->dicPos; + SizeT dicBufSize = p->dicBufSize; + unsigned len = (unsigned)p->remainLen; + SizeT rep0 = p->reps[0]; /* we use SizeT to avoid the BUG of VC14 for AMD64 */ + SizeT rem = limit - dicPos; + if (rem < len) + len = (unsigned)(rem); + + if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len) + p->checkDicSize = p->prop.dicSize; + + p->processedPos += (UInt32)len; + p->remainLen -= (UInt32)len; + while (len != 0) + { + len--; + dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)]; + dicPos++; + } + p->dicPos = dicPos; + } + } + +#define kRange0 0xFFFFFFFF +#define kBound0 ((kRange0 >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1)) +#define kBadRepCode (kBound0 + (((kRange0 - kBound0) >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1))) +#if kBadRepCode != (0xC0000000 - 0x400) +#error Stop_Compiling_Bad_LZMA_Check +#endif + + static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec* p, SizeT limit, const Byte* bufLimit) + { + do + { + SizeT limit2 = limit; + if (p->checkDicSize == 0) + { + UInt32 rem = p->prop.dicSize - p->processedPos; + if (limit - p->dicPos > rem) + limit2 = p->dicPos + rem; + + if (p->processedPos == 0) + if (p->code >= kBadRepCode) + return SZ_ERROR_DATA; + } + + RINOK(LZMA_DECODE_REAL(p, limit2, bufLimit)); + + if (p->checkDicSize == 0 && p->processedPos >= p->prop.dicSize) + p->checkDicSize = p->prop.dicSize; + + LzmaDec_WriteRem(p, limit); + } while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart); + + return 0; + } + + typedef enum + { + DUMMY_ERROR, /* unexpected end of input stream */ + DUMMY_LIT, + DUMMY_MATCH, + DUMMY_REP + } ELzmaDummy; + + static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec* p, const Byte* buf, SizeT inSize) + { + UInt32 range = p->range; + UInt32 code = p->code; + const Byte* bufLimit = buf + inSize; + const CLzmaProb* probs = GET_PROBS; + unsigned state = (unsigned)p->state; + ELzmaDummy res; + + { + const CLzmaProb* prob; + UInt32 bound; + unsigned ttt; + unsigned posState = CALC_POS_STATE(p->processedPos, (1 << p->prop.pb) - 1); + + prob = probs + IsMatch + COMBINED_PS_STATE; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK + + /* if (bufLimit - buf >= 7) return DUMMY_LIT; */ + + prob = probs + Literal; + if (p->checkDicSize != 0 || p->processedPos != 0) + prob += ((UInt32)LZMA_LIT_SIZE * + ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) + + (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc)))); + + if (state < kNumLitStates) + { + unsigned symbol = 1; + do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100); + } + else + { + unsigned matchByte = p->dic[p->dicPos - p->reps[0] + + (p->dicPos < p->reps[0] ? p->dicBufSize : 0)]; + unsigned offs = 0x100; + unsigned symbol = 1; + do + { + unsigned bit; + const CLzmaProb* probLit; + matchByte += matchByte; + bit = offs; + offs &= matchByte; + probLit = prob + (offs + bit + symbol); + GET_BIT2_CHECK(probLit, symbol, offs ^= bit;, ; ) + } while (symbol < 0x100); + } + res = DUMMY_LIT; + } + else + { + unsigned len; + UPDATE_1_CHECK; + + prob = probs + IsRep + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + state = 0; + prob = probs + LenCoder; + res = DUMMY_MATCH; + } + else + { + UPDATE_1_CHECK; + res = DUMMY_REP; + prob = probs + IsRepG0 + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + prob = probs + IsRep0Long + COMBINED_PS_STATE; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + NORMALIZE_CHECK; + return DUMMY_REP; + } + else + { + UPDATE_1_CHECK; + } + } + else + { + UPDATE_1_CHECK; + prob = probs + IsRepG1 + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + } + else + { + UPDATE_1_CHECK; + prob = probs + IsRepG2 + state; + IF_BIT_0_CHECK(prob) + { + UPDATE_0_CHECK; + } + else + { + UPDATE_1_CHECK; + } + } + } + state = kNumStates; + prob = probs + RepLenCoder; + } + { + unsigned limit, offset; + const CLzmaProb* probLen = prob + LenChoice; + IF_BIT_0_CHECK(probLen) + { + UPDATE_0_CHECK; + probLen = prob + LenLow + GET_LEN_STATE; + offset = 0; + limit = 1 << kLenNumLowBits; + } + else + { + UPDATE_1_CHECK; + probLen = prob + LenChoice2; + IF_BIT_0_CHECK(probLen) + { + UPDATE_0_CHECK; + probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits); + offset = kLenNumLowSymbols; + limit = 1 << kLenNumLowBits; + } + else + { + UPDATE_1_CHECK; + probLen = prob + LenHigh; + offset = kLenNumLowSymbols * 2; + limit = 1 << kLenNumHighBits; + } + } + TREE_DECODE_CHECK(probLen, limit, len); + len += offset; + } + + if (state < 4) + { + unsigned posSlot; + prob = probs + PosSlot + + ((len < kNumLenToPosStates - 1 ? len : kNumLenToPosStates - 1) << + kNumPosSlotBits); + TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot); + if (posSlot >= kStartPosModelIndex) + { + unsigned numDirectBits = ((posSlot >> 1) - 1); + + /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */ + + if (posSlot < kEndPosModelIndex) + { + prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits); + } + else + { + numDirectBits -= kNumAlignBits; + do + { + NORMALIZE_CHECK + range >>= 1; + code -= range & (((code - range) >> 31) - 1); + /* if (code >= range) code -= range; */ + } while (--numDirectBits); + prob = probs + Align; + numDirectBits = kNumAlignBits; + } + { + unsigned i = 1; + unsigned m = 1; + do + { + REV_BIT_CHECK(prob, i, m); + } while (--numDirectBits); + } + } + } + } + } + NORMALIZE_CHECK; + return res; + } + + void LzmaDec_InitDicAndState(CLzmaDec* p, BoolInt initDic, BoolInt initState) + { + p->remainLen = kMatchSpecLenStart + 1; + p->tempBufSize = 0; + + if (initDic) + { + p->processedPos = 0; + p->checkDicSize = 0; + p->remainLen = kMatchSpecLenStart + 2; + } + if (initState) + p->remainLen = kMatchSpecLenStart + 2; + } + + void LzmaDec_Init(CLzmaDec* p) + { + p->dicPos = 0; + LzmaDec_InitDicAndState(p, True, True); + } + + SRes LzmaDec_DecodeToDic(CLzmaDec* p, SizeT dicLimit, const Byte* src, SizeT* srcLen, + ELzmaFinishMode finishMode, ELzmaStatus* status) + { + SizeT inSize = *srcLen; + (*srcLen) = 0; + + *status = LZMA_STATUS_NOT_SPECIFIED; + + if (p->remainLen > kMatchSpecLenStart) + { + for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--) + p->tempBuf[p->tempBufSize++] = *src++; + if (p->tempBufSize != 0 && p->tempBuf[0] != 0) + return SZ_ERROR_DATA; + if (p->tempBufSize < RC_INIT_SIZE) + { + *status = LZMA_STATUS_NEEDS_MORE_INPUT; + return SZ_OK; + } + p->code = + ((UInt32)p->tempBuf[1] << 24) + | ((UInt32)p->tempBuf[2] << 16) + | ((UInt32)p->tempBuf[3] << 8) + | ((UInt32)p->tempBuf[4]); + p->range = 0xFFFFFFFF; + p->tempBufSize = 0; + + if (p->remainLen > kMatchSpecLenStart + 1) + { + SizeT numProbs = LzmaProps_GetNumProbs(&p->prop); + SizeT i; + CLzmaProb* probs = p->probs; + for (i = 0; i < numProbs; i++) + probs[i] = kBitModelTotal >> 1; + p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1; + p->state = 0; + } + + p->remainLen = 0; + } + + LzmaDec_WriteRem(p, dicLimit); + + while (p->remainLen != kMatchSpecLenStart) + { + int checkEndMarkNow = 0; + + if (p->dicPos >= dicLimit) + { + if (p->remainLen == 0 && p->code == 0) + { + *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK; + return SZ_OK; + } + if (finishMode == LZMA_FINISH_ANY) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_OK; + } + if (p->remainLen != 0) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_ERROR_DATA; + } + checkEndMarkNow = 1; + } + + if (p->tempBufSize == 0) + { + SizeT processed; + const Byte* bufLimit; + if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) + { + int dummyRes = LzmaDec_TryDummy(p, src, inSize); + if (dummyRes == DUMMY_ERROR) + { + memcpy(p->tempBuf, src, inSize); + p->tempBufSize = (unsigned)inSize; + (*srcLen) += inSize; + *status = LZMA_STATUS_NEEDS_MORE_INPUT; + return SZ_OK; + } + if (checkEndMarkNow && dummyRes != DUMMY_MATCH) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_ERROR_DATA; + } + bufLimit = src; + } + else + bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX; + p->buf = src; + if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0) + return SZ_ERROR_DATA; + processed = (SizeT)(p->buf - src); + (*srcLen) += processed; + src += processed; + inSize -= processed; + } + else + { + unsigned rem = p->tempBufSize, lookAhead = 0; + while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize) + p->tempBuf[rem++] = src[lookAhead++]; + p->tempBufSize = rem; + if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow) + { + int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, (SizeT)rem); + if (dummyRes == DUMMY_ERROR) + { + (*srcLen) += (SizeT)lookAhead; + *status = LZMA_STATUS_NEEDS_MORE_INPUT; + return SZ_OK; + } + if (checkEndMarkNow && dummyRes != DUMMY_MATCH) + { + *status = LZMA_STATUS_NOT_FINISHED; + return SZ_ERROR_DATA; + } + } + p->buf = p->tempBuf; + if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0) + return SZ_ERROR_DATA; + + { + unsigned kkk = (unsigned)(p->buf - p->tempBuf); + if (rem < kkk) + return SZ_ERROR_FAIL; /* some internal error */ + rem -= kkk; + if (lookAhead < rem) + return SZ_ERROR_FAIL; /* some internal error */ + lookAhead -= rem; + } + (*srcLen) += (SizeT)lookAhead; + src += lookAhead; + inSize -= (SizeT)lookAhead; + p->tempBufSize = 0; + } + } + + if (p->code != 0) + return SZ_ERROR_DATA; + *status = LZMA_STATUS_FINISHED_WITH_MARK; + return SZ_OK; + } + + SRes LzmaDec_DecodeToBuf(CLzmaDec* p, Byte* dest, SizeT* destLen, const Byte* src, SizeT* srcLen, ELzmaFinishMode finishMode, ELzmaStatus* status) + { + SizeT outSize = *destLen; + SizeT inSize = *srcLen; + *srcLen = *destLen = 0; + for (;;) + { + SizeT inSizeCur = inSize, outSizeCur, dicPos; + ELzmaFinishMode curFinishMode; + SRes res; + if (p->dicPos == p->dicBufSize) + p->dicPos = 0; + dicPos = p->dicPos; + if (outSize > p->dicBufSize - dicPos) + { + outSizeCur = p->dicBufSize; + curFinishMode = LZMA_FINISH_ANY; + } + else + { + outSizeCur = dicPos + outSize; + curFinishMode = finishMode; + } + + res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status); + src += inSizeCur; + inSize -= inSizeCur; + *srcLen += inSizeCur; + outSizeCur = p->dicPos - dicPos; + memcpy(dest, p->dic + dicPos, outSizeCur); + dest += outSizeCur; + outSize -= outSizeCur; + *destLen += outSizeCur; + if (res != 0) + return res; + if (outSizeCur == 0 || outSize == 0) + return SZ_OK; + } + } + + void LzmaDec_FreeProbs(CLzmaDec* p, ISzAllocPtr alloc) + { + ISzAlloc_Free(alloc, p->probs); + p->probs = NULL; + } + + static void LzmaDec_FreeDict(CLzmaDec* p, ISzAllocPtr alloc) + { + ISzAlloc_Free(alloc, p->dic); + p->dic = NULL; + } + + void LzmaDec_Free(CLzmaDec* p, ISzAllocPtr alloc) + { + LzmaDec_FreeProbs(p, alloc); + LzmaDec_FreeDict(p, alloc); + } + + SRes LzmaProps_Decode(CLzmaProps* p, const Byte* data, unsigned size) + { + UInt32 dicSize; + Byte d; + + if (size < LZMA_PROPS_SIZE) + return SZ_ERROR_UNSUPPORTED; + else + dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24); + + if (dicSize < LZMA_DIC_MIN) + dicSize = LZMA_DIC_MIN; + p->dicSize = dicSize; + + d = data[0]; + if (d >= (9 * 5 * 5)) + return SZ_ERROR_UNSUPPORTED; + + p->lc = (Byte)(d % 9); + d /= 9; + p->pb = (Byte)(d / 5); + p->lp = (Byte)(d % 5); + + return SZ_OK; + } + + static SRes LzmaDec_AllocateProbs2(CLzmaDec* p, const CLzmaProps* propNew, ISzAllocPtr alloc) + { + UInt32 numProbs = LzmaProps_GetNumProbs(propNew); + if (!p->probs || numProbs != p->numProbs) + { + LzmaDec_FreeProbs(p, alloc); + p->probs = (CLzmaProb*)ISzAlloc_Alloc(alloc, numProbs * sizeof(CLzmaProb)); + if (!p->probs) + return SZ_ERROR_MEM; + p->probs_1664 = p->probs + 1664; + p->numProbs = numProbs; + } + return SZ_OK; + } + + SRes LzmaDec_AllocateProbs(CLzmaDec* p, const Byte* props, unsigned propsSize, ISzAllocPtr alloc) + { + CLzmaProps propNew; + RINOK(LzmaProps_Decode(&propNew, props, propsSize)); + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); + p->prop = propNew; + return SZ_OK; + } + + SRes LzmaDec_Allocate(CLzmaDec* p, const Byte* props, unsigned propsSize, ISzAllocPtr alloc) + { + CLzmaProps propNew; + SizeT dicBufSize; + RINOK(LzmaProps_Decode(&propNew, props, propsSize)); + RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc)); + + { + UInt32 dictSize = propNew.dicSize; + SizeT mask = ((UInt32)1 << 12) - 1; + if (dictSize >= ((UInt32)1 << 30)) mask = ((UInt32)1 << 22) - 1; + else if (dictSize >= ((UInt32)1 << 22)) mask = ((UInt32)1 << 20) - 1;; + dicBufSize = ((SizeT)dictSize + mask) & ~mask; + if (dicBufSize < dictSize) + dicBufSize = dictSize; + } + + if (!p->dic || dicBufSize != p->dicBufSize) + { + LzmaDec_FreeDict(p, alloc); + p->dic = (Byte*)ISzAlloc_Alloc(alloc, dicBufSize); + if (!p->dic) + { + LzmaDec_FreeProbs(p, alloc); + return SZ_ERROR_MEM; + } + } + p->dicBufSize = dicBufSize; + p->prop = propNew; + return SZ_OK; + } + + SRes LzmaDecode(Byte* dest, SizeT* destLen, const Byte* src, SizeT* srcLen, + const Byte* propData, unsigned propSize, ELzmaFinishMode finishMode, + ELzmaStatus* status, ISzAllocPtr alloc) + { + CLzmaDec p; + SRes res; + SizeT outSize = *destLen, inSize = *srcLen; + *destLen = *srcLen = 0; + *status = LZMA_STATUS_NOT_SPECIFIED; + if (inSize < RC_INIT_SIZE) + return SZ_ERROR_INPUT_EOF; + LzmaDec_Construct(&p); + RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc)); + p.dic = dest; + p.dicBufSize = outSize; + LzmaDec_Init(&p); + *srcLen = inSize; + res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status); + *destLen = p.dicPos; + if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT) + res = SZ_ERROR_INPUT_EOF; + LzmaDec_FreeProbs(&p, alloc); + return res; + } + + /*** End of inlined file: LzmaDec.c ***/ + + + /*** Start of inlined file: LzmaEnc.c ***/ + //#include + + /* #define SHOW_STAT */ + /* #define SHOW_STAT2 */ + + //#if defined(SHOW_STAT) || defined(SHOW_STAT2) + //#include + //#endif + + + /*** Start of inlined file: LzFind.h ***/ +#ifndef __LZ_FIND_H +#define __LZ_FIND_H + + EXTERN_C_BEGIN + + typedef UInt32 CLzRef; + + typedef struct _CMatchFinder + { + Byte* buffer; + UInt32 pos; + UInt32 posLimit; + UInt32 streamPos; + UInt32 lenLimit; + + UInt32 cyclicBufferPos; + UInt32 cyclicBufferSize; /* it must be = (historySize + 1) */ + + Byte streamEndWasReached; + Byte btMode; + Byte bigHash; + Byte directInput; + + UInt32 matchMaxLen; + CLzRef* hash; + CLzRef* son; + UInt32 hashMask; + UInt32 cutValue; + + Byte* bufferBase; + ISeqInStream* stream; + + UInt32 blockSize; + UInt32 keepSizeBefore; + UInt32 keepSizeAfter; + + UInt32 numHashBytes; + size_t directInputRem; + UInt32 historySize; + UInt32 fixedHashSize; + UInt32 hashSizeSum; + SRes result; + UInt32 crc[256]; + size_t numRefs; + + UInt64 expectedDataSize; + } CMatchFinder; + +#define Inline_MatchFinder_GetPointerToCurrentPos(p) ((p)->buffer) + +#define Inline_MatchFinder_GetNumAvailableBytes(p) ((p)->streamPos - (p)->pos) + +#define Inline_MatchFinder_IsFinishedOK(p) \ + ((p)->streamEndWasReached \ + && (p)->streamPos == (p)->pos \ + && (!(p)->directInput || (p)->directInputRem == 0)) + + int MatchFinder_NeedMove(CMatchFinder* p); + Byte* MatchFinder_GetPointerToCurrentPos(CMatchFinder* p); + void MatchFinder_MoveBlock(CMatchFinder* p); + void MatchFinder_ReadIfRequired(CMatchFinder* p); + + void MatchFinder_Construct(CMatchFinder* p); + + /* Conditions: + historySize <= 3 GB + keepAddBufferBefore + matchMaxLen + keepAddBufferAfter < 511MB + */ + int MatchFinder_Create(CMatchFinder* p, UInt32 historySize, + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, + ISzAllocPtr alloc); + void MatchFinder_Free(CMatchFinder* p, ISzAllocPtr alloc); + void MatchFinder_Normalize3(UInt32 subValue, CLzRef* items, size_t numItems); + void MatchFinder_ReduceOffsets(CMatchFinder* p, UInt32 subValue); + + UInt32* GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte* buffer, CLzRef* son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 _cutValue, + UInt32* distances, UInt32 maxLen); + + /* + Conditions: + Mf_GetNumAvailableBytes_Func must be called before each Mf_GetMatchLen_Func. + Mf_GetPointerToCurrentPos_Func's result must be used only before any other function + */ + + typedef void (*Mf_Init_Func)(void* object); + typedef UInt32(*Mf_GetNumAvailableBytes_Func)(void* object); + typedef const Byte* (*Mf_GetPointerToCurrentPos_Func)(void* object); + typedef UInt32(*Mf_GetMatches_Func)(void* object, UInt32* distances); + typedef void (*Mf_Skip_Func)(void* object, UInt32); + + typedef struct _IMatchFinder + { + Mf_Init_Func Init; + Mf_GetNumAvailableBytes_Func GetNumAvailableBytes; + Mf_GetPointerToCurrentPos_Func GetPointerToCurrentPos; + Mf_GetMatches_Func GetMatches; + Mf_Skip_Func Skip; + } IMatchFinder; + + void MatchFinder_CreateVTable(CMatchFinder* p, IMatchFinder* vTable); + + void MatchFinder_Init_LowHash(CMatchFinder* p); + void MatchFinder_Init_HighHash(CMatchFinder* p); + void MatchFinder_Init_3(CMatchFinder* p, int readData); + void MatchFinder_Init(CMatchFinder* p); + + UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances); + UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances); + + void Bt3Zip_MatchFinder_Skip(CMatchFinder* p, UInt32 num); + void Hc3Zip_MatchFinder_Skip(CMatchFinder* p, UInt32 num); + + EXTERN_C_END + +#endif + + /*** End of inlined file: LzFind.h ***/ + +#ifdef SHOW_STAT + static unsigned g_STAT_OFFSET = 0; +#endif + +#define kLzmaMaxHistorySize ((UInt32)3 << 29) + /* #define kLzmaMaxHistorySize ((UInt32)7 << 29) */ + +#define kNumTopBits 24 +#define kTopValue ((UInt32)1 << kNumTopBits) + +#define kNumBitModelTotalBits 11 +#define kBitModelTotal (1 << kNumBitModelTotalBits) +#define kNumMoveBits 5 +#define kProbInitValue (kBitModelTotal >> 1) + +#define kNumMoveReducingBits 4 +#define kNumBitPriceShiftBits 4 +#define kBitPrice (1 << kNumBitPriceShiftBits) + +#define REP_LEN_COUNT 64 + + void LzmaEncProps_Init(CLzmaEncProps* p) + { + p->level = 5; + p->dictSize = p->mc = 0; + p->reduceSize = (UInt64)(Int64)-1; + p->lc = p->lp = p->pb = p->algo = p->fb = p->btMode = p->numHashBytes = p->numThreads = -1; + p->writeEndMark = 0; + } + + void LzmaEncProps_Normalize(CLzmaEncProps* p) + { + int level = p->level; + if (level < 0) level = 5; + p->level = level; + + if (p->dictSize == 0) p->dictSize = (level <= 5 ? (1 << (level * 2 + 14)) : (level <= 7 ? (1 << 25) : (1 << 26))); + if (p->dictSize > p->reduceSize) + { + unsigned i; + UInt32 reduceSize = (UInt32)p->reduceSize; + for (i = 11; i <= 30; i++) + { + if (reduceSize <= ((UInt32)2 << i)) { p->dictSize = ((UInt32)2 << i); break; } + if (reduceSize <= ((UInt32)3 << i)) { p->dictSize = ((UInt32)3 << i); break; } + } + } + + if (p->lc < 0) p->lc = 3; + if (p->lp < 0) p->lp = 0; + if (p->pb < 0) p->pb = 2; + + if (p->algo < 0) p->algo = (level < 5 ? 0 : 1); + if (p->fb < 0) p->fb = (level < 7 ? 32 : 64); + if (p->btMode < 0) p->btMode = (p->algo == 0 ? 0 : 1); + if (p->numHashBytes < 0) p->numHashBytes = 4; + if (p->mc == 0) p->mc = (16 + (p->fb >> 1)) >> (p->btMode ? 0 : 1); + + if (p->numThreads < 0) + p->numThreads = 1; + + } + + UInt32 LzmaEncProps_GetDictSize(const CLzmaEncProps* props2) + { + CLzmaEncProps props = *props2; + LzmaEncProps_Normalize(&props); + return props.dictSize; + } + +#if (_MSC_VER >= 1400) + /* BSR code is fast for some new CPUs */ + /* #define LZMA_LOG_BSR */ +#endif + +#ifdef LZMA_LOG_BSR + +#define kDicLogSizeMaxCompress 32 + +#define BSR2_RET(pos, res) { unsigned long zz; _BitScanReverse(&zz, (pos)); res = (zz + zz) + ((pos >> (zz - 1)) & 1); } + + static unsigned GetPosSlot1(UInt32 pos) + { + unsigned res; + BSR2_RET(pos, res); + return res; + } +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } +#define GetPosSlot(pos, res) { if (pos < 2) res = pos; else BSR2_RET(pos, res); } + +#else + +#define kNumLogBits (9 + sizeof(size_t) / 2) +/* #define kNumLogBits (11 + sizeof(size_t) / 8 * 3) */ + +#define kDicLogSizeMaxCompress ((kNumLogBits - 1) * 2 + 7) + + static void LzmaEnc_FastPosInit(Byte* g_FastPos) + { + unsigned slot; + g_FastPos[0] = 0; + g_FastPos[1] = 1; + g_FastPos += 2; + + for (slot = 2; slot < kNumLogBits * 2; slot++) + { + size_t k = ((size_t)1 << ((slot >> 1) - 1)); + size_t j; + for (j = 0; j < k; j++) + g_FastPos[j] = (Byte)slot; + g_FastPos += k; + } + } + + /* we can use ((limit - pos) >> 31) only if (pos < ((UInt32)1 << 31)) */ + /* + #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \ + (0 - (((((UInt32)1 << (kNumLogBits + 6)) - 1) - pos) >> 31))); \ + res = p->g_FastPos[pos >> zz] + (zz * 2); } + */ + + /* + #define BSR2_RET(pos, res) { unsigned zz = 6 + ((kNumLogBits - 1) & \ + (0 - (((((UInt32)1 << (kNumLogBits)) - 1) - (pos >> 6)) >> 31))); \ + res = p->g_FastPos[pos >> zz] + (zz * 2); } + */ + +#define BSR2_RET(pos, res) { unsigned zz = (pos < (1 << (kNumLogBits + 6))) ? 6 : 6 + kNumLogBits - 1; \ + res = p->g_FastPos[pos >> zz] + (zz * 2); } + + /* + #define BSR2_RET(pos, res) { res = (pos < (1 << (kNumLogBits + 6))) ? \ + p->g_FastPos[pos >> 6] + 12 : \ + p->g_FastPos[pos >> (6 + kNumLogBits - 1)] + (6 + (kNumLogBits - 1)) * 2; } + */ + +#define GetPosSlot1(pos) p->g_FastPos[pos] +#define GetPosSlot2(pos, res) { BSR2_RET(pos, res); } +#define GetPosSlot(pos, res) { if (pos < kNumFullDistances) res = p->g_FastPos[pos & (kNumFullDistances - 1)]; else BSR2_RET(pos, res); } + +#endif + +#define LZMA_NUM_REPS 4 + + typedef UInt16 CState; + typedef UInt16 CExtra; + + typedef struct + { + UInt32 price; + CState state; + CExtra extra; + // 0 : normal + // 1 : LIT : MATCH + // > 1 : MATCH (extra-1) : LIT : REP0 (len) + UInt32 len; + UInt32 dist; + UInt32 reps[LZMA_NUM_REPS]; + } COptimal; + + // 18.06 +#define kNumOpts (1 << 11) +#define kPackReserve (kNumOpts * 8) +// #define kNumOpts (1 << 12) +// #define kPackReserve (1 + kNumOpts * 2) + +#define kNumLenToPosStates 4 +#define kNumPosSlotBits 6 +#define kDicLogSizeMin 0 +#define kDicLogSizeMax 32 +#define kDistTableSizeMax (kDicLogSizeMax * 2) + +#define kNumAlignBits 4 +#define kAlignTableSize (1 << kNumAlignBits) +#define kAlignMask (kAlignTableSize - 1) + +#define kStartPosModelIndex 4 +#define kEndPosModelIndex 14 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1)) + + typedef +#ifdef _LZMA_PROB32 + UInt32 +#else + UInt16 +#endif + CLzmaProb; + +#define LZMA_PB_MAX 4 +#define LZMA_LC_MAX 8 +#define LZMA_LP_MAX 4 + +#define LZMA_NUM_PB_STATES_MAX (1 << LZMA_PB_MAX) + +#define kLenNumLowBits 3 +#define kLenNumLowSymbols (1 << kLenNumLowBits) +#define kLenNumHighBits 8 +#define kLenNumHighSymbols (1 << kLenNumHighBits) +#define kLenNumSymbolsTotal (kLenNumLowSymbols * 2 + kLenNumHighSymbols) + +#define LZMA_MATCH_LEN_MIN 2 +#define LZMA_MATCH_LEN_MAX (LZMA_MATCH_LEN_MIN + kLenNumSymbolsTotal - 1) + +#define kNumStates 12 + + typedef struct + { + CLzmaProb low[LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)]; + CLzmaProb high[kLenNumHighSymbols]; + } CLenEnc; + + typedef struct + { + unsigned tableSize; + UInt32 prices[LZMA_NUM_PB_STATES_MAX][kLenNumSymbolsTotal]; + // UInt32 prices1[LZMA_NUM_PB_STATES_MAX][kLenNumLowSymbols * 2]; + // UInt32 prices2[kLenNumSymbolsTotal]; + } CLenPriceEnc; + +#define GET_PRICE_LEN(p, posState, len) \ + ((p)->prices[posState][(size_t)(len) - LZMA_MATCH_LEN_MIN]) + + /* + #define GET_PRICE_LEN(p, posState, len) \ + ((p)->prices2[(size_t)(len) - 2] + ((p)->prices1[posState][((len) - 2) & (kLenNumLowSymbols * 2 - 1)] & (((len) - 2 - kLenNumLowSymbols * 2) >> 9))) + */ + + typedef struct + { + UInt32 range; + unsigned cache; + UInt64 low; + UInt64 cacheSize; + Byte* buf; + Byte* bufLim; + Byte* bufBase; + ISeqOutStream* outStream; + UInt64 processed; + SRes res; + } CRangeEnc; + + typedef struct + { + CLzmaProb* litProbs; + + unsigned state; + UInt32 reps[LZMA_NUM_REPS]; + + CLzmaProb posAlignEncoder[1 << kNumAlignBits]; + CLzmaProb isRep[kNumStates]; + CLzmaProb isRepG0[kNumStates]; + CLzmaProb isRepG1[kNumStates]; + CLzmaProb isRepG2[kNumStates]; + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; + + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; + CLzmaProb posEncoders[kNumFullDistances]; + + CLenEnc lenProbs; + CLenEnc repLenProbs; + + } CSaveState; + + typedef UInt32 CProbPrice; + + typedef struct + { + void* matchFinderObj; + IMatchFinder matchFinder; + + unsigned optCur; + unsigned optEnd; + + unsigned longestMatchLen; + unsigned numPairs; + UInt32 numAvail; + + unsigned state; + unsigned numFastBytes; + unsigned additionalOffset; + UInt32 reps[LZMA_NUM_REPS]; + unsigned lpMask, pbMask; + CLzmaProb* litProbs; + CRangeEnc rc; + + UInt32 backRes; + + unsigned lc, lp, pb; + unsigned lclp; + + BoolInt fastMode; + BoolInt writeEndMark; + BoolInt finished; + BoolInt multiThread; + BoolInt needInit; + // BoolInt _maxMode; + + UInt64 nowPos64; + + unsigned matchPriceCount; + // unsigned alignPriceCount; + int repLenEncCounter; + + unsigned distTableSize; + + UInt32 dictSize; + SRes result; + + CMatchFinder matchFinderBase; + + // LZ thread + CProbPrice ProbPrices[kBitModelTotal >> kNumMoveReducingBits]; + + UInt32 matches[LZMA_MATCH_LEN_MAX * 2 + 2 + 1]; + + UInt32 alignPrices[kAlignTableSize]; + UInt32 posSlotPrices[kNumLenToPosStates][kDistTableSizeMax]; + UInt32 distancesPrices[kNumLenToPosStates][kNumFullDistances]; + + CLzmaProb posAlignEncoder[1 << kNumAlignBits]; + CLzmaProb isRep[kNumStates]; + CLzmaProb isRepG0[kNumStates]; + CLzmaProb isRepG1[kNumStates]; + CLzmaProb isRepG2[kNumStates]; + CLzmaProb isMatch[kNumStates][LZMA_NUM_PB_STATES_MAX]; + CLzmaProb isRep0Long[kNumStates][LZMA_NUM_PB_STATES_MAX]; + CLzmaProb posSlotEncoder[kNumLenToPosStates][1 << kNumPosSlotBits]; + CLzmaProb posEncoders[kNumFullDistances]; + + CLenEnc lenProbs; + CLenEnc repLenProbs; + +#ifndef LZMA_LOG_BSR + Byte g_FastPos[1 << kNumLogBits]; +#endif + + CLenPriceEnc lenEnc; + CLenPriceEnc repLenEnc; + + COptimal opt[kNumOpts]; + + CSaveState saveState; + + } CLzmaEnc; + +#define COPY_ARR(dest, src, arr) memcpy(dest->arr, src->arr, sizeof(src->arr)); + + void LzmaEnc_SaveState(CLzmaEncHandle pp) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + CSaveState* dest = &p->saveState; + + dest->state = p->state; + + dest->lenProbs = p->lenProbs; + dest->repLenProbs = p->repLenProbs; + + COPY_ARR(dest, p, reps); + + COPY_ARR(dest, p, posAlignEncoder); + COPY_ARR(dest, p, isRep); + COPY_ARR(dest, p, isRepG0); + COPY_ARR(dest, p, isRepG1); + COPY_ARR(dest, p, isRepG2); + COPY_ARR(dest, p, isMatch); + COPY_ARR(dest, p, isRep0Long); + COPY_ARR(dest, p, posSlotEncoder); + COPY_ARR(dest, p, posEncoders); + + memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << p->lclp) * sizeof(CLzmaProb)); + } + + void LzmaEnc_RestoreState(CLzmaEncHandle pp) + { + CLzmaEnc* dest = (CLzmaEnc*)pp; + const CSaveState* p = &dest->saveState; + + dest->state = p->state; + + dest->lenProbs = p->lenProbs; + dest->repLenProbs = p->repLenProbs; + + COPY_ARR(dest, p, reps); + + COPY_ARR(dest, p, posAlignEncoder); + COPY_ARR(dest, p, isRep); + COPY_ARR(dest, p, isRepG0); + COPY_ARR(dest, p, isRepG1); + COPY_ARR(dest, p, isRepG2); + COPY_ARR(dest, p, isMatch); + COPY_ARR(dest, p, isRep0Long); + COPY_ARR(dest, p, posSlotEncoder); + COPY_ARR(dest, p, posEncoders); + + memcpy(dest->litProbs, p->litProbs, ((UInt32)0x300 << dest->lclp) * sizeof(CLzmaProb)); + } + + SRes LzmaEnc_SetProps(CLzmaEncHandle pp, const CLzmaEncProps* props2) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + CLzmaEncProps props = *props2; + LzmaEncProps_Normalize(&props); + + if (props.lc > LZMA_LC_MAX + || props.lp > LZMA_LP_MAX + || props.pb > LZMA_PB_MAX + || props.dictSize > ((UInt64)1 << kDicLogSizeMaxCompress) + || props.dictSize > kLzmaMaxHistorySize) + return SZ_ERROR_PARAM; + + p->dictSize = props.dictSize; + { + unsigned fb = props.fb; + if (fb < 5) + fb = 5; + if (fb > LZMA_MATCH_LEN_MAX) + fb = LZMA_MATCH_LEN_MAX; + p->numFastBytes = fb; + } + p->lc = props.lc; + p->lp = props.lp; + p->pb = props.pb; + p->fastMode = (props.algo == 0); + // p->_maxMode = True; + p->matchFinderBase.btMode = (Byte)(props.btMode ? 1 : 0); + { + unsigned numHashBytes = 4; + if (props.btMode) + { + if (props.numHashBytes < 2) + numHashBytes = 2; + else if (props.numHashBytes < 4) + numHashBytes = props.numHashBytes; + } + p->matchFinderBase.numHashBytes = numHashBytes; + } + + p->matchFinderBase.cutValue = props.mc; + + p->writeEndMark = props.writeEndMark; + + return SZ_OK; + } + + void LzmaEnc_SetDataSize(CLzmaEncHandle pp, UInt64 expectedDataSiize) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + p->matchFinderBase.expectedDataSize = expectedDataSiize; + } + +#define kState_Start 0 +#define kState_LitAfterMatch 4 +#define kState_LitAfterRep 5 +#define kState_MatchAfterLit 7 +#define kState_RepAfterLit 8 + + static const Byte kLiteralNextStates[kNumStates] = { 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5 }; + static const Byte kMatchNextStates[kNumStates] = { 7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10 }; + static const Byte kRepNextStates[kNumStates] = { 8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11 }; + static const Byte kShortRepNextStates[kNumStates] = { 9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11 }; + +#define IsLitState(s) ((s) < 7) +#define GetLenToPosState2(len) (((len) < kNumLenToPosStates - 1) ? (len) : kNumLenToPosStates - 1) +#define GetLenToPosState(len) (((len) < kNumLenToPosStates + 1) ? (len) - 2 : kNumLenToPosStates - 1) + +#define kInfinityPrice (1 << 30) + + static void RangeEnc_Construct(CRangeEnc* p) + { + p->outStream = NULL; + p->bufBase = NULL; + } + +#define RangeEnc_GetProcessed(p) ((p)->processed + ((p)->buf - (p)->bufBase) + (p)->cacheSize) +#define RangeEnc_GetProcessed_sizet(p) ((size_t)(p)->processed + ((p)->buf - (p)->bufBase) + (size_t)(p)->cacheSize) + +#define RC_BUF_SIZE (1 << 16) + + static int RangeEnc_Alloc(CRangeEnc* p, ISzAllocPtr alloc) + { + if (!p->bufBase) + { + p->bufBase = (Byte*)ISzAlloc_Alloc(alloc, RC_BUF_SIZE); + if (!p->bufBase) + return 0; + p->bufLim = p->bufBase + RC_BUF_SIZE; + } + return 1; + } + + static void RangeEnc_Free(CRangeEnc* p, ISzAllocPtr alloc) + { + ISzAlloc_Free(alloc, p->bufBase); + p->bufBase = 0; + } + + static void RangeEnc_Init(CRangeEnc* p) + { + /* Stream.Init(); */ + p->range = 0xFFFFFFFF; + p->cache = 0; + p->low = 0; + p->cacheSize = 0; + + p->buf = p->bufBase; + + p->processed = 0; + p->res = SZ_OK; + } + + MY_NO_INLINE static void RangeEnc_FlushStream(CRangeEnc* p) + { + size_t num; + if (p->res != SZ_OK) + return; + num = p->buf - p->bufBase; + if (num != ISeqOutStream_Write(p->outStream, p->bufBase, num)) + p->res = SZ_ERROR_WRITE; + p->processed += num; + p->buf = p->bufBase; + } + + MY_NO_INLINE static void MY_FAST_CALL RangeEnc_ShiftLow(CRangeEnc* p) + { + UInt32 low = (UInt32)p->low; + unsigned high = (unsigned)(p->low >> 32); + p->low = (UInt32)(low << 8); + if (low < (UInt32)0xFF000000 || high != 0) + { + { + Byte* buf = p->buf; + *buf++ = (Byte)(p->cache + high); + p->cache = (unsigned)(low >> 24); + p->buf = buf; + if (buf == p->bufLim) + RangeEnc_FlushStream(p); + if (p->cacheSize == 0) + return; + } + high += 0xFF; + for (;;) + { + Byte* buf = p->buf; + *buf++ = (Byte)(high); + p->buf = buf; + if (buf == p->bufLim) + RangeEnc_FlushStream(p); + if (--p->cacheSize == 0) + return; + } + } + p->cacheSize++; + } + + static void RangeEnc_FlushData(CRangeEnc* p) + { + int i; + for (i = 0; i < 5; i++) + RangeEnc_ShiftLow(p); + } + +#define RC_NORM(p) if (range < kTopValue) { range <<= 8; RangeEnc_ShiftLow(p); } + +#define RC_BIT_PRE(p, prob) \ + ttt = *(prob); \ + newBound = (range >> kNumBitModelTotalBits) * ttt; + + // #define _LZMA_ENC_USE_BRANCH + +#ifdef _LZMA_ENC_USE_BRANCH + +#define RC_BIT(p, prob, bit) { \ + RC_BIT_PRE(p, prob) \ + if (bit == 0) { range = newBound; ttt += (kBitModelTotal - ttt) >> kNumMoveBits; } \ + else { (p)->low += newBound; range -= newBound; ttt -= ttt >> kNumMoveBits; } \ + *(prob) = (CLzmaProb)ttt; \ + RC_NORM(p) \ + } + +#else + +#define RC_BIT(p, prob, bit) { \ + UInt32 mask; \ + RC_BIT_PRE(p, prob) \ + mask = 0 - (UInt32)bit; \ + range &= mask; \ + mask &= newBound; \ + range -= mask; \ + (p)->low += mask; \ + mask = (UInt32)bit - 1; \ + range += newBound & mask; \ + mask &= (kBitModelTotal - ((1 << kNumMoveBits) - 1)); \ + mask += ((1 << kNumMoveBits) - 1); \ + ttt += (Int32)(mask - ttt) >> kNumMoveBits; \ + *(prob) = (CLzmaProb)ttt; \ + RC_NORM(p) \ + } + +#endif + +#define RC_BIT_0_BASE(p, prob) \ + range = newBound; *(prob) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); + +#define RC_BIT_1_BASE(p, prob) \ + range -= newBound; (p)->low += newBound; *(prob) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits)); \ + +#define RC_BIT_0(p, prob) \ + RC_BIT_0_BASE(p, prob) \ + RC_NORM(p) + +#define RC_BIT_1(p, prob) \ + RC_BIT_1_BASE(p, prob) \ + RC_NORM(p) + + static void RangeEnc_EncodeBit_0(CRangeEnc* p, CLzmaProb* prob) + { + UInt32 range, ttt, newBound; + range = p->range; + RC_BIT_PRE(p, prob) + RC_BIT_0(p, prob) + p->range = range; + } + + static void LitEnc_Encode(CRangeEnc* p, CLzmaProb* probs, UInt32 sym) + { + UInt32 range = p->range; + sym |= 0x100; + do + { + UInt32 ttt, newBound; + // RangeEnc_EncodeBit(p, probs + (sym >> 8), (sym >> 7) & 1); + CLzmaProb* prob = probs + (sym >> 8); + UInt32 bit = (sym >> 7) & 1; + sym <<= 1; + RC_BIT(p, prob, bit); + } while (sym < 0x10000); + p->range = range; + } + + static void LitEnc_EncodeMatched(CRangeEnc* p, CLzmaProb* probs, UInt32 sym, UInt32 matchByte) + { + UInt32 range = p->range; + UInt32 offs = 0x100; + sym |= 0x100; + do + { + UInt32 ttt, newBound; + CLzmaProb* prob; + UInt32 bit; + matchByte <<= 1; + // RangeEnc_EncodeBit(p, probs + (offs + (matchByte & offs) + (sym >> 8)), (sym >> 7) & 1); + prob = probs + (offs + (matchByte & offs) + (sym >> 8)); + bit = (sym >> 7) & 1; + sym <<= 1; + offs &= ~(matchByte ^ sym); + RC_BIT(p, prob, bit); + } while (sym < 0x10000); + p->range = range; + } + + static void LzmaEnc_InitPriceTables(CProbPrice* ProbPrices) + { + UInt32 i; + for (i = 0; i < (kBitModelTotal >> kNumMoveReducingBits); i++) + { + const unsigned kCyclesBits = kNumBitPriceShiftBits; + UInt32 w = (i << kNumMoveReducingBits) + (1 << (kNumMoveReducingBits - 1)); + unsigned bitCount = 0; + unsigned j; + for (j = 0; j < kCyclesBits; j++) + { + w = w * w; + bitCount <<= 1; + while (w >= ((UInt32)1 << 16)) + { + w >>= 1; + bitCount++; + } + } + ProbPrices[i] = (CProbPrice)((kNumBitModelTotalBits << kCyclesBits) - 15 - bitCount); + // printf("\n%3d: %5d", i, ProbPrices[i]); + } + } + +#define GET_PRICE(prob, bit) \ + p->ProbPrices[((prob) ^ (unsigned)(((-(int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; + +#define GET_PRICEa(prob, bit) \ + ProbPrices[((prob) ^ (unsigned)((-((int)(bit))) & (kBitModelTotal - 1))) >> kNumMoveReducingBits]; + +#define GET_PRICE_0(prob) p->ProbPrices[(prob) >> kNumMoveReducingBits] +#define GET_PRICE_1(prob) p->ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] + +#define GET_PRICEa_0(prob) ProbPrices[(prob) >> kNumMoveReducingBits] +#define GET_PRICEa_1(prob) ProbPrices[((prob) ^ (kBitModelTotal - 1)) >> kNumMoveReducingBits] + + static UInt32 LitEnc_GetPrice(const CLzmaProb* probs, UInt32 sym, const CProbPrice* ProbPrices) + { + UInt32 price = 0; + sym |= 0x100; + do + { + unsigned bit = sym & 1; + sym >>= 1; + price += GET_PRICEa(probs[sym], bit); + } while (sym >= 2); + return price; + } + + static UInt32 LitEnc_Matched_GetPrice(const CLzmaProb* probs, UInt32 sym, UInt32 matchByte, const CProbPrice* ProbPrices) + { + UInt32 price = 0; + UInt32 offs = 0x100; + sym |= 0x100; + do + { + matchByte <<= 1; + price += GET_PRICEa(probs[offs + (matchByte & offs) + (sym >> 8)], (sym >> 7) & 1); + sym <<= 1; + offs &= ~(matchByte ^ sym); + } while (sym < 0x10000); + return price; + } + + static void RcTree_ReverseEncode(CRangeEnc* rc, CLzmaProb* probs, unsigned numBits, unsigned sym) + { + UInt32 range = rc->range; + unsigned m = 1; + do + { + UInt32 ttt, newBound; + unsigned bit = sym & 1; + // RangeEnc_EncodeBit(rc, probs + m, bit); + sym >>= 1; + RC_BIT(rc, probs + m, bit); + m = (m << 1) | bit; + } while (--numBits); + rc->range = range; + } + + static void LenEnc_Init(CLenEnc* p) + { + unsigned i; + for (i = 0; i < (LZMA_NUM_PB_STATES_MAX << (kLenNumLowBits + 1)); i++) + p->low[i] = kProbInitValue; + for (i = 0; i < kLenNumHighSymbols; i++) + p->high[i] = kProbInitValue; + } + + static void LenEnc_Encode(CLenEnc* p, CRangeEnc* rc, unsigned sym, unsigned posState) + { + UInt32 range, ttt, newBound; + CLzmaProb* probs = p->low; + range = rc->range; + RC_BIT_PRE(rc, probs); + if (sym >= kLenNumLowSymbols) + { + RC_BIT_1(rc, probs); + probs += kLenNumLowSymbols; + RC_BIT_PRE(rc, probs); + if (sym >= kLenNumLowSymbols * 2) + { + RC_BIT_1(rc, probs); + rc->range = range; + // RcTree_Encode(rc, p->high, kLenNumHighBits, sym - kLenNumLowSymbols * 2); + LitEnc_Encode(rc, p->high, sym - kLenNumLowSymbols * 2); + return; + } + sym -= kLenNumLowSymbols; + } + + // RcTree_Encode(rc, probs + (posState << kLenNumLowBits), kLenNumLowBits, sym); + { + unsigned m; + unsigned bit; + RC_BIT_0(rc, probs); + probs += (posState << (1 + kLenNumLowBits)); + bit = (sym >> 2); RC_BIT(rc, probs + 1, bit); m = (1 << 1) + bit; + bit = (sym >> 1) & 1; RC_BIT(rc, probs + m, bit); m = (m << 1) + bit; + bit = sym & 1; RC_BIT(rc, probs + m, bit); + rc->range = range; + } + } + + static void SetPrices_3(const CLzmaProb* probs, UInt32 startPrice, UInt32* prices, const CProbPrice* ProbPrices) + { + unsigned i; + for (i = 0; i < 8; i += 2) + { + UInt32 price = startPrice; + UInt32 prob; + price += GET_PRICEa(probs[1], (i >> 2)); + price += GET_PRICEa(probs[2 + (i >> 2)], (i >> 1) & 1); + prob = probs[4 + (i >> 1)]; + prices[i] = price + GET_PRICEa_0(prob); + prices[i + 1] = price + GET_PRICEa_1(prob); + } + } + + MY_NO_INLINE static void MY_FAST_CALL LenPriceEnc_UpdateTables( + CLenPriceEnc* p, + unsigned numPosStates, + const CLenEnc* enc, + const CProbPrice* ProbPrices) + { + UInt32 b; + + { + unsigned prob = enc->low[0]; + UInt32 a, c; + unsigned posState; + b = GET_PRICEa_1(prob); + a = GET_PRICEa_0(prob); + c = b + GET_PRICEa_0(enc->low[kLenNumLowSymbols]); + for (posState = 0; posState < numPosStates; posState++) + { + UInt32* prices = p->prices[posState]; + const CLzmaProb* probs = enc->low + (posState << (1 + kLenNumLowBits)); + SetPrices_3(probs, a, prices, ProbPrices); + SetPrices_3(probs + kLenNumLowSymbols, c, prices + kLenNumLowSymbols, ProbPrices); + } + } + + /* + { + unsigned i; + UInt32 b; + a = GET_PRICEa_0(enc->low[0]); + for (i = 0; i < kLenNumLowSymbols; i++) + p->prices2[i] = a; + a = GET_PRICEa_1(enc->low[0]); + b = a + GET_PRICEa_0(enc->low[kLenNumLowSymbols]); + for (i = kLenNumLowSymbols; i < kLenNumLowSymbols * 2; i++) + p->prices2[i] = b; + a += GET_PRICEa_1(enc->low[kLenNumLowSymbols]); + } + */ + + // p->counter = numSymbols; + // p->counter = 64; + + { + unsigned i = p->tableSize; + + if (i > kLenNumLowSymbols * 2) + { + const CLzmaProb* probs = enc->high; + UInt32* prices = p->prices[0] + kLenNumLowSymbols * 2; + i -= kLenNumLowSymbols * 2 - 1; + i >>= 1; + b += GET_PRICEa_1(enc->low[kLenNumLowSymbols]); + do + { + /* + p->prices2[i] = a + + // RcTree_GetPrice(enc->high, kLenNumHighBits, i - kLenNumLowSymbols * 2, ProbPrices); + LitEnc_GetPrice(probs, i - kLenNumLowSymbols * 2, ProbPrices); + */ + // UInt32 price = a + RcTree_GetPrice(probs, kLenNumHighBits - 1, sym, ProbPrices); + unsigned sym = --i + (1 << (kLenNumHighBits - 1)); + UInt32 price = b; + do + { + unsigned bit = sym & 1; + sym >>= 1; + price += GET_PRICEa(probs[sym], bit); + } while (sym >= 2); + + { + unsigned prob = probs[(size_t)i + (1 << (kLenNumHighBits - 1))]; + prices[(size_t)i * 2] = price + GET_PRICEa_0(prob); + prices[(size_t)i * 2 + 1] = price + GET_PRICEa_1(prob); + } + } while (i); + + { + unsigned posState; + size_t num = (p->tableSize - kLenNumLowSymbols * 2) * sizeof(p->prices[0][0]); + for (posState = 1; posState < numPosStates; posState++) + memcpy(p->prices[posState] + kLenNumLowSymbols * 2, p->prices[0] + kLenNumLowSymbols * 2, num); + } + } + } + } + + /* + #ifdef SHOW_STAT + g_STAT_OFFSET += num; + printf("\n MovePos %u", num); + #endif + */ + +#define MOVE_POS(p, num) { \ + p->additionalOffset += (num); \ + p->matchFinder.Skip(p->matchFinderObj, (UInt32)(num)); } + + static unsigned ReadMatchDistances(CLzmaEnc* p, unsigned* numPairsRes) + { + unsigned numPairs; + + p->additionalOffset++; + p->numAvail = p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); + numPairs = p->matchFinder.GetMatches(p->matchFinderObj, p->matches); + *numPairsRes = numPairs; + +#ifdef SHOW_STAT + printf("\n i = %u numPairs = %u ", g_STAT_OFFSET, numPairs / 2); + g_STAT_OFFSET++; + { + unsigned i; + for (i = 0; i < numPairs; i += 2) + printf("%2u %6u | ", p->matches[i], p->matches[i + 1]); + } +#endif + + if (numPairs == 0) + return 0; + { + unsigned len = p->matches[(size_t)numPairs - 2]; + if (len != p->numFastBytes) + return len; + { + UInt32 numAvail = p->numAvail; + if (numAvail > LZMA_MATCH_LEN_MAX) + numAvail = LZMA_MATCH_LEN_MAX; + { + const Byte* p1 = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + const Byte* p2 = p1 + len; + ptrdiff_t dif = (ptrdiff_t)-1 - p->matches[(size_t)numPairs - 1]; + const Byte* lim = p1 + numAvail; + for (; p2 != lim && *p2 == p2[dif]; p2++) + { + } + return (unsigned)(p2 - p1); + } + } + } + } + +#define MARK_LIT ((UInt32)(Int32)-1) + +#define MakeAs_Lit(p) { (p)->dist = MARK_LIT; (p)->extra = 0; } +#define MakeAs_ShortRep(p) { (p)->dist = 0; (p)->extra = 0; } +#define IsShortRep(p) ((p)->dist == 0) + +#define GetPrice_ShortRep(p, state, posState) \ + ( GET_PRICE_0(p->isRepG0[state]) + GET_PRICE_0(p->isRep0Long[state][posState])) + +#define GetPrice_Rep_0(p, state, posState) ( \ + GET_PRICE_1(p->isMatch[state][posState]) \ + + GET_PRICE_1(p->isRep0Long[state][posState])) \ + + GET_PRICE_1(p->isRep[state]) \ + + GET_PRICE_0(p->isRepG0[state]) + + MY_FORCE_INLINE + static UInt32 GetPrice_PureRep(const CLzmaEnc* p, unsigned repIndex, size_t state, size_t posState) + { + UInt32 price; + UInt32 prob = p->isRepG0[state]; + if (repIndex == 0) + { + price = GET_PRICE_0(prob); + price += GET_PRICE_1(p->isRep0Long[state][posState]); + } + else + { + price = GET_PRICE_1(prob); + prob = p->isRepG1[state]; + if (repIndex == 1) + price += GET_PRICE_0(prob); + else + { + price += GET_PRICE_1(prob); + price += GET_PRICE(p->isRepG2[state], repIndex - 2); + } + } + return price; + } + + static unsigned Backward(CLzmaEnc* p, unsigned cur) + { + unsigned wr = cur + 1; + p->optEnd = wr; + + for (;;) + { + UInt32 dist = p->opt[cur].dist; + unsigned len = (unsigned)p->opt[cur].len; + unsigned extra = (unsigned)p->opt[cur].extra; + cur -= len; + + if (extra) + { + wr--; + p->opt[wr].len = (UInt32)len; + cur -= extra; + len = extra; + if (extra == 1) + { + p->opt[wr].dist = dist; + dist = MARK_LIT; + } + else + { + p->opt[wr].dist = 0; + len--; + wr--; + p->opt[wr].dist = MARK_LIT; + p->opt[wr].len = 1; + } + } + + if (cur == 0) + { + p->backRes = dist; + p->optCur = wr; + return len; + } + + wr--; + p->opt[wr].dist = dist; + p->opt[wr].len = (UInt32)len; + } + } + +#define LIT_PROBS(pos, prevByte) \ + (p->litProbs + (UInt32)3 * (((((pos) << 8) + (prevByte)) & p->lpMask) << p->lc)) + + static unsigned GetOptimum(CLzmaEnc* p, UInt32 position) + { + unsigned last, cur; + UInt32 reps[LZMA_NUM_REPS]; + unsigned repLens[LZMA_NUM_REPS]; + UInt32* matches; + + { + UInt32 numAvail; + unsigned numPairs, mainLen, repMaxIndex, i, posState; + UInt32 matchPrice, repMatchPrice; + const Byte* data; + Byte curByte, matchByte; + + p->optCur = p->optEnd = 0; + + if (p->additionalOffset == 0) + mainLen = ReadMatchDistances(p, &numPairs); + else + { + mainLen = p->longestMatchLen; + numPairs = p->numPairs; + } + + numAvail = p->numAvail; + if (numAvail < 2) + { + p->backRes = MARK_LIT; + return 1; + } + if (numAvail > LZMA_MATCH_LEN_MAX) + numAvail = LZMA_MATCH_LEN_MAX; + + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + repMaxIndex = 0; + + for (i = 0; i < LZMA_NUM_REPS; i++) + { + unsigned len; + const Byte* data2; + reps[i] = p->reps[i]; + data2 = data - reps[i]; + if (data[0] != data2[0] || data[1] != data2[1]) + { + repLens[i] = 0; + continue; + } + for (len = 2; len < numAvail && data[len] == data2[len]; len++) + { + } + repLens[i] = len; + if (len > repLens[repMaxIndex]) + repMaxIndex = i; + } + + if (repLens[repMaxIndex] >= p->numFastBytes) + { + unsigned len; + p->backRes = (UInt32)repMaxIndex; + len = repLens[repMaxIndex]; + MOVE_POS(p, len - 1) + return len; + } + + matches = p->matches; + + if (mainLen >= p->numFastBytes) + { + p->backRes = matches[(size_t)numPairs - 1] + LZMA_NUM_REPS; + MOVE_POS(p, mainLen - 1) + return mainLen; + } + + curByte = *data; + matchByte = *(data - reps[0]); + + last = repLens[repMaxIndex]; + if (last <= mainLen) + last = mainLen; + + if (last < 2 && curByte != matchByte) + { + p->backRes = MARK_LIT; + return 1; + } + + p->opt[0].state = (CState)p->state; + + posState = (position & p->pbMask); + + { + const CLzmaProb* probs = LIT_PROBS(position, *(data - 1)); + p->opt[1].price = GET_PRICE_0(p->isMatch[p->state][posState]) + + (!IsLitState(p->state) ? + LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) : + LitEnc_GetPrice(probs, curByte, p->ProbPrices)); + } + + MakeAs_Lit(&p->opt[1]); + + matchPrice = GET_PRICE_1(p->isMatch[p->state][posState]); + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[p->state]); + + // 18.06 + if (matchByte == curByte && repLens[0] == 0) + { + UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, p->state, posState); + if (shortRepPrice < p->opt[1].price) + { + p->opt[1].price = shortRepPrice; + MakeAs_ShortRep(&p->opt[1]); + } + if (last < 2) + { + p->backRes = p->opt[1].dist; + return 1; + } + } + + p->opt[1].len = 1; + + p->opt[0].reps[0] = reps[0]; + p->opt[0].reps[1] = reps[1]; + p->opt[0].reps[2] = reps[2]; + p->opt[0].reps[3] = reps[3]; + + // ---------- REP ---------- + + for (i = 0; i < LZMA_NUM_REPS; i++) + { + unsigned repLen = repLens[i]; + UInt32 price; + if (repLen < 2) + continue; + price = repMatchPrice + GetPrice_PureRep(p, i, p->state, posState); + do + { + UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, repLen); + COptimal* opt = &p->opt[repLen]; + if (price2 < opt->price) + { + opt->price = price2; + opt->len = (UInt32)repLen; + opt->dist = (UInt32)i; + opt->extra = 0; + } + } while (--repLen >= 2); + } + + // ---------- MATCH ---------- + { + unsigned len = repLens[0] + 1; + if (len <= mainLen) + { + unsigned offs = 0; + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[p->state]); + + if (len < 2) + len = 2; + else + while (len > matches[offs]) + offs += 2; + + for (; ; len++) + { + COptimal* opt; + UInt32 dist = matches[(size_t)offs + 1]; + UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len); + unsigned lenToPosState = GetLenToPosState(len); + + if (dist < kNumFullDistances) + price += p->distancesPrices[lenToPosState][dist & (kNumFullDistances - 1)]; + else + { + unsigned slot; + GetPosSlot2(dist, slot); + price += p->alignPrices[dist & kAlignMask]; + price += p->posSlotPrices[lenToPosState][slot]; + } + + opt = &p->opt[len]; + + if (price < opt->price) + { + opt->price = price; + opt->len = (UInt32)len; + opt->dist = dist + LZMA_NUM_REPS; + opt->extra = 0; + } + + if (len == matches[offs]) + { + offs += 2; + if (offs == numPairs) + break; + } + } + } + } + + cur = 0; + +#ifdef SHOW_STAT2 + /* if (position >= 0) */ + { + unsigned i; + printf("\n pos = %4X", position); + for (i = cur; i <= last; i++) + printf("\nprice[%4X] = %u", position - cur + i, p->opt[i].price); + } +#endif + } + + // ---------- Optimal Parsing ---------- + + for (;;) + { + unsigned numAvail; + UInt32 numAvailFull; + unsigned newLen, numPairs, prev, state, posState, startLen; + UInt32 litPrice, matchPrice, repMatchPrice; + BoolInt nextIsLit; + Byte curByte, matchByte; + const Byte* data; + COptimal* curOpt, * nextOpt; + + if (++cur == last) + break; + + // 18.06 + if (cur >= kNumOpts - 64) + { + unsigned j, best; + UInt32 price = p->opt[cur].price; + best = cur; + for (j = cur + 1; j <= last; j++) + { + UInt32 price2 = p->opt[j].price; + if (price >= price2) + { + price = price2; + best = j; + } + } + { + unsigned delta = best - cur; + if (delta != 0) + { + MOVE_POS(p, delta); + } + } + cur = best; + break; + } + + newLen = ReadMatchDistances(p, &numPairs); + + if (newLen >= p->numFastBytes) + { + p->numPairs = numPairs; + p->longestMatchLen = newLen; + break; + } + + curOpt = &p->opt[cur]; + + position++; + + // we need that check here, if skip_items in p->opt are possible + /* + if (curOpt->price >= kInfinityPrice) + continue; + */ + + prev = cur - curOpt->len; + + if (curOpt->len == 1) + { + state = (unsigned)p->opt[prev].state; + if (IsShortRep(curOpt)) + state = kShortRepNextStates[state]; + else + state = kLiteralNextStates[state]; + } + else + { + const COptimal* prevOpt; + UInt32 b0; + UInt32 dist = curOpt->dist; + + if (curOpt->extra) + { + prev -= (unsigned)curOpt->extra; + state = kState_RepAfterLit; + if (curOpt->extra == 1) + state = (dist < LZMA_NUM_REPS ? kState_RepAfterLit : kState_MatchAfterLit); + } + else + { + state = (unsigned)p->opt[prev].state; + if (dist < LZMA_NUM_REPS) + state = kRepNextStates[state]; + else + state = kMatchNextStates[state]; + } + + prevOpt = &p->opt[prev]; + b0 = prevOpt->reps[0]; + + if (dist < LZMA_NUM_REPS) + { + if (dist == 0) + { + reps[0] = b0; + reps[1] = prevOpt->reps[1]; + reps[2] = prevOpt->reps[2]; + reps[3] = prevOpt->reps[3]; + } + else + { + reps[1] = b0; + b0 = prevOpt->reps[1]; + if (dist == 1) + { + reps[0] = b0; + reps[2] = prevOpt->reps[2]; + reps[3] = prevOpt->reps[3]; + } + else + { + reps[2] = b0; + reps[0] = prevOpt->reps[dist]; + reps[3] = prevOpt->reps[dist ^ 1]; + } + } + } + else + { + reps[0] = (dist - LZMA_NUM_REPS + 1); + reps[1] = b0; + reps[2] = prevOpt->reps[1]; + reps[3] = prevOpt->reps[2]; + } + } + + curOpt->state = (CState)state; + curOpt->reps[0] = reps[0]; + curOpt->reps[1] = reps[1]; + curOpt->reps[2] = reps[2]; + curOpt->reps[3] = reps[3]; + + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + curByte = *data; + matchByte = *(data - reps[0]); + + posState = (position & p->pbMask); + + /* + The order of Price checks: + < LIT + <= SHORT_REP + < LIT : REP_0 + < REP [ : LIT : REP_0 ] + < MATCH [ : LIT : REP_0 ] + */ + + { + UInt32 curPrice = curOpt->price; + unsigned prob = p->isMatch[state][posState]; + matchPrice = curPrice + GET_PRICE_1(prob); + litPrice = curPrice + GET_PRICE_0(prob); + } + + nextOpt = &p->opt[(size_t)cur + 1]; + nextIsLit = False; + + // here we can allow skip_items in p->opt, if we don't check (nextOpt->price < kInfinityPrice) + // 18.new.06 + if ((nextOpt->price < kInfinityPrice + // && !IsLitState(state) + && matchByte == curByte) + || litPrice > nextOpt->price + ) + litPrice = 0; + else + { + const CLzmaProb* probs = LIT_PROBS(position, *(data - 1)); + litPrice += (!IsLitState(state) ? + LitEnc_Matched_GetPrice(probs, curByte, matchByte, p->ProbPrices) : + LitEnc_GetPrice(probs, curByte, p->ProbPrices)); + + if (litPrice < nextOpt->price) + { + nextOpt->price = litPrice; + nextOpt->len = 1; + MakeAs_Lit(nextOpt); + nextIsLit = True; + } + } + + repMatchPrice = matchPrice + GET_PRICE_1(p->isRep[state]); + + numAvailFull = p->numAvail; + { + unsigned temp = kNumOpts - 1 - cur; + if (numAvailFull > temp) + numAvailFull = (UInt32)temp; + } + + // 18.06 + // ---------- SHORT_REP ---------- + if (IsLitState(state)) // 18.new + if (matchByte == curByte) + if (repMatchPrice < nextOpt->price) // 18.new + // if (numAvailFull < 2 || data[1] != *(data - reps[0] + 1)) + if ( + // nextOpt->price >= kInfinityPrice || + nextOpt->len < 2 // we can check nextOpt->len, if skip items are not allowed in p->opt + || (nextOpt->dist != 0 + // && nextOpt->extra <= 1 // 17.old + ) + ) + { + UInt32 shortRepPrice = repMatchPrice + GetPrice_ShortRep(p, state, posState); + // if (shortRepPrice <= nextOpt->price) // 17.old + if (shortRepPrice < nextOpt->price) // 18.new + { + nextOpt->price = shortRepPrice; + nextOpt->len = 1; + MakeAs_ShortRep(nextOpt); + nextIsLit = False; + } + } + + if (numAvailFull < 2) + continue; + numAvail = (numAvailFull <= p->numFastBytes ? numAvailFull : p->numFastBytes); + + // numAvail <= p->numFastBytes + + // ---------- LIT : REP_0 ---------- + + if (!nextIsLit + && litPrice != 0 // 18.new + && matchByte != curByte + && numAvailFull > 2) + { + const Byte* data2 = data - reps[0]; + if (data[1] == data2[1] && data[2] == data2[2]) + { + unsigned len; + unsigned limit = p->numFastBytes + 1; + if (limit > numAvailFull) + limit = numAvailFull; + for (len = 3; len < limit && data[len] == data2[len]; len++) + { + } + + { + unsigned state2 = kLiteralNextStates[state]; + unsigned posState2 = (position + 1) & p->pbMask; + UInt32 price = litPrice + GetPrice_Rep_0(p, state2, posState2); + { + unsigned offset = cur + len; + + if (last < offset) + last = offset; + + // do + { + UInt32 price2; + COptimal* opt; + len--; + // price2 = price + GetPrice_Len_Rep_0(p, len, state2, posState2); + price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len); + + opt = &p->opt[offset]; + // offset--; + if (price2 < opt->price) + { + opt->price = price2; + opt->len = (UInt32)len; + opt->dist = 0; + opt->extra = 1; + } + } + // while (len >= 3); + } + } + } + } + + startLen = 2; /* speed optimization */ + + { + // ---------- REP ---------- + unsigned repIndex = 0; // 17.old + // unsigned repIndex = IsLitState(state) ? 0 : 1; // 18.notused + for (; repIndex < LZMA_NUM_REPS; repIndex++) + { + unsigned len; + UInt32 price; + const Byte* data2 = data - reps[repIndex]; + if (data[0] != data2[0] || data[1] != data2[1]) + continue; + + for (len = 2; len < numAvail && data[len] == data2[len]; len++) + { + } + + // if (len < startLen) continue; // 18.new: speed optimization + + { + unsigned offset = cur + len; + if (last < offset) + last = offset; + } + { + unsigned len2 = len; + price = repMatchPrice + GetPrice_PureRep(p, repIndex, state, posState); + do + { + UInt32 price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState, len2); + COptimal* opt = &p->opt[cur + len2]; + if (price2 < opt->price) + { + opt->price = price2; + opt->len = (UInt32)len2; + opt->dist = (UInt32)repIndex; + opt->extra = 0; + } + } while (--len2 >= 2); + } + + if (repIndex == 0) startLen = len + 1; // 17.old + // startLen = len + 1; // 18.new + + /* if (_maxMode) */ + { + // ---------- REP : LIT : REP_0 ---------- + // numFastBytes + 1 + numFastBytes + + unsigned len2 = len + 1; + unsigned limit = len2 + p->numFastBytes; + if (limit > numAvailFull) + limit = numAvailFull; + + len2 += 2; + if (len2 <= limit) + if (data[len2 - 2] == data2[len2 - 2]) + if (data[len2 - 1] == data2[len2 - 1]) + { + unsigned state2 = kRepNextStates[state]; + unsigned posState2 = (position + len) & p->pbMask; + price += GET_PRICE_LEN(&p->repLenEnc, posState, len) + + GET_PRICE_0(p->isMatch[state2][posState2]) + + LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]), + data[len], data2[len], p->ProbPrices); + + // state2 = kLiteralNextStates[state2]; + state2 = kState_LitAfterRep; + posState2 = (posState2 + 1) & p->pbMask; + + price += GetPrice_Rep_0(p, state2, posState2); + + for (; len2 < limit && data[len2] == data2[len2]; len2++) + { + } + + len2 -= len; + // if (len2 >= 3) + { + { + unsigned offset = cur + len + len2; + + if (last < offset) + last = offset; + // do + { + UInt32 price2; + COptimal* opt; + len2--; + // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2); + price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2); + + opt = &p->opt[offset]; + // offset--; + if (price2 < opt->price) + { + opt->price = price2; + opt->len = (UInt32)len2; + opt->extra = (CExtra)(len + 1); + opt->dist = (UInt32)repIndex; + } + } + // while (len2 >= 3); + } + } + } + } + } + } + + // ---------- MATCH ---------- + /* for (unsigned len = 2; len <= newLen; len++) */ + if (newLen > numAvail) + { + newLen = numAvail; + for (numPairs = 0; newLen > matches[numPairs]; numPairs += 2); + matches[numPairs] = (UInt32)newLen; + numPairs += 2; + } + + // startLen = 2; /* speed optimization */ + + if (newLen >= startLen) + { + UInt32 normalMatchPrice = matchPrice + GET_PRICE_0(p->isRep[state]); + UInt32 dist; + unsigned offs, posSlot, len; + + { + unsigned offset = cur + newLen; + if (last < offset) + last = offset; + } + + offs = 0; + while (startLen > matches[offs]) + offs += 2; + dist = matches[(size_t)offs + 1]; + + // if (dist >= kNumFullDistances) + GetPosSlot2(dist, posSlot); + + for (len = /*2*/ startLen; ; len++) + { + UInt32 price = normalMatchPrice + GET_PRICE_LEN(&p->lenEnc, posState, len); + { + COptimal* opt; + unsigned lenNorm = len - 2; + lenNorm = GetLenToPosState2(lenNorm); + if (dist < kNumFullDistances) + price += p->distancesPrices[lenNorm][dist & (kNumFullDistances - 1)]; + else + price += p->posSlotPrices[lenNorm][posSlot] + p->alignPrices[dist & kAlignMask]; + + opt = &p->opt[cur + len]; + if (price < opt->price) + { + opt->price = price; + opt->len = (UInt32)len; + opt->dist = dist + LZMA_NUM_REPS; + opt->extra = 0; + } + } + + if (len == matches[offs]) + { + // if (p->_maxMode) { + // MATCH : LIT : REP_0 + + const Byte* data2 = data - dist - 1; + unsigned len2 = len + 1; + unsigned limit = len2 + p->numFastBytes; + if (limit > numAvailFull) + limit = numAvailFull; + + len2 += 2; + if (len2 <= limit) + if (data[len2 - 2] == data2[len2 - 2]) + if (data[len2 - 1] == data2[len2 - 1]) + { + for (; len2 < limit && data[len2] == data2[len2]; len2++) + { + } + + len2 -= len; + + // if (len2 >= 3) + { + unsigned state2 = kMatchNextStates[state]; + unsigned posState2 = (position + len) & p->pbMask; + unsigned offset; + price += GET_PRICE_0(p->isMatch[state2][posState2]); + price += LitEnc_Matched_GetPrice(LIT_PROBS(position + len, data[(size_t)len - 1]), + data[len], data2[len], p->ProbPrices); + + // state2 = kLiteralNextStates[state2]; + state2 = kState_LitAfterMatch; + + posState2 = (posState2 + 1) & p->pbMask; + price += GetPrice_Rep_0(p, state2, posState2); + + offset = cur + len + len2; + + if (last < offset) + last = offset; + // do + { + UInt32 price2; + COptimal* opt; + len2--; + // price2 = price + GetPrice_Len_Rep_0(p, len2, state2, posState2); + price2 = price + GET_PRICE_LEN(&p->repLenEnc, posState2, len2); + opt = &p->opt[offset]; + // offset--; + if (price2 < opt->price) + { + opt->price = price2; + opt->len = (UInt32)len2; + opt->extra = (CExtra)(len + 1); + opt->dist = dist + LZMA_NUM_REPS; + } + } + // while (len2 >= 3); + } + + } + + offs += 2; + if (offs == numPairs) + break; + dist = matches[(size_t)offs + 1]; + // if (dist >= kNumFullDistances) + GetPosSlot2(dist, posSlot); + } + } + } + } + + do + p->opt[last].price = kInfinityPrice; + while (--last); + + return Backward(p, cur); + } + +#define ChangePair(smallDist, bigDist) (((bigDist) >> 7) > (smallDist)) + + static unsigned GetOptimumFast(CLzmaEnc* p) + { + UInt32 numAvail, mainDist; + unsigned mainLen, numPairs, repIndex, repLen, i; + const Byte* data; + + if (p->additionalOffset == 0) + mainLen = ReadMatchDistances(p, &numPairs); + else + { + mainLen = p->longestMatchLen; + numPairs = p->numPairs; + } + + numAvail = p->numAvail; + p->backRes = MARK_LIT; + if (numAvail < 2) + return 1; + // if (mainLen < 2 && p->state == 0) return 1; // 18.06.notused + if (numAvail > LZMA_MATCH_LEN_MAX) + numAvail = LZMA_MATCH_LEN_MAX; + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + repLen = repIndex = 0; + + for (i = 0; i < LZMA_NUM_REPS; i++) + { + unsigned len; + const Byte* data2 = data - p->reps[i]; + if (data[0] != data2[0] || data[1] != data2[1]) + continue; + for (len = 2; len < numAvail && data[len] == data2[len]; len++) + { + } + if (len >= p->numFastBytes) + { + p->backRes = (UInt32)i; + MOVE_POS(p, len - 1) + return len; + } + if (len > repLen) + { + repIndex = i; + repLen = len; + } + } + + if (mainLen >= p->numFastBytes) + { + p->backRes = p->matches[(size_t)numPairs - 1] + LZMA_NUM_REPS; + MOVE_POS(p, mainLen - 1) + return mainLen; + } + + mainDist = 0; /* for GCC */ + + if (mainLen >= 2) + { + mainDist = p->matches[(size_t)numPairs - 1]; + while (numPairs > 2) + { + UInt32 dist2; + if (mainLen != p->matches[(size_t)numPairs - 4] + 1) + break; + dist2 = p->matches[(size_t)numPairs - 3]; + if (!ChangePair(dist2, mainDist)) + break; + numPairs -= 2; + mainLen--; + mainDist = dist2; + } + if (mainLen == 2 && mainDist >= 0x80) + mainLen = 1; + } + + if (repLen >= 2) + if (repLen + 1 >= mainLen + || (repLen + 2 >= mainLen && mainDist >= (1 << 9)) + || (repLen + 3 >= mainLen && mainDist >= (1 << 15))) + { + p->backRes = (UInt32)repIndex; + MOVE_POS(p, repLen - 1) + return repLen; + } + + if (mainLen < 2 || numAvail <= 2) + return 1; + + { + unsigned len1 = ReadMatchDistances(p, &p->numPairs); + p->longestMatchLen = len1; + + if (len1 >= 2) + { + UInt32 newDist = p->matches[(size_t)p->numPairs - 1]; + if ((len1 >= mainLen && newDist < mainDist) + || (len1 == mainLen + 1 && !ChangePair(mainDist, newDist)) + || (len1 > mainLen + 1) + || (len1 + 1 >= mainLen && mainLen >= 3 && ChangePair(newDist, mainDist))) + return 1; + } + } + + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - 1; + + for (i = 0; i < LZMA_NUM_REPS; i++) + { + unsigned len, limit; + const Byte* data2 = data - p->reps[i]; + if (data[0] != data2[0] || data[1] != data2[1]) + continue; + limit = mainLen - 1; + for (len = 2;; len++) + { + if (len >= limit) + return 1; + if (data[len] != data2[len]) + break; + } + } + + p->backRes = mainDist + LZMA_NUM_REPS; + if (mainLen != 2) + { + MOVE_POS(p, mainLen - 2) + } + return mainLen; + } + + static void WriteEndMarker(CLzmaEnc* p, unsigned posState) + { + UInt32 range; + range = p->rc.range; + { + UInt32 ttt, newBound; + CLzmaProb* prob = &p->isMatch[p->state][posState]; + RC_BIT_PRE(&p->rc, prob) + RC_BIT_1(&p->rc, prob) + prob = &p->isRep[p->state]; + RC_BIT_PRE(&p->rc, prob) + RC_BIT_0(&p->rc, prob) + } + p->state = kMatchNextStates[p->state]; + + p->rc.range = range; + LenEnc_Encode(&p->lenProbs, &p->rc, 0, posState); + range = p->rc.range; + + { + // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[0], (1 << kNumPosSlotBits) - 1); + CLzmaProb* probs = p->posSlotEncoder[0]; + unsigned m = 1; + do + { + UInt32 ttt, newBound; + RC_BIT_PRE(p, probs + m) + RC_BIT_1(&p->rc, probs + m); + m = (m << 1) + 1; + } while (m < (1 << kNumPosSlotBits)); + } + { + // RangeEnc_EncodeDirectBits(&p->rc, ((UInt32)1 << (30 - kNumAlignBits)) - 1, 30 - kNumAlignBits); UInt32 range = p->range; + unsigned numBits = 30 - kNumAlignBits; + do + { + range >>= 1; + p->rc.low += range; + RC_NORM(&p->rc) + } while (--numBits); + } + + { + // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, kAlignMask); + CLzmaProb* probs = p->posAlignEncoder; + unsigned m = 1; + do + { + UInt32 ttt, newBound; + RC_BIT_PRE(p, probs + m) + RC_BIT_1(&p->rc, probs + m); + m = (m << 1) + 1; + } while (m < kAlignTableSize); + } + p->rc.range = range; + } + + static SRes CheckErrors(CLzmaEnc* p) + { + if (p->result != SZ_OK) + return p->result; + if (p->rc.res != SZ_OK) + p->result = SZ_ERROR_WRITE; + if (p->matchFinderBase.result != SZ_OK) + p->result = SZ_ERROR_READ; + if (p->result != SZ_OK) + p->finished = True; + return p->result; + } + + MY_NO_INLINE static SRes Flush(CLzmaEnc* p, UInt32 nowPos) + { + /* ReleaseMFStream(); */ + p->finished = True; + if (p->writeEndMark) + WriteEndMarker(p, nowPos & p->pbMask); + RangeEnc_FlushData(&p->rc); + RangeEnc_FlushStream(&p->rc); + return CheckErrors(p); + } + + MY_NO_INLINE static void FillAlignPrices(CLzmaEnc* p) + { + unsigned i; + const CProbPrice* ProbPrices = p->ProbPrices; + const CLzmaProb* probs = p->posAlignEncoder; + // p->alignPriceCount = 0; + for (i = 0; i < kAlignTableSize / 2; i++) + { + UInt32 price = 0; + unsigned sym = i; + unsigned m = 1; + unsigned bit; + UInt32 prob; + bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit; + bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit; + bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[m], bit); m = (m << 1) + bit; + prob = probs[m]; + p->alignPrices[i] = price + GET_PRICEa_0(prob); + p->alignPrices[i + 8] = price + GET_PRICEa_1(prob); + // p->alignPrices[i] = RcTree_ReverseGetPrice(p->posAlignEncoder, kNumAlignBits, i, p->ProbPrices); + } + } + + MY_NO_INLINE static void FillDistancesPrices(CLzmaEnc* p) + { + // int y; for (y = 0; y < 100; y++) { + + UInt32 tempPrices[kNumFullDistances]; + unsigned i, lps; + + const CProbPrice* ProbPrices = p->ProbPrices; + p->matchPriceCount = 0; + + for (i = kStartPosModelIndex / 2; i < kNumFullDistances / 2; i++) + { + unsigned posSlot = GetPosSlot1(i); + unsigned footerBits = (posSlot >> 1) - 1; + unsigned base = ((2 | (posSlot & 1)) << footerBits); + const CLzmaProb* probs = p->posEncoders + (size_t)base * 2; + // tempPrices[i] = RcTree_ReverseGetPrice(p->posEncoders + base, footerBits, i - base, p->ProbPrices); + UInt32 price = 0; + unsigned m = 1; + unsigned sym = i; + unsigned offset = (unsigned)1 << footerBits; + base += i; + + if (footerBits) + do + { + unsigned bit = sym & 1; + sym >>= 1; + price += GET_PRICEa(probs[m], bit); + m = (m << 1) + bit; + } while (--footerBits); + + { + unsigned prob = probs[m]; + tempPrices[base] = price + GET_PRICEa_0(prob); + tempPrices[base + offset] = price + GET_PRICEa_1(prob); + } + } + + for (lps = 0; lps < kNumLenToPosStates; lps++) + { + unsigned slot; + unsigned distTableSize2 = (p->distTableSize + 1) >> 1; + UInt32* posSlotPrices = p->posSlotPrices[lps]; + const CLzmaProb* probs = p->posSlotEncoder[lps]; + + for (slot = 0; slot < distTableSize2; slot++) + { + // posSlotPrices[slot] = RcTree_GetPrice(encoder, kNumPosSlotBits, slot, p->ProbPrices); + UInt32 price; + unsigned bit; + unsigned sym = slot + (1 << (kNumPosSlotBits - 1)); + unsigned prob; + bit = sym & 1; sym >>= 1; price = GET_PRICEa(probs[sym], bit); + bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); + bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); + bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); + bit = sym & 1; sym >>= 1; price += GET_PRICEa(probs[sym], bit); + prob = probs[(size_t)slot + (1 << (kNumPosSlotBits - 1))]; + posSlotPrices[(size_t)slot * 2] = price + GET_PRICEa_0(prob); + posSlotPrices[(size_t)slot * 2 + 1] = price + GET_PRICEa_1(prob); + } + + { + UInt32 delta = ((UInt32)((kEndPosModelIndex / 2 - 1) - kNumAlignBits) << kNumBitPriceShiftBits); + for (slot = kEndPosModelIndex / 2; slot < distTableSize2; slot++) + { + posSlotPrices[(size_t)slot * 2] += delta; + posSlotPrices[(size_t)slot * 2 + 1] += delta; + delta += ((UInt32)1 << kNumBitPriceShiftBits); + } + } + + { + UInt32* dp = p->distancesPrices[lps]; + + dp[0] = posSlotPrices[0]; + dp[1] = posSlotPrices[1]; + dp[2] = posSlotPrices[2]; + dp[3] = posSlotPrices[3]; + + for (i = 4; i < kNumFullDistances; i += 2) + { + UInt32 slotPrice = posSlotPrices[GetPosSlot1(i)]; + dp[i] = slotPrice + tempPrices[i]; + dp[i + 1] = slotPrice + tempPrices[i + 1]; + } + } + } + // } + } + + void LzmaEnc_Construct(CLzmaEnc* p) + { + RangeEnc_Construct(&p->rc); + MatchFinder_Construct(&p->matchFinderBase); + + { + CLzmaEncProps props; + LzmaEncProps_Init(&props); + LzmaEnc_SetProps(p, &props); + } + +#ifndef LZMA_LOG_BSR + LzmaEnc_FastPosInit(p->g_FastPos); +#endif + + LzmaEnc_InitPriceTables(p->ProbPrices); + p->litProbs = NULL; + p->saveState.litProbs = NULL; + + } + + CLzmaEncHandle LzmaEnc_Create(ISzAllocPtr alloc) + { + void* p; + p = ISzAlloc_Alloc(alloc, sizeof(CLzmaEnc)); + if (p) + LzmaEnc_Construct((CLzmaEnc*)p); + return p; + } + + void LzmaEnc_FreeLits(CLzmaEnc* p, ISzAllocPtr alloc) + { + ISzAlloc_Free(alloc, p->litProbs); + ISzAlloc_Free(alloc, p->saveState.litProbs); + p->litProbs = NULL; + p->saveState.litProbs = NULL; + } + + void LzmaEnc_Destruct(CLzmaEnc* p, ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + MatchFinder_Free(&p->matchFinderBase, allocBig); + LzmaEnc_FreeLits(p, alloc); + RangeEnc_Free(&p->rc, alloc); + } + + void LzmaEnc_Destroy(CLzmaEncHandle p, ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + LzmaEnc_Destruct((CLzmaEnc*)p, alloc, allocBig); + ISzAlloc_Free(alloc, p); + } + + static SRes LzmaEnc_CodeOneBlock(CLzmaEnc* p, UInt32 maxPackSize, UInt32 maxUnpackSize) + { + UInt32 nowPos32, startPos32; + if (p->needInit) + { + p->matchFinder.Init(p->matchFinderObj); + p->needInit = 0; + } + + if (p->finished) + return p->result; + RINOK(CheckErrors(p)); + + nowPos32 = (UInt32)p->nowPos64; + startPos32 = nowPos32; + + if (p->nowPos64 == 0) + { + unsigned numPairs; + Byte curByte; + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) + return Flush(p, nowPos32); + ReadMatchDistances(p, &numPairs); + RangeEnc_EncodeBit_0(&p->rc, &p->isMatch[kState_Start][0]); + // p->state = kLiteralNextStates[p->state]; + curByte = *(p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset); + LitEnc_Encode(&p->rc, p->litProbs, curByte); + p->additionalOffset--; + nowPos32++; + } + + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) != 0) + + for (;;) + { + UInt32 dist; + unsigned len, posState; + UInt32 range, ttt, newBound; + CLzmaProb* probs; + + if (p->fastMode) + len = GetOptimumFast(p); + else + { + unsigned oci = p->optCur; + if (p->optEnd == oci) + len = GetOptimum(p, nowPos32); + else + { + const COptimal* opt = &p->opt[oci]; + len = opt->len; + p->backRes = opt->dist; + p->optCur = oci + 1; + } + } + + posState = (unsigned)nowPos32 & p->pbMask; + range = p->rc.range; + probs = &p->isMatch[p->state][posState]; + + RC_BIT_PRE(&p->rc, probs) + + dist = p->backRes; + +#ifdef SHOW_STAT2 + printf("\n pos = %6X, len = %3u pos = %6u", nowPos32, len, dist); +#endif + + if (dist == MARK_LIT) + { + Byte curByte; + const Byte* data; + unsigned state; + + RC_BIT_0(&p->rc, probs); + p->rc.range = range; + data = p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; + probs = LIT_PROBS(nowPos32, *(data - 1)); + curByte = *data; + state = p->state; + p->state = kLiteralNextStates[state]; + if (IsLitState(state)) + LitEnc_Encode(&p->rc, probs, curByte); + else + LitEnc_EncodeMatched(&p->rc, probs, curByte, *(data - p->reps[0])); + } + else + { + RC_BIT_1(&p->rc, probs); + probs = &p->isRep[p->state]; + RC_BIT_PRE(&p->rc, probs) + + if (dist < LZMA_NUM_REPS) + { + RC_BIT_1(&p->rc, probs); + probs = &p->isRepG0[p->state]; + RC_BIT_PRE(&p->rc, probs) + if (dist == 0) + { + RC_BIT_0(&p->rc, probs); + probs = &p->isRep0Long[p->state][posState]; + RC_BIT_PRE(&p->rc, probs) + if (len != 1) + { + RC_BIT_1_BASE(&p->rc, probs); + } + else + { + RC_BIT_0_BASE(&p->rc, probs); + p->state = kShortRepNextStates[p->state]; + } + } + else + { + RC_BIT_1(&p->rc, probs); + probs = &p->isRepG1[p->state]; + RC_BIT_PRE(&p->rc, probs) + if (dist == 1) + { + RC_BIT_0_BASE(&p->rc, probs); + dist = p->reps[1]; + } + else + { + RC_BIT_1(&p->rc, probs); + probs = &p->isRepG2[p->state]; + RC_BIT_PRE(&p->rc, probs) + if (dist == 2) + { + RC_BIT_0_BASE(&p->rc, probs); + dist = p->reps[2]; + } + else + { + RC_BIT_1_BASE(&p->rc, probs); + dist = p->reps[3]; + p->reps[3] = p->reps[2]; + } + p->reps[2] = p->reps[1]; + } + p->reps[1] = p->reps[0]; + p->reps[0] = dist; + } + + RC_NORM(&p->rc) + + p->rc.range = range; + + if (len != 1) + { + LenEnc_Encode(&p->repLenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState); + --p->repLenEncCounter; + p->state = kRepNextStates[p->state]; + } + } + else + { + unsigned posSlot; + RC_BIT_0(&p->rc, probs); + p->rc.range = range; + p->state = kMatchNextStates[p->state]; + + LenEnc_Encode(&p->lenProbs, &p->rc, len - LZMA_MATCH_LEN_MIN, posState); + // --p->lenEnc.counter; + + dist -= LZMA_NUM_REPS; + p->reps[3] = p->reps[2]; + p->reps[2] = p->reps[1]; + p->reps[1] = p->reps[0]; + p->reps[0] = dist + 1; + + p->matchPriceCount++; + GetPosSlot(dist, posSlot); + // RcTree_Encode_PosSlot(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], posSlot); + { + UInt32 sym = (UInt32)posSlot + (1 << kNumPosSlotBits); + range = p->rc.range; + probs = p->posSlotEncoder[GetLenToPosState(len)]; + do + { + CLzmaProb* prob = probs + (sym >> kNumPosSlotBits); + UInt32 bit = (sym >> (kNumPosSlotBits - 1)) & 1; + sym <<= 1; + RC_BIT(&p->rc, prob, bit); + } while (sym < (1 << kNumPosSlotBits * 2)); + p->rc.range = range; + } + + if (dist >= kStartPosModelIndex) + { + unsigned footerBits = ((posSlot >> 1) - 1); + + if (dist < kNumFullDistances) + { + unsigned base = ((2 | (posSlot & 1)) << footerBits); + RcTree_ReverseEncode(&p->rc, p->posEncoders + base, footerBits, (unsigned)(dist /* - base */)); + } + else + { + UInt32 pos2 = (dist | 0xF) << (32 - footerBits); + range = p->rc.range; + // RangeEnc_EncodeDirectBits(&p->rc, posReduced >> kNumAlignBits, footerBits - kNumAlignBits); + /* + do + { + range >>= 1; + p->rc.low += range & (0 - ((dist >> --footerBits) & 1)); + RC_NORM(&p->rc) + } + while (footerBits > kNumAlignBits); + */ + do + { + range >>= 1; + p->rc.low += range & (0 - (pos2 >> 31)); + pos2 += pos2; + RC_NORM(&p->rc) + } while (pos2 != 0xF0000000); + + // RcTree_ReverseEncode(&p->rc, p->posAlignEncoder, kNumAlignBits, posReduced & kAlignMask); + + { + unsigned m = 1; + unsigned bit; + bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; + bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; + bit = dist & 1; dist >>= 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); m = (m << 1) + bit; + bit = dist & 1; RC_BIT(&p->rc, p->posAlignEncoder + m, bit); + p->rc.range = range; + // p->alignPriceCount++; + } + } + } + } + } + + nowPos32 += (UInt32)len; + p->additionalOffset -= len; + + if (p->additionalOffset == 0) + { + UInt32 processed; + + if (!p->fastMode) + { + /* + if (p->alignPriceCount >= 16) // kAlignTableSize + FillAlignPrices(p); + if (p->matchPriceCount >= 128) + FillDistancesPrices(p); + if (p->lenEnc.counter <= 0) + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices); + */ + if (p->matchPriceCount >= 64) + { + FillAlignPrices(p); + // { int y; for (y = 0; y < 100; y++) { + FillDistancesPrices(p); + // }} + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices); + } + if (p->repLenEncCounter <= 0) + { + p->repLenEncCounter = REP_LEN_COUNT; + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, &p->repLenProbs, p->ProbPrices); + } + } + + if (p->matchFinder.GetNumAvailableBytes(p->matchFinderObj) == 0) + break; + processed = nowPos32 - startPos32; + + if (maxPackSize) + { + if (processed + kNumOpts + 300 >= maxUnpackSize + || RangeEnc_GetProcessed_sizet(&p->rc) + kPackReserve >= maxPackSize) + break; + } + else if (processed >= (1 << 17)) + { + p->nowPos64 += nowPos32 - startPos32; + return CheckErrors(p); + } + } + } + + p->nowPos64 += nowPos32 - startPos32; + return Flush(p, nowPos32); + } + +#define kBigHashDicLimit ((UInt32)1 << 24) + + static SRes LzmaEnc_Alloc(CLzmaEnc* p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + UInt32 beforeSize = kNumOpts; + if (!RangeEnc_Alloc(&p->rc, alloc)) + return SZ_ERROR_MEM; + + { + unsigned lclp = p->lc + p->lp; + if (!p->litProbs || !p->saveState.litProbs || p->lclp != lclp) + { + LzmaEnc_FreeLits(p, alloc); + p->litProbs = (CLzmaProb*)ISzAlloc_Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb)); + p->saveState.litProbs = (CLzmaProb*)ISzAlloc_Alloc(alloc, ((UInt32)0x300 << lclp) * sizeof(CLzmaProb)); + if (!p->litProbs || !p->saveState.litProbs) + { + LzmaEnc_FreeLits(p, alloc); + return SZ_ERROR_MEM; + } + p->lclp = lclp; + } + } + + p->matchFinderBase.bigHash = (Byte)(p->dictSize > kBigHashDicLimit ? 1 : 0); + + if (beforeSize + p->dictSize < keepWindowSize) + beforeSize = keepWindowSize - p->dictSize; + + { + if (!MatchFinder_Create(&p->matchFinderBase, p->dictSize, beforeSize, p->numFastBytes, LZMA_MATCH_LEN_MAX, allocBig)) + return SZ_ERROR_MEM; + p->matchFinderObj = &p->matchFinderBase; + MatchFinder_CreateVTable(&p->matchFinderBase, &p->matchFinder); + } + + return SZ_OK; + } + + void LzmaEnc_Init(CLzmaEnc* p) + { + unsigned i; + p->state = 0; + p->reps[0] = + p->reps[1] = + p->reps[2] = + p->reps[3] = 1; + + RangeEnc_Init(&p->rc); + + for (i = 0; i < (1 << kNumAlignBits); i++) + p->posAlignEncoder[i] = kProbInitValue; + + for (i = 0; i < kNumStates; i++) + { + unsigned j; + for (j = 0; j < LZMA_NUM_PB_STATES_MAX; j++) + { + p->isMatch[i][j] = kProbInitValue; + p->isRep0Long[i][j] = kProbInitValue; + } + p->isRep[i] = kProbInitValue; + p->isRepG0[i] = kProbInitValue; + p->isRepG1[i] = kProbInitValue; + p->isRepG2[i] = kProbInitValue; + } + + { + for (i = 0; i < kNumLenToPosStates; i++) + { + CLzmaProb* probs = p->posSlotEncoder[i]; + unsigned j; + for (j = 0; j < (1 << kNumPosSlotBits); j++) + probs[j] = kProbInitValue; + } + } + { + for (i = 0; i < kNumFullDistances; i++) + p->posEncoders[i] = kProbInitValue; + } + + { + UInt32 num = (UInt32)0x300 << (p->lp + p->lc); + UInt32 k; + CLzmaProb* probs = p->litProbs; + for (k = 0; k < num; k++) + probs[k] = kProbInitValue; + } + + LenEnc_Init(&p->lenProbs); + LenEnc_Init(&p->repLenProbs); + + p->optEnd = 0; + p->optCur = 0; + + { + for (i = 0; i < kNumOpts; i++) + p->opt[i].price = kInfinityPrice; + } + + p->additionalOffset = 0; + + p->pbMask = (1 << p->pb) - 1; + p->lpMask = ((UInt32)0x100 << p->lp) - ((unsigned)0x100 >> p->lc); + } + + void LzmaEnc_InitPrices(CLzmaEnc* p) + { + if (!p->fastMode) + { + FillDistancesPrices(p); + FillAlignPrices(p); + } + + p->lenEnc.tableSize = + p->repLenEnc.tableSize = + p->numFastBytes + 1 - LZMA_MATCH_LEN_MIN; + + p->repLenEncCounter = REP_LEN_COUNT; + + LenPriceEnc_UpdateTables(&p->lenEnc, 1 << p->pb, &p->lenProbs, p->ProbPrices); + LenPriceEnc_UpdateTables(&p->repLenEnc, 1 << p->pb, &p->repLenProbs, p->ProbPrices); + } + + static SRes LzmaEnc_AllocAndInit(CLzmaEnc* p, UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + unsigned i; + for (i = kEndPosModelIndex / 2; i < kDicLogSizeMax; i++) + if (p->dictSize <= ((UInt32)1 << i)) + break; + p->distTableSize = i * 2; + + p->finished = False; + p->result = SZ_OK; + RINOK(LzmaEnc_Alloc(p, keepWindowSize, alloc, allocBig)); + LzmaEnc_Init(p); + LzmaEnc_InitPrices(p); + p->nowPos64 = 0; + return SZ_OK; + } + + static SRes LzmaEnc_Prepare(CLzmaEncHandle pp, ISeqOutStream* outStream, ISeqInStream* inStream, + ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + p->matchFinderBase.stream = inStream; + p->needInit = 1; + p->rc.outStream = outStream; + return LzmaEnc_AllocAndInit(p, 0, alloc, allocBig); + } + + SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, + ISeqInStream* inStream, UInt32 keepWindowSize, + ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + p->matchFinderBase.stream = inStream; + p->needInit = 1; + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); + } + + static void LzmaEnc_SetInputBuf(CLzmaEnc* p, const Byte* src, SizeT srcLen) + { + p->matchFinderBase.directInput = 1; + p->matchFinderBase.bufferBase = (Byte*)src; + p->matchFinderBase.directInputRem = srcLen; + } + + SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte* src, SizeT srcLen, + UInt32 keepWindowSize, ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + LzmaEnc_SetInputBuf(p, src, srcLen); + p->needInit = 1; + + LzmaEnc_SetDataSize(pp, srcLen); + return LzmaEnc_AllocAndInit(p, keepWindowSize, alloc, allocBig); + } + + void LzmaEnc_Finish(CLzmaEncHandle pp) + { + UNUSED_VAR(pp) + } + + typedef struct + { + ISeqOutStream vt; + Byte* data; + SizeT rem; + BoolInt overflow; + } CLzmaEnc_SeqOutStreamBuf; + + static size_t SeqOutStreamBuf_Write(const ISeqOutStream* pp, const void* data, size_t size) + { + CLzmaEnc_SeqOutStreamBuf* p = CONTAINER_FROM_VTBL(pp, CLzmaEnc_SeqOutStreamBuf, vt); + if (p->rem < size) + { + size = p->rem; + p->overflow = True; + } + memcpy(p->data, data, size); + p->rem -= size; + p->data += size; + return size; + } + + UInt32 LzmaEnc_GetNumAvailableBytes(CLzmaEncHandle pp) + { + const CLzmaEnc* p = (CLzmaEnc*)pp; + return p->matchFinder.GetNumAvailableBytes(p->matchFinderObj); + } + + const Byte* LzmaEnc_GetCurBuf(CLzmaEncHandle pp) + { + const CLzmaEnc* p = (CLzmaEnc*)pp; + return p->matchFinder.GetPointerToCurrentPos(p->matchFinderObj) - p->additionalOffset; + } + + SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, BoolInt reInit, + Byte* dest, size_t* destLen, UInt32 desiredPackSize, UInt32* unpackSize) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + UInt64 nowPos64; + SRes res; + CLzmaEnc_SeqOutStreamBuf outStream; + + outStream.vt.Write = SeqOutStreamBuf_Write; + outStream.data = dest; + outStream.rem = *destLen; + outStream.overflow = False; + + p->writeEndMark = False; + p->finished = False; + p->result = SZ_OK; + + if (reInit) + LzmaEnc_Init(p); + LzmaEnc_InitPrices(p); + + nowPos64 = p->nowPos64; + RangeEnc_Init(&p->rc); + p->rc.outStream = &outStream.vt; + + if (desiredPackSize == 0) + return SZ_ERROR_OUTPUT_EOF; + + res = LzmaEnc_CodeOneBlock(p, desiredPackSize, *unpackSize); + + *unpackSize = (UInt32)(p->nowPos64 - nowPos64); + *destLen -= outStream.rem; + if (outStream.overflow) + return SZ_ERROR_OUTPUT_EOF; + + return res; + } + + static SRes LzmaEnc_Encode2(CLzmaEnc* p, ICompressProgress* progress) + { + SRes res = SZ_OK; + + for (;;) + { + res = LzmaEnc_CodeOneBlock(p, 0, 0); + if (res != SZ_OK || p->finished) + break; + if (progress) + { + res = ICompressProgress_Progress(progress, p->nowPos64, RangeEnc_GetProcessed(&p->rc)); + if (res != SZ_OK) + { + res = SZ_ERROR_PROGRESS; + break; + } + } + } + + LzmaEnc_Finish(p); + + /* + if (res == SZ_OK && !Inline_MatchFinder_IsFinishedOK(&p->matchFinderBase)) + res = SZ_ERROR_FAIL; + } + */ + + return res; + } + + SRes LzmaEnc_Encode(CLzmaEncHandle pp, ISeqOutStream* outStream, ISeqInStream* inStream, ICompressProgress* progress, + ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + RINOK(LzmaEnc_Prepare(pp, outStream, inStream, alloc, allocBig)); + return LzmaEnc_Encode2((CLzmaEnc*)pp, progress); + } + + SRes LzmaEnc_WriteProperties(CLzmaEncHandle pp, Byte* props, SizeT* size) + { + CLzmaEnc* p = (CLzmaEnc*)pp; + unsigned i; + UInt32 dictSize = p->dictSize; + if (*size < LZMA_PROPS_SIZE) + return SZ_ERROR_PARAM; + *size = LZMA_PROPS_SIZE; + props[0] = (Byte)((p->pb * 5 + p->lp) * 9 + p->lc); + + if (dictSize >= ((UInt32)1 << 22)) + { + UInt32 kDictMask = ((UInt32)1 << 20) - 1; + if (dictSize < (UInt32)0xFFFFFFFF - kDictMask) + dictSize = (dictSize + kDictMask) & ~kDictMask; + } + else for (i = 11; i <= 30; i++) + { + if (dictSize <= ((UInt32)2 << i)) { dictSize = (2 << i); break; } + if (dictSize <= ((UInt32)3 << i)) { dictSize = (3 << i); break; } + } + + for (i = 0; i < 4; i++) + props[1 + i] = (Byte)(dictSize >> (8 * i)); + return SZ_OK; + } + + unsigned LzmaEnc_IsWriteEndMark(CLzmaEncHandle pp) + { + return ((CLzmaEnc*)pp)->writeEndMark; + } + + SRes LzmaEnc_MemEncode(CLzmaEncHandle pp, Byte* dest, SizeT* destLen, const Byte* src, SizeT srcLen, + int writeEndMark, ICompressProgress* progress, ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + SRes res; + CLzmaEnc* p = (CLzmaEnc*)pp; + + CLzmaEnc_SeqOutStreamBuf outStream; + + outStream.vt.Write = SeqOutStreamBuf_Write; + outStream.data = dest; + outStream.rem = *destLen; + outStream.overflow = False; + + p->writeEndMark = writeEndMark; + p->rc.outStream = &outStream.vt; + + res = LzmaEnc_MemPrepare(pp, src, srcLen, 0, alloc, allocBig); + + if (res == SZ_OK) + { + res = LzmaEnc_Encode2(p, progress); + if (res == SZ_OK && p->nowPos64 != srcLen) + res = SZ_ERROR_FAIL; + } + + *destLen -= outStream.rem; + if (outStream.overflow) + return SZ_ERROR_OUTPUT_EOF; + return res; + } + + SRes LzmaEncode(Byte* dest, SizeT* destLen, const Byte* src, SizeT srcLen, + const CLzmaEncProps* props, Byte* propsEncoded, SizeT* propsSize, int writeEndMark, + ICompressProgress* progress, ISzAllocPtr alloc, ISzAllocPtr allocBig) + { + CLzmaEnc* p = (CLzmaEnc*)LzmaEnc_Create(alloc); + SRes res; + if (!p) + return SZ_ERROR_MEM; + + res = LzmaEnc_SetProps(p, props); + if (res == SZ_OK) + { + res = LzmaEnc_WriteProperties(p, propsEncoded, propsSize); + if (res == SZ_OK) + res = LzmaEnc_MemEncode(p, dest, destLen, src, srcLen, + writeEndMark, progress, alloc, allocBig); + } + + LzmaEnc_Destroy(p, alloc, allocBig); + return res; + } + + /*** End of inlined file: LzmaEnc.c ***/ + + + /*** Start of inlined file: LzFind.c ***/ + //#include + + + /*** Start of inlined file: LzHash.h ***/ +#ifndef __LZ_HASH_H +#define __LZ_HASH_H + +#define kHash2Size (1 << 10) +#define kHash3Size (1 << 16) +#define kHash4Size (1 << 20) + +#define kFix3HashSize (kHash2Size) +#define kFix4HashSize (kHash2Size + kHash3Size) +#define kFix5HashSize (kHash2Size + kHash3Size + kHash4Size) + +#define HASH2_CALC hv = cur[0] | ((UInt32)cur[1] << 8); + +#define HASH3_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + h2 = temp & (kHash2Size - 1); \ + hv = (temp ^ ((UInt32)cur[2] << 8)) & p->hashMask; } + +#define HASH4_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + h2 = temp & (kHash2Size - 1); \ + temp ^= ((UInt32)cur[2] << 8); \ + h3 = temp & (kHash3Size - 1); \ + hv = (temp ^ (p->crc[cur[3]] << 5)) & p->hashMask; } + +#define HASH5_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + h2 = temp & (kHash2Size - 1); \ + temp ^= ((UInt32)cur[2] << 8); \ + h3 = temp & (kHash3Size - 1); \ + temp ^= (p->crc[cur[3]] << 5); \ + h4 = temp & (kHash4Size - 1); \ + hv = (temp ^ (p->crc[cur[4]] << 3)) & p->hashMask; } + +/* #define HASH_ZIP_CALC hv = ((cur[0] | ((UInt32)cur[1] << 8)) ^ p->crc[cur[2]]) & 0xFFFF; */ +#define HASH_ZIP_CALC hv = ((cur[2] | ((UInt32)cur[0] << 8)) ^ p->crc[cur[1]]) & 0xFFFF; + +#define MT_HASH2_CALC \ + h2 = (p->crc[cur[0]] ^ cur[1]) & (kHash2Size - 1); + +#define MT_HASH3_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + h2 = temp & (kHash2Size - 1); \ + h3 = (temp ^ ((UInt32)cur[2] << 8)) & (kHash3Size - 1); } + +#define MT_HASH4_CALC { \ + UInt32 temp = p->crc[cur[0]] ^ cur[1]; \ + h2 = temp & (kHash2Size - 1); \ + temp ^= ((UInt32)cur[2] << 8); \ + h3 = temp & (kHash3Size - 1); \ + h4 = (temp ^ (p->crc[cur[3]] << 5)) & (kHash4Size - 1); } + +#endif + +/*** End of inlined file: LzHash.h ***/ + +#define kEmptyHashValue 0 +#define kMaxValForNormalize ((UInt32)0xFFFFFFFF) +#define kNormalizeStepMin (1 << 10) /* it must be power of 2 */ +#define kNormalizeMask (~(UInt32)(kNormalizeStepMin - 1)) +#define kMaxHistorySize ((UInt32)7 << 29) + +#define kStartMaxLen 3 + + static void LzInWindow_Free(CMatchFinder* p, ISzAllocPtr alloc) + { + if (!p->directInput) + { + ISzAlloc_Free(alloc, p->bufferBase); + p->bufferBase = NULL; + } + } + + /* keepSizeBefore + keepSizeAfter + keepSizeReserv must be < 4G) */ + + static int LzInWindow_Create(CMatchFinder* p, UInt32 keepSizeReserv, ISzAllocPtr alloc) + { + UInt32 blockSize = p->keepSizeBefore + p->keepSizeAfter + keepSizeReserv; + if (p->directInput) + { + p->blockSize = blockSize; + return 1; + } + if (!p->bufferBase || p->blockSize != blockSize) + { + LzInWindow_Free(p, alloc); + p->blockSize = blockSize; + p->bufferBase = (Byte*)ISzAlloc_Alloc(alloc, (size_t)blockSize); + } + return (p->bufferBase != NULL); + } + + Byte* MatchFinder_GetPointerToCurrentPos(CMatchFinder* p) { return p->buffer; } + + UInt32 MatchFinder_GetNumAvailableBytes(CMatchFinder* p) { return p->streamPos - p->pos; } + + void MatchFinder_ReduceOffsets(CMatchFinder* p, UInt32 subValue) + { + p->posLimit -= subValue; + p->pos -= subValue; + p->streamPos -= subValue; + } + + static void MatchFinder_ReadBlock(CMatchFinder* p) + { + if (p->streamEndWasReached || p->result != SZ_OK) + return; + + /* We use (p->streamPos - p->pos) value. (p->streamPos < p->pos) is allowed. */ + + if (p->directInput) + { + UInt32 curSize = 0xFFFFFFFF - (p->streamPos - p->pos); + if (curSize > p->directInputRem) + curSize = (UInt32)p->directInputRem; + p->directInputRem -= curSize; + p->streamPos += curSize; + if (p->directInputRem == 0) + p->streamEndWasReached = 1; + return; + } + + for (;;) + { + Byte* dest = p->buffer + (p->streamPos - p->pos); + size_t size = (p->bufferBase + p->blockSize - dest); + if (size == 0) + return; + + p->result = ISeqInStream_Read(p->stream, dest, &size); + if (p->result != SZ_OK) + return; + if (size == 0) + { + p->streamEndWasReached = 1; + return; + } + p->streamPos += (UInt32)size; + if (p->streamPos - p->pos > p->keepSizeAfter) + return; + } + } + + void MatchFinder_MoveBlock(CMatchFinder* p) + { + memmove(p->bufferBase, + p->buffer - p->keepSizeBefore, + (size_t)(p->streamPos - p->pos) + p->keepSizeBefore); + p->buffer = p->bufferBase + p->keepSizeBefore; + } + + int MatchFinder_NeedMove(CMatchFinder* p) + { + if (p->directInput) + return 0; + /* if (p->streamEndWasReached) return 0; */ + return ((size_t)(p->bufferBase + p->blockSize - p->buffer) <= p->keepSizeAfter); + } + + void MatchFinder_ReadIfRequired(CMatchFinder* p) + { + if (p->streamEndWasReached) + return; + if (p->keepSizeAfter >= p->streamPos - p->pos) + MatchFinder_ReadBlock(p); + } + + static void MatchFinder_CheckAndMoveAndRead(CMatchFinder* p) + { + if (MatchFinder_NeedMove(p)) + MatchFinder_MoveBlock(p); + MatchFinder_ReadBlock(p); + } + + static void MatchFinder_SetDefaultSettings(CMatchFinder* p) + { + p->cutValue = 32; + p->btMode = 1; + p->numHashBytes = 4; + p->bigHash = 0; + } + +#define kCrcPoly 0xEDB88320 + + void MatchFinder_Construct(CMatchFinder* p) + { + unsigned i; + p->bufferBase = NULL; + p->directInput = 0; + p->hash = NULL; + p->expectedDataSize = (UInt64)(Int64)-1; + MatchFinder_SetDefaultSettings(p); + + for (i = 0; i < 256; i++) + { + UInt32 r = (UInt32)i; + unsigned j; + for (j = 0; j < 8; j++) + r = (r >> 1) ^ (kCrcPoly & ((UInt32)0 - (r & 1))); + p->crc[i] = r; + } + } + + static void MatchFinder_FreeThisClassMemory(CMatchFinder* p, ISzAllocPtr alloc) + { + ISzAlloc_Free(alloc, p->hash); + p->hash = NULL; + } + + void MatchFinder_Free(CMatchFinder* p, ISzAllocPtr alloc) + { + MatchFinder_FreeThisClassMemory(p, alloc); + LzInWindow_Free(p, alloc); + } + + static CLzRef* AllocRefs(size_t num, ISzAllocPtr alloc) + { + size_t sizeInBytes = (size_t)num * sizeof(CLzRef); + if (sizeInBytes / sizeof(CLzRef) != num) + return NULL; + return (CLzRef*)ISzAlloc_Alloc(alloc, sizeInBytes); + } + + int MatchFinder_Create(CMatchFinder* p, UInt32 historySize, + UInt32 keepAddBufferBefore, UInt32 matchMaxLen, UInt32 keepAddBufferAfter, + ISzAllocPtr alloc) + { + UInt32 sizeReserv; + + if (historySize > kMaxHistorySize) + { + MatchFinder_Free(p, alloc); + return 0; + } + + sizeReserv = historySize >> 1; + if (historySize >= ((UInt32)3 << 30)) sizeReserv = historySize >> 3; + else if (historySize >= ((UInt32)2 << 30)) sizeReserv = historySize >> 2; + + sizeReserv += (keepAddBufferBefore + matchMaxLen + keepAddBufferAfter) / 2 + (1 << 19); + + p->keepSizeBefore = historySize + keepAddBufferBefore + 1; + p->keepSizeAfter = matchMaxLen + keepAddBufferAfter; + + /* we need one additional byte, since we use MoveBlock after pos++ and before dictionary using */ + + if (LzInWindow_Create(p, sizeReserv, alloc)) + { + UInt32 newCyclicBufferSize = historySize + 1; + UInt32 hs; + p->matchMaxLen = matchMaxLen; + { + p->fixedHashSize = 0; + if (p->numHashBytes == 2) + hs = (1 << 16) - 1; + else + { + hs = historySize; + if (hs > p->expectedDataSize) + hs = (UInt32)p->expectedDataSize; + if (hs != 0) + hs--; + hs |= (hs >> 1); + hs |= (hs >> 2); + hs |= (hs >> 4); + hs |= (hs >> 8); + hs >>= 1; + hs |= 0xFFFF; /* don't change it! It's required for Deflate */ + if (hs > (1 << 24)) + { + if (p->numHashBytes == 3) + hs = (1 << 24) - 1; + else + hs >>= 1; + /* if (bigHash) mode, GetHeads4b() in LzFindMt.c needs (hs >= ((1 << 24) - 1))) */ + } + } + p->hashMask = hs; + hs++; + if (p->numHashBytes > 2) p->fixedHashSize += kHash2Size; + if (p->numHashBytes > 3) p->fixedHashSize += kHash3Size; + if (p->numHashBytes > 4) p->fixedHashSize += kHash4Size; + hs += p->fixedHashSize; + } + + { + size_t newSize; + size_t numSons; + p->historySize = historySize; + p->hashSizeSum = hs; + p->cyclicBufferSize = newCyclicBufferSize; + + numSons = newCyclicBufferSize; + if (p->btMode) + numSons <<= 1; + newSize = hs + numSons; + + if (p->hash && p->numRefs == newSize) + return 1; + + MatchFinder_FreeThisClassMemory(p, alloc); + p->numRefs = newSize; + p->hash = AllocRefs(newSize, alloc); + + if (p->hash) + { + p->son = p->hash + p->hashSizeSum; + return 1; + } + } + } + + MatchFinder_Free(p, alloc); + return 0; + } + + static void MatchFinder_SetLimits(CMatchFinder* p) + { + UInt32 limit = kMaxValForNormalize - p->pos; + UInt32 limit2 = p->cyclicBufferSize - p->cyclicBufferPos; + + if (limit2 < limit) + limit = limit2; + limit2 = p->streamPos - p->pos; + + if (limit2 <= p->keepSizeAfter) + { + if (limit2 > 0) + limit2 = 1; + } + else + limit2 -= p->keepSizeAfter; + + if (limit2 < limit) + limit = limit2; + + { + UInt32 lenLimit = p->streamPos - p->pos; + if (lenLimit > p->matchMaxLen) + lenLimit = p->matchMaxLen; + p->lenLimit = lenLimit; + } + p->posLimit = p->pos + limit; + } + + void MatchFinder_Init_LowHash(CMatchFinder* p) + { + size_t i; + CLzRef* items = p->hash; + size_t numItems = p->fixedHashSize; + for (i = 0; i < numItems; i++) + items[i] = kEmptyHashValue; + } + + void MatchFinder_Init_HighHash(CMatchFinder* p) + { + size_t i; + CLzRef* items = p->hash + p->fixedHashSize; + size_t numItems = (size_t)p->hashMask + 1; + for (i = 0; i < numItems; i++) + items[i] = kEmptyHashValue; + } + + void MatchFinder_Init_3(CMatchFinder* p, int readData) + { + p->cyclicBufferPos = 0; + p->buffer = p->bufferBase; + p->pos = + p->streamPos = p->cyclicBufferSize; + p->result = SZ_OK; + p->streamEndWasReached = 0; + + if (readData) + MatchFinder_ReadBlock(p); + + MatchFinder_SetLimits(p); + } + + void MatchFinder_Init(CMatchFinder* p) + { + MatchFinder_Init_HighHash(p); + MatchFinder_Init_LowHash(p); + MatchFinder_Init_3(p, True); + } + + static UInt32 MatchFinder_GetSubValue(CMatchFinder* p) + { + return (p->pos - p->historySize - 1) & kNormalizeMask; + } + + void MatchFinder_Normalize3(UInt32 subValue, CLzRef* items, size_t numItems) + { + size_t i; + for (i = 0; i < numItems; i++) + { + UInt32 value = items[i]; + if (value <= subValue) + value = kEmptyHashValue; + else + value -= subValue; + items[i] = value; + } + } + + static void MatchFinder_Normalize(CMatchFinder* p) + { + UInt32 subValue = MatchFinder_GetSubValue(p); + MatchFinder_Normalize3(subValue, p->hash, p->numRefs); + MatchFinder_ReduceOffsets(p, subValue); + } + + MY_NO_INLINE + static void MatchFinder_CheckLimits(CMatchFinder* p) + { + if (p->pos == kMaxValForNormalize) + MatchFinder_Normalize(p); + if (!p->streamEndWasReached && p->keepSizeAfter == p->streamPos - p->pos) + MatchFinder_CheckAndMoveAndRead(p); + if (p->cyclicBufferPos == p->cyclicBufferSize) + p->cyclicBufferPos = 0; + MatchFinder_SetLimits(p); + } + + /* + (lenLimit > maxLen) + */ + MY_FORCE_INLINE + static UInt32* Hc_GetMatchesSpec(unsigned lenLimit, UInt32 curMatch, UInt32 pos, const Byte* cur, CLzRef* son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue, + UInt32* distances, unsigned maxLen) + { + /* + son[_cyclicBufferPos] = curMatch; + for (;;) + { + UInt32 delta = pos - curMatch; + if (cutValue-- == 0 || delta >= _cyclicBufferSize) + return distances; + { + const Byte *pb = cur - delta; + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)]; + if (pb[maxLen] == cur[maxLen] && *pb == *cur) + { + UInt32 len = 0; + while (++len != lenLimit) + if (pb[len] != cur[len]) + break; + if (maxLen < len) + { + maxLen = len; + *distances++ = len; + *distances++ = delta - 1; + if (len == lenLimit) + return distances; + } + } + } + } + */ + + const Byte* lim = cur + lenLimit; + son[_cyclicBufferPos] = curMatch; + do + { + UInt32 delta = pos - curMatch; + if (delta >= _cyclicBufferSize) + break; + { + ptrdiff_t diff; + curMatch = son[_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)]; + diff = (ptrdiff_t)0 - delta; + if (cur[maxLen] == cur[maxLen + diff]) + { + const Byte* c = cur; + while (*c == c[diff]) + { + if (++c == lim) + { + distances[0] = (UInt32)(lim - cur); + distances[1] = delta - 1; + return distances + 2; + } + } + { + unsigned len = (unsigned)(c - cur); + if (maxLen < len) + { + maxLen = len; + distances[0] = (UInt32)len; + distances[1] = delta - 1; + distances += 2; + } + } + } + } + } while (--cutValue); + + return distances; + } + + MY_FORCE_INLINE + UInt32* GetMatchesSpec1(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte* cur, CLzRef* son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue, + UInt32* distances, UInt32 maxLen) + { + CLzRef* ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1; + CLzRef* ptr1 = son + ((size_t)_cyclicBufferPos << 1); + unsigned len0 = 0, len1 = 0; + for (;;) + { + UInt32 delta = pos - curMatch; + if (cutValue-- == 0 || delta >= _cyclicBufferSize) + { + *ptr0 = *ptr1 = kEmptyHashValue; + return distances; + } + { + CLzRef* pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1); + const Byte* pb = cur - delta; + unsigned len = (len0 < len1 ? len0 : len1); + UInt32 pair0 = pair[0]; + if (pb[len] == cur[len]) + { + if (++len != lenLimit && pb[len] == cur[len]) + while (++len != lenLimit) + if (pb[len] != cur[len]) + break; + if (maxLen < len) + { + maxLen = (UInt32)len; + *distances++ = (UInt32)len; + *distances++ = delta - 1; + if (len == lenLimit) + { + *ptr1 = pair0; + *ptr0 = pair[1]; + return distances; + } + } + } + if (pb[len] < cur[len]) + { + *ptr1 = curMatch; + ptr1 = pair + 1; + curMatch = *ptr1; + len1 = len; + } + else + { + *ptr0 = curMatch; + ptr0 = pair; + curMatch = *ptr0; + len0 = len; + } + } + } + } + + static void SkipMatchesSpec(UInt32 lenLimit, UInt32 curMatch, UInt32 pos, const Byte* cur, CLzRef* son, + UInt32 _cyclicBufferPos, UInt32 _cyclicBufferSize, UInt32 cutValue) + { + CLzRef* ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1; + CLzRef* ptr1 = son + ((size_t)_cyclicBufferPos << 1); + unsigned len0 = 0, len1 = 0; + for (;;) + { + UInt32 delta = pos - curMatch; + if (cutValue-- == 0 || delta >= _cyclicBufferSize) + { + *ptr0 = *ptr1 = kEmptyHashValue; + return; + } + { + CLzRef* pair = son + ((size_t)(_cyclicBufferPos - delta + ((delta > _cyclicBufferPos) ? _cyclicBufferSize : 0)) << 1); + const Byte* pb = cur - delta; + unsigned len = (len0 < len1 ? len0 : len1); + if (pb[len] == cur[len]) + { + while (++len != lenLimit) + if (pb[len] != cur[len]) + break; + { + if (len == lenLimit) + { + *ptr1 = pair[0]; + *ptr0 = pair[1]; + return; + } + } + } + if (pb[len] < cur[len]) + { + *ptr1 = curMatch; + ptr1 = pair + 1; + curMatch = *ptr1; + len1 = len; + } + else + { + *ptr0 = curMatch; + ptr0 = pair; + curMatch = *ptr0; + len0 = len; + } + } + } + } + +#define MOVE_POS \ + ++p->cyclicBufferPos; \ + p->buffer++; \ + if (++p->pos == p->posLimit) MatchFinder_CheckLimits(p); + +#define MOVE_POS_RET MOVE_POS return (UInt32)offset; + + static void MatchFinder_MovePos(CMatchFinder* p) { MOVE_POS; } + +#define GET_MATCHES_HEADER2(minLen, ret_op) \ + unsigned lenLimit; UInt32 hv; const Byte *cur; UInt32 curMatch; \ + lenLimit = (unsigned)p->lenLimit; { if (lenLimit < minLen) { MatchFinder_MovePos(p); ret_op; }} \ + cur = p->buffer; + +#define GET_MATCHES_HEADER(minLen) GET_MATCHES_HEADER2(minLen, return 0) +#define SKIP_HEADER(minLen) GET_MATCHES_HEADER2(minLen, continue) + +#define MF_PARAMS(p) p->pos, p->buffer, p->son, p->cyclicBufferPos, p->cyclicBufferSize, p->cutValue + +#define GET_MATCHES_FOOTER(offset, maxLen) \ + offset = (unsigned)(GetMatchesSpec1((UInt32)lenLimit, curMatch, MF_PARAMS(p), \ + distances + offset, (UInt32)maxLen) - distances); MOVE_POS_RET; + +#define SKIP_FOOTER \ + SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p)); MOVE_POS; + +#define UPDATE_maxLen { \ + ptrdiff_t diff = (ptrdiff_t)0 - d2; \ + const Byte *c = cur + maxLen; \ + const Byte *lim = cur + lenLimit; \ + for (; c != lim; c++) if (*(c + diff) != *c) break; \ + maxLen = (unsigned)(c - cur); } + + static UInt32 Bt2_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances) + { + unsigned offset; + GET_MATCHES_HEADER(2) + HASH2_CALC; + curMatch = p->hash[hv]; + p->hash[hv] = p->pos; + offset = 0; + GET_MATCHES_FOOTER(offset, 1) + } + + UInt32 Bt3Zip_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances) + { + unsigned offset; + GET_MATCHES_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hv]; + p->hash[hv] = p->pos; + offset = 0; + GET_MATCHES_FOOTER(offset, 2) + } + + static UInt32 Bt3_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances) + { + UInt32 h2, d2, pos; + unsigned maxLen, offset; + UInt32* hash; + GET_MATCHES_HEADER(3) + + HASH3_CALC; + + hash = p->hash; + pos = p->pos; + + d2 = pos - hash[h2]; + + curMatch = (hash + kFix3HashSize)[hv]; + + hash[h2] = pos; + (hash + kFix3HashSize)[hv] = pos; + + maxLen = 2; + offset = 0; + + if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur) + { + UPDATE_maxLen + distances[0] = (UInt32)maxLen; + distances[1] = d2 - 1; + offset = 2; + if (maxLen == lenLimit) + { + SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p)); + MOVE_POS_RET; + } + } + + GET_MATCHES_FOOTER(offset, maxLen) + } + + static UInt32 Bt4_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances) + { + UInt32 h2, h3, d2, d3, pos; + unsigned maxLen, offset; + UInt32* hash; + GET_MATCHES_HEADER(4) + + HASH4_CALC; + + hash = p->hash; + pos = p->pos; + + d2 = pos - hash[h2]; + d3 = pos - (hash + kFix3HashSize)[h3]; + + curMatch = (hash + kFix4HashSize)[hv]; + + hash[h2] = pos; + (hash + kFix3HashSize)[h3] = pos; + (hash + kFix4HashSize)[hv] = pos; + + maxLen = 0; + offset = 0; + + if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur) + { + maxLen = 2; + distances[0] = 2; + distances[1] = d2 - 1; + offset = 2; + } + + if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur) + { + maxLen = 3; + distances[(size_t)offset + 1] = d3 - 1; + offset += 2; + d2 = d3; + } + + if (offset != 0) + { + UPDATE_maxLen + distances[(size_t)offset - 2] = (UInt32)maxLen; + if (maxLen == lenLimit) + { + SkipMatchesSpec((UInt32)lenLimit, curMatch, MF_PARAMS(p)); + MOVE_POS_RET; + } + } + + if (maxLen < 3) + maxLen = 3; + + GET_MATCHES_FOOTER(offset, maxLen) + } + + /* + static UInt32 Bt5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) + { + UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos; + UInt32 *hash; + GET_MATCHES_HEADER(5) + + HASH5_CALC; + + hash = p->hash; + pos = p->pos; + + d2 = pos - hash [h2]; + d3 = pos - (hash + kFix3HashSize)[h3]; + d4 = pos - (hash + kFix4HashSize)[h4]; + + curMatch = (hash + kFix5HashSize)[hv]; + + hash [h2] = pos; + (hash + kFix3HashSize)[h3] = pos; + (hash + kFix4HashSize)[h4] = pos; + (hash + kFix5HashSize)[hv] = pos; + + maxLen = 0; + offset = 0; + + if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur) + { + distances[0] = maxLen = 2; + distances[1] = d2 - 1; + offset = 2; + if (*(cur - d2 + 2) == cur[2]) + distances[0] = maxLen = 3; + else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur) + { + distances[2] = maxLen = 3; + distances[3] = d3 - 1; + offset = 4; + d2 = d3; + } + } + else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur) + { + distances[0] = maxLen = 3; + distances[1] = d3 - 1; + offset = 2; + d2 = d3; + } + + if (d2 != d4 && d4 < p->cyclicBufferSize + && *(cur - d4) == *cur + && *(cur - d4 + 3) == *(cur + 3)) + { + maxLen = 4; + distances[(size_t)offset + 1] = d4 - 1; + offset += 2; + d2 = d4; + } + + if (offset != 0) + { + UPDATE_maxLen + distances[(size_t)offset - 2] = maxLen; + if (maxLen == lenLimit) + { + SkipMatchesSpec(lenLimit, curMatch, MF_PARAMS(p)); + MOVE_POS_RET; + } + } + + if (maxLen < 4) + maxLen = 4; + + GET_MATCHES_FOOTER(offset, maxLen) + } + */ + + static UInt32 Hc4_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances) + { + UInt32 h2, h3, d2, d3, pos; + unsigned maxLen, offset; + UInt32* hash; + GET_MATCHES_HEADER(4) + + HASH4_CALC; + + hash = p->hash; + pos = p->pos; + + d2 = pos - hash[h2]; + d3 = pos - (hash + kFix3HashSize)[h3]; + curMatch = (hash + kFix4HashSize)[hv]; + + hash[h2] = pos; + (hash + kFix3HashSize)[h3] = pos; + (hash + kFix4HashSize)[hv] = pos; + + maxLen = 0; + offset = 0; + + if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur) + { + maxLen = 2; + distances[0] = 2; + distances[1] = d2 - 1; + offset = 2; + } + + if (d2 != d3 && d3 < p->cyclicBufferSize && *(cur - d3) == *cur) + { + maxLen = 3; + distances[(size_t)offset + 1] = d3 - 1; + offset += 2; + d2 = d3; + } + + if (offset != 0) + { + UPDATE_maxLen + distances[(size_t)offset - 2] = (UInt32)maxLen; + if (maxLen == lenLimit) + { + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS_RET; + } + } + + if (maxLen < 3) + maxLen = 3; + + offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p), + distances + offset, maxLen) - (distances)); + MOVE_POS_RET + } + + /* + static UInt32 Hc5_MatchFinder_GetMatches(CMatchFinder *p, UInt32 *distances) + { + UInt32 h2, h3, h4, d2, d3, d4, maxLen, offset, pos + UInt32 *hash; + GET_MATCHES_HEADER(5) + + HASH5_CALC; + + hash = p->hash; + pos = p->pos; + + d2 = pos - hash [h2]; + d3 = pos - (hash + kFix3HashSize)[h3]; + d4 = pos - (hash + kFix4HashSize)[h4]; + + curMatch = (hash + kFix5HashSize)[hv]; + + hash [h2] = pos; + (hash + kFix3HashSize)[h3] = pos; + (hash + kFix4HashSize)[h4] = pos; + (hash + kFix5HashSize)[hv] = pos; + + maxLen = 0; + offset = 0; + + if (d2 < p->cyclicBufferSize && *(cur - d2) == *cur) + { + distances[0] = maxLen = 2; + distances[1] = d2 - 1; + offset = 2; + if (*(cur - d2 + 2) == cur[2]) + distances[0] = maxLen = 3; + else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur) + { + distances[2] = maxLen = 3; + distances[3] = d3 - 1; + offset = 4; + d2 = d3; + } + } + else if (d3 < p->cyclicBufferSize && *(cur - d3) == *cur) + { + distances[0] = maxLen = 3; + distances[1] = d3 - 1; + offset = 2; + d2 = d3; + } + + if (d2 != d4 && d4 < p->cyclicBufferSize + && *(cur - d4) == *cur + && *(cur - d4 + 3) == *(cur + 3)) + { + maxLen = 4; + distances[(size_t)offset + 1] = d4 - 1; + offset += 2; + d2 = d4; + } + + if (offset != 0) + { + UPDATE_maxLen + distances[(size_t)offset - 2] = maxLen; + if (maxLen == lenLimit) + { + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS_RET; + } + } + + if (maxLen < 4) + maxLen = 4; + + offset = (UInt32)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p), + distances + offset, maxLen) - (distances)); + MOVE_POS_RET + } + */ + + UInt32 Hc3Zip_MatchFinder_GetMatches(CMatchFinder* p, UInt32* distances) + { + unsigned offset; + GET_MATCHES_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hv]; + p->hash[hv] = p->pos; + offset = (unsigned)(Hc_GetMatchesSpec(lenLimit, curMatch, MF_PARAMS(p), + distances, 2) - (distances)); + MOVE_POS_RET + } + + static void Bt2_MatchFinder_Skip(CMatchFinder* p, UInt32 num) + { + do + { + SKIP_HEADER(2) + HASH2_CALC; + curMatch = p->hash[hv]; + p->hash[hv] = p->pos; + SKIP_FOOTER + } while (--num != 0); + } + + void Bt3Zip_MatchFinder_Skip(CMatchFinder* p, UInt32 num) + { + do + { + SKIP_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hv]; + p->hash[hv] = p->pos; + SKIP_FOOTER + } while (--num != 0); + } + + static void Bt3_MatchFinder_Skip(CMatchFinder* p, UInt32 num) + { + do + { + UInt32 h2; + UInt32* hash; + SKIP_HEADER(3) + HASH3_CALC; + hash = p->hash; + curMatch = (hash + kFix3HashSize)[hv]; + hash[h2] = + (hash + kFix3HashSize)[hv] = p->pos; + SKIP_FOOTER + } while (--num != 0); + } + + static void Bt4_MatchFinder_Skip(CMatchFinder* p, UInt32 num) + { + do + { + UInt32 h2, h3; + UInt32* hash; + SKIP_HEADER(4) + HASH4_CALC; + hash = p->hash; + curMatch = (hash + kFix4HashSize)[hv]; + hash[h2] = + (hash + kFix3HashSize)[h3] = + (hash + kFix4HashSize)[hv] = p->pos; + SKIP_FOOTER + } while (--num != 0); + } + + /* + static void Bt5_MatchFinder_Skip(CMatchFinder *p, UInt32 num) + { + do + { + UInt32 h2, h3, h4; + UInt32 *hash; + SKIP_HEADER(5) + HASH5_CALC; + hash = p->hash; + curMatch = (hash + kFix5HashSize)[hv]; + hash [h2] = + (hash + kFix3HashSize)[h3] = + (hash + kFix4HashSize)[h4] = + (hash + kFix5HashSize)[hv] = p->pos; + SKIP_FOOTER + } + while (--num != 0); + } + */ + + static void Hc4_MatchFinder_Skip(CMatchFinder* p, UInt32 num) + { + do + { + UInt32 h2, h3; + UInt32* hash; + SKIP_HEADER(4) + HASH4_CALC; + hash = p->hash; + curMatch = (hash + kFix4HashSize)[hv]; + hash[h2] = + (hash + kFix3HashSize)[h3] = + (hash + kFix4HashSize)[hv] = p->pos; + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS + } while (--num != 0); + } + + /* + static void Hc5_MatchFinder_Skip(CMatchFinder *p, UInt32 num) + { + do + { + UInt32 h2, h3, h4; + UInt32 *hash; + SKIP_HEADER(5) + HASH5_CALC; + hash = p->hash; + curMatch = hash + kFix5HashSize)[hv]; + hash [h2] = + (hash + kFix3HashSize)[h3] = + (hash + kFix4HashSize)[h4] = + (hash + kFix5HashSize)[hv] = p->pos; + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS + } + while (--num != 0); + } + */ + + void Hc3Zip_MatchFinder_Skip(CMatchFinder* p, UInt32 num) + { + do + { + SKIP_HEADER(3) + HASH_ZIP_CALC; + curMatch = p->hash[hv]; + p->hash[hv] = p->pos; + p->son[p->cyclicBufferPos] = curMatch; + MOVE_POS + } while (--num != 0); + } + + void MatchFinder_CreateVTable(CMatchFinder* p, IMatchFinder* vTable) + { + vTable->Init = (Mf_Init_Func)MatchFinder_Init; + vTable->GetNumAvailableBytes = (Mf_GetNumAvailableBytes_Func)MatchFinder_GetNumAvailableBytes; + vTable->GetPointerToCurrentPos = (Mf_GetPointerToCurrentPos_Func)MatchFinder_GetPointerToCurrentPos; + if (!p->btMode) + { + /* if (p->numHashBytes <= 4) */ + { + vTable->GetMatches = (Mf_GetMatches_Func)Hc4_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Hc4_MatchFinder_Skip; + } + /* + else + { + vTable->GetMatches = (Mf_GetMatches_Func)Hc5_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Hc5_MatchFinder_Skip; + } + */ + } + else if (p->numHashBytes == 2) + { + vTable->GetMatches = (Mf_GetMatches_Func)Bt2_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Bt2_MatchFinder_Skip; + } + else if (p->numHashBytes == 3) + { + vTable->GetMatches = (Mf_GetMatches_Func)Bt3_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Bt3_MatchFinder_Skip; + } + else /* if (p->numHashBytes == 4) */ + { + vTable->GetMatches = (Mf_GetMatches_Func)Bt4_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Bt4_MatchFinder_Skip; + } + /* + else + { + vTable->GetMatches = (Mf_GetMatches_Func)Bt5_MatchFinder_GetMatches; + vTable->Skip = (Mf_Skip_Func)Bt5_MatchFinder_Skip; + } + */ + } + + /*** End of inlined file: LzFind.c ***/ + + + /*** Start of inlined file: LzmaLib.c ***/ + MY_STDAPI LzmaCompress(unsigned char* dest, size_t* destLen, const unsigned char* src, size_t srcLen, + unsigned char* outProps, size_t* outPropsSize, + int level, /* 0 <= level <= 9, default = 5 */ + unsigned dictSize, /* use (1 << N) or (3 << N). 4 KB < dictSize <= 128 MB */ + int lc, /* 0 <= lc <= 8, default = 3 */ + int lp, /* 0 <= lp <= 4, default = 0 */ + int pb, /* 0 <= pb <= 4, default = 2 */ + int fb, /* 5 <= fb <= 273, default = 32 */ + int numThreads /* 1 or 2, default = 2 */ + ) + { + CLzmaEncProps props; + LzmaEncProps_Init(&props); + props.level = level; + props.dictSize = dictSize; + props.lc = lc; + props.lp = lp; + props.pb = pb; + props.fb = fb; + props.numThreads = numThreads; + + return LzmaEncode(dest, destLen, src, srcLen, &props, outProps, outPropsSize, 0, + NULL, &g_Alloc, &g_Alloc); + } + + MY_STDAPI LzmaUncompress(unsigned char* dest, size_t* destLen, const unsigned char* src, size_t* srcLen, + const unsigned char* props, size_t propsSize) + { + ELzmaStatus status; + return LzmaDecode(dest, destLen, src, srcLen, props, (unsigned)propsSize, LZMA_FINISH_ANY, &status, &g_Alloc); + } + + /*** End of inlined file: LzmaLib.c ***/ + +#endif //POCKETLZMA_LZMA_C_DEFINE + } +} + +/*** Start of inlined file: pocketlzma_common.hpp ***/ +// +// Created by robin on 29.12.2020. +// + +#ifndef POCKETLZMA_POCKETLZMA_COMMON_HPP +#define POCKETLZMA_POCKETLZMA_COMMON_HPP +#include +#include +#include + +#include +#include +#include + +namespace plz +{ + const uint8_t PLZ_MAX_LEVEL{ 9 }; + const uint32_t PLZ_MIN_DICTIONARY_SIZE{ 1 << 8 }; + const uint32_t PLZ_MAX_DICTIONARY_SIZE{ 1 << 30 }; + const uint8_t PLZ_MAX_LITERAL_CONTEXT_BITS{ 8 }; + const uint8_t PLZ_MAX_LITERAL_POSITION_BITS{ 4 }; + const uint8_t PLZ_MAX_POSITION_BITS{ 4 }; + const uint16_t PLZ_MIN_FAST_BYTES{ 5 }; + const uint16_t PLZ_MAX_FAST_BYTES{ 273 }; + + const uint32_t PLZ_BUFFER_SIZE{ 1 << 16 }; //65536 bytes + const uint8_t PLZ_MINIMUM_LZMA_SIZE{ 12 }; + + enum class StatusCode + { + + Ok = SZ_OK, //0 + ErrorData = SZ_ERROR_DATA, //1 + ErrorMem = SZ_ERROR_MEM, //2 + ErrorCrc = SZ_ERROR_CRC, //3 + ErrorUnsupported = SZ_ERROR_UNSUPPORTED, //4 + ErrorParam = SZ_ERROR_PARAM, //5 + ErrorInputEof = SZ_ERROR_INPUT_EOF, //6 + ErrorOutputEof = SZ_ERROR_OUTPUT_EOF, //7 + ErrorRead = SZ_ERROR_READ, //8 + ErrorWrite = SZ_ERROR_WRITE, //9 + ErrorProgress = SZ_ERROR_PROGRESS, //10 + ErrorFail = SZ_ERROR_FAIL, //11 + ErrorThread = SZ_ERROR_THREAD, //12 + ErrorArchive = SZ_ERROR_ARCHIVE, //16 + ErrorNoArchive = SZ_ERROR_NO_ARCHIVE, //17 + + /*! When you attempt to decompress something that cannot be LZMA data */ + InvalidLzmaData = 100, + + /*! If you get this, you probably have attempted to decompress corrupted/garbage LZMA data */ + UndefinedError = 999 + }; + + enum class Preset : uint8_t + { + Default = 0, + Fastest = 1, + Fast = 2, + GoodCompression = 3, + BestCompression = 4 + }; +} +#endif //POCKETLZMA_POCKETLZMA_COMMON_HPP + +/*** End of inlined file: pocketlzma_common.hpp ***/ + + + +/*** Start of inlined file: Settings.hpp ***/ +// +// Created by robin on 29.12.2020. +// + +#ifndef POCKETLZMA_SETTINGS_HPP +#define POCKETLZMA_SETTINGS_HPP + +namespace plz +{ + class Settings + { + public: + + Settings() = default; + inline explicit Settings(Preset preset); + /*! + * Makes sure no values are out of valid range + */ + inline void validate(); + + inline void usePreset(Preset preset); + + /*! + * level - compression level: 0 <= level <= 9; + + * level dictSize algo fb + * 0: 16 KB 0 32 + * 1: 64 KB 0 32 + * 2: 256 KB 0 32 + * 3: 1 MB 0 32 + * 4: 4 MB 0 32 + * 5: 16 MB 1 32 + * 6: 32 MB 1 32 + * 7+: 64 MB 1 64 + * + * The default value for "level" is 5. + * + * algo = 0 means fast method + * algo = 1 means normal method + */ + uint8_t level{ 5 }; + + /*! + * The dictionary size in bytes. The maximum value is + * 128 MB = (1 << 27) bytes for 32-bit version + * 1 GB = (1 << 30) bytes for 64-bit version + * The default value is 16 MB = (1 << 24) bytes. + * It's recommended to use the dictionary that is larger than 4 KB and + * that can be calculated as (1 << N) or (3 << N) sizes. + * + * pocketlzma has a lower limit of (1 << 8) (256 bytes) + */ + uint32_t dictionarySize{ 1 << 24 }; + + /*! + * lc - The number of literal context bits (high bits of previous literal). + * It can be in the range from 0 to 8. The default value is 3. + * Sometimes lc=4 gives the gain for big files. + */ + uint8_t literalContextBits{ 3 }; + + /*! + * lp - The number of literal pos bits (low bits of current position for literals). + * It can be in the range from 0 to 4. The default value is 0. + * The lp switch is intended for periodical data when the period is equal to 2^lp. + * For example, for 32-bit (4 bytes) periodical data you can use lp=2. Often it's + * better to set lc=0, if you change lp switch. + */ + uint8_t literalPositionBits{ 0 }; + + /*! + * pb - The number of pos bits (low bits of current position). + It can be in the range from 0 to 4. The default value is 2. + The pb switch is intended for periodical data when the period is equal 2^pb. + */ + uint8_t positionBits{ 2 }; + + /*! + * fb - Word size (the number of fast bytes). + * It can be in the range from 5 to 273. The default value is 32. + * Usually, a big number gives a little bit better compression ratio and + * slower compression process. + */ + uint16_t fastBytes{ 32 }; + }; + + Settings::Settings(Preset preset) + { + usePreset(preset); + } + + void Settings::validate() + { + if (level > PLZ_MAX_LEVEL) + level = PLZ_MAX_LEVEL; + + if (dictionarySize < PLZ_MIN_DICTIONARY_SIZE) + dictionarySize = PLZ_MIN_DICTIONARY_SIZE; + else if (dictionarySize > PLZ_MAX_DICTIONARY_SIZE) + dictionarySize = PLZ_MAX_DICTIONARY_SIZE; + + if (literalContextBits > PLZ_MAX_LITERAL_CONTEXT_BITS) + literalContextBits = PLZ_MAX_LITERAL_CONTEXT_BITS; + + if (literalPositionBits > PLZ_MAX_LITERAL_POSITION_BITS) + literalPositionBits = PLZ_MAX_LITERAL_POSITION_BITS; + + if (positionBits > PLZ_MAX_POSITION_BITS) + positionBits = PLZ_MAX_POSITION_BITS; + + if (fastBytes < PLZ_MIN_FAST_BYTES) + fastBytes = PLZ_MIN_FAST_BYTES; + else if (fastBytes > PLZ_MAX_FAST_BYTES) + fastBytes = PLZ_MAX_FAST_BYTES; + + } + + void Settings::usePreset(Preset preset) + { + switch (preset) + { + case Preset::Default: + level = 5; + dictionarySize = 1 << 24; + literalContextBits = 3; + literalPositionBits = 0; + positionBits = 2; + fastBytes = 32; + break; + + case Preset::Fastest: + level = 1; + dictionarySize = 1 << 16; + literalContextBits = 4; + literalPositionBits = 0; + positionBits = 2; + fastBytes = 8; + break; + + case Preset::Fast: + level = 4; + dictionarySize = 1 << 22; + literalContextBits = 4; + literalPositionBits = 0; + positionBits = 2; + fastBytes = 16; + break; + + case Preset::GoodCompression: + level = 7; + dictionarySize = 1 << 26; + literalContextBits = 3; + literalPositionBits = 0; + positionBits = 2; + fastBytes = 64; + break; + + case Preset::BestCompression: + level = 9; + dictionarySize = 1 << 27; + literalContextBits = 3; + literalPositionBits = 0; + positionBits = 2; + fastBytes = 128; + break; + + } + } + +} + +#endif //POCKETLZMA_SETTINGS_HPP + +/*** End of inlined file: Settings.hpp ***/ + + +/*** Start of inlined file: FileStatus.hpp ***/ +// +// Created by robin on 29.12.2020. +// + +#ifndef POCKETLZMA_FILESTATUS_HPP +#define POCKETLZMA_FILESTATUS_HPP + +namespace plz +{ + class FileStatus + { + public: + + enum class Code + { + Ok = 0, + FileWriteError = 100, + FileWriteErrorBadBit = 101, + FileWriteErrorFailBit = 102, + + FileReadError = 200, + FileReadErrorBadBit = 201, + FileReadErrorFailBit = 202 + }; + + inline FileStatus() = default; + inline FileStatus(FileStatus::Code status, int code, const std::string& exception, const std::string& category, const std::string& message); + + inline void set(FileStatus::Code status, int code, const std::string& exception, const std::string& category, const std::string& message); + + inline Code status() const; + inline int code() const; + inline const std::string& exception() const; + inline const std::string& category() const; + inline const std::string& message() const; + + private: + Code m_status{ Code::Ok }; + int m_code{ 0 }; + std::string m_exception; + std::string m_category; + std::string m_message; + + }; + + FileStatus::FileStatus(FileStatus::Code status, int code, const std::string& exception, const std::string& category, const std::string& message) + { + set(status, code, exception, category, message); + } + + void FileStatus::set(FileStatus::Code status, int code, const std::string& exception, const std::string& category, const std::string& message) + { + m_status = status; + m_code = code; + m_exception = exception; + m_category = category; + m_message = message; + } + + FileStatus::Code FileStatus::status() const + { + return m_status; + } + + int FileStatus::code() const + { + return m_code; + } + + const std::string& FileStatus::exception() const + { + return m_exception; + } + + const std::string& FileStatus::category() const + { + return m_category; + } + + const std::string& FileStatus::message() const + { + return m_message; + } +} + +#endif //POCKETLZMA_FILESTATUS_HPP + +/*** End of inlined file: FileStatus.hpp ***/ + + +/*** Start of inlined file: File.hpp ***/ +// +// Created by robin on 28.12.2020. +// + +#ifndef POCKETLZMA_FILE_HPP +#define POCKETLZMA_FILE_HPP + + +/*** Start of inlined file: MemoryStream.hpp ***/ +// +// Created by robin on 28.12.2020. +// + +#ifndef POCKETLZMA_MEMORYSTREAM_HPP +#define POCKETLZMA_MEMORYSTREAM_HPP + + +/*** Start of inlined file: MemoryBuffer.hpp ***/ +// +// Created by robin on 28.12.2020. +// + +#ifndef POCKETLZMA_MEMORYBUFFER_HPP +#define POCKETLZMA_MEMORYBUFFER_HPP + +#include + +namespace plz +{ + class MemoryBuffer : public std::basic_streambuf { + public: + MemoryBuffer(const uint8_t* p, size_t l) { + setg((char*)p, (char*)p, (char*)p + l); + } + }; +} + +#endif //POCKETLZMA_MEMORYBUFFER_HPP + +/*** End of inlined file: MemoryBuffer.hpp ***/ + +namespace plz +{ + class MemoryStream : public std::istream { + public: + MemoryStream(const uint8_t* p, size_t l) : + std::istream(&m_buffer), + m_buffer(p, l) + { + m_size = l; + rdbuf(&m_buffer); + } + + size_t size() const { return m_size; } + + private: + MemoryBuffer m_buffer; + size_t m_size; + }; +} + +#endif //POCKETLZMA_MEMORYSTREAM_HPP + +/*** End of inlined file: MemoryStream.hpp ***/ + +#include +namespace plz +{ + class File + { + public: + File() = delete; + static inline std::vector FromMemory(const void* data, size_t size); + static inline void FromMemory(const void* data, size_t size, std::vector& output); + + static inline std::vector FromFile(const std::string& path); + static inline FileStatus FromFile(const std::string& path, std::vector& output); + + static inline FileStatus ToFile(const std::string& path, const std::vector& data); + }; + + std::vector File::FromMemory(const void* data, size_t size) + { + std::vector bytes(size); + FromMemory(data, size, bytes); + return bytes; + } + + void File::FromMemory(const void* data, size_t size, std::vector& output) + { + output.resize(size); + plz::MemoryStream mem{ (uint8_t*)data, size }; + mem.read((char*)&output[0], size); + } + + std::vector File::FromFile(const std::string& path) + { + std::fstream file; + file = std::fstream(path, std::ios::in | std::ios::binary); + + //Find size + file.seekg(0, std::ios::end); + size_t fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + std::vector bytes(fileSize); + + file.read((char*)&bytes[0], fileSize); + file.close(); + + return bytes; + } + + FileStatus File::FromFile(const std::string& path, std::vector& output) + { + std::fstream file; + try + { + file = std::fstream(path, std::ios::in | std::ios::binary); + file.exceptions(std::fstream::failbit | std::fstream::badbit); + bool isBad = file.bad(); + bool isFail = file.fail(); + bool isOpen = file.is_open(); + + if (isBad || isFail || !isOpen) + file.close(); + + if (isBad) + return FileStatus(FileStatus::Code::FileWriteErrorBadBit, 0, "", "", ""); + else if (isFail) + return FileStatus(FileStatus::Code::FileWriteErrorFailBit, 0, "", "", ""); + else if (!isOpen) + return FileStatus(FileStatus::Code::FileWriteError, 0, "", "", ""); + + //Find size + file.seekg(0, std::ios::end); + size_t fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + output.resize(fileSize); + + file.read((char*)&output[0], fileSize); + file.close(); + + return FileStatus(); + } + catch (const std::fstream::failure& e) + { + if (file.is_open()) + file.close(); + + return FileStatus(FileStatus::Code::FileReadError, e.code().value(), e.what(), e.code().category().name(), e.code().message()); + } + } + + FileStatus File::ToFile(const std::string& path, const std::vector& data) + { + std::fstream file; + file.exceptions(std::fstream::failbit | std::fstream::badbit); + + try + { + file = std::fstream(path, std::ios::out | std::ios::binary); + bool isBad = file.bad(); + bool isFail = file.fail(); + bool isOpen = file.is_open(); + + if (isBad || isFail || !isOpen) + file.close(); + + if (isBad) + return FileStatus(FileStatus::Code::FileWriteErrorBadBit, 0, "", "", ""); + else if (isFail) + return FileStatus(FileStatus::Code::FileWriteErrorFailBit, 0, "", "", ""); + else if (!isOpen) + return FileStatus(FileStatus::Code::FileWriteError, 0, "", "", ""); + + for (const auto& b : data) //b = byte + file << b; + + file.close(); + return FileStatus(); + } + catch (const std::fstream::failure& e) + { + if (file.is_open()) + file.close(); + + return FileStatus(FileStatus::Code::FileWriteError, e.code().value(), e.what(), e.code().category().name(), e.code().message()); + } + } +} + +#endif //POCKETLZMA_FILE_HPP + +/*** End of inlined file: File.hpp ***/ + + +/*** Start of inlined file: pocketlzma_class.hpp ***/ +// +// Created by robin on 29.12.2020. +// + +#ifndef POCKETLZMA_POCKETLZMA_CLASS_HPP +#define POCKETLZMA_POCKETLZMA_CLASS_HPP + +namespace plz +{ + class PocketLzma + { + public: + PocketLzma() = default; + inline explicit PocketLzma(Preset preset); + inline explicit PocketLzma(const Settings& settings) : m_settings{ settings } {}; + inline void setSettings(const Settings& settings); + inline void usePreset(Preset preset); + + inline StatusCode compress(const std::vector& input, std::vector& output); + inline StatusCode compress(const uint8_t* input, const size_t inputSize, std::vector& output); + + inline StatusCode decompress(const std::vector& input, std::vector& output); + inline StatusCode decompress(const uint8_t* input, const size_t inputSize, std::vector& output); + + inline StatusCode decompressBuffered(const std::vector& input, std::vector& output, uint32_t bufferSize = PLZ_BUFFER_SIZE); + inline StatusCode decompressBuffered(const uint8_t* input, const size_t inputSize, std::vector& output, uint32_t bufferSize = PLZ_BUFFER_SIZE); + private: + Settings m_settings{}; + }; + + PocketLzma::PocketLzma(Preset preset) + { + usePreset(preset); + } + + /*! + * This is optional. + * PocketLzma uses default values if not set by the user. + * + * If you are a casual user: usePreset() is recommended. + * + * @param settings new settings + */ + void PocketLzma::setSettings(const Settings& settings) + { + m_settings = settings; + } + + /*! + * Set a preset to control compression speed vs ratio. + * + * Recommended: + * For fast compression: Preset::Fast + * For balanced compression: Preset::Default + * For good compression: Preset::GoodCompression + * + * Note: Not used when decompressing. + */ + void PocketLzma::usePreset(Preset preset) + { + m_settings.usePreset(preset); + } + + StatusCode PocketLzma::compress(const std::vector & input, std::vector & output) + { + return compress(&input[0], input.size(), output); + } + + StatusCode PocketLzma::compress(const uint8_t* input, const size_t inputSize, std::vector& output) + { + m_settings.validate(); + size_t propsSize = LZMA_PROPS_SIZE; + uint8_t propsEncoded[LZMA_PROPS_SIZE]; + + size_t outSize = inputSize + (inputSize / 3) + 128; + //uint8_t out[outSize]; + std::unique_ptr out(new uint8_t[outSize]); + + int rc = plz::c::LzmaCompress(&out[0], &outSize, input, inputSize, propsEncoded, &propsSize, m_settings.level, m_settings.dictionarySize, + m_settings.literalContextBits, m_settings.literalPositionBits, m_settings.positionBits, m_settings.fastBytes, 1); + + StatusCode status = static_cast(rc); + if (status == StatusCode::Ok) + { + std::vector sizeBits; + for (int i = 0; i < 8; i++) + sizeBits.push_back((inputSize >> (i * 8)) & 0xFF); + + output.insert(output.end(), propsEncoded, propsEncoded + propsSize); // Property header + output.insert(output.end(), sizeBits.begin(), sizeBits.end()); // Add decompress size information + output.insert(output.end(), out.get(), out.get() + outSize); // Data + } + + return status; + } + + /*! + * Decompresses LZMA data. + * + * Will choose the best solution based on whether lzma file size in header is known or not. + * In cases where the file size is unknown, decompressBuffered() will be called + * + * @param input The input compressed data + * @param output The decompressed data + * @return Status for the decompression process + */ + StatusCode PocketLzma::decompress(const std::vector& input, std::vector& output) + { + return decompress(&input[0], input.size(), output); + } + + /*! + * Decompresses LZMA data. + * + * Will choose the best solution based on whether lzma file size in header is known or not. + * In cases where the file size is unknown, decompressBuffered() will be called + * + * @param input The input compressed data + * @param output The decompressed data + * @return Status for the decompression process + */ + StatusCode PocketLzma::decompress(const uint8_t* input, const size_t inputSize, std::vector& output) + { + if (inputSize <= PLZ_MINIMUM_LZMA_SIZE) + return StatusCode::InvalidLzmaData; + + size_t propsSize = LZMA_PROPS_SIZE + 8; //header + decompress_size + size_t size = 0; + bool sizeInfoMissing = true; //True until proven otherwise + + for (int i = 0; i < 8; i++) + { + uint8_t value = input[LZMA_PROPS_SIZE + i]; + if (value != 0xFF) + sizeInfoMissing = false; + + size |= (value << (i * 8)); + } + + if (sizeInfoMissing) + return decompressBuffered(input, inputSize, output, PLZ_BUFFER_SIZE); //StatusCode::MissingSizeInfoInHeader; + + size_t outSize = size; + //uint8_t out[size]; + std::unique_ptr out(new uint8_t[size]); + + size_t inSize = inputSize - propsSize; + int rc = plz::c::LzmaUncompress(&out[0], &outSize, &input[propsSize], &inSize, &input[0], LZMA_PROPS_SIZE); + + StatusCode status = static_cast(rc); + + output.insert(output.end(), out.get(), out.get() + outSize); + + return status; + } + + /*! + * Prefer using decompress(). + * + * Only use this if you for some reason required the decompression to be buffered or somehow think you can benefit from reading the data buffered. + * decompressBuffered() will probably always be slower than the regular decompress() when the LZMA header contains a known file size. + * decompress() will automatically call decompressBuffered() when file size in LZMA header is unknown. + * + * When file size is unknown in the LZMA header, data is always read using the buffered system. + * This will be slightly slower than normal decompression, but does not require the file result to be known + * + * @param input The input compressed data + * @param output The decompressed data + * @param bufferSize The buffer size. Default buffer size for pocketlzma is 65536 bytes. + * @return Status for the decompression process + */ + StatusCode PocketLzma::decompressBuffered(const std::vector& input, std::vector& output, uint32_t bufferSize) + { + return decompressBuffered(&input[0], input.size(), output, bufferSize); + } + + /*! + * Prefer using decompress(). + * + * Only use this if you for some reason required the decompression to be buffered or somehow think you can benefit from reading the data buffered. + * decompressBuffered() will probably always be slower than the regular decompress() when the LZMA header contains a known file size. + * decompress() will automatically call decompressBuffered() when file size in LZMA header is unknown. + * + * When file size is unknown in the LZMA header, data is always read using the buffered system. + * This will be slightly slower than normal decompression, but does not require the file result to be known + * + * @param input The input data + * @param output The decompressed data + * @param bufferSize The buffer size. Default buffer size for pocketlzma is 65536 bytes. + * @return Status for the decompression process + */ + StatusCode PocketLzma::decompressBuffered(const uint8_t* input, const size_t inputSize, std::vector& output, uint32_t bufferSize) + { + if (inputSize <= PLZ_MINIMUM_LZMA_SIZE) + return StatusCode::InvalidLzmaData; + + //size_t unpackSize = 0; + + plz::c::CLzmaDec state; + size_t propsSize = LZMA_PROPS_SIZE + 8; //header + decompress_size + + /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ + unsigned char header[LZMA_PROPS_SIZE + 8]; //MSVC requires this fully constant... + + //Read header data + for (int i = 0; i < propsSize; ++i) + header[i] = input[i]; + + LzmaDec_Construct(&state); + int res = 0; + res = LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &plz::c::g_Alloc); + + //uint8_t outBuf[bufferSize]; + std::unique_ptr outBuf(new uint8_t[bufferSize]); + size_t inPos = 0, inSize = 0, outPos = 0; + inSize = inputSize - propsSize; + plz::c::LzmaDec_Init(&state); + for (;;) + { + { + plz::c::SizeT inProcessed = inSize - inPos; + plz::c::SizeT outProcessed = bufferSize - outPos; + plz::c::ELzmaFinishMode finishMode = plz::c::LZMA_FINISH_ANY; + plz::c::ELzmaStatus status; + + res = plz::c::LzmaDec_DecodeToBuf(&state, outBuf.get() + outPos, &outProcessed, + &input[propsSize] + inPos, &inProcessed, finishMode, &status); + + inPos += inProcessed; + outPos += outProcessed; + //unpackSize -= outProcessed; + + output.insert(output.end(), outBuf.get(), outBuf.get() + outPos); + + outPos = 0; + + if (res != SZ_OK) + break; + + if (inProcessed == 0 && outProcessed == 0) + { + if (status != plz::c::LZMA_STATUS_FINISHED_WITH_MARK) + { + LzmaDec_Free(&state, &plz::c::g_Alloc); + return static_cast(SZ_ERROR_DATA); + } + break; + } + } + } + LzmaDec_Free(&state, &plz::c::g_Alloc); + + return static_cast(res); + } + +} + +#endif //POCKETLZMA_POCKETLZMA_CLASS_HPP + +/*** End of inlined file: pocketlzma_class.hpp ***/ + +#endif //POCKETLZMA_POCKETLZMA_H diff --git a/loader/src/inc/pump.h b/loader/src/inc/pump.h new file mode 100644 index 0000000..f6b578b --- /dev/null +++ b/loader/src/inc/pump.h @@ -0,0 +1,46 @@ +// +// Created by drof on 12.02.24. +// + +#ifndef CLIENT_PUMP_H +#define CLIENT_PUMP_H + +#include + +using Pump_read_callback_fn = void(*)(int /*SCK*/, proto::Packet_header_t /*PACK_HDR*/, std::vector /*VEC_DATA*/ ); +struct Pump_read_handler_t +{ + u32 target_packet_id; + Pump_read_callback_fn cbk; +}; + +struct Pump_queued_write_packet_t +{ + u32 id; + std::vector vec_contents; +}; + +class Pump +{ +public: + Pump() = delete; + Pump(int sck, unsigned char* client_pk, unsigned char* client_sk, unsigned char* server_pk); + + void add_read_handler( u32 target_packet_id, Pump_read_callback_fn cbk ); + + void push_packet_to_write( u32 id, const std::vector& vec_contents ); + void push_packet_to_write( u32 id, const void* data, u32 len ); +private: + void packets_read(); + void packets_write(); +private: + int m_socket; + bool m_is_activated; + bool m_is_failed; + std::vector m_vec_read_handlers; + std::deque< Pump_queued_write_packet_t > m_vec_write_queue; + unsigned char m_client_pk[crypto_kx_PUBLICKEYBYTES], m_client_sk[crypto_kx_SECRETKEYBYTES]; + unsigned char m_server_pk[crypto_kx_PUBLICKEYBYTES]; +}; + +#endif //CLIENT_PUMP_H diff --git a/loader/src/inc/respawn.h b/loader/src/inc/respawn.h new file mode 100644 index 0000000..fad7c54 --- /dev/null +++ b/loader/src/inc/respawn.h @@ -0,0 +1,6 @@ +#pragma once + +namespace respawn +{ + bool collect_offsets(Context* ctx, LoaderContext* lctx, uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap = false); +} diff --git a/loader/src/inc/rotate.h b/loader/src/inc/rotate.h new file mode 100644 index 0000000..09c89e0 --- /dev/null +++ b/loader/src/inc/rotate.h @@ -0,0 +1,84 @@ +// +// Created by drof on 27.01.24. +// + +#ifndef BACKEND_ROTATE_H +#define BACKEND_ROTATE_H + +#pragma once + +namespace rotate +{ + template + constexpr T left(T value, int count) + { + const uint nbits = sizeof(T) * 8; + + if (count > 0) + { + count %= nbits; + T high = value >> (nbits - count); + if (T(-1) < 0) // signed value + high &= ~((T(-1) << count)); + value <<= count; + value |= high; + } + else + { + count = -count % nbits; + T low = value << (nbits - count); + value >>= count; + value |= low; + } + return value; + } + + template + constexpr T right(T value, int count) + { + count = -count; // invert it for this op. + + const uint nbits = sizeof(T) * 8; + + if (count > 0) + { + count %= nbits; + T high = value >> (nbits - count); + if (T(-1) < 0) // signed value + high &= ~((T(-1) << count)); + value <<= count; + value |= high; + } + else + { + count = -count % nbits; + T low = value << (nbits - count); + value >>= count; + value |= low; + } + return value; + } + + inline auto encode(void* data, u32 len) + { + for (u32 i = 0; i < len; i++) + { + reinterpret_cast(data)[i] = + left(reinterpret_cast(data)[i], 1); + reinterpret_cast(data)[i] ^= len; + } + } + + inline auto decode(void* data, u32 len) + { + for (u32 i = 0; i < len; i++) + { + reinterpret_cast(data)[i] ^= len; + reinterpret_cast(data)[i] = + right(reinterpret_cast(data)[i], 1); + } + } +} + + +#endif //BACKEND_ROTATE_H diff --git a/loader/src/inc/sockets-wrapper.h b/loader/src/inc/sockets-wrapper.h new file mode 100644 index 0000000..1676885 --- /dev/null +++ b/loader/src/inc/sockets-wrapper.h @@ -0,0 +1,98 @@ +// +// Created by drof on 26.01.24. +// + +#ifndef BACKEND_SOCKETS_WRAPPER_H +#define BACKEND_SOCKETS_WRAPPER_H + +namespace sock +{ + inline int recursive_send(int socket, const void* pdata, size_t size) + { + auto data = (const char*)pdata; + if (size == 0) { + return 0; // Nothing to send + } + + ssize_t sent_bytes = send(socket, data, size, 0); + + if (sent_bytes == -1) { + // Handle error, e.g., print an error message or return an error code + return -1; + } + + if (sent_bytes < static_cast(size)) { + // Call send recursively for the remaining data + return sent_bytes + recursive_send(socket, data + sent_bytes, size - sent_bytes); + } + + return sent_bytes; + } + + inline int recursive_recv(int socket, void* pbuffer, size_t size) + { + auto buffer= (char*)pbuffer; + if (size == 0) { + return 0; // Nothing to receive + } + + ssize_t received_bytes = recv(socket, buffer, size, 0); + + if (received_bytes == -1) { + // Handle error, e.g., print an error message or return an error code + return -1; + } + + if (received_bytes == 0) { + // Connection closed by the other side + return 0; + } + + // Call recv recursively for the remaining data + return received_bytes + recursive_recv(socket, buffer + received_bytes, size - received_bytes); + } + + inline int recursive_recv_buffered(int socket, void* pbuffer, int size, int chunksize, int chunkratio) + { + int bytes_received = 0; + int bytes_left = size; + + auto data = reinterpret_cast(pbuffer); + + auto bytes_per_chunk = chunksize / chunkratio; + + double last_percent = 0.f; + + while (bytes_left > 0) + { + if(bytes_left < bytes_per_chunk) + bytes_per_chunk = bytes_left; + + int result = recv(socket, &data[bytes_received], bytes_per_chunk, 0); + if (result > 0) + { + double current_percent = (static_cast(bytes_received) / size) * 100; + if( (int)last_percent != (int)current_percent) + { + printf("[DL][BUFFERING] %.0f%%\n", + current_percent); + + last_percent = current_percent; + } + bytes_left -= result; + bytes_received += result; + } else + { + if(result < 0) + { + return -1; + } + } + } + + return bytes_received; + } +} + +#endif //BACKEND_SOCKETS_WRAPPER_H + diff --git a/loader/src/inc/string-table.h b/loader/src/inc/string-table.h new file mode 100644 index 0000000..ab2996b --- /dev/null +++ b/loader/src/inc/string-table.h @@ -0,0 +1,30 @@ +#pragma once + +namespace string_table +{ + extern void stream_encode(uint8_t *buffer, size_t length, uint64_t key); + extern void stream_decode(uint8_t *buffer, size_t length, uint64_t key); + +#define STRREC_MAX_CONTENT_LENGTH 64 + + struct String_record + { + uint32_t length; + uint32_t hash; + char content[STRREC_MAX_CONTENT_LENGTH]; + } __attribute__((packed)); + + struct String_table + { + uint32_t magic; + uint32_t table_total_length; + uint32_t table_records_count; + String_record table[1]; + } __attribute__((packed)); + + extern String_table *Create(const std::vector &vec_contents); + extern void Dump_records(String_table *table); + extern void Dump_contents(String_table *table, size_t table_len); + + extern void Encrypt(String_table* table); +} diff --git a/loader/src/inc/table64.h b/loader/src/inc/table64.h new file mode 100644 index 0000000..31e06a9 --- /dev/null +++ b/loader/src/inc/table64.h @@ -0,0 +1,69 @@ +#pragma once + +#define C_NONE 0x00 +#define C_MODRM 0x01 +#define C_IMM8 0x02 +#define C_IMM16 0x04 +#define C_IMM_P66 0x10 +#define C_REL8 0x20 +#define C_REL32 0x40 +#define C_GROUP 0x80 +#define C_ERROR 0xff + +#define PRE_ANY 0x00 +#define PRE_NONE 0x01 +#define PRE_F2 0x02 +#define PRE_F3 0x04 +#define PRE_66 0x08 +#define PRE_67 0x10 +#define PRE_LOCK 0x20 +#define PRE_SEG 0x40 +#define PRE_ALL 0xff + +#define DELTA_OPCODES 0x4a +#define DELTA_FPU_REG 0xfd +#define DELTA_FPU_MODRM 0x104 +#define DELTA_PREFIXES 0x13c +#define DELTA_OP_LOCK_OK 0x1ae +#define DELTA_OP2_LOCK_OK 0x1c6 +#define DELTA_OP_ONLY_MEM 0x1d8 +#define DELTA_OP2_ONLY_MEM 0x1e7 + +inline unsigned char hde64_table[] = { + 0xa5,0xaa,0xa5,0xb8,0xa5,0xaa,0xa5,0xaa,0xa5,0xb8,0xa5,0xb8,0xa5,0xb8,0xa5, + 0xb8,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xc0,0xac,0xc0,0xcc,0xc0,0xa1,0xa1, + 0xa1,0xa1,0xb1,0xa5,0xa5,0xa6,0xc0,0xc0,0xd7,0xda,0xe0,0xc0,0xe4,0xc0,0xea, + 0xea,0xe0,0xe0,0x98,0xc8,0xee,0xf1,0xa5,0xd3,0xa5,0xa5,0xa1,0xea,0x9e,0xc0, + 0xc0,0xc2,0xc0,0xe6,0x03,0x7f,0x11,0x7f,0x01,0x7f,0x01,0x3f,0x01,0x01,0xab, + 0x8b,0x90,0x64,0x5b,0x5b,0x5b,0x5b,0x5b,0x92,0x5b,0x5b,0x76,0x90,0x92,0x92, + 0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x6a,0x73,0x90, + 0x5b,0x52,0x52,0x52,0x52,0x5b,0x5b,0x5b,0x5b,0x77,0x7c,0x77,0x85,0x5b,0x5b, + 0x70,0x5b,0x7a,0xaf,0x76,0x76,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b,0x5b, + 0x5b,0x5b,0x86,0x01,0x03,0x01,0x04,0x03,0xd5,0x03,0xd5,0x03,0xcc,0x01,0xbc, + 0x03,0xf0,0x03,0x03,0x04,0x00,0x50,0x50,0x50,0x50,0xff,0x20,0x20,0x20,0x20, + 0x01,0x01,0x01,0x01,0xc4,0x02,0x10,0xff,0xff,0xff,0x01,0x00,0x03,0x11,0xff, + 0x03,0xc4,0xc6,0xc8,0x02,0x10,0x00,0xff,0xcc,0x01,0x01,0x01,0x00,0x00,0x00, + 0x00,0x01,0x01,0x03,0x01,0xff,0xff,0xc0,0xc2,0x10,0x11,0x02,0x03,0x01,0x01, + 0x01,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0x00,0x00,0xff,0xff,0xff,0xff,0x10, + 0x10,0x10,0x10,0x02,0x10,0x00,0x00,0xc6,0xc8,0x02,0x02,0x02,0x02,0x06,0x00, + 0x04,0x00,0x02,0xff,0x00,0xc0,0xc2,0x01,0x01,0x03,0x03,0x03,0xca,0x40,0x00, + 0x0a,0x00,0x04,0x00,0x00,0x00,0x00,0x7f,0x00,0x33,0x01,0x00,0x00,0x00,0x00, + 0x00,0x00,0xff,0xbf,0xff,0xff,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0xff,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff, + 0x00,0x00,0x00,0xbf,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7f,0x00,0x00, + 0xff,0x40,0x40,0x40,0x40,0x41,0x49,0x40,0x40,0x40,0x40,0x4c,0x42,0x40,0x40, + 0x40,0x40,0x40,0x40,0x40,0x40,0x4f,0x44,0x53,0x40,0x40,0x40,0x44,0x57,0x43, + 0x5c,0x40,0x60,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x40,0x40,0x64,0x66,0x6e,0x6b,0x40,0x40,0x6a,0x46,0x40,0x40,0x44,0x46,0x40, + 0x40,0x5b,0x44,0x40,0x40,0x00,0x00,0x00,0x00,0x06,0x06,0x06,0x06,0x01,0x06, + 0x06,0x02,0x06,0x06,0x00,0x06,0x00,0x0a,0x0a,0x00,0x00,0x00,0x02,0x07,0x07, + 0x06,0x02,0x0d,0x06,0x06,0x06,0x0e,0x05,0x05,0x02,0x02,0x00,0x00,0x04,0x04, + 0x04,0x04,0x05,0x06,0x06,0x06,0x00,0x00,0x00,0x0e,0x00,0x00,0x08,0x00,0x10, + 0x00,0x18,0x00,0x20,0x00,0x28,0x00,0x30,0x00,0x80,0x01,0x82,0x01,0x86,0x00, + 0xf6,0xcf,0xfe,0x3f,0xab,0x00,0xb0,0x00,0xb1,0x00,0xb3,0x00,0xba,0xf8,0xbb, + 0x00,0xc0,0x00,0xc1,0x00,0xc7,0xbf,0x62,0xff,0x00,0x8d,0xff,0x00,0xc4,0xff, + 0x00,0xc5,0xff,0x00,0xff,0xff,0xeb,0x01,0xff,0x0e,0x12,0x08,0x00,0x13,0x09, + 0x00,0x16,0x08,0x00,0x17,0x09,0x00,0x2b,0x09,0x00,0xae,0xff,0x07,0xb2,0xff, + 0x00,0xb4,0xff,0x00,0xb5,0xff,0x00,0xc3,0x01,0x00,0xc7,0xff,0xbf,0xe7,0x08, + 0x00,0xf0,0x02,0x00 +}; \ No newline at end of file diff --git a/loader/src/inc/tools.h b/loader/src/inc/tools.h new file mode 100644 index 0000000..5000381 --- /dev/null +++ b/loader/src/inc/tools.h @@ -0,0 +1,69 @@ +#pragma once + +namespace tools +{ + bool shell_execute(const char *cmd, char *buf, size_t max_len); + int proc_name2id(const char *name); + void proc_suspend(int pid); + void proc_resume(int pid); + u64 get_mapping_base(int pid, const char *name); + char query_virt_mem(u64 addr, hr_procmaps &out_desc); + std::string get_current_process_path(); + bool get_self_memory_bounds(uint64_t &base, uint64_t &length); + + inline uint64_t curtick() + { + static auto start = std::chrono::high_resolution_clock::now(); + auto now = std::chrono::high_resolution_clock::now(); + auto elapsed = std::chrono::duration_cast(now - start); + return elapsed.count(); + } + + inline std::vector aob_read(const std::string &filename) + { + std::ifstream file(filename, std::ios::binary); + std::vector data((std::istreambuf_iterator(file)), std::istreambuf_iterator()); + return data; + } + + inline bool aob_write_to_disk(const char *path, void *buf, size_t len) + { + FILE *file = fopen(path, "wb"); + if (fwrite(buf, 1, len, file) == len) + { + return true; + } + else + { + return false; + } + + fclose(file); + } + + inline void hex_dump(const void *ptr, int buflen) + { + unsigned char *buf = (unsigned char *)ptr; + int i, j; + for (i = 0; i < buflen; i += 16) + { + printf("%06x: ", i); + for (j = 0; j < 16; j++) + if (i + j < buflen) + printf("%02x ", buf[i + j]); + else + printf(" "); + printf(" "); + for (j = 0; j < 16; j++) + if (i + j < buflen) + printf("%c", isprint(buf[i + j]) ? buf[i + j] : '.'); + printf("\n"); + } + } + + inline bool is_running_as_root() + { + uid_t euid = geteuid(); + return (euid == 0); + } +} diff --git a/loader/src/inc/vmt.h b/loader/src/inc/vmt.h new file mode 100644 index 0000000..f02f860 --- /dev/null +++ b/loader/src/inc/vmt.h @@ -0,0 +1,48 @@ +#pragma once + +inline u64 vft_get_handler(u64 inst, int idx) +{ + auto vft_base = gmm->tread(inst); + if (!vft_base) + return 0; + + return gmm->tread(vft_base + (idx * 8)); +} + +inline bool vft_set_handler(int pid, u64 inst, int idx, u64 value) +{ + auto vft_base = gmm->tread(inst); + if (!vft_base) + return 0; + + return pt::write(pid, vft_base + (idx * 8), value); +} + +size_t vft_get_count(u64 inst); + +inline bool vft_dump(u64 inst, size_t count, std::vector &buf) +{ + auto table = gmm->tread(inst); + if (!table) + return false; + + buf.resize(count); + + size_t index = 0; + + while(index < count) + { + buf[index] = gmm->tread(table + (index * sizeof(u64))); + + index++; + } + + return index == count; +} + +struct vft_override_t +{ + u64 index; + u64 handler; +}; +bool make_remote_vft(u64 inst, u64 &remote_vft_addr, std::vector &dump, std::vector &dump_origs, const std::initializer_list &overrides); \ No newline at end of file diff --git a/loader/src/inc/wptrace.h b/loader/src/inc/wptrace.h new file mode 100644 index 0000000..266aeca --- /dev/null +++ b/loader/src/inc/wptrace.h @@ -0,0 +1,125 @@ +#pragma once + +namespace pt +{ + extern int _status; + extern int _pid; + extern u64 _gadget; + + inline int sync(int pid) + { + waitpid(pid, &_status, 0); + return _status; + } + + inline bool attach(int pid) + { + auto rs = ptrace(PTRACE_ATTACH, pid, 0, 0) > -1; + + if(!rs) + { + safeLog_msg("pt::attach failed -> %i", errno); + return false; + } + + sync(pid); + + return true; + } + + inline bool detach(int pid) + { + auto rs = ptrace(PTRACE_DETACH, pid, 0, 0) > -1; + + if(!rs) + { + safeLog_msg("pt::detach failed -> %i", errno); + return false; + } + + return true; + } + + inline bool context_capture(int pid, user_regs_struct* buf) + { + auto rs = ptrace(PTRACE_GETREGS, pid, 0, buf) > -1; + + if(!rs) + { + safeLog_msg("pt::context_capture failed -> %i", errno); + return false; + } + + return true; + } + + inline bool context_swap(int pid, user_regs_struct* buf) + { + auto rs = ptrace(PTRACE_SETREGS, pid, 0, buf) > -1; + + if(!rs) + { + safeLog_msg("pt::context_swap failed -> %i", errno); + return false; + } + + return true; + } + + inline user_regs_struct context(int pid) + { + user_regs_struct rs; + memset(&rs, 0, sizeof(rs)); + + if(!context_capture(pid, &rs)) + safeLog_msg("failed capture context."); + + return rs; + } + + inline bool resume(int pid) + { + auto rs = ptrace(PTRACE_CONT, pid, 0, 0) > -1; + + if(!rs) + { + safeLog_msg("pt::Resume failed -> %i", errno); + return false; + } + + return true; + } + + inline bool single_step(int pid) + { + auto rs = ptrace(PTRACE_SINGLESTEP, pid, 0, 0) > -1; + + if(!rs) + { + safeLog_msg("pt::SingleStep failed -> %i", errno); + return false; + } + + return true; + } + + inline bool write(int pid, u64 addr, u64 value) + { + auto rs = ptrace(PTRACE_POKETEXT, pid, addr, value) > -1; + + if(!rs) + { + safeLog_msg("pt::Write failed -> %i", errno); + return false; + } + + return true; + } + + bool syscall(int pid, u64 gadget, int sid, + u64 param_rdi, u64 param_rsi, u64 param_rdx, + u64 param_r10, u64 param_r8, u64 param_r9, u64* result); + + u64 mmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset); + bool mprotect(u64 addr, size_t len, int prot); +} diff --git a/loader/src/inc/xorstr.h b/loader/src/inc/xorstr.h new file mode 100644 index 0000000..bd07654 --- /dev/null +++ b/loader/src/inc/xorstr.h @@ -0,0 +1,4 @@ +#pragma once + +#define _XS(X) X +#define _XSW(X) X diff --git a/loader/src/integrity.cpp b/loader/src/integrity.cpp new file mode 100644 index 0000000..e09bc5d --- /dev/null +++ b/loader/src/integrity.cpp @@ -0,0 +1,253 @@ +#include "inc/include.h" + +#if defined R5I_PROD +Integrity* gInteg = new Integrity(); +#else +Integrity* gInteg = nullptr; +#endif + +bool Integrity::is_process_being_traced() +{ + bool result = false; + + std::string line; + std::ifstream file("/proc/self/status"); + + while (std::getline(file, line)) + { + std::istringstream _stream(line); + std::string tag, value; + _stream >> tag >> value; + + if (tag == "TracerPid:" && value != "0") + result = true; + } + + return result; +} + +bool Integrity::is_process_being_patched() +{ + bool result = false; + + uptr my_base = 0, my_length = 0; + + if(!tools::get_self_memory_bounds(my_base, my_length)) + { + /* in case someone tries to tamper with some API or some shit. */ + return true; + } + + dbg("myBase: %p, myLEn: %llx", my_base, my_length); + + if(!my_base || !my_length) + { + /* it could be an error, but not very likely. more likely is that someone tampers with us */ + return true; + } + + auto image_ondisk = ELFImage(tools::get_current_process_path().c_str()); + auto pscn_text = image_ondisk.find_section(".text"); + + if(!pscn_text) + { + /* no text section present? that is very fucking sus, at the VERY least. */ + return true; + } + + std::vector vec_snapshot_text(pscn_text->length); + memcpy( vec_snapshot_text.data(), PTR_OF(pscn_text->addr), vec_snapshot_text.size() ); + + auto current_checksum = SSE_CRC32( vec_snapshot_text.data(), vec_snapshot_text.size(), 0 ); + if(!current_checksum) + { + /* this cannot possibly happen, but still check for it just in case. */ + return true; + } + + if(!m_checksum_self_seg_text) + { + dbg("inited the checksum with %x", current_checksum); + m_checksum_self_seg_text = current_checksum; + } else + { + if(current_checksum != m_checksum_self_seg_text) + { + /* nice try */ + return true; + } + } + + return result; +} + +Integrity::Integrity() +{ +#if !defined(R5I_DISABLE_INTEGRITY_CHECKS) + std::thread(&Integrity::check1_thread, this).detach(); + std::thread(&Integrity::check2_thread, this).detach(); +#endif +} + +void Integrity::check1_thread() +{ + while(true) + { + if(check_debugger_presence()) + { + push_violation( proto::msg::integrity_event::IDS_VIOLATION_DETECTED_DEBUGGER_PRESENT, {} ); + break; + } + + if(check_blacklisted_program_presence()) + { + push_violation( proto::msg::integrity_event::IDS_VIOLATION_BLACKLISTED_PROGRAM_RUNNING, {} ); + break; + } + + if(is_process_being_patched()) + { + push_violation( proto::msg::integrity_event::IDS_VIOLATION_CHECKSUM_MISMATCH, {} ); + break; + } + + std::this_thread::sleep_for(500ms); + } + + std::this_thread::sleep_for(5s); + terminate_safe(); +} + +void Integrity::check2_thread() +{ + while(true) + { + if(check_debugger_presence()) + { + push_violation( proto::msg::integrity_event::IDS_VIOLATION_DETECTED_DEBUGGER_PRESENT, {} ); + break; + } + + if(is_process_being_patched()) + { + push_violation( proto::msg::integrity_event::IDS_VIOLATION_CHECKSUM_MISMATCH, {} ); + break; + } + + if(check_blacklisted_program_presence()) + { + push_violation( proto::msg::integrity_event::IDS_VIOLATION_BLACKLISTED_PROGRAM_RUNNING, {} ); + break; + } + + std::this_thread::sleep_for(2000ms); + } + + std::this_thread::sleep_for(5s); + terminate_safe(); +} + +bool Integrity::check_debugger_presence() +{ + if(is_process_being_traced()) + return true; + + return false; +} + +bool Integrity::check_blacklisted_program_presence() +{ + std::vector< std::string > vec_blacklisted_program_names = + { + "ida", + "gdb", + }; + + for(const auto& text : vec_blacklisted_program_names) + { + auto pid = tools::proc_name2id(text.c_str()); + + if(pid != 0) + { + dbg("[Integrity] detected a blacklisted process currently executing: %s with PID %i", text.c_str(), pid); + return true; + } + } + + return false; +} + +struct Queued_integ_record +{ + proto::msg::integrity_event::ids id; + std::vector vec_contents; +}; +std::vector< Queued_integ_record > gvQueuedIntegPackets; +void Integrity::push_violation(proto::msg::integrity_event::ids id, proto::msg::integrity_event::contents_t contents) +{ + proto::msg::integrity_event pack; + memset(&pack, 0, sizeof(pack)); + + pack.id = id; + pack.component = proto::msg::integrity_event::COMPONENTS_LOADER; + + memcpy(&pack.contents, &contents, sizeof(pack.contents)); + + auto pump = gNetSDK->pump(); + if(pump) + { + pump->push_packet_to_write( proto::PACKET_ID_CLIENT_INTEGRITY_EVENT, &pack, sizeof(pack) ); + + if(!gvQueuedIntegPackets.empty()) + { + for(auto rec : gvQueuedIntegPackets) + { + pump->push_packet_to_write( proto::PACKET_ID_CLIENT_INTEGRITY_EVENT, rec.vec_contents ); + } + + gvQueuedIntegPackets.clear( ); + } + } else + { + std::vector vec_contents(sizeof(pack)); + memcpy(vec_contents.data(), &pack, vec_contents.size()); + + Queued_integ_record rec{ }; + rec.id = id; + rec.vec_contents = vec_contents; + gvQueuedIntegPackets.push_back( rec ); + } +} + +u32 Integrity::calc_self_code_checksum() +{ + uptr my_base = 0, my_length = 0; + + if(!tools::get_self_memory_bounds(my_base, my_length)) + { + /* in case someone tries to tamper with some API or some shit. */ + return true; + } + + dbg("myBase: %p, myLEn: %llx", my_base, my_length); + + if(!my_base || !my_length) + { + /* it could be an error, but not very likely. more likely is that someone tampers with us */ + return true; + } + + auto image_ondisk = ELFImage(tools::get_current_process_path().c_str()); + auto pscn_text = image_ondisk.find_section(".text"); + + if(!pscn_text) + { + /* no text section present? that is very fucking sus, at the VERY least. */ + return true; + } + + std::vector vec_snapshot_text(pscn_text->length); + memcpy( vec_snapshot_text.data(), PTR_OF(pscn_text->addr), vec_snapshot_text.size() ); + + return SSE_CRC32( vec_snapshot_text.data(), vec_snapshot_text.size(), 0 ); +} diff --git a/loader/src/libpmap.cpp b/loader/src/libpmap.cpp new file mode 100644 index 0000000..88e97f6 --- /dev/null +++ b/loader/src/libpmap.cpp @@ -0,0 +1,147 @@ +#include "inc/include.h" + +/** + * @file parse_procmaps.c + * @brief Source file for lib procmaps + * @author Lilian VERLHAC + * @version 1.0 + * @date 2020-04-18 + */ + +#include "inc/libpmap.h" +#include +#include +#include +#include +#include + +void destroy_procmaps(hr_procmaps **procmaps) +{ + size_t i = 0; + + while (procmaps[i]) { + free(procmaps[i]->pathname); + free(procmaps[i]); + ++i; + } + free(procmaps); +} + +static unsigned char make_perms(char const *perms) +{ + unsigned char perm = 0x00; + + if (perms[0] == 'r') { + perm |= PERMS_READ; + } + if (perms[1] == 'w') { + perm |= PERMS_WRITE; + } + if (perms[2] == 'x') { + perm |= PERMS_EXECUTE; + } + if (perms[3] == 'p') { + perm |= PERMS_PRIVATE; + } else if (perms[3] == 's') { + perm |= PERMS_SHARED; + } + return perm; +} + +procmaps_row_t *parse_procmaps_line(char *line) +{ + auto procmaps_row = (procmaps_row_t*)malloc(sizeof(procmaps_row_t)); + char *pathname_token = NULL; + + if (procmaps_row == NULL) + return NULL; + procmaps_row->addr_begin = strtoull(strtok(line, "-"), NULL, 16); + procmaps_row->addr_end = strtoull(strtok(NULL, " "), NULL, 16); + procmaps_row->perms = make_perms(strtok(NULL, " ")); + procmaps_row->offset = strtoull(strtok(NULL, " "), NULL, 16); + procmaps_row->dev.major = atoi(strtok(NULL, ":")); + procmaps_row->dev.minor = atoi(strtok(NULL, " ")); + procmaps_row->inode = atoi(strtok(NULL, " ")); + pathname_token = strtok(NULL, " \n"); + if (pathname_token != NULL) { + procmaps_row->pathname = strdup(pathname_token); + if (procmaps_row->pathname == NULL) { + free(procmaps_row); + return NULL; + } + } else { + procmaps_row->pathname = NULL; + } + return procmaps_row; +} + +static hr_procmaps **make_procmaps_array(FILE *procmaps_file) +{ + size_t array_size = PROCMAPS_ARRAY_BASE_SIZE; + hr_procmaps **procmaps_array = (hr_procmaps**)calloc(array_size, sizeof(hr_procmaps)); + size_t i = 0; + size_t size = 0; + char *line = NULL; + + if (procmaps_array == NULL) + return NULL; + while (getline(&line, &size, procmaps_file) != -1) { + if (line != NULL) + procmaps_array[i] = parse_procmaps_line(line); + ++i; + if (i == array_size) { + procmaps_array = (hr_procmaps**)realloc(procmaps_array, (array_size + 16) * sizeof(*procmaps_array)); + if (procmaps_array == NULL) { + destroy_procmaps(procmaps_array); + return NULL; + } + memset(procmaps_array + array_size, 0, 16 * sizeof(*procmaps_array)); + array_size += 16; + } + free(line), line = NULL; + } + free(line); + if (errno == EINVAL || errno == ENOMEM) { + destroy_procmaps(procmaps_array); + return NULL; + } + return procmaps_array; +} + +std::mutex procmaps_lock; + +hr_procmaps **contruct_procmaps(int pid) +{ + + + char path[100]; + FILE *procmaps_file = NULL; + int errno_saver = 0; + hr_procmaps **procmaps_array = NULL; + + if (pid <= 0) { + strcpy(path, "/proc/self/maps"); + } else { + sprintf(path, "/proc/%d/maps", pid); + } + procmaps_file = fopen(path, "r"); + if (procmaps_file == NULL) { + perror("fopen"); + return NULL; + } + procmaps_array = make_procmaps_array(procmaps_file); + errno_saver = errno; + if (fclose(procmaps_file) != -1) + errno = errno_saver; + return procmaps_array; +} + +void procmaps_begin_transaction() +{ + procmaps_lock.lock(); +} + +void procmaps_end_transaction() +{ + procmaps_lock.unlock(); +} diff --git a/loader/src/loader.cpp b/loader/src/loader.cpp new file mode 100644 index 0000000..868ec15 --- /dev/null +++ b/loader/src/loader.cpp @@ -0,0 +1,349 @@ +#include "inc/include.h" + +Loader* gLoader = nullptr; + +void Loader::start(int argc, char* argv[]) +{ +#if defined(R5I_PROD) + //msg("aes-key: '%s'", STRINGIFIED_DEF(_LOADER_COMPILATION_AES_KEY)); + //msg("email: '%s'", STRINGIFIED_DEF(_LOADER_USER_EMAIL)); + //msg("token: '%s'", STRINGIFIED_DEF(_LOADER_USER_ACCESS_TOKEN)); + //msg("ts: '%s'", STRINGIFIED_DEF(_BUILD_TIMESTAMP)); + + if( std::string(STRINGIFIED_DEF(_LOADER_USER_EMAIL)).empty() || std::string(STRINGIFIED_DEF(_LOADER_USER_ACCESS_TOKEN)).empty() ) + { + msg("internal error: AEFC"); + return; + } + + if( std::string(STRINGIFIED_DEF(_LOADER_COMPILATION_AES_KEY)).empty() || std::string(STRINGIFIED_DEF(_BUILD_TIMESTAMP)).empty() ) + { + msg("internal error: AEFD"); + return; + } + + if(!setup_netsdk()) + { + msg("networking error: 10AE"); + return; + } + + if(!this->authenticate()) + { + msg("failed authentication. please check your credentials!"); + std::string reason = "N/A"; + switch(gNetSsnContext->value_authentication_result) + { + case proto::AUTH_STATUS_INVALID_CREDENTIALS: reason ="invalid credentials"; break; + case proto::AUTH_STATUS_USER_BANNED: reason = "you currently have an active ban on record (1)!"; break; + case proto::AUTH_STATUS_HARDWARE_BANNED: reason = "you currently have an active ban on record (2)!"; break; + case proto::AUTH_STATUS_VM_CHECKS_FAILED: reason = "your hardware is not genuine. please contact support for more information."; break; + case proto::AUTH_STATUS_COOLDOWN: reason = "slow down! because of too many failed attempts, you will have to wait a few minutes before trying again."; break; + case proto::AUTH_STATUS_CLIENT_SIDE_ERROR: reason = "an unexpected error has occurred during authentication attempt on the client side."; break; + case proto::AUTH_STATUS_INTERNAL_ERROR: reason = "an unexpected error has occurred during authentication attempt internally."; break; + } + msg("failure reason: %s", reason.c_str()); + msg("do not hesistate to contact support by sending an email to support@cryot.de, should you believe this is an error, or require support in general."); + return; + } +#endif + + // + + /* + * PRE-LAUNCH PROCEDURES + */ + gctx = new Context(); + memset(&gctx->m_lctx, 0, sizeof(gctx->m_lctx)); + +#if !defined(R5I_PROD) + if (argc > 1) + { + std::vector args; + + for (int i = 1; i < argc; i++) + { + args.push_back(std::string(argv[i])); + } + + for (const auto &arg : args) + parse_argument(arg); + } +#else + gctx->m_opts.use_unsafe_memory = true; +#endif + + if(!tools::is_running_as_root()) + { + safeLog_msg("!! you must run this as root !!"); + return; + } + + if(tools::proc_name2id(TARGET_NAME) != 0 && (!gctx->m_opts.dump_game_image && !gctx->m_opts.debug_sigs)) + { + safeLog_msg("!! you must run this before the game launches !!"); + return; + } + +#if !defined(R5I_PROD) + std::string image_path = _XS("/mnt/dev-r5i/r5_internal_proton_llvm/patched_r5.dll"); + + if(gctx->m_opts.use_custom_payload_path) + { + safeLog_msg("!! overriding image path with custom option: %s.", gctx->m_opts.custom_payload_path.c_str()); + image_path = gctx->m_opts.custom_payload_path; + } + + std::vector image = tools::aob_read(image_path); + if (image.empty()) + { + safeLog_msg("failed to read default image."); + return; + } +#else + //tools::hex_dump( vec_game_dump.data(), 0x1000 ); + + if(!gNetSDK->request_dlc(0x6523)) + { + msg("networking error: 10AF"); + return; + } + + while(!gNetSsnContext->received_request_dlc_response) + { + _mm_pause(); + std::this_thread::sleep_for(100ms); + } + + if(!gNetSsnContext->will_server_send_requested_dlc) + { + msg("networking error: 10AD"); + return; + } + + msg("confirmed!"); + + while(!gNetSsnContext->received_dlc) + { + _mm_pause(); + std::this_thread::sleep_for(100ms); + } + + std::vector image = gNetSsnContext->vec_dlc_contents; + + gctx->m_opts.analysis_from_dump = false; +#endif + + if (!mapper::fetch_image(image)) + { + safeLog_msg("failed to fetch image."); + return; + } + + if (!mapper::resolve_linux_crt()) + { + safeLog_msg("failed to resolve LINUX CRT runtime."); + return; + } + + if (!mapper::setup_pre()) + { + safeLog_msg("failed to mapper setup PRE stage."); + return; + } + + /* + * POST-LAUNCH PROCEDURES + */ + safeLog_msg("waiting for the game..."); + msg("waiting for the game..."); + + int pid = 0; + while (!(pid = tools::proc_name2id(TARGET_NAME))) + { /* ... */ + } + + safeLog_msg("process ID of the target is (%i)", pid); + + gmm = new MemoryManager(pid); + + if (!mapper::setup(pid)) + { + safeLog_msg("failed setup!"); + return; + } + + if (!mapper::load(image)) + { + safeLog_msg("failed load!"); + return; + } + + safeLog_msg("successfully load!"); + + if (gctx->m_opts.payload_debug) + { + do + { + auto ctx = gmm->tread(gctx->m_ctx_base); + + if (!ctx.inited) + break; + + usleep(500000); + } while (true); + } + + delete gmm; + delete gctx; +} + +bool Loader::setup_netsdk() +{ + msg("connecting... (1/7)"); + + gNetSDK = new NetSDK("192.168.1.109", 10000); + if (!gNetSDK->start()) + { + msg("failed to establish connection with www.cryot.de."); + return false; + } + + msg("connecting... (2/7)"); + + if (!gNetSDK->handshake()) + { + msg("failed to establish connection with www.cryot.de (2)."); + return false; + } + + msg("connecting... (3/7)"); + + if (!gNetSDK->wait_for_activation()) + { + msg("failed to establish connection with www.cryot.de (3)."); + return false; + } + + msg("connecting... (4/7)"); + + if (!gNetSDK->start_pump()) + { + msg("failed to establish connection with www.cryot.de (4)."); + return false; + } + + msg("connecting... (5/7)"); + + if(!gNetSDK->ping()) + { + msg("failed to establish connection with www.cryot.de (5)."); + return false; + } + + msg("connecting... (6/7)"); + + while(!gNetSsnContext->received_ping) + { + _mm_pause(); + } + + if(!gNetSsnContext->has_server_acknowledged_ping) + { + msg("failed to establish connection with www.cryot.de (6)."); + return false; + } + + msg("connecting... (7/7)"); + + return true; +} + +bool Loader::authenticate() +{ + std::string input_email = STRINGIFIED_DEF(_LOADER_USER_EMAIL), input_password = STRINGIFIED_DEF(_LOADER_USER_ACCESS_TOKEN); + + if(input_email.empty() || input_password.empty()) + { + msg("please fix those empty input fields."); + return false; + } + + if(!gNetSDK->authenticate(input_email, input_password)) + { + msg("failed authentication due to networking error (1)."); + return false; + } + + while(!gNetSsnContext->received_authentication_result) + { + _mm_pause(); + } + + return (gNetSsnContext->value_authentication_result == proto::AUTH_STATUS_SUCCESS); +} + +std::string Loader::get_argument_param(const std::string &str) +{ + std::string delimiter = "="; + std::size_t pos = str.find(delimiter); + if (pos != std::string::npos) + { + // Extract the path by removing the prefix and '=' character + std::string arg1 = str.substr(pos + delimiter.length()); + return arg1; + } + + return ""; +} + +void Loader::parse_argument(const std::string &str) +{ +#if !defined(R5I_PROD) + if (str == "--debug-payload") + { + safeLog_msg("!! enabled payload debug mode !!"); + gctx->m_opts.payload_debug = true; + } + + if (str == "--debug-sigs") + { + safeLog_msg("!! enable debug_SIGS mode !!"); + gctx->m_opts.debug_sigs = true; + } + + if (str == "--dump-game-image") + { + safeLog_msg("!! enable dump_GAME_IMAGE mode !!"); + gctx->m_opts.dump_game_image = true; + } + + if (str.find("--analysis-from-dump") != std::string::npos) + { + auto path = get_argument_param(str); + if(!path.empty()) + { + safeLog_msg("!! performing analysis on dump file at %s", path.c_str()); + gctx->m_opts.analysis_from_dump = true; + gctx->m_opts.analysis_from_dump_path = path; + } + } + + if(str.find("--custom-payload-path") != std::string::npos) + { + auto path = get_argument_param(str); + if(!path.empty()) + { + safeLog_msg("!! using custom payload path, specified as %s", path.c_str()); + gctx->m_opts.use_custom_payload_path = true; + gctx->m_opts.custom_payload_path = path; + } + } + + if (str.find("--use-unsafe-memory") != std::string::npos) + { + safeLog_msg("!! using unsafe memory for mapping the payload, this is UNSAFE as fuck."); + gctx->m_opts.use_unsafe_memory = true; + } +#endif +} diff --git a/loader/src/log.cpp b/loader/src/log.cpp new file mode 100644 index 0000000..83983ac --- /dev/null +++ b/loader/src/log.cpp @@ -0,0 +1,28 @@ +#include "inc/include.h" + +namespace xlog +{ + void __msg(const char *fmt, ...) + { + time_t tm_curr = time(0); + + va_list vl; + va_start(vl, fmt); + printf("MSG "); + vprintf(fmt, vl); + printf("\n"); + va_end(vl); + } + + void __error(const char *fmt, ...) + { + time_t tm_curr = time(0); + + va_list vl; + va_start(vl, fmt); + printf("ERROR "); + vprintf(fmt, vl); + printf("\n"); + va_end(vl); + } +} diff --git a/loader/src/lzmapp.cpp b/loader/src/lzmapp.cpp new file mode 100644 index 0000000..5d74f8d --- /dev/null +++ b/loader/src/lzmapp.cpp @@ -0,0 +1,43 @@ +#include "inc/include.h" + +#pragma warning(push, 0) +#define POCKETLZMA_LZMA_C_DEFINE +#include "inc/pocketlzma.h" +#pragma warning(pop) + +namespace lzma +{ + std::vector compress(const std::vector& data) + { + std::vector compressed_data; + + plz::PocketLzma p; + p.usePreset(plz::Preset::Fast); + plz::StatusCode status = p.compress(data, compressed_data); + if (status != plz::StatusCode::Ok) + { + dbg("failed CALL to compress: %lld", status); + compressed_data.clear(); + return compressed_data; + } + + return compressed_data; + } + + std::vector decompress(const std::vector& data) + { + std::vector decompressed_data; + + plz::PocketLzma p; + plz::StatusCode status = p.decompress(data, decompressed_data); + if (status != plz::StatusCode::Ok) + { + dbg("failed CALL to compress: %lld", status); + decompressed_data.clear(); + return decompressed_data; + } + + return decompressed_data; + } +} + diff --git a/loader/src/mapper.cpp b/loader/src/mapper.cpp new file mode 100644 index 0000000..b4962e2 --- /dev/null +++ b/loader/src/mapper.cpp @@ -0,0 +1,1008 @@ +#include "inc/include.h" + +uptr get_directory_file_addr(uptr base, PIMAGE_NT_HEADERS64 nt_hdr, u8 index) +{ + auto dir = &nt_hdr->OptionalHeader.DataDirectory[index]; + if (!dir->VirtualAddress || dir->Size < 1) + { + return 0; + } + + auto dir_ptr = pe::get_ptr_from_va(dir->VirtualAddress, nt_hdr, (u8 *)base); + if (dir_ptr == nullptr) + { + return 0; + } + + return (uptr)dir_ptr; +} + +bool erase_directory(uptr base, PIMAGE_NT_HEADERS64 nt_hdr, u8 index) +{ + auto dir = &nt_hdr->OptionalHeader.DataDirectory[index]; + if (!dir->VirtualAddress || dir->Size < 1) + { + safeLog_msg("[erase-directory]: given index is not present."); + return false; + } + + auto dir_ptr = pe::get_ptr_from_va(dir->VirtualAddress, nt_hdr, (u8 *)base); + if (dir_ptr == nullptr) + { + safeLog_msg("[erase-directory]: could not resolve pointer."); + return false; + } + + memset(dir_ptr, 0, dir->Size); + + safeLog_msg("[erase-directory]: handled directory %i with base %p and length %llx", + index, dir_ptr, dir->Size); + + return true; +} + +bool wipe_sensitive_data(uintptr_t base, PIMAGE_DOS_HEADER dos_hdr, PIMAGE_NT_HEADERS64 nt_hdr) +{ + if (!erase_directory(base, nt_hdr, IMAGE_DIRECTORY_ENTRY_EXPORT)) + { + safeLog_msg("failed to erase sensitive directories."); + return false; + } + + auto debug_dir_ptr = (PIMAGE_DEBUG_DIRECTORY)get_directory_file_addr(base, nt_hdr, IMAGE_DIRECTORY_ENTRY_DEBUG); + if (debug_dir_ptr) + { + safeLog_msg("ddptr:%p", debug_dir_ptr); + auto debug_data = pe::get_ptr_from_va(debug_dir_ptr->AddressOfRawData, nt_hdr, (u8 *)base); + + safeLog_msg("dataptr:%p", debug_data); + safeLog_msg("len: %x", debug_dir_ptr->SizeOfData); + + memset(debug_data, 0, debug_dir_ptr->SizeOfData); + + if (!erase_directory(base, nt_hdr, IMAGE_DIRECTORY_ENTRY_DEBUG)) + { + safeLog_msg("failed to erase sensitive directories."); + return false; + } + } + else + { + safeLog_msg("debug dir does not exist."); + } + + auto image_len = nt_hdr->OptionalHeader.SizeOfImage; + auto sizeof_hdrs = nt_hdr->OptionalHeader.SizeOfHeaders; + safeLog_msg("size of headers is %llx", sizeof_hdrs); + + if (sizeof_hdrs > 0) + RAND_bytes((unsigned char *)base, sizeof_hdrs); + +#if !defined(R5I_PROD) + if (!tools::aob_write_to_disk(_XS("/home/drof/Documents/wiped.bin"), (void *)base, image_len)) + { + safeLog_msg("failed to write wiped image onto disk."); + return false; + } +#endif + + return true; +} + +relocation_list mapper::get_relocation_list(u64 image_base, PIMAGE_NT_HEADERS64 nt_header) +{ + relocation_list result; + + if (nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress == 0) + return result; + + PIMAGE_BASE_RELOCATION relocationTable = + (PIMAGE_BASE_RELOCATION)(image_base + nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress); + + DWORD relocationEntriesCount = 0; + PBASE_RELOCATION_ENTRY relocationRVA = NULL; + + while (relocationTable->SizeOfBlock > 0) + { + relocationEntriesCount = (relocationTable->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(USHORT); + relocationRVA = (PBASE_RELOCATION_ENTRY)(relocationTable + 1); + + for (uint32_t i = 0; i < relocationEntriesCount; i++) + { + if (relocationRVA[i].Offset) + { + auto &entry = result.emplace_back(); + + entry.m_type = relocationRVA[i].Type; + entry.m_address = image_base + relocationTable->VirtualAddress + relocationRVA[i].Offset; + } + } + + relocationTable = (PIMAGE_BASE_RELOCATION)((u64)relocationTable + relocationTable->SizeOfBlock); + } + + return result; +} + +bool index_game_build(int pid, u64 game_image_base, u64 game_image_len, u64 cs_base, u64 cs_len) +{ + auto remote_shdr_text = gctx->m_game_secs.get_sec_hdr(".text"); + if (!remote_shdr_text.VirtualAddress) + { + safeLog_msg("failed to grab remote segment header for TEXT."); + return false; + } + + auto game_cs_base_remote = remote_shdr_text.VirtualAddress; + + auto version_base = FIND_PTR_LAJR(cs_base, cs_len, game_image_base, game_cs_base_remote, + "game_version", "48 8D 15 ? ? ? ? 48 8D 0D ? ? ? ? E8 ? ? ? ? 84 C0 75 1A", 3, 7, true); + if (!version_base) + return false; + + safeLog_msg("version_base_off= %lx", version_base); + safeLog_msg("waiting for game version data to be populated..."); + + char version[16]; + ZERO_MEM(version, sizeof(version)); + + do + { + if (!gmm->read(version, (gctx->m_game_base + version_base), sizeof(version))) + { + safeLog_msg("index_game_build: failed to read version base."); + return false; + } + + if (strstr(version, "v") && strstr(version, ".")) + { + safeLog_msg("found a valid game version info!"); + break; + } + + usleep(500000); + } while (true); + + auto build_id_base = FIND_PTR_LAJR(cs_base, cs_len, game_image_base, game_cs_base_remote, + "build_id", "48 8D 15 ? ? ? ? 48 8D 0D ? ? ? ? E8 ? ? ? ? 88 05 ? ? ? ? 84 C0 75 44 48 8B 0D", 3, 7, true); + if (!build_id_base) + return false; + + safeLog_msg("build_id_base_off= %lx", build_id_base); + + char build_id[64]; + ZERO_MEM(build_id, sizeof(build_id)); + + if (!gmm->read(build_id, (gctx->m_game_base + build_id_base), sizeof(build_id))) + { + safeLog_msg("index_game_build: failed to read build_id base."); + return false; + } + + safeLog_msg("------ GAME BUILD INFO BELOW ------"); + safeLog_msg("version = '%s'", version); + safeLog_msg("build_id = '%s'", build_id); + safeLog_msg("-----------------------------------"); + + if (gctx->m_opts.dump_game_image) + { + char dump_path[256]; + ZERO_MEM(dump_path, sizeof(dump_path)); + + sprintf(dump_path, _XS("/home/drof/Documents/(%s)_%s"), version, build_id); + + if (!tools::aob_write_to_disk(dump_path, PTR_OF(game_image_base), game_image_len)) + { + safeLog_msg("index_game_build: failed to dump game image."); + return false; + } + + safeLog_msg("index_game_build: dump successful!"); + } + + return true; +} + +bool create_memdump_from_remote_image(uptr base, std::vector& buf) +{ + std::vector headers(0x1000); + + if(!gmm->read(headers.data(), base, headers.size())) + return false; + + auto image = headers.data(); + auto dh = reinterpret_cast(image); + auto nh = reinterpret_cast(BASE_OF(image) + dh->e_lfanew); + + if(dh->e_magic != IMAGE_DOS_SIGNATURE || nh->Signature != IMAGE_NT_SIGNATURE) + return false; + + auto length = nh->OptionalHeader.SizeOfImage; + if(length < 1) + return false; + + buf.resize( length ); + return gmm->read(buf.data(), base, buf.size()); +} + +bool resolve_winapis() +{ + const auto handle_kernel32 = []() + { + std::vector kernel32_dump; + if (!create_memdump_from_remote_image(gctx->m_kernel32_base, kernel32_dump)) + { + safeLog_msg("failed to read kernel32 headers."); + return false; + } + + auto image = kernel32_dump.data(); + auto dh = (PIMAGE_DOS_HEADER)image; + if (dh->e_magic != IMAGE_DOS_SIGNATURE) + { + safeLog_msg("invalid kernel32 DOS header."); + return false; + } + + auto nh = reinterpret_cast(BASE_OF(image) + dh->e_lfanew); + if (nh->Signature == IMAGE_NT_SIGNATURE) + { + gctx->m_lctx.winapis.create_file = pe::find_export_address_virt(BASE_OF(image), dh, nh, "CreateFileA") - BASE_OF(image) + gctx->m_kernel32_base; + gctx->m_lctx.winapis.get_file_size = pe::find_export_address_virt(BASE_OF(image), dh, nh, "GetFileSize") - BASE_OF(image) + gctx->m_kernel32_base; + gctx->m_lctx.winapis.close_handle = pe::find_export_address_virt(BASE_OF(image), dh, nh, "CloseHandle") - BASE_OF(image) + gctx->m_kernel32_base; + gctx->m_lctx.winapis.read_file = pe::find_export_address_virt(BASE_OF(image), dh, nh, "ReadFile") - BASE_OF(image) + gctx->m_kernel32_base; + + safeLog_msg("[winapis] CreateFile: %p", gctx->m_lctx.winapis.create_file); + safeLog_msg("[winapis] GetFileSize: %p", gctx->m_lctx.winapis.get_file_size); + safeLog_msg("[winapis] CloseHandle: %p", gctx->m_lctx.winapis.close_handle); + safeLog_msg("[winapis] ReadFile: %p", gctx->m_lctx.winapis.read_file); + } + + return true; + }; + + const auto handle_ntdll = []() + { + std::vector dump; + if (!create_memdump_from_remote_image(gctx->m_ntdll_base, dump)) + { + safeLog_msg("failed to read NTDLL headers."); + return false; + } + + auto image = dump.data(); + auto dh = (PIMAGE_DOS_HEADER)image; + if (dh->e_magic != IMAGE_DOS_SIGNATURE) + { + safeLog_msg("invalid NTDLL DOS header."); + return false; + } + + auto nh = reinterpret_cast(BASE_OF(image) + dh->e_lfanew); + if (nh->Signature == IMAGE_NT_SIGNATURE) + { + gctx->m_lctx.winapis.capture_stack_back_trace = pe::find_export_address_virt(BASE_OF(image), dh, nh, "RtlCaptureStackBackTrace") - BASE_OF(image) + gctx->m_ntdll_base; + + safeLog_msg("[winapis] CaptureStackBackTrace: %p", gctx->m_lctx.winapis.capture_stack_back_trace); + } + + return true; + }; + + + if (!handle_kernel32()) + return false; + + if(!handle_ntdll()) + return false; + + return true; +} + +template +bool __verbose_assign(const char *caller, const char *name, T &dst, uptr value) +{ + if (value == 0) + { + safeLog_msg("%s -> failed to find %s!", caller, name); + return false; + } + + dst = value; + + safeLog_msg("%s -> resolved %s to %p", caller, name, value); + return true; +}; + +#define XVERBOSE_ASSIGN(NAME, TYPE, DST, VAL) __verbose_assign(__FUNCTION__, NAME, DST, VAL) +#define XVERBOSE_RESOLVE(NAME, TYPE, DST) XVERBOSE_ASSIGN(NAME, TYPE, DST, image.find_symbol(NAME)) + +u8 *lcrt_resolve_math_wrapper1(void* image_ptr, LoaderContext& lctx, u8* head) +{ + auto curr = head; + bool found = false; + + while ((curr - head) < 32) + { + if (curr[0] == 0x48 && curr[1] == 0x8D) + { + found = true; + break; + } + + ++curr; + } + + if (!found) + { + safeLog_msg("failed to analyze sinf32 (1)."); + return nullptr; + } + + auto resolved_addr = mem::resolve_operandof_lea(curr); + if (!resolved_addr) + { + safeLog_msg("failed to analyze sinf32 (2)."); + return nullptr; + } + + return reinterpret_cast(resolved_addr); +} + +bool mapper::resolve_linux_crt() +{ + safeLog_msg("resolving the linux CRT..."); + + auto &lctx = gctx->m_lctx; + + const auto handle_libcpp = [&]() + { + auto image = ELFImage("/lib/libc.so.6"); + auto image_ptr = image.buf(); + + auto scn_text = image.find_section(".text"); + auto scn_text_begin = BASE_OF(image_ptr) + scn_text->addr; + + safeLog_msg("libc TEXT (%llx, L%llx)", scn_text->addr, scn_text->length); + + auto libc_syscall_gadget = FIND_SIG(BASE_OF(image_ptr), + scn_text_begin, scn_text->length, "libc_syscall_gadget_offset", "0F 05"); + gctx->m_libc_syscall_gadget_offset = libc_syscall_gadget; + if (!libc_syscall_gadget) + return false; + + if (!XVERBOSE_RESOLVE("fopen", u32, lctx.linuxcrt_cpp.fopen)) + return false; + if (!XVERBOSE_RESOLVE("fputs", u32, lctx.linuxcrt_cpp.fputs)) + return false; + if (!XVERBOSE_RESOLVE("fclose", u32, lctx.linuxcrt_cpp.fclose)) + return false; + if (!XVERBOSE_RESOLVE("sprintf", u32, lctx.linuxcrt_cpp.sprintf)) + return false; + if (!XVERBOSE_RESOLVE("sleep", u32, lctx.linuxcrt_cpp.sleep)) + return false; + + return true; + }; + + const auto handle_libmath = [&]() + { + auto image = ELFImage("/lib/libm.so.6"); + auto image_ptr = image.buf(); + + auto scn_text = image.find_section(".text"); + auto scn_text_begin = BASE_OF(image_ptr) + scn_text->addr; + safeLog_msg("libm TEXT (%llx, L%llx)", scn_text->addr, scn_text->length); + + if (!XVERBOSE_RESOLVE("acosf", u32, lctx.linuxcrt_math.acosf)) + return false; + if (!XVERBOSE_RESOLVE("atan2f", u32, lctx.linuxcrt_math.atan2f)) + return false; + if (!XVERBOSE_RESOLVE("fabsf", u32, lctx.linuxcrt_math.fabsf)) + return false; + + if (XVERBOSE_RESOLVE("sinf32", u32, lctx.linuxcrt_math.sinf)) + { + auto head = reinterpret_cast(BASE_OF(image_ptr) + lctx.linuxcrt_math.sinf); + auto resolved_addr = lcrt_resolve_math_wrapper1(image_ptr, lctx, head); + auto resolved_rva = resolved_addr - BASE_OF(image_ptr); + if(!resolved_addr) + { + safeLog_msg("failed to analyze sinf32 (2)."); + return false; + } + + safeLog_msg("DYN_sinf: %p", resolved_rva); + } else + return false; + + if (XVERBOSE_RESOLVE("cosf32", u32, lctx.linuxcrt_math.cosf)) + { + auto head = reinterpret_cast(BASE_OF(image_ptr) + lctx.linuxcrt_math.cosf); + auto resolved_addr = lcrt_resolve_math_wrapper1(image_ptr, lctx, head); + auto resolved_rva = resolved_addr - BASE_OF(image_ptr); + if (!resolved_addr) + { + safeLog_msg("failed to analyze cosf (2)."); + return false; + } + + safeLog_msg("DYN_cosf: %p", resolved_rva); + } + else + return false; + + return true; + }; + + if (!handle_libcpp()) + { + safeLog_msg("mapper::resolve_linux_crt() -> failed to handle LIBCPP."); + return false; + } + + safeLog_msg("-------------------------"); + + if (!handle_libmath()) + { + safeLog_msg("mapper::resolve_linux_crt() -> failed to handle LIBMATH."); + return false; + } + + return true; +} + +#include + +int aob_map_file(const char* file_path, void** mapped_data, off_t* file_size) { + int fd = open(file_path, O_RDONLY); + if (fd == -1) { + safeLog_msg("Error opening file"); + return 1; + } + + struct stat file_stat; + if (fstat(fd, &file_stat) == -1) { + close(fd); + safeLog_msg("Error getting file size"); + return 1; + } + + *file_size = file_stat.st_size; + + *mapped_data = mmap(NULL, *file_size, PROT_READ, MAP_SHARED, fd, 0); + if (*mapped_data == MAP_FAILED) { + close(fd); + safeLog_msg("Error mapping file into memory"); + return 1; + } + + close(fd); + return 0; +} + +bool mapper::setup_pre() +{ + if (gctx->m_opts.analysis_from_dump) + { + /* !! MUST BE A VIRTUAL DUMP !! */ + void* gsnap = nullptr; + off_t gsnap_len = 0; + + if (!gctx->m_opts.analysis_from_dump_vec) + { + if (aob_map_file(gctx->m_opts.analysis_from_dump_path.c_str(), &gsnap, &gsnap_len) != 0) + { + safeLog_msg("failed to read game snapshot."); + return false; + } + } + else + { + gsnap = gctx->m_opts.vec_analysis_from_dump.data(); + gsnap_len = gctx->m_opts.vec_analysis_from_dump.size(); + } + + auto gsnap_base = BASE_OF(gsnap); + auto dh = reinterpret_cast(gsnap); + auto nh = reinterpret_cast(gsnap_base + dh->e_lfanew); + + if (dh->e_magic != IMAGE_DOS_SIGNATURE) + { + safeLog_msg("failed to very SIG_DOS for game snapshot."); + return false; + } + + if (nh->Signature != IMAGE_NT_SIGNATURE) + { + safeLog_msg("failed to verify SIG_NT for game snapshot"); + return false; + } + + auto sec_cs = pe::find_section_base_virt(gsnap, ".text"); + auto cs_base = gsnap_base + sec_cs->VirtualAddress; + auto cs_len = sec_cs->Misc.VirtualSize; + + auto lctx = &gctx->m_lctx; + + if (!respawn::collect_offsets(gctx, lctx, gsnap_base, cs_base, cs_len, true)) + { + safeLog_msg("failed to collect respawn offsets!"); + return false; + } + } + + gctx->m_strtbl = string_table::Create({ + "ANON", + "VIS", + " FAR", + " OUL", + "L", + "R", + "RCO", + " MDS", + " STR", + "AA", + " SLO", + " SLOSTR", + " ROR", + " FOV", + " DZ", + "RAO", + "Build ID: %s", + "%s | %s", + "%s (%.1f)", + "%s (%.2f)", + "%s (%.3f)", + "%s (%.4f)", + "%s (%.0f)", + "TGB", + }); + + auto strtbl_length = gctx->m_strtbl->table_total_length; + +#if !defined(R5I_PROD) + string_table::Dump_records(gctx->m_strtbl); +#endif + + string_table::Encrypt(gctx->m_strtbl); + +#if !defined(R5I_PROD) + string_table::Dump_contents(gctx->m_strtbl, strtbl_length); +#endif + + return true; +} + +bool mapper::setup(int pid) +{ + gctx->m_pid = pid; + gctx->m_game_base = tools::get_mapping_base(pid, MAPPING_NAME); + if (!gctx->m_game_base) + return false; + + safeLog_msg("base is %p", gctx->m_game_base); + + gctx->m_libc_base = tools::get_mapping_base(pid, _XS("libc.so.6")); + gctx->m_kernel32_base = tools::get_mapping_base(pid, _XS("kernel32.dll")); + gctx->m_ntdll_base = tools::get_mapping_base(pid, _XS("ntdll.dll")); + safeLog_msg("base of libc is %p", gctx->m_libc_base); + safeLog_msg("base of kernel32 is %p", gctx->m_kernel32_base); + safeLog_msg("base of ntdll is %p", gctx->m_ntdll_base); + + if (!resolve_winapis()) + { + safeLog_msg("failed to resolve winAPIs."); + return false; + } + + if (!pe_dump_image(pid, gctx->m_game_base, &gctx->m_game_len)) + { + safeLog_msg("failed to dump the PE image."); + return false; + } + + auto game_sec_cs = gctx->m_game_secs.snap_cs; + auto game_cs_base = BASE_OF(game_sec_cs.data()); + auto game_cs_len = game_sec_cs.size(); + + safeLog_msg("game_cs_base -> %lx L%lx", game_cs_base, game_cs_len); + + auto lctx = &gctx->m_lctx; + + lctx->magic = 'BY'; + lctx->game_base = gctx->m_game_base; + lctx->game_len = gctx->m_game_len; + + auto game_sec_hdr_rdata = gctx->m_game_secs.get_sec_hdr(".rdata"); + if(!game_sec_hdr_rdata.VirtualAddress || game_sec_hdr_rdata.Misc.VirtualSize < 1) + { + safeLog_msg("failed to find game RDATA section HDR."); + return false; + } + lctx->game_rdata_base = gctx->m_game_base + game_sec_hdr_rdata.VirtualAddress; + lctx->game_rdata_len = game_sec_hdr_rdata.Misc.VirtualSize; + + auto game_sec_hdr_cs = gctx->m_game_secs.get_sec_hdr(".text"); + if(!game_sec_hdr_cs.VirtualAddress || game_sec_hdr_cs.Misc.VirtualSize < 1) + { + safeLog_msg("failed to find game TEXT section HDR."); + return false; + } + lctx->game_cs_base = gctx->m_game_base + game_sec_hdr_cs.VirtualAddress; + lctx->game_cs_len = game_sec_hdr_cs.Misc.VirtualSize; + + auto game_sec_hdr_data = gctx->m_game_secs.get_sec_hdr(".data"); + if(!game_sec_hdr_data.VirtualAddress || game_sec_hdr_data.Misc.VirtualSize < 1) + { + safeLog_msg("failed to find game DATA section HDR."); + return false; + } + lctx->game_data_base = gctx->m_game_base + game_sec_hdr_data.VirtualAddress; + lctx->game_data_len = game_sec_hdr_data.Misc.VirtualSize; + + lctx->libc_base = gctx->m_libc_base; + lctx->libm_base = tools::get_mapping_base(pid, _XS("libm.so.6")); + + if (!gctx->m_opts.analysis_from_dump) + { + if (!respawn::collect_offsets(gctx, lctx, gctx->m_game_base, game_cs_base, game_cs_len)) + { + safeLog_msg("failed to collect respawn offsets!"); + return false; + } + } + + if (gctx->m_opts.debug_sigs) + { + safeLog_msg("!! sig debug mode is enabled, quitting now."); + exit(0); + } + + return true; +} + +bool mapper::fetch_image(std::vector& buf) +{ + auto image = buf.data(); + + auto dh = (PIMAGE_DOS_HEADER)image; + auto nh = reinterpret_cast(BASE_OF(image) + dh->e_lfanew); + + if (dh->e_magic != IMAGE_DOS_SIGNATURE || nh->Signature != IMAGE_NT_SIGNATURE) + { + safeLog_msg("fetch-image: invalid headers!"); + return false; + } + + gctx->m_payload.entry = nh->OptionalHeader.AddressOfEntryPoint; + gctx->m_payload.length = nh->OptionalHeader.SizeOfImage; + safeLog_msg("payload entry -> 0x%x", gctx->m_payload.entry); + safeLog_msg("payload len -> 0x%x", gctx->m_payload.length); + + auto sh = IMAGE_FIRST_SECTION64(nh); + + for (u16 i = 0; i < nh->FileHeader.NumberOfSections; i++) + { + auto sec = &sh[i]; + auto name = (char *)sec->Name; + + bool add = false; + + if (!sec->PointerToRawData && !sec->SizeOfRawData) + { + safeLog_msg("skipping an empty section %s", name); + continue; + } + + if (strcmp(name, ".text") == 0 || strcmp(name, ".data") == 0 || strcmp(name, ".rdata") == 0) + add = true; + + if (!add) + continue; + + safeLog_msg(" %i added to copy list -> %s (%llx, %x)", i, name, sec->PointerToRawData, sec->Misc.VirtualSize); + gctx->m_payload.sections.push_back(*sec); + } + + gctx->m_hooks.fn_init = pe::find_export_address(BASE_OF(image), dh, nh, "_kb", false) - BASE_OF(image); + safeLog_msg("** _kb -> %llx", gctx->m_hooks.fn_init); + + gctx->m_payload.ptr_new_loader_ctx = mm::aob_search_buf_single_u64(image, buf.size(), 0xAAAAAAAAAAAAAAAC); + if (!gctx->m_payload.ptr_new_loader_ctx) + return false; + safeLog_msg("fetch-image: found new_loader_ctx at %llx", (gctx->m_payload.ptr_new_loader_ctx - BASE_OF(image))); + + // + // After being done with exports, wipe them. + // + if (!wipe_sensitive_data(BASE_OF(image), dh, nh)) + { + safeLog_msg("failed to wipe sensitive payload data."); + return false; + } + + return true; +} + +bool restore_instance_vft(uptr inst, uptr vft_orig) +{ + if (!gmm->twrite(inst, vft_orig)) + { + safeLog_msg("restore_instance_vft: failed to update vft for %p.", inst); + return false; + } + + if (gmm->tread(inst) != vft_orig) + { + safeLog_msg("restore_instance_vft: the new vft base doesn't match with what i wrote for %p.", inst); + return false; + } + + safeLog_msg("restored vft for %p with success, its vft now points to %p", inst, gmm->tread(inst)); + + return true; +} + +#define VFT_INDEX_INIT 4 +bool prepare_vfts() +{ + u64 base_client = gctx->m_game_base + gctx->m_offsets.iface_base_client; + gctx->m_vft_orig_ptrs.base_client = gmm->tread(gctx->m_ix.base_client); + + gctx->m_lctx.offsets.base_client = gctx->m_offsets.iface_base_client; + gctx->m_lctx.vft_orig_ptrs.base_client = gctx->m_vft_orig_ptrs.base_client; + + std::vector base_client_vft_dump, base_client_vft_dump_orig; + + if (!make_remote_vft(base_client, gctx->m_vfts_remote.base_client, base_client_vft_dump, base_client_vft_dump_orig, + { + {VFT_INDEX_INIT, gctx->m_shell_base + gctx->m_hooks.fn_init}, + })) + return false; + + gctx->m_lctx.origs.init = base_client_vft_dump_orig[VFT_INDEX_INIT]; + + safeLog_msg("base_client: init_func orig is at %p", gctx->m_lctx.origs.init); + safeLog_msg("base_client: setting the init_func handler to %lx", gctx->m_shell_base + gctx->m_hooks.fn_init); + safeLog_msg("wrote vft OK for base_client."); + + return true; +} + +bool place_vfts() +{ + if (!gmm->twrite(gctx->m_ix.base_client, gctx->m_vfts_remote.base_client)) + { + safeLog_msg("failed to update vft for base_client!"); + return false; + } + + return true; +} + +bool restore_vfts() +{ + if (!restore_instance_vft(gctx->m_ix.base_client, gctx->m_vft_orig_ptrs.base_client)) + { + safeLog_msg("failed to restore vft for base_client!"); + return false; + } + + return true; +} + +bool copy_image_sections_to_remote(void *local_image, u64 shell_base) +{ + for (const auto &sec : gctx->m_payload.sections) + { + u64 copy_addr = BASE_OF(local_image) + sec.PointerToRawData; + u64 dest_addr = shell_base + sec.VirtualAddress; + u64 copy_len = sec.SizeOfRawData; + + safeLog_msg(" - copying %s from %p into %p.", sec.Name, copy_addr, dest_addr); + + if (!gmm->write(dest_addr, PTR_OF(copy_addr), copy_len)) + { + safeLog_msg(" copy ERR."); + return false; + } + + if (strcmp((char *)sec.Name, ".text") == 0) + { + safeLog_msg(" protecting code."); + + if (!pt::mprotect(dest_addr, sec.Misc.VirtualSize, PROT_READ | PROT_EXEC)) + { + safeLog_msg("failed protect (code)."); + return false; + } + } + } + + return true; +} + +bool perform_mapping(uptr map_addr, u8* local_image) +{ + u64 shell_base = map_addr; + u64 ctx_base = pt::mmap(0, sizeof(LoaderContext), + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + u64 strtbl_base = pt::mmap(0, sizeof(LoaderContext), + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (!shell_base) + { + safeLog_msg("failed alloc (shell)."); + return false; + } + + if (!ctx_base) + { + safeLog_msg("failed alloc (ctx)."); + return false; + } + + if (!strtbl_base) + { + safeLog_msg("failed alloc (strtbl)."); + return false; + } + + if (!gmm->fill(shell_base, 0, gctx->m_payload.length) || !gmm->fill(ctx_base, 0, sizeof(LoaderContext)) || + !gmm->fill(strtbl_base, 0, gctx->m_strtbl->table_total_length)) + { + safeLog_msg("failed to fill memory."); + return false; + } + + gctx->m_shell_base = shell_base; + gctx->m_ctx_base = ctx_base; + gctx->m_strtbl_base = strtbl_base; + + safeLog_msg("remote shell at %p", shell_base); + safeLog_msg("remote ctx at %p", ctx_base); + safeLog_msg("remote strtbl at %p", strtbl_base); + + *(u64 *)gctx->m_payload.ptr_new_loader_ctx = ctx_base; + + if (!copy_image_sections_to_remote(local_image, shell_base)) + { + safeLog_msg("failed to copy image sections to remote."); + return false; + } + + gctx->m_lctx.strtbl = reinterpret_cast(gctx->m_strtbl_base); + + if (!prepare_vfts()) + { + safeLog_msg("failed to prepare VFTs."); + return false; + } + + if (!gmm->write(gctx->m_ctx_base, &gctx->m_lctx, sizeof(gctx->m_lctx))) + { + safeLog_msg("failed to write remote loader context."); + return false; + } + + if (!gmm->write(gctx->m_strtbl_base, gctx->m_strtbl, gctx->m_strtbl->table_total_length)) + { + safeLog_msg("failed to write remote string table."); + return false; + } + + return true; +} + +bool mapper::load(std::vector &buf) +{ + auto local_image = buf.data(); + + // + // Setup Interfaces + // + + gctx->m_ix.base_client = (gctx->m_game_base + gctx->m_offsets.iface_base_client); + + pt::_pid = gctx->m_pid; + pt::_gadget = (gctx->m_libc_base + gctx->m_libc_syscall_gadget_offset); + + if (gctx->m_opts.dump_game_image != true) + { + uptr map_addr = 0; + uptr map_len = 0; + + if (!gctx->m_opts.use_unsafe_memory) + { + uptr compstui_base = 0; + do + { + compstui_base = tools::get_mapping_base(gctx->m_pid, _XS("compstui.dll")); + } while (!compstui_base); + safeLog_msg("compstui at %p", compstui_base); + + map_len = 0xEC40; + map_addr = compstui_base + 0x4000; + } else + { + safeLog_msg("!!!!!! USING UNSAFE MEMORY, ARE YOU FUCKING CRAZY ?! !!!!!!!"); + map_len = gctx->m_payload.length; + + if (!pt::attach(pt::_pid)) + { + safeLog_msg("load-image: PT failed attach."); + return false; + } + + map_addr = pt::mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + safeLog_msg("maplen=%llx", map_len); + safeLog_msg("mapaddr: %p", map_addr); + + if (!pt::detach(pt::_pid)) + { + safeLog_msg("load-image: PT failed detach."); + return false; + } + } + + if (!gmm->fill(map_addr, 0x00, map_len)) + { + safeLog_msg("load-image: failed fill."); + return false; + } + + if (map_len < gctx->m_payload.length) + { + safeLog_msg("load-image: space is too small for payload."); + return false; + } + + do + { + safeLog_msg("waiting for valid bc..."); + } while (gmm->tread(gctx->m_ix.base_client) == 0); + + if (!pt::attach(pt::_pid)) + { + safeLog_msg("load-image: PT failed attach."); + return false; + } + + perform_mapping(map_addr, local_image); + + if (!pt::detach(pt::_pid)) + { + safeLog_msg("load-image: PT failed detach."); + return false; + } + + if (!place_vfts()) + { + safeLog_msg("failed to place vfts."); + return false; + } + safeLog_msg("vft have been placed."); + } + else + { + auto game_sec_cs = gctx->m_game_secs.snap_cs; + auto game_sec_cs_ptr = game_sec_cs.data(); + auto game_sec_cs_len = game_sec_cs.size(); + + if (!index_game_build( + gctx->m_pid, + BASE_OF(gctx->m_snap_game.data()), + gctx->m_snap_game.size(), + BASE_OF(game_sec_cs_ptr), + game_sec_cs_len)) + { + safeLog_msg("failed to index game build!"); + return false; + } + } + + return true; +} diff --git a/loader/src/mm.cpp b/loader/src/mm.cpp new file mode 100644 index 0000000..58d9712 --- /dev/null +++ b/loader/src/mm.cpp @@ -0,0 +1,87 @@ +#include "inc/include.h" + +MemoryManager* gmm; + +u64 mm::detail::aob_search_buf_single(void *pHayStack, void *pNeedle, size_t uHayStackLength, size_t uNeedleLength) +{ + if (pHayStack == nullptr || pNeedle == nullptr) + return 0; + + if (uHayStackLength <= 0 || uNeedleLength <= 0) + return 0; + + for (auto *pBuffer = static_cast(pHayStack); uHayStackLength >= uNeedleLength; ++pBuffer, --uHayStackLength) + { + if (memcmp(pBuffer, pNeedle, uNeedleLength) == 0) + return reinterpret_cast(pBuffer); + } + + return 0; +} + +char mm::detail::vm_read(int pid, void* dst, u64 src, u64 len) +{ + struct iovec local[1]; + struct iovec remote[1]; + + local[0].iov_len = len; + remote[0].iov_len = len; + + local[0].iov_base = dst; + remote[0].iov_base = PTR_OF(src); + + size_t rs = process_vm_readv( + pid, + local, 1, + remote, 1, + 0 + ); + + if(rs == -1) + { + //msg("vm_read failed: %i", errno); + return 0; + } + + return (rs > 0); +} + +char mm::detail::vm_write(int pid, u64 dst, const void* src, u64 len) +{ + struct iovec local[1]; + struct iovec remote[1]; + + local[0].iov_len = len; + remote[0].iov_len = len; + + local[0].iov_base = (void*)src; + remote[0].iov_base = PTR_OF(dst); + + size_t rs = process_vm_writev( + pid, + local, 1, + remote, 1, + 0 + ); + + if(rs == -1) + { + //msg("vm_read failed: %i", errno); + return 0; + } + + return (rs > 0); +} + +bool MemoryManager::read(void* dst, u64 src, size_t len) +{ + if(!mm::detail::vm_read(m_pid, dst, src, len)) return false; + return true; +} + +bool MemoryManager::write(u64 dst, const void* src, size_t len) +{ + if(!mm::detail::vm_write(m_pid, dst, src, len)) return false; + return true; +} + diff --git a/loader/src/netsdk.cpp b/loader/src/netsdk.cpp new file mode 100644 index 0000000..7c33d61 --- /dev/null +++ b/loader/src/netsdk.cpp @@ -0,0 +1,250 @@ +#include "inc/include.h" + +NetSDK *gNetSDK = nullptr; + +bool NetSDK::start() +{ + int clientSocket = socket(AF_INET, SOCK_STREAM, 0); + if (clientSocket == -1) + { + return false; + } + + struct sockaddr_in serverAddress; + serverAddress.sin_family = AF_INET; + serverAddress.sin_port = htons(m_port); + inet_pton(AF_INET, m_hostname.c_str(), &serverAddress.sin_addr); + + if (connect(clientSocket, reinterpret_cast(&serverAddress), sizeof(serverAddress)) == -1) + { + close(clientSocket); + return false; + } + + m_sck = clientSocket; + + return true; +} + +bool NetSDK::handshake() +{ + proto::Packet_header_t hdr; + std::vector contents; + if (!proto::read_packet(m_sck, hdr, contents)) + return false; + + auto pack = reinterpret_cast(contents.data()); + + memcpy(m_server_pk, pack->public_key, sizeof(m_server_pk)); + + crypto_kx_keypair(m_client_pk, m_client_sk); + + if (crypto_kx_client_session_keys(m_client_rx, m_client_tx, + m_client_pk, m_client_sk, pack->public_key) != 0) + { + msg("networking error: CE001."); + return false; + } + + proto::msg::Client_hello_t hello; + memset(&hello, 0, sizeof(hello)); + hello.version = 100; + memcpy(hello.public_key, m_client_pk, sizeof(hello.public_key)); + strcpy(hello.compilation_aes_key, STRINGIFIED_DEF(_LOADER_COMPILATION_AES_KEY)); + strcpy(hello.build_timestamp, STRINGIFIED_DEF(_BUILD_TIMESTAMP)); + if (!proto::send_packet(m_sck, proto::PACKET_ID_CLIENT_HELLO, &hello, sizeof(hello))) + { + msg("networking error: CE002."); + return false; + } + + return true; +} + +bool NetSDK::wait_for_activation() +{ + proto::Packet_header_t hdr; + std::vector vec_contents; + if (proto::read_packet_sealed(m_sck, hdr, vec_contents, m_client_pk, m_client_sk)) + { + // tools::hex_dump(vec_contents.data(), vec_contents.size()); + + if (strcmp((char *)vec_contents.data(), "activated!") == 0) + { + return true; + } else + { + printf("%s\n", (char *)vec_contents.data()); + } + } + + return false; +} + +bool NetSDK::ping() +{ + proto::msg::Client_Ping_t pack; + pack.syn = true; + pack.tick = tools::curtick(); + m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_PING, &pack, sizeof(pack)); + return true; +} + +bool NetSDK::start_pump() +{ + if(!gNetSsnContext) + { + gNetSsnContext = new NetworkSessionContext_t(); + } + + m_pump = new Pump(m_sck, m_client_pk, m_client_sk, m_server_pk); + if (!m_pump) + return false; + + m_pump->add_read_handler(proto::PACKET_ID_SERVER_RESPONSE_TO_PING, packet_handlers::handle_response_to_ping); + m_pump->add_read_handler(proto::PACKET_ID_SERVER_RESPONSE_TO_AUTH, packet_handlers::handle_response_to_auth); + m_pump->add_read_handler(proto::PACKET_ID_SERVER_RESPONSE_TO_REQUEST_DLC, packet_handlers::handle_response_to_request_dlc); + m_pump->add_read_handler(proto::PACKET_ID_SERVER_DROP_CLIENT, packet_handlers::handle_server_drop_client); + m_pump->add_read_handler(proto::PACKET_ID_SERVER_DATA_TRANSFER, packet_handlers::handle_server_data_transfer); + + return true; +} + +static inline void native_cpuid(unsigned int *eax, unsigned int *ebx, + unsigned int *ecx, unsigned int *edx) +{ + /* ecx is often an input as well as an output. */ + asm volatile("cpuid" + : "=a" (*eax), + "=b" (*ebx), + "=c" (*ecx), + "=d" (*edx) + : "0" (*eax), "2" (*ecx)); +} + +bool NetSDK::authenticate(const std::string &email, const std::string &password) +{ + if(!gHwid->m_available) + { + error("internal error: AW1267"); + return false; + } + + auto dbuf = new DrauseDataBuf(); + if (!dbuf) + return false; + + dbuf->write_string(email); + dbuf->write_string(password); + + // TODO: Add HWID down here. + dbuf->write(gHwid->m_vec_disks.size()); + dbuf->write(gHwid->m_vec_device_macs.size()); + + // construct the list of DISKS first. + for (const auto &rec : gHwid->m_vec_disks) + { + dbuf->write_string(rec.serial_number); // SERIAL + dbuf->write_string(rec.model_name); // VERSION_OFFSET + char dummy[40]; + memset(dummy, 0, sizeof(dummy)); + dbuf->write_data(dummy, sizeof(dummy)); + } + + // afterwards we need to construct the list of NICS to ensure data completeness. + for (const auto &rec : gHwid->m_vec_device_macs) + { + dbuf->write_string(rec.device); // MAC_DESC + dbuf->write_string(rec.mac_address_permanent); // MAC_CURR + dbuf->write_string(rec.mac_address_permanent); // MAC_PERM + } + + /* additionally we can collect some VM checks related data. */ + + // CPUID Capabilities and CPU Info + + unsigned int cpuid_regs[4] = {0, 0, 0, 0}; + + memset(cpuid_regs, 0, sizeof(cpuid_regs)); + cpuid_regs[0] = 0; /* processor vendor */ + native_cpuid(&cpuid_regs[0], &cpuid_regs[1], &cpuid_regs[2], &cpuid_regs[3]); + dbuf->write_data(cpuid_regs, sizeof(cpuid_regs)); + + memset(cpuid_regs, 0, sizeof(cpuid_regs)); + cpuid_regs[0] = 1; /* processor info and feature bits */ + native_cpuid(&cpuid_regs[0], &cpuid_regs[1], &cpuid_regs[2], &cpuid_regs[3]); + dbuf->write_data(cpuid_regs, sizeof(cpuid_regs)); + + // Firmware Tables of SMBIOS + auto vec_smbios_dump = gHwid->dump_smbios_data(); + if(vec_smbios_dump.empty()) + { + error("internal error: AW1268"); + return false; + } + + dbuf->write(vec_smbios_dump.size()); + if (!vec_smbios_dump.empty()) + { + dbuf->write_data_vec(vec_smbios_dump); + } + + auto vec_hwid_buffer = dbuf->tovec(); + + m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_AUTH, vec_hwid_buffer.data(), vec_hwid_buffer.size()); + delete dbuf; + + return true; +} + +bool NetSDK::request_dlc(u32 id) +{ + proto::msg::Request_Dlc_t req; + memset(&req, 0, sizeof(req)); + req.id = id; + req.remote_base = 0; + + gNetSsnContext->requested_dlc_id = id; + m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_REQUEST_DLC, &req, sizeof(req)); + return true; +} + +bool NetSDK::request_asset(u32 id) +{ + gNetSsnContext->requested_asset_id = id; + m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_REQUEST_ASSET, &id, sizeof id); + + return true; +} + +#define CLOUD_LOG_MAX_MESSAGE_BUFFER_LENGTH 2048 +#define CLOUD_LOG_MAX_MESSAGE_LENGTH (CLOUD_LOG_MAX_MESSAGE_BUFFER_LENGTH-100) +void NetSDK::push_message_to_cloud_log(const char* fmt, ...) +{ + va_list args; + va_start(args, fmt); + + va_list args_copy; + va_copy(args_copy, args); + int len = vsnprintf(nullptr, 0, fmt, args_copy) + 1; + va_end(args_copy); + + char *buffer = new char[len]; + + vsnprintf(buffer, len, fmt, args); + + if(strlen(buffer) > CLOUD_LOG_MAX_MESSAGE_LENGTH || len > CLOUD_LOG_MAX_MESSAGE_BUFFER_LENGTH) + return; + + proto::msg::cloud_log_push_message_t req; + memset(&req, 0, sizeof(req)); + + req.severity = 0; // 0 - MESSAGE, 1 - WARNING, 2 - ERROR, 3 - CRITICAL + req.bucket_id = 0x6523; + strcpy(req.contents, buffer); + + delete[] buffer; + va_end(args); + + m_pump->push_packet_to_write(proto::PACKET_ID_CLIENT_PUSH_MESSAGE_TO_CLOUD_LOG, &req, sizeof(req)); +} diff --git a/loader/src/packet-handlers.cpp b/loader/src/packet-handlers.cpp new file mode 100644 index 0000000..cbd5ad4 --- /dev/null +++ b/loader/src/packet-handlers.cpp @@ -0,0 +1,89 @@ +#include "inc/include.h" + +NetworkSessionContext_t *gNetSsnContext = nullptr; + +void packet_handlers::handle_response_to_ping(int sck, proto::Packet_header_t hdr, std::vector vec_contents) +{ + auto presponse = reinterpret_cast(vec_contents.data()); + + if (!presponse->ack) + { + gNetSsnContext->has_server_acknowledged_ping = false; + gNetSsnContext->received_ping = true; + return; + } + + gNetSsnContext->has_server_acknowledged_ping = true; + gNetSsnContext->received_ping = true; +} + +void packet_handlers::handle_response_to_auth(int sck, proto::Packet_header_t hdr, std::vector vec_contents) +{ + auto result = *reinterpret_cast(vec_contents.data()); + + gNetSsnContext->value_authentication_result = (proto::AUTH_STATES)result; + gNetSsnContext->received_authentication_result = true; +} + +void packet_handlers::handle_server_drop_client(int sck, proto::Packet_header_t hdr, std::vector vec_contents) +{ + auto presponse = reinterpret_cast(vec_contents.data()); + + if (strlen(presponse->reason) > 0) + { + printf("dropped: %s\n", presponse->reason); + } + + close(sck); +} + +void packet_handlers::handle_server_data_transfer(int sck, proto::Packet_header_t hdr, std::vector vec_contents) +{ + auto dbuf = new DrauseDataBuf(vec_contents.data(), vec_contents.size()); + if (dbuf) + { + auto id = dbuf->read(); + auto length_compressed = dbuf->read(); + auto length_decompressed = dbuf->read(); + + if (length_compressed > 0 && length_decompressed > 0) + { + std::vector vec_dlc_contents(length_compressed ); + dbuf->read_data(vec_dlc_contents.data(), vec_dlc_contents.size()); + + std::vector vec_dlc_contents_decompressed(length_decompressed ); + vec_dlc_contents_decompressed = lzma::decompress(vec_dlc_contents); + + rotate::decode(vec_dlc_contents_decompressed.data(), vec_dlc_contents_decompressed.size() ); + //tools::hex_dump(vec_dlc_contents_decompressed.data(), 0x1000 ); + + if(id == gNetSsnContext->requested_dlc_id) + { + gNetSsnContext->vec_dlc_contents = vec_dlc_contents_decompressed; + gNetSsnContext->received_dlc = true; + } else if(id == gNetSsnContext->requested_asset_id) + { + gNetSsnContext->vec_asset_contents = vec_dlc_contents_decompressed; + gNetSsnContext->received_requested_asset = true; + } else + { + dbg("unknown file transfer received, with ID %x", id); + } + } + + delete dbuf; + } +} + +void packet_handlers::handle_response_to_request_dlc(int sck, proto::Packet_header_t hdr, std::vector vec_contents) +{ + auto dbuf = new DrauseDataBuf(vec_contents.data(), vec_contents.size()); + if (!dbuf) + { + return; + } + + bool will_send = dbuf->read(); + gNetSsnContext->will_server_send_requested_dlc = will_send; + gNetSsnContext->received_request_dlc_response = true; +} diff --git a/loader/src/pe.cpp b/loader/src/pe.cpp new file mode 100644 index 0000000..75c5044 --- /dev/null +++ b/loader/src/pe.cpp @@ -0,0 +1,194 @@ +#include "inc/include.h" + +/* + * Relocates some section details so that the dump can be analyzed statically. + * IDA and other tools will fuck up if you don't do this on a direct memory dump. + */ +void pe_dump_fix(void *image) +{ + PIMAGE_DOS_HEADER dh = (PIMAGE_DOS_HEADER)image; + PIMAGE_NT_HEADERS64 nh = (PIMAGE_NT_HEADERS64)(BASE_OF(image) + dh->e_lfanew); + + if (dh->e_magic != IMAGE_DOS_SIGNATURE) + { + safeLog_msg("[pe-dump-fix] provided image dump contains invalid DOS header."); + return; + } + + if (nh->Signature != IMAGE_NT_SIGNATURE) + { + safeLog_msg("[pe-dump-fix] provided image dump contains invalid NT header."); + return; + } + + PIMAGE_SECTION_HEADER shdr = IMAGE_FIRST_SECTION64(nh); + u32 scnt = nh->FileHeader.NumberOfSections; + + for (u32 i = 0; i < scnt; i++) + { + PIMAGE_SECTION_HEADER sec = &shdr[i]; + + // msg(" fixing %i:%s", i, sec->Name); + + sec->PointerToRawData = sec->VirtualAddress; + sec->SizeOfRawData = sec->Misc.VirtualSize; + } +} + +bool pe_dump_image(int pid, u64 base, u64* out_len) +{ + IMAGE_DOS_HEADER dh; + ZERO_MEM(&dh, sizeof(dh)); + + if (!mm::detail::vm_read(pid, &dh, base, sizeof(dh))) + { + safeLog_msg("[pe-dump] failed to read DOS header."); + return false; + } + + if (dh.e_magic != IMAGE_DOS_SIGNATURE) + { + safeLog_msg("[pe-dump] invalid DOS signature."); + return false; + } + + IMAGE_NT_HEADERS_GENERIC nhg; + ZERO_MEM(&nhg, sizeof(nhg)); + + if (!mm::detail::vm_read(pid, &nhg, base + dh.e_lfanew, sizeof(nhg))) + { + safeLog_msg("[pe-dump] failed to read NT(G) header."); + return false; + } + + if (nhg.Signature != IMAGE_NT_SIGNATURE) + { + safeLog_msg("[pe-dump] invalid NT(G) signature."); + return false; + } + + if (nhg.FileHeader.Machine != 0x8664 /* AMD64 */) + { + safeLog_msg("[pe-dump] unsupported image arch."); + return false; + } + + IMAGE_NT_HEADERS64 nh; + if (!mm::detail::vm_read(pid, &nh, base + dh.e_lfanew, sizeof(nh))) + { + safeLog_msg("[pe-dump] failed to read NT header."); + return false; + } + + safeLog_msg("[pe-dump] seems valid, nos: %i sizeofimg: %llx", + nh.FileHeader.NumberOfSections, + nh.OptionalHeader.SizeOfImage); + + *out_len = nh.OptionalHeader.SizeOfImage; + + if (gctx->m_opts.dump_game_image) + { + // dump full game image, for analysis. + size_t dump_len = nh.OptionalHeader.SizeOfImage; + void *dump = malloc(dump_len); + + if (dump) + { + ZERO_MEM(dump, dump_len); + + if (mm::detail::vm_read(pid, dump, base, dump_len)) + { + pe_dump_fix(dump); + safeLog_msg("[pe-dump] captured dump into memory."); + + auto &snap = gctx->m_snap_game; + snap.resize(dump_len); + memcpy(snap.data(), dump, snap.size()); + } + else + safeLog_msg("[pe-dump] failed to capture dump into memory."); + + free(dump); + } + } + + // only dump necessary sections. + auto sec_hdr_base = (base + dh.e_lfanew + 0x18 + nh.FileHeader.SizeOfOptionalHeader); + auto sec_cnt = nh.FileHeader.NumberOfSections; + auto sec_hdrs_len = sec_cnt * sizeof(IMAGE_SECTION_HEADER); + + if (sec_cnt > 0) + { + auto sec_hdrs = std::vector(sec_cnt); + + if (!mm::detail::vm_read(pid, sec_hdrs.data(), sec_hdr_base, sec_hdrs_len)) + { + safeLog_msg("[pe-dump] failed to dump section header table."); + return false; + } + + gctx->m_game_secs.table = sec_hdrs; + + safeLog_msg("[pe-dump] dumped section header table."); + + for (const auto &sec : sec_hdrs) + { + auto sname = reinterpret_cast(&sec.Name); + + if (!strcmp(sname, ".text")) + { + auto sbase = (uptr)sec.VirtualAddress; + auto slen = sec.Misc.VirtualSize; + + if (sbase && slen > 0) + { + sbase += base; + + safeLog_msg("[pe-dump] image base= %p", base); + safeLog_msg("[pe-dump] found code section, base=%p len=%llx", sbase, slen); + + auto sdump = std::vector(slen); + + if(!mm::detail::vm_read(pid, sdump.data(), sbase, slen)) + { + safeLog_msg("[pe-dump] failed to read a segment."); + return false; + } + + gctx->m_game_secs.snap_cs = sdump; + } + } + } + + return true; + } + + return false; +} + +PIMAGE_SECTION_HEADER pe::find_section_base_virt(void *image, const char *name) +{ + auto dh = (PIMAGE_DOS_HEADER)image; + auto nh = reinterpret_cast(BASE_OF(image) + dh->e_lfanew); + + if (dh->e_magic != IMAGE_DOS_SIGNATURE || nh->Signature != IMAGE_NT_SIGNATURE) + { + safeLog_msg("fetch-image: invalid headers!"); + return nullptr; + } + + auto sh = IMAGE_FIRST_SECTION64(nh); + + for (u16 i = 0; i < nh->FileHeader.NumberOfSections; i++) + { + auto sec = &sh[i]; + auto name = (char *)sec->Name; + + bool add = false; + + if (strcmp(name, ".text") == 0) + return sec; + } + + return nullptr; +} diff --git a/loader/src/pump.cpp b/loader/src/pump.cpp new file mode 100644 index 0000000..da9c108 --- /dev/null +++ b/loader/src/pump.cpp @@ -0,0 +1,92 @@ +#include "inc/include.h" + +Pump::Pump(int sck, unsigned char *client_pk, unsigned char *client_sk, unsigned char *server_pk) +{ + m_socket = sck; + m_is_activated = true; + + memcpy(m_client_pk, client_pk, sizeof(m_client_pk)); + memcpy(m_client_sk, client_sk, sizeof(m_client_sk)); + memcpy(m_server_pk, server_pk, sizeof(m_server_pk)); + + std::thread(&Pump::packets_read, this).detach(); + std::thread(&Pump::packets_write, this).detach(); +} + +void Pump::add_read_handler(u32 target_packet_id, Pump_read_callback_fn cbk) +{ + Pump_read_handler_t rec; + memset(&rec, 0, sizeof(rec)); + + rec.target_packet_id = target_packet_id; + rec.cbk = cbk; + + m_vec_read_handlers.push_back(rec); +} + +void Pump::push_packet_to_write(u32 id, const std::vector &vec_contents) +{ + Pump_queued_write_packet_t rec; + rec.id = id; + rec.vec_contents = vec_contents; + m_vec_write_queue.push_back(rec); +} + +void Pump::push_packet_to_write(u32 id, const void *data, u32 len) +{ + std::vector vec_temp(len); + memcpy(vec_temp.data(), data, vec_temp.size()); + return push_packet_to_write(id, vec_temp); +} + +void Pump::packets_read() +{ + while (m_is_activated) + { + std::vector vec_contents; + proto::Packet_header_t hdr; + memset(&hdr, 0, sizeof(hdr)); + + if (!proto::read_packet_sealed(m_socket, hdr, vec_contents, m_client_pk, m_client_sk)) + { + m_is_failed = true; + dbg("[pump] read operation has failed!\n"); + break; + } + + // printf("[PUMP-DBG] received packet with ID %i\n", hdr.id); + + for (auto handler : m_vec_read_handlers) + { + if (handler.target_packet_id == hdr.id) + std::thread(handler.cbk, m_socket, hdr, vec_contents).detach(); + } + + std::this_thread::sleep_for(50ms); + } + + m_is_activated = false; +} + +void Pump::packets_write() +{ + while (m_is_activated) + { + if (!m_vec_write_queue.empty()) + { + auto curr = m_vec_write_queue.front(); + if (!proto::send_packet_sealed(m_socket, curr.id, curr.vec_contents.data(), + curr.vec_contents.size(), m_server_pk)) + { + m_is_failed = true; + dbg("[pump] write operation has failed!\n"); + break; + } + m_vec_write_queue.pop_front(); + } + + std::this_thread::sleep_for(50ms); + } + + m_is_activated = false; +} diff --git a/loader/src/respawn.cpp b/loader/src/respawn.cpp new file mode 100644 index 0000000..d868493 --- /dev/null +++ b/loader/src/respawn.cpp @@ -0,0 +1,229 @@ +#include "inc/include.h" + +namespace respawn +{ + namespace find + { + uptr wrap(const char *name, uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap, uptr (*func)(uptr, uptr, uptr, bool)) + { + auto rs = func(game_image_base, game_cs_base, game_cs_len, from_snap); + if (rs) + { + safeLog_msg("[respawn] found offset with name '%s' at %llx", name, rs); + return rs; + } + + safeLog_msg("[respawn] failed to find offset with name '%s'.", name); + return 0; + } + + uptr name_list(uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap) + { + bool fix = false; + uptr game_cs_base_remote = 0; + + if (!from_snap) + { + auto remote_shdr_text = gctx->m_game_secs.get_sec_hdr(".text"); + if (!remote_shdr_text.VirtualAddress) + { + safeLog_msg("failed to grab remote segment header for TEXT."); + return false; + } + + game_cs_base_remote = remote_shdr_text.VirtualAddress; + fix = true; + } + + auto start_addr = offsets::detail::find_ptr(game_cs_base, game_cs_len, + "E8 ? ? ? ? 48 8B CB 48 8B F8 E8 ? ? ? ? 4C 8D 05", 1, 5); + + if (start_addr) + { + uptr addr = start_addr; + auto disasm = hde::disasm_buffer_ex((void *)start_addr, MAX_INSTRUCTION_LEN * 20); + if (!disasm.empty()) + { + for (auto it = disasm.begin(); it != disasm.end(); ++it) + { + const auto &ins = *it; + + if (ins.opcode == 0x8D && ins.opcode2 == 0x00) + { + auto imm = *(u32 *)(&reinterpret_cast(addr)[3]); + auto rs = (imm + addr + ins.len) - (fix ? game_cs_base : game_image_base); + return fix ? (offsets::fix_scn_off(PTR_OF(game_image_base), game_cs_base_remote, rs) - game_image_base) : rs; + } + + addr += ins.len; + } + } + } + return 0; + } + + uptr player_view_offset(uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap) + { + auto start_addr = FIND_SIG(game_image_base, game_cs_base, game_cs_len, "find::player_view_offset::start_addr", "48 83 EC 38 8B 05 ? ? ? ? 0F 29 74 24 ? 83 F8 FF 74"); + if (start_addr) + { + start_addr += game_image_base; + uptr addr = start_addr; + auto disasm = hde::disasm_buffer_ex((void *)start_addr, MAX_INSTRUCTION_LEN * 20); + if (!disasm.empty()) + { + for (const auto &ins : disasm) + { + if (ins.opcode == 0x0F && ins.opcode2 == 0x10) + { + auto result = *reinterpret_cast(addr + 5); + return result; + } + + addr += ins.len; + } + } + } + return 0; + } + + uptr player_view_angle(uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap) + { + auto start_addr = FIND_PTR(game_image_base, game_cs_base, game_cs_len, "find::player_view_angle::start_addr", "E8 ? ? ? ? 80 BB ? ? ? ? ? 74 71 8B 83", 1, 5); + if (start_addr) + { + start_addr += game_image_base; + uptr addr = start_addr; + auto disasm = hde::disasm_buffer_ex((void *)start_addr, MAX_INSTRUCTION_LEN * 20); + if (!disasm.empty()) + { + for (auto it = disasm.begin(); it != disasm.end(); ++it) + { + const auto &ins = *it; + const hde64s *pnext_ins = nullptr; + + if (it + 1 != disasm.end()) + { + const auto &next_ins = *(it + 1); + pnext_ins = &next_ins; + } + + if (pnext_ins != nullptr) + { + if (ins.opcode == 0x0F && ins.opcode2 == 0x11) + { + auto rs = *reinterpret_cast(addr + 4); + return rs; + } + } + + addr += ins.len; + } + } + } + return 0; + } + + } + + bool collect_offsets(Context *ctx, LoaderContext *lctx, uptr game_image_base, uptr game_cs_base, uptr game_cs_len, bool from_snap) + { + if(gctx->m_opts.dump_game_image && !gctx->m_opts.debug_sigs) return true; + + bool fix = false; + uptr game_cs_base_remote = 0; + + if (!from_snap) + { + auto remote_shdr_text = gctx->m_game_secs.get_sec_hdr(".text"); + if (!remote_shdr_text.VirtualAddress) + { + safeLog_msg("failed to grab remote segment header for TEXT."); + return false; + } + game_cs_base_remote = remote_shdr_text.VirtualAddress; + fix = true; + } + + // + // Grab Offsets + // + gctx->m_offsets.iface_base_client = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, + "base_client", "48 8B 05 ? ? ? ? 48 8D 15 ? ? ? ? 48 89 0D ? ? ? ? FF 10 48 8B 0D", 3, 7, fix); + + // + // Interfaces & Variables + // + lctx->offsets.entity_list = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "entity_list", "48 8D 15 ? ? ? ? 48 C1 E0 05 C1 E9 10 39 4C 10 08 75 34", 3, 7, fix); + lctx->offsets.signon_state = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "signon_state", "48 63 05 ? ? ? ? 4C 8D 0D ? ? ? ? 4D 8B 0C C1", 3, 7, fix); + lctx->offsets.input_system = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "input_system", "48 8D 0D ? ? ? ? 85 FD 74 13 41 3B DE BA ? ? ? ? 41 0F 45 D7", 3, 7, fix); + lctx->offsets.mp_gamemode = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "mp_gamemode", "48 8B 0D ? ? ? ? 49 8B D6 48 8B 01 FF 50 70", 3, 7, fix); + lctx->offsets.global_vars = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "global_vars", "48 8B 05 ? ? ? ? F3 0F 58 70 ? 0F 28 C6", 3, 7, fix); + lctx->offsets.game_movement = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "game_movement", "48 8B 05 ? ? ? ? FF 50 20 48 8B 0D ? ? ? ? 4C 8D 3D", 3, 7, fix); + lctx->offsets.map_name = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "map_name", "4C 8D 3D ? ? ? ? 49 C7 C0 ? ? ? ? 0F 1F 44 00 ? 49 FF C0 43 80 3C 07", 3, 7, fix); + lctx->offsets.mat_system_surface = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "mat_system_surface", "48 8B 0D ? ? ? ? 8B 96 ? ? ? ? 48 8B 01 FF 50 68", 3, 7, fix); + lctx->offsets.view_render = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "view_render", "48 8B 1D ? ? ? ? 48 8B 48 60 B8 ? ? ? ? 48 8B 51 10 66 39 02 75 58 48 63 4A 3C B8 ? ? ? ? 48 03 CA 66 39", 3, 7, fix); + lctx->offsets.client_state = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "client_state", "48 8D 05 ? ? ? ? 33 D2 48 69 C9 ? ? ? ? 48 03 C8", 3, 7, fix); + lctx->offsets.client_mode = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "client_mode", "48 8B 0D ? ? ? ? 48 8B 01 FF 50 68 84 C0", 3, 7, fix); + lctx->offsets.kwewed_render_ctx = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "kwewed_render_ctx", "48 89 05 ? ? ? ? 48 8D 1D ? ? ? ? 48 8D 05 ? ? ? ? 66 0F 7F 05", 3, 7, fix); + lctx->offsets.rs_cvar_gamepad_checks_enabled = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "rs_cvar_gamepad_checks_enabled", "48 8B 05 ? ? ? ? 48 8B DA 48 8B F1 83 78", 3, 7, fix); + lctx->offsets.rs_gamepad_checks_last = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "rs_gamepad_checks_last", "F2 0F 11 0D ? ? ? ? 48 89 1D ? ? ? ? 89 1D ? ? ? ? 88 1D ? ? ? ? 48 89 1D ? ? ? ? 88 1D ? ? ? ? 48 83 C4", 4, 8, fix); + lctx->offsets.tag_list = FIND_PTR_LAJR(game_cs_base, game_cs_len, game_image_base, game_cs_base_remote, "tag_list", "48 8D 05 ? ? ? ? 48 8B 44 C8 ? 48 8B CF 48 85 C0 48 0F 45 D0 E8", 3, 7, fix); + + lctx->offsets.vft_caves.generic_1 = FIND_PTR_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "vft_caves_generic_1", "48 8D 05 ? ? ? ? 48 8D 04 C8 C3", 3, 7, fix); + + lctx->offsets.client_class_head = FIND_PTR_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "client_class_head", "4C 8B 1D ? ? ? ? 4D 85 DB 74 1D 0F 1F 40 00 49 8B 4B 10 48 8B D3", 3, 7, fix); + + lctx->offsets.string_dict_user_names = FIND_PTR_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "string_dict_user_names", "48 8B 0D ? ? ? ? 48 85 C9 0F 84 ? ? ? ? 48 8B 01 45 33 C0 FF 90", 3, 7, fix); + + // + // Functions + // + lctx->offsets.fns.get_mem_alloc = FIND_PTR_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, + "fns.get_mem_alloc", + "E8 ? ? ? ? 48 89 05 ? ? ? ? 4C 8B 00 BA ? ? ? ? 48 8B C8 41 FF 50 08 4C 8B 05", + 1, + 5, + fix); + + lctx->offsets.fns.mss_draw_filled_rect = FIND_PTR_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "fns.mss_draw_filled_rect", "E8 ? ? ? ? 8B B4 24 ? ? ? ? 44 8B C5", 1, 5, fix); + + lctx->offsets.fns.math_ang2vec = FIND_OFF_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "fns.math_ang2vec", + "48 8B C4 48 81 EC ? ? ? ? F3 0F 10 05 ? ? ? ? F3 0F 10 69 ? F3 0F 59 2D", fix); + lctx->offsets.fns.math_vec2ang = FIND_OFF_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "fns.math_vec2ang", + "48 83 EC 68 F3 0F 10 59 ? F3 0F 10 11 0F 28 C3 0F 29 74 24 ? 0F 29 7C 24", fix); + // + // Dynamic Offsets + // + lctx->offsets.name_list = find::wrap("name_list", game_image_base, game_cs_base, game_cs_len, from_snap, find::name_list); + lctx->offsets.player.view_offset = find::wrap("player::view_offset", game_image_base, game_cs_base, game_cs_len, from_snap, find::player_view_offset); + lctx->offsets.player.view_angle = find::wrap("player::view_angle", game_image_base, game_cs_base, game_cs_len, from_snap, find::player_view_angle); + + // + // Return Addresses + // + lctx->offsets.ret_addrs.mkrctx_unk1 = FIND_OFF_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "ret_addrs.mkrctx_unk1", + "FF 90 ? ? 00 00 48 ? ? 48 ? ? C6 05 ? ? ? ? 00 0F", fix); + if(lctx->offsets.ret_addrs.mkrctx_unk1 != 0) lctx->offsets.ret_addrs.mkrctx_unk1 += 6; + + lctx->offsets.ret_addrs.create_move = FIND_OFF_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "ret_addrs.create_move", + "FF 15 ? ? ? ? 0F B7 F8 85 FF 0F 84 ? ? ? ? 44 8D 0C FD ? ? ? ? 33 C0 45 85 C9 74 3C 44 0F BE 45 ? 48 8D 55 89 B9", fix); + if(lctx->offsets.ret_addrs.create_move != 0) lctx->offsets.ret_addrs.create_move += 6; + + lctx->offsets.ret_addrs.override_mouse = FIND_OFF_LAJR(game_cs_base, game_cs_len, + game_image_base, game_cs_base_remote, "ret_addrs.override_mouse", + "FF 90 08 01 ? ? F3 ? ? ? ? ? ? ? ? 84 C0", fix); + if(lctx->offsets.ret_addrs.override_mouse != 0) lctx->offsets.ret_addrs.override_mouse += 6; + + return true; + } +} diff --git a/loader/src/string-table.cpp b/loader/src/string-table.cpp new file mode 100644 index 0000000..e7fddae --- /dev/null +++ b/loader/src/string-table.cpp @@ -0,0 +1,85 @@ +#include "inc/include.h" + +void string_table::stream_encode(uint8_t *buffer, size_t length, uint64_t key) +{ + for (size_t i = 0; i < length; i++) + { + buffer[i] ^= (key >> (8 * (i % 8))) & 0xFF; + } + + rotate::encode(buffer, length); +} + +void string_table::stream_decode(uint8_t *buffer, size_t length, uint64_t key) +{ + rotate::decode(buffer, length); + + for (size_t i = 0; i < length; i++) + { + buffer[i] ^= (key >> (8 * (i % 8))) & 0xFF; + } +} + +string_table::String_table *string_table::Create(const std::vector &vec_contents) +{ + auto num_strings = vec_contents.size(); + auto table_alloc_base_length = sizeof(String_table) - sizeof(String_record); + auto table_alloc_final_length = table_alloc_base_length + (sizeof(String_record) * num_strings); + auto table = reinterpret_cast(malloc(table_alloc_final_length)); + + if (table) + { + table->table_total_length = table_alloc_final_length; + table->table_records_count = num_strings; + + for (size_t i = 0; i < num_strings; i++) + { + auto curr_table_record = &table->table[i]; + auto curr_string = vec_contents[i]; + + curr_table_record->length = curr_string.size() + 1 /* plus 1 for null terminator */; + curr_table_record->hash = string_hash(curr_string.c_str(), curr_string.size()); + strcpy(curr_table_record->content, curr_string.c_str()); + } + + return table; + } + + return nullptr; +} + +uint64_t gen_rand_u64() +{ + uint64_t randomNum; + RAND_bytes(reinterpret_cast(&randomNum), sizeof(randomNum)); + return randomNum; +} + +void string_table::Dump_records(String_table *table) +{ + for (uint32_t i = 0; i < table->table_records_count; i++) + { + auto record = table->table[i]; + msg("[S] H:%x L%llx '%s'", record.hash, record.length, record.content); + } + + auto table_length = table->table_total_length; +} + +void string_table::Dump_contents(String_table *table, size_t table_len) +{ + tools::hex_dump(table, table_len); +} + +void string_table::Encrypt(String_table *table) +{ + auto key = gen_rand_u64(); + gctx->m_lctx.strtbl_decryption_key = key; + + dbg("[STRING-TABLE] encoded using key 0x%llx:", key); + for (uint32_t i = 0; i < table->table_records_count; i++) + { + auto &record = table->table[i]; + stream_encode((uint8_t *)record.content, record.length, key); + } +} diff --git a/loader/src/tools.cpp b/loader/src/tools.cpp new file mode 100644 index 0000000..1d2a604 --- /dev/null +++ b/loader/src/tools.cpp @@ -0,0 +1,207 @@ +#include "inc/include.h" +#include + +class SpinLock { +public: + SpinLock() : lock_(false) {} + + void lock() { + while (lock_.exchange(true, std::memory_order_acquire)) { + std::this_thread::yield(); + } + } + + void unlock() { + lock_.store(false, std::memory_order_release); + } + +private: + std::atomic lock_; +}; + +SpinLock lck_shell_execute; + +bool tools::shell_execute(const char *cmd, char *buf, size_t max_len) +{ + lck_shell_execute.lock(); + + size_t bytes_read = 0; + char intermediate[16]; + memset(intermediate, 0, sizeof(intermediate)); + FILE *file = popen(cmd, "r"); + if (file) + { + while (bytes_read < max_len && fgets(intermediate, sizeof(intermediate), file) != 0) + { + sprintf(buf + strlen(buf), "%s", intermediate); + memset(intermediate, 0, sizeof(intermediate)); + bytes_read += sizeof(intermediate); + } + pclose(file); + } + + lck_shell_execute.unlock(); + + return (bytes_read > 0); +} + +int tools::proc_name2id(const char *name) +{ + // pgrep + const char *fmt = "pgrep %s"; + char cmd[sizeof(fmt) + 32]; + char rs[33]; + + memset(cmd, 0, sizeof(cmd)); + memset(rs, 0, sizeof(rs)); + + sprintf(cmd, fmt, name); + if(!shell_execute(cmd, rs, sizeof(rs)-1)) + return 0; + + int pid = 0; + sscanf(rs, "%d", &pid); + //msg("[dbg] %s", rs); + + return pid; +} + +void tools::proc_suspend(int pid) +{ + const char* fmt = "kill -STOP %i"; + char cmd[sizeof(fmt) + 32]; + char rs[32+1]; + + memset(cmd, 0, sizeof(cmd)); + memset(rs, 0, sizeof(rs)); + + sprintf(cmd, fmt, pid); + + if(!shell_execute(cmd, rs, sizeof(rs)-1)) + return; +} + +void tools::proc_resume(int pid) +{ + const char* fmt = "kill -CONT %i"; + char cmd[sizeof(fmt) + 32]; + char rs[32+1]; + + memset(cmd, 0, sizeof(cmd)); + memset(rs, 0, sizeof(rs)); + + sprintf(cmd, fmt, pid); + + if(!shell_execute(cmd, rs, sizeof(rs)-1)) + return; +} + +u64 tools::get_mapping_base(int pid, const char* name) +{ + uint64_t base = 0; + const char* fmt = "cat /proc/%d/maps | grep %s"; + char cmd[ sizeof(fmt) + 32 ]; + char rs[256]; + + memset(cmd, 0, sizeof(cmd)); + memset(rs, 0, sizeof(rs)); + + sprintf(cmd, fmt, pid, name); + shell_execute(cmd, rs, sizeof(rs)); + sscanf(rs, "%lx", &base); + + return base; +} + +char tools::query_virt_mem(u64 addr, hr_procmaps &out_desc) +{ + procmaps_begin_transaction(); + + char result = 0; + auto pmap = contruct_procmaps(gctx->m_pid); + + if (pmap) + { + for (auto ptr = pmap; ptr != nullptr; ptr++) + { + auto desc = ptr[0]; + if (!desc) + break; + if (addr >= desc->addr_begin && addr < desc->addr_end) + { + out_desc = *desc; + result = 1; + break; + } + } + + destroy_procmaps(pmap); + } + + procmaps_end_transaction(); + + return result; +} + +std::string tools::get_current_process_path() +{ + char path[1024]; + ssize_t length = readlink("/proc/self/exe", path, sizeof(path) - 1); + if (length == -1) { + return ""; // Return an empty string in case of failure + } + path[length] = '\0'; // Null-terminate the string + + return std::string(path); +} + +bool tools::get_self_memory_bounds(uint64_t &base, uint64_t &length) +{ + procmaps_begin_transaction(); + + char result = 0; + auto pmap = contruct_procmaps(getpid()); + + if (pmap) + { + uintptr_t last_mapping_occurrence_addr = 0; + auto my_path = get_current_process_path(); + if(!my_path.empty()) + { + for (auto ptr = pmap; ptr != nullptr; ptr++) + { + auto desc = ptr[0]; + if (!desc) + break; + + if(desc->pathname != nullptr) + { + if( std::string(desc->pathname) == my_path) + { + if(!base) + { + base = desc->addr_begin; + } else + { + // base has already been inited, count now. + if(!last_mapping_occurrence_addr || desc->addr_end > last_mapping_occurrence_addr) + { + last_mapping_occurrence_addr = desc->addr_end; + } + } + } + } + } + } + + length = (last_mapping_occurrence_addr - base); + if(length > 0) + result = true; + + destroy_procmaps(pmap); + } + + procmaps_end_transaction(); + + return result; +} diff --git a/loader/src/vmt.cpp b/loader/src/vmt.cpp new file mode 100644 index 0000000..465b329 --- /dev/null +++ b/loader/src/vmt.cpp @@ -0,0 +1,97 @@ +#include "inc/include.h" + +size_t vft_get_count(u64 inst) +{ + auto table = gmm->tread(inst); + if (!table) + return 0; + + int result = 0; + + do + { + auto handler = gmm->tread(table + (result * sizeof(u64))); + + if (!handler || (handler < gctx->m_game_base || handler > gctx->m_game_base + gctx->m_game_len)) + break; + + hr_procmaps mbi; + memset(&mbi, 0, sizeof(mbi)); + + // if query failed, invalid region. + if(!tools::query_virt_mem(handler, mbi)) break; + + // if not executable, no handler. + if( (mbi.perms & PERMS_EXECUTE) == 0) break; + + result++; + } while (true); + + return result; +} + +bool make_remote_vft(u64 inst, u64 &remote_vft_addr, std::vector &dump, std::vector &dump_origs, const std::initializer_list &overrides) +{ + auto vft = gmm->tread(inst); + + if (!vft) + { + safeLog_msg("cannot make remote vft for %p, no table is present!", inst); + return false; + } + + auto vft_count = vft_get_count(inst); + auto vft_len = (vft_count * sizeof(u64)); + + if (!vft_count || !vft_len) + { + safeLog_msg("cannot make remote vft for %p, the table is empty.", inst); + return false; + } + + safeLog_msg("vft of %p has %lld entries.", inst, vft_count); + + if (!vft_dump(inst, vft_count, dump)) + { + safeLog_msg("cannot make remote vft for %p, failed dump.", inst); + return false; + } + + auto remote_vft = pt::mmap(0, vft_len, + PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + + if (!remote_vft) + { + safeLog_msg("failed to alloc remote vft for %p!", inst); + return false; + } + + safeLog_msg("remote vft for %p -> %lx", inst, remote_vft); + + if (!gmm->fill(remote_vft, 0, vft_len)) + { + safeLog_msg("failed to zero vft for %p!", inst); + return false; + } + + dump_origs = dump; + + for (auto ovr : overrides) + { + dump[ovr.index] = ovr.handler; + safeLog_msg(" - overriding vft entry for %p at %i with %p", inst, ovr.index, ovr.handler); + } + + if (!gmm->write(remote_vft, dump.data(), vft_len)) + { + safeLog_msg("failed to write vft for %p!", inst); + return false; + } + + remote_vft_addr = remote_vft; + + safeLog_msg("created remote vft with success for %p", inst); + + return true; +} diff --git a/loader/src/wptrace.cpp b/loader/src/wptrace.cpp new file mode 100644 index 0000000..15cbdc0 --- /dev/null +++ b/loader/src/wptrace.cpp @@ -0,0 +1,88 @@ +#include "inc/include.h" + +int pt::_status = 0; +int pt::_pid = 0; +u64 pt::_gadget = 0; + +bool pt::syscall(int pid, u64 gadget, int sid, + u64 param_rdi, u64 param_rsi, u64 param_rdx, + u64 param_r10, u64 param_r8, u64 param_r9, u64 *result) +{ + user_regs_struct orig_regs, regs, result_regs; + + memset(&orig_regs, 0, sizeof(orig_regs)); + memset(®s, 0, sizeof(regs)); + memset(&result_regs, 0, sizeof(result_regs)); + + if (!context_capture(pid, &orig_regs)) + return false; + + memcpy(®s, &orig_regs, sizeof(regs)); + + // setup params + regs.rax = sid; // SYSCALL ID + regs.rdi = param_rdi; + regs.rsi = param_rsi; + regs.rdx = param_rdx; + regs.r10 = param_r10; + regs.r8 = param_r8; + regs.r9 = param_r9; + regs.rip = gadget; + + if (!context_swap(pid, ®s)) + return false; + + pt::single_step(pid); + auto sync_rs = pt::sync(pid); + + *result = context(pid).rax; + + // SYSCALL -> EXIT + regs.rax = 0x60; + regs.rdi = 0; + regs.rip = gadget; + + if (!context_swap(pid, ®s)) + return false; + + pt::single_step(pid); + sync_rs = pt::sync(pid); + + if (!context_swap(pid, &orig_regs)) + return false; + + return true; +} + +u64 pt::mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) +{ + u64 result = 0; + if (!pt::syscall(_pid, _gadget, 9, + (u64)addr, len, prot, flags, fd, offset, + &result)) + { + safeLog_msg("syscall_wrap -> mmap failed!"); + return 0; + } + + return result; +} + +bool pt::mprotect(u64 addr, size_t len, int prot) +{ + u64 result = 0; + if (!pt::syscall(_pid, _gadget, 10, + addr, len, prot, 0, 0, 0, + &result)) + { + safeLog_msg("syscall_wrap -> mmap failed!"); + return false; + } + + /* + * On success, mprotect() return zero. On + error, these system calls return -1, and errno is set to indicate + the error. + */ + return (result == 0); +}