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