Dashboard sipadu mbip
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CharacterStream.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime;
  11. /**
  12. * @author Fabien Potencier <fabien@symfony.com>
  13. * @author Xavier De Cock <xdecock@gmail.com>
  14. *
  15. * @internal
  16. *
  17. * @experimental in 4.3
  18. */
  19. final class CharacterStream
  20. {
  21. /** Pre-computed for optimization */
  22. private const UTF8_LENGTH_MAP = [
  23. "\x00" => 1, "\x01" => 1, "\x02" => 1, "\x03" => 1, "\x04" => 1, "\x05" => 1, "\x06" => 1, "\x07" => 1,
  24. "\x08" => 1, "\x09" => 1, "\x0a" => 1, "\x0b" => 1, "\x0c" => 1, "\x0d" => 1, "\x0e" => 1, "\x0f" => 1,
  25. "\x10" => 1, "\x11" => 1, "\x12" => 1, "\x13" => 1, "\x14" => 1, "\x15" => 1, "\x16" => 1, "\x17" => 1,
  26. "\x18" => 1, "\x19" => 1, "\x1a" => 1, "\x1b" => 1, "\x1c" => 1, "\x1d" => 1, "\x1e" => 1, "\x1f" => 1,
  27. "\x20" => 1, "\x21" => 1, "\x22" => 1, "\x23" => 1, "\x24" => 1, "\x25" => 1, "\x26" => 1, "\x27" => 1,
  28. "\x28" => 1, "\x29" => 1, "\x2a" => 1, "\x2b" => 1, "\x2c" => 1, "\x2d" => 1, "\x2e" => 1, "\x2f" => 1,
  29. "\x30" => 1, "\x31" => 1, "\x32" => 1, "\x33" => 1, "\x34" => 1, "\x35" => 1, "\x36" => 1, "\x37" => 1,
  30. "\x38" => 1, "\x39" => 1, "\x3a" => 1, "\x3b" => 1, "\x3c" => 1, "\x3d" => 1, "\x3e" => 1, "\x3f" => 1,
  31. "\x40" => 1, "\x41" => 1, "\x42" => 1, "\x43" => 1, "\x44" => 1, "\x45" => 1, "\x46" => 1, "\x47" => 1,
  32. "\x48" => 1, "\x49" => 1, "\x4a" => 1, "\x4b" => 1, "\x4c" => 1, "\x4d" => 1, "\x4e" => 1, "\x4f" => 1,
  33. "\x50" => 1, "\x51" => 1, "\x52" => 1, "\x53" => 1, "\x54" => 1, "\x55" => 1, "\x56" => 1, "\x57" => 1,
  34. "\x58" => 1, "\x59" => 1, "\x5a" => 1, "\x5b" => 1, "\x5c" => 1, "\x5d" => 1, "\x5e" => 1, "\x5f" => 1,
  35. "\x60" => 1, "\x61" => 1, "\x62" => 1, "\x63" => 1, "\x64" => 1, "\x65" => 1, "\x66" => 1, "\x67" => 1,
  36. "\x68" => 1, "\x69" => 1, "\x6a" => 1, "\x6b" => 1, "\x6c" => 1, "\x6d" => 1, "\x6e" => 1, "\x6f" => 1,
  37. "\x70" => 1, "\x71" => 1, "\x72" => 1, "\x73" => 1, "\x74" => 1, "\x75" => 1, "\x76" => 1, "\x77" => 1,
  38. "\x78" => 1, "\x79" => 1, "\x7a" => 1, "\x7b" => 1, "\x7c" => 1, "\x7d" => 1, "\x7e" => 1, "\x7f" => 1,
  39. "\x80" => 0, "\x81" => 0, "\x82" => 0, "\x83" => 0, "\x84" => 0, "\x85" => 0, "\x86" => 0, "\x87" => 0,
  40. "\x88" => 0, "\x89" => 0, "\x8a" => 0, "\x8b" => 0, "\x8c" => 0, "\x8d" => 0, "\x8e" => 0, "\x8f" => 0,
  41. "\x90" => 0, "\x91" => 0, "\x92" => 0, "\x93" => 0, "\x94" => 0, "\x95" => 0, "\x96" => 0, "\x97" => 0,
  42. "\x98" => 0, "\x99" => 0, "\x9a" => 0, "\x9b" => 0, "\x9c" => 0, "\x9d" => 0, "\x9e" => 0, "\x9f" => 0,
  43. "\xa0" => 0, "\xa1" => 0, "\xa2" => 0, "\xa3" => 0, "\xa4" => 0, "\xa5" => 0, "\xa6" => 0, "\xa7" => 0,
  44. "\xa8" => 0, "\xa9" => 0, "\xaa" => 0, "\xab" => 0, "\xac" => 0, "\xad" => 0, "\xae" => 0, "\xaf" => 0,
  45. "\xb0" => 0, "\xb1" => 0, "\xb2" => 0, "\xb3" => 0, "\xb4" => 0, "\xb5" => 0, "\xb6" => 0, "\xb7" => 0,
  46. "\xb8" => 0, "\xb9" => 0, "\xba" => 0, "\xbb" => 0, "\xbc" => 0, "\xbd" => 0, "\xbe" => 0, "\xbf" => 0,
  47. "\xc0" => 2, "\xc1" => 2, "\xc2" => 2, "\xc3" => 2, "\xc4" => 2, "\xc5" => 2, "\xc6" => 2, "\xc7" => 2,
  48. "\xc8" => 2, "\xc9" => 2, "\xca" => 2, "\xcb" => 2, "\xcc" => 2, "\xcd" => 2, "\xce" => 2, "\xcf" => 2,
  49. "\xd0" => 2, "\xd1" => 2, "\xd2" => 2, "\xd3" => 2, "\xd4" => 2, "\xd5" => 2, "\xd6" => 2, "\xd7" => 2,
  50. "\xd8" => 2, "\xd9" => 2, "\xda" => 2, "\xdb" => 2, "\xdc" => 2, "\xdd" => 2, "\xde" => 2, "\xdf" => 2,
  51. "\xe0" => 3, "\xe1" => 3, "\xe2" => 3, "\xe3" => 3, "\xe4" => 3, "\xe5" => 3, "\xe6" => 3, "\xe7" => 3,
  52. "\xe8" => 3, "\xe9" => 3, "\xea" => 3, "\xeb" => 3, "\xec" => 3, "\xed" => 3, "\xee" => 3, "\xef" => 3,
  53. "\xf0" => 4, "\xf1" => 4, "\xf2" => 4, "\xf3" => 4, "\xf4" => 4, "\xf5" => 4, "\xf6" => 4, "\xf7" => 4,
  54. "\xf8" => 5, "\xf9" => 5, "\xfa" => 5, "\xfb" => 5, "\xfc" => 6, "\xfd" => 6, "\xfe" => 0, "\xff" => 0,
  55. ];
  56. private $data = '';
  57. private $dataSize = 0;
  58. private $map = [];
  59. private $charCount = 0;
  60. private $currentPos = 0;
  61. private $fixedWidth = 0;
  62. /**
  63. * @param resource|string $input
  64. */
  65. public function __construct($input, ?string $charset = 'utf-8')
  66. {
  67. $charset = strtolower(trim($charset)) ?: 'utf-8';
  68. if ('utf-8' === $charset || 'utf8' === $charset) {
  69. $this->fixedWidth = 0;
  70. $this->map = ['p' => [], 'i' => []];
  71. } else {
  72. switch ($charset) {
  73. // 16 bits
  74. case 'ucs2':
  75. case 'ucs-2':
  76. case 'utf16':
  77. case 'utf-16':
  78. $this->fixedWidth = 2;
  79. break;
  80. // 32 bits
  81. case 'ucs4':
  82. case 'ucs-4':
  83. case 'utf32':
  84. case 'utf-32':
  85. $this->fixedWidth = 4;
  86. break;
  87. // 7-8 bit charsets: (us-)?ascii, (iso|iec)-?8859-?[0-9]+, windows-?125[0-9], cp-?[0-9]+, ansi, macintosh,
  88. // koi-?7, koi-?8-?.+, mik, (cork|t1), v?iscii
  89. // and fallback
  90. default:
  91. $this->fixedWidth = 1;
  92. }
  93. }
  94. if (\is_resource($input)) {
  95. $blocks = 512;
  96. if (stream_get_meta_data($input)['seekable'] ?? false) {
  97. rewind($input);
  98. }
  99. while (false !== $read = fread($input, $blocks)) {
  100. $this->write($read);
  101. }
  102. } else {
  103. $this->write($input);
  104. }
  105. }
  106. public function read(int $length): ?string
  107. {
  108. if ($this->currentPos >= $this->charCount) {
  109. return null;
  110. }
  111. $ret = null;
  112. $length = ($this->currentPos + $length > $this->charCount) ? $this->charCount - $this->currentPos : $length;
  113. if ($this->fixedWidth > 0) {
  114. $len = $length * $this->fixedWidth;
  115. $ret = substr($this->data, $this->currentPos * $this->fixedWidth, $len);
  116. $this->currentPos += $length;
  117. } else {
  118. $end = $this->currentPos + $length;
  119. $end = $end > $this->charCount ? $this->charCount : $end;
  120. $ret = '';
  121. $start = 0;
  122. if ($this->currentPos > 0) {
  123. $start = $this->map['p'][$this->currentPos - 1];
  124. }
  125. $to = $start;
  126. for (; $this->currentPos < $end; ++$this->currentPos) {
  127. if (isset($this->map['i'][$this->currentPos])) {
  128. $ret .= substr($this->data, $start, $to - $start).'?';
  129. $start = $this->map['p'][$this->currentPos];
  130. } else {
  131. $to = $this->map['p'][$this->currentPos];
  132. }
  133. }
  134. $ret .= substr($this->data, $start, $to - $start);
  135. }
  136. return $ret;
  137. }
  138. public function readBytes(int $length): ?array
  139. {
  140. if (null !== $read = $this->read($length)) {
  141. return array_map('ord', str_split($read, 1));
  142. }
  143. return null;
  144. }
  145. public function setPointer(int $charOffset): void
  146. {
  147. if ($this->charCount < $charOffset) {
  148. $charOffset = $this->charCount;
  149. }
  150. $this->currentPos = $charOffset;
  151. }
  152. public function write(string $chars): void
  153. {
  154. $ignored = '';
  155. $this->data .= $chars;
  156. if ($this->fixedWidth > 0) {
  157. $strlen = \strlen($chars);
  158. $ignoredL = $strlen % $this->fixedWidth;
  159. $ignored = $ignoredL ? substr($chars, -$ignoredL) : '';
  160. $this->charCount += ($strlen - $ignoredL) / $this->fixedWidth;
  161. } else {
  162. $this->charCount += $this->getUtf8CharPositions($chars, $this->dataSize, $ignored);
  163. }
  164. $this->dataSize = \strlen($this->data) - \strlen($ignored);
  165. }
  166. private function getUtf8CharPositions(string $string, int $startOffset, &$ignoredChars): int
  167. {
  168. $strlen = \strlen($string);
  169. $charPos = \count($this->map['p']);
  170. $foundChars = 0;
  171. $invalid = false;
  172. for ($i = 0; $i < $strlen; ++$i) {
  173. $char = $string[$i];
  174. $size = self::UTF8_LENGTH_MAP[$char];
  175. if (0 == $size) {
  176. /* char is invalid, we must wait for a resync */
  177. $invalid = true;
  178. continue;
  179. }
  180. if ($invalid) {
  181. /* We mark the chars as invalid and start a new char */
  182. $this->map['p'][$charPos + $foundChars] = $startOffset + $i;
  183. $this->map['i'][$charPos + $foundChars] = true;
  184. ++$foundChars;
  185. $invalid = false;
  186. }
  187. if (($i + $size) > $strlen) {
  188. $ignoredChars = substr($string, $i);
  189. break;
  190. }
  191. for ($j = 1; $j < $size; ++$j) {
  192. $char = $string[$i + $j];
  193. if ($char > "\x7F" && $char < "\xC0") {
  194. // Valid - continue parsing
  195. } else {
  196. /* char is invalid, we must wait for a resync */
  197. $invalid = true;
  198. continue 2;
  199. }
  200. }
  201. /* Ok we got a complete char here */
  202. $this->map['p'][$charPos + $foundChars] = $startOffset + $i + $size;
  203. $i += $j - 1;
  204. ++$foundChars;
  205. }
  206. return $foundChars;
  207. }
  208. }