-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbsDecrypt.py
187 lines (170 loc) · 5.43 KB
/
dbsDecrypt.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
# -*- coding: utf-8 -*-
# For Windows OS Only....
import sys
import os
import struct
from io import BytesIO
from Decryption import Decrypt5,Decrypt3,Decompress
'''
dbsHeader():
fileSize I
lineCount I
dataCount I
lineIndexOffset I
dataFormatOffset I
lineDataIndexOffset I
textOffset I
lineIndex i
dataFormat():
dataIndex I
dataType I #0x53=string, 0x56=int
lineDataIndex[dataIndex] I
if dataType==0x56:
lineData=lineDataIndex[dataIndex]
if dataType==0x53:
lineDataOffset=lineDataIndex[dataIndex]
'''
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 main(argv):
if argv.count('-a')>0:
noDump=True
argv.remove('-a')
else:
noDump=False
if argv.count('-x')>0:
xlsxMode=True
import openpyxl
argv.remove('-x')
else:
xlsxMode=False
if len(argv)<2 or argv[1]=='':
print ("Usage: "+argv[0][argv[0].rfind("\\")+1:]+" <dbs file> [-a/-x]")
return False
try:
f=open(argv[1],'rb')
f.read()
f.close()
except:
return False
dbs=open(argv[1],'rb')
head=dbs.read(4)
if head==b'\x00\x00\x00\x00':
isUTF=False
else:
isUTF=True
data=dbs.read()
dbs.close()
dataA=Decrypt5(data)
'''
output=open(argv[1]+'.dec','wb')
output.write(dataA)
output.close()
'''
compSize,decompSize=struct.unpack('2I',dataA[:8])
dataB=Decompress(dataA[8:],decompSize)
dataC=Decrypt3(dataB)
if xlsxMode:
file=BytesIO(dataC)
else:
output=open(argv[1]+'.out','wb')
output.write(dataC)
output.close()
file=open(argv[1]+'.out','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()
if xlsxMode:
xls=argv[1]+".xlsx"
workBook=openpyxl.Workbook()
workSheet=workBook.active
workSheet.title="Translation"
workSheet.column_dimensions['A'].width=10
tempIndex=["#DATANO"]
tempType=["#DATATYPE"]
for i,j in zip(dataIndex,dataType):
tempIndex.append(i)
if j==0x53:
tempType.append("S")
else:
tempType.append("V")
workSheet.append(tempIndex)
workSheet.append(tempType)
for l in range(0,header.lineCount):
tempLine=[lineIndex[l],*lineData[l]]
workSheet.append(tempLine)
workSheetCopy=workBook.copy_worksheet(workSheet)
workSheetCopy.title="Text"
workBook.save(xls)
else:
txt=open(argv[1]+'.txt','w',1,"UTF-16")
if isUTF:
txt.write('Unicode\n')
else:
txt.write('ASCII\n')
for m in range(0,header.lineCount):
txt.write('[%.4d]\n'%lineIndex[m])
for n in range(0,header.dataCount):
tempIndex=dataIndex[n]
tempData=lineData[m][n]
if dataType[n]==0x53 and (tempData!='' or noDump):
txt.write('○%.2d○'%tempIndex+tempData+'\n●%.2d●'%tempIndex+lineData[m][n]+'\n\n')
#int data:
elif dataType[n]==0x56 and noDump:
txt.write('{%.2d}'%tempIndex+str(tempData)+'\n<%.2d>'%tempIndex+str(tempData)+'\n\n')
txt.write('\n')
txt.close()
return True
if __name__=="__main__":
main(sys.argv)