All pastes #79209 Raw Edit

Stuff

public text v1 · immutable
·published 2006-07-05 02:07 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)
		{
			$reference = (strlen($out_data) - (($ctrl & 0x1F) << 8) - 1);

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

			/**
			* For some reason, $reference can be equal to the last byte of the data.
			* We can't replace these lines with substr($out_data, $reference, 2) because
			* at the time it would be called, $out_data[$reference + 1] may not exist
			*/
			if (isset($out_data[$reference + $len]))
			{
				$out_data .= substr($out_data, $reference, $len);
				$reference += $len;
			}
			else
			{
				do
				{
					$out_data .= $out_data[$reference++];
				}
				while (--$len);
			}
		}
		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;
}