-
Notifications
You must be signed in to change notification settings - Fork 0
/
poketext.py
157 lines (129 loc) · 4.72 KB
/
poketext.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
# -*- coding: latin-1 -*-
import array
import unicodeparser
class BinaryData16:
def __init__(self, string):
self.s = array.array('H',string)
def read16(self, ofs):
return self.s[ofs>>1]
def write16(self, d,ofs):
self.s[ofs>>1]=d
def read32(self, ofs):
return self.s[ofs>>1] | (self.s[(ofs>>1)+1]<<16)
def write32(self, d, ofs):
self.s[ofs>>1]=(d&0xffff)
self.s[(ofs>>1)+1]=((d>>16)&0xffff)
def getStr(self):
return self.s.tostring()
class BinaryData:
def __init__(self, string):
self.s = array.array('c',string)
def read16(self, ofs):
return ord(self.s[ofs]) | (ord(self.s[ofs+1])<<8)
def write16(self, d,ofs):
self.s[ofs]=chr(d&0xff)
self.s[ofs+1]=chr((d>>8)&0xff)
def read32(self, ofs):
return ord(self.s[ofs]) | (ord(self.s[ofs+1])<<8) | (ord(self.s[ofs+2])<<16) | (ord(self.s[ofs+3])<<24)
def write32(self, d, ofs):
self.s[ofs]=chr(d&0xff)
self.s[ofs+1]=chr((d>>8)&0xff)
self.s[ofs+2]=chr((d>>16)&0xff)
self.s[ofs+3]=chr((d>>24)&0xff)
def getStr(self):
return self.s.tostring()
class PokeTextData(BinaryData16): #16 = mayor speed improvement
def decrypt(self):
self.DecyptPtrs(self.read16(0), self.read16(2), 4)
self.ptrlist = self.CreatePtrList(self.read16(0), 4)
self.strlist = []
for i in range(self.read16(0)):
ptr, chars = self.ptrlist[i]
self.DecyptTxt(chars, i+1, ptr)
m = self.MakeString(chars, ptr)
#print m
self.strlist.append(m)
def encrypt(self):
self.ptrlist = self.CreatePtrList(self.read16(0), 4)
for i in range(self.read16(0)):
ptr, chars = self.ptrlist[i]
self.DecyptTxt(chars, i+1, ptr)
self.DecyptPtrs(self.read16(0), self.read16(2), 4)
def DecyptPtrs(self, count, key, sdidx):
key = (key * 0x2FD) &0xffff
for i in range(count):
key2 = (key*(i+1)&0xffff)
realkey = key2 | (key2<<16)
self.write32(self.read32(sdidx)^realkey, sdidx)
self.write32(self.read32(sdidx+4)^realkey, sdidx+4)
sdidx+=8
def CreatePtrList(self, count, sdidx):
ptrlist = []
for i in range(count):
ptrlist.extend([[self.read32(sdidx), self.read32(sdidx+4)]])
sdidx+=8
return ptrlist
def DecyptTxt(self, count, id, idx):
key = (0x91BD3*id)&0xffff
for i in range(count):
self.write16(self.read16(idx)^key, idx)
key +=0x493D
key&=0xffff
idx+=2
def MakeString(self, count, idx):
string = ""
chars=[]
uncomp=[]
for i in range(0, count):
chars.append(self.read16(idx))
idx+=2
if chars[0] ==0xF100:
j=1
shift1 = 0
trans = 0
while 1:
tmp = chars[j]
tmp = tmp >> shift1
tmp1 = tmp
if shift1 >= 0xf:
shift1 -= 0xf
if shift1 > 0:
tmp1 = (trans | ((chars[j]<< (9-shift1)) & 0x1FF))
if tmp1 == 0x1ff:
break
#string += unicodeparser.tb[tmp1]
uncomp.append(tmp1)
else:
tmp1 = ((chars[j] >> shift1) & 0x1FF)
if tmp1 == 0x1ff:
break
#string += unicodeparser.tb[tmp1]
uncomp.append(tmp1)
shift1 += 9
if shift1 < 0xf:
trans = ((chars[j] >> shift1) & 0x1FF)
shift1 += 9
j+=1
chars=uncomp
i = 0
#constructs \v0000\z0000\z0000 <-- auto sets second Char
for c in range(0, len(chars)):
try:
string = string + unicodeparser.tb[chars[i]]
except:
if chars[i]==0xfffe:
i+=1
string = string + "\\v" + "%04X"%chars[i]
i+=1
total = chars[i]
for z in range(0,total):
i+=1
string = string + "\\z" + "%04X"%chars[i]
elif chars[i]==0xffff:
break
else:
string = string + "\\x" + "%04X"%chars[i]
i+=1
return string
def SetKey(self, key):
self.write16(key,2)