blob: 1c3f3429032667dca3939a237e76121898898372 [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""Macintosh binhex compression/decompression.
Guido van Rossum4acc25b2000-02-02 15:10:15 +00002
Jack Jansenfcdffea1995-08-07 14:36:51 +00003easy interface:
4binhex(inputfilename, outputfilename)
5hexbin(inputfilename, outputfilename)
6"""
7
8#
9# Jack Jansen, CWI, August 1995.
10#
11# The module is supposed to be as compatible as possible. Especially the
12# easy interface should work "as expected" on any platform.
13# XXXX Note: currently, textfiles appear in mac-form on all platforms.
14# We seem to lack a simple character-translate in python.
15# (we should probably use ISO-Latin-1 on all but the mac platform).
Jeremy Hyltona05e2932000-06-28 14:48:01 +000016# XXXX The simple routines are too simple: they expect to hold the complete
Jack Jansenfcdffea1995-08-07 14:36:51 +000017# files in-core. Should be fixed.
Roger E. Masse469848a1997-01-16 16:51:57 +000018# XXXX It would be nice to handle AppleDouble format on unix
19# (for servers serving macs).
Jack Jansenfcdffea1995-08-07 14:36:51 +000020# XXXX I don't understand what happens when you get 0x90 times the same byte on
21# input. The resulting code (xx 90 90) would appear to be interpreted as an
22# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
23#
Guido van Rossumdcee3c02007-05-22 22:25:42 +000024import io
Jack Jansenfcdffea1995-08-07 14:36:51 +000025import os
Guido van Rossumdcee3c02007-05-22 22:25:42 +000026import sys
Jack Jansenfcdffea1995-08-07 14:36:51 +000027import struct
Jack Jansenfcdffea1995-08-07 14:36:51 +000028import binascii
Tim Peters11cf6052001-01-14 21:54:20 +000029
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000030__all__ = ["binhex","hexbin","Error"]
31
Fred Drake227b1202000-08-17 05:06:49 +000032class Error(Exception):
33 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +000034
35# States (what have we written)
36[_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
37
38# Various constants
Collin Winter115cde32007-08-28 06:06:33 +000039REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
40LINELEN = 64
Jeremy Hyltonda3f2282007-08-29 17:26:34 +000041RUNCHAR = b"\x90"
Jack Jansenfcdffea1995-08-07 14:36:51 +000042
43#
Roger E. Masse469848a1997-01-16 16:51:57 +000044# This code is no longer byte-order dependent
Jack Jansenfcdffea1995-08-07 14:36:51 +000045
Tim Peters11cf6052001-01-14 21:54:20 +000046
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000047class FInfo:
48 def __init__(self):
49 self.Type = '????'
50 self.Creator = '????'
51 self.Flags = 0
Tim Peters11cf6052001-01-14 21:54:20 +000052
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000053def getfileinfo(name):
54 finfo = FInfo()
55 fp = io.open(name, 'rb')
56 # Quick check for textfile
57 data = fp.read(512)
58 if 0 not in data:
59 finfo.Type = 'TEXT'
60 fp.seek(0, 2)
61 dsize = fp.tell()
62 fp.close()
63 dir, file = os.path.split(name)
64 file = file.replace(':', '-', 1)
65 return file, finfo, dsize, 0
Jack Jansenfcdffea1995-08-07 14:36:51 +000066
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000067class openrsrc:
68 def __init__(self, *args):
69 pass
Tim Peters11cf6052001-01-14 21:54:20 +000070
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000071 def read(self, *args):
72 return b''
Jack Jansenfcdffea1995-08-07 14:36:51 +000073
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000074 def write(self, *args):
75 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +000076
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000077 def close(self):
78 pass
Tim Peters11cf6052001-01-14 21:54:20 +000079
Jack Jansenfcdffea1995-08-07 14:36:51 +000080class _Hqxcoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +000081 """Write data to the coder in 3-byte chunks"""
Tim Peters11cf6052001-01-14 21:54:20 +000082
Guido van Rossum4acc25b2000-02-02 15:10:15 +000083 def __init__(self, ofp):
84 self.ofp = ofp
Guido van Rossumdcee3c02007-05-22 22:25:42 +000085 self.data = b''
86 self.hqxdata = b''
Collin Winter115cde32007-08-28 06:06:33 +000087 self.linelen = LINELEN - 1
Jack Jansenfcdffea1995-08-07 14:36:51 +000088
Guido van Rossum4acc25b2000-02-02 15:10:15 +000089 def write(self, data):
90 self.data = self.data + data
91 datalen = len(self.data)
Collin Winter115cde32007-08-28 06:06:33 +000092 todo = (datalen // 3) * 3
Guido van Rossum4acc25b2000-02-02 15:10:15 +000093 data = self.data[:todo]
94 self.data = self.data[todo:]
95 if not data:
96 return
97 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
98 self._flush(0)
Guido van Rossumcce074e1996-03-25 18:54:33 +000099
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000100 def _flush(self, force):
101 first = 0
Collin Winter115cde32007-08-28 06:06:33 +0000102 while first <= len(self.hqxdata) - self.linelen:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000103 last = first + self.linelen
Collin Winter115cde32007-08-28 06:06:33 +0000104 self.ofp.write(self.hqxdata[first:last] + b'\n')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000105 self.linelen = LINELEN
106 first = last
107 self.hqxdata = self.hqxdata[first:]
108 if force:
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000109 self.ofp.write(self.hqxdata + b':\n')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000110
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000111 def close(self):
112 if self.data:
Collin Winter115cde32007-08-28 06:06:33 +0000113 self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000114 self._flush(1)
115 self.ofp.close()
116 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000117
118class _Rlecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000119 """Write data to the RLE-coder in suitably large chunks"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000120
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000121 def __init__(self, ofp):
122 self.ofp = ofp
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000123 self.data = b''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000124
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000125 def write(self, data):
126 self.data = self.data + data
127 if len(self.data) < REASONABLY_LARGE:
128 return
129 rledata = binascii.rlecode_hqx(self.data)
130 self.ofp.write(rledata)
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000131 self.data = b''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000132
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000133 def close(self):
134 if self.data:
135 rledata = binascii.rlecode_hqx(self.data)
136 self.ofp.write(rledata)
137 self.ofp.close()
138 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000139
140class BinHex:
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000141 def __init__(self, name_finfo_dlen_rlen, ofp):
142 name, finfo, dlen, rlen = name_finfo_dlen_rlen
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000143 if isinstance(ofp, str):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000144 ofname = ofp
Guido van Rossum34a042d2007-05-24 22:05:19 +0000145 ofp = io.open(ofname, 'wb')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000146 if os.name == 'mac':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000147 fss = FSSpec(ofname)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000148 fss.SetCreatorType('BnHq', 'TEXT')
Guido van Rossum34a042d2007-05-24 22:05:19 +0000149 ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000150 hqxer = _Hqxcoderengine(ofp)
151 self.ofp = _Rlecoderengine(hqxer)
152 self.crc = 0
Fred Drake8152d322000-12-12 23:20:45 +0000153 if finfo is None:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000154 finfo = FInfo()
155 self.dlen = dlen
156 self.rlen = rlen
157 self._writeinfo(name, finfo)
158 self.state = _DID_HEADER
Jack Jansenfcdffea1995-08-07 14:36:51 +0000159
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000160 def _writeinfo(self, name, finfo):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000161 nl = len(name)
162 if nl > 63:
Collin Winter115cde32007-08-28 06:06:33 +0000163 raise Error('Filename too long')
Guido van Rossum09549f42007-08-27 20:40:10 +0000164 d = bytes([nl]) + name.encode("latin-1") + b'\0'
Guido van Rossum1b261df2007-08-28 03:35:35 +0000165 tp, cr = finfo.Type, finfo.Creator
166 if isinstance(tp, str):
167 tp = tp.encode("latin-1")
168 if isinstance(cr, str):
169 cr = cr.encode("latin-1")
170 d2 = tp + cr
Roger E. Masse469848a1997-01-16 16:51:57 +0000171
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000172 # Force all structs to be packed with big-endian
173 d3 = struct.pack('>h', finfo.Flags)
174 d4 = struct.pack('>ii', self.dlen, self.rlen)
175 info = d + d2 + d3 + d4
176 self._write(info)
177 self._writecrc()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000178
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000179 def _write(self, data):
180 self.crc = binascii.crc_hqx(data, self.crc)
181 self.ofp.write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000182
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000183 def _writecrc(self):
184 # XXXX Should this be here??
185 # self.crc = binascii.crc_hqx('\0\0', self.crc)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000186 if self.crc < 0:
187 fmt = '>h'
188 else:
189 fmt = '>H'
190 self.ofp.write(struct.pack(fmt, self.crc))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000191 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000192
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000193 def write(self, data):
194 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000195 raise Error('Writing data at the wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000196 self.dlen = self.dlen - len(data)
197 self._write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000198
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000199 def close_data(self):
Fred Drake8152d322000-12-12 23:20:45 +0000200 if self.dlen != 0:
Collin Winter115cde32007-08-28 06:06:33 +0000201 raise Error('Incorrect data size, diff=%r' % (self.rlen,))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000202 self._writecrc()
203 self.state = _DID_DATA
Jack Jansenfcdffea1995-08-07 14:36:51 +0000204
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000205 def write_rsrc(self, data):
206 if self.state < _DID_DATA:
207 self.close_data()
208 if self.state != _DID_DATA:
Collin Winter115cde32007-08-28 06:06:33 +0000209 raise Error('Writing resource data at the wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000210 self.rlen = self.rlen - len(data)
211 self._write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000212
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000213 def close(self):
214 if self.state < _DID_DATA:
215 self.close_data()
216 if self.state != _DID_DATA:
Collin Winter115cde32007-08-28 06:06:33 +0000217 raise Error('Close at the wrong time')
Fred Drake8152d322000-12-12 23:20:45 +0000218 if self.rlen != 0:
Collin Winter115cde32007-08-28 06:06:33 +0000219 raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000220 self._writecrc()
221 self.ofp.close()
222 self.state = None
223 del self.ofp
Tim Peters11cf6052001-01-14 21:54:20 +0000224
Jack Jansenfcdffea1995-08-07 14:36:51 +0000225def binhex(inp, out):
Collin Winter115cde32007-08-28 06:06:33 +0000226 """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000227 finfo = getfileinfo(inp)
228 ofp = BinHex(finfo, out)
Tim Peters11cf6052001-01-14 21:54:20 +0000229
Guido van Rossum34a042d2007-05-24 22:05:19 +0000230 ifp = io.open(inp, 'rb')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000231 # XXXX Do textfile translation on non-mac systems
Collin Winter115cde32007-08-28 06:06:33 +0000232 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000233 d = ifp.read(128000)
234 if not d: break
235 ofp.write(d)
236 ofp.close_data()
237 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000238
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000239 ifp = openrsrc(inp, 'rb')
Collin Winter115cde32007-08-28 06:06:33 +0000240 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000241 d = ifp.read(128000)
242 if not d: break
243 ofp.write_rsrc(d)
244 ofp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000245 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000246
247class _Hqxdecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000248 """Read data via the decoder in 4-byte chunks"""
Tim Peters11cf6052001-01-14 21:54:20 +0000249
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000250 def __init__(self, ifp):
251 self.ifp = ifp
252 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000253
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000254 def read(self, totalwtd):
255 """Read at least wtd bytes (or until EOF)"""
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000256 decdata = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000257 wtd = totalwtd
258 #
Tim Peters11cf6052001-01-14 21:54:20 +0000259 # The loop here is convoluted, since we don't really now how
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000260 # much to decode: there may be newlines in the incoming data.
261 while wtd > 0:
262 if self.eof: return decdata
Collin Winter115cde32007-08-28 06:06:33 +0000263 wtd = ((wtd + 2) // 3) * 4
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000264 data = self.ifp.read(wtd)
265 #
266 # Next problem: there may not be a complete number of
267 # bytes in what we pass to a2b. Solve by yet another
268 # loop.
269 #
Collin Winter115cde32007-08-28 06:06:33 +0000270 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000271 try:
Collin Winter115cde32007-08-28 06:06:33 +0000272 decdatacur, self.eof = binascii.a2b_hqx(data)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000273 break
274 except binascii.Incomplete:
275 pass
276 newdata = self.ifp.read(1)
277 if not newdata:
Collin Winter115cde32007-08-28 06:06:33 +0000278 raise Error('Premature EOF on binhex file')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000279 data = data + newdata
280 decdata = decdata + decdatacur
281 wtd = totalwtd - len(decdata)
282 if not decdata and not self.eof:
Collin Winter115cde32007-08-28 06:06:33 +0000283 raise Error('Premature EOF on binhex file')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000284 return decdata
Jack Jansenfcdffea1995-08-07 14:36:51 +0000285
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000286 def close(self):
287 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000288
289class _Rledecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000290 """Read data via the RLE-coder"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000291
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000292 def __init__(self, ifp):
293 self.ifp = ifp
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000294 self.pre_buffer = b''
295 self.post_buffer = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000296 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000297
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000298 def read(self, wtd):
299 if wtd > len(self.post_buffer):
Collin Winter115cde32007-08-28 06:06:33 +0000300 self._fill(wtd - len(self.post_buffer))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000301 rv = self.post_buffer[:wtd]
302 self.post_buffer = self.post_buffer[wtd:]
303 return rv
Jack Jansenfcdffea1995-08-07 14:36:51 +0000304
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000305 def _fill(self, wtd):
Collin Winter115cde32007-08-28 06:06:33 +0000306 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000307 if self.ifp.eof:
308 self.post_buffer = self.post_buffer + \
309 binascii.rledecode_hqx(self.pre_buffer)
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000310 self.pre_buffer = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000311 return
Tim Peters11cf6052001-01-14 21:54:20 +0000312
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000313 #
314 # Obfuscated code ahead. We have to take care that we don't
315 # end up with an orphaned RUNCHAR later on. So, we keep a couple
316 # of bytes in the buffer, depending on what the end of
317 # the buffer looks like:
318 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
319 # '?\220' - Keep 2 bytes: repeated something-else
320 # '\220\0' - Escaped \220: Keep 2 bytes.
321 # '?\220?' - Complete repeat sequence: decode all
322 # otherwise: keep 1 byte.
323 #
324 mark = len(self.pre_buffer)
Jeremy Hyltonda3f2282007-08-29 17:26:34 +0000325 if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000326 mark = mark - 3
327 elif self.pre_buffer[-1] == RUNCHAR:
328 mark = mark - 2
Jeremy Hyltonda3f2282007-08-29 17:26:34 +0000329 elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000330 mark = mark - 2
331 elif self.pre_buffer[-2] == RUNCHAR:
332 pass # Decode all
333 else:
334 mark = mark - 1
Guido van Rossum50b82e81997-02-11 16:39:31 +0000335
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000336 self.post_buffer = self.post_buffer + \
337 binascii.rledecode_hqx(self.pre_buffer[:mark])
338 self.pre_buffer = self.pre_buffer[mark:]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000339
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000340 def close(self):
341 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000342
343class HexBin:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000344 def __init__(self, ifp):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000345 if isinstance(ifp, str):
Guido van Rossum34a042d2007-05-24 22:05:19 +0000346 ifp = io.open(ifp, 'rb')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000347 #
348 # Find initial colon.
349 #
Collin Winter115cde32007-08-28 06:06:33 +0000350 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000351 ch = ifp.read(1)
352 if not ch:
Collin Winter115cde32007-08-28 06:06:33 +0000353 raise Error("No binhex data found")
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000354 # Cater for \r\n terminated lines (which show up as \n\r, hence
355 # all lines start with \r)
Guido van Rossum34a042d2007-05-24 22:05:19 +0000356 if ch == b'\r':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000357 continue
Guido van Rossum34a042d2007-05-24 22:05:19 +0000358 if ch == b':':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000359 break
Tim Peters11cf6052001-01-14 21:54:20 +0000360
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000361 hqxifp = _Hqxdecoderengine(ifp)
362 self.ifp = _Rledecoderengine(hqxifp)
363 self.crc = 0
364 self._readheader()
Tim Peters11cf6052001-01-14 21:54:20 +0000365
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000366 def _read(self, len):
367 data = self.ifp.read(len)
368 self.crc = binascii.crc_hqx(data, self.crc)
369 return data
Tim Peters11cf6052001-01-14 21:54:20 +0000370
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000371 def _checkcrc(self):
372 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
373 #self.crc = binascii.crc_hqx('\0\0', self.crc)
374 # XXXX Is this needed??
375 self.crc = self.crc & 0xffff
376 if filecrc != self.crc:
Collin Winter115cde32007-08-28 06:06:33 +0000377 raise Error('CRC error, computed %x, read %x'
378 % (self.crc, filecrc))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000379 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000380
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000381 def _readheader(self):
382 len = self._read(1)
383 fname = self._read(ord(len))
Collin Winter115cde32007-08-28 06:06:33 +0000384 rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000385 self._checkcrc()
Tim Peters11cf6052001-01-14 21:54:20 +0000386
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000387 type = rest[1:5]
388 creator = rest[5:9]
389 flags = struct.unpack('>h', rest[9:11])[0]
390 self.dlen = struct.unpack('>l', rest[11:15])[0]
391 self.rlen = struct.unpack('>l', rest[15:19])[0]
Tim Peters11cf6052001-01-14 21:54:20 +0000392
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000393 self.FName = fname
394 self.FInfo = FInfo()
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000395 self.FInfo.Creator = creator
396 self.FInfo.Type = type
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000397 self.FInfo.Flags = flags
Tim Peters11cf6052001-01-14 21:54:20 +0000398
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000399 self.state = _DID_HEADER
Tim Peters11cf6052001-01-14 21:54:20 +0000400
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000401 def read(self, *n):
402 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000403 raise Error('Read data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000404 if n:
405 n = n[0]
406 n = min(n, self.dlen)
407 else:
408 n = self.dlen
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000409 rv = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000410 while len(rv) < n:
411 rv = rv + self._read(n-len(rv))
412 self.dlen = self.dlen - n
413 return rv
Tim Peters11cf6052001-01-14 21:54:20 +0000414
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000415 def close_data(self):
416 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000417 raise Error('close_data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000418 if self.dlen:
419 dummy = self._read(self.dlen)
420 self._checkcrc()
421 self.state = _DID_DATA
Tim Peters11cf6052001-01-14 21:54:20 +0000422
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000423 def read_rsrc(self, *n):
424 if self.state == _DID_HEADER:
425 self.close_data()
426 if self.state != _DID_DATA:
Collin Winter115cde32007-08-28 06:06:33 +0000427 raise Error('Read resource data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000428 if n:
429 n = n[0]
430 n = min(n, self.rlen)
431 else:
432 n = self.rlen
433 self.rlen = self.rlen - n
434 return self._read(n)
Tim Peters11cf6052001-01-14 21:54:20 +0000435
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000436 def close(self):
437 if self.rlen:
438 dummy = self.read_rsrc(self.rlen)
439 self._checkcrc()
440 self.state = _DID_RSRC
441 self.ifp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000442
Jack Jansenfcdffea1995-08-07 14:36:51 +0000443def hexbin(inp, out):
Collin Winter115cde32007-08-28 06:06:33 +0000444 """hexbin(infilename, outfilename) - Decode binhexed file"""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000445 ifp = HexBin(inp)
446 finfo = ifp.FInfo
447 if not out:
448 out = ifp.FName
449 if os.name == 'mac':
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000450 ofss = FSSpec(out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000451 out = ofss.as_pathname()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000452
Guido van Rossum34a042d2007-05-24 22:05:19 +0000453 ofp = io.open(out, 'wb')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000454 # XXXX Do translation on non-mac systems
Collin Winter115cde32007-08-28 06:06:33 +0000455 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000456 d = ifp.read(128000)
457 if not d: break
458 ofp.write(d)
459 ofp.close()
460 ifp.close_data()
Tim Peters11cf6052001-01-14 21:54:20 +0000461
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000462 d = ifp.read_rsrc(128000)
463 if d:
464 ofp = openrsrc(out, 'wb')
465 ofp.write(d)
Collin Winter115cde32007-08-28 06:06:33 +0000466 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000467 d = ifp.read_rsrc(128000)
468 if not d: break
469 ofp.write(d)
470 ofp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000471
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000472 if os.name == 'mac':
473 nfinfo = ofss.GetFInfo()
474 nfinfo.Creator = finfo.Creator
475 nfinfo.Type = finfo.Type
476 nfinfo.Flags = finfo.Flags
477 ofss.SetFInfo(nfinfo)
Tim Peters11cf6052001-01-14 21:54:20 +0000478
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000479 ifp.close()