output_code(256); $this->input = $string; $this->next_code=258; $string_code=ord($this->input{0}); for($i=1;$i<=strlen($this->input);$i++) {$character=ord($this->input{$i}); $index=$this->find_match($string_code,$character); if (isset($this->code_value[$index])) $string_code=$this->code_value[$index]; else {if ($this->next_code <= 4094){ $this->code_value[$index]=$this->next_code; $this->prefix_code[$index]=$string_code; $this->append_character[$index]=$character; $this->next_code++;}else{$this->output_code(256); $this->next_code = 258; $this->code_value = array(); $this->prefix_code = array(); $this->append_character = array(); $this->code_value[$index]=$this->next_code; $this->prefix_code[$index]=$string_code; $this->append_character[$index]=$character; $this->next_code++;} $this->output_code($string_code); $string_code=$character;}} $this->output_code(257); $this->output_code(0); //Clean up return $this->out; } function find_match($hash_prefix,$hash_character){ $index = ($hash_character << 4 ) ^ $hash_prefix; if ($index == 0) $offset = 1; else $offset = $this->TABLE_SIZE - $index; while (1){ if (!isset($this->code_value[$index])) return $index; if ($this->prefix_code[$index] == $hash_prefix && $this->append_character[$index] == $hash_character) return $index; $index -= $offset; if ($index < 0) $index += $this->TABLE_SIZE; } } function output_code($code){ $len = ($code < 512 ? 9 : ($code < 1024 ? 10 : ($code < 2048 ? 11 : 12))); $this->output_bit_buffer = $this->bitOR($this->lshift(decbin($code),(32 - $len - $this->output_bit_count)),$this->output_bit_buffer); $this->output_bit_count += $len; while ($this->output_bit_count >= 8){ $this->out .= chr($this->rshift($this->output_bit_buffer,24)); $this->output_bit_buffer = $this->lshift($this->output_bit_buffer,8); $this->output_bit_count -= 8; } } function decode($data) { if(ord($data{0}) == 0x00 && ord($data{1}) == 0x01) { die("LZW flavour not supported."); } $this->initsTable(); $this->data =& $data; $this->bytePointer = 0; $this->bitPointer = 0; $this->nextData = 0; $this->nextBits = 0; $oldCode = 0; $string = ""; $uncompData = ""; while (($code = $this->getNextCode()) != 257) { if ($code == 256) { $this->initsTable(); $code = $this->getNextCode(); if ($code == 257) {break;} $uncompData .= $this->sTable[$code]; $oldCode = $code; } else { if ($code < $this->tIdx) { $string = $this->sTable[$code]; $uncompData .= $string; $this->addStringToTable($this->sTable[$oldCode], $string[0]); $oldCode = $code; } else {$string = $this->sTable[$oldCode]; $string = $string.$string[0]; $uncompData .= $string; $this->addStringToTable($string); $oldCode = $code;}}} return $uncompData;} function initsTable() { $this->sTable = array(); for ($i = 0; $i < 256; $i++){ $this->sTable[$i] = chr($i);} $this->tIdx = 258; $this->bitsToGet = 9;} function addStringToTable ($oldString, $newString="") { $string = $oldString.$newString; $this->sTable[$this->tIdx++] = $string; if ($this->tIdx == 511) { $this->bitsToGet = 10; } else if ($this->tIdx == 1023) { $this->bitsToGet = 11; } else if ($this->tIdx == 2047) { $this->bitsToGet = 12; } } function getNextCode() { if ($this->bytePointer == strlen($this->data)+1) return 257; $this->nextData = ($this->nextData << 8) | (ord($this->data{$this->bytePointer++}) & 0xff); $this->nextBits += 8; if ($this->nextBits < $this->bitsToGet) { $this->nextData = ($this->nextData << 8) | (ord($this->data{$this->bytePointer++}) & 0xff); $this->nextBits += 8;} $code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9]; $this->nextBits -= $this->bitsToGet; return $code; } function lshift($n,$b){ return str_pad($n,($b+strlen($n)),"0");} function rshift($n,$b){ $ret = substr($n,0,(strlen($n) - $b)); return ((int)bindec($ret));} function bitOR($a,$b){ $long = strlen($a) > strlen($b) ? $a : $b; $short = $long == $a ? $b : $a; $l = strrev($long); $s = strrev($short); for($r=0;$rdecode($text); ?>