/
pac5.cpp
executable file
·183 lines (140 loc) · 4.21 KB
/
pac5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// open horizon -- undefined_darkness@outlook.com
//
#include "pac5.h"
#include "memory/tmp_buffer.h"
#include <string.h>
#include <assert.h>
//------------------------------------------------------------
bool pac5_file::open(const char *name)
{
close();
if (!name)
return false;
if (!nya_resources::check_extension(name, ".PAC"))
{
nya_resources::log()<<"invalid extension, should be .PAC\n";
return false;
}
std::string tbl_name(name);
tbl_name.resize(tbl_name.size() - 3);
tbl_name.append("TBL");
auto tbl_data = nya_resources::get_resources_provider().access(tbl_name.c_str());
if (!tbl_data)
{
nya_resources::log()<<"unable to open corresponding .TBL file\n";
return false;
}
std::vector<uint32_t> tbls(tbl_data->get_size() / sizeof(uint32_t));
tbl_data->read_all(tbls.data());
tbl_data->release();
m_data = nya_resources::get_resources_provider().access(name);
if (!m_data)
{
nya_resources::log()<<"unable to open .PAC file\n";
return false;
}
size_t offset = 0;
const auto count = tbls[offset++];
const auto unknown_zero = tbls[offset++];
assert(unknown_zero == 0);
assert(offset + count * 2 <= tbls.size());
m_entries.resize(count);
for (auto &e: m_entries)
{
e.offset = tbls[offset++];
e.size = tbls[offset++];
assert(e.offset + e.size <= m_data->get_size());
}
if (offset == tbls.size())
{
for (auto &e: m_entries)
e.unpacked_size = e.size;
m_compressed = false;
return true;
}
assert(offset + count <= tbls.size());
for (auto &e: m_entries)
e.unpacked_size = tbls[offset++];
m_compressed = true;
return true;
}
//------------------------------------------------------------
void pac5_file::close()
{
if (m_data)
m_data->release();
m_entries.clear();
m_data = 0;
}
//------------------------------------------------------------
uint32_t pac5_file::get_file_size(int idx) const
{
if (idx < 0 || idx >= get_files_count())
return 0;
return m_entries[idx].unpacked_size;
}
//------------------------------------------------------------
static bool uncompress_ulz2(const uint8_t *in_data, uint32_t in_size, uint8_t *out_data)
{
struct ulz_header
{
char sign[4];
uint32_t size;
uint32_t pos;
uint32_t count_pos;
};
ulz_header h = *(const ulz_header *)in_data;
if (memcmp(h.sign, "Ulz\x1a", 4) != 0)
return false;
const uint32_t type = h.size >> 24;
if (type != 2)
return false;
const uint16_t c1 = (uint16_t)((h.pos & 0xFF000000) >> 24);
const uint16_t c2 = (uint16_t)((1 << (uint32_t)c1) + 0xFFFF);
h.size &= 0x00FFFFFF;
h.pos &= 0x00FFFFFF;
const int *flags = (int *)(in_data + sizeof(ulz_header));
int flag = *flags, flag_upd = 0;
const uint16_t *counts = (uint16_t *)(in_data + h.count_pos);
in_data += h.pos;
while (h.size)
{
if (!flag_upd--)
{
flag = *flags++;
flag_upd = 31;
}
const bool is_comp = flag >= 0;
flag <<= 1;
if (!is_comp)
{
*out_data++ = *in_data++, --h.size;
continue;
}
const uint16_t c = *counts++;
const uint32_t t_pos = (c & c2) + 1;
const uint32_t count = (c >> c1) + 3;
if (count > h.size)
return false;
auto *tmp = out_data - t_pos;
for (uint32_t j = 0; j < count; j++)
*out_data++ = tmp[j];
h.size -= count;
}
return true;
}
//------------------------------------------------------------
bool pac5_file::read_file_data(int idx, void *data) const
{
if (idx < 0 || idx >= get_files_count() || !data)
return false;
auto &e = m_entries[idx];
if (!m_compressed)
return m_data->read_chunk(data, e.size, e.offset);
nya_memory::tmp_buffer_scoped buf(e.size);
if (!m_data->read_chunk(buf.get_data(), e.size, e.offset))
return false;
return uncompress_ulz2((uint8_t *)buf.get_data(), e.size, (uint8_t *)data);
}
//------------------------------------------------------------