-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain.c
178 lines (165 loc) · 5.28 KB
/
main.c
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
#if defined(_WIN32) && defined(_LGPL)
#include "main.h"
#endif
#include "pak_defs.h"
#include "pak_file.h"
#include "pak_file_io.h"
#include "pak_get_file_type.h"
#include "pak_header.h"
#include "pak_pack.h"
#define HELP_TEXT \
"Pack/Unpack chrome pak file.\n\n" \
"%s -u pak_file destination_path\n" \
"Unpack chrome pak file at pak_file to destination_path.\n\n" \
"%s -p pak_index_file destination_pak_file\n" \
"Pack chrome pak file using pak_index_file to destination_pak_file.\n" \
"pak_index_file would be found in unpacked path.\n" \
"\nNote: existing destination files would be overwritten\n"
void printHelp() {
// get self path
char selfName[PATH_MAX];
#ifdef _WIN32
GetModuleFileName(NULL, selfName, PATH_MAX);
// get file name from path
const char *ptr = strrchr(selfName, '\\');
#else
int ret = readlink("/proc/self/exe", selfName, sizeof(selfName) - 1);
if (ret == -1)
strcpy(selfName, "pak");
else
selfName[ret] = 0;
// get file name from path
const char *ptr = strrchr(selfName, '/');
#endif
if (ptr != NULL)
strcpy(selfName, ptr + 1);
printf(HELP_TEXT, selfName, selfName);
}
int pakUnpackPath(char *pakFilePath, char *outputPath) {
PakFile pakFile = readFile(pakFilePath);
if (pakFile.buffer == NULL) {
printf("Error: cannot read pak file %s", pakFilePath);
return 1;
}
MyPakHeader myHeader;
if (!pakParseHeader(pakFile.buffer, &myHeader)) {
return 2;
}
if (!pakCheckFormat(pakFile.buffer, pakFile.size)) {
return 3;
}
if (!pakUnpack(pakFile.buffer, outputPath)) {
freeFile(pakFile);
return 4;
}
freeFile(pakFile);
return 0;
}
int pakPackIndexFile(char *indexPath, char *outputFilePath) {
int returnCode = 0;
PakFile pakPackedFile = NULL_File;
PakFile pakIndexFile = NULL_File;
char *filesPath = NULL;
bool freeFilesPath = false;
char *outputFilePath2 = NULL;
char *index = strrchr(indexPath, '\\');
if (index == NULL) {
index = strrchr(indexPath, '/');
}
if (index != NULL && index > indexPath) {
filesPath = calloc(index - indexPath + 2, sizeof(char));
if (filesPath == NULL) {
returnCode = 5;
goto PAK_PACK_INDEX_END;
}
strncpy(filesPath, indexPath, index - indexPath + 1);
freeFilesPath = true;
} else {
filesPath = "";
}
pakIndexFile = readFile(indexPath);
if (pakIndexFile.buffer == NULL) {
printf("Error: cannot read file %s", indexPath);
returnCode = 6;
goto PAK_PACK_INDEX_END;
}
// workaround outputFilePath="" after pakPack()
outputFilePath2 = calloc(strlen(outputFilePath) + 1, sizeof(char));
if (outputFilePath2 == NULL) {
returnCode = 7;
goto PAK_PACK_INDEX_END;
}
strcpy(outputFilePath2, outputFilePath);
pakPackedFile = pakPack(pakIndexFile, filesPath);
if (pakPackedFile.buffer == NULL) {
returnCode = 8;
goto PAK_PACK_INDEX_END;
}
if (!writeFile(outputFilePath2, pakPackedFile)) {
printf("Error: cannot write to %s", outputFilePath2);
returnCode = 9;
goto PAK_PACK_INDEX_END;
}
PAK_PACK_INDEX_END:
if (pakIndexFile.buffer != NULL)
freeFile(pakIndexFile);
if (pakPackedFile.buffer != NULL)
freeFile(pakPackedFile);
if (outputFilePath2 != NULL)
free(outputFilePath2);
if (freeFilesPath && filesPath != NULL)
free(filesPath);
return returnCode;
}
#define PAK_FLAGS_HELP 0
#define PAK_FLAGS_UNPACK 1
#define PAK_FLAGS_PACK 2
int main(int argc, char *argv[]) {
uint32_t flags = 0;
bool process = false;
char path1[PATH_MAX];
memset(path1, 0, PATH_MAX);
char path2[PATH_MAX];
memset(path2, 0, PATH_MAX);
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
bool isConfig = false;
if (*arg != '\0' && (*arg == '/' || *arg == '-')) {
arg++;
isConfig = true;
}
if (isConfig) {
switch (*arg) {
case 'h':
flags = PAK_FLAGS_HELP;
break;
case 'a':
case 'p':
flags = PAK_FLAGS_PACK;
break;
case 'u':
case 'e':
case 'x':
flags = PAK_FLAGS_UNPACK;
break;
}
}
if ((flags == PAK_FLAGS_UNPACK || flags == PAK_FLAGS_PACK) &&
argc - i > 2) {
strcpy(path1, argv[i + 1]);
strcpy(path2, argv[i + 2]);
process = true;
break;
}
}
if (flags == PAK_FLAGS_HELP || !process) {
printHelp();
return 0;
}
if (flags == PAK_FLAGS_UNPACK) {
return pakUnpackPath(path1, path2);
} else if (flags == PAK_FLAGS_PACK) {
return pakPackIndexFile(path1, path2);
}
return 32;
}