Esempio n. 1
0
$f->Add('S', 5);
$f->Add('T', 7);
$f->Add('U', 4);
$f->Add('V', 2);
$f->Add('W', 2);
$f->Add('X', 1);
$f->Add('Y', 2);
$f->Add('Z', 1);
$binary = HuffmanStream::GetBinaryStream($f, 'INFORMATIONISTHES');
//echo strlen($binary);exit;
//echo '<br>';
$base64 = Base64::Encode($binary);
echo $base64;
echo '<br>';
$f->Reset();
echo HuffmanStream::GetSymbolStream($f, Base64::Decode($base64));
class HuffmanStream
{
    var $frequency_table;
    public static function GetBinaryStream($frequencies, $symbol_stream)
    {
        $stream = '';
        $length = strlen($symbol_stream);
        for ($index = 0; $index < $length; $index++) {
            $codes = Huffman::GetCodes($frequencies->AsTable());
            $symbol = substr($symbol_stream, $index, 1);
            $stream .= $codes[$symbol];
            $frequencies->Adjust($symbol, -1);
        }
        return $stream;
    }