binhex.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 binascii
  23. import contextlib
  24. import io
  25. import os
  26. import struct
  27. import warnings
  28. warnings.warn('the binhex module is deprecated', DeprecationWarning,
  29. stacklevel=2)
  30. __all__ = ["binhex","hexbin","Error"]
  31. class Error(Exception):
  32. pass
  33. # States (what have we written)
  34. _DID_HEADER = 0
  35. _DID_DATA = 1
  36. # Various constants
  37. REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
  38. LINELEN = 64
  39. RUNCHAR = b"\x90"
  40. #
  41. # This code is no longer byte-order dependent
  42. class FInfo:
  43. def __init__(self):
  44. self.Type = '????'
  45. self.Creator = '????'
  46. self.Flags = 0
  47. def getfileinfo(name):
  48. finfo = FInfo()
  49. with io.open(name, 'rb') as fp:
  50. # Quick check for textfile
  51. data = fp.read(512)
  52. if 0 not in data:
  53. finfo.Type = 'TEXT'
  54. fp.seek(0, 2)
  55. dsize = fp.tell()
  56. dir, file = os.path.split(name)
  57. file = file.replace(':', '-', 1)
  58. return file, finfo, dsize, 0
  59. class openrsrc:
  60. def __init__(self, *args):
  61. pass
  62. def read(self, *args):
  63. return b''
  64. def write(self, *args):
  65. pass
  66. def close(self):
  67. pass
  68. # DeprecationWarning is already emitted on "import binhex". There is no need
  69. # to repeat the warning at each call to deprecated binascii functions.
  70. @contextlib.contextmanager
  71. def _ignore_deprecation_warning():
  72. with warnings.catch_warnings():
  73. warnings.filterwarnings('ignore', '', DeprecationWarning)
  74. yield
  75. class _Hqxcoderengine:
  76. """Write data to the coder in 3-byte chunks"""
  77. def __init__(self, ofp):
  78. self.ofp = ofp
  79. self.data = b''
  80. self.hqxdata = b''
  81. self.linelen = LINELEN - 1
  82. def write(self, data):
  83. self.data = self.data + data
  84. datalen = len(self.data)
  85. todo = (datalen // 3) * 3
  86. data = self.data[:todo]
  87. self.data = self.data[todo:]
  88. if not data:
  89. return
  90. with _ignore_deprecation_warning():
  91. self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
  92. self._flush(0)
  93. def _flush(self, force):
  94. first = 0
  95. while first <= len(self.hqxdata) - self.linelen:
  96. last = first + self.linelen
  97. self.ofp.write(self.hqxdata[first:last] + b'\r')
  98. self.linelen = LINELEN
  99. first = last
  100. self.hqxdata = self.hqxdata[first:]
  101. if force:
  102. self.ofp.write(self.hqxdata + b':\r')
  103. def close(self):
  104. if self.data:
  105. with _ignore_deprecation_warning():
  106. self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data)
  107. self._flush(1)
  108. self.ofp.close()
  109. del self.ofp
  110. class _Rlecoderengine:
  111. """Write data to the RLE-coder in suitably large chunks"""
  112. def __init__(self, ofp):
  113. self.ofp = ofp
  114. self.data = b''
  115. def write(self, data):
  116. self.data = self.data + data
  117. if len(self.data) < REASONABLY_LARGE:
  118. return
  119. with _ignore_deprecation_warning():
  120. rledata = binascii.rlecode_hqx(self.data)
  121. self.ofp.write(rledata)
  122. self.data = b''
  123. def close(self):
  124. if self.data:
  125. with _ignore_deprecation_warning():
  126. rledata = binascii.rlecode_hqx(self.data)
  127. self.ofp.write(rledata)
  128. self.ofp.close()
  129. del self.ofp
  130. class BinHex:
  131. def __init__(self, name_finfo_dlen_rlen, ofp):
  132. name, finfo, dlen, rlen = name_finfo_dlen_rlen
  133. close_on_error = False
  134. if isinstance(ofp, str):
  135. ofname = ofp
  136. ofp = io.open(ofname, 'wb')
  137. close_on_error = True
  138. try:
  139. ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
  140. hqxer = _Hqxcoderengine(ofp)
  141. self.ofp = _Rlecoderengine(hqxer)
  142. self.crc = 0
  143. if finfo is None:
  144. finfo = FInfo()
  145. self.dlen = dlen
  146. self.rlen = rlen
  147. self._writeinfo(name, finfo)
  148. self.state = _DID_HEADER
  149. except:
  150. if close_on_error:
  151. ofp.close()
  152. raise
  153. def _writeinfo(self, name, finfo):
  154. nl = len(name)
  155. if nl > 63:
  156. raise Error('Filename too long')
  157. d = bytes([nl]) + name.encode("latin-1") + b'\0'
  158. tp, cr = finfo.Type, finfo.Creator
  159. if isinstance(tp, str):
  160. tp = tp.encode("latin-1")
  161. if isinstance(cr, str):
  162. cr = cr.encode("latin-1")
  163. d2 = tp + cr
  164. # Force all structs to be packed with big-endian
  165. d3 = struct.pack('>h', finfo.Flags)
  166. d4 = struct.pack('>ii', self.dlen, self.rlen)
  167. info = d + d2 + d3 + d4
  168. self._write(info)
  169. self._writecrc()
  170. def _write(self, data):
  171. self.crc = binascii.crc_hqx(data, self.crc)
  172. self.ofp.write(data)
  173. def _writecrc(self):
  174. # XXXX Should this be here??
  175. # self.crc = binascii.crc_hqx('\0\0', self.crc)
  176. if self.crc < 0:
  177. fmt = '>h'
  178. else:
  179. fmt = '>H'
  180. self.ofp.write(struct.pack(fmt, self.crc))
  181. self.crc = 0
  182. def write(self, data):
  183. if self.state != _DID_HEADER:
  184. raise Error('Writing data at the wrong time')
  185. self.dlen = self.dlen - len(data)
  186. self._write(data)
  187. def close_data(self):
  188. if self.dlen != 0:
  189. raise Error('Incorrect data size, diff=%r' % (self.rlen,))
  190. self._writecrc()
  191. self.state = _DID_DATA
  192. def write_rsrc(self, data):
  193. if self.state < _DID_DATA:
  194. self.close_data()
  195. if self.state != _DID_DATA:
  196. raise Error('Writing resource data at the wrong time')
  197. self.rlen = self.rlen - len(data)
  198. self._write(data)
  199. def close(self):
  200. if self.state is None:
  201. return
  202. try:
  203. if self.state < _DID_DATA:
  204. self.close_data()
  205. if self.state != _DID_DATA:
  206. raise Error('Close at the wrong time')
  207. if self.rlen != 0:
  208. raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
  209. self._writecrc()
  210. finally:
  211. self.state = None
  212. ofp = self.ofp
  213. del self.ofp
  214. ofp.close()
  215. def binhex(inp, out):
  216. """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
  217. finfo = getfileinfo(inp)
  218. ofp = BinHex(finfo, out)
  219. with io.open(inp, 'rb') as ifp:
  220. # XXXX Do textfile translation on non-mac systems
  221. while True:
  222. d = ifp.read(128000)
  223. if not d: break
  224. ofp.write(d)
  225. ofp.close_data()
  226. ifp = openrsrc(inp, 'rb')
  227. while True:
  228. d = ifp.read(128000)
  229. if not d: break
  230. ofp.write_rsrc(d)
  231. ofp.close()
  232. ifp.close()
  233. class _Hqxdecoderengine:
  234. """Read data via the decoder in 4-byte chunks"""
  235. def __init__(self, ifp):
  236. self.ifp = ifp
  237. self.eof = 0
  238. def read(self, totalwtd):
  239. """Read at least wtd bytes (or until EOF)"""
  240. decdata = b''
  241. wtd = totalwtd
  242. #
  243. # The loop here is convoluted, since we don't really now how
  244. # much to decode: there may be newlines in the incoming data.
  245. while wtd > 0:
  246. if self.eof: return decdata
  247. wtd = ((wtd + 2) // 3) * 4
  248. data = self.ifp.read(wtd)
  249. #
  250. # Next problem: there may not be a complete number of
  251. # bytes in what we pass to a2b. Solve by yet another
  252. # loop.
  253. #
  254. while True:
  255. try:
  256. with _ignore_deprecation_warning():
  257. decdatacur, self.eof = binascii.a2b_hqx(data)
  258. break
  259. except binascii.Incomplete:
  260. pass
  261. newdata = self.ifp.read(1)
  262. if not newdata:
  263. raise Error('Premature EOF on binhex file')
  264. data = data + newdata
  265. decdata = decdata + decdatacur
  266. wtd = totalwtd - len(decdata)
  267. if not decdata and not self.eof:
  268. raise Error('Premature EOF on binhex file')
  269. return decdata
  270. def close(self):
  271. self.ifp.close()
  272. class _Rledecoderengine:
  273. """Read data via the RLE-coder"""
  274. def __init__(self, ifp):
  275. self.ifp = ifp
  276. self.pre_buffer = b''
  277. self.post_buffer = b''
  278. self.eof = 0
  279. def read(self, wtd):
  280. if wtd > len(self.post_buffer):
  281. self._fill(wtd - len(self.post_buffer))
  282. rv = self.post_buffer[:wtd]
  283. self.post_buffer = self.post_buffer[wtd:]
  284. return rv
  285. def _fill(self, wtd):
  286. self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
  287. if self.ifp.eof:
  288. with _ignore_deprecation_warning():
  289. self.post_buffer = self.post_buffer + \
  290. binascii.rledecode_hqx(self.pre_buffer)
  291. self.pre_buffer = b''
  292. return
  293. #
  294. # Obfuscated code ahead. We have to take care that we don't
  295. # end up with an orphaned RUNCHAR later on. So, we keep a couple
  296. # of bytes in the buffer, depending on what the end of
  297. # the buffer looks like:
  298. # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
  299. # '?\220' - Keep 2 bytes: repeated something-else
  300. # '\220\0' - Escaped \220: Keep 2 bytes.
  301. # '?\220?' - Complete repeat sequence: decode all
  302. # otherwise: keep 1 byte.
  303. #
  304. mark = len(self.pre_buffer)
  305. if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
  306. mark = mark - 3
  307. elif self.pre_buffer[-1:] == RUNCHAR:
  308. mark = mark - 2
  309. elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
  310. mark = mark - 2
  311. elif self.pre_buffer[-2:-1] == RUNCHAR:
  312. pass # Decode all
  313. else:
  314. mark = mark - 1
  315. with _ignore_deprecation_warning():
  316. self.post_buffer = self.post_buffer + \
  317. binascii.rledecode_hqx(self.pre_buffer[:mark])
  318. self.pre_buffer = self.pre_buffer[mark:]
  319. def close(self):
  320. self.ifp.close()
  321. class HexBin:
  322. def __init__(self, ifp):
  323. if isinstance(ifp, str):
  324. ifp = io.open(ifp, 'rb')
  325. #
  326. # Find initial colon.
  327. #
  328. while True:
  329. ch = ifp.read(1)
  330. if not ch:
  331. raise Error("No binhex data found")
  332. # Cater for \r\n terminated lines (which show up as \n\r, hence
  333. # all lines start with \r)
  334. if ch == b'\r':
  335. continue
  336. if ch == b':':
  337. break
  338. hqxifp = _Hqxdecoderengine(ifp)
  339. self.ifp = _Rledecoderengine(hqxifp)
  340. self.crc = 0
  341. self._readheader()
  342. def _read(self, len):
  343. data = self.ifp.read(len)
  344. self.crc = binascii.crc_hqx(data, self.crc)
  345. return data
  346. def _checkcrc(self):
  347. filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
  348. #self.crc = binascii.crc_hqx('\0\0', self.crc)
  349. # XXXX Is this needed??
  350. self.crc = self.crc & 0xffff
  351. if filecrc != self.crc:
  352. raise Error('CRC error, computed %x, read %x'
  353. % (self.crc, filecrc))
  354. self.crc = 0
  355. def _readheader(self):
  356. len = self._read(1)
  357. fname = self._read(ord(len))
  358. rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
  359. self._checkcrc()
  360. type = rest[1:5]
  361. creator = rest[5:9]
  362. flags = struct.unpack('>h', rest[9:11])[0]
  363. self.dlen = struct.unpack('>l', rest[11:15])[0]
  364. self.rlen = struct.unpack('>l', rest[15:19])[0]
  365. self.FName = fname
  366. self.FInfo = FInfo()
  367. self.FInfo.Creator = creator
  368. self.FInfo.Type = type
  369. self.FInfo.Flags = flags
  370. self.state = _DID_HEADER
  371. def read(self, *n):
  372. if self.state != _DID_HEADER:
  373. raise Error('Read data at wrong time')
  374. if n:
  375. n = n[0]
  376. n = min(n, self.dlen)
  377. else:
  378. n = self.dlen
  379. rv = b''
  380. while len(rv) < n:
  381. rv = rv + self._read(n-len(rv))
  382. self.dlen = self.dlen - n
  383. return rv
  384. def close_data(self):
  385. if self.state != _DID_HEADER:
  386. raise Error('close_data at wrong time')
  387. if self.dlen:
  388. dummy = self._read(self.dlen)
  389. self._checkcrc()
  390. self.state = _DID_DATA
  391. def read_rsrc(self, *n):
  392. if self.state == _DID_HEADER:
  393. self.close_data()
  394. if self.state != _DID_DATA:
  395. raise Error('Read resource data at wrong time')
  396. if n:
  397. n = n[0]
  398. n = min(n, self.rlen)
  399. else:
  400. n = self.rlen
  401. self.rlen = self.rlen - n
  402. return self._read(n)
  403. def close(self):
  404. if self.state is None:
  405. return
  406. try:
  407. if self.rlen:
  408. dummy = self.read_rsrc(self.rlen)
  409. self._checkcrc()
  410. finally:
  411. self.state = None
  412. self.ifp.close()
  413. def hexbin(inp, out):
  414. """hexbin(infilename, outfilename) - Decode binhexed file"""
  415. ifp = HexBin(inp)
  416. finfo = ifp.FInfo
  417. if not out:
  418. out = ifp.FName
  419. with io.open(out, 'wb') as ofp:
  420. # XXXX Do translation on non-mac systems
  421. while True:
  422. d = ifp.read(128000)
  423. if not d: break
  424. ofp.write(d)
  425. ifp.close_data()
  426. d = ifp.read_rsrc(128000)
  427. if d:
  428. ofp = openrsrc(out, 'wb')
  429. ofp.write(d)
  430. while True:
  431. d = ifp.read_rsrc(128000)
  432. if not d: break
  433. ofp.write(d)
  434. ofp.close()
  435. ifp.close()