annotate Lib/binhex.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 3716b116fb31
children 71a294bd7950
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 name = name
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
201 nl = len(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
202 if nl > 63:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
203 raise Error, 'Filename too long'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
204 d = chr(nl) + name + '\0'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
205 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
206
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
207 # 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
208 d3 = struct.pack('>h', finfo.Flags)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
209 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
210 info = d + d2 + d3 + d4
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
211 self._write(info)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
212 self._writecrc()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
213
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
214 def _write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
215 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
216 self.ofp.write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
217
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
218 def _writecrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
219 # XXXX Should this be here??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
220 # 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
221 self.ofp.write(struct.pack('>h', self.crc))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
222 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
223
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
224 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
225 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
226 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
227 self.dlen = self.dlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
228 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
229
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
230 def close_data(self):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
231 if self.dlen != 0:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
232 raise Error, 'Incorrect data size, diff='+`self.rlen`
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
233 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
234 self.state = _DID_DATA
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
235
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
236 def write_rsrc(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
237 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
238 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
239 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
240 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
241 self.rlen = self.rlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
242 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
243
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
244 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
245 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
246 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
247 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
248 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
249 if self.rlen != 0:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
250 raise Error, \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
251 "Incorrect resource-datasize, diff="+`self.rlen`
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
252 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
253 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
254 self.state = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
255 del self.ofp
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
256
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
257 def binhex(inp, out):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
258 """(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
259 finfo = getfileinfo(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
260 ofp = BinHex(finfo, out)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
261
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
262 ifp = open(inp, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
263 # 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
264 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
265 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
266 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
267 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
268 ofp.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
269 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
270
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
271 ifp = openrsrc(inp, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
272 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
273 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
274 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
275 ofp.write_rsrc(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
276 ofp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
277 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
278
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
279 class _Hqxdecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
280 """Read data via the decoder in 4-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
281
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
282 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
283 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
284 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
285
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
286 def read(self, totalwtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
287 """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
288 decdata = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
289 wtd = totalwtd
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
290 #
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
291 # 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
292 # 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
293 while wtd > 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
294 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
295 wtd = ((wtd+2)//3)*4
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
296 data = self.ifp.read(wtd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
297 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
298 # 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
299 # 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
300 # loop.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
301 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
302 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
303 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
304 decdatacur, self.eof = \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
305 binascii.a2b_hqx(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
306 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
307 except binascii.Incomplete:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
308 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
309 newdata = self.ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
310 if not newdata:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
311 raise Error, \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
312 'Premature EOF on binhex file'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
313 data = data + newdata
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
314 decdata = decdata + decdatacur
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
315 wtd = totalwtd - len(decdata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
316 if not decdata and not self.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
317 raise Error, 'Premature EOF on binhex file'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
318 return decdata
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
319
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
320 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
321 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
322
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
323 class _Rledecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
324 """Read data via the RLE-coder"""
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
325
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
326 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
327 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
328 self.pre_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
329 self.post_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
330 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
331
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
332 def read(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
333 if wtd > len(self.post_buffer):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
334 self._fill(wtd-len(self.post_buffer))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
335 rv = self.post_buffer[:wtd]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
336 self.post_buffer = self.post_buffer[wtd:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
337 return rv
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
338
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
339 def _fill(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
340 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
341 if self.ifp.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
342 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
343 binascii.rledecode_hqx(self.pre_buffer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
344 self.pre_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
345 return
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
346
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
347 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
348 # 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
349 # 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
350 # 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
351 # the buffer looks like:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
352 # '\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
353 # '?\220' - Keep 2 bytes: repeated something-else
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
354 # '\220\0' - Escaped \220: Keep 2 bytes.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
355 # '?\220?' - Complete repeat sequence: decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
356 # otherwise: keep 1 byte.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
357 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
358 mark = len(self.pre_buffer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
359 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
360 mark = mark - 3
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
361 elif self.pre_buffer[-1] == RUNCHAR:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
362 mark = mark - 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
363 elif self.pre_buffer[-2:] == RUNCHAR + '\0':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
364 mark = mark - 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
365 elif self.pre_buffer[-2] == RUNCHAR:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
366 pass # Decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
367 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
368 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
369
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
370 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
371 binascii.rledecode_hqx(self.pre_buffer[:mark])
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
372 self.pre_buffer = self.pre_buffer[mark:]
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
373
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
374 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
375 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
376
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
377 class HexBin:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
378 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
379 if type(ifp) == type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
380 ifp = open(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
381 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
382 # Find initial colon.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
383 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
384 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
385 ch = ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
386 if not ch:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
387 raise Error, "No binhex data found"
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
388 # 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
389 # all lines start with \r)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
390 if ch == '\r':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
391 continue
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
392 if ch == ':':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
393 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
394 if ch != '\n':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
395 dummy = ifp.readline()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
396
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
397 hqxifp = _Hqxdecoderengine(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
398 self.ifp = _Rledecoderengine(hqxifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
399 self.crc = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
400 self._readheader()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
401
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
402 def _read(self, len):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
403 data = self.ifp.read(len)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
404 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
405 return data
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
406
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
407 def _checkcrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
408 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
409 #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
410 # XXXX Is this needed??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
411 self.crc = self.crc & 0xffff
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
412 if filecrc != self.crc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
413 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
414 %(self.crc, filecrc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
415 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
416
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
417 def _readheader(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
418 len = self._read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
419 fname = self._read(ord(len))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
420 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
421 self._checkcrc()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
422
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
423 type = rest[1:5]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
424 creator = rest[5:9]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
425 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
426 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
427 self.rlen = struct.unpack('>l', rest[15:19])[0]
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
428
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
429 self.FName = fname
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
430 self.FInfo = FInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
431 self.FInfo.Creator = creator
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
432 self.FInfo.Type = type
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
433 self.FInfo.Flags = flags
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
434
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
435 self.state = _DID_HEADER
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
436
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
437 def read(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
438 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
439 raise Error, 'Read data at wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
440 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
441 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
442 n = min(n, self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
443 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
444 n = self.dlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
445 rv = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
446 while len(rv) < n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
447 rv = rv + self._read(n-len(rv))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
448 self.dlen = self.dlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
449 return rv
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
450
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
451 def close_data(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
452 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
453 raise Error, 'close_data at wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
454 if self.dlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
455 dummy = self._read(self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
456 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
457 self.state = _DID_DATA
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
458
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
459 def read_rsrc(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
460 if self.state == _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
461 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
462 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
463 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
464 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
465 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
466 n = min(n, self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
467 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
468 n = self.rlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
469 self.rlen = self.rlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
470 return self._read(n)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
471
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
472 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
473 if self.rlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
474 dummy = self.read_rsrc(self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
475 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
476 self.state = _DID_RSRC
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
477 self.ifp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
478
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
479 def hexbin(inp, out):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
480 """(infilename, outfilename) - Decode binhexed file"""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
481 ifp = HexBin(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
482 finfo = ifp.FInfo
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
483 if not out:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
484 out = ifp.FName
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
485 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
486 ofss = macfs.FSSpec(out)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
487 out = ofss.as_pathname()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
488
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
489 ofp = open(out, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
490 # XXXX Do translation on non-mac systems
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
491 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
492 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
493 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
494 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
495 ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
496 ifp.close_data()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
497
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
498 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
499 if d:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
500 ofp = openrsrc(out, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
501 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
502 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
503 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
504 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
505 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
506 ofp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
507
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
508 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
509 nfinfo = ofss.GetFInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
510 nfinfo.Creator = finfo.Creator
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
511 nfinfo.Type = finfo.Type
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
512 nfinfo.Flags = finfo.Flags
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
513 ofss.SetFInfo(nfinfo)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
514
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
515 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
516
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
517 def _test():
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
518 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
519 fss, ok = macfs.PromptGetFile('File to convert:')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
520 if not ok:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
521 sys.exit(0)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
522 fname = fss.as_pathname()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
523 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
524 fname = sys.argv[1]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
525 binhex(fname, fname+'.hqx')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
526 hexbin(fname+'.hqx', fname+'.viahqx')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
527 #hexbin(fname, fname+'.unpacked')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
528 sys.exit(1)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
529
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
530 if __name__ == '__main__':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
531 _test()