00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 final class Binary
00026 {
00027 static public function uint16($str, $pos=0)
00028 {
00029 return ord($str{$pos+0}) << 8 | ord($str{$pos+1});
00030 }
00031
00032 static public function uint32($str, $pos=0)
00033 {
00034 $a = unpack('Nx', substr($str, $pos, 4));
00035 return $a['x'];
00036 }
00037
00038 static public function nuint32($n, $str, $pos=0)
00039 {
00040 $r = array();
00041 for ($i = 0; $i < $n; $i++, $pos += 4)
00042 $r[] = Binary::uint32($str, $pos);
00043 return $r;
00044 }
00045
00046 static public function fuint32($f) { return Binary::uint32(fread($f, 4)); }
00047 static public function nfuint32($n, $f) { return Binary::nuint32($n, fread($f, 4*$n)); }
00048
00049 static public function git_varint($str, &$pos=0)
00050 {
00051 $r = 0;
00052 $c = 0x80;
00053 for ($i = 0; $c & 0x80; $i += 7)
00054 {
00055 $c = ord($str{$pos++});
00056 $r |= (($c & 0x7F) << $i);
00057 }
00058 return $r;
00059 }
00060 }
00061
00062 ?>