Fix typo. Backport if anyone cares. :-)
[python.git] / Lib / binhex.py
blob4f3882ac06718165a39a8f4a3ad26dc85a3b3dff
1 """Macintosh binhex compression/decompression.
3 easy interface:
4 binhex(inputfilename, outputfilename)
5 hexbin(inputfilename, outputfilename)
6 """
9 # Jack Jansen, CWI, August 1995.
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).
16 # XXXX The simple routines are too simple: they expect to hold the complete
17 # files in-core. Should be fixed.
18 # XXXX It would be nice to handle AppleDouble format on unix
19 # (for servers serving macs).
20 # 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...
24 import sys
25 import os
26 import struct
27 import binascii
29 __all__ = ["binhex","hexbin","Error"]
31 class Error(Exception):
32 pass
34 # States (what have we written)
35 [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
37 # Various constants
38 REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder
39 LINELEN=64
40 RUNCHAR=chr(0x90) # run-length introducer
43 # This code is no longer byte-order dependent
46 # Workarounds for non-mac machines.
47 if os.name == 'mac':
48 import macfs
49 import MacOS
50 try:
51 openrf = MacOS.openrf
52 except AttributeError:
53 # Backward compatibility
54 openrf = open
56 def FInfo():
57 return macfs.FInfo()
59 def getfileinfo(name):
60 finfo = macfs.FSSpec(name).GetFInfo()
61 dir, file = os.path.split(name)
62 # XXXX Get resource/data sizes
63 fp = open(name, 'rb')
64 fp.seek(0, 2)
65 dlen = fp.tell()
66 fp = openrf(name, '*rb')
67 fp.seek(0, 2)
68 rlen = fp.tell()
69 return file, finfo, dlen, rlen
71 def openrsrc(name, *mode):
72 if not mode:
73 mode = '*rb'
74 else:
75 mode = '*' + mode[0]
76 return openrf(name, mode)
78 else:
80 # Glue code for non-macintosh usage
83 class FInfo:
84 def __init__(self):
85 self.Type = '????'
86 self.Creator = '????'
87 self.Flags = 0
89 def getfileinfo(name):
90 finfo = FInfo()
91 # Quick check for textfile
92 fp = open(name)
93 data = open(name).read(256)
94 for c in data:
95 if not c.isspace() and (c<' ' or ord(c) > 0x7f):
96 break
97 else:
98 finfo.Type = 'TEXT'
99 fp.seek(0, 2)
100 dsize = fp.tell()
101 fp.close()
102 dir, file = os.path.split(name)
103 file = file.replace(':', '-', 1)
104 return file, finfo, dsize, 0
106 class openrsrc:
107 def __init__(self, *args):
108 pass
110 def read(self, *args):
111 return ''
113 def write(self, *args):
114 pass
116 def close(self):
117 pass
119 class _Hqxcoderengine:
120 """Write data to the coder in 3-byte chunks"""
122 def __init__(self, ofp):
123 self.ofp = ofp
124 self.data = ''
125 self.hqxdata = ''
126 self.linelen = LINELEN-1
128 def write(self, data):
129 self.data = self.data + data
130 datalen = len(self.data)
131 todo = (datalen//3)*3
132 data = self.data[:todo]
133 self.data = self.data[todo:]
134 if not data:
135 return
136 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
137 self._flush(0)
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')
144 self.linelen = LINELEN
145 first = last
146 self.hqxdata = self.hqxdata[first:]
147 if force:
148 self.ofp.write(self.hqxdata + ':\n')
150 def close(self):
151 if self.data:
152 self.hqxdata = \
153 self.hqxdata + binascii.b2a_hqx(self.data)
154 self._flush(1)
155 self.ofp.close()
156 del self.ofp
158 class _Rlecoderengine:
159 """Write data to the RLE-coder in suitably large chunks"""
161 def __init__(self, ofp):
162 self.ofp = ofp
163 self.data = ''
165 def write(self, data):
166 self.data = self.data + data
167 if len(self.data) < REASONABLY_LARGE:
168 return
169 rledata = binascii.rlecode_hqx(self.data)
170 self.ofp.write(rledata)
171 self.data = ''
173 def close(self):
174 if self.data:
175 rledata = binascii.rlecode_hqx(self.data)
176 self.ofp.write(rledata)
177 self.ofp.close()
178 del self.ofp
180 class BinHex:
181 def __init__(self, (name, finfo, dlen, rlen), ofp):
182 if type(ofp) == type(''):
183 ofname = ofp
184 ofp = open(ofname, 'w')
185 if os.name == 'mac':
186 fss = macfs.FSSpec(ofname)
187 fss.SetCreatorType('BnHq', 'TEXT')
188 ofp.write('(This file must be converted with BinHex 4.0)\n\n:')
189 hqxer = _Hqxcoderengine(ofp)
190 self.ofp = _Rlecoderengine(hqxer)
191 self.crc = 0
192 if finfo is None:
193 finfo = FInfo()
194 self.dlen = dlen
195 self.rlen = rlen
196 self._writeinfo(name, finfo)
197 self.state = _DID_HEADER
199 def _writeinfo(self, name, finfo):
200 nl = len(name)
201 if nl > 63:
202 raise Error, 'Filename too long'
203 d = chr(nl) + name + '\0'
204 d2 = finfo.Type + finfo.Creator
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)
209 info = d + d2 + d3 + d4
210 self._write(info)
211 self._writecrc()
213 def _write(self, data):
214 self.crc = binascii.crc_hqx(data, self.crc)
215 self.ofp.write(data)
217 def _writecrc(self):
218 # XXXX Should this be here??
219 # self.crc = binascii.crc_hqx('\0\0', self.crc)
220 if self.crc < 0:
221 fmt = '>h'
222 else:
223 fmt = '>H'
224 self.ofp.write(struct.pack(fmt, self.crc))
225 self.crc = 0
227 def write(self, data):
228 if self.state != _DID_HEADER:
229 raise Error, 'Writing data at the wrong time'
230 self.dlen = self.dlen - len(data)
231 self._write(data)
233 def close_data(self):
234 if self.dlen != 0:
235 raise Error, 'Incorrect data size, diff=%r' % (self.rlen,)
236 self._writecrc()
237 self.state = _DID_DATA
239 def write_rsrc(self, data):
240 if self.state < _DID_DATA:
241 self.close_data()
242 if self.state != _DID_DATA:
243 raise Error, 'Writing resource data at the wrong time'
244 self.rlen = self.rlen - len(data)
245 self._write(data)
247 def close(self):
248 if self.state < _DID_DATA:
249 self.close_data()
250 if self.state != _DID_DATA:
251 raise Error, 'Close at the wrong time'
252 if self.rlen != 0:
253 raise Error, \
254 "Incorrect resource-datasize, diff=%r" % (self.rlen,)
255 self._writecrc()
256 self.ofp.close()
257 self.state = None
258 del self.ofp
260 def binhex(inp, out):
261 """(infilename, outfilename) - Create binhex-encoded copy of a file"""
262 finfo = getfileinfo(inp)
263 ofp = BinHex(finfo, out)
265 ifp = open(inp, 'rb')
266 # XXXX Do textfile translation on non-mac systems
267 while 1:
268 d = ifp.read(128000)
269 if not d: break
270 ofp.write(d)
271 ofp.close_data()
272 ifp.close()
274 ifp = openrsrc(inp, 'rb')
275 while 1:
276 d = ifp.read(128000)
277 if not d: break
278 ofp.write_rsrc(d)
279 ofp.close()
280 ifp.close()
282 class _Hqxdecoderengine:
283 """Read data via the decoder in 4-byte chunks"""
285 def __init__(self, ifp):
286 self.ifp = ifp
287 self.eof = 0
289 def read(self, totalwtd):
290 """Read at least wtd bytes (or until EOF)"""
291 decdata = ''
292 wtd = totalwtd
294 # The loop here is convoluted, since we don't really now how
295 # much to decode: there may be newlines in the incoming data.
296 while wtd > 0:
297 if self.eof: return decdata
298 wtd = ((wtd+2)//3)*4
299 data = self.ifp.read(wtd)
301 # Next problem: there may not be a complete number of
302 # bytes in what we pass to a2b. Solve by yet another
303 # loop.
305 while 1:
306 try:
307 decdatacur, self.eof = \
308 binascii.a2b_hqx(data)
309 break
310 except binascii.Incomplete:
311 pass
312 newdata = self.ifp.read(1)
313 if not newdata:
314 raise Error, \
315 'Premature EOF on binhex file'
316 data = data + newdata
317 decdata = decdata + decdatacur
318 wtd = totalwtd - len(decdata)
319 if not decdata and not self.eof:
320 raise Error, 'Premature EOF on binhex file'
321 return decdata
323 def close(self):
324 self.ifp.close()
326 class _Rledecoderengine:
327 """Read data via the RLE-coder"""
329 def __init__(self, ifp):
330 self.ifp = ifp
331 self.pre_buffer = ''
332 self.post_buffer = ''
333 self.eof = 0
335 def read(self, wtd):
336 if wtd > len(self.post_buffer):
337 self._fill(wtd-len(self.post_buffer))
338 rv = self.post_buffer[:wtd]
339 self.post_buffer = self.post_buffer[wtd:]
340 return rv
342 def _fill(self, wtd):
343 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+4)
344 if self.ifp.eof:
345 self.post_buffer = self.post_buffer + \
346 binascii.rledecode_hqx(self.pre_buffer)
347 self.pre_buffer = ''
348 return
351 # Obfuscated code ahead. We have to take care that we don't
352 # end up with an orphaned RUNCHAR later on. So, we keep a couple
353 # of bytes in the buffer, depending on what the end of
354 # the buffer looks like:
355 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
356 # '?\220' - Keep 2 bytes: repeated something-else
357 # '\220\0' - Escaped \220: Keep 2 bytes.
358 # '?\220?' - Complete repeat sequence: decode all
359 # otherwise: keep 1 byte.
361 mark = len(self.pre_buffer)
362 if self.pre_buffer[-3:] == RUNCHAR + '\0' + RUNCHAR:
363 mark = mark - 3
364 elif self.pre_buffer[-1] == RUNCHAR:
365 mark = mark - 2
366 elif self.pre_buffer[-2:] == RUNCHAR + '\0':
367 mark = mark - 2
368 elif self.pre_buffer[-2] == RUNCHAR:
369 pass # Decode all
370 else:
371 mark = mark - 1
373 self.post_buffer = self.post_buffer + \
374 binascii.rledecode_hqx(self.pre_buffer[:mark])
375 self.pre_buffer = self.pre_buffer[mark:]
377 def close(self):
378 self.ifp.close()
380 class HexBin:
381 def __init__(self, ifp):
382 if type(ifp) == type(''):
383 ifp = open(ifp)
385 # Find initial colon.
387 while 1:
388 ch = ifp.read(1)
389 if not ch:
390 raise Error, "No binhex data found"
391 # Cater for \r\n terminated lines (which show up as \n\r, hence
392 # all lines start with \r)
393 if ch == '\r':
394 continue
395 if ch == ':':
396 break
397 if ch != '\n':
398 dummy = ifp.readline()
400 hqxifp = _Hqxdecoderengine(ifp)
401 self.ifp = _Rledecoderengine(hqxifp)
402 self.crc = 0
403 self._readheader()
405 def _read(self, len):
406 data = self.ifp.read(len)
407 self.crc = binascii.crc_hqx(data, self.crc)
408 return data
410 def _checkcrc(self):
411 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
412 #self.crc = binascii.crc_hqx('\0\0', self.crc)
413 # XXXX Is this needed??
414 self.crc = self.crc & 0xffff
415 if filecrc != self.crc:
416 raise Error, 'CRC error, computed %x, read %x' \
417 %(self.crc, filecrc)
418 self.crc = 0
420 def _readheader(self):
421 len = self._read(1)
422 fname = self._read(ord(len))
423 rest = self._read(1+4+4+2+4+4)
424 self._checkcrc()
426 type = rest[1:5]
427 creator = rest[5:9]
428 flags = struct.unpack('>h', rest[9:11])[0]
429 self.dlen = struct.unpack('>l', rest[11:15])[0]
430 self.rlen = struct.unpack('>l', rest[15:19])[0]
432 self.FName = fname
433 self.FInfo = FInfo()
434 self.FInfo.Creator = creator
435 self.FInfo.Type = type
436 self.FInfo.Flags = flags
438 self.state = _DID_HEADER
440 def read(self, *n):
441 if self.state != _DID_HEADER:
442 raise Error, 'Read data at wrong time'
443 if n:
444 n = n[0]
445 n = min(n, self.dlen)
446 else:
447 n = self.dlen
448 rv = ''
449 while len(rv) < n:
450 rv = rv + self._read(n-len(rv))
451 self.dlen = self.dlen - n
452 return rv
454 def close_data(self):
455 if self.state != _DID_HEADER:
456 raise Error, 'close_data at wrong time'
457 if self.dlen:
458 dummy = self._read(self.dlen)
459 self._checkcrc()
460 self.state = _DID_DATA
462 def read_rsrc(self, *n):
463 if self.state == _DID_HEADER:
464 self.close_data()
465 if self.state != _DID_DATA:
466 raise Error, 'Read resource data at wrong time'
467 if n:
468 n = n[0]
469 n = min(n, self.rlen)
470 else:
471 n = self.rlen
472 self.rlen = self.rlen - n
473 return self._read(n)
475 def close(self):
476 if self.rlen:
477 dummy = self.read_rsrc(self.rlen)
478 self._checkcrc()
479 self.state = _DID_RSRC
480 self.ifp.close()
482 def hexbin(inp, out):
483 """(infilename, outfilename) - Decode binhexed file"""
484 ifp = HexBin(inp)
485 finfo = ifp.FInfo
486 if not out:
487 out = ifp.FName
488 if os.name == 'mac':
489 ofss = macfs.FSSpec(out)
490 out = ofss.as_pathname()
492 ofp = open(out, 'wb')
493 # XXXX Do translation on non-mac systems
494 while 1:
495 d = ifp.read(128000)
496 if not d: break
497 ofp.write(d)
498 ofp.close()
499 ifp.close_data()
501 d = ifp.read_rsrc(128000)
502 if d:
503 ofp = openrsrc(out, 'wb')
504 ofp.write(d)
505 while 1:
506 d = ifp.read_rsrc(128000)
507 if not d: break
508 ofp.write(d)
509 ofp.close()
511 if os.name == 'mac':
512 nfinfo = ofss.GetFInfo()
513 nfinfo.Creator = finfo.Creator
514 nfinfo.Type = finfo.Type
515 nfinfo.Flags = finfo.Flags
516 ofss.SetFInfo(nfinfo)
518 ifp.close()
520 def _test():
521 if os.name == 'mac':
522 fss, ok = macfs.PromptGetFile('File to convert:')
523 if not ok:
524 sys.exit(0)
525 fname = fss.as_pathname()
526 else:
527 fname = sys.argv[1]
528 binhex(fname, fname+'.hqx')
529 hexbin(fname+'.hqx', fname+'.viahqx')
530 #hexbin(fname, fname+'.unpacked')
531 sys.exit(1)
533 if __name__ == '__main__':
534 _test()