[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Encode / Decode Base64 VLQ. 5 * 6 * @package Less 7 * @subpackage SourceMap 8 */ 9 class Less_SourceMap_Base64VLQ { 10 11 /** 12 * Shift 13 * 14 * @var integer 15 */ 16 private $shift = 5; 17 18 /** 19 * Mask 20 * 21 * @var integer 22 */ 23 private $mask = 0x1F; // == (1 << shift) == 0b00011111 24 25 /** 26 * Continuation bit 27 * 28 * @var integer 29 */ 30 private $continuationBit = 0x20; // == (mask - 1 ) == 0b00100000 31 32 /** 33 * Char to integer map 34 * 35 * @var array 36 */ 37 private $charToIntMap = array( 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 = array( 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 string $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 integer $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 string $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 { 118 $digit = $vlq & $this->mask; 119 $vlq = $this->zeroFill($vlq, $this->shift); 120 if($vlq > 0){ 121 $digit |= $this->continuationBit; 122 } 123 $encoded .= $this->base64Encode($digit); 124 } while($vlq > 0); 125 126 return $encoded; 127 } 128 129 /** 130 * Return the value decoded from base 64 VLQ. 131 * 132 * @param string $encoded The encoded value to decode 133 * @return integer The decoded value 134 */ 135 public function decode($encoded){ 136 $vlq = 0; 137 $i = 0; 138 do 139 { 140 $digit = $this->base64Decode($encoded[$i]); 141 $vlq |= ($digit & $this->mask) << ($i * $this->shift); 142 $i++; 143 } while($digit & $this->continuationBit); 144 145 return $this->fromVLQSigned($vlq); 146 } 147 148 /** 149 * Right shift with zero fill. 150 * 151 * @param integer $a number to shift 152 * @param integer $b number of bits to shift 153 * @return integer 154 */ 155 public function zeroFill($a, $b){ 156 return ($a >= 0) ? ($a >> $b) : ($a >> $b) & (PHP_INT_MAX >> ($b - 1)); 157 } 158 159 /** 160 * Encode single 6-bit digit as base64. 161 * 162 * @param integer $number 163 * @return string 164 * @throws Exception If the number is invalid 165 */ 166 public function base64Encode($number){ 167 if($number < 0 || $number > 63){ 168 throw new Exception(sprintf('Invalid number "%s" given. Must be between 0 and 63.', $number)); 169 } 170 return $this->intToCharMap[$number]; 171 } 172 173 /** 174 * Decode single 6-bit digit from base64 175 * 176 * @param string $char 177 * @return number 178 * @throws Exception If the number is invalid 179 */ 180 public function base64Decode($char){ 181 if(!array_key_exists($char, $this->charToIntMap)){ 182 throw new Exception(sprintf('Invalid base 64 digit "%s" given.', $char)); 183 } 184 return $this->charToIntMap[$char]; 185 } 186 187 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |