bitstream now uses struct instead of ctypes for type conversion.

This commit is contained in:
lcdr 2015-09-01 20:50:21 +02:00
parent 3a1c0a73e0
commit 9419e656b5
3 changed files with 11 additions and 26 deletions

View File

@ -5,12 +5,11 @@ import tkinter.filedialog as filedialog
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import zipfile import zipfile
from collections import OrderedDict 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 from tkinter import BooleanVar, END, Menu
import viewer import viewer
import structparser 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: with open("packetdefinitions/replica/creation_header.structs", encoding="utf-8") as file:
creation_header_parser = structparser.StructParser(file.read()) creation_header_parser = structparser.StructParser(file.read())
@ -422,7 +421,7 @@ class CaptureViewer(viewer.Viewer):
traceback.print_exc() 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()])) 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"] 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)) values = ("likely not "+msg_name, "Error while parsing, likely not this message!\n"+str(e)+"\nlen: "+str(len(packet)-10))
tags = ["error"] tags = ["error"]
else: else:

View File

@ -1,13 +1,12 @@
import configparser import configparser
import os.path import os.path
import sqlite3 import sqlite3
from ctypes import c_float, c_int64, c_ubyte, c_uint, c_uint64, c_ushort
import tkinter.filedialog as filedialog import tkinter.filedialog as filedialog
from tkinter import END, Menu from tkinter import END, Menu
import viewer 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): class LUZViewer(viewer.Viewer):
def __init__(self): def __init__(self):

View File

@ -2,16 +2,15 @@
Module for parsing binary data into structs. Module for parsing binary data into structs.
""" """
import argparse import argparse
import ctypes
import re import re
from collections import namedtuple 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\[\]]+" VAR_CHARS = r"[^ \t\[\]]+"
BIT = r"(BIT[0-7])?" BIT = r"(BIT[0-7])?"
TYPES = "bytes", "string", "wstring", "char", "wchar", "float", "double", "s8", "u8", "s16", "u16", "s32", "u32", "s64", "u64" 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(TYPES)+")" TYPES_RE = "("+"|".join(BITSTREAM_TYPES.keys())+")"
DEFINITION_SYNTAX = re.compile(r"""^ DEFINITION_SYNTAX = re.compile(r"""^
(?P<indent>\t*) # Indentation (?P<indent>\t*) # Indentation
@ -134,31 +133,19 @@ class StructParser:
else: else:
eval_ = None eval_ = None
if def_["type"] is not None: if def_["type"] is not None:
if def_["type"] == "bytes": type_ = BITSTREAM_TYPES[def_["type"]]
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)<bitlength>
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:]]
else: else:
# try to find a type based on the length # try to find a type based on the length
if length_bits == 1: if length_bits == 1:
type_ = c_bit type_ = c_bit
elif length_bits == 8: elif length_bits == 8:
type_ = ctypes.c_byte type_ = c_int8
elif length_bits == 16: elif length_bits == 16:
type_ = ctypes.c_short type_ = c_int16
elif length_bits == 32: elif length_bits == 32:
type_ = ctypes.c_int type_ = c_int32
elif length_bits == 64: elif length_bits == 64:
type_ = ctypes.c_int64 type_ = c_int64
else: else:
if length_bits % 8 == 0: if length_bits % 8 == 0:
type_ = bytes type_ = bytes