see if we can get this to work on windows
[python/dscho.git] / Lib / binhex.py
blob1c3f3429032667dca3939a237e76121898898372
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 if os.name == 'mac':
147 fss = FSSpec(ofname)
148 fss.SetCreatorType('BnHq', 'TEXT')
149 ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
150 hqxer = _Hqxcoderengine(ofp)
151 self.ofp = _Rlecoderengine(hqxer)
152 self.crc = 0
153 if finfo is None:
154 finfo = FInfo()
155 self.dlen = dlen
156 self.rlen = rlen
157 self._writeinfo(name, finfo)
158 self.state = _DID_HEADER
160 def _writeinfo(self, name, finfo):
161 nl = len(name)
162 if nl > 63:
163 raise Error('Filename too long')
164 d = bytes([nl]) + name.encode("latin-1") + b'\0'
165 tp, cr = finfo.Type, finfo.Creator
166 if isinstance(tp, str):
167 tp = tp.encode("latin-1")
168 if isinstance(cr, str):
169 cr = cr.encode("latin-1")
170 d2 = tp + cr
172 # Force all structs to be packed with big-endian
173 d3 = struct.pack('>h', finfo.Flags)
174 d4 = struct.pack('>ii', self.dlen, self.rlen)
175 info = d + d2 + d3 + d4
176 self._write(info)
177 self._writecrc()
179 def _write(self, data):
180 self.crc = binascii.crc_hqx(data, self.crc)
181 self.ofp.write(data)
183 def _writecrc(self):
184 # XXXX Should this be here??
185 # self.crc = binascii.crc_hqx('\0\0', self.crc)
186 if self.crc < 0:
187 fmt = '>h'
188 else:
189 fmt = '>H'
190 self.ofp.write(struct.pack(fmt, self.crc))
191 self.crc = 0
193 def write(self, data):
194 if self.state != _DID_HEADER:
195 raise Error('Writing data at the wrong time')
196 self.dlen = self.dlen - len(data)
197 self._write(data)
199 def close_data(self):
200 if self.dlen != 0:
201 raise Error('Incorrect data size, diff=%r' % (self.rlen,))
202 self._writecrc()
203 self.state = _DID_DATA
205 def write_rsrc(self, data):
206 if self.state < _DID_DATA:
207 self.close_data()
208 if self.state != _DID_DATA:
209 raise Error('Writing resource data at the wrong time')
210 self.rlen = self.rlen - len(data)
211 self._write(data)
213 def close(self):
214 if self.state < _DID_DATA:
215 self.close_data()
216 if self.state != _DID_DATA:
217 raise Error('Close at the wrong time')
218 if self.rlen != 0:
219 raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
220 self._writecrc()
221 self.ofp.close()
222 self.state = None
223 del self.ofp
225 def binhex(inp, out):
226 """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
227 finfo = getfileinfo(inp)
228 ofp = BinHex(finfo, out)
230 ifp = io.open(inp, 'rb')
231 # XXXX Do textfile translation on non-mac systems
232 while True:
233 d = ifp.read(128000)
234 if not d: break
235 ofp.write(d)
236 ofp.close_data()
237 ifp.close()
239 ifp = openrsrc(inp, 'rb')
240 while True:
241 d = ifp.read(128000)
242 if not d: break
243 ofp.write_rsrc(d)
244 ofp.close()
245 ifp.close()
247 class _Hqxdecoderengine:
248 """Read data via the decoder in 4-byte chunks"""
250 def __init__(self, ifp):
251 self.ifp = ifp
252 self.eof = 0
254 def read(self, totalwtd):
255 """Read at least wtd bytes (or until EOF)"""
256 decdata = b''
257 wtd = totalwtd
259 # The loop here is convoluted, since we don't really now how
260 # much to decode: there may be newlines in the incoming data.
261 while wtd > 0:
262 if self.eof: return decdata
263 wtd = ((wtd + 2) // 3) * 4
264 data = self.ifp.read(wtd)
266 # Next problem: there may not be a complete number of
267 # bytes in what we pass to a2b. Solve by yet another
268 # loop.
270 while True:
271 try:
272 decdatacur, self.eof = binascii.a2b_hqx(data)
273 break
274 except binascii.Incomplete:
275 pass
276 newdata = self.ifp.read(1)
277 if not newdata:
278 raise Error('Premature EOF on binhex file')
279 data = data + newdata
280 decdata = decdata + decdatacur
281 wtd = totalwtd - len(decdata)
282 if not decdata and not self.eof:
283 raise Error('Premature EOF on binhex file')
284 return decdata
286 def close(self):
287 self.ifp.close()
289 class _Rledecoderengine:
290 """Read data via the RLE-coder"""
292 def __init__(self, ifp):
293 self.ifp = ifp
294 self.pre_buffer = b''
295 self.post_buffer = b''
296 self.eof = 0
298 def read(self, wtd):
299 if wtd > len(self.post_buffer):
300 self._fill(wtd - len(self.post_buffer))
301 rv = self.post_buffer[:wtd]
302 self.post_buffer = self.post_buffer[wtd:]
303 return rv
305 def _fill(self, wtd):
306 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
307 if self.ifp.eof:
308 self.post_buffer = self.post_buffer + \
309 binascii.rledecode_hqx(self.pre_buffer)
310 self.pre_buffer = b''
311 return
314 # Obfuscated code ahead. We have to take care that we don't
315 # end up with an orphaned RUNCHAR later on. So, we keep a couple
316 # of bytes in the buffer, depending on what the end of
317 # the buffer looks like:
318 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
319 # '?\220' - Keep 2 bytes: repeated something-else
320 # '\220\0' - Escaped \220: Keep 2 bytes.
321 # '?\220?' - Complete repeat sequence: decode all
322 # otherwise: keep 1 byte.
324 mark = len(self.pre_buffer)
325 if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
326 mark = mark - 3
327 elif self.pre_buffer[-1] == RUNCHAR:
328 mark = mark - 2
329 elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
330 mark = mark - 2
331 elif self.pre_buffer[-2] == RUNCHAR:
332 pass # Decode all
333 else:
334 mark = mark - 1
336 self.post_buffer = self.post_buffer + \
337 binascii.rledecode_hqx(self.pre_buffer[:mark])
338 self.pre_buffer = self.pre_buffer[mark:]
340 def close(self):
341 self.ifp.close()
343 class HexBin:
344 def __init__(self, ifp):
345 if isinstance(ifp, str):
346 ifp = io.open(ifp, 'rb')
348 # Find initial colon.
350 while True:
351 ch = ifp.read(1)
352 if not ch:
353 raise Error("No binhex data found")
354 # Cater for \r\n terminated lines (which show up as \n\r, hence
355 # all lines start with \r)
356 if ch == b'\r':
357 continue
358 if ch == b':':
359 break
361 hqxifp = _Hqxdecoderengine(ifp)
362 self.ifp = _Rledecoderengine(hqxifp)
363 self.crc = 0
364 self._readheader()
366 def _read(self, len):
367 data = self.ifp.read(len)
368 self.crc = binascii.crc_hqx(data, self.crc)
369 return data
371 def _checkcrc(self):
372 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
373 #self.crc = binascii.crc_hqx('\0\0', self.crc)
374 # XXXX Is this needed??
375 self.crc = self.crc & 0xffff
376 if filecrc != self.crc:
377 raise Error('CRC error, computed %x, read %x'
378 % (self.crc, filecrc))
379 self.crc = 0
381 def _readheader(self):
382 len = self._read(1)
383 fname = self._read(ord(len))
384 rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
385 self._checkcrc()
387 type = rest[1:5]
388 creator = rest[5:9]
389 flags = struct.unpack('>h', rest[9:11])[0]
390 self.dlen = struct.unpack('>l', rest[11:15])[0]
391 self.rlen = struct.unpack('>l', rest[15:19])[0]
393 self.FName = fname
394 self.FInfo = FInfo()
395 self.FInfo.Creator = creator
396 self.FInfo.Type = type
397 self.FInfo.Flags = flags
399 self.state = _DID_HEADER
401 def read(self, *n):
402 if self.state != _DID_HEADER:
403 raise Error('Read data at wrong time')
404 if n:
405 n = n[0]
406 n = min(n, self.dlen)
407 else:
408 n = self.dlen
409 rv = b''
410 while len(rv) < n:
411 rv = rv + self._read(n-len(rv))
412 self.dlen = self.dlen - n
413 return rv
415 def close_data(self):
416 if self.state != _DID_HEADER:
417 raise Error('close_data at wrong time')
418 if self.dlen:
419 dummy = self._read(self.dlen)
420 self._checkcrc()
421 self.state = _DID_DATA
423 def read_rsrc(self, *n):
424 if self.state == _DID_HEADER:
425 self.close_data()
426 if self.state != _DID_DATA:
427 raise Error('Read resource data at wrong time')
428 if n:
429 n = n[0]
430 n = min(n, self.rlen)
431 else:
432 n = self.rlen
433 self.rlen = self.rlen - n
434 return self._read(n)
436 def close(self):
437 if self.rlen:
438 dummy = self.read_rsrc(self.rlen)
439 self._checkcrc()
440 self.state = _DID_RSRC
441 self.ifp.close()
443 def hexbin(inp, out):
444 """hexbin(infilename, outfilename) - Decode binhexed file"""
445 ifp = HexBin(inp)
446 finfo = ifp.FInfo
447 if not out:
448 out = ifp.FName
449 if os.name == 'mac':
450 ofss = FSSpec(out)
451 out = ofss.as_pathname()
453 ofp = io.open(out, 'wb')
454 # XXXX Do translation on non-mac systems
455 while True:
456 d = ifp.read(128000)
457 if not d: break
458 ofp.write(d)
459 ofp.close()
460 ifp.close_data()
462 d = ifp.read_rsrc(128000)
463 if d:
464 ofp = openrsrc(out, 'wb')
465 ofp.write(d)
466 while True:
467 d = ifp.read_rsrc(128000)
468 if not d: break
469 ofp.write(d)
470 ofp.close()
472 if os.name == 'mac':
473 nfinfo = ofss.GetFInfo()
474 nfinfo.Creator = finfo.Creator
475 nfinfo.Type = finfo.Type
476 nfinfo.Flags = finfo.Flags
477 ofss.SetFInfo(nfinfo)
479 ifp.close()