mò
­fIc           @   sþ  d  Z  d k Z d k Z d k Z d d d d d d d d	 d
 d d d d d g Z g  Z e d ƒ D] Z e e e ƒ qb [ Z	 d Z
 d „  Z e d „ Z e d „ Z d „  Z d „  Z d „  Z d „  Z h  d d <d d <d d <d d  <d! d" <d# d$ <d% d& <d' d( <d) d* <d+ d, <d- d. <d/ d0 <d1 d2 <d3 d4 <d5 d6 <d7 d8 <d9 d: <d; d< <d= d> <d? d@ <dA dB <dC dD <dE dF <dG dH <dI dJ <dK dL <dM dN <dO dP <dQ dR <dS dT <dU dV <dW dX <Z e i ƒ  Z e i ƒ  g  Z e D] \ Z Z e e q[ Z e g  Z e i ƒ  D] \ Z Z e e e e ƒ f q@[ ƒ Z dY „  Z e e dZ „ Z d[ „  Z e d\ „ Z  d] Z! e! d9 d1 Z" d^ „  Z# d_ „  Z$ d` „  Z% da „  Z& db „  Z' dc „  Z( e) dd j o e' ƒ  n d S(e   s/   RFC 3548: Base16, Base32, Base64 Data EncodingsNt   encodet   decodet   encodestringt   decodestringt	   b64encodet	   b64decodet	   b32encodet	   b32decodet	   b16encodet	   b16decodet   standard_b64encodet   standard_b64decodet   urlsafe_b64encodet   urlsafe_b64decodei   t    c         C   sJ   t  } x* | i ƒ  D] \ } } | | t | ƒ <q W|  i d i	 | ƒ ƒ S(   NR   (
   t   _translationt   translationt   altcharst   itemst   kt   vt   ordt   st	   translatet   join(   R   R   R   R   R   (    (    t#   /mit/python/lib/python2.4/base64.pyt
   _translate    s
     c         C   sO   t  i |  ƒ d  } | d j	 o+ t | h  d | d <d | d <ƒ Sn | S(   sl  Encode a string using Base64.

    s is the string to encode.  Optional altchars must be a string of at least
    length 2 (additional characters are ignored) which specifies an
    alternative alphabet for the '+' and '/' characters.  This allows an
    application to e.g. generate url or filesystem safe Base64 strings.

    The encoded string is returned.
    iÿÿÿÿt   +i    t   /i   N(   t   binasciit
   b2a_base64R   t   encodedR   t   NoneR   (   R   R   R   (    (    R   R   *   s
    	 +c         C   sv   | d j	 o- t |  h  | d d <| d d <ƒ }  n y t i |  ƒ SWn$ t i j
 o } t | ƒ ‚ n Xd S(   sž  Decode a Base64 encoded string.

    s is the string to decode.  Optional altchars must be a string of at least
    length 2 (additional characters are ignored) which specifies the
    alternative alphabet used instead of the '+' and '/' characters.

    The decoded string is returned.  A TypeError is raised if s were
    incorrectly padded or if there are non-alphabet characters present in the
    string.
    i    R   i   R   N(	   R   R    R   R   R   t
   a2b_base64t   Errort   msgt	   TypeError(   R   R   R#   (    (    R   R   ;   s    
 -c         C   s
   t  |  ƒ S(   sy   Encode a string using the standard Base64 alphabet.

    s is the string to encode.  The encoded string is returned.
    N(   R   R   (   R   (    (    R   R
   O   s     c         C   s
   t  |  ƒ S(   s  Decode a string encoded with the standard Base64 alphabet.

    s is the string to decode.  The decoded string is returned.  A TypeError
    is raised if the string is incorrectly padded or if there are non-alphabet
    characters present in the string.
    N(   R   R   (   R   (    (    R   R   V   s     c         C   s   t  |  d ƒ S(   s¹   Encode a string using a url-safe Base64 alphabet.

    s is the string to encode.  The encoded string is returned.  The alphabet
    uses '-' instead of '+' and '_' instead of '/'.
    s   -_N(   R   R   (   R   (    (    R   R   _   s     c         C   s   t  |  d ƒ S(   sD  Decode a string encoded with the standard Base64 alphabet.

    s is the string to decode.  The decoded string is returned.  A TypeError
    is raised if the string is incorrectly padded or if there are non-alphabet
    characters present in the string.

    The alphabet uses '-' instead of '+' and '_' instead of '/'.
    s   -_N(   R   R   (   R   (    (    R   R   g   s     i    t   Ai	   t   Ji   t   Si   t   3i   t   Bi
   t   Ki   t   Ti   t   4i   t   Ci   t   Li   t   Ui   t   5i   t   Di   t   Mi   t   Vi   t   6i   t   Ei   t   Ni   t   Wi   t   7i   t   Fi   t   Oi   t   Xi   t   Gi   t   Pi   t   Yi   t   Hi   t   Qi   t   Zi   t   Ii   t   Ri   t   2c   	      C   s­  g  } t t |  ƒ d ƒ \ } } | o  |  d d | 7}  | d 7} n xÛ t | ƒ D]Í } t i	 d |  | d | d d !ƒ \ } } } | | d @d >7} | | d @d >7} | i t | d ?t | d	 ?d
 @t | d ?d
 @t | d ?t | d ?d
 @t | d ?d
 @t | d ?t | d
 @g ƒ qU Wt i | ƒ } | d j o | d  d SnX | d j o | d  d Sn; | d j o | d  d Sn | d j o | d  d Sn | S(   sc   Encode a string using Base32.

    s is the string to encode.  The encoded string is returned.
    i   t    i   s   !HHBi   i   i   i   i   i   i   i   i   iúÿÿÿs   ======iüÿÿÿs   ====iýÿÿÿs   ===i   iÿÿÿÿt   =N(   t   partst   divmodt   lenR   t   quantat   leftovert   ranget   it   structt   unpackt   c1t   c2t   c3t   extendt   _b32tabt   EMPTYSTRINGR   R   (	   R   RM   R   RG   RJ   RQ   RR   RK   RP   (    (    R   R   ‡   s,      .y	c         C   s'  t  t |  ƒ d ƒ \ } } | o t d ƒ ‚ n | o% t |  h  d d <d | <ƒ }  n | o |  i	 ƒ  }  n d }
 t i d |  ƒ } | o5 t | i d ƒ ƒ }
 |
 d j o |  |
  }  qÉ n g  } d } d	 } x |  D]… } t i | ƒ } | d j o t d
 ƒ ‚ n | t | | >7} | d 8} | d j  o* | i t i d | ƒ ƒ d } d	 } qâ qâ Wt i d | ƒ }	 |
 d j o
 d }	 ny |
 d j o |	 d  }	 n^ |
 d j o |	 d  }	 nC |
 d j o |	 d  }	 n( |
 d j o |	 d  }	 n t d ƒ ‚ | i |	 ƒ t i | ƒ S(   sG  Decode a Base32 encoded string.

    s is the string to decode.  Optional casefold is a flag specifying whether
    a lowercase alphabet is acceptable as input.  For security purposes, the
    default is False.

    RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O
    (oh), and for optional mapping of the digit 1 (one) to either the letter I
    (eye) or letter L (el).  The optional argument map01 when not None,
    specifies which letter the digit 1 should be mapped to (when map01 is not
    None, the digit 0 is always mapped to the letter O).  For security
    purposes the default is None, so that 0 and 1 are not allowed in the
    input.

    The decoded string is returned.  A TypeError is raised if s were
    incorrectly padded or if there are non-alphabet characters present in the
    string.
    i   s   Incorrect paddingt   0R:   t   1i    s   (?P<pad>[=]*)$t   padi#   s   Non-base32 digit foundi   s   %010xR   i   iÿÿÿÿi   iþÿÿÿi   iýÿÿÿi   iüÿÿÿN(   RH   RI   R   RJ   RK   R$   t   map01R   t   casefoldt   uppert   padcharst   ret   searcht   mot   groupRG   t   acct   shiftt   ct   _b32revt   gett   valR    t   appendR   t	   unhexlifyt   lastRU   R   (   R   RZ   RY   Rf   RG   RJ   RK   Ra   Rc   Ri   R\   Rb   R_   (    (    R   R   ±   sT     % 

c         C   s   t  i |  ƒ i ƒ  S(   sc   Encode a string using Base16.

    s is the string to encode.  The encoded string is returned.
    N(   R   t   hexlifyR   R[   (   R   (    (    R   R   û   s     c         C   sG   | o |  i ƒ  }  n t i d |  ƒ o t d ƒ ‚ n t i |  ƒ S(   sw  Decode a Base16 encoded string.

    s is the string to decode.  Optional casefold is a flag specifying whether
    a lowercase alphabet is acceptable as input.  For security purposes, the
    default is False.

    The decoded string is returned.  A TypeError is raised if s were
    incorrectly padded or if there are non-alphabet characters present in the
    string.
    s	   [^0-9A-F]s   Non-base16 digit foundN(   RZ   R   R[   R]   R^   R$   R   Rh   (   R   RZ   (    (    R   R	     s    
 iL   c         C   s”   x t  o… |  i t ƒ } | p Pn xG t | ƒ t j  o3 |  i t t | ƒ ƒ } | p Pn | | 7} q( Wt i | ƒ } | i | ƒ q Wd S(   s   Encode a file.N(   t   Truet   inputt   readt
   MAXBINSIZER   RI   t   nsR   R   t   linet   outputt   write(   Rl   Rq   R   Rp   Ro   (    (    R   R      s       c         C   sG   x@ t  o8 |  i ƒ  } | p Pn t i | ƒ } | i | ƒ q Wd S(   s   Decode a file.N(	   Rk   Rl   t   readlineRp   R   R!   R   Rq   Rr   (   Rl   Rq   R   Rp   (    (    R   R   ,  s      c         C   s]   g  } xG t d t |  ƒ t ƒ D]- } |  | | t !} | i t i	 | ƒ ƒ q Wd i
 | ƒ S(   s   Encode a string.i    R   N(   t   piecesRL   RI   R   Rn   RM   t   chunkRg   R   R   R   (   R   RM   Ru   Rt   (    (    R   R   6  s      c         C   s   t  i |  ƒ S(   s   Decode a string.N(   R   R!   R   (   R   (    (    R   R   ?  s     c          C   sL  d k  } d k } y# | i | i d d ƒ \ } } WnF | i j
 o7 } | i | _ | GHd | i d GH| i	 d ƒ n Xt
 } xu | D]m \ } }  | d j o
 t
 } n | d j o
 t } n | d	 j o
 t } n | d
 j o t ƒ  d Sq‹ q‹ W| o2 | d d j o! | t | d d ƒ | i ƒ n | | i | i ƒ d S(   s   Small test programNi   t   deuts   usage: %s [-d|-e|-u|-t] [file|-]
        -d, -u: decode
        -e: encode (default)
        -t: encode and decode string 'Aladdin:open sesame'i    i   s   -es   -ds   -us   -tt   -t   rb(   t   syst   getoptt   argvt   optst   argst   errorR#   t   stderrt   stdoutt   exitR    t   funct   ot   aR   t   test1t   opent   stdin(   R„   R}   Rƒ   Ry   R‚   R#   Rz   R|   (    (    R   t   testF  s.     #  
 
 
 !c          C   s5   d } t | ƒ } t | ƒ }  | Gt | ƒ G|  GHd  S(   Ns   Aladdin:open sesame(   t   s0R   t   s1R   t   s2t   repr(   R‹   RŠ   R‰   (    (    R   R…   _  s    t   __main__(*   t   __doc__R]   RN   R   t   __all__t   _[1]RL   t   _xt   chrR   RU   R   R    R   R   R
   R   R   R   t   _b32alphabetR   RT   t   sortR   R   t   dictt   longRd   R   t   FalseR   R   R	   t   MAXLINESIZERn   R    R   R   R   Rˆ   R…   t   __name__(    R…   R   R
   Rd   R   RT   R   Rn   RN   R   R‘   R   R   R   R]   R   R    R˜   R   R   Rˆ   R   R   R   R   R	   R   RU   R   R“   R   R   (    (    R   t   ?   sB   			0-	
					ÿ '
'?	*J			
					