Created
September 22, 2017 15:32
-
-
Save jsbain/1b5066eebac9467accabaec88e993f46 to your computer and use it in GitHub Desktop.
Untitled_155.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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