Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # vim: sw=4 ts=4 expandtab ai
- import sys
- BLOCK_SIZE = 550
- HEADER_SIZE = 32
- CODEC = 'hex_codec'
- def gen_record(inf, labels=('\x32', '\x33', '\x34'),
- hsize=HEADER_SIZE, bsize=BLOCK_SIZE):
- yield (0, inf.read(hsize))
- # skip to the beginning of the first record
- while inf.read(1) not in labels:
- pass
- block = inf.read(bsize)
- rno = 1
- while True:
- pos = 0
- while True:
- pos = block.find('\xff', pos)
- if pos == -1:
- print "Error: end of record %d (%s) not found" % \
- (rno, block.encode(CODEC))
- sys.exit(1)
- if len(block) == pos+1:
- yield (rno, block[:pos])
- return
- if block[pos+1] in labels:
- pos = pos + 2
- yield (rno, block[:pos])
- block = block[pos:] + inf.read(bsize-len(block[pos:]))
- rno += 1
- break
- def main(argv):
- fname = argv[1]
- inf = open(fname,"rb")
- outf = open(fname + '.out', "w")
- for rno, record in gen_record(inf):
- record = record.encode(CODEC)
- if rno:
- outf.write("<RECORD>%s<RECORD>\n" % record)
- else:
- outf.write("<ADM RECORD>%s<RECORD>\n" % record)
- outf.close()
- inf.close()
- return 0
- if __name__ == "__main__":
- sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement