/usr/share/php/Horde/Pack.php is in php-horde-pack 1.0.7-1.
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 | <?php
/**
* Copyright 2013-2016 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2013-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Pack
*/
/**
* A replacement for serialize()/json_encode() that will automatically
* use the most efficient serialization available based on the input.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013-2016 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Pack
*/
class Horde_Pack
{
/* Default compress length (in bytes). */
const DEFAULT_COMPRESS = 128;
/* Mask for compressed data. */
const COMPRESS_MASK = 64;
/**
* Instance of Horde_Compress_Fast shared between all instances.
*
* @var Horde_Compress_Fast
*/
protected static $_compress;
/**
* Drivers. Shared between all instances.
*
* @var array
*/
protected static $_drivers = array();
/**
* Constructor.
*/
public function __construct()
{
if (empty(self::$_drivers)) {
$fi = new FilesystemIterator(__DIR__ . '/Pack/Driver');
$class_prefix = __CLASS__ . '_Driver_';
foreach ($fi as $val) {
if ($val->isFile()) {
$cname = $class_prefix . $val->getBasename('.php');
if (class_exists($cname) && $cname::supported()) {
$ob = new $cname();
self::$_drivers[$ob->id] = $ob;
}
}
}
krsort(self::$_drivers, SORT_NUMERIC);
self::$_compress = new Horde_Compress_Fast();
}
}
/**
*/
public function __sleep()
{
throw new LogicException('Object can not be serialized.');
}
/**
* Pack a string.
*
* @param mixed $data The data to pack.
* @param array $opts Additional options:
* <pre>
* - compress: (mixed) If false, don't use compression. If true, uses
* default compress length (DEFAULT). If 0, always compress.
* All other integer values: compress only if data is
* greater than this string length.
* - drivers: (array) Only use these drivers to pack. By default, driver
* to use is auto-determined.
* - phpob: (boolean) If true, the data contains PHP serializable
* objects (i.e. objects that have a PHP-specific serialized
* representation). If false, the data does not contain any of
* these objects. If not present, will auto-determine
* existence of these objects.
* </pre>
*
* @return string The packed string.
* @throws Horde_Pack_Exception
*/
public function pack($data, array $opts = array())
{
$opts = array_merge(array(
'compress' => true
), $opts);
if (!isset($opts['phpob'])) {
$auto = new Horde_Pack_Autodetermine($data);
$opts['phpob'] = $auto->phpob;
}
foreach (self::$_drivers as $key => $val) {
if (!empty($opts['phpob']) && !$val->phpob) {
continue;
}
if (isset($opts['drivers'])) {
if (!in_array(get_class($val), $opts['drivers'])) {
continue;
}
}
/* Format of data:
* First-byte:
* 1,2,4,8,16,32 - Reserved for pack format.
* 64 - Packed data has been compressed.
* 128 - RESERVED for future use (if set, indicates that initial
* byte will extend into next byte).
* Packed (and compressed data) follows this byte. */
try {
$packed = $val->pack($data);
} catch (Horde_Pack_Exception $e) {
continue;
}
if ($opts['compress'] !== false) {
if ($opts['compress'] === 0) {
$compress = true;
} else {
if ($opts['compress'] === true) {
$opts['compress'] = self::DEFAULT_COMPRESS;
}
$compress = strlen($packed) > $opts['compress'];
}
if ($compress) {
$packed = self::$_compress->compress($packed);
$key |= self::COMPRESS_MASK;
}
}
return pack('C', $key) . $packed;
}
throw new Horde_Pack_Exception('Could not pack data.');
}
/**
* Unpack a string.
*
* @param string $data The packed string.
*
* @return mixed The unpacked data.
* @throws Horde_Pack_Exception
*/
public function unpack($data)
{
if (!$data) {
return $data;
}
if (is_string($data)) {
$mask = unpack('C*', $data[0]);
$mask = reset($mask);
$data = substr($data, 1);
if ($mask & self::COMPRESS_MASK) {
$data = self::$_compress->decompress($data);
$mask ^= self::COMPRESS_MASK;
}
if (isset(self::$_drivers[$mask])) {
try {
return self::$_drivers[$mask]->unpack($data);
} catch (Horde_Pack_Exception $e) {
throw $e;
} catch (Exception $e) {
/* Unknown exceptions are handled with the throw below. */
}
}
}
throw new Horde_Pack_Exception('Could not unpack data');
}
}
|