NgCharacterStream.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /*
  3. CharacterStream implementation using an array in Swift Mailer.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /**
  16. * A CharacterStream implementation which stores characters in an internal array.
  17. * @package Swift
  18. * @subpackage CharacterStream
  19. * @author Xavier De Cock <xdecock@gmail.com>
  20. */
  21. Class Swift_CharacterStream_NgCharacterStream
  22. implements Swift_CharacterStream
  23. {
  24. /**
  25. * The char reader (lazy-loaded) for the current charset.
  26. * @var Swift_CharacterReader
  27. * @access private
  28. */
  29. private $_charReader;
  30. /**
  31. * A factory for creatiing CharacterReader instances.
  32. * @var Swift_CharacterReaderFactory
  33. * @access private
  34. */
  35. private $_charReaderFactory;
  36. /**
  37. * The character set this stream is using.
  38. * @var string
  39. * @access private
  40. */
  41. private $_charset;
  42. /**
  43. * The datas stored as is
  44. *
  45. * @var string
  46. */
  47. private $_datas = "";
  48. /**
  49. * Number of bytes in the stream
  50. *
  51. * @var int
  52. */
  53. private $_datasSize = 0;
  54. /**
  55. * Map
  56. *
  57. * @var mixed
  58. */
  59. private $_map;
  60. /**
  61. * Map Type
  62. *
  63. * @var int
  64. */
  65. private $_mapType = 0;
  66. /**
  67. * Number of characters in the stream
  68. *
  69. * @var int
  70. */
  71. private $_charCount = 0;
  72. /**
  73. * Position in the stream
  74. *
  75. * @var unknown_type
  76. */
  77. private $_currentPos = 0;
  78. /**
  79. * The constructor
  80. *
  81. * @param Swift_CharacterReaderFactory $factory
  82. * @param unknown_type $charset
  83. */
  84. public function __construct(Swift_CharacterReaderFactory $factory,
  85. $charset)
  86. {
  87. $this->setCharacterReaderFactory($factory);
  88. $this->setCharacterSet($charset);
  89. }
  90. /* -- Changing parameters of the stream -- */
  91. /**
  92. * Set the character set used in this CharacterStream.
  93. * @param string $charset
  94. */
  95. public function setCharacterSet($charset)
  96. {
  97. $this->_charset = $charset;
  98. $this->_charReader = null;
  99. $this->_mapType = 0;
  100. }
  101. /**
  102. * Set the CharacterReaderFactory for multi charset support.
  103. * @param Swift_CharacterReaderFactory $factory
  104. */
  105. public function setCharacterReaderFactory(
  106. Swift_CharacterReaderFactory $factory)
  107. {
  108. $this->_charReaderFactory = $factory;
  109. }
  110. /**
  111. * @see Swift_CharacterStream::flushContents()
  112. *
  113. */
  114. public function flushContents()
  115. {
  116. $this->_datas = null;
  117. $this->_map = null;
  118. $this->_charCount = 0;
  119. $this->_currentPos = 0;
  120. $this->_datasSize = 0;
  121. }
  122. /**
  123. * @see Swift_CharacterStream::importByteStream()
  124. *
  125. * @param Swift_OutputByteStream $os
  126. */
  127. public function importByteStream(Swift_OutputByteStream $os)
  128. {
  129. $this->flushContents();
  130. $blocks=512;
  131. $os->setReadPointer(0);
  132. while(false!==($read = $os->read($blocks)))
  133. $this->write($read);
  134. }
  135. /**
  136. * @see Swift_CharacterStream::importString()
  137. *
  138. * @param string $string
  139. */
  140. public function importString($string)
  141. {
  142. $this->flushContents();
  143. $this->write($string);
  144. }
  145. /**
  146. * @see Swift_CharacterStream::read()
  147. *
  148. * @param int $length
  149. * @return string
  150. */
  151. public function read($length)
  152. {
  153. if ($this->_currentPos>=$this->_charCount)
  154. {
  155. return false;
  156. }
  157. $ret=false;
  158. $length = ($this->_currentPos+$length > $this->_charCount)
  159. ? $this->_charCount - $this->_currentPos
  160. : $length;
  161. switch ($this->_mapType)
  162. {
  163. case Swift_CharacterReader::MAP_TYPE_FIXED_LEN:
  164. $len = $length*$this->_map;
  165. $ret = substr($this->_datas,
  166. $this->_currentPos * $this->_map,
  167. $len);
  168. $this->_currentPos += $length;
  169. break;
  170. case Swift_CharacterReader::MAP_TYPE_INVALID:
  171. $end = $this->_currentPos + $length;
  172. $end = $end > $this->_charCount
  173. ?$this->_charCount
  174. :$end;
  175. $ret = '';
  176. for (; $this->_currentPos < $length; ++$this->_currentPos)
  177. {
  178. if (isset ($this->_map[$this->_currentPos]))
  179. {
  180. $ret .= '?';
  181. }
  182. else
  183. {
  184. $ret .= $this->_datas[$this->_currentPos];
  185. }
  186. }
  187. break;
  188. case Swift_CharacterReader::MAP_TYPE_POSITIONS:
  189. $end = $this->_currentPos + $length;
  190. $end = $end > $this->_charCount
  191. ?$this->_charCount
  192. :$end;
  193. $ret = '';
  194. $start = 0;
  195. if ($this->_currentPos>0)
  196. {
  197. $start = $this->_map['p'][$this->_currentPos-1];
  198. }
  199. $to = $start;
  200. for (; $this->_currentPos < $end; ++$this->_currentPos)
  201. {
  202. if (isset($this->_map['i'][$this->_currentPos])) {
  203. $ret .= substr($this->_datas, $start, $to - $start).'?';
  204. $start = $this->_map['p'][$this->_currentPos];
  205. } else {
  206. $to = $this->_map['p'][$this->_currentPos];
  207. }
  208. }
  209. $ret .= substr($this->_datas, $start, $to - $start);
  210. break;
  211. }
  212. return $ret;
  213. }
  214. /**
  215. * @see Swift_CharacterStream::readBytes()
  216. *
  217. * @param int $length
  218. * @return int[]
  219. */
  220. public function readBytes($length)
  221. {
  222. $read=$this->read($length);
  223. if ($read!==false)
  224. {
  225. $ret = array_map('ord', str_split($read, 1));
  226. return $ret;
  227. }
  228. return false;
  229. }
  230. /**
  231. * @see Swift_CharacterStream::setPointer()
  232. *
  233. * @param int $charOffset
  234. */
  235. public function setPointer($charOffset)
  236. {
  237. if ($this->_charCount<$charOffset){
  238. $charOffset=$this->_charCount;
  239. }
  240. $this->_currentPos = $charOffset;
  241. }
  242. /**
  243. * @see Swift_CharacterStream::write()
  244. *
  245. * @param string $chars
  246. */
  247. public function write($chars)
  248. {
  249. if (!isset($this->_charReader))
  250. {
  251. $this->_charReader = $this->_charReaderFactory->getReaderFor(
  252. $this->_charset);
  253. $this->_map = array();
  254. $this->_mapType = $this->_charReader->getMapType();
  255. }
  256. $ignored='';
  257. $this->_datas .= $chars;
  258. $this->_charCount += $this->_charReader->getCharPositions(substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored);
  259. if ($ignored!==false) {
  260. $this->_datasSize=strlen($this->_datas)-strlen($ignored);
  261. }
  262. else
  263. {
  264. $this->_datasSize=strlen($this->_datas);
  265. }
  266. }
  267. }