1try:
2    import uzlib as zlib
3    import uio as io
4except ImportError:
5    print("SKIP")
6    raise SystemExit
7
8
9# gzip bitstream
10buf = io.BytesIO(
11    b"\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
12)
13inp = zlib.DecompIO(buf, 16 + 8)
14print(buf.seek(0, 1))
15print(inp.read(1))
16print(buf.seek(0, 1))
17print(inp.read(2))
18print(inp.read())
19print(buf.seek(0, 1))
20print(inp.read(1))
21print(inp.read())
22print(buf.seek(0, 1))
23
24# Check FHCRC field
25buf = io.BytesIO(
26    b"\x1f\x8b\x08\x02\x99\x0c\xe5W\x00\x03\x00\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
27)
28inp = zlib.DecompIO(buf, 16 + 8)
29print(inp.read())
30
31# Check FEXTRA field
32buf = io.BytesIO(
33    b"\x1f\x8b\x08\x04\x99\x0c\xe5W\x00\x03\x01\x00X\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
34)
35inp = zlib.DecompIO(buf, 16 + 8)
36print(inp.read())
37
38# broken header
39buf = io.BytesIO(
40    b"\x1f\x8c\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00"
41)
42try:
43    inp = zlib.DecompIO(buf, 16 + 8)
44except ValueError:
45    print("ValueError")
46
47# broken crc32
48buf = io.BytesIO(
49    b"\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa7\x106\x05\x00\x00\x00"
50)
51inp = zlib.DecompIO(buf, 16 + 8)
52try:
53    inp.read(6)
54except OSError as e:
55    print(repr(e))
56
57# broken uncompressed size - not checked so far
58# buf = io.BytesIO(b'\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x06\x00\x00\x00')
59# inp = zlib.DecompIO(buf, 16 + 8)
60# inp.read(6)
61