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 |
| 26 | import struct |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 27 | import binascii |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 28 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 29 | __all__ = ["binhex","hexbin","Error"] |
| 30 | |
Fred Drake | 227b120 | 2000-08-17 05:06:49 +0000 | [diff] [blame] | 31 | class Error(Exception): |
| 32 | pass |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 33 | |
| 34 | # States (what have we written) |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 35 | _DID_HEADER = 0 |
| 36 | _DID_DATA = 1 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 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): |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 217 | if self.state is None: |
| 218 | return |
| 219 | try: |
| 220 | if self.state < _DID_DATA: |
| 221 | self.close_data() |
| 222 | if self.state != _DID_DATA: |
| 223 | raise Error('Close at the wrong time') |
| 224 | if self.rlen != 0: |
| 225 | raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,)) |
| 226 | self._writecrc() |
| 227 | finally: |
| 228 | self.state = None |
| 229 | ofp = self.ofp |
| 230 | del self.ofp |
| 231 | ofp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 232 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 233 | def binhex(inp, out): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 234 | """binhex(infilename, outfilename): create binhex-encoded copy of a file""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 235 | finfo = getfileinfo(inp) |
| 236 | ofp = BinHex(finfo, out) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 237 | |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 238 | with io.open(inp, 'rb') as ifp: |
| 239 | # XXXX Do textfile translation on non-mac systems |
| 240 | while True: |
| 241 | d = ifp.read(128000) |
| 242 | if not d: break |
| 243 | ofp.write(d) |
| 244 | ofp.close_data() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 245 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 246 | ifp = openrsrc(inp, 'rb') |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 247 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 248 | d = ifp.read(128000) |
| 249 | if not d: break |
| 250 | ofp.write_rsrc(d) |
| 251 | ofp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 252 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 253 | |
| 254 | class _Hqxdecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 255 | """Read data via the decoder in 4-byte chunks""" |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 257 | def __init__(self, ifp): |
| 258 | self.ifp = ifp |
| 259 | self.eof = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 260 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 261 | def read(self, totalwtd): |
| 262 | """Read at least wtd bytes (or until EOF)""" |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 263 | decdata = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 264 | wtd = totalwtd |
| 265 | # |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 266 | # 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] | 267 | # much to decode: there may be newlines in the incoming data. |
| 268 | while wtd > 0: |
| 269 | if self.eof: return decdata |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 270 | wtd = ((wtd + 2) // 3) * 4 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 271 | data = self.ifp.read(wtd) |
| 272 | # |
| 273 | # Next problem: there may not be a complete number of |
| 274 | # bytes in what we pass to a2b. Solve by yet another |
| 275 | # loop. |
| 276 | # |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 277 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 278 | try: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 279 | decdatacur, self.eof = binascii.a2b_hqx(data) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 280 | break |
| 281 | except binascii.Incomplete: |
| 282 | pass |
| 283 | newdata = self.ifp.read(1) |
| 284 | if not newdata: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 285 | raise Error('Premature EOF on binhex file') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 286 | data = data + newdata |
| 287 | decdata = decdata + decdatacur |
| 288 | wtd = totalwtd - len(decdata) |
| 289 | if not decdata and not self.eof: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 290 | raise Error('Premature EOF on binhex file') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 291 | return decdata |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 293 | def close(self): |
| 294 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 295 | |
| 296 | class _Rledecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 297 | """Read data via the RLE-coder""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 298 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 299 | def __init__(self, ifp): |
| 300 | self.ifp = ifp |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 301 | self.pre_buffer = b'' |
| 302 | self.post_buffer = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 303 | self.eof = 0 |
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 read(self, wtd): |
| 306 | if wtd > len(self.post_buffer): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 307 | self._fill(wtd - len(self.post_buffer)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 308 | rv = self.post_buffer[:wtd] |
| 309 | self.post_buffer = self.post_buffer[wtd:] |
| 310 | return rv |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 311 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 312 | def _fill(self, wtd): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 313 | self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 314 | if self.ifp.eof: |
| 315 | self.post_buffer = self.post_buffer + \ |
| 316 | binascii.rledecode_hqx(self.pre_buffer) |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 317 | self.pre_buffer = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 318 | return |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 320 | # |
| 321 | # Obfuscated code ahead. We have to take care that we don't |
| 322 | # end up with an orphaned RUNCHAR later on. So, we keep a couple |
| 323 | # of bytes in the buffer, depending on what the end of |
| 324 | # the buffer looks like: |
| 325 | # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0) |
| 326 | # '?\220' - Keep 2 bytes: repeated something-else |
| 327 | # '\220\0' - Escaped \220: Keep 2 bytes. |
| 328 | # '?\220?' - Complete repeat sequence: decode all |
| 329 | # otherwise: keep 1 byte. |
| 330 | # |
| 331 | mark = len(self.pre_buffer) |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 332 | if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 333 | mark = mark - 3 |
Antoine Pitrou | 5492395 | 2009-07-01 14:53:06 +0000 | [diff] [blame] | 334 | elif self.pre_buffer[-1:] == RUNCHAR: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 335 | mark = mark - 2 |
Jeremy Hylton | da3f228 | 2007-08-29 17:26:34 +0000 | [diff] [blame] | 336 | elif self.pre_buffer[-2:] == RUNCHAR + b'\0': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 337 | mark = mark - 2 |
Antoine Pitrou | 5492395 | 2009-07-01 14:53:06 +0000 | [diff] [blame] | 338 | elif self.pre_buffer[-2:-1] == RUNCHAR: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 339 | pass # Decode all |
| 340 | else: |
| 341 | mark = mark - 1 |
Guido van Rossum | 50b82e8 | 1997-02-11 16:39:31 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 343 | self.post_buffer = self.post_buffer + \ |
| 344 | binascii.rledecode_hqx(self.pre_buffer[:mark]) |
| 345 | self.pre_buffer = self.pre_buffer[mark:] |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 347 | def close(self): |
| 348 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 349 | |
| 350 | class HexBin: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 351 | def __init__(self, ifp): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 352 | if isinstance(ifp, str): |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 353 | ifp = io.open(ifp, 'rb') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 354 | # |
| 355 | # Find initial colon. |
| 356 | # |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 357 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 358 | ch = ifp.read(1) |
| 359 | if not ch: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 360 | raise Error("No binhex data found") |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 361 | # Cater for \r\n terminated lines (which show up as \n\r, hence |
| 362 | # all lines start with \r) |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 363 | if ch == b'\r': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 364 | continue |
Guido van Rossum | 34a042d | 2007-05-24 22:05:19 +0000 | [diff] [blame] | 365 | if ch == b':': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 366 | break |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 367 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 368 | hqxifp = _Hqxdecoderengine(ifp) |
| 369 | self.ifp = _Rledecoderengine(hqxifp) |
| 370 | self.crc = 0 |
| 371 | self._readheader() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 373 | def _read(self, len): |
| 374 | data = self.ifp.read(len) |
| 375 | self.crc = binascii.crc_hqx(data, self.crc) |
| 376 | return data |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 378 | def _checkcrc(self): |
| 379 | filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff |
| 380 | #self.crc = binascii.crc_hqx('\0\0', self.crc) |
| 381 | # XXXX Is this needed?? |
| 382 | self.crc = self.crc & 0xffff |
| 383 | if filecrc != self.crc: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 384 | raise Error('CRC error, computed %x, read %x' |
| 385 | % (self.crc, filecrc)) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 386 | self.crc = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 387 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 388 | def _readheader(self): |
| 389 | len = self._read(1) |
| 390 | fname = self._read(ord(len)) |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 391 | rest = self._read(1 + 4 + 4 + 2 + 4 + 4) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 392 | self._checkcrc() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 394 | type = rest[1:5] |
| 395 | creator = rest[5:9] |
| 396 | flags = struct.unpack('>h', rest[9:11])[0] |
| 397 | self.dlen = struct.unpack('>l', rest[11:15])[0] |
| 398 | self.rlen = struct.unpack('>l', rest[15:19])[0] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 399 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 400 | self.FName = fname |
| 401 | self.FInfo = FInfo() |
Neal Norwitz | 6ea45d3 | 2007-08-26 04:19:43 +0000 | [diff] [blame] | 402 | self.FInfo.Creator = creator |
| 403 | self.FInfo.Type = type |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 404 | self.FInfo.Flags = flags |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 406 | self.state = _DID_HEADER |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 408 | def read(self, *n): |
| 409 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 410 | raise Error('Read data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 411 | if n: |
| 412 | n = n[0] |
| 413 | n = min(n, self.dlen) |
| 414 | else: |
| 415 | n = self.dlen |
Guido van Rossum | dcee3c0 | 2007-05-22 22:25:42 +0000 | [diff] [blame] | 416 | rv = b'' |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 417 | while len(rv) < n: |
| 418 | rv = rv + self._read(n-len(rv)) |
| 419 | self.dlen = self.dlen - n |
| 420 | return rv |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 421 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 422 | def close_data(self): |
| 423 | if self.state != _DID_HEADER: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 424 | raise Error('close_data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 425 | if self.dlen: |
| 426 | dummy = self._read(self.dlen) |
| 427 | self._checkcrc() |
| 428 | self.state = _DID_DATA |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 430 | def read_rsrc(self, *n): |
| 431 | if self.state == _DID_HEADER: |
| 432 | self.close_data() |
| 433 | if self.state != _DID_DATA: |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 434 | raise Error('Read resource data at wrong time') |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 435 | if n: |
| 436 | n = n[0] |
| 437 | n = min(n, self.rlen) |
| 438 | else: |
| 439 | n = self.rlen |
| 440 | self.rlen = self.rlen - n |
| 441 | return self._read(n) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 442 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 443 | def close(self): |
Serhiy Storchaka | 7e7a3db | 2015-04-10 13:24:41 +0300 | [diff] [blame] | 444 | if self.state is None: |
| 445 | return |
| 446 | try: |
| 447 | if self.rlen: |
| 448 | dummy = self.read_rsrc(self.rlen) |
| 449 | self._checkcrc() |
| 450 | finally: |
| 451 | self.state = None |
| 452 | self.ifp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 453 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 454 | def hexbin(inp, out): |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 455 | """hexbin(infilename, outfilename) - Decode binhexed file""" |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 456 | ifp = HexBin(inp) |
| 457 | finfo = ifp.FInfo |
| 458 | if not out: |
| 459 | out = ifp.FName |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 460 | |
Serhiy Storchaka | 46ba6c8 | 2015-04-04 11:01:02 +0300 | [diff] [blame] | 461 | with io.open(out, 'wb') as ofp: |
| 462 | # XXXX Do translation on non-mac systems |
| 463 | while True: |
| 464 | d = ifp.read(128000) |
| 465 | if not d: break |
| 466 | ofp.write(d) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 467 | ifp.close_data() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 468 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 469 | d = ifp.read_rsrc(128000) |
| 470 | if d: |
| 471 | ofp = openrsrc(out, 'wb') |
| 472 | ofp.write(d) |
Collin Winter | 115cde3 | 2007-08-28 06:06:33 +0000 | [diff] [blame] | 473 | while True: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 474 | d = ifp.read_rsrc(128000) |
| 475 | if not d: break |
| 476 | ofp.write(d) |
| 477 | ofp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 478 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 479 | ifp.close() |