練習問題

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from string import ascii_lowercase, ascii_uppercase

table = ascii_uppercase + ascii_lowercase + ''.join(map(str, range(10))) + '+/'

def binstr(n, maxbits=None):
    absn = abs(n)
    s = [ chr(((n>>b)&1)+48) for b in range(maxbits or len(hex(n))*4) if maxbits or absn>>b or not b or (n<0 and absn>>(b-1)) ]
    return ''.join(reversed(s))

def encode(filename):
    with open(filename, 'rb') as F:
        bit = ''.join(binstr(ord(x), 8) for x in F.read())
    sz = len(bit)
    fill = 6 - sz % 6
    bit += '0' * fill
    sz += fill
    res = ''.join(table[int(bit[i:i+6], 2)] for i in range(0, sz, 6))
    return res + '=' * (4 - len(res) % 4)

def decode(filename):
    with open(filename, 'rb') as F:
        bit = ''.join(format(table.find(x), 'b').zfill(6) for x in F.read() if x in table)
    sz = len(bit)
    extra = 0
    while (sz-extra) % 8 > 0 and bit[-extra-1] == '0':
        extra += 1
    bit = bit[:-extra-1]
    sz -= extra
    res = ''.join(chr(int(bit[i:i+8], 2)) for i in range(0, sz, 8))
    return res

N = 76
option = sys.argv[1]
res = ''
if option == '-e':
    encoded = encode(sys.argv[2])
    res = '\n'.join(encoded[i:i+N] for i in range(0, len(encoded), N))
    print(res)
elif option == '-d':
    res = decode(sys.argv[2])
    sys.stdout.write(res)
else:
    sys.stderr.write('Huh?\n')