-
Notifications
You must be signed in to change notification settings - Fork 1
/
packer.c
98 lines (77 loc) · 2.43 KB
/
packer.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
int main( int argc, char *argv[] ) {
FILE *source, *dest, *fileTable, *timFile;
char filenameBuffer[256], *fileBuffer, buffer;
uint32_t address, currPos, totalSize, timSize;
int bytes, i;
if (argc != 2) {
printf("Usage: packer.exe filename\nExample: packer.exe 0001");
return -1;
}
source = fopen(argv[1], "rb");
if (!source) {
printf("Error opening file %s", argv[1]);
return -1;
}
//get file total size
fseek(source, 0L, SEEK_END);
totalSize = ftell(source);
fileBuffer = (char*) malloc(sizeof(char)* totalSize);
fseek(source, 0, SEEK_SET);
sprintf(filenameBuffer, "%s_filetable.txt", argv[1]);
fileTable = fopen(filenameBuffer, "r");
if (!fileTable) {
printf("Error opening file %s. There's a file table created with the extractor?\n", filenameBuffer);
return -1;
}
//making backup file
sprintf(filenameBuffer, "%s.bak", argv[1]);
dest = fopen(filenameBuffer, "wb");
if (!dest) {
printf("Error writing file %s. Did you have write permission here?\n", filenameBuffer);
fclose(source);
return -1;
}
while (0 < (bytes = fread(&buffer, 1, sizeof(buffer), source))) fwrite(&buffer, 1, bytes, dest);
fclose(dest);
fclose(source);
//write at original file as output, using backup as original file
source = fopen(filenameBuffer, "rb");
dest = fopen(argv[1], "wb");
currPos = 0;
i = 0;
while (fscanf(fileTable, "%d\n", &address)!= EOF) {
//write from currPos until address from original file
fseek(source, currPos, SEEK_SET);
fread(fileBuffer, address - currPos, 1, source);
fwrite(fileBuffer, address - currPos, 1, dest);
currPos += address - currPos;
sprintf(filenameBuffer, "%s_extract_%d.tim", argv[1], i);
i++;
printf("Patching %s into 0x%.4x ... \n", filenameBuffer, address);
timFile = fopen(filenameBuffer, "rb");
if (!source) {
printf("Error opening file %s", filenameBuffer);
fclose(source);
fclose(dest);
return -1;
}
//write file content into the packed file
fseek(timFile, 0L, SEEK_END);
timSize = ftell(timFile);
fseek(timFile, 0, SEEK_SET);
fread(fileBuffer, timSize, 1, timFile);
fwrite(fileBuffer, timSize, 1, dest);
currPos += timSize;
fclose(timFile);
}
//remains? write here!
fseek(source, currPos, SEEK_SET);
fread(fileBuffer, totalSize - currPos, 1, source);
fwrite(fileBuffer, totalSize - currPos, 1, dest);
fclose(source);
fclose(dest);
return 0;
}