Source for file HuffmanExpand.php
Documentation is available at HuffmanExpand.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Markus Nix <mnix@docuverse.de> |
// | David Holmes <exaton@free.fr> (original version) |
// +----------------------------------------------------------------------+
require_once 'Text/Huffman.php';
* Huffman Expansion Class
* Size of the output file, in bytes
* For use in Huffman Tree reconstruction
// Initializing expansion-specific variables
throw new Exception ('Files not provided.');
// From header: reading Huffman tree (with no weights, mind you)
$this->_reconstructTree ();
// From header: number of characters to read (ie. size of output file)
// Reading bit-by-bit and generating output
$this->_readToMakeOutput ();
// Writing the output and closing resource handles
* Reconstruct the Huffman tree transmitted in header.
private function _readTPForChild ($par, $child, $childid, $charin)
// Creating child, setting right parent and right child for parent
$this->_nodes[$par][$child] = $childid;
$char = ($charin == $this->_nodeCharC)? '' : $charin;
$this->_nodes[$childid] = $node;
// Special business if we have a Branch Node
// Doing all of this for the child!
$this->_readTreePart ($childid);
private function _readTreePart ($nodenum)
// Reading from the header, creating a child
$this->_readTPForChild ($nodenum, '_child0', ++ $this->_ttlnodes, $charin);
$this->_readTPForChild ($nodenum, '_child1', ++ $this->_ttlnodes, $charin);
private function _reconstructTree ()
// Creating Root Node. Here root is indexed 0.
// It's parent is -1, it's children are as yet unknown.
// NOTE : weights no longer have the slightest importance here
// Launching the business
* Reading the compressed data bit-by-bit and generating the output.
* Huffman Compression has unique-prefix property, so as soon as
* we recognise a code, we can assume the corresponding char.
* All adding up, by reading $ofsize chars from the file, we should get
private function _readUntilLeaf ($curnode)
if ($curnode['_char'] !== '') {
return $curnode['_char'];
return $this->_readUntilLeaf ($this->_nodes[$curnode['_child1']]);
return $this->_readUntilLeaf ($this->_nodes[$curnode['_child0']]);
* We follow the Tree down from Root with the successive bits read
* We know we have found the character as soon as we hit a leaf Node.
private function _readToMakeOutput ()
for ($i = 0; $i < $this->_ofsize; $i++ ) {
Documentation generated on Mon, 11 Mar 2019 13:54:22 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|