Permalink
Browse files

Interface to a parrot lib Base64 library that passes most tests

  • Loading branch information...
1 parent 93868fc commit 6bb32146ff67ed37fdced96b5f260f14fd15e91a @ronaldxs committed Oct 10, 2012
View
@@ -0,0 +1,54 @@
+module Enc::MIME::Base64:auth<ronaldxs>:ver<0.01> {
+
+ pir::load_bytecode__vs('MIME/Base64.pbc');
+
+ our Str sub encode_base64(Buf $b, Str $eol = "\n") is export {
+ if $eol ne "\n" {
+ die "eol parameter only supported in PurePerl for now"
+ }
+ my Mu $b_internal := nqp::getattr(nqp::p6decont($b), Buf, '$!buffer');
+ my $encoded-str = nqp::p6box_s Q:PIR {
+ .local pmc encode
+ encode = get_root_global ['parrot'; 'MIME'; 'Base64'], 'encode_base64_bb'
+ $P0 = find_lex '$b_internal'
+ %r = encode($P0)
+ };
+ return $encoded-str;
+ }
+
+ ######################################################################
+ # Greatly wondering whether building everything in a list of Int
+ # and then newing the Buf is the most efficient way
+ ######################################################################
+ our Buf sub decode_base64(Str $s) is export {
+ my $ret := Buf.new();
+# my $bb := pir::new__Ps('ByteBuffer');
+ my $bb := Q:PIR {
+ .local pmc decode
+ decode = get_root_global ['parrot'; 'MIME'; 'Base64'], 'decode_base64_bb'
+ $P1 = find_lex '$s'
+ $S1 = repr_unbox_str $P1
+ %r = decode($S1)
+ };
+ nqp::bindattr($ret, Buf, '$!buffer', $bb);
+ return $ret;
+ }
+
+ ######################################################################
+ our Str sub encode_base64_str(Str $s, Str $e = 'utf-8', :$eol = "\n")
+ is export
+ {
+ if $eol ne "\n" {
+ die "eol parameter only supported in PurePerl for now"
+ }
+ encode_base64($s.encode($e), $eol);
+ }
+
+ ######################################################################
+ our Str sub decode_base64_str(Str $s, Str $d = 'utf-8')
+ is export
+ {
+ decode_base64($s).decode($d);
+ }
+
+}
View
@@ -1,7 +1,7 @@
use v6;
use Test;
-use PP::Enc::MIME::Base64;
+use Enc::MIME::Base64;
plan 8;
@@ -24,3 +24,9 @@ my Str $camelia_b64 = slurp(
);
is encode_base64($camelia_ico), $camelia_b64.chomp, 'binary img encode test';
ok decode_base64($camelia_b64) eq $camelia_ico, 'binary img decode test';
+my $x = decode_base64($camelia_b64);
+say $x.bytes;
+say $camelia_ico.bytes;
+loop (my $i = 0; $i < $x.bytes; $i++) {
+ if $x[$i] != $camelia_ico[$i] { say $i }
+}
View
@@ -1,7 +1,7 @@
use v6;
use Test;
-use PP::Enc::MIME::Base64;
+use Enc::MIME::Base64;
plan 18;
@@ -1,21 +1,16 @@
use v6;
use Test;
-use PP::Enc::MIME::Base64;
+use Enc::MIME::Base64;
-plan 7;
+plan 6;
is encode_base64_str(
'This is a long line whose base64 encoding should be broken into multiple lines as required by MIME.'),
"VGhpcyBpcyBhIGxvbmcgbGluZSB3aG9zZSBiYXNlNjQgZW5jb2Rpbmcgc2hvdWxkIGJlIGJyb2tl
biBpbnRvIG11bHRpcGxlIGxpbmVzIGFzIHJlcXVpcmVkIGJ5IE1JTUUu",
'Encode Break up long line test';
-is encode_base64_str(
- 'This is a long line whose base64 encoding should be broken into multiple lines as required by MIME.', eol => ''),
-"VGhpcyBpcyBhIGxvbmcgbGluZSB3aG9zZSBiYXNlNjQgZW5jb2Rpbmcgc2hvdWxkIGJlIGJyb2tlbiBpbnRvIG11bHRpcGxlIGxpbmVzIGFzIHJlcXVpcmVkIGJ5IE1JTUUu",
- 'Encode Break up long line test with breakup override';
-
is decode_base64_str(
"VGhpcyBpcyBhIGxvbmcgbGluZSB3aG9zZSBiYXNlNjQgZW5jb2Rpbmcgc2hvdWxkIGJlIGJyb2tl
biBpbnRvIG11bHRpcGxlIGxpbmVzIGFzIHJlcXVpcmVkIGJ5IE1JTUUu"),
View
@@ -0,0 +1,26 @@
+use v6;
+
+use Test;
+use PP::Enc::MIME::Base64;
+
+plan 8;
+
+# from Perl 5 Mime::Base64 base64.t
+# I don't think it makes sense any more to test encoding each individual byte
+# value but may make sense to do a few end cases
+is encode_base64(Buf.new(0)), 'AA==', 'encode Test on NULL/0 byte';
+is encode_base64(Buf.new(1)), 'AQ==', 'encode Test on byte value 1';
+is encode_base64(Buf.new(255)), '/w==', 'encode Test on byte value 255';
+ok decode_base64('AA==') eq Buf.new(0), 'decode Test on NULL/0 byte';
+ok decode_base64('AQ==') eq Buf.new(1), 'decode Test on byte value 1';
+ok decode_base64('/w==') eq Buf.new(255), 'decode Test on byte value 255';
+
+my Buf $camelia_ico = slurp(
+ $?FILE.path.directory ~ '/data/camelia-favicon.ico', :bin
+);
+# .b64 file generated from .ico file with gnu base64 program
+my Str $camelia_b64 = slurp(
+ $?FILE.path.directory ~ '/data/camelia-favicon.b64'
+);
+is encode_base64($camelia_ico), $camelia_b64.chomp, 'binary img encode test';
+ok decode_base64($camelia_b64) eq $camelia_ico, 'binary img decode test';
@@ -0,0 +1,21 @@
+AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAA
+AAAAAAAAAAAAAAAA/wAAAP8AAACGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAABA
+AAAA/wAAAP8AAAAAAAAA/wAAAP9NAbX/TQG13gAAAPsAAABAAAAAAAAAAP8AAAD/AAAAAAAAAEAA
+AAD/TQG1/00Btf8AAAD/AAAA/wAAAP8AAAD/TQG1//8AAP9rAvv/AAAA/wAAAEAAAABlAAAAAAAA
+AP8AAAD/awL7//8AAP9NAbX/AAAA+QAAAP8AAAAAAAAA/wAAAP9NAbX/TQG1/wAAAP8AAAD/AAAA
+2gAAAP8AAAD/AAAA/00Btf9NAbX/AAAA/wAAANMAAAAJAAAAAAAAAAAAAABlAAAA/wAAAP8AnJz/
+AOLi/wD///8A////ANjY/wCFhf8AAAD/AAAA/wAAAEAAAAAAAAAACwAAAAAAAAAAAAAAAAAAAP8A
+hYX/AP///wD///9rAvv/awL7/wD///8A////AIWF/wAAAP8AAAAAAAAAAAAAAAIAAAAAAAAAAAAA
+AP8AAAD/AOPj/wD///9rAvv/AP///2sC+/8A////AP///wDj4/8AAAD/AAAA/wAAAAAAAAAAAAAA
+AAAAAP8AAAD//35+/wD///8A////AP///wD///8A////AP///wD///8A////AAAA/wAAAP8AAAD/
+AAAAAAAAAFYAAAD//35+/4UAAP8A////AIWF/wDT0/8A////AP///wCFhf8AxcX/AP///4UAAP+F
+AAD/AAAA/wAAAAAAAACubycn/99vb//GAAD/XFxc//////9cXFz/AP///1xcXP//////XFxc/wAA
+AP//fn7//35+/4UAAP8AAABAQAAA//8fH///fn7/AAAA//////8AAAD//////wAAAP//////AAAA
+////////AAD/awL7/4UAAP//fn7/AAAA/1IoKP//fn7//35+//8AAP9cXFz//////wCFhf8A////
+AIWF//////+FhYX//35+/4UAAP9rAvv//35+/wAAAP9mMjL//35+/2sC+///fn7/APsC/wAAAP8A
+////AP///wD///8AAAD/AAAA//9+fv/9AAD//35+/wAAAP8AAAD/hQAA//9+fv9rAvv//35+/wD7
+Av8AAAD/AAAAAAAAAEAAAAAAAAAAGQAAAP/RAACR/35+/wAAAP8A+wL/AAAA2gAAAGX/AADa/35+
+//9+fv8AAAD/AAAAAAAAAAAAAAB7AAAAAAAAAEAAAAAAAAAA//0AAP//fn7//35+/wAAAGUAAAAP
+AAAAZQAAAP8AAAD/AAAAAAAAAAAAAABlAAAA/4WFhQEAAAD/AAAAQAAAAAAAAAD/AAAA2gAAAGUA
+AAAAj/kAAAZgAAADgAAAgAEAAOAHAADgBwAAwAMAAIABAACAAQAAAAEAAAAAAAAAAAAAAAAAAAPA
+AACH4QAAzrMAAA==
Binary file not shown.
View
@@ -0,0 +1,26 @@
+use v6;
+
+use Test;
+use PP::Enc::MIME::Base64;
+
+plan 18;
+
+is encode_base64_str(''), '', 'Encoding the empty string';
+is encode_base64_str('A'), 'QQ==', 'Encoding "A"';
+is encode_base64_str('Ab'), 'QWI=', 'Encoding "Ab"';
+is encode_base64_str('Abc'), 'QWJj', 'Encoding "Abc"';
+is encode_base64_str('Abcd'), 'QWJjZA==', 'Encoding "Abcd"';
+is encode_base64_str('Perl'), 'UGVybA==', 'Encoding "Perl"';
+is encode_base64_str('Perl6'), 'UGVybDY=', 'Encoding "Perl6"';
+is encode_base64_str('Another test!'), 'QW5vdGhlciB0ZXN0IQ==', '"Encoding "Another test!"';
+is encode_base64_str('username:thisisnotmypassword'), 'dXNlcm5hbWU6dGhpc2lzbm90bXlwYXNzd29yZA==', 'Encoding "username:thisisnotmypassword"';
+
+is decode_base64_str(''), '', 'Decoding the empty string';
+is decode_base64_str('QQ=='), 'A', 'Decoding "A"';
+is decode_base64_str('QWI='), 'Ab', 'Decoding "Ab"';
+is decode_base64_str('QWJj'), 'Abc', 'Decoding "Abc"';
+is decode_base64_str('QWJjZA=='), 'Abcd', 'Decoding "Abcd"';
+is decode_base64_str('UGVybA=='), 'Perl', 'Decoding "Perl"';
+is decode_base64_str('UGVybDY='), 'Perl6', 'Decoding "Perl6"';
+is decode_base64_str('QW5vdGhlciB0ZXN0IQ=='), 'Another test!', '"Decoding "Another test!"';
+is decode_base64_str('dXNlcm5hbWU6dGhpc2lzbm90bXlwYXNzd29yZA=='), 'username:thisisnotmypassword', 'Decoding "username:thisisnotmypassword"';
@@ -0,0 +1,29 @@
+use v6;
+
+use Test;
+use PP::Enc::MIME::Base64;
+
+plan 7;
+
+is encode_base64_str(
+ 'This is a long line whose base64 encoding should be broken into multiple lines as required by MIME.'),
+"VGhpcyBpcyBhIGxvbmcgbGluZSB3aG9zZSBiYXNlNjQgZW5jb2Rpbmcgc2hvdWxkIGJlIGJyb2tl
+biBpbnRvIG11bHRpcGxlIGxpbmVzIGFzIHJlcXVpcmVkIGJ5IE1JTUUu",
+ 'Encode Break up long line test';
+
+is encode_base64_str(
+ 'This is a long line whose base64 encoding should be broken into multiple lines as required by MIME.', eol => ''),
+"VGhpcyBpcyBhIGxvbmcgbGluZSB3aG9zZSBiYXNlNjQgZW5jb2Rpbmcgc2hvdWxkIGJlIGJyb2tlbiBpbnRvIG11bHRpcGxlIGxpbmVzIGFzIHJlcXVpcmVkIGJ5IE1JTUUu",
+ 'Encode Break up long line test with breakup override';
+
+is decode_base64_str(
+"VGhpcyBpcyBhIGxvbmcgbGluZSB3aG9zZSBiYXNlNjQgZW5jb2Rpbmcgc2hvdWxkIGJlIGJyb2tl
+biBpbnRvIG11bHRpcGxlIGxpbmVzIGFzIHJlcXVpcmVkIGJ5IE1JTUUu"),
+ 'This is a long line whose base64 encoding should be broken into multiple lines as required by MIME.',
+ 'Decode Break up long line test';
+
+my $screen_w_accent_e = chr(233) ~ "cran";
+is encode_base64_str($screen_w_accent_e, 'iso-8859-1'), '6WNyYW4=', 'latin-1 string encoding test';
+is decode_base64_str('6WNyYW4=', 'iso-8859-1'), $screen_w_accent_e, 'latin-1 string decoding test';
+is encode_base64_str($screen_w_accent_e), 'w6ljcmFu', 'utf-8 string encoding test';
+is decode_base64_str('w6ljcmFu'), $screen_w_accent_e, 'utf-8 string decoding test';
View
@@ -0,0 +1,31 @@
+use v6;
+
+# from http://tools.ietf.org/html/rfc4648#section-10
+
+use Test;
+use PP::Enc::MIME::Base64;
+
+plan 16;
+
+is encode_base64_str(''), '', 'Encoding the empty string';
+is encode_base64_str('f'), 'Zg==', 'Encoding "f"';
+is encode_base64_str('fo'), 'Zm8=', 'Encoding "fo"';
+is encode_base64_str('foo'), 'Zm9v', 'Encoding "foo"';
+is encode_base64_str('foob'), 'Zm9vYg==', 'Encoding "foob"';
+is encode_base64_str('fooba'), 'Zm9vYmE=', 'Encoding "fooba"';
+is encode_base64_str('foobar'), 'Zm9vYmFy', 'Encoding "foobar"';
+
+is decode_base64_str(''), '', 'Decoding the empty string';
+is decode_base64_str('Zg=='), 'f', 'Decoding "f"';
+is decode_base64_str('Zm8='), 'fo', 'Decoding "fo"';
+is decode_base64_str('Zm9v'), 'foo', 'Decoding "foo"';
+is decode_base64_str('Zm9vYg=='), 'foob', 'Decoding "foob"';
+is decode_base64_str('Zm9vYmE='), 'fooba', 'Decoding "fooba"';
+is decode_base64_str('Zm9vYmFy'), 'foobar', 'Decoding "foobar"';
+
+# not from RFC test vector but one odd test case from w3 HTTP spec and
+# perl 5 test suite
+is encode_base64_str('Aladdin:open sesame'),
+ 'QWxhZGRpbjpvcGVuIHNlc2FtZQ==', 'Encoding w3 http spec test open sesame';
+is decode_base64_str('QWxhZGRpbjpvcGVuIHNlc2FtZQ=='), 'Aladdin:open sesame',
+ 'Decoding w3 http spec test open sesame';
View
@@ -3,7 +3,7 @@ use v6;
# from http://tools.ietf.org/html/rfc4648#section-10
use Test;
-use PP::Enc::MIME::Base64;
+use Enc::MIME::Base64;
plan 16;

0 comments on commit 6bb3214

Please sign in to comment.