U e5d‚6ã@sÞdZddlZddlZddlZddlZdddgZGdd„deƒZdZdZ dZ d Z d Z Gd d „d ƒZ d d„ZGdd„dƒZGdd„dƒZGdd„dƒZGdd„dƒZdd„ZGdd„dƒZGdd„dƒZGdd„dƒZdd„ZdS)z‰Macintosh binhex compression/decompression. easy interface: binhex(inputfilename, outputfilename) hexbin(inputfilename, outputfilename) éNÚbinhexÚhexbinÚErrorc@s eZdZdS)rN)Ú__name__Ú __module__Ú __qualname__©rrú/usr/lib64/python3.8/binhex.pyrséi€é@óc@seZdZdd„ZdS)ÚFInfocCsd|_d|_d|_dS)Nz????r)ÚTypeÚCreatorÚFlags©Úselfrrr Ú__init__0szFInfo.__init__N)rrrrrrrr r /sr c Csttƒ}t |d¡2}| d¡}d|kr,d|_| dd¡| ¡}W5QRXtj  |¡\}}|  ddd¡}|||dfS) NÚrbirZTEXTéú:ú-r ) r ÚioÚopenÚreadrÚseekÚtellÚosÚpathÚsplitÚreplace)ÚnameÚfinfoÚfpÚdataZdsizeÚdirÚfilerrr Ú getfileinfo5s  r'c@s,eZdZdd„Zdd„Zdd„Zdd„Zd S) ÚopenrsrccGsdS©Nr©rÚargsrrr rCszopenrsrc.__init__cGsdS©Nórr*rrr rFsz openrsrc.readcGsdSr)rr*rrr ÚwriteIszopenrsrc.writecCsdSr)rrrrr ÚcloseLszopenrsrc.closeN)rrrrrr.r/rrrr r(Bsr(c@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú_Hqxcoderenginez(Write data to the coder in 3-byte chunkscCs ||_d|_d|_td|_dS)Nr-r )Úofpr$ÚhqxdataÚLINELENÚlinelen©rr1rrr rRsz_Hqxcoderengine.__init__cCsh|j||_t|jƒ}|dd}|jd|…}|j|d…|_|sHdS|jt |¡|_| d¡dS)Nér)r$Úlenr2ÚbinasciiÚb2a_hqxÚ_flush)rr$ZdatalenZtodorrr r.Xs   z_Hqxcoderengine.writecCsrd}|t|jƒ|jkrH||j}|j |j||…d¡t|_|}q|j|d…|_|rn|j |jd¡dS)Nró s: )r7r2r4r1r.r3)rZforceÚfirstZlastrrr r:cs z_Hqxcoderengine._flushcCs6|jr|jt |j¡|_| d¡|j ¡|`dS)Nr )r$r2r8r9r:r1r/rrrr r/ns   z_Hqxcoderengine.closeN)rrrÚ__doc__rr.r:r/rrrr r0Os   r0c@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú_Rlecoderenginez4Write data to the RLE-coder in suitably large chunkscCs||_d|_dSr,)r1r$r5rrr rxsz_Rlecoderengine.__init__cCs@|j||_t|jƒtkrdSt |j¡}|j |¡d|_dSr,)r$r7ÚREASONABLY_LARGEr8Ú rlecode_hqxr1r.)rr$Úrledatarrr r.|s    z_Rlecoderengine.writecCs0|jrt |j¡}|j |¡|j ¡|`dSr))r$r8r@r1r.r/)rrArrr r/„s    z_Rlecoderengine.closeN)rrrr=rr.r/rrrr r>usr>c@sLeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dS)ÚBinHexc Cs |\}}}}d}t|tƒr.|}t |d¡}d}zR| d¡t|ƒ} t| ƒ|_d|_|dkr`t ƒ}||_ ||_ |  ||¡t |_Wn|r”| ¡‚YnXdS)NFÚwbTs0(This file must be converted with BinHex 4.0) :r)Ú isinstanceÚstrrrr.r0r>r1Úcrcr ÚdlenÚrlenÚ _writeinfoÚ _DID_HEADERÚstater/) rZname_finfo_dlen_rlenr1r!r"rGrHZclose_on_errorZofnameZhqxerrrr rŒs*       zBinHex.__init__c Cs´t|ƒ}|dkrtdƒ‚t|gƒ| d¡d}|j|j}}t|tƒrR| d¡}t|tƒrf| d¡}||}t  d|j ¡}t  d|j |j ¡} |||| } |  | ¡| ¡dS)Né?zFilename too longzlatin-1óú>hz>ii)r7rÚbytesÚencoderrrDrEÚstructÚpackrrGrHÚ_writeÚ _writecrc) rr!r"ÚnlÚdÚtpZcrZd2Zd3Zd4Úinforrr rI£s     zBinHex._writeinfocCs t ||j¡|_|j |¡dSr))r8Úcrc_hqxrFr1r.©rr$rrr rS¶sz BinHex._writecCs4|jdkrd}nd}|j t ||j¡¡d|_dS)NrrNz>H)rFr1r.rQrR)rZfmtrrr rTºs  zBinHex._writecrccCs0|jtkrtdƒ‚|jt|ƒ|_| |¡dS)NzWriting data at the wrong time)rKrJrrGr7rSrZrrr r.Äs z BinHex.writecCs,|jdkrtd|jfƒ‚| ¡t|_dS)NrzIncorrect data size, diff=%r)rGrrHrTÚ _DID_DATArKrrrr Ú close_dataÊs zBinHex.close_datacCsB|jtkr| ¡|jtkr$tdƒ‚|jt|ƒ|_| |¡dS)Nz'Writing resource data at the wrong time)rKr[r\rrHr7rSrZrrr Ú write_rsrcÐs   zBinHex.write_rsrccCsx|jdkrdSzJ|jtkr"| ¡|jtkr4tdƒ‚|jdkrNtd|jfƒ‚| ¡W5d|_|j}|`| ¡XdS)NzClose at the wrong timerz$Incorrect resource-datasize, diff=%r)rKr1r/r[r\rrHrTr5rrr r/Øs     z BinHex.closeN) rrrrrIrSrTr.r\r]r/rrrr rB‹s rBc Csˆt|ƒ}t||ƒ}t |d¡*}| d¡}|s0q<| |¡q | ¡W5QRXt|dƒ}| d¡}|shqt| |¡qX|  ¡|  ¡dS)zEbinhex(infilename, outfilename): create binhex-encoded copy of a fileréôN) r'rBrrrr.r\r(r]r/)ÚinpÚoutr"r1ÚifprVrrr rés      c@s(eZdZdZdd„Zdd„Zdd„ZdS) Ú_Hqxdecoderenginez*Read data via the decoder in 4-byte chunkscCs||_d|_dS)Nr)raÚeof©rrarrr rsz_Hqxdecoderengine.__init__cCs´d}|}|dkr°|jr|S|ddd}|j |¡}zt |¡\}|_WqˆWntjk rdYnX|j d¡}|s~tdƒ‚||}q6||}|t|ƒ}|s|jstdƒ‚q|S)z&Read at least wtd bytes (or until EOF)r-rrr6ér zPremature EOF on binhex file)rcrarr8Za2b_hqxZ Incompleterr7)rZtotalwtdZdecdataÚwtdr$Z decdatacurÚnewdatarrr rs*      z_Hqxdecoderengine.readcCs|j ¡dSr)©rar/rrrr r/%sz_Hqxdecoderengine.closeN)rrrr=rrr/rrrr rbþs rbc@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) Ú_RledecoderenginezRead data via the RLE-codercCs||_d|_d|_d|_dS)Nr-r)raÚ pre_bufferÚ post_bufferrcrdrrr r+sz_Rledecoderengine.__init__cCsD|t|jƒkr"| |t|jƒ¡|jd|…}|j|d…|_|Sr))r7rkÚ_fill)rrfÚrvrrr r1s z_Rledecoderengine.readcCsô|j|j |d¡|_|jjr>|jt |j¡|_d|_dSt|jƒ}|jdd…tdtkrl|d}nX|jdd…tkrˆ|d}n<|jdd…tdkr¨|d}n|jdd…tkr¼n|d }|jt |jd|…¡|_|j|d…|_dS) Nrer-éýÿÿÿrMr6éÿÿÿÿréþÿÿÿr ) rjrarrcrkr8Z rledecode_hqxr7ÚRUNCHAR)rrfZmarkrrr rl8s* ÿ    ÿz_Rledecoderengine._fillcCs|j ¡dSr)rhrrrr r/[sz_Rledecoderengine.closeN)rrrr=rrrlr/rrrr ri(s #ric@sLeZdZdd„Zdd„Zdd„Zdd„Zd d „Zd d „Zd d„Z dd„Z dS)ÚHexBincCsft|tƒrt |d¡}| d¡}|s,tdƒ‚|dkr6q|dkrqBqt|ƒ}t|ƒ|_d|_ |  ¡dS)Nrr zNo binhex data foundr;ó:r) rDrErrrrrbrirarFÚ _readheader)rraZchZhqxifprrr r_s    zHexBin.__init__cCs |j |¡}t ||j¡|_|Sr))rarr8rYrF)rr7r$rrr Ú_readus z HexBin._readcCsNt d|j d¡¡dd@}|jd@|_||jkrDtd|j|fƒ‚d|_dS)NrNrriÿÿzCRC error, computed %x, read %x)rQÚunpackrarrFr)rZfilecrcrrr Ú _checkcrczs  ÿzHexBin._checkcrccCs¾| d¡}| t|ƒ¡}| d¡}| ¡|dd…}|dd…}t d|dd…¡d}t d|dd …¡d|_t d|d d…¡d|_||_tƒ|_||j_ ||j_ ||j_ t |_ dS) Nr ééé rNé rz>lé)ruÚordrwrQrvrGrHÚFNamer rrrrJrK)rr7ZfnameÚrestÚtypeZcreatorÚflagsrrr rt„s    zHexBin._readheadercGsj|jtkrtdƒ‚|r,|d}t||jƒ}n|j}d}t|ƒ|krZ|| |t|ƒ¡}q6|j||_|S)NzRead data at wrong timerr-)rKrJrÚminrGr7ru)rÚnrmrrr r˜s   z HexBin.readcCs6|jtkrtdƒ‚|jr$| |j¡}| ¡t|_dS)Nzclose_data at wrong time)rKrJrrGrurwr[©rZdummyrrr r\¦s   zHexBin.close_datacGsZ|jtkr| ¡|jtkr$tdƒ‚|r>|d}t||jƒ}n|j}|j||_| |¡S)Nz Read resource data at wrong timer)rKrJr\r[rr‚rHru)rrƒrrr Ú read_rsrc®s   zHexBin.read_rsrccCsD|jdkrdSz|jr"| |j¡}| ¡W5d|_|j ¡XdSr))rKrar/rHr…rwr„rrr r/»s   z HexBin.closeN) rrrrrurwrtrr\r…r/rrrr rr^s  rrc Cs¦t|ƒ}|j}|s|j}t |d¡"}| d¡}|s6qB| |¡q&W5QRX| ¡| d¡}|ršt |dƒ}| |¡| d¡}|s†q’| |¡qv|  ¡|  ¡dS)z6hexbin(infilename, outfilename) - Decode binhexed filerCr^N) rrr r~rrrr.r\r…r(r/)r_r`rar"r1rVrrr rÆs(      )r=rrrQr8Ú__all__Ú ExceptionrrJr[r?r3rqr r'r(r0r>rBrrbrirrrrrrr Ús,   &^*6h