�
��c@sdZddkZddkZddkZddddddd d
ddd
dddgZgZed�D]Zeee�qk[Z dZ
d�Zdd�Z
dd�Zd�Zd�Zd�Zd�Zhdd<dd<dd<d d!<d"d#<d$d%<d&d'<d(d)<d*d+<d,d-<d.d/<d0d1<d2d3<d4d5<d6d7<d8d9<d:d;<d<d=<d>d?<d@dA<dBdC<dDdE<dFdG<dHdI<dJdK<dLdM<dNdO<dPdQ<dRdS<dTdU<dVdW<dXdY<Zei�Zei�gZeD]\ZZeeq[ZegZei�D]\ZZeeee�fqI[�ZdZ�Zedd[�Z d\�Z!ed]�Z"d^Z#e#d;d3Z$d_�Z%d`�Z&da�Z'db�Z(dc�Z)dd�Z*e+dejoe)�ndS(fs/RFC 3548: Base16, Base32, Base64 Data Encodingsi�Ntencodetdecodetencodestringtdecodestringt b64encodet b64decodet b32encodet b32decodet b16encodet b16decodetstandard_b64encodetstandard_b64decodeturlsafe_b64encodeturlsafe_b64decodeitcCsJt}x*|i�D]\}}||t|�<qW|idi|��S(NR(t_translationtitemstordt translatetjoin(tstaltcharsttranslationtktv((s/sys/lib/python/base64.pyt
_translate s
cCsOti|�d }|dj o+t|h|dd<|dd<�Sn|S(slEncode 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�it+it/N(tbinasciit
b2a_base64tNoneR(RRtencoded((s/sys/lib/python/base64.pyR*s
+cCsv|dj o-t|hd|d<d|d<�}nyti|�SWn$tij
o}t|��nXdS(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.
RiRiN(RRRt
a2b_base64tErrort TypeError(RRtmsg((s/sys/lib/python/base64.pyR;s
-cCs
t|�S(syEncode a string using the standard Base64 alphabet.
s is the string to encode. The encoded string is returned.
(R(R((s/sys/lib/python/base64.pyR
OscCs
t|�S(sDecode 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.
(R(R((s/sys/lib/python/base64.pyRVscCs
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-_(R(R((s/sys/lib/python/base64.pyR_scCs
t|d�S(sDDecode 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-_(R(R((s/sys/lib/python/base64.pyR
gs tAitJi tSit3itBitKi
tTit4itCitLitUit5itDitMitVit6itEitNi
tWit7itFitOitXitGitPitYitHitQitZitIitRit2ic
Cs�g}tt|�d�\}}|o |dd|7}|d7}nx�t|�D]�}tid||d|dd!�\}}}||d@d>7}||d@d>7}|it|d?t|d ?d
@t|d?d
@t|d?t|d?d
@t|d
?d
@t|d?t|d
@g�qUWti|�}|djo|d dSnX|d
jo|d dSn;|djo|d dSn|djo|d dSn|S(scEncode a string using Base32.
s is the string to encode. The encoded string is returned.
itis!HHBiiiiiiiiii�s======i�s====i�s===ii�t=( tdivmodtlentrangetstructtunpacktextendt_b32tabtEMPTYSTRINGR( Rtpartstquantatleftovertitc1tc2tc3R((s/sys/lib/python/base64.pyR�s8
.
c
Cs'tt|�d�\}}|otd��n|o%t|hdd<|d<�}n|o|i�}nd}tid|�}|o5t|id��}|djo|| }q�ng}d}d } x�|D]�}
ti |
�}|djotd
��n|t|
| >7}| d8} | djo*|iti
d|��d}d } q�q�ti
d|�}|djo
d
}ny|djo|d }n^|djo|d }nC|djo|d }n(|djo|d }n
td��|i|�ti|�S(sGDecode 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.
isIncorrect paddingR9t0t1is(?P<pad>[=]*)$tpadi#sNon-base32 digit foundis%010xRii�ii�ii�ii�N(RFRGR"Rtuppertretsearchtgroupt_b32revtgetRtappendRt unhexlifyRMR(
Rtcasefoldtmap01RORPtpadcharstmoRNtacctshifttctvaltlast((s/sys/lib/python/base64.pyR�sR%
cCsti|�i�S(scEncode a string using Base16.
s is the string to encode. The encoded string is returned.
(RthexlifyRX(R((s/sys/lib/python/base64.pyR�scCsG|o|i�}ntid|�otd��nti|�S(swDecode 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]sNon-base16 digit found(RXRYRZR"RR_(RR`((s/sys/lib/python/base64.pyR s
iLcCs�x�to�|it�}|pPnxGt|�tjo3|itt|��}|pPn||7}q(Wti|�}|i|�qWdS(sEncode a file.N(tTruetreadt
MAXBINSIZERGRRtwrite(tinputtoutputRtnstline((s/sys/lib/python/base64.pyRs
cCsGx@to8|i�}|pPnti|�}|i|�qWdS(sDecode a file.N(RjtreadlineRR Rm(RnRoRqR((s/sys/lib/python/base64.pyR,s
cCs]g}xGtdt|�t�D]-}|||t!}|iti|��qWdi|�S(sEncode a string.iR(RHRGRlR^RRR(RtpiecesRQtchunk((s/sys/lib/python/base64.pyR6scCs
ti|�S(sDecode a string.(RR (R((s/sys/lib/python/base64.pyR?scCsRddk}ddk}y#|i|idd�\}}WnF|ij
o7}|i|_|GHd|idGH|id�nXt}xu|D]m\}}|djo
t}n|d jo
t}n|d
jo
t}n|djot �dSq�q�W|o2|ddjo!|t
|dd
�|i�n||i|i�dS(sSmall test programi�Nitdeuts�usage: %s [-d|-e|-u|-t] [file|-]
-d, -u: decode
-e: encode (default)
-t: encode and decode string 'Aladdin:open sesame'iis-es-ds-us-tt-trb(tsystgetopttargvterrortstderrtstdouttexitRRttest1topentstdin(RxRytoptstargsR#tfunctota((s/sys/lib/python/base64.pyttestFs$# !cCs5d}t|�}t|�}|Gt|�G|GHdS(NsAladdin:open sesame(RRtrepr(ts0ts1ts2((s/sys/lib/python/base64.pyR_st__main__(,t__doc__RYRIRt__all__t_[1]RHt_xtchrRRMRRRRR
RRR
t_b32alphabetRRLtsortt_[2]RRtdictt_[3]tlongR\RtFalseRRR tMAXLINESIZERlRRRRR�Rt__name__(((s/sys/lib/python/base64.pys<module>sZ-
$$$$$
'? *J
|