-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbup_parse.py
450 lines (336 loc) · 11.4 KB
/
bup_parse.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
# Python script to dump\create Sega Saturn .BUP save game files
import time
import datetime
import hashlib
BUP_HEADER_SIZE = 64
BUP_HEADER_MAGIC = "Vmem"
BUP_EXTENSION = ".BUP" # required file extension
MAX_SAVE_NAME_LENGTH = 12
MAX_SAVE_COMMENT_LENGTH = 11
import sys
import getopt
# prints out script usage and quits
def usage():
print("\nUsage:")
print("Validate .BUP file.")
print('python3 bup_parse.py --input_bup <save_game.bup>\n')
print("Extract raw save from .BUP file.")
print('python3 bup_parse.py --input_bup <save_game.bup> --extract_raw_save\n')
print("Create .BUP from raw save.")
print('python3 bup_parse --save_name <name> --save_comment <comment> --save_date <date> --save_language <language> --input_save <raw_save.bin>\n')
sys.exit(1)
# returns the language based on the langId
def getBUPLanguage(langId):
if langId == 0:
return "Japanese"
elif langId == 1:
return "English"
elif langId == 2:
return "Francais"
elif langId == 3:
return "Deutsch"
elif langId == 4:
return "Espanol"
elif langId == 5:
return "Italiano"
# language not found
return None
# returns the langId based on the language
def getBUPLangId(language):
language = language.lower()
if language == "japanese":
return 0
if language == "english":
return 1
if language == "francais":
return 2
if language == "deutsch":
return 3
if language == "espanol":
return 4
if language == "italiano":
return 5
# language not found
return None
# converts a 4-byte BUP date to a Python datetime
# ported from bup_getdate()
def convertBUPDatetoDatetime(bupDate):
# minutes
mins = (bupDate % 60) & 0xFF
hours = int(bupDate % (60*24) / 60) & 0xFF
# Compute days count
div = bupDate / (60*24);
div = int(div)
year_base = int(div / ((365*4) + 1))
year_base = int(year_base * 4)
days_remain = int(div % ((365*4) + 1))
days_count = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
month = 0;
for i in range(0, 48):
days_per_month = days_count[i % 12];
if i == 1:
days_per_month += 1
if days_remain < days_per_month:
break
days_remain -= days_per_month
month += 1
if i % 12 == 11:
month = 0
year_base += 1
year_base += 1980
month += 1
days_remain += 1
return datetime.datetime(year_base, month, days_remain, hours, mins)
# converts a Python datetime to a 4-byte BUP date
# ported from bup_setdate()
def convertDatetimetoBUPDate(dt):
# Table of elapsed days per month
monthtbl = [
31,
31+28,
31+28+31,
31+28+31+30,
31+28+31+30+31,
31+28+31+30+31+30,
31+28+31+30+31+30+31,
31+28+31+30+31+30+31+31,
31+28+31+30+31+30+31+31+30,
31+28+31+30+31+30+31+31+30+31,
31+28+31+30+31+30+31+31+30+31+30
]
date = 0
data = 0
remainder = 0
# invalid all NULLs case
if dt.year == 0 and dt.month == 0 and dt.day and dt.hour == 0 and dt.minutes== 0:
return 0x008246A0
# Add year to result
data = int((dt.year - 1980)) & 0xFF
date = int(data / 4) * 0x5B5
date = int(date)
data = int(data)
remainder = data % 4
if remainder:
date += (remainder * 0x16D) + 1 # 0x16D = 365
date = int(date)
# Leap year fix.
year = dt.year & 0xFFFF
leap_year = 0
if (year % 4) != 0:
leap_year = 0;
elif (year % 100) != 0:
leap_year = 1
elif (year % 400) != 0:
leap_year = 0
else:
leap_year = 1
date -= 1
if leap_year != 0 and dt.month == 2:
date -= 1
if (year > 2000) and ((year % 100) == 0) and (dt.month == 2):
date -= 1
# Add month to result
data = dt.month
if data != 1 and data < 13:
date += monthtbl[data - 2];
if date > 2 and remainder == 0:
date += 1
# Add day to result
date += dt.day
date *= 0x5A0
# Add hour to result
date += (dt.hour * 0x3C)
# Add minute to result
date += dt.minute
return int(date)
# basic sanity checks of the .BUP file
# exits if error is detected
# file has already been checked for .BUP extension
def validateBUPData(bupData):
if len(bupData) < BUP_HEADER_SIZE:
print(len(bupData))
print("Invalid BUP file: too small (" + str(len(bupData)) + ")")
sys.exit(-1)
magic = bupData[0:4].decode("ascii")
if magic != BUP_HEADER_MAGIC:
print("Invalid BUP file: incorrect magic bytes (" + magic + ")")
sys.exit(-1)
# read fields from the dir struct
saveName = bupData[16:28].decode("ascii").rstrip('\x00')
print("\tSave name: " + saveName)
comment = bupData[28:39].decode("sjis")
print("\tComment: " + comment)
langId = int(bupData[39])
language = getBUPLanguage(langId)
if langId == None:
print("Invalid BUP file: incorrect magic bytes")
sys.exit(-1)
print("\tLanguage: " + language)
date = int.from_bytes(bupData[40:44], 'big')
dataSize = int.from_bytes(bupData[44:48], 'big')
blockSize = int.from_bytes(bupData[48:50], 'big')
dt = convertBUPDatetoDatetime(date)
print("\tDate: " + str(dt) + " (" + hex(date) + ")")
print("\tData Size: " + str(dataSize))
print("\tBlock Size: " + str(blockSize))
if dataSize != len(bupData) - BUP_HEADER_SIZE:
print("Invalid BUP file: incorrect save size")
print("Expected " + str(len(bupData) - BUP_HEADER_SIZE));
print("Got " + str(dataSize))
sys.exit(-1)
# print the hash for the user
computedHashResult = hashlib.md5(bupData[BUP_HEADER_SIZE:])
computedHash = computedHashResult.hexdigest()
print("\tMD5: " + computedHash)
# .BUP is valid
return saveName
# on success returns a .BUP file
def createBUPHeader(saveName, saveComment, saveDate, saveLanguage, saveData):
zero_int = 0
# create the vmem_bup_header
bupHeader = b''
# magic
bupHeader += BUP_HEADER_MAGIC.encode('utf-8')
# save id, unused
bupHeader += zero_int.to_bytes(4, "big")
# stats, unused
bupHeader += zero_int.to_bytes(4, "big")
# usused1, unused
bupHeader += zero_int.to_bytes(4, "big")
#
# dir struct
#
# saveName is 11 characters + NULL terminator
if len(saveName) > MAX_SAVE_NAME_LENGTH - 1:
print("Save name must be less than " + str(MAX_SAVE_NAME_LENGTH) + " characters!!")
sys.exit(-1)
while len(saveName) < MAX_SAVE_NAME_LENGTH:
saveName += '\x00'
bupHeader += saveName.encode('utf-8')
# saveComment is 10 characters + NULL terminator
if len(saveComment) > MAX_SAVE_COMMENT_LENGTH - 1:
print("Comment name must be less than " + str(MAX_SAVE_COMMENT_LENGTH) + " characters!!")
sys.exit(-1)
while len(saveComment) < MAX_SAVE_COMMENT_LENGTH:
saveComment += '\x00'
bupHeader += saveComment.encode('utf-8')
# langId is one byte
langId = getBUPLangId(saveLanguage)
if langId == None:
print("Save language must be one of: Japanese, English, Francais, Deutsch, Espanol, or Italiano")
sys.exit(-1)
bupHeader += langId.to_bytes(1, "big")
# four bytes of date
date = convertDatetimetoBUPDate(saveDate)
bupHeader += date.to_bytes(4, "big")
# four bytes of datasize
saveSize = len(saveData)
bupHeader += saveSize.to_bytes(4, "big")
# two bytes of blocksize, we can leave this zero
bupHeader += langId.to_bytes(2, "big")
# two bytes of padding
bupHeader += zero_int.to_bytes(2, "big")
#
# end of dir struct
#
# 4 byte date structure is repeated
bupHeader += date.to_bytes(4, "big")
# 8 bytes more of unsused2 padding
bupHeader += date.to_bytes(8, "big")
if len(bupHeader) != BUP_HEADER_SIZE:
print("Invalid bupHeader length!!")
sys.exit(-1)
# concatenate the bupHeader with the saveData
bupHeader = bupHeader + saveData
return bupHeader
def main(argv):
inSaveFilename = ""
outSaveFilename = ""
inSaveBuf = b''
outSaveBuf = b''
extractSave = False
inBUPFilename = ""
inBUPBuf = b''
# default .BUP header values
saveName = ""
saveComment = "SGC"
saveDate = datetime.datetime(1994, 11, 24) # launch date of the Saturn
saveLanguage = "English"
try:
opts, args = getopt.getopt(argv,"h",["help", "input_bup=", "extract_raw_save", "save_name=", "save_comment=", "save_date=", "save_language=", "input_save="])
except getopt.GetoptError:
usage()
for opt, arg in opts:
if opt in ('-h', "--help"):
usage()
sys.exit()
elif opt in ("-i", "--input_bup"):
inBUPFilename = arg
elif opt in ("--extract_raw_save"):
extractSave = True
elif opt in ("--save_name"):
saveName = arg
elif opt in ("--save_comment"):
saveComment = arg
elif opt in ("--save_date"):
dateStr = arg
try:
t = time.strptime(dateStr, '%Y-%m-%d')
saveDate = datetime.datetime(*t[:6])
except ValueError:
print(dateStr + ' is not a proper date string. Must be YYYY-MM-D')
sys.exit(-1)
elif opt in ("--save_language"):
saveLanguage = arg
elif opt in ("--input_save"):
inSaveFilename = arg
if len(inSaveFilename) == 0 and len(inBUPFilename) == 0:
print("Input .BUP or input .BUP file must be supplied.")
usage()
if len(inSaveFilename) != 0 and len(inBUPFilename) != 0:
print("Only one input .BUP or input save file must be supplied.")
usage()
if len(inSaveFilename) != 0 and len(saveName) == 0:
print("save_name is required!!")
usage()
# reading from a .BUP file
if len(inBUPFilename):
if inBUPFilename.endswith(BUP_EXTENSION) != True:
print("BUP file must end with .BUP extension.")
usage()
# open and sanity check the input save file
inFile = open(inBUPFilename, "rb")
inBUPBuf = inFile.read()
inFile.close()
print("Validating " + inBUPFilename)
saveName = validateBUPData(inBUPBuf)
print("Valid .BUP file")
if extractSave:
# everything after the BUP header is the raw save
outBuf = inBUPBuf[BUP_HEADER_SIZE:]
outFile = open(saveName, "wb")
outFile.write(outBuf)
outFile.close()
print("Successfully extracted " + saveName + " (" + str(len(outBuf)) + ")")
return
return
# creating a .BUP file
if len(inSaveFilename):
# read the save save file
inFile = open(inSaveFilename, "rb")
inSaveBuf = inFile.read()
inFile.close()
outBUPBuf = createBUPHeader(saveName, saveComment, saveDate, saveLanguage, inSaveBuf)
# .BUP is the required extension
outBUPFilename = saveName + BUP_EXTENSION
outFile = open(outBUPFilename, "wb")
outFile.write(outBUPBuf)
outFile.close()
print("Successfully created " + outBUPFilename + " from " + saveName + " raw save")
return
if __name__ == "__main__":
# Python3 only
if sys.version_info.major < 3:
print("Python 3 required")
sys.exit(-1)
main(sys.argv[1:])