1 | /* |
2 | * uzlib - tiny deflate/inflate library (deflate, gzip, zlib) |
3 | * |
4 | * Copyright (c) 2003 by Joergen Ibsen / Jibz |
5 | * All Rights Reserved |
6 | * |
7 | * http://www.ibsensoftware.com/ |
8 | * |
9 | * Copyright (c) 2014-2018 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 4 |
41 | #define FNAME 8 |
42 | #define 16 |
43 | |
44 | void tinf_skip_bytes(TINF_DATA *d, int num); |
45 | uint16_t tinf_get_uint16(TINF_DATA *d); |
46 | |
47 | void tinf_skip_bytes(TINF_DATA *d, int num) |
48 | { |
49 | while (num--) uzlib_get_byte(d); |
50 | } |
51 | |
52 | uint16_t tinf_get_uint16(TINF_DATA *d) |
53 | { |
54 | unsigned int v = uzlib_get_byte(d); |
55 | v = (uzlib_get_byte(d) << 8) | v; |
56 | return v; |
57 | } |
58 | |
59 | int (TINF_DATA *d) |
60 | { |
61 | unsigned char flg; |
62 | |
63 | /* -- check format -- */ |
64 | |
65 | /* check id bytes */ |
66 | if (uzlib_get_byte(d) != 0x1f || uzlib_get_byte(d) != 0x8b) return TINF_DATA_ERROR; |
67 | |
68 | /* check method is deflate */ |
69 | if (uzlib_get_byte(d) != 8) return TINF_DATA_ERROR; |
70 | |
71 | /* get flag byte */ |
72 | flg = uzlib_get_byte(d); |
73 | |
74 | /* check that reserved bits are zero */ |
75 | if (flg & 0xe0) return TINF_DATA_ERROR; |
76 | |
77 | /* -- find start of compressed data -- */ |
78 | |
79 | /* skip rest of base header of 10 bytes */ |
80 | tinf_skip_bytes(d, 6); |
81 | |
82 | /* skip extra data if present */ |
83 | if (flg & FEXTRA) |
84 | { |
85 | unsigned int xlen = tinf_get_uint16(d); |
86 | tinf_skip_bytes(d, xlen); |
87 | } |
88 | |
89 | /* skip file name if present */ |
90 | if (flg & FNAME) { while (uzlib_get_byte(d)); } |
91 | |
92 | /* skip file comment if present */ |
93 | if (flg & FCOMMENT) { while (uzlib_get_byte(d)); } |
94 | |
95 | /* check header crc if present */ |
96 | if (flg & FHCRC) |
97 | { |
98 | /*unsigned int hcrc =*/ tinf_get_uint16(d); |
99 | |
100 | // TODO: Check! |
101 | // if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff)) |
102 | // return TINF_DATA_ERROR; |
103 | } |
104 | |
105 | /* initialize for crc32 checksum */ |
106 | d->checksum_type = TINF_CHKSUM_CRC; |
107 | d->checksum = ~0; |
108 | |
109 | return TINF_OK; |
110 | } |
111 | |