Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Macintosh binhex compression/decompression. |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 2 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 3 | easy interface: |
| 4 | binhex(inputfilename, outputfilename) |
| 5 | hexbin(inputfilename, outputfilename) |
| 6 | """ |
| 7 | |
| 8 | # |
| 9 | # Jack Jansen, CWI, August 1995. |
| 10 | # |
| 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). |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 16 | # XXXX The simple routines are too simple: they expect to hold the complete |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 17 | # files in-core. Should be fixed. |
Roger E. Masse | 469848a | 1997-01-16 16:51:57 +0000 | [diff] [blame] | 18 | # XXXX It would be nice to handle AppleDouble format on unix |
| 19 | # (for servers serving macs). |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 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... |
| 23 | # |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 24 | import io |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 25 | import os |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 26 | import sys |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 27 | import struct |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 28 | import binascii |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 29 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 30 | __all__ = ["binhex","hexbin","Error"] |
| 31 | |
Fred Drake | 227b120 | 2000-08-17 05:06:49 +0000 | [diff] [blame] | 32 | class Error(Exception): |
| 33 | pass |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 34 | |
| 35 | # States (what have we written) |
| 36 | [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3) |
| 37 | |
| 38 | # Various constants |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 39 | REASONABLY_LARGE = 32768 # Minimal amount we pass the rle-coder |
| 40 | LINELEN = 64 |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 41 | RUNCHAR = b"\x90" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 42 | |
| 43 | # |
Roger E. Masse | 469848a | 1997-01-16 16:51:57 +0000 | [diff] [blame] | 44 | # This code is no longer byte-order dependent |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 45 | |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 46 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 47 | class FInfo: |
| 48 | def __init__(self): |
| 49 | self.Type = '????' |
| 50 | self.Creator = '????' |
| 51 | self.Flags = 0 |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 52 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 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 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 67 | class openrsrc: |
| 68 | def __init__(self, *args): |
| 69 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 70 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 71 | def read(self, *args): |
| 72 | return b'' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 74 | def write(self, *args): |
| 75 | pass |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 76 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 77 | def close(self): |
| 78 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 79 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 80 | class _Hqxcoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 81 | """Write data to the coder in 3-byte chunks""" |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 83 | def __init__(self, ofp): |
| 84 | self.ofp = ofp |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 85 | self.data = b'' |
| 86 | self.hqxdata = b'' |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 87 | self.linelen = LINELEN - 1 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 89 | def write(self, data): |
| 90 | self.data = self.data + data |
| 91 | datalen = len(self.data) |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 92 | todo = (datalen // 3) * 3 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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) |
Guido van Rossum | cce074e | 1996-03-25 18:54:33 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 100 | def _flush(self, force): |
| 101 | first = 0 |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 102 | while first <= len(self.hqxdata) - self.linelen: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 103 | last = first + self.linelen |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 104 | self.ofp.write(self.hqxdata[first:last] + b'\n') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 105 | self.linelen = LINELEN |
| 106 | first = last |
| 107 | self.hqxdata = self.hqxdata[first:] |
| 108 | if force: |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 109 | self.ofp.write(self.hqxdata + b':\n') |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 110 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 111 | def close(self): |
| 112 | if self.data: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 113 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 114 | self._flush(1) |
| 115 | self.ofp.close() |
| 116 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 117 | |
| 118 | class _Rlecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 119 | """Write data to the RLE-coder in suitably large chunks""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 121 | def __init__(self, ofp): |
| 122 | self.ofp = ofp |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 123 | self.data = b'' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 124 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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) |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 131 | self.data = b'' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 139 | |
| 140 | class BinHex: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 141 | def __init__(self, name_finfo_dlen_rlen, ofp): |
| 142 | name, finfo, dlen, rlen = name_finfo_dlen_rlen |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 143 | if isinstance(ofp, str): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 144 | ofname = ofp |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 145 | ofp = io.open(ofname, 'wb') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 146 | if os.name == 'mac': |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 147 | fss = FSSpec(ofname) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 148 | fss.SetCreatorType('BnHq', 'TEXT') |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 149 | ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 150 | hqxer = _Hqxcoderengine(ofp) |
| 151 | self.ofp = _Rlecoderengine(hqxer) |
| 152 | self.crc = 0 |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 153 | if finfo is None: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 154 | finfo = FInfo() |
| 155 | self.dlen = dlen |
| 156 | self.rlen = rlen |
| 157 | self._writeinfo(name, finfo) |
| 158 | self.state = _DID_HEADER |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 160 | def _writeinfo(self, name, finfo): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 161 | nl = len(name) |
| 162 | if nl > 63: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 163 | raise Error('Filename too long') |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 164 | d = bytes([nl]) + name.encode("latin-1") + b'\0' |
Guido van Rossum | 1b261df | 2007-08-28 03:35:35 +0000 | [diff] [blame] | 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 |
Roger E. Masse | 469848a | 1997-01-16 16:51:57 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 179 | def _write(self, data): |
| 180 | self.crc = binascii.crc_hqx(data, self.crc) |
| 181 | self.ofp.write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 182 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 183 | def _writecrc(self): |
| 184 | # XXXX Should this be here?? |
| 185 | # self.crc = binascii.crc_hqx('\0\0', self.crc) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 186 | if self.crc < 0: |
| 187 | fmt = '>h' |
| 188 | else: |
| 189 | fmt = '>H' |
| 190 | self.ofp.write(struct.pack(fmt, self.crc)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 191 | self.crc = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 193 | def write(self, data): |
| 194 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 195 | raise Error('Writing data at the wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 196 | self.dlen = self.dlen - len(data) |
| 197 | self._write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 199 | def close_data(self): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 200 | if self.dlen != 0: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 201 | raise Error('Incorrect data size, diff=%r' % (self.rlen,)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 202 | self._writecrc() |
| 203 | self.state = _DID_DATA |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 205 | def write_rsrc(self, data): |
| 206 | if self.state < _DID_DATA: |
| 207 | self.close_data() |
| 208 | if self.state != _DID_DATA: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 209 | raise Error('Writing resource data at the wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 210 | self.rlen = self.rlen - len(data) |
| 211 | self._write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 212 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 213 | def close(self): |
| 214 | if self.state < _DID_DATA: |
| 215 | self.close_data() |
| 216 | if self.state != _DID_DATA: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 217 | raise Error('Close at the wrong time') |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 218 | if self.rlen != 0: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 219 | raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 220 | self._writecrc() |
| 221 | self.ofp.close() |
| 222 | self.state = None |
| 223 | del self.ofp |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 224 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 225 | def binhex(inp, out): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 226 | """binhex(infilename, outfilename): create binhex-encoded copy of a file""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 227 | finfo = getfileinfo(inp) |
| 228 | ofp = BinHex(finfo, out) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 230 | ifp = io.open(inp, 'rb') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 231 | # XXXX Do textfile translation on non-mac systems |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 232 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 233 | d = ifp.read(128000) |
| 234 | if not d: break |
| 235 | ofp.write(d) |
| 236 | ofp.close_data() |
| 237 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 238 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 239 | ifp = openrsrc(inp, 'rb') |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 240 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 241 | d = ifp.read(128000) |
| 242 | if not d: break |
| 243 | ofp.write_rsrc(d) |
| 244 | ofp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 245 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 246 | |
| 247 | class _Hqxdecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 248 | """Read data via the decoder in 4-byte chunks""" |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 249 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 250 | def __init__(self, ifp): |
| 251 | self.ifp = ifp |
| 252 | self.eof = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 253 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 254 | def read(self, totalwtd): |
| 255 | """Read at least wtd bytes (or until EOF)""" |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 256 | decdata = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 257 | wtd = totalwtd |
| 258 | # |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 259 | # The loop here is convoluted, since we don't really now how |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 260 | # much to decode: there may be newlines in the incoming data. |
| 261 | while wtd > 0: |
| 262 | if self.eof: return decdata |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 263 | wtd = ((wtd + 2) // 3) * 4 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 264 | data = self.ifp.read(wtd) |
| 265 | # |
| 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. |
| 269 | # |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 270 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 271 | try: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 272 | decdatacur, self.eof = binascii.a2b_hqx(data) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 273 | break |
| 274 | except binascii.Incomplete: |
| 275 | pass |
| 276 | newdata = self.ifp.read(1) |
| 277 | if not newdata: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 278 | raise Error('Premature EOF on binhex file') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 279 | data = data + newdata |
| 280 | decdata = decdata + decdatacur |
| 281 | wtd = totalwtd - len(decdata) |
| 282 | if not decdata and not self.eof: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 283 | raise Error('Premature EOF on binhex file') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 284 | return decdata |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 286 | def close(self): |
| 287 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 288 | |
| 289 | class _Rledecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 290 | """Read data via the RLE-coder""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 291 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 292 | def __init__(self, ifp): |
| 293 | self.ifp = ifp |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 294 | self.pre_buffer = b'' |
| 295 | self.post_buffer = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 296 | self.eof = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 298 | def read(self, wtd): |
| 299 | if wtd > len(self.post_buffer): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 300 | self._fill(wtd - len(self.post_buffer)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 301 | rv = self.post_buffer[:wtd] |
| 302 | self.post_buffer = self.post_buffer[wtd:] |
| 303 | return rv |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 305 | def _fill(self, wtd): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 306 | self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 307 | if self.ifp.eof: |
| 308 | self.post_buffer = self.post_buffer + \ |
| 309 | binascii.rledecode_hqx(self.pre_buffer) |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 310 | self.pre_buffer = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 311 | return |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 313 | # |
| 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. |
| 323 | # |
| 324 | mark = len(self.pre_buffer) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 325 | if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 326 | mark = mark - 3 |
| 327 | elif self.pre_buffer[-1] == RUNCHAR: |
| 328 | mark = mark - 2 |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 329 | elif self.pre_buffer[-2:] == RUNCHAR + b'\0': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 330 | mark = mark - 2 |
| 331 | elif self.pre_buffer[-2] == RUNCHAR: |
| 332 | pass # Decode all |
| 333 | else: |
| 334 | mark = mark - 1 |
Guido van Rossum | 50b82e8 | 1997-02-11 16:39:31 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 336 | self.post_buffer = self.post_buffer + \ |
| 337 | binascii.rledecode_hqx(self.pre_buffer[:mark]) |
| 338 | self.pre_buffer = self.pre_buffer[mark:] |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 339 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 340 | def close(self): |
| 341 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 342 | |
| 343 | class HexBin: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 344 | def __init__(self, ifp): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 345 | if isinstance(ifp, str): |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 346 | ifp = io.open(ifp, 'rb') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 347 | # |
| 348 | # Find initial colon. |
| 349 | # |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 350 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 351 | ch = ifp.read(1) |
| 352 | if not ch: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 353 | raise Error("No binhex data found") |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 354 | # Cater for \r\n terminated lines (which show up as \n\r, hence |
| 355 | # all lines start with \r) |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 356 | if ch == b'\r': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 357 | continue |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 358 | if ch == b':': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 359 | break |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 361 | hqxifp = _Hqxdecoderengine(ifp) |
| 362 | self.ifp = _Rledecoderengine(hqxifp) |
| 363 | self.crc = 0 |
| 364 | self._readheader() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 366 | def _read(self, len): |
| 367 | data = self.ifp.read(len) |
| 368 | self.crc = binascii.crc_hqx(data, self.crc) |
| 369 | return data |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 370 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 377 | raise Error('CRC error, computed %x, read %x' |
| 378 | % (self.crc, filecrc)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 379 | self.crc = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 381 | def _readheader(self): |
| 382 | len = self._read(1) |
| 383 | fname = self._read(ord(len)) |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 384 | rest = self._read(1 + 4 + 4 + 2 + 4 + 4) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 385 | self._checkcrc() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 386 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 392 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 393 | self.FName = fname |
| 394 | self.FInfo = FInfo() |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 395 | self.FInfo.Creator = creator |
| 396 | self.FInfo.Type = type |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 397 | self.FInfo.Flags = flags |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 399 | self.state = _DID_HEADER |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 400 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 401 | def read(self, *n): |
| 402 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 403 | raise Error('Read data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 404 | if n: |
| 405 | n = n[0] |
| 406 | n = min(n, self.dlen) |
| 407 | else: |
| 408 | n = self.dlen |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 409 | rv = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 410 | while len(rv) < n: |
| 411 | rv = rv + self._read(n-len(rv)) |
| 412 | self.dlen = self.dlen - n |
| 413 | return rv |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 414 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 415 | def close_data(self): |
| 416 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 417 | raise Error('close_data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 418 | if self.dlen: |
| 419 | dummy = self._read(self.dlen) |
| 420 | self._checkcrc() |
| 421 | self.state = _DID_DATA |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 422 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 423 | def read_rsrc(self, *n): |
| 424 | if self.state == _DID_HEADER: |
| 425 | self.close_data() |
| 426 | if self.state != _DID_DATA: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 427 | raise Error('Read resource data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 435 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 442 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 443 | def hexbin(inp, out): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 444 | """hexbin(infilename, outfilename) - Decode binhexed file""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 445 | ifp = HexBin(inp) |
| 446 | finfo = ifp.FInfo |
| 447 | if not out: |
| 448 | out = ifp.FName |
| 449 | if os.name == 'mac': |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 450 | ofss = FSSpec(out) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 451 | out = ofss.as_pathname() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 452 | |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 453 | ofp = io.open(out, 'wb') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 454 | # XXXX Do translation on non-mac systems |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 455 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 456 | d = ifp.read(128000) |
| 457 | if not d: break |
| 458 | ofp.write(d) |
| 459 | ofp.close() |
| 460 | ifp.close_data() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 461 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 462 | d = ifp.read_rsrc(128000) |
| 463 | if d: |
| 464 | ofp = openrsrc(out, 'wb') |
| 465 | ofp.write(d) |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 466 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 467 | d = ifp.read_rsrc(128000) |
| 468 | if not d: break |
| 469 | ofp.write(d) |
| 470 | ofp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 479 | ifp.close() |