Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/decoder.cpp
ShadPKG / common / decoder.cpp
1// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3 
4#include <fmt/format.h>
5#include "common/decoder.h"
6 
7namespace Common {
8 
9DecoderImpl::DecoderImpl() {
10 ZydisDecoderInit(&m_decoder, ZYDIS_MACHINE_MODE_LONG_64, ZYDIS_STACK_WIDTH_64);
11 ZydisFormatterInit(&m_formatter, ZYDIS_FORMATTER_STYLE_INTEL);
12}
13 
14DecoderImpl::~DecoderImpl() = default;
15 
16std::string DecoderImpl::disassembleInst(ZydisDecodedInstruction& inst,
17 ZydisDecodedOperand* operands, u64 address) {
18 const int bufLen = 256;
19 char szBuffer[bufLen];
20 ZydisFormatterFormatInstruction(&m_formatter, &inst, operands, inst.operand_count_visible,
21 szBuffer, sizeof(szBuffer), address, ZYAN_NULL);
22 return szBuffer;
23}
24 
25void DecoderImpl::printInstruction(void* code, u64 address) {
26 ZydisDecodedInstruction instruction;
27 ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE];
28 ZyanStatus status =
29 ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code), &instruction, operands);
30 if (!ZYAN_SUCCESS(status)) {
31 fmt::print("decode instruction failed at {}\n", fmt::ptr(code));
32 } else {
33 printInst(instruction, operands, address);
34 }
35}
36 
37void DecoderImpl::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,
38 u64 address) {
39 std::string s = disassembleInst(inst, operands, address);
40 fmt::print("instruction: {}\n", s);
41}
42 
43ZyanStatus DecoderImpl::decodeInstruction(ZydisDecodedInstruction& inst,
44 ZydisDecodedOperand* operands, void* data, u64 size) {
45 return ZydisDecoderDecodeFull(&m_decoder, data, size, &inst, operands);
46}
47 
48} // namespace Common
49