Skip to content

Commit 2693a4c

Browse files
authored
Merge pull request #6069 from gamblor21/uzlib-module
zlib Module
2 parents 1a329cf + 8ed7b11 commit 2693a4c

File tree

20 files changed

+326
-4
lines changed

20 files changed

+326
-4
lines changed

docs/library/zlib.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
.. module:: zlib
77
:synopsis: zlib decompression
8+
:noindex:
89

910
|see_cpython_module| :mod:`cpython:zlib`.
1011

@@ -26,6 +27,7 @@ Functions
2627
CPython and is ignored.
2728

2829
.. class:: DecompIO(stream, wbits=0, /)
30+
:noindex:
2931

3032
Create a ``stream`` wrapper which allows transparent decompression of
3133
compressed data in another *stream*. This allows to process compressed

lib/uzlib/uzlib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ typedef struct {
8383
} TINF_TREE;
8484

8585
struct uzlib_uncomp {
86+
/* Point to the CircuitPython object that owns this decompression stream */
87+
void *self;
8688
/* Pointer to the next byte in the input buffer */
8789
const unsigned char *source;
8890
/* Pointer to the next byte past the input buffer (source_limit = source + len) */

locale/circuitpython.pot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3019,7 +3019,7 @@ msgstr ""
30193019
msgid "complex values not supported"
30203020
msgstr ""
30213021

3022-
#: extmod/moduzlib.c
3022+
#: extmod/moduzlib.c shared-module/zlib/DecompIO.c
30233023
msgid "compression header"
30243024
msgstr ""
30253025

ports/atmel-samd/boards/cp_sapling_m0_spiflash/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ EXTERNAL_FLASH_DEVICES = AT25DF081A
1313

1414
CIRCUITPY_AUDIOIO = 0
1515
CIRCUITPY_AUDIOBUSIO = 0
16+
CIRCUITPY_ZLIB = 0

ports/atmel-samd/boards/matrixportal_m4/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CIRCUITPY_PARALLELDISPLAY = 0
1616
CIRCUITPY_SDCARDIO = 0
1717
CIRCUITPY_SHARPDISPLAY = 0
1818
CIRCUITPY_TRACEBACK = 0
19+
CIRCUITPY_ZLIB=0
1920

2021
# Include these Python libraries in firmware.
2122
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Requests

ports/atmel-samd/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ CIRCUITPY_SYNTHIO ?= 0
4646
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
4747
CIRCUITPY_ULAB = 0
4848
CIRCUITPY_VECTORIO = 0
49+
CIRCUITPY_ZLIB = 0
4950

5051
# TODO: In CircuitPython 8.0, turn this back on, after `busio.OneWire` is removed.
5152
# We'd like a smoother transition, but we can't afford the space to have both

ports/nrf/boards/bluemicro833/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ CIRCUITPY_TRACEBACK = 0
4242
CIRCUITPY_ULAB = 0
4343
CIRCUITPY_USB_MIDI = 0
4444
CIRCUITPY_VECTORIO = 0
45+
CIRCUITPY_ZLIB = 0
4546

4647
MICROPY_PY_ASYNC_AWAIT = 0
4748

ports/nrf/boards/pca10100/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ CIRCUITPY_SYNTHIO = 0
3232
CIRCUITPY_ULAB = 0
3333
CIRCUITPY_USB_MIDI = 0
3434
CIRCUITPY_VECTORIO = 0
35+
CIRCUITPY_ZLIB = 0
3536

3637
MICROPY_PY_ASYNC_AWAIT = 0
3738

ports/nrf/boards/simmel/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ CIRCUITPY_ULAB = 0
4242
CIRCUITPY_USB_CDC = 0
4343
CIRCUITPY_USB_MIDI = 0
4444
CIRCUITPY_WATCHDOG = 1
45+
CIRCUITPY_ZLIB = 0
4546

4647
# Enable micropython.native
4748
#CIRCUITPY_ENABLE_MPY_NATIVE = 1

ports/stm/boards/meowbit_v121/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ CIRCUITPY_BLEIO_HCI = 0
2626
CIRCUITPY_GIFIO = 0
2727
CIRCUITPY_ULAB = 0
2828
CIRCUITPY_STAGE = 1
29+
CIRCUITPY_ZLIB = 0
2930

3031
FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/meowbit

ports/stm/boards/thunderpack_v11/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ CIRCUITPY_BITMAPTOOLS = 0
2121
CIRCUITPY_BLEIO_HCI = 0
2222
CIRCUITPY_VECTORIO = 0
2323
CIRCUITPY_ULAB = 0
24+
CIRCUITPY_ZLIB = 0

ports/stm/boards/thunderpack_v12/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ EXTERNAL_FLASH_DEVICES = GD25Q16C
1414
CIRCUITPY_NVM = 1
1515
CIRCUITPY_BITMAPTOOLS = 0
1616
CIRCUITPY_BLEIO_HCI = 0
17+
CIRCUITPY_ZLIB = 0
1718

1819
MCU_SERIES = F4
1920
MCU_VARIANT = STM32F411xE

ports/unix/variants/coverage/mpconfigvariant.mk

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ SRC_BITMAP := \
3636
shared-bindings/rainbowio/__init__.c \
3737
shared-bindings/traceback/__init__.c \
3838
shared-bindings/util.c \
39+
shared-bindings/zlib/__init__.c \
3940
shared-module/aesio/aes.c \
4041
shared-module/aesio/__init__.c \
4142
shared-module/bitmaptools/__init__.c \
@@ -44,7 +45,8 @@ SRC_BITMAP := \
4445
shared-module/displayio/ColorConverter.c \
4546
shared-module/displayio/ColorConverter.c \
4647
shared-module/rainbowio/__init__.c \
47-
shared-module/traceback/__init__.c
48+
shared-module/traceback/__init__.c \
49+
shared-module/zlib/__init__.c \
4850

4951
$(info $(SRC_BITMAP))
5052
SRC_C += $(SRC_BITMAP)
@@ -55,7 +57,8 @@ CFLAGS += \
5557
-DCIRCUITPY_DISPLAYIO_UNIX=1 \
5658
-DCIRCUITPY_GIFIO=1 \
5759
-DCIRCUITPY_RAINBOWIO=1 \
58-
-DCIRCUITPY_TRACEBACK=1
60+
-DCIRCUITPY_TRACEBACK=1 \
61+
-DCIRCUITPY_ZLIB=1
5962

6063
SRC_C += coverage.c
6164
SRC_CXX += coveragecpp.cpp

py/circuitpy_defns.mk

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ endif
331331
ifeq ($(CIRCUITPY_USTACK),1)
332332
SRC_PATTERNS += ustack/%
333333
endif
334+
ifeq ($(CIRCUITPY_ZLIB),1)
335+
SRC_PATTERNS += zlib/%
336+
endif
334337
ifeq ($(CIRCUITPY_VIDEOCORE),1)
335338
SRC_PATTERNS += videocore/%
336339
endif
@@ -597,6 +600,7 @@ SRC_SHARED_MODULE_ALL = \
597600
usb/core/__init__.c \
598601
usb/core/Device.c \
599602
ustack/__init__.c \
603+
zlib/__init__.c \
600604
vectorio/Circle.c \
601605
vectorio/Polygon.c \
602606
vectorio/Rectangle.c \
@@ -653,6 +657,17 @@ SRC_MOD += $(addprefix lib/protomatter/src/, \
653657
$(BUILD)/lib/protomatter/src/core.o: CFLAGS += -include "shared-module/rgbmatrix/allocator.h" -DCIRCUITPY -Wno-missing-braces -Wno-missing-prototypes
654658
endif
655659

660+
ifeq ($(CIRCUITPY_ZLIB),1)
661+
SRC_MOD += $(addprefix lib/uzlib/, \
662+
tinflate.c \
663+
tinfzlib.c \
664+
tinfgzip.c \
665+
adler32.c \
666+
crc32.c \
667+
)
668+
$(BUILD)/lib/uzlib/tinflate.o: CFLAGS += -Wno-missing-braces -Wno-missing-prototypes
669+
endif
670+
656671
# All possible sources are listed here, and are filtered by SRC_PATTERNS.
657672
SRC_SHARED_MODULE_INTERNAL = \
658673
$(filter $(SRC_PATTERNS), \

py/circuitpy_mpconfig.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,10 @@ CFLAGS += -DUSB_NUM_ENDPOINT_PAIRS=$(USB_NUM_ENDPOINT_PAIRS)
450450
CIRCUITPY_USTACK ?= 0
451451
CFLAGS += -DCIRCUITPY_USTACK=$(CIRCUITPY_USTACK)
452452

453+
# for decompressing utlities
454+
CIRCUITPY_ZLIB ?= 1
455+
CFLAGS += -DCIRCUITPY_ZLIB=$(CIRCUITPY_ZLIB)
456+
453457
# ulab numerics library
454458
CIRCUITPY_ULAB ?= $(CIRCUITPY_FULL_BUILD)
455459
CFLAGS += -DCIRCUITPY_ULAB=$(CIRCUITPY_ULAB)

shared-bindings/zlib/__init__.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Mark Komus
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 <stdint.h>
28+
#include <assert.h>
29+
#include <string.h>
30+
31+
#include "py/obj.h"
32+
#include "py/runtime.h"
33+
#include "py/stream.h"
34+
#include "py/mperrno.h"
35+
#include "py/builtin.h"
36+
#include "py/objtuple.h"
37+
#include "py/binary.h"
38+
#include "py/parsenum.h"
39+
40+
#include "shared-bindings/zlib/__init__.h"
41+
42+
#include "supervisor/shared/translate.h"
43+
44+
//| """zlib decompression functionality
45+
//|
46+
//| The `zlib` module allows limited functionality similar to the CPython zlib library.
47+
//| This module allows to decompress binary data compressed with DEFLATE algorithm
48+
//| (commonly used in zlib library and gzip archiver). Compression is not yet implemented."""
49+
//|
50+
51+
//| def zlib_decompress(data: bytes, wbits: Optional[int] = 0, bufsize: Optional[int] = 0) -> bytes:
52+
//| """Return decompressed *data* as bytes. *wbits* is DEFLATE dictionary window
53+
//| size used during compression (8-15, the dictionary size is power of 2 of
54+
//| that value). Additionally, if value is positive, *data* is assumed to be
55+
//| zlib stream (with zlib header). Otherwise, if it's negative, it's assumed
56+
//| to be raw DEFLATE stream.
57+
//|
58+
//| The wbits parameter controls the size of the history buffer (or “window size”), and what header
59+
//| and trailer format is expected.
60+
//|
61+
//| Common wbits values:
62+
//|
63+
//| * To decompress deflate format, use wbits = -15
64+
//| * To decompress zlib format, use wbits = 15
65+
//| * To decompress gzip format, use wbits = 31
66+
//|
67+
//| :param bytes data: data to be decompressed
68+
//| :param int wbits: DEFLATE dictionary window size used during compression. See above.
69+
//| :param int bufsize: ignored for compatibility with CPython only
70+
//| """
71+
//| ...
72+
//|
73+
STATIC mp_obj_t zlib_decompress(size_t n_args, const mp_obj_t *args) {
74+
bool is_zlib = true;
75+
if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
76+
is_zlib = false;
77+
}
78+
79+
return common_hal_zlib_decompress(args[0], is_zlib);
80+
}
81+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(zlib_decompress_obj, 1, 3, zlib_decompress);
82+
83+
STATIC const mp_rom_map_elem_t zlib_globals_table[] = {
84+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zlib) },
85+
{ MP_ROM_QSTR(MP_QSTR_decompress), MP_ROM_PTR(&zlib_decompress_obj) },
86+
};
87+
88+
STATIC MP_DEFINE_CONST_DICT(zlib_globals, zlib_globals_table);
89+
90+
const mp_obj_module_t zlib_module = {
91+
.base = { &mp_type_module },
92+
.globals = (mp_obj_dict_t *)&zlib_globals,
93+
};
94+
95+
MP_REGISTER_MODULE(MP_QSTR_zlib, zlib_module, CIRCUITPY_ZLIB);

shared-bindings/zlib/__init__.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Mark Komus
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+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
29+
30+
mp_obj_t common_hal_zlib_decompress(mp_obj_t data, bool is_zlib);
31+
32+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H

0 commit comments

Comments
 (0)