File modules/util/ui/minifier/Base64VLQ.class.php

Last commit: Sun Oct 13 13:17:32 2024 +0200	Jan Dankert	New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.
1 <?php 2 3 namespace util\ui\minifier; 4 5 /** 6 * Encode / Decode Base64 VLQ. 7 * 8 * @author bspot 9 */ 10 class Base64VLQ { 11 12 public static $SHIFT = 5; 13 public static $MASK = 0x1F; // == (1 << SHIFT) == 0b00011111 14 public static $CONTINUATION_BIT = 0x20; // == (MASK - 1 ) == 0b00100000 15 16 public static $CHAR_TO_INT = array(); 17 public static $INT_TO_CHAR = array(); 18 19 /** 20 * Convert from a two-complement value to a value where the sign bit is 21 * is placed in the least significant bit. For example, as decimals: 22 * 1 becomes 2 (10 binary), -1 becomes 3 (11 binary) 23 * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) 24 * We generate the value for 32 bit machines, hence 25 * -2147483648 becomes 1, not 4294967297, 26 * even on a 64 bit machine. 27 */ 28 public static function toVLQSigned($aValue) { 29 return 0xffffffff & ($aValue < 0 ? ((-$aValue) << 1) + 1 : ($aValue << 1) + 0); 30 } 31 32 /** 33 * Convert to a two-complement value from a value where the sign bit is 34 * is placed in the least significant bit. For example, as decimals: 35 * 2 (10 binary) becomes 1, 3 (11 binary) becomes -1 36 * 4 (100 binary) becomes 2, 5 (101 binary) becomes -2 37 * We assume that the value was generated with a 32 bit machine in mind. 38 * Hence 39 * 1 becomes -2147483648 40 * even on a 64 bit machine. 41 */ 42 public static function fromVLQSigned($aValue) { 43 return $aValue & 1 ? self::zeroFill(~$aValue+2, 1) | (-1 - 0x7fffffff) : self::zeroFill($aValue, 1); 44 } 45 46 /** 47 * Return the base 64 VLQ encoded value. 48 */ 49 public static function encode($aValue) { 50 $encoded = ""; 51 52 $vlq = self::toVLQSigned($aValue); 53 54 do { 55 $digit = $vlq & self::$MASK; 56 $vlq = self::zeroFill($vlq, self::$SHIFT); 57 if ($vlq > 0) { 58 $digit |= self::$CONTINUATION_BIT; 59 } 60 $encoded .= self::base64Encode($digit); 61 } while ($vlq > 0); 62 63 return $encoded; 64 } 65 66 /** 67 * Return the value decoded from base 64 VLQ. 68 */ 69 public static function decode($encoded) { 70 $vlq = 0; 71 72 $i = 0; 73 do { 74 $digit = self::base64Decode($encoded[$i]); 75 $vlq |= ($digit & self::$MASK) << ($i*self::$SHIFT); 76 $i++; 77 } while ($digit & self::$CONTINUATION_BIT); 78 79 return self::fromVLQSigned($vlq); 80 } 81 82 /** 83 * Right shift with zero fill. 84 * 85 * @param number $a number to shift 86 * @param nunber $b number of bits to shift 87 * @return number 88 */ 89 public static function zeroFill($a, $b) { 90 return ($a >= 0) ? ($a >> $b) : ($a >> $b) & (PHP_INT_MAX >> ($b-1)); 91 } 92 93 /** 94 * Encode single 6-bit digit as base64. 95 * 96 * @param number $number 97 * @return string 98 */ 99 public static function base64Encode($number) { 100 if ($number < 0 || $number > 63) { 101 throw new Exception("Must be between 0 and 63: " . $number); 102 } 103 return self::$INT_TO_CHAR[$number]; 104 } 105 106 /** 107 * Decode single 6-bit digit from base64 108 * 109 * @param string $char 110 * @return number 111 */ 112 public static function base64Decode($char) { 113 if (!array_key_exists($char, self::$CHAR_TO_INT)) { 114 throw new Exception("Not a valid base 64 digit: " . $char); 115 } 116 return self::$CHAR_TO_INT[$char]; 117 } 118 } 119 120 // Initialize char conversion table. 121 Base64VLQ::$CHAR_TO_INT = array(); 122 Base64VLQ::$INT_TO_CHAR = array(); 123 124 foreach (str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/') as $i => $char) { 125 Base64VLQ::$CHAR_TO_INT[$char] = $i; 126 Base64VLQ::$INT_TO_CHAR[$i] = $char; 127 }
Download modules/util/ui/minifier/Base64VLQ.class.php
History Sun, 13 Oct 2024 13:17:32 +0200 Jan Dankert New Minifier for CSS and JS: Leave JS Linebreaks as they are (for better debugging); Exploded CSS files (instead of a combined one), LESS is necessary but should be avoided in the future.