From 9419e656b577f0265509406aecb51f480786a069 Mon Sep 17 00:00:00 2001 From: lcdr Date: Tue, 1 Sep 2015 20:50:21 +0200 Subject: [PATCH] bitstream now uses struct instead of ctypes for type conversion. --- captureviewer.pyw | 5 ++--- luzviewer.pyw | 3 +-- structparser.py | 29 ++++++++--------------------- 3 files changed, 11 insertions(+), 26 deletions(-) diff --git a/captureviewer.pyw b/captureviewer.pyw index edd254e..9f033e1 100644 --- a/captureviewer.pyw +++ b/captureviewer.pyw @@ -5,12 +5,11 @@ import tkinter.filedialog as filedialog import xml.etree.ElementTree as ET import zipfile from collections import OrderedDict -from ctypes import c_float, c_int, c_int64, c_ubyte, c_uint, c_ushort from tkinter import BooleanVar, END, Menu import viewer import structparser -from pyraknet.bitstream import BitStream, c_bit +from pyraknet.bitstream import BitStream, c_bit, c_float, c_int, c_int64, c_ubyte, c_uint, c_ushort with open("packetdefinitions/replica/creation_header.structs", encoding="utf-8") as file: creation_header_parser = structparser.StructParser(file.read()) @@ -422,7 +421,7 @@ class CaptureViewer(viewer.Viewer): traceback.print_exc() values = ("likely not "+msg_name, "Error while parsing, likely not this message!\n"+str(e)+"\nlen: "+str(len(packet)-10)+"\n"+"\n".join(["%s = %s" % (a, b) for a, b in attr_values.items()])) tags = ["error"] - except ValueError as e: + except Exception as e: values = ("likely not "+msg_name, "Error while parsing, likely not this message!\n"+str(e)+"\nlen: "+str(len(packet)-10)) tags = ["error"] else: diff --git a/luzviewer.pyw b/luzviewer.pyw index 454250d..181a46f 100644 --- a/luzviewer.pyw +++ b/luzviewer.pyw @@ -1,13 +1,12 @@ import configparser import os.path import sqlite3 -from ctypes import c_float, c_int64, c_ubyte, c_uint, c_uint64, c_ushort import tkinter.filedialog as filedialog from tkinter import END, Menu import viewer -from pyraknet.bitstream import BitStream +from pyraknet.bitstream import BitStream, c_float, c_int64, c_ubyte, c_uint, c_uint64, c_ushort class LUZViewer(viewer.Viewer): def __init__(self): diff --git a/structparser.py b/structparser.py index ab52a1f..bdc48e0 100644 --- a/structparser.py +++ b/structparser.py @@ -2,16 +2,15 @@ Module for parsing binary data into structs. """ import argparse -import ctypes import re from collections import namedtuple -from pyraknet.bitstream import BitStream, c_bit +from pyraknet.bitstream import BitStream, c_bit, c_float, c_double, c_int8, c_uint8, c_int16, c_uint16, c_int32, c_uint32, c_int64, c_uint64 VAR_CHARS = r"[^ \t\[\]]+" BIT = r"(BIT[0-7])?" -TYPES = "bytes", "string", "wstring", "char", "wchar", "float", "double", "s8", "u8", "s16", "u16", "s32", "u32", "s64", "u64" -TYPES_RE = "("+"|".join(TYPES)+")" +BITSTREAM_TYPES = {"bytes": bytes, "string": (str, 1), "wstring": (str, 2), "float": c_float, "double": c_double, "s8": c_int8, "u8": c_uint8, "s16": c_int16, "u16": c_uint16, "s32": c_int32, "u32": c_uint32, "s64": c_int64, "u64": c_uint64} +TYPES_RE = "("+"|".join(BITSTREAM_TYPES.keys())+")" DEFINITION_SYNTAX = re.compile(r"""^ (?P\t*) # Indentation @@ -134,31 +133,19 @@ class StructParser: else: eval_ = None if def_["type"] is not None: - if def_["type"] == "bytes": - type_ = bytes - elif def_["type"] == "string": - type_ = str, 1 - elif def_["type"] == "wstring": - type_ = str, 2 - elif def_["type"] in ("char", "wchar", "float", "double"): - type_ = vars(ctypes)["c_"+def_["type"]] - # the rest of types are in the format (s|u) - elif def_["type"].startswith("s"): - type_ = vars(ctypes)["c_int"+def_["type"][1:]] - elif def_["type"].startswith("u"): - type_ = vars(ctypes)["c_uint"+def_["type"][1:]] + type_ = BITSTREAM_TYPES[def_["type"]] else: # try to find a type based on the length if length_bits == 1: type_ = c_bit elif length_bits == 8: - type_ = ctypes.c_byte + type_ = c_int8 elif length_bits == 16: - type_ = ctypes.c_short + type_ = c_int16 elif length_bits == 32: - type_ = ctypes.c_int + type_ = c_int32 elif length_bits == 64: - type_ = ctypes.c_int64 + type_ = c_int64 else: if length_bits % 8 == 0: type_ = bytes