File: AMQPByteStream.php

package info (click to toggle)
php-amqplib 3.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,060 kB
  • sloc: php: 13,145; makefile: 77; sh: 27
file content (100 lines) | stat: -rw-r--r-- 2,281 bytes parent folder | download | duplicates (2)
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
<?php

namespace PhpAmqpLib\Wire;

use PhpAmqpLib\Helper\BigInteger;

abstract class AMQPByteStream
{
    public const BIT = 1;
    public const OCTET = 1;
    public const SHORTSTR = 1;
    public const SHORT = 2;
    public const LONG = 4;
    public const SIGNED_LONG = 4;
    public const READ_PHP_INT = 4; // use READ_ to avoid possible clashes with PHP
    public const LONGLONG = 8;
    public const TIMESTAMP = 8;

    /** @var bool */
    protected const PLATFORM_64BIT = PHP_INT_SIZE === 8;

    /** @var BigInteger[][] */
    protected static $bigIntegers = array();

    /**
     * @var bool
     */
    protected static $isLittleEndian;

    /**
     * Converts byte-string between native and network byte order, in both directions
     *
     * @param string $bytes
     * @return string
     */
    protected function correctEndianness($bytes)
    {
        return self::isLittleEndian() ? $this->convertByteOrder($bytes) : $bytes;
    }

    /**
     * @param string $bytes
     * @return string
     */
    protected function convertByteOrder($bytes)
    {
        return strrev($bytes);
    }

    /**
     * @param int $longInt
     * @return bool
     */
    protected function getLongMSB($longInt)
    {
        return (bool) ($longInt & 0x80000000);
    }

    /**
     * @param string $bytes
     * @return bool
     */
    protected function getMSB($bytes)
    {
        return ord($bytes[0]) > 127;
    }

    /**
     * @return bool
     */
    protected static function isLittleEndian()
    {
        if (self::$isLittleEndian === null) {
            $tmp = unpack('S', "\x01\x00"); // to maintain 5.3 compatibility
            self::$isLittleEndian = $tmp[1] === 1;
        }

        return self::$isLittleEndian;
    }

    /**
     * @param string $value
     * @param int $base
     * @return BigInteger
     */
    protected static function getBigInteger($value, $base = 10)
    {
        if (!isset(self::$bigIntegers[$base])) {
            self::$bigIntegers[$base] = array();
        }
        if (isset(self::$bigIntegers[$base][$value])) {
            return self::$bigIntegers[$base][$value];
        }

        $integer = new BigInteger($value, $base);
        self::$bigIntegers[$base][$value] = $integer;

        return $integer;
    }
}