-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunzip.c
More file actions
48 lines (37 loc) · 1.1 KB
/
Copy pathunzip.c
File metadata and controls
48 lines (37 loc) · 1.1 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
#include <stdio.h>
#include <stdlib.h>
#define MAX_OUTPUT 21000000
char output[MAX_OUTPUT];
int outputIndex = 0;
void Decompress(const char *inputFile, const char *outputFile) {
FILE *in = fopen(inputFile, "rb");
if (!in) {
printf("Failed to open input file.\n");
return;
}
unsigned short offset;
unsigned char length;
char ch;
while (fread(&offset, sizeof(unsigned short), 1, in) == 1 &&
fread(&length, sizeof(unsigned char), 1, in) == 1 &&
fread(&ch, sizeof(char), 1, in) == 1) {
if (length > 0 && offset > 0) {
int start = outputIndex - offset;
for (int i = 0; i < length && outputIndex < MAX_OUTPUT; i++) {
output[outputIndex] = output[start + i];
outputIndex++;
}
}
if (ch != '#') {
output[outputIndex++] = ch;
}
}
fclose(in);
FILE *out = fopen(outputFile, "w");
if (!out) {
printf("Failed to open output file.\n");
return;
}
fwrite(output, sizeof(char), outputIndex, out);
fclose(out);
}