Subversion Repositories oidplus

Rev

Rev 1192 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * binary.class.php:
  5.  * Utility functions for dealing with binary files/strings.
  6.  * All functions assume network byte order (big-endian).
  7.  *
  8.  * Copyright (C) 2008, 2009 Patrik Fimml
  9.  * Copyright (c) 2023 Daniel Marschall
  10.  *
  11.  * This file is part of glip.
  12.  *
  13.  * glip is free software: you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation, either version 2 of the License, or
  16.  * (at your option) any later version.
  17.  
  18.  * glip is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with glip.  If not, see <http://www.gnu.org/licenses/>.
  25.  */
  26.  
  27. namespace ViaThinkSoft\Glip;
  28.  
  29. final class Binary
  30. {
  31.         // 16 bit
  32.  
  33.         static public function uint16($str, $pos = 0) {
  34.                 return ord($str[$pos + 0]) << 8 | ord($str[$pos + 1]);
  35.         }
  36.  
  37.         // 32 bit
  38.  
  39.         static public function uint32($str, $pos = 0) {
  40.                 $a = unpack('Nx', substr($str, $pos, 4));
  41.                 return $a['x'];
  42.         }
  43.  
  44.         static public function nuint32($n, $str, $pos = 0) {
  45.                 $r = array();
  46.                 for ($i = 0; $i < $n; $i++, $pos += 4)
  47.                         $r[] = Binary::uint32($str, $pos);
  48.                 return $r;
  49.         }
  50.  
  51.         static public function fuint32($f) {
  52.                 return Binary::uint32(fread($f, 4));
  53.         }
  54.  
  55.         static public function nfuint32($n, $f) {
  56.                 return Binary::nuint32($n, fread($f, 4 * $n));
  57.         }
  58.  
  59.         // 64 bit
  60.  
  61.         static public function uint64($str, $pos = 0) {
  62.                 $a = unpack('Jx', substr($str, $pos, 8));
  63.                 return $a['x'];
  64.         }
  65.  
  66.         static public function nuint64($n, $str, $pos = 0) {
  67.                 $r = array();
  68.                 for ($i = 0; $i < $n; $i++, $pos += 8)
  69.                         $r[] = Binary::uint64($str, $pos);
  70.                 return $r;
  71.         }
  72.  
  73.         static public function fuint64($f) {
  74.                 return Binary::uint64(fread($f, 8));
  75.         }
  76.  
  77.         static public function nfuint64($n, $f) {
  78.                 return Binary::nuint64($n, fread($f, 8 * $n));
  79.         }
  80.  
  81.         // other
  82.  
  83.         static public function git_varint($str, &$pos = 0) {
  84.                 $r = 0;
  85.                 $c = 0x80;
  86.                 for ($i = 0; $c & 0x80; $i += 7) {
  87.                         $c = ord($str[$pos++]);
  88.                         $r |= (($c & 0x7F) << $i);
  89.                 }
  90.                 return $r;
  91.         }
  92. }
  93.