Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[GH #813 + #814] Use Bytebuffer for MIME::Base64, add 2nd enc arg to …

…decode_base64

Use bytebuffer representations of the encoded string, not the encoded ord value.
Also fix the tests to match this conformant behaviour.

The problem is now that base64 encoded files are endian dependent, and the multibyte
tests need to be skipped on big-endian.
  • Loading branch information...
commit dc5d2846960872fdf7863aa11dc34f37b2c10a37 1 parent 072156a
Reini Urban rurban authored
90 runtime/parrot/library/MIME/Base64.pir
View
@@ -25,19 +25,18 @@ is the string to encode.
The returned encoded string is broken into lines
of no more than 76 characters each.
-=item C<decode_base64( str )>
+Note: Unicode stored as MIME::Base64 is inherently endian-dependent.
+
+=item C<decode_base64( str, ?:encoding )>
Decode a base64 string by calling the decode_base64() function.
-This function takes a single argument which is the string to decode
-and returns the decoded data.
+This function takes as first argument the string to decode,
+as optional second argument the encoding string for the decoded data.
+It returns the decoded data.
Any character not part of the 65-character base64 subset is silently ignored.
Characters occurring after a '=' padding character are never decoded.
-If the length of the string to decode, after ignoring non-base64 chars,
-is not a multiple of 4 or if padding occurs too early,
-then a warning is generated if perl is running under -w.
-
=back
=cut
@@ -93,12 +92,13 @@ then a warning is generated if perl is running under -w.
six_to_eight = get_global 'six_to_eight'
.local int len, len_mod_3
- len = length plain
.local pmc bb
+ # For unicode we cannot use chr/ord. This breaks endianness.
+ # GH 813 and #814
+ len = bytelength plain
bb = new ['ByteBuffer'], len
bb = plain
- len = elements bb
len_mod_3 = len % 3
# Fill up with with null bytes
if len_mod_3 == 0 goto END_1
@@ -121,7 +121,6 @@ then a warning is generated if perl is running under -w.
if i >= len goto END_3
# read 3*8 bits
- # TODO GH #813 and #814 unicode chars
eight_0 = bb[i]
inc i
eight_1 = bb[i]
@@ -183,32 +182,37 @@ then a warning is generated if perl is running under -w.
.sub decode_base64
.param string base64
+ .param string enc :optional
+ .param int has_enc :opt_flag
- .local string plain, base64_cleaned
+ .local string result, base64_cleaned
+ .local int enc_num
base64_cleaned = ''
- plain = ''
+ if has_enc goto HAS_ENC
+ enc = 'ascii'
+ HAS_ENC:
- .local pmc eight_to_six
+ .local pmc eight_to_six, bb
eight_to_six = get_global 'eight_to_six'
.local int i, len
.local int tmp_int_1, tmp_int_2
- .local string s_tmp_1
# Get rid of non-base64 chars
len = length base64
i = 0
- START_5:
+ START_5:
+ .local string s_tmp_1
if i >= len goto END_5
tmp_int_1 = ord base64, i
- inc i
- tmp_int_2 = eight_to_six[tmp_int_1]
- if tmp_int_2 == -1 goto START_5
- s_tmp_1 = chr tmp_int_1
- base64_cleaned = concat base64_cleaned, s_tmp_1
+ inc i
+ tmp_int_2 = eight_to_six[tmp_int_1]
+ if tmp_int_2 == -1 goto START_5
+ s_tmp_1 = chr tmp_int_1
+ base64_cleaned = concat base64_cleaned, s_tmp_1
goto START_5
- END_5:
+ END_5:
.local int len_mod_4
len = length base64_cleaned
len_mod_4 = len % 4
@@ -216,27 +220,27 @@ then a warning is generated if perl is running under -w.
# make sure that there are dummy bits beyond
base64_cleaned = concat base64_cleaned, ascii:"\0\0\0"
+ bb = new ['ByteBuffer']
.local int eight_0, eight_1, eight_2
.local int six_0, six_1, six_2, six_3
i = 0
- START_2:
+ START_2:
if i >= len goto END_2
# read 4*6 bits
tmp_int_1 = ord base64_cleaned, i
- six_0 = eight_to_six[tmp_int_1]
- inc i
+ six_0 = eight_to_six[tmp_int_1]
+ inc i
tmp_int_1 = ord base64_cleaned, i
- six_1 = eight_to_six[tmp_int_1]
- inc i
+ six_1 = eight_to_six[tmp_int_1]
+ inc i
tmp_int_1 = ord base64_cleaned, i
- six_2 = eight_to_six[tmp_int_1]
- inc i
+ six_2 = eight_to_six[tmp_int_1]
+ inc i
tmp_int_1 = ord base64_cleaned, i
- six_3 = eight_to_six[tmp_int_1]
- inc i
-
+ six_3 = eight_to_six[tmp_int_1]
+ inc i
# (f64[t.charAt(i)]<<2) | (f64[t.charAt(i+1)]>>4)
shl tmp_int_1, six_0, 2
@@ -256,25 +260,25 @@ then a warning is generated if perl is running under -w.
# write 3*8 bits
# output is larger than input
- s_tmp_1 = chr eight_0
- plain = concat plain, s_tmp_1
- s_tmp_1 = chr eight_1
- plain = concat plain, s_tmp_1
- s_tmp_1 = chr eight_2
- plain = concat plain, s_tmp_1
-
+ push bb, eight_0
+ push bb, eight_1
+ push bb, eight_2
goto START_2
- END_2:
+ END_2:
# cut padded '='
if len_mod_4 == 0 goto END_3
if len_mod_4 == 1 goto END_3
- plain = chopn plain, 1
+ len = elements bb
+ dec len
+ bb = len
if len_mod_4 == 3 goto END_3
- plain = chopn plain, 1
- END_3:
+ dec len
+ bb = len
- .return( plain )
+ END_3:
+ result = bb.'get_string'(enc)
+ .return( result )
.end
=head1 SEE ALSO
300 t/library/mime_base64.t
View
@@ -37,7 +37,7 @@ Test cases taken from base64.t of MIME::Base64.
.local pmc encode_decode_tests, decode_tests
encode_decode_tests = json.'compile'( <<'END_JSON' )
[ ["Hello, World!\n","SGVsbG8sIFdvcmxkIQo="],
- ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ ["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh\nYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYQ=="],
["\u0000","AA=="],
["\u0001","AQ=="],
@@ -167,136 +167,136 @@ Test cases taken from base64.t of MIME::Base64.
["}","fQ=="],
["~","fg=="],
["\u007f","fw=="],
- ["\u0080","gA=="],
- ["\u0081","gQ=="],
- ["\u0082","gg=="],
- ["\u0083","gw=="],
- ["\u0084","hA=="],
- ["\u0085","hQ=="],
- ["\u0086","hg=="],
- ["\u0087","hw=="],
- ["\u0088","iA=="],
- ["\u0089","iQ=="],
- ["\u008a","ig=="],
- ["\u008b","iw=="],
- ["\u008c","jA=="],
- ["\u008d","jQ=="],
- ["\u008e","jg=="],
- ["\u008f","jw=="],
- ["\u0090","kA=="],
- ["\u0091","kQ=="],
- ["\u0092","kg=="],
- ["\u0093","kw=="],
- ["\u0094","lA=="],
- ["\u0095","lQ=="],
- ["\u0096","lg=="],
- ["\u0097","lw=="],
- ["\u0098","mA=="],
- ["\u0099","mQ=="],
- ["\u009a","mg=="],
- ["\u009b","mw=="],
- ["\u009c","nA=="],
- ["\u009d","nQ=="],
- ["\u009e","ng=="],
- ["\u009f","nw=="],
- ["\u00a0","oA=="],
- ["\u00a1","oQ=="],
- ["\u00a2","og=="],
- ["\u00a3","ow=="],
- ["\u00a4","pA=="],
- ["\u00a5","pQ=="],
- ["\u00a6","pg=="],
- ["\u00a7","pw=="],
- ["\u00a8","qA=="],
- ["\u00a9","qQ=="],
- ["\u00aa","qg=="],
- ["\u00ab","qw=="],
- ["\u00ac","rA=="],
- ["\u00ad","rQ=="],
- ["\u00ae","rg=="],
- ["\u00af","rw=="],
- ["\u00b0","sA=="],
- ["\u00b1","sQ=="],
- ["\u00b2","sg=="],
- ["\u00b3","sw=="],
- ["\u00b4","tA=="],
- ["\u00b5","tQ=="],
- ["\u00b6","tg=="],
- ["\u00b7","tw=="],
- ["\u00b8","uA=="],
- ["\u00b9","uQ=="],
- ["\u00ba","ug=="],
- ["\u00bb","uw=="],
- ["\u00bc","vA=="],
- ["\u00bd","vQ=="],
- ["\u00be","vg=="],
- ["\u00bf","vw=="],
- ["\u00c0","wA=="],
- ["\u00c1","wQ=="],
- ["\u00c2","wg=="],
- ["\u00c3","ww=="],
- ["\u00c4","xA=="],
- ["\u00c5","xQ=="],
- ["\u00c6","xg=="],
- ["\u00c7","xw=="],
- ["\u00c8","yA=="],
- ["\u00c9","yQ=="],
- ["\u00ca","yg=="],
- ["\u00cb","yw=="],
- ["\u00cc","zA=="],
- ["\u00cd","zQ=="],
- ["\u00ce","zg=="],
- ["\u00cf","zw=="],
- ["\u00d0","0A=="],
- ["\u00d1","0Q=="],
- ["\u00d2","0g=="],
- ["\u00d3","0w=="],
- ["\u00d4","1A=="],
- ["\u00d5","1Q=="],
- ["\u00d6","1g=="],
- ["\u00d7","1w=="],
- ["\u00d8","2A=="],
- ["\u00d9","2Q=="],
- ["\u00da","2g=="],
- ["\u00db","2w=="],
- ["\u00dc","3A=="],
- ["\u00dd","3Q=="],
- ["\u00de","3g=="],
- ["\u00df","3w=="],
- ["\u00e0","4A=="],
- ["\u00e1","4Q=="],
- ["\u00e2","4g=="],
- ["\u00e3","4w=="],
- ["\u00e4","5A=="],
- ["\u00e5","5Q=="],
- ["\u00e6","5g=="],
- ["\u00e7","5w=="],
- ["\u00e8","6A=="],
- ["\u00e9","6Q=="],
- ["\u00ea","6g=="],
- ["\u00eb","6w=="],
- ["\u00ec","7A=="],
- ["\u00ed","7Q=="],
- ["\u00ee","7g=="],
- ["\u00ef","7w=="],
- ["\u00f0","8A=="],
- ["\u00f1","8Q=="],
- ["\u00f2","8g=="],
- ["\u00f3","8w=="],
- ["\u00f4","9A=="],
- ["\u00f5","9Q=="],
- ["\u00f6","9g=="],
- ["\u00f7","9w=="],
- ["\u00f8","+A=="],
- ["\u00f9","+Q=="],
- ["\u00fa","+g=="],
- ["\u00fb","+w=="],
- ["\u00fc","/A=="],
- ["\u00fd","/Q=="],
- ["\u00fe","/g=="],
- ["\u00ff","/w=="],
- ["\u0000\u00ff","AP8="],
- ["\u00ff\u0000","/wA="],
+ ["\u0080","woA="],
+ ["\u0081","woE="],
+ ["\u0082","woI="],
+ ["\u0083","woM="],
+ ["\u0084","woQ="],
+ ["\u0085","woU="],
+ ["\u0086","woY="],
+ ["\u0087","woc="],
+ ["\u0088","wog="],
+ ["\u0089","wok="],
+ ["\u008a","woo="],
+ ["\u008b","wos="],
+ ["\u008c","wow="],
+ ["\u008d","wo0="],
+ ["\u008e","wo4="],
+ ["\u008f","wo8="],
+ ["\u0090","wpA="],
+ ["\u0091","wpE="],
+ ["\u0092","wpI="],
+ ["\u0093","wpM="],
+ ["\u0094","wpQ="],
+ ["\u0095","wpU="],
+ ["\u0096","wpY="],
+ ["\u0097","wpc="],
+ ["\u0098","wpg="],
+ ["\u0099","wpk="],
+ ["\u009a","wpo="],
+ ["\u009b","wps="],
+ ["\u009c","wpw="],
+ ["\u009d","wp0="],
+ ["\u009e","wp4="],
+ ["\u009f","wp8="],
+ ["\u00a0","wqA="],
+ ["\u00a1","wqE="],
+ ["\u00a2","wqI="],
+ ["\u00a3","wqM="],
+ ["\u00a4","wqQ="],
+ ["\u00a5","wqU="],
+ ["\u00a6","wqY="],
+ ["\u00a7","wqc="],
+ ["\u00a8","wqg="],
+ ["\u00a9","wqk="],
+ ["\u00aa","wqo="],
+ ["\u00ab","wqs="],
+ ["\u00ac","wqw="],
+ ["\u00ad","wq0="],
+ ["\u00ae","wq4="],
+ ["\u00af","wq8="],
+ ["\u00b0","wrA="],
+ ["\u00b1","wrE="],
+ ["\u00b2","wrI="],
+ ["\u00b3","wrM="],
+ ["\u00b4","wrQ="],
+ ["\u00b5","wrU="],
+ ["\u00b6","wrY="],
+ ["\u00b7","wrc="],
+ ["\u00b8","wrg="],
+ ["\u00b9","wrk="],
+ ["\u00ba","wro="],
+ ["\u00bb","wrs="],
+ ["\u00bc","wrw="],
+ ["\u00bd","wr0="],
+ ["\u00be","wr4="],
+ ["\u00bf","wr8="],
+ ["\u00c0","w4A="],
+ ["\u00c1","w4E="],
+ ["\u00c2","w4I="],
+ ["\u00c3","w4M="],
+ ["\u00c4","w4Q="],
+ ["\u00c5","w4U="],
+ ["\u00c6","w4Y="],
+ ["\u00c7","w4c="],
+ ["\u00c8","w4g="],
+ ["\u00c9","w4k="],
+ ["\u00ca","w4o="],
+ ["\u00cb","w4s="],
+ ["\u00cc","w4w="],
+ ["\u00cd","w40="],
+ ["\u00ce","w44="],
+ ["\u00cf","w48="],
+ ["\u00d0","w5A="],
+ ["\u00d1","w5E="],
+ ["\u00d2","w5I="],
+ ["\u00d3","w5M="],
+ ["\u00d4","w5Q="],
+ ["\u00d5","w5U="],
+ ["\u00d6","w5Y="],
+ ["\u00d7","w5c="],
+ ["\u00d8","w5g="],
+ ["\u00d9","w5k="],
+ ["\u00da","w5o="],
+ ["\u00db","w5s="],
+ ["\u00dc","w5w="],
+ ["\u00dd","w50="],
+ ["\u00de","w54="],
+ ["\u00df","w58="],
+ ["\u00e0","w6A="],
+ ["\u00e1","w6E="],
+ ["\u00e2","w6I="],
+ ["\u00e3","w6M="],
+ ["\u00e4","w6Q="],
+ ["\u00e5","w6U="],
+ ["\u00e6","w6Y="],
+ ["\u00e7","w6c="],
+ ["\u00e8","w6g="],
+ ["\u00e9","w6k="],
+ ["\u00ea","w6o="],
+ ["\u00eb","w6s="],
+ ["\u00ec","w6w="],
+ ["\u00ed","w60="],
+ ["\u00ee","w64="],
+ ["\u00ef","w68="],
+ ["\u00f0","w7A="],
+ ["\u00f1","w7E="],
+ ["\u00f2","w7I="],
+ ["\u00f3","w7M="],
+ ["\u00f4","w7Q="],
+ ["\u00f5","w7U="],
+ ["\u00f6","w7Y="],
+ ["\u00f7","w7c="],
+ ["\u00f8","w7g="],
+ ["\u00f9","w7k="],
+ ["\u00fa","w7o="],
+ ["\u00fb","w7s="],
+ ["\u00fc","w7w="],
+ ["\u00fd","w70="],
+ ["\u00fe","w74="],
+ ["\u00ff","w78="],
+ ["\u0000\u00ff","AMO/"],
+ ["\u00ff\u0000","w78A"],
["\u0000\u0000\u0000","AAAA"],
["",""],
["a","YQ=="],
@@ -332,7 +332,7 @@ END_JSON
.local int count
count = 0
.local pmc test_iterator, test_case
- .local string plain, base64, comment, comment_count
+ .local string plain, base64, comment, comment_count, enc, esc_plain
encode_decode_tests = encode_decode_tests()
test_iterator = iter encode_decode_tests
@@ -343,9 +343,15 @@ END_JSON
base64 = shift test_case
comment = 'encode'
comment_count = count
+ $I0 = encoding plain
+ enc = encodingname $I0
+ esc_plain = escape plain
comment = concat comment, comment_count
- # comment = concat comment, " "
- # comment = concat comment, plain
+ comment = concat comment, " "
+ comment = concat comment, enc
+ comment = concat comment, ":\""
+ comment = concat comment, esc_plain
+ comment = concat comment, "\""
test_encode( plain, base64, comment )
comment = 'decode'
comment_count = count
@@ -415,27 +421,37 @@ CODE
.local pmc dec_sub
dec_sub = get_global [ "MIME"; "Base64" ], 'decode_base64'
- .local pmc eight_to_six
- eight_to_six = get_global 'eight_to_six'
-
.local pmc is
is = get_hll_global [ 'Test'; 'More' ], 'is'
.local string decode, result_decode
- .local string enc
+ .local string enc, enc1
$I0 = encoding plain
enc = encodingname $I0
- decode = dec_sub( base64 )
- result_decode = trans_encoding decode, $I0
+ if $I0 > 2 goto DEC_ENC
+ # ascii, latin1
+ decode = dec_sub( base64 )
+ decode = trans_encoding decode, $I0
+ goto DEC_2
+ DEC_ENC:
+ decode = dec_sub( base64, enc )
+ DEC_2:
+ $I1 = encoding decode
+ enc1 = encodingname $I1
+ .local string plain_norm, result_norm
+ result_norm = compose decode
+ plain_norm = compose plain
+ comment = concat comment, " "
+ comment = concat comment, enc1
comment = concat comment, " <-"
comment = concat comment, enc
- is( result_decode, plain, comment )
+ is( result_norm, plain_norm, comment )
.end
=head1 AUTHOR
-Bernhard Schmalhofer <Bernhard Schmalhofer@gmx.de> and others.
+Bernhard Schmalhofer <Bernhard Schmalhofer@gmx.de> and Reini Urban.
=cut
176 t/library/mime_base64u.t
View
@@ -11,31 +11,76 @@ t/library/mime_base64u.t - test unicode [ 'MIME'; 'Base64' ]
=head1 DESCRIPTION
-Test utf8 encoded [ 'MIME'; 'Base64' ]
+Test non-ascii encoded MIME::Base64, without JSON.
+
+perl -MMIME::Base64 -e'for ([qq(a2)],[qq(c2a2)],[qw(203e)],[qw(3e 20)],[qw(1000)],[qw(00c7)],[qw(00ff 0000)])
+{ $s=pack "H*",@{$_}; printf "0x%s\t=> %s",join("",@{$_}),encode_base64($s) }'
=cut
.sub test :main
load_bytecode 'Test/More.pbc'
- load_bytecode 'MIME/Base64.pbc'
+ load_bytecode 'MIME/Base64.pir'
.local pmc plan, is, ok
plan = get_hll_global [ 'Test'; 'More' ], 'plan'
is = get_hll_global [ 'Test'; 'More' ], 'is'
ok = get_hll_global [ 'Test'; 'More' ], 'ok'
- plan(5)
+ plan(20)
.local pmc encode_decode_tests, decode_tests
- encode_decode_tests = new 'FixedPMCArray', 2
+ .local int i, size
+ i = 0
+ size = 9
+ encode_decode_tests = new 'FixedPMCArray', size
+
$P0 = new 'FixedStringArray', 2
- $P0[0] = utf8:"\x{a2}"
- $P0[1] = "wqI="
- encode_decode_tests[0] = $P0
+ $P0[0] = binary:"\x3e\x20" # same as 0x203e bswapped
+ $P0[1] = "PiA="
+ encode_decode_tests[i] = $P0
+ inc i
$P0 = new 'FixedStringArray', 2
- $P0[0] = utf8:"\x{203e}"
+ $P0[0] = ucs2:"\x{203e}" # OVERLINE ‾ 0x203e
+ # TODO on big endian we'll get ID4=
+ $P0[1] = "PiA="
+ encode_decode_tests[i] = $P0
+ inc i
+ $P0 = new 'FixedStringArray', 2
+ $P0[0] = utf8:"\u203e" # OVERLINE ‾ 0xe280be in utf8
$P0[1] = "4oC+"
- encode_decode_tests[1] = $P0
+ encode_decode_tests[i] = $P0
+ inc i
+ $P0 = new 'FixedStringArray', 2
+ $P0[0] = iso-8859-1:"\x{a2}" # ¢
+ $P0[1] = "og=="
+ encode_decode_tests[i] = $P0
+ inc i
+ $P0 = new 'FixedStringArray', 2
+ $P0[0] = utf8:"\x{a2}" # ¢ 0xc2a2 in utf8
+ $P0[1] = "wqI="
+ encode_decode_tests[i] = $P0
+ inc i
+ $P0 = new 'FixedStringArray', 2
+ $P0[0] = ucs2:"\x{0100}" # Ā 0xc480 in utf8
+ $P0[1] = "AAE="
+ encode_decode_tests[i] = $P0
+ inc i
+ $P0 = new 'FixedStringArray', 2
+ $P0[0] = utf8:"\u0100" # Ā
+ $P0[1] = "xIA="
+ encode_decode_tests[i] = $P0
+ inc i
+ $P0 = new 'FixedStringArray', 2
+ $P0[0] = utf16:"\x{00c7}" # Ç
+ $P0[1] = "xwA="
+ encode_decode_tests[i] = $P0
+ inc i
+ $P0 = new 'FixedStringArray', 2
+ $P0[0] = ucs2:"\x{00ff}\x{0000}" # "ÿ\0"
+ $P0[1] = "/wAAAA=="
+ encode_decode_tests[i] = $P0
+ inc i
decode_tests = new 'FixedPMCArray', 2
$P0 = new 'FixedStringArray', 2
@@ -46,11 +91,12 @@ Test utf8 encoded [ 'MIME'; 'Base64' ]
$P0[0] = "YQ"
$P0[1] = "a"
decode_tests[1] = $P0
-
- .local int count
+
+ .local int count, enc_num, hex_cnt, bufused, len
count = 0
.local pmc test_iterator, test_case
- .local string plain, base64, comment, comment_count
+ .local string plain, base64, comment, comment_count, enc, esc_plain, hex_str
+ hex_str = ''
test_iterator = iter encode_decode_tests
enc_dec_loop:
@@ -58,11 +104,20 @@ Test utf8 encoded [ 'MIME'; 'Base64' ]
test_case = shift test_iterator
plain = test_case[0]
base64 = test_case[1]
- comment = 'encode'
- comment_count = count
- comment = concat comment, comment_count
- comment = concat comment, " "
- comment = concat comment, plain
+ enc_num = encoding plain
+ enc = encodingname enc_num
+ esc_plain = escape plain
+ bufused = bytelength plain
+ len = length plain
+ hex_str = hex_chars( plain, enc )
+ $P0 = new 'FixedPMCArray', 6
+ $P0[0] = count
+ $P0[1] = enc
+ $P0[2] = esc_plain
+ $P0[3] = hex_str
+ $P0[4] = bufused
+ $P0[5] = len
+ comment = sprintf "encode%d %s:\"%s\" %s(size=%d,len=%d)", $P0
test_encode( plain, base64, comment )
comment = 'decode'
comment_count = count
@@ -78,17 +133,47 @@ Test utf8 encoded [ 'MIME'; 'Base64' ]
test_case = shift test_iterator
base64 = test_case[0]
plain = test_case[1]
- comment = 'decode'
- comment_count = count
- comment = concat comment, comment_count
- comment = concat comment, " "
- comment = concat comment, plain
+ $I0 = encoding plain
+ enc = encodingname $I0
+ esc_plain = escape plain
+ $P0 = new 'ResizablePMCArray'
+ push $P0, comment_count
+ push $P0, enc
+ push $P0, esc_plain
+ push $P0, $I0
+ comment = sprintf "decode%d %s:\"%s\"(enc=%d)", $P0
test_decode( plain, base64, comment )
inc count
goto dec_loop
dec_loop_end:
.end
+.sub hex_chars
+ .param string chars
+ .param string enc
+
+ .local string s
+ .local pmc bb
+ .local int len
+ bb = new 'ByteBuffer'
+ s = clone chars
+ bb = s
+ s = bb.'get_string'(enc)
+ len = length s
+ $P0 = new 'FixedPMCArray', 2
+ $I1 = 0
+ $S0 = ''
+loop_1:
+ $I0 = ord s, $I1
+ $P0[0] = $S0
+ $P0[1] = $I0
+ $S0 = sprintf '%s\x%x', $P0
+ inc $I1
+ if $I1 < len goto loop_1
+ ## $S0 = chopn $S0, 2
+ .return ($S0)
+.end
+
.sub test_encode
.param string plain
.param string base64
@@ -111,29 +196,54 @@ Test utf8 encoded [ 'MIME'; 'Base64' ]
.param string base64
.param string comment
+ #.include "iglobals.pasm"
+ #.local pmc interp
+ #interp = getinterp
+ #.local pmc config
+ #config = interp[.IGLOBALS_CONFIG_HASH]
.local pmc dec_sub
dec_sub = get_global [ "MIME"; "Base64" ], 'decode_base64'
- .local pmc eight_to_six
- eight_to_six = get_global 'eight_to_six'
-
.local pmc is
is = get_hll_global [ 'Test'; 'More' ], 'is'
- .local string decode, result_decode
- .local string enc
+ .local string decode, result_norm, plain_norm
+ .local string enc, enc1
$I0 = encoding plain
enc = encodingname $I0
- decode = dec_sub( base64 )
- result_decode = trans_encoding decode, $I0
- comment = concat comment, " <-"
- comment = concat comment, enc
- is( result_decode, plain, comment )
+
+ decode = dec_sub( base64, enc )
+
+ $I1 = encoding decode
+ enc1 = encodingname $I1
+ $P0 = new 'ResizablePMCArray'
+ push $P0, comment
+ push $P0, enc1
+ push $P0, enc
+ push $P0, $I0
+ comment = sprintf "%s %s<-%s(enc=%d)", $P0
+ if enc == 'binary' goto HEX_C
+ .local int has_icu
+ #has_icu = config['has_icu']
+ has_icu = 1
+ if has_icu goto HAS_ICU
+
+ is( decode, plain, comment )
+ HAS_ICU:
+ result_norm = compose decode
+ plain_norm = compose plain
+ is( result_norm, plain_norm, comment )
+ goto END
+ HEX_C:
+ $S0 = hex_chars(decode, enc1)
+ $S1 = hex_chars(plain, enc)
+ is( $S0, $S1, comment )
+ END:
.end
=head1 AUTHOR
-Bernhard Schmalhofer <Bernhard Schmalhofer@gmx.de> and others.
+Reini Urban, Bernhard Schmalhofer <Bernhard Schmalhofer@gmx.de> and others.
=cut
Please sign in to comment.
Something went wrong with that request. Please try again.