blob: 9059880e24e5aac9f345d7adde45074f1a9b6fad [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#
24import sys
25import 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)
35[_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
36
37# Various constants
Guido van Rossum4acc25b2000-02-02 15:10:15 +000038REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder
Guido van Rossuma220e671996-03-23 19:19:04 +000039LINELEN=64
Guido van Rossum4acc25b2000-02-02 15:10:15 +000040RUNCHAR=chr(0x90) # run-length introducer
Jack Jansenfcdffea1995-08-07 14:36:51 +000041
42#
Roger E. Masse469848a1997-01-16 16:51:57 +000043# This code is no longer byte-order dependent
Jack Jansenfcdffea1995-08-07 14:36:51 +000044
45#
46# Workarounds for non-mac machines.
Bob Ippolito5ea4bf12006-07-15 16:53:15 +000047try:
48 from Carbon.File import FSSpec, FInfo
49 from MacOS import openrf
Tim Peters11cf6052001-01-14 21:54:20 +000050
Guido van Rossum4acc25b2000-02-02 15:10:15 +000051 def getfileinfo(name):
Bob Ippolito5ea4bf12006-07-15 16:53:15 +000052 finfo = FSSpec(name).FSpGetFInfo()
Guido van Rossum4acc25b2000-02-02 15:10:15 +000053 dir, file = os.path.split(name)
Bob Ippolito5ea4bf12006-07-15 16:53:15 +000054 # XXX Get resource/data sizes
Guido van Rossum4acc25b2000-02-02 15:10:15 +000055 fp = open(name, 'rb')
56 fp.seek(0, 2)
57 dlen = fp.tell()
58 fp = openrf(name, '*rb')
59 fp.seek(0, 2)
60 rlen = fp.tell()
61 return file, finfo, dlen, rlen
Tim Peters11cf6052001-01-14 21:54:20 +000062
Guido van Rossum4acc25b2000-02-02 15:10:15 +000063 def openrsrc(name, *mode):
64 if not mode:
65 mode = '*rb'
66 else:
67 mode = '*' + mode[0]
68 return openrf(name, mode)
Jack Jansenfcdffea1995-08-07 14:36:51 +000069
Bob Ippolito5ea4bf12006-07-15 16:53:15 +000070except ImportError:
Guido van Rossum4acc25b2000-02-02 15:10:15 +000071 #
Thomas Wouters7e474022000-07-16 12:04:32 +000072 # Glue code for non-macintosh usage
Guido van Rossum4acc25b2000-02-02 15:10:15 +000073 #
Tim Peters11cf6052001-01-14 21:54:20 +000074
Guido van Rossum4acc25b2000-02-02 15:10:15 +000075 class FInfo:
76 def __init__(self):
77 self.Type = '????'
78 self.Creator = '????'
79 self.Flags = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +000080
Guido van Rossum4acc25b2000-02-02 15:10:15 +000081 def getfileinfo(name):
82 finfo = FInfo()
83 # Quick check for textfile
84 fp = open(name)
85 data = open(name).read(256)
86 for c in data:
Guido van Rossumb363c1f2001-08-01 18:17:23 +000087 if not c.isspace() and (c<' ' or ord(c) > 0x7f):
Guido van Rossum4acc25b2000-02-02 15:10:15 +000088 break
89 else:
90 finfo.Type = 'TEXT'
91 fp.seek(0, 2)
92 dsize = fp.tell()
93 fp.close()
94 dir, file = os.path.split(name)
Eric S. Raymondb49f4a42001-02-09 05:07:04 +000095 file = file.replace(':', '-', 1)
Guido van Rossum4acc25b2000-02-02 15:10:15 +000096 return file, finfo, dsize, 0
Jack Jansenfcdffea1995-08-07 14:36:51 +000097
Guido van Rossum4acc25b2000-02-02 15:10:15 +000098 class openrsrc:
99 def __init__(self, *args):
100 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000101
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000102 def read(self, *args):
103 return ''
Tim Peters11cf6052001-01-14 21:54:20 +0000104
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000105 def write(self, *args):
106 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000107
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000108 def close(self):
109 pass
Tim Peters11cf6052001-01-14 21:54:20 +0000110
Jack Jansenfcdffea1995-08-07 14:36:51 +0000111class _Hqxcoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000112 """Write data to the coder in 3-byte chunks"""
Tim Peters11cf6052001-01-14 21:54:20 +0000113
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000114 def __init__(self, ofp):
115 self.ofp = ofp
116 self.data = ''
117 self.hqxdata = ''
118 self.linelen = LINELEN-1
Jack Jansenfcdffea1995-08-07 14:36:51 +0000119
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000120 def write(self, data):
121 self.data = self.data + data
122 datalen = len(self.data)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000123 todo = (datalen//3)*3
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000124 data = self.data[:todo]
125 self.data = self.data[todo:]
126 if not data:
127 return
128 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
129 self._flush(0)
Guido van Rossumcce074e1996-03-25 18:54:33 +0000130
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000131 def _flush(self, force):
132 first = 0
133 while first <= len(self.hqxdata)-self.linelen:
134 last = first + self.linelen
135 self.ofp.write(self.hqxdata[first:last]+'\n')
136 self.linelen = LINELEN
137 first = last
138 self.hqxdata = self.hqxdata[first:]
139 if force:
140 self.ofp.write(self.hqxdata + ':\n')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000141
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000142 def close(self):
143 if self.data:
144 self.hqxdata = \
145 self.hqxdata + binascii.b2a_hqx(self.data)
146 self._flush(1)
147 self.ofp.close()
148 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000149
150class _Rlecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000151 """Write data to the RLE-coder in suitably large chunks"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000152
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000153 def __init__(self, ofp):
154 self.ofp = ofp
155 self.data = ''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000156
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000157 def write(self, data):
158 self.data = self.data + data
159 if len(self.data) < REASONABLY_LARGE:
160 return
161 rledata = binascii.rlecode_hqx(self.data)
162 self.ofp.write(rledata)
163 self.data = ''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000164
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000165 def close(self):
166 if self.data:
167 rledata = binascii.rlecode_hqx(self.data)
168 self.ofp.write(rledata)
169 self.ofp.close()
170 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000171
172class BinHex:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000173 def __init__(self, (name, finfo, dlen, rlen), ofp):
174 if type(ofp) == type(''):
175 ofname = ofp
176 ofp = open(ofname, 'w')
177 if os.name == 'mac':
Bob Ippolito5ea4bf12006-07-15 16:53:15 +0000178 fss = FSSpec(ofname)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000179 fss.SetCreatorType('BnHq', 'TEXT')
180 ofp.write('(This file must be converted with BinHex 4.0)\n\n:')
181 hqxer = _Hqxcoderengine(ofp)
182 self.ofp = _Rlecoderengine(hqxer)
183 self.crc = 0
Fred Drake8152d322000-12-12 23:20:45 +0000184 if finfo is None:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000185 finfo = FInfo()
186 self.dlen = dlen
187 self.rlen = rlen
188 self._writeinfo(name, finfo)
189 self.state = _DID_HEADER
Jack Jansenfcdffea1995-08-07 14:36:51 +0000190
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000191 def _writeinfo(self, name, finfo):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000192 nl = len(name)
193 if nl > 63:
194 raise Error, 'Filename too long'
195 d = chr(nl) + name + '\0'
196 d2 = finfo.Type + finfo.Creator
Roger E. Masse469848a1997-01-16 16:51:57 +0000197
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000198 # Force all structs to be packed with big-endian
199 d3 = struct.pack('>h', finfo.Flags)
200 d4 = struct.pack('>ii', self.dlen, self.rlen)
201 info = d + d2 + d3 + d4
202 self._write(info)
203 self._writecrc()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000204
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000205 def _write(self, data):
206 self.crc = binascii.crc_hqx(data, self.crc)
207 self.ofp.write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000208
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000209 def _writecrc(self):
210 # XXXX Should this be here??
211 # self.crc = binascii.crc_hqx('\0\0', self.crc)
Bob Ippolito910a08f2006-05-26 12:52:53 +0000212 if self.crc < 0:
213 fmt = '>h'
214 else:
215 fmt = '>H'
216 self.ofp.write(struct.pack(fmt, self.crc))
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000217 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000218
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000219 def write(self, data):
220 if self.state != _DID_HEADER:
221 raise Error, 'Writing data at the wrong time'
222 self.dlen = self.dlen - len(data)
223 self._write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000224
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000225 def close_data(self):
Fred Drake8152d322000-12-12 23:20:45 +0000226 if self.dlen != 0:
Walter Dörwald70a6b492004-02-12 17:35:32 +0000227 raise Error, 'Incorrect data size, diff=%r' % (self.rlen,)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000228 self._writecrc()
229 self.state = _DID_DATA
Jack Jansenfcdffea1995-08-07 14:36:51 +0000230
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000231 def write_rsrc(self, data):
232 if self.state < _DID_DATA:
233 self.close_data()
234 if self.state != _DID_DATA:
235 raise Error, 'Writing resource data at the wrong time'
236 self.rlen = self.rlen - len(data)
237 self._write(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000238
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000239 def close(self):
240 if self.state < _DID_DATA:
241 self.close_data()
242 if self.state != _DID_DATA:
243 raise Error, 'Close at the wrong time'
Fred Drake8152d322000-12-12 23:20:45 +0000244 if self.rlen != 0:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000245 raise Error, \
Walter Dörwald70a6b492004-02-12 17:35:32 +0000246 "Incorrect resource-datasize, diff=%r" % (self.rlen,)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000247 self._writecrc()
248 self.ofp.close()
249 self.state = None
250 del self.ofp
Tim Peters11cf6052001-01-14 21:54:20 +0000251
Jack Jansenfcdffea1995-08-07 14:36:51 +0000252def binhex(inp, out):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000253 """(infilename, outfilename) - Create binhex-encoded copy of a file"""
254 finfo = getfileinfo(inp)
255 ofp = BinHex(finfo, out)
Tim Peters11cf6052001-01-14 21:54:20 +0000256
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000257 ifp = open(inp, 'rb')
258 # XXXX Do textfile translation on non-mac systems
259 while 1:
260 d = ifp.read(128000)
261 if not d: break
262 ofp.write(d)
263 ofp.close_data()
264 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000265
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000266 ifp = openrsrc(inp, 'rb')
267 while 1:
268 d = ifp.read(128000)
269 if not d: break
270 ofp.write_rsrc(d)
271 ofp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000272 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000273
274class _Hqxdecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000275 """Read data via the decoder in 4-byte chunks"""
Tim Peters11cf6052001-01-14 21:54:20 +0000276
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000277 def __init__(self, ifp):
278 self.ifp = ifp
279 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000280
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000281 def read(self, totalwtd):
282 """Read at least wtd bytes (or until EOF)"""
283 decdata = ''
284 wtd = totalwtd
285 #
Tim Peters11cf6052001-01-14 21:54:20 +0000286 # The loop here is convoluted, since we don't really now how
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000287 # much to decode: there may be newlines in the incoming data.
288 while wtd > 0:
289 if self.eof: return decdata
Guido van Rossum54e54c62001-09-04 19:14:14 +0000290 wtd = ((wtd+2)//3)*4
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000291 data = self.ifp.read(wtd)
292 #
293 # Next problem: there may not be a complete number of
294 # bytes in what we pass to a2b. Solve by yet another
295 # loop.
296 #
297 while 1:
298 try:
299 decdatacur, self.eof = \
300 binascii.a2b_hqx(data)
301 break
302 except binascii.Incomplete:
303 pass
304 newdata = self.ifp.read(1)
305 if not newdata:
306 raise Error, \
307 'Premature EOF on binhex file'
308 data = data + newdata
309 decdata = decdata + decdatacur
310 wtd = totalwtd - len(decdata)
311 if not decdata and not self.eof:
312 raise Error, 'Premature EOF on binhex file'
313 return decdata
Jack Jansenfcdffea1995-08-07 14:36:51 +0000314
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000315 def close(self):
316 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000317
318class _Rledecoderengine:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000319 """Read data via the RLE-coder"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000320
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000321 def __init__(self, ifp):
322 self.ifp = ifp
323 self.pre_buffer = ''
324 self.post_buffer = ''
325 self.eof = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000326
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000327 def read(self, wtd):
328 if wtd > len(self.post_buffer):
329 self._fill(wtd-len(self.post_buffer))
330 rv = self.post_buffer[:wtd]
331 self.post_buffer = self.post_buffer[wtd:]
332 return rv
Jack Jansenfcdffea1995-08-07 14:36:51 +0000333
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000334 def _fill(self, wtd):
335 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+4)
336 if self.ifp.eof:
337 self.post_buffer = self.post_buffer + \
338 binascii.rledecode_hqx(self.pre_buffer)
339 self.pre_buffer = ''
340 return
Tim Peters11cf6052001-01-14 21:54:20 +0000341
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000342 #
343 # Obfuscated code ahead. We have to take care that we don't
344 # end up with an orphaned RUNCHAR later on. So, we keep a couple
345 # of bytes in the buffer, depending on what the end of
346 # the buffer looks like:
347 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
348 # '?\220' - Keep 2 bytes: repeated something-else
349 # '\220\0' - Escaped \220: Keep 2 bytes.
350 # '?\220?' - Complete repeat sequence: decode all
351 # otherwise: keep 1 byte.
352 #
353 mark = len(self.pre_buffer)
354 if self.pre_buffer[-3:] == RUNCHAR + '\0' + RUNCHAR:
355 mark = mark - 3
356 elif self.pre_buffer[-1] == RUNCHAR:
357 mark = mark - 2
358 elif self.pre_buffer[-2:] == RUNCHAR + '\0':
359 mark = mark - 2
360 elif self.pre_buffer[-2] == RUNCHAR:
361 pass # Decode all
362 else:
363 mark = mark - 1
Guido van Rossum50b82e81997-02-11 16:39:31 +0000364
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000365 self.post_buffer = self.post_buffer + \
366 binascii.rledecode_hqx(self.pre_buffer[:mark])
367 self.pre_buffer = self.pre_buffer[mark:]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000368
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000369 def close(self):
370 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000371
372class HexBin:
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000373 def __init__(self, ifp):
374 if type(ifp) == type(''):
375 ifp = open(ifp)
376 #
377 # Find initial colon.
378 #
379 while 1:
380 ch = ifp.read(1)
381 if not ch:
382 raise Error, "No binhex data found"
383 # Cater for \r\n terminated lines (which show up as \n\r, hence
384 # all lines start with \r)
385 if ch == '\r':
386 continue
387 if ch == ':':
388 break
389 if ch != '\n':
390 dummy = ifp.readline()
Tim Peters11cf6052001-01-14 21:54:20 +0000391
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000392 hqxifp = _Hqxdecoderengine(ifp)
393 self.ifp = _Rledecoderengine(hqxifp)
394 self.crc = 0
395 self._readheader()
Tim Peters11cf6052001-01-14 21:54:20 +0000396
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000397 def _read(self, len):
398 data = self.ifp.read(len)
399 self.crc = binascii.crc_hqx(data, self.crc)
400 return data
Tim Peters11cf6052001-01-14 21:54:20 +0000401
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000402 def _checkcrc(self):
403 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
404 #self.crc = binascii.crc_hqx('\0\0', self.crc)
405 # XXXX Is this needed??
406 self.crc = self.crc & 0xffff
407 if filecrc != self.crc:
408 raise Error, 'CRC error, computed %x, read %x' \
409 %(self.crc, filecrc)
410 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000411
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000412 def _readheader(self):
413 len = self._read(1)
414 fname = self._read(ord(len))
415 rest = self._read(1+4+4+2+4+4)
416 self._checkcrc()
Tim Peters11cf6052001-01-14 21:54:20 +0000417
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000418 type = rest[1:5]
419 creator = rest[5:9]
420 flags = struct.unpack('>h', rest[9:11])[0]
421 self.dlen = struct.unpack('>l', rest[11:15])[0]
422 self.rlen = struct.unpack('>l', rest[15:19])[0]
Tim Peters11cf6052001-01-14 21:54:20 +0000423
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000424 self.FName = fname
425 self.FInfo = FInfo()
426 self.FInfo.Creator = creator
427 self.FInfo.Type = type
428 self.FInfo.Flags = flags
Tim Peters11cf6052001-01-14 21:54:20 +0000429
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000430 self.state = _DID_HEADER
Tim Peters11cf6052001-01-14 21:54:20 +0000431
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000432 def read(self, *n):
433 if self.state != _DID_HEADER:
434 raise Error, 'Read data at wrong time'
435 if n:
436 n = n[0]
437 n = min(n, self.dlen)
438 else:
439 n = self.dlen
440 rv = ''
441 while len(rv) < n:
442 rv = rv + self._read(n-len(rv))
443 self.dlen = self.dlen - n
444 return rv
Tim Peters11cf6052001-01-14 21:54:20 +0000445
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000446 def close_data(self):
447 if self.state != _DID_HEADER:
448 raise Error, 'close_data at wrong time'
449 if self.dlen:
450 dummy = self._read(self.dlen)
451 self._checkcrc()
452 self.state = _DID_DATA
Tim Peters11cf6052001-01-14 21:54:20 +0000453
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000454 def read_rsrc(self, *n):
455 if self.state == _DID_HEADER:
456 self.close_data()
457 if self.state != _DID_DATA:
458 raise Error, 'Read resource data at wrong time'
459 if n:
460 n = n[0]
461 n = min(n, self.rlen)
462 else:
463 n = self.rlen
464 self.rlen = self.rlen - n
465 return self._read(n)
Tim Peters11cf6052001-01-14 21:54:20 +0000466
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000467 def close(self):
468 if self.rlen:
469 dummy = self.read_rsrc(self.rlen)
470 self._checkcrc()
471 self.state = _DID_RSRC
472 self.ifp.close()
Tim Peters11cf6052001-01-14 21:54:20 +0000473
Jack Jansenfcdffea1995-08-07 14:36:51 +0000474def hexbin(inp, out):
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000475 """(infilename, outfilename) - Decode binhexed file"""
476 ifp = HexBin(inp)
477 finfo = ifp.FInfo
478 if not out:
479 out = ifp.FName
480 if os.name == 'mac':
Bob Ippolito5ea4bf12006-07-15 16:53:15 +0000481 ofss = FSSpec(out)
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000482 out = ofss.as_pathname()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000483
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000484 ofp = open(out, 'wb')
485 # XXXX Do translation on non-mac systems
486 while 1:
487 d = ifp.read(128000)
488 if not d: break
489 ofp.write(d)
490 ofp.close()
491 ifp.close_data()
Tim Peters11cf6052001-01-14 21:54:20 +0000492
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000493 d = ifp.read_rsrc(128000)
494 if d:
495 ofp = openrsrc(out, 'wb')
496 ofp.write(d)
497 while 1:
498 d = ifp.read_rsrc(128000)
499 if not d: break
500 ofp.write(d)
501 ofp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000502
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000503 if os.name == 'mac':
504 nfinfo = ofss.GetFInfo()
505 nfinfo.Creator = finfo.Creator
506 nfinfo.Type = finfo.Type
507 nfinfo.Flags = finfo.Flags
508 ofss.SetFInfo(nfinfo)
Tim Peters11cf6052001-01-14 21:54:20 +0000509
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000510 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000511
512def _test():
Brett Cannon5e263512007-05-20 23:17:38 +0000513 fname = sys.argv[1]
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000514 binhex(fname, fname+'.hqx')
515 hexbin(fname+'.hqx', fname+'.viahqx')
516 #hexbin(fname, fname+'.unpacked')
517 sys.exit(1)
Tim Peters11cf6052001-01-14 21:54:20 +0000518
Jack Jansenfcdffea1995-08-07 14:36:51 +0000519if __name__ == '__main__':
Guido van Rossum4acc25b2000-02-02 15:10:15 +0000520 _test()