StreamReader.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. <?php
  2. /**
  3. * This file is part of FPDI
  4. *
  5. * @package Fpdi
  6. * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
  7. * @license http://opensource.org/licenses/mit-license The MIT License
  8. */
  9. namespace Fpdi\PdfParser;
  10. /**
  11. * A stream reader class
  12. */
  13. class StreamReader
  14. {
  15. /**
  16. * Creates a stream reader instance by a string value.
  17. *
  18. * @param string $content
  19. * @param int $maxMemory
  20. * @return StreamReader
  21. */
  22. public static function createByString($content, $maxMemory = 2097152)
  23. {
  24. $h = \fopen('php://temp/maxmemory:' . ((int) $maxMemory), 'r+b');
  25. \fwrite($h, $content);
  26. \rewind($h);
  27. return new self($h, true);
  28. }
  29. /**
  30. * Creates a stream reader instance by a filename.
  31. *
  32. * @param string $filename
  33. * @return StreamReader
  34. */
  35. public static function createByFile($filename)
  36. {
  37. $h = \fopen($filename, 'rb');
  38. return new self($h, true);
  39. }
  40. /**
  41. * Defines whether the stream should be closed when the stream reader instance is deconstructed or not.
  42. *
  43. * @var bool
  44. */
  45. protected $closeStream;
  46. /**
  47. * The stream resource.
  48. *
  49. * @var resource
  50. */
  51. protected $stream;
  52. /**
  53. * The byte-offset position in the stream.
  54. *
  55. * @var int
  56. */
  57. protected $position;
  58. /**
  59. * The byte-offset position in the buffer.
  60. *
  61. * @var int
  62. */
  63. protected $offset;
  64. /**
  65. * The buffer length.
  66. *
  67. * @var int
  68. */
  69. protected $bufferLength;
  70. /**
  71. * The total length of the stream.
  72. *
  73. * @var int
  74. */
  75. protected $totalLength;
  76. /**
  77. * The buffer.
  78. *
  79. * @var string
  80. */
  81. protected $buffer;
  82. /**
  83. * StreamReader constructor.
  84. *
  85. * @param resource $stream
  86. * @param bool $closeStream Defines whether to close the stream resource if the instance is destructed or not.
  87. */
  88. public function __construct($stream, $closeStream = false)
  89. {
  90. if (!\is_resource($stream)) {
  91. throw new \InvalidArgumentException(
  92. 'No stream given.'
  93. );
  94. }
  95. $metaData = \stream_get_meta_data($stream);
  96. if (!$metaData['seekable']) {
  97. throw new \InvalidArgumentException(
  98. 'Given stream is not seekable!'
  99. );
  100. }
  101. $this->stream = $stream;
  102. $this->closeStream = $closeStream;
  103. $this->reset();
  104. }
  105. /**
  106. * The destructor.
  107. */
  108. public function __destruct()
  109. {
  110. $this->cleanUp();
  111. }
  112. /**
  113. * Closes the file handle.
  114. */
  115. public function cleanUp()
  116. {
  117. if ($this->closeStream && is_resource($this->stream)) {
  118. \fclose($this->stream);
  119. }
  120. }
  121. /**
  122. * Returns the byte length of the buffer.
  123. *
  124. * @param bool $atOffset
  125. * @return int
  126. */
  127. public function getBufferLength($atOffset = false)
  128. {
  129. if ($atOffset === false) {
  130. return $this->bufferLength;
  131. }
  132. return $this->bufferLength - $this->offset;
  133. }
  134. /**
  135. * Get the current position in the stream.
  136. *
  137. * @return int
  138. */
  139. public function getPosition()
  140. {
  141. return $this->position;
  142. }
  143. /**
  144. * Returns the current buffer.
  145. *
  146. * @param bool $atOffset
  147. * @return string
  148. */
  149. public function getBuffer($atOffset = true)
  150. {
  151. if ($atOffset === false) {
  152. return $this->buffer;
  153. }
  154. $string = \substr($this->buffer, $this->offset);
  155. return (string) $string;
  156. }
  157. /**
  158. * Gets a byte at a specific position in the buffer.
  159. *
  160. * If the position is invalid the method will return false.
  161. *
  162. * If the $position parameter is set to null the value of $this->offset will be used.
  163. *
  164. * @param int|null $position
  165. * @return string|bool
  166. */
  167. public function getByte($position = null)
  168. {
  169. $position = (int) ($position !== null ? $position : $this->offset);
  170. if (
  171. $position >= $this->bufferLength
  172. && (!$this->increaseLength() || $position >= $this->bufferLength)
  173. ) {
  174. return false;
  175. }
  176. return $this->buffer[$position];
  177. }
  178. /**
  179. * Returns a byte at a specific position, and set the offset to the next byte position.
  180. *
  181. * If the position is invalid the method will return false.
  182. *
  183. * If the $position parameter is set to null the value of $this->offset will be used.
  184. *
  185. * @param int|null $position
  186. * @return string|bool
  187. */
  188. public function readByte($position = null)
  189. {
  190. if ($position !== null) {
  191. $position = (int) $position;
  192. // check if needed bytes are available in the current buffer
  193. if (!($position >= $this->position && $position < $this->position + $this->bufferLength)) {
  194. $this->reset($position);
  195. $offset = $this->offset;
  196. } else {
  197. $offset = $position - $this->position;
  198. }
  199. } else {
  200. $offset = $this->offset;
  201. }
  202. if (
  203. $offset >= $this->bufferLength
  204. && ((!$this->increaseLength()) || $offset >= $this->bufferLength)
  205. ) {
  206. return false;
  207. }
  208. $this->offset = $offset + 1;
  209. return $this->buffer[$offset];
  210. }
  211. /**
  212. * Read bytes from the current or a specific offset position and set the internal pointer to the next byte.
  213. *
  214. * If the position is invalid the method will return false.
  215. *
  216. * If the $position parameter is set to null the value of $this->offset will be used.
  217. *
  218. * @param int $length
  219. * @param int|null $position
  220. * @return string|false
  221. */
  222. public function readBytes($length, $position = null)
  223. {
  224. $length = (int) $length;
  225. if ($position !== null) {
  226. // check if needed bytes are available in the current buffer
  227. if (!($position >= $this->position && $position < $this->position + $this->bufferLength)) {
  228. $this->reset($position, $length);
  229. $offset = $this->offset;
  230. } else {
  231. $offset = $position - $this->position;
  232. }
  233. } else {
  234. $offset = $this->offset;
  235. }
  236. if (
  237. ($offset + $length) > $this->bufferLength
  238. && ((!$this->increaseLength($length)) || ($offset + $length) > $this->bufferLength)
  239. ) {
  240. return false;
  241. }
  242. $bytes = \substr($this->buffer, $offset, $length);
  243. $this->offset = $offset + $length;
  244. return $bytes;
  245. }
  246. /**
  247. * Read a line from the current position.
  248. *
  249. * @param int $length
  250. * @return string|bool
  251. */
  252. public function readLine($length = 1024)
  253. {
  254. if ($this->ensureContent() === false) {
  255. return false;
  256. }
  257. $line = '';
  258. while ($this->ensureContent()) {
  259. $char = $this->readByte();
  260. if ($char === "\n") {
  261. break;
  262. }
  263. if ($char === "\r") {
  264. if ($this->getByte() === "\n") {
  265. $this->addOffset(1);
  266. }
  267. break;
  268. }
  269. $line .= $char;
  270. if (\strlen($line) >= $length) {
  271. break;
  272. }
  273. }
  274. return $line;
  275. }
  276. /**
  277. * Set the offset position in the current buffer.
  278. *
  279. * @param int $offset
  280. */
  281. public function setOffset($offset)
  282. {
  283. if ($offset > $this->bufferLength || $offset < 0) {
  284. throw new \OutOfRangeException(
  285. \sprintf('Offset (%s) out of range (length: %s)', $offset, $this->bufferLength)
  286. );
  287. }
  288. $this->offset = (int) $offset;
  289. }
  290. /**
  291. * Returns the current offset in the current buffer.
  292. *
  293. * @return int
  294. */
  295. public function getOffset()
  296. {
  297. return $this->offset;
  298. }
  299. /**
  300. * Add an offset to the current offset.
  301. *
  302. * @param int $offset
  303. */
  304. public function addOffset($offset)
  305. {
  306. $this->setOffset($this->offset + $offset);
  307. }
  308. /**
  309. * Make sure that there is at least one character beyond the current offset in the buffer.
  310. *
  311. * @return bool
  312. */
  313. public function ensureContent()
  314. {
  315. while ($this->offset >= $this->bufferLength) {
  316. if (!$this->increaseLength()) {
  317. return false;
  318. }
  319. }
  320. return true;
  321. }
  322. /**
  323. * Returns the stream.
  324. *
  325. * @return resource
  326. */
  327. public function getStream()
  328. {
  329. return $this->stream;
  330. }
  331. /**
  332. * Gets the total available length.
  333. *
  334. * @return int
  335. */
  336. public function getTotalLength()
  337. {
  338. if ($this->totalLength === null) {
  339. $stat = \fstat($this->stream);
  340. $this->totalLength = $stat['size'];
  341. }
  342. return $this->totalLength;
  343. }
  344. /**
  345. * Resets the buffer to a position and re-read the buffer with the given length.
  346. *
  347. * If the $pos parameter is negative the start buffer position will be the $pos'th position from
  348. * the end of the file.
  349. *
  350. * If the $pos parameter is negative and the absolute value is bigger then the totalLength of
  351. * the file $pos will set to zero.
  352. *
  353. * @param int|null $pos Start position of the new buffer
  354. * @param int $length Length of the new buffer. Mustn't be negative
  355. */
  356. public function reset($pos = 0, $length = 200)
  357. {
  358. if ($pos === null) {
  359. $pos = $this->position + $this->offset;
  360. } elseif ($pos < 0) {
  361. $pos = \max(0, $this->getTotalLength() + $pos);
  362. }
  363. \fseek($this->stream, $pos);
  364. $this->position = $pos;
  365. $this->buffer = $length > 0 ? \fread($this->stream, $length) : '';
  366. $this->bufferLength = \strlen($this->buffer);
  367. $this->offset = 0;
  368. // If a stream wrapper is in use it is possible that
  369. // length values > 8096 will be ignored, so use the
  370. // increaseLength()-method to correct that behavior
  371. if ($this->bufferLength < $length && $this->increaseLength($length - $this->bufferLength)) {
  372. // increaseLength parameter is $minLength, so cut to have only the required bytes in the buffer
  373. $this->buffer = \substr($this->buffer, 0, $length);
  374. $this->bufferLength = \strlen($this->buffer);
  375. }
  376. }
  377. /**
  378. * Ensures bytes in the buffer with a specific length and location in the file.
  379. *
  380. * @param int $pos
  381. * @param int $length
  382. * @see reset()
  383. */
  384. public function ensure($pos, $length)
  385. {
  386. if (
  387. $pos >= $this->position
  388. && $pos < ($this->position + $this->bufferLength)
  389. && ($this->position + $this->bufferLength) >= ($pos + $length)
  390. ) {
  391. $this->offset = $pos - $this->position;
  392. } else {
  393. $this->reset($pos, $length);
  394. }
  395. }
  396. /**
  397. * Forcefully read more data into the buffer.
  398. *
  399. * @param int $minLength
  400. * @return bool Returns false if the stream reaches the end
  401. */
  402. public function increaseLength($minLength = 100)
  403. {
  404. $length = \max($minLength, 100);
  405. if (\feof($this->stream) || $this->getTotalLength() === $this->position + $this->bufferLength) {
  406. return false;
  407. }
  408. $newLength = $this->bufferLength + $length;
  409. do {
  410. $this->buffer .= \fread($this->stream, $newLength - $this->bufferLength);
  411. $this->bufferLength = \strlen($this->buffer);
  412. } while (($this->bufferLength !== $newLength) && !\feof($this->stream));
  413. return true;
  414. }
  415. }