Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
53.85% covered (warning)
53.85%
14 / 26
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Less_SourceMap_Base64VLQ
53.85% covered (warning)
53.85%
14 / 26
50.00% covered (danger)
50.00%
4 / 8
37.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toVLQSigned
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 fromVLQSigned
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 encode
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 decode
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 zeroFill
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 base64Encode
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 base64Decode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4/**
5 * Encode / Decode Base64 VLQ.
6 *
7 * @private
8 */
9class Less_SourceMap_Base64VLQ {
10
11    /**
12     * Shift
13     *
14     * @var int
15     */
16    private $shift = 5;
17
18    /**
19     * Mask
20     *
21     * @var int
22     */
23    private $mask = 0x1F; // == (1 << shift) == 0b00011111
24
25    /**
26     * Continuation bit
27     *
28     * @var int
29     */
30    private $continuationBit = 0x20; // == (mask - 1 ) == 0b00100000
31
32    /**
33     * Char to integer map
34     *
35     * @var array
36     */
37    private $charToIntMap = [
38        'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6,
39        'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13,
40        'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20,
41        'V' => 21, 'W' => 22, 'X' => 23, 'Y' => 24, 'Z' => 25, 'a' => 26, 'b' => 27,
42        'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, 'g' => 32, 'h' => 33, 'i' => 34,
43        'j' => 35, 'k' => 36, 'l' => 37, 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41,
44        'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48,
45        'x' => 49, 'y' => 50, 'z' => 51, 0 => 52, 1 => 53, 2 => 54, 3 => 55, 4 => 56,
46        5 => 57, 6 => 58, 7 => 59, 8 => 60, 9 => 61, '+' => 62, '/' => 63,
47    ];
48
49    /**
50     * Integer to char map
51     *
52     * @var array
53     */
54    private $intToCharMap = [
55        0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G',
56        7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N',
57        14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U',
58        21 => 'V', 22 => 'W', 23 => 'X', 24 => 'Y', 25 => 'Z', 26 => 'a', 27 => 'b',
59        28 => 'c', 29 => 'd', 30 => 'e', 31 => 'f', 32 => 'g', 33 => 'h', 34 => 'i',
60        35 => 'j', 36 => 'k', 37 => 'l', 38 => 'm', 39 => 'n', 40 => 'o', 41 => 'p',
61        42 => 'q', 43 => 'r', 44 => 's', 45 => 't', 46 => 'u', 47 => 'v', 48 => 'w',
62        49 => 'x', 50 => 'y', 51 => 'z', 52 => '0', 53 => '1', 54 => '2', 55 => '3',
63        56 => '4', 57 => '5', 58 => '6', 59 => '7', 60 => '8', 61 => '9', 62 => '+',
64        63 => '/',
65    ];
66
67    /**
68     * Constructor
69     */
70    public function __construct() {
71        // I leave it here for future reference
72        // foreach(str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') as $i => $char)
73        // {
74        //     $this->charToIntMap[$char] = $i;
75        //     $this->intToCharMap[$i] = $char;
76        // }
77    }
78
79    /**
80     * Convert from a two-complement value to a value where the sign bit is
81     * is placed in the least significant bit.    For example, as decimals:
82     *     1 becomes 2 (10 binary), -1 becomes 3 (11 binary)
83     *     2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
84     * We generate the value for 32 bit machines, hence -2147483648 becomes 1, not 4294967297,
85     * even on a 64 bit machine.
86     * @param int $aValue
87     */
88    public function toVLQSigned( $aValue ) {
89        return 0xffffffff & ( $aValue < 0 ? ( ( -$aValue ) << 1 ) + 1 : ( $aValue << 1 ) + 0 );
90    }
91
92    /**
93     * Convert to a two-complement value from a value where the sign bit is
94     * is placed in the least significant bit. For example, as decimals:
95     *     2 (10 binary) becomes 1, 3 (11 binary) becomes -1
96     *     4 (100 binary) becomes 2, 5 (101 binary) becomes -2
97     * We assume that the value was generated with a 32 bit machine in mind.
98     * Hence
99     *     1 becomes -2147483648
100     * even on a 64 bit machine.
101     * @param int $aValue
102     */
103    public function fromVLQSigned( $aValue ) {
104        return $aValue & 1 ? $this->zeroFill( ~$aValue + 2, 1 ) | ( -1 - 0x7fffffff ) : $this->zeroFill( $aValue, 1 );
105    }
106
107    /**
108     * Return the base 64 VLQ encoded value.
109     *
110     * @param int $aValue The value to encode
111     * @return string The encoded value
112     */
113    public function encode( $aValue ) {
114        $encoded = '';
115        $vlq = $this->toVLQSigned( $aValue );
116        do {
117            $digit = $vlq & $this->mask;
118            $vlq = $this->zeroFill( $vlq, $this->shift );
119            if ( $vlq > 0 ) {
120                $digit |= $this->continuationBit;
121            }
122            $encoded .= $this->base64Encode( $digit );
123        } while ( $vlq > 0 );
124
125        return $encoded;
126    }
127
128    /**
129     * Return the value decoded from base 64 VLQ.
130     *
131     * @param string $encoded The encoded value to decode
132     * @return int The decoded value
133     */
134    public function decode( $encoded ) {
135        $vlq = 0;
136        $i = 0;
137        do {
138            $digit = $this->base64Decode( $encoded[$i] );
139            $vlq |= ( $digit & $this->mask ) << ( $i * $this->shift );
140            $i++;
141        } while ( $digit & $this->continuationBit );
142
143        return $this->fromVLQSigned( $vlq );
144    }
145
146    /**
147     * Right shift with zero fill.
148     *
149     * @param int $a number to shift
150     * @param int $b number of bits to shift
151     * @return int
152     */
153    public function zeroFill( $a, $b ) {
154        return ( $a >= 0 ) ? ( $a >> $b ) : ( $a >> $b ) & ( PHP_INT_MAX >> ( $b - 1 ) );
155    }
156
157    /**
158     * Encode single 6-bit digit as base64.
159     *
160     * @param int $number
161     * @return string
162     * @throws Exception If the number is invalid
163     */
164    public function base64Encode( $number ) {
165        if ( $number < 0 || $number > 63 ) {
166            throw new Exception( "Invalid number \"$number\" given. Must be between 0 and 63." );
167        }
168        return $this->intToCharMap[$number];
169    }
170
171    /**
172     * Decode single 6-bit digit from base64
173     *
174     * @param string $char
175     * @return int
176     * @throws Exception If the number is invalid
177     */
178    public function base64Decode( $char ) {
179        if ( !array_key_exists( $char, $this->charToIntMap ) ) {
180            throw new Exception( sprintf( 'Invalid base 64 digit "%s" given.', $char ) );
181        }
182        return $this->charToIntMap[$char];
183    }
184
185}