summaryrefslogtreecommitdiffstats
path: root/vendor/fgrosse/phpasn1/lib/ASN1/Base128.php
blob: 3cb3511a229943afe5bfd39b09393a56eafadbec (plain) (blame)
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
<?php

namespace FG\ASN1;

use FG\Utility\BigInteger;
use InvalidArgumentException;

/**
 * A base-128 decoder.
 */
class Base128
{
    /**
     * @param int $value
     *
     * @return string
     */
    public static function encode($value)
    {
        $value = BigInteger::create($value);
        $octets = chr($value->modulus(0x80)->toInteger());

        $value = $value->shiftRight(7);
        while ($value->compare(0) > 0) {
            $octets .= chr(0x80 | $value->modulus(0x80)->toInteger());
            $value = $value->shiftRight(7);
        }

        return strrev($octets);
    }

    /**
     * @param string $octets
     *
     * @throws InvalidArgumentException if the given octets represent a malformed base-128 value or the decoded value would exceed the the maximum integer length
     *
     * @return int
     */
    public static function decode($octets)
    {
        $bitsPerOctet = 7;
        $value = BigInteger::create(0);
        $i = 0;

        while (true) {
            if (!isset($octets[$i])) {
                throw new InvalidArgumentException(sprintf('Malformed base-128 encoded value (0x%s).', strtoupper(bin2hex($octets)) ?: '0'));
            }

            $octet = ord($octets[$i++]);

            $l1 = $value->shiftLeft($bitsPerOctet);
            $r1 = $octet & 0x7f;
            $value = $l1->add($r1);

            if (0 === ($octet & 0x80)) {
                break;
            }
        }

        return (string)$value;
    }
}