diff --git a/src/iw8/xsk/assembler.cpp b/src/iw8/xsk/assembler.cpp index 8f39fbfd..d23ddf90 100644 --- a/src/iw8/xsk/assembler.cpp +++ b/src/iw8/xsk/assembler.cpp @@ -147,7 +147,7 @@ void assembler::assemble_function(const function::ptr& func) if (func->id == 0) { - stack_->write_c_string(func->name); + stack_->write_c_string(encrypt_string(func->name)); } for (const auto& inst : func->instructions) @@ -272,19 +272,19 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_GetIString: script_->write(static_cast(inst->opcode)); script_->write(0); - stack_->write_c_string(inst->data[0]); + stack_->write_c_string(encrypt_string(inst->data[0])); break; case opcode::OP_GetAnimation: script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); - stack_->write_c_string(inst->data[0]); - stack_->write_c_string(inst->data[1]); + stack_->write_c_string(encrypt_string(inst->data[0])); + stack_->write_c_string(encrypt_string(inst->data[1])); break; case opcode::OP_GetAnimTree: script_->write(static_cast(inst->opcode)); script_->write(0); - stack_->write_c_string(inst->data[0]); + stack_->write_c_string(encrypt_string(inst->data[0])); break; case opcode::OP_waittillmatch: script_->write(static_cast(inst->opcode)); @@ -487,9 +487,9 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) const auto func_id = resolver::token_id(inst->data[1]); stack_->write(file_id); - if (file_id == 0) stack_->write_c_string(inst->data[0]); + if (file_id == 0) stack_->write_c_string(encrypt_string(inst->data[0])); stack_->write(func_id); - if (func_id == 0) stack_->write_c_string(inst->data[1]); + if (func_id == 0) stack_->write_c_string(encrypt_string(inst->data[1])); } void assembler::assemble_switch(const instruction::ptr& inst) @@ -522,7 +522,7 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) else { script_->write(i + 1); - stack_->write_c_string(inst->data[1 + (3 * i) + 1]); + stack_->write_c_string(encrypt_string(inst->data[1 + (3 * i) + 1])); } index += 4; @@ -566,7 +566,7 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) if (id > max_string_id) { stack_->write(0); - stack_->write_c_string(inst->data[0]); + stack_->write_c_string(encrypt_string(inst->data[0])); } } @@ -643,4 +643,21 @@ auto assembler::resolve_label(const std::string& name) -> std::int32_t throw asm_error("couldn't resolve label address of '" + name + "'!"); } +auto assembler::encrypt_string(const std::string& str) -> std::string +{ + if (str.starts_with("_encstr_") && str.size() % 2 == 0) + { + std::string data{}; + + for (auto i = 8u; i < str.size(); i += 2) + { + data += static_cast(std::stoul(str.substr(i, 2), 0, 16)); + } + + return data; + } + + return str; +} + } // namespace xsk::gsc::iw8 diff --git a/src/iw8/xsk/assembler.hpp b/src/iw8/xsk/assembler.hpp index 63bd2665..0f8c5872 100644 --- a/src/iw8/xsk/assembler.hpp +++ b/src/iw8/xsk/assembler.hpp @@ -36,6 +36,7 @@ private: void assemble_offset(std::int32_t offset); auto resolve_function(const std::string& name) -> std::int32_t; auto resolve_label(const std::string& name) -> std::int32_t; + auto encrypt_string(const std::string& str) -> std::string; }; } // namespace xsk::gsc::iw8 diff --git a/src/iw8/xsk/disassembler.cpp b/src/iw8/xsk/disassembler.cpp index dabfb0d0..2b910232 100644 --- a/src/iw8/xsk/disassembler.cpp +++ b/src/iw8/xsk/disassembler.cpp @@ -51,7 +51,7 @@ void disassembler::disassemble(const std::string& file, std::vectorindex = static_cast(script_->pos()); func->size = stack_->read(); func->id = stack_->read(); - func->name = func->id == 0 ? patch_enc_string(stack_->read_c_string()) : resolver::token_name(func->id); + func->name = func->id == 0 ? decrypt_string(stack_->read_c_string()) : resolver::token_name(func->id); dissasemble_function(func); @@ -190,16 +190,16 @@ void disassembler::dissasemble_instruction(const instruction::ptr& inst) case opcode::OP_GetString: case opcode::OP_GetIString: script_->seek(4); - inst->data.push_back(utils::string::to_literal(patch_enc_string(stack_->read_c_string()))); + inst->data.push_back(utils::string::to_literal(decrypt_string(stack_->read_c_string()))); break; case opcode::OP_GetAnimation: script_->seek(8); - inst->data.push_back(utils::string::quote(patch_enc_string(stack_->read_c_string()), false)); - inst->data.push_back(utils::string::quote(patch_enc_string(stack_->read_c_string()), false)); + inst->data.push_back(utils::string::quote(decrypt_string(stack_->read_c_string()), false)); + inst->data.push_back(utils::string::quote(decrypt_string(stack_->read_c_string()), false)); break; case opcode::OP_GetAnimTree: script_->seek(1); - inst->data.push_back(utils::string::quote(patch_enc_string(stack_->read_c_string()), false)); + inst->data.push_back(utils::string::quote(decrypt_string(stack_->read_c_string()), false)); break; case opcode::OP_waittillmatch: inst->data.push_back(utils::string::va("%i", script_->read())); @@ -387,9 +387,9 @@ void disassembler::disassemble_far_call(const instruction::ptr& inst, bool threa } const auto file_id = stack_->read(); - const auto file_name = file_id == 0 ? patch_enc_string(stack_->read_c_string()) : resolver::file_name(file_id); + const auto file_name = file_id == 0 ? decrypt_string(stack_->read_c_string()) : resolver::file_name(file_id); const auto func_id = stack_->read(); - const auto func_name = func_id == 0 ? patch_enc_string(stack_->read_c_string()) : resolver::token_name(func_id); + const auto func_name = func_id == 0 ? decrypt_string(stack_->read_c_string()) : resolver::token_name(func_id); inst->data.emplace(inst->data.begin(), func_name); inst->data.emplace(inst->data.begin(), file_name); @@ -420,7 +420,7 @@ void disassembler::disassemble_end_switch(const instruction::ptr& inst) if (value < 0x100000 && value > 0) { inst->data.push_back("case"); - inst->data.push_back(utils::string::quote(patch_enc_string(stack_->read_c_string()), false)); + inst->data.push_back(utils::string::quote(decrypt_string(stack_->read_c_string()), false)); } else if (value == 0) { @@ -455,7 +455,7 @@ void disassembler::disassemble_field_variable(const instruction::ptr& inst) if (id > max_string_id) { auto temp = stack_->read(); - name = temp == 0 ? patch_enc_string(stack_->read_c_string()) : std::to_string(temp); + name = temp == 0 ? decrypt_string(stack_->read_c_string()) : std::to_string(temp); } else { @@ -562,7 +562,7 @@ auto disassembler::resolve_function(const std::string& index) -> std::string throw disasm_error(utils::string::va("\"%s\" is not valid function address!", index.data())); } -auto disassembler::patch_enc_string(const std::string& str) -> std::string +auto disassembler::decrypt_string(const std::string& str) -> std::string { if (str.size() > 0 && ((static_cast(str[0]) & 0xC0) == 0x80)) { diff --git a/src/iw8/xsk/disassembler.hpp b/src/iw8/xsk/disassembler.hpp index c0be6db9..995a280d 100644 --- a/src/iw8/xsk/disassembler.hpp +++ b/src/iw8/xsk/disassembler.hpp @@ -36,7 +36,7 @@ private: auto disassemble_offset() -> std::int32_t; void resolve_local_functions(); auto resolve_function(const std::string& index) -> std::string; - auto patch_enc_string(const std::string& str) -> std::string; + auto decrypt_string(const std::string& str) -> std::string; void print_function(const function::ptr& func); void print_instruction(const instruction::ptr& inst); };