Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileParser.php
Go to the documentation of this file.
1 <?php
39 abstract class Zend_Pdf_FileParser
40 {
41  /**** Class Constants ****/
42 
47 
52 
53 
54 
55  /**** Instance Variables ****/
56 
57 
62  protected $_isScreened = false;
63 
68  protected $_isParsed = false;
69 
74  protected $_dataSource = null;
75 
76 
77 
78  /**** Public Interface ****/
79 
80 
81  /* Abstract Methods */
82 
91  abstract public function screen();
92 
100  abstract public function parse();
101 
102 
103  /* Object Lifecycle */
104 
113  public function __construct(Zend_Pdf_FileParserDataSource $dataSource)
114  {
115  if ($dataSource->getSize() == 0) {
116  #require_once 'Zend/Pdf/Exception.php';
117  throw new Zend_Pdf_Exception('The data source has not been properly initialized',
119  }
120  $this->_dataSource = $dataSource;
121  }
122 
128  public function __destruct()
129  {
130  $this->_dataSource = null;
131  }
132 
133 
134  /* Accessors */
135 
141  public function isScreened()
142  {
143  return $this->_isScreened;
144  }
145 
151  public function isParsed()
152  {
153  return $this->_isParsed;
154  }
155 
161  public function getDataSource()
162  {
163  return $this->_dataSource;
164  }
165 
166 
167  /* Primitive Methods */
168 
175  public function moveToOffset($offset)
176  {
177  $this->_dataSource->moveToOffset($offset);
178  }
179 
180  public function getOffset() {
181  return $this->_dataSource->getOffset();
182  }
183 
184  public function getSize() {
185  return $this->_dataSource->getSize();
186  }
187 
195  public function readBytes($byteCount)
196  {
197  return $this->_dataSource->readBytes($byteCount);
198  }
199 
206  public function skipBytes($byteCount)
207  {
208  $this->_dataSource->skipBytes($byteCount);
209  }
210 
211 
212  /* Parser Methods */
213 
228  public function readInt($size, $byteOrder = Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN)
229  {
230  if (($size < 1) || ($size > 4)) {
231  #require_once 'Zend/Pdf/Exception.php';
232  throw new Zend_Pdf_Exception("Invalid signed integer size: $size",
234  }
235  $bytes = $this->_dataSource->readBytes($size);
236  /* unpack() will not work for this method because it always works in
237  * the host byte order for signed integers. It also does not allow for
238  * variable integer sizes.
239  */
240  if ($byteOrder == Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN) {
241  $number = ord($bytes[0]);
242  if (($number & 0x80) == 0x80) {
243  /* This number is negative. Extract the positive equivalent.
244  */
245  $number = (~ $number) & 0xff;
246  for ($i = 1; $i < $size; $i++) {
247  $number = ($number << 8) | ((~ ord($bytes[$i])) & 0xff);
248  }
249  /* Now turn this back into a negative number by taking the
250  * two's complement (we didn't add one above so won't
251  * subtract it below). This works reliably on both 32- and
252  * 64-bit systems.
253  */
254  $number = ~$number;
255  } else {
256  for ($i = 1; $i < $size; $i++) {
257  $number = ($number << 8) | ord($bytes[$i]);
258  }
259  }
260  } else if ($byteOrder == Zend_Pdf_FileParser::BYTE_ORDER_LITTLE_ENDIAN) {
261  $number = ord($bytes[$size - 1]);
262  if (($number & 0x80) == 0x80) {
263  /* Negative number. See discussion above.
264  */
265  $number = 0;
266  for ($i = --$size; $i >= 0; $i--) {
267  $number |= ((~ ord($bytes[$i])) & 0xff) << ($i * 8);
268  }
269  $number = ~$number;
270  } else {
271  $number = 0;
272  for ($i = --$size; $i >= 0; $i--) {
273  $number |= ord($bytes[$i]) << ($i * 8);
274  }
275  }
276  } else {
277  #require_once 'Zend/Pdf/Exception.php';
278  throw new Zend_Pdf_Exception("Invalid byte order: $byteOrder",
280  }
281  return $number;
282  }
283 
303  public function readUInt($size, $byteOrder = Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN)
304  {
305  if (($size < 1) || ($size > 4)) {
306  #require_once 'Zend/Pdf/Exception.php';
307  throw new Zend_Pdf_Exception("Invalid unsigned integer size: $size",
309  }
310  $bytes = $this->_dataSource->readBytes($size);
311  /* unpack() is a bit heavyweight for this simple conversion. Just
312  * work the bytes directly.
313  */
314  if ($byteOrder == Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN) {
315  $number = ord($bytes[0]);
316  for ($i = 1; $i < $size; $i++) {
317  $number = ($number << 8) | ord($bytes[$i]);
318  }
319  } else if ($byteOrder == Zend_Pdf_FileParser::BYTE_ORDER_LITTLE_ENDIAN) {
320  $number = 0;
321  for ($i = --$size; $i >= 0; $i--) {
322  $number |= ord($bytes[$i]) << ($i * 8);
323  }
324  } else {
325  #require_once 'Zend/Pdf/Exception.php';
326  throw new Zend_Pdf_Exception("Invalid byte order: $byteOrder",
328  }
329  return $number;
330  }
331 
339  public function isBitSet($bit, $bitField)
340  {
341  $bitMask = 1 << $bit;
342  $isSet = (($bitField & $bitMask) == $bitMask);
343  return $isSet;
344  }
345 
363  public function readFixed($mantissaBits, $fractionBits,
365  {
366  $bitsToRead = $mantissaBits + $fractionBits;
367  if (($bitsToRead % 8) !== 0) {
368  #require_once 'Zend/Pdf/Exception.php';
369  throw new Zend_Pdf_Exception('Fixed-point numbers are whole bytes',
371  }
372  $number = $this->readInt(($bitsToRead >> 3), $byteOrder) / (1 << $fractionBits);
373  return $number;
374  }
375 
401  public function readStringUTF16($byteCount,
403  $characterSet = '')
404  {
405  if ($byteCount == 0) {
406  return '';
407  }
408  $bytes = $this->_dataSource->readBytes($byteCount);
409  if ($byteOrder == Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN) {
410  if ($characterSet == 'UTF-16BE') {
411  return $bytes;
412  }
413  return iconv('UTF-16BE', $characterSet, $bytes);
414  } else if ($byteOrder == Zend_Pdf_FileParser::BYTE_ORDER_LITTLE_ENDIAN) {
415  if ($characterSet == 'UTF-16LE') {
416  return $bytes;
417  }
418  return iconv('UTF-16LE', $characterSet, $bytes);
419  } else {
420  #require_once 'Zend/Pdf/Exception.php';
421  throw new Zend_Pdf_Exception("Invalid byte order: $byteOrder",
423  }
424  }
425 
442  public function readStringMacRoman($byteCount, $characterSet = '')
443  {
444  if ($byteCount == 0) {
445  return '';
446  }
447  $bytes = $this->_dataSource->readBytes($byteCount);
448  if ($characterSet == 'MacRoman') {
449  return $bytes;
450  }
451  return iconv('MacRoman', $characterSet, $bytes);
452  }
453 
472  public function readStringPascal($characterSet = '', $lengthBytes = 1)
473  {
474  $byteCount = $this->readUInt($lengthBytes);
475  if ($byteCount == 0) {
476  return '';
477  }
478  $bytes = $this->_dataSource->readBytes($byteCount);
479  if ($characterSet == 'ASCII') {
480  return $bytes;
481  }
482  return iconv('ASCII', $characterSet, $bytes);
483  }
484 
485 }
readInt($size, $byteOrder=Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN)
Definition: FileParser.php:228
isBitSet($bit, $bitField)
Definition: FileParser.php:339
readStringMacRoman($byteCount, $characterSet='')
Definition: FileParser.php:442
$number
Definition: details.phtml:22
const INVALID_INTEGER_SIZE
Definition: Exception.php:196
readStringPascal($characterSet='', $lengthBytes=1)
Definition: FileParser.php:472
readBytes($byteCount)
Definition: FileParser.php:195
const BAD_FIXED_POINT_SIZE
Definition: Exception.php:201
const BYTE_ORDER_BIG_ENDIAN
Definition: FileParser.php:51
readFixed($mantissaBits, $fractionBits, $byteOrder=Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN)
Definition: FileParser.php:363
const INVALID_BYTE_ORDER
Definition: Exception.php:191
__construct(Zend_Pdf_FileParserDataSource $dataSource)
Definition: FileParser.php:113
const BYTE_ORDER_LITTLE_ENDIAN
Definition: FileParser.php:46
skipBytes($byteCount)
Definition: FileParser.php:206
readStringUTF16($byteCount, $byteOrder=Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN, $characterSet='')
Definition: FileParser.php:401
$i
Definition: gallery.phtml:31
readUInt($size, $byteOrder=Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN)
Definition: FileParser.php:303