📄 fileparser.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @package Zend_Pdf * @subpackage FileParser * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License *//** Zend_Pdf_Exception */require_once 'Zend/Pdf/Exception.php';/** * Abstract utility class for parsing binary files. * * Provides a library of methods to quickly navigate and extract various data * types (signed and unsigned integers, floating- and fixed-point numbers, * strings, etc.) from the file. * * File access is managed via a {@link Zend_Pdf_FileParserDataSource} object. * This allows the same parser code to work with many different data sources: * in-memory objects, filesystem files, etc. * * @package Zend_Pdf * @subpackage FileParser * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */abstract class Zend_Pdf_FileParser{ /**** Class Constants ****/ /** * Little-endian byte order (0x04 0x03 0x02 0x01). */ const BYTE_ORDER_LITTLE_ENDIAN = 0; /** * Big-endian byte order (0x01 0x02 0x03 0x04). */ const BYTE_ORDER_BIG_ENDIAN = 1; /**** Instance Variables ****/ /** * Flag indicating that the file has passed a cursory validation check. * @var boolean */ protected $_isScreened = false; /** * Flag indicating that the file has been sucessfully parsed. * @var boolean */ protected $_isParsed = false; /** * Object representing the data source to be parsed. * @var Zend_Pdf_FileParserDataSource */ protected $_dataSource = null; /**** Public Interface ****/ /* Abstract Methods */ /** * Performs a cursory check to verify that the binary file is in the expected * format. Intended to quickly weed out obviously bogus files. * * Must set $this->_isScreened to true if successful. * * @throws Zend_Pdf_Exception */ abstract public function screen(); /** * Reads and parses the complete binary file. * * Must set $this->_isParsed to true if successful. * * @throws Zend_Pdf_Exception */ abstract public function parse(); /* Object Lifecycle */ /** * Object constructor. * * Verifies that the data source has been properly initialized. * * @param Zend_Pdf_FileParserDataSource $dataSource * @throws Zend_Pdf_Exception */ public function __construct(Zend_Pdf_FileParserDataSource $dataSource) { if ($dataSource->getSize() == 0) { throw new Zend_Pdf_Exception('The data source has not been properly initialized', Zend_Pdf_Exception::BAD_DATA_SOURCE); } $this->_dataSource = $dataSource; } /** * Object destructor. * * Discards the data source object. */ public function __destruct() { $this->_dataSource = null; } /* Accessors */ /** * Returns true if the file has passed a cursory validation check. * * @return boolean */ public function isScreened() { return $this->_isScreened; } /** * Returns true if the file has been successfully parsed. * * @return boolean */ public function isParsed() { return $this->_isParsed; } /** * Returns the data source object representing the file being parsed. * * @return Zend_Pdf_FileParserDataSource */ public function getDataSource() { return $this->_dataSource; } /* Primitive Methods */ /** * Convenience wrapper for the data source object's moveToOffset() method. * * @param integer $offset Destination byte offset. * @throws Zend_Pdf_Exception */ public function moveToOffset($offset) { $this->_dataSource->moveToOffset($offset); } public function getOffset() { return $this->_dataSource->getOffset(); } public function getSize() { return $this->_dataSource->getSize(); } /** * Convenience wrapper for the data source object's readBytes() method. * * @param integer $byteCount Number of bytes to read. * @return string * @throws Zend_Pdf_Exception */ public function readBytes($byteCount) { return $this->_dataSource->readBytes($byteCount); } /** * Convenience wrapper for the data source object's skipBytes() method. * * @param integer $byteCount Number of bytes to skip. * @throws Zend_Pdf_Exception */ public function skipBytes($byteCount) { $this->_dataSource->skipBytes($byteCount); } /* Parser Methods */ /** * Reads the signed integer value from the binary file at the current byte * offset. * * Advances the offset by the number of bytes read. Throws an exception if * an error occurs. * * @param integer $size Size of integer in bytes: 1-4 * @param integer $byteOrder (optional) Big- or little-endian byte order. * Use the BYTE_ORDER_ constants defined in {@link Zend_Pdf_FileParser}. * If omitted, uses big-endian. * @return integer * @throws Zend_Pdf_Exception */ public function readInt($size, $byteOrder = Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN) { if (($size < 1) || ($size > 4)) { throw new Zend_Pdf_Exception("Invalid signed integer size: $size", Zend_Pdf_Exception::INVALID_INTEGER_SIZE); } $bytes = $this->_dataSource->readBytes($size); /* unpack() will not work for this method because it always works in * the host byte order for signed integers. It also does not allow for * variable integer sizes. */ if ($byteOrder == Zend_Pdf_FileParser::BYTE_ORDER_BIG_ENDIAN) { $number = ord($bytes[0]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -