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 
uzlib_zlib_parse_header(TINF_DATA * d)38 int uzlib_zlib_parse_header(TINF_DATA *d)
39 {
40    unsigned char cmf, flg;
41 
42    /* -- get header bytes -- */
43 
44    cmf = uzlib_get_byte(d);
45    flg = uzlib_get_byte(d);
46 
47    /* -- check format -- */
48 
49    /* check checksum */
50    if ((256*cmf + flg) % 31) return TINF_DATA_ERROR;
51 
52    /* check method is deflate */
53    if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR;
54 
55    /* check window size is valid */
56    if ((cmf >> 4) > 7) return TINF_DATA_ERROR;
57 
58    /* check there is no preset dictionary */
59    if (flg & 0x20) return TINF_DATA_ERROR;
60 
61    /* initialize for adler32 checksum */
62    d->checksum_type = TINF_CHKSUM_ADLER;
63    d->checksum = 1;
64 
65    return cmf >> 4;
66 }
67