Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import struct
- import os
- import sys
- def get_data(filename):
- with open(filename, 'rb') as f:
- return f.read()
- def is_jis_char(s):
- try:
- s.decode('cp932')
- except UnicodeDecodeError:
- return False
- if len(s.decode('cp932')) == 1:
- return True
- return False
- def script_unpack(filename):
- filedata = get_data(filename)
- num_of_records = struct.unpack('<H',filedata[:2])[0] / 2
- pos = 0
- ptrs = []
- for i in range(num_of_records):
- ptr = struct.unpack('<H',filedata[pos:pos+2])[0]
- tmp_list = [x[1] for x in ptrs]
- if ptr in tmp_list:
- ## print 'hit'
- ptrs.append(('duplicate',tmp_list.index(ptr)))
- else:
- ptrs.append((i, ptr))
- pos += 2
- output = [filename + '\n']
- for i, (num, ptr) in enumerate(ptrs):
- if isinstance(num, basestring):
- output.append(str(i) + '\tDuplicate of {}\n'.format(str(ptr)))
- continue
- output.append(str(i) + '\t')
- pos = ptr
- while True:
- if is_jis_char(filedata[pos:pos+2]):
- output.append(filedata[pos:pos+2])
- ## print filedata[pos:pos+2].decode('cp932')
- pos += 2
- elif (ord(filedata[pos]) > 0x19 and ord(filedata[pos]) < 0x80) or \
- (ord(filedata[pos]) > 0xa0 and ord(filedata[pos]) < 0xe0):
- output.append(filedata[pos])
- pos += 1
- elif filedata[pos] == '\x0d':
- output.append('\tlinebreak\n\t')
- pos += 1
- elif filedata[pos:pos+2] == '\x08\x0d':
- output.append('\tPTT\n\t')
- pos += 2
- elif filedata[pos:pos+2] == '\x08\x03':
- output.append('\tpagebreak\n\t')
- pos += 2
- elif filedata[pos] == '\x08':
- output.append('\tpause\n\t')
- pos += 1
- elif filedata[pos] == '\x03':
- output.append('\tpagebreak2\n\t')
- pos += 1
- elif filedata[pos:pos+2] == '\x00\x00':
- output.append('\tterminalcode\n\t')
- break
- elif filedata[pos] == '\x00':
- output.append('\tnull\n\t')
- pos += 1
- else:
- print i, hex(pos)
- quit()
- output[-1] = output[-1][:-1]
- with open(os.path.splitext(filename)[0] + '.tsv', 'wb') as f:
- f.write(''.join(output))
- if __name__ == '__main__':
- if len(sys.argv) < 2:
- print ('Usage:\n'
- 'python script.py script_file_name_minus_extension')
- quit()
- ## For use with IDLE
- ## sys.argv.append('TALK01')
- script_unpack(sys.argv[1] + '.bzh')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement