-
Notifications
You must be signed in to change notification settings - Fork 14
/
eamxml.py
61 lines (50 loc) · 2.05 KB
/
eamxml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import argparse
import lxml.etree as etree
try:
from ctypes import *
import codecs
libeamxml = cdll.LoadLibrary("libeamxml.dll")
libeamxml.get_raw_xml.restype = c_char_p
libeamxml.get_binxml.restype = POINTER(c_byte)
def get_raw_xml(input):
try:
output = libeamxml.get_raw_xml(bytes(input), len(input))
output = bytes(output.decode("Shift-JIS"), 'shift-jis')
root = etree.fromstring(output)
return etree.tostring(root, encoding='unicode', pretty_print=True)
except:
return ""
def get_binxml(input):
input = input.encode("shift-jis")
output_size = c_int()
return bytearray(string_at(libeamxml.get_binxml(input, len(input), byref(output_size)), output_size.value))
except:
try:
from kbinxml import KBinXML
def get_raw_xml(input):
try:
output = KBinXML(input).to_text()
root = etree.fromstring(output.encode('utf-8'))
return etree.tostring(root, encoding='unicode', pretty_print=True)
except:
return ""
def get_binxml(input):
return KBinXML(input.encode('utf-8')).to_binary()
except:
print("Couldn't load any code to handle binary XML files")
exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-e', '--encode', action='store_true', help='Encode mode')
group.add_argument('-d', '--decode', action='store_true', help='Decode mode')
parser.add_argument('-i', '--input', help='Input file', required=True)
parser.add_argument('-o', '--output', help='Output file', required=True)
args = parser.parse_args()
data = open(args.input, "rb").read()
if args.encode:
output = get_binxml(data.decode('utf-8'))
else:
output = bytearray(get_raw_xml(data), encoding="utf-8")
open(args.output, "wb").write(output)
print("Finished writing data to {}!".format(args.output))