From cac83658f9cff2339661c6dae5440352397a86a9 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sat, 15 Jan 2022 12:48:30 +0100 Subject: [PATCH] tool/dump_pklg: use struct module --- tool/dump_pklg.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/tool/dump_pklg.py b/tool/dump_pklg.py index f7e5c211e..be2e54bdc 100755 --- a/tool/dump_pklg.py +++ b/tool/dump_pklg.py @@ -13,23 +13,16 @@ import sys import datetime +import struct packet_types = [ "CMD =>", "EVT <=", "ACL =>", "ACL <="] -def read_net_32(f): - a = f.read(1) - if a == '': - return -1 - b = f.read(1) - if b == '': - return -1 - c = f.read(1) - if c == '': - return -1 - d = f.read(1) - if d == '': - return -1 - return ord(a) << 24 | ord(b) << 16 | ord(c) << 8 | ord(d) +def read_header(f): + bytes_read = f.read(13) + if bytes_read: + return struct.unpack(">IIIB", bytes_read) + else: + return (-1, 0, 0, 0) def as_hex(data): str_list = [] @@ -50,12 +43,9 @@ with open (infile, 'rb') as fin: pos = 0 try: while True: - len = read_net_32(fin) + (len, ts_sec, ts_usec, type) = read_header(fin) if len < 0: break - ts_sec = read_net_32(fin) - ts_usec = read_net_32(fin) - type = ord(fin.read(1)) packet_len = len - 9; if (packet_len > 66000): print ("Error parsing pklg at offset %u (%x)." % (pos, pos))