Skip to content

Commit 1ed4508

Browse files
ES-Alexanderpatrickelectric
authored andcommitted
examples: decode_sensor_binary_log: handle windows timestamp encoding
1 parent 261525d commit 1ed4508

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

examples/decode_sensor_binary_log.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class PingViewerLogReader:
8282
TIMESTAMP_FORMAT = re.compile(
8383
b'(\x00?\\d){2}(\x00?:\x00?[0-5]\x00?\\d){2}\x00?\\.(\x00?\\d){3}')
8484
MAX_TIMESTAMP_LENGTH = 12 * 2
85+
# byte encoding assumes posix until proven otherwise
86+
ENCODING = 'UTF-8'
8587

8688
def __init__(self, filename: str):
8789
self.filename = filename
@@ -107,9 +109,8 @@ def unpack_array(cls, file: IO[Any]):
107109
if array_size <= cls.MAX_ARRAY_LENGTH:
108110
return file.read(array_size)
109111

110-
@classmethod
111-
def unpack_string(cls, file: IO[Any]):
112-
return cls.unpack_array(file).decode('UTF-8')
112+
def unpack_string(self, file: IO[Any]):
113+
return self.unpack_array(file).decode(self.ENCODING)
113114

114115
def unpack_message(self, file: IO[Any]):
115116
timestamp = self.unpack_string(file)
@@ -145,7 +146,7 @@ def recover(self, file: IO[Any]):
145146
else:
146147
# match was found
147148
end = match.end()
148-
timestamp = roi[match.start():end].decode('UTF-8')
149+
timestamp = roi[match.start():end].decode(self.ENCODING)
149150
amount_read -= (len(roi) - end)
150151
self.failed_bytes += amount_read
151152
# return the file pointer to the end of this timestamp
@@ -163,6 +164,10 @@ def recover(self, file: IO[Any]):
163164

164165
def unpack_header(self, file: IO[Any]):
165166
self.header.string = self.unpack_string(file)
167+
if '\x00' in self.header.string:
168+
# Windows uses big-endian wide characters
169+
self.ENCODING = 'UTF-16-be'
170+
self.header.string = self.header.string.encode('UTF-8').decode(self.ENCODING)
166171
self.header.version = self.unpack_int(file)
167172

168173
for info in ('hash_commit', 'date', 'tag', 'os_name', 'os_version'):

0 commit comments

Comments
 (0)