-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
113 lines (103 loc) · 2.09 KB
/
main.c
File metadata and controls
113 lines (103 loc) · 2.09 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef unsigned char uchar;
char* bname = NULL;
char fname[256];
uchar pframe[0x300]; // prev frame data
uchar cframe[0x300]; // curr frame data
uchar xframe[0x300]; // curr ^ prev
uchar pdata[0x1000]; // packed
void help() {
printf("Usage:\n");
printf("-o outfile\n");
printf("-b basename (basename.XXX.rch)\n");
}
int packFrame(uchar* src, uchar* xor, uchar* dst) {
uchar* st = dst;
int adr = 0;
// int zcount;
// int ncount = 0;
uchar* ptr;
while (adr < 0x300) {
*dst = 0x00;
while ((xor[adr] == 0x00) && (adr < 0x300)) {
if (*dst == 0xff) {
dst++;
*dst = 0x00;
dst++;
*dst = 0x00;
}
*dst = *dst + 1;
adr++;
}
dst++;
if (adr > 0x2ff) break;
ptr = dst;
*ptr = 0x00;
dst++;
while ((xor[adr] != 0x00) && (adr < 0x300)) {
if (*ptr == 0xff) {
*dst = 0x00;
dst++;
ptr = dst;
dst++;
*ptr = 0x00;
}
*ptr = *ptr + 1;
*dst = src[adr];
dst++;
adr++;
}
}
return dst - st;
}
int processFrame(int num) {
sprintf(fname,"%s.%.3i.rch",bname,num); // filename XXX.rch
FILE* file = fopen(fname, "rb");
if (!file) return 0;
fread(cframe, 0x300, 1, file);
fclose(file);
int cnt;
for (cnt = 0; cnt < 0x300; cnt++)
xframe[cnt] = pframe[cnt] ^ cframe[cnt];
cnt = packFrame(cframe, xframe, pdata); // cnt = size of frame
memcpy(pframe, cframe, 0x300);
return cnt;
}
int main(int ac, char** av) {
char* oname = NULL;
int cnt = 1;
char* parg;
while (cnt < ac) {
parg = av[cnt++];
if (strcmp(parg, "-o") == 0) oname = av[cnt++];
if (strcmp(parg, "-b") == 0) bname = av[cnt++];
}
if (!oname || !bname) {
help();
return 1;
}
FILE* ofile = fopen(oname, "wb");
if (!ofile) {
printf("Can't open output file\n");
return 2;
}
fputc(0x00, ofile);
memset(pframe, 0, 0x300);
cnt = processFrame(0);
fwrite(pdata, cnt, 1, ofile);
int num = 1;
while (1) {
cnt = processFrame(num);
if (cnt == 0) break;
fwrite(pdata,cnt,1,ofile);
num++;
}
cnt = processFrame(0);
fwrite(pdata, cnt, 1, ofile);
rewind(ofile);
fputc(num, ofile);
fclose(ofile);
return 1;
}