Skip to content

Commit

Permalink
attempt to decompress
Browse files Browse the repository at this point in the history
  • Loading branch information
danielrh committed Nov 19, 2018
1 parent 7abee8f commit 97889d0
Showing 1 changed file with 61 additions and 7 deletions.
68 changes: 61 additions & 7 deletions c/py/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ctypes
from ctypes import c_uint, pointer, POINTER, c_size_t, c_void_p, c_uint32, c_ubyte
from ctypes import c_uint, pointer, POINTER, c_size_t, c_void_p, c_uint32, c_ubyte, c_char_p
class BrotliEncoderWorkPool(ctypes.Structure):
pass
BrotliEncoderWorkPool= ctypes.POINTER(BrotliEncoderWorkPool)
Expand All @@ -11,8 +11,27 @@ class BrotliEncoderWorkPool(ctypes.Structure):
_BrotliEncoderCreateWorkPool.restype = POINTER(BrotliEncoderWorkPool)
_BrotliEncoderCompressWorkPool = brotli_library.BrotliEncoderCompressWorkPool
_BrotliEncoderCompressWorkPool.restype = c_uint32
class BrotliDecoderState(ctypes.Structure):
pass
class BrotliDecompressorException(Exception):
pass
_BrotliDecoderCreateInstance = brotli_library.BrotliDecoderCreateInstance
_BrotliDecoderCreateInstance.restype = POINTER(BrotliDecoderState)
_BrotliDecoderDestroyInstance = brotli_library.BrotliDecoderDestroyInstance
_BrotliDecoderDestroyInstance.restype = None
BrotliEncoderMaxCompressedSizeMulti = brotli_library.BrotliEncoderMaxCompressedSizeMulti
BrotliEncoderMaxCompressedSizeMulti.restype = c_size_t
_BrotliDecoderDecompressStream = brotli_library.BrotliDecoderDecompressStream
_BrotliDecoderDecompressStream.restype = None

_BrotliDecoderGetErrorString = brotli_library.BrotliDecoderGetErrorString
_BrotliDecoderGetErrorString.restype = c_char_p

BROTLI_DECODER_RESULT_ERROR = 0
BROTLI_DECODER_RESULT_SUCCESS = 1
BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT = 2
BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT = 3

def BrotliEncoderCreateWorkPool(num_workers):
return _BrotliEncoderCreateWorkPool(c_size_t(num_workers),
c_void_p(),
Expand Down Expand Up @@ -68,6 +87,40 @@ def BrotliEncoderCompressWorkPool(
+ " threads")
return bytearray(encoded[:encoded_size.value])

def BrotliDecode(const_input):
state = _BrotliDecoderCreateInstance(c_void_p(),
c_void_p(),
c_void_p())
input = memoryview(const_input)
output = []
try:
while True:
next_in = (c_ubyte * len(input))(len(input))
available_in = c_size_t(len(const_input))

orig_out = 65536
available_out = c_size_t(orig_out)
out_buf = (c_ubyte * orig_out)()
next_out = out_buf

res = _BrotliDecoderDecompressStream(state,
pointer(available_in),
ctypes.byref(next_in),
pointer(available_out),
pointer(next_out),
c_void_p())
if res == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
raise BrotliDecompressorException("EarlyEOF")
elif res == BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
output.append(out_buf[:orig_out - available_out])
elif res == BROTLI_DECODER_RESULT_SUCCESS:
break
else:
raise BrotliDecompressorException(_BrotliDecoderGetErrorString(state))
finally:
_BrotliDecoderDestroyInstance(state)
return ''.join(output)

def BrotliEncoderCompress(
input,
compression_options_map={},
Expand Down Expand Up @@ -98,8 +151,7 @@ def BrotliEncoderCompress(
c_size_t(num_threads),
c_void_p(),
c_void_p(),
c_void_p(),
)
c_void_p())
if ret_code == 0:
raise BrotliCompressorException("Insufficient space "
+ str(max_size)
Expand Down Expand Up @@ -204,9 +256,11 @@ def main():
data = f.read()
else:
data = sys.stdin.read()
if work_pool:
if decompress:
processed = BrotliDecode(data)
elif work_pool:
work_pool = BrotliEncoderCreateWorkPool(4)
encoded = BrotliEncoderCompressWorkPool(work_pool, data, {
processed = BrotliEncoderCompressWorkPool(work_pool, data, {
BROTLI_PARAM_QUALITY:11,
BROTLI_PARAM_Q9_5:0,
BROTLI_PARAM_LGWIN: 16,
Expand All @@ -216,15 +270,15 @@ def main():
},4 )
BrotliEncoderDestroyWorkPool(work_pool)
else:
encoded = BrotliEncoderCompress(data, {
processed = BrotliEncoderCompress(data, {
BROTLI_PARAM_QUALITY:11,
BROTLI_PARAM_Q9_5:0,
BROTLI_PARAM_LGWIN: 16,
BROTLI_PARAM_MAGIC_NUMBER: 0,
BROTLI_PARAM_CATABLE: 0,
BROTLI_PARAM_SIZE_HINT: len(data),
},4 )
sys.stdout.write(encoded)
sys.stdout.write(processed)

if __name__ == "__main__":
main()
Expand Down

0 comments on commit 97889d0

Please sign in to comment.