Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bitstring
- import sys
- import os
- def printusage():
- print('Usage: file2codegrp.py infile [outfile]')
- exit()
- def str_base(number, base):
- (d,m) = divmod(number,len(base))
- if d > 0:
- return str_base(d,base)+base[m]
- return base[m]
- #bitstring.BitArray(uie=1322)
- bs = bitstring.BitArray()
- lastchar = 0
- s = []
- def putchar(c):
- global s
- s += c
- if len(s) >= 5:
- f2.write(' '.join(s[:5])+'. ')
- del s[:5]
- def putbytes(b):
- global lastchar
- bs.append(b)
- while len(bs) >= 19:
- code = bs[:19].uint
- del bs[:19]
- code = str_base(code, [[0], [1], [2], [3], [4], [5], [6], [7], [8]])
- if len(code) < 6:
- code = [0] * (6 - len(code)) + code
- for i in range(len(code)):
- code[i] = (lastchar + code[i] + 1) % 10
- lastchar = code[i]
- putchar([chr(lastchar+48)])
- if len(sys.argv) < 2:
- printusage()
- infile_name = sys.argv[1]
- if len(sys.argv) > 2:
- outfile_name = sys.argv[2]
- else:
- outfile_name = infile_name + '.txt'
- try:
- f1 = open(infile_name, 'rb')
- except:
- print('Error opening infile')
- print("Unexpected error:", sys.exc_info()[0])
- printusage()
- try:
- f2 = open(outfile_name, 'w')
- except:
- print('Error opening outfile')
- print("Unexpected error:", sys.exc_info()[0])
- printusage()
- fn = os.path.basename(infile_name).encode('utf-8')
- bs.append(bitstring.Bits(uie=len(fn)))
- bs.append(fn)
- bs.append(bitstring.Bits(uie=os.stat(infile_name).st_size))
- buf = f1.read(512)
- while len(buf) > 0:
- putbytes(buf)
- buf = f1.read(512)
- f1.close()
- putbytes(b'\0' * (((19 - len(bs)) % 19 + 7) // 8))
- putchar('0' * ((5 - len(s)) % 5))
- f2.close()
Advertisement
Add Comment
Please, Sign In to add comment