-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcx_decompiler.py
1037 lines (713 loc) · 23.8 KB
/
gcx_decompiler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# GCX/GCL scripts decompiler by oct0xor
import sys, os, struct
class code_info:
def __init__(self, name, func):
self.name = name
self.func = func
class command_info:
def __init__(self, name, func):
self.name = name
self.func = func
class gcx_parser:
def __init__(self, data, print_header = False, print_debug = False, print_resources = False, dictionary = []):
self.data = data
self.pos = 0
self.print_debug = print_debug
self.dictionary = dictionary
self.unknown_strcodes = []
self.strings = []
self.version = self.read_u32()
self.procs = []
self.proc_table_type = 0
flag = False
while(True):
# Parse proc table type 1
proc_value_1 = self.read_u32()
proc_value_2 = self.read_u32()
if (proc_value_1 == 0 and proc_value_2 == 0):
self.proc_table_type = 1
flag = True
break
if (proc_value_1 == 0xFFFFFFFF or proc_value_2 == 0xFFFFFFFF):
break
self.procs.append([proc_value_1, proc_value_2])
if (not flag):
# Parse proc table type 2
self.pos = 4
self.procs = []
while(True):
proc_value = self.read_u32()
if (proc_value == 0xFFFFFFFF):
self.proc_table_type = 2
flag = True
break
self.procs.append([0, proc_value])
if (not flag):
print("ERROR! UNABLE TO PARSE PROC TABLE")
exit(0)
self.gcl_strres_block_top = self.pos
script_offset = self.read_u32()
resource_table_offset = self.read_u32()
string_table_offset = self.read_u32()
font_data_offset = self.read_u32()
key = self.read_u32()
self.gcl_script_proc_body_size = self.read_u32_at_offset(self.gcl_strres_block_top + script_offset)
self.gcl_script_proc_body_data = self.gcl_strres_block_top + script_offset + 4
self.gcl_script_main_body_size = self.read_u32_at_offset(self.gcl_script_proc_body_data + self.gcl_script_proc_body_size)
self.gcl_script_main_body_data = self.gcl_script_proc_body_data + self.gcl_script_proc_body_size + 4
self.gcl_strres_resource_table = self.gcl_strres_block_top + resource_table_offset
self.gcl_strres_string_table = self.gcl_strres_block_top + string_table_offset
self.gcl_strres_font_data = self.gcl_strres_block_top + font_data_offset
self.script = ""
if (print_header):
self.script += "// Timestamp: 0x%X\n" % self.version
self.script += "//\n"
self.script += "// Proc Num: 0x%X\n" % len(self.procs)
for proc in self.procs:
self.script += "// \tProc: 0x%X 0x%X\n" % (proc[0], proc[1])
self.script += "//\n"
self.script += "// Resource Block Top: 0x%X\n" % self.gcl_strres_block_top
self.script += "// Script Proc Body: 0x%X (0x%X)\n" % (self.gcl_script_proc_body_data, self.gcl_script_proc_body_size)
self.script += "// Script Main Body: 0x%X (0x%X)\n" % (self.gcl_script_main_body_data, self.gcl_script_main_body_size)
self.script += "// Resource Table: 0x%X\n" % self.gcl_strres_resource_table
self.script += "// Resource String Table: 0x%X\n" % self.gcl_strres_string_table
self.script += "// Resource Font Data: 0x%X\n" % self.gcl_strres_font_data
self.script += "\n"
if (print_resources and self.gcl_strres_resource_table != self.gcl_strres_string_table):
count = (self.gcl_strres_string_table - self.gcl_strres_resource_table) / 4
resources = []
for i in range(count):
resources.append(self.gcl_strres_string_table + self.read_u32_at_offset(self.gcl_strres_resource_table + i * 4))
resource_strings = []
if (len(resources) > 0):
# TODO: In MGS3/MGS4 resources are encrypted
# do
# {
# v11 = v12-- <= 1;
# v10 = 0x7D2B89DD * v10 + 0xCF9;
# *v8++ ^= v10 >> 15;
# }
# while ( !v11 );
resources.append(self.gcl_strres_font_data)
for i in range(len(resources) - 1):
start = resources[i] & 0xFFFFFF
end = resources[i+1] & 0xFFFFFF
resource = self.data[start:end]
#text = ""
#for c in resource:
# if (c in string.printable and c not in ['\n', '\r', '\t']):
# text += c
# else:
# text += "\\x%02X" % (ord(c))
text = resource.encode('hex')
resource_strings.append(text)
resources.pop()
self.script += "// Resources Num: 0x%X\n" % count
for i in range(count):
self.script += "// \tResource: 0x%X - %s\n" % (resources[i], resource_strings[i])
self.script += "\n"
self.code_types = {
0: code_info("End", None),
1: code_info("Short", self.handle_numeric_value),
2: code_info("Byte", self.handle_numeric_value),
3: code_info("Byte", self.handle_numeric_value),
4: code_info("Byte", self.handle_numeric_value),
6: code_info("StrCode", self.handle_numeric_value),
7: code_info("String", self.handle_string),
8: code_info("StrCode", self.handle_numeric_value),
9: code_info("Long", self.handle_numeric_value),
0xA: code_info("Long", self.handle_numeric_value),
0xD: code_info("Long", self.handle_numeric_value),
0xE: code_info("StringResource", self.handle_string_resource),
0x10: code_info("Var", self.handle_0x10_0x20),
0x20: code_info("VarArray", self.handle_0x10_0x20),
0x30: code_info("Expr", self.handle_0x30),
0x40: code_info("Args", self.handle_0x40),
0x50: code_info("Param", self.handle_0x50),
0x60: code_info("Command", self.handle_0x60),
0x70: code_info("Call", self.handle_0x70),
0x80: code_info("Proc", self.handle_0x80),
0x90: code_info("Local", self.handle_0x90),
0xC0: code_info("Num", self.handle_0xC0),
}
self.builtin_commands = {
0xD86: command_info(".if", self.handle_if), # strcode("if")
0xA65DB5: command_info(".switch", self.handle_switch), # strcode("switch")
0x34648C: command_info(".eval", None), # strcode("eval")
0x3311EC: command_info(".call", None), # strcode("call")
0x8BE398: command_info(".return", None), # strcode("return")
0x3AB23B: command_info(".print", None), # strcode("print")
}
self.script_commands = {
0x3822C7: command_info(".mesg", None), # strcode("mesg")
0x82BC9: command_info(".command", None), # strcode("command")
0x6592A7: command_info(".chara", None), # strcode("chara")
0x3BD490: command_info(".trap", None), # strcode("trap")
0x37C884: command_info(".load", None), # strcode("load")
0x8B3DF5: command_info(".script_command_0x8B3DF5", None),
0x1C090: command_info(".map", None), # strcode("map")
0x6BB005: command_info(".restart", None), # strcode("restart")
}
self.indent_level = 0
self.pos = self.gcl_script_main_body_data
size = self.get_block_size()
end = self.pos + size
self.decompile(end, "main")
i = 1
for proc in self.procs:
strcode = proc[0]
pos = proc[1] & 0xFFFFFF
self.pos = self.gcl_script_proc_body_data + pos
comment = ""
if (self.proc_table_type == 1):
proc_name = ""
if (strcode in self.dictionary):
proc_name = "code(\"%s\")" % self.dictionary[strcode]
elif (strcode not in self.unknown_strcodes):
self.unknown_strcodes.append(strcode)
if (self.print_debug):
if (proc_name != ""):
comment = " // 0x%X - %s" % (self.pos, proc_name)
else:
comment = " // 0x%X" % self.pos
elif (proc_name != ""):
comment = " // %s" % proc_name
name = "proc_0x%X" % strcode
elif (self.proc_table_type == 2):
if (self.print_debug):
comment = " // 0x%X" % self.pos
name = "proc_id_%d" % i
else:
print("ERROR! UNEXPECTED PROC TABLE TYPE")
exit(0)
size = self.get_block_size()
end = self.pos + size
self.decompile(end, name, comment)
i += 1
def read_u8_at_offset(self, offset):
value = struct.unpack("<B", self.data[offset:offset+1])[0]
return value
def read_u8(self):
value = self.read_u8_at_offset(self.pos)
self.pos += 1
return value
def read_u16(self):
value = struct.unpack("<H", self.data[self.pos:self.pos+2])[0]
self.pos += 2
return value
def read_u24(self):
return (self.read_u8() | (self.read_u16() << 8))
def read_u32_at_offset(self, offset):
value = struct.unpack("<L", self.data[offset:offset+4])[0]
return value
def read_u32(self):
value = self.read_u32_at_offset(self.pos)
self.pos += 4
return value
def read_u32be(self):
value = struct.unpack(">L", self.data[self.pos:self.pos+4])[0]
self.pos += 4
return value
def decompile(self, block_end, func_name, comment = ""):
self.stop = False
s = "%s {%s\n\n" % (func_name, comment)
while (self.pos < block_end):
code = self.read_u8_at_offset(self.pos)
type_p = self.decode_type(code)
value = self.decode_value(block_end)
if (type_p == 0x30): # Expr, we dont want to have parentheses in this case
value = self.fix_expr(value)
s += value
if (self.stop):
break
s += "}\n"
s = s.replace('\n\n\n', '\n\n')
self.script += s + '\n'
def decode_type(self, code):
code_high = code & 0xF0
code_low = code & 0xF
if ((code & 0xC0) == 0xC0):
return 0xC0
if (code_high != 0):
if (code_high in [0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90]):
return code_high
else:
if (code in [0, 1, 2, 3, 4, 6, 7, 8, 9, 0xA, 0xD, 0xE]):
return code
self.stop = True
def get_block_size(self):
value = self.read_u8() & 0xF
if (value == 13):
# 2 bytes
return self.read_u8()
elif (value == 14):
# 3 bytes
return self.read_u16()
elif (value == 15):
# 4 bytes
return self.read_u24()
return value
def handle_numeric_value(self, block_end, inline):
code = self.read_u8()
if (code == 1): # Short
return "0x%X" % self.read_u16()
elif (code in [2, 3, 4]): # Byte
return "0x%X" % self.read_u8()
#return "Type_%d: 0x%X" % (code, self.read_u8())
elif (code in [6, 8]): # StrCode (24 bits) # TODO: Type_8 is proc_ ?
strcode = self.read_u24()
value = ""
if (strcode in self.dictionary):
value = "code(\"%s\")" % self.dictionary[strcode]
else:
if (strcode not in self.unknown_strcodes):
self.unknown_strcodes.append(strcode)
value = "0x%X" % strcode
return value
#return "Type_%d: %s" % (code, value)
elif (code in [9, 0xA, 0xD]): # Long
return "0x%X" % self.read_u32() # TODO: Type_13 is BE?
#return "Type_%d: 0x%X" % (code, self.read_u32())
def handle_string(self, block_end, inline):
self.pos += 1
size = self.read_u8()
s = self.data[self.pos:self.pos + size - 1]
self.pos += size
if (s not in self.strings):
self.strings.append(s)
try:
s = s.decode("EUC-JP")
except:
s = "DECOMPILER: FAILED TO DECODE EUC-JP STRING!"
return '"' + s + '"'
def handle_string_resource(self, block_end, inline):
self.pos += 1
value = self.read_u16()
offset = self.read_u32_at_offset(self.gcl_strres_resource_table + value * 4)
return "resource(0x%X)" % (self.gcl_strres_string_table + offset)
def handle_0x10_0x20(self, block_end, inline):
return self.gcl_get_var()
def handle_0x30(self, block_end, inline):
size = self.get_block_size()
end = self.pos + size
expr = self.gcl_expr(end, inline)
self.pos = end
return expr
def handle_0x40(self, block_end, inline):
code_low = self.read_u8() & 0xF
if (code_low == 15):
# 2 bytes
arg_id = self.read_u8() + 15
else:
# 1 byte
arg_id = code_low
return "$arg%d" % arg_id
def handle_0x50(self, block_end, inline):
# Param - tricky case...
# In original code param_type is packed into type_p (type_p |= self.read_u8() << 16) and passed outside (if_command for example)
size = self.get_block_size()
pos = self.pos
# 1 byte = type_p, value
param_type = self.read_u8()
param_pos = self.pos
self.pos = pos + size
return [param_type, param_pos, self.pos]
def handle_0x60(self, block_end, inline):
size = self.get_block_size()
end = self.pos + size
cmd = self.gcl_command(end, inline)
self.pos = end
return cmd
def handle_0x70(self, block_end, inline):
size = self.get_block_size()
end = self.pos + size
call = self.gcl_call(end, inline)
self.pos = end
return call
def fix_expr(self, value):
indents = 0
for c in value:
if (c == '\t'):
indents += 1
else:
break
if (value[indents] == '(' and value[-2:] == ")\n"):
value = "\t" * indents + value[indents+1:-2] + '\n'
return value
def handle_0x80(self, block_end, inline):
size = self.get_block_size()
end = self.pos + size
if (self.print_debug):
s = "proc_block_0x%X {\n" % self.pos
else:
s = "proc {\n"
while (self.pos < end):
code = self.read_u8_at_offset(self.pos)
type_p = self.decode_type(code)
value = self.decode_value(end, False)
if (type_p == 0x30): # Expr, we dont want to have parentheses in this case
value = self.fix_expr(value)
s += value
if (self.stop):
return s
s += "\t" * self.indent_level + '} '
self.pos = end
return s
def handle_0x90(self, block_end, inline):
return self.gcl_get_local(self.read_u8())
def handle_0xC0(self, block_end, inline):
return self.gcl_get_num(self.read_u8())
def format_param(self, param_type, param_start, param_end, values, inline):
if (self.print_debug):
s = "{ Param type = '%s' (0x%X), start = 0x%X, end = 0x%X } {" % (param_type, ord(param_type), param_start, param_end)
else:
s = ".param('%s') {" % param_type
if (len(values) > 0):
s += '\n'
for value in values:
s += value
if (not s.endswith('\n')):
#print("ERROR! PARAM NOT ENDS WITH '\\n'")
#exit(0)
s += "/* ERROR! PARAM NOT ENDS WITH '\\n' */"
else:
s = s[:-1]
s += ', \n'
s = s[:-3] + '\n'
if (not inline):
s += "\t" * self.indent_level
s += '}'
return s
def decode_value(self, block_end = None, inline = False, param_formatter = None):
start = self.pos
code = self.read_u8_at_offset(self.pos)
type_p = self.decode_type(code)
if (self.stop):
print("ERROR! WRONG CODE 0x%X - 0x%X" % (self.pos, code))
exit(0)
type_info = self.code_types[type_p]
type_name = type_info.name
type_func = type_info.func
self.indent_level += 1
value = ""
if (type_p == 0):
self.pos += 1
else:
value = type_func(block_end, inline)
if (type_p == 0x50):
param_type = chr(value[0])
param_start = value[1]
param_end = value[2]
pos_copy = self.pos
self.pos = param_start
param_values = []
while (self.pos < param_end):
param_values.append(self.decode_value(param_end, inline))
if (self.stop):
break
self.pos = pos_copy
if (param_formatter):
value = param_formatter(param_type, param_start, param_end, param_values, inline)
else:
value = self.format_param(param_type, param_start, param_end, param_values, inline)
s = ""
if (self.print_debug):
s = "0x%X : " % start + type_name
if (type_p != 0):
s += " : "
s += value
if (not inline and (self.print_debug or type_p != 0)):
s = "\t" * self.indent_level + s + "\n"
self.indent_level -= 1
if (type_p in [0x60, 0x70]):
s = '\n' + s
return s
def gcl_get_short_size(self):
value = self.read_u8()
if ((value & 0x80) == 0):
# 1 byte
return value
else:
# 2 bytes
value2 = self.read_u8()
value = ((value << 8) | (value2)) & 0x7FFF
return value
def parse_command(self, name, block_end):
args = []
while (self.pos < block_end):
args.append(self.decode_value(block_end, True))
if (self.stop):
break
s = name + '('
if (len(args) > 0):
for arg in args:
s += arg
s += ", "
s = s[:-2]
s += ')\n'
return s
def parse_params(self, block_end, inline, param_formatter = None):
s = ""
params = []
params_start = self.pos
if (self.stop == False):
while (self.pos < block_end):
params.append(self.decode_value(block_end, inline, param_formatter))
if (self.stop):
break
if (len(params) > 0):
if (self.print_debug):
s += "\t" * self.indent_level + "0x%X : " % params_start + "Command line:\n"
for param in params:
s += param
return s
def format_switch_param(self, param_type, param_start, param_end, values, inline):
if (param_type == 'c'):
s = "case %s:\n" % values[0].replace('\t', '').replace('\n', '')
s += values[1]
s += "\t" * (self.indent_level + 1) + "break" + "\n"
return s
if (param_type == 'd'):
s = "default:\n"
s += values[0]
s += "\t" * (self.indent_level + 1) + "break" + "\n"
return s
s = self.format_param(param_type, param_start, param_end, values, inline)
return s
def handle_switch(self, block_end, inline):
s = "\t" * self.indent_level + "{" + "\n"
s += self.parse_params(block_end, inline, self.format_switch_param) + "\n"
s += "\t" * self.indent_level + "}" + "\n"
return s
def format_if_param(self, param_type, param_start, param_end, values, inline):
s = None
if (param_type == 'i'):
s = ".elif("
elif (param_type == 'e'):
s = ".else("
if (s != None):
if (len(values) > 0):
for value in values:
while (value[0] == '\t'):
value = value[1:]
s += value[:-1]
s += ", "
s = s[:-2]
s += ')\n'
return s
return self.format_param(param_type, param_start, param_end, values, inline)
def handle_if(self, block_end, inline):
s = self.parse_params(block_end, inline, self.format_if_param) + "\n"
return s
def gcl_command(self, block_end, inline):
s = ""
if (inline == True):
#print("ERROR! UNEXPECTED INLINE")
#exit(0)
s += "/* ERROR! UNEXPECTED INLINE */"
command = self.read_u24()
size = self.gcl_get_short_size()
name = None
func = None
if (command in self.builtin_commands):
name = self.builtin_commands[command].name
func = self.builtin_commands[command].func
elif (command in self.script_commands):
name = self.script_commands[command].name
func = self.script_commands[command].func
elif (command in self.dictionary):
name = '.' + self.dictionary[command]
else:
name = ".unknown_command_0x%X" % command
s += self.parse_command(name, self.pos + size)
if (func):
s += func(block_end, inline)
else:
s += self.parse_params(block_end, inline)
return s
def gcl_get_num(self, code):
return "%d" % ((code & 0x3F) - 1)
def gcl_get_local(self, code):
return "$local:%d" % (code & 0x0F)
def gcl_get_var(self):
varcode = self.read_u32be()
code = varcode >> 0x18
region = "varbuf"
if ((varcode & 0xF00000) == 0x800000):
region = "linkvarbuf"
elif ((varcode & 0xF00000) == 0x100000):
region = "localvarbuf"
offset = varcode & 0xFFFF
s = "$var:%s_0x%X" % (region, offset)
if ((code & 0xF0) == 0x20):
array_max = self.decode_value(None, True)
if (self.stop):
s += array_max
return s
array_offset = self.decode_value(None, True)
if (self.stop):
s += array_offset
return s
s += "[%s,%s]" % (array_offset, array_max)
return s
def gcl_call(self, block_end, inline):
args = []
proc_name = ""
if (self.proc_table_type == 1):
proc = self.read_u24()
if (proc in self.dictionary):
proc_name = " code(\"%s\")" % self.dictionary[proc]
elif (proc not in self.unknown_strcodes):
self.unknown_strcodes.append(proc)
proc_name = "proc_0x%X%s" % (proc, proc_name)
elif (self.proc_table_type == 2):
proc = self.read_u16()
proc_name = "proc_id_%d" % proc
else:
print("ERROR! UNEXPECTED PROC TABLE TYPE")
exit(0)
code = self.read_u8_at_offset(self.pos)
type_p = self.decode_type(code) # TODO: We actually need to have the last decoded
if (self.stop):
print("ERROR! WRONG CODE 0x%X - 0x%X" % (self.pos, code))
exit(0)
value = self.decode_value(block_end, True)
if (self.stop):
return value
while (type_p != 0):
if (len(args) >= 16):
print("ERROR! TOO MANY ARGS PROC 0x%X" % proc)
exit(0)
args.append(value)
code = self.read_u8_at_offset(self.pos)
type_p = self.decode_type(code)
if (self.stop):
print("ERROR! WRONG CODE 0x%X - 0x%X" % (self.pos, code))
exit(0)
value = self.decode_value(block_end, True)
if (self.stop):
return value
s = proc_name + " ("
if (len(args) > 0):
for arg in args:
s += arg + ", "
s = s[:-2]
s += ')'
return s
def gcl_expr(self, block_end, inline):
expr_stack = [] # In original code MAX = 8, buffer overflow lol
while (True):
while (True):
pos = self.pos
code = self.read_u8_at_offset(self.pos)
type_p = code & 0xE0
if (type_p == 0xA0):
break
value = self.decode_value(block_end, True)
if (self.stop):
return value
expr_stack.append([value, pos])
opcode = code & 0xFFFFFF1F
if (opcode == 0):
break
value2 = expr_stack.pop()[0]
value1 = expr_stack.pop()[0]
value = self.decode_opcode(opcode, value1, value2)
expr_stack.append(["(" + value + ")", None])
self.pos += 1
return expr_stack[0][0]
def decode_opcode(self, opcode, value1, value2):
if (opcode == 0): return None
elif (opcode == 1): return "-" + value2
elif (opcode == 2): return value2 + " == 0"
elif (opcode == 3): return "~" + value2
elif (opcode == 4): return value1 + " + " + value2
elif (opcode == 5): return value1 + " - " + value2
elif (opcode == 6): return value1 + " * " + value2
elif (opcode == 7): return value1 + " / " + value2
elif (opcode == 8): return value1 + " % " + value2
elif (opcode == 9): return value1 + " << " + value2
elif (opcode == 10): return value1 + " >> " + value2
elif (opcode == 11): return value1 + " == " + value2
elif (opcode == 12): return value1 + " != " + value2
elif (opcode == 13): return value1 + " < " + value2
elif (opcode == 14): return value1 + " <= " + value2
elif (opcode == 15): return value1 + " > " + value2
elif (opcode == 16): return value1 + " >= " + value2
elif (opcode == 17): return value1 + " | " + value2
elif (opcode == 18): return value1 + " & " + value2
elif (opcode == 19): return value1 + " ^ " + value2
elif (opcode == 20): return value1 + " || " + value2
elif (opcode == 21): return value1 + " && " + value2
elif (opcode == 22): return value1 + " = " + value2
elif (opcode == 23): return value2
else:
print("ERROR! BAD OPCODE: 0x%X" % opcode)
exit(0)
STRCODES = {}
BAD_STRCODES = []
def read_dictionary(path):
global STRCODES
global BAD_STRCODES
f = open(path, "rb")
lines = f.readlines()
f.close()
for line in lines:
s = line.split()
if (len(s) == 2):
strcode = int(s[0], 16)
name = s[1].decode()
if (strcode in STRCODES):
#print("WARNING! StrCode 0x%X is already present in dictionary! %s - %s" % (strcode, STRCODES[strcode], name))
BAD_STRCODES.append(strcode)
continue
STRCODES[strcode] = name
def clean_dictionary():
global STRCODES
global BAD_STRCODES
temp = {}
for strcode in STRCODES:
if (strcode not in BAD_STRCODES):
temp[strcode] = STRCODES[strcode]
STRCODES = temp
directory = "dictionaries"
if (os.path.isdir(directory)):
for entry in os.listdir(directory):
try:
read_dictionary(os.path.join(directory, entry))
except:
pass
clean_dictionary()
if (len(sys.argv) == 1):
print("Usage: %s <input file/folder> <output file/folder>" % os.path.basename(sys.argv[0]))
exit(0)
input_is_directory = os.path.isdir(sys.argv[1])
output_is_file = False