<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import struct <EOL> import binascii <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Error ( Exception ) : <EOL> pass <EOL> _DID_HEADER = <NUM_LIT:0> <EOL> _DID_DATA = <NUM_LIT:1> <EOL> REASONABLY_LARGE = <NUM_LIT> <EOL> LINELEN = <NUM_LIT:64> <EOL> RUNCHAR = chr ( <NUM_LIT> ) <EOL> try : <EOL> from Carbon . File import FSSpec , FInfo <EOL> from MacOS import openrf <EOL> def getfileinfo ( name ) : <EOL> finfo = FSSpec ( name ) . FSpGetFInfo ( ) <EOL> dir , file = os . path . split ( name ) <EOL> fp = open ( name , '<STR_LIT:rb>' ) <EOL> fp . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> dlen = fp . tell ( ) <EOL> fp = openrf ( name , '<STR_LIT>' ) <EOL> fp . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> rlen = fp . tell ( ) <EOL> return file , finfo , dlen , rlen <EOL> def openrsrc ( name , * mode ) : <EOL> if not mode : <EOL> mode = '<STR_LIT>' <EOL> else : <EOL> mode = '<STR_LIT:*>' + mode [ <NUM_LIT:0> ] <EOL> return openrf ( name , mode ) <EOL> except ImportError : <EOL> class FInfo : <EOL> def __init__ ( self ) : <EOL> self . Type = '<STR_LIT>' <EOL> self . Creator = '<STR_LIT>' <EOL> self . Flags = <NUM_LIT:0> <EOL> def getfileinfo ( name ) : <EOL> finfo = FInfo ( ) <EOL> fp = open ( name ) <EOL> data = open ( name ) . read ( <NUM_LIT> ) <EOL> for c in data : <EOL> if not c . isspace ( ) and ( c < '<STR_LIT:U+0020>' or ord ( c ) > <NUM_LIT> ) : <EOL> break <EOL> else : <EOL> finfo . Type = '<STR_LIT>' <EOL> fp . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> dsize = fp . tell ( ) <EOL> fp . close ( ) <EOL> dir , file = os . path . split ( name ) <EOL> file = file . replace ( '<STR_LIT::>' , '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> return file , finfo , dsize , <NUM_LIT:0> <EOL> class openrsrc : <EOL> def __init__ ( self , * args ) : <EOL> pass <EOL> def read ( self , * args ) : <EOL> return '<STR_LIT>' <EOL> def write ( self , * args ) : <EOL> pass <EOL> def close ( self ) : <EOL> pass <EOL> class _Hqxcoderengine : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ofp ) : <EOL> self . ofp = ofp <EOL> self . data = '<STR_LIT>' <EOL> self . hqxdata = '<STR_LIT>' <EOL> self . linelen = LINELEN - <NUM_LIT:1> <EOL> def write ( self , data ) : <EOL> self . data = self . data + data <EOL> datalen = len ( self . data ) <EOL> todo = ( datalen // <NUM_LIT:3> ) * <NUM_LIT:3> <EOL> data = self . data [ : todo ] <EOL> self . data = self . data [ todo : ] <EOL> if not data : <EOL> return <EOL> self . hqxdata = self . hqxdata + binascii . b2a_hqx ( data ) <EOL> self . _flush ( <NUM_LIT:0> ) <EOL> def _flush ( self , force ) : <EOL> first = <NUM_LIT:0> <EOL> while first <= len ( self . hqxdata ) - self . linelen : <EOL> last = first + self . linelen <EOL> self . ofp . write ( self . hqxdata [ first : last ] + '<STR_LIT:\n>' ) <EOL> self . linelen = LINELEN <EOL> first = last <EOL> self . hqxdata = self . hqxdata [ first : ] <EOL> if force : <EOL> self . ofp . write ( self . hqxdata + '<STR_LIT>' ) <EOL> def close ( self ) : <EOL> if self . data : <EOL> self . hqxdata = self . hqxdata + binascii . b2a_hqx ( self . data ) <EOL> self . _flush ( <NUM_LIT:1> ) <EOL> self . ofp . close ( ) <EOL> del self . ofp <EOL> class _Rlecoderengine : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ofp ) : <EOL> self . ofp = ofp <EOL> self . data = '<STR_LIT>' <EOL> def write ( self , data ) : <EOL> self . data = self . data + data <EOL> if len ( self . data ) < REASONABLY_LARGE : <EOL> return <EOL> rledata = binascii . rlecode_hqx ( self . data ) <EOL> self . ofp . write ( rledata ) <EOL> self . data = '<STR_LIT>' <EOL> def close ( self ) : <EOL> if self . data : <EOL> rledata = binascii . rlecode_hqx ( self . data ) <EOL> self . ofp . write ( rledata ) <EOL> self . ofp . close ( ) <EOL> del self . ofp <EOL> class BinHex : <EOL> def __init__ ( self , name_finfo_dlen_rlen , ofp ) : <EOL> name , finfo , dlen , rlen = name_finfo_dlen_rlen <EOL> if type ( ofp ) == type ( '<STR_LIT>' ) : <EOL> ofname = ofp <EOL> ofp = open ( ofname , '<STR_LIT:w>' ) <EOL> ofp . write ( '<STR_LIT>' ) <EOL> hqxer = _Hqxcoderengine ( ofp ) <EOL> self . ofp = _Rlecoderengine ( hqxer ) <EOL> self . crc = <NUM_LIT:0> <EOL> if finfo is None : <EOL> finfo = FInfo ( ) <EOL> self . dlen = dlen <EOL> self . rlen = rlen <EOL> self . _writeinfo ( name , finfo ) <EOL> self . state = _DID_HEADER <EOL> def _writeinfo ( self , name , finfo ) : <EOL> nl = len ( name ) <EOL> if nl > <NUM_LIT> : <EOL> raise Error , '<STR_LIT>' <EOL> d = chr ( nl ) + name + '<STR_LIT>' <EOL> d2 = finfo . Type + finfo . Creator <EOL> d3 = struct . pack ( '<STR_LIT>' , finfo . Flags ) <EOL> d4 = struct . pack ( '<STR_LIT>' , self . dlen , self . rlen ) <EOL> info = d + d2 + d3 + d4 <EOL> self . _write ( info ) <EOL> self . _writecrc ( ) <EOL> def _write ( self , data ) : <EOL> self . crc = binascii . crc_hqx ( data , self . crc ) <EOL> self . ofp . write ( data ) <EOL> def _writecrc ( self ) : <EOL> if self . crc < <NUM_LIT:0> : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> fmt = '<STR_LIT>' <EOL> self . ofp . write ( struct . pack ( fmt , self . crc ) ) <EOL> self . crc = <NUM_LIT:0> <EOL> def write ( self , data ) : <EOL> if self . state != _DID_HEADER : <EOL> raise Error , '<STR_LIT>' <EOL> self . dlen = self . dlen - len ( data ) <EOL> self . _write ( data ) <EOL> def close_data ( self ) : <EOL> if self . dlen != <NUM_LIT:0> : <EOL> raise Error , '<STR_LIT>' % ( self . rlen , ) <EOL> self . _writecrc ( ) <EOL> self . state = _DID_DATA <EOL> def write_rsrc ( self , data ) : <EOL> if self . state < _DID_DATA : <EOL> self . close_data ( ) <EOL> if self . state != _DID_DATA : <EOL> raise Error , '<STR_LIT>' <EOL> self . rlen = self . rlen - len ( data ) <EOL> self . _write ( data ) <EOL> def close ( self ) : <EOL> if self . state is None : <EOL> return <EOL> try : <EOL> if self . state < _DID_DATA : <EOL> self . close_data ( ) <EOL> if self . state != _DID_DATA : <EOL> raise Error , '<STR_LIT>' <EOL> if self . rlen != <NUM_LIT:0> : <EOL> raise Error , "<STR_LIT>" % ( self . rlen , ) <EOL> self . _writecrc ( ) <EOL> finally : <EOL> self . state = None <EOL> ofp = self . ofp <EOL> del self . ofp <EOL> ofp . close ( ) <EOL> def binhex ( inp , out ) : <EOL> """<STR_LIT>""" <EOL> finfo = getfileinfo ( inp ) <EOL> ofp = BinHex ( finfo , out ) <EOL> ifp = open ( inp , '<STR_LIT:rb>' ) <EOL> while <NUM_LIT:1> : <EOL> d = ifp . read ( <NUM_LIT> ) <EOL> if not d : break <EOL> ofp . write ( d ) <EOL> ofp . close_data ( ) <EOL> ifp . close ( ) <EOL> ifp = openrsrc ( inp , '<STR_LIT:rb>' ) <EOL> while <NUM_LIT:1> : <EOL> d = ifp . read ( <NUM_LIT> ) <EOL> if not d : break <EOL> ofp . write_rsrc ( d ) <EOL> ofp . close ( ) <EOL> ifp . close ( ) <EOL> class _Hqxdecoderengine : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ifp ) : <EOL> self . ifp = ifp <EOL> self . eof = <NUM_LIT:0> <EOL> def read ( self , totalwtd ) : <EOL> """<STR_LIT>""" <EOL> decdata = '<STR_LIT>' <EOL> wtd = totalwtd <EOL> while wtd > <NUM_LIT:0> : <EOL> if self . eof : return decdata <EOL> wtd = ( ( wtd + <NUM_LIT:2> ) // <NUM_LIT:3> ) * <NUM_LIT:4> <EOL> data = self . ifp . read ( wtd ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> decdatacur , self . eof = binascii . a2b_hqx ( data ) <EOL> break <EOL> except binascii . Incomplete : <EOL> pass <EOL> newdata = self . ifp . read ( <NUM_LIT:1> ) <EOL> if not newdata : <EOL> raise Error , '<STR_LIT>' <EOL> data = data + newdata <EOL> decdata = decdata + decdatacur <EOL> wtd = totalwtd - len ( decdata ) <EOL> if not decdata and not self . eof : <EOL> raise Error , '<STR_LIT>' <EOL> return decdata <EOL> def close ( self ) : <EOL> self . ifp . close ( ) <EOL> class _Rledecoderengine : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ifp ) : <EOL> self . ifp = ifp <EOL> self . pre_buffer = '<STR_LIT>' <EOL> self . post_buffer = '<STR_LIT>' <EOL> self . eof = <NUM_LIT:0> <EOL> def read ( self , wtd ) : <EOL> if wtd > len ( self . post_buffer ) : <EOL> self . _fill ( wtd - len ( self . post_buffer ) ) <EOL> rv = self . post_buffer [ : wtd ] <EOL> self . post_buffer = self . post_buffer [ wtd : ] <EOL> return rv <EOL> def _fill ( self , wtd ) : <EOL> self . pre_buffer = self . pre_buffer + self . ifp . read ( wtd + <NUM_LIT:4> ) <EOL> if self . ifp . eof : <EOL> self . post_buffer = self . post_buffer + binascii . rledecode_hqx ( self . pre_buffer ) <EOL> self . pre_buffer = '<STR_LIT>' <EOL> return <EOL> mark = len ( self . pre_buffer ) <EOL> if self . pre_buffer [ - <NUM_LIT:3> : ] == RUNCHAR + '<STR_LIT>' + RUNCHAR : <EOL> mark = mark - <NUM_LIT:3> <EOL> elif self . pre_buffer [ - <NUM_LIT:1> ] == RUNCHAR : <EOL> mark = mark - <NUM_LIT:2> <EOL> elif self . pre_buffer [ - <NUM_LIT:2> : ] == RUNCHAR + '<STR_LIT>' : <EOL> mark = mark - <NUM_LIT:2> <EOL> elif self . pre_buffer [ - <NUM_LIT:2> ] == RUNCHAR : <EOL> pass <EOL> else : <EOL> mark = mark - <NUM_LIT:1> <EOL> self . post_buffer = self . post_buffer + binascii . rledecode_hqx ( self . pre_buffer [ : mark ] ) <EOL> self . pre_buffer = self . pre_buffer [ mark : ] <EOL> def close ( self ) : <EOL> self . ifp . close ( ) <EOL> class HexBin : <EOL> def __init__ ( self , ifp ) : <EOL> if type ( ifp ) == type ( '<STR_LIT>' ) : <EOL> ifp = open ( ifp ) <EOL> while <NUM_LIT:1> : <EOL> ch = ifp . read ( <NUM_LIT:1> ) <EOL> if not ch : <EOL> raise Error , "<STR_LIT>" <EOL> if ch == '<STR_LIT:\r>' : <EOL> continue <EOL> if ch == '<STR_LIT::>' : <EOL> break <EOL> if ch != '<STR_LIT:\n>' : <EOL> dummy = ifp . readline ( ) <EOL> hqxifp = _Hqxdecoderengine ( ifp ) <EOL> self . ifp = _Rledecoderengine ( hqxifp ) <EOL> self . crc = <NUM_LIT:0> <EOL> self . _readheader ( ) <EOL> def _read ( self , len ) : <EOL> data = self . ifp . read ( len ) <EOL> self . crc = binascii . crc_hqx ( data , self . crc ) <EOL> return data <EOL> def _checkcrc ( self ) : <EOL> filecrc = struct . unpack ( '<STR_LIT>' , self . ifp . read ( <NUM_LIT:2> ) ) [ <NUM_LIT:0> ] & <NUM_LIT> <EOL> self . crc = self . crc & <NUM_LIT> <EOL> if filecrc != self . crc : <EOL> raise Error , '<STR_LIT>' % ( self . crc , filecrc ) <EOL> self . crc = <NUM_LIT:0> <EOL> def _readheader ( self ) : <EOL> len = self . _read ( <NUM_LIT:1> ) <EOL> fname = self . _read ( ord ( len ) ) <EOL> rest = self . _read ( <NUM_LIT:1> + <NUM_LIT:4> + <NUM_LIT:4> + <NUM_LIT:2> + <NUM_LIT:4> + <NUM_LIT:4> ) <EOL> self . _checkcrc ( ) <EOL> type = rest [ <NUM_LIT:1> : <NUM_LIT:5> ] <EOL> creator = rest [ <NUM_LIT:5> : <NUM_LIT:9> ] <EOL> flags = struct . unpack ( '<STR_LIT>' , rest [ <NUM_LIT:9> : <NUM_LIT:11> ] ) [ <NUM_LIT:0> ] <EOL> self . dlen = struct . unpack ( '<STR_LIT>' , rest [ <NUM_LIT:11> : <NUM_LIT:15> ] ) [ <NUM_LIT:0> ] <EOL> self . rlen = struct . unpack ( '<STR_LIT>' , rest [ <NUM_LIT:15> : <NUM_LIT> ] ) [ <NUM_LIT:0> ] <EOL> self . FName = fname <EOL> self . FInfo = FInfo ( ) <EOL> self . FInfo . Creator = creator <EOL> self . FInfo . Type = type <EOL> self . FInfo . Flags = flags <EOL> self . state = _DID_HEADER <EOL> def read ( self , * n ) : <EOL> if self . state != _DID_HEADER : <EOL> raise Error , '<STR_LIT>' <EOL> if n : <EOL> n = n [ <NUM_LIT:0> ] <EOL> n = min ( n , self . dlen ) <EOL> else : <EOL> n = self . dlen <EOL> rv = '<STR_LIT>' <EOL> while len ( rv ) < n : <EOL> rv = rv + self . _read ( n - len ( rv ) ) <EOL> self . dlen = self . dlen - n <EOL> return rv <EOL> def close_data ( self ) : <EOL> if self . state != _DID_HEADER : <EOL> raise Error , '<STR_LIT>' <EOL> if self . dlen : <EOL> dummy = self . _read ( self . dlen ) <EOL> self . _checkcrc ( ) <EOL> self . state = _DID_DATA <EOL> def read_rsrc ( self , * n ) : <EOL> if self . state == _DID_HEADER : <EOL> self . close_data ( ) <EOL> if self . state != _DID_DATA : <EOL> raise Error , '<STR_LIT>' <EOL> if n : <EOL> n = n [ <NUM_LIT:0> ] <EOL> n = min ( n , self . rlen ) <EOL> else : <EOL> n = self . rlen <EOL> self . rlen = self . rlen - n <EOL> return self . _read ( n ) <EOL> def close ( self ) : <EOL> if self . state is None : <EOL> return <EOL> try : <EOL> if self . rlen : <EOL> dummy = self . read_rsrc ( self . rlen ) <EOL> self . _checkcrc ( ) <EOL> finally : <EOL> self . state = None <EOL> self . ifp . close ( ) <EOL> def hexbin ( inp , out ) : <EOL> """<STR_LIT>""" <EOL> ifp = HexBin ( inp ) <EOL> finfo = ifp . FInfo <EOL> if not out : <EOL> out = ifp . FName <EOL> ofp = open ( out , '<STR_LIT:wb>' ) <EOL> while <NUM_LIT:1> : <EOL> d = ifp . read ( <NUM_LIT> ) <EOL> if not d : break <EOL> ofp . write ( d ) <EOL> ofp . close ( ) <EOL> ifp . close_data ( ) <EOL> d = ifp . read_rsrc ( <NUM_LIT> ) <EOL> if d : <EOL> ofp = openrsrc ( out , '<STR_LIT:wb>' ) <EOL> ofp . write ( d ) <EOL> while <NUM_LIT:1> : <EOL> d = ifp . read_rsrc ( <NUM_LIT> ) <EOL> if not d : break <EOL> ofp . write ( d ) <EOL> ofp . close ( ) <EOL> ifp . close ( ) <EOL> def _test ( ) : <EOL> fname = sys . argv [ <NUM_LIT:1> ] <EOL> binhex ( fname , fname + '<STR_LIT>' ) <EOL> hexbin ( fname + '<STR_LIT>' , fname + '<STR_LIT>' ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
