blob: 999a675513347e2c87e9d507cb31ef3b2477dacd [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()
Nick Coghlanb3c728f2011-03-16 21:26:40 -040055 with io.open(name, 'rb') as fp:
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()
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000062 dir, file = os.path.split(name)
63 file = file.replace(':', '-', 1)
64 return file, finfo, dsize, 0
Jack Jansenfcdffea1995-08-07 14:36:51 +000065
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000066class openrsrc:
67 def __init__(self, *args):
68 pass
Tim Peters11cf6052001-01-14 21:54:20 +000069
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000070 def read(self, *args):
71 return b''
Jack Jansenfcdffea1995-08-07 14:36:51 +000072
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000073 def write(self, *args):
74 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +000075
Benjamin Peterson69a07fb2008-05-12 22:25:16 +000076 def close(self):
77 pass
Tim Peters11cf6052001-01-14 21:54:20 +000078
Jack Jansenfcdffea1995-08-07 14:36:51 +000079class _Hqxcoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +000080 """Write data to the coder in 3-byte chunks"""
Tim Peters11cf6052001-01-14 21:54:20 +000081
Guido van Rossum4acc25b2000-02-02 15:10:15 +000082 def __init__(self, ofp):
83 self.ofp = ofp
Guido van Rossumdcee3c02007-05-22 22:25:42 +000084 self.data = b''
85 self.hqxdata = b''
Collin Winter115cde32007-08-28 06:06:33 +000086 self.linelen = LINELEN - 1
Jack Jansenfcdffea1995-08-07 14:36:51 +000087
Guido van Rossum4acc25b2000-02-02 15:10:15 +000088 def write(self, data):
89 self.data = self.data + data
90 datalen = len(self.data)
Collin Winter115cde32007-08-28 06:06:33 +000091 todo = (datalen // 3) * 3
Guido van Rossum4acc25b2000-02-02 15:10:15 +000092 data = self.data[:todo]
93 self.data = self.data[todo:]
94 if not data:
95 return
96 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
97 self._flush(0)
Guido van Rossumcce074e1996-03-25 18:54:33 +000098
Guido van Rossum4acc25b2000-02-02 15:10:15 +000099 def _flush(self, force):
100 first = 0
Collin Winter115cde32007-08-28 06:06:33 +0000101 while first <= len(self.hqxdata) - self.linelen:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000102 last = first + self.linelen
Collin Winter115cde32007-08-28 06:06:33 +0000103 self.ofp.write(self.hqxdata[first:last] + b'\n')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000104 self.linelen = LINELEN
105 first = last
106 self.hqxdata = self.hqxdata[first:]
107 if force:
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000108 self.ofp.write(self.hqxdata + b':\n')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000109
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000110 def close(self):
111 if self.data:
Collin Winter115cde32007-08-28 06:06:33 +0000112 self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000113 self._flush(1)
114 self.ofp.close()
115 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000116
117class _Rlecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000118 """Write data to the RLE-coder in suitably large chunks"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000119
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000120 def __init__(self, ofp):
121 self.ofp = ofp
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000122 self.data = b''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000123
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000124 def write(self, data):
125 self.data = self.data + data
126 if len(self.data) < REASONABLY_LARGE:
127 return
128 rledata = binascii.rlecode_hqx(self.data)
129 self.ofp.write(rledata)
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000130 self.data = b''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000131
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000132 def close(self):
133 if self.data:
134 rledata = binascii.rlecode_hqx(self.data)
135 self.ofp.write(rledata)
136 self.ofp.close()
137 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000138
139class BinHex:
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000140 def __init__(self, name_finfo_dlen_rlen, ofp):
141 name, finfo, dlen, rlen = name_finfo_dlen_rlen
Nick Coghlanb3c728f2011-03-16 21:26:40 -0400142 close_on_error = False
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')
Nick Coghlanb3c728f2011-03-16 21:26:40 -0400146 close_on_error = True
147 try:
148 ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
149 hqxer = _Hqxcoderengine(ofp)
150 self.ofp = _Rlecoderengine(hqxer)
151 self.crc = 0
152 if finfo is None:
153 finfo = FInfo()
154 self.dlen = dlen
155 self.rlen = rlen
156 self._writeinfo(name, finfo)
157 self.state = _DID_HEADER
158 except:
159 if close_on_error:
160 ofp.close()
161 raise
Jack Jansenfcdffea1995-08-07 14:36:51 +0000162
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000163 def _writeinfo(self, name, finfo):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000164 nl = len(name)
165 if nl > 63:
Collin Winter115cde32007-08-28 06:06:33 +0000166 raise Error('Filename too long')
Guido van Rossum09549f42007-08-27 20:40:10 +0000167 d = bytes([nl]) + name.encode("latin-1") + b'\0'
Guido van Rossum1b261df2007-08-28 03:35:35 +0000168 tp, cr = finfo.Type, finfo.Creator
169 if isinstance(tp, str):
170 tp = tp.encode("latin-1")
171 if isinstance(cr, str):
172 cr = cr.encode("latin-1")
173 d2 = tp + cr
Roger E. Masse469848a1997-01-16 16:51:57 +0000174
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000175 # Force all structs to be packed with big-endian
176 d3 = struct.pack('>h', finfo.Flags)
177 d4 = struct.pack('>ii', self.dlen, self.rlen)
178 info = d + d2 + d3 + d4
179 self._write(info)
180 self._writecrc()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000181
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000182 def _write(self, data):
183 self.crc = binascii.crc_hqx(data, self.crc)
184 self.ofp.write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000185
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000186 def _writecrc(self):
187 # XXXX Should this be here??
188 # self.crc = binascii.crc_hqx('\0\0', self.crc)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000189 if self.crc < 0:
190 fmt = '>h'
191 else:
192 fmt = '>H'
193 self.ofp.write(struct.pack(fmt, self.crc))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000194 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000195
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000196 def write(self, data):
197 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000198 raise Error('Writing data at the wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000199 self.dlen = self.dlen - len(data)
200 self._write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000201
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000202 def close_data(self):
Fred Drake8152d322000-12-12 23:20:45 +0000203 if self.dlen != 0:
Collin Winter115cde32007-08-28 06:06:33 +0000204 raise Error('Incorrect data size, diff=%r' % (self.rlen,))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000205 self._writecrc()
206 self.state = _DID_DATA
Jack Jansenfcdffea1995-08-07 14:36:51 +0000207
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000208 def write_rsrc(self, data):
209 if self.state < _DID_DATA:
210 self.close_data()
211 if self.state != _DID_DATA:
Collin Winter115cde32007-08-28 06:06:33 +0000212 raise Error('Writing resource data at the wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000213 self.rlen = self.rlen - len(data)
214 self._write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000215
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000216 def close(self):
217 if self.state < _DID_DATA:
218 self.close_data()
219 if self.state != _DID_DATA:
Collin Winter115cde32007-08-28 06:06:33 +0000220 raise Error('Close at the wrong time')
Fred Drake8152d322000-12-12 23:20:45 +0000221 if self.rlen != 0:
Collin Winter115cde32007-08-28 06:06:33 +0000222 raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000223 self._writecrc()
224 self.ofp.close()
225 self.state = None
226 del self.ofp
Tim Peters11cf6052001-01-14 21:54:20 +0000227
Jack Jansenfcdffea1995-08-07 14:36:51 +0000228def binhex(inp, out):
Collin Winter115cde32007-08-28 06:06:33 +0000229 """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000230 finfo = getfileinfo(inp)
231 ofp = BinHex(finfo, out)
Tim Peters11cf6052001-01-14 21:54:20 +0000232
Guido van Rossum34a042d2007-05-24 22:05:19 +0000233 ifp = io.open(inp, 'rb')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000234 # XXXX Do textfile translation on non-mac systems
Collin Winter115cde32007-08-28 06:06:33 +0000235 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000236 d = ifp.read(128000)
237 if not d: break
238 ofp.write(d)
239 ofp.close_data()
240 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000241
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000242 ifp = openrsrc(inp, 'rb')
Collin Winter115cde32007-08-28 06:06:33 +0000243 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000244 d = ifp.read(128000)
245 if not d: break
246 ofp.write_rsrc(d)
247 ofp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000248 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000249
250class _Hqxdecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000251 """Read data via the decoder in 4-byte chunks"""
Tim Peters11cf6052001-01-14 21:54:20 +0000252
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000253 def __init__(self, ifp):
254 self.ifp = ifp
255 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000256
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000257 def read(self, totalwtd):
258 """Read at least wtd bytes (or until EOF)"""
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000259 decdata = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000260 wtd = totalwtd
261 #
Tim Peters11cf6052001-01-14 21:54:20 +0000262 # The loop here is convoluted, since we don't really now how
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000263 # much to decode: there may be newlines in the incoming data.
264 while wtd > 0:
265 if self.eof: return decdata
Collin Winter115cde32007-08-28 06:06:33 +0000266 wtd = ((wtd + 2) // 3) * 4
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000267 data = self.ifp.read(wtd)
268 #
269 # Next problem: there may not be a complete number of
270 # bytes in what we pass to a2b. Solve by yet another
271 # loop.
272 #
Collin Winter115cde32007-08-28 06:06:33 +0000273 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000274 try:
Collin Winter115cde32007-08-28 06:06:33 +0000275 decdatacur, self.eof = binascii.a2b_hqx(data)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000276 break
277 except binascii.Incomplete:
278 pass
279 newdata = self.ifp.read(1)
280 if not newdata:
Collin Winter115cde32007-08-28 06:06:33 +0000281 raise Error('Premature EOF on binhex file')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000282 data = data + newdata
283 decdata = decdata + decdatacur
284 wtd = totalwtd - len(decdata)
285 if not decdata and not self.eof:
Collin Winter115cde32007-08-28 06:06:33 +0000286 raise Error('Premature EOF on binhex file')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000287 return decdata
Jack Jansenfcdffea1995-08-07 14:36:51 +0000288
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000289 def close(self):
290 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000291
292class _Rledecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000293 """Read data via the RLE-coder"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000294
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000295 def __init__(self, ifp):
296 self.ifp = ifp
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000297 self.pre_buffer = b''
298 self.post_buffer = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000299 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000300
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000301 def read(self, wtd):
302 if wtd > len(self.post_buffer):
Collin Winter115cde32007-08-28 06:06:33 +0000303 self._fill(wtd - len(self.post_buffer))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000304 rv = self.post_buffer[:wtd]
305 self.post_buffer = self.post_buffer[wtd:]
306 return rv
Jack Jansenfcdffea1995-08-07 14:36:51 +0000307
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000308 def _fill(self, wtd):
Collin Winter115cde32007-08-28 06:06:33 +0000309 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000310 if self.ifp.eof:
311 self.post_buffer = self.post_buffer + \
312 binascii.rledecode_hqx(self.pre_buffer)
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000313 self.pre_buffer = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000314 return
Tim Peters11cf6052001-01-14 21:54:20 +0000315
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000316 #
317 # Obfuscated code ahead. We have to take care that we don't
318 # end up with an orphaned RUNCHAR later on. So, we keep a couple
319 # of bytes in the buffer, depending on what the end of
320 # the buffer looks like:
321 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
322 # '?\220' - Keep 2 bytes: repeated something-else
323 # '\220\0' - Escaped \220: Keep 2 bytes.
324 # '?\220?' - Complete repeat sequence: decode all
325 # otherwise: keep 1 byte.
326 #
327 mark = len(self.pre_buffer)
Jeremy Hyltonda3f2282007-08-29 17:26:34 +0000328 if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000329 mark = mark - 3
Antoine Pitrou54923952009-07-01 14:53:06 +0000330 elif self.pre_buffer[-1:] == RUNCHAR:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000331 mark = mark - 2
Jeremy Hyltonda3f2282007-08-29 17:26:34 +0000332 elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000333 mark = mark - 2
Antoine Pitrou54923952009-07-01 14:53:06 +0000334 elif self.pre_buffer[-2:-1] == RUNCHAR:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000335 pass # Decode all
336 else:
337 mark = mark - 1
Guido van Rossum50b82e81997-02-11 16:39:31 +0000338
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000339 self.post_buffer = self.post_buffer + \
340 binascii.rledecode_hqx(self.pre_buffer[:mark])
341 self.pre_buffer = self.pre_buffer[mark:]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000342
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000343 def close(self):
344 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000345
346class HexBin:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000347 def __init__(self, ifp):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000348 if isinstance(ifp, str):
Guido van Rossum34a042d2007-05-24 22:05:19 +0000349 ifp = io.open(ifp, 'rb')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000350 #
351 # Find initial colon.
352 #
Collin Winter115cde32007-08-28 06:06:33 +0000353 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000354 ch = ifp.read(1)
355 if not ch:
Collin Winter115cde32007-08-28 06:06:33 +0000356 raise Error("No binhex data found")
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000357 # Cater for \r\n terminated lines (which show up as \n\r, hence
358 # all lines start with \r)
Guido van Rossum34a042d2007-05-24 22:05:19 +0000359 if ch == b'\r':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000360 continue
Guido van Rossum34a042d2007-05-24 22:05:19 +0000361 if ch == b':':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000362 break
Tim Peters11cf6052001-01-14 21:54:20 +0000363
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000364 hqxifp = _Hqxdecoderengine(ifp)
365 self.ifp = _Rledecoderengine(hqxifp)
366 self.crc = 0
367 self._readheader()
Tim Peters11cf6052001-01-14 21:54:20 +0000368
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000369 def _read(self, len):
370 data = self.ifp.read(len)
371 self.crc = binascii.crc_hqx(data, self.crc)
372 return data
Tim Peters11cf6052001-01-14 21:54:20 +0000373
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000374 def _checkcrc(self):
375 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
376 #self.crc = binascii.crc_hqx('\0\0', self.crc)
377 # XXXX Is this needed??
378 self.crc = self.crc & 0xffff
379 if filecrc != self.crc:
Collin Winter115cde32007-08-28 06:06:33 +0000380 raise Error('CRC error, computed %x, read %x'
381 % (self.crc, filecrc))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000382 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000383
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000384 def _readheader(self):
385 len = self._read(1)
386 fname = self._read(ord(len))
Collin Winter115cde32007-08-28 06:06:33 +0000387 rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000388 self._checkcrc()
Tim Peters11cf6052001-01-14 21:54:20 +0000389
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000390 type = rest[1:5]
391 creator = rest[5:9]
392 flags = struct.unpack('>h', rest[9:11])[0]
393 self.dlen = struct.unpack('>l', rest[11:15])[0]
394 self.rlen = struct.unpack('>l', rest[15:19])[0]
Tim Peters11cf6052001-01-14 21:54:20 +0000395
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000396 self.FName = fname
397 self.FInfo = FInfo()
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000398 self.FInfo.Creator = creator
399 self.FInfo.Type = type
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000400 self.FInfo.Flags = flags
Tim Peters11cf6052001-01-14 21:54:20 +0000401
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000402 self.state = _DID_HEADER
Tim Peters11cf6052001-01-14 21:54:20 +0000403
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000404 def read(self, *n):
405 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000406 raise Error('Read data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000407 if n:
408 n = n[0]
409 n = min(n, self.dlen)
410 else:
411 n = self.dlen
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000412 rv = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000413 while len(rv) < n:
414 rv = rv + self._read(n-len(rv))
415 self.dlen = self.dlen - n
416 return rv
Tim Peters11cf6052001-01-14 21:54:20 +0000417
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000418 def close_data(self):
419 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000420 raise Error('close_data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000421 if self.dlen:
422 dummy = self._read(self.dlen)
423 self._checkcrc()
424 self.state = _DID_DATA
Tim Peters11cf6052001-01-14 21:54:20 +0000425
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000426 def read_rsrc(self, *n):
427 if self.state == _DID_HEADER:
428 self.close_data()
429 if self.state != _DID_DATA:
Collin Winter115cde32007-08-28 06:06:33 +0000430 raise Error('Read resource data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000431 if n:
432 n = n[0]
433 n = min(n, self.rlen)
434 else:
435 n = self.rlen
436 self.rlen = self.rlen - n
437 return self._read(n)
Tim Peters11cf6052001-01-14 21:54:20 +0000438
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000439 def close(self):
440 if self.rlen:
441 dummy = self.read_rsrc(self.rlen)
442 self._checkcrc()
443 self.state = _DID_RSRC
444 self.ifp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000445
Jack Jansenfcdffea1995-08-07 14:36:51 +0000446def hexbin(inp, out):
Collin Winter115cde32007-08-28 06:06:33 +0000447 """hexbin(infilename, outfilename) - Decode binhexed file"""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000448 ifp = HexBin(inp)
449 finfo = ifp.FInfo
450 if not out:
451 out = ifp.FName
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 ifp.close()