summaryrefslogtreecommitdiffstats
path: root/main.c
blob: b52e32dc12625d99c42db08d63c4de1f561b49a8 (plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <time.h>

#include <zlib.h>

#include <assert.h>

#include "GenericList.h"

#define C4GroupMaxMaker 30
#define C4GroupMaxPassword 30

typedef struct {
	char id[25];
	uint8_t Reserved1[3];
	int32_t Ver1, Ver2;
	int32_t Entries;
	char Maker[C4GroupMaxMaker + 2];
	char Password[C4GroupMaxPassword + 2];
	int32_t Creation;
	int32_t Original;
	uint8_t Reserved2[92];
} __attribute__((__packed__)) C4GroupHeader;

typedef struct {
	char FileName[257];
	uint8_t Reserved1[3];
	int32_t Packed;
	int32_t Directory;
	int32_t Size;
	int32_t Reserved2;
	int32_t Offset;
	int32_t Modified;
	uint8_t HasCRC;
	uint32_t CRC;
	uint8_t Executable;
	uint8_t Reserved3[26];
} __attribute__((__packed__)) C4GroupEntryCore;

struct list_GroupEntryList;
typedef struct {
	C4GroupEntryCore core;
	uint8_t* data;
	struct list_GroupEntryList* children;
} C4GroupEntryData;

LIST_AUTO(C4GroupEntryData, GroupEntryList)
#define ForeachGroupEntry(list) LIST_FOREACH(GroupEntryList, list, entry)

off_t fileSizeFd(int fd)
{
	struct stat st;

	if(fstat(fd, &st) == -1)
	{
		return -1;
	}

	return st.st_size;
}

void memScrambleHeader(uint8_t* data)
{
	// XOR deface
	for(size_t i = 0; i < sizeof(C4GroupHeader); i++)
		data[i] ^= 237;
	// byte swap
	for(size_t i = 0; i + 2 < sizeof(C4GroupHeader); i += 3)
	{
		uint8_t temp = data[i];
		data[i] = data[i + 2];
		data[i + 2] = temp;
	}
}

void buildChildren(C4GroupEntryData* entry)
{
	C4GroupHeader* header = (C4GroupHeader*)entry->data;

	entry->children = GroupEntryListNew();

	uint8_t* data = entry->data + sizeof(C4GroupHeader);
	uint8_t* childData = entry->data + sizeof(C4GroupHeader) + sizeof(C4GroupEntryCore) * header->Entries;

	for(size_t i = 0; i < (size_t)header->Entries; ++i)
	{
		C4GroupEntryCore* core = (C4GroupEntryCore*)(data + sizeof(C4GroupEntryCore) * i);
		C4GroupEntryData* childEntry = &GroupEntryListAppend(entry->children, (C4GroupEntryData){.core = *core, .data = childData + core->Offset, .children = NULL})->value;

		if(core->Directory)
		{
			memScrambleHeader(childEntry->data);
			buildChildren(childEntry);
		}
	}
}

const char* formatTime(int32_t time)
{
	time_t tTime = time;
	static char ret[128];
	strftime(ret, sizeof(ret), "%c", localtime(&tTime));
	return ret;
}

void handleChildren(GroupEntryList* entries, size_t indent, const char* path)
{
	size_t pathLen = strlen(path);
	char* targetPath = malloc(pathLen + 260 + 1);

	strcpy(targetPath, path);
	strcpy(targetPath + pathLen++, "/");

	if(mkdir(path, 0755) == -1)
	{
		fprintf(stderr, "ERROR: Creating target directory \"%s\": %s\n", path, strerror(errno));
		goto ret;
	}

	ForeachGroupEntry(entries)
	{
		for(size_t i = 0; i < indent; ++i)
		{
			putchar('\t');
		}

		strcpy(targetPath + pathLen, entry->value.core.FileName);

// 		printf("%s\t%s\t%d B\n", targetPath, formatTime(entry->value.core.Modified), entry->value.core.Size);

		if(!entry->value.core.Directory)
		{
			int file = open(targetPath, O_WRONLY | O_CREAT, entry->value.core.Executable ? 0755 : 0644);
			if(file == -1)
			{
				fprintf(stderr, "ERROR: Creating target file \"%s\": %s\n", targetPath, strerror(errno));
				goto ret;
			}
			write(file, entry->value.data, entry->value.core.Size);

			if(close(file) == -1)
			{
				fprintf(stderr, "ERROR: Closing file \"%s\": %s\n", targetPath, strerror(errno));
			}

			struct timeval tv[2] = {{.tv_usec = 0, .tv_sec = entry->value.core.Modified}, {.tv_usec = 0, .tv_sec = entry->value.core.Modified}};
			if(utimes(targetPath, tv) == -1)
			{
				fprintf(stderr, "ERROR: Setting modification time for \"%s\": %s\n", targetPath, strerror(errno));
			}
		}

		if(entry->value.core.Directory)
		{
			handleChildren(entry->value.children, indent + 1, targetPath);
		}
	}

ret:
	free(targetPath);
}

void deleteChildren(GroupEntryList* entries)
{
	ForeachGroupEntry(entries)
	{
		if(entry->value.core.Directory)
		{
			deleteChildren(entry->value.children);
		}
	}

	GroupEntryListDestroy(entries);
}

int main(int argc, char* argv[])
{
	if(argc != 3)
	{
		fprintf(stderr, "USAGE: %s <group> <target>\n", argv[0]);
		return EXIT_FAILURE;
	}

	int file = open(argv[1], O_RDONLY);
	if(file == -1)
	{
		fprintf(stderr, "ERROR: Opening file \"%s\": %s\n", argv[1], strerror(errno));
		return EXIT_FAILURE;
	}

	__off_t size = fileSizeFd(file);
	if(size == -1)
	{
		fprintf(stderr, "ERROR: Getting file size: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}
	else
	{
		printf("Size: %ld\n", size);
	}

	uint8_t* mappedFile = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, file, 0);

	if(mappedFile == MAP_FAILED)
	{
		fprintf(stderr, "ERROR: Mapping file: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	if(close(file) == -1)
	{
		fprintf(stderr, "ERROR: Closing file: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	if((mappedFile[0] != 0x1e && mappedFile[0] != 0x1f) || (mappedFile[1] != 0x8c && mappedFile[1] != 0x8b))
	{
		fprintf(stderr, "ERROR: The file is not a valid group file. Magic bytes don't match.\n");
		return EXIT_FAILURE;
	}

	mappedFile[0] = 0x1f;
	mappedFile[1] = 0x8b;

	z_stream strm = {
		.zalloc = NULL,
		.zfree = NULL,
		.opaque = NULL,
		.next_in = mappedFile,
		.avail_in = size
	};

	int ret = inflateInit2(&strm, 15 + 16); // window size 15 + automatic gzip

	if(ret != Z_OK)
	{
		fprintf(stderr, "ERROR: inflateInit2: %s\n", zError(ret));
		return EXIT_FAILURE;
	}

	size_t currentSize = sizeof(C4GroupHeader);
	C4GroupHeader* header = malloc(currentSize);

	strm.next_out = (Bytef*)header;
	strm.avail_out = currentSize;

	ret = inflate(&strm, Z_SYNC_FLUSH);
	if(ret != Z_OK)
	{
		fprintf(stderr, "ERROR: inflating header: %s\n", zError(ret));
		return EXIT_FAILURE;
	}

	memScrambleHeader((uint8_t*)header);

	printf("Version %d.%d\n", header->Ver1, header->Ver2);
	printf("%d Entries\n", header->Entries);
	printf("%sOriginal\n", header->Original == 1234567 ? "" : "Not ");
	printf("Created %s\n", formatTime(header->Creation));
	puts(header->id);
	puts(header->Maker);

	assert(header->Ver1 == 1);
	assert(header->Ver2 == 2);

	currentSize += sizeof(C4GroupEntryCore) * header->Entries;
	header = realloc(header, currentSize);
	C4GroupEntryCore* cores = (C4GroupEntryCore*)((void*)(header) + sizeof(C4GroupHeader));

	GroupEntryList* entries = GroupEntryListNew();

	strm.next_out = (Bytef*)cores;
	strm.avail_out = sizeof(C4GroupEntryCore) * header->Entries;

	ret = inflate(&strm, Z_SYNC_FLUSH);
	if(ret != Z_OK)
	{
		fprintf(stderr, "ERROR: inflating header: %s\n", zError(ret));
		return EXIT_FAILURE;
	}

	C4GroupEntryCore* last = cores + header->Entries - 1;
	size_t uncompressedSize = last->Offset + last->Size;

	currentSize += uncompressedSize;

#define TMP_FILE "cc4group.tmp"
	int tmpFile = open(TMP_FILE, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0600);
	if(tmpFile == -1)
	{
		fprintf(stderr, "ERROR: Opening tmp file \"%s\": %s\n", "cc4group.tmp", strerror(errno));
		return EXIT_FAILURE;
	}

	if(unlink(TMP_FILE) == -1)
	{
		fprintf(stderr, "ERROR: Deleting tmp file \"%s\". Manual deletion is required: %s\n", TMP_FILE, strerror(errno));
	}
#undef TMP_FILE

	// allocate file size
	// https://gist.github.com/marcetcheverry/991042
	if(lseek(tmpFile, currentSize - 1, SEEK_SET) == -1)
	{
		fprintf(stderr, "ERROR: Seeking the tmp file to the end: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	if(write(tmpFile, "", 1) == -1)
	{
		fprintf(stderr, "ERROR: Writing to the tmp file's end: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	uint8_t* mmappedHeader = mmap(NULL, currentSize, PROT_READ | PROT_WRITE, MAP_SHARED, tmpFile, 0);
	if(mmappedHeader == MAP_FAILED)
	{
		fprintf(stderr, "ERROR: Mapping tmp file: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	if(close(tmpFile) == -1)
	{
		fprintf(stderr, "ERROR: Closing tmp file \"cc4group.tmp\": %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	uint8_t* data = (void*)(mmappedHeader) + sizeof(C4GroupHeader) + sizeof(C4GroupEntryCore) * header->Entries;

	// write alredy decompressed header and cores into the file
	memcpy(mmappedHeader, header, data - mmappedHeader);

	strm.next_out = data;
	strm.avail_out = uncompressedSize;

	ret = inflate(&strm, Z_SYNC_FLUSH);
	if(ret != Z_STREAM_END)
	{
		fprintf(stderr, "ERROR: inflating header: %s\n", zError(ret));
		return EXIT_FAILURE;
	}

	inflateEnd(&strm);

	if(munmap(mappedFile, size) == -1)
	{
		fprintf(stderr, "ERROR: Unmapping file: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	C4GroupEntryData root = {.data = (uint8_t*)mmappedHeader, .children = NULL};
	buildChildren(&root);

	handleChildren(root.children, 0, argv[2]);

	deleteChildren(entries);

	free(header);

	if(munmap(mmappedHeader, size) == -1)
	{
		fprintf(stderr, "ERROR: Unmapping tmp file: %s\n", strerror(errno));
		return EXIT_FAILURE;
	}

	return EXIT_SUCCESS;
}