Completed
Push — master ( 368df1...25b1b2 )
by Michele
05:24 queued 02:41
created

Tree::isIntel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace CHMLib\LZX;
4
5
use Exception;
6
use CHMLib\Reader\BitReader;
7
8
/**
9
 * Huffman tree used in LZX decoding.
10
 */
11
class Tree
12
{
13
    /**
14
     * Table decoding overruns are allowed.
15
     *
16
     * @var int
17
     */
18
    const LENTABLE_SAFETY = 64;
19
20
    /**
21
     * Maximum number of symbols in the pre-tree.
22
     *
23
     * @var int
24
     */
25
    const PRETREE_NUM_ELEMENTS = 20;
26
27
    /**
28
     * The number of bits.
29
     *
30
     * @var int
31
     */
32
    protected $bits;
33
34
    /**
35
     * The maximum symbol.
36
     *
37
     * @var int
38
     */
39
    protected $maxSymbol;
40
41
    /**
42
     * The symbol list used to decode.
43
     *
44
     * @var int[]
45
     */
46
    protected $symbols;
47
48
    /**
49
     * The code lengths table.
50
     *
51
     * @var int[]
52
     */
53
    protected $lens;
54
55
    /**
56
     * Initialize the instance.
57
     *
58
     * @param int $bits The number of bits.
59
     * @param int $maxSymbol The maximum symbol.
60
     */
61 464
    public function __construct($bits, $maxSymbol)
62
    {
63 464
        $this->bits = $bits;
64 464
        $this->maxSymbol = $maxSymbol;
65 464
        $this->symbols = array_fill(0, (1 << $this->bits) + ($this->maxSymbol << 1), 0);
66 464
        $this->lens = array_fill(0, $this->maxSymbol + static::LENTABLE_SAFETY, 0);
67 464
    }
68
69
    /**
70
     * Build a fast Huffman decoding table out of just a canonical Huffman code lengths table.
71
     *
72
     * @throws Exception Throws an Exception in case of errors.
73
     *
74
     * @author This function was coded by David Tritscher.
75
     */
76 23
    public function makeSymbolTable()
77
    {
78 23
        $bitNum = 1;
79 23
        $pos = 0;
80 23
        $tableMask = 1 << $this->bits;
81 23
        $bitMask = $tableMask >> 1;
82 23
        $nextSymbol = $bitMask;
83 23
        while ($bitNum <= $this->bits) {
84 23
            for ($symbol = 0; $symbol < $this->maxSymbol; ++$symbol) {
85 23
                if ($this->lens[$symbol] === $bitNum) {
86 23
                    $leaf = $pos;
87 23
                    $pos += $bitMask;
88 23
                    if ($pos > $tableMask) {
89
                        throw new Exception('Symbol table overruns');
90
                    }
91 23
                    while ($leaf < $pos) {
92 23
                        $this->symbols[$leaf++] = $symbol;
93
                    }
94
                }
95
            }
96 23
            $bitMask >>= 1;
97 23
            ++$bitNum;
98
        }
99 23
        if ($pos !== $tableMask) {
100 23
            for ($i = $pos; $i < $tableMask; ++$i) {
101 23
                $this->symbols[$i] = 0;
102
            }
103 23
            $pos <<= 16;
104 23
            $tableMask <<= 16;
105 23
            $bitMask = 1 << 15;
106 23
            while ($bitNum <= 16) {
107 23
                for ($symbol = 0; $symbol < $this->maxSymbol; ++$symbol) {
108 23
                    if ($this->lens[$symbol] === $bitNum) {
109 23
                        $leaf = $pos >> 16;
110 23
                        for ($fill = 0; $fill < $bitNum - $this->bits; ++$fill) {
111 23
                            if ($this->symbols[$leaf] === 0) {
112 23
                                $nextSymbol2 = $nextSymbol << 1;
113 23
                                $this->symbols[$nextSymbol2] = 0;
114 23
                                $this->symbols[$nextSymbol2 + 1] = 0;
115 23
                                $this->symbols[$leaf] = $nextSymbol++;
116
                            }
117 23
                            $leaf = $this->symbols[$leaf] << 1;
118 23
                            if ((($pos >> (15 - $fill)) & 1) !== 0) {
119 23
                                ++$leaf;
120
                            }
121
                        }
122 23
                        $this->symbols[$leaf] = $symbol;
123 23
                        $pos += $bitMask;
124 23
                        if ($pos > $tableMask) {
125
                            throw new Exception('Symbol table overflow');
126
                        }
127
                    }
128
                }
129 23
                $bitMask >>= 1;
130 23
                ++$bitNum;
131
            }
132
        }
133 23
        if ($pos !== $tableMask) {
134
            for ($sym = 0; $sym < $this->maxSymbol; ++$sym) {
135
                if ($this->lens[$sym] !== 0) {
136
                    throw new Exception('Erroneous symbol table');
137
                }
138
            }
139
        }
140 23
    }
141
142
    /**
143
     * Read the align lengths data.
144
     *
145
     * @param BitReader $reader The reader that provides the data.
146
     *
147
     * @throws Exception Throws an Exception in case of errors.
148
     */
149 3
    public function readAlignLengthTable(BitReader $reader)
150
    {
151 3 View Code Duplication
        for ($i = 0; $i < $this->maxSymbol; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152 3
            $this->lens[$i] = $reader->readLE(3);
153
        }
154 3
    }
155
156
    /**
157
     * Read in code lengths for symbols first to last in the given table.
158
     * The code lengths are stored in their own special LZX way.
159
     *
160
     * @param BitReader $reader The reader that provides the data.
161
     * @param int $first
162
     * @param int $last
163
     */
164 23
    public function readLengthTable(BitReader $reader, $first, $last)
165
    {
166 23
        $preTree = new self(6, static::PRETREE_NUM_ELEMENTS);
167 23 View Code Duplication
        for ($i = 0; $i < $preTree->maxSymbol; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168 23
            $preTree->lens[$i] = $reader->readLE(4);
169
        }
170 23
        $preTree->makeSymbolTable();
171 23
        for ($pos = $first; $pos < $last;) {
172 23
            $symbol = $preTree->readHuffmanSymbol($reader);
173
            switch ($symbol) {
174 23 View Code Duplication
                case 0x11:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175 23
                    $pos2 = $pos + $reader->readLE(4) + 4;
176 23
                    while ($pos < $pos2) {
177 23
                        $this->lens[$pos++] = 0;
178
                    }
179 23
                    break;
180 23 View Code Duplication
                case 0x12:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181 23
                    $pos2 = $pos + $reader->readLE(5) + 20;
182 23
                    while ($pos < $pos2) {
183 23
                        $this->lens[$pos++] = 0;
184
                    }
185 23
                    break;
186 23
                case 0x13:
187 22
                    $pos2 = $pos + $reader->readLE(1) + 4;
188 22
                    $symbol = $this->lens[$pos] - $preTree->readHuffmanSymbol($reader);
189 22
                    if ($symbol < 0) {
190 22
                        $symbol += 0x11;
191
                    }
192 22
                    while ($pos < $pos2) {
193 22
                        $this->lens[$pos++] = $symbol;
194
                    }
195 22
                    break;
196
                default:
197 23
                    $symbol = $this->lens[$pos] - $symbol;
198 23
                    if ($symbol < 0) {
199 23
                        $symbol += 0x11;
200
                    }
201 23
                    $this->lens[$pos++] = $symbol;
202 23
                    break;
203
            }
204
        }
205 23
    }
206
207
    /**
208
     * Decode a Huffman symbol from the bitstream using the stated table and return it.
209
     *
210
     * @param BitReader $reader The reader that provides the data.
211
     *
212
     * @throws Exception Throws an Exception in case of errors.
213
     *
214
     * @return int
215
     */
216 23
    public function readHuffmanSymbol(BitReader $reader)
217
    {
218 23
        $next = $reader->peek(16, true);
219 23
        $symbol = $this->symbols[$reader->peek($this->bits, true)];
220 23
        if ($symbol >= $this->maxSymbol) {
221 23
            $j = 1 << (16 - $this->bits);
222
            do {
223 23
                $j >>= 1;
224 23
                $symbol <<= 1;
225 23
                $symbol |= ($next & $j) > 0 ? 1 : 0;
226 23
                $symbol = $this->symbols[$symbol];
227 23
            } while ($symbol >= $this->maxSymbol);
228
        }
229 23
        $reader->readLE($this->lens[$symbol]);
230
231 23
        return $symbol;
232
    }
233
234
    /**
235
     * Clear the code lengths table.
236
     */
237 23
    public function clear()
238
    {
239 23
        $count = count($this->lens);
240 23
        $this->lens = array_fill(0, $count, 0);
241 23
    }
242
243
    /**
244
     * Check if the length at 0xe8 is not zero.
245
     *
246
     * @return bool
247
     */
248 23
    public function isIntel()
249
    {
250 23
        return $this->lens[0xe8] !== 0;
251
    }
252
}
253