Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
import operator
import functools
import itertools
import os
import json
import struct
#import math
import bitarray
def encode(minbytes):
mbinarr = bitarray.bitarray()
mbinarr.frombytes(minbytes)
orglength = len(mbinarr)
for i in itertools.count(0,8):
pos = orglength - 7*(i//8) - 7
if pos < 0: break
mbinarr.insert(pos,0)
padlen = (8 - (len(mbinarr) % 8)) % 8
#padlen2 = math.ceil(len(mbinarr) / 8) * 8 - len(mbinarr)
#if padlen != padlen2: raise Exception("uoverenstemmelse!" + str((len(mbinarr), padlen, padlen2, mbinarr)))
for i in range(padlen): mbinarr.insert(0,0)
mstring = mbinarr.tobytes().decode("utf-8")
return (orglength, mstring)
#tochars1 = lambda mbinarr: '' if len(mbinarr) == 0 else bin(int.from_bytes(mbinarr.tobytes(), 'big'))[2:].zfill(len(mbinarr))
#tochars2 = lambda mbinarr: ''.join(list(map(lambda x: '1' if x else '0', mbinarr.tolist()))) # .zfill(mbinarr.length())
#tochars3 = lambda mbinarr: mbinarr.to01()
def decode(pair):
(rawlen, mstring) = pair
mbinarr = bitarray.bitarray()
mbinarr.frombytes(mstring.encode())
orglength = len(mbinarr)
for i in itertools.count(0,8):
pos = orglength - i - 8
if pos < 0: break
mbinarr.pop(pos)
for i in range(len(mbinarr)-rawlen):
mbinarr.pop(0)
return mbinarr.tobytes()
#data = (0x12345678abcdef).to_bytes(7,'big')
#data = (0xffffffffffffff).to_bytes(7,'big')
#data = functools.reduce(operator.add, (map(lambda x: x.to_bytes(4,'big'), [0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd, 0xeeeeeeee, 0xffffffff])))
def test():
def iteration(data):
data2 = decode(encode(data))
if data2 != data:
raise Exception("error!\n{}\n{}".format(data, data2))
iteration(b"")
n = 100
for i in range(1,n+1):
l = int.from_bytes(os.urandom(2),'big') >> 7
data = os.urandom(l)
iteration(data)
if (i) % (n // 10) == 0: print(i // (n // 10) * 10 , '% complete')
if __name__ == "__main__": test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.