]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Lib/binhex.py
1 """Macintosh binhex compression/decompression.
4 binhex(inputfilename, outputfilename)
5 hexbin(inputfilename, outputfilename)
9 # Jack Jansen, CWI, August 1995.
11 # The module is supposed to be as compatible as possible. Especially the
12 # easy interface should work "as expected" on any platform.
13 # XXXX Note: currently, textfiles appear in mac-form on all platforms.
14 # We seem to lack a simple character-translate in python.
15 # (we should probably use ISO-Latin-1 on all but the mac platform).
16 # XXXX The simple routines are too simple: they expect to hold the complete
17 # files in-core. Should be fixed.
18 # XXXX It would be nice to handle AppleDouble format on unix
19 # (for servers serving macs).
20 # XXXX I don't understand what happens when you get 0x90 times the same byte on
21 # input. The resulting code (xx 90 90) would appear to be interpreted as an
22 # escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
29 __all__
= ["binhex","hexbin","Error"]
31 class Error(Exception):
34 # States (what have we written)
39 REASONABLY_LARGE
=32768 # Minimal amount we pass the rle-coder
41 RUNCHAR
=chr(0x90) # run-length introducer
44 # This code is no longer byte-order dependent
47 # Workarounds for non-mac machines.
49 from Carbon
.File
import FSSpec
, FInfo
50 from MacOS
import openrf
52 def getfileinfo(name
):
53 finfo
= FSSpec(name
).FSpGetFInfo()
54 dir, file = os
.path
.split(name
)
55 # XXX Get resource/data sizes
59 fp
= openrf(name
, '*rb')
62 return file, finfo
, dlen
, rlen
64 def openrsrc(name
, *mode
):
69 return openrf(name
, mode
)
73 # Glue code for non-macintosh usage
82 def getfileinfo(name
):
84 # Quick check for textfile
86 data
= open(name
).read(256)
88 if not c
.isspace() and (c
<' ' or ord(c
) > 0x7f):
95 dir, file = os
.path
.split(name
)
96 file = file.replace(':', '-', 1)
97 return file, finfo
, dsize
, 0
100 def __init__(self
, *args
):
103 def read(self
, *args
):
106 def write(self
, *args
):
112 class _Hqxcoderengine
:
113 """Write data to the coder in 3-byte chunks"""
115 def __init__(self
, ofp
):
119 self
.linelen
= LINELEN
-1
121 def write(self
, data
):
122 self
.data
= self
.data
+ data
123 datalen
= len(self
.data
)
124 todo
= (datalen
//3)*3
125 data
= self
.data
[:todo
]
126 self
.data
= self
.data
[todo
:]
129 self
.hqxdata
= self
.hqxdata
+ binascii
.b2a_hqx(data
)
132 def _flush(self
, force
):
134 while first
<= len(self
.hqxdata
)-self
.linelen
:
135 last
= first
+ self
.linelen
136 self
.ofp
.write(self
.hqxdata
[first
:last
]+'\n')
137 self
.linelen
= LINELEN
139 self
.hqxdata
= self
.hqxdata
[first
:]
141 self
.ofp
.write(self
.hqxdata
+ ':\n')
146 self
.hqxdata
+ binascii
.b2a_hqx(self
.data
)
151 class _Rlecoderengine
:
152 """Write data to the RLE-coder in suitably large chunks"""
154 def __init__(self
, ofp
):
158 def write(self
, data
):
159 self
.data
= self
.data
+ data
160 if len(self
.data
) < REASONABLY_LARGE
:
162 rledata
= binascii
.rlecode_hqx(self
.data
)
163 self
.ofp
.write(rledata
)
168 rledata
= binascii
.rlecode_hqx(self
.data
)
169 self
.ofp
.write(rledata
)
174 def __init__(self
, name_finfo_dlen_rlen
, ofp
):
175 name
, finfo
, dlen
, rlen
= name_finfo_dlen_rlen
176 if type(ofp
) == type(''):
178 ofp
= open(ofname
, 'w')
179 ofp
.write('(This file must be converted with BinHex 4.0)\n\n:')
180 hqxer
= _Hqxcoderengine(ofp
)
181 self
.ofp
= _Rlecoderengine(hqxer
)
187 self
._writeinfo
(name
, finfo
)
188 self
.state
= _DID_HEADER
190 def _writeinfo(self
, name
, finfo
):
193 raise Error
, 'Filename too long'
194 d
= chr(nl
) + name
+ '\0'
195 d2
= finfo
.Type
+ finfo
.Creator
197 # Force all structs to be packed with big-endian
198 d3
= struct
.pack('>h', finfo
.Flags
)
199 d4
= struct
.pack('>ii', self
.dlen
, self
.rlen
)
200 info
= d
+ d2
+ d3
+ d4
204 def _write(self
, data
):
205 self
.crc
= binascii
.crc_hqx(data
, self
.crc
)
209 # XXXX Should this be here??
210 # self.crc = binascii.crc_hqx('\0\0', self.crc)
215 self
.ofp
.write(struct
.pack(fmt
, self
.crc
))
218 def write(self
, data
):
219 if self
.state
!= _DID_HEADER
:
220 raise Error
, 'Writing data at the wrong time'
221 self
.dlen
= self
.dlen
- len(data
)
224 def close_data(self
):
226 raise Error
, 'Incorrect data size, diff=%r' % (self
.rlen
,)
228 self
.state
= _DID_DATA
230 def write_rsrc(self
, data
):
231 if self
.state
< _DID_DATA
:
233 if self
.state
!= _DID_DATA
:
234 raise Error
, 'Writing resource data at the wrong time'
235 self
.rlen
= self
.rlen
- len(data
)
239 if self
.state
is None:
242 if self
.state
< _DID_DATA
:
244 if self
.state
!= _DID_DATA
:
245 raise Error
, 'Close at the wrong time'
248 "Incorrect resource-datasize, diff=%r" % (self
.rlen
,)
256 def binhex(inp
, out
):
257 """(infilename, outfilename) - Create binhex-encoded copy of a file"""
258 finfo
= getfileinfo(inp
)
259 ofp
= BinHex(finfo
, out
)
261 ifp
= open(inp
, 'rb')
262 # XXXX Do textfile translation on non-mac systems
270 ifp
= openrsrc(inp
, 'rb')
278 class _Hqxdecoderengine
:
279 """Read data via the decoder in 4-byte chunks"""
281 def __init__(self
, ifp
):
285 def read(self
, totalwtd
):
286 """Read at least wtd bytes (or until EOF)"""
290 # The loop here is convoluted, since we don't really now how
291 # much to decode: there may be newlines in the incoming data.
293 if self
.eof
: return decdata
295 data
= self
.ifp
.read(wtd
)
297 # Next problem: there may not be a complete number of
298 # bytes in what we pass to a2b. Solve by yet another
303 decdatacur
, self
.eof
= \
304 binascii
.a2b_hqx(data
)
306 except binascii
.Incomplete
:
308 newdata
= self
.ifp
.read(1)
311 'Premature EOF on binhex file'
312 data
= data
+ newdata
313 decdata
= decdata
+ decdatacur
314 wtd
= totalwtd
- len(decdata
)
315 if not decdata
and not self
.eof
:
316 raise Error
, 'Premature EOF on binhex file'
322 class _Rledecoderengine
:
323 """Read data via the RLE-coder"""
325 def __init__(self
, ifp
):
328 self
.post_buffer
= ''
332 if wtd
> len(self
.post_buffer
):
333 self
._fill
(wtd
-len(self
.post_buffer
))
334 rv
= self
.post_buffer
[:wtd
]
335 self
.post_buffer
= self
.post_buffer
[wtd
:]
338 def _fill(self
, wtd
):
339 self
.pre_buffer
= self
.pre_buffer
+ self
.ifp
.read(wtd
+4)
341 self
.post_buffer
= self
.post_buffer
+ \
342 binascii
.rledecode_hqx(self
.pre_buffer
)
347 # Obfuscated code ahead. We have to take care that we don't
348 # end up with an orphaned RUNCHAR later on. So, we keep a couple
349 # of bytes in the buffer, depending on what the end of
350 # the buffer looks like:
351 # '\220\0\220' - Keep 3 bytes: repeated \220 (escaped as \220\0)
352 # '?\220' - Keep 2 bytes: repeated something-else
353 # '\220\0' - Escaped \220: Keep 2 bytes.
354 # '?\220?' - Complete repeat sequence: decode all
355 # otherwise: keep 1 byte.
357 mark
= len(self
.pre_buffer
)
358 if self
.pre_buffer
[-3:] == RUNCHAR
+ '\0' + RUNCHAR
:
360 elif self
.pre_buffer
[-1] == RUNCHAR
:
362 elif self
.pre_buffer
[-2:] == RUNCHAR
+ '\0':
364 elif self
.pre_buffer
[-2] == RUNCHAR
:
369 self
.post_buffer
= self
.post_buffer
+ \
370 binascii
.rledecode_hqx(self
.pre_buffer
[:mark
])
371 self
.pre_buffer
= self
.pre_buffer
[mark
:]
377 def __init__(self
, ifp
):
378 if type(ifp
) == type(''):
381 # Find initial colon.
386 raise Error
, "No binhex data found"
387 # Cater for \r\n terminated lines (which show up as \n\r, hence
388 # all lines start with \r)
394 dummy
= ifp
.readline()
396 hqxifp
= _Hqxdecoderengine(ifp
)
397 self
.ifp
= _Rledecoderengine(hqxifp
)
401 def _read(self
, len):
402 data
= self
.ifp
.read(len)
403 self
.crc
= binascii
.crc_hqx(data
, self
.crc
)
407 filecrc
= struct
.unpack('>h', self
.ifp
.read(2))[0] & 0xffff
408 #self.crc = binascii.crc_hqx('\0\0', self.crc)
409 # XXXX Is this needed??
410 self
.crc
= self
.crc
& 0xffff
411 if filecrc
!= self
.crc
:
412 raise Error
, 'CRC error, computed %x, read %x' \
416 def _readheader(self
):
418 fname
= self
._read
(ord(len))
419 rest
= self
._read
(1+4+4+2+4+4)
424 flags
= struct
.unpack('>h', rest
[9:11])[0]
425 self
.dlen
= struct
.unpack('>l', rest
[11:15])[0]
426 self
.rlen
= struct
.unpack('>l', rest
[15:19])[0]
430 self
.FInfo
.Creator
= creator
431 self
.FInfo
.Type
= type
432 self
.FInfo
.Flags
= flags
434 self
.state
= _DID_HEADER
437 if self
.state
!= _DID_HEADER
:
438 raise Error
, 'Read data at wrong time'
441 n
= min(n
, self
.dlen
)
446 rv
= rv
+ self
._read
(n
-len(rv
))
447 self
.dlen
= self
.dlen
- n
450 def close_data(self
):
451 if self
.state
!= _DID_HEADER
:
452 raise Error
, 'close_data at wrong time'
454 dummy
= self
._read
(self
.dlen
)
456 self
.state
= _DID_DATA
458 def read_rsrc(self
, *n
):
459 if self
.state
== _DID_HEADER
:
461 if self
.state
!= _DID_DATA
:
462 raise Error
, 'Read resource data at wrong time'
465 n
= min(n
, self
.rlen
)
468 self
.rlen
= self
.rlen
- n
472 if self
.state
is None:
476 dummy
= self
.read_rsrc(self
.rlen
)
482 def hexbin(inp
, out
):
483 """(infilename, outfilename) - Decode binhexed file"""
489 ofp
= open(out
, 'wb')
490 # XXXX Do translation on non-mac systems
498 d
= ifp
.read_rsrc(128000)
500 ofp
= openrsrc(out
, 'wb')
503 d
= ifp
.read_rsrc(128000)
512 binhex(fname
, fname
+'.hqx')
513 hexbin(fname
+'.hqx', fname
+'.viahqx')
514 #hexbin(fname, fname+'.unpacked')
517 if __name__
== '__main__':