Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 1 | """binhex - Macintosh binhex compression/decompression |
| 2 | easy interface: |
| 3 | binhex(inputfilename, outputfilename) |
| 4 | hexbin(inputfilename, outputfilename) |
| 5 | """ |
| 6 | |
| 7 | # |
| 8 | # Jack Jansen, CWI, August 1995. |
| 9 | # |
| 10 | # The module is supposed to be as compatible as possible. Especially the |
| 11 | # easy interface should work "as expected" on any platform. |
| 12 | # XXXX Note: currently, textfiles appear in mac-form on all platforms. |
| 13 | # We seem to lack a simple character-translate in python. |
| 14 | # (we should probably use ISO-Latin-1 on all but the mac platform). |
| 15 | # XXXX The simeple routines are too simple: they expect to hold the complete |
| 16 | # files in-core. Should be fixed. |
| 17 | # XXXX It would be nice to handle AppleDouble format on unix (for servers serving |
| 18 | # macs). |
| 19 | # XXXX I don't understand what happens when you get 0x90 times the same byte on |
| 20 | # input. The resulting code (xx 90 90) would appear to be interpreted as an |
| 21 | # escaped *value* of 0x90. All coders I've seen appear to ignore this nicety... |
| 22 | # |
| 23 | import sys |
| 24 | import os |
| 25 | import struct |
| 26 | import string |
| 27 | import binascii |
Jack Jansen | 479c1b3 | 1995-08-14 12:41:20 +0000 | [diff] [blame] | 28 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 29 | Error = 'binhex.Error' |
| 30 | |
| 31 | # States (what have we written) |
| 32 | [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3) |
| 33 | |
| 34 | # Various constants |
| 35 | REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 36 | LINELEN=64 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 37 | RUNCHAR=chr(0x90) # run-length introducer |
| 38 | |
| 39 | # |
| 40 | # The code is currently byte-order dependent |
| 41 | if struct.pack('i', 0177) != '\0\0\0\177': |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 42 | raise ImportError, 'Module binhex is big-endian only' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 43 | |
| 44 | # |
| 45 | # Workarounds for non-mac machines. |
| 46 | if os.name == 'mac': |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 47 | import macfs |
| 48 | import MacOS |
| 49 | try: |
| 50 | openrf = MacOS.openrf |
| 51 | except AttributeError: |
| 52 | # Backward compatability |
| 53 | openrf = open |
| 54 | |
| 55 | def FInfo(): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 56 | return macfs.FInfo() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 57 | |
| 58 | def getfileinfo(name): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 59 | finfo = macfs.FSSpec(name).GetFInfo() |
| 60 | dir, file = os.path.split(name) |
| 61 | # XXXX Get resource/data sizes |
| 62 | fp = open(name, 'rb') |
| 63 | fp.seek(0, 2) |
| 64 | dlen = fp.tell() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 65 | fp = openrf(name, '*rb') |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 66 | fp.seek(0, 2) |
| 67 | rlen = fp.tell() |
| 68 | return file, finfo, dlen, rlen |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 69 | |
| 70 | def openrsrc(name, *mode): |
| 71 | if not mode: |
| 72 | mode = '*rb' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 73 | else: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 74 | mode = '*' + mode[0] |
| 75 | return openrf(name, mode) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 76 | |
| 77 | else: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 78 | # |
| 79 | # Glue code for non-macintosh useage |
| 80 | # |
| 81 | import regsub |
| 82 | |
| 83 | class FInfo: |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 84 | def __init__(self): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 85 | self.Type = '????' |
| 86 | self.Creator = '????' |
| 87 | self.Flags = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 89 | def getfileinfo(name): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 90 | finfo = FInfo() |
| 91 | # Quick check for textfile |
| 92 | fp = open(name) |
| 93 | data = open(name).read(256) |
| 94 | for c in data: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 95 | if not c in string.whitespace and (c<' ' or ord(c) > 0177): |
| 96 | break |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 97 | else: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 98 | finfo.Type = 'TEXT' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 99 | fp.seek(0, 2) |
| 100 | dsize = fp.tell() |
| 101 | fp.close() |
| 102 | dir, file = os.path.split(name) |
| 103 | file = regsub.sub(':', '-', file) |
| 104 | return file, finfo, dsize, 0 |
| 105 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 106 | class openrsrc: |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 107 | def __init__(self, *args): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 108 | pass |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 109 | |
| 110 | def read(self, *args): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 111 | return '' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 112 | |
| 113 | def write(self, *args): |
| 114 | pass |
| 115 | |
| 116 | def close(self): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 117 | pass |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 118 | |
| 119 | class _Hqxcoderengine: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 120 | """Write data to the coder in 3-byte chunks""" |
| 121 | |
| 122 | def __init__(self, ofp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 123 | self.ofp = ofp |
| 124 | self.data = '' |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 125 | self.hqxdata = '' |
| 126 | self.linelen = LINELEN-1 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 128 | def write(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 129 | self.data = self.data + data |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 130 | datalen = len(self.data) |
| 131 | todo = (datalen/3)*3 |
| 132 | data = self.data[:todo] |
| 133 | self.data = self.data[todo:] |
| 134 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(data) |
| 135 | while len(self.hqxdata) > self.linelen: |
| 136 | self.ofp.write(self.hqxdata[:self.linelen]+'\n') |
| 137 | self.hqxdata = self.hqxdata[self.linelen:] |
| 138 | self.linelen = LINELEN |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 140 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 141 | if self.data: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 142 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data) |
| 143 | while self.hqxdata: |
| 144 | self.ofp.write(self.hqxdata[:self.linelen]) |
| 145 | self.hqxdata = self.hqxdata[self.linelen:] |
| 146 | self.linelen = LINELEN |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 147 | self.ofp.write(':\n') |
| 148 | self.ofp.close() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 149 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 150 | |
| 151 | class _Rlecoderengine: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 152 | """Write data to the RLE-coder in suitably large chunks""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 153 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 154 | def __init__(self, ofp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 155 | self.ofp = ofp |
| 156 | self.data = '' |
| 157 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 158 | def write(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 159 | self.data = self.data + data |
| 160 | if len(self.data) < REASONABLY_LARGE: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 161 | return |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 162 | rledata = binascii.rlecode_hqx(self.data) |
| 163 | self.ofp.write(rledata) |
| 164 | self.data = '' |
| 165 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 166 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 167 | if self.data: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 168 | rledata = binascii.rlecode_hqx(self.data) |
| 169 | self.ofp.write(rledata) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 170 | self.ofp.close() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 171 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 172 | |
| 173 | class BinHex: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 174 | def __init__(self, (name, finfo, dlen, rlen), ofp): |
| 175 | if type(ofp) == type(''): |
| 176 | ofname = ofp |
| 177 | ofp = open(ofname, 'w') |
| 178 | if os.name == 'mac': |
| 179 | fss = macfs.FSSpec(ofname) |
| 180 | fss.SetCreatorType('BnHq', 'TEXT') |
| 181 | ofp.write('(This file must be converted with BinHex 4.0)\n\n:') |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 182 | hqxer = _Hqxcoderengine(ofp) |
| 183 | self.ofp = _Rlecoderengine(hqxer) |
| 184 | self.crc = 0 |
| 185 | if finfo == None: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 186 | finfo = FInfo() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 187 | self.dlen = dlen |
| 188 | self.rlen = rlen |
| 189 | self._writeinfo(name, finfo) |
| 190 | self.state = _DID_HEADER |
| 191 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 192 | def _writeinfo(self, name, finfo): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 193 | name = name |
| 194 | nl = len(name) |
| 195 | if nl > 63: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 196 | raise Error, 'Filename too long' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 197 | d = chr(nl) + name + '\0' |
| 198 | d2 = finfo.Type + finfo.Creator |
| 199 | d3 = struct.pack('h', finfo.Flags) |
| 200 | d4 = struct.pack('ii', self.dlen, self.rlen) |
| 201 | info = d + d2 + d3 + d4 |
| 202 | self._write(info) |
| 203 | self._writecrc() |
| 204 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 205 | def _write(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 206 | self.crc = binascii.crc_hqx(data, self.crc) |
| 207 | self.ofp.write(data) |
| 208 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 209 | def _writecrc(self): |
| 210 | ## self.crc = binascii.crc_hqx('\0\0', self.crc) # XXXX Should this be here?? |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 211 | self.ofp.write(struct.pack('h', self.crc)) |
| 212 | self.crc = 0 |
| 213 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 214 | def write(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 215 | if self.state != _DID_HEADER: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 216 | raise Error, 'Writing data at the wrong time' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 217 | self.dlen = self.dlen - len(data) |
| 218 | self._write(data) |
| 219 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 220 | def close_data(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 221 | if self.dlen <> 0: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 222 | raise Error, 'Incorrect data size, diff='+`self.rlen` |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 223 | self._writecrc() |
| 224 | self.state = _DID_DATA |
| 225 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 226 | def write_rsrc(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 227 | if self.state < _DID_DATA: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 228 | self.close_data() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 229 | if self.state != _DID_DATA: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 230 | raise Error, 'Writing resource data at the wrong time' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 231 | self.rlen = self.rlen - len(data) |
| 232 | self._write(data) |
| 233 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 234 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 235 | if self.state < _DID_DATA: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 236 | self.close_data() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 237 | if self.state != _DID_DATA: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 238 | raise Error, 'Close at the wrong time' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 239 | if self.rlen <> 0: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 240 | raise Error, "Incorrect resource-datasize, diff="+`self.rlen` |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 241 | self._writecrc() |
| 242 | self.ofp.close() |
| 243 | self.state = None |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 244 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 245 | |
| 246 | def binhex(inp, out): |
| 247 | """(infilename, outfilename) - Create binhex-encoded copy of a file""" |
| 248 | finfo = getfileinfo(inp) |
| 249 | ofp = BinHex(finfo, out) |
| 250 | |
| 251 | ifp = open(inp, 'rb') |
| 252 | # XXXX Do textfile translation on non-mac systems |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 253 | while 1: |
| 254 | d = ifp.read(128000) |
| 255 | if not d: break |
| 256 | ofp.write(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 257 | ofp.close_data() |
| 258 | ifp.close() |
| 259 | |
| 260 | ifp = openrsrc(inp, 'rb') |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 261 | while 1: |
| 262 | d = ifp.read(128000) |
| 263 | if not d: break |
| 264 | ofp.write_rsrc(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 265 | ofp.close() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 266 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 267 | |
| 268 | class _Hqxdecoderengine: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 269 | """Read data via the decoder in 4-byte chunks""" |
| 270 | |
| 271 | def __init__(self, ifp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 272 | self.ifp = ifp |
| 273 | self.eof = 0 |
| 274 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 275 | def read(self, totalwtd): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 276 | """Read at least wtd bytes (or until EOF)""" |
| 277 | decdata = '' |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 278 | wtd = totalwtd |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 279 | # |
| 280 | # The loop here is convoluted, since we don't really now how much |
| 281 | # to decode: there may be newlines in the incoming data. |
| 282 | while wtd > 0: |
| 283 | if self.eof: return decdata |
| 284 | wtd = ((wtd+2)/3)*4 |
| 285 | data = self.ifp.read(wtd) |
| 286 | # |
| 287 | # Next problem: there may not be a complete number of bytes in what we |
| 288 | # pass to a2b. Solve by yet another loop. |
| 289 | # |
| 290 | while 1: |
| 291 | try: |
| 292 | decdatacur, self.eof = binascii.a2b_hqx(data) |
| 293 | if self.eof: print 'EOF' |
| 294 | break |
| 295 | except binascii.Incomplete: |
| 296 | pass |
| 297 | newdata = self.ifp.read(1) |
| 298 | if not newdata: |
| 299 | raise Error, 'Premature EOF on binhex file' |
| 300 | data = data + newdata |
| 301 | decdata = decdata + decdatacur |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 302 | wtd = totalwtd - len(decdata) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 303 | if not decdata and not self.eof: |
| 304 | raise Error, 'Premature EOF on binhex file' |
| 305 | return decdata |
| 306 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 307 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 308 | self.ifp.close() |
| 309 | |
| 310 | class _Rledecoderengine: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 311 | """Read data via the RLE-coder""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 313 | def __init__(self, ifp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 314 | self.ifp = ifp |
| 315 | self.pre_buffer = '' |
| 316 | self.post_buffer = '' |
| 317 | self.eof = 0 |
| 318 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 319 | def read(self, wtd): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 320 | if wtd > len(self.post_buffer): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 321 | self._fill(wtd-len(self.post_buffer)) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 322 | rv = self.post_buffer[:wtd] |
| 323 | self.post_buffer = self.post_buffer[wtd:] |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 324 | return rv |
| 325 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 326 | def _fill(self, wtd): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 327 | # |
| 328 | # Obfuscated code ahead. We keep at least one byte in the pre_buffer, |
| 329 | # so we don't stumble over an orphaned RUNCHAR later on. If the |
| 330 | # last or second-last char is a RUNCHAR we keep more bytes. |
| 331 | # |
| 332 | self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+2) |
| 333 | if self.ifp.eof: |
| 334 | self.post_buffer = self.post_buffer + \ |
| 335 | binascii.rledecode_hqx(self.pre_buffer) |
| 336 | self.pre_buffer = '' |
| 337 | return |
| 338 | |
| 339 | lastrle = string.rfind(self.pre_buffer, RUNCHAR) |
| 340 | if lastrle > 0 and lastrle == len(self.pre_buffer)-1: |
| 341 | # Last byte is an RLE, keep two bytes |
| 342 | mark = len(self.pre_buffer)-2 |
| 343 | elif lastrle > 0 and lastrle == len(self.pre_buffer)-2: |
| 344 | # second-last byte is an RLE. Decode all. |
| 345 | mark = len(self.pre_buffer) |
| 346 | else: |
| 347 | mark = len(self.pre_buffer)-1 |
| 348 | self.post_buffer = self.post_buffer + \ |
| 349 | binascii.rledecode_hqx(self.pre_buffer[:mark]) |
| 350 | self.pre_buffer = self.pre_buffer[mark:] |
| 351 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 352 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 353 | self.ifp.close() |
| 354 | |
| 355 | class HexBin: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 356 | def __init__(self, ifp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 357 | if type(ifp) == type(''): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 358 | ifp = open(ifp) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 359 | # |
| 360 | # Find initial colon. |
| 361 | # |
| 362 | while 1: |
| 363 | ch = ifp.read(1) |
| 364 | if not ch: |
| 365 | raise Error, "No binhex data found" |
| 366 | if ch == ':': |
| 367 | break |
| 368 | if ch != '\n': |
| 369 | dummy = ifp.readline() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 370 | |
| 371 | hqxifp = _Hqxdecoderengine(ifp) |
| 372 | self.ifp = _Rledecoderengine(hqxifp) |
| 373 | self.crc = 0 |
| 374 | self._readheader() |
| 375 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 376 | def _read(self, len): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 377 | data = self.ifp.read(len) |
| 378 | self.crc = binascii.crc_hqx(data, self.crc) |
| 379 | return data |
| 380 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 381 | def _checkcrc(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 382 | filecrc = struct.unpack('h', self.ifp.read(2))[0] & 0xffff |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 383 | ## self.crc = binascii.crc_hqx('\0\0', self.crc) # XXXX Is this needed?? |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 384 | self.crc = self.crc & 0xffff |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 385 | if filecrc != self.crc: |
| 386 | raise Error, 'CRC error, computed %x, read %x'%(self.crc, filecrc) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 387 | self.crc = 0 |
| 388 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 389 | def _readheader(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 390 | len = self._read(1) |
| 391 | fname = self._read(ord(len)) |
| 392 | rest = self._read(1+4+4+2+4+4) |
| 393 | self._checkcrc() |
| 394 | |
| 395 | type = rest[1:5] |
| 396 | creator = rest[5:9] |
| 397 | flags = struct.unpack('h', rest[9:11])[0] |
| 398 | self.dlen = struct.unpack('l', rest[11:15])[0] |
| 399 | self.rlen = struct.unpack('l', rest[15:19])[0] |
| 400 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 401 | self.FName = fname |
| 402 | self.FInfo = FInfo() |
| 403 | self.FInfo.Creator = creator |
| 404 | self.FInfo.Type = type |
| 405 | self.FInfo.Flags = flags |
| 406 | |
| 407 | self.state = _DID_HEADER |
| 408 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 409 | def read(self, *n): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 410 | if self.state != _DID_HEADER: |
| 411 | raise Error, 'Read data at wrong time' |
| 412 | if n: |
| 413 | n = n[0] |
| 414 | n = min(n, self.dlen) |
| 415 | else: |
| 416 | n = self.dlen |
| 417 | self.dlen = self.dlen - n |
| 418 | return self._read(n) |
| 419 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 420 | def close_data(self): |
| 421 | if self.state != _DID_HEADER: |
| 422 | raise Error, 'close_data at wrong time' |
| 423 | if self.dlen: |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 424 | dummy = self._read(self.dlen) |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 425 | self._checkcrc() |
| 426 | self.state = _DID_DATA |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 427 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 428 | def read_rsrc(self, *n): |
| 429 | if self.state == _DID_HEADER: |
| 430 | self.close_data() |
| 431 | if self.state != _DID_DATA: |
| 432 | raise Error, 'Read resource data at wrong time' |
| 433 | if n: |
| 434 | n = n[0] |
| 435 | n = min(n, self.rlen) |
| 436 | else: |
| 437 | n = self.rlen |
| 438 | self.rlen = self.rlen - n |
| 439 | return self._read(n) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 440 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 441 | def close(self): |
| 442 | if self.rlen: |
| 443 | dummy = self.read_rsrc(self.rlen) |
| 444 | self._checkcrc() |
| 445 | self.state = _DID_RSRC |
| 446 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 447 | |
| 448 | def hexbin(inp, out): |
| 449 | """(infilename, outfilename) - Decode binhexed file""" |
| 450 | ifp = HexBin(inp) |
| 451 | finfo = ifp.FInfo |
| 452 | if not out: |
| 453 | out = ifp.FName |
| 454 | if os.name == 'mac': |
| 455 | ofss = macfs.FSSpec(out) |
| 456 | out = ofss.as_pathname() |
| 457 | |
| 458 | ofp = open(out, 'wb') |
| 459 | # XXXX Do translation on non-mac systems |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 460 | while 1: |
| 461 | d = ifp.read(128000) |
| 462 | if not d: break |
| 463 | ofp.write(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 464 | ofp.close() |
| 465 | ifp.close_data() |
| 466 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 467 | d = ifp.read_rsrc(128000) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 468 | if d: |
| 469 | ofp = openrsrc(out, 'wb') |
| 470 | ofp.write(d) |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame^] | 471 | while 1: |
| 472 | d = ifp.read_rsrc(128000) |
| 473 | if not d: break |
| 474 | ofp.write(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 475 | ofp.close() |
| 476 | |
| 477 | if os.name == 'mac': |
| 478 | nfinfo = ofss.GetFInfo() |
| 479 | nfinfo.Creator = finfo.Creator |
| 480 | nfinfo.Type = finfo.Type |
| 481 | nfinfo.Flags = finfo.Flags |
| 482 | ofss.SetFInfo(nfinfo) |
| 483 | |
| 484 | ifp.close() |
| 485 | |
| 486 | def _test(): |
| 487 | if os.name == 'mac': |
Jack Jansen | 479c1b3 | 1995-08-14 12:41:20 +0000 | [diff] [blame] | 488 | fss, ok = macfs.PromptGetFile('File to convert:') |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 489 | if not ok: |
| 490 | sys.exit(0) |
| 491 | fname = fss.as_pathname() |
| 492 | else: |
| 493 | fname = sys.argv[1] |
| 494 | #binhex(fname, fname+'.hqx') |
| 495 | #hexbin(fname+'.hqx', fname+'.viahqx') |
| 496 | hexbin(fname, fname+'.unpacked') |
| 497 | sys.exit(1) |
| 498 | |
| 499 | if __name__ == '__main__': |
| 500 | _test() |