2019-12-08 22:43:49 +01:00
|
|
|
import copy
|
|
|
|
import struct
|
2021-06-07 07:46:39 +02:00
|
|
|
from typing import Any, Dict, Iterator, List, Optional, Tuple
|
|
|
|
from typing_extensions import Final
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
from bemani.protocol.stream import InputStream
|
|
|
|
from bemani.protocol.node import Node
|
|
|
|
|
|
|
|
|
|
|
|
class XmlEncodingException(Exception):
|
|
|
|
"""
|
|
|
|
An exception that is thrown when we encounter an error encoding to or decoding from XML.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class XmlDecoder:
|
|
|
|
"""
|
|
|
|
A hand-rolled XML parser, suitable for parsing old-style XML documents in
|
|
|
|
game data or from legacy game traffic. I did consider using lxml and other
|
|
|
|
data stores, but they insist on mangling data inside binary/string blobs
|
|
|
|
making them unsuitable for a protocol with exact specifications.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, data: bytes, encoding: str) -> None:
|
|
|
|
"""
|
|
|
|
Initialize the XML decoder.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
data - String XML data which should be decoded into Nodes.
|
|
|
|
encoding - The expected encoding of the XML.
|
|
|
|
"""
|
|
|
|
self.stream = InputStream(data)
|
|
|
|
self.root: Optional[Node] = None
|
|
|
|
self.current: List[Node] = []
|
|
|
|
self.encoding = encoding
|
|
|
|
|
|
|
|
def __start_element(self, tag: bytes, attributes: Dict[str, str]) -> None:
|
|
|
|
"""
|
|
|
|
Called when we encounter an element open tag. Also called when we encounter
|
|
|
|
an empty element. Creates a new node with the specified name and attributes.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
tag - The string tag name.
|
|
|
|
attributes - A dictionary keyed by attribute name and whose values are the string
|
|
|
|
attribute values. This attribute values should already be decoded from
|
|
|
|
the XML's encoding.
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
data_type = attributes.get("__type")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
array_str = attributes.get("__count")
|
2019-12-08 22:43:49 +01:00
|
|
|
if array_str is not None:
|
|
|
|
array = True
|
|
|
|
else:
|
|
|
|
array = False
|
|
|
|
|
|
|
|
if data_type is None:
|
|
|
|
# Special case for nodes that don't have a type
|
2022-10-15 20:56:30 +02:00
|
|
|
node = Node(name=tag.decode("ascii"), type=Node.NODE_TYPE_VOID)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
# Get the data value
|
|
|
|
type_int = Node.typename_to_type(data_type)
|
|
|
|
if type_int is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise XmlEncodingException(
|
|
|
|
f'Invalid node type {data_type} for node {tag.decode("ascii")}'
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
node = Node(name=tag.decode("ascii"), type=type_int, array=array)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Now, do the attributes
|
|
|
|
for attr in attributes:
|
2022-10-15 20:56:30 +02:00
|
|
|
if attr == "__type" or attr == "__count":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Skip these, handled
|
|
|
|
continue
|
|
|
|
else:
|
2021-05-31 20:08:31 +02:00
|
|
|
node.set_attribute(attr, attributes[attr])
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
self.current.append(node)
|
|
|
|
|
|
|
|
def __end_element(self, tag: bytes) -> None:
|
|
|
|
"""
|
|
|
|
Called when we encounter an element close tag. Also called when we encounter an empty element,
|
|
|
|
after __start_element is called. Does bookkeeping related to element order.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
tag - The string tag name.
|
|
|
|
"""
|
|
|
|
node = self.current.pop()
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if node.name != tag.decode("ascii"):
|
|
|
|
raise Exception(
|
|
|
|
f'Logic error, expected {tag.decode("ascii")} but got {node.name}'
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if len(self.current) == 0:
|
|
|
|
self.root = node
|
|
|
|
else:
|
|
|
|
parent = self.current[-1]
|
|
|
|
parent.add_child(node)
|
|
|
|
|
|
|
|
def __yield_values(self, text: str) -> Iterator[str]:
|
2022-10-15 20:56:30 +02:00
|
|
|
value = ""
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
for c in text:
|
|
|
|
if c.isspace():
|
|
|
|
if len(value) > 0:
|
|
|
|
yield value
|
2022-10-15 20:56:30 +02:00
|
|
|
value = ""
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
value = value + c
|
|
|
|
|
|
|
|
if len(value) > 0:
|
|
|
|
yield value
|
|
|
|
|
|
|
|
def __text(self, text: bytes) -> None:
|
|
|
|
"""
|
|
|
|
Called when we finish parsing arbitrary non-element text. Note that the text passed in is in
|
|
|
|
the XML document's encoding and it is this function's responsibility to decode it.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
text - String text value of the node, as encoded by the XML document's encoding.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
value = text.decode(self.encoding)
|
|
|
|
except UnicodeDecodeError:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise XmlEncodingException("Failed to decode text node with given encoding")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if len(self.current) > 0:
|
|
|
|
data_type = self.current[-1].data_type
|
|
|
|
composite = self.current[-1].is_composite
|
|
|
|
array = self.current[-1].is_array
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if data_type == "void":
|
2019-12-08 22:43:49 +01:00
|
|
|
# We can't handle this
|
|
|
|
return
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if data_type == "str":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Do nothing, already fine
|
2022-10-15 20:56:30 +02:00
|
|
|
value = value.replace("&", "&")
|
|
|
|
value = value.replace("<", "<")
|
|
|
|
value = value.replace(">", ">")
|
|
|
|
value = value.replace("'", "'")
|
|
|
|
value = value.replace(""", '"')
|
2019-12-08 22:43:49 +01:00
|
|
|
if self.current[-1].value is None:
|
|
|
|
self.current[-1].set_value(value)
|
|
|
|
else:
|
|
|
|
self.current[-1].set_value(self.current[-1].value + value)
|
2022-10-15 20:56:30 +02:00
|
|
|
elif data_type == "bin":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Convert from a hex string
|
|
|
|
def hex_to_bin(hexval: str) -> bytes:
|
|
|
|
intval = int(hexval, 16)
|
2022-10-15 20:56:30 +02:00
|
|
|
return struct.pack(">B", intval)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Remove any spaces first
|
2022-10-15 20:56:30 +02:00
|
|
|
value = "".join([c for c in value if not c.isspace()])
|
2019-12-08 22:43:49 +01:00
|
|
|
if self.current[-1].value is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
self.current[-1].set_value(
|
|
|
|
b"".join(
|
|
|
|
[
|
|
|
|
hex_to_bin(value[i : (i + 2)])
|
|
|
|
for i in range(0, len(value), 2)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
self.current[-1].set_value(
|
|
|
|
self.current[-1].value
|
|
|
|
+ b"".join(
|
|
|
|
[
|
|
|
|
hex_to_bin(value[i : (i + 2)])
|
|
|
|
for i in range(0, len(value), 2)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif data_type == "ip4":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Do nothing, already fine
|
|
|
|
self.current[-1].set_value(value)
|
2022-10-15 20:56:30 +02:00
|
|
|
elif data_type == "bool":
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
def conv_bool(val: str) -> bool:
|
2022-10-15 20:56:30 +02:00
|
|
|
if val and val.lower() in ["0", "false"]:
|
2019-12-08 22:43:49 +01:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if array or composite:
|
2022-10-15 20:56:30 +02:00
|
|
|
self.current[-1].set_value(
|
|
|
|
[conv_bool(v) for v in self.__yield_values(value)]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
self.current[-1].set_value(conv_bool(value))
|
2022-10-15 20:56:30 +02:00
|
|
|
elif data_type == "float":
|
2019-12-08 22:43:49 +01:00
|
|
|
if array or composite:
|
2022-10-15 20:56:30 +02:00
|
|
|
self.current[-1].set_value(
|
|
|
|
[float(v) for v in self.__yield_values(value)]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
self.current[-1].set_value(float(value))
|
|
|
|
else:
|
|
|
|
if array or composite:
|
2022-10-15 20:56:30 +02:00
|
|
|
self.current[-1].set_value(
|
|
|
|
[int(v) for v in self.__yield_values(value)]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
self.current[-1].set_value(int(value))
|
|
|
|
|
|
|
|
def __parse_attributes(self, attributes: bytes) -> Dict[str, str]:
|
|
|
|
"""
|
|
|
|
Given a string representing zero or more possible attributes, parse them into
|
|
|
|
a dictionary.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A dictionary keyed by the attribute name and who's values are unescaped strings.
|
|
|
|
If no attributes exist, this returns an empty dictionary.
|
|
|
|
"""
|
|
|
|
attr_stream = InputStream(attributes)
|
|
|
|
parsed_attrs: Dict[str, str] = {}
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "space"
|
|
|
|
attr = b""
|
|
|
|
val = b""
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
def unescape(value: bytes) -> str:
|
|
|
|
val = value.decode(self.encoding)
|
2022-10-15 20:56:30 +02:00
|
|
|
val = val.replace("&", "&")
|
|
|
|
val = val.replace("<", "<")
|
|
|
|
val = val.replace(">", ">")
|
|
|
|
val = val.replace("'", "'")
|
|
|
|
val = val.replace(""", '"')
|
|
|
|
val = val.replace(" ", "\r")
|
|
|
|
return val.replace(" ", "\n")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
c = attr_stream.read_byte()
|
|
|
|
|
|
|
|
if c is None:
|
|
|
|
return parsed_attrs
|
2022-10-15 20:56:30 +02:00
|
|
|
if state == "space":
|
2019-12-08 22:43:49 +01:00
|
|
|
if not c.isspace():
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "attr"
|
2019-12-08 22:43:49 +01:00
|
|
|
attr = c
|
2022-10-15 20:56:30 +02:00
|
|
|
elif state == "attr":
|
|
|
|
if c == b"=":
|
2019-12-08 22:43:49 +01:00
|
|
|
attr = attr.strip()
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "valstart"
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
attr = attr + c
|
2022-10-15 20:56:30 +02:00
|
|
|
elif state == "valstart":
|
2019-12-08 22:43:49 +01:00
|
|
|
if c == b'"':
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "valdouble"
|
|
|
|
val = b""
|
|
|
|
elif c == b"'":
|
|
|
|
state = "valsingle"
|
|
|
|
val = b""
|
|
|
|
elif state == "valdouble":
|
2019-12-08 22:43:49 +01:00
|
|
|
if c == b'"':
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "space"
|
|
|
|
parsed_attrs[attr.decode("ascii")] = unescape(val)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
val = val + c
|
2022-10-15 20:56:30 +02:00
|
|
|
elif state == "valsingle":
|
|
|
|
if c == b"'":
|
|
|
|
state = "space"
|
|
|
|
parsed_attrs[attr.decode("ascii")] = unescape(val)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
val = val + c
|
|
|
|
|
|
|
|
def __split_node(self, content: bytes) -> Tuple[bytes, bytes]:
|
|
|
|
node_stream = InputStream(content)
|
2022-10-15 20:56:30 +02:00
|
|
|
tag = b""
|
|
|
|
attributes = b""
|
2019-12-08 22:43:49 +01:00
|
|
|
state = "tag"
|
|
|
|
|
|
|
|
while True:
|
|
|
|
c = node_stream.read_byte()
|
|
|
|
|
|
|
|
if c is None:
|
|
|
|
break
|
|
|
|
if state == "tag":
|
|
|
|
if c.isspace():
|
|
|
|
state = "space"
|
|
|
|
else:
|
|
|
|
tag = tag + c
|
|
|
|
elif state == "space":
|
|
|
|
if not c.isspace():
|
|
|
|
attributes = c
|
|
|
|
state = "attributes"
|
|
|
|
elif state == "attributes":
|
|
|
|
attributes = attributes + c
|
|
|
|
|
|
|
|
return (tag, attributes)
|
|
|
|
|
|
|
|
def __handle_node(self, content: bytes) -> None:
|
|
|
|
"""
|
|
|
|
Called whenever we encounter any node type. Filters out special nodes,
|
|
|
|
determines whether this is a start, end or empty node, and fires off
|
|
|
|
calls to the respective __start_element and __end_element functions.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
The node contents, minus the < and > characters. This will be encoded
|
|
|
|
in the XML document's encoding.
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
if content[:1] == b"?" and content[-1:] == b"?":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Special node, parse to get the encoding.
|
|
|
|
tag, attributes = self.__split_node(content[1:-1])
|
2022-10-15 20:56:30 +02:00
|
|
|
if tag == b"xml":
|
2019-12-08 22:43:49 +01:00
|
|
|
attributes_dict = self.__parse_attributes(attributes)
|
2022-10-15 20:56:30 +02:00
|
|
|
if "encoding" in attributes_dict:
|
|
|
|
self.encoding = attributes_dict["encoding"]
|
2019-12-08 22:43:49 +01:00
|
|
|
return
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
if content[:1] == b"/":
|
2019-12-08 22:43:49 +01:00
|
|
|
# We got an element end
|
|
|
|
self.__end_element(content[1:])
|
|
|
|
else:
|
|
|
|
# We got a start element
|
2022-10-15 20:56:30 +02:00
|
|
|
if content[-1:] == b"/":
|
2019-12-08 22:43:49 +01:00
|
|
|
# This is an empty element
|
|
|
|
empty = True
|
|
|
|
content = content[:-1]
|
|
|
|
else:
|
|
|
|
# This node has subnodes or text
|
|
|
|
empty = False
|
|
|
|
|
|
|
|
tag, attributes = self.__split_node(content)
|
|
|
|
self.__start_element(tag, self.__parse_attributes(attributes))
|
|
|
|
if empty:
|
|
|
|
self.__end_element(tag)
|
|
|
|
|
2021-05-31 20:08:31 +02:00
|
|
|
def get_tree(self) -> Optional[Node]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Walk the XML document and parse into nodes.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A Node object representing the root of the XML document.
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "text"
|
|
|
|
text = b""
|
|
|
|
node = b""
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
while True:
|
|
|
|
c = self.stream.read_byte()
|
|
|
|
|
|
|
|
if c is None:
|
|
|
|
return self.root
|
2022-10-15 20:56:30 +02:00
|
|
|
elif state == "text":
|
|
|
|
if c == b"<":
|
2019-12-08 22:43:49 +01:00
|
|
|
self.__text(text)
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "node"
|
|
|
|
node = b""
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
text = text + c
|
2022-10-15 20:56:30 +02:00
|
|
|
elif state == "node":
|
|
|
|
if c == b">":
|
2019-12-08 22:43:49 +01:00
|
|
|
self.__handle_node(node)
|
2022-10-15 20:56:30 +02:00
|
|
|
state = "text"
|
|
|
|
text = b""
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
node = node + c
|
|
|
|
|
|
|
|
|
|
|
|
class XmlEncoder:
|
|
|
|
def __init__(self, tree: Node, encoding: str) -> None:
|
|
|
|
"""
|
|
|
|
Initialize the XML encoder.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
tree - A binary blob of data to be decoded
|
|
|
|
encoding - A string representing the text encoding for string elements. Should be either
|
2021-04-05 02:50:08 +02:00
|
|
|
'shift-jis', 'euc-jp', 'utf-8' or 'ascii'.
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
self.tree = tree
|
|
|
|
self.encoding = encoding
|
|
|
|
|
|
|
|
def get_data(self) -> bytes:
|
2022-10-15 20:56:30 +02:00
|
|
|
magic = f'<?xml version="1.0" encoding="{self.encoding}"?>'.encode("ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
payload = self.to_xml(self.tree)
|
|
|
|
|
|
|
|
return magic + payload
|
|
|
|
|
|
|
|
def to_xml(self, node: Node) -> bytes:
|
|
|
|
"""
|
|
|
|
Convert this node, attributes and all children to an XML-like representation of the tree.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
node: A Node representing the root of the tree to be encoded.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Bytes representing the XML-like data for this node and all children.
|
|
|
|
"""
|
|
|
|
attrs_dict = copy.deepcopy(node.attributes)
|
|
|
|
order = sorted(attrs_dict.keys())
|
|
|
|
if node.data_length != 0:
|
|
|
|
# Represent type and length
|
|
|
|
if node.is_array:
|
|
|
|
if node.value is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
attrs_dict["__count"] = "0"
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
attrs_dict["__count"] = str(len(node.value))
|
|
|
|
order.insert(0, "__count")
|
|
|
|
attrs_dict["__type"] = node.data_type
|
|
|
|
order.insert(0, "__type")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def escape(val: Any, attr: bool = False) -> bytes:
|
2019-12-08 22:43:49 +01:00
|
|
|
if isinstance(val, str):
|
2022-10-15 20:56:30 +02:00
|
|
|
val = val.replace("&", "&")
|
|
|
|
val = val.replace("<", "<")
|
|
|
|
val = val.replace(">", ">")
|
|
|
|
val = val.replace("'", "'")
|
|
|
|
val = val.replace('"', """)
|
2019-12-08 22:43:49 +01:00
|
|
|
if attr:
|
2022-10-15 20:56:30 +02:00
|
|
|
val = val.replace("\r", " ")
|
|
|
|
val = val.replace("\n", " ")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return val.encode(self.encoding)
|
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
return str(val).encode("ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if attrs_dict:
|
2022-10-15 20:56:30 +02:00
|
|
|
attrs = b" " + b" ".join(
|
|
|
|
[
|
|
|
|
b"".join(
|
|
|
|
[
|
|
|
|
attr.encode("ascii"),
|
|
|
|
b'="',
|
|
|
|
escape(attrs_dict[attr], attr=True),
|
|
|
|
b'"',
|
|
|
|
]
|
|
|
|
)
|
|
|
|
for attr in order
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
attrs = b""
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
if node.children:
|
|
|
|
# Has children nodes
|
|
|
|
children = [self.to_xml(child) for child in node.children]
|
2022-10-15 20:56:30 +02:00
|
|
|
string = b"".join(
|
|
|
|
[
|
|
|
|
b"<",
|
|
|
|
node.name.encode("ascii"),
|
|
|
|
attrs,
|
|
|
|
b">",
|
|
|
|
b"".join(children),
|
|
|
|
b"</",
|
|
|
|
node.name.encode("ascii"),
|
|
|
|
b">",
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
# Doesn't have children nodes
|
|
|
|
if node.data_length == 0:
|
|
|
|
# Void node
|
2022-10-15 20:56:30 +02:00
|
|
|
string = b"".join(
|
|
|
|
[
|
|
|
|
b"<",
|
|
|
|
node.name.encode("ascii"),
|
|
|
|
attrs,
|
|
|
|
b"/>",
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
# Node with values
|
|
|
|
if node.is_array or node.is_composite:
|
|
|
|
if node.value is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
vals = ""
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
if node.data_type == "bool":
|
|
|
|
vals = " ".join(
|
|
|
|
[("1" if val else "0") for val in node.value]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
2022-10-15 20:56:30 +02:00
|
|
|
vals = " ".join([str(val) for val in node.value])
|
|
|
|
binary = vals.encode("ascii")
|
|
|
|
elif node.data_type == "str":
|
2019-12-08 22:43:49 +01:00
|
|
|
binary = escape(node.value)
|
2022-10-15 20:56:30 +02:00
|
|
|
elif node.data_type == "bool":
|
|
|
|
binary = b"1" if node.value else b"0"
|
|
|
|
elif node.data_type == "ip4":
|
|
|
|
vals = ".".join([str(val) for val in node.value])
|
|
|
|
binary = vals.encode("ascii")
|
|
|
|
elif node.data_type == "bin":
|
2019-12-08 22:43:49 +01:00
|
|
|
# Convert to a hex string
|
|
|
|
def bin_to_hex(binary: int) -> str:
|
|
|
|
val = hex(binary)[2:]
|
|
|
|
while len(val) < 2:
|
2022-10-15 20:56:30 +02:00
|
|
|
val = "0" + val
|
2019-12-08 22:43:49 +01:00
|
|
|
return val
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
vals = "".join([bin_to_hex(v) for v in node.value])
|
|
|
|
binary = vals.encode("ascii")
|
2019-12-08 22:43:49 +01:00
|
|
|
else:
|
|
|
|
vals = str(node.value)
|
2022-10-15 20:56:30 +02:00
|
|
|
binary = vals.encode("ascii")
|
|
|
|
|
|
|
|
string = b"".join(
|
|
|
|
[
|
|
|
|
b"<",
|
|
|
|
node.name.encode("ascii"),
|
|
|
|
attrs,
|
|
|
|
b">",
|
|
|
|
binary,
|
|
|
|
b"</",
|
|
|
|
node.name.encode("ascii"),
|
|
|
|
b">",
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
|
|
|
class XmlEncoding:
|
|
|
|
"""
|
|
|
|
Wrapper class representing an XML encoding.
|
|
|
|
"""
|
2022-10-15 20:56:30 +02:00
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
# The string values should match the constants in EAmuseProtocol.
|
|
|
|
# I have no better way to link these than to write this comment,
|
|
|
|
# as otherwise we would have a circular dependency.
|
2022-10-15 20:56:30 +02:00
|
|
|
ACCEPTED_ENCODINGS: Final[List[str]] = ["shift-jis", "euc-jp", "utf-8", "ascii"]
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""
|
|
|
|
Initialize the encoding object.
|
|
|
|
"""
|
|
|
|
self.encoding: Optional[str] = None
|
|
|
|
|
|
|
|
def __fix_encoding(self, encoding: str) -> str:
|
|
|
|
"""
|
|
|
|
Given an encoding, try to normalize it, looking for specific ways that
|
|
|
|
older games might send it back.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
encoding - The encoding we want to normalize.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A new encoding string that is equivalent but normalized.
|
|
|
|
"""
|
|
|
|
encoding = encoding.lower()
|
2022-10-15 20:56:30 +02:00
|
|
|
encoding = encoding.replace("_", "-")
|
2019-12-08 22:43:49 +01:00
|
|
|
return encoding
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def decode(self, data: bytes, skip_on_exceptions: bool = False) -> Optional[Node]:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a data blob, decode the data with the current encoding. Will set
|
|
|
|
the class property value 'encoding' to the encoding used on the last
|
|
|
|
decode.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
data - Blob of text representing the data to decode.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Node object representing the root of the decoded tree, or None
|
|
|
|
if we couldn't decode the object for some reason.
|
|
|
|
"""
|
|
|
|
# Always assume this, unless we get told otherwise in the XML
|
2022-10-15 20:56:30 +02:00
|
|
|
self.encoding = "shift-jis"
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Decode property/value
|
|
|
|
try:
|
|
|
|
xml = XmlDecoder(data, self.encoding)
|
|
|
|
tree = xml.get_tree()
|
|
|
|
self.encoding = xml.encoding
|
|
|
|
return tree
|
|
|
|
except XmlEncodingException:
|
|
|
|
if skip_on_exceptions:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2022-10-15 20:56:30 +02:00
|
|
|
def encode(self, tree: Node, encoding: Optional[str] = None) -> bytes:
|
2019-12-08 22:43:49 +01:00
|
|
|
"""
|
|
|
|
Given a tree of Node objects, encode the data with the current encoding.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
tree - Node tree representing the data to encode
|
|
|
|
encoding - The text encoding to use. If None, will try to use the encoding from
|
|
|
|
the last successful decode
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
String blob representing encoded data as XML.
|
|
|
|
"""
|
|
|
|
# Ensure we got the right encoding
|
|
|
|
if encoding is None:
|
|
|
|
encoding = self.encoding
|
|
|
|
if encoding is None:
|
2022-10-15 20:56:30 +02:00
|
|
|
raise XmlEncodingException("Unknown encoding")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
encoding = self.__fix_encoding(encoding)
|
|
|
|
if encoding not in XmlEncoding.ACCEPTED_ENCODINGS:
|
2021-04-05 02:50:08 +02:00
|
|
|
# XML pages only support a few encodings.
|
2020-01-07 22:29:07 +01:00
|
|
|
raise XmlEncodingException(f"Invalid text encoding {encoding}")
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
xml = XmlEncoder(tree, encoding)
|
|
|
|
return xml.get_data()
|