# -*- coding: utf-8 -*-
#@File name : binhex.py
#@Auther : GhostFromHeaven[csdn]
#@date : 2012-07-19
import struct
from filehelper import *
def binhex(inp, out, extfun=lambda x: x, blocksize=256):
"""
Convert a binary file 'inp' to binhex file output.
The inp may be a filename or a file-like object supporting read() and close() methods.
The output parameter can either be a filename or a file-like object supporting a write() and close() method.
"""
def _binhex(fin, fout):
filesize = 0
while True:
chunk = fin.read(blocksize)
if chunk:
redlen = len(chunk)
for b in chunk:
fout.write('%02X ' % extfun(ord(b)))
fout.write('\n')
filesize += redlen
else:
break
return filesize
return fileinoutpattern(inp, out, _binhex, inmode="rb", outmode="w")
def hexbin(inp, out, extfun=slambda x: x):
"""
Decode a binhex file inp to binary file outpu.
The inp may be a filename or a file-like object supporting read() and close() methods.
The output parameter can either be a filename or a file-like object supporting a write() and close() method.
"""
def _hexbin(fin, fout):
for line in fin:
for i in range(len(line)/3):
x = int(line[3*i:3*(i+1)], 16)
fout.write(struct.pack('B', extfun(x)))
fileinoutpattern(inp, out, _hexbin, inmode="r", outmode="wb")
#self testing
def test():
"""
Test case.
"""
# binhex test
zipfilename = r"D:\2.zip"
binhex(zipfilename, r"D:\2.1.txt")
with open(zipfilename, "rb") as fin:
binhex(fin, r"D:\2.2.txt")
with open(r"D:\2.3.txt", "w") as fout:
binhex(zipfilename, fout)
with open(zipfilename, "rb") as fin, open(r"D:\2.4.txt", "w") as fout:
binhex(fin, fout)
# hexbin test
txtfile = r"D:\2.1.txt"
hexbin(txtfile, r"D:\2.1.zip")
with open(txtfile, "r") as fin:
hexbin(fin, r"D:\2.2.zip")
with open(r"D:\2.3.zip", "wb") as fout:
hexbin(txtfile, fout)
with open(txtfile, "r") as fin, open(r"D:\2.4.zip", "wb") as fout:
hexbin(fin, fout)
def XOR(x):
def _XOR(y):
return x ^ y
return _XOR
xor0x13 = XOR(0x13)
binhex(zipfilename, r"D:\2.encode.txt", extfun=xor0x13)
hexbin(r"D:\2.encode.txt", r"D:\2.5.zip", extfun=xor0x13)
print "OK."
if __name__ == "__main__":
test()
binhex[GhostFromHeavn]
5星 · 超过95%的资源 需积分: 10 64 浏览量
2012-07-24
23:26:58
上传
评论
收藏 5KB ZIP 举报
GhostFromHeaven
- 粉丝: 39
- 资源: 27
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈