Drop bitarray requirement

This commit is contained in:
William Toohey 2018-05-25 23:59:55 +10:00
parent dccacdb6e6
commit 042323917c
2 changed files with 20 additions and 14 deletions

View File

@ -1,18 +1,20 @@
# python 3 style, ints instead of b''
from builtins import bytes
from bitarray import bitarray
charmap = '0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
bytemap = {charmap[i] : bytes(chr(i).encode()) for i in range(len(charmap))}
bytemap = {charmap[i] : i for i in range(len(charmap))}
def pack_sixbit(string, byteBuf):
chars = [bytemap[x] for x in string]
bits = bitarray(endian='big')
padding = 8 - (len(string)*6 % 8)
if padding == 8:
padding = 0
bits = 0
for c in chars:
bits.frombytes(c)
# leave only the 6 bits we care for
del bits[-8:-6]
data = bytes(bits.tobytes())
bits <<= 6
bits |= c
bits <<= padding
data = bytes(bits.to_bytes((len(string)*6 + padding) // 8, byteorder='big'))
byteBuf.append_bytes((len(string),))
byteBuf.append_bytes(data)
@ -20,8 +22,13 @@ def unpack_sixbit(byteBuf):
length = byteBuf.get_u8()
length_bits = length * 6
length_bytes = (length_bits + 7) // 8
bitBuf = bitarray(endian='big')
bitBuf.frombytes(bytes(byteBuf.get_bytes(length_bytes)))
result = [bytes(bitBuf[offset:offset+6].tobytes())[0] >> 2
for offset in range(0, length_bits, 6)]
return ''.join([charmap[x] for x in result])
padding = 8 - (length_bits % 8)
if padding == 8:
padding = 0
bits = int.from_bytes(bytes(byteBuf.get_bytes(length_bytes)), byteorder='big')
bits >>= padding
result = []
for _ in range(length):
result.append(bits & 0b111111)
bits >>= 6
return ''.join([charmap[x] for x in result[::-1]])

View File

@ -3,13 +3,12 @@ import sys
requires = [
'bitarray',
'lxml',
]
if sys.version_info < (3,0):
requires.append('future')
version = '1.4'
version = '1.5'
setup(
name='kbinxml',
description="Decoder/encoder for Konami's binary XML format",