Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deserialization support for new binary VDF w/ key table #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions tests/test_binary_vdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ def test_raise_on_remaining_with_file(self):
vdf.binary_load(buf, raise_on_remaining=True)
self.assertEqual(buf.read(), b'aaaa')

def test_key_table(self):
test = b'\x01\x00\x00\x00\x00value\x00\x01\x02\x00\x00\x00value3\x00\x08'
key_table = ['key', 'key2', 'key3', 'key4']

self.assertEqual({'key': 'value', 'key3': 'value3'}, vdf.binary_loads(test, key_table=key_table))

def test_vbkv_loads_empty(self):
with self.assertRaises(ValueError):
vdf.vbkv_loads(b'')
Expand Down
26 changes: 22 additions & 4 deletions vdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class COLOR(BASE_INT):
BIN_INT64 = b'\x0A'
BIN_END_ALT = b'\x0B'

def binary_loads(b, mapper=dict, merge_duplicate_keys=True, alt_format=False, raise_on_remaining=True):
def binary_loads(b, mapper=dict, merge_duplicate_keys=True, alt_format=False, key_table=None, raise_on_remaining=True):
"""
Deserialize ``b`` (``bytes`` containing a VDF in "binary form")
to a Python object.
Expand All @@ -307,13 +307,18 @@ def binary_loads(b, mapper=dict, merge_duplicate_keys=True, alt_format=False, ra
``merge_duplicate_keys`` when ``True`` will merge multiple KeyValue lists with the
same key into one instead of overwriting. You can se this to ``False`` if you are
using ``VDFDict`` and need to preserve the duplicates.

``key_table`` will be used to translate keys in binary VDF objects
which do not encode strings directly but instead store them in an out-of-band
table. Newer `appinfo.vdf` format stores this table the end of the file,
and it is needed to deserialize the binary VDF objects in that file.
"""
if not isinstance(b, bytes):
raise TypeError("Expected s to be bytes, got %s" % type(b))

return binary_load(BytesIO(b), mapper, merge_duplicate_keys, alt_format, raise_on_remaining)
return binary_load(BytesIO(b), mapper, merge_duplicate_keys, alt_format, key_table, raise_on_remaining)

def binary_load(fp, mapper=dict, merge_duplicate_keys=True, alt_format=False, raise_on_remaining=False):
def binary_load(fp, mapper=dict, merge_duplicate_keys=True, alt_format=False, key_table=None, raise_on_remaining=False):
"""
Deserialize ``fp`` (a ``.read()``-supporting file-like object containing
binary VDF) to a Python object.
Expand All @@ -325,6 +330,11 @@ def binary_load(fp, mapper=dict, merge_duplicate_keys=True, alt_format=False, ra
``merge_duplicate_keys`` when ``True`` will merge multiple KeyValue lists with the
same key into one instead of overwriting. You can se this to ``False`` if you are
using ``VDFDict`` and need to preserve the duplicates.

``key_table`` will be used to translate keys in binary VDF objects
which do not encode strings directly but instead store them in an out-of-band
table. Newer `appinfo.vdf` format stores this table the end of the file,
and it is needed to deserialize the binary VDF objects in that file.
"""
if not hasattr(fp, 'read') or not hasattr(fp, 'tell') or not hasattr(fp, 'seek'):
raise TypeError("Expected fp to be a file-like object with tell()/seek() and read() returning bytes")
Expand Down Expand Up @@ -382,7 +392,15 @@ def read_string(fp, wide=False):
continue
break

key = read_string(fp)
if key_table:
# If 'key_table' was provided, each key is an int32 value that
# needs to be mapped to an actual field name using a key table.
# Newer appinfo.vdf (V29+) stores this table at the end of the file.
index = int32.unpack(fp.read(int32.size))[0]

key = key_table[index]
else:
key = read_string(fp)

if t == BIN_NONE:
if merge_duplicate_keys and key in stack[-1]:
Expand Down