Skip to content

Commit

Permalink
binary: utf16 decode + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rossengeorgiev committed Jul 26, 2019
1 parent b287bd0 commit 01d63dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions tests/test_binary_vdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,8 @@ def test_vbkv_loads_invalid_checksum(self):
with self.assertRaises(ValueError):
vdf.vbkv_loads(b'VBKV\x01\x02\x03\x04\x00a\x00\x0b\x0b')

def test_loads_utf8_invalmid(self):
self.assertEqual({'aaa': b'bb\xef\xbf\xbdbb'.decode('utf-8')}, vdf.binary_loads(b'\x01aaa\x00bb\xffbb\x00\x08'))

def test_loads_utf16(self):
self.assertEqual({'aaa': b'b\x00b\x00\xff\xffb\x00b\x00'.decode('utf-16le')}, vdf.binary_loads(b'\x05aaa\x00b\x00b\x00\xff\xffb\x00b\x00\x00\x00\x08'))
10 changes: 8 additions & 2 deletions vdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,18 @@ def binary_loads(s, mapper=dict, merge_duplicate_keys=True, alt_format=False):
float32 = struct.Struct('<f')

def read_string(s, idx, wide=False):
end = s.find(b'\x00\x00' if wide else b'\x00', idx)
if wide:
end = s.find(b'\x00\x00', idx)
if (end - idx) % 2 != 0:
end += 1
else:
end = s.find(b'\x00', idx)

if end == -1:
raise SyntaxError("Unterminated cstring, index: %d" % idx)
result = s[idx:end]
if wide:
result = result.decode('utf-16', 'replace')
result = result.decode('utf-16')
elif bytes is not str:
result = result.decode('utf-8', 'replace')
else:
Expand Down

0 comments on commit 01d63dd

Please sign in to comment.