-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDeflate.c
295 lines (246 loc) · 8.49 KB
/
Deflate.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* NuFX archive manipulation library
* Copyright (C) 2000-2007 by Andy McFadden, All Rights Reserved.
* This is free software; you can redistribute it and/or modify it under the
* terms of the BSD License, see the file COPYING-LIB.
*
* Support for the "deflate" algorithm, via the "zlib" library.
*
* This compression format is totally unsupported on the Apple II. This
* is provided primarily for the benefit of Apple II emulators that want
* a better storage format for disk images than SHK+LZW or a ZIP file.
*
* This code was developed and tested with ZLIB_VERSION "1.1.3". It is
* expected to work with any version >= 1.1.3 and < 2.x. Please visit
* http://www.zlib.org/ for more information.
*/
#include "NufxLibPriv.h"
#ifdef ENABLE_DEFLATE
#include "zlib.h"
#define kNuDeflateLevel 9 /* use maximum compression */
/*
* Alloc and free functions provided to zlib.
*/
static voidpf Nu_zalloc(voidpf opaque, uInt items, uInt size)
{
return Nu_Malloc(opaque, items * size);
}
static void Nu_zfree(voidpf opaque, voidpf address)
{
Nu_Free(opaque, address);
}
/*
* ===========================================================================
* Compression
* ===========================================================================
*/
/*
* Compress "srcLen" bytes from "pStraw" to "fp".
*/
NuError Nu_CompressDeflate(NuArchive* pArchive, NuStraw* pStraw, FILE* fp,
uint32_t srcLen, uint32_t* pDstLen, uint16_t* pCrc)
{
NuError err = kNuErrNone;
z_stream zstream;
int zerr;
Bytef* outbuf = NULL;
Assert(pArchive != NULL);
Assert(pStraw != NULL);
Assert(fp != NULL);
Assert(srcLen > 0);
Assert(pDstLen != NULL);
Assert(pCrc != NULL);
err = Nu_AllocCompressionBufferIFN(pArchive);
if (err != kNuErrNone)
return err;
/* allocate a similarly-sized buffer for the output */
outbuf = Nu_Malloc(pArchive, kNuGenCompBufSize);
BailAlloc(outbuf);
/*
* Initialize the zlib stream.
*/
zstream.zalloc = Nu_zalloc;
zstream.zfree = Nu_zfree;
zstream.opaque = pArchive;
zstream.next_in = NULL;
zstream.avail_in = 0;
zstream.next_out = outbuf;
zstream.avail_out = kNuGenCompBufSize;
zstream.data_type = Z_UNKNOWN;
zerr = deflateInit(&zstream, kNuDeflateLevel);
if (zerr != Z_OK) {
err = kNuErrInternal;
if (zerr == Z_VERSION_ERROR) {
Nu_ReportError(NU_BLOB, err,
"installed zlib is not compatible with linked version (%s)",
ZLIB_VERSION);
} else {
Nu_ReportError(NU_BLOB, err,
"call to deflateInit failed (zerr=%d)", zerr);
}
goto bail;
}
/*
* Loop while we have data.
*/
do {
uint32_t getSize;
int flush;
/* should be able to read a full buffer every time */
if (zstream.avail_in == 0 && srcLen) {
getSize = (srcLen > kNuGenCompBufSize) ? kNuGenCompBufSize : srcLen;
DBUG(("+++ reading %ld bytes\n", getSize));
err = Nu_StrawRead(pArchive, pStraw, pArchive->compBuf, getSize);
if (err != kNuErrNone) {
Nu_ReportError(NU_BLOB, err, "deflate read failed");
goto z_bail;
}
srcLen -= getSize;
*pCrc = Nu_CalcCRC16(*pCrc, pArchive->compBuf, getSize);
zstream.next_in = pArchive->compBuf;
zstream.avail_in = getSize;
}
if (srcLen == 0)
flush = Z_FINISH; /* tell zlib that we're done */
else
flush = Z_NO_FLUSH; /* more to come! */
zerr = deflate(&zstream, flush);
if (zerr != Z_OK && zerr != Z_STREAM_END) {
err = kNuErrInternal;
Nu_ReportError(NU_BLOB, err, "zlib deflate call failed (zerr=%d)",
zerr);
goto z_bail;
}
/* write when we're full or when we're done */
if (zstream.avail_out == 0 ||
(zerr == Z_STREAM_END && zstream.avail_out != kNuGenCompBufSize))
{
DBUG(("+++ writing %d bytes\n", zstream.next_out - outbuf));
err = Nu_FWrite(fp, outbuf, zstream.next_out - outbuf);
if (err != kNuErrNone) {
Nu_ReportError(NU_BLOB, err, "fwrite failed in deflate");
goto z_bail;
}
zstream.next_out = outbuf;
zstream.avail_out = kNuGenCompBufSize;
}
} while (zerr == Z_OK);
Assert(zerr == Z_STREAM_END); /* other errors should've been caught */
*pDstLen = zstream.total_out;
z_bail:
deflateEnd(&zstream); /* free up any allocated structures */
bail:
if (outbuf != NULL)
free(outbuf);
return err;
}
/*
* ===========================================================================
* Expansion
* ===========================================================================
*/
/*
* Expand from "infp" to "pFunnel".
*/
NuError Nu_ExpandDeflate(NuArchive* pArchive, const NuRecord* pRecord,
const NuThread* pThread, FILE* infp, NuFunnel* pFunnel, uint16_t* pCrc)
{
NuError err = kNuErrNone;
z_stream zstream;
int zerr;
uint32_t compRemaining;
Bytef* outbuf;
Assert(pArchive != NULL);
Assert(pThread != NULL);
Assert(infp != NULL);
Assert(pFunnel != NULL);
err = Nu_AllocCompressionBufferIFN(pArchive);
if (err != kNuErrNone)
return err;
/* allocate a similarly-sized buffer for the output */
outbuf = Nu_Malloc(pArchive, kNuGenCompBufSize);
BailAlloc(outbuf);
compRemaining = pThread->thCompThreadEOF;
/*
* Initialize the zlib stream.
*/
zstream.zalloc = Nu_zalloc;
zstream.zfree = Nu_zfree;
zstream.opaque = pArchive;
zstream.next_in = NULL;
zstream.avail_in = 0;
zstream.next_out = outbuf;
zstream.avail_out = kNuGenCompBufSize;
zstream.data_type = Z_UNKNOWN;
zerr = inflateInit(&zstream);
if (zerr != Z_OK) {
err = kNuErrInternal;
if (zerr == Z_VERSION_ERROR) {
Nu_ReportError(NU_BLOB, err,
"installed zlib is not compatible with linked version (%s)",
ZLIB_VERSION);
} else {
Nu_ReportError(NU_BLOB, err,
"call to inflateInit failed (zerr=%d)", zerr);
}
goto bail;
}
/*
* Loop while we have data.
*/
do {
uint32_t getSize;
/* read as much as we can */
if (zstream.avail_in == 0) {
getSize = (compRemaining > kNuGenCompBufSize) ?
kNuGenCompBufSize : compRemaining;
DBUG(("+++ reading %ld bytes (%ld left)\n", getSize,
compRemaining));
err = Nu_FRead(infp, pArchive->compBuf, getSize);
if (err != kNuErrNone) {
Nu_ReportError(NU_BLOB, err, "inflate read failed");
goto z_bail;
}
compRemaining -= getSize;
zstream.next_in = pArchive->compBuf;
zstream.avail_in = getSize;
}
/* uncompress the data */
zerr = inflate(&zstream, Z_NO_FLUSH);
if (zerr != Z_OK && zerr != Z_STREAM_END) {
err = kNuErrInternal;
Nu_ReportError(NU_BLOB, err, "zlib inflate call failed (zerr=%d)",
zerr);
goto z_bail;
}
/* write every time there's anything (buffer will usually be full) */
if (zstream.avail_out != kNuGenCompBufSize) {
DBUG(("+++ writing %d bytes\n", zstream.next_out - outbuf));
err = Nu_FunnelWrite(pArchive, pFunnel, outbuf,
zstream.next_out - outbuf);
if (err != kNuErrNone) {
Nu_ReportError(NU_BLOB, err, "write failed in inflate");
goto z_bail;
}
if (pCrc != NULL)
*pCrc = Nu_CalcCRC16(*pCrc, outbuf, zstream.next_out - outbuf);
zstream.next_out = outbuf;
zstream.avail_out = kNuGenCompBufSize;
}
} while (zerr == Z_OK);
Assert(zerr == Z_STREAM_END); /* other errors should've been caught */
if (zstream.total_out != pThread->actualThreadEOF) {
err = kNuErrBadData;
Nu_ReportError(NU_BLOB, err,
"size mismatch on inflated file (%ld vs %u)",
zstream.total_out, pThread->actualThreadEOF);
goto z_bail;
}
z_bail:
inflateEnd(&zstream); /* free up any allocated structures */
bail:
if (outbuf != NULL)
free(outbuf);
return err;
}
#endif /*ENABLE_DEFLATE*/