-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbsEncrypt.py
230 lines (215 loc) · 6.49 KB
/
dbsEncrypt.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
# -*- coding: utf-8 -*-
# For Windows OS Only....
import sys
import os
from io import BytesIO
import struct
from Decryption import Decrypt5,Decrypt3,FakeCompress,Compress
class Header:
fileSize=0
headerData=b''
fileSize=0
lineCount=0
dataCount=0
lineIndexOffset=0
dataFormatOffset=0
lineDataIndexOffset=0
textOffset=0
def __init__(H,f):
f.seek(0)
H.fileSize=struct.unpack('I',f.read(4))[0]
H.headerData=f.read(24)
H.headerList=struct.unpack('6I',H.headerData)
H.lineCount=H.headerList[0]
H.dataCount=H.headerList[1]
H.lineIndexOffset=H.headerList[2]
H.dataFormatOffset=H.headerList[3]
H.lineDataIndexOffset=H.headerList[4]
H.textOffset=H.headerList[5]
def Transcode(uni):
tran=''
for ch in uni:
try:
ch.encode("GBK")
except:
tran+=u"·"
else:
tran+=ch
return tran.encode("GBK")
def main(argv):
if argv.count('-c')>0:
try:
comp=int(argv[argv.index('-c')+1])
except:
comp=17
else:
argv.pop(argv.index('-c')+1)
if comp<2:
comp=2
elif comp>17:
comp=17
argv.remove('-c')
else:
comp=0
if len(argv)<2 or argv[1]=='':
print ("Usage: "+argv[0][argv[0].rfind("\\")+1:]+" <dbs.out> [dbs.txt] [-c [2~17]]")
return False
elif len(argv) >2 and argv[2]:
txtFN=argv[2]
else:
txtFN=argv[1].replace('.out','.txt')
inFN=argv[1]
if inFN[-8:]!='.dbs.out':
print("Input file MUST be *.dbs.out")
return False
outFN=argv[1].replace('.out','.new')
try:
f=open(inFN,'rb')
f.read()
f.close()
except:
print("Input file error!")
return False
try:
f=open(txtFN,'r',1,'UTF-16')
f.read()
f.close()
except:
print("Text file error!")
return False
txt=open(txtFN,'r',1,'UTF-16')
head=txt.readline()
if head=='ASCII\n':
isUTF=False
elif head=='Unicode\n':
isUTF=True
else:
print("Text error!")
txt.close()
return False
file=open(inFN,'rb')
header=Header(file)
file.seek(header.lineIndexOffset)
lineIndex=struct.unpack('%di'%header.lineCount,file.read(header.lineCount*4))
dataIndex=[]
dataType=[]
for n in range(0,header.dataCount):
dataIndex.append(struct.unpack('I',file.read(4))[0])
dataType.append(struct.unpack('I',file.read(4))[0])
lineData=[]
for m in range(0,header.lineCount):
lineData.append([])
for n in range(0,header.dataCount):
tempData=struct.unpack('I',file.read(4))[0]
if dataType[n]==0x53:
tempTell=file.tell()
file.seek(header.textOffset+tempData)
tempString=b''
if isUTF:
tempChar=file.read(2)
while tempChar!=b'\x00\x00' and tempChar!=b'':
tempString+=tempChar
tempChar=file.read(2)
lineData[m].append(tempString.decode("UTF-16"))
else:
tempChar=file.read(1)
while tempChar!=b'\x00' and tempChar!=b'':
tempString+=tempChar
tempChar=file.read(1)
lineData[m].append(tempString.decode("Shift-JIS"))
file.seek(tempTell)
else:
lineData[m].append(tempData)
file.seek(header.fileSize)
#dummy=file.read()
file.close()
index=0
correctIndex=0
count=0
correctCount=0
for line in txt.readlines():
if line=='':
break
if line[0]=='[':
index=int(line[1:line.find(']',1)])
try:
correctIndex=lineIndex.index(index)
except:
continue
elif line[0]=='<':
count=int(line[1:line.find('>',1)])
try:
correctCount=dataIndex.index(count)
except:
continue
lineData[correctIndex][correctCount]=int(line[line.find('>',1)+1:])
elif line[0]=='●':
count=int(line[1:line.find('●',1)])
try:
correctCount=dataIndex.index(count)
except:
continue
lineData[correctIndex][correctCount]=line[line.find('●',1)+1:].replace('\n','')
txt.close()
dbs=BytesIO()
dbs.write(bytes(4))
dbs.write(header.headerData)
dbs.write(struct.pack('%di'%header.lineCount,*lineIndex))
for n in range(0,header.dataCount):
dbs.write(struct.pack('2I',dataIndex[n],dataType[n]))
textData=b''
textOffset=0
for m in range(0,header.lineCount):
for n in range(0,header.dataCount):
if dataType[n]==0x53:
if isUTF:
tempString=lineData[m][n].encode("UTF-16")[2:]+b'\x00\x00'
else:
tempString=Transcode(lineData[m][n])+b'\x00'
dbs.write(struct.pack('I',textOffset))
textData+=tempString
textOffset+=len(tempString)
else:
dbs.write(struct.pack('I',lineData[m][n]))
'''
if dbs.tell()!=header.textOffset:
tempOffset=dbs.tell()
dbs.seek(24)
dbs.write(struct.pack('I',tempOffset))
dbs.seek(0,2)
'''
dbs.write(textData)
dbsSize=dbs.tell()
dbs.seek(0)
dbs.write(struct.pack('I',dbsSize))
dbs.seek(0,2)
#dbs.write(dummy)
dbs.write(bytes(32-dbsSize%32))
dbsSize=dbs.tell()
'''
dbsFile=open(outFN.replace('.new','.ori'),'wb')
dbsFile.write(dbs.getvalue())
dbsFile.close()
'''
dataB=Decrypt3(dbs.getvalue())
dbs.close()
if comp:
dataA=Compress(dataB,comp)
else:
dataA=FakeCompress(dataB)
'''
dbsFile=open(outFN.replace('.new','.cmp'),'wb')
dbsFile.write(dataA)
dbsFile.close()
'''
data=Decrypt5(dataA)
output=open(outFN,'wb')
if isUTF:
output.write(b'\x01\x00\x00\x00')
else:
output.write(b'\x00\x00\x00\x00')
output.write(data)
output.close()
return True
if __name__=="__main__":
main(sys.argv)