/usr/share/php/ZipStreamer/lib/Count64.php is in php-zipstreamer 0.7-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | <?php
/**
* Simple class to support some very basic operations on 64 bit intergers
* on 32 bit machines.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Nicolai Ehemann <en@enlightened.de>
* @copyright Copyright (C) 2013-2014 Nicolai Ehemann and contributors
* @license GNU GPL
*/
namespace ZipStreamer;
const INT64_HIGH_MAP = 0xffffffff00000000;
const INT64_LOW_MAP = 0x00000000ffffffff;
const INT_MAX_32 = 0xffffffff;
/**
* Unsigned right shift
*
* @param int $bits integer to be shifted
* @param int $shift number of bits to be shifted
* @return int shifted integer
*/
function urShift($bits, $shift) {
if ($shift == 0) {
return $bits;
}
return ($bits >> $shift) & ~(1 << (8 * PHP_INT_SIZE - 1) >> ($shift - 1));
}
/**
* Convert binary data string to readable hex string
*
* @param string $data binary string
* @return string readable hex string
*/
function byte2hex($data) {
return unpack("h*", $data);
}
/**
* Pack 1 byte data into binary string
*
* @param mixed $data data
* @return string 1 byte binary string
*/
function pack8($data) {
return pack('C', $data);
}
/**
* Pack 2 byte data into binary string, little endian format
*
* @param mixed $data data
* @return string 2 byte binary string
*/
function pack16le($data) {
return pack('v', $data);
}
/**
* Unpack 2 byte binary string, little endian format to 2 byte data
*
* @param string $data binary string
* @return integer 2 byte data
*/
function unpack16le($data) {
$result = unpack('v', $data);
return $result[1];
}
/**
* Pack 4 byte data into binary string, little endian format
*
* @param mixed $data data
* @return 4 byte binary string
*/
function pack32le($data) {
return pack('V', $data);
}
/**
* Unpack 4 byte binary string, little endian format to 4 byte data
*
* @param string $data binary string
* @return integer 4 byte data
*/
function unpack32le($data) {
$result = unpack('V', $data);
return $result[1];
}
/**
* Pack 8 byte data into binary string, little endian format
*
* @param mixed $data data
* @return string 8 byte binary string
*/
function pack64le($data) {
if (is_object($data)) {
if ("Count64_32" == get_class($data)) {
$value = $data->_getValue();
$hiBytess = $value[0];
$loBytess = $value[1];
} else {
$hiBytess = ($data->_getValue() & INT64_HIGH_MAP) >> 32;
$loBytess = $data->_getValue() & INT64_LOW_MAP;
}
} else if (4 == PHP_INT_SIZE) {
$hiBytess = 0;
$loBytess = $data;
} else {
$hiBytess = ($data & INT64_HIGH_MAP) >> 32;
$loBytess = $data & INT64_LOW_MAP;
}
return pack('VV', $loBytess, $hiBytess);
}
/**
* Unpack 8 byte binary string, little endian format to 8 byte data
*
* @param string $data binary string
* @return Count64Base data
*/
function unpack64le($data) {
$bytes = unpack('V2', $data);
return Count64::construct(array(
$bytes[1],
$bytes[2]
));
}
abstract class Count64Base {
protected $limit32Bit = False;
function __construct($value = 0, $limit32Bit = False) {
$this->limit32Bit = $limit32Bit;
$this->set($value);
}
abstract public function set($value);
abstract public function add($value);
abstract public function getHiBytes();
abstract public function getLoBytes();
abstract public function _getValue();
const EXCEPTION_SET_INVALID_ARGUMENT = "Count64 object can only be set() to integer or Count64 values";
const EXCEPTION_ADD_INVALID_ARGUMENT = "Count64 object can only be add()ed integer or Count64 values";
const EXCEPTION_32BIT_OVERFLOW = "Count64 object limited to 32 bit (overflow)";
}
class Count64_32 extends Count64Base {
private $loBytes;
private $hiBytes;
public function getHiBytes() {
return $this->hiBytes;
}
public function getLoBytes() {
return $this->loBytes;
}
public function _getValue() {
return array($this->hiBytes, $this->loBytes);
}
public function set($value) {
if (is_int($value)) {
$this->loBytes = $value;
$this->hiBytes = 0;
} else if (is_array($value) && 2 == sizeof($value)) {
$this->loBytes = $value[0];
if ($this->limit32Bit && 0 !== $value[1]) {
throw new \OverflowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->hiBytes = $value[1];
} else if (is_object($value) && __CLASS__ == get_class($value)) {
$value = $value->_getValue();
if ($this->limit32Bit && 0 !== $value[0]) {
throw new \OverflowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->hiBytes = $value[0];
$this->loBytes = $value[1];
} else {
throw new \InvalidArgumentException(self::EXCEPTION_SET_INVALID_ARGUMENT);
}
return $this;
}
public function add($value) {
if (is_int($value)) {
$sum = (int) ($this->loBytes + $value);
// overflow!
if (($this->loBytes > -1 && $sum < $this->loBytes && $sum > -1)
|| ($this->loBytes < 0 && ($sum < $this->loBytes || $sum > -1))) {
if ($this->limit32Bit) {
throw new \OverflowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->hiBytes = (int) ($this->hiBytes + 1);
}
$this->loBytes = $sum;
} else if (is_object($value) && __CLASS__ == get_class($value)) {
$value = $value->_getValue();
$sum = (int) ($this->loBytes + $value[1]);
if (($this->loBytes > -1 && $sum < $this->loBytes && $sum > -1)
|| ($this->loBytes < 0 && ($sum < $this->loBytes || $sum > -1))) {
if ($this->limit32Bit) {
throw new \OverflowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->hiBytes = (int) ($this->hiBytes + 1);
}
$this->loBytes = $sum;
if ($this->limit32Bit && 0 !== $value[0]) {
throw new \OverflowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->hiBytes = (int) ($this->hiBytes + $value[0]);
} else {
throw new \InvalidArgumentException(self::EXCEPTION_ADD_INVALID_ARGUMENT);
}
return $this;
}
}
class Count64_64 extends Count64Base {
private $value;
public function getHiBytes() {
return urShift($this->value, 32);
}
public function getLoBytes() {
return $this->value & INT64_LOW_MAP;
}
public function _getValue() {
return $this->value;
}
public function set($value) {
if (is_int($value)) {
if ($this->limit32Bit && INT_MAX_32 < $value) {
throw new \OverFlowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->value = $value;
} else if (is_array($value) && 2 == sizeof($value)) {
if ($this->limit32Bit && 0 !== $value[1]) {
throw new \OverFlowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->value = $value[1];
$this->value = $this->value << 32;
$this->value = $this->value + $value[0];
} else if (is_object($value) && __CLASS__ == get_class($value)) {
$value = $value->_getValue();
if ($this->limit32Bit && INT_MAX_32 < $value) {
throw new \OverFlowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->value = $value;
} else {
throw new \InvalidArgumentException(self::EXCEPTION_SET_INVALID_ARGUMENT);
}
return $this;
}
public function add($value) {
if (is_int($value)) {
$sum = (int) ($this->value + $value);
} else if (is_object($value) && __CLASS__ == get_class($value)) {
$sum = (int) ($this->value + $value->_getValue());
} else {
throw new \InvalidArgumentException(self::EXCEPTION_ADD_INVALID_ARGUMENT);
}
if ($this->limit32Bit && INT_MAX_32 < $sum) {
throw new \OverFlowException(self::EXCEPTION_32BIT_OVERFLOW);
}
$this->value = $sum;
return $this;
}
}
abstract class Count64 {
public static function construct($value = 0, $limit32Bit = False) {
if (4 == PHP_INT_SIZE) {
return new Count64_32($value, $limit32Bit);
} else {
return new Count64_64($value, $limit32Bit);
}
}
}
?>
|