1--TEST--
2mb_convert_variables()
3--EXTENSIONS--
4mbstring
5--INI--
6output_handler=
7mbstring.language=Japanese
8--FILE--
9<?php
10// TODO: Add more tests
11//$debug = true; // Uncomment this line to view error/warning/notice message in *.out file
12ini_set('include_path', __DIR__);
13include_once('common.inc');
14
15// SJIS string (BASE64 encoded)
16$sjis = base64_decode('k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==');
17// JIS string (BASE64 encoded)
18$jis = base64_decode('GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==');
19// EUC-JP string
20$euc_jp = mb_convert_encoding("日本語テキストです。0123456789。", 'EUC-JP', 'UTF-8');
21
22// Test for single scalar
23echo "== SCALAR TEST ==\n";
24$s = $sjis;
25$encoding = mb_convert_variables('EUC-JP', 'SJIS', $s);
26print("$encoding\n"); // SJIS
27echo bin2hex($s), "\n"; // Converted to EUC-JP
28
29$s = $jis;
30$encoding = mb_convert_variables('EUC-JP', 'JIS', $s);
31print("$encoding\n"); // JIS
32echo bin2hex($s), "\n"; // Converted to EUC-JP
33
34$s = $euc_jp;
35$encoding = mb_convert_variables('SJIS', 'EUC-JP', $s);
36print("$encoding\n"); // EUC-JP
37print(base64_encode($s)."\n"); // Converted to SJIS (base64 encoded)
38
39$s = $euc_jp;
40$encoding = mb_convert_variables('JIS', 'EUC-JP', $s);
41print("$encoding\n"); // EUC-JP
42print(base64_encode($s)."\n"); // Converted to JIS (base64 encoded)
43
44// Test for multiple scalar
45$s1 = $euc_jp;
46$s2 = $euc_jp;
47$s3 = $euc_jp;
48$encoding = mb_convert_variables('EUC-JP', 'auto', $s1, $s2, $s3);
49print("$encoding\n"); // EUC-JP
50echo bin2hex("$s1$s2$s3"), "\n"; // Converted to EUC-JP
51
52// Note: Mixing encoding in array/object is not supported
53echo "== ARRAY TEST ==\n";
54$a = array($s3, $s2, $s1);
55$aa = $a;
56$encoding = mb_convert_variables('EUC-JP', 'auto', $aa);
57print("$encoding\n"); // EUC-JP
58echo bin2hex("{$aa[0]}{$aa[1]}{$aa[2]}"), "\n"; // Converted to EUC-JP
59
60$a = array($s1, $s2, $s3);
61$aa = $a;
62$encoding = mb_convert_variables('EUC-JP', 'auto', $aa);
63print("$encoding\n"); // EUC-JP
64echo bin2hex("{$aa[0]}{$aa[1]}{$aa[2]}"), "\n"; // Converted to EUC-JP
65
66// Test for object
67echo "== OBJECT TEST ==\n";
68class foo
69{
70    public $s1;
71    public $s2;
72    public $s3;
73
74    function __construct()
75    {
76        global $sjis, $jis, $euc_jp;
77
78        $this->s1 = $euc_jp;
79        $this->s2 = $euc_jp;
80        $this->s3 = $euc_jp;
81    }
82}
83
84class bar
85{
86    public $s1;
87    public $s2;
88    public $s3;
89
90    function __construct()
91    {
92        global $sjis, $jis, $euc_jp;
93
94        $this->s1 = $euc_jp;
95        $this->s2 = $euc_jp;
96        $this->s3 = $euc_jp;
97    }
98}
99
100$o = new foo;
101$oo = $o;
102$encoding = mb_convert_variables('EUC-JP', 'auto', $oo);
103print("$encoding\n");   // EUC-JP
104echo bin2hex("{$oo->s1}{$oo->s2}{$oo->s3}"), "\n"; // Converted to EUC-JP
105
106$o = new bar;
107$oo = $o;
108$encoding = mb_convert_variables('EUC-JP', 'auto', $oo);
109print("$encoding\n"); // EUC-JP
110echo bin2hex("{$oo->s1}{$oo->s2}{$oo->s3}"), "\n"; // Converted to EUC-JP
111
112// Test for scalar, array and object
113echo "== SCALAR, ARRAY AND OBJECT TEST ==\n";
114
115$s1 = $euc_jp;
116$s2 = $euc_jp;
117$s3 = $euc_jp;
118$a = array($s1, $s2, $s3);
119$aa = $a;
120$oo = $o;
121
122$encoding = mb_convert_variables('EUC-JP', 'auto', $s1, $s2, $s3, $aa, $oo);
123print("$encoding\n"); // EUC-JP
124echo bin2hex("$s1$s2$s3"), "\n"; // Converted to EUC-JP
125echo bin2hex("{$aa[0]}{$aa[1]}{$aa[2]}"), "\n"; // Converted to EUC-JP
126echo bin2hex("{$oo->s1}{$oo->s2}{$oo->s3}"), "\n"; // Converted to EUC-JP
127
128echo "== DEEPLY NESTED OBJECT/ARRAY TEST ==\n";
129
130class Nested
131{
132    public $inner;
133
134    function __construct($value)
135    {
136        $this->inner = $value;
137    }
138}
139
140$deeplyNested = array(new Nested(array(new Nested(array(new Nested("BLAH"))))));
141
142$encoding = mb_convert_variables('UTF-16LE', 'UTF-8', $deeplyNested);
143echo $encoding, "\n";
144echo bin2hex($deeplyNested[0]->inner[0]->inner[0]->inner), "\n";
145
146echo "== INVALID STRING ENCODING TEST ==\n";
147// Make sure both that the correct invalid encoding marker is used,
148// and that the count of illegal characters is incremented
149
150$illegalCount = mb_get_info('illegal_chars');
151$nested = array(new Nested("\xFF"));
152mb_substitute_character(0x25);
153mb_convert_variables('UTF-16LE', 'UTF-8', $nested);
154echo bin2hex($nested[0]->inner), "\n";
155echo "# of illegal characters detected: ", mb_get_info('illegal_chars') - $illegalCount, "\n";
156
157$illegalCount = mb_get_info('illegal_chars');
158$nested = array(new Nested("\xFF"));
159mb_substitute_character(0x26);
160mb_convert_variables('UTF-16LE', 'UTF-8', $nested);
161echo bin2hex($nested[0]->inner), "\n";
162echo "# of illegal characters detected: ", mb_get_info('illegal_chars') - $illegalCount, "\n";
163
164echo "== ENCODING AUTO-DETECTION TEST ==\n";
165
166ini_set('mbstring.strict_detection', '1');
167$bad_utf7 = "abc + abc";
168var_dump(mb_convert_variables('UTF-8', 'UTF-7,UTF-8', $bad_utf7));
169var_dump($bad_utf7);
170
171$bad_utf7imap = "abc &";
172var_dump(mb_convert_variables('UTF-8', 'UTF7-IMAP,UTF-8', $bad_utf7imap));
173var_dump($bad_utf7imap);
174
175?>
176--EXPECT--
177== SCALAR TEST ==
178SJIS
179c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
180JIS
181c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
182EUC-JP
183k/qWe4zqg2WDTINYg2eCxYK3gUIwMTIzNIJUglWCVoJXgliBQg==
184EUC-JP
185GyRCRnxLXDhsJUYlLSU5JUgkRyQ5ISMbKEIwMTIzNBskQiM1IzYjNyM4IzkhIxsoQg==
186EUC-JP
187c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
188== ARRAY TEST ==
189EUC-JP
190c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
191EUC-JP
192c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
193== OBJECT TEST ==
194EUC-JP
195c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
196EUC-JP
197c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
198== SCALAR, ARRAY AND OBJECT TEST ==
199EUC-JP
200c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
201c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
202c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3c6fccbdcb8eca5c6a5ada5b9a5c8a4c7a4b9a1a33031323334a3b5a3b6a3b7a3b8a3b9a1a3
203== DEEPLY NESTED OBJECT/ARRAY TEST ==
204UTF-8
20542004c0041004800
206== INVALID STRING ENCODING TEST ==
2072500
208# of illegal characters detected: 1
2092600
210# of illegal characters detected: 1
211== ENCODING AUTO-DETECTION TEST ==
212string(5) "UTF-8"
213string(9) "abc + abc"
214string(5) "UTF-8"
215string(5) "abc &"
216