ref: bbe9c621a38627a8ce44ee46045f586b8e44704c
converter/vendor/rybakit/msgpack/src/Packer.php
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 |
<?php /* * This file is part of the rybakit/msgpack.php package. * * (c) Eugene Leonovich <gen.work@gmail.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace MessagePack; use MessagePack\Exception\InvalidOptionException; use MessagePack\Exception\PackingFailedException; use MessagePack\TypeTransformer\Packable; class Packer { private const UTF8_REGEX = '/\A(?: [\x00-\x7F]++ # ASCII | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16 )*+\z/x'; private $isDetectStrBin; private $isForceStr; private $isDetectArrMap; private $isForceArr; private $isForceFloat32; /** * @var Packable[]|null */ private $transformers; /** * @param PackOptions|int|null $options * * @throws InvalidOptionException */ public function __construct($options = null) { if (null === $options) { $options = PackOptions::fromDefaults(); } elseif (!$options instanceof PackOptions) { $options = PackOptions::fromBitmask($options); } $this->isDetectStrBin = $options->isDetectStrBinMode(); $this->isForceStr = $options->isForceStrMode(); $this->isDetectArrMap = $options->isDetectArrMapMode(); $this->isForceArr = $options->isForceArrMode(); $this->isForceFloat32 = $options->isForceFloat32Mode(); } public function registerTransformer(Packable $transformer) : self { $this->transformers[] = $transformer; return $this; } public function pack($value) { if (\is_int($value)) { return $this->packInt($value); } if (\is_string($value)) { if ($this->isForceStr) { return $this->packStr($value); } if ($this->isDetectStrBin) { return \preg_match(self::UTF8_REGEX, $value) ? $this->packStr($value) : $this->packBin($value); } return $this->packBin($value); } if (\is_array($value)) { if ($this->isDetectArrMap) { if (!isset($value[0]) && !\array_key_exists(0, $value)) { return empty($value) ? "\x90" : $this->packMap($value); } return \array_values($value) === $value ? $this->packArray($value) : $this->packMap($value); } return $this->isForceArr ? $this->packArray($value) : $this->packMap($value); } if (null === $value) { return "\xc0"; } if (\is_bool($value)) { return $value ? "\xc3" : "\xc2"; } if (\is_float($value)) { return $this->packFloat($value); } if ($value instanceof Ext) { return $this->packExt($value->type, $value->data); } if ($this->transformers) { foreach ($this->transformers as $transformer) { if (null !== $packed = $transformer->pack($this, $value)) { return $packed; } } } throw PackingFailedException::unsupportedType($value); } public function packNil() { return "\xc0"; } public function packBool($bool) { return $bool ? "\xc3" : "\xc2"; } public function packInt($int) { if ($int >= 0) { if ($int <= 0x7f) { return \chr($int); } if ($int <= 0xff) { return "\xcc".\chr($int); } if ($int <= 0xffff) { return "\xcd".\chr($int >> 8).\chr($int); } if ($int <= 0xffffffff) { return \pack('CN', 0xce, $int); } return \pack('CJ', 0xcf, $int); } if ($int >= -0x20) { return \chr(0xe0 | $int); } if ($int >= -0x80) { return "\xd0".\chr($int); } if ($int >= -0x8000) { return "\xd1".\chr($int >> 8).\chr($int); } if ($int >= -0x80000000) { return \pack('CN', 0xd2, $int); } return \pack('CJ', 0xd3, $int); } public function packFloat($float) { return $this->isForceFloat32 ? "\xca".\pack('G', $float) : "\xcb".\pack('E', $float); } public function packStr($str) { $length = \strlen($str); if ($length < 32) { return \chr(0xa0 | $length).$str; } if ($length <= 0xff) { return "\xd9".\chr($length).$str; } if ($length <= 0xffff) { return "\xda".\chr($length >> 8).\chr($length).$str; } return \pack('CN', 0xdb, $length).$str; } public function packBin($str) { $length = \strlen($str); if ($length <= 0xff) { return "\xc4".\chr($length).$str; } if ($length <= 0xffff) { return "\xc5".\chr($length >> 8).\chr($length).$str; } return \pack('CN', 0xc6, $length).$str; } public function packArray($array) { $data = $this->packArrayHeader(\count($array)); foreach ($array as $val) { $data .= $this->pack($val); } return $data; } public function packArrayHeader($size) { if ($size <= 0xf) { return \chr(0x90 | $size); } if ($size <= 0xffff) { return "\xdc".\chr($size >> 8).\chr($size); } return \pack('CN', 0xdd, $size); } public function packMap($map) { $data = $this->packMapHeader(\count($map)); if ($this->isForceStr) { foreach ($map as $key => $val) { $data .= \is_string($key) ? $this->packStr($key) : $this->packInt($key); $data .= $this->pack($val); } return $data; } if ($this->isDetectStrBin) { foreach ($map as $key => $val) { $data .= \is_string($key) ? (\preg_match(self::UTF8_REGEX, $key) ? $this->packStr($key) : $this->packBin($key)) : $this->packInt($key); $data .= $this->pack($val); } return $data; } foreach ($map as $key => $val) { $data .= \is_string($key) ? $this->packBin($key) : $this->packInt($key); $data .= $this->pack($val); } return $data; } public function packMapHeader($size) { if ($size <= 0xf) { return \chr(0x80 | $size); } if ($size <= 0xffff) { return "\xde".\chr($size >> 8).\chr($size); } return \pack('CN', 0xdf, $size); } public function packExt($type, $data) { $length = \strlen($data); switch ($length) { case 1: return "\xd4".\chr($type).$data; case 2: return "\xd5".\chr($type).$data; case 4: return "\xd6".\chr($type).$data; case 8: return "\xd7".\chr($type).$data; case 16: return "\xd8".\chr($type).$data; } if ($length <= 0xff) { return "\xc7".\chr($length).\chr($type).$data; } if ($length <= 0xffff) { return \pack('CnC', 0xc8, $length, $type).$data; } return \pack('CNC', 0xc9, $length, $type).$data; } } |