annotate Lib/binhex.py @ 39042:c02fa187e0f4 legacy-trunk

Part of SF patch #1484695. This removes dead code. The chksum was already verified in .frombuf() on the lines above. If there was a problem an exception is raised, so there was no way this condition could have been true.
author Neal Norwitz <nnorwitz@gmail.com>
date Mon, 10 Jul 2006 00:23:17 +0000
parents 454bde7e416e
children 00715353f82c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11708
4a23cc49e2b0 Actually, the previous batch's comment should have been different;
Guido van Rossum <guido@python.org>
parents: 11691
diff changeset
1 """Macintosh binhex compression/decompression.
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
2
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
3 easy interface:
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
4 binhex(inputfilename, outputfilename)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
5 hexbin(inputfilename, outputfilename)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
6 """
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
7
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
8 #
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
9 # Jack Jansen, CWI, August 1995.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
10 #
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
11 # The module is supposed to be as compatible as possible. Especially the
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
12 # easy interface should work "as expected" on any platform.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
13 # XXXX Note: currently, textfiles appear in mac-form on all platforms.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
14 # We seem to lack a simple character-translate in python.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
15 # (we should probably use ISO-Latin-1 on all but the mac platform).
13050
a195962e389c typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11708
diff changeset
16 # XXXX The simple routines are too simple: they expect to hold the complete
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
17 # files in-core. Should be fixed.
4972
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
18 # XXXX It would be nice to handle AppleDouble format on unix
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
19 # (for servers serving macs).
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
20 # XXXX I don't understand what happens when you get 0x90 times the same byte on
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
21 # input. The resulting code (xx 90 90) would appear to be interpreted as an
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
22 # escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
23 #
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
24 import sys
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
25 import os
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
26 import struct
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
27 import binascii
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
28
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
29 __all__ = ["binhex","hexbin","Error"]
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents: 15860
diff changeset
30
14022
d44603f2b928 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
31 class Error(Exception):
d44603f2b928 Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
32 pass
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
33
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
34 # States (what have we written)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
35 [_DID_HEADER, _DID_DATA, _DID_RSRC] = range(3)
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
36
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
37 # Various constants
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
38 REASONABLY_LARGE=32768 # Minimal amount we pass the rle-coder
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
39 LINELEN=64
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
40 RUNCHAR=chr(0x90) # run-length introducer
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
41
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
42 #
4972
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
43 # This code is no longer byte-order dependent
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
44
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
45 #
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
46 # Workarounds for non-mac machines.
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
47 if os.name == 'mac':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
48 import macfs
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
49 import MacOS
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
50 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
51 openrf = MacOS.openrf
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
52 except AttributeError:
13050
a195962e389c typos fixed by Rob Hooft
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11708
diff changeset
53 # Backward compatibility
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
54 openrf = open
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
55
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
56 def FInfo():
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
57 return macfs.FInfo()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
58
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
59 def getfileinfo(name):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
60 finfo = macfs.FSSpec(name).GetFInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
61 dir, file = os.path.split(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
62 # XXXX Get resource/data sizes
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
63 fp = open(name, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
64 fp.seek(0, 2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
65 dlen = fp.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
66 fp = openrf(name, '*rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
67 fp.seek(0, 2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
68 rlen = fp.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
69 return file, finfo, dlen, rlen
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
70
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
71 def openrsrc(name, *mode):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
72 if not mode:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
73 mode = '*rb'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
74 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
75 mode = '*' + mode[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
76 return openrf(name, mode)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
77
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
78 else:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
79 #
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 13050
diff changeset
80 # Glue code for non-macintosh usage
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
81 #
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
82
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
83 class FInfo:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
84 def __init__(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
85 self.Type = '????'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
86 self.Creator = '????'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
87 self.Flags = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
88
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
89 def getfileinfo(name):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
90 finfo = FInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
91 # Quick check for textfile
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
92 fp = open(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
93 data = open(name).read(256)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
94 for c in data:
18926
0aa1916cf103 Turn an octal constant into a hex constant.
Guido van Rossum <guido@python.org>
parents: 16547
diff changeset
95 if not c.isspace() and (c<' ' or ord(c) > 0x7f):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
96 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
97 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
98 finfo.Type = 'TEXT'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
99 fp.seek(0, 2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
100 dsize = fp.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
101 fp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
102 dir, file = os.path.split(name)
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
103 file = file.replace(':', '-', 1)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
104 return file, finfo, dsize, 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
105
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
106 class openrsrc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
107 def __init__(self, *args):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
108 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
109
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
110 def read(self, *args):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
111 return ''
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
112
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
113 def write(self, *args):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
114 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
115
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
116 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
117 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
118
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
119 class _Hqxcoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
120 """Write data to the coder in 3-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
121
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
122 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
123 self.ofp = ofp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
124 self.data = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
125 self.hqxdata = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
126 self.linelen = LINELEN-1
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
127
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
128 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
129 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
130 datalen = len(self.data)
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 18926
diff changeset
131 todo = (datalen//3)*3
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
132 data = self.data[:todo]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
133 self.data = self.data[todo:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
134 if not data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
135 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
136 self.hqxdata = self.hqxdata + binascii.b2a_hqx(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
137 self._flush(0)
3479
aec8bbcf8b0e new binhex from Jack (much faster)
Guido van Rossum <guido@python.org>
parents: 3474
diff changeset
138
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
139 def _flush(self, force):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
140 first = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
141 while first <= len(self.hqxdata)-self.linelen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
142 last = first + self.linelen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
143 self.ofp.write(self.hqxdata[first:last]+'\n')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
144 self.linelen = LINELEN
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
145 first = last
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
146 self.hqxdata = self.hqxdata[first:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
147 if force:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
148 self.ofp.write(self.hqxdata + ':\n')
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
149
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
150 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
151 if self.data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
152 self.hqxdata = \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
153 self.hqxdata + binascii.b2a_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
154 self._flush(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
155 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
156 del self.ofp
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
157
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
158 class _Rlecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
159 """Write data to the RLE-coder in suitably large chunks"""
3474
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
160
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
161 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
162 self.ofp = ofp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
163 self.data = ''
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
164
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
165 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
166 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
167 if len(self.data) < REASONABLY_LARGE:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
168 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
169 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
170 self.ofp.write(rledata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
171 self.data = ''
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
172
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
173 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
174 if self.data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
175 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
176 self.ofp.write(rledata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
177 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
178 del self.ofp
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
179
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
180 class BinHex:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
181 def __init__(self, (name, finfo, dlen, rlen), ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
182 if type(ofp) == type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
183 ofname = ofp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
184 ofp = open(ofname, 'w')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
185 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
186 fss = macfs.FSSpec(ofname)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
187 fss.SetCreatorType('BnHq', 'TEXT')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
188 ofp.write('(This file must be converted with BinHex 4.0)\n\n:')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
189 hqxer = _Hqxcoderengine(ofp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
190 self.ofp = _Rlecoderengine(hqxer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
191 self.crc = 0
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
192 if finfo is None:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
193 finfo = FInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
194 self.dlen = dlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
195 self.rlen = rlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
196 self._writeinfo(name, finfo)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
197 self.state = _DID_HEADER
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
198
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
199 def _writeinfo(self, name, finfo):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
200 nl = len(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
201 if nl > 63:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
202 raise Error, 'Filename too long'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
203 d = chr(nl) + name + '\0'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
204 d2 = finfo.Type + finfo.Creator
4972
6ece5cfb49ff As per GvR recomendations, remove the restriction that binhex only run on
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 3479
diff changeset
205
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
206 # Force all structs to be packed with big-endian
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
207 d3 = struct.pack('>h', finfo.Flags)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
208 d4 = struct.pack('>ii', self.dlen, self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
209 info = d + d2 + d3 + d4
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
210 self._write(info)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
211 self._writecrc()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
212
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
213 def _write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
214 self.crc = binascii.crc_hqx(data, self.crc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
215 self.ofp.write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
216
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
217 def _writecrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
218 # XXXX Should this be here??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
219 # self.crc = binascii.crc_hqx('\0\0', self.crc)
38360
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
220 if self.crc < 0:
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
221 fmt = '>h'
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
222 else:
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
223 fmt = '>H'
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
224 self.ofp.write(struct.pack(fmt, self.crc))
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
225 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
226
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
227 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
228 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
229 raise Error, 'Writing data at the wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
230 self.dlen = self.dlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
231 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
232
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
233 def close_data(self):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
234 if self.dlen != 0:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 19694
diff changeset
235 raise Error, 'Incorrect data size, diff=%r' % (self.rlen,)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
236 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
237 self.state = _DID_DATA
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
238
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
239 def write_rsrc(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
240 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
241 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
242 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
243 raise Error, 'Writing resource data at the wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
244 self.rlen = self.rlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
245 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
246
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
247 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
248 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
249 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
250 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
251 raise Error, 'Close at the wrong time'
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
252 if self.rlen != 0:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
253 raise Error, \
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 19694
diff changeset
254 "Incorrect resource-datasize, diff=%r" % (self.rlen,)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
255 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
256 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
257 self.state = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
258 del self.ofp
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
259
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
260 def binhex(inp, out):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
261 """(infilename, outfilename) - Create binhex-encoded copy of a file"""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
262 finfo = getfileinfo(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
263 ofp = BinHex(finfo, out)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
264
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
265 ifp = open(inp, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
266 # XXXX Do textfile translation on non-mac systems
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
267 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
268 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
269 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
270 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
271 ofp.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
272 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
273
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
274 ifp = openrsrc(inp, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
275 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
276 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
277 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
278 ofp.write_rsrc(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
279 ofp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
280 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
281
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
282 class _Hqxdecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
283 """Read data via the decoder in 4-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
284
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
285 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
286 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
287 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
288
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
289 def read(self, totalwtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
290 """Read at least wtd bytes (or until EOF)"""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
291 decdata = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
292 wtd = totalwtd
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
293 #
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
294 # The loop here is convoluted, since we don't really now how
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
295 # much to decode: there may be newlines in the incoming data.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
296 while wtd > 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
297 if self.eof: return decdata
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 18926
diff changeset
298 wtd = ((wtd+2)//3)*4
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
299 data = self.ifp.read(wtd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
300 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
301 # Next problem: there may not be a complete number of
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
302 # bytes in what we pass to a2b. Solve by yet another
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
303 # loop.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
304 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
305 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
306 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
307 decdatacur, self.eof = \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
308 binascii.a2b_hqx(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
309 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
310 except binascii.Incomplete:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
311 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
312 newdata = self.ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
313 if not newdata:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
314 raise Error, \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
315 'Premature EOF on binhex file'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
316 data = data + newdata
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
317 decdata = decdata + decdatacur
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
318 wtd = totalwtd - len(decdata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
319 if not decdata and not self.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
320 raise Error, 'Premature EOF on binhex file'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
321 return decdata
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
322
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
323 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
324 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
325
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
326 class _Rledecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
327 """Read data via the RLE-coder"""
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
328
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
329 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
330 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
331 self.pre_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
332 self.post_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
333 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
334
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
335 def read(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
336 if wtd > len(self.post_buffer):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
337 self._fill(wtd-len(self.post_buffer))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
338 rv = self.post_buffer[:wtd]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
339 self.post_buffer = self.post_buffer[wtd:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
340 return rv
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
341
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
342 def _fill(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
343 self.pre_buffer = self.pre_buffer + self.ifp.read(wtd+4)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
344 if self.ifp.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
345 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
346 binascii.rledecode_hqx(self.pre_buffer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
347 self.pre_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
348 return
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
349
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
350 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
351 # Obfuscated code ahead. We have to take care that we don't
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
352 # end up with an orphaned RUNCHAR later on. So, we keep a couple
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
353 # of bytes in the buffer, depending on what the end of
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
354 # the buffer looks like:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
355 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
356 # '?\220' - Keep 2 bytes: repeated something-else
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
357 # '\220\0' - Escaped \220: Keep 2 bytes.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
358 # '?\220?' - Complete repeat sequence: decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
359 # otherwise: keep 1 byte.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
360 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
361 mark = len(self.pre_buffer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
362 if self.pre_buffer[-3:] == RUNCHAR + '\0' + RUNCHAR:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
363 mark = mark - 3
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
364 elif self.pre_buffer[-1] == RUNCHAR:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
365 mark = mark - 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
366 elif self.pre_buffer[-2:] == RUNCHAR + '\0':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
367 mark = mark - 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
368 elif self.pre_buffer[-2] == RUNCHAR:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
369 pass # Decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
370 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
371 mark = mark - 1
5090
b67c02bd5e23 Merged in Jack's fixes. This brings some lines beyond 79 chars again;
Guido van Rossum <guido@python.org>
parents: 4972
diff changeset
372
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
373 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
374 binascii.rledecode_hqx(self.pre_buffer[:mark])
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
375 self.pre_buffer = self.pre_buffer[mark:]
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
376
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
377 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
378 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
379
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
380 class HexBin:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
381 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
382 if type(ifp) == type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
383 ifp = open(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
384 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
385 # Find initial colon.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
386 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
387 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
388 ch = ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
389 if not ch:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
390 raise Error, "No binhex data found"
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
391 # Cater for \r\n terminated lines (which show up as \n\r, hence
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
392 # all lines start with \r)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
393 if ch == '\r':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
394 continue
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
395 if ch == ':':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
396 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
397 if ch != '\n':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
398 dummy = ifp.readline()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
399
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
400 hqxifp = _Hqxdecoderengine(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
401 self.ifp = _Rledecoderengine(hqxifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
402 self.crc = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
403 self._readheader()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
404
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
405 def _read(self, len):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
406 data = self.ifp.read(len)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
407 self.crc = binascii.crc_hqx(data, self.crc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
408 return data
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
409
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
410 def _checkcrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
411 filecrc = struct.unpack('>h', self.ifp.read(2))[0] & 0xffff
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
412 #self.crc = binascii.crc_hqx('\0\0', self.crc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
413 # XXXX Is this needed??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
414 self.crc = self.crc & 0xffff
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
415 if filecrc != self.crc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
416 raise Error, 'CRC error, computed %x, read %x' \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
417 %(self.crc, filecrc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
418 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
419
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
420 def _readheader(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
421 len = self._read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
422 fname = self._read(ord(len))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
423 rest = self._read(1+4+4+2+4+4)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
424 self._checkcrc()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
425
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
426 type = rest[1:5]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
427 creator = rest[5:9]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
428 flags = struct.unpack('>h', rest[9:11])[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
429 self.dlen = struct.unpack('>l', rest[11:15])[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
430 self.rlen = struct.unpack('>l', rest[15:19])[0]
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
431
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
432 self.FName = fname
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
433 self.FInfo = FInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
434 self.FInfo.Creator = creator
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
435 self.FInfo.Type = type
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
436 self.FInfo.Flags = flags
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
437
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
438 self.state = _DID_HEADER
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
439
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
440 def read(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
441 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
442 raise Error, 'Read data at wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
443 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
444 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
445 n = min(n, self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
446 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
447 n = self.dlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
448 rv = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
449 while len(rv) < n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
450 rv = rv + self._read(n-len(rv))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
451 self.dlen = self.dlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
452 return rv
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
453
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
454 def close_data(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
455 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
456 raise Error, 'close_data at wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
457 if self.dlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
458 dummy = self._read(self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
459 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
460 self.state = _DID_DATA
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
461
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
462 def read_rsrc(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
463 if self.state == _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
464 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
465 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
466 raise Error, 'Read resource data at wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
467 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
468 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
469 n = min(n, self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
470 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
471 n = self.rlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
472 self.rlen = self.rlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
473 return self._read(n)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
474
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
475 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
476 if self.rlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
477 dummy = self.read_rsrc(self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
478 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
479 self.state = _DID_RSRC
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
480 self.ifp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
481
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
482 def hexbin(inp, out):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
483 """(infilename, outfilename) - Decode binhexed file"""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
484 ifp = HexBin(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
485 finfo = ifp.FInfo
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
486 if not out:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
487 out = ifp.FName
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
488 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
489 ofss = macfs.FSSpec(out)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
490 out = ofss.as_pathname()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
491
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
492 ofp = open(out, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
493 # XXXX Do translation on non-mac systems
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
494 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
495 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
496 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
497 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
498 ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
499 ifp.close_data()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
500
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
501 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
502 if d:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
503 ofp = openrsrc(out, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
504 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
505 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
506 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
507 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
508 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
509 ofp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
510
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
511 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
512 nfinfo = ofss.GetFInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
513 nfinfo.Creator = finfo.Creator
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
514 nfinfo.Type = finfo.Type
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
515 nfinfo.Flags = finfo.Flags
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
516 ofss.SetFInfo(nfinfo)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
517
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
518 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
519
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
520 def _test():
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
521 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
522 fss, ok = macfs.PromptGetFile('File to convert:')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
523 if not ok:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
524 sys.exit(0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
525 fname = fss.as_pathname()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
526 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
527 fname = sys.argv[1]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
528 binhex(fname, fname+'.hqx')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
529 hexbin(fname+'.hqx', fname+'.viahqx')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
530 #hexbin(fname, fname+'.unpacked')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
531 sys.exit(1)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
532
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
533 if __name__ == '__main__':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
534 _test()