blob: cdb961ef7c4742e987d28f2809a30f76beeb3454 [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.
17# XXXX It would be nice to handle AppleDouble format on unix (for servers serving
18# macs).
19# 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#
40# The code is currently byte-order dependent
41if struct.pack('i', 0177) != '\0\0\0\177':
Guido van Rossuma220e671996-03-23 19:19:04 +000042 raise ImportError, 'Module binhex is big-endian only'
Jack Jansenfcdffea1995-08-07 14:36:51 +000043
44#
45# Workarounds for non-mac machines.
46if os.name == 'mac':
Guido van Rossuma220e671996-03-23 19:19:04 +000047 import macfs
48 import MacOS
49 try:
50 openrf = MacOS.openrf
51 except AttributeError:
52 # Backward compatability
53 openrf = open
54
55 def FInfo():
Jack Jansenfcdffea1995-08-07 14:36:51 +000056 return macfs.FInfo()
Guido van Rossuma220e671996-03-23 19:19:04 +000057
58 def getfileinfo(name):
Jack Jansenfcdffea1995-08-07 14:36:51 +000059 finfo = macfs.FSSpec(name).GetFInfo()
60 dir, file = os.path.split(name)
61 # XXXX Get resource/data sizes
62 fp = open(name, 'rb')
63 fp.seek(0, 2)
64 dlen = fp.tell()
Guido van Rossuma220e671996-03-23 19:19:04 +000065 fp = openrf(name, '*rb')
Jack Jansenfcdffea1995-08-07 14:36:51 +000066 fp.seek(0, 2)
67 rlen = fp.tell()
68 return file, finfo, dlen, rlen
Guido van Rossuma220e671996-03-23 19:19:04 +000069
70 def openrsrc(name, *mode):
71 if not mode:
72 mode = '*rb'
Jack Jansenfcdffea1995-08-07 14:36:51 +000073 else:
Guido van Rossuma220e671996-03-23 19:19:04 +000074 mode = '*' + mode[0]
75 return openrf(name, mode)
Jack Jansenfcdffea1995-08-07 14:36:51 +000076
77else:
Guido van Rossuma220e671996-03-23 19:19:04 +000078 #
79 # Glue code for non-macintosh useage
80 #
81 import regsub
82
83 class FInfo:
Jack Jansenfcdffea1995-08-07 14:36:51 +000084 def __init__(self):
Guido van Rossuma220e671996-03-23 19:19:04 +000085 self.Type = '????'
86 self.Creator = '????'
87 self.Flags = 0
Jack Jansenfcdffea1995-08-07 14:36:51 +000088
Guido van Rossuma220e671996-03-23 19:19:04 +000089 def getfileinfo(name):
Jack Jansenfcdffea1995-08-07 14:36:51 +000090 finfo = FInfo()
91 # Quick check for textfile
92 fp = open(name)
93 data = open(name).read(256)
94 for c in data:
Guido van Rossuma220e671996-03-23 19:19:04 +000095 if not c in string.whitespace and (c<' ' or ord(c) > 0177):
96 break
Jack Jansenfcdffea1995-08-07 14:36:51 +000097 else:
Guido van Rossuma220e671996-03-23 19:19:04 +000098 finfo.Type = 'TEXT'
Jack Jansenfcdffea1995-08-07 14:36:51 +000099 fp.seek(0, 2)
100 dsize = fp.tell()
101 fp.close()
102 dir, file = os.path.split(name)
103 file = regsub.sub(':', '-', file)
104 return file, finfo, dsize, 0
105
Guido van Rossuma220e671996-03-23 19:19:04 +0000106 class openrsrc:
Jack Jansenfcdffea1995-08-07 14:36:51 +0000107 def __init__(self, *args):
Guido van Rossuma220e671996-03-23 19:19:04 +0000108 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +0000109
110 def read(self, *args):
Guido van Rossuma220e671996-03-23 19:19:04 +0000111 return ''
Jack Jansenfcdffea1995-08-07 14:36:51 +0000112
113 def write(self, *args):
114 pass
115
116 def close(self):
Guido van Rossuma220e671996-03-23 19:19:04 +0000117 pass
Jack Jansenfcdffea1995-08-07 14:36:51 +0000118
119class _Hqxcoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000120 """Write data to the coder in 3-byte chunks"""
121
122 def __init__(self, ofp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000123 self.ofp = ofp
124 self.data = ''
Guido van Rossuma220e671996-03-23 19:19:04 +0000125 self.hqxdata = ''
126 self.linelen = LINELEN-1
Jack Jansenfcdffea1995-08-07 14:36:51 +0000127
Guido van Rossuma220e671996-03-23 19:19:04 +0000128 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000129 self.data = self.data + data
Guido van Rossuma220e671996-03-23 19:19:04 +0000130 datalen = len(self.data)
131 todo = (datalen/3)*3
132 data = self.data[:todo]
133 self.data = self.data[todo:]
Guido van Rossumcce074e1996-03-25 18:54:33 +0000134 if not data:
135 return
Guido van Rossuma220e671996-03-23 19:19:04 +0000136 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
Guido van Rossumcce074e1996-03-25 18:54:33 +0000137 self._flush(0)
138
139 def _flush(self, force):
140 first = 0
141 while first <= len(self.hqxdata)-self.linelen:
142 last = first + self.linelen
143 self.ofp.write(self.hqxdata[first:last]+'\n')
Guido van Rossuma220e671996-03-23 19:19:04 +0000144 self.linelen = LINELEN
Guido van Rossumcce074e1996-03-25 18:54:33 +0000145 first = last
146 self.hqxdata = self.hqxdata[first:]
147 if force:
148 self.ofp.write(self.hqxdata + ':\n')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000149
Guido van Rossuma220e671996-03-23 19:19:04 +0000150 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000151 if self.data:
Guido van Rossuma220e671996-03-23 19:19:04 +0000152 self.hqxdata = 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
205 d3 = struct.pack('h', finfo.Flags)
206 d4 = struct.pack('ii', self.dlen, self.rlen)
207 info = d + d2 + d3 + d4
208 self._write(info)
209 self._writecrc()
210
Guido van Rossuma220e671996-03-23 19:19:04 +0000211 def _write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000212 self.crc = binascii.crc_hqx(data, self.crc)
213 self.ofp.write(data)
214
Guido van Rossuma220e671996-03-23 19:19:04 +0000215 def _writecrc(self):
216## self.crc = binascii.crc_hqx('\0\0', self.crc) # XXXX Should this be here??
Jack Jansenfcdffea1995-08-07 14:36:51 +0000217 self.ofp.write(struct.pack('h', self.crc))
218 self.crc = 0
219
Guido van Rossuma220e671996-03-23 19:19:04 +0000220 def write(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000221 if self.state != _DID_HEADER:
Guido van Rossuma220e671996-03-23 19:19:04 +0000222 raise Error, 'Writing data at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000223 self.dlen = self.dlen - len(data)
224 self._write(data)
225
Guido van Rossuma220e671996-03-23 19:19:04 +0000226 def close_data(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000227 if self.dlen <> 0:
Guido van Rossuma220e671996-03-23 19:19:04 +0000228 raise Error, 'Incorrect data size, diff='+`self.rlen`
Jack Jansenfcdffea1995-08-07 14:36:51 +0000229 self._writecrc()
230 self.state = _DID_DATA
231
Guido van Rossuma220e671996-03-23 19:19:04 +0000232 def write_rsrc(self, data):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000233 if self.state < _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000234 self.close_data()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000235 if self.state != _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000236 raise Error, 'Writing resource data at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000237 self.rlen = self.rlen - len(data)
238 self._write(data)
239
Guido van Rossuma220e671996-03-23 19:19:04 +0000240 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000241 if self.state < _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000242 self.close_data()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000243 if self.state != _DID_DATA:
Guido van Rossuma220e671996-03-23 19:19:04 +0000244 raise Error, 'Close at the wrong time'
Jack Jansenfcdffea1995-08-07 14:36:51 +0000245 if self.rlen <> 0:
Guido van Rossuma220e671996-03-23 19:19:04 +0000246 raise Error, "Incorrect resource-datasize, diff="+`self.rlen`
Jack Jansenfcdffea1995-08-07 14:36:51 +0000247 self._writecrc()
248 self.ofp.close()
249 self.state = None
Guido van Rossuma220e671996-03-23 19:19:04 +0000250 del self.ofp
Jack Jansenfcdffea1995-08-07 14:36:51 +0000251
252def binhex(inp, out):
253 """(infilename, outfilename) - Create binhex-encoded copy of a file"""
254 finfo = getfileinfo(inp)
255 ofp = BinHex(finfo, out)
256
257 ifp = open(inp, 'rb')
258 # XXXX Do textfile translation on non-mac systems
Guido van Rossuma220e671996-03-23 19:19:04 +0000259 while 1:
260 d = ifp.read(128000)
261 if not d: break
262 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000263 ofp.close_data()
264 ifp.close()
265
266 ifp = openrsrc(inp, 'rb')
Guido van Rossuma220e671996-03-23 19:19:04 +0000267 while 1:
268 d = ifp.read(128000)
269 if not d: break
270 ofp.write_rsrc(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000271 ofp.close()
Guido van Rossuma220e671996-03-23 19:19:04 +0000272 ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000273
274class _Hqxdecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000275 """Read data via the decoder in 4-byte chunks"""
276
277 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000278 self.ifp = ifp
279 self.eof = 0
280
Guido van Rossuma220e671996-03-23 19:19:04 +0000281 def read(self, totalwtd):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000282 """Read at least wtd bytes (or until EOF)"""
283 decdata = ''
Jack Jansen685e16d1995-10-03 14:41:15 +0000284 wtd = totalwtd
Jack Jansenfcdffea1995-08-07 14:36:51 +0000285 #
286 # The loop here is convoluted, since we don't really now how much
287 # to decode: there may be newlines in the incoming data.
288 while wtd > 0:
289 if self.eof: return decdata
290 wtd = ((wtd+2)/3)*4
291 data = self.ifp.read(wtd)
292 #
293 # Next problem: there may not be a complete number of bytes in what we
294 # pass to a2b. Solve by yet another loop.
295 #
296 while 1:
297 try:
298 decdatacur, self.eof = binascii.a2b_hqx(data)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000299 break
300 except binascii.Incomplete:
301 pass
302 newdata = self.ifp.read(1)
303 if not newdata:
304 raise Error, 'Premature EOF on binhex file'
305 data = data + newdata
306 decdata = decdata + decdatacur
Jack Jansen685e16d1995-10-03 14:41:15 +0000307 wtd = totalwtd - len(decdata)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000308 if not decdata and not self.eof:
309 raise Error, 'Premature EOF on binhex file'
310 return decdata
311
Guido van Rossuma220e671996-03-23 19:19:04 +0000312 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000313 self.ifp.close()
314
315class _Rledecoderengine:
Guido van Rossuma220e671996-03-23 19:19:04 +0000316 """Read data via the RLE-coder"""
Jack Jansenfcdffea1995-08-07 14:36:51 +0000317
Guido van Rossuma220e671996-03-23 19:19:04 +0000318 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000319 self.ifp = ifp
320 self.pre_buffer = ''
321 self.post_buffer = ''
322 self.eof = 0
323
Guido van Rossuma220e671996-03-23 19:19:04 +0000324 def read(self, wtd):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000325 if wtd > len(self.post_buffer):
Guido van Rossuma220e671996-03-23 19:19:04 +0000326 self._fill(wtd-len(self.post_buffer))
Jack Jansenfcdffea1995-08-07 14:36:51 +0000327 rv = self.post_buffer[:wtd]
328 self.post_buffer = self.post_buffer[wtd:]
Jack Jansenfcdffea1995-08-07 14:36:51 +0000329 return rv
330
Guido van Rossuma220e671996-03-23 19:19:04 +0000331 def _fill(self, wtd):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000332 #
333 # Obfuscated code ahead. We keep at least one byte in the pre_buffer,
334 # so we don't stumble over an orphaned RUNCHAR later on. If the
335 # last or second-last char is a RUNCHAR we keep more bytes.
336 #
337 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+2)
338 if self.ifp.eof:
339 self.post_buffer = self.post_buffer + \
340 binascii.rledecode_hqx(self.pre_buffer)
341 self.pre_buffer = ''
342 return
343
344 lastrle = string.rfind(self.pre_buffer, RUNCHAR)
345 if lastrle > 0 and lastrle == len(self.pre_buffer)-1:
346 # Last byte is an RLE, keep two bytes
347 mark = len(self.pre_buffer)-2
348 elif lastrle > 0 and lastrle == len(self.pre_buffer)-2:
349 # second-last byte is an RLE. Decode all.
350 mark = len(self.pre_buffer)
351 else:
352 mark = len(self.pre_buffer)-1
353 self.post_buffer = self.post_buffer + \
354 binascii.rledecode_hqx(self.pre_buffer[:mark])
355 self.pre_buffer = self.pre_buffer[mark:]
356
Guido van Rossuma220e671996-03-23 19:19:04 +0000357 def close(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000358 self.ifp.close()
359
360class HexBin:
Guido van Rossuma220e671996-03-23 19:19:04 +0000361 def __init__(self, ifp):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000362 if type(ifp) == type(''):
Guido van Rossuma220e671996-03-23 19:19:04 +0000363 ifp = open(ifp)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000364 #
365 # Find initial colon.
366 #
367 while 1:
368 ch = ifp.read(1)
369 if not ch:
370 raise Error, "No binhex data found"
371 if ch == ':':
372 break
373 if ch != '\n':
374 dummy = ifp.readline()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000375
376 hqxifp = _Hqxdecoderengine(ifp)
377 self.ifp = _Rledecoderengine(hqxifp)
378 self.crc = 0
379 self._readheader()
380
Guido van Rossuma220e671996-03-23 19:19:04 +0000381 def _read(self, len):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000382 data = self.ifp.read(len)
383 self.crc = binascii.crc_hqx(data, self.crc)
384 return data
385
Guido van Rossuma220e671996-03-23 19:19:04 +0000386 def _checkcrc(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000387 filecrc = struct.unpack('h', self.ifp.read(2))[0] & 0xffff
Jack Jansen685e16d1995-10-03 14:41:15 +0000388## self.crc = binascii.crc_hqx('\0\0', self.crc) # XXXX Is this needed??
Jack Jansenfcdffea1995-08-07 14:36:51 +0000389 self.crc = self.crc & 0xffff
Jack Jansen685e16d1995-10-03 14:41:15 +0000390 if filecrc != self.crc:
391 raise Error, 'CRC error, computed %x, read %x'%(self.crc, filecrc)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000392 self.crc = 0
393
Guido van Rossuma220e671996-03-23 19:19:04 +0000394 def _readheader(self):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000395 len = self._read(1)
396 fname = self._read(ord(len))
397 rest = self._read(1+4+4+2+4+4)
398 self._checkcrc()
399
400 type = rest[1:5]
401 creator = rest[5:9]
402 flags = struct.unpack('h', rest[9:11])[0]
403 self.dlen = struct.unpack('l', rest[11:15])[0]
404 self.rlen = struct.unpack('l', rest[15:19])[0]
405
Jack Jansenfcdffea1995-08-07 14:36:51 +0000406 self.FName = fname
407 self.FInfo = FInfo()
408 self.FInfo.Creator = creator
409 self.FInfo.Type = type
410 self.FInfo.Flags = flags
411
412 self.state = _DID_HEADER
413
Guido van Rossuma220e671996-03-23 19:19:04 +0000414 def read(self, *n):
Jack Jansenfcdffea1995-08-07 14:36:51 +0000415 if self.state != _DID_HEADER:
416 raise Error, 'Read data at wrong time'
417 if n:
418 n = n[0]
419 n = min(n, self.dlen)
420 else:
421 n = self.dlen
422 self.dlen = self.dlen - n
423 return self._read(n)
424
Guido van Rossuma220e671996-03-23 19:19:04 +0000425 def close_data(self):
426 if self.state != _DID_HEADER:
427 raise Error, 'close_data at wrong time'
428 if self.dlen:
Jack Jansenfcdffea1995-08-07 14:36:51 +0000429 dummy = self._read(self.dlen)
Guido van Rossuma220e671996-03-23 19:19:04 +0000430 self._checkcrc()
431 self.state = _DID_DATA
Jack Jansenfcdffea1995-08-07 14:36:51 +0000432
Guido van Rossuma220e671996-03-23 19:19:04 +0000433 def read_rsrc(self, *n):
434 if self.state == _DID_HEADER:
435 self.close_data()
436 if self.state != _DID_DATA:
437 raise Error, 'Read resource data at wrong time'
438 if n:
439 n = n[0]
440 n = min(n, self.rlen)
441 else:
442 n = self.rlen
443 self.rlen = self.rlen - n
444 return self._read(n)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000445
Guido van Rossuma220e671996-03-23 19:19:04 +0000446 def close(self):
447 if self.rlen:
448 dummy = self.read_rsrc(self.rlen)
449 self._checkcrc()
450 self.state = _DID_RSRC
451 self.ifp.close()
Jack Jansenfcdffea1995-08-07 14:36:51 +0000452
453def hexbin(inp, out):
454 """(infilename, outfilename) - Decode binhexed file"""
455 ifp = HexBin(inp)
456 finfo = ifp.FInfo
457 if not out:
458 out = ifp.FName
459 if os.name == 'mac':
460 ofss = macfs.FSSpec(out)
461 out = ofss.as_pathname()
462
463 ofp = open(out, 'wb')
464 # XXXX Do translation on non-mac systems
Guido van Rossuma220e671996-03-23 19:19:04 +0000465 while 1:
466 d = ifp.read(128000)
467 if not d: break
468 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000469 ofp.close()
470 ifp.close_data()
471
Guido van Rossuma220e671996-03-23 19:19:04 +0000472 d = ifp.read_rsrc(128000)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000473 if d:
474 ofp = openrsrc(out, 'wb')
475 ofp.write(d)
Guido van Rossuma220e671996-03-23 19:19:04 +0000476 while 1:
477 d = ifp.read_rsrc(128000)
478 if not d: break
479 ofp.write(d)
Jack Jansenfcdffea1995-08-07 14:36:51 +0000480 ofp.close()
481
482 if os.name == 'mac':
483 nfinfo = ofss.GetFInfo()
484 nfinfo.Creator = finfo.Creator
485 nfinfo.Type = finfo.Type
486 nfinfo.Flags = finfo.Flags
487 ofss.SetFInfo(nfinfo)
488
489 ifp.close()
490
491def _test():
492 if os.name == 'mac':
Jack Jansen479c1b31995-08-14 12:41:20 +0000493 fss, ok = macfs.PromptGetFile('File to convert:')
Jack Jansenfcdffea1995-08-07 14:36:51 +0000494 if not ok:
495 sys.exit(0)
496 fname = fss.as_pathname()
497 else:
498 fname = sys.argv[1]
499 #binhex(fname, fname+'.hqx')
500 #hexbin(fname+'.hqx', fname+'.viahqx')
501 hexbin(fname, fname+'.unpacked')
502 sys.exit(1)
503
504if __name__ == '__main__':
505 _test()