stream = $stream; } // }}} // {{{ public static function Khex2bin() /** * hex to bin * * @param string $string * @static * @access protected * @return string (raw) */ public static function Khex2bin($string) { if (function_exists('\hex2bin')) { return \hex2bin($string); } else { $bin = ''; $len = strlen($string); for ($i = 0; $i < $len; $i += 2) { $bin .= pack('H*', substr($string, $i, 2)); } return $bin; } } // }}} // {{{ public static function unpack() /** * Unpack a bit integer as big endian long * * @static * @access public * @return integer */ public static function unpack($type, $bytes) { self::checkLen($type, $bytes); if ($type == self::BIT_B64) { $set = unpack($type, $bytes); $original = ($set[1] & 0xFFFFFFFF) << 32 | ($set[2] & 0xFFFFFFFF); return $original; } else { return unpack($type, $bytes); } } // }}} // {{{ public static function pack() /** * pack a bit integer as big endian long * * @static * @access public * @return integer */ public static function pack($type, $data) { if ($type == self::BIT_B64) { if ($data == -1) { // -1L $data = self::Khex2bin('ffffffffffffffff'); } elseif ($data == -2) { // -2L $data = self::Khex2bin('fffffffffffffffe'); } else { $left = 0xffffffff00000000; $right = 0x00000000ffffffff; $l = ($data & $left) >> 32; $r = $data & $right; $data = pack($type, $l, $r); } } else { $data = pack($type, $data); } return $data; } // }}} // {{{ protected static function checkLen() /** * check unpack bit is valid * * @param string $type * @param string(raw) $bytes * @static * @access protected * @return void */ protected static function checkLen($type, $bytes) { $len = 0; switch($type) { case self::BIT_B64: $len = 8; break; case self::BIT_B32: $len = 4; break; case self::BIT_B16: $len = 2; break; case self::BIT_B8: $len = 1; break; } if (strlen($bytes) != $len) { throw new \Kafka\Exception\Protocol('unpack failed. string(raw) length is ' . strlen($bytes) . ' , TO ' . $type); } } // }}} // }}} }