X-Git-Url: http://git.ithinksw.org/~jspiros/python-ebml.git/blobdiff_plain/8915a957c75a1f8656e3373474a693ccb307131d..495182be96c0580426942c5921be0d709e32b443:/ebml/core.py?ds=inline diff --git a/ebml/core.py b/ebml/core.py index 24d849f..8c01068 100644 --- a/ebml/core.py +++ b/ebml/core.py @@ -138,13 +138,13 @@ def read_signed_integer(stream, size): value = 0 if size > 0: - byte = ord(stream.read(1)) - if (byte & 0b10000000) == 0b10000000: - value = -1 << 8 - value |= byte + first_byte = ord(stream.read(1)) + value = first_byte for i in xrange(1, size): byte = ord(stream.read(1)) - value = (value << 1) | byte + value = (value << 8) | byte + if (first_byte & 0b10000000) == 0b10000000: + value = -(2**(size*8) - value) return value @@ -189,6 +189,7 @@ def read_string(stream, size): value = '' if size > 0: value = stream.read(size) + value = value.partition(chr(0))[0] return value @@ -208,6 +209,7 @@ def read_unicode_string(stream, size): value = u'' if size > 0: data = stream.read(size) + data = data.partition(chr(0))[0] value = unicode(data, 'utf_8') return value @@ -391,9 +393,7 @@ def encode_signed_integer(sint, length=None): if sint >= 0: uint = sint else: - uint = 0b10000000 << (length - 1) - uint += sint - uint |= 0b10000000 << (length - 1) + uint = 2**(length*8) - abs(sint) data = bytearray(length) for index in reversed(xrange(length)):