3 ^6@sdZddlZddlZddlZddlZdddgZGdddeZdZdZ dZ d Z d Z Gd d d Z d dZGdddZGdddZGdddZGdddZddZGdddZGdddZGdddZddZdS)zMacintosh binhex compression/decompression. easy interface: binhex(inputfilename, outputfilename) hexbin(inputfilename, outputfilename) NbinhexhexbinErrorc@s eZdZdS)rN)__name__ __module__ __qualname__rr/usr/lib64/python3.6/binhex.pyrsi@c@seZdZddZdS)FInfocCsd|_d|_d|_dS)Nz????r)TypeCreatorFlags)selfrrr __init__0szFInfo.__init__N)rrrrrrrr r /sr c Cstt}tj|d2}|jd}d|kr,d|_|jdd|j}WdQRXtjj |\}}|j ddd}|||dfS) NrbirZTEXT:-r ) r ioopenreadrseektellospathsplitreplace)namefinfofpdataZdsizedirfilerrr getfileinfo5s  r&c@s,eZdZddZddZddZddZd S) openrsrccGsdS)Nr)rargsrrr rCszopenrsrc.__init__cGsdS)Nr)rr(rrr rFsz openrsrc.readcGsdS)Nr)rr(rrr writeIszopenrsrc.writecCsdS)Nr)rrrr closeLszopenrsrc.closeN)rrrrrr*r+rrrr r'Bsr'c@s0eZdZdZddZddZddZdd Zd S) _Hqxcoderenginez(Write data to the coder in 3-byte chunkscCs ||_d|_d|_td|_dS)Nr)r )ofpr#hqxdataLINELENlinelen)rr-rrr rRsz_Hqxcoderengine.__init__cCsh|j||_t|j}|dd}|jd|}|j|d|_|sHdS|jtj||_|jddS)Nr)r#lenr.binasciib2a_hqx_flush)rr#ZdatalenZtodorrr r*Xs   z_Hqxcoderengine.writecCsvd}xF|t|j|jkrJ||j}|jj|j||dt|_|}qW|j|d|_|rr|jj|jddS)Nr s: )r2r.r0r-r*r/)rZforcefirstZlastrrr r5cs z_Hqxcoderengine._flushcCs6|jr|jtj|j|_|jd|jj|`dS)Nr )r#r.r3r4r5r-r+)rrrr r+ns   z_Hqxcoderengine.closeN)rrr__doc__rr*r5r+rrrr r,Os   r,c@s(eZdZdZddZddZddZdS) _Rlecoderenginez4Write data to the RLE-coder in suitably large chunkscCs||_d|_dS)Nr))r-r#)rr-rrr rxsz_Rlecoderengine.__init__cCs@|j||_t|jtkrdStj|j}|jj|d|_dS)Nr))r#r2REASONABLY_LARGEr3 rlecode_hqxr-r*)rr#rledatarrr r*|s    z_Rlecoderengine.writecCs0|jrtj|j}|jj||jj|`dS)N)r#r3r;r-r*r+)rr<rrr r+s    z_Rlecoderengine.closeN)rrrr8rr*r+rrrr r9usr9c@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS)BinHexc Cs|\}}}}d}t|tr.|}tj|d}d}yR|jdt|} t| |_d|_|dkr`t }||_ ||_ |j ||t |_Wn|r|jYnXdS)NFwbTs0(This file must be converted with BinHex 4.0) :r) isinstancestrrrr*r,r9r-crcr dlenrlen _writeinfo _DID_HEADERstater+) rZname_finfo_dlen_rlenr-r r!rBrCZclose_on_errorZofnameZhqxerrrr rs*       zBinHex.__init__c Cst|}|dkrtdt|g|jdd}|j|j}}t|trR|jd}t|trf|jd}||}tj d|j }tj d|j |j } |||| } |j | |jdS)N?zFilename too longzlatin-1z>hz>ii)r2rbytesencoderrr?r@structpackrrBrC_write _writecrc) rr r!nldtpZcrZd2Zd3Zd4inforrr rDs     zBinHex._writeinfocCs tj||j|_|jj|dS)N)r3crc_hqxrAr-r*)rr#rrr rMsz BinHex._writecCs4|jdkrd}nd}|jjtj||jd|_dS)Nrz>hz>H)rAr-r*rKrL)rZfmtrrr rNs  zBinHex._writecrccCs0|jtkrtd|jt||_|j|dS)NzWriting data at the wrong time)rFrErrBr2rM)rr#rrr r*s z BinHex.writecCs,|jdkrtd|jf|jt|_dS)NrzIncorrect data size, diff=%r)rBrrCrN _DID_DATArF)rrrr close_datas zBinHex.close_datacCsB|jtkr|j|jtkr$td|jt||_|j|dS)Nz'Writing resource data at the wrong time)rFrTrUrrCr2rM)rr#rrr write_rsrcs   zBinHex.write_rsrcc Csx|jdkrdSzJ|jtkr"|j|jtkr4td|jdkrNtd|jf|jWdd|_|j}|`|jXdS)NzClose at the wrong timerz$Incorrect resource-datasize, diff=%r)rFrTrUrrCrNr-r+)rr-rrr r+s     z BinHex.closeN) rrrrrDrMrNr*rUrVr+rrrr r=s r=c Cst|}t||}tj|d.}x|jd}|s2P|j|q"W|jWdQRXt|d}x|jd}|snP|j|q^W|j |j dS)zEbinhex(infilename, outfilename): create binhex-encoded copy of a fileriN) r&r=rrrr*rUr'rVr+)inpoutr!r-ifprPrrr rs"    c@s(eZdZdZddZddZddZdS) _Hqxdecoderenginez*Read data via the decoder in 4-byte chunkscCs||_d|_dS)Nr)rYeof)rrYrrr rsz_Hqxdecoderengine.__init__c Csd}|}x|dkr|jr|S|ddd}|jj|}xRytj|\}|_PWntjk rfYnX|jjd}|std||}q:W||}|t|}| r |j r tdq W|S)z&Read at least wtd bytes (or until EOF)r)rrr1r zPremature EOF on binhex file)r[rYrr3Za2b_hqxZ Incompleterr2)rZtotalwtdZdecdatawtdr#Z decdatacurnewdatarrr rs,      z_Hqxdecoderengine.readcCs|jjdS)N)rYr+)rrrr r+%sz_Hqxdecoderengine.closeN)rrrr8rrr+rrrr rZs rZc@s0eZdZdZddZddZddZdd Zd S) _RledecoderenginezRead data via the RLE-codercCs||_d|_d|_d|_dS)Nr)r)rY pre_buffer post_bufferr[)rrYrrr r+sz_Rledecoderengine.__init__cCsD|t|jkr"|j|t|j|jd|}|j|d|_|S)N)r2ra_fill)rr]rvrrr r1s z_Rledecoderengine.readcCs|j|jj|d|_|jjr>|jtj|j|_d|_dSt|j}|jddtdtkrl|d}nX|jddtkr|d}n<|jd dtdkr|d}n|jd d tkrn|d}|jtj|jd||_|j|d|_dS) Nr\r)r1rHr rrfre) r`rYrr[rar3Z rledecode_hqxr2RUNCHAR)rr]Zmarkrrr rb8s&    z_Rledecoderengine._fillcCs|jjdS)N)rYr+)rrrr r+[sz_Rledecoderengine.closeN)rrrr8rrrbr+rrrr r_(s #r_c@sLeZdZddZddZddZddZd d Zd d Zd dZ ddZ dS)HexBincCsjt|trtj|d}x.|jd}|s.td|dkr8q|dkrPqWt|}t||_d|_ |j dS)Nrr zNo binhex data found :r) r?r@rrrrrZr_rYrA _readheader)rrYZchZhqxifprrr r_s    zHexBin.__init__cCs |jj|}tj||j|_|S)N)rYrr3rSrA)rr2r#rrr _readus z HexBin._readcCsNtjd|jjddd@}|jd@|_||jkrDtd|j|fd|_dS)Nz>hrrizCRC error, computed %x, read %x)rKunpackrYrrAr)rZfilecrcrrr _checkcrczs   zHexBin._checkcrccCs|jd}|jt|}|jd}|j|dd}|dd}tjd|ddd}tjd |dd d|_tjd |d d d|_||_t|_||j_ ||j_ ||j_ t |_ dS)Nr r\r z>h rz>lrorprqrrrs)rlordrnrKrmrBrCFNamer rrrrErF)rr2ZfnameresttypeZcreatorflagsrrr rks    zHexBin._readheadercGsn|jtkrtd|r,|d}t||j}n|j}d}x&t||kr\||j|t|}q8W|j||_|S)NzRead data at wrong timerr))rFrErminrBr2rl)rnrcrrr rs  z HexBin.readcCs6|jtkrtd|jr$|j|j}|jt|_dS)Nzclose_data at wrong time)rFrErrBrlrnrT)rdummyrrr rUs   zHexBin.close_datacGsZ|jtkr|j|jtkr$td|r>|d}t||j}n|j}|j||_|j|S)Nz Read resource data at wrong timer)rFrErUrTrryrCrl)rrzrrr read_rsrcs   zHexBin.read_rsrcc CsD|jdkrdSz|jr"|j|j}|jWdd|_|jjXdS)N)rFrCr|rnrYr+)rr{rrr r+s   z HexBin.closeN) rrrrrlrnrkrrUr|r+rrrr rh^s  rhc Cst|}|j}|s|j}tj|d&}x|jd}|s8P|j|q(WWdQRX|j|jd}|rt |d}|j|x|jd}|sP|j|q|W|j |j dS)z6hexbin(infilename, outfilename) - Decode binhexed filer>iN) rhr rurrrr*rUr|r'r+)rWrXrYr!r-rPrrr rs,     )r8rrrKr3__all__ ExceptionrrErTr:r/rgr r&r'r,r9r=rrZr_rhrrrrr s,   &^*6h