Skip to content

Commit 29f5682

Browse files
committed
Merge pull request adafruit#904 from pfalcon/moduzlib
Module "uzlib" - based on similarly named library
2 parents 5006258 + bfb6af8 commit 29f5682

File tree

12 files changed

+923
-2
lines changed

12 files changed

+923
-2
lines changed

extmod/moduzlib.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Paul Sokolovsky
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <unistd.h>
29+
#include <string.h>
30+
#include <time.h>
31+
#include <sys/time.h>
32+
#include <math.h>
33+
34+
#include "mpconfig.h"
35+
#include "misc.h"
36+
#include "qstr.h"
37+
#include "nlr.h"
38+
#include "obj.h"
39+
#include "runtime.h"
40+
41+
#if MICROPY_PY_UZLIB
42+
43+
#include "uzlib/tinf.h"
44+
45+
#if 0 // print debugging info
46+
#define DEBUG_printf DEBUG_printf
47+
#else // don't print debugging info
48+
#define DEBUG_printf(...) (void)0
49+
#endif
50+
51+
STATIC int mod_uzlib_grow_buf(TINF_DATA *d, unsigned alloc_req)
52+
{
53+
if (alloc_req < 256) {
54+
alloc_req = 256;
55+
}
56+
DEBUG_printf("uzlib: Resizing buffer to " UINT_FMT " bytes\n", d->destSize + alloc_req);
57+
d->destStart = m_renew(byte, d->destStart, d->destSize, d->destSize + alloc_req);
58+
d->destSize += alloc_req;
59+
return 0;
60+
}
61+
62+
STATIC mp_obj_t mod_uzlib_decompress(uint n_args, mp_obj_t *args) {
63+
mp_obj_t data = args[0];
64+
mp_buffer_info_t bufinfo;
65+
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
66+
67+
TINF_DATA *decomp = m_new_obj(TINF_DATA);
68+
DEBUG_printf("sizeof(TINF_DATA)=" UINT_FMT "\n", sizeof(*decomp));
69+
70+
decomp->destStart = m_new(byte, bufinfo.len);
71+
decomp->destSize = bufinfo.len;
72+
decomp->destGrow = mod_uzlib_grow_buf;
73+
decomp->source = bufinfo.buf;
74+
75+
int st = tinf_zlib_uncompress_dyn(decomp, bufinfo.len);
76+
if (st != 0) {
77+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st)));
78+
}
79+
80+
mp_obj_t res = mp_obj_new_bytearray_by_ref(decomp->dest - decomp->destStart, decomp->destStart);
81+
m_del_obj(TINF_DATA, decomp);
82+
return res;
83+
}
84+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_uzlib_decompress_obj, 1, 3, mod_uzlib_decompress);
85+
86+
STATIC const mp_map_elem_t mp_module_uzlib_globals_table[] = {
87+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uzlib) },
88+
{ MP_OBJ_NEW_QSTR(MP_QSTR_decompress), (mp_obj_t)&mod_uzlib_decompress_obj },
89+
};
90+
91+
STATIC const mp_obj_dict_t mp_module_uzlib_globals = {
92+
.base = {&mp_type_dict},
93+
.map = {
94+
.all_keys_are_qstrs = 1,
95+
.table_is_fixed_array = 1,
96+
.used = MP_ARRAY_SIZE(mp_module_uzlib_globals_table),
97+
.alloc = MP_ARRAY_SIZE(mp_module_uzlib_globals_table),
98+
.table = (mp_map_elem_t*)mp_module_uzlib_globals_table,
99+
},
100+
};
101+
102+
const mp_obj_module_t mp_module_uzlib = {
103+
.base = { &mp_type_module },
104+
.name = MP_QSTR_uzlib,
105+
.globals = (mp_obj_dict_t*)&mp_module_uzlib_globals,
106+
};
107+
108+
// Source files #include'd here to make sure they're compiled in
109+
// only if module is enabled by config setting.
110+
111+
#include "uzlib/tinflate.c"
112+
#include "uzlib/tinfzlib.c"
113+
#include "uzlib/adler32.c"
114+
115+
#endif //MICROPY_PY_UZLIB

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy