-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecompress.c
180 lines (159 loc) · 6.36 KB
/
decompress.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
179
180
/*
MIT License
Copyright (c) 2024 Curtis Whitley (TurboVega)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
// This implementation uses a window size of 256 bytes, and a code size of 10 bits.
// The maximum compressed byte string size is 16 bytes.
//
// Code bits:
// 0000000000
// 9876543210
// ----------
// 00xxxxxxxx New original data byte with value of xxxxxxxx
// 01iiiiiiii String of 4 bytes starting at window index iiiiiiii
// 10iiiiiiii String of 8 bytes starting at window index iiiiiiii
// 11iiiiiiii String of 16 bytes starting at window index iiiiiiii
//
#define COMPRESSION_WINDOW_SIZE 256 // power of 2
#define COMPRESSION_STRING_SIZE 16 // power of 2
#define COMPRESSION_TYPE_TURBO 'T' // TurboVega-style compression
#pragma pack(push, 1)
typedef struct {
uint8_t marker[3];
uint8_t type;
uint32_t orig_size;
} CompressionFileHeader;
#pragma pack(pop)
typedef void (*WriteDecompressedByte)(void* context, uint8_t);
typedef struct {
void* context;
WriteDecompressedByte write_fcn;
uint32_t window_size;
uint32_t window_write_index;
uint32_t input_count;
uint32_t output_count;
uint8_t window_data[COMPRESSION_WINDOW_SIZE];
uint16_t code;
uint8_t code_bits;
} DecompressionData;
void my_write_decompressed_byte(void* context, uint8_t orig_data) {
fwrite(&orig_data, 1, 1, (FILE*)context);
}
void agon_init_decompression(DecompressionData* dd, void* context, WriteDecompressedByte write_fcn) {
memset(dd, 0, sizeof(DecompressionData));
dd->context = context;
dd->write_fcn = write_fcn;
}
void agon_decompress_byte(DecompressionData* dd, uint8_t comp_byte) {
for (uint8_t bit = 0; bit < 8; bit++) {
dd->code = (dd->code << 1) | ((comp_byte & 0x80) ? 1 : 0);
comp_byte <<= 1;
if (++(dd->code_bits) >= 10) {
// Interpret the incoming code
uint16_t command = (dd->code >> 8);
uint8_t value = (uint8_t)dd->code;
dd->code = 0;
dd->code_bits = 0;
uint8_t size;
switch (command)
{
case 0: // value is copy of original byte
// Add the new decompressed byte to the window
dd->window_data[dd->window_write_index++] = value;
dd->window_write_index &= (COMPRESSION_WINDOW_SIZE - 1);
if (dd->window_size < COMPRESSION_WINDOW_SIZE) {
(dd->window_size)++;
}
(*(dd->write_fcn))(dd->context, value);
dd->output_count++;
continue;
case 1: // value is index to string of 4 bytes
size = 4;
break;
case 2: // value is index to string of 8 bytes
size = 8;
break;
case 3: // value is index to string of 16 bytes
size = 16;
break;
}
// Extract a byte string from the window
uint8_t string_data[COMPRESSION_STRING_SIZE];
uint32_t wi = value;
for (uint8_t si = 0; si < size; si++) {
uint8_t out_byte = dd->window_data[wi++];
wi &= (COMPRESSION_WINDOW_SIZE - 1);
string_data[si] = out_byte;
(*(dd->write_fcn))(dd->context, out_byte);
dd->output_count++;
}
}
}
}
int main(int argc, const char** argv) {
if (argc != 3) {
printf("Use: decompress <inputfilepath> <outputfilepath>\r\n");
return -3;
}
printf("Decompressing %s to %s\r\n", argv[1], argv[2]);
FILE* fin = fopen(argv[1], "rb");
if (fin) {
CompressionFileHeader hdr;
if (fread(&hdr, sizeof(hdr), 1, fin) != 1 ||
hdr.marker[0] != 'C' ||
hdr.marker[1] != 'm' ||
hdr.marker[2] != 'p' ||
hdr.type != COMPRESSION_TYPE_TURBO) {
fclose(fin);
printf("Not a Turbo-compressed file: %s", argv[2]);
return -4;
}
FILE* fout = fopen(argv[2], "wb");
if (fout) {
DecompressionData dd;
agon_init_decompression(&dd, fout, &my_write_decompressed_byte);
dd.input_count = sizeof(hdr);
uint8_t input;
while (fread(&input, 1, 1, fin) == 1) {
dd.input_count++;
agon_decompress_byte(&dd, input);
}
fclose(fout);
fclose(fin);
uint32_t pct = (dd.output_count * 100) / dd.input_count;
printf(" Decompressed %u input bytes to %u output bytes (%u%%)\r\n",
dd.input_count, dd.output_count, pct);
if (dd.output_count != hdr.orig_size) {
printf("Decompressed file size %u does not equal original size %u\r\n",
dd.output_count, hdr.orig_size);
return -5;
}
return 0;
} else {
fclose(fin);
printf("Cannot open %s", argv[2]);
return -2;
}
} else {
printf("Cannot open %s", argv[1]);
return -1;
}
}