|
| 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 | + * Optimised for MicroPython: |
| 12 | + * Copyright (c) 2023 by Jim Mussared |
| 13 | + * |
| 14 | + * This software is provided 'as-is', without any express |
| 15 | + * or implied warranty. In no event will the authors be |
| 16 | + * held liable for any damages arising from the use of |
| 17 | + * this software. |
| 18 | + * |
| 19 | + * Permission is granted to anyone to use this software |
| 20 | + * for any purpose, including commercial applications, |
| 21 | + * and to alter it and redistribute it freely, subject to |
| 22 | + * the following restrictions: |
| 23 | + * |
| 24 | + * 1. The origin of this software must not be |
| 25 | + * misrepresented; you must not claim that you |
| 26 | + * wrote the original software. If you use this |
| 27 | + * software in a product, an acknowledgment in |
| 28 | + * the product documentation would be appreciated |
| 29 | + * but is not required. |
| 30 | + * |
| 31 | + * 2. Altered source versions must be plainly marked |
| 32 | + * as such, and must not be misrepresented as |
| 33 | + * being the original software. |
| 34 | + * |
| 35 | + * 3. This notice may not be removed or altered from |
| 36 | + * any source distribution. |
| 37 | + */ |
| 38 | + |
| 39 | +#include "uzlib.h" |
| 40 | + |
| 41 | +#define FTEXT 1 |
| 42 | +#define FHCRC 2 |
| 43 | +#define FEXTRA 4 |
| 44 | +#define FNAME 8 |
| 45 | +#define FCOMMENT 16 |
| 46 | + |
| 47 | +void tinf_skip_bytes(uzlib_uncomp_t *d, int num); |
| 48 | +uint16_t tinf_get_uint16(uzlib_uncomp_t *d); |
| 49 | + |
| 50 | +void tinf_skip_bytes(uzlib_uncomp_t *d, int num) |
| 51 | +{ |
| 52 | + while (num--) uzlib_get_byte(d); |
| 53 | +} |
| 54 | + |
| 55 | +uint16_t tinf_get_uint16(uzlib_uncomp_t *d) |
| 56 | +{ |
| 57 | + unsigned int v = uzlib_get_byte(d); |
| 58 | + v = (uzlib_get_byte(d) << 8) | v; |
| 59 | + return v; |
| 60 | +} |
| 61 | + |
| 62 | +int uzlib_parse_zlib_gzip_header(uzlib_uncomp_t *d, int *wbits) |
| 63 | +{ |
| 64 | + /* -- check format -- */ |
| 65 | + unsigned char cmf = uzlib_get_byte(d); |
| 66 | + unsigned char flg = uzlib_get_byte(d); |
| 67 | + |
| 68 | + /* check for gzip id bytes */ |
| 69 | + if (cmf == 0x1f && flg == 0x8b) { |
| 70 | + /* check method is deflate */ |
| 71 | + if (uzlib_get_byte(d) != 8) return UZLIB_DATA_ERROR; |
| 72 | + |
| 73 | + /* get flag byte */ |
| 74 | + flg = uzlib_get_byte(d); |
| 75 | + |
| 76 | + /* check that reserved bits are zero */ |
| 77 | + if (flg & 0xe0) return UZLIB_DATA_ERROR; |
| 78 | + |
| 79 | + /* -- find start of compressed data -- */ |
| 80 | + |
| 81 | + /* skip rest of base header of 10 bytes */ |
| 82 | + tinf_skip_bytes(d, 6); |
| 83 | + |
| 84 | + /* skip extra data if present */ |
| 85 | + if (flg & FEXTRA) |
| 86 | + { |
| 87 | + unsigned int xlen = tinf_get_uint16(d); |
| 88 | + tinf_skip_bytes(d, xlen); |
| 89 | + } |
| 90 | + |
| 91 | + /* skip file name if present */ |
| 92 | + if (flg & FNAME) { while (uzlib_get_byte(d)); } |
| 93 | + |
| 94 | + /* skip file comment if present */ |
| 95 | + if (flg & FCOMMENT) { while (uzlib_get_byte(d)); } |
| 96 | + |
| 97 | + /* check header crc if present */ |
| 98 | + if (flg & FHCRC) |
| 99 | + { |
| 100 | + /*unsigned int hcrc =*/ tinf_get_uint16(d); |
| 101 | + |
| 102 | + // TODO: Check! |
| 103 | + // if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff)) |
| 104 | + // return UZLIB_DATA_ERROR; |
| 105 | + } |
| 106 | + |
| 107 | + /* initialize for crc32 checksum */ |
| 108 | + d->checksum_type = UZLIB_CHKSUM_CRC; |
| 109 | + d->checksum = ~0; |
| 110 | + |
| 111 | + return UZLIB_HEADER_GZIP; |
| 112 | + } else { |
| 113 | + /* check checksum */ |
| 114 | + if ((256*cmf + flg) % 31) return UZLIB_DATA_ERROR; |
| 115 | + |
| 116 | + /* check method is deflate */ |
| 117 | + if ((cmf & 0x0f) != 8) return UZLIB_DATA_ERROR; |
| 118 | + |
| 119 | + /* check window size is valid */ |
| 120 | + if ((cmf >> 4) > 7) return UZLIB_DATA_ERROR; |
| 121 | + |
| 122 | + /* check there is no preset dictionary */ |
| 123 | + if (flg & 0x20) return UZLIB_DATA_ERROR; |
| 124 | + |
| 125 | + /* initialize for adler32 checksum */ |
| 126 | + d->checksum_type = UZLIB_CHKSUM_ADLER; |
| 127 | + d->checksum = 1; |
| 128 | + |
| 129 | + *wbits = (cmf >> 4) + 8; |
| 130 | + |
| 131 | + return UZLIB_HEADER_ZLIB; |
| 132 | + } |
| 133 | +} |
0 commit comments