bug32001b.phpt 5.5 KB

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