All pastes #637429 Raw Edit

Someone

public text v1 · immutable
#637429 ·published 2007-07-28 21:50 UTC
rendered paste body
function parse_variable(&$data) {
	$variable = null;

	list($data_type, $data) = slice($data, 1);
	if (ord($data_type) == 1) {		// Nobody connected
		$variable = "0 - Nobody connected";
	}
	if (ord($data_type) == 4) {		// 32 bit signed value following
		list($value, $data) = slice($data, 4);
		list(, $variable) = unpack("l", $value);
	}
	if (ord($data_type) == 5) {		// 16 bit signed value following
		list($value, $data) = slice($data, 2);
		list(, $variable) = unpack("s", $value);
	}
	if (ord($data_type) == 6) {		// 8 bit signed value following
		list($value, $data) = slice($data, 1);
		list(, $variable) = unpack("c", $value);
	}
	if (ord($data_type) == 8) {		// 0 value
		$variable = 0;
	}
	if (ord($data_type) == 9) {		// 1 value
		$variable = 1;
	}
	if (ord($data_type) == 10) {	// Double precision floating point value following
		list($value, $data) = slice($data, 8);
		list(, $variable) = unpack("d", $value);
	}
	if (ord($data_type) == 16 || ord($data_type) == 46) {	// String, next byte is length, then string content
		list($value, $data) = slice($data, 1);
		list(, $string_length) = unpack("C", $value);
		list($variable, $data) = slice($data, $string_length);
	}
	if (ord($data_type) == 17) {	// --- some variable, looks like it is followed by an 8 bit value
		list($value, $data) = slice($data, 1);
		list(, $variable) = unpack("c", $value);
	}
	if (ord($data_type) == 20) {	// Python tuple, next byte is items in the object
		list($value, $data) = slice($data, 1);
		list(, $variable) = unpack("C", $value);
	}
	if (ord($data_type) == 22) {	// Array
		list($value, $data) = slice($data, 1);
		list(, $array_size) = unpack("C", $value);
		$variable = array();
		for ($i = 0; $i < $array_size; $i++) {
			$value = parse_variable($data);
			$key = parse_variable($data);
			$variable[$key] = $value;
		}
	}
	if (ord($data_type) == 23) {	// --- some python thingy ---
		$variable = "Exception object?";
	}
	if (ord($data_type) == 47) {	// --- variable length clock thingy ---
		list($value, $data) = slice($data, 1);
		list(, $string_length) = unpack("C", $value);
		list($value, $data) = slice($data, $string_length);
		if ($string_length == 1) {
			list(, $variable) = unpack("C", $value);
		} else if ($string_length == 2) {
			list(, $variable) = unpack("S", $value);
		} else if ($string_length == 4) {
			list(, $variable) = unpack("L", $value);
		} else if ($string_length == 8) {
			list($value1, $value2) = slice($value, 4);
			list(, $variable1) = unpack("L", $value1);
			list(, $variable2) = unpack("L", $value2);
			$variable = $variable1."-".$variable2;
		}
//		for ($i = 0; $i < strlen($value); $i++) {
//			$variable .= sprintf("\\x%02X", ord($value[$i]));
//		}
	}
	if (ord($data_type) == 126) {	// Marshal stream, next 4 bytes is a 32 bit checksum
		list($value, $data) = slice($data, 4);
		list(, $variable) = unpack("V", $value);
	}
	
	if (is_null($variable)) {
		echo "--- Unknown variable type (".ord($data_type).")---\n";
		debug_binary($data);
	}

	return $variable;
}