From fe85ab850bc0f5abe6bc788e167c75d6d09d981b Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Fri, 15 Dec 2023 13:00:45 +0000 Subject: [PATCH] Treat UTF-16 strings in binary VDF as little-endian Integers in binary VDF are already treated as little-endian (least significant byte first) regardless of CPU architecture, but the 16-bit units in UTF-16 didn't get the same treatment. This led to a test failure on big-endian machines. Resolves: https://github.com/ValvePython/vdf/issues/33 Signed-off-by: Simon McVittie --- vdf/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vdf/__init__.py b/vdf/__init__.py index 6b47213..8b6a5c0 100644 --- a/vdf/__init__.py +++ b/vdf/__init__.py @@ -361,7 +361,7 @@ def read_string(fp, wide=False): result = buf[:end] if wide: - result = result.decode('utf-16') + result = result.decode('utf-16le') elif bytes is not str: result = result.decode('utf-8', 'replace') else: @@ -469,7 +469,7 @@ def _binary_dump_gen(obj, level=0, alt_format=False): value = value.encode('utf-8') + BIN_NONE yield BIN_STRING except: - value = value.encode('utf-16') + BIN_NONE*2 + value = value.encode('utf-16le') + BIN_NONE*2 yield BIN_WIDESTRING yield key + BIN_NONE + value elif isinstance(value, float):