Skip to content

Instantly share code, notes, and snippets.

@jsbain
Created September 22, 2017 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbain/1b5066eebac9467accabaec88e993f46 to your computer and use it in GitHub Desktop.
Save jsbain/1b5066eebac9467accabaec88e993f46 to your computer and use it in GitHub Desktop.
Untitled_155.py
from ctypes import *
COMPRESSION_LZMA = 0x306
c=CDLL(None)
def encode(instr):
outbuf = create_string_buffer(max(32767,len(instr)))
inbuf = create_string_buffer(len(instr))
insz=len(instr)
inbuf[:len(instr)]=instr
outsz=c.compression_encode_buffer(addressof(outbuf), sizeof(outbuf), addressof(inbuf),insz,None,COMPRESSION_LZMA)
return outbuf[:outsz]
def decode(instr,outsz):
#we have to guess outsz, and keep trying i guess
#better to use streaming methods
outbuf = create_string_buffer(outsz)
inbuf = create_string_buffer(len(instr))
insz=len(instr)
inbuf[:len(instr)]=instr
while True:
outwritten=c.compression_decode_buffer(addressof(outbuf), sizeof(outbuf), addressof(inbuf), insz, None, COMPRESSION_LZMA)
if outwritten==outsz:
outsz=4*outsz
outbuf = create_string_buffer(outsz)
else:
break
return outbuf[:outwritten]
encoded=encode(b'hello world')
decoded=decode(encoded,100)
print(decoded)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment