2 * tinfgzip - tiny gzip decompressor
4 * Copyright (c) 2003 by Joergen Ibsen / Jibz
7 * http://www.ibsensoftware.com/
9 * This software is provided 'as-is', without any express
10 * or implied warranty. In no event will the authors be
11 * held liable for any damages arising from the use of
14 * Permission is granted to anyone to use this software
15 * for any purpose, including commercial applications,
16 * and to alter it and redistribute it freely, subject to
17 * the following restrictions:
19 * 1. The origin of this software must not be
20 * misrepresented; you must not claim that you
21 * wrote the original software. If you use this
22 * software in a product, an acknowledgment in
23 * the product documentation would be appreciated
24 * but is not required.
26 * 2. Altered source versions must be plainly marked
27 * as such, and must not be misrepresented as
28 * being the original software.
30 * 3. This notice may not be removed or altered from
31 * any source distribution.
43 int tinf_gzip_uncompress(void *dest, unsigned int *destLen,
44 const void *source, unsigned int sourceLen)
46 unsigned char *src = (unsigned char *)source;
47 unsigned char *dst = (unsigned char *)dest;
49 unsigned int dlen, crc32;
53 /* -- check format -- */
56 if (src[0] != 0x1f || src[1] != 0x8b) return TINF_DATA_ERROR;
58 /* check method is deflate */
59 if (src[2] != 8) return TINF_DATA_ERROR;
64 /* check that reserved bits are zero */
65 if (flg & 0xe0) return TINF_DATA_ERROR;
67 /* -- find start of compressed data -- */
69 /* skip base header of 10 bytes */
72 /* skip extra data if present */
75 unsigned int xlen = start[1];
76 xlen = 256*xlen + start[0];
80 /* skip file name if present */
81 if (flg & FNAME) { while (*start) ++start; ++start; }
83 /* skip file comment if present */
84 if (flg & FCOMMENT) { while (*start) ++start; ++start; }
86 /* check header crc if present */
89 unsigned int hcrc = start[1];
90 hcrc = 256*hcrc + start[0];
92 if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff))
93 return TINF_DATA_ERROR;
98 /* -- get decompressed length -- */
100 dlen = src[sourceLen - 1];
101 dlen = 256*dlen + src[sourceLen - 2];
102 dlen = 256*dlen + src[sourceLen - 3];
103 dlen = 256*dlen + src[sourceLen - 4];
105 /* -- get crc32 of decompressed data -- */
107 crc32 = src[sourceLen - 5];
108 crc32 = 256*crc32 + src[sourceLen - 6];
109 crc32 = 256*crc32 + src[sourceLen - 7];
110 crc32 = 256*crc32 + src[sourceLen - 8];
112 /* -- decompress data -- */
114 res = tinf_uncompress(dst, destLen, start, src + sourceLen - start - 8);
116 if (res != TINF_OK) {
117 puts("uncompress error");
118 return TINF_DATA_ERROR;
122 if (*destLen != dlen) {
123 printf("len error %d != %d", *destLen, dlen);
124 return TINF_DATA_ERROR;
127 /* -- check CRC32 checksum -- */
129 if (crc32 != tinf_crc32(dst, *destLen)) {
130 return TINF_DATA_ERROR;
133 printf("expected len %08x vs decoded %08x\n", dlen, *destLen);
134 printf("expected crc32 %08x vs calculated %08x\n", crc32, tinf_crc32(dst, *destLen));