blob: 7c22c4e0c7e1148835aadbf5ee853bcf1b526678 [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 #
79 import regsub
80
81 class FInfo:
Jack Jansenfcdffea1995-08-07 14:36:51 +000082 def __init__(self):
Guido van Rossuma220e671996-03-23 19:19:04 +000083 self.Type = '????'
84 self.Creator = '????'
85 self.Flags = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +000086
Guido van Rossuma220e671996-03-23 19:19:04 +000087 def getfileinfo(name):
Jack Jansenfcdffea1995-08-07 14:36:51 +000088 finfo = FInfo()
89 # Quick check for textfile
90 fp = open(name)
91 data = open(name).read(256)
92 for c in data:
Roger E. Masse469848a1997-01-16 16:51:57 +000093 if not c in string.whitespace \
94 and (c<' ' or ord(c) > 0177):
95 break
Guido van Rossum50b82e81997-02-11 16:39:31 +000096 else:
Guido van Rossuma220e671996-03-23 19:19:04 +000097 finfo.Type = 'TEXT'
Jack Jansenfcdffea1995-08-07 14:36:51 +000098 fp.seek(0, 2)
99 dsize = fp.tell()
100 fp.close()
101 dir, file = os.path.split(name)
102 file = regsub.sub(':', '-', file)
103 return file, finfo, dsize, 0
104
Guido van Rossuma220e671996-03-23 19:19:04 +0000105 class openrsrc:
Jack Jansenfcdffea1995-08-07 14:36:51 +0000106 def __init__(self, *args):
Guido van Rossuma220e671996-03-23 19:19:04 +0000107 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +0000108
109 def read(self, *args):
Guido van Rossuma220e671996-03-23 19:19:04 +0000110 return ''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000111
112 def write(self, *args):
113 pass
114
115 def close(self):
Guido van Rossuma220e671996-03-23 19:19:04 +0000116 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +0000117
118class _Hqxcoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000119 """Write data to the coder in 3-byte chunks"""
120
121 def __init__(self, ofp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000122 self.ofp = ofp
123 self.data = ''
Guido van Rossuma220e671996-03-23 19:19:04 +0000124 self.hqxdata = ''
125 self.linelen = LINELEN-1
Jack Jansenfcdffea1995-08-07 14:36:51 +0000126
Guido van Rossuma220e671996-03-23 19:19:04 +0000127 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000128 self.data = self.data + data
Guido van Rossuma220e671996-03-23 19:19:04 +0000129 datalen = len(self.data)
130 todo = (datalen/3)*3
131 data = self.data[:todo]
132 self.data = self.data[todo:]
Guido van Rossumcce074e1996-03-25 18:54:33 +0000133 if not data:
134 return
Guido van Rossuma220e671996-03-23 19:19:04 +0000135 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
Guido van Rossumcce074e1996-03-25 18:54:33 +0000136 self._flush(0)
137
138 def _flush(self, force):
139 first = 0
140 while first <= len(self.hqxdata)-self.linelen:
141 last = first + self.linelen
142 self.ofp.write(self.hqxdata[first:last]+'\n')
Guido van Rossuma220e671996-03-23 19:19:04 +0000143 self.linelen = LINELEN
Guido van Rossumcce074e1996-03-25 18:54:33 +0000144 first = last
145 self.hqxdata = self.hqxdata[first:]
146 if force:
147 self.ofp.write(self.hqxdata + ':\n')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000148
Guido van Rossuma220e671996-03-23 19:19:04 +0000149 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000150 if self.data:
Roger E. Masse469848a1997-01-16 16:51:57 +0000151 self.hqxdata = \
152 self.hqxdata + binascii.b2a_hqx(self.data)
Guido van Rossumcce074e1996-03-25 18:54:33 +0000153 self._flush(1)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000154 self.ofp.close()
Guido van Rossuma220e671996-03-23 19:19:04 +0000155 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000156
157class _Rlecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000158 """Write data to the RLE-coder in suitably large chunks"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000159
Guido van Rossuma220e671996-03-23 19:19:04 +0000160 def __init__(self, ofp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000161 self.ofp = ofp
162 self.data = ''
163
Guido van Rossuma220e671996-03-23 19:19:04 +0000164 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000165 self.data = self.data + data
166 if len(self.data) < REASONABLY_LARGE:
Guido van Rossuma220e671996-03-23 19:19:04 +0000167 return
Jack Jansenfcdffea1995-08-07 14:36:51 +0000168 rledata = binascii.rlecode_hqx(self.data)
169 self.ofp.write(rledata)
170 self.data = ''
171
Guido van Rossuma220e671996-03-23 19:19:04 +0000172 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000173 if self.data:
Guido van Rossuma220e671996-03-23 19:19:04 +0000174 rledata = binascii.rlecode_hqx(self.data)
175 self.ofp.write(rledata)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000176 self.ofp.close()
Guido van Rossuma220e671996-03-23 19:19:04 +0000177 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000178
179class BinHex:
Guido van Rossuma220e671996-03-23 19:19:04 +0000180 def __init__(self, (name, finfo, dlen, rlen), ofp):
181 if type(ofp) == type(''):
182 ofname = ofp
183 ofp = open(ofname, 'w')
184 if os.name == 'mac':
185 fss = macfs.FSSpec(ofname)
186 fss.SetCreatorType('BnHq', 'TEXT')
187 ofp.write('(This file must be converted with BinHex 4.0)\n\n:')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000188 hqxer = _Hqxcoderengine(ofp)
189 self.ofp = _Rlecoderengine(hqxer)
190 self.crc = 0
191 if finfo == None:
Guido van Rossuma220e671996-03-23 19:19:04 +0000192 finfo = FInfo()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000193 self.dlen = dlen
194 self.rlen = rlen
195 self._writeinfo(name, finfo)
196 self.state = _DID_HEADER
197
Guido van Rossuma220e671996-03-23 19:19:04 +0000198 def _writeinfo(self, name, finfo):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000199 name = name
200 nl = len(name)
201 if nl > 63:
Guido van Rossuma220e671996-03-23 19:19:04 +0000202 raise Error, 'Filename too long'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000203 d = chr(nl) + name + '\0'
204 d2 = finfo.Type + finfo.Creator
Roger E. Masse469848a1997-01-16 16:51:57 +0000205
206 # Force all structs to be packed with big-endian
207 d3 = struct.pack('>h', finfo.Flags)
208 d4 = struct.pack('>ii', self.dlen, self.rlen)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000209 info = d + d2 + d3 + d4
210 self._write(info)
211 self._writecrc()
212
Guido van Rossuma220e671996-03-23 19:19:04 +0000213 def _write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000214 self.crc = binascii.crc_hqx(data, self.crc)
215 self.ofp.write(data)
216
Guido van Rossuma220e671996-03-23 19:19:04 +0000217 def _writecrc(self):
Roger E. Masse469848a1997-01-16 16:51:57 +0000218 # XXXX Should this be here??
219 # self.crc = binascii.crc_hqx('\0\0', self.crc)
220 self.ofp.write(struct.pack('>h', self.crc))
221 self.crc = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +0000222
Guido van Rossuma220e671996-03-23 19:19:04 +0000223 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000224 if self.state != _DID_HEADER:
Guido van Rossuma220e671996-03-23 19:19:04 +0000225 raise Error, 'Writing data at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000226 self.dlen = self.dlen - len(data)
227 self._write(data)
228
Guido van Rossuma220e671996-03-23 19:19:04 +0000229 def close_data(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000230 if self.dlen <> 0:
Guido van Rossuma220e671996-03-23 19:19:04 +0000231 raise Error, 'Incorrect data size, diff='+`self.rlen`
Jack Jansenfcdffea1995-08-07 14:36:51 +0000232 self._writecrc()
233 self.state = _DID_DATA
234
Guido van Rossuma220e671996-03-23 19:19:04 +0000235 def write_rsrc(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000236 if self.state < _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000237 self.close_data()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000238 if self.state != _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000239 raise Error, 'Writing resource data at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000240 self.rlen = self.rlen - len(data)
241 self._write(data)
242
Guido van Rossuma220e671996-03-23 19:19:04 +0000243 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000244 if self.state < _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000245 self.close_data()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000246 if self.state != _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000247 raise Error, 'Close at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000248 if self.rlen <> 0:
Roger E. Masse469848a1997-01-16 16:51:57 +0000249 raise Error, \
250 "Incorrect resource-datasize, diff="+`self.rlen`
Jack Jansenfcdffea1995-08-07 14:36:51 +0000251 self._writecrc()
252 self.ofp.close()
253 self.state = None
Guido van Rossuma220e671996-03-23 19:19:04 +0000254 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000255
256def binhex(inp, out):
257 """(infilename, outfilename) - Create binhex-encoded copy of a file"""
258 finfo = getfileinfo(inp)
259 ofp = BinHex(finfo, out)
260
261 ifp = open(inp, 'rb')
262 # XXXX Do textfile translation on non-mac systems
Guido van Rossuma220e671996-03-23 19:19:04 +0000263 while 1:
264 d = ifp.read(128000)
265 if not d: break
266 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000267 ofp.close_data()
268 ifp.close()
269
270 ifp = openrsrc(inp, 'rb')
Guido van Rossuma220e671996-03-23 19:19:04 +0000271 while 1:
272 d = ifp.read(128000)
273 if not d: break
274 ofp.write_rsrc(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000275 ofp.close()
Guido van Rossuma220e671996-03-23 19:19:04 +0000276 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000277
278class _Hqxdecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000279 """Read data via the decoder in 4-byte chunks"""
280
281 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000282 self.ifp = ifp
283 self.eof = 0
284
Guido van Rossuma220e671996-03-23 19:19:04 +0000285 def read(self, totalwtd):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000286 """Read at least wtd bytes (or until EOF)"""
287 decdata = ''
Jack Jansen685e16d1995-10-03 14:41:15 +0000288 wtd = totalwtd
Jack Jansenfcdffea1995-08-07 14:36:51 +0000289 #
Roger E. Masse469848a1997-01-16 16:51:57 +0000290 # The loop here is convoluted, since we don't really now how
291 # much to decode: there may be newlines in the incoming data.
Jack Jansenfcdffea1995-08-07 14:36:51 +0000292 while wtd > 0:
293 if self.eof: return decdata
294 wtd = ((wtd+2)/3)*4
295 data = self.ifp.read(wtd)
296 #
Roger E. Masse469848a1997-01-16 16:51:57 +0000297 # Next problem: there may not be a complete number of
298 # bytes in what we pass to a2b. Solve by yet another
299 # loop.
Jack Jansenfcdffea1995-08-07 14:36:51 +0000300 #
301 while 1:
302 try:
Roger E. Masse469848a1997-01-16 16:51:57 +0000303 decdatacur, self.eof = \
304 binascii.a2b_hqx(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000305 break
306 except binascii.Incomplete:
307 pass
308 newdata = self.ifp.read(1)
309 if not newdata:
Roger E. Masse469848a1997-01-16 16:51:57 +0000310 raise Error, \
311 'Premature EOF on binhex file'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000312 data = data + newdata
313 decdata = decdata + decdatacur
Jack Jansen685e16d1995-10-03 14:41:15 +0000314 wtd = totalwtd - len(decdata)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000315 if not decdata and not self.eof:
316 raise Error, 'Premature EOF on binhex file'
317 return decdata
318
Guido van Rossuma220e671996-03-23 19:19:04 +0000319 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000320 self.ifp.close()
321
322class _Rledecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000323 """Read data via the RLE-coder"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000324
Guido van Rossuma220e671996-03-23 19:19:04 +0000325 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000326 self.ifp = ifp
327 self.pre_buffer = ''
328 self.post_buffer = ''
329 self.eof = 0
330
Guido van Rossuma220e671996-03-23 19:19:04 +0000331 def read(self, wtd):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000332 if wtd > len(self.post_buffer):
Guido van Rossuma220e671996-03-23 19:19:04 +0000333 self._fill(wtd-len(self.post_buffer))
Jack Jansenfcdffea1995-08-07 14:36:51 +0000334 rv = self.post_buffer[:wtd]
335 self.post_buffer = self.post_buffer[wtd:]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000336 return rv
337
Guido van Rossuma220e671996-03-23 19:19:04 +0000338 def _fill(self, wtd):
Guido van Rossum50b82e81997-02-11 16:39:31 +0000339 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+4)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000340 if self.ifp.eof:
341 self.post_buffer = self.post_buffer + \
342 binascii.rledecode_hqx(self.pre_buffer)
343 self.pre_buffer = ''
344 return
345
Guido van Rossum50b82e81997-02-11 16:39:31 +0000346 #
347 # Obfuscated code ahead. We have to take care that we don't
348 # end up with an orphaned RUNCHAR later on. So, we keep a couple
349 # of bytes in the buffer, depending on what the end of
350 # the buffer looks like:
351 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
352 # '?\220' - Keep 2 bytes: repeated something-else
353 # '\220\0' - Escaped \220: Keep 2 bytes.
354 # '?\220?' - Complete repeat sequence: decode all
355 # otherwise: keep 1 byte.
356 #
357 mark = len(self.pre_buffer)
358 if self.pre_buffer[-3:] == RUNCHAR + '\0' + RUNCHAR:
359 mark = mark - 3
360 elif self.pre_buffer[-1] == RUNCHAR:
361 mark = mark - 2
362 elif self.pre_buffer[-2:] == RUNCHAR + '\0':
363 mark = mark - 2
364 elif self.pre_buffer[-2] == RUNCHAR:
365 pass # Decode all
Jack Jansenfcdffea1995-08-07 14:36:51 +0000366 else:
Guido van Rossum50b82e81997-02-11 16:39:31 +0000367 mark = mark - 1
368
Jack Jansenfcdffea1995-08-07 14:36:51 +0000369 self.post_buffer = self.post_buffer + \
370 binascii.rledecode_hqx(self.pre_buffer[:mark])
371 self.pre_buffer = self.pre_buffer[mark:]
372
Guido van Rossuma220e671996-03-23 19:19:04 +0000373 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000374 self.ifp.close()
375
376class HexBin:
Guido van Rossuma220e671996-03-23 19:19:04 +0000377 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000378 if type(ifp) == type(''):
Guido van Rossuma220e671996-03-23 19:19:04 +0000379 ifp = open(ifp)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000380 #
381 # Find initial colon.
382 #
383 while 1:
384 ch = ifp.read(1)
385 if not ch:
386 raise Error, "No binhex data found"
Guido van Rossum50b82e81997-02-11 16:39:31 +0000387 # Cater for \r\n terminated lines (which show up as \n\r, hence
388 # all lines start with \r)
389 if ch == '\r':
390 continue
Jack Jansenfcdffea1995-08-07 14:36:51 +0000391 if ch == ':':
392 break
393 if ch != '\n':
394 dummy = ifp.readline()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000395
396 hqxifp = _Hqxdecoderengine(ifp)
397 self.ifp = _Rledecoderengine(hqxifp)
398 self.crc = 0
399 self._readheader()
400
Guido van Rossuma220e671996-03-23 19:19:04 +0000401 def _read(self, len):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000402 data = self.ifp.read(len)
403 self.crc = binascii.crc_hqx(data, self.crc)
404 return data
405
Guido van Rossuma220e671996-03-23 19:19:04 +0000406 def _checkcrc(self):
Roger E. Masse469848a1997-01-16 16:51:57 +0000407 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
408 #self.crc = binascii.crc_hqx('\0\0', self.crc)
409 # XXXX Is this needed??
Jack Jansenfcdffea1995-08-07 14:36:51 +0000410 self.crc = self.crc & 0xffff
Jack Jansen685e16d1995-10-03 14:41:15 +0000411 if filecrc != self.crc:
Roger E. Masse469848a1997-01-16 16:51:57 +0000412 raise Error, 'CRC error, computed %x, read %x' \
413 %(self.crc, filecrc)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000414 self.crc = 0
415
Guido van Rossuma220e671996-03-23 19:19:04 +0000416 def _readheader(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000417 len = self._read(1)
418 fname = self._read(ord(len))
419 rest = self._read(1+4+4+2+4+4)
420 self._checkcrc()
421
422 type = rest[1:5]
423 creator = rest[5:9]
Roger E. Masse469848a1997-01-16 16:51:57 +0000424 flags = struct.unpack('>h', rest[9:11])[0]
425 self.dlen = struct.unpack('>l', rest[11:15])[0]
426 self.rlen = struct.unpack('>l', rest[15:19])[0]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000427
Jack Jansenfcdffea1995-08-07 14:36:51 +0000428 self.FName = fname
429 self.FInfo = FInfo()
430 self.FInfo.Creator = creator
431 self.FInfo.Type = type
432 self.FInfo.Flags = flags
433
434 self.state = _DID_HEADER
435
Guido van Rossuma220e671996-03-23 19:19:04 +0000436 def read(self, *n):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000437 if self.state != _DID_HEADER:
438 raise Error, 'Read data at wrong time'
439 if n:
440 n = n[0]
441 n = min(n, self.dlen)
442 else:
443 n = self.dlen
Guido van Rossum50b82e81997-02-11 16:39:31 +0000444 rv = ''
445 while len(rv) < n:
446 rv = rv + self._read(n-len(rv))
Jack Jansenfcdffea1995-08-07 14:36:51 +0000447 self.dlen = self.dlen - n
Guido van Rossum50b82e81997-02-11 16:39:31 +0000448 return rv
Jack Jansenfcdffea1995-08-07 14:36:51 +0000449
Guido van Rossuma220e671996-03-23 19:19:04 +0000450 def close_data(self):
451 if self.state != _DID_HEADER:
452 raise Error, 'close_data at wrong time'
453 if self.dlen:
Jack Jansenfcdffea1995-08-07 14:36:51 +0000454 dummy = self._read(self.dlen)
Guido van Rossuma220e671996-03-23 19:19:04 +0000455 self._checkcrc()
456 self.state = _DID_DATA
Jack Jansenfcdffea1995-08-07 14:36:51 +0000457
Guido van Rossuma220e671996-03-23 19:19:04 +0000458 def read_rsrc(self, *n):
459 if self.state == _DID_HEADER:
460 self.close_data()
461 if self.state != _DID_DATA:
462 raise Error, 'Read resource data at wrong time'
463 if n:
464 n = n[0]
465 n = min(n, self.rlen)
466 else:
467 n = self.rlen
468 self.rlen = self.rlen - n
469 return self._read(n)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000470
Guido van Rossuma220e671996-03-23 19:19:04 +0000471 def close(self):
472 if self.rlen:
473 dummy = self.read_rsrc(self.rlen)
474 self._checkcrc()
475 self.state = _DID_RSRC
476 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000477
478def hexbin(inp, out):
479 """(infilename, outfilename) - Decode binhexed file"""
480 ifp = HexBin(inp)
481 finfo = ifp.FInfo
482 if not out:
483 out = ifp.FName
484 if os.name == 'mac':
485 ofss = macfs.FSSpec(out)
486 out = ofss.as_pathname()
487
488 ofp = open(out, 'wb')
489 # XXXX Do translation on non-mac systems
Guido van Rossuma220e671996-03-23 19:19:04 +0000490 while 1:
491 d = ifp.read(128000)
492 if not d: break
493 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000494 ofp.close()
495 ifp.close_data()
496
Guido van Rossuma220e671996-03-23 19:19:04 +0000497 d = ifp.read_rsrc(128000)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000498 if d:
499 ofp = openrsrc(out, 'wb')
500 ofp.write(d)
Guido van Rossuma220e671996-03-23 19:19:04 +0000501 while 1:
502 d = ifp.read_rsrc(128000)
503 if not d: break
504 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000505 ofp.close()
506
507 if os.name == 'mac':
508 nfinfo = ofss.GetFInfo()
509 nfinfo.Creator = finfo.Creator
510 nfinfo.Type = finfo.Type
511 nfinfo.Flags = finfo.Flags
512 ofss.SetFInfo(nfinfo)
513
514 ifp.close()
515
516def _test():
517 if os.name == 'mac':
Jack Jansen479c1b31995-08-14 12:41:20 +0000518 fss, ok = macfs.PromptGetFile('File to convert:')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000519 if not ok:
520 sys.exit(0)
521 fname = fss.as_pathname()
522 else:
523 fname = sys.argv[1]
Roger E. Masse469848a1997-01-16 16:51:57 +0000524 binhex(fname, fname+'.hqx')
525 hexbin(fname+'.hqx', fname+'.viahqx')
526 #hexbin(fname, fname+'.unpacked')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000527 sys.exit(1)
528
529if __name__ == '__main__':
530 _test()