• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Macintosh binhex compression/decompression.
2
3easy interface:
4binhex(inputfilename, outputfilename)
5hexbin(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).
16# XXXX The simple routines are too simple: they expect to hold the complete
17# files in-core. Should be fixed.
18# XXXX It would be nice to handle AppleDouble format on unix
19# (for servers serving macs).
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#
24import binascii
25import contextlib
26import io
27import os
28import struct
29import warnings
30
31warnings.warn('the binhex module is deprecated', DeprecationWarning,
32              stacklevel=2)
33
34
35__all__ = ["binhex","hexbin","Error"]
36
37class Error(Exception):
38    pass
39
40# States (what have we written)
41_DID_HEADER = 0
42_DID_DATA = 1
43
44# Various constants
45REASONABLY_LARGE = 32768  # Minimal amount we pass the rle-coder
46LINELEN = 64
47RUNCHAR = b"\x90"
48
49#
50# This code is no longer byte-order dependent
51
52
53class FInfo:
54    def __init__(self):
55        self.Type = '????'
56        self.Creator = '????'
57        self.Flags = 0
58
59def getfileinfo(name):
60    finfo = FInfo()
61    with io.open(name, 'rb') as fp:
62        # Quick check for textfile
63        data = fp.read(512)
64        if 0 not in data:
65            finfo.Type = 'TEXT'
66        fp.seek(0, 2)
67        dsize = fp.tell()
68    dir, file = os.path.split(name)
69    file = file.replace(':', '-', 1)
70    return file, finfo, dsize, 0
71
72class openrsrc:
73    def __init__(self, *args):
74        pass
75
76    def read(self, *args):
77        return b''
78
79    def write(self, *args):
80        pass
81
82    def close(self):
83        pass
84
85
86# DeprecationWarning is already emitted on "import binhex". There is no need
87# to repeat the warning at each call to deprecated binascii functions.
88@contextlib.contextmanager
89def _ignore_deprecation_warning():
90    with warnings.catch_warnings():
91        warnings.filterwarnings('ignore', '', DeprecationWarning)
92        yield
93
94
95class _Hqxcoderengine:
96    """Write data to the coder in 3-byte chunks"""
97
98    def __init__(self, ofp):
99        self.ofp = ofp
100        self.data = b''
101        self.hqxdata = b''
102        self.linelen = LINELEN - 1
103
104    def write(self, data):
105        self.data = self.data + data
106        datalen = len(self.data)
107        todo = (datalen // 3) * 3
108        data = self.data[:todo]
109        self.data = self.data[todo:]
110        if not data:
111            return
112        with _ignore_deprecation_warning():
113            self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
114        self._flush(0)
115
116    def _flush(self, force):
117        first = 0
118        while first <= len(self.hqxdata) - self.linelen:
119            last = first + self.linelen
120            self.ofp.write(self.hqxdata[first:last] + b'\r')
121            self.linelen = LINELEN
122            first = last
123        self.hqxdata = self.hqxdata[first:]
124        if force:
125            self.ofp.write(self.hqxdata + b':\r')
126
127    def close(self):
128        if self.data:
129            with _ignore_deprecation_warning():
130                self.hqxdata = self.hqxdata + binascii.b2a_hqx(self.data)
131        self._flush(1)
132        self.ofp.close()
133        del self.ofp
134
135class _Rlecoderengine:
136    """Write data to the RLE-coder in suitably large chunks"""
137
138    def __init__(self, ofp):
139        self.ofp = ofp
140        self.data = b''
141
142    def write(self, data):
143        self.data = self.data + data
144        if len(self.data) < REASONABLY_LARGE:
145            return
146        with _ignore_deprecation_warning():
147            rledata = binascii.rlecode_hqx(self.data)
148        self.ofp.write(rledata)
149        self.data = b''
150
151    def close(self):
152        if self.data:
153            with _ignore_deprecation_warning():
154                rledata = binascii.rlecode_hqx(self.data)
155            self.ofp.write(rledata)
156        self.ofp.close()
157        del self.ofp
158
159class BinHex:
160    def __init__(self, name_finfo_dlen_rlen, ofp):
161        name, finfo, dlen, rlen = name_finfo_dlen_rlen
162        close_on_error = False
163        if isinstance(ofp, str):
164            ofname = ofp
165            ofp = io.open(ofname, 'wb')
166            close_on_error = True
167        try:
168            ofp.write(b'(This file must be converted with BinHex 4.0)\r\r:')
169            hqxer = _Hqxcoderengine(ofp)
170            self.ofp = _Rlecoderengine(hqxer)
171            self.crc = 0
172            if finfo is None:
173                finfo = FInfo()
174            self.dlen = dlen
175            self.rlen = rlen
176            self._writeinfo(name, finfo)
177            self.state = _DID_HEADER
178        except:
179            if close_on_error:
180                ofp.close()
181            raise
182
183    def _writeinfo(self, name, finfo):
184        nl = len(name)
185        if nl > 63:
186            raise Error('Filename too long')
187        d = bytes([nl]) + name.encode("latin-1") + b'\0'
188        tp, cr = finfo.Type, finfo.Creator
189        if isinstance(tp, str):
190            tp = tp.encode("latin-1")
191        if isinstance(cr, str):
192            cr = cr.encode("latin-1")
193        d2 = tp + cr
194
195        # Force all structs to be packed with big-endian
196        d3 = struct.pack('>h', finfo.Flags)
197        d4 = struct.pack('>ii', self.dlen, self.rlen)
198        info = d + d2 + d3 + d4
199        self._write(info)
200        self._writecrc()
201
202    def _write(self, data):
203        self.crc = binascii.crc_hqx(data, self.crc)
204        self.ofp.write(data)
205
206    def _writecrc(self):
207        # XXXX Should this be here??
208        # self.crc = binascii.crc_hqx('\0\0', self.crc)
209        if self.crc < 0:
210            fmt = '>h'
211        else:
212            fmt = '>H'
213        self.ofp.write(struct.pack(fmt, self.crc))
214        self.crc = 0
215
216    def write(self, data):
217        if self.state != _DID_HEADER:
218            raise Error('Writing data at the wrong time')
219        self.dlen = self.dlen - len(data)
220        self._write(data)
221
222    def close_data(self):
223        if self.dlen != 0:
224            raise Error('Incorrect data size, diff=%r' % (self.rlen,))
225        self._writecrc()
226        self.state = _DID_DATA
227
228    def write_rsrc(self, data):
229        if self.state < _DID_DATA:
230            self.close_data()
231        if self.state != _DID_DATA:
232            raise Error('Writing resource data at the wrong time')
233        self.rlen = self.rlen - len(data)
234        self._write(data)
235
236    def close(self):
237        if self.state is None:
238            return
239        try:
240            if self.state < _DID_DATA:
241                self.close_data()
242            if self.state != _DID_DATA:
243                raise Error('Close at the wrong time')
244            if self.rlen != 0:
245                raise Error("Incorrect resource-datasize, diff=%r" % (self.rlen,))
246            self._writecrc()
247        finally:
248            self.state = None
249            ofp = self.ofp
250            del self.ofp
251            ofp.close()
252
253def binhex(inp, out):
254    """binhex(infilename, outfilename): create binhex-encoded copy of a file"""
255    finfo = getfileinfo(inp)
256    ofp = BinHex(finfo, out)
257
258    with io.open(inp, 'rb') as ifp:
259        # XXXX Do textfile translation on non-mac systems
260        while True:
261            d = ifp.read(128000)
262            if not d: break
263            ofp.write(d)
264        ofp.close_data()
265
266    ifp = openrsrc(inp, 'rb')
267    while True:
268        d = ifp.read(128000)
269        if not d: break
270        ofp.write_rsrc(d)
271    ofp.close()
272    ifp.close()
273
274class _Hqxdecoderengine:
275    """Read data via the decoder in 4-byte chunks"""
276
277    def __init__(self, ifp):
278        self.ifp = ifp
279        self.eof = 0
280
281    def read(self, totalwtd):
282        """Read at least wtd bytes (or until EOF)"""
283        decdata = b''
284        wtd = totalwtd
285        #
286        # The loop here is convoluted, since we don't really now how
287        # much 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
294            # bytes in what we pass to a2b. Solve by yet another
295            # loop.
296            #
297            while True:
298                try:
299                    with _ignore_deprecation_warning():
300                        decdatacur, self.eof = binascii.a2b_hqx(data)
301                    break
302                except binascii.Incomplete:
303                    pass
304                newdata = self.ifp.read(1)
305                if not newdata:
306                    raise Error('Premature EOF on binhex file')
307                data = data + newdata
308            decdata = decdata + decdatacur
309            wtd = totalwtd - len(decdata)
310            if not decdata and not self.eof:
311                raise Error('Premature EOF on binhex file')
312        return decdata
313
314    def close(self):
315        self.ifp.close()
316
317class _Rledecoderengine:
318    """Read data via the RLE-coder"""
319
320    def __init__(self, ifp):
321        self.ifp = ifp
322        self.pre_buffer = b''
323        self.post_buffer = b''
324        self.eof = 0
325
326    def read(self, wtd):
327        if wtd > len(self.post_buffer):
328            self._fill(wtd - len(self.post_buffer))
329        rv = self.post_buffer[:wtd]
330        self.post_buffer = self.post_buffer[wtd:]
331        return rv
332
333    def _fill(self, wtd):
334        self.pre_buffer = self.pre_buffer + self.ifp.read(wtd + 4)
335        if self.ifp.eof:
336            with _ignore_deprecation_warning():
337                self.post_buffer = self.post_buffer + \
338                    binascii.rledecode_hqx(self.pre_buffer)
339            self.pre_buffer = b''
340            return
341
342        #
343        # Obfuscated code ahead. We have to take care that we don't
344        # end up with an orphaned RUNCHAR later on. So, we keep a couple
345        # of bytes in the buffer, depending on what the end of
346        # the buffer looks like:
347        # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
348        # '?\220' - Keep 2 bytes: repeated something-else
349        # '\220\0' - Escaped \220: Keep 2 bytes.
350        # '?\220?' - Complete repeat sequence: decode all
351        # otherwise: keep 1 byte.
352        #
353        mark = len(self.pre_buffer)
354        if self.pre_buffer[-3:] == RUNCHAR + b'\0' + RUNCHAR:
355            mark = mark - 3
356        elif self.pre_buffer[-1:] == RUNCHAR:
357            mark = mark - 2
358        elif self.pre_buffer[-2:] == RUNCHAR + b'\0':
359            mark = mark - 2
360        elif self.pre_buffer[-2:-1] == RUNCHAR:
361            pass # Decode all
362        else:
363            mark = mark - 1
364
365        with _ignore_deprecation_warning():
366            self.post_buffer = self.post_buffer + \
367                binascii.rledecode_hqx(self.pre_buffer[:mark])
368        self.pre_buffer = self.pre_buffer[mark:]
369
370    def close(self):
371        self.ifp.close()
372
373class HexBin:
374    def __init__(self, ifp):
375        if isinstance(ifp, str):
376            ifp = io.open(ifp, 'rb')
377        #
378        # Find initial colon.
379        #
380        while True:
381            ch = ifp.read(1)
382            if not ch:
383                raise Error("No binhex data found")
384            # Cater for \r\n terminated lines (which show up as \n\r, hence
385            # all lines start with \r)
386            if ch == b'\r':
387                continue
388            if ch == b':':
389                break
390
391        hqxifp = _Hqxdecoderengine(ifp)
392        self.ifp = _Rledecoderengine(hqxifp)
393        self.crc = 0
394        self._readheader()
395
396    def _read(self, len):
397        data = self.ifp.read(len)
398        self.crc = binascii.crc_hqx(data, self.crc)
399        return data
400
401    def _checkcrc(self):
402        filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
403        #self.crc = binascii.crc_hqx('\0\0', self.crc)
404        # XXXX Is this needed??
405        self.crc = self.crc & 0xffff
406        if filecrc != self.crc:
407            raise Error('CRC error, computed %x, read %x'
408                        % (self.crc, filecrc))
409        self.crc = 0
410
411    def _readheader(self):
412        len = self._read(1)
413        fname = self._read(ord(len))
414        rest = self._read(1 + 4 + 4 + 2 + 4 + 4)
415        self._checkcrc()
416
417        type = rest[1:5]
418        creator = rest[5:9]
419        flags = struct.unpack('>h', rest[9:11])[0]
420        self.dlen = struct.unpack('>l', rest[11:15])[0]
421        self.rlen = struct.unpack('>l', rest[15:19])[0]
422
423        self.FName = fname
424        self.FInfo = FInfo()
425        self.FInfo.Creator = creator
426        self.FInfo.Type = type
427        self.FInfo.Flags = flags
428
429        self.state = _DID_HEADER
430
431    def read(self, *n):
432        if self.state != _DID_HEADER:
433            raise Error('Read data at wrong time')
434        if n:
435            n = n[0]
436            n = min(n, self.dlen)
437        else:
438            n = self.dlen
439        rv = b''
440        while len(rv) < n:
441            rv = rv + self._read(n-len(rv))
442        self.dlen = self.dlen - n
443        return rv
444
445    def close_data(self):
446        if self.state != _DID_HEADER:
447            raise Error('close_data at wrong time')
448        if self.dlen:
449            dummy = self._read(self.dlen)
450        self._checkcrc()
451        self.state = _DID_DATA
452
453    def read_rsrc(self, *n):
454        if self.state == _DID_HEADER:
455            self.close_data()
456        if self.state != _DID_DATA:
457            raise Error('Read resource data at wrong time')
458        if n:
459            n = n[0]
460            n = min(n, self.rlen)
461        else:
462            n = self.rlen
463        self.rlen = self.rlen - n
464        return self._read(n)
465
466    def close(self):
467        if self.state is None:
468            return
469        try:
470            if self.rlen:
471                dummy = self.read_rsrc(self.rlen)
472            self._checkcrc()
473        finally:
474            self.state = None
475            self.ifp.close()
476
477def hexbin(inp, out):
478    """hexbin(infilename, outfilename) - Decode binhexed file"""
479    ifp = HexBin(inp)
480    finfo = ifp.FInfo
481    if not out:
482        out = ifp.FName
483
484    with io.open(out, 'wb') as ofp:
485        # XXXX Do translation on non-mac systems
486        while True:
487            d = ifp.read(128000)
488            if not d: break
489            ofp.write(d)
490    ifp.close_data()
491
492    d = ifp.read_rsrc(128000)
493    if d:
494        ofp = openrsrc(out, 'wb')
495        ofp.write(d)
496        while True:
497            d = ifp.read_rsrc(128000)
498            if not d: break
499            ofp.write(d)
500        ofp.close()
501
502    ifp.close()
503