Change file properties

git-svn-id: http://php-reader.googlecode.com/svn/trunk@3 51a70ab9-7547-0410-9469-37e369ee0574
This commit is contained in:
svollbehr
2008-03-02 19:28:25 +00:00
parent f5852a4ddc
commit 16dd117216
3 changed files with 510 additions and 510 deletions

View File

@@ -1,182 +1,182 @@
<?php <?php
/** /**
* $Id$ * $Id$
* *
* *
* Copyright (C) 2006, 2007 The Bearpaw Project Work Group. All Rights Reserved. * Copyright (C) 2006, 2007 The Bearpaw Project Work Group. All Rights Reserved.
* Copyright (C) 2007, 2008 BEHR Software Systems. All Rights Reserved. * Copyright (C) 2007, 2008 BEHR Software Systems. All Rights Reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* - Redistributions of source code must retain the above copyright notice, * - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, * - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* - Neither the name of the BEHR Software Systems nor the names of its * - Neither the name of the BEHR Software Systems nor the names of its
* contributors may be used to endorse or promote products derived from this * contributors may be used to endorse or promote products derived from this
* software without specific prior written permission. * software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
* @package php-reader * @package php-reader
*/ */
/**#@+ @ignore */ /**#@+ @ignore */
require_once("ReaderException.php"); require_once("ReaderException.php");
require_once("Transform.php"); require_once("Transform.php");
/**#@-*/ /**#@-*/
/** /**
* The Reader class encapsulates a file. It is hence responsible of upkeeping * The Reader class encapsulates a file. It is hence responsible of upkeeping
* the connection to the file, keeping track of the cursor position and reading * the connection to the file, keeping track of the cursor position and reading
* data from it. * data from it.
* *
* @package php-reader * @package php-reader
* @author Sven Vollbehr <sven.vollbehr@behrss.eu> * @author Sven Vollbehr <sven.vollbehr@behrss.eu>
* @copyright 2006, 2007 The Bearpaw Project Work Group * @copyright 2006, 2007 The Bearpaw Project Work Group
* @copyright 2007, 2008 BEHR Software Systems * @copyright 2007, 2008 BEHR Software Systems
* @version $Rev$ * @version $Rev$
*/ */
class Reader class Reader
{ {
/** /**
* @var resource The underlying file descriptor. * @var resource The underlying file descriptor.
*/ */
private $_fd; private $_fd;
/** /**
* @var integer The file size. * @var integer The file size.
*/ */
private $_size; private $_size;
/** /**
* Opens the file given as a parameter. * Opens the file given as a parameter.
* *
* @param string $filename The absolute or relative path to the file. * @param string $filename The absolute or relative path to the file.
* @throws ReaderException if the file cannot be read. * @throws ReaderException if the file cannot be read.
*/ */
public function __construct($filename) public function __construct($filename)
{ {
if (($this->_fd = fopen($filename, "rb")) === false) if (($this->_fd = fopen($filename, "rb")) === false)
throw new ReaderException("Unable to open file:" . $filename); throw new ReaderException("Unable to open file:" . $filename);
fseek($this->_fd, 0, SEEK_END); fseek($this->_fd, 0, SEEK_END);
$this->_size = ftell($this->_fd); $this->_size = ftell($this->_fd);
fseek($this->_fd, 0); fseek($this->_fd, 0);
} }
/** /**
* Closes the file. * Closes the file.
*/ */
public function __destruct() public function __destruct()
{ {
@fclose($this->_fd); @fclose($this->_fd);
} }
/** /**
* Checks whether there is more to be read in the file. * Checks whether there is more to be read in the file.
* *
* @return boolean Returns <var>true</var> if the end of the file has not yet * @return boolean Returns <var>true</var> if the end of the file has not yet
* been reached; <var>false</var> otherwise. * been reached; <var>false</var> otherwise.
*/ */
public function available() public function available()
{ {
return $this->getOffset() < $this->_size; return $this->getOffset() < $this->_size;
} }
/** /**
* Jumps <var>size</var> amount of bytes in the file stream. * Jumps <var>size</var> amount of bytes in the file stream.
* *
* @return void * @return void
* @throws ReaderException if <var>size</var> attribute is not greater or * @throws ReaderException if <var>size</var> attribute is not greater or
* equal than zero. * equal than zero.
*/ */
public function skip($size) public function skip($size)
{ {
if ($size < 0) if ($size < 0)
throw new ReaderException("Invalid argument"); throw new ReaderException("Invalid argument");
fseek($this->_fd, $size, SEEK_CUR); fseek($this->_fd, $size, SEEK_CUR);
} }
/** /**
* Reads <var>length</var> amount of bytes from the file stream. * Reads <var>length</var> amount of bytes from the file stream.
* *
* @return string Returns read bytes as a string * @return string Returns read bytes as a string
* @throws ReaderException if <var>length</var> attribute is not greater than * @throws ReaderException if <var>length</var> attribute is not greater than
* zero. * zero.
*/ */
public function read($length) public function read($length)
{ {
if ($length <= 0) if ($length <= 0)
throw new ReaderException("Invalid argument"); throw new ReaderException("Invalid argument");
return fread($this->_fd, $length); return fread($this->_fd, $length);
} }
/** /**
* Returns the current point of operation. * Returns the current point of operation.
* *
* @return integer Returns the current cursor position. * @return integer Returns the current cursor position.
*/ */
public function getOffset() public function getOffset()
{ {
return ftell($this->_fd); return ftell($this->_fd);
} }
/** /**
* Sets the point of operation, ie the cursor offset value. * Sets the point of operation, ie the cursor offset value.
* *
* @return void * @return void
*/ */
public function setOffset($offset) public function setOffset($offset)
{ {
fseek($this->_fd, $offset); fseek($this->_fd, $offset);
} }
/** /**
* Returns the file size in bytes. * Returns the file size in bytes.
* *
* @return integer Returns the file size in bytes. * @return integer Returns the file size in bytes.
*/ */
public function getSize() public function getSize()
{ {
return $this->_size; return $this->_size;
} }
/** /**
* Magic function to delegate the call to helper methods of Transform class * Magic function to delegate the call to helper methods of Transform class
* to transform read data in another format. * to transform read data in another format.
* *
* The read data length is determined from the helper method name. For methods * The read data length is determined from the helper method name. For methods
* where arbitrary data lengths are accepted a parameter can be used to * where arbitrary data lengths are accepted a parameter can be used to
* specify the length. * specify the length.
* *
* @param string $method The method to be called. * @param string $method The method to be called.
* @param string $params The parameters should the function accept them. * @param string $params The parameters should the function accept them.
* @return mixed * @return mixed
* @throws ReaderException if no such transformer is implemented * @throws ReaderException if no such transformer is implemented
*/ */
public function __call($method, $params) { public function __call($method, $params) {
$chunks = array(); $chunks = array();
if (preg_match if (preg_match
("/get([a-z]{3,6})?(\d{1,2})?(?:LE|BE)?/i", $method, $chunks) && ("/get([a-z]{3,6})?(\d{1,2})?(?:LE|BE)?/i", $method, $chunks) &&
method_exists("Transform", $method)) { method_exists("Transform", $method)) {
return call_user_func return call_user_func
(array("Transform", $method), (array("Transform", $method),
$this->read(preg_match("/String|(?:H|L)Hex/", $chunks[1]) ? $this->read(preg_match("/String|(?:H|L)Hex/", $chunks[1]) ?
(isset($params[0]) ? $params[0] : 1) : (isset($params[0]) ? $params[0] : 1) :
($chunks[1] == "GUID" ? 16 : $chunks[2] / 8))); ($chunks[1] == "GUID" ? 16 : $chunks[2] / 8)));
} else throw new ReaderException("Unknown method: " . $method); } else throw new ReaderException("Unknown method: " . $method);
} }
} }

View File

@@ -1,46 +1,46 @@
<?php <?php
/** /**
* $Id$ * $Id$
* *
* *
* Copyright (C) 2008 BEHR Software Systems. All Rights Reserved. * Copyright (C) 2008 BEHR Software Systems. All Rights Reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* - Redistributions of source code must retain the above copyright notice, * - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, * - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* - Neither the name of the BEHR Software Systems nor the names of its * - Neither the name of the BEHR Software Systems nor the names of its
* contributors may be used to endorse or promote products derived from this * contributors may be used to endorse or promote products derived from this
* software without specific prior written permission. * software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
* @package php-reader * @package php-reader
*/ */
/** /**
* The ReaderException is thrown whenever an error occurs within the Reader * The ReaderException is thrown whenever an error occurs within the Reader
* class during a file operation. * class during a file operation.
* *
* @package php-reader * @package php-reader
* @author Sven Vollbehr <sven.vollbehr@behrss.eu> * @author Sven Vollbehr <sven.vollbehr@behrss.eu>
* @copyright 2008 BEHR Software Systems * @copyright 2008 BEHR Software Systems
* @version $Rev$ * @version $Rev$
*/ */
class ReaderException extends Exception class ReaderException extends Exception
{ {
} }

View File

@@ -1,282 +1,282 @@
<?php <?php
/** /**
* $Id$ * $Id$
* *
* *
* Copyright (C) 2006, 2007 The Bearpaw Project Work Group. All Rights Reserved. * Copyright (C) 2006, 2007 The Bearpaw Project Work Group. All Rights Reserved.
* Copyright (C) 2007, 2008 BEHR Software Systems. All Rights Reserved. * Copyright (C) 2007, 2008 BEHR Software Systems. All Rights Reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* - Redistributions of source code must retain the above copyright notice, * - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer. * this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, * - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* - Neither the name of the BEHR Software Systems nor the names of its * - Neither the name of the BEHR Software Systems nor the names of its
* contributors may be used to endorse or promote products derived from this * contributors may be used to endorse or promote products derived from this
* software without specific prior written permission. * software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
* @package php-reader * @package php-reader
*/ */
/** /**
* An utility class to perform simple byte transformations on data. * An utility class to perform simple byte transformations on data.
* *
* @package php-reader * @package php-reader
* @author Sven Vollbehr <sven.vollbehr@behrss.eu> * @author Sven Vollbehr <sven.vollbehr@behrss.eu>
* @copyright 2006, 2007 The Bearpaw Project Work Group * @copyright 2006, 2007 The Bearpaw Project Work Group
* @copyright 2007, 2008 BEHR Software Systems * @copyright 2007, 2008 BEHR Software Systems
* @version $Rev$ * @version $Rev$
* @static * @static
*/ */
final class Transform final class Transform
{ {
const MACHINE_ENDIAN_ORDER = 0; const MACHINE_ENDIAN_ORDER = 0;
const LITTLE_ENDIAN_ORDER = 1; const LITTLE_ENDIAN_ORDER = 1;
const BIG_ENDIAN_ORDER = 2; const BIG_ENDIAN_ORDER = 2;
/** /**
* Default private constructor for a static class. * Default private constructor for a static class.
*/ */
private function __construct() {} private function __construct() {}
/** /**
* Returns machine-endian ordered binary data as 64-bit float. PHP does not * Returns machine-endian ordered binary data as 64-bit float. PHP does not
* support 64-bit integers as the long integer is of 32-bits but using * support 64-bit integers as the long integer is of 32-bits but using
* aritmetic operations it is implicitly converted into floating point which * aritmetic operations it is implicitly converted into floating point which
* is of 64-bits long. * is of 64-bits long.
* *
* @return integer Returns the resulting 64-bit integer. * @return integer Returns the resulting 64-bit integer.
*/ */
public static function getInt64($raw, $order = self::MACHINE_ENDIAN_ORDER) public static function getInt64($raw, $order = self::MACHINE_ENDIAN_ORDER)
{ {
list(, $lo, $hi) = unpack(($order == 2 ? "L" : list(, $lo, $hi) = unpack(($order == 2 ? "L" :
($order == 1 ? "V" : "N")) . "*", $raw); ($order == 1 ? "V" : "N")) . "*", $raw);
return $hi * 0xffffffff + $lo; return $hi * 0xffffffff + $lo;
} }
/** /**
* Returns little-endian ordered binary data as 64-bit float. PHP does not * Returns little-endian ordered binary data as 64-bit float. PHP does not
* support 64-bit integers as the long integer is of 32-bits but using * support 64-bit integers as the long integer is of 32-bits but using
* aritmetic operations it is implicitly converted into floating point which * aritmetic operations it is implicitly converted into floating point which
* is of 64-bits long. * is of 64-bits long.
* *
* @return integer Returns the resulting 64-bit integer. * @return integer Returns the resulting 64-bit integer.
*/ */
public static function getInt64LE($raw) public static function getInt64LE($raw)
{ {
return ReaderUtils::getInt64($raw, self::LITTLE_ENDIAN_ORDER); return ReaderUtils::getInt64($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
* Returns big-endian ordered binary data as 64-bit float. PHP does not * Returns big-endian ordered binary data as 64-bit float. PHP does not
* support 64-bit integers as the long integer is of 32-bits but using * support 64-bit integers as the long integer is of 32-bits but using
* aritmetic operations it is implicitly converted into floating point which * aritmetic operations it is implicitly converted into floating point which
* is of 64-bits long. * is of 64-bits long.
* *
* @return integer Returns the resulting 64-bit integer. * @return integer Returns the resulting 64-bit integer.
*/ */
public static function getInt64BE($raw) public static function getInt64BE($raw)
{ {
return ReaderUtils::getInt64($raw, self::BIG_ENDIAN_ORDER); return ReaderUtils::getInt64($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**
* Returns machine-endian ordered binary data as signed 32-bit integer. * Returns machine-endian ordered binary data as signed 32-bit integer.
* *
* @return integer Returns the resulting signed 32-bit integer. * @return integer Returns the resulting signed 32-bit integer.
*/ */
public static function getInt32($raw) public static function getInt32($raw)
{ {
list(, $int) = unpack("l*", $raw); list(, $int) = unpack("l*", $raw);
return $int; return $int;
} }
/** /**
* Returns machine-endian ordered binary data as unsigned 32-bit integer. * Returns machine-endian ordered binary data as unsigned 32-bit integer.
* *
* @return integer Returns the resulting unsigned 32-bit integer. * @return integer Returns the resulting unsigned 32-bit integer.
*/ */
public static function getUInt32($raw, $order = self::MACHINE_ENDIAN_ORDER) public static function getUInt32($raw, $order = self::MACHINE_ENDIAN_ORDER)
{ {
list(, $int) = unpack(($order == 2 ? "N" : list(, $int) = unpack(($order == 2 ? "N" :
($order == 1 ? "V" : "L")) . "*", $raw); ($order == 1 ? "V" : "L")) . "*", $raw);
return $int; return $int;
} }
/** /**
* Returns little-endian ordered binary data as unsigned 32-bit integer. * Returns little-endian ordered binary data as unsigned 32-bit integer.
* *
* @return integer Returns the resulting unsigned 32-bit integer. * @return integer Returns the resulting unsigned 32-bit integer.
*/ */
public static function getUInt32LE($raw) public static function getUInt32LE($raw)
{ {
return ReaderUtils::getUInt32($raw, self::LITTLE_ENDIAN_ORDER); return ReaderUtils::getUInt32($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
* Returns big-endian ordered binary data as unsigned 32-bit integer. * Returns big-endian ordered binary data as unsigned 32-bit integer.
* *
* @return integer Returns the resulting unsigned 32-bit integer. * @return integer Returns the resulting unsigned 32-bit integer.
*/ */
public static function getUInt32BE($raw) public static function getUInt32BE($raw)
{ {
return ReaderUtils::getUInt32($raw, self::BIG_ENDIAN_ORDER); return ReaderUtils::getUInt32($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**
* Returns machine endian ordered binary data as signed 16-bit integer. * Returns machine endian ordered binary data as signed 16-bit integer.
* *
* @return integer Returns the resulting signed 16-bit integer. * @return integer Returns the resulting signed 16-bit integer.
*/ */
public static function getInt16($raw) public static function getInt16($raw)
{ {
list(, $int) = unpack("s*", $raw); list(, $int) = unpack("s*", $raw);
return $int; return $int;
} }
/** /**
* Returns machine endian ordered binary data as unsigned 16-bit integer. * Returns machine endian ordered binary data as unsigned 16-bit integer.
* *
* @return integer Returns the resulting unsigned 16-bit integer. * @return integer Returns the resulting unsigned 16-bit integer.
*/ */
public static function getUInt16($raw, $order) public static function getUInt16($raw, $order)
{ {
list(, $int) = unpack(($order == 2 ? "n" : list(, $int) = unpack(($order == 2 ? "n" :
($order == 1 ? "v" : "S")) . "*", $raw); ($order == 1 ? "v" : "S")) . "*", $raw);
return $int; return $int;
} }
/** /**
* Returns little-endian ordered binary data as unsigned 16-bit integer. * Returns little-endian ordered binary data as unsigned 16-bit integer.
* *
* @return integer Returns the resulting unsigned 16-bit integer. * @return integer Returns the resulting unsigned 16-bit integer.
*/ */
public static function getUInt16LE($raw) public static function getUInt16LE($raw)
{ {
return ReaderUtils::getUInt16($raw, self::LITTLE_ENDIAN_ORDER); return ReaderUtils::getUInt16($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
* Returns big-endian ordered binary data as unsigned 16-bit integer. * Returns big-endian ordered binary data as unsigned 16-bit integer.
* *
* @return integer Returns the resulting unsigned 16-bit integer. * @return integer Returns the resulting unsigned 16-bit integer.
*/ */
public static function getUInt16BE($raw) public static function getUInt16BE($raw)
{ {
return ReaderUtils::getUInt16($raw, self::BIG_ENDIAN_ORDER); return ReaderUtils::getUInt16($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**
* Returns binary data as 8-bit integer. * Returns binary data as 8-bit integer.
* *
* @return integer Returns the resulting 8-bit integer. * @return integer Returns the resulting 8-bit integer.
*/ */
public static function getInt8($raw) public static function getInt8($raw)
{ {
return $raw; return $raw;
} }
/** /**
* Returns binary data as string. Removes terminating zero. * Returns binary data as string. Removes terminating zero.
* *
* @return string Returns the resulting string. * @return string Returns the resulting string.
*/ */
public static function getString8($raw) public static function getString8($raw)
{ {
$string = ""; $string = "";
foreach (unpack("C*", $raw) as $char) foreach (unpack("C*", $raw) as $char)
$string .= pack("c", $char); $string .= pack("c", $char);
return rtrim($string, "\0"); return rtrim($string, "\0");
} }
/** /**
* Returns machine-endian ordered binary data as string. * Returns machine-endian ordered binary data as string.
* *
* @return string Returns the resulting string. * @return string Returns the resulting string.
*/ */
public static function getString16($raw, $order = self::MACHINE_ENDIAN_ORDER) public static function getString16($raw, $order = self::MACHINE_ENDIAN_ORDER)
{ {
$string = ""; $string = "";
foreach (unpack(($order == 2 ? "n" : foreach (unpack(($order == 2 ? "n" :
($order == 1 ? "v" : "S")) . "*", $raw) as $char) ($order == 1 ? "v" : "S")) . "*", $raw) as $char)
$string .= pack("S", $char); $string .= pack("S", $char);
return rtrim($string, "\0"); return rtrim($string, "\0");
} }
/** /**
* Returns little-endian ordered binary data as string. * Returns little-endian ordered binary data as string.
* *
* @return string Returns the resulting string. * @return string Returns the resulting string.
*/ */
public static function getString16LE($raw) public static function getString16LE($raw)
{ {
return ReaderUtils::getString16($raw, self::LITTLE_ENDIAN_ORDER); return ReaderUtils::getString16($raw, self::LITTLE_ENDIAN_ORDER);
} }
/** /**
* Returns big-endian ordered binary data as string. * Returns big-endian ordered binary data as string.
* *
* @return string Returns the resulting string. * @return string Returns the resulting string.
*/ */
public static function getString16BE($raw) public static function getString16BE($raw)
{ {
return ReaderUtils::getString16($raw, self::BIG_ENDIAN_ORDER); return ReaderUtils::getString16($raw, self::BIG_ENDIAN_ORDER);
} }
/** /**
* Returns binary data as hexadecimal string having high nibble first. * Returns binary data as hexadecimal string having high nibble first.
* *
* @return string Returns the resulting string. * @return string Returns the resulting string.
*/ */
public static function getHHex($raw) public static function getHHex($raw)
{ {
list($hex) = unpack("H*0", $raw); list($hex) = unpack("H*0", $raw);
return $hex; return $hex;
} }
/** /**
* Returns binary data as hexadecimal string having low nibble first. * Returns binary data as hexadecimal string having low nibble first.
* *
* @return string Returns the resulting string. * @return string Returns the resulting string.
*/ */
public static function getLHex($raw) public static function getLHex($raw)
{ {
list($hex) = unpack("h*0", $raw); list($hex) = unpack("h*0", $raw);
return $hex; return $hex;
} }
/** /**
* Returns the little-endian ordered raw data as big-endian ordered * Returns the little-endian ordered raw data as big-endian ordered
* hexadecimal GUID string. * hexadecimal GUID string.
* *
* @return string Returns the raw data in appropriate GUID format. * @return string Returns the raw data in appropriate GUID format.
*/ */
public static function getGUID($raw) public static function getGUID($raw)
{ {
$C = @unpack("V1V/v2v/N2N", $raw); $C = @unpack("V1V/v2v/N2N", $raw);
list($hex) = @unpack("H*0", pack list($hex) = @unpack("H*0", pack
("NnnNN", $C["V"], $C["v1"], $C["v2"], $C["N1"], $C["N2"])); ("NnnNN", $C["V"], $C["v1"], $C["v2"], $C["N1"], $C["N2"]));
/* Fixes a bug in PHP versions earlier than Jan 25 2006 */ /* Fixes a bug in PHP versions earlier than Jan 25 2006 */
if (implode("", unpack("H*", pack("H*", "a"))) == "a00") if (implode("", unpack("H*", pack("H*", "a"))) == "a00")
$hex = substr($hex, 0, -1); $hex = substr($hex, 0, -1);
return preg_replace return preg_replace
("/^(.{8})(.{4})(.{4})(.{4})/", "\\1-\\2-\\3-\\4-", $hex); ("/^(.{8})(.{4})(.{4})(.{4})/", "\\1-\\2-\\3-\\4-", $hex);
} }
} }