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() |
Nick Coghlan | b3c728f | 2011-03-16 21:26:40 -0400 | [diff] [blame] | 55 | with io.open(name, 'rb') as fp: |
| 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() |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 62 | dir, file = os.path.split(name) |
| 63 | file = file.replace(':', '-', 1) |
| 64 | return file, finfo, dsize, 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 65 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 66 | class openrsrc: |
| 67 | def __init__(self, *args): |
| 68 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 70 | def read(self, *args): |
| 71 | return b'' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 72 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 73 | def write(self, *args): |
| 74 | pass |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 75 | |
Benjamin Peterson | 69a07fb | 2008-05-12 22:25:16 +0000 | [diff] [blame] | 76 | def close(self): |
| 77 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 78 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 79 | class _Hqxcoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 80 | """Write data to the coder in 3-byte chunks""" |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 81 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 82 | def __init__(self, ofp): |
| 83 | self.ofp = ofp |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 84 | self.data = b'' |
| 85 | self.hqxdata = b'' |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 86 | self.linelen = LINELEN - 1 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 88 | def write(self, data): |
| 89 | self.data = self.data + data |
| 90 | datalen = len(self.data) |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 91 | todo = (datalen // 3) * 3 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 92 | data = self.data[:todo] |
| 93 | self.data = self.data[todo:] |
| 94 | if not data: |
| 95 | return |
| 96 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(data) |
| 97 | self._flush(0) |
Guido van Rossum | cce074e | 1996-03-25 18:54:33 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 99 | def _flush(self, force): |
| 100 | first = 0 |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 101 | while first <= len(self.hqxdata) - self.linelen: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 102 | last = first + self.linelen |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 103 | self.ofp.write(self.hqxdata[first:last] + b'\n') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 104 | self.linelen = LINELEN |
| 105 | first = last |
| 106 | self.hqxdata = self.hqxdata[first:] |
| 107 | if force: |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 108 | self.ofp.write(self.hqxdata + b':\n') |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 110 | def close(self): |
| 111 | if self.data: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 112 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 113 | self._flush(1) |
| 114 | self.ofp.close() |
| 115 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 116 | |
| 117 | class _Rlecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 118 | """Write data to the RLE-coder in suitably large chunks""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 120 | def __init__(self, ofp): |
| 121 | self.ofp = ofp |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 122 | self.data = b'' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 123 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 124 | def write(self, data): |
| 125 | self.data = self.data + data |
| 126 | if len(self.data) < REASONABLY_LARGE: |
| 127 | return |
| 128 | rledata = binascii.rlecode_hqx(self.data) |
| 129 | self.ofp.write(rledata) |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 130 | self.data = b'' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 132 | def close(self): |
| 133 | if self.data: |
| 134 | rledata = binascii.rlecode_hqx(self.data) |
| 135 | self.ofp.write(rledata) |
| 136 | self.ofp.close() |
| 137 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 138 | |
| 139 | class BinHex: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 140 | def __init__(self, name_finfo_dlen_rlen, ofp): |
| 141 | name, finfo, dlen, rlen = name_finfo_dlen_rlen |
Nick Coghlan | b3c728f | 2011-03-16 21:26:40 -0400 | [diff] [blame] | 142 | close_on_error = False |
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') |
Nick Coghlan | b3c728f | 2011-03-16 21:26:40 -0400 | [diff] [blame] | 146 | close_on_error = True |
| 147 | try: |
| 148 | ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:') |
| 149 | hqxer = _Hqxcoderengine(ofp) |
| 150 | self.ofp = _Rlecoderengine(hqxer) |
| 151 | self.crc = 0 |
| 152 | if finfo is None: |
| 153 | finfo = FInfo() |
| 154 | self.dlen = dlen |
| 155 | self.rlen = rlen |
| 156 | self._writeinfo(name, finfo) |
| 157 | self.state = _DID_HEADER |
| 158 | except: |
| 159 | if close_on_error: |
| 160 | ofp.close() |
| 161 | raise |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 163 | def _writeinfo(self, name, finfo): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 164 | nl = len(name) |
| 165 | if nl > 63: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 166 | raise Error('Filename too long') |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 167 | d = bytes([nl]) + name.encode("latin-1") + b'\0' |
Guido van Rossum | 1b261df | 2007-08-28 03:35:35 +0000 | [diff] [blame] | 168 | tp, cr = finfo.Type, finfo.Creator |
| 169 | if isinstance(tp, str): |
| 170 | tp = tp.encode("latin-1") |
| 171 | if isinstance(cr, str): |
| 172 | cr = cr.encode("latin-1") |
| 173 | d2 = tp + cr |
Roger E. Masse | 469848a | 1997-01-16 16:51:57 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 175 | # Force all structs to be packed with big-endian |
| 176 | d3 = struct.pack('>h', finfo.Flags) |
| 177 | d4 = struct.pack('>ii', self.dlen, self.rlen) |
| 178 | info = d + d2 + d3 + d4 |
| 179 | self._write(info) |
| 180 | self._writecrc() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 181 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 182 | def _write(self, data): |
| 183 | self.crc = binascii.crc_hqx(data, self.crc) |
| 184 | self.ofp.write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 186 | def _writecrc(self): |
| 187 | # XXXX Should this be here?? |
| 188 | # self.crc = binascii.crc_hqx('\0\0', self.crc) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 189 | if self.crc < 0: |
| 190 | fmt = '>h' |
| 191 | else: |
| 192 | fmt = '>H' |
| 193 | self.ofp.write(struct.pack(fmt, self.crc)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 194 | self.crc = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 196 | def write(self, data): |
| 197 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 198 | raise Error('Writing data at the wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 199 | self.dlen = self.dlen - len(data) |
| 200 | self._write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 202 | def close_data(self): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 203 | if self.dlen != 0: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 204 | raise Error('Incorrect data size, diff=%r' % (self.rlen,)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 205 | self._writecrc() |
| 206 | self.state = _DID_DATA |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 208 | def write_rsrc(self, data): |
| 209 | if self.state < _DID_DATA: |
| 210 | self.close_data() |
| 211 | if self.state != _DID_DATA: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 212 | raise Error('Writing resource data at the wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 213 | self.rlen = self.rlen - len(data) |
| 214 | self._write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 216 | def close(self): |
| 217 | if self.state < _DID_DATA: |
| 218 | self.close_data() |
| 219 | if self.state != _DID_DATA: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 220 | raise Error('Close at the wrong time') |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 221 | if self.rlen != 0: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 222 | raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 223 | self._writecrc() |
| 224 | self.ofp.close() |
| 225 | self.state = None |
| 226 | del self.ofp |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 227 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 228 | def binhex(inp, out): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 229 | """binhex(infilename, outfilename): create binhex-encoded copy of a file""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 230 | finfo = getfileinfo(inp) |
| 231 | ofp = BinHex(finfo, out) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 233 | ifp = io.open(inp, 'rb') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 234 | # XXXX Do textfile translation on non-mac systems |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 235 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 236 | d = ifp.read(128000) |
| 237 | if not d: break |
| 238 | ofp.write(d) |
| 239 | ofp.close_data() |
| 240 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 242 | ifp = openrsrc(inp, 'rb') |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 243 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 244 | d = ifp.read(128000) |
| 245 | if not d: break |
| 246 | ofp.write_rsrc(d) |
| 247 | ofp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 248 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 249 | |
| 250 | class _Hqxdecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 251 | """Read data via the decoder in 4-byte chunks""" |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 253 | def __init__(self, ifp): |
| 254 | self.ifp = ifp |
| 255 | self.eof = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 257 | def read(self, totalwtd): |
| 258 | """Read at least wtd bytes (or until EOF)""" |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 259 | decdata = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 260 | wtd = totalwtd |
| 261 | # |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 262 | # 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] | 263 | # much to decode: there may be newlines in the incoming data. |
| 264 | while wtd > 0: |
| 265 | if self.eof: return decdata |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 266 | wtd = ((wtd + 2) // 3) * 4 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 267 | data = self.ifp.read(wtd) |
| 268 | # |
| 269 | # Next problem: there may not be a complete number of |
| 270 | # bytes in what we pass to a2b. Solve by yet another |
| 271 | # loop. |
| 272 | # |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 273 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 274 | try: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 275 | decdatacur, self.eof = binascii.a2b_hqx(data) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 276 | break |
| 277 | except binascii.Incomplete: |
| 278 | pass |
| 279 | newdata = self.ifp.read(1) |
| 280 | if not newdata: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 281 | raise Error('Premature EOF on binhex file') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 282 | data = data + newdata |
| 283 | decdata = decdata + decdatacur |
| 284 | wtd = totalwtd - len(decdata) |
| 285 | if not decdata and not self.eof: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 286 | raise Error('Premature EOF on binhex file') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 287 | return decdata |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 288 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 289 | def close(self): |
| 290 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 291 | |
| 292 | class _Rledecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 293 | """Read data via the RLE-coder""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 295 | def __init__(self, ifp): |
| 296 | self.ifp = ifp |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 297 | self.pre_buffer = b'' |
| 298 | self.post_buffer = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 299 | self.eof = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 301 | def read(self, wtd): |
| 302 | if wtd > len(self.post_buffer): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 303 | self._fill(wtd - len(self.post_buffer)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 304 | rv = self.post_buffer[:wtd] |
| 305 | self.post_buffer = self.post_buffer[wtd:] |
| 306 | return rv |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 307 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 308 | def _fill(self, wtd): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 309 | self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 310 | if self.ifp.eof: |
| 311 | self.post_buffer = self.post_buffer + \ |
| 312 | binascii.rledecode_hqx(self.pre_buffer) |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 313 | self.pre_buffer = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 314 | return |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 316 | # |
| 317 | # Obfuscated code ahead. We have to take care that we don't |
| 318 | # end up with an orphaned RUNCHAR later on. So, we keep a couple |
| 319 | # of bytes in the buffer, depending on what the end of |
| 320 | # the buffer looks like: |
| 321 | # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0) |
| 322 | # '?\220' - Keep 2 bytes: repeated something-else |
| 323 | # '\220\0' - Escaped \220: Keep 2 bytes. |
| 324 | # '?\220?' - Complete repeat sequence: decode all |
| 325 | # otherwise: keep 1 byte. |
| 326 | # |
| 327 | mark = len(self.pre_buffer) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 328 | if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 329 | mark = mark - 3 |
Antoine Pitrou | 5492395 | 2009-07-01 14:53:06 +0000 | [diff] [blame] | 330 | elif self.pre_buffer[-1:] == RUNCHAR: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 331 | mark = mark - 2 |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 332 | elif self.pre_buffer[-2:] == RUNCHAR + b'\0': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 333 | mark = mark - 2 |
Antoine Pitrou | 5492395 | 2009-07-01 14:53:06 +0000 | [diff] [blame] | 334 | elif self.pre_buffer[-2:-1] == RUNCHAR: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 335 | pass # Decode all |
| 336 | else: |
| 337 | mark = mark - 1 |
Guido van Rossum | 50b82e8 | 1997-02-11 16:39:31 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 339 | self.post_buffer = self.post_buffer + \ |
| 340 | binascii.rledecode_hqx(self.pre_buffer[:mark]) |
| 341 | self.pre_buffer = self.pre_buffer[mark:] |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 343 | def close(self): |
| 344 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 345 | |
| 346 | class HexBin: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 347 | def __init__(self, ifp): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 348 | if isinstance(ifp, str): |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 349 | ifp = io.open(ifp, 'rb') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 350 | # |
| 351 | # Find initial colon. |
| 352 | # |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 353 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 354 | ch = ifp.read(1) |
| 355 | if not ch: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 356 | raise Error("No binhex data found") |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 357 | # Cater for \r\n terminated lines (which show up as \n\r, hence |
| 358 | # all lines start with \r) |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 359 | if ch == b'\r': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 360 | continue |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 361 | if ch == b':': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 362 | break |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 364 | hqxifp = _Hqxdecoderengine(ifp) |
| 365 | self.ifp = _Rledecoderengine(hqxifp) |
| 366 | self.crc = 0 |
| 367 | self._readheader() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 369 | def _read(self, len): |
| 370 | data = self.ifp.read(len) |
| 371 | self.crc = binascii.crc_hqx(data, self.crc) |
| 372 | return data |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 374 | def _checkcrc(self): |
| 375 | filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff |
| 376 | #self.crc = binascii.crc_hqx('\0\0', self.crc) |
| 377 | # XXXX Is this needed?? |
| 378 | self.crc = self.crc & 0xffff |
| 379 | if filecrc != self.crc: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 380 | raise Error('CRC error, computed %x, read %x' |
| 381 | % (self.crc, filecrc)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 382 | self.crc = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 383 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 384 | def _readheader(self): |
| 385 | len = self._read(1) |
| 386 | fname = self._read(ord(len)) |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 387 | rest = self._read(1 + 4 + 4 + 2 + 4 + 4) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 388 | self._checkcrc() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 389 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 390 | type = rest[1:5] |
| 391 | creator = rest[5:9] |
| 392 | flags = struct.unpack('>h', rest[9:11])[0] |
| 393 | self.dlen = struct.unpack('>l', rest[11:15])[0] |
| 394 | self.rlen = struct.unpack('>l', rest[15:19])[0] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 395 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 396 | self.FName = fname |
| 397 | self.FInfo = FInfo() |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 398 | self.FInfo.Creator = creator |
| 399 | self.FInfo.Type = type |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 400 | self.FInfo.Flags = flags |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 402 | self.state = _DID_HEADER |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 403 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 404 | def read(self, *n): |
| 405 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 406 | raise Error('Read data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 407 | if n: |
| 408 | n = n[0] |
| 409 | n = min(n, self.dlen) |
| 410 | else: |
| 411 | n = self.dlen |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 412 | rv = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 413 | while len(rv) < n: |
| 414 | rv = rv + self._read(n-len(rv)) |
| 415 | self.dlen = self.dlen - n |
| 416 | return rv |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 418 | def close_data(self): |
| 419 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 420 | raise Error('close_data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 421 | if self.dlen: |
| 422 | dummy = self._read(self.dlen) |
| 423 | self._checkcrc() |
| 424 | self.state = _DID_DATA |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 426 | def read_rsrc(self, *n): |
| 427 | if self.state == _DID_HEADER: |
| 428 | self.close_data() |
| 429 | if self.state != _DID_DATA: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 430 | raise Error('Read resource data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 431 | if n: |
| 432 | n = n[0] |
| 433 | n = min(n, self.rlen) |
| 434 | else: |
| 435 | n = self.rlen |
| 436 | self.rlen = self.rlen - n |
| 437 | return self._read(n) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 439 | def close(self): |
| 440 | if self.rlen: |
| 441 | dummy = self.read_rsrc(self.rlen) |
| 442 | self._checkcrc() |
| 443 | self.state = _DID_RSRC |
| 444 | self.ifp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 445 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 446 | def hexbin(inp, out): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 447 | """hexbin(infilename, outfilename) - Decode binhexed file""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 448 | ifp = HexBin(inp) |
| 449 | finfo = ifp.FInfo |
| 450 | if not out: |
| 451 | out = ifp.FName |
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 | ifp.close() |