Remove local modifications from tinfgzip.c that were included accidentally.
[oweals/u-boot_mod.git] / u-boot / lib_generic / tinfgzip.c
1 /*
2  * tinfgzip  -  tiny gzip decompressor
3  *
4  * Copyright (c) 2003 by Joergen Ibsen / Jibz
5  * All Rights Reserved
6  *
7  * http://www.ibsensoftware.com/
8  *
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
12  * this software.
13  *
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:
18  *
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.
25  *
26  * 2. Altered source versions must be plainly marked
27  *    as such, and must not be misrepresented as
28  *    being the original software.
29  *
30  * 3. This notice may not be removed or altered from
31  *    any source distribution.
32  */
33
34 #include "tinf.h"
35
36 #define FTEXT    1
37 #define FHCRC    2
38 #define FEXTRA   4
39 #define FNAME    8
40 #define FCOMMENT 16
41
42 int tinf_gzip_uncompress(void *dest, unsigned int *destLen,
43                          const void *source, unsigned int sourceLen)
44 {
45     unsigned char *src = (unsigned char *)source;
46     unsigned char *dst = (unsigned char *)dest;
47     unsigned char *start;
48     unsigned int dlen, crc32;
49     int res;
50     unsigned char flg;
51
52     /* -- check format -- */
53
54     /* check id bytes */
55     if (src[0] != 0x1f || src[1] != 0x8b) return TINF_DATA_ERROR;
56
57     /* check method is deflate */
58     if (src[2] != 8) return TINF_DATA_ERROR;
59
60     /* get flag byte */
61     flg = src[3];
62
63     /* check that reserved bits are zero */
64     if (flg & 0xe0) return TINF_DATA_ERROR;
65
66     /* -- find start of compressed data -- */
67
68     /* skip base header of 10 bytes */
69     start = src + 10;
70
71     /* skip extra data if present */
72     if (flg & FEXTRA)
73     {
74        unsigned int xlen = start[1];
75        xlen = 256*xlen + start[0];
76        start += xlen + 2;
77     }
78
79     /* skip file name if present */
80     if (flg & FNAME) { while (*start) ++start; ++start; }
81
82     /* skip file comment if present */
83     if (flg & FCOMMENT) { while (*start) ++start; ++start; }
84
85     /* check header crc if present */
86     if (flg & FHCRC)
87     {
88        unsigned int hcrc = start[1];
89        hcrc = 256*hcrc + start[0];
90
91        if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff))
92           return TINF_DATA_ERROR;
93
94        start += 2;
95     }
96
97     /* -- get decompressed length -- */
98
99     dlen =            src[sourceLen - 1];
100     dlen = 256*dlen + src[sourceLen - 2];
101     dlen = 256*dlen + src[sourceLen - 3];
102     dlen = 256*dlen + src[sourceLen - 4];
103
104     /* -- get crc32 of decompressed data -- */
105
106     crc32 =             src[sourceLen - 5];
107     crc32 = 256*crc32 + src[sourceLen - 6];
108     crc32 = 256*crc32 + src[sourceLen - 7];
109     crc32 = 256*crc32 + src[sourceLen - 8];
110
111     /* -- decompress data -- */
112
113     res = tinf_uncompress(dst, destLen, start, src + sourceLen - start - 8);
114
115     if (res != TINF_OK) return TINF_DATA_ERROR;
116
117     if (*destLen != dlen) return TINF_DATA_ERROR;
118
119     /* -- check CRC32 checksum -- */
120
121     if (crc32 != tinf_crc32(dst, dlen)) return TINF_DATA_ERROR;
122
123     return TINF_OK;
124 }