@@ -545,6 +545,40 @@ def test_b85encode(self):
545545 self .check_other_types (base64 .b85encode , b"www.python.org" ,
546546 b'cXxL#aCvlSZ*DGca%T' )
547547
548+ def test_z85encode (self ):
549+ eq = self .assertEqual
550+
551+ tests = {
552+ b'' : b'' ,
553+ b'www.python.org' : b'CxXl-AcVLsz/dgCA+t' ,
554+ bytes (range (255 )): b"""009c61o!#m2NH?C3>iWS5d]J*6CRx17-skh9337x"""
555+ b"""ar.{NbQB=+c[cR@eg&FcfFLssg=mfIi5%2YjuU>)kTv.7l}6Nnnj=AD"""
556+ b"""oIFnTp/ga?r8($2sxO*itWpVyu$0IOwmYv=xLzi%y&a6dAb/]tBAI+J"""
557+ b"""CZjQZE0{D[FpSr8GOteoH(41EJe-<UKDCY&L:dM3N3<zjOsMmzPRn9P"""
558+ b"""Q[%@^ShV!$TGwUeU^7HuW6^uKXvGh.YUh4]Z})[9-kP:p:JqPF+*1CV"""
559+ b"""^9Zp<!yAd4/Xb0k*$*&A&nJXQ<MkK!>&}x#)cTlf[Bu8v].4}L}1:^-"""
560+ b"""@qDP""" ,
561+ b"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"""
562+ b"""0123456789!@#0^&*();:<>,. []{}""" :
563+ b"""vpA.SwObN*x>?B1zeKohADlbxB-}$ND3R+ylQTvjm[uizoh55PpF:[^"""
564+ b"""q=D:$s6eQefFLssg=mfIi5@cEbqrBJdKV-ciY]OSe*aw7DWL""" ,
565+ b'no padding..' : b'zF{UpvpS[.zF7NO' ,
566+ b'zero compression\x00 \x00 \x00 \x00 ' : b'Ds.bnay/tbAb]JhB7]Mg00000' ,
567+ b'zero compression\x00 \x00 \x00 ' : b'Ds.bnay/tbAb]JhB7]Mg0000' ,
568+ b"""Boundary:\x00 \x00 \x00 \x00 """ : b"""lt}0:wmoI7iSGcW00""" ,
569+ b'Space compr: ' : b'q/DePwGUG3ze:IRarR^H' ,
570+ b'\xff ' : b'@@' ,
571+ b'\xff ' * 2 : b'%nJ' ,
572+ b'\xff ' * 3 : b'%nS9' ,
573+ b'\xff ' * 4 : b'%nSc0' ,
574+ }
575+
576+ for data , res in tests .items ():
577+ eq (base64 .z85encode (data ), res )
578+
579+ self .check_other_types (base64 .z85encode , b"www.python.org" ,
580+ b'CxXl-AcVLsz/dgCA+t' )
581+
548582 def test_a85decode (self ):
549583 eq = self .assertEqual
550584
@@ -626,6 +660,41 @@ def test_b85decode(self):
626660 self .check_other_types (base64 .b85decode , b'cXxL#aCvlSZ*DGca%T' ,
627661 b"www.python.org" )
628662
663+ def test_z85decode (self ):
664+ eq = self .assertEqual
665+
666+ tests = {
667+ b'' : b'' ,
668+ b'CxXl-AcVLsz/dgCA+t' : b'www.python.org' ,
669+ b"""009c61o!#m2NH?C3>iWS5d]J*6CRx17-skh9337x"""
670+ b"""ar.{NbQB=+c[cR@eg&FcfFLssg=mfIi5%2YjuU>)kTv.7l}6Nnnj=AD"""
671+ b"""oIFnTp/ga?r8($2sxO*itWpVyu$0IOwmYv=xLzi%y&a6dAb/]tBAI+J"""
672+ b"""CZjQZE0{D[FpSr8GOteoH(41EJe-<UKDCY&L:dM3N3<zjOsMmzPRn9P"""
673+ b"""Q[%@^ShV!$TGwUeU^7HuW6^uKXvGh.YUh4]Z})[9-kP:p:JqPF+*1CV"""
674+ b"""^9Zp<!yAd4/Xb0k*$*&A&nJXQ<MkK!>&}x#)cTlf[Bu8v].4}L}1:^-"""
675+ b"""@qDP""" : bytes (range (255 )),
676+ b"""vpA.SwObN*x>?B1zeKohADlbxB-}$ND3R+ylQTvjm[uizoh55PpF:[^"""
677+ b"""q=D:$s6eQefFLssg=mfIi5@cEbqrBJdKV-ciY]OSe*aw7DWL""" :
678+ b"""abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"""
679+ b"""0123456789!@#0^&*();:<>,. []{}""" ,
680+ b'zF{UpvpS[.zF7NO' : b'no padding..' ,
681+ b'Ds.bnay/tbAb]JhB7]Mg00000' : b'zero compression\x00 \x00 \x00 \x00 ' ,
682+ b'Ds.bnay/tbAb]JhB7]Mg0000' : b'zero compression\x00 \x00 \x00 ' ,
683+ b"""lt}0:wmoI7iSGcW00""" : b"""Boundary:\x00 \x00 \x00 \x00 """ ,
684+ b'q/DePwGUG3ze:IRarR^H' : b'Space compr: ' ,
685+ b'@@' : b'\xff ' ,
686+ b'%nJ' : b'\xff ' * 2 ,
687+ b'%nS9' : b'\xff ' * 3 ,
688+ b'%nSc0' : b'\xff ' * 4 ,
689+ }
690+
691+ for data , res in tests .items ():
692+ eq (base64 .z85decode (data ), res )
693+ eq (base64 .z85decode (data .decode ("ascii" )), res )
694+
695+ self .check_other_types (base64 .z85decode , b'CxXl-AcVLsz/dgCA+t' ,
696+ b'www.python.org' )
697+
629698 def test_a85_padding (self ):
630699 eq = self .assertEqual
631700
@@ -707,14 +776,30 @@ def test_b85decode_errors(self):
707776 self .assertRaises (ValueError , base64 .b85decode , b'|NsC' )
708777 self .assertRaises (ValueError , base64 .b85decode , b'|NsC1' )
709778
779+ def test_z85decode_errors (self ):
780+ illegal = list (range (33 )) + \
781+ list (b'"\' ,;_`|\\ ~' ) + \
782+ list (range (128 , 256 ))
783+ for c in illegal :
784+ with self .assertRaises (ValueError , msg = bytes ([c ])):
785+ base64 .z85decode (b'0000' + bytes ([c ]))
786+
787+ # b'\xff\xff\xff\xff' encodes to b'%nSc0', the following will overflow:
788+ self .assertRaises (ValueError , base64 .z85decode , b'%' )
789+ self .assertRaises (ValueError , base64 .z85decode , b'%n' )
790+ self .assertRaises (ValueError , base64 .z85decode , b'%nS' )
791+ self .assertRaises (ValueError , base64 .z85decode , b'%nSc' )
792+ self .assertRaises (ValueError , base64 .z85decode , b'%nSc1' )
793+
710794 def test_decode_nonascii_str (self ):
711795 decode_funcs = (base64 .b64decode ,
712796 base64 .standard_b64decode ,
713797 base64 .urlsafe_b64decode ,
714798 base64 .b32decode ,
715799 base64 .b16decode ,
716800 base64 .b85decode ,
717- base64 .a85decode )
801+ base64 .a85decode ,
802+ base64 .z85decode )
718803 for f in decode_funcs :
719804 self .assertRaises (ValueError , f , 'with non-ascii \xcb ' )
720805
0 commit comments