Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Docs

  • Loading branch information...
commit 3a14c5547f2ef43dc3413bff0dc122606fa7a843 1 parent f6af40b
@kakserpom authored
Showing with 177 additions and 19 deletions.
  1. +177 −19 lib/Binary.php
View
196 lib/Binary.php
@@ -1,5 +1,10 @@
<?php
class Binary {
+ /**
+ * Build structure of labels
+ * @param string Dot-separated labels list
+ * @return binary
+ */
public static function labels($q) {
$e = explode('.', $q);
$r = '';
@@ -11,8 +16,14 @@ public static function labels($q) {
}
return $r;
}
+
+ /**
+ * Parse structure of labels
+ * @param binary
+ * @return string Dot-separated labels list
+ */
public static function parseLabels(&$data, $orig = null) {
- $domain = '';
+ $str = '';
while (strlen($data) > 0) {
$l = ord($data[0]);
@@ -20,74 +31,145 @@ public static function parseLabels(&$data, $orig = null) {
$pos = Binary::bytes2int(chr($l - 192) . binarySubstr($data, 1, 1));
$data = binarySubstr($data, 2);
$ref = binarySubstr($orig, $pos);
- return $domain . Binary::parseLabels($ref);
+ return $str . Binary::parseLabels($ref);
}
$p = substr($data, 1, $l);
- $domain .= $p.(($l !== 0)?'.':'');
+ $str .= $p.(($l !== 0)?'.':'');
$data = substr($data, $l + 1);
if ($l === 0) {
break;
}
}
- return $domain;
+ return $str;
}
- public static function LV($string, $len = 1, $lrev = FALSE) {
- $l = self::i2b($len, strlen($string));
+
+ /**
+ * Build length-value binary snippet
+ * @param string Data
+ * @param [string Number of bytes to encode length. Default is 1
+ * @return binary
+ */
+ public static function LV($str, $len = 1, $lrev = FALSE) {
+ $l = self::i2b($len, strlen($str));
if ($lrev) {
$l = strrev($l);
}
- return $l . $string;
+ return $l . $str;
}
- public static function LVnull($string) {
- return self::LV($string."\x00",2,TRUE);
+
+ /**
+ * Build nul-terminated string, with 2-byte of length
+ * @param string Data
+ * @return binary
+ */
+ public static function LVnull($str) {
+ return self::LV($str."\x00", 2, true);
}
+
+ /**
+ * Build byte
+ * @param integer Byte number
+ * @return binary
+ */
public static function byte($int) {
return chr($int);
}
+ /**
+ * Build word (2 bytes) big-endian
+ * @param integer Integer
+ * @return binary
+ */
public static function word($int) {
return self::i2b(2, $int);
}
+ /**
+ * Build word (2 bytes) little-endian
+ * @param integer Integer
+ * @return binary
+ */
public static function wordl($int) {
return strrev(self::word($int));
}
+ /**
+ * Build double word (4 bytes) big-endian
+ * @param integer Integer
+ * @return binary
+ */
public static function dword($int) {
return self::i2b(4,$int);
}
+ /**
+ * Build double word (4 bytes) little endian
+ * @param integer Integer
+ * @return binary
+ */
public static function dwordl($int) {
return strrev(self::dword($int));
}
+ /**
+ * Build quadro word (8 bytes) big endian
+ * @param integer Integer
+ * @return binary
+ */
public static function qword($int) {
return self::i2b(8, $int);
}
+ /**
+ * Build quadro word (8 bytes) little endian
+ * @param integer Integer
+ * @return binary
+ */
public static function qwordl($int) {
return strrev(self::qword($int));
}
+ /**
+ * Parse byte, and remove it
+ * @param &string Data
+ * @return integer
+ */
public static function getByte(&$p) {
$r = self::bytes2int($p{0});
$p = binarySubstr($p, 1);
return (int) $r;
}
+ /**
+ * Get single-byte character
+ * @param &string Data
+ * @return string
+ */
public static function getChar(&$p) {
$r = $p{0};
$p = binarySubstr($p, 1);
return $r;
}
-
+
+ /**
+ * Parse word (2 bytes)
+ * @param &string Data
+ * @param boolean Little endian?
+ * @return integer
+ */
public static function getWord(&$p, $l = false) {
- $r = self::bytes2int(binarySubstr($p,0,2),!!$l);
+ $r = self::bytes2int(binarySubstr($p, 0, 2), !!$l);
$p = binarySubstr($p,2);
return intval($r);
}
+ /**
+ * Get word (2 bytes)
+ * @param &string Data
+ * @param boolean Little endian?
+ * @return binary
+ */
public static function getStrWord(&$p, $l = false) {
$r = binarySubstr($p, 0, 2);
$p = binarySubstr($p, 2);
@@ -97,17 +179,38 @@ public static function getStrWord(&$p, $l = false) {
return $r;
}
+
+ /**
+ * Get double word (4 bytes)
+ * @param &string Data
+ * @param boolean Little endian?
+ * @return integer
+ */
public static function getDWord(&$p,$l = false) {
$r = self::bytes2int(binarySubstr($p,0,4),!!$l);
$p = binarySubstr($p,4);
return intval($r);
}
+
+ /**
+ * Parse quadro word (8 bytes)
+ * @param &string Data
+ * @param boolean Little endian?
+ * @return integer
+ */
public static function getQword(&$p, $l = false) {
$r = self::bytes2int(binarySubstr($p,0,8),!!$l);
$p = binarySubstr($p,8);
return intval($r);
}
- public static function getStrQWord(&$p,$l = false) {
+
+ /**
+ * Get quadro word (8 bytes)
+ * @param &string Data
+ * @param boolean Little endian?
+ * @return binary
+ */
+ public static function getStrQWord(&$p, $l = false) {
$r = binarySubstr($p, 0, 8);
if ($l) {
$r = strrev($r);
@@ -115,18 +218,32 @@ public static function getStrQWord(&$p,$l = false) {
$p = binarySubstr($p, 8);
return $r;
}
+
+ /**
+ * Parse nul-terminated string
+ * @param &string Data
+ * @return binary
+ */
public static function getString(&$str) {
- $p = strpos($str,"\x00");
- if ($p === FALSE) {
+ $p = strpos($str, "\x00");
+ if ($p === false) {
return '';
}
- $r = substr($str,0,$p);
- $str = substr($str, $p+1);
+ $r = binarySubstr($str, 0, $p);
+ $str = binarySubstr($str, $p+1);
return $r;
}
+
+ /**
+ * Parse length-value structure
+ * @param &string Data
+ * @param number Number of length bytes
+ * @param boolean Nul-terminated? Default is false
+ * @param boolean Length is little endian?
+ * @return string
+ */
public static function getLV(&$p, $l = 1, $nul = false, $lrev = false) {
$s = self::b2i(binarySubstr($p,0,$l),!!$lrev);
- //Daemon::log('s = '.$s. ' -- '.Debug::exportBytes(binarySubstr($p,0,$l), true));
$p = binarySubstr($p,$l);
if ($s == 0) {
return '';
@@ -155,6 +272,13 @@ public static function getLV(&$p, $l = 1, $nul = false, $lrev = false) {
return $r;
}
+
+ /**
+ * Convert integer into binary bytes
+ * @param number Number of length bytes
+ * @param integer
+ * @return string
+ */
public static function int2bytes($len,$int = 1) {
$hexstr = dechex($int);
if ($len === NULL) {
@@ -173,6 +297,13 @@ public static function int2bytes($len,$int = 1) {
return $bin;
}
+
+ /**
+ * Convert array of flags into bit array
+ * @param array Flags
+ * @param integer Length. Default is 4
+ * @return string
+ */
public static function flags2bitarray($flags, $len = 4) {
$ret = 0;
foreach($flags as $v) {
@@ -180,10 +311,21 @@ public static function flags2bitarray($flags, $len = 4) {
}
return self::i2b($len,$ret);
}
+
+ /**
+ * @alias int2bytes
+ */
public static function i2b($bytes, $val = 0) {
return self::int2bytes($bytes, $val);
}
-
+
+
+ /**
+ * Convert bytes into integer
+ * @param binary Bytes
+ * @param boolean Little endian? Default is false
+ * @return integer
+ */
public static function bytes2int($str, $l = false) {
if ($l) {
$str = strrev($str);
@@ -195,11 +337,21 @@ public static function bytes2int($str, $l = false) {
}
return $dec;
}
-
+
+ /**
+ * @alias bytes2int
+ */
public static function b2i($hex = 0, $l = false) {
return self::bytes2int($hex, $l);
}
+
+ /**
+ * Convert bitmap into bytes
+ * @param string Bitmap
+ * @param boolean Check length?
+ * @return binary
+ */
public static function bitmap2bytes($bitmap, $check_len = 0) {
$r = '';
$bitmap = str_pad($bitmap, ceil(strlen($bitmap) / 8) * 8,'0',STR_PAD_LEFT);
@@ -211,6 +363,12 @@ public static function bitmap2bytes($bitmap, $check_len = 0) {
}
return $r;
}
+
+ /**
+ * Get bitmap
+ * @param byte
+ * @return string
+ */
public static function getbitmap($byte) {
return sprintf('%08b',$byte);
}
Please sign in to comment.
Something went wrong with that request. Please try again.