Merged revisions 81656 via svnmerge from
[python/dscho.git] / Lib / binhex.py
blob4b7997a7102ef64c1fa137cc7001acc7747cdf5b
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 io
25 import os
26 import sys
27 import struct
28 import binascii
30 __all__ = ["binhex","hexbin","Error"]
32 class Error(Exception):
33 pass
35 # States (what have we written)
36 [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
38 # Various constants
39 REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder
40 LINELEN = 64
41 RUNCHAR = b"\x90"
44 # This code is no longer byte-order dependent
47 class FInfo:
48 def __init__(self):
49 self.Type = '????'
50 self.Creator = '????'
51 self.Flags = 0
53 def getfileinfo(name):
54 finfo = FInfo()
55 fp = io.open(name, 'rb')
56 # Quick check for textfile
57 data = fp.read(512)
58 if 0 not in data:
59 finfo.Type = 'TEXT'
60 fp.seek(0, 2)
61 dsize = fp.tell()
62 fp.close()
63 dir, file = os.path.split(name)
64 file = file.replace(':', '-', 1)
65 return file, finfo, dsize, 0
67 class openrsrc:
68 def __init__(self, *args):
69 pass
71 def read(self, *args):
72 return b''
74 def write(self, *args):
75 pass
77 def close(self):
78 pass
80 class _Hqxcoderengine:
81 """Write data to the coder in 3-byte chunks"""
83 def __init__(self, ofp):
84 self.ofp = ofp
85 self.data = b''
86 self.hqxdata = b''
87 self.linelen = LINELEN - 1
89 def write(self, data):
90 self.data = self.data + data
91 datalen = len(self.data)
92 todo = (datalen // 3) * 3
93 data = self.data[:todo]
94 self.data = self.data[todo:]
95 if not data:
96 return
97 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
98 self._flush(0)
100 def _flush(self, force):
101 first = 0
102 while first <= len(self.hqxdata) - self.linelen:
103 last = first + self.linelen
104 self.ofp.write(self.hqxdata[first:last] + b'\n')
105 self.linelen = LINELEN
106 first = last
107 self.hqxdata = self.hqxdata[first:]
108 if force:
109 self.ofp.write(self.hqxdata + b':\n')
111 def close(self):
112 if self.data:
113 self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data)
114 self._flush(1)
115 self.ofp.close()
116 del self.ofp
118 class _Rlecoderengine:
119 """Write data to the RLE-coder in suitably large chunks"""
121 def __init__(self, ofp):
122 self.ofp = ofp
123 self.data = b''
125 def write(self, data):
126 self.data = self.data + data
127 if len(self.data) < REASONABLY_LARGE:
128 return
129 rledata = binascii.rlecode_hqx(self.data)
130 self.ofp.write(rledata)
131 self.data = b''
133 def close(self):
134 if self.data:
135 rledata = binascii.rlecode_hqx(self.data)
136 self.ofp.write(rledata)
137 self.ofp.close()
138 del self.ofp
140 class BinHex:
141 def __init__(self, name_finfo_dlen_rlen, ofp):
142 name, finfo, dlen, rlen = name_finfo_dlen_rlen
143 if isinstance(ofp, str):
144 ofname = ofp
145 ofp = io.open(ofname, 'wb')
146 ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
147 hqxer = _Hqxcoderengine(ofp)
148 self.ofp = _Rlecoderengine(hqxer)
149 self.crc = 0
150 if finfo is None:
151 finfo = FInfo()
152 self.dlen = dlen
153 self.rlen = rlen
154 self._writeinfo(name, finfo)
155 self.state = _DID_HEADER
157 def _writeinfo(self, name, finfo):
158 nl = len(name)
159 if nl > 63:
160 raise Error('Filename too long')
161 d = bytes([nl]) + name.encode("latin-1") + b'\0'
162 tp, cr = finfo.Type, finfo.Creator
163 if isinstance(tp, str):
164 tp = tp.encode("latin-1")
165 if isinstance(cr, str):
166 cr = cr.encode("latin-1")
167 d2 = tp + cr
169 # Force all structs to be packed with big-endian
170 d3 = struct.pack('>h', finfo.Flags)
171 d4 = struct.pack('>ii', self.dlen, self.rlen)
172 info = d + d2 + d3 + d4
173 self._write(info)
174 self._writecrc()
176 def _write(self, data):
177 self.crc = binascii.crc_hqx(data, self.crc)
178 self.ofp.write(data)
180 def _writecrc(self):
181 # XXXX Should this be here??
182 # self.crc = binascii.crc_hqx('\0\0', self.crc)
183 if self.crc < 0:
184 fmt = '>h'
185 else:
186 fmt = '>H'
187 self.ofp.write(struct.pack(fmt, self.crc))
188 self.crc = 0
190 def write(self, data):
191 if self.state != _DID_HEADER:
192 raise Error('Writing data at the wrong time')
193 self.dlen = self.dlen - len(data)
194 self._write(data)
196 def close_data(self):
197 if self.dlen != 0:
198 raise Error('Incorrect data size, diff=%r' % (self.rlen,))
199 self._writecrc()
200 self.state = _DID_DATA
202 def write_rsrc(self, data):
203 if self.state < _DID_DATA:
204 self.close_data()
205 if self.state != _DID_DATA:
206 raise Error('Writing resource data at the wrong time')
207 self.rlen = self.rlen - len(data)
208 self._write(data)
210 def close(self):
211 if self.state < _DID_DATA:
212 self.close_data()
213 if self.state != _DID_DATA:
214 raise Error('Close at the wrong time')
215 if self.rlen != 0:
216 raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
217 self._writecrc()
218 self.ofp.close()
219 self.state = None
220 del self.ofp
222 def binhex(inp, out):
223 """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
224 finfo = getfileinfo(inp)
225 ofp = BinHex(finfo, out)
227 ifp = io.open(inp, 'rb')
228 # XXXX Do textfile translation on non-mac systems
229 while True:
230 d = ifp.read(128000)
231 if not d: break
232 ofp.write(d)
233 ofp.close_data()
234 ifp.close()
236 ifp = openrsrc(inp, 'rb')
237 while True:
238 d = ifp.read(128000)
239 if not d: break
240 ofp.write_rsrc(d)
241 ofp.close()
242 ifp.close()
244 class _Hqxdecoderengine:
245 """Read data via the decoder in 4-byte chunks"""
247 def __init__(self, ifp):
248 self.ifp = ifp
249 self.eof = 0
251 def read(self, totalwtd):
252 """Read at least wtd bytes (or until EOF)"""
253 decdata = b''
254 wtd = totalwtd
256 # The loop here is convoluted, since we don't really now how
257 # much to decode: there may be newlines in the incoming data.
258 while wtd > 0:
259 if self.eof: return decdata
260 wtd = ((wtd + 2) // 3) * 4
261 data = self.ifp.read(wtd)
263 # Next problem: there may not be a complete number of
264 # bytes in what we pass to a2b. Solve by yet another
265 # loop.
267 while True:
268 try:
269 decdatacur, self.eof = binascii.a2b_hqx(data)
270 break
271 except binascii.Incomplete:
272 pass
273 newdata = self.ifp.read(1)
274 if not newdata:
275 raise Error('Premature EOF on binhex file')
276 data = data + newdata
277 decdata = decdata + decdatacur
278 wtd = totalwtd - len(decdata)
279 if not decdata and not self.eof:
280 raise Error('Premature EOF on binhex file')
281 return decdata
283 def close(self):
284 self.ifp.close()
286 class _Rledecoderengine:
287 """Read data via the RLE-coder"""
289 def __init__(self, ifp):
290 self.ifp = ifp
291 self.pre_buffer = b''
292 self.post_buffer = b''
293 self.eof = 0
295 def read(self, wtd):
296 if wtd > len(self.post_buffer):
297 self._fill(wtd - len(self.post_buffer))
298 rv = self.post_buffer[:wtd]
299 self.post_buffer = self.post_buffer[wtd:]
300 return rv
302 def _fill(self, wtd):
303 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
304 if self.ifp.eof:
305 self.post_buffer = self.post_buffer + \
306 binascii.rledecode_hqx(self.pre_buffer)
307 self.pre_buffer = b''
308 return
311 # Obfuscated code ahead. We have to take care that we don't
312 # end up with an orphaned RUNCHAR later on. So, we keep a couple
313 # of bytes in the buffer, depending on what the end of
314 # the buffer looks like:
315 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
316 # '?\220' - Keep 2 bytes: repeated something-else
317 # '\220\0' - Escaped \220: Keep 2 bytes.
318 # '?\220?' - Complete repeat sequence: decode all
319 # otherwise: keep 1 byte.
321 mark = len(self.pre_buffer)
322 if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
323 mark = mark - 3
324 elif self.pre_buffer[-1:] == RUNCHAR:
325 mark = mark - 2
326 elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
327 mark = mark - 2
328 elif self.pre_buffer[-2:-1] == RUNCHAR:
329 pass # Decode all
330 else:
331 mark = mark - 1
333 self.post_buffer = self.post_buffer + \
334 binascii.rledecode_hqx(self.pre_buffer[:mark])
335 self.pre_buffer = self.pre_buffer[mark:]
337 def close(self):
338 self.ifp.close()
340 class HexBin:
341 def __init__(self, ifp):
342 if isinstance(ifp, str):
343 ifp = io.open(ifp, 'rb')
345 # Find initial colon.
347 while True:
348 ch = ifp.read(1)
349 if not ch:
350 raise Error("No binhex data found")
351 # Cater for \r\n terminated lines (which show up as \n\r, hence
352 # all lines start with \r)
353 if ch == b'\r':
354 continue
355 if ch == b':':
356 break
358 hqxifp = _Hqxdecoderengine(ifp)
359 self.ifp = _Rledecoderengine(hqxifp)
360 self.crc = 0
361 self._readheader()
363 def _read(self, len):
364 data = self.ifp.read(len)
365 self.crc = binascii.crc_hqx(data, self.crc)
366 return data
368 def _checkcrc(self):
369 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
370 #self.crc = binascii.crc_hqx('\0\0', self.crc)
371 # XXXX Is this needed??
372 self.crc = self.crc & 0xffff
373 if filecrc != self.crc:
374 raise Error('CRC error, computed %x, read %x'
375 % (self.crc, filecrc))
376 self.crc = 0
378 def _readheader(self):
379 len = self._read(1)
380 fname = self._read(ord(len))
381 rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
382 self._checkcrc()
384 type = rest[1:5]
385 creator = rest[5:9]
386 flags = struct.unpack('>h', rest[9:11])[0]
387 self.dlen = struct.unpack('>l', rest[11:15])[0]
388 self.rlen = struct.unpack('>l', rest[15:19])[0]
390 self.FName = fname
391 self.FInfo = FInfo()
392 self.FInfo.Creator = creator
393 self.FInfo.Type = type
394 self.FInfo.Flags = flags
396 self.state = _DID_HEADER
398 def read(self, *n):
399 if self.state != _DID_HEADER:
400 raise Error('Read data at wrong time')
401 if n:
402 n = n[0]
403 n = min(n, self.dlen)
404 else:
405 n = self.dlen
406 rv = b''
407 while len(rv) < n:
408 rv = rv + self._read(n-len(rv))
409 self.dlen = self.dlen - n
410 return rv
412 def close_data(self):
413 if self.state != _DID_HEADER:
414 raise Error('close_data at wrong time')
415 if self.dlen:
416 dummy = self._read(self.dlen)
417 self._checkcrc()
418 self.state = _DID_DATA
420 def read_rsrc(self, *n):
421 if self.state == _DID_HEADER:
422 self.close_data()
423 if self.state != _DID_DATA:
424 raise Error('Read resource data at wrong time')
425 if n:
426 n = n[0]
427 n = min(n, self.rlen)
428 else:
429 n = self.rlen
430 self.rlen = self.rlen - n
431 return self._read(n)
433 def close(self):
434 if self.rlen:
435 dummy = self.read_rsrc(self.rlen)
436 self._checkcrc()
437 self.state = _DID_RSRC
438 self.ifp.close()
440 def hexbin(inp, out):
441 """hexbin(infilename, outfilename) - Decode binhexed file"""
442 ifp = HexBin(inp)
443 finfo = ifp.FInfo
444 if not out:
445 out = ifp.FName
447 ofp = io.open(out, 'wb')
448 # XXXX Do translation on non-mac systems
449 while True:
450 d = ifp.read(128000)
451 if not d: break
452 ofp.write(d)
453 ofp.close()
454 ifp.close_data()
456 d = ifp.read_rsrc(128000)
457 if d:
458 ofp = openrsrc(out, 'wb')
459 ofp.write(d)
460 while True:
461 d = ifp.read_rsrc(128000)
462 if not d: break
463 ofp.write(d)
464 ofp.close()
466 ifp.close()