ArrayCharacterStream.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * A CharacterStream implementation which stores characters in an internal array.
  11. *
  12. * @author Chris Corbyn
  13. */
  14. class Swift_CharacterStream_ArrayCharacterStream implements Swift_CharacterStream
  15. {
  16. /** A map of byte values and their respective characters */
  17. private static $charMap;
  18. /** A map of characters and their derivative byte values */
  19. private static $byteMap;
  20. /** The char reader (lazy-loaded) for the current charset */
  21. private $charReader;
  22. /** A factory for creating CharacterReader instances */
  23. private $charReaderFactory;
  24. /** The character set this stream is using */
  25. private $charset;
  26. /** Array of characters */
  27. private $array = [];
  28. /** Size of the array of character */
  29. private $array_size = [];
  30. /** The current character offset in the stream */
  31. private $offset = 0;
  32. /**
  33. * Create a new CharacterStream with the given $chars, if set.
  34. *
  35. * @param Swift_CharacterReaderFactory $factory for loading validators
  36. * @param string $charset used in the stream
  37. */
  38. public function __construct(Swift_CharacterReaderFactory $factory, $charset)
  39. {
  40. self::initializeMaps();
  41. $this->setCharacterReaderFactory($factory);
  42. $this->setCharacterSet($charset);
  43. }
  44. /**
  45. * Set the character set used in this CharacterStream.
  46. *
  47. * @param string $charset
  48. */
  49. public function setCharacterSet($charset)
  50. {
  51. $this->charset = $charset;
  52. $this->charReader = null;
  53. }
  54. /**
  55. * Set the CharacterReaderFactory for multi charset support.
  56. */
  57. public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory)
  58. {
  59. $this->charReaderFactory = $factory;
  60. }
  61. /**
  62. * Overwrite this character stream using the byte sequence in the byte stream.
  63. *
  64. * @param Swift_OutputByteStream $os output stream to read from
  65. */
  66. public function importByteStream(Swift_OutputByteStream $os)
  67. {
  68. if (!isset($this->charReader)) {
  69. $this->charReader = $this->charReaderFactory
  70. ->getReaderFor($this->charset);
  71. }
  72. $startLength = $this->charReader->getInitialByteSize();
  73. while (false !== $bytes = $os->read($startLength)) {
  74. $c = [];
  75. for ($i = 0, $len = strlen($bytes); $i < $len; ++$i) {
  76. $c[] = self::$byteMap[$bytes[$i]];
  77. }
  78. $size = count($c);
  79. $need = $this->charReader
  80. ->validateByteSequence($c, $size);
  81. if ($need > 0 &&
  82. false !== $bytes = $os->read($need)) {
  83. for ($i = 0, $len = strlen($bytes); $i < $len; ++$i) {
  84. $c[] = self::$byteMap[$bytes[$i]];
  85. }
  86. }
  87. $this->array[] = $c;
  88. ++$this->array_size;
  89. }
  90. }
  91. /**
  92. * Import a string a bytes into this CharacterStream, overwriting any existing
  93. * data in the stream.
  94. *
  95. * @param string $string
  96. */
  97. public function importString($string)
  98. {
  99. $this->flushContents();
  100. $this->write($string);
  101. }
  102. /**
  103. * Read $length characters from the stream and move the internal pointer
  104. * $length further into the stream.
  105. *
  106. * @param int $length
  107. *
  108. * @return string
  109. */
  110. public function read($length)
  111. {
  112. if ($this->offset == $this->array_size) {
  113. return false;
  114. }
  115. // Don't use array slice
  116. $arrays = [];
  117. $end = $length + $this->offset;
  118. for ($i = $this->offset; $i < $end; ++$i) {
  119. if (!isset($this->array[$i])) {
  120. break;
  121. }
  122. $arrays[] = $this->array[$i];
  123. }
  124. $this->offset += $i - $this->offset; // Limit function calls
  125. $chars = false;
  126. foreach ($arrays as $array) {
  127. $chars .= implode('', array_map('chr', $array));
  128. }
  129. return $chars;
  130. }
  131. /**
  132. * Read $length characters from the stream and return a 1-dimensional array
  133. * containing there octet values.
  134. *
  135. * @param int $length
  136. *
  137. * @return int[]
  138. */
  139. public function readBytes($length)
  140. {
  141. if ($this->offset == $this->array_size) {
  142. return false;
  143. }
  144. $arrays = [];
  145. $end = $length + $this->offset;
  146. for ($i = $this->offset; $i < $end; ++$i) {
  147. if (!isset($this->array[$i])) {
  148. break;
  149. }
  150. $arrays[] = $this->array[$i];
  151. }
  152. $this->offset += ($i - $this->offset); // Limit function calls
  153. return array_merge(...$arrays);
  154. }
  155. /**
  156. * Write $chars to the end of the stream.
  157. *
  158. * @param string $chars
  159. */
  160. public function write($chars)
  161. {
  162. if (!isset($this->charReader)) {
  163. $this->charReader = $this->charReaderFactory->getReaderFor(
  164. $this->charset);
  165. }
  166. $startLength = $this->charReader->getInitialByteSize();
  167. $fp = fopen('php://memory', 'w+b');
  168. fwrite($fp, $chars);
  169. unset($chars);
  170. fseek($fp, 0, SEEK_SET);
  171. $buffer = [0];
  172. $buf_pos = 1;
  173. $buf_len = 1;
  174. $has_datas = true;
  175. do {
  176. $bytes = [];
  177. // Buffer Filing
  178. if ($buf_len - $buf_pos < $startLength) {
  179. $buf = array_splice($buffer, $buf_pos);
  180. $new = $this->reloadBuffer($fp, 100);
  181. if ($new) {
  182. $buffer = array_merge($buf, $new);
  183. $buf_len = count($buffer);
  184. $buf_pos = 0;
  185. } else {
  186. $has_datas = false;
  187. }
  188. }
  189. if ($buf_len - $buf_pos > 0) {
  190. $size = 0;
  191. for ($i = 0; $i < $startLength && isset($buffer[$buf_pos]); ++$i) {
  192. ++$size;
  193. $bytes[] = $buffer[$buf_pos++];
  194. }
  195. $need = $this->charReader->validateByteSequence(
  196. $bytes, $size);
  197. if ($need > 0) {
  198. if ($buf_len - $buf_pos < $need) {
  199. $new = $this->reloadBuffer($fp, $need);
  200. if ($new) {
  201. $buffer = array_merge($buffer, $new);
  202. $buf_len = count($buffer);
  203. }
  204. }
  205. for ($i = 0; $i < $need && isset($buffer[$buf_pos]); ++$i) {
  206. $bytes[] = $buffer[$buf_pos++];
  207. }
  208. }
  209. $this->array[] = $bytes;
  210. ++$this->array_size;
  211. }
  212. } while ($has_datas);
  213. fclose($fp);
  214. }
  215. /**
  216. * Move the internal pointer to $charOffset in the stream.
  217. *
  218. * @param int $charOffset
  219. */
  220. public function setPointer($charOffset)
  221. {
  222. if ($charOffset > $this->array_size) {
  223. $charOffset = $this->array_size;
  224. } elseif ($charOffset < 0) {
  225. $charOffset = 0;
  226. }
  227. $this->offset = $charOffset;
  228. }
  229. /**
  230. * Empty the stream and reset the internal pointer.
  231. */
  232. public function flushContents()
  233. {
  234. $this->offset = 0;
  235. $this->array = [];
  236. $this->array_size = 0;
  237. }
  238. private function reloadBuffer($fp, $len)
  239. {
  240. if (!feof($fp) && false !== ($bytes = fread($fp, $len))) {
  241. $buf = [];
  242. for ($i = 0, $len = strlen($bytes); $i < $len; ++$i) {
  243. $buf[] = self::$byteMap[$bytes[$i]];
  244. }
  245. return $buf;
  246. }
  247. return false;
  248. }
  249. private static function initializeMaps()
  250. {
  251. if (!isset(self::$charMap)) {
  252. self::$charMap = [];
  253. for ($byte = 0; $byte < 256; ++$byte) {
  254. self::$charMap[$byte] = chr($byte);
  255. }
  256. self::$byteMap = array_flip(self::$charMap);
  257. }
  258. }
  259. }