Skip to content

Commit bfb6af8

Browse files
committed
moduzlib: Import uzlib v1.1.
https://github.com/pfalcon/uzlib
1 parent 3416287 commit bfb6af8

File tree

4 files changed

+792
-0
lines changed

4 files changed

+792
-0
lines changed

extmod/uzlib/adler32.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Adler-32 checksum
3+
*
4+
* Copyright (c) 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+
* Adler-32 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+
#define A32_BASE 65521
42+
#define A32_NMAX 5552
43+
44+
unsigned int tinf_adler32(const void *data, unsigned int length)
45+
{
46+
const unsigned char *buf = (const unsigned char *)data;
47+
48+
unsigned int s1 = 1;
49+
unsigned int s2 = 0;
50+
51+
while (length > 0)
52+
{
53+
int k = length < A32_NMAX ? length : A32_NMAX;
54+
int i;
55+
56+
for (i = k / 16; i; --i, buf += 16)
57+
{
58+
s1 += buf[0]; s2 += s1; s1 += buf[1]; s2 += s1;
59+
s1 += buf[2]; s2 += s1; s1 += buf[3]; s2 += s1;
60+
s1 += buf[4]; s2 += s1; s1 += buf[5]; s2 += s1;
61+
s1 += buf[6]; s2 += s1; s1 += buf[7]; s2 += s1;
62+
63+
s1 += buf[8]; s2 += s1; s1 += buf[9]; s2 += s1;
64+
s1 += buf[10]; s2 += s1; s1 += buf[11]; s2 += s1;
65+
s1 += buf[12]; s2 += s1; s1 += buf[13]; s2 += s1;
66+
s1 += buf[14]; s2 += s1; s1 += buf[15]; s2 += s1;
67+
}
68+
69+
for (i = k % 16; i; --i) { s1 += *buf++; s2 += s1; }
70+
71+
s1 %= A32_BASE;
72+
s2 %= A32_BASE;
73+
74+
length -= k;
75+
}
76+
77+
return (s2 << 16) | s1;
78+
}

extmod/uzlib/tinf.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
3+
*
4+
* Copyright (c) 2003 by Joergen Ibsen / Jibz
5+
* All Rights Reserved
6+
* http://www.ibsensoftware.com/
7+
*
8+
* Copyright (c) 2014 by Paul Sokolovsky
9+
*/
10+
11+
#ifndef TINF_H_INCLUDED
12+
#define TINF_H_INCLUDED
13+
14+
#include <stdint.h>
15+
16+
/* calling convention */
17+
#ifndef TINFCC
18+
#ifdef __WATCOMC__
19+
#define TINFCC __cdecl
20+
#else
21+
#define TINFCC
22+
#endif
23+
#endif
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
#define TINF_OK 0
30+
#define TINF_DATA_ERROR (-3)
31+
#define TINF_DEST_OVERFLOW (-4)
32+
33+
/* data structures */
34+
35+
typedef struct {
36+
unsigned short table[16]; /* table of code length counts */
37+
unsigned short trans[288]; /* code -> symbol translation table */
38+
} TINF_TREE;
39+
40+
struct TINF_DATA;
41+
typedef struct TINF_DATA {
42+
const unsigned char *source;
43+
unsigned int tag;
44+
unsigned int bitcount;
45+
46+
/* Buffer start */
47+
unsigned char *destStart;
48+
/* Buffer total size */
49+
unsigned int destSize;
50+
/* Current pointer in buffer */
51+
unsigned char *dest;
52+
/* Remaining bytes in buffer */
53+
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);
59+
60+
TINF_TREE ltree; /* dynamic length/symbol tree */
61+
TINF_TREE dtree; /* dynamic distance tree */
62+
} TINF_DATA;
63+
64+
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();
80+
81+
int TINFCC tinf_uncompress(void *dest, unsigned int *destLen,
82+
const void *source, unsigned int sourceLen);
83+
84+
int TINFCC tinf_gzip_uncompress(void *dest, unsigned int *destLen,
85+
const void *source, unsigned int sourceLen);
86+
87+
int TINFCC tinf_zlib_uncompress(void *dest, unsigned int *destLen,
88+
const void *source, unsigned int sourceLen);
89+
90+
unsigned int TINFCC tinf_adler32(const void *data, unsigned int length);
91+
92+
unsigned int TINFCC tinf_crc32(const void *data, unsigned int length);
93+
94+
/* compression API */
95+
96+
void TINFCC tinf_compress(void *data, const uint8_t *src, unsigned slen);
97+
98+
#ifdef __cplusplus
99+
} /* extern "C" */
100+
#endif
101+
102+
#endif /* TINF_H_INCLUDED */

0 commit comments

Comments
 (0)