annotate Lib/binhex.py @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents 751c6dd71b5e
children f6785bce54b5
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.
39091
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
47 try:
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
48 from Carbon.File import FSSpec, FInfo
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
49 from MacOS import openrf
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
50
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
51 def getfileinfo(name):
39091
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
52 finfo = FSSpec(name).FSpGetFInfo()
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
53 dir, file = os.path.split(name)
39091
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
54 # XXX Get resource/data sizes
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
55 fp = open(name, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
56 fp.seek(0, 2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
57 dlen = fp.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
58 fp = openrf(name, '*rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
59 fp.seek(0, 2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
60 rlen = fp.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
61 return file, finfo, dlen, rlen
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
62
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
63 def openrsrc(name, *mode):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
64 if not mode:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
65 mode = '*rb'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
66 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
67 mode = '*' + mode[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
68 return openrf(name, mode)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
69
39091
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
70 except ImportError:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
71 #
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
72 # Glue code for non-macintosh usage
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
73 #
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
74
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
75 class FInfo:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
76 def __init__(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
77 self.Type = '????'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
78 self.Creator = '????'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
79 self.Flags = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
80
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
81 def getfileinfo(name):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
82 finfo = FInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
83 # Quick check for textfile
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
84 fp = open(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
85 data = open(name).read(256)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
86 for c in data:
18926
0aa1916cf103 Turn an octal constant into a hex constant.
Guido van Rossum <guido@python.org>
parents: 16547
diff changeset
87 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
88 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
89 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
90 finfo.Type = 'TEXT'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
91 fp.seek(0, 2)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
92 dsize = fp.tell()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
93 fp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
94 dir, file = os.path.split(name)
16479
d59663d3e433 String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 16070
diff changeset
95 file = file.replace(':', '-', 1)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
96 return file, finfo, dsize, 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
97
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
98 class openrsrc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
99 def __init__(self, *args):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
100 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
101
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
102 def read(self, *args):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
103 return ''
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
104
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
105 def write(self, *args):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
106 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
107
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
108 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
109 pass
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
110
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
111 class _Hqxcoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
112 """Write data to the coder in 3-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
113
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
114 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
115 self.ofp = ofp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
116 self.data = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
117 self.hqxdata = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
118 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
119
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
120 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
121 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
122 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
123 todo = (datalen//3)*3
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
124 data = self.data[:todo]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
125 self.data = self.data[todo:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
126 if not data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
127 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
128 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
129 self._flush(0)
3479
aec8bbcf8b0e new binhex from Jack (much faster)
Guido van Rossum <guido@python.org>
parents: 3474
diff changeset
130
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
131 def _flush(self, force):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
132 first = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
133 while first <= len(self.hqxdata)-self.linelen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
134 last = first + self.linelen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
135 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
136 self.linelen = LINELEN
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
137 first = last
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
138 self.hqxdata = self.hqxdata[first:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
139 if force:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
140 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
141
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
142 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
143 if self.data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
144 self.hqxdata = \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
145 self.hqxdata + binascii.b2a_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
146 self._flush(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
147 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
148 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
149
11299e95cae8 Finally check in Jack's latest version, which fixes all known bugs.
Guido van Rossum <guido@python.org>
parents: 3142
diff changeset
150 class _Rlecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
151 """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
152
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
153 def __init__(self, ofp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
154 self.ofp = ofp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
155 self.data = ''
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
156
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
157 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
158 self.data = self.data + data
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
159 if len(self.data) < REASONABLY_LARGE:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
160 return
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
161 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
162 self.ofp.write(rledata)
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 close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
166 if self.data:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
167 rledata = binascii.rlecode_hqx(self.data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
168 self.ofp.write(rledata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
169 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
170 del self.ofp
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
171
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
172 class BinHex:
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 41793
diff changeset
173 def __init__(self, name_finfo_dlen_rlen, ofp):
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 41793
diff changeset
174 name, finfo, dlen, rlen = name_finfo_dlen_rlen
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
175 if type(ofp) == type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
176 ofname = ofp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
177 ofp = open(ofname, 'w')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
178 if os.name == 'mac':
39091
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
179 fss = FSSpec(ofname)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
180 fss.SetCreatorType('BnHq', 'TEXT')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
181 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
182 hqxer = _Hqxcoderengine(ofp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
183 self.ofp = _Rlecoderengine(hqxer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
184 self.crc = 0
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
185 if finfo is None:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
186 finfo = FInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
187 self.dlen = dlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
188 self.rlen = rlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
189 self._writeinfo(name, finfo)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
190 self.state = _DID_HEADER
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
191
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
192 def _writeinfo(self, name, finfo):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
193 nl = len(name)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
194 if nl > 63:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
195 raise Error, 'Filename too long'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
196 d = chr(nl) + name + '\0'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
197 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
198
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
199 # 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
200 d3 = struct.pack('>h', finfo.Flags)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
201 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
202 info = d + d2 + d3 + d4
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
203 self._write(info)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
204 self._writecrc()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
205
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
206 def _write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
207 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
208 self.ofp.write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
209
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
210 def _writecrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
211 # XXXX Should this be here??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
212 # 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
213 if self.crc < 0:
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
214 fmt = '>h'
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
215 else:
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
216 fmt = '>H'
454bde7e416e quick hack to fix busted binhex test
Bob Ippolito <bob@redivi.com>
parents: 32289
diff changeset
217 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
218 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
219
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
220 def write(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
221 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
222 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
223 self.dlen = self.dlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
224 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
225
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
226 def close_data(self):
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 14022
diff changeset
227 if self.dlen != 0:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 19694
diff changeset
228 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
229 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
230 self.state = _DID_DATA
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
231
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
232 def write_rsrc(self, data):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
233 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
234 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
235 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
236 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
237 self.rlen = self.rlen - len(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
238 self._write(data)
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
239
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
240 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
241 if self.state < _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
242 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
243 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
244 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
245 if self.rlen != 0:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
246 raise Error, \
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 19694
diff changeset
247 "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
248 self._writecrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
249 self.ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
250 self.state = None
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
251 del self.ofp
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
252
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
253 def binhex(inp, out):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
254 """(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
255 finfo = getfileinfo(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
256 ofp = BinHex(finfo, out)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
257
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
258 ifp = open(inp, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
259 # 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
260 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
261 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
262 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
263 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
264 ofp.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
265 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
266
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
267 ifp = openrsrc(inp, 'rb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
268 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
269 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
270 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
271 ofp.write_rsrc(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
272 ofp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
273 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
274
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
275 class _Hqxdecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
276 """Read data via the decoder in 4-byte chunks"""
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
277
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
278 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
279 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
280 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
281
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
282 def read(self, totalwtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
283 """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
284 decdata = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
285 wtd = totalwtd
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
286 #
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
287 # 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
288 # 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
289 while wtd > 0:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
290 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
291 wtd = ((wtd+2)//3)*4
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
292 data = self.ifp.read(wtd)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
293 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
294 # 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
295 # 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
296 # loop.
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 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
299 try:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
300 decdatacur, self.eof = \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
301 binascii.a2b_hqx(data)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
302 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
303 except binascii.Incomplete:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
304 pass
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
305 newdata = self.ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
306 if not newdata:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
307 raise Error, \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
308 'Premature EOF on binhex file'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
309 data = data + newdata
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
310 decdata = decdata + decdatacur
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
311 wtd = totalwtd - len(decdata)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
312 if not decdata and not self.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
313 raise Error, 'Premature EOF on binhex file'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
314 return decdata
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
315
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
316 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
317 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
318
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
319 class _Rledecoderengine:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
320 """Read data via the RLE-coder"""
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
321
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
322 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
323 self.ifp = ifp
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
324 self.pre_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
325 self.post_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
326 self.eof = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
327
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
328 def read(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
329 if wtd > len(self.post_buffer):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
330 self._fill(wtd-len(self.post_buffer))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
331 rv = self.post_buffer[:wtd]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
332 self.post_buffer = self.post_buffer[wtd:]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
333 return rv
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 _fill(self, wtd):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
336 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
337 if self.ifp.eof:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
338 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
339 binascii.rledecode_hqx(self.pre_buffer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
340 self.pre_buffer = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
341 return
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
342
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
343 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
344 # 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
345 # 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
346 # 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
347 # the buffer looks like:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
348 # '\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
349 # '?\220' - Keep 2 bytes: repeated something-else
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
350 # '\220\0' - Escaped \220: Keep 2 bytes.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
351 # '?\220?' - Complete repeat sequence: decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
352 # otherwise: keep 1 byte.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
353 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
354 mark = len(self.pre_buffer)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
355 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
356 mark = mark - 3
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
357 elif self.pre_buffer[-1] == RUNCHAR:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
358 mark = mark - 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
359 elif self.pre_buffer[-2:] == RUNCHAR + '\0':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
360 mark = mark - 2
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
361 elif self.pre_buffer[-2] == RUNCHAR:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
362 pass # Decode all
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
363 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
364 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
365
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
366 self.post_buffer = self.post_buffer + \
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
367 binascii.rledecode_hqx(self.pre_buffer[:mark])
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
368 self.pre_buffer = self.pre_buffer[mark:]
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
369
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
370 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
371 self.ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
372
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
373 class HexBin:
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
374 def __init__(self, ifp):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
375 if type(ifp) == type(''):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
376 ifp = open(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
377 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
378 # Find initial colon.
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
379 #
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
380 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
381 ch = ifp.read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
382 if not ch:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
383 raise Error, "No binhex data found"
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
384 # 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
385 # all lines start with \r)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
386 if ch == '\r':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
387 continue
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
388 if ch == ':':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
389 break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
390 if ch != '\n':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
391 dummy = ifp.readline()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
392
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
393 hqxifp = _Hqxdecoderengine(ifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
394 self.ifp = _Rledecoderengine(hqxifp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
395 self.crc = 0
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
396 self._readheader()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
397
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
398 def _read(self, len):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
399 data = self.ifp.read(len)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
400 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
401 return data
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
402
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
403 def _checkcrc(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
404 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
405 #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
406 # XXXX Is this needed??
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
407 self.crc = self.crc & 0xffff
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
408 if filecrc != self.crc:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
409 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
410 %(self.crc, filecrc)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
411 self.crc = 0
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
412
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
413 def _readheader(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
414 len = self._read(1)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
415 fname = self._read(ord(len))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
416 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
417 self._checkcrc()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
418
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
419 type = rest[1:5]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
420 creator = rest[5:9]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
421 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
422 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
423 self.rlen = struct.unpack('>l', rest[15:19])[0]
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
424
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
425 self.FName = fname
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
426 self.FInfo = FInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
427 self.FInfo.Creator = creator
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
428 self.FInfo.Type = type
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
429 self.FInfo.Flags = flags
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
430
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
431 self.state = _DID_HEADER
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
432
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
433 def read(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
434 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
435 raise Error, 'Read data at wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
436 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
437 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
438 n = min(n, self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
439 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
440 n = self.dlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
441 rv = ''
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
442 while len(rv) < n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
443 rv = rv + self._read(n-len(rv))
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
444 self.dlen = self.dlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
445 return rv
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
446
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
447 def close_data(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
448 if self.state != _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
449 raise Error, 'close_data at wrong time'
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
450 if self.dlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
451 dummy = self._read(self.dlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
452 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
453 self.state = _DID_DATA
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
454
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
455 def read_rsrc(self, *n):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
456 if self.state == _DID_HEADER:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
457 self.close_data()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
458 if self.state != _DID_DATA:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
459 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
460 if n:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
461 n = n[0]
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
462 n = min(n, self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
463 else:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
464 n = self.rlen
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
465 self.rlen = self.rlen - n
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
466 return self._read(n)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
467
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
468 def close(self):
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
469 if self.rlen:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
470 dummy = self.read_rsrc(self.rlen)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
471 self._checkcrc()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
472 self.state = _DID_RSRC
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
473 self.ifp.close()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
474
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
475 def hexbin(inp, out):
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
476 """(infilename, outfilename) - Decode binhexed file"""
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
477 ifp = HexBin(inp)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
478 finfo = ifp.FInfo
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
479 if not out:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
480 out = ifp.FName
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
481 if os.name == 'mac':
39091
00715353f82c Patch #1220874: Update the binhex module for Mach-O.
Bob Ippolito <bob@redivi.com>
parents: 38360
diff changeset
482 ofss = FSSpec(out)
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
483 out = ofss.as_pathname()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
484
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
485 ofp = open(out, 'wb')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
486 # XXXX Do translation on non-mac systems
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
487 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
488 d = ifp.read(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
489 if not d: break
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
490 ofp.write(d)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
491 ofp.close()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
492 ifp.close_data()
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
493
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
494 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
495 if d:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
496 ofp = openrsrc(out, 'wb')
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 while 1:
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
499 d = ifp.read_rsrc(128000)
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
500 if not d: break
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 ofp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
503
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
504 if os.name == 'mac':
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
505 nfinfo = ofss.GetFInfo()
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
506 nfinfo.Creator = finfo.Creator
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
507 nfinfo.Type = finfo.Type
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
508 nfinfo.Flags = finfo.Flags
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
509 ofss.SetFInfo(nfinfo)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
510
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
511 ifp.close()
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
512
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
513 def _test():
41793
c0e89d0ee0e5 Remove the macfs module. This led to the deprecation of macostools.touched();
Brett Cannon <bcannon@gmail.com>
parents: 39091
diff changeset
514 fname = sys.argv[1]
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
515 binhex(fname, fname+'.hqx')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
516 hexbin(fname+'.hqx', fname+'.viahqx')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
517 #hexbin(fname, fname+'.unpacked')
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
518 sys.exit(1)
15860
083a5c75ed90 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
519
2914
e16e18b9626c Implement binhex protocol.
Jack Jansen <jack.jansen@cwi.nl>
parents:
diff changeset
520 if __name__ == '__main__':
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 7752
diff changeset
521 _test()