xref: /PHP-7.4/ext/xml/tests/bug32001b.phpt (revision 92ac598a)
1--TEST--
2Bug #32001 (xml_parse*() goes into infinite loop when autodetection in effect), using EUC-JP, Shift_JIS, GB2312
3--SKIPIF--
4<?php
5require_once("skipif.inc");
6if (!extension_loaded('iconv')) die ("skip iconv extension not available");
7foreach(array('EUC-JP', 'Shift_JISP', 'GB2312') as $encoding) {
8	if (@xml_parser_create($encoding) === false) die("skip libxml2 does not support $encoding encoding");
9}
10?>
11--FILE--
12<?php
13class testcase {
14	private $encoding;
15	private $bom;
16	private $prologue;
17	private $tags;
18	private $chunk_size;
19
20	function testcase($enc, $chunk_size = 0, $bom = 0, $omit_prologue = 0) {
21		$this->encoding = $enc;
22		$this->chunk_size = $chunk_size;
23		$this->bom = $bom;
24		$this->prologue = !$omit_prologue;
25		$this->tags = array();
26	}
27
28	function start_element($parser, $name, $attrs) {
29		$attrs = array_map('bin2hex', $attrs);
30		$this->tags[] = bin2hex($name).": ".implode(', ', $attrs);
31	}
32
33	function end_element($parser, $name) {
34	}
35
36	function run() {
37		$data = '';
38
39		if ($this->prologue) {
40			$canonical_name = preg_replace('/BE|LE/i', '', $this->encoding);
41			$data .= "<?xml version=\"1.0\" encoding=\"$canonical_name\" ?>\n";
42		}
43
44		$data .= <<<HERE
45<テスト:テスト1 xmlns:テスト="http://www.example.com/テスト/" テスト="テスト">
46  <テスト:テスト2 テスト="テスト">
47	<テスト:テスト3>
48	  test!
49	</テスト:テスト3>
50  </テスト:テスト2>
51</テスト:テスト1>
52HERE;
53
54		$data = iconv("UTF-8", $this->encoding, $data);
55
56		$parser = xml_parser_create(NULL);
57		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
58		xml_set_element_handler($parser, "start_element", "end_element");
59		xml_set_object($parser, $this);
60
61		if ($this->chunk_size == 0) {
62			$success = @xml_parse($parser, $data, true);
63		} else {
64			for ($offset = 0; $offset < strlen($data);
65					$offset += $this->chunk_size) {
66				$success = @xml_parse($parser, substr($data, $offset, $this->chunk_size), false);
67				if (!$success) {
68					break;
69				}
70			}
71			if ($success) {
72				$success = @xml_parse($parser, "", true);
73			}
74		}
75
76		echo "Encoding: $this->encoding\n";
77		echo "XML Prologue: ".($this->prologue ? 'present': 'not present'), "\n";
78		echo "Chunk size: ".($this->chunk_size ? "$this->chunk_size byte(s)\n": "all data at once\n");
79		echo "BOM: ".($this->bom ? 'prepended': 'not prepended'), "\n";
80
81		if ($success) {
82			var_dump($this->tags);
83		} else {
84			echo "[Error] ", xml_error_string(xml_get_error_code($parser)), "\n";
85		}
86	}
87}
88$suite = array(
89	new testcase("EUC-JP"  ,  0),
90	new testcase("EUC-JP"  ,  1),
91	new testcase("Shift_JIS", 0),
92	new testcase("Shift_JIS", 1),
93	new testcase("GB2312",    0),
94	new testcase("GB2312",    1),
95);
96
97if (XML_SAX_IMPL == 'libxml') {
98  echo "libxml2 Version => " . LIBXML_DOTTED_VERSION. "\n";
99} else {
100  echo "libxml2 Version => NONE\n";
101}
102
103foreach ($suite as $testcase) {
104	$testcase->run();
105}
106
107?>
108--EXPECTF--
109libxml2 Version => %s
110Encoding: EUC-JP
111XML Prologue: present
112Chunk size: all data at once
113BOM: not prepended
114array(3) {
115  [0]=>
116  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
117  [1]=>
118  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
119  [2]=>
120  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
121}
122Encoding: EUC-JP
123XML Prologue: present
124Chunk size: 1 byte(s)
125BOM: not prepended
126array(3) {
127  [0]=>
128  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
129  [1]=>
130  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
131  [2]=>
132  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
133}
134Encoding: Shift_JIS
135XML Prologue: present
136Chunk size: all data at once
137BOM: not prepended
138array(3) {
139  [0]=>
140  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
141  [1]=>
142  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
143  [2]=>
144  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
145}
146Encoding: Shift_JIS
147XML Prologue: present
148Chunk size: 1 byte(s)
149BOM: not prepended
150array(3) {
151  [0]=>
152  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
153  [1]=>
154  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
155  [2]=>
156  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
157}
158Encoding: GB2312
159XML Prologue: present
160Chunk size: all data at once
161BOM: not prepended
162array(3) {
163  [0]=>
164  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
165  [1]=>
166  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
167  [2]=>
168  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
169}
170Encoding: GB2312
171XML Prologue: present
172Chunk size: 1 byte(s)
173BOM: not prepended
174array(3) {
175  [0]=>
176  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
177  [1]=>
178  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
179  [2]=>
180  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
181}
182