blob: 56b5f852c0038ac3320c34bd976925e2ee140c06 [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
26import struct
Jack Jansenfcdffea1995-08-07 14:36:51 +000027import binascii
Tim Peters11cf6052001-01-14 21:54:20 +000028
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000029__all__ = ["binhex","hexbin","Error"]
30
Fred Drake227b1202000-08-17 05:06:49 +000031class Error(Exception):
32 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +000033
34# States (what have we written)
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +030035_DID_HEADER = 0
36_DID_DATA = 1
Jack Jansenfcdffea1995-08-07 14:36:51 +000037
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):
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300217 if self.state is None:
218 return
219 try:
220 if self.state < _DID_DATA:
221 self.close_data()
222 if self.state != _DID_DATA:
223 raise Error('Close at the wrong time')
224 if self.rlen != 0:
225 raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
226 self._writecrc()
227 finally:
228 self.state = None
229 ofp = self.ofp
230 del self.ofp
231 ofp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000232
Jack Jansenfcdffea1995-08-07 14:36:51 +0000233def binhex(inp, out):
Collin Winter115cde32007-08-28 06:06:33 +0000234 """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000235 finfo = getfileinfo(inp)
236 ofp = BinHex(finfo, out)
Tim Peters11cf6052001-01-14 21:54:20 +0000237
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300238 with io.open(inp, 'rb') as ifp:
239 # XXXX Do textfile translation on non-mac systems
240 while True:
241 d = ifp.read(128000)
242 if not d: break
243 ofp.write(d)
244 ofp.close_data()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000245
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000246 ifp = openrsrc(inp, 'rb')
Collin Winter115cde32007-08-28 06:06:33 +0000247 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000248 d = ifp.read(128000)
249 if not d: break
250 ofp.write_rsrc(d)
251 ofp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000252 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000253
254class _Hqxdecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000255 """Read data via the decoder in 4-byte chunks"""
Tim Peters11cf6052001-01-14 21:54:20 +0000256
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000257 def __init__(self, ifp):
258 self.ifp = ifp
259 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000260
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000261 def read(self, totalwtd):
262 """Read at least wtd bytes (or until EOF)"""
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000263 decdata = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000264 wtd = totalwtd
265 #
Tim Peters11cf6052001-01-14 21:54:20 +0000266 # The loop here is convoluted, since we don't really now how
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000267 # much to decode: there may be newlines in the incoming data.
268 while wtd > 0:
269 if self.eof: return decdata
Collin Winter115cde32007-08-28 06:06:33 +0000270 wtd = ((wtd + 2) // 3) * 4
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000271 data = self.ifp.read(wtd)
272 #
273 # Next problem: there may not be a complete number of
274 # bytes in what we pass to a2b. Solve by yet another
275 # loop.
276 #
Collin Winter115cde32007-08-28 06:06:33 +0000277 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000278 try:
Collin Winter115cde32007-08-28 06:06:33 +0000279 decdatacur, self.eof = binascii.a2b_hqx(data)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000280 break
281 except binascii.Incomplete:
282 pass
283 newdata = self.ifp.read(1)
284 if not newdata:
Collin Winter115cde32007-08-28 06:06:33 +0000285 raise Error('Premature EOF on binhex file')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000286 data = data + newdata
287 decdata = decdata + decdatacur
288 wtd = totalwtd - len(decdata)
289 if not decdata and not self.eof:
Collin Winter115cde32007-08-28 06:06:33 +0000290 raise Error('Premature EOF on binhex file')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000291 return decdata
Jack Jansenfcdffea1995-08-07 14:36:51 +0000292
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000293 def close(self):
294 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000295
296class _Rledecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000297 """Read data via the RLE-coder"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000298
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000299 def __init__(self, ifp):
300 self.ifp = ifp
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000301 self.pre_buffer = b''
302 self.post_buffer = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000303 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000304
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000305 def read(self, wtd):
306 if wtd > len(self.post_buffer):
Collin Winter115cde32007-08-28 06:06:33 +0000307 self._fill(wtd - len(self.post_buffer))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000308 rv = self.post_buffer[:wtd]
309 self.post_buffer = self.post_buffer[wtd:]
310 return rv
Jack Jansenfcdffea1995-08-07 14:36:51 +0000311
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000312 def _fill(self, wtd):
Collin Winter115cde32007-08-28 06:06:33 +0000313 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000314 if self.ifp.eof:
315 self.post_buffer = self.post_buffer + \
316 binascii.rledecode_hqx(self.pre_buffer)
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000317 self.pre_buffer = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000318 return
Tim Peters11cf6052001-01-14 21:54:20 +0000319
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000320 #
321 # Obfuscated code ahead. We have to take care that we don't
322 # end up with an orphaned RUNCHAR later on. So, we keep a couple
323 # of bytes in the buffer, depending on what the end of
324 # the buffer looks like:
325 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
326 # '?\220' - Keep 2 bytes: repeated something-else
327 # '\220\0' - Escaped \220: Keep 2 bytes.
328 # '?\220?' - Complete repeat sequence: decode all
329 # otherwise: keep 1 byte.
330 #
331 mark = len(self.pre_buffer)
Jeremy Hyltonda3f2282007-08-29 17:26:34 +0000332 if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000333 mark = mark - 3
Antoine Pitrou54923952009-07-01 14:53:06 +0000334 elif self.pre_buffer[-1:] == RUNCHAR:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000335 mark = mark - 2
Jeremy Hyltonda3f2282007-08-29 17:26:34 +0000336 elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000337 mark = mark - 2
Antoine Pitrou54923952009-07-01 14:53:06 +0000338 elif self.pre_buffer[-2:-1] == RUNCHAR:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000339 pass # Decode all
340 else:
341 mark = mark - 1
Guido van Rossum50b82e81997-02-11 16:39:31 +0000342
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000343 self.post_buffer = self.post_buffer + \
344 binascii.rledecode_hqx(self.pre_buffer[:mark])
345 self.pre_buffer = self.pre_buffer[mark:]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000346
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000347 def close(self):
348 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000349
350class HexBin:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000351 def __init__(self, ifp):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000352 if isinstance(ifp, str):
Guido van Rossum34a042d2007-05-24 22:05:19 +0000353 ifp = io.open(ifp, 'rb')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000354 #
355 # Find initial colon.
356 #
Collin Winter115cde32007-08-28 06:06:33 +0000357 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000358 ch = ifp.read(1)
359 if not ch:
Collin Winter115cde32007-08-28 06:06:33 +0000360 raise Error("No binhex data found")
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000361 # Cater for \r\n terminated lines (which show up as \n\r, hence
362 # all lines start with \r)
Guido van Rossum34a042d2007-05-24 22:05:19 +0000363 if ch == b'\r':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000364 continue
Guido van Rossum34a042d2007-05-24 22:05:19 +0000365 if ch == b':':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000366 break
Tim Peters11cf6052001-01-14 21:54:20 +0000367
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000368 hqxifp = _Hqxdecoderengine(ifp)
369 self.ifp = _Rledecoderengine(hqxifp)
370 self.crc = 0
371 self._readheader()
Tim Peters11cf6052001-01-14 21:54:20 +0000372
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000373 def _read(self, len):
374 data = self.ifp.read(len)
375 self.crc = binascii.crc_hqx(data, self.crc)
376 return data
Tim Peters11cf6052001-01-14 21:54:20 +0000377
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000378 def _checkcrc(self):
379 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
380 #self.crc = binascii.crc_hqx('\0\0', self.crc)
381 # XXXX Is this needed??
382 self.crc = self.crc & 0xffff
383 if filecrc != self.crc:
Collin Winter115cde32007-08-28 06:06:33 +0000384 raise Error('CRC error, computed %x, read %x'
385 % (self.crc, filecrc))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000386 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000387
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000388 def _readheader(self):
389 len = self._read(1)
390 fname = self._read(ord(len))
Collin Winter115cde32007-08-28 06:06:33 +0000391 rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000392 self._checkcrc()
Tim Peters11cf6052001-01-14 21:54:20 +0000393
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000394 type = rest[1:5]
395 creator = rest[5:9]
396 flags = struct.unpack('>h', rest[9:11])[0]
397 self.dlen = struct.unpack('>l', rest[11:15])[0]
398 self.rlen = struct.unpack('>l', rest[15:19])[0]
Tim Peters11cf6052001-01-14 21:54:20 +0000399
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000400 self.FName = fname
401 self.FInfo = FInfo()
Neal Norwitz6ea45d32007-08-26 04:19:43 +0000402 self.FInfo.Creator = creator
403 self.FInfo.Type = type
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000404 self.FInfo.Flags = flags
Tim Peters11cf6052001-01-14 21:54:20 +0000405
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000406 self.state = _DID_HEADER
Tim Peters11cf6052001-01-14 21:54:20 +0000407
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000408 def read(self, *n):
409 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000410 raise Error('Read data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000411 if n:
412 n = n[0]
413 n = min(n, self.dlen)
414 else:
415 n = self.dlen
Guido van Rossumdcee3c02007-05-22 22:25:42 +0000416 rv = b''
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000417 while len(rv) < n:
418 rv = rv + self._read(n-len(rv))
419 self.dlen = self.dlen - n
420 return rv
Tim Peters11cf6052001-01-14 21:54:20 +0000421
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000422 def close_data(self):
423 if self.state != _DID_HEADER:
Collin Winter115cde32007-08-28 06:06:33 +0000424 raise Error('close_data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000425 if self.dlen:
426 dummy = self._read(self.dlen)
427 self._checkcrc()
428 self.state = _DID_DATA
Tim Peters11cf6052001-01-14 21:54:20 +0000429
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000430 def read_rsrc(self, *n):
431 if self.state == _DID_HEADER:
432 self.close_data()
433 if self.state != _DID_DATA:
Collin Winter115cde32007-08-28 06:06:33 +0000434 raise Error('Read resource data at wrong time')
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000435 if n:
436 n = n[0]
437 n = min(n, self.rlen)
438 else:
439 n = self.rlen
440 self.rlen = self.rlen - n
441 return self._read(n)
Tim Peters11cf6052001-01-14 21:54:20 +0000442
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000443 def close(self):
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300444 if self.state is None:
445 return
446 try:
447 if self.rlen:
448 dummy = self.read_rsrc(self.rlen)
449 self._checkcrc()
450 finally:
451 self.state = None
452 self.ifp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000453
Jack Jansenfcdffea1995-08-07 14:36:51 +0000454def hexbin(inp, out):
Collin Winter115cde32007-08-28 06:06:33 +0000455 """hexbin(infilename, outfilename) - Decode binhexed file"""
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000456 ifp = HexBin(inp)
457 finfo = ifp.FInfo
458 if not out:
459 out = ifp.FName
Jack Jansenfcdffea1995-08-07 14:36:51 +0000460
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300461 with io.open(out, 'wb') as ofp:
462 # XXXX Do translation on non-mac systems
463 while True:
464 d = ifp.read(128000)
465 if not d: break
466 ofp.write(d)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000467 ifp.close_data()
Tim Peters11cf6052001-01-14 21:54:20 +0000468
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000469 d = ifp.read_rsrc(128000)
470 if d:
471 ofp = openrsrc(out, 'wb')
472 ofp.write(d)
Collin Winter115cde32007-08-28 06:06:33 +0000473 while True:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000474 d = ifp.read_rsrc(128000)
475 if not d: break
476 ofp.write(d)
477 ofp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000478
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000479 ifp.close()