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:] |
Guido van Rossum | cce074e | 1996-03-25 18:54:33 +0000 | [diff] [blame] | 134 | if not data: |
| 135 | return |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 136 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(data) |
Guido van Rossum | cce074e | 1996-03-25 18:54:33 +0000 | [diff] [blame] | 137 | self._flush(0) |
| 138 | |
| 139 | def _flush(self, force): |
| 140 | first = 0 |
| 141 | while first <= len(self.hqxdata)-self.linelen: |
| 142 | last = first + self.linelen |
| 143 | self.ofp.write(self.hqxdata[first:last]+'\n') |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 144 | self.linelen = LINELEN |
Guido van Rossum | cce074e | 1996-03-25 18:54:33 +0000 | [diff] [blame] | 145 | first = last |
| 146 | self.hqxdata = self.hqxdata[first:] |
| 147 | if force: |
| 148 | self.ofp.write(self.hqxdata + ':\n') |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 150 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 151 | if self.data: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 152 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data) |
Guido van Rossum | cce074e | 1996-03-25 18:54:33 +0000 | [diff] [blame] | 153 | self._flush(1) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 154 | self.ofp.close() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 155 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 156 | |
| 157 | class _Rlecoderengine: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 158 | """Write data to the RLE-coder in suitably large chunks""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 159 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 160 | def __init__(self, ofp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 161 | self.ofp = ofp |
| 162 | self.data = '' |
| 163 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 164 | def write(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 165 | self.data = self.data + data |
| 166 | if len(self.data) < REASONABLY_LARGE: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 167 | return |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 168 | rledata = binascii.rlecode_hqx(self.data) |
| 169 | self.ofp.write(rledata) |
| 170 | self.data = '' |
| 171 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 172 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 173 | if self.data: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 174 | rledata = binascii.rlecode_hqx(self.data) |
| 175 | self.ofp.write(rledata) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 176 | self.ofp.close() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 177 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 178 | |
| 179 | class BinHex: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 180 | def __init__(self, (name, finfo, dlen, rlen), ofp): |
| 181 | if type(ofp) == type(''): |
| 182 | ofname = ofp |
| 183 | ofp = open(ofname, 'w') |
| 184 | if os.name == 'mac': |
| 185 | fss = macfs.FSSpec(ofname) |
| 186 | fss.SetCreatorType('BnHq', 'TEXT') |
| 187 | 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] | 188 | hqxer = _Hqxcoderengine(ofp) |
| 189 | self.ofp = _Rlecoderengine(hqxer) |
| 190 | self.crc = 0 |
| 191 | if finfo == None: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 192 | finfo = FInfo() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 193 | self.dlen = dlen |
| 194 | self.rlen = rlen |
| 195 | self._writeinfo(name, finfo) |
| 196 | self.state = _DID_HEADER |
| 197 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 198 | def _writeinfo(self, name, finfo): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 199 | name = name |
| 200 | nl = len(name) |
| 201 | if nl > 63: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 202 | raise Error, 'Filename too long' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 203 | d = chr(nl) + name + '\0' |
| 204 | d2 = finfo.Type + finfo.Creator |
| 205 | d3 = struct.pack('h', finfo.Flags) |
| 206 | d4 = struct.pack('ii', self.dlen, self.rlen) |
| 207 | info = d + d2 + d3 + d4 |
| 208 | self._write(info) |
| 209 | self._writecrc() |
| 210 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 211 | def _write(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 212 | self.crc = binascii.crc_hqx(data, self.crc) |
| 213 | self.ofp.write(data) |
| 214 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 215 | def _writecrc(self): |
| 216 | ## 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] | 217 | self.ofp.write(struct.pack('h', self.crc)) |
| 218 | self.crc = 0 |
| 219 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 220 | def write(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 221 | if self.state != _DID_HEADER: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 222 | raise Error, 'Writing data at the wrong time' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 223 | self.dlen = self.dlen - len(data) |
| 224 | self._write(data) |
| 225 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 226 | def close_data(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 227 | if self.dlen <> 0: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 228 | raise Error, 'Incorrect data size, diff='+`self.rlen` |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 229 | self._writecrc() |
| 230 | self.state = _DID_DATA |
| 231 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 232 | def write_rsrc(self, data): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 233 | if self.state < _DID_DATA: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 234 | self.close_data() |
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 | raise Error, 'Writing resource data at the wrong time' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 237 | self.rlen = self.rlen - len(data) |
| 238 | self._write(data) |
| 239 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 240 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 241 | if self.state < _DID_DATA: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 242 | self.close_data() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 243 | if self.state != _DID_DATA: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 244 | raise Error, 'Close at the wrong time' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 245 | if self.rlen <> 0: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 246 | raise Error, "Incorrect resource-datasize, diff="+`self.rlen` |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 247 | self._writecrc() |
| 248 | self.ofp.close() |
| 249 | self.state = None |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 250 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 251 | |
| 252 | def binhex(inp, out): |
| 253 | """(infilename, outfilename) - Create binhex-encoded copy of a file""" |
| 254 | finfo = getfileinfo(inp) |
| 255 | ofp = BinHex(finfo, out) |
| 256 | |
| 257 | ifp = open(inp, 'rb') |
| 258 | # XXXX Do textfile translation on non-mac systems |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 259 | while 1: |
| 260 | d = ifp.read(128000) |
| 261 | if not d: break |
| 262 | ofp.write(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 263 | ofp.close_data() |
| 264 | ifp.close() |
| 265 | |
| 266 | ifp = openrsrc(inp, 'rb') |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 267 | while 1: |
| 268 | d = ifp.read(128000) |
| 269 | if not d: break |
| 270 | ofp.write_rsrc(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 271 | ofp.close() |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 272 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 273 | |
| 274 | class _Hqxdecoderengine: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 275 | """Read data via the decoder in 4-byte chunks""" |
| 276 | |
| 277 | def __init__(self, ifp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 278 | self.ifp = ifp |
| 279 | self.eof = 0 |
| 280 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 281 | def read(self, totalwtd): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 282 | """Read at least wtd bytes (or until EOF)""" |
| 283 | decdata = '' |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 284 | wtd = totalwtd |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 285 | # |
| 286 | # The loop here is convoluted, since we don't really now how much |
| 287 | # to decode: there may be newlines in the incoming data. |
| 288 | while wtd > 0: |
| 289 | if self.eof: return decdata |
| 290 | wtd = ((wtd+2)/3)*4 |
| 291 | data = self.ifp.read(wtd) |
| 292 | # |
| 293 | # Next problem: there may not be a complete number of bytes in what we |
| 294 | # pass to a2b. Solve by yet another loop. |
| 295 | # |
| 296 | while 1: |
| 297 | try: |
| 298 | decdatacur, self.eof = binascii.a2b_hqx(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 299 | break |
| 300 | except binascii.Incomplete: |
| 301 | pass |
| 302 | newdata = self.ifp.read(1) |
| 303 | if not newdata: |
| 304 | raise Error, 'Premature EOF on binhex file' |
| 305 | data = data + newdata |
| 306 | decdata = decdata + decdatacur |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 307 | wtd = totalwtd - len(decdata) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 308 | if not decdata and not self.eof: |
| 309 | raise Error, 'Premature EOF on binhex file' |
| 310 | return decdata |
| 311 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 312 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 313 | self.ifp.close() |
| 314 | |
| 315 | class _Rledecoderengine: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 316 | """Read data via the RLE-coder""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 317 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 318 | def __init__(self, ifp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 319 | self.ifp = ifp |
| 320 | self.pre_buffer = '' |
| 321 | self.post_buffer = '' |
| 322 | self.eof = 0 |
| 323 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 324 | def read(self, wtd): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 325 | if wtd > len(self.post_buffer): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 326 | self._fill(wtd-len(self.post_buffer)) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 327 | rv = self.post_buffer[:wtd] |
| 328 | self.post_buffer = self.post_buffer[wtd:] |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 329 | return rv |
| 330 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 331 | def _fill(self, wtd): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 332 | # |
| 333 | # Obfuscated code ahead. We keep at least one byte in the pre_buffer, |
| 334 | # so we don't stumble over an orphaned RUNCHAR later on. If the |
| 335 | # last or second-last char is a RUNCHAR we keep more bytes. |
| 336 | # |
| 337 | self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+2) |
| 338 | if self.ifp.eof: |
| 339 | self.post_buffer = self.post_buffer + \ |
| 340 | binascii.rledecode_hqx(self.pre_buffer) |
| 341 | self.pre_buffer = '' |
| 342 | return |
| 343 | |
| 344 | lastrle = string.rfind(self.pre_buffer, RUNCHAR) |
| 345 | if lastrle > 0 and lastrle == len(self.pre_buffer)-1: |
| 346 | # Last byte is an RLE, keep two bytes |
| 347 | mark = len(self.pre_buffer)-2 |
| 348 | elif lastrle > 0 and lastrle == len(self.pre_buffer)-2: |
| 349 | # second-last byte is an RLE. Decode all. |
| 350 | mark = len(self.pre_buffer) |
| 351 | else: |
| 352 | mark = len(self.pre_buffer)-1 |
| 353 | self.post_buffer = self.post_buffer + \ |
| 354 | binascii.rledecode_hqx(self.pre_buffer[:mark]) |
| 355 | self.pre_buffer = self.pre_buffer[mark:] |
| 356 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 357 | def close(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 358 | self.ifp.close() |
| 359 | |
| 360 | class HexBin: |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 361 | def __init__(self, ifp): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 362 | if type(ifp) == type(''): |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 363 | ifp = open(ifp) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 364 | # |
| 365 | # Find initial colon. |
| 366 | # |
| 367 | while 1: |
| 368 | ch = ifp.read(1) |
| 369 | if not ch: |
| 370 | raise Error, "No binhex data found" |
| 371 | if ch == ':': |
| 372 | break |
| 373 | if ch != '\n': |
| 374 | dummy = ifp.readline() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 375 | |
| 376 | hqxifp = _Hqxdecoderengine(ifp) |
| 377 | self.ifp = _Rledecoderengine(hqxifp) |
| 378 | self.crc = 0 |
| 379 | self._readheader() |
| 380 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 381 | def _read(self, len): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 382 | data = self.ifp.read(len) |
| 383 | self.crc = binascii.crc_hqx(data, self.crc) |
| 384 | return data |
| 385 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 386 | def _checkcrc(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 387 | filecrc = struct.unpack('h', self.ifp.read(2))[0] & 0xffff |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 388 | ## 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] | 389 | self.crc = self.crc & 0xffff |
Jack Jansen | 685e16d | 1995-10-03 14:41:15 +0000 | [diff] [blame] | 390 | if filecrc != self.crc: |
| 391 | raise Error, 'CRC error, computed %x, read %x'%(self.crc, filecrc) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 392 | self.crc = 0 |
| 393 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 394 | def _readheader(self): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 395 | len = self._read(1) |
| 396 | fname = self._read(ord(len)) |
| 397 | rest = self._read(1+4+4+2+4+4) |
| 398 | self._checkcrc() |
| 399 | |
| 400 | type = rest[1:5] |
| 401 | creator = rest[5:9] |
| 402 | flags = struct.unpack('h', rest[9:11])[0] |
| 403 | self.dlen = struct.unpack('l', rest[11:15])[0] |
| 404 | self.rlen = struct.unpack('l', rest[15:19])[0] |
| 405 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 406 | self.FName = fname |
| 407 | self.FInfo = FInfo() |
| 408 | self.FInfo.Creator = creator |
| 409 | self.FInfo.Type = type |
| 410 | self.FInfo.Flags = flags |
| 411 | |
| 412 | self.state = _DID_HEADER |
| 413 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 414 | def read(self, *n): |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 415 | if self.state != _DID_HEADER: |
| 416 | raise Error, 'Read data at wrong time' |
| 417 | if n: |
| 418 | n = n[0] |
| 419 | n = min(n, self.dlen) |
| 420 | else: |
| 421 | n = self.dlen |
| 422 | self.dlen = self.dlen - n |
| 423 | return self._read(n) |
| 424 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 425 | def close_data(self): |
| 426 | if self.state != _DID_HEADER: |
| 427 | raise Error, 'close_data at wrong time' |
| 428 | if self.dlen: |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 429 | dummy = self._read(self.dlen) |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 430 | self._checkcrc() |
| 431 | self.state = _DID_DATA |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 433 | def read_rsrc(self, *n): |
| 434 | if self.state == _DID_HEADER: |
| 435 | self.close_data() |
| 436 | if self.state != _DID_DATA: |
| 437 | raise Error, 'Read resource data at wrong time' |
| 438 | if n: |
| 439 | n = n[0] |
| 440 | n = min(n, self.rlen) |
| 441 | else: |
| 442 | n = self.rlen |
| 443 | self.rlen = self.rlen - n |
| 444 | return self._read(n) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 446 | def close(self): |
| 447 | if self.rlen: |
| 448 | dummy = self.read_rsrc(self.rlen) |
| 449 | self._checkcrc() |
| 450 | self.state = _DID_RSRC |
| 451 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 452 | |
| 453 | def hexbin(inp, out): |
| 454 | """(infilename, outfilename) - Decode binhexed file""" |
| 455 | ifp = HexBin(inp) |
| 456 | finfo = ifp.FInfo |
| 457 | if not out: |
| 458 | out = ifp.FName |
| 459 | if os.name == 'mac': |
| 460 | ofss = macfs.FSSpec(out) |
| 461 | out = ofss.as_pathname() |
| 462 | |
| 463 | ofp = open(out, 'wb') |
| 464 | # XXXX Do translation on non-mac systems |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 465 | while 1: |
| 466 | d = ifp.read(128000) |
| 467 | if not d: break |
| 468 | ofp.write(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 469 | ofp.close() |
| 470 | ifp.close_data() |
| 471 | |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 472 | d = ifp.read_rsrc(128000) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 473 | if d: |
| 474 | ofp = openrsrc(out, 'wb') |
| 475 | ofp.write(d) |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 476 | while 1: |
| 477 | d = ifp.read_rsrc(128000) |
| 478 | if not d: break |
| 479 | ofp.write(d) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 480 | ofp.close() |
| 481 | |
| 482 | if os.name == 'mac': |
| 483 | nfinfo = ofss.GetFInfo() |
| 484 | nfinfo.Creator = finfo.Creator |
| 485 | nfinfo.Type = finfo.Type |
| 486 | nfinfo.Flags = finfo.Flags |
| 487 | ofss.SetFInfo(nfinfo) |
| 488 | |
| 489 | ifp.close() |
| 490 | |
| 491 | def _test(): |
| 492 | if os.name == 'mac': |
Jack Jansen | 479c1b3 | 1995-08-14 12:41:20 +0000 | [diff] [blame] | 493 | fss, ok = macfs.PromptGetFile('File to convert:') |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 494 | if not ok: |
| 495 | sys.exit(0) |
| 496 | fname = fss.as_pathname() |
| 497 | else: |
| 498 | fname = sys.argv[1] |
| 499 | #binhex(fname, fname+'.hqx') |
| 500 | #hexbin(fname+'.hqx', fname+'.viahqx') |
| 501 | hexbin(fname, fname+'.unpacked') |
| 502 | sys.exit(1) |
| 503 | |
| 504 | if __name__ == '__main__': |
| 505 | _test() |