Last active
October 2, 2015 20:47
-
-
Save qi7chen/2317610 to your computer and use it in GitHub Desktop.
run length compress variant
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
#! /usr/bin/env python | |
from itertools import groupby | |
MAX_LEN = 8 | |
OFFSET = 0xFF - MAX_LEN # 0xF7 | |
XORCODE = 0x8f | |
def encode_char(count, ch): | |
assert ch and count > 0 | |
if count == 1: | |
if ord(ch) > OFFSET: | |
return chr(OFFSET+1) + ch | |
else: | |
return ch | |
else: | |
data = (chr(0xFF) + ch) * (count // MAX_LEN) | |
return data + chr(OFFSET + count % MAX_LEN) + ch | |
def encode(input_string): | |
seq = [(len(list(itr)), ch) for ch, itr in groupby(input_string)] | |
return ''.join([encode_char(*x) for x in seq]) | |
def decode_generator(input_string): | |
escape = False | |
for i, v in enumerate(input_string): | |
if escape: | |
escape = False; | |
continue; | |
if ord(v) <= OFFSET: | |
yield v; | |
else: | |
escape = True | |
assert(i+1 < len(input_string)) | |
yield input_string[i+1] * (ord(v) - OFFSET) | |
def decode(input_string): | |
return ''.join([x for x in decode_generator(input_string)]) | |
def test(): | |
msg = 'aaaaab111111111shitkkkmmm' | |
print('message', msg) | |
buf = encode(msg) | |
print('encoded:', repr(buf)) | |
r1 = decode(buf) | |
print('decoded:', len(r1), r1) | |
assert r1 == msg | |
if __name__ == '__main__': | |
test() | |
xorcode = 0x329fa929 | |
i = 0 | |
for (; i < j-4; i+=4) | |
(unsigned*)&output[i] ^= xorcode | |
for (; i < j; ++i) | |
output[i] ^= XORCODE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment