Skip to content

Commit 36e74c1

Browse files
jimmodpgeorge
authored andcommitted
zlib: Add zlib module.
This is a replacement for the `zlib` module that used to be built-in and has been replaced by the MicroPython-specific `deflate` module. Also updates the `gzip` module in a similar fashion and provide the `gzip.GzipFile` class and `gzip.open` function. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]>
1 parent 4da6e6f commit 36e74c1

File tree

4 files changed

+68
-26
lines changed

4 files changed

+68
-26
lines changed

python-stdlib/gzip/gzip.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
# import zlib
2-
import uzlib as zlib
1+
# MicroPython gzip module
2+
# MIT license; Copyright (c) 2023 Jim Mussared
33

4-
FTEXT = 1
5-
FHCRC = 2
6-
FEXTRA = 4
7-
FNAME = 8
8-
FCOMMENT = 16
4+
_WBITS = const(15)
5+
6+
import io, deflate
7+
8+
9+
def GzipFile(fileobj):
10+
return deflate.DeflateIO(fileobj, deflate.GZIP, _WBITS)
11+
12+
13+
def open(filename, mode):
14+
return deflate.DeflateIO(open(filename, mode), deflate.GZIP, _WBITS, True)
15+
16+
17+
if hasattr(deflate.DeflateIO, "write"):
18+
19+
def compress(data):
20+
f = io.BytesIO()
21+
with GzipFile(fileobj=f) as g:
22+
g.write(data)
23+
return f.getvalue()
924

1025

1126
def decompress(data):
12-
assert data[0] == 0x1F and data[1] == 0x8B
13-
assert data[2] == 8
14-
flg = data[3]
15-
assert flg & 0xE0 == 0
16-
i = 10
17-
if flg & FEXTRA:
18-
i += data[11] << 8 + data[10] + 2
19-
if flg & FNAME:
20-
while data[i]:
21-
i += 1
22-
i += 1
23-
if flg & FCOMMENT:
24-
while data[i]:
25-
i += 1
26-
i += 1
27-
if flg & FHCRC:
28-
i += 2
29-
return zlib.decompress(memoryview(data)[i:], -15)
27+
f = io.BytesIO(data)
28+
with GzipFile(fileobj=f) as g:
29+
return g.read()

python-stdlib/gzip/manifest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
metadata(version="0.1.1")
1+
metadata(version="1.0.0")
22

33
module("gzip.py")

python-stdlib/zlib/manifest.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
metadata(version="1.0.0", description="Compression and decompression using the deflate algorithm")
2+
3+
module("zlib.py")

python-stdlib/zlib/zlib.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# MicroPython zlib module
2+
# MIT license; Copyright (c) 2023 Jim Mussared
3+
4+
import io, deflate
5+
6+
_MAX_WBITS = const(15)
7+
8+
9+
def _decode_wbits(wbits, decompress):
10+
if -15 <= wbits <= -5:
11+
return (
12+
deflate.RAW,
13+
-wbits,
14+
)
15+
elif 5 <= wbits <= 15:
16+
return (deflate.ZLIB, wbits)
17+
elif decompress and wbits == 0:
18+
return (deflate.ZLIB,)
19+
elif 21 <= wbits <= 31:
20+
return (deflate.GZIP, wbits - 16)
21+
elif decompress and 35 <= wbits <= 47:
22+
return (deflate.AUTO, wbits - 32)
23+
else:
24+
raise ValueError("wbits")
25+
26+
27+
if hasattr(deflate.DeflateIO, "write"):
28+
29+
def compress(data, wbits=_MAX_WBITS):
30+
f = io.BytesIO()
31+
with deflate.DeflateIO(f, *_decode_wbits(wbits, False)) as g:
32+
g.write(data)
33+
return f.getvalue()
34+
35+
36+
def decompress(data, wbits=_MAX_WBITS):
37+
f = io.BytesIO(data)
38+
with deflate.DeflateIO(f, *_decode_wbits(wbits, True)) as g:
39+
return g.read()

0 commit comments

Comments
 (0)