bug32001b.phpt 5.2 KB

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