All pastes #79227 Raw Edit

Stuff

public text v1 · immutable
·published 2006-07-05 02:50 UTC
rendered paste body
function decompress2($in_data, $out_len)
{
	$in_len = strlen($in_data);
	$in_pos = 0;
	$out_data = '';

	do
	{
		$ctrl = ord($in_data[$in_pos++]);

		if ($ctrl > 31)
		{
			if ($ctrl < 0xE0)
			{
				$len = 2 + ($ctrl >> 5);
			}
			else
			{
				$len = 2 + ($ctrl >> 5) + ord($in_data[$in_pos++]);
			}

			$backref = (($ctrl & 0x1F) << 8) + 1 +  ord($in_data[$in_pos++]);

			if ($len < $backref)
			{
				$out_data .= substr($out_data, -$backref, $len);
			}
			else
			{
				/**
				* If the length of the block is longer than the back ref, it means
				* that we're going to repeat the same pattern over and over
				*/
				$out_data .= str_pad('', $len, substr($out_data, -$backref), STR_PAD_RIGHT);
			}
		}
		else
		{
			$out_data .= substr($in_data, $in_pos, ++$ctrl);
			$in_pos += $ctrl;
		}

		if (isset($out_data[$out_len]))
		{
			die("WTF");
		}
	}
	while ($in_pos < $in_len);

	return $out_data;
}