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