blob: 469344330757711089f6e3651fbc0b52cdef1e56 [file] [log] [blame]
Jack Jansenfcdffea1995-08-07 14:36:51 +00001"""binhex - Macintosh binhex compression/decompression
2easy interface:
3binhex(inputfilename, outputfilename)
4hexbin(inputfilename, outputfilename)
5"""
6
7#
8# Jack Jansen, CWI, August 1995.
9#
10# The module is supposed to be as compatible as possible. Especially the
11# easy interface should work "as expected" on any platform.
12# XXXX Note: currently, textfiles appear in mac-form on all platforms.
13# We seem to lack a simple character-translate in python.
14# (we should probably use ISO-Latin-1 on all but the mac platform).
15# XXXX The simeple routines are too simple: they expect to hold the complete
16# files in-core. Should be fixed.
Roger E. Masse469848a1997-01-16 16:51:57 +000017# XXXX It would be nice to handle AppleDouble format on unix
18# (for servers serving macs).
Jack Jansenfcdffea1995-08-07 14:36:51 +000019# XXXX I don't understand what happens when you get 0x90 times the same byte on
20# input. The resulting code (xx 90 90) would appear to be interpreted as an
21# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
22#
23import sys
24import os
25import struct
26import string
27import binascii
Jack Jansen479c1b31995-08-14 12:41:20 +000028
Jack Jansenfcdffea1995-08-07 14:36:51 +000029Error = 'binhex.Error'
30
31# States (what have we written)
32[_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
33
34# Various constants
35REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder
Guido van Rossuma220e671996-03-23 19:19:04 +000036LINELEN=64
Jack Jansenfcdffea1995-08-07 14:36:51 +000037RUNCHAR=chr(0x90) # run-length introducer
38
39#
Roger E. Masse469848a1997-01-16 16:51:57 +000040# This code is no longer byte-order dependent
Jack Jansenfcdffea1995-08-07 14:36:51 +000041
42#
43# Workarounds for non-mac machines.
44if os.name == 'mac':
Guido van Rossuma220e671996-03-23 19:19:04 +000045 import macfs
46 import MacOS
47 try:
48 openrf = MacOS.openrf
49 except AttributeError:
50 # Backward compatability
51 openrf = open
52
53 def FInfo():
Jack Jansenfcdffea1995-08-07 14:36:51 +000054 return macfs.FInfo()
Guido van Rossuma220e671996-03-23 19:19:04 +000055
56 def getfileinfo(name):
Jack Jansenfcdffea1995-08-07 14:36:51 +000057 finfo = macfs.FSSpec(name).GetFInfo()
58 dir, file = os.path.split(name)
59 # XXXX Get resource/data sizes
60 fp = open(name, 'rb')
61 fp.seek(0, 2)
62 dlen = fp.tell()
Guido van Rossuma220e671996-03-23 19:19:04 +000063 fp = openrf(name, '*rb')
Jack Jansenfcdffea1995-08-07 14:36:51 +000064 fp.seek(0, 2)
65 rlen = fp.tell()
66 return file, finfo, dlen, rlen
Guido van Rossuma220e671996-03-23 19:19:04 +000067
68 def openrsrc(name, *mode):
69 if not mode:
70 mode = '*rb'
Jack Jansenfcdffea1995-08-07 14:36:51 +000071 else:
Guido van Rossuma220e671996-03-23 19:19:04 +000072 mode = '*' + mode[0]
73 return openrf(name, mode)
Jack Jansenfcdffea1995-08-07 14:36:51 +000074
75else:
Guido van Rossuma220e671996-03-23 19:19:04 +000076 #
77 # Glue code for non-macintosh useage
78 #
Guido van Rossuma220e671996-03-23 19:19:04 +000079
80 class FInfo:
Jack Jansenfcdffea1995-08-07 14:36:51 +000081 def __init__(self):
Guido van Rossuma220e671996-03-23 19:19:04 +000082 self.Type = '????'
83 self.Creator = '????'
84 self.Flags = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +000085
Guido van Rossuma220e671996-03-23 19:19:04 +000086 def getfileinfo(name):
Jack Jansenfcdffea1995-08-07 14:36:51 +000087 finfo = FInfo()
88 # Quick check for textfile
89 fp = open(name)
90 data = open(name).read(256)
91 for c in data:
Roger E. Masse469848a1997-01-16 16:51:57 +000092 if not c in string.whitespace \
93 and (c<' ' or ord(c) > 0177):
94 break
Guido van Rossum50b82e81997-02-11 16:39:31 +000095 else:
Guido van Rossuma220e671996-03-23 19:19:04 +000096 finfo.Type = 'TEXT'
Jack Jansenfcdffea1995-08-07 14:36:51 +000097 fp.seek(0, 2)
98 dsize = fp.tell()
99 fp.close()
100 dir, file = os.path.split(name)
Guido van Rossum00f9fea1997-12-24 21:18:41 +0000101 file = string.replace(file, ':', '-', 1)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000102 return file, finfo, dsize, 0
103
Guido van Rossuma220e671996-03-23 19:19:04 +0000104 class openrsrc:
Jack Jansenfcdffea1995-08-07 14:36:51 +0000105 def __init__(self, *args):
Guido van Rossuma220e671996-03-23 19:19:04 +0000106 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +0000107
108 def read(self, *args):
Guido van Rossuma220e671996-03-23 19:19:04 +0000109 return ''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000110
111 def write(self, *args):
112 pass
113
114 def close(self):
Guido van Rossuma220e671996-03-23 19:19:04 +0000115 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +0000116
117class _Hqxcoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000118 """Write data to the coder in 3-byte chunks"""
119
120 def __init__(self, ofp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000121 self.ofp = ofp
122 self.data = ''
Guido van Rossuma220e671996-03-23 19:19:04 +0000123 self.hqxdata = ''
124 self.linelen = LINELEN-1
Jack Jansenfcdffea1995-08-07 14:36:51 +0000125
Guido van Rossuma220e671996-03-23 19:19:04 +0000126 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000127 self.data = self.data + data
Guido van Rossuma220e671996-03-23 19:19:04 +0000128 datalen = len(self.data)
129 todo = (datalen/3)*3
130 data = self.data[:todo]
131 self.data = self.data[todo:]
Guido van Rossumcce074e1996-03-25 18:54:33 +0000132 if not data:
133 return
Guido van Rossuma220e671996-03-23 19:19:04 +0000134 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
Guido van Rossumcce074e1996-03-25 18:54:33 +0000135 self._flush(0)
136
137 def _flush(self, force):
138 first = 0
139 while first <= len(self.hqxdata)-self.linelen:
140 last = first + self.linelen
141 self.ofp.write(self.hqxdata[first:last]+'\n')
Guido van Rossuma220e671996-03-23 19:19:04 +0000142 self.linelen = LINELEN
Guido van Rossumcce074e1996-03-25 18:54:33 +0000143 first = last
144 self.hqxdata = self.hqxdata[first:]
145 if force:
146 self.ofp.write(self.hqxdata + ':\n')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000147
Guido van Rossuma220e671996-03-23 19:19:04 +0000148 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000149 if self.data:
Roger E. Masse469848a1997-01-16 16:51:57 +0000150 self.hqxdata = \
151 self.hqxdata + binascii.b2a_hqx(self.data)
Guido van Rossumcce074e1996-03-25 18:54:33 +0000152 self._flush(1)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000153 self.ofp.close()
Guido van Rossuma220e671996-03-23 19:19:04 +0000154 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000155
156class _Rlecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000157 """Write data to the RLE-coder in suitably large chunks"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000158
Guido van Rossuma220e671996-03-23 19:19:04 +0000159 def __init__(self, ofp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000160 self.ofp = ofp
161 self.data = ''
162
Guido van Rossuma220e671996-03-23 19:19:04 +0000163 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000164 self.data = self.data + data
165 if len(self.data) < REASONABLY_LARGE:
Guido van Rossuma220e671996-03-23 19:19:04 +0000166 return
Jack Jansenfcdffea1995-08-07 14:36:51 +0000167 rledata = binascii.rlecode_hqx(self.data)
168 self.ofp.write(rledata)
169 self.data = ''
170
Guido van Rossuma220e671996-03-23 19:19:04 +0000171 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000172 if self.data:
Guido van Rossuma220e671996-03-23 19:19:04 +0000173 rledata = binascii.rlecode_hqx(self.data)
174 self.ofp.write(rledata)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000175 self.ofp.close()
Guido van Rossuma220e671996-03-23 19:19:04 +0000176 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000177
178class BinHex:
Guido van Rossuma220e671996-03-23 19:19:04 +0000179 def __init__(self, (name, finfo, dlen, rlen), ofp):
180 if type(ofp) == type(''):
181 ofname = ofp
182 ofp = open(ofname, 'w')
183 if os.name == 'mac':
184 fss = macfs.FSSpec(ofname)
185 fss.SetCreatorType('BnHq', 'TEXT')
186 ofp.write('(This file must be converted with BinHex 4.0)\n\n:')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000187 hqxer = _Hqxcoderengine(ofp)
188 self.ofp = _Rlecoderengine(hqxer)
189 self.crc = 0
190 if finfo == None:
Guido van Rossuma220e671996-03-23 19:19:04 +0000191 finfo = FInfo()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000192 self.dlen = dlen
193 self.rlen = rlen
194 self._writeinfo(name, finfo)
195 self.state = _DID_HEADER
196
Guido van Rossuma220e671996-03-23 19:19:04 +0000197 def _writeinfo(self, name, finfo):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000198 name = name
199 nl = len(name)
200 if nl > 63:
Guido van Rossuma220e671996-03-23 19:19:04 +0000201 raise Error, 'Filename too long'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000202 d = chr(nl) + name + '\0'
203 d2 = finfo.Type + finfo.Creator
Roger E. Masse469848a1997-01-16 16:51:57 +0000204
205 # Force all structs to be packed with big-endian
206 d3 = struct.pack('>h', finfo.Flags)
207 d4 = struct.pack('>ii', self.dlen, self.rlen)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000208 info = d + d2 + d3 + d4
209 self._write(info)
210 self._writecrc()
211
Guido van Rossuma220e671996-03-23 19:19:04 +0000212 def _write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000213 self.crc = binascii.crc_hqx(data, self.crc)
214 self.ofp.write(data)
215
Guido van Rossuma220e671996-03-23 19:19:04 +0000216 def _writecrc(self):
Roger E. Masse469848a1997-01-16 16:51:57 +0000217 # XXXX Should this be here??
218 # self.crc = binascii.crc_hqx('\0\0', self.crc)
219 self.ofp.write(struct.pack('>h', self.crc))
220 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000221
Guido van Rossuma220e671996-03-23 19:19:04 +0000222 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000223 if self.state != _DID_HEADER:
Guido van Rossuma220e671996-03-23 19:19:04 +0000224 raise Error, 'Writing data at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000225 self.dlen = self.dlen - len(data)
226 self._write(data)
227
Guido van Rossuma220e671996-03-23 19:19:04 +0000228 def close_data(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000229 if self.dlen <> 0:
Guido van Rossuma220e671996-03-23 19:19:04 +0000230 raise Error, 'Incorrect data size, diff='+`self.rlen`
Jack Jansenfcdffea1995-08-07 14:36:51 +0000231 self._writecrc()
232 self.state = _DID_DATA
233
Guido van Rossuma220e671996-03-23 19:19:04 +0000234 def write_rsrc(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000235 if self.state < _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000236 self.close_data()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000237 if self.state != _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000238 raise Error, 'Writing resource data at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000239 self.rlen = self.rlen - len(data)
240 self._write(data)
241
Guido van Rossuma220e671996-03-23 19:19:04 +0000242 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000243 if self.state < _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000244 self.close_data()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000245 if self.state != _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000246 raise Error, 'Close at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000247 if self.rlen <> 0:
Roger E. Masse469848a1997-01-16 16:51:57 +0000248 raise Error, \
249 "Incorrect resource-datasize, diff="+`self.rlen`
Jack Jansenfcdffea1995-08-07 14:36:51 +0000250 self._writecrc()
251 self.ofp.close()
252 self.state = None
Guido van Rossuma220e671996-03-23 19:19:04 +0000253 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000254
255def binhex(inp, out):
256 """(infilename, outfilename) - Create binhex-encoded copy of a file"""
257 finfo = getfileinfo(inp)
258 ofp = BinHex(finfo, out)
259
260 ifp = open(inp, 'rb')
261 # XXXX Do textfile translation on non-mac systems
Guido van Rossuma220e671996-03-23 19:19:04 +0000262 while 1:
263 d = ifp.read(128000)
264 if not d: break
265 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000266 ofp.close_data()
267 ifp.close()
268
269 ifp = openrsrc(inp, 'rb')
Guido van Rossuma220e671996-03-23 19:19:04 +0000270 while 1:
271 d = ifp.read(128000)
272 if not d: break
273 ofp.write_rsrc(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000274 ofp.close()
Guido van Rossuma220e671996-03-23 19:19:04 +0000275 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000276
277class _Hqxdecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000278 """Read data via the decoder in 4-byte chunks"""
279
280 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000281 self.ifp = ifp
282 self.eof = 0
283
Guido van Rossuma220e671996-03-23 19:19:04 +0000284 def read(self, totalwtd):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000285 """Read at least wtd bytes (or until EOF)"""
286 decdata = ''
Jack Jansen685e16d1995-10-03 14:41:15 +0000287 wtd = totalwtd
Jack Jansenfcdffea1995-08-07 14:36:51 +0000288 #
Roger E. Masse469848a1997-01-16 16:51:57 +0000289 # The loop here is convoluted, since we don't really now how
290 # much to decode: there may be newlines in the incoming data.
Jack Jansenfcdffea1995-08-07 14:36:51 +0000291 while wtd > 0:
292 if self.eof: return decdata
293 wtd = ((wtd+2)/3)*4
294 data = self.ifp.read(wtd)
295 #
Roger E. Masse469848a1997-01-16 16:51:57 +0000296 # Next problem: there may not be a complete number of
297 # bytes in what we pass to a2b. Solve by yet another
298 # loop.
Jack Jansenfcdffea1995-08-07 14:36:51 +0000299 #
300 while 1:
301 try:
Roger E. Masse469848a1997-01-16 16:51:57 +0000302 decdatacur, self.eof = \
303 binascii.a2b_hqx(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000304 break
305 except binascii.Incomplete:
306 pass
307 newdata = self.ifp.read(1)
308 if not newdata:
Roger E. Masse469848a1997-01-16 16:51:57 +0000309 raise Error, \
310 'Premature EOF on binhex file'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000311 data = data + newdata
312 decdata = decdata + decdatacur
Jack Jansen685e16d1995-10-03 14:41:15 +0000313 wtd = totalwtd - len(decdata)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000314 if not decdata and not self.eof:
315 raise Error, 'Premature EOF on binhex file'
316 return decdata
317
Guido van Rossuma220e671996-03-23 19:19:04 +0000318 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000319 self.ifp.close()
320
321class _Rledecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000322 """Read data via the RLE-coder"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000323
Guido van Rossuma220e671996-03-23 19:19:04 +0000324 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000325 self.ifp = ifp
326 self.pre_buffer = ''
327 self.post_buffer = ''
328 self.eof = 0
329
Guido van Rossuma220e671996-03-23 19:19:04 +0000330 def read(self, wtd):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000331 if wtd > len(self.post_buffer):
Guido van Rossuma220e671996-03-23 19:19:04 +0000332 self._fill(wtd-len(self.post_buffer))
Jack Jansenfcdffea1995-08-07 14:36:51 +0000333 rv = self.post_buffer[:wtd]
334 self.post_buffer = self.post_buffer[wtd:]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000335 return rv
336
Guido van Rossuma220e671996-03-23 19:19:04 +0000337 def _fill(self, wtd):
Guido van Rossum50b82e81997-02-11 16:39:31 +0000338 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+4)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000339 if self.ifp.eof:
340 self.post_buffer = self.post_buffer + \
341 binascii.rledecode_hqx(self.pre_buffer)
342 self.pre_buffer = ''
343 return
344
Guido van Rossum50b82e81997-02-11 16:39:31 +0000345 #
346 # Obfuscated code ahead. We have to take care that we don't
347 # end up with an orphaned RUNCHAR later on. So, we keep a couple
348 # of bytes in the buffer, depending on what the end of
349 # the buffer looks like:
350 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
351 # '?\220' - Keep 2 bytes: repeated something-else
352 # '\220\0' - Escaped \220: Keep 2 bytes.
353 # '?\220?' - Complete repeat sequence: decode all
354 # otherwise: keep 1 byte.
355 #
356 mark = len(self.pre_buffer)
357 if self.pre_buffer[-3:] == RUNCHAR + '\0' + RUNCHAR:
358 mark = mark - 3
359 elif self.pre_buffer[-1] == RUNCHAR:
360 mark = mark - 2
361 elif self.pre_buffer[-2:] == RUNCHAR + '\0':
362 mark = mark - 2
363 elif self.pre_buffer[-2] == RUNCHAR:
364 pass # Decode all
Jack Jansenfcdffea1995-08-07 14:36:51 +0000365 else:
Guido van Rossum50b82e81997-02-11 16:39:31 +0000366 mark = mark - 1
367
Jack Jansenfcdffea1995-08-07 14:36:51 +0000368 self.post_buffer = self.post_buffer + \
369 binascii.rledecode_hqx(self.pre_buffer[:mark])
370 self.pre_buffer = self.pre_buffer[mark:]
371
Guido van Rossuma220e671996-03-23 19:19:04 +0000372 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000373 self.ifp.close()
374
375class HexBin:
Guido van Rossuma220e671996-03-23 19:19:04 +0000376 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000377 if type(ifp) == type(''):
Guido van Rossuma220e671996-03-23 19:19:04 +0000378 ifp = open(ifp)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000379 #
380 # Find initial colon.
381 #
382 while 1:
383 ch = ifp.read(1)
384 if not ch:
385 raise Error, "No binhex data found"
Guido van Rossum50b82e81997-02-11 16:39:31 +0000386 # Cater for \r\n terminated lines (which show up as \n\r, hence
387 # all lines start with \r)
388 if ch == '\r':
389 continue
Jack Jansenfcdffea1995-08-07 14:36:51 +0000390 if ch == ':':
391 break
392 if ch != '\n':
393 dummy = ifp.readline()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000394
395 hqxifp = _Hqxdecoderengine(ifp)
396 self.ifp = _Rledecoderengine(hqxifp)
397 self.crc = 0
398 self._readheader()
399
Guido van Rossuma220e671996-03-23 19:19:04 +0000400 def _read(self, len):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000401 data = self.ifp.read(len)
402 self.crc = binascii.crc_hqx(data, self.crc)
403 return data
404
Guido van Rossuma220e671996-03-23 19:19:04 +0000405 def _checkcrc(self):
Roger E. Masse469848a1997-01-16 16:51:57 +0000406 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
407 #self.crc = binascii.crc_hqx('\0\0', self.crc)
408 # XXXX Is this needed??
Jack Jansenfcdffea1995-08-07 14:36:51 +0000409 self.crc = self.crc & 0xffff
Jack Jansen685e16d1995-10-03 14:41:15 +0000410 if filecrc != self.crc:
Roger E. Masse469848a1997-01-16 16:51:57 +0000411 raise Error, 'CRC error, computed %x, read %x' \
412 %(self.crc, filecrc)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000413 self.crc = 0
414
Guido van Rossuma220e671996-03-23 19:19:04 +0000415 def _readheader(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000416 len = self._read(1)
417 fname = self._read(ord(len))
418 rest = self._read(1+4+4+2+4+4)
419 self._checkcrc()
420
421 type = rest[1:5]
422 creator = rest[5:9]
Roger E. Masse469848a1997-01-16 16:51:57 +0000423 flags = struct.unpack('>h', rest[9:11])[0]
424 self.dlen = struct.unpack('>l', rest[11:15])[0]
425 self.rlen = struct.unpack('>l', rest[15:19])[0]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000426
Jack Jansenfcdffea1995-08-07 14:36:51 +0000427 self.FName = fname
428 self.FInfo = FInfo()
429 self.FInfo.Creator = creator
430 self.FInfo.Type = type
431 self.FInfo.Flags = flags
432
433 self.state = _DID_HEADER
434
Guido van Rossuma220e671996-03-23 19:19:04 +0000435 def read(self, *n):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000436 if self.state != _DID_HEADER:
437 raise Error, 'Read data at wrong time'
438 if n:
439 n = n[0]
440 n = min(n, self.dlen)
441 else:
442 n = self.dlen
Guido van Rossum50b82e81997-02-11 16:39:31 +0000443 rv = ''
444 while len(rv) < n:
445 rv = rv + self._read(n-len(rv))
Jack Jansenfcdffea1995-08-07 14:36:51 +0000446 self.dlen = self.dlen - n
Guido van Rossum50b82e81997-02-11 16:39:31 +0000447 return rv
Jack Jansenfcdffea1995-08-07 14:36:51 +0000448
Guido van Rossuma220e671996-03-23 19:19:04 +0000449 def close_data(self):
450 if self.state != _DID_HEADER:
451 raise Error, 'close_data at wrong time'
452 if self.dlen:
Jack Jansenfcdffea1995-08-07 14:36:51 +0000453 dummy = self._read(self.dlen)
Guido van Rossuma220e671996-03-23 19:19:04 +0000454 self._checkcrc()
455 self.state = _DID_DATA
Jack Jansenfcdffea1995-08-07 14:36:51 +0000456
Guido van Rossuma220e671996-03-23 19:19:04 +0000457 def read_rsrc(self, *n):
458 if self.state == _DID_HEADER:
459 self.close_data()
460 if self.state != _DID_DATA:
461 raise Error, 'Read resource data at wrong time'
462 if n:
463 n = n[0]
464 n = min(n, self.rlen)
465 else:
466 n = self.rlen
467 self.rlen = self.rlen - n
468 return self._read(n)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000469
Guido van Rossuma220e671996-03-23 19:19:04 +0000470 def close(self):
471 if self.rlen:
472 dummy = self.read_rsrc(self.rlen)
473 self._checkcrc()
474 self.state = _DID_RSRC
475 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000476
477def hexbin(inp, out):
478 """(infilename, outfilename) - Decode binhexed file"""
479 ifp = HexBin(inp)
480 finfo = ifp.FInfo
481 if not out:
482 out = ifp.FName
483 if os.name == 'mac':
484 ofss = macfs.FSSpec(out)
485 out = ofss.as_pathname()
486
487 ofp = open(out, 'wb')
488 # XXXX Do translation on non-mac systems
Guido van Rossuma220e671996-03-23 19:19:04 +0000489 while 1:
490 d = ifp.read(128000)
491 if not d: break
492 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000493 ofp.close()
494 ifp.close_data()
495
Guido van Rossuma220e671996-03-23 19:19:04 +0000496 d = ifp.read_rsrc(128000)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000497 if d:
498 ofp = openrsrc(out, 'wb')
499 ofp.write(d)
Guido van Rossuma220e671996-03-23 19:19:04 +0000500 while 1:
501 d = ifp.read_rsrc(128000)
502 if not d: break
503 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000504 ofp.close()
505
506 if os.name == 'mac':
507 nfinfo = ofss.GetFInfo()
508 nfinfo.Creator = finfo.Creator
509 nfinfo.Type = finfo.Type
510 nfinfo.Flags = finfo.Flags
511 ofss.SetFInfo(nfinfo)
512
513 ifp.close()
514
515def _test():
516 if os.name == 'mac':
Jack Jansen479c1b31995-08-14 12:41:20 +0000517 fss, ok = macfs.PromptGetFile('File to convert:')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000518 if not ok:
519 sys.exit(0)
520 fname = fss.as_pathname()
521 else:
522 fname = sys.argv[1]
Roger E. Masse469848a1997-01-16 16:51:57 +0000523 binhex(fname, fname+'.hqx')
524 hexbin(fname+'.hqx', fname+'.viahqx')
525 #hexbin(fname, fname+'.unpacked')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000526 sys.exit(1)
527
528if __name__ == '__main__':
529 _test()