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 | # |
| 24 | import sys |
| 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) |
| 35 | [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3) |
| 36 | |
| 37 | # Various constants |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 38 | REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder |
Guido van Rossum | a220e67 | 1996-03-23 19:19:04 +0000 | [diff] [blame] | 39 | LINELEN=64 |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 40 | RUNCHAR=chr(0x90) # run-length introducer |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 41 | |
| 42 | # |
Roger E. Masse | 469848a | 1997-01-16 16:51:57 +0000 | [diff] [blame] | 43 | # This code is no longer byte-order dependent |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 44 | |
| 45 | # |
| 46 | # Workarounds for non-mac machines. |
| 47 | if os.name == 'mac': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 48 | import macfs |
| 49 | import MacOS |
| 50 | try: |
| 51 | openrf = MacOS.openrf |
| 52 | except AttributeError: |
Jeremy Hylton | a05e293 | 2000-06-28 14:48:01 +0000 | [diff] [blame] | 53 | # Backward compatibility |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 54 | openrf = open |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 56 | def FInfo(): |
| 57 | return macfs.FInfo() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 59 | def getfileinfo(name): |
| 60 | finfo = macfs.FSSpec(name).GetFInfo() |
| 61 | dir, file = os.path.split(name) |
| 62 | # XXXX Get resource/data sizes |
| 63 | fp = open(name, 'rb') |
| 64 | fp.seek(0, 2) |
| 65 | dlen = fp.tell() |
| 66 | fp = openrf(name, '*rb') |
| 67 | fp.seek(0, 2) |
| 68 | rlen = fp.tell() |
| 69 | return file, finfo, dlen, rlen |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 70 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 71 | def openrsrc(name, *mode): |
| 72 | if not mode: |
| 73 | mode = '*rb' |
| 74 | else: |
| 75 | mode = '*' + mode[0] |
| 76 | return openrf(name, mode) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 77 | |
| 78 | else: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 79 | # |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 80 | # Glue code for non-macintosh usage |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 81 | # |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 83 | class FInfo: |
| 84 | def __init__(self): |
| 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 | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 89 | def getfileinfo(name): |
| 90 | finfo = FInfo() |
| 91 | # Quick check for textfile |
| 92 | fp = open(name) |
| 93 | data = open(name).read(256) |
| 94 | for c in data: |
Eric S. Raymond | c8c6aa2 | 2001-02-10 00:06:00 +0000 | [diff] [blame] | 95 | if not c.isspace() and (c<' ' or ord(c) > 0177): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 96 | break |
| 97 | else: |
| 98 | finfo.Type = 'TEXT' |
| 99 | fp.seek(0, 2) |
| 100 | dsize = fp.tell() |
| 101 | fp.close() |
| 102 | dir, file = os.path.split(name) |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 103 | file = file.replace(':', '-', 1) |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 104 | return file, finfo, dsize, 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 105 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 106 | class openrsrc: |
| 107 | def __init__(self, *args): |
| 108 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 110 | def read(self, *args): |
| 111 | return '' |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 113 | def write(self, *args): |
| 114 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 116 | def close(self): |
| 117 | pass |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 118 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 119 | class _Hqxcoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 120 | """Write data to the coder in 3-byte chunks""" |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 121 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 122 | def __init__(self, ofp): |
| 123 | self.ofp = ofp |
| 124 | self.data = '' |
| 125 | self.hqxdata = '' |
| 126 | self.linelen = LINELEN-1 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 128 | def write(self, data): |
| 129 | self.data = self.data + data |
| 130 | datalen = len(self.data) |
| 131 | todo = (datalen/3)*3 |
| 132 | data = self.data[:todo] |
| 133 | self.data = self.data[todo:] |
| 134 | if not data: |
| 135 | return |
| 136 | self.hqxdata = self.hqxdata + binascii.b2a_hqx(data) |
| 137 | self._flush(0) |
Guido van Rossum | cce074e | 1996-03-25 18:54:33 +0000 | [diff] [blame] | 138 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 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') |
| 144 | self.linelen = LINELEN |
| 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 | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 150 | def close(self): |
| 151 | if self.data: |
| 152 | self.hqxdata = \ |
| 153 | self.hqxdata + binascii.b2a_hqx(self.data) |
| 154 | self._flush(1) |
| 155 | self.ofp.close() |
| 156 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 157 | |
| 158 | class _Rlecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 159 | """Write data to the RLE-coder in suitably large chunks""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 161 | def __init__(self, ofp): |
| 162 | self.ofp = ofp |
| 163 | self.data = '' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 165 | def write(self, data): |
| 166 | self.data = self.data + data |
| 167 | if len(self.data) < REASONABLY_LARGE: |
| 168 | return |
| 169 | rledata = binascii.rlecode_hqx(self.data) |
| 170 | self.ofp.write(rledata) |
| 171 | self.data = '' |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 173 | def close(self): |
| 174 | if self.data: |
| 175 | rledata = binascii.rlecode_hqx(self.data) |
| 176 | self.ofp.write(rledata) |
| 177 | self.ofp.close() |
| 178 | del self.ofp |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 179 | |
| 180 | class BinHex: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 181 | def __init__(self, (name, finfo, dlen, rlen), ofp): |
| 182 | if type(ofp) == type(''): |
| 183 | ofname = ofp |
| 184 | ofp = open(ofname, 'w') |
| 185 | if os.name == 'mac': |
| 186 | fss = macfs.FSSpec(ofname) |
| 187 | fss.SetCreatorType('BnHq', 'TEXT') |
| 188 | ofp.write('(This file must be converted with BinHex 4.0)\n\n:') |
| 189 | hqxer = _Hqxcoderengine(ofp) |
| 190 | self.ofp = _Rlecoderengine(hqxer) |
| 191 | self.crc = 0 |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 192 | if finfo is None: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 193 | finfo = FInfo() |
| 194 | self.dlen = dlen |
| 195 | self.rlen = rlen |
| 196 | self._writeinfo(name, finfo) |
| 197 | self.state = _DID_HEADER |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 198 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 199 | def _writeinfo(self, name, finfo): |
| 200 | name = name |
| 201 | nl = len(name) |
| 202 | if nl > 63: |
| 203 | raise Error, 'Filename too long' |
| 204 | d = chr(nl) + name + '\0' |
| 205 | d2 = finfo.Type + finfo.Creator |
Roger E. Masse | 469848a | 1997-01-16 16:51:57 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 207 | # Force all structs to be packed with big-endian |
| 208 | d3 = struct.pack('>h', finfo.Flags) |
| 209 | d4 = struct.pack('>ii', self.dlen, self.rlen) |
| 210 | info = d + d2 + d3 + d4 |
| 211 | self._write(info) |
| 212 | self._writecrc() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 213 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 214 | def _write(self, data): |
| 215 | self.crc = binascii.crc_hqx(data, self.crc) |
| 216 | self.ofp.write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 218 | def _writecrc(self): |
| 219 | # XXXX Should this be here?? |
| 220 | # self.crc = binascii.crc_hqx('\0\0', self.crc) |
| 221 | self.ofp.write(struct.pack('>h', self.crc)) |
| 222 | self.crc = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 224 | def write(self, data): |
| 225 | if self.state != _DID_HEADER: |
| 226 | raise Error, 'Writing data at the wrong time' |
| 227 | self.dlen = self.dlen - len(data) |
| 228 | self._write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 230 | def close_data(self): |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 231 | if self.dlen != 0: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 232 | raise Error, 'Incorrect data size, diff='+`self.rlen` |
| 233 | self._writecrc() |
| 234 | self.state = _DID_DATA |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 236 | def write_rsrc(self, data): |
| 237 | if self.state < _DID_DATA: |
| 238 | self.close_data() |
| 239 | if self.state != _DID_DATA: |
| 240 | raise Error, 'Writing resource data at the wrong time' |
| 241 | self.rlen = self.rlen - len(data) |
| 242 | self._write(data) |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 243 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 244 | def close(self): |
| 245 | if self.state < _DID_DATA: |
| 246 | self.close_data() |
| 247 | if self.state != _DID_DATA: |
| 248 | raise Error, 'Close at the wrong time' |
Fred Drake | 8152d32 | 2000-12-12 23:20:45 +0000 | [diff] [blame] | 249 | if self.rlen != 0: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 250 | raise Error, \ |
| 251 | "Incorrect resource-datasize, diff="+`self.rlen` |
| 252 | self._writecrc() |
| 253 | self.ofp.close() |
| 254 | self.state = None |
| 255 | del self.ofp |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 256 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 257 | def binhex(inp, out): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 258 | """(infilename, outfilename) - Create binhex-encoded copy of a file""" |
| 259 | finfo = getfileinfo(inp) |
| 260 | ofp = BinHex(finfo, out) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 262 | ifp = open(inp, 'rb') |
| 263 | # XXXX Do textfile translation on non-mac systems |
| 264 | while 1: |
| 265 | d = ifp.read(128000) |
| 266 | if not d: break |
| 267 | ofp.write(d) |
| 268 | ofp.close_data() |
| 269 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 270 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 271 | ifp = openrsrc(inp, 'rb') |
| 272 | while 1: |
| 273 | d = ifp.read(128000) |
| 274 | if not d: break |
| 275 | ofp.write_rsrc(d) |
| 276 | ofp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 277 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 278 | |
| 279 | class _Hqxdecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 280 | """Read data via the decoder in 4-byte chunks""" |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 281 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 282 | def __init__(self, ifp): |
| 283 | self.ifp = ifp |
| 284 | self.eof = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 286 | def read(self, totalwtd): |
| 287 | """Read at least wtd bytes (or until EOF)""" |
| 288 | decdata = '' |
| 289 | wtd = totalwtd |
| 290 | # |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 291 | # 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] | 292 | # much to decode: there may be newlines in the incoming data. |
| 293 | while wtd > 0: |
| 294 | if self.eof: return decdata |
| 295 | wtd = ((wtd+2)/3)*4 |
| 296 | data = self.ifp.read(wtd) |
| 297 | # |
| 298 | # Next problem: there may not be a complete number of |
| 299 | # bytes in what we pass to a2b. Solve by yet another |
| 300 | # loop. |
| 301 | # |
| 302 | while 1: |
| 303 | try: |
| 304 | decdatacur, self.eof = \ |
| 305 | binascii.a2b_hqx(data) |
| 306 | break |
| 307 | except binascii.Incomplete: |
| 308 | pass |
| 309 | newdata = self.ifp.read(1) |
| 310 | if not newdata: |
| 311 | raise Error, \ |
| 312 | 'Premature EOF on binhex file' |
| 313 | data = data + newdata |
| 314 | decdata = decdata + decdatacur |
| 315 | wtd = totalwtd - len(decdata) |
| 316 | if not decdata and not self.eof: |
| 317 | raise Error, 'Premature EOF on binhex file' |
| 318 | return decdata |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 319 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 320 | def close(self): |
| 321 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 322 | |
| 323 | class _Rledecoderengine: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 324 | """Read data via the RLE-coder""" |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 326 | def __init__(self, ifp): |
| 327 | self.ifp = ifp |
| 328 | self.pre_buffer = '' |
| 329 | self.post_buffer = '' |
| 330 | self.eof = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 331 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 332 | def read(self, wtd): |
| 333 | if wtd > len(self.post_buffer): |
| 334 | self._fill(wtd-len(self.post_buffer)) |
| 335 | rv = self.post_buffer[:wtd] |
| 336 | self.post_buffer = self.post_buffer[wtd:] |
| 337 | return rv |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 338 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 339 | def _fill(self, wtd): |
| 340 | self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+4) |
| 341 | if self.ifp.eof: |
| 342 | self.post_buffer = self.post_buffer + \ |
| 343 | binascii.rledecode_hqx(self.pre_buffer) |
| 344 | self.pre_buffer = '' |
| 345 | return |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 347 | # |
| 348 | # Obfuscated code ahead. We have to take care that we don't |
| 349 | # end up with an orphaned RUNCHAR later on. So, we keep a couple |
| 350 | # of bytes in the buffer, depending on what the end of |
| 351 | # the buffer looks like: |
| 352 | # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0) |
| 353 | # '?\220' - Keep 2 bytes: repeated something-else |
| 354 | # '\220\0' - Escaped \220: Keep 2 bytes. |
| 355 | # '?\220?' - Complete repeat sequence: decode all |
| 356 | # otherwise: keep 1 byte. |
| 357 | # |
| 358 | mark = len(self.pre_buffer) |
| 359 | if self.pre_buffer[-3:] == RUNCHAR + '\0' + RUNCHAR: |
| 360 | mark = mark - 3 |
| 361 | elif self.pre_buffer[-1] == RUNCHAR: |
| 362 | mark = mark - 2 |
| 363 | elif self.pre_buffer[-2:] == RUNCHAR + '\0': |
| 364 | mark = mark - 2 |
| 365 | elif self.pre_buffer[-2] == RUNCHAR: |
| 366 | pass # Decode all |
| 367 | else: |
| 368 | mark = mark - 1 |
Guido van Rossum | 50b82e8 | 1997-02-11 16:39:31 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 370 | self.post_buffer = self.post_buffer + \ |
| 371 | binascii.rledecode_hqx(self.pre_buffer[:mark]) |
| 372 | self.pre_buffer = self.pre_buffer[mark:] |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 373 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 374 | def close(self): |
| 375 | self.ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 376 | |
| 377 | class HexBin: |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 378 | def __init__(self, ifp): |
| 379 | if type(ifp) == type(''): |
| 380 | ifp = open(ifp) |
| 381 | # |
| 382 | # Find initial colon. |
| 383 | # |
| 384 | while 1: |
| 385 | ch = ifp.read(1) |
| 386 | if not ch: |
| 387 | raise Error, "No binhex data found" |
| 388 | # Cater for \r\n terminated lines (which show up as \n\r, hence |
| 389 | # all lines start with \r) |
| 390 | if ch == '\r': |
| 391 | continue |
| 392 | if ch == ':': |
| 393 | break |
| 394 | if ch != '\n': |
| 395 | dummy = ifp.readline() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 397 | hqxifp = _Hqxdecoderengine(ifp) |
| 398 | self.ifp = _Rledecoderengine(hqxifp) |
| 399 | self.crc = 0 |
| 400 | self._readheader() |
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 | def _read(self, len): |
| 403 | data = self.ifp.read(len) |
| 404 | self.crc = binascii.crc_hqx(data, self.crc) |
| 405 | return data |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 406 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 407 | def _checkcrc(self): |
| 408 | filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff |
| 409 | #self.crc = binascii.crc_hqx('\0\0', self.crc) |
| 410 | # XXXX Is this needed?? |
| 411 | self.crc = self.crc & 0xffff |
| 412 | if filecrc != self.crc: |
| 413 | raise Error, 'CRC error, computed %x, read %x' \ |
| 414 | %(self.crc, filecrc) |
| 415 | self.crc = 0 |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 416 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 417 | def _readheader(self): |
| 418 | len = self._read(1) |
| 419 | fname = self._read(ord(len)) |
| 420 | rest = self._read(1+4+4+2+4+4) |
| 421 | self._checkcrc() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 422 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 423 | type = rest[1:5] |
| 424 | creator = rest[5:9] |
| 425 | flags = struct.unpack('>h', rest[9:11])[0] |
| 426 | self.dlen = struct.unpack('>l', rest[11:15])[0] |
| 427 | self.rlen = struct.unpack('>l', rest[15:19])[0] |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 428 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 429 | self.FName = fname |
| 430 | self.FInfo = FInfo() |
| 431 | self.FInfo.Creator = creator |
| 432 | self.FInfo.Type = type |
| 433 | self.FInfo.Flags = flags |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 435 | self.state = _DID_HEADER |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 436 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 437 | def read(self, *n): |
| 438 | if self.state != _DID_HEADER: |
| 439 | raise Error, 'Read data at wrong time' |
| 440 | if n: |
| 441 | n = n[0] |
| 442 | n = min(n, self.dlen) |
| 443 | else: |
| 444 | n = self.dlen |
| 445 | rv = '' |
| 446 | while len(rv) < n: |
| 447 | rv = rv + self._read(n-len(rv)) |
| 448 | self.dlen = self.dlen - n |
| 449 | return rv |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 450 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 451 | def close_data(self): |
| 452 | if self.state != _DID_HEADER: |
| 453 | raise Error, 'close_data at wrong time' |
| 454 | if self.dlen: |
| 455 | dummy = self._read(self.dlen) |
| 456 | self._checkcrc() |
| 457 | self.state = _DID_DATA |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 459 | def read_rsrc(self, *n): |
| 460 | if self.state == _DID_HEADER: |
| 461 | self.close_data() |
| 462 | if self.state != _DID_DATA: |
| 463 | raise Error, 'Read resource data at wrong time' |
| 464 | if n: |
| 465 | n = n[0] |
| 466 | n = min(n, self.rlen) |
| 467 | else: |
| 468 | n = self.rlen |
| 469 | self.rlen = self.rlen - n |
| 470 | return self._read(n) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 472 | def close(self): |
| 473 | if self.rlen: |
| 474 | dummy = self.read_rsrc(self.rlen) |
| 475 | self._checkcrc() |
| 476 | self.state = _DID_RSRC |
| 477 | self.ifp.close() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 478 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 479 | def hexbin(inp, out): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 480 | """(infilename, outfilename) - Decode binhexed file""" |
| 481 | ifp = HexBin(inp) |
| 482 | finfo = ifp.FInfo |
| 483 | if not out: |
| 484 | out = ifp.FName |
| 485 | if os.name == 'mac': |
| 486 | ofss = macfs.FSSpec(out) |
| 487 | out = ofss.as_pathname() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 488 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 489 | ofp = open(out, 'wb') |
| 490 | # XXXX Do translation on non-mac systems |
| 491 | while 1: |
| 492 | d = ifp.read(128000) |
| 493 | if not d: break |
| 494 | ofp.write(d) |
| 495 | ofp.close() |
| 496 | ifp.close_data() |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 497 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 498 | d = ifp.read_rsrc(128000) |
| 499 | if d: |
| 500 | ofp = openrsrc(out, 'wb') |
| 501 | ofp.write(d) |
| 502 | while 1: |
| 503 | d = ifp.read_rsrc(128000) |
| 504 | if not d: break |
| 505 | ofp.write(d) |
| 506 | ofp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 507 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 508 | if os.name == 'mac': |
| 509 | nfinfo = ofss.GetFInfo() |
| 510 | nfinfo.Creator = finfo.Creator |
| 511 | nfinfo.Type = finfo.Type |
| 512 | nfinfo.Flags = finfo.Flags |
| 513 | ofss.SetFInfo(nfinfo) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 515 | ifp.close() |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 516 | |
| 517 | def _test(): |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 518 | if os.name == 'mac': |
| 519 | fss, ok = macfs.PromptGetFile('File to convert:') |
| 520 | if not ok: |
| 521 | sys.exit(0) |
| 522 | fname = fss.as_pathname() |
| 523 | else: |
| 524 | fname = sys.argv[1] |
| 525 | binhex(fname, fname+'.hqx') |
| 526 | hexbin(fname+'.hqx', fname+'.viahqx') |
| 527 | #hexbin(fname, fname+'.unpacked') |
| 528 | sys.exit(1) |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 529 | |
Jack Jansen | fcdffea | 1995-08-07 14:36:51 +0000 | [diff] [blame] | 530 | if __name__ == '__main__': |
Guido van Rossum | 4acc25b | 2000-02-02 15:10:15 +0000 | [diff] [blame] | 531 | _test() |