diff --git a/src/Reader.php b/src/Reader.php index 7558395..998c8f2 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -34,7 +34,7 @@ */ /**#@+ @ignore */ -require_once("ReaderException.php"); +require_once("Reader/Exception.php"); require_once("Transform.php"); /**#@-*/ @@ -47,6 +47,7 @@ require_once("Transform.php"); * @author Sven Vollbehr * @copyright 2006, 2007 The Bearpaw Project Work Group * @copyright 2007, 2008 BEHR Software Systems + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License * @version $Rev$ */ class Reader @@ -65,12 +66,12 @@ class Reader * Opens the file given as a parameter. * * @param string $filename The absolute or relative path to the file. - * @throws ReaderException if the file cannot be read. + * @throws Reader_Exception if the file cannot be read. */ public function __construct($filename) { if (($this->_fd = fopen($filename, "rb")) === false) - throw new ReaderException("Unable to open file:" . $filename); + throw new Reader_Exception("Unable to open file:" . $filename); fseek($this->_fd, 0, SEEK_END); $this->_size = ftell($this->_fd); @@ -100,13 +101,14 @@ class Reader * Jumps size amount of bytes in the file stream. * * @return void - * @throws ReaderException if size attribute is not greater or - * equal than zero. + * @throws Reader_Exception if size attribute is negative. */ public function skip($size) { if ($size < 0) - throw new ReaderException("Invalid argument"); + throw new Reader_Exception("Invalid argument"); + if ($size == 0) + return; fseek($this->_fd, $size, SEEK_CUR); } @@ -114,13 +116,14 @@ class Reader * Reads length amount of bytes from the file stream. * * @return string Returns read bytes as a string - * @throws ReaderException if length attribute is not greater than - * zero. + * @throws Reader_Exception if length attribute is negative. */ public function read($length) { - if ($length <= 0) - throw new ReaderException("Invalid argument"); + if ($length < 0) + throw new Reader_Exception("Invalid argument"); + if ($length == 0) + return ""; return fread($this->_fd, $length); } @@ -165,7 +168,7 @@ class Reader * @param string $method The method to be called. * @param string $params The parameters should the function accept them. * @return mixed - * @throws ReaderException if no such transformer is implemented + * @throws Reader_Exception if no such transformer is implemented */ public function __call($method, $params) { $chunks = array(); @@ -177,6 +180,6 @@ class Reader $this->read(preg_match("/String|(?:H|L)Hex/", $chunks[1]) ? (isset($params[0]) ? $params[0] : 1) : ($chunks[1] == "GUID" ? 16 : $chunks[2] / 8))); - } else throw new ReaderException("Unknown method: " . $method); + } else throw new Reader_Exception("Unknown method: " . $method); } } diff --git a/src/ReaderException.php b/src/Reader/Exception.php similarity index 93% rename from src/ReaderException.php rename to src/Reader/Exception.php index e14d95f..7f62714 100644 --- a/src/ReaderException.php +++ b/src/Reader/Exception.php @@ -39,8 +39,9 @@ * @package php-reader * @author Sven Vollbehr * @copyright 2008 BEHR Software Systems + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License * @version $Rev$ */ -class ReaderException extends Exception +class Reader_Exception extends Exception { } diff --git a/src/Transform.php b/src/Transform.php index 22efbac..46f8f03 100644 --- a/src/Transform.php +++ b/src/Transform.php @@ -40,6 +40,7 @@ * @author Sven Vollbehr * @copyright 2006, 2007 The Bearpaw Project Work Group * @copyright 2007, 2008 BEHR Software Systems + * @license http://www.opensource.org/licenses/bsd-license.php New BSD License * @version $Rev$ * @static */ @@ -79,7 +80,7 @@ final class Transform */ public static function getInt64LE($raw) { - return ReaderUtils::getInt64($raw, self::LITTLE_ENDIAN_ORDER); + return self::getInt64($raw, self::LITTLE_ENDIAN_ORDER); } /** @@ -92,7 +93,7 @@ final class Transform */ public static function getInt64BE($raw) { - return ReaderUtils::getInt64($raw, self::BIG_ENDIAN_ORDER); + return self::getInt64($raw, self::BIG_ENDIAN_ORDER); } /** @@ -125,7 +126,7 @@ final class Transform */ public static function getUInt32LE($raw) { - return ReaderUtils::getUInt32($raw, self::LITTLE_ENDIAN_ORDER); + return self::getUInt32($raw, self::LITTLE_ENDIAN_ORDER); } /** @@ -135,7 +136,7 @@ final class Transform */ public static function getUInt32BE($raw) { - return ReaderUtils::getUInt32($raw, self::BIG_ENDIAN_ORDER); + return self::getUInt32($raw, self::BIG_ENDIAN_ORDER); } /** @@ -168,7 +169,7 @@ final class Transform */ public static function getUInt16LE($raw) { - return ReaderUtils::getUInt16($raw, self::LITTLE_ENDIAN_ORDER); + return self::getUInt16($raw, self::LITTLE_ENDIAN_ORDER); } /** @@ -178,7 +179,7 @@ final class Transform */ public static function getUInt16BE($raw) { - return ReaderUtils::getUInt16($raw, self::BIG_ENDIAN_ORDER); + return self::getUInt16($raw, self::BIG_ENDIAN_ORDER); } /** @@ -215,7 +216,7 @@ final class Transform foreach (unpack(($order == 2 ? "n" : ($order == 1 ? "v" : "S")) . "*", $raw) as $char) $string .= pack("S", $char); - return rtrim($string, "\0"); + return $string; } /** @@ -225,7 +226,7 @@ final class Transform */ public static function getString16LE($raw) { - return ReaderUtils::getString16($raw, self::LITTLE_ENDIAN_ORDER); + return self::getString16($raw, self::LITTLE_ENDIAN_ORDER); } /** @@ -235,7 +236,7 @@ final class Transform */ public static function getString16BE($raw) { - return ReaderUtils::getString16($raw, self::BIG_ENDIAN_ORDER); + return self::getString16($raw, self::BIG_ENDIAN_ORDER); } /**