You are here

private function Decoder::decodeBigUint in Smart IP 6.2

Same name and namespace in other branches
  1. 7.2 includes/vendor/maxmind-db/reader/src/MaxMind/Db/Reader/Decoder.php \MaxMind\Db\Reader\Decoder::decodeBigUint()
1 call to Decoder::decodeBigUint()
Decoder::decodeByType in includes/vendor/maxmind-db/reader/src/MaxMind/Db/Reader/Decoder.php

File

includes/vendor/maxmind-db/reader/src/MaxMind/Db/Reader/Decoder.php, line 232

Class

Decoder

Namespace

MaxMind\Db\Reader

Code

private function decodeBigUint($bytes, $byteLength) {
  $maxUintBytes = log(PHP_INT_MAX, 2) / 8;
  if ($byteLength == 0) {
    return 0;
  }
  $numberOfLongs = ceil($byteLength / 4);
  $paddedLength = $numberOfLongs * 4;
  $paddedBytes = $this
    ->zeroPadLeft($bytes, $paddedLength);
  $unpacked = array_merge(unpack("N{$numberOfLongs}", $paddedBytes));
  $integer = 0;

  // 2^32
  $twoTo32 = '4294967296';
  foreach ($unpacked as $part) {

    // We only use gmp or bcmath if the final value is too big
    if ($byteLength <= $maxUintBytes) {
      $integer = ($integer << 32) + $part;
    }
    elseif (extension_loaded('gmp')) {
      $integer = gmp_strval(gmp_add(gmp_mul($integer, $twoTo32), $part));
    }
    elseif (extension_loaded('bcmath')) {
      $integer = bcadd(bcmul($integer, $twoTo32), $part);
    }
    else {
      throw new \RuntimeException('The gmp or bcmath extension must be installed to read this database.');
    }
  }
  return $integer;
}