mò
­fIc           @   s¾  d  Z  d k Z d k Z d k Z d k Z d d d g Z d e f d „  ƒ  YZ e d ƒ \ Z	 Z
 Z d Z d Z e d	 ƒ Z e i d
 j oZ d k Z d k Z y e i Z Wn e j
 o e Z n Xd „  Z d „  Z d „  Z n0 d f  d „  ƒ  YZ d „  Z d f  d „  ƒ  YZ d f  d „  ƒ  YZ d f  d „  ƒ  YZ d f  d „  ƒ  YZ d „  Z d f  d „  ƒ  YZ d f  d „  ƒ  YZ d f  d „  ƒ  YZ d  „  Z  d! „  Z! e" d" j o e! ƒ  n d S(#   s‰   Macintosh binhex compression/decompression.

easy interface:
binhex(inputfilename, outputfilename)
hexbin(inputfilename, outputfilename)
Nt   binhext   hexbint   Errorc           B   s   t  Z RS(   N(   t   __name__t
   __module__(    (    (    t#   /mit/python/lib/python2.4/binhex.pyR      s   i   i €  i@   i   t   macc           C   s
   t  i ƒ  S(   N(   t   macfst   FInfo(    (    (    R   R   8   s    c         C   s“   t  i |  ƒ i ƒ  } t i i |  ƒ \ } } t
 |  d ƒ } | i d d ƒ | i ƒ  } t |  d ƒ } | i d d ƒ | i ƒ  } | | | | f S(   Nt   rbi    i   s   *rb(   R   t   FSSpect   namet   GetFInfot   finfot   ost   patht   splitt   dirt   filet   opent   fpt   seekt   tellt   dlent   openrft   rlen(   R   R   R   R   R   R   R   (    (    R   t   getfileinfo;   s    c         G   s,   | p
 d } n d | d } t |  | ƒ S(   Ns   *rbt   *i    (   t   modeR   R   (   R   R   (    (    R   t   openrsrcG   s    
R   c           B   s   t  Z d „  Z RS(   Nc         C   s   d |  _ d |  _ d |  _ d  S(   Ns   ????i    (   t   selft   Typet   Creatort   Flags(   R   (    (    R   t   __init__T   s    		(   R   R   R"   (    (    (    R   R   S   s   c         C   sÚ   t  ƒ  } t |  ƒ } t |  ƒ i d ƒ } xJ | D]9 } | i ƒ  o% | d j  p t	 | ƒ d j o Pq1 q1 Wd | _
 | i d d ƒ | i ƒ  } | i ƒ  t i i |  ƒ \ } } | i d d d	 ƒ } | | | d f S(
   Ni   t    i   t   TEXTi    i   t   :t   -i   (   R   R   R   R   R   t   readt   datat   ct   isspacet   ordR   R   R   t   dsizet   closeR   R   R   R   R   t   replace(   R   R   R)   R,   R   R   R(   R   (    (    R   R   Y   s    	 .		
R   c           B   s,   t  Z d „  Z d „  Z d „  Z d „  Z RS(   Nc         G   s   d  S(   N(    (   R   t   args(    (    R   R"   k   s    c         G   s   d S(   Nt    (    (   R   R/   (    (    R   R'   n   s    c         G   s   d  S(   N(    (   R   R/   (    (    R   t   writeq   s    c         C   s   d  S(   N(    (   R   (    (    R   R-   t   s    (   R   R   R"   R'   R1   R-   (    (    (    R   R   j   s   			t   _Hqxcoderenginec           B   s2   t  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s(   Write data to the coder in 3-byte chunksc         C   s,   | |  _  d |  _ d |  _ t d |  _ d  S(   NR0   i   (   t   ofpR   R(   t   hqxdatat   LINELENt   linelen(   R   R3   (    (    R   R"   z   s    			c         C   sƒ   |  i | |  _ t |  i ƒ } | d d } |  i |  } |  i | |  _ | p d  Sn |  i t i | ƒ |  _ |  i d ƒ d  S(   Ni   i    (	   R   R(   t   lent   datalent   todoR4   t   binasciit   b2a_hqxt   _flush(   R   R(   R8   R9   (    (    R   R1   €   s    c         C   s›   d } x\ | t |  i ƒ |  i j o> | |  i } |  i i |  i | | !d ƒ t |  _ | } q	 W|  i | |  _ | o |  i i |  i d ƒ n d  S(   Ni    s   
s   :
(
   t   firstR7   R   R4   R6   t   lastR3   R1   R5   t   force(   R   R?   R>   R=   (    (    R   R<   ‹   s     	c         C   sN   |  i o  |  i t i |  i ƒ |  _ n |  i d ƒ |  i i ƒ  |  ` d  S(   Ni   (   R   R(   R4   R:   R;   R<   R3   R-   (   R   (    (    R   R-   –   s
    
 (   R   R   t   __doc__R"   R1   R<   R-   (    (    (    R   R2   w   s
    			t   _Rlecoderenginec           B   s)   t  Z d  Z d „  Z d „  Z d „  Z RS(   s4   Write data to the RLE-coder in suitably large chunksc         C   s   | |  _  d |  _ d  S(   NR0   (   R3   R   R(   (   R   R3   (    (    R   R"   ¡   s    	c         C   s]   |  i | |  _ t |  i ƒ t j  o d  Sn t i |  i ƒ } |  i i | ƒ d |  _ d  S(   NR0   (	   R   R(   R7   t   REASONABLY_LARGER:   t   rlecode_hqxt   rledataR3   R1   (   R   R(   RD   (    (    R   R1   ¥   s    c         C   sG   |  i o& t i |  i ƒ } |  i i | ƒ n |  i i ƒ  |  ` d  S(   N(   R   R(   R:   RC   RD   R3   R1   R-   (   R   RD   (    (    R   R-   ­   s
    
(   R   R   R@   R"   R1   R-   (    (    (    R   RA   ž   s    		t   BinHexc           B   sP   t  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(	   Nc   
      C   sñ   | \ } } } } t | ƒ t d ƒ j oL | } t | d ƒ } t i  d j o# t	 i
 | ƒ } | i d d ƒ qw n | i d ƒ t | ƒ }	 t |	 ƒ |  _ d |  _ | d  j o t ƒ  } n | |  _ | |  _ |  i | | ƒ t |  _ d  S(   NR0   t   wR   t   BnHqR$   s0   (This file must be converted with BinHex 4.0)

:i    (   R   R   R   R   t   typeR3   t   ofnameR   R   R   R
   t   fsst   SetCreatorTypeR1   R2   t   hqxerRA   R   t   crct   NoneR   t
   _writeinfot   _DID_HEADERt   state(
   R   t   .2R3   R   R   R   R   RJ   RI   RL   (    (    R   R"   µ   s    			c   	      C   s§   t  | ƒ } | d j o t d ‚ n t | ƒ | d } | i | i } t
 i d | i ƒ } t
 i d |  i |  i ƒ } | | | | } |  i | ƒ |  i ƒ  d  S(   Ni?   s   Filename too longt    s   >hs   >ii(   R7   R   t   nlR   t   chrt   dR   R   R    t   d2t   structt   packR!   t   d3R   R   R   t   d4t   infot   _writet	   _writecrc(	   R   R   R   R\   RT   R[   RW   RZ   RV   (    (    R   RO   Ç   s    c         C   s,   t  i | |  i ƒ |  _ |  i i | ƒ d  S(   N(   R:   t   crc_hqxR(   R   RM   R3   R1   (   R   R(   (    (    R   R]   Õ   s    c         C   s,   |  i i t i d |  i ƒ ƒ d |  _ d  S(   Ns   >hi    (   R   R3   R1   RX   RY   RM   (   R   (    (    R   R^   Ù   s    c         C   sD   |  i t j o t d ‚ n |  i t | ƒ |  _ |  i | ƒ d  S(   Ns   Writing data at the wrong time(   R   RQ   RP   R   R   R7   R(   R]   (   R   R(   (    (    R   R1   ß   s    c         C   s>   |  i d j o t d |  i f ‚ n |  i ƒ  t |  _ d  S(   Ni    s   Incorrect data size, diff=%r(   R   R   R   R   R^   t	   _DID_DATARQ   (   R   (    (    R   t
   close_dataå   s    
c         C   sb   |  i t j  o |  i ƒ  n |  i t j o t d ‚ n |  i t | ƒ |  _ |  i | ƒ d  S(   Ns'   Writing resource data at the wrong time(	   R   RQ   R`   Ra   R   R   R7   R(   R]   (   R   R(   (    (    R   t
   write_rsrcë   s    c         C   sŒ   |  i t j  o |  i ƒ  n |  i t j o t d ‚ n |  i d j o t d |  i f ‚ n |  i ƒ  |  i i ƒ  d  |  _ |  ` d  S(   Ns   Close at the wrong timei    s$   Incorrect resource-datasize, diff=%r(
   R   RQ   R`   Ra   R   R   R^   R3   R-   RN   (   R   (    (    R   R-   ó   s    
	(
   R   R   R"   RO   R]   R^   R1   Ra   Rb   R-   (    (    (    R   RE   ´   s   							c         C   sÅ   t  |  ƒ } t | | ƒ } t |  d ƒ } x- | i d ƒ } | p Pn | i
 | ƒ q- W| i ƒ  | i ƒ  t |  d ƒ } x- | i d ƒ } | p Pn | i | ƒ q€ W| i ƒ  | i ƒ  d S(   s@   (infilename, outfilename) - Create binhex-encoded copy of a fileR	   i   i ô N(   R   t   inpR   RE   t   outR3   R   t   ifpR'   RV   R1   Ra   R-   R   Rb   (   Rc   Rd   Re   RV   R3   R   (    (    R   R       s*        

   
t   _Hqxdecoderenginec           B   s)   t  Z d  Z d „  Z d „  Z d „  Z RS(   s*   Read data via the decoder in 4-byte chunksc         C   s   | |  _  d |  _ d  S(   Ni    (   Re   R   t   eof(   R   Re   (    (    R   R"     s    	c         C   s  d } | } xð | d j oâ |  i o | Sn | d d d } |  i i | ƒ } xk y t i	 | ƒ \ } |  _ PWn t i j
 o n X|  i i d ƒ } | p t d ‚ n | | } qU W| | } | t | ƒ } | o |  i o t d ‚ q q W| S(	   s&   Read at least wtd bytes (or until EOF)R0   i    i   i   i   i   s   Premature EOF on binhex fileN(   t   decdatat   totalwtdt   wtdR   Rg   Re   R'   R(   R:   t   a2b_hqxt
   decdatacurt
   Incompletet   newdataR   R7   (   R   Ri   R(   Rj   Rh   Rl   Rn   (    (    R   R'     s2      
   
c         C   s   |  i i ƒ  d  S(   N(   R   Re   R-   (   R   (    (    R   R-   ?  s    (   R   R   R@   R"   R'   R-   (    (    (    R   Rf     s    		"t   _Rledecoderenginec           B   s2   t  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s   Read data via the RLE-coderc         C   s(   | |  _  d |  _ d |  _ d |  _ d  S(   NR0   i    (   Re   R   t
   pre_buffert   post_bufferRg   (   R   Re   (    (    R   R"   E  s    			c         C   sU   | t |  i ƒ j o |  i | t |  i ƒ ƒ n |  i |  } |  i | |  _ | S(   N(   Rj   R7   R   Rq   t   _fillt   rv(   R   Rj   Rs   (    (    R   R'   K  s
    c         C   s1  |  i |  i i | d ƒ |  _ |  i i o- |  i t i |  i ƒ |  _ d |  _ d  Sn t	 |  i ƒ } |  i d t d t j o | d } nk |  i d t j o | d } nI |  i d t d j o | d } n# |  i d t j o n | d	 } |  i t i |  i |  ƒ |  _ |  i | |  _ d  S(
   Ni   R0   iýÿÿÿRS   i   iÿÿÿÿi   iþÿÿÿi   (   R   Rp   Re   R'   Rj   Rg   Rq   R:   t   rledecode_hqxR7   t   markt   RUNCHAR(   R   Rj   Ru   (    (    R   Rr   R  s"     	
 c         C   s   |  i i ƒ  d  S(   N(   R   Re   R-   (   R   (    (    R   R-   u  s    (   R   R   R@   R"   R'   Rr   R-   (    (    (    R   Ro   B  s
    			#t   HexBinc           B   sP   t  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(	   Nc         C   sÉ   t  | ƒ t  d ƒ j o t | ƒ } n xk | i d ƒ } | p t d ‚ n | d j o q, n | d j o Pn | d j o | i ƒ  } q, q, Wt | ƒ } t
 | ƒ |  _ d |  _ |  i ƒ  d  S(   NR0   i   s   No binhex data founds   R%   s   
i    (   RH   Re   R   R'   t   chR   t   readlinet   dummyRf   t   hqxifpRo   R   RM   t   _readheader(   R   Re   Rz   Rx   R{   (    (    R   R"   y  s"      	c         C   s.   |  i i | ƒ } t i | |  i ƒ |  _ | S(   N(   R   Re   R'   R7   R(   R:   R_   RM   (   R   R7   R(   (    (    R   t   _read‘  s    c         C   sm   t  i d |  i i d ƒ ƒ d d @} |  i d @|  _ | |  i j o t d |  i | f ‚ n d |  _ d  S(   Ns   >hi   i    iÿÿ  s   CRC error, computed %x, read %x(   RX   t   unpackR   Re   R'   t   filecrcRM   R   (   R   R   (    (    R   t	   _checkcrc–  s
    &c         C   s  |  i d ƒ } |  i t | ƒ ƒ } |  i d d d d d d ƒ } |  i ƒ  | d d !} | d d !} t	 i
 d | d d !ƒ d } t	 i
 d	 | d d
 !ƒ d |  _ t	 i
 d	 | d
 d !ƒ d |  _ | |  _ t ƒ  |  _ | |  i _ | |  i _ | |  i _ t |  _ d  S(   Ni   i   i   i   i	   s   >hi   i    s   >li   i   (   R   R}   R7   R+   t   fnamet   restR€   RH   t   creatorRX   R~   t   flagsR   R   t   FNameR   R    R   R!   RP   RQ   (   R   Rƒ   R7   R„   R‚   R   RH   (    (    R   R|      s    #
  	c         G   sŸ   |  i t j o t d ‚ n | o  | d } t | |  i ƒ } n
 |  i } d } x5 t | ƒ | j  o! | |  i	 | t | ƒ ƒ } qV W|  i | |  _ | S(   Ns   Read data at wrong timei    R0   (
   R   RQ   RP   R   t   nt   minR   Rs   R7   R}   (   R   R†   Rs   (    (    R   R'   ´  s    
	 "c         C   sT   |  i t j o t d ‚ n |  i o |  i |  i ƒ } n |  i ƒ  t |  _ d  S(   Ns   close_data at wrong time(	   R   RQ   RP   R   R   R}   Rz   R€   R`   (   R   Rz   (    (    R   Ra   Â  s    

c         G   sˆ   |  i t j o |  i ƒ  n |  i t j o t d ‚ n | o  | d } t | |  i ƒ } n
 |  i } |  i | |  _ |  i	 | ƒ S(   Ns    Read resource data at wrong timei    (
   R   RQ   RP   Ra   R`   R   R†   R‡   R   R}   (   R   R†   (    (    R   t	   read_rsrcÊ  s    
	c         C   sD   |  i o |  i |  i ƒ } n |  i ƒ  t |  _ |  i i ƒ  d  S(   N(	   R   R   Rˆ   Rz   R€   t	   _DID_RSRCRQ   Re   R-   (   R   Rz   (    (    R   R-   ×  s
    

	(
   R   R   R"   R}   R€   R|   R'   Ra   Rˆ   R-   (    (    (    R   Rw   x  s   			
				c         C   sz  t  |  ƒ } | i } | p | i } n t i d j o t	 i
 | ƒ } | i ƒ  } n t | d ƒ } x- | i d ƒ } | p Pn | i | ƒ qj W| i ƒ  | i ƒ  | i d ƒ } | oZ t | d ƒ } | i | ƒ x- | i d ƒ } | p Pn | i | ƒ qà W| i ƒ  n t i d j oA | i ƒ  } | i | _ | i | _ | i | _ | i | ƒ n | i ƒ  d S(   s0   (infilename, outfilename) - Decode binhexed fileR   t   wbi   i ô N(   Rw   Rc   Re   R   R   Rd   R…   R   R   R   R
   t   ofsst   as_pathnameR   R3   R'   RV   R1   R-   Ra   Rˆ   R   R   t   nfinfoR    R   R!   t   SetFInfo(   Rc   Rd   R‹   Re   RV   R3   R   R   (    (    R   R   Þ  sF     	   

   c          C   s‘   t  i d j o= t i d ƒ \ } }  |  p t i d ƒ n | i ƒ  } n t i
 d } t | | d ƒ t | d | d ƒ t i d ƒ d  S(   NR   s   File to convert:i    i   s   .hqxs   .viahqx(   R   R   R   t   PromptGetFileRJ   t   okt   syst   exitRŒ   R   t   argvR    R   (   R   RJ   R   (    (    R   t   _test  s    t   __main__(#   R@   R‘   R   RX   R:   t   __all__t	   ExceptionR   t   rangeRP   R`   R‰   RB   R5   RU   Rv   R   R   t   MacOSR   t   AttributeErrorR   R   R   R   R2   RA   RE   R    Rf   Ro   Rw   R   R”   R   (   R™   Rw   R    R”   Rf   RA   Ro   R   R5   RX   R–   R:   R   R`   R   R‰   R   R   R‘   RB   RP   R   Rv   R   RE   R   R2   (    (    R   t   ?   sD   									'L	,6f	&	