Skip to content

Commit

Permalink
Save wrong MIME::Base64 rewrite with sliding multi-byte buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Reini Urban committed Oct 1, 2012
1 parent b21352c commit ee32193
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 34 deletions.
43 changes: 26 additions & 17 deletions runtime/parrot/library/MIME/Base64.pir
Expand Up @@ -89,11 +89,16 @@ Characters occurring after a '=' padding character are never decoded.
.local pmc six_to_eight
six_to_eight = get_global 'six_to_eight'

.local int len, len_mod_3
.local int blen, len, len_mod_3
len = length plain
len_mod_3 = len % 3
print "len="
say len
blen = bytelength plain
len_mod_3 = blen % 3
print "# len="
print len
print ", blen="
print blen
print ", len_mod_3="
say len_mod_3

# Fill up with with null bytes
if len_mod_3 == 1 goto END_1
Expand Down Expand Up @@ -125,7 +130,7 @@ say len
DRAIN_BUF:
# With more than 3 elements (0-2) in our buf, "drain it", move all elements to the left
if ix < 4 goto LOOP_3
print "drain buf: ix="
print "# drain buf: ix="
say ix
$I0 = ix - 1
$I1 = buf[ix]
Expand All @@ -138,10 +143,10 @@ say ix
if ix > 2 goto HAVE_3
if i >= len goto HAVE_3
$I0 = ord plain, i
print "ord="
say $I0
print "ix="
say ix
print "# ord="
print $I0
print ", ix="
print ix
print "i="
say i
buf[ix] = $I0
Expand All @@ -154,9 +159,9 @@ say i
$S0 = substr plain, i, 1
bl = bytelength $S0
inc i
print "mb bl="
say bl
print "enc="
print "# MB bl="
print bl
print ", enc="
$I2 = encoding $S0
$S1 = encodingname $I2
say $S1
Expand All @@ -178,16 +183,18 @@ say $S1
eight_2 = buf[2]
if i < len goto MB_2
if ix > 2 goto MB_2
say "reset ix 2" # ix = 2
say "# reset ix 2" # ix = 2
eight_2 = 0
if ix > 1 goto MB_2
say "reset ix 1" # ix = 1
say "# reset ix 1" # ix = 1
eight_1 = 0
MB_2:
ix = ix - 3
say "eight_0-2:"
print "# eight_0="
say eight_0
print "# eight_1="
say eight_1
print "# eight_2="
say eight_2
shr six_0, eight_0, 2

Expand Down Expand Up @@ -232,8 +239,10 @@ say eight_2
END_3:
# padding with '='
if len_mod_3 == 0 goto END_2
say "# pad=" #mod 1 or 2
base64 = replace base64, -1, 1, ascii:"="
if len_mod_3 == 2 goto END_2
if len_mod_3 == 1 goto END_2
say "# pad==" #mod 1
base64 = replace base64, -2, 1, ascii:"="
END_2:
.return( base64 )
Expand All @@ -252,7 +261,7 @@ say eight_2
enc = 'ascii'
ENC_1:
enc_num = find_encoding enc
plain = trans_encoding plain, $I0
plain = trans_encoding plain, enc_num

.local pmc eight_to_six
eight_to_six = get_global 'eight_to_six'
Expand Down
29 changes: 12 additions & 17 deletions t/library/mime_base64u.t
Expand Up @@ -13,8 +13,8 @@ t/library/mime_base64u.t - test unicode [ 'MIME'; 'Base64' ]

Test non-ascii encoded MIME::Base64, without JSON.

perl -MMIME::Base64 -e'for ([qq(a2)],[qw(20 3e)],[qw(00 10)],[qw(c7)],[qw(00 ff 00 00)])
{ $s=pack "H*",@{$_}; printf "0x%s\t=> %s\n",join("",@{$_}),encode_base64($s) }'
perl -MMIME::Base64 -e'for ([qq(a2)],[qw(20 3e)],[qw(3e 20)],[qw(00 10)],[qw(c7)],[qw(00 ff 00 00)])
{ $s=pack "H*",@{$_}; printf "0x%s\t=> %s",join("",@{$_}),encode_base64($s) }'

=cut

Expand All @@ -32,28 +32,23 @@ perl -MMIME::Base64 -e'for ([qq(a2)],[qw(20 3e)],[qw(00 10)],[qw(c7)],[qw(00 ff
.local pmc encode_decode_tests, decode_tests
.local int i, size
i = 0
size = 10
size = 9
encode_decode_tests = new 'FixedPMCArray', size

$P0 = new 'FixedStringArray', 2
$P0[0] = "Hello, World!\n"
$P0[1] = "SGVsbG8sIFdvcmxkIQo="
encode_decode_tests[i] = $P0
inc i

$P0 = new 'FixedStringArray', 2
$P0[0] = binary:"\x3e\x20" # same as OVERLINE ‾ 0x203e bswapped
$P0[1] = "Pi=="
$P0[0] = binary:"\x3e\x20" # same as 0x203e bswapped
$P0[1] = "Pg=="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = ucs2:"\x{203e}" # OVERLINE ‾ 0x203e
$P0[1] = "Pi=="
# TODO on big endian we'll get ID4=
$P0[1] = "Pg=="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = utf8:"\u203e" # OVERLINE ‾ 0xe280be in utf8
$P0[1] = "Pi=="
$P0[1] = "4g=="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
Expand All @@ -68,22 +63,22 @@ perl -MMIME::Base64 -e'for ([qq(a2)],[qw(20 3e)],[qw(00 10)],[qw(c7)],[qw(00 ff
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = ucs2:"\x{0100}" # Ā 0xc480 in utf8
$P0[1] = "AAE="
$P0[1] = "EAA="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = utf8:"\u0100" # Ā
$P0[1] = "xIA="
$P0[1] = "xI=="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = utf16:"\x{00c7}" # Ç
$P0[1] = "xwA="
$P0[1] = "AMc="
encode_decode_tests[i] = $P0
inc i
$P0 = new 'FixedStringArray', 2
$P0[0] = ucs2:"\x{00ff}\x{0000}" # "ÿ\0"
$P0[1] = "/wAAAA=="
$P0[1] = "AP8="
encode_decode_tests[i] = $P0
inc i

Expand Down

0 comments on commit ee32193

Please sign in to comment.