forked from Bush2021/chrome_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPakFile.h
184 lines (151 loc) · 5.36 KB
/
PakFile.h
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
184
#pragma warning(disable : 4334)
#pragma warning(disable : 4267)
extern "C"
{
#include "..\mini_gzip\miniz.c"
#include "..\mini_gzip\mini_gzip.h"
#include "..\mini_gzip\mini_gzip.c"
}
#pragma pack(push)
#pragma pack(1)
#define PACK4_FILE_VERSION (4)
#define PACK5_FILE_VERSION (5)
struct PAK4_HEADER
{
uint32_t num_entries;
uint8_t encodeing;
};
struct PAK5_HEADER
{
uint32_t encodeing;
uint16_t resource_count;
uint16_t alias_count;
};
struct PAK_ENTRY
{
uint16_t resource_id;
uint32_t file_offset;
};
struct PAK_ALIAS
{
uint16_t resource_id;
uint16_t entry_index;
};
#pragma pack(pop)
bool CheckHeader(uint8_t *buffer, PAK_ENTRY *&pak_entry, PAK_ENTRY *&end_entry)
{
uint32_t version = *(uint32_t *)buffer;
if (version != PACK4_FILE_VERSION && version != PACK5_FILE_VERSION)
return false;
if (version == PACK4_FILE_VERSION)
{
PAK4_HEADER *pak_header = (PAK4_HEADER *)(buffer + sizeof(uint32_t));
if (pak_header->encodeing != 1)
return false;
pak_entry = (PAK_ENTRY *)(buffer + sizeof(uint32_t) + sizeof(PAK4_HEADER));
end_entry = pak_entry + pak_header->num_entries;
}
if (version == PACK5_FILE_VERSION)
{
PAK5_HEADER *pak_header = (PAK5_HEADER *)(buffer + sizeof(uint32_t));
if (pak_header->encodeing != 1)
return false;
pak_entry = (PAK_ENTRY *)(buffer + sizeof(uint32_t) + sizeof(PAK5_HEADER));
end_entry = pak_entry + pak_header->resource_count;
}
// 为了保存最后一条的"下一条",这条特殊的条目的id一定为0
if (!end_entry || end_entry->resource_id != 0)
return false;
return true;
}
template <typename Function>
void PakFind(uint8_t *buffer, uint8_t *pos, Function f)
{
PAK_ENTRY *pak_entry = NULL;
PAK_ENTRY *end_entry = NULL;
// 检查文件头
if (!CheckHeader(buffer, pak_entry, end_entry))
return;
do
{
PAK_ENTRY *next_entry = pak_entry + 1;
if (pos >= buffer + pak_entry->file_offset && pos <= buffer + next_entry->file_offset)
{
f(buffer + pak_entry->file_offset, next_entry->file_offset - pak_entry->file_offset);
break;
}
pak_entry = next_entry;
} while (pak_entry->resource_id != 0);
}
template <typename Function>
void TraversalGZIPFile(uint8_t *buffer, Function f)
{
PAK_ENTRY *pak_entry = NULL;
PAK_ENTRY *end_entry = NULL;
// 检查文件头
if (!CheckHeader(buffer, pak_entry, end_entry))
return;
do
{
PAK_ENTRY *next_entry = pak_entry + 1;
uint32_t old_size = next_entry->file_offset - pak_entry->file_offset;
if (old_size < 10 * 1024)
{
// 小于10k文件跳过
pak_entry = next_entry;
continue;
}
BYTE gzip[] = {0x1F, 0x8B, 0x08};
size_t gzip_len = sizeof(gzip);
if (memcmp(buffer + pak_entry->file_offset, gzip, gzip_len) != 0)
{
// 不是gzip文件跳过
pak_entry = next_entry;
continue;
}
uint32_t original_size = *(uint32_t *)(buffer + next_entry->file_offset - 4);
uint8_t *unpack_buffer = (uint8_t *)malloc(original_size);
if (!unpack_buffer)
return;
struct mini_gzip gz;
mini_gz_start(&gz, buffer + pak_entry->file_offset, old_size);
int unpack_len = mini_gz_unpack(&gz, unpack_buffer, original_size);
if (original_size == unpack_len)
{
uint32_t new_len = old_size;
bool changed = f(unpack_buffer, unpack_len, new_len);
if (changed)
{
// 如果有改变
size_t compress_size = 0;
uint8_t *compress_buffer = (uint8_t *)gzip_compress(unpack_buffer, new_len, &compress_size);
if (compress_buffer && compress_size < old_size)
{
/*FILE *fp = fopen("test.gz", "wb");
fwrite(compress_buffer, compress_size, 1, fp);
fclose(fp);*/
// gzip头
memcpy(buffer + pak_entry->file_offset, compress_buffer, 10);
// extra
buffer[pak_entry->file_offset + 3] = 0x04;
uint16_t extra_length = old_size - compress_size - 2;
memcpy(buffer + pak_entry->file_offset + 10, &extra_length, sizeof(extra_length));
memset(buffer + pak_entry->file_offset + 12, '\0', extra_length);
// compress
memcpy(buffer + pak_entry->file_offset + 12 + extra_length, compress_buffer + 10, compress_size - 10);
/*fp = fopen("test2.gz", "wb");
fwrite(buffer + pak_entry->file_offset, old_size, 1, fp);
fclose(fp);*/
}
else
{
DebugLog(L"gzip compress error %d %d", compress_size, old_size);
}
if (compress_buffer)
free(compress_buffer);
}
}
free(unpack_buffer);
pak_entry = next_entry;
} while (pak_entry->resource_id != 0);
}