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'' # python 3 style, ints instead of b''
from builtins import bytes from builtins import bytes
from bitarray import bitarray
charmap = '0123456789:ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz' 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): def pack_sixbit(string, byteBuf):
chars = [bytemap[x] for x in string] 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: for c in chars:
bits.frombytes(c) bits <<= 6
# leave only the 6 bits we care for bits |= c
del bits[-8:-6] bits <<= padding
data = bytes(bits.tobytes()) data = bytes(bits.to_bytes((len(string)*6 + padding) // 8, byteorder='big'))
byteBuf.append_bytes((len(string),)) byteBuf.append_bytes((len(string),))
byteBuf.append_bytes(data) byteBuf.append_bytes(data)
@ -20,8 +22,13 @@ def unpack_sixbit(byteBuf):
length = byteBuf.get_u8() length = byteBuf.get_u8()
length_bits = length * 6 length_bits = length * 6
length_bytes = (length_bits + 7) // 8 length_bytes = (length_bits + 7) // 8
bitBuf = bitarray(endian='big') padding = 8 - (length_bits % 8)
bitBuf.frombytes(bytes(byteBuf.get_bytes(length_bytes))) if padding == 8:
result = [bytes(bitBuf[offset:offset+6].tobytes())[0] >> 2 padding = 0
for offset in range(0, length_bits, 6)] bits = int.from_bytes(bytes(byteBuf.get_bytes(length_bytes)), byteorder='big')
return ''.join([charmap[x] for x in result]) 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 = [ requires = [
'bitarray',
'lxml', 'lxml',
] ]
if sys.version_info < (3,0): if sys.version_info < (3,0):
requires.append('future') requires.append('future')
version = '1.4' version = '1.5'
setup( setup(
name='kbinxml', name='kbinxml',
description="Decoder/encoder for Konami's binary XML format", description="Decoder/encoder for Konami's binary XML format",