You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

binhex.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. """Macintosh binhex compression/decompression.
  2. easy interface:
  3. binhex(inputfilename, outputfilename)
  4. hexbin(inputfilename, outputfilename)
  5. """
  6. #
  7. # Jack Jansen, CWI, August 1995.
  8. #
  9. # The module is supposed to be as compatible as possible. Especially the
  10. # easy interface should work "as expected" on any platform.
  11. # XXXX Note: currently, textfiles appear in mac-form on all platforms.
  12. # We seem to lack a simple character-translate in python.
  13. # (we should probably use ISO-Latin-1 on all but the mac platform).
  14. # XXXX The simple routines are too simple: they expect to hold the complete
  15. # files in-core. Should be fixed.
  16. # XXXX It would be nice to handle AppleDouble format on unix
  17. # (for servers serving macs).
  18. # XXXX I don't understand what happens when you get 0x90 times the same byte on
  19. # input. The resulting code (xx 90 90) would appear to be interpreted as an
  20. # escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
  21. #
  22. import sys
  23. import os
  24. import struct
  25. import binascii
  26. __all__ = ["binhex","hexbin","Error"]
  27. class Error(Exception):
  28. pass
  29. # States (what have we written)
  30. [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
  31. # Various constants
  32. REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder
  33. LINELEN=64
  34. RUNCHAR=chr(0x90) # run-length introducer
  35. #
  36. # This code is no longer byte-order dependent
  37. #
  38. # Workarounds for non-mac machines.
  39. if os.name == 'mac':
  40. import macfs
  41. import MacOS
  42. try:
  43. openrf = MacOS.openrf
  44. except AttributeError:
  45. # Backward compatibility
  46. openrf = open
  47. def FInfo():
  48. return macfs.FInfo()
  49. def getfileinfo(name):
  50. finfo = macfs.FSSpec(name).GetFInfo()
  51. dir, file = os.path.split(name)
  52. # XXXX Get resource/data sizes
  53. fp = open(name, 'rb')
  54. fp.seek(0, 2)
  55. dlen = fp.tell()
  56. fp = openrf(name, '*rb')
  57. fp.seek(0, 2)
  58. rlen = fp.tell()
  59. return file, finfo, dlen, rlen
  60. def openrsrc(name, *mode):
  61. if not mode:
  62. mode = '*rb'
  63. else:
  64. mode = '*' + mode[0]
  65. return openrf(name, mode)
  66. else:
  67. #
  68. # Glue code for non-macintosh usage
  69. #
  70. class FInfo:
  71. def __init__(self):
  72. self.Type = '????'
  73. self.Creator = '????'
  74. self.Flags = 0
  75. def getfileinfo(name):
  76. finfo = FInfo()
  77. # Quick check for textfile
  78. fp = open(name)
  79. data = open(name).read(256)
  80. for c in data:
  81. if not c.isspace() and (c<' ' or ord(c) > 0177):
  82. break
  83. else:
  84. finfo.Type = 'TEXT'
  85. fp.seek(0, 2)
  86. dsize = fp.tell()
  87. fp.close()
  88. dir, file = os.path.split(name)
  89. file = file.replace(':', '-', 1)
  90. return file, finfo, dsize, 0
  91. class openrsrc:
  92. def __init__(self, *args):
  93. pass
  94. def read(self, *args):
  95. return ''
  96. def write(self, *args):
  97. pass
  98. def close(self):
  99. pass
  100. class _Hqxcoderengine:
  101. """Write data to the coder in 3-byte chunks"""
  102. def __init__(self, ofp):
  103. self.ofp = ofp
  104. self.data = ''
  105. self.hqxdata = ''
  106. self.linelen = LINELEN-1
  107. def write(self, data):
  108. self.data = self.data + data
  109. datalen = len(self.data)
  110. todo = (datalen/3)*3
  111. data = self.data[:todo]
  112. self.data = self.data[todo:]
  113. if not data:
  114. return
  115. self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
  116. self._flush(0)
  117. def _flush(self, force):
  118. first = 0
  119. while first <= len(self.hqxdata)-self.linelen:
  120. last = first + self.linelen
  121. self.ofp.write(self.hqxdata[first:last]+'\n')
  122. self.linelen = LINELEN
  123. first = last
  124. self.hqxdata = self.hqxdata[first:]
  125. if force:
  126. self.ofp.write(self.hqxdata + ':\n')
  127. def close(self):
  128. if self.data:
  129. self.hqxdata = \
  130. self.hqxdata + binascii.b2a_hqx(self.data)
  131. self._flush(1)
  132. self.ofp.close()
  133. del self.ofp
  134. class _Rlecoderengine:
  135. """Write data to the RLE-coder in suitably large chunks"""
  136. def __init__(self, ofp):
  137. self.ofp = ofp
  138. self.data = ''
  139. def write(self, data):
  140. self.data = self.data + data
  141. if len(self.data) < REASONABLY_LARGE:
  142. return
  143. rledata = binascii.rlecode_hqx(self.data)
  144. self.ofp.write(rledata)
  145. self.data = ''
  146. def close(self):
  147. if self.data:
  148. rledata = binascii.rlecode_hqx(self.data)
  149. self.ofp.write(rledata)
  150. self.ofp.close()
  151. del self.ofp
  152. class BinHex:
  153. def __init__(self, (name, finfo, dlen, rlen), ofp):
  154. if type(ofp) == type(''):
  155. ofname = ofp
  156. ofp = open(ofname, 'w')
  157. if os.name == 'mac':
  158. fss = macfs.FSSpec(ofname)
  159. fss.SetCreatorType('BnHq', 'TEXT')
  160. ofp.write('(This file must be converted with BinHex 4.0)\n\n:')
  161. hqxer = _Hqxcoderengine(ofp)
  162. self.ofp = _Rlecoderengine(hqxer)
  163. self.crc = 0
  164. if finfo is None:
  165. finfo = FInfo()
  166. self.dlen = dlen
  167. self.rlen = rlen
  168. self._writeinfo(name, finfo)
  169. self.state = _DID_HEADER
  170. def _writeinfo(self, name, finfo):
  171. name = name
  172. nl = len(name)
  173. if nl > 63:
  174. raise Error, 'Filename too long'
  175. d = chr(nl) + name + '\0'
  176. d2 = finfo.Type + finfo.Creator
  177. # Force all structs to be packed with big-endian
  178. d3 = struct.pack('>h', finfo.Flags)
  179. d4 = struct.pack('>ii', self.dlen, self.rlen)
  180. info = d + d2 + d3 + d4
  181. self._write(info)
  182. self._writecrc()
  183. def _write(self, data):
  184. self.crc = binascii.crc_hqx(data, self.crc)
  185. self.ofp.write(data)
  186. def _writecrc(self):
  187. # XXXX Should this be here??
  188. # self.crc = binascii.crc_hqx('\0\0', self.crc)
  189. self.ofp.write(struct.pack('>h', self.crc))
  190. self.crc = 0
  191. def write(self, data):
  192. if self.state != _DID_HEADER:
  193. raise Error, 'Writing data at the wrong time'
  194. self.dlen = self.dlen - len(data)
  195. self._write(data)
  196. def close_data(self):
  197. if self.dlen != 0:
  198. raise Error, 'Incorrect data size, diff='+`self.rlen`
  199. self._writecrc()
  200. self.state = _DID_DATA
  201. def write_rsrc(self, data):
  202. if self.state < _DID_DATA:
  203. self.close_data()
  204. if self.state != _DID_DATA:
  205. raise Error, 'Writing resource data at the wrong time'
  206. self.rlen = self.rlen - len(data)
  207. self._write(data)
  208. def close(self):
  209. if self.state < _DID_DATA:
  210. self.close_data()
  211. if self.state != _DID_DATA:
  212. raise Error, 'Close at the wrong time'
  213. if self.rlen != 0:
  214. raise Error, \
  215. "Incorrect resource-datasize, diff="+`self.rlen`
  216. self._writecrc()
  217. self.ofp.close()
  218. self.state = None
  219. del self.ofp
  220. def binhex(inp, out):
  221. """(infilename, outfilename) - Create binhex-encoded copy of a file"""
  222. finfo = getfileinfo(inp)
  223. ofp = BinHex(finfo, out)
  224. ifp = open(inp, 'rb')
  225. # XXXX Do textfile translation on non-mac systems
  226. while 1:
  227. d = ifp.read(128000)
  228. if not d: break
  229. ofp.write(d)
  230. ofp.close_data()
  231. ifp.close()
  232. ifp = openrsrc(inp, 'rb')
  233. while 1:
  234. d = ifp.read(128000)
  235. if not d: break
  236. ofp.write_rsrc(d)
  237. ofp.close()
  238. ifp.close()
  239. class _Hqxdecoderengine:
  240. """Read data via the decoder in 4-byte chunks"""
  241. def __init__(self, ifp):
  242. self.ifp = ifp
  243. self.eof = 0
  244. def read(self, totalwtd):
  245. """Read at least wtd bytes (or until EOF)"""
  246. decdata = ''
  247. wtd = totalwtd
  248. #
  249. # The loop here is convoluted, since we don't really now how
  250. # much to decode: there may be newlines in the incoming data.
  251. while wtd > 0:
  252. if self.eof: return decdata
  253. wtd = ((wtd+2)/3)*4
  254. data = self.ifp.read(wtd)
  255. #
  256. # Next problem: there may not be a complete number of
  257. # bytes in what we pass to a2b. Solve by yet another
  258. # loop.
  259. #
  260. while 1:
  261. try:
  262. decdatacur, self.eof = \
  263. binascii.a2b_hqx(data)
  264. break
  265. except binascii.Incomplete:
  266. pass
  267. newdata = self.ifp.read(1)
  268. if not newdata:
  269. raise Error, \
  270. 'Premature EOF on binhex file'
  271. data = data + newdata
  272. decdata = decdata + decdatacur
  273. wtd = totalwtd - len(decdata)
  274. if not decdata and not self.eof:
  275. raise Error, 'Premature EOF on binhex file'
  276. return decdata
  277. def close(self):
  278. self.ifp.close()
  279. class _Rledecoderengine:
  280. """Read data via the RLE-coder"""
  281. def __init__(self, ifp):
  282. self.ifp = ifp
  283. self.pre_buffer = ''
  284. self.post_buffer = ''
  285. self.eof = 0
  286. def read(self, wtd):
  287. if wtd > len(self.post_buffer):
  288. self._fill(wtd-len(self.post_buffer))
  289. rv = self.post_buffer[:wtd]
  290. self.post_buffer = self.post_buffer[wtd:]
  291. return rv
  292. def _fill(self, wtd):
  293. self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+4)
  294. if self.ifp.eof:
  295. self.post_buffer = self.post_buffer + \
  296. binascii.rledecode_hqx(self.pre_buffer)
  297. self.pre_buffer = ''
  298. return
  299. #
  300. # Obfuscated code ahead. We have to take care that we don't
  301. # end up with an orphaned RUNCHAR later on. So, we keep a couple
  302. # of bytes in the buffer, depending on what the end of
  303. # the buffer looks like:
  304. # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
  305. # '?\220' - Keep 2 bytes: repeated something-else
  306. # '\220\0' - Escaped \220: Keep 2 bytes.
  307. # '?\220?' - Complete repeat sequence: decode all
  308. # otherwise: keep 1 byte.
  309. #
  310. mark = len(self.pre_buffer)
  311. if self.pre_buffer[-3:] == RUNCHAR + '\0' + RUNCHAR:
  312. mark = mark - 3
  313. elif self.pre_buffer[-1] == RUNCHAR:
  314. mark = mark - 2
  315. elif self.pre_buffer[-2:] == RUNCHAR + '\0':
  316. mark = mark - 2
  317. elif self.pre_buffer[-2] == RUNCHAR:
  318. pass # Decode all
  319. else:
  320. mark = mark - 1
  321. self.post_buffer = self.post_buffer + \
  322. binascii.rledecode_hqx(self.pre_buffer[:mark])
  323. self.pre_buffer = self.pre_buffer[mark:]
  324. def close(self):
  325. self.ifp.close()
  326. class HexBin:
  327. def __init__(self, ifp):
  328. if type(ifp) == type(''):
  329. ifp = open(ifp)
  330. #
  331. # Find initial colon.
  332. #
  333. while 1:
  334. ch = ifp.read(1)
  335. if not ch:
  336. raise Error, "No binhex data found"
  337. # Cater for \r\n terminated lines (which show up as \n\r, hence
  338. # all lines start with \r)
  339. if ch == '\r':
  340. continue
  341. if ch == ':':
  342. break
  343. if ch != '\n':
  344. dummy = ifp.readline()
  345. hqxifp = _Hqxdecoderengine(ifp)
  346. self.ifp = _Rledecoderengine(hqxifp)
  347. self.crc = 0
  348. self._readheader()
  349. def _read(self, len):
  350. data = self.ifp.read(len)
  351. self.crc = binascii.crc_hqx(data, self.crc)
  352. return data
  353. def _checkcrc(self):
  354. filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
  355. #self.crc = binascii.crc_hqx('\0\0', self.crc)
  356. # XXXX Is this needed??
  357. self.crc = self.crc & 0xffff
  358. if filecrc != self.crc:
  359. raise Error, 'CRC error, computed %x, read %x' \
  360. %(self.crc, filecrc)
  361. self.crc = 0
  362. def _readheader(self):
  363. len = self._read(1)
  364. fname = self._read(ord(len))
  365. rest = self._read(1+4+4+2+4+4)
  366. self._checkcrc()
  367. type = rest[1:5]
  368. creator = rest[5:9]
  369. flags = struct.unpack('>h', rest[9:11])[0]
  370. self.dlen = struct.unpack('>l', rest[11:15])[0]
  371. self.rlen = struct.unpack('>l', rest[15:19])[0]
  372. self.FName = fname
  373. self.FInfo = FInfo()
  374. self.FInfo.Creator = creator
  375. self.FInfo.Type = type
  376. self.FInfo.Flags = flags
  377. self.state = _DID_HEADER
  378. def read(self, *n):
  379. if self.state != _DID_HEADER:
  380. raise Error, 'Read data at wrong time'
  381. if n:
  382. n = n[0]
  383. n = min(n, self.dlen)
  384. else:
  385. n = self.dlen
  386. rv = ''
  387. while len(rv) < n:
  388. rv = rv + self._read(n-len(rv))
  389. self.dlen = self.dlen - n
  390. return rv
  391. def close_data(self):
  392. if self.state != _DID_HEADER:
  393. raise Error, 'close_data at wrong time'
  394. if self.dlen:
  395. dummy = self._read(self.dlen)
  396. self._checkcrc()
  397. self.state = _DID_DATA
  398. def read_rsrc(self, *n):
  399. if self.state == _DID_HEADER:
  400. self.close_data()
  401. if self.state != _DID_DATA:
  402. raise Error, 'Read resource data at wrong time'
  403. if n:
  404. n = n[0]
  405. n = min(n, self.rlen)
  406. else:
  407. n = self.rlen
  408. self.rlen = self.rlen - n
  409. return self._read(n)
  410. def close(self):
  411. if self.rlen:
  412. dummy = self.read_rsrc(self.rlen)
  413. self._checkcrc()
  414. self.state = _DID_RSRC
  415. self.ifp.close()
  416. def hexbin(inp, out):
  417. """(infilename, outfilename) - Decode binhexed file"""
  418. ifp = HexBin(inp)
  419. finfo = ifp.FInfo
  420. if not out:
  421. out = ifp.FName
  422. if os.name == 'mac':
  423. ofss = macfs.FSSpec(out)
  424. out = ofss.as_pathname()
  425. ofp = open(out, 'wb')
  426. # XXXX Do translation on non-mac systems
  427. while 1:
  428. d = ifp.read(128000)
  429. if not d: break
  430. ofp.write(d)
  431. ofp.close()
  432. ifp.close_data()
  433. d = ifp.read_rsrc(128000)
  434. if d:
  435. ofp = openrsrc(out, 'wb')
  436. ofp.write(d)
  437. while 1:
  438. d = ifp.read_rsrc(128000)
  439. if not d: break
  440. ofp.write(d)
  441. ofp.close()
  442. if os.name == 'mac':
  443. nfinfo = ofss.GetFInfo()
  444. nfinfo.Creator = finfo.Creator
  445. nfinfo.Type = finfo.Type
  446. nfinfo.Flags = finfo.Flags
  447. ofss.SetFInfo(nfinfo)
  448. ifp.close()
  449. def _test():
  450. if os.name == 'mac':
  451. fss, ok = macfs.PromptGetFile('File to convert:')
  452. if not ok:
  453. sys.exit(0)
  454. fname = fss.as_pathname()
  455. else:
  456. fname = sys.argv[1]
  457. binhex(fname, fname+'.hqx')
  458. hexbin(fname+'.hqx', fname+'.viahqx')
  459. #hexbin(fname, fname+'.unpacked')
  460. sys.exit(1)
  461. if __name__ == '__main__':
  462. _test()