Skip to content

Commit ff1c2b0

Browse files
committed
extmod/uzlib/: Update uzlib to v2.0.
New API supporting stream decompression.
1 parent bb19e7b commit ff1c2b0

File tree

5 files changed

+323
-252
lines changed

5 files changed

+323
-252
lines changed

extmod/uzlib/adler32.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
#define A32_BASE 65521
4242
#define A32_NMAX 5552
4343

44-
unsigned int tinf_adler32(const void *data, unsigned int length)
44+
unsigned int uzlib_adler32(const void *data, unsigned int length, unsigned int prev_sum /* 1 */)
4545
{
4646
const unsigned char *buf = (const unsigned char *)data;
4747

48-
unsigned int s1 = 1;
49-
unsigned int s2 = 0;
48+
unsigned int s1 = prev_sum & 0xffff;
49+
unsigned int s2 = prev_sum >> 16;
5050

5151
while (length > 0)
5252
{

extmod/uzlib/crc32.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* CRC32 checksum
3+
*
4+
* Copyright (c) 1998-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+
/*
35+
* CRC32 algorithm taken from the zlib source, which is
36+
* Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler
37+
*/
38+
39+
#include "tinf.h"
40+
41+
static const unsigned int tinf_crc32tab[16] = {
42+
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190,
43+
0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344,
44+
0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278,
45+
0xbdbdf21c
46+
};
47+
48+
/* crc is previous value for incremental computation, 0xffffffff initially */
49+
unsigned int uzlib_crc32(const void *data, unsigned int length, unsigned int crc)
50+
{
51+
const unsigned char *buf = (const unsigned char *)data;
52+
unsigned int i;
53+
54+
for (i = 0; i < length; ++i)
55+
{
56+
crc ^= buf[i];
57+
crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
58+
crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4);
59+
}
60+
61+
// return value suitable for passing in next time, for final value invert it
62+
return crc/* ^ 0xffffffff*/;
63+
}

extmod/uzlib/tinf.h

Lines changed: 46 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* All Rights Reserved
66
* http://www.ibsensoftware.com/
77
*
8-
* Copyright (c) 2014 by Paul Sokolovsky
8+
* Copyright (c) 2014-2016 by Paul Sokolovsky
99
*/
1010

1111
#ifndef TINF_H_INCLUDED
@@ -26,9 +26,17 @@
2626
extern "C" {
2727
#endif
2828

29+
/* ok status, more data produced */
2930
#define TINF_OK 0
31+
/* end of compressed stream reached */
32+
#define TINF_DONE 1
3033
#define TINF_DATA_ERROR (-3)
31-
#define TINF_DEST_OVERFLOW (-4)
34+
#define TINF_CHKSUM_ERROR (-4)
35+
36+
/* checksum types */
37+
#define TINF_CHKSUM_NONE 0
38+
#define TINF_CHKSUM_ADLER 1
39+
#define TINF_CHKSUM_CRC 2
3240

3341
/* data structures */
3442

@@ -40,6 +48,10 @@ typedef struct {
4048
struct TINF_DATA;
4149
typedef struct TINF_DATA {
4250
const unsigned char *source;
51+
/* If source above is NULL, this function will be used to read
52+
next byte from source stream */
53+
unsigned char (*readSource)(struct TINF_DATA *data);
54+
4355
unsigned int tag;
4456
unsigned int bitcount;
4557

@@ -51,49 +63,51 @@ typedef struct TINF_DATA {
5163
unsigned char *dest;
5264
/* Remaining bytes in buffer */
5365
unsigned int destRemaining;
54-
/* Argument is the allocation size which didn't fit into buffer. Note that
55-
exact mimumum size to grow buffer by is lastAlloc - destRemaining. But
56-
growing by this exact size is ineficient, as the next allocation will
57-
fail again. */
58-
int (*destGrow)(struct TINF_DATA *data, unsigned int lastAlloc);
66+
67+
/* Accumulating checksum */
68+
unsigned int checksum;
69+
char checksum_type;
70+
71+
int btype;
72+
int bfinal;
73+
unsigned int curlen;
74+
int lzOff;
75+
unsigned char *dict_ring;
76+
unsigned int dict_size;
77+
unsigned int dict_idx;
5978

6079
TINF_TREE ltree; /* dynamic length/symbol tree */
6180
TINF_TREE dtree; /* dynamic distance tree */
6281
} TINF_DATA;
6382

83+
#define TINF_PUT(d, c) \
84+
{ \
85+
*d->dest++ = c; \
86+
if (d->dict_ring) { d->dict_ring[d->dict_idx++] = c; if (d->dict_idx == d->dict_size) d->dict_idx = 0; } \
87+
}
6488

65-
/* low-level API */
66-
67-
/* Step 1: Allocate TINF_DATA structure */
68-
/* Step 2: Set destStart, destSize, and destGrow fields */
69-
/* Step 3: Set source field */
70-
/* Step 4: Call tinf_uncompress_dyn() */
71-
/* Step 5: In response to destGrow callback, update destStart and destSize fields */
72-
/* Step 6: When tinf_uncompress_dyn() returns, buf.dest points to a byte past last uncompressed byte */
73-
74-
int TINFCC tinf_uncompress_dyn(TINF_DATA *d);
75-
int TINFCC tinf_zlib_uncompress_dyn(TINF_DATA *d, unsigned int sourceLen);
76-
77-
/* high-level API */
78-
79-
void TINFCC tinf_init(void);
89+
unsigned char TINFCC uzlib_get_byte(TINF_DATA *d);
8090

81-
int TINFCC tinf_uncompress(void *dest, unsigned int *destLen,
82-
const void *source, unsigned int sourceLen);
91+
/* Decompression API */
8392

84-
int TINFCC tinf_gzip_uncompress(void *dest, unsigned int *destLen,
85-
const void *source, unsigned int sourceLen);
93+
void TINFCC uzlib_init(void);
94+
void TINFCC uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen);
95+
int TINFCC uzlib_uncompress(TINF_DATA *d);
96+
int TINFCC uzlib_uncompress_chksum(TINF_DATA *d);
8697

87-
int TINFCC tinf_zlib_uncompress(void *dest, unsigned int *destLen,
88-
const void *source, unsigned int sourceLen);
98+
int TINFCC uzlib_zlib_parse_header(TINF_DATA *d);
99+
int TINFCC uzlib_gzip_parse_header(TINF_DATA *d);
89100

90-
unsigned int TINFCC tinf_adler32(const void *data, unsigned int length);
101+
/* Compression API */
91102

92-
unsigned int TINFCC tinf_crc32(const void *data, unsigned int length);
103+
void TINFCC uzlib_compress(void *data, const uint8_t *src, unsigned slen);
93104

94-
/* compression API */
105+
/* Checksum API */
95106

96-
void TINFCC tinf_compress(void *data, const uint8_t *src, unsigned slen);
107+
/* prev_sum is previous value for incremental computation, 1 initially */
108+
uint32_t TINFCC uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum);
109+
/* crc is previous value for incremental computation, 0xffffffff initially */
110+
uint32_t TINFCC uzlib_crc32(const void *data, unsigned int length, uint32_t crc);
97111

98112
#ifdef __cplusplus
99113
} /* extern "C" */

0 commit comments

Comments
 (0)