Initialize shared Metin2 SDK with rendering optimizations and tests

This commit is contained in:
shen
2026-10-11 17:52:55 -07:00
commit 86759bb726
147 changed files with 27272 additions and 0 deletions
+338
View File
@@ -0,0 +1,338 @@
#include "eterpack/archive.h"
#include "eterpack/crc32.h"
#include "eterpack/lzo.h"
#include <fstream>
#include <cstring>
#include <algorithm>
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
namespace eterpack {
static std::string normalize_path(std::string_view p) {
std::string s(p);
for (char& c : s) {
if (c == '\\') c = '/';
else c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return s;
}
EterPackArchive::EterPackArchive() {
std::memcpy(m_security_key, DEFAULT_SECURITY_KEY, sizeof(m_security_key));
}
EterPackArchive::~EterPackArchive() {
close();
}
void EterPackArchive::close() {
if (m_epk_file.is_open()) {
m_epk_file.close();
}
m_epk_memory.clear();
m_entries.clear();
m_crc_map.clear();
m_name_map.clear();
m_is_open = false;
m_archive_name.clear();
m_epk_path.clear();
}
std::string EterPackArchive::resolve_case_insensitive(const std::string& path_str) {
fs::path p(path_str);
if (fs::exists(p)) return p.string();
fs::path parent = p.parent_path();
if (parent.empty()) parent = ".";
if (!fs::exists(parent)) return path_str;
std::string target_name = p.filename().string();
for (const auto& entry : fs::directory_iterator(parent)) {
std::string name = entry.path().filename().string();
if (name.size() == target_name.size()) {
bool match = true;
for (size_t i = 0; i < name.size(); ++i) {
if (std::tolower(static_cast<unsigned char>(name[i])) !=
std::tolower(static_cast<unsigned char>(target_name[i]))) {
match = false;
break;
}
}
if (match) return entry.path().string();
}
}
return path_str;
}
bool EterPackArchive::open(const std::string& base_or_eix_path,
const uint32_t index_key[4],
const uint32_t security_key[4]) {
close();
if (security_key) {
std::memcpy(m_security_key, security_key, sizeof(m_security_key));
}
std::string base = base_or_eix_path;
if (base.size() >= 4 && base.substr(base.size() - 4) == ".eix") {
base = base.substr(0, base.size() - 4);
}
std::string eix_path = resolve_case_insensitive(base + ".eix");
m_epk_path = resolve_case_insensitive(base + ".epk");
std::ifstream eix_file(eix_path, std::ios::binary);
if (!eix_file.is_open()) {
return false;
}
eix_file.seekg(0, std::ios::end);
size_t eix_size = static_cast<size_t>(eix_file.tellg());
eix_file.seekg(0, std::ios::beg);
std::vector<uint8_t> eix_bytes(eix_size);
if (!eix_file.read(reinterpret_cast<char*>(eix_bytes.data()), eix_size)) {
return false;
}
eix_file.close();
if (!parse_index_buffer(eix_bytes, index_key)) {
return false;
}
m_epk_file.open(m_epk_path, std::ios::binary);
if (!m_epk_file.is_open()) {
close();
return false;
}
m_archive_name = base;
m_is_open = true;
return true;
}
bool EterPackArchive::open_memory(std::span<const uint8_t> eix_data,
std::span<const uint8_t> epk_data,
const uint32_t index_key[4],
const uint32_t security_key[4]) {
close();
if (security_key) {
std::memcpy(m_security_key, security_key, sizeof(m_security_key));
}
if (!parse_index_buffer(eix_data, index_key)) {
return false;
}
m_epk_memory.assign(epk_data.begin(), epk_data.end());
m_archive_name = "memory";
m_is_open = true;
return true;
}
bool EterPackArchive::parse_index_buffer(std::span<const uint8_t> eix_bytes,
const uint32_t index_key[4]) {
if (eix_bytes.size() < ETERPACK_HEADER_SIZE) {
return false;
}
uint32_t fourcc = *reinterpret_cast<const uint32_t*>(eix_bytes.data());
std::vector<uint8_t> decompressed_eix;
std::span<const uint8_t> payload = eix_bytes;
if (fourcc == FOURCC_MCOZ) {
if (!LZO::instance().decompress_mcoz(eix_bytes, decompressed_eix, index_key)) {
return false;
}
payload = decompressed_eix;
} else if (fourcc != FOURCC_EPKD) {
return false;
}
if (payload.size() < ETERPACK_HEADER_SIZE) {
return false;
}
const uint8_t* ptr = payload.data();
uint32_t inner_fourcc = *reinterpret_cast<const uint32_t*>(ptr);
uint32_t version = *reinterpret_cast<const uint32_t*>(ptr + 4);
int32_t count = *reinterpret_cast<const int32_t*>(ptr + 8);
if (inner_fourcc != FOURCC_EPKD || version != ETERPACK_VERSION || count < 0) {
return false;
}
size_t expected_size = ETERPACK_HEADER_SIZE + static_cast<size_t>(count) * sizeof(EterPackIndexEntry);
if (payload.size() < expected_size) {
return false;
}
const auto* entries_ptr = reinterpret_cast<const EterPackIndexEntry*>(ptr + ETERPACK_HEADER_SIZE);
m_entries.assign(entries_ptr, entries_ptr + count);
for (size_t i = 0; i < m_entries.size(); ++i) {
const auto& entry = m_entries[i];
if (entry.filename_crc != 0) {
m_crc_map[entry.filename_crc] = i;
m_name_map[normalize_path(entry.filename)] = i;
}
}
return true;
}
bool EterPackArchive::has_file(std::string_view filename) const {
if (find_entry(filename)) return true;
uint32_t crc = GetCRC32(filename);
return has_file(crc);
}
bool EterPackArchive::has_file(uint32_t crc) const {
return m_crc_map.find(crc) != m_crc_map.end();
}
const EterPackIndexEntry* EterPackArchive::find_entry(std::string_view filename) const {
auto it = m_name_map.find(normalize_path(filename));
if (it != m_name_map.end()) {
return &m_entries[it->second];
}
uint32_t crc = GetCRC32(filename);
return find_entry(crc);
}
const EterPackIndexEntry* EterPackArchive::find_entry(uint32_t crc) const {
auto it = m_crc_map.find(crc);
if (it != m_crc_map.end()) {
return &m_entries[it->second];
}
return nullptr;
}
bool EterPackArchive::extract(std::string_view filename, std::vector<uint8_t>& out_data) const {
const auto* entry = find_entry(filename);
if (!entry) return false;
return extract(*entry, out_data);
}
bool EterPackArchive::extract(const EterPackIndexEntry& entry, std::vector<uint8_t>& out_data) const {
if (!m_is_open) return false;
std::vector<uint8_t> raw_bytes(entry.data_size);
if (!m_epk_memory.empty()) {
if (entry.data_position < 0 ||
static_cast<size_t>(entry.data_position + entry.data_size) > m_epk_memory.size()) {
return false;
}
std::memcpy(raw_bytes.data(), m_epk_memory.data() + entry.data_position, entry.data_size);
} else if (m_epk_file.is_open()) {
m_epk_file.seekg(entry.data_position, std::ios::beg);
if (!m_epk_file.read(reinterpret_cast<char*>(raw_bytes.data()), entry.data_size)) {
return false;
}
} else {
return false;
}
// CRC validation for security types
if (entry.compressed_type == static_cast<uint8_t>(CompressionType::Security) ||
entry.compressed_type == static_cast<uint8_t>(CompressionType::Panama)) {
if (GetCRC32(raw_bytes) != entry.data_crc) {
return false;
}
}
if (entry.compressed_type == static_cast<uint8_t>(CompressionType::None)) {
out_data = std::move(raw_bytes);
return true;
} else if (entry.compressed_type == static_cast<uint8_t>(CompressionType::Compress)) {
return LZO::instance().decompress_mcoz(raw_bytes, out_data, nullptr);
} else if (entry.compressed_type == static_cast<uint8_t>(CompressionType::Security)) {
return LZO::instance().decompress_mcoz(raw_bytes, out_data, m_security_key);
}
return false;
}
bool EterPackArchive::create_archive(
const std::string& base_path,
const std::vector<std::pair<std::string, std::vector<uint8_t>>>& files,
bool encrypt_index,
const uint32_t index_key[4],
CompressionType comp_type,
const uint32_t security_key[4]) {
std::string epk_path = base_path + ".epk";
std::string eix_path = base_path + ".eix";
std::ofstream epk_out(epk_path, std::ios::binary);
if (!epk_out.is_open()) return false;
std::vector<EterPackIndexEntry> entries;
entries.reserve(files.size());
int32_t current_pos = 0;
for (int32_t i = 0; i < static_cast<int32_t>(files.size()); ++i) {
const auto& [name, content] = files[i];
EterPackIndexEntry entry{};
entry.id = i;
std::strncpy(entry.filename, name.c_str(), FILENAME_MAX_LEN);
entry.filename_crc = GetCRC32(name);
entry.real_data_size = static_cast<int32_t>(content.size());
std::vector<uint8_t> payload;
if (comp_type == CompressionType::Compress) {
if (!LZO::instance().compress_mcoz(content, payload, nullptr)) return false;
entry.compressed_type = static_cast<uint8_t>(CompressionType::Compress);
} else if (comp_type == CompressionType::Security) {
if (!LZO::instance().compress_mcoz(content, payload, security_key)) return false;
entry.compressed_type = static_cast<uint8_t>(CompressionType::Security);
} else {
payload = content;
entry.compressed_type = static_cast<uint8_t>(CompressionType::None);
}
entry.data_size = static_cast<int32_t>(payload.size());
entry.data_crc = GetCRC32(payload);
entry.data_position = current_pos;
epk_out.write(reinterpret_cast<const char*>(payload.data()), payload.size());
current_pos += entry.data_size;
entries.push_back(entry);
}
epk_out.close();
// Construct index payload
size_t index_payload_size = ETERPACK_HEADER_SIZE + entries.size() * sizeof(EterPackIndexEntry);
std::vector<uint8_t> plain_index(index_payload_size);
uint32_t fourcc = FOURCC_EPKD;
uint32_t version = ETERPACK_VERSION;
int32_t count = static_cast<int32_t>(entries.size());
std::memcpy(plain_index.data(), &fourcc, 4);
std::memcpy(plain_index.data() + 4, &version, 4);
std::memcpy(plain_index.data() + 8, &count, 4);
std::memcpy(plain_index.data() + ETERPACK_HEADER_SIZE, entries.data(), entries.size() * sizeof(EterPackIndexEntry));
std::vector<uint8_t> final_eix;
if (encrypt_index) {
if (!LZO::instance().compress_mcoz(plain_index, final_eix, index_key)) {
return false;
}
} else {
final_eix = std::move(plain_index);
}
std::ofstream eix_out(eix_path, std::ios::binary);
if (!eix_out.is_open()) return false;
eix_out.write(reinterpret_cast<const char*>(final_eix.data()), final_eix.size());
eix_out.close();
return true;
}
} // namespace eterpack
+85
View File
@@ -0,0 +1,85 @@
#include "eterpack/crc32.h"
namespace eterpack {
namespace {
struct CrcTable {
uint32_t v[256];
constexpr CrcTable() : v{} {
for (uint32_t n = 0; n < 256; ++n) {
uint32_t c = n;
for (int k = 0; k < 8; ++k) {
c = (c & 1) ? (0xEDB88320u ^ (c >> 1)) : (c >> 1);
}
v[n] = c;
}
}
};
static constexpr CrcTable s_crcTable{};
static const uint32_t* const CRCTable = s_crcTable.v;
} // namespace
#define DO1(buf, i) crc = CRCTable[(crc ^ static_cast<uint8_t>(buf[i])) & 0xff] ^ (crc >> 8)
#define DO2(buf, i) DO1(buf, i); DO1(buf, i + 1)
#define DO4(buf, i) DO2(buf, i); DO2(buf, i + 2)
#define DO8(buf, i) DO4(buf, i); DO4(buf, i + 4)
#define DO16(buf, i) DO8(buf, i); DO8(buf, i + 8)
uint32_t GetCRC32(const char* buf, size_t len) {
uint32_t crc = 0xffffffff;
if (len >= 16) {
do {
DO16(buf, 0);
buf += 16;
len -= 16;
} while (len >= 16);
}
if (len != 0) {
do {
DO1(buf, 0);
++buf;
--len;
} while (len > 0);
}
crc ^= 0xffffffff;
return crc;
}
#ifndef UPPER
#define UPPER(c) (((c) >= 'a' && (c) <= 'z') ? ((c) + ('A' - 'a')) : (c))
#endif
#define DO1CI(buf, i) crc = CRCTable[(crc ^ static_cast<uint8_t>(UPPER(buf[i]))) & 0xff] ^ (crc >> 8)
#define DO2CI(buf, i) DO1CI(buf, i); DO1CI(buf, i + 1)
#define DO4CI(buf, i) DO2CI(buf, i); DO2CI(buf, i + 2)
#define DO8CI(buf, i) DO4CI(buf, i); DO4CI(buf, i + 4)
#define DO16CI(buf, i) DO8CI(buf, i); DO8CI(buf, i + 8)
uint32_t GetCaseCRC32(const char* buf, size_t len) {
uint32_t crc = 0xffffffff;
if (16 <= len) {
do {
DO16CI(buf, 0);
buf += 16;
len -= 16;
} while (len >= 16);
}
if (0 != len) {
do {
DO1CI(buf, 0);
++buf;
--len;
} while (len > 0);
}
crc ^= 0xffffffff;
return crc;
}
} // namespace eterpack
+149
View File
@@ -0,0 +1,149 @@
#include "eterpack/lzo.h"
#include "eterpack/tea.h"
#include "minilzo/minilzo.h"
#include <cstdlib>
#include <cstring>
#include <algorithm>
namespace eterpack {
LZO& LZO::instance() {
static LZO s_instance;
return s_instance;
}
LZO::LZO() {
if (lzo_init() != LZO_E_OK) {
// init failed
}
m_workmem = std::malloc(LZO1X_1_MEM_COMPRESS);
}
LZO::~LZO() {
if (m_workmem) {
std::free(m_workmem);
m_workmem = nullptr;
}
}
bool LZO::raw_decompress(const uint8_t* in, size_t in_len, uint8_t* out, size_t out_len) {
lzo_uint actual_out = out_len;
int r = lzo1x_decompress(in, static_cast<lzo_uint>(in_len), out, &actual_out, nullptr);
return (r == LZO_E_OK && actual_out == out_len);
}
bool LZO::raw_compress(const uint8_t* in, size_t in_len, std::vector<uint8_t>& out) {
lzo_uint max_out = in_len + in_len / 16 + 64 + 3;
out.resize(max_out);
lzo_uint actual_out = 0;
int r = lzo1x_1_compress(in, static_cast<lzo_uint>(in_len), out.data(), &actual_out, m_workmem);
if (r != LZO_E_OK) {
return false;
}
out.resize(actual_out);
return true;
}
bool LZO::decompress_mcoz(std::span<const uint8_t> input,
std::vector<uint8_t>& output,
const uint32_t key[4]) {
if (input.size() < sizeof(LZHeader)) {
return false;
}
const auto* hdr = reinterpret_cast<const LZHeader*>(input.data());
if (hdr->fourcc != FOURCC_MCOZ) {
return false;
}
if (hdr->real_size == 0) {
output.clear();
return true;
}
output.resize(hdr->real_size);
if (hdr->encrypt_size > 0) {
if (!key) {
return false;
}
if (input.size() < sizeof(LZHeader) + hdr->encrypt_size) {
return false;
}
std::vector<uint8_t> decrypted(hdr->encrypt_size);
tea_decrypt(reinterpret_cast<uint32_t*>(decrypted.data()),
reinterpret_cast<const uint32_t*>(input.data() + sizeof(LZHeader)),
key, static_cast<int>(hdr->encrypt_size));
if (decrypted.size() < 4) {
return false;
}
uint32_t inner_fourcc = *reinterpret_cast<const uint32_t*>(decrypted.data());
if (inner_fourcc != FOURCC_MCOZ) {
return false;
}
if (hdr->compressed_size > decrypted.size() - 4) {
return false;
}
const uint8_t* comp_data = decrypted.data() + 4;
return raw_decompress(comp_data, hdr->compressed_size, output.data(), hdr->real_size);
} else {
if (input.size() < sizeof(LZHeader) + 4 + hdr->compressed_size) {
return false;
}
uint32_t inner_fourcc = *reinterpret_cast<const uint32_t*>(input.data() + sizeof(LZHeader));
if (inner_fourcc != FOURCC_MCOZ) {
return false;
}
const uint8_t* comp_data = input.data() + sizeof(LZHeader) + 4;
return raw_decompress(comp_data, hdr->compressed_size, output.data(), hdr->real_size);
}
}
bool LZO::compress_mcoz(std::span<const uint8_t> input,
std::vector<uint8_t>& output,
const uint32_t key[4]) {
std::vector<uint8_t> comp;
if (!raw_compress(input.data(), input.size(), comp)) {
return false;
}
LZHeader hdr;
hdr.fourcc = FOURCC_MCOZ;
hdr.compressed_size = static_cast<uint32_t>(comp.size());
hdr.real_size = static_cast<uint32_t>(input.size());
if (key) {
size_t plain_payload = 4 + comp.size();
size_t enc_size = (plain_payload % 8 != 0) ? (plain_payload + 8 - (plain_payload % 8)) : plain_payload;
hdr.encrypt_size = static_cast<uint32_t>(enc_size);
output.resize(sizeof(LZHeader) + enc_size);
std::memcpy(output.data(), &hdr, sizeof(LZHeader));
std::vector<uint8_t> plain(enc_size, 0);
uint32_t fourcc = FOURCC_MCOZ;
std::memcpy(plain.data(), &fourcc, 4);
std::memcpy(plain.data() + 4, comp.data(), comp.size());
tea_encrypt(reinterpret_cast<uint32_t*>(output.data() + sizeof(LZHeader)),
reinterpret_cast<const uint32_t*>(plain.data()),
key, static_cast<int>(plain_payload));
} else {
hdr.encrypt_size = 0;
output.resize(sizeof(LZHeader) + 4 + comp.size());
std::memcpy(output.data(), &hdr, sizeof(LZHeader));
uint32_t fourcc = FOURCC_MCOZ;
std::memcpy(output.data() + sizeof(LZHeader), &fourcc, 4);
std::memcpy(output.data() + sizeof(LZHeader) + 4, comp.data(), comp.size());
}
return true;
}
} // namespace eterpack
+453
View File
@@ -0,0 +1,453 @@
/* lzoconf.h -- configuration of the LZO data compression library
This file is part of the LZO real-time data compression library.
Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
All Rights Reserved.
The LZO library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
The LZO library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the LZO library; see the file COPYING.
If not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
http://www.oberhumer.com/opensource/lzo/
*/
#ifndef __LZOCONF_H_INCLUDED
#define __LZOCONF_H_INCLUDED 1
#define LZO_VERSION 0x20a0 /* 2.10 */
#define LZO_VERSION_STRING "2.10"
#define LZO_VERSION_DATE "Mar 01 2017"
/* internal Autoconf configuration file - only used when building LZO */
#if defined(LZO_HAVE_CONFIG_H)
# include <config.h>
#endif
#include <limits.h>
#include <stddef.h>
/***********************************************************************
// LZO requires a conforming <limits.h>
************************************************************************/
#if !defined(CHAR_BIT) || (CHAR_BIT != 8)
# error "invalid CHAR_BIT"
#endif
#if !defined(UCHAR_MAX) || !defined(USHRT_MAX) || !defined(UINT_MAX) || !defined(ULONG_MAX)
# error "check your compiler installation"
#endif
#if (USHRT_MAX < 1) || (UINT_MAX < 1) || (ULONG_MAX < 1)
# error "your limits.h macros are broken"
#endif
/* get OS and architecture defines */
#ifndef __LZODEFS_H_INCLUDED
#include <lzo/lzodefs.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/***********************************************************************
// some core defines
************************************************************************/
/* memory checkers */
#if !defined(__LZO_CHECKER)
# if defined(__BOUNDS_CHECKING_ON)
# define __LZO_CHECKER 1
# elif defined(__CHECKER__)
# define __LZO_CHECKER 1
# elif defined(__INSURE__)
# define __LZO_CHECKER 1
# elif defined(__PURIFY__)
# define __LZO_CHECKER 1
# endif
#endif
/***********************************************************************
// integral and pointer types
************************************************************************/
/* lzo_uint must match size_t */
#if !defined(LZO_UINT_MAX)
# if (LZO_ABI_LLP64)
# if (LZO_OS_WIN64)
typedef unsigned __int64 lzo_uint;
typedef __int64 lzo_int;
# define LZO_TYPEOF_LZO_INT LZO_TYPEOF___INT64
# else
typedef lzo_ullong_t lzo_uint;
typedef lzo_llong_t lzo_int;
# define LZO_TYPEOF_LZO_INT LZO_TYPEOF_LONG_LONG
# endif
# define LZO_SIZEOF_LZO_INT 8
# define LZO_UINT_MAX 0xffffffffffffffffull
# define LZO_INT_MAX 9223372036854775807LL
# define LZO_INT_MIN (-1LL - LZO_INT_MAX)
# elif (LZO_ABI_IP32L64) /* MIPS R5900 */
typedef unsigned int lzo_uint;
typedef int lzo_int;
# define LZO_SIZEOF_LZO_INT LZO_SIZEOF_INT
# define LZO_TYPEOF_LZO_INT LZO_TYPEOF_INT
# define LZO_UINT_MAX UINT_MAX
# define LZO_INT_MAX INT_MAX
# define LZO_INT_MIN INT_MIN
# elif (ULONG_MAX >= LZO_0xffffffffL)
typedef unsigned long lzo_uint;
typedef long lzo_int;
# define LZO_SIZEOF_LZO_INT LZO_SIZEOF_LONG
# define LZO_TYPEOF_LZO_INT LZO_TYPEOF_LONG
# define LZO_UINT_MAX ULONG_MAX
# define LZO_INT_MAX LONG_MAX
# define LZO_INT_MIN LONG_MIN
# else
# error "lzo_uint"
# endif
#endif
/* The larger type of lzo_uint and lzo_uint32_t. */
#if (LZO_SIZEOF_LZO_INT >= 4)
# define lzo_xint lzo_uint
#else
# define lzo_xint lzo_uint32_t
#endif
typedef int lzo_bool;
/* sanity checks */
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_int) == LZO_SIZEOF_LZO_INT)
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == LZO_SIZEOF_LZO_INT)
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_xint) >= sizeof(lzo_uint))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_xint) >= sizeof(lzo_uint32_t))
#ifndef __LZO_MMODEL
#define __LZO_MMODEL /*empty*/
#endif
/* no typedef here because of const-pointer issues */
#define lzo_bytep unsigned char __LZO_MMODEL *
#define lzo_charp char __LZO_MMODEL *
#define lzo_voidp void __LZO_MMODEL *
#define lzo_shortp short __LZO_MMODEL *
#define lzo_ushortp unsigned short __LZO_MMODEL *
#define lzo_intp lzo_int __LZO_MMODEL *
#define lzo_uintp lzo_uint __LZO_MMODEL *
#define lzo_xintp lzo_xint __LZO_MMODEL *
#define lzo_voidpp lzo_voidp __LZO_MMODEL *
#define lzo_bytepp lzo_bytep __LZO_MMODEL *
#define lzo_int8_tp lzo_int8_t __LZO_MMODEL *
#define lzo_uint8_tp lzo_uint8_t __LZO_MMODEL *
#define lzo_int16_tp lzo_int16_t __LZO_MMODEL *
#define lzo_uint16_tp lzo_uint16_t __LZO_MMODEL *
#define lzo_int32_tp lzo_int32_t __LZO_MMODEL *
#define lzo_uint32_tp lzo_uint32_t __LZO_MMODEL *
#if defined(lzo_int64_t)
#define lzo_int64_tp lzo_int64_t __LZO_MMODEL *
#define lzo_uint64_tp lzo_uint64_t __LZO_MMODEL *
#endif
/* Older LZO versions used to support ancient systems and memory models
* such as 16-bit MSDOS with __huge pointers or Cray PVP, but these
* obsolete configurations are not supported any longer.
*/
#if defined(__LZO_MMODEL_HUGE)
#error "__LZO_MMODEL_HUGE memory model is unsupported"
#endif
#if (LZO_MM_PVP)
#error "LZO_MM_PVP memory model is unsupported"
#endif
#if (LZO_SIZEOF_INT < 4)
#error "LZO_SIZEOF_INT < 4 is unsupported"
#endif
#if (__LZO_UINTPTR_T_IS_POINTER)
#error "__LZO_UINTPTR_T_IS_POINTER is unsupported"
#endif
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(int) >= 4)
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) >= 4)
/* Strange configurations where sizeof(lzo_uint) != sizeof(size_t) should
* work but have not received much testing lately, so be strict here.
*/
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == sizeof(size_t))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == sizeof(ptrdiff_t))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(lzo_uint) == sizeof(lzo_uintptr_t))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(void *) == sizeof(lzo_uintptr_t))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(char *) == sizeof(lzo_uintptr_t))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(long *) == sizeof(lzo_uintptr_t))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(void *) == sizeof(lzo_voidp))
LZO_COMPILE_TIME_ASSERT_HEADER(sizeof(char *) == sizeof(lzo_bytep))
/***********************************************************************
// function types
************************************************************************/
/* name mangling */
#if !defined(__LZO_EXTERN_C)
# ifdef __cplusplus
# define __LZO_EXTERN_C extern "C"
# else
# define __LZO_EXTERN_C extern
# endif
#endif
/* calling convention */
#if !defined(__LZO_CDECL)
# define __LZO_CDECL __lzo_cdecl
#endif
/* DLL export information */
#if !defined(__LZO_EXPORT1)
# define __LZO_EXPORT1 /*empty*/
#endif
#if !defined(__LZO_EXPORT2)
# define __LZO_EXPORT2 /*empty*/
#endif
/* __cdecl calling convention for public C and assembly functions */
#if !defined(LZO_PUBLIC)
# define LZO_PUBLIC(r) __LZO_EXPORT1 r __LZO_EXPORT2 __LZO_CDECL
#endif
#if !defined(LZO_EXTERN)
# define LZO_EXTERN(r) __LZO_EXTERN_C LZO_PUBLIC(r)
#endif
#if !defined(LZO_PRIVATE)
# define LZO_PRIVATE(r) static r __LZO_CDECL
#endif
/* function types */
typedef int
(__LZO_CDECL *lzo_compress_t) ( const lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem );
typedef int
(__LZO_CDECL *lzo_decompress_t) ( const lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem );
typedef int
(__LZO_CDECL *lzo_optimize_t) ( lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem );
typedef int
(__LZO_CDECL *lzo_compress_dict_t)(const lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem,
const lzo_bytep dict, lzo_uint dict_len );
typedef int
(__LZO_CDECL *lzo_decompress_dict_t)(const lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem,
const lzo_bytep dict, lzo_uint dict_len );
/* Callback interface. Currently only the progress indicator ("nprogress")
* is used, but this may change in a future release. */
struct lzo_callback_t;
typedef struct lzo_callback_t lzo_callback_t;
#define lzo_callback_p lzo_callback_t __LZO_MMODEL *
/* malloc & free function types */
typedef lzo_voidp (__LZO_CDECL *lzo_alloc_func_t)
(lzo_callback_p self, lzo_uint items, lzo_uint size);
typedef void (__LZO_CDECL *lzo_free_func_t)
(lzo_callback_p self, lzo_voidp ptr);
/* a progress indicator callback function */
typedef void (__LZO_CDECL *lzo_progress_func_t)
(lzo_callback_p, lzo_uint, lzo_uint, int);
struct lzo_callback_t
{
/* custom allocators (set to 0 to disable) */
lzo_alloc_func_t nalloc; /* [not used right now] */
lzo_free_func_t nfree; /* [not used right now] */
/* a progress indicator callback function (set to 0 to disable) */
lzo_progress_func_t nprogress;
/* INFO: the first parameter "self" of the nalloc/nfree/nprogress
* callbacks points back to this struct, so you are free to store
* some extra info in the following variables. */
lzo_voidp user1;
lzo_xint user2;
lzo_xint user3;
};
/***********************************************************************
// error codes and prototypes
************************************************************************/
/* Error codes for the compression/decompression functions. Negative
* values are errors, positive values will be used for special but
* normal events.
*/
#define LZO_E_OK 0
#define LZO_E_ERROR (-1)
#define LZO_E_OUT_OF_MEMORY (-2) /* [lzo_alloc_func_t failure] */
#define LZO_E_NOT_COMPRESSIBLE (-3) /* [not used right now] */
#define LZO_E_INPUT_OVERRUN (-4)
#define LZO_E_OUTPUT_OVERRUN (-5)
#define LZO_E_LOOKBEHIND_OVERRUN (-6)
#define LZO_E_EOF_NOT_FOUND (-7)
#define LZO_E_INPUT_NOT_CONSUMED (-8)
#define LZO_E_NOT_YET_IMPLEMENTED (-9) /* [not used right now] */
#define LZO_E_INVALID_ARGUMENT (-10)
#define LZO_E_INVALID_ALIGNMENT (-11) /* pointer argument is not properly aligned */
#define LZO_E_OUTPUT_NOT_CONSUMED (-12)
#define LZO_E_INTERNAL_ERROR (-99)
#ifndef lzo_sizeof_dict_t
# define lzo_sizeof_dict_t ((unsigned)sizeof(lzo_bytep))
#endif
/* lzo_init() should be the first function you call.
* Check the return code !
*
* lzo_init() is a macro to allow checking that the library and the
* compiler's view of various types are consistent.
*/
#define lzo_init() __lzo_init_v2(LZO_VERSION,(int)sizeof(short),(int)sizeof(int),\
(int)sizeof(long),(int)sizeof(lzo_uint32_t),(int)sizeof(lzo_uint),\
(int)lzo_sizeof_dict_t,(int)sizeof(char *),(int)sizeof(lzo_voidp),\
(int)sizeof(lzo_callback_t))
LZO_EXTERN(int) __lzo_init_v2(unsigned,int,int,int,int,int,int,int,int,int);
/* version functions (useful for shared libraries) */
LZO_EXTERN(unsigned) lzo_version(void);
LZO_EXTERN(const char *) lzo_version_string(void);
LZO_EXTERN(const char *) lzo_version_date(void);
LZO_EXTERN(const lzo_charp) _lzo_version_string(void);
LZO_EXTERN(const lzo_charp) _lzo_version_date(void);
/* string functions */
LZO_EXTERN(int)
lzo_memcmp(const lzo_voidp a, const lzo_voidp b, lzo_uint len);
LZO_EXTERN(lzo_voidp)
lzo_memcpy(lzo_voidp dst, const lzo_voidp src, lzo_uint len);
LZO_EXTERN(lzo_voidp)
lzo_memmove(lzo_voidp dst, const lzo_voidp src, lzo_uint len);
LZO_EXTERN(lzo_voidp)
lzo_memset(lzo_voidp buf, int c, lzo_uint len);
/* checksum functions */
LZO_EXTERN(lzo_uint32_t)
lzo_adler32(lzo_uint32_t c, const lzo_bytep buf, lzo_uint len);
LZO_EXTERN(lzo_uint32_t)
lzo_crc32(lzo_uint32_t c, const lzo_bytep buf, lzo_uint len);
LZO_EXTERN(const lzo_uint32_tp)
lzo_get_crc32_table(void);
/* misc. */
LZO_EXTERN(int) _lzo_config_check(void);
typedef union {
lzo_voidp a00; lzo_bytep a01; lzo_uint a02; lzo_xint a03; lzo_uintptr_t a04;
void *a05; unsigned char *a06; unsigned long a07; size_t a08; ptrdiff_t a09;
#if defined(lzo_int64_t)
lzo_uint64_t a10;
#endif
} lzo_align_t;
/* align a char pointer on a boundary that is a multiple of 'size' */
LZO_EXTERN(unsigned) __lzo_align_gap(const lzo_voidp p, lzo_uint size);
#define LZO_PTR_ALIGN_UP(p,size) \
((p) + (lzo_uint) __lzo_align_gap((const lzo_voidp)(p),(lzo_uint)(size)))
/***********************************************************************
// deprecated macros - only for backward compatibility
************************************************************************/
/* deprecated - use 'lzo_bytep' instead of 'lzo_byte *' */
#define lzo_byte unsigned char
/* deprecated type names */
#define lzo_int32 lzo_int32_t
#define lzo_uint32 lzo_uint32_t
#define lzo_int32p lzo_int32_t __LZO_MMODEL *
#define lzo_uint32p lzo_uint32_t __LZO_MMODEL *
#define LZO_INT32_MAX LZO_INT32_C(2147483647)
#define LZO_UINT32_MAX LZO_UINT32_C(4294967295)
#if defined(lzo_int64_t)
#define lzo_int64 lzo_int64_t
#define lzo_uint64 lzo_uint64_t
#define lzo_int64p lzo_int64_t __LZO_MMODEL *
#define lzo_uint64p lzo_uint64_t __LZO_MMODEL *
#define LZO_INT64_MAX LZO_INT64_C(9223372036854775807)
#define LZO_UINT64_MAX LZO_UINT64_C(18446744073709551615)
#endif
/* deprecated types */
typedef union { lzo_bytep a; lzo_uint b; } __lzo_pu_u;
typedef union { lzo_bytep a; lzo_uint32_t b; } __lzo_pu32_u;
/* deprecated defines */
#if !defined(LZO_SIZEOF_LZO_UINT)
# define LZO_SIZEOF_LZO_UINT LZO_SIZEOF_LZO_INT
#endif
#if defined(LZO_CFG_COMPAT)
#define __LZOCONF_H 1
#if defined(LZO_ARCH_I086)
# define __LZO_i386 1
#elif defined(LZO_ARCH_I386)
# define __LZO_i386 1
#endif
#if defined(LZO_OS_DOS16)
# define __LZO_DOS 1
# define __LZO_DOS16 1
#elif defined(LZO_OS_DOS32)
# define __LZO_DOS 1
#elif defined(LZO_OS_WIN16)
# define __LZO_WIN 1
# define __LZO_WIN16 1
#elif defined(LZO_OS_WIN32)
# define __LZO_WIN 1
#endif
#define __LZO_CMODEL /*empty*/
#define __LZO_DMODEL /*empty*/
#define __LZO_ENTRY __LZO_CDECL
#define LZO_EXTERN_CDECL LZO_EXTERN
#define LZO_ALIGN LZO_PTR_ALIGN_UP
#define lzo_compress_asm_t lzo_compress_t
#define lzo_decompress_asm_t lzo_decompress_t
#endif /* LZO_CFG_COMPAT */
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* already included */
/* vim:set ts=4 sw=4 et: */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+106
View File
@@ -0,0 +1,106 @@
/* minilzo.h -- mini subset of the LZO real-time data compression library
This file is part of the LZO real-time data compression library.
Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
All Rights Reserved.
The LZO library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or (at your option) any later version.
The LZO library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the LZO library; see the file COPYING.
If not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Markus F.X.J. Oberhumer
<markus@oberhumer.com>
http://www.oberhumer.com/opensource/lzo/
*/
/*
* NOTE:
* the full LZO package can be found at
* http://www.oberhumer.com/opensource/lzo/
*/
#ifndef __MINILZO_H_INCLUDED
#define __MINILZO_H_INCLUDED 1
#define MINILZO_VERSION 0x20a0 /* 2.10 */
#if defined(__LZOCONF_H_INCLUDED)
# error "you cannot use both LZO and miniLZO"
#endif
/* internal Autoconf configuration file - only used when building miniLZO */
#ifdef MINILZO_HAVE_CONFIG_H
# include <config.h>
#endif
#include <limits.h>
#include <stddef.h>
#ifndef __LZODEFS_H_INCLUDED
#include "lzodefs.h"
#endif
#undef LZO_HAVE_CONFIG_H
#include "lzoconf.h"
#if !defined(LZO_VERSION) || (LZO_VERSION != MINILZO_VERSION)
# error "version mismatch in header files"
#endif
#ifdef __cplusplus
extern "C" {
#endif
/***********************************************************************
//
************************************************************************/
/* Memory required for the wrkmem parameter.
* When the required size is 0, you can also pass a NULL pointer.
*/
#define LZO1X_MEM_COMPRESS LZO1X_1_MEM_COMPRESS
#define LZO1X_1_MEM_COMPRESS ((lzo_uint32_t) (16384L * lzo_sizeof_dict_t))
#define LZO1X_MEM_DECOMPRESS (0)
/* compression */
LZO_EXTERN(int)
lzo1x_1_compress ( const lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem );
/* decompression */
LZO_EXTERN(int)
lzo1x_decompress ( const lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem /* NOT USED */ );
/* safe decompression with overrun testing */
LZO_EXTERN(int)
lzo1x_decompress_safe ( const lzo_bytep src, lzo_uint src_len,
lzo_bytep dst, lzo_uintp dst_len,
lzo_voidp wrkmem /* NOT USED */ );
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* already included */
/* vim:set ts=4 sw=4 et: */
+87
View File
@@ -0,0 +1,87 @@
#include "eterpack/tea.h"
#include <cstring>
namespace eterpack {
static constexpr uint32_t TEA_ROUND = 32;
static constexpr uint32_t DELTA = 0x9E3779B9;
void tea_code(uint32_t sz, uint32_t sy, const uint32_t key[4], uint32_t dest[2]) {
uint32_t y = sy;
uint32_t z = sz;
uint32_t sum = 0;
uint32_t n = TEA_ROUND;
while (n-- > 0) {
y += ((z << 4 ^ z >> 5) + z) ^ (sum + key[sum & 3]);
sum += DELTA;
z += ((y << 4 ^ y >> 5) + y) ^ (sum + key[(sum >> 11) & 3]);
}
dest[0] = y;
dest[1] = z;
}
void tea_decode(uint32_t sz, uint32_t sy, const uint32_t key[4], uint32_t dest[2]) {
uint32_t y = sy;
uint32_t z = sz;
uint32_t sum = DELTA * TEA_ROUND;
uint32_t n = TEA_ROUND;
while (n-- > 0) {
z -= ((y << 4 ^ y >> 5) + y) ^ (sum + key[(sum >> 11) & 3]);
sum -= DELTA;
y -= ((z << 4 ^ z >> 5) + z) ^ (sum + key[sum & 3]);
}
dest[0] = y;
dest[1] = z;
}
int tea_encrypt(uint32_t* dest, const uint32_t* src, const uint32_t key[4], int size) {
int resize = (size % 8 != 0) ? (size + 8 - (size % 8)) : size;
if (resize > size) {
std::memset(reinterpret_cast<char*>(const_cast<uint32_t*>(src)) + size, 0, resize - size);
}
for (int i = 0; i < resize >> 3; ++i, dest += 2, src += 2) {
tea_code(*(src + 1), *src, key, dest);
}
return resize;
}
int tea_decrypt(uint32_t* dest, const uint32_t* src, const uint32_t key[4], int size) {
int resize = (size % 8 != 0) ? (size + 8 - (size % 8)) : size;
for (int i = 0; i < resize >> 3; ++i, dest += 2, src += 2) {
tea_decode(*(src + 1), *src, key, dest);
}
return resize;
}
std::vector<uint8_t> tea_encrypt_bytes(std::span<const uint8_t> data, const uint32_t key[4]) {
size_t size = data.size();
size_t padded_size = (size % 8 != 0) ? (size + 8 - (size % 8)) : size;
std::vector<uint8_t> out(padded_size, 0);
std::memcpy(out.data(), data.data(), size);
std::vector<uint8_t> result(padded_size);
tea_encrypt(reinterpret_cast<uint32_t*>(result.data()),
reinterpret_cast<const uint32_t*>(out.data()),
key, static_cast<int>(size));
return result;
}
std::vector<uint8_t> tea_decrypt_bytes(std::span<const uint8_t> data, const uint32_t key[4]) {
size_t size = data.size();
size_t padded_size = (size % 8 != 0) ? (size + 8 - (size % 8)) : size;
std::vector<uint8_t> in(padded_size, 0);
std::memcpy(in.data(), data.data(), size);
std::vector<uint8_t> result(padded_size);
tea_decrypt(reinterpret_cast<uint32_t*>(result.data()),
reinterpret_cast<const uint32_t*>(in.data()),
key, static_cast<int>(size));
return result;
}
} // namespace eterpack
+190
View File
@@ -0,0 +1,190 @@
#include "eterpack/vfs.h"
#include "eterpack/crc32.h"
#include <fstream>
#include <filesystem>
#include <algorithm>
namespace fs = std::filesystem;
namespace eterpack {
static std::string normalize_vfs_path(std::string_view p) {
std::string s(p);
for (char& c : s) {
if (c == '\\') c = '/';
else c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return s;
}
EterPackVFS::EterPackVFS() = default;
EterPackVFS::~EterPackVFS() = default;
void EterPackVFS::clear() {
std::lock_guard<std::mutex> lock(m_mutex);
m_archives.clear();
m_file_map.clear();
m_crc_map.clear();
}
size_t EterPackVFS::get_archive_count() const {
std::lock_guard<std::mutex> lock(m_mutex);
return m_archives.size();
}
size_t EterPackVFS::get_total_entry_count() const {
std::lock_guard<std::mutex> lock(m_mutex);
return m_file_map.size();
}
bool EterPackVFS::mount(const std::string& base_path,
const uint32_t index_key[4],
const uint32_t security_key[4],
bool override_existing) {
auto archive = std::make_unique<EterPackArchive>();
if (!archive->open(base_path, index_key, security_key)) {
return false;
}
std::lock_guard<std::mutex> lock(m_mutex);
size_t arch_idx = m_archives.size();
for (const auto& entry : archive->get_entries()) {
if (entry.filename_crc == 0) continue;
std::string norm = normalize_vfs_path(entry.filename);
FileLocation loc{arch_idx, &entry};
if (override_existing) {
m_file_map[norm] = loc;
m_crc_map[entry.filename_crc] = loc;
} else {
m_file_map.try_emplace(norm, loc);
m_crc_map.try_emplace(entry.filename_crc, loc);
}
}
m_archives.push_back(std::move(archive));
return true;
}
bool EterPackVFS::mount_index_file(const std::string& index_file_path,
const std::string& pack_dir) {
std::ifstream in(index_file_path);
if (!in.is_open()) return false;
std::vector<std::string> lines;
std::string line;
while (std::getline(in, line)) {
while (!line.empty() && (line.back() == '\r' || line.back() == ' ' || line.back() == '\t')) {
line.pop_back();
}
if (!line.empty()) {
lines.push_back(line);
}
}
if (lines.empty() || lines[0] != "PACK") {
return false;
}
for (size_t i = 1; i + 1 < lines.size(); i += 2) {
const std::string& pack_name = lines[i + 1];
for (const std::string& stem : {pack_name, pack_name + "_texcache"}) {
std::string full_path = pack_dir + "/" + stem;
mount(full_path, DEFAULT_ETERPACK_KEY, DEFAULT_SECURITY_KEY, false);
}
}
return true;
}
bool EterPackVFS::exists(std::string_view path) const {
if (m_search_mode == VFSSearchMode::DiskFirst) {
if (fs::exists(path)) return true;
}
std::lock_guard<std::mutex> lock(m_mutex);
std::string norm = normalize_vfs_path(path);
if (m_file_map.find(norm) != m_file_map.end()) {
return true;
}
uint32_t crc = GetCRC32(path);
if (m_crc_map.find(crc) != m_crc_map.end()) {
return true;
}
if (m_search_mode == VFSSearchMode::PackFirst) {
if (fs::exists(path)) return true;
}
return false;
}
bool EterPackVFS::read_file(std::string_view path, std::vector<uint8_t>& out_data) const {
auto read_from_disk = [&]() -> bool {
if (!fs::exists(path) || fs::is_directory(path)) return false;
std::ifstream f(std::string(path), std::ios::binary);
if (!f.is_open()) return false;
f.seekg(0, std::ios::end);
size_t sz = static_cast<size_t>(f.tellg());
f.seekg(0, std::ios::beg);
out_data.resize(sz);
return static_cast<bool>(f.read(reinterpret_cast<char*>(out_data.data()), sz));
};
if (m_search_mode == VFSSearchMode::DiskFirst) {
if (read_from_disk()) return true;
}
{
std::lock_guard<std::mutex> lock(m_mutex);
std::string norm = normalize_vfs_path(path);
auto it = m_file_map.find(norm);
if (it == m_file_map.end()) {
uint32_t crc = GetCRC32(path);
it = m_crc_map.find(crc) != m_crc_map.end() ?
m_file_map.find(normalize_vfs_path(m_crc_map.find(crc)->second.entry->filename)) :
m_file_map.end();
}
if (it != m_file_map.end()) {
const auto& loc = it->second;
return m_archives[loc.archive_index]->extract(*loc.entry, out_data);
}
}
if (m_search_mode == VFSSearchMode::PackFirst) {
if (read_from_disk()) return true;
}
return false;
}
bool EterPackVFS::exists_crc(uint32_t crc) const {
std::lock_guard<std::mutex> lock(m_mutex);
return m_crc_map.find(crc) != m_crc_map.end();
}
bool EterPackVFS::read_file_crc(uint32_t crc, std::vector<uint8_t>& out_data) const {
std::lock_guard<std::mutex> lock(m_mutex);
auto it = m_crc_map.find(crc);
if (it != m_crc_map.end()) {
const auto& loc = it->second;
return m_archives[loc.archive_index]->extract(*loc.entry, out_data);
}
return false;
}
std::vector<std::string> EterPackVFS::list_files() const {
std::lock_guard<std::mutex> lock(m_mutex);
std::vector<std::string> result;
result.reserve(m_file_map.size());
for (const auto& [name, loc] : m_file_map) {
result.push_back(loc.entry->filename);
}
return result;
}
} // namespace eterpack