MCPcopy Create free account
hub / github.com/OGSR/OGSR-Engine / open_archive_chunk

Function open_archive_chunk

ogsr_engine/xrCore/arc_db.cpp:7–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include "trivial_encryptor.h"
6
7static IReader* open_archive_chunk(void* ptr, u32 ID, const char* archiveName, size_t archiveSize)
8{
9 const bool shouldDecrypt = !strstr(archiveName, ".xdb");
10
11 u32 dwType = INVALID_SET_FILE_POINTER;
12 DWORD read_byte;
13 u32 dwPtr = SetFilePointer(ptr, 0, nullptr, FILE_BEGIN);
14 R_ASSERT(dwPtr != INVALID_SET_FILE_POINTER, archiveName, Debug.error2string(GetLastError()));
15 while (true)
16 {
17 bool res = ReadFile(ptr, &dwType, 4, &read_byte, nullptr);
18 R_ASSERT(res && read_byte == 4, archiveName, Debug.error2string(GetLastError()));
19
20 u32 tempSize = 0;
21 res = ReadFile(ptr, &tempSize, 4, &read_byte, nullptr);
22 size_t dwSize = tempSize;
23 R_ASSERT(res && read_byte == 4, archiveName, Debug.error2string(GetLastError()));
24
25 if ((dwType & ~CFS_CompressMark) == ID)
26 {
27 u8* src_data = xr_alloc<u8>(dwSize);
28 res = ReadFile(ptr, src_data, dwSize, &read_byte, nullptr);
29 R_ASSERT(res && read_byte == dwSize, archiveName, Debug.error2string(GetLastError()));
30 if (dwType & CFS_CompressMark)
31 {
32 BYTE* dest{};
33 size_t dest_sz{};
34
35 if (shouldDecrypt)
36 g_trivial_encryptor.decode(src_data, dwSize, src_data); // Try WW key first
37
38 bool result = _decompressLZ(&dest, &dest_sz, src_data, dwSize, archiveSize);
39
40 if (!result && shouldDecrypt) // Let's try to decode with Rus key
41 {
42 MsgDbg("~~[%s]: decoding of [%s] with WW key failed, trying RU key...", __FUNCTION__, archiveName);
43 g_trivial_encryptor.encode(src_data, dwSize, src_data); // rollback
44 g_trivial_encryptor.decode(src_data, dwSize, src_data, trivial_encryptor::key_flag::russian);
45 result = _decompressLZ(&dest, &dest_sz, src_data, dwSize, archiveSize);
46
47 if (!result)
48 {
49 g_trivial_encryptor.encode(src_data, dwSize, src_data, trivial_encryptor::key_flag::russian); // rollback
50
51 result = _decompressLZ(&dest, &dest_sz, src_data, dwSize, archiveSize);
52 if (result)
53 MsgDbg("--[%s]: found CS/COP archive [%s]", __FUNCTION__, archiveName);
54 }
55 }
56
57 CHECK_OR_EXIT(result, make_string("[%s] Can't decompress archive [%s]", __FUNCTION__, archiveName));
58
59 xr_free(src_data);
60 return xr_new<CTempReader>(dest, dest_sz, 0);
61 }
62 return xr_new<CTempReader>(src_data, dwSize, 0);
63 }
64

Callers 1

index_dbMethod · 0.85

Calls 7

ReadFileFunction · 0.85
_decompressLZFunction · 0.85
make_stringFunction · 0.85
xr_freeFunction · 0.85
error2stringMethod · 0.80
decodeMethod · 0.80
encodeMethod · 0.80

Tested by

no test coverage detected