Issues (25)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/LZX/Tree.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 483
    public function __construct($bits, $maxSymbol)
62
    {
63 483
        $this->bits = $bits;
64 483
        $this->maxSymbol = $maxSymbol;
65 483
        $this->symbols = array_fill(0, (1 << $this->bits) + ($this->maxSymbol << 1), 0);
66 483
        $this->lens = array_fill(0, $this->maxSymbol + static::LENTABLE_SAFETY, 0);
67 483
    }
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 26
    public function makeSymbolTable()
77
    {
78 26
        $bitNum = 1;
79 26
        $pos = 0;
80 26
        $tableMask = 1 << $this->bits;
81 26
        $bitMask = $tableMask >> 1;
82 26
        $nextSymbol = $bitMask;
83 26
        while ($bitNum <= $this->bits) {
84 26
            for ($symbol = 0; $symbol < $this->maxSymbol; ++$symbol) {
85 26
                if ($this->lens[$symbol] === $bitNum) {
86 26
                    $leaf = $pos;
87 26
                    $pos += $bitMask;
88 26
                    if ($pos > $tableMask) {
89
                        throw new Exception('Symbol table overruns');
90
                    }
91 26
                    while ($leaf < $pos) {
92 26
                        $this->symbols[$leaf++] = $symbol;
93
                    }
94
                }
95
            }
96 26
            $bitMask >>= 1;
97 26
            ++$bitNum;
98
        }
99 26
        if ($pos !== $tableMask) {
100 26
            for ($i = $pos; $i < $tableMask; ++$i) {
101 26
                $this->symbols[$i] = 0;
102
            }
103 26
            $pos <<= 16;
104 26
            $tableMask <<= 16;
105 26
            $bitMask = 1 << 15;
106 26
            while ($bitNum <= 16) {
107 26
                for ($symbol = 0; $symbol < $this->maxSymbol; ++$symbol) {
108 26
                    if ($this->lens[$symbol] === $bitNum) {
109 26
                        $leaf = $pos >> 16;
110 26
                        for ($fill = 0; $fill < $bitNum - $this->bits; ++$fill) {
111 26
                            if ($this->symbols[$leaf] === 0) {
112 26
                                $nextSymbol2 = $nextSymbol << 1;
113 26
                                $this->symbols[$nextSymbol2] = 0;
114 26
                                $this->symbols[$nextSymbol2 + 1] = 0;
115 26
                                $this->symbols[$leaf] = $nextSymbol++;
116
                            }
117 26
                            $leaf = $this->symbols[$leaf] << 1;
118 26
                            if ((($pos >> (15 - $fill)) & 1) !== 0) {
119 26
                                ++$leaf;
120
                            }
121
                        }
122 26
                        $this->symbols[$leaf] = $symbol;
123 26
                        $pos += $bitMask;
124 26
                        if ($pos > $tableMask) {
125
                            throw new Exception('Symbol table overflow');
126
                        }
127
                    }
128
                }
129 26
                $bitMask >>= 1;
130 26
                ++$bitNum;
131
            }
132
        }
133 26
        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 26
    }
141
142
    /**
143
     * Read the align lengths data.
144
     *
145
     * @param \CHMLib\Reader\BitReader $reader The reader that provides the data.
146
     *
147
     * @throws \Exception Throws an Exception in case of errors.
148
     */
149 6
    public function readAlignLengthTable(BitReader $reader)
150
    {
151 6 View Code Duplication
        for ($i = 0; $i < $this->maxSymbol; ++$i) {
0 ignored issues
show
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 6
            $this->lens[$i] = $reader->readLE(3);
153
        }
154 6
    }
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 \CHMLib\Reader\BitReader $reader The reader that provides the data.
161
     * @param int $first
162
     * @param int $last
163
     */
164 26
    public function readLengthTable(BitReader $reader, $first, $last)
165
    {
166 26
        $preTree = new self(6, static::PRETREE_NUM_ELEMENTS);
167 26 View Code Duplication
        for ($i = 0; $i < $preTree->maxSymbol; ++$i) {
0 ignored issues
show
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 26
            $preTree->lens[$i] = $reader->readLE(4);
169
        }
170 26
        $preTree->makeSymbolTable();
171 26
        for ($pos = $first; $pos < $last;) {
172 26
            $symbol = $preTree->readHuffmanSymbol($reader);
173
            switch ($symbol) {
174 26 View Code Duplication
                case 0x11:
0 ignored issues
show
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 26
                    $pos2 = $pos + $reader->readLE(4) + 4;
176 26
                    while ($pos < $pos2) {
177 26
                        $this->lens[$pos++] = 0;
178
                    }
179 26
                    break;
180 26 View Code Duplication
                case 0x12:
0 ignored issues
show
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 26
                    $pos2 = $pos + $reader->readLE(5) + 20;
182 26
                    while ($pos < $pos2) {
183 26
                        $this->lens[$pos++] = 0;
184
                    }
185 26
                    break;
186 26
                case 0x13:
187 25
                    $pos2 = $pos + $reader->readLE(1) + 4;
188 25
                    $symbol = $this->lens[$pos] - $preTree->readHuffmanSymbol($reader);
189 25
                    if ($symbol < 0) {
190 25
                        $symbol += 0x11;
191
                    }
192 25
                    while ($pos < $pos2) {
193 25
                        $this->lens[$pos++] = $symbol;
194
                    }
195 25
                    break;
196
                default:
197 26
                    $symbol = $this->lens[$pos] - $symbol;
198 26
                    if ($symbol < 0) {
199 26
                        $symbol += 0x11;
200
                    }
201 26
                    $this->lens[$pos++] = $symbol;
202 26
                    break;
203
            }
204
        }
205 26
    }
206
207
    /**
208
     * Decode a Huffman symbol from the bitstream using the stated table and return it.
209
     *
210
     * @param \CHMLib\Reader\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 26
    public function readHuffmanSymbol(BitReader $reader)
217
    {
218 26
        $next = $reader->peek(16, true);
219 26
        $symbol = $this->symbols[$reader->peek($this->bits, true)];
220 26
        if ($symbol >= $this->maxSymbol) {
221 26
            $j = 1 << (16 - $this->bits);
222
            do {
223 26
                $j >>= 1;
224 26
                $symbol <<= 1;
225 26
                $symbol |= ($next & $j) > 0 ? 1 : 0;
226 26
                $symbol = $this->symbols[$symbol];
227 26
            } while ($symbol >= $this->maxSymbol);
228
        }
229 26
        $reader->readLE($this->lens[$symbol]);
230
231 26
        return $symbol;
232
    }
233
234
    /**
235
     * Clear the code lengths table.
236
     */
237 26
    public function clear()
238
    {
239 26
        $count = count($this->lens);
240 26
        $this->lens = array_fill(0, $count, 0);
241 26
    }
242
243
    /**
244
     * Check if the length at 0xe8 is not zero.
245
     *
246
     * @return bool
247
     */
248 26
    public function isIntel()
249
    {
250 26
        return $this->lens[0xe8] !== 0;
251
    }
252
}
253