forked from jno6809/jc_reborn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuncompress.c
More file actions
235 lines (177 loc) · 5.26 KB
/
uncompress.c
File metadata and controls
235 lines (177 loc) · 5.26 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
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
/*
* This file is part of 'Johnny Reborn'
*
* An open-source engine for the classic
* 'Johnny Castaway' screensaver by Sierra.
*
* Copyright (C) 2019 Jeremie GUILLAUME
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include "mytypes.h"
#include "utils.h"
static int nextbit;
static uint8 current;
static uint32 inOffset;
static uint32 maxInOffset;
struct TCodeTableEntry {
uint16 prefix;
uint8 append;
};
static uint8 getByte(FILE *f)
{
if (inOffset >= maxInOffset) {
return 0;
}
else {
inOffset++;
uint8 b = (uint8) fgetc(f);
return b;
}
}
static uint16 getBits(FILE *f, uint32 n)
{
if (n == 0)
return 0;
uint32 x = 0;
for (uint32 i=0; i < n; i++) {
if (current & (1 << nextbit))
x |= (uint32) (1 << i);
nextbit++;
if (nextbit > 7) {
current = (uint8) getByte(f);
nextbit = 0;
}
}
return x;
}
uint8 *uncompressLZW(FILE *f, uint32 inSize, uint32 outSize)
{
uint8 *outData;
struct TCodeTableEntry codeTable[4096];
uint8 decodeStack[4096];
uint32 stackPtr = 0;
uint8 n_bits = 9;
uint32 free_entry = 257;
uint16 oldcode;
uint16 lastbyte;
uint32 bitpos = 0;
uint32 outOffset = 0;
if (outSize == 0)
fatalError("uncompressLZW() : can't uncompress to 0 bytes\n");
maxInOffset = inSize;
nextbit = 0;
inOffset = 0;
outData = safe_malloc(outSize * sizeof(uint8));
current = (uint8) getByte(f);
lastbyte = oldcode = getBits(f, n_bits);
outData[outOffset++] = (uint8) oldcode;
while (inOffset < inSize) {
uint16 newcode = getBits(f, n_bits);
bitpos += n_bits;
if (newcode == 256) {
uint32 nbits3 = n_bits << 3;
uint32 nskip = (nbits3 - ((bitpos - 1) % nbits3)) - 1;
getBits(f, nskip);
n_bits = 9;
free_entry = 256;
bitpos = 0;
}
else {
uint16 code = newcode;
if (code >= free_entry) {
if (stackPtr > 4095)
break;
decodeStack[stackPtr] = (uint8) lastbyte;
stackPtr++;
code = oldcode;
}
while (code > 255) {
if (code > 4095)
break;
decodeStack[stackPtr] = codeTable[code].append; // TODO stackPtr >= 4096 ?
stackPtr++;
code = codeTable[code].prefix;
}
decodeStack[stackPtr] = (uint8) code; // TODO stackPtr >= 4096 ?
stackPtr++;
lastbyte = code;
while (stackPtr > 0) {
stackPtr--;
if (outOffset >= outSize)
return outData;
outData[outOffset++] = decodeStack[stackPtr];
}
if (free_entry < 4096) {
codeTable[free_entry].prefix = (uint16)oldcode;
codeTable[free_entry].append = (uint8)lastbyte;
free_entry++;
uint32 temp = 1 << n_bits;
if (free_entry >= temp && n_bits < 12) {
n_bits++;
bitpos = 0;
}
}
oldcode = newcode;
}
}
if (inOffset != inSize)
fatalError("error while uncompressing LZW");
return outData;
}
uint8 *uncompressRLE(FILE *f, uint32 inSize, uint32 outSize)
{
uint8 *outData;
uint32 outOffset = 0;
inOffset = 0;
maxInOffset = inSize;
outData = safe_malloc(outSize * sizeof(uint8));
while (outOffset < outSize) {
uint8 control = (uint8) fgetc(f);
inOffset++;
if ((control & 0x80) == 0x80) {
uint8 length = control & 0x7F;
uint8 b = (uint8) fgetc(f);
inOffset++;
for (int i=0; i < length; i++)
outData[outOffset++] = b; // TODO outOffset > outSize ?
}
else {
for (int i=0; i < control; i++) {
outData[outOffset++] = (uint8) fgetc(f); // TODO idem
inOffset++;
}
}
}
if (inOffset != inSize)
fatalError("error while uncompressing RLE");
return outData;
}
uint8 *uncompress(FILE *f, uint8 compressionMethod, uint32 inSize, uint32 outSize)
{
switch (compressionMethod) {
case 1:
return uncompressRLE(f, inSize, outSize);
break;
case 2:
return uncompressLZW(f, inSize, outSize);
break;
default:
return NULL;
break;
}
}