uzlib_decompio_gz.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. try:
  2. import uzlib as zlib
  3. import uio as io
  4. except ImportError:
  5. print("SKIP")
  6. raise SystemExit
  7. # gzip bitstream
  8. buf = io.BytesIO(b'\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00')
  9. inp = zlib.DecompIO(buf, 16 + 8)
  10. print(buf.seek(0, 1))
  11. print(inp.read(1))
  12. print(buf.seek(0, 1))
  13. print(inp.read(2))
  14. print(inp.read())
  15. print(buf.seek(0, 1))
  16. print(inp.read(1))
  17. print(inp.read())
  18. print(buf.seek(0, 1))
  19. # Check FHCRC field
  20. buf = io.BytesIO(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')
  21. inp = zlib.DecompIO(buf, 16 + 8)
  22. print(inp.read())
  23. # Check FEXTRA field
  24. buf = io.BytesIO(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')
  25. inp = zlib.DecompIO(buf, 16 + 8)
  26. print(inp.read())
  27. # broken header
  28. buf = io.BytesIO(b'\x1f\x8c\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa6\x106\x05\x00\x00\x00')
  29. try:
  30. inp = zlib.DecompIO(buf, 16 + 8)
  31. except ValueError:
  32. print("ValueError")
  33. # broken crc32
  34. buf = io.BytesIO(b'\x1f\x8b\x08\x08\x99\x0c\xe5W\x00\x03hello\x00\xcbH\xcd\xc9\xc9\x07\x00\x86\xa7\x106\x05\x00\x00\x00')
  35. inp = zlib.DecompIO(buf, 16 + 8)
  36. try:
  37. inp.read(6)
  38. except OSError as e:
  39. print(repr(e))
  40. # broken uncompressed size - not checked so far
  41. #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')
  42. #inp = zlib.DecompIO(buf, 16 + 8)
  43. #inp.read(6)