Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLO_inflate.c « intern « inflate « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62c90fcb06a7ed04ba9de1fb8fe8aff5e5790889 (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
 * $Id$
 *
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * 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 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * 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, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 * zlib inflate decompression wrapper library
 */

#include <stdio.h>
#include <stdlib.h>

#include "zlib.h"

#include "GEN_messaging.h"

#include "BLO_readStreamGlue.h"
#include "BLO_in_de_flateHeader.h"	/* used by deflate and inflate */

#include "BLO_inflate.h"

// TODO use other error function
static int CHECK_ERR(int err, char *msg);

static int CHECK_ERR(int err, char *msg)
{ 
	if (err != Z_OK) {
#ifndef NDEBUG
		fprintf(GEN_errorstream,
				"%s error: %d\n",
				msg,
				err); 
#endif
		return 1;
	}
	return 0;
}

struct inflateStructType {
	uInt compresSize;	/* fixed compresBuf size in bytes */
	Bytef *compresBuf;	/* reusable fixed size output buffer for inflate */
	struct readStreamGlueStruct *streamGlue;
	struct BLO_in_de_flateHeaderStruct *streamHeader;
	unsigned int streamDone;
	unsigned char headerbuffer[IN_DE_FLATEHEADERSTRUCTSIZE];
	z_stream d_stream;	/* decompression stream */
	char dictionary[50];
	uLong dictId;		/* Adler32 value of the dictionary */
	void *endControl;
};

/**
 * zlib inflate decompression initializer
 * @retval pointer to inflate control structure
 */
	BLO_inflateStructHandle
BLO_inflate_begin(
	void *endControl)
{
	int err = 0;     /* our own error */
	char *errmessage = "inflateInit";
	
	struct inflateStructType *control;
	control = malloc(sizeof(struct inflateStructType));
	if (!control) return NULL;

	control->compresSize = (100000 * 1.1) + 12;
	control->compresBuf = (Bytef *)malloc(control->compresSize);
	if (!control->compresBuf) {
		free(control);
		return NULL;
	}

	control->streamGlue = NULL;
	control->streamHeader = malloc(IN_DE_FLATEHEADERSTRUCTSIZE);
	if (!control->streamHeader) {
		free(control->compresBuf);
		free(control);
		return NULL; 
	}

	control->streamHeader->magic = 0;
	control->streamHeader->compressedLength = 0;
	control->streamHeader->uncompressedLength = 0;
	control->streamHeader->dictionary_id = 0;
	control->streamHeader->dictId = 0;
	control->streamHeader->crc = 0;
	control->streamDone = 0;
	memset(control->headerbuffer, 0, IN_DE_FLATEHEADERSTRUCTSIZE);
	control->d_stream.zalloc = (alloc_func)0;
	control->d_stream.zfree = (free_func)0;
	control->d_stream.opaque = (voidpf)0;
	// TODO use dictionary index, this is id = 1 :
	strcpy(control->dictionary, "sure this is not a number");

	/* we need to rewire this to also return err */
	err = inflateInit(&(control->d_stream));
	err = CHECK_ERR(err, errmessage);
	if (err) {
		free(control->compresBuf);
		free(control->streamHeader);
		free(control);
		return NULL;
	}

	control->dictId = control->d_stream.adler;

	control->d_stream.next_out = control->compresBuf;
	control->d_stream.avail_out = control->compresSize;

	control->d_stream.next_in = NULL;
	control->d_stream.avail_in = 0;

	control->endControl = endControl;
	return((BLO_inflateStructHandle) control);
}

/**
 * zlib inflate dataprocessor wrapper
 * @param BLO_inflate Pointer to inflate control structure
 * @param data Pointer to new data
 * @param dataIn New data amount
 * @retval streamGlueRead return value
 */
	int
BLO_inflate_process(
	BLO_inflateStructHandle BLO_inflate_handle,
	unsigned char *data,
	unsigned int dataIn)
{
	int zlib_err = 0;
	int err = 0;
	char *errmsg1 = "inflateSetDictionary";
	
	struct inflateStructType *BLO_inflate =
		(struct inflateStructType *) BLO_inflate_handle;

	if (!BLO_inflate) {
		err = BRS_SETFUNCTION(BRS_INFLATE) |
			  BRS_SETGENERR(BRS_NULL);
		return err;
	}

	/* First check if we have our header filled in yet */
	if (BLO_inflate->streamHeader->compressedLength == 0) {
		unsigned int processed;
		if (dataIn == 0) return err;	/* really need data to do anything */
		processed = ((dataIn + BLO_inflate->streamDone) <=
					 IN_DE_FLATEHEADERSTRUCTSIZE)
					 ? dataIn : IN_DE_FLATEHEADERSTRUCTSIZE;
		memcpy(BLO_inflate->headerbuffer + BLO_inflate->streamDone,
			   data, processed);
		BLO_inflate->streamDone += processed;
		dataIn -= processed;
		data += processed;
		if (BLO_inflate->streamDone == IN_DE_FLATEHEADERSTRUCTSIZE) {
			/* we have the whole header, absorb it */
			struct BLO_in_de_flateHeaderStruct *header;
			uint32_t crc;
			header = (struct BLO_in_de_flateHeaderStruct *)
				BLO_inflate->headerbuffer;
			BLO_inflate->streamHeader->compressedLength =
				ntohl(header->compressedLength);
			BLO_inflate->streamHeader->uncompressedLength =
				ntohl(header->uncompressedLength);
			BLO_inflate->streamHeader->dictId =
				ntohl(header->dictId);
			BLO_inflate->streamHeader->dictionary_id =
				ntohl(header->dictionary_id);
			crc = crc32(0L, (const Bytef *) header,
						IN_DE_FLATEHEADERSTRUCTSIZE - 4);

			if (header->magic == 'B') {
#ifndef NDEBUG
				fprintf(GEN_errorstream,
						"BLO_in_de_flateHeaderStruct Magic confirmed\n");
#endif
			} else {
#ifndef NDEBUG
				fprintf(GEN_errorstream,
						"ERROR BLO_in_de_flateHeaderStruct Magic NOT confirmed\n");
#endif
				err = BRS_SETFUNCTION(BRS_INFLATE) |
					  BRS_SETGENERR(BRS_MAGIC);
				if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
				if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
				if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
				free(BLO_inflate);
				return err;
			}

			if (crc == ntohl(header->crc)) {
#ifndef NDEBUG
				fprintf(GEN_errorstream,
						"BLO_in_de_flateHeader CRC correct\n");
#endif
			} else {
#ifndef NDEBUG
				fprintf(GEN_errorstream,
						"ERROR BLO_in_de_flateHeader CRC NOT correct\n");
#endif
				err = BRS_SETFUNCTION(BRS_INFLATE) |
					  BRS_SETGENERR(BRS_CRCHEADER);
				if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
				if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
				if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
				free(BLO_inflate);
				return err;
			}
			
#ifndef NDEBUG
			fprintf(GEN_errorstream,
					"BLO_inflate_process gets %u compressed bytes, will be %u uncompressed\n",
					(unsigned int) BLO_inflate->streamHeader->compressedLength,
					(unsigned int) BLO_inflate->streamHeader->uncompressedLength);
#endif

		}
	}

	/* Is there really (still) new data available ? */
	if (dataIn > 0) {
		int inflateWantsToLoopAgain = 0;
		BLO_inflate->d_stream.next_in = data;
		BLO_inflate->d_stream.avail_in = dataIn;
		do {
			zlib_err = inflate(&(BLO_inflate->d_stream), Z_SYNC_FLUSH);
			if (zlib_err == Z_NEED_DICT) {
				// TODO we can use BLO_inflate->d_stream.adler (it has
				// multiple uses) to select the dictionary to use. This is id=1
				zlib_err = inflateSetDictionary(&(BLO_inflate->d_stream),
								   (const Bytef*)BLO_inflate->dictionary,
								   strlen(BLO_inflate->dictionary));
				err = CHECK_ERR(zlib_err, errmsg1);
				if (err) {
					err = BRS_SETFUNCTION(BRS_INFLATE) |
						  BRS_SETSPECERR(BRS_INFLATEERROR);
					if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
					if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
					if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
					free(BLO_inflate);
					return err;
				}

				// go again
				zlib_err = inflate(&(BLO_inflate->d_stream), Z_SYNC_FLUSH);
			}
			if (zlib_err == Z_STREAM_END) {
#ifndef NDEBUG
				fprintf(GEN_errorstream,
						"Note: inflate returned Z_STREAM_END\n");
#endif
			} else if (zlib_err != Z_OK) {
#ifndef NDEBUG
				fprintf(GEN_errorstream, "Error: inflate should return Z_OK, not %d\n", zlib_err);
#endif
				err = BRS_SETFUNCTION(BRS_INFLATE) |
					  BRS_SETSPECERR(BRS_INFLATEERROR);
				if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
				if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
				if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
				free(BLO_inflate);
				return err;
			}
			if (BLO_inflate->d_stream.avail_out == 0) {
#ifndef NDEBUG
				fprintf(GEN_errorstream,
						"Note: inflate needs more output space, loop again %u\n",
						(unsigned int) BLO_inflate->d_stream.avail_in);
#endif
				inflateWantsToLoopAgain = 1;
			} else {
#ifndef NDEBUG
				if (inflateWantsToLoopAgain == 1)
					fprintf(GEN_errorstream,
							"Note: this is inflates last loop\n");
#endif
				inflateWantsToLoopAgain = 0;

#ifndef NDEBUG
				fprintf(GEN_errorstream, "inflated %u to %u (flushes) err=%d\n",
						dataIn,
						(unsigned int) (BLO_inflate->compresSize - BLO_inflate->d_stream.avail_out),
						err);
#endif
			}

			// give data to streamGlueRead, it will find out what to do next
			err = readStreamGlue(
				BLO_inflate->endControl,
				&(BLO_inflate->streamGlue),
				BLO_inflate->compresBuf,
				BLO_inflate->compresSize - BLO_inflate->d_stream.avail_out);
			BLO_inflate->d_stream.next_out = BLO_inflate->compresBuf;
			BLO_inflate->d_stream.avail_out = BLO_inflate->compresSize;
		} while (inflateWantsToLoopAgain == 1);
	}
	return err;
}

/**
 * zlib inflate final call and cleanup
 * @param BLO_inflate Pointer to inflate control structure
 * @retval streamGlueRead return value
 */
	int
BLO_inflate_end(
	BLO_inflateStructHandle BLO_inflate_handle)
{
	char *errmsg2 = "inflateEnd";
	int err = 0;
	int zlib_err = 0;
	struct inflateStructType *BLO_inflate =
		(struct inflateStructType *) BLO_inflate_handle;
	// TODO perhaps check streamHeader->totalStreamLength

	if (!BLO_inflate) {
		err = BRS_SETFUNCTION(BRS_INFLATE) |
			  BRS_SETGENERR(BRS_NULL);
		return err;
	}
	
	BLO_inflate->d_stream.avail_in = 0;
	// Note: do not also set BLO_inflate->d_stream.next_in to NULL, it
	// is illegal (zlib.h:374) and causes a Z_STREAM_ERROR

	zlib_err = inflate(&(BLO_inflate->d_stream), Z_FINISH);
	if (zlib_err != Z_STREAM_END) {
#ifdef NDEBUG
		fprintf(GEN_errorstream,
				"inflate should report Z_STREAM_END, not %d\n",
				err);
		
		if (BLO_inflate->d_stream.avail_out == 0) {
			fprintf(GEN_errorstream,
					"Error: inflate wanted more output buffer space\n");
			// Note that we CANNOT inflate-loop again !
			// But this should never happen because we Z_SYNC_FLUSH
		}
#endif
		err = BRS_SETFUNCTION(BRS_INFLATE) |
			  BRS_SETSPECERR(BRS_INFLATEERROR);
		if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
		if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
		if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
		free(BLO_inflate);
		return err;
	}

	zlib_err = inflateEnd(&(BLO_inflate->d_stream));
	err = CHECK_ERR(zlib_err, errmsg2);
	if (err) {
		err = BRS_SETFUNCTION(BRS_INFLATE) |
			  BRS_SETSPECERR(BRS_INFLATEERROR);
		if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
		if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
		if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
		free(BLO_inflate);
		return err;
	}

	if (BLO_inflate->d_stream.adler != BLO_inflate->dictId) {
		// data was corrupted
#ifndef NDEBUG
		fprintf(GEN_errorstream,
				"Failed adler checksum\n");
#endif 
		err = BRS_SETFUNCTION(BRS_INFLATE) |
			  BRS_SETGENERR(BRS_CRCDATA);
		if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
		if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
		if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
		free(BLO_inflate);
		return err;
	} else {
#ifndef NDEBUG
		fprintf(GEN_errorstream,
				"Passed adler checksum\n");
#endif
	}

	/* ready decompressing */
#ifndef NDEBUG
	fprintf(GEN_errorstream,
			"DeCompressed %ld bytes to %ld (%.0f%%)\n",
			BLO_inflate->d_stream.total_in, BLO_inflate->d_stream.total_out,
			100. * (float)BLO_inflate->d_stream.total_out /
			(float)BLO_inflate->d_stream.total_in);
#endif

	err = readStreamGlue(
		BLO_inflate->endControl,
		&(BLO_inflate->streamGlue),
		BLO_inflate->compresBuf,
		BLO_inflate->compresSize - BLO_inflate->d_stream.avail_out);
	
	BLO_inflate->d_stream.next_out = BLO_inflate->compresBuf;
	BLO_inflate->d_stream.avail_out = BLO_inflate->compresSize;
	
	if (BLO_inflate->streamGlue) free(BLO_inflate->streamGlue);
	if (BLO_inflate->streamHeader) free(BLO_inflate->streamHeader);
	if (BLO_inflate->compresBuf) free(BLO_inflate->compresBuf);
	free(BLO_inflate);
	
	return err;
}