Skip to content

zlib Module #6069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/library/zlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

.. module:: zlib
:synopsis: zlib decompression
:noindex:

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

Expand All @@ -26,6 +27,7 @@ Functions
CPython and is ignored.

.. class:: DecompIO(stream, wbits=0, /)
:noindex:

Create a ``stream`` wrapper which allows transparent decompression of
compressed data in another *stream*. This allows to process compressed
Expand Down
2 changes: 2 additions & 0 deletions lib/uzlib/uzlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ typedef struct {
} TINF_TREE;

struct uzlib_uncomp {
/* Point to the CircuitPython object that owns this decompression stream */
void *self;
/* Pointer to the next byte in the input buffer */
const unsigned char *source;
/* Pointer to the next byte past the input buffer (source_limit = source + len) */
Expand Down
2 changes: 1 addition & 1 deletion locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ msgstr ""
msgid "complex values not supported"
msgstr ""

#: extmod/moduzlib.c
#: extmod/moduzlib.c shared-module/zlib/DecompIO.c
msgid "compression header"
msgstr ""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ EXTERNAL_FLASH_DEVICES = AT25DF081A

CIRCUITPY_AUDIOIO = 0
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_ZLIB = 0
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/matrixportal_m4/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_PARALLELDISPLAY = 0
CIRCUITPY_SDCARDIO = 0
CIRCUITPY_SHARPDISPLAY = 0
CIRCUITPY_TRACEBACK = 0
CIRCUITPY_ZLIB=0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_Requests
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ CIRCUITPY_SYNTHIO ?= 0
CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1
CIRCUITPY_ULAB = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_ZLIB = 0

# TODO: In CircuitPython 8.0, turn this back on, after `busio.OneWire` is removed.
# We'd like a smoother transition, but we can't afford the space to have both
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/bluemicro833/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ CIRCUITPY_TRACEBACK = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_ZLIB = 0

MICROPY_PY_ASYNC_AWAIT = 0

Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/pca10100/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ CIRCUITPY_SYNTHIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_ZLIB = 0

MICROPY_PY_ASYNC_AWAIT = 0

Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/simmel/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ CIRCUITPY_ULAB = 0
CIRCUITPY_USB_CDC = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_WATCHDOG = 1
CIRCUITPY_ZLIB = 0

# Enable micropython.native
#CIRCUITPY_ENABLE_MPY_NATIVE = 1
Expand Down
1 change: 1 addition & 0 deletions ports/stm/boards/meowbit_v121/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_GIFIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_STAGE = 1
CIRCUITPY_ZLIB = 0

FROZEN_MPY_DIRS += $(TOP)/frozen/circuitpython-stage/meowbit
1 change: 1 addition & 0 deletions ports/stm/boards/thunderpack_v11/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_VECTORIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_ZLIB = 0
1 change: 1 addition & 0 deletions ports/stm/boards/thunderpack_v12/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ EXTERNAL_FLASH_DEVICES = GD25Q16C
CIRCUITPY_NVM = 1
CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_ZLIB = 0

MCU_SERIES = F4
MCU_VARIANT = STM32F411xE
Expand Down
7 changes: 5 additions & 2 deletions ports/unix/variants/coverage/mpconfigvariant.mk
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ SRC_BITMAP := \
shared-bindings/rainbowio/__init__.c \
shared-bindings/traceback/__init__.c \
shared-bindings/util.c \
shared-bindings/zlib/__init__.c \
shared-module/aesio/aes.c \
shared-module/aesio/__init__.c \
shared-module/bitmaptools/__init__.c \
Expand All @@ -44,7 +45,8 @@ SRC_BITMAP := \
shared-module/displayio/ColorConverter.c \
shared-module/displayio/ColorConverter.c \
shared-module/rainbowio/__init__.c \
shared-module/traceback/__init__.c
shared-module/traceback/__init__.c \
shared-module/zlib/__init__.c \

$(info $(SRC_BITMAP))
SRC_C += $(SRC_BITMAP)
Expand All @@ -55,7 +57,8 @@ CFLAGS += \
-DCIRCUITPY_DISPLAYIO_UNIX=1 \
-DCIRCUITPY_GIFIO=1 \
-DCIRCUITPY_RAINBOWIO=1 \
-DCIRCUITPY_TRACEBACK=1
-DCIRCUITPY_TRACEBACK=1 \
-DCIRCUITPY_ZLIB=1

SRC_C += coverage.c
SRC_CXX += coveragecpp.cpp
15 changes: 15 additions & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ endif
ifeq ($(CIRCUITPY_USTACK),1)
SRC_PATTERNS += ustack/%
endif
ifeq ($(CIRCUITPY_ZLIB),1)
SRC_PATTERNS += zlib/%
endif
ifeq ($(CIRCUITPY_VIDEOCORE),1)
SRC_PATTERNS += videocore/%
endif
Expand Down Expand Up @@ -597,6 +600,7 @@ SRC_SHARED_MODULE_ALL = \
usb/core/__init__.c \
usb/core/Device.c \
ustack/__init__.c \
zlib/__init__.c \
vectorio/Circle.c \
vectorio/Polygon.c \
vectorio/Rectangle.c \
Expand Down Expand Up @@ -653,6 +657,17 @@ SRC_MOD += $(addprefix lib/protomatter/src/, \
$(BUILD)/lib/protomatter/src/core.o: CFLAGS += -include "shared-module/rgbmatrix/allocator.h" -DCIRCUITPY -Wno-missing-braces -Wno-missing-prototypes
endif

ifeq ($(CIRCUITPY_ZLIB),1)
SRC_MOD += $(addprefix lib/uzlib/, \
tinflate.c \
tinfzlib.c \
tinfgzip.c \
adler32.c \
crc32.c \
)
$(BUILD)/lib/uzlib/tinflate.o: CFLAGS += -Wno-missing-braces -Wno-missing-prototypes
endif

# All possible sources are listed here, and are filtered by SRC_PATTERNS.
SRC_SHARED_MODULE_INTERNAL = \
$(filter $(SRC_PATTERNS), \
Expand Down
4 changes: 4 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@ CFLAGS += -DUSB_NUM_ENDPOINT_PAIRS=$(USB_NUM_ENDPOINT_PAIRS)
CIRCUITPY_USTACK ?= 0
CFLAGS += -DCIRCUITPY_USTACK=$(CIRCUITPY_USTACK)

# for decompressing utlities
CIRCUITPY_ZLIB ?= 1
CFLAGS += -DCIRCUITPY_ZLIB=$(CIRCUITPY_ZLIB)

# ulab numerics library
CIRCUITPY_ULAB ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_ULAB=$(CIRCUITPY_ULAB)
Expand Down
95 changes: 95 additions & 0 deletions shared-bindings/zlib/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Mark Komus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <stdint.h>
#include <assert.h>
#include <string.h>

#include "py/obj.h"
#include "py/runtime.h"
#include "py/stream.h"
#include "py/mperrno.h"
#include "py/builtin.h"
#include "py/objtuple.h"
#include "py/binary.h"
#include "py/parsenum.h"

#include "shared-bindings/zlib/__init__.h"

#include "supervisor/shared/translate.h"

//| """zlib decompression functionality
//|
//| The `zlib` module allows limited functionality similar to the CPython zlib library.
//| This module allows to decompress binary data compressed with DEFLATE algorithm
//| (commonly used in zlib library and gzip archiver). Compression is not yet implemented."""
//|

//| def zlib_decompress(data: bytes, wbits: Optional[int] = 0, bufsize: Optional[int] = 0) -> bytes:
//| """Return decompressed *data* as bytes. *wbits* is DEFLATE dictionary window
//| size used during compression (8-15, the dictionary size is power of 2 of
//| that value). Additionally, if value is positive, *data* is assumed to be
//| zlib stream (with zlib header). Otherwise, if it's negative, it's assumed
//| to be raw DEFLATE stream.
//|
//| The wbits parameter controls the size of the history buffer (or “window size”), and what header
//| and trailer format is expected.
//|
//| Common wbits values:
//|
//| * To decompress deflate format, use wbits = -15
//| * To decompress zlib format, use wbits = 15
//| * To decompress gzip format, use wbits = 31
//|
//| :param bytes data: data to be decompressed
//| :param int wbits: DEFLATE dictionary window size used during compression. See above.
//| :param int bufsize: ignored for compatibility with CPython only
//| """
//| ...
//|
STATIC mp_obj_t zlib_decompress(size_t n_args, const mp_obj_t *args) {
bool is_zlib = true;
if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
is_zlib = false;
}

return common_hal_zlib_decompress(args[0], is_zlib);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(zlib_decompress_obj, 1, 3, zlib_decompress);

STATIC const mp_rom_map_elem_t zlib_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zlib) },
{ MP_ROM_QSTR(MP_QSTR_decompress), MP_ROM_PTR(&zlib_decompress_obj) },
};

STATIC MP_DEFINE_CONST_DICT(zlib_globals, zlib_globals_table);

const mp_obj_module_t zlib_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&zlib_globals,
};

MP_REGISTER_MODULE(MP_QSTR_zlib, zlib_module, CIRCUITPY_ZLIB);
32 changes: 32 additions & 0 deletions shared-bindings/zlib/__init__.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Mark Komus
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H

mp_obj_t common_hal_zlib_decompress(mp_obj_t data, bool is_zlib);

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
Loading