[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/fgrosse/phpasn1/lib/ASN1/ -> Base128.php (source)

   1  <?php
   2  
   3  namespace FG\ASN1;
   4  
   5  use FG\Utility\BigInteger;
   6  use InvalidArgumentException;
   7  
   8  /**
   9   * A base-128 decoder.
  10   */
  11  class Base128
  12  {
  13      /**
  14       * @param int $value
  15       *
  16       * @return string
  17       */
  18      public static function encode($value)
  19      {
  20          $value = BigInteger::create($value);
  21          $octets = chr($value->modulus(0x80)->toInteger());
  22  
  23          $value = $value->shiftRight(7);
  24          while ($value->compare(0) > 0) {
  25              $octets .= chr(0x80 | $value->modulus(0x80)->toInteger());
  26              $value = $value->shiftRight(7);
  27          }
  28  
  29          return strrev($octets);
  30      }
  31  
  32      /**
  33       * @param string $octets
  34       *
  35       * @throws InvalidArgumentException if the given octets represent a malformed base-128 value or the decoded value would exceed the the maximum integer length
  36       *
  37       * @return int
  38       */
  39      public static function decode($octets)
  40      {
  41          $bitsPerOctet = 7;
  42          $value = BigInteger::create(0);
  43          $i = 0;
  44  
  45          while (true) {
  46              if (!isset($octets[$i])) {
  47                  throw new InvalidArgumentException(sprintf('Malformed base-128 encoded value (0x%s).', strtoupper(bin2hex($octets)) ?: '0'));
  48              }
  49  
  50              $octet = ord($octets[$i++]);
  51  
  52              $l1 = $value->shiftLeft($bitsPerOctet);
  53              $r1 = $octet & 0x7f;
  54              $value = $l1->add($r1);
  55  
  56              if (0 === ($octet & 0x80)) {
  57                  break;
  58              }
  59          }
  60  
  61          return (string)$value;
  62      }
  63  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer