8000 extmod/uzlib: Add tinfgzip.c (gzip header parsing) from upstream. · sparkfun/circuitpython@d8a4d9d · GitHub
[go: up one dir, main page]

Skip to content

Commit d8a4d9d

Browse files
committed
extmod/uzlib: Add tinfgzip.c (gzip header parsing) from upstream.
1 parent 4c63986 commit d8a4d9d

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

extmod/uzlib/tinfgzip.c

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
* Copyright (c) 2014-2016 by Paul Sokolovsky
10+
*
11+
* This software is provided 'as-is', without any express
12+
* or implied warranty. In no event will the authors be
13+
* held liable for any damages arising from the use of
14+
* this software.
15+
*
16+
* Permission is granted to anyone to use this software
17+
* for any purpose, including commercial applications,
18+
* and to alter it and redistribute it freely, subject to
19+
* the following restrictions:
20+
*
21+
* 1. The origin of this software must not be
22+
* misrepresented; you must not claim that you
23+
* wrote the original software. If you use this
24+
* software in a product, an acknowledgment in
25+
* the product documentation would be appreciated
26+
* but is not required.
27+
*
28+
* 2. Altered source versions must be plainly marked
29+
* as such, and must not be misrepresented as
30+
* being the original software.
31+
*
32+
* 3. This notice may not be removed or altered from
33+
* any source distribution.
34+
*/
35+
36+
#include "tinf.h"
37+
38+
#define FTEXT 1
39+
#define FHCRC 2
40+
#define FEXTRA 4
41+
#define FNAME 8
42+
#define FCOMMENT 16
43+
44+
void tinf_skip_bytes(TINF_DATA *d, int num)
45+
{
46+
while (num--) uzlib_get_byte(d);
47+
}
48+
49+
uint16_t tinf_get_uint16(TINF_DATA *d)
50+
{
51+
unsigned int v = uzlib_get_byte(d);
52+
v = (uzlib_get_byte(d) << 8) | v;
53+
return v;
54+
}
55+
56+
int uzlib_gzip_parse_header(TINF_DATA *d)
57+
{
58+
unsigned char flg;
59+
60+
/* -- check format -- */
61+
62+
/* check id bytes */
63+
if (uzlib_get_byte(d) != 0x1f || uzlib_get_byte(d) != 0x8b) return TINF_DATA_ERROR;
64+
65+
/* check method is deflate */
66+
if (uzlib_get_byte(d) != 8) return TINF_DATA_ERROR;
67+
68+
/* get flag byte */
69+
flg = uzlib_get_byte(d);
70+
71+
/* check that reserved bits are zero */
72+
if (flg & 0xe0) return TINF_DATA_ERROR;
73+
74+
/* -- find start of compressed data -- */
75+
76+
/* skip rest of base header of 10 bytes */
77+
tinf_skip_bytes(d, 6);
78+
79+
/* skip extra data if present */
80+
if (flg & FEXTRA)
81+
{
82+
unsigned int xlen = tinf_get_uint16(d);
83+
tinf_skip_bytes(d, xlen);
84+
}
85+
86+
/* skip file name if present */
87+
if (flg & FNAME) { while (uzlib_get_byte(d)); }
88+
89+
/* skip file comment if present */
90+
if (flg & FCOMMENT) { while (uzlib_get_byte(d)); }
91+
92+
/* check header crc if present */
93+
if (flg & FHCRC)
94+
{
95+
/*unsigned int hcrc =*/ tinf_get_uint16(d);
96+
97+
// TODO: Check!
98+
// if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff))
99+
// return TINF_DATA_ERROR;
100+
}
101+
102+
/* initialize for crc32 checksum */
103+
d->checksum_type = TINF_CHKSUM_CRC;
104+
d->checksum = ~0;
105+
106+
return TINF_OK;
107+
}

0 commit comments

Comments
 (0)
0