Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import os
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- infile.close()
- return totalfiledata
- def is_jis_char(s):
- if len(s) != 2: #If it's not a string of length 2, then...
- return False
- try: #Python error handling
- if len(s.decode('cp932')) != 1: #For example, 2 ascii characters will be length 2
- return False #Two bytes that decode to a SHIFT-JIS character will be length 1
- except UnicodeDecodeError: #The string couldn't be decoded
- return False #Not a SHIFT-JIS character
- return True #It is a SHIFT-JIS character
- def decode_string(s):
- mode = True
- s1 = u'。、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゙゚'
- s2 = u'。、Xをぁぃぅぇぉゃゅょっーあいうえおかきくけこさしすせそたちつてと\
- なにぬねのはひふへほまみむめもやゆよらりるれろわんXX'
- s3 = u'。、Xヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテト\
- ナニヌネノハヒフヘホマミムメモヤユヨラリルレロワンXX'
- t = zip(s1,s2,s3) #Put 'em, togther, whatdayya got? :)
- sd = ''
- pos = 0
- while pos < len(s):
- if ord(s[pos]) == 0xA2: #Set hiragana flag
- mode = False
- pos += 1
- elif ord(s[pos]) == 0xA3: #Set katakana flag, the default
- mode = True
- pos += 1
- elif ord(s[pos]) == 1: #Linebreak, not sure
- sd += '\n'
- pos += 1
- elif ord(s[pos]) == 2: #Delay, not sure
- sd += 'delay'
- pos += 1
- elif ord(s[pos]) == 3:
- sd += 'Unknown Opcode 03'
- pos += 1
- elif ord(s[pos]) == 4:
- sd += 'Unknown Opcode 04'
- pos += 1
- elif ord(s[pos]) == 5:
- sd += 'Unknown Opcode 05'
- pos += 1
- elif ord(s[pos]) == 6:
- sd += 'Unknown Opcode 06'
- pos += 1
- elif ord(s[pos]) == 0x14:
- sd += 'Unknown Opcode 14'
- pos += 1
- elif ord(s[pos]) >= 0x20 and ord(s[pos]) < 0x7F: #ASCII
- sd += s[pos]
- pos += 1
- elif pos < len(s) - 1 and is_jis_char(s[pos:pos+2]): #SHIFT-JIS char, double-byte
- sd += s[pos:pos+2]
- pos += 2
- elif unicode(s[pos],'cp932') in s1: #SHIFT-JIS char, single-byte
- for x, y, z in t:
- if unicode(s[pos],'cp932') == x:
- if mode == True:
- sd += y.encode('cp932')
- else:
- sd += z.encode('cp932')
- pos += 1
- break
- else:
- print 'Unknown character.'
- print pos
- return sd
- return sd
- filedata = get_data('crap2.dat')
- pos = 0x4e0
- opcodes = ('\x00\x0f','\x04\x0f')
- while True:
- r = []
- for op in opcodes:
- r.append(filedata.find(op,pos))
- pos = min(r) + 2
- print hex(pos)
- if pos == 1:
- break
- endpos = filedata.find('\x00', pos)
- s = filedata[pos:endpos]
- sd = decode_string(s)
- print sd.decode('cp932')
- pos = endpos
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement