Completed
Push — develop ( febbe8...0477e6 )
by Adrien
25:31 queued 16:01
created

Root::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;
4
5
/* vim: set expandtab tabstop=4 shiftwidth=4: */
6
// +----------------------------------------------------------------------+
7
// | PHP Version 4                                                        |
8
// +----------------------------------------------------------------------+
9
// | Copyright (c) 1997-2002 The PHP Group                                |
10
// +----------------------------------------------------------------------+
11
// | This source file is subject to version 2.02 of the PHP license,      |
12
// | that is bundled with this package in the file LICENSE, and is        |
13
// | available at through the world-wide-web at                           |
14
// | http://www.php.net/license/2_02.txt.                                 |
15
// | If you did not receive a copy of the PHP license and are unable to   |
16
// | obtain it through the world-wide-web, please send a note to          |
17
// | [email protected] so we can mail you a copy immediately.               |
18
// +----------------------------------------------------------------------+
19
// | Author: Xavier Noguer <[email protected]>                              |
20
// | Based on OLE::Storage_Lite by Kawai, Takanori                        |
21
// +----------------------------------------------------------------------+
22
//
23
use PhpOffice\PhpSpreadsheet\Shared\OLE;
24
use PhpOffice\PhpSpreadsheet\Shared\OLE\PPS;
25
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
26
27
/**
28
 * Class for creating Root PPS's for OLE containers.
29
 *
30
 * @author   Xavier Noguer <[email protected]>
31
 *
32
 * @category PhpSpreadsheet
33
 */
34
class Root extends PPS
35
{
36
    /**
37
     * Directory for temporary files.
38
     *
39
     * @var string
40
     */
41
    protected $tempDirectory = null;
42
43
    /**
44
     * @param int $time_1st A timestamp
45
     * @param int $time_2nd A timestamp
46
     * @param File[] $raChild
47
     */
48 39
    public function __construct($time_1st, $time_2nd, $raChild)
49
    {
50 39
        $this->_tempDir = \PhpOffice\PhpSpreadsheet\Shared\File::sysGetTempDir();
0 ignored issues
show
Bug introduced by
The property _tempDir does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51
52 39
        parent::__construct(null, OLE::ascToUcs('Root Entry'), OLE::OLE_PPS_TYPE_ROOT, null, null, null, $time_1st, $time_2nd, null, $raChild);
53 39
    }
54
55
    /**
56
     * Method for saving the whole OLE container (including files).
57
     * In fact, if called with an empty argument (or '-'), it saves to a
58
     * temporary file and then outputs it's contents to stdout.
59
     * If a resource pointer to a stream created by fopen() is passed
60
     * it will be used, but you have to close such stream by yourself.
61
     *
62
     * @param string|resource $filename the name of the file or stream where to save the OLE container
63
     *
64
     * @throws WriterException
65
     *
66
     * @return bool true on success
67
     */
68 39
    public function save($filename)
69
    {
70
        // Initial Setting for saving
71 39
        $this->_BIG_BLOCK_SIZE = pow(
0 ignored issues
show
Bug introduced by
The property _BIG_BLOCK_SIZE does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72 39
            2,
73 39
            (isset($this->_BIG_BLOCK_SIZE)) ? self::adjust2($this->_BIG_BLOCK_SIZE) : 9
74
        );
75 39
        $this->_SMALL_BLOCK_SIZE = pow(
0 ignored issues
show
Bug introduced by
The property _SMALL_BLOCK_SIZE does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
76 39
            2,
77 39
            (isset($this->_SMALL_BLOCK_SIZE)) ? self::adjust2($this->_SMALL_BLOCK_SIZE) : 6
78
        );
79
80 39
        if (is_resource($filename)) {
81
            $this->_FILEH_ = $filename;
0 ignored issues
show
Bug introduced by
The property _FILEH_ does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82 39
        } elseif ($filename == '-' || $filename == '') {
83
            if ($this->tempDirectory === null) {
84
                $this->tempDirectory = \PhpOffice\PhpSpreadsheet\Shared\File::sysGetTempDir();
85
            }
86
            $this->_tmp_filename = tempnam($this->tempDirectory, 'OLE_PPS_Root');
0 ignored issues
show
Bug introduced by
The property _tmp_filename does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
87
            $this->_FILEH_ = fopen($this->_tmp_filename, 'w+b');
88
            if ($this->_FILEH_ == false) {
89
                throw new WriterException("Can't create temporary file.");
90
            }
91
        } else {
92 39
            $this->_FILEH_ = fopen($filename, 'wb');
93
        }
94 39
        if ($this->_FILEH_ == false) {
95
            throw new WriterException("Can't open $filename. It may be in use or protected.");
96
        }
97
        // Make an array of PPS's (for Save)
98 39
        $aList = [];
99 39
        PPS::_savePpsSetPnt($aList, [$this]);
100
        // calculate values for header
101 39
        list($iSBDcnt, $iBBcnt, $iPPScnt) = $this->_calcSize($aList); //, $rhInfo);
102
        // Save Header
103 39
        $this->_saveHeader($iSBDcnt, $iBBcnt, $iPPScnt);
104
105
        // Make Small Data string (write SBD)
106 39
        $this->_data = $this->_makeSmallData($aList);
107
108
        // Write BB
109 39
        $this->_saveBigData($iSBDcnt, $aList);
110
        // Write PPS
111 39
        $this->_savePps($aList);
112
        // Write Big Block Depot and BDList and Adding Header informations
113 39
        $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt);
114
115 39
        if (!is_resource($filename)) {
116 39
            fclose($this->_FILEH_);
117
        }
118
119 39
        return true;
120
    }
121
122
    /**
123
     * Calculate some numbers.
124
     *
125
     * @param array $raList Reference to an array of PPS's
126
     *
127
     * @return float[] The array of numbers
128
     */
129 39
    public function _calcSize(&$raList)
130
    {
131
        // Calculate Basic Setting
132 39
        list($iSBDcnt, $iBBcnt, $iPPScnt) = [0, 0, 0];
0 ignored issues
show
Unused Code introduced by
The assignment to $iSBDcnt is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $iPPScnt is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
133 39
        $iSmallLen = 0;
0 ignored issues
show
Unused Code introduced by
$iSmallLen is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
134 39
        $iSBcnt = 0;
135 39
        $iCount = count($raList);
136 39
        for ($i = 0; $i < $iCount; ++$i) {
137 39
            if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) {
138 39
                $raList[$i]->Size = $raList[$i]->getDataLen();
139 39
                if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) {
140 39
                    $iSBcnt += floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
141 39
                                  + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) ? 1 : 0);
142
                } else {
143 20
                    $iBBcnt += (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
144 20
                        (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) ? 1 : 0));
145
                }
146
            }
147
        }
148 39
        $iSmallLen = $iSBcnt * $this->_SMALL_BLOCK_SIZE;
149 39
        $iSlCnt = floor($this->_BIG_BLOCK_SIZE / OLE::OLE_LONG_INT_SIZE);
150 39
        $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt) ? 1 : 0);
151 39
        $iBBcnt += (floor($iSmallLen / $this->_BIG_BLOCK_SIZE) +
152 39
                      (($iSmallLen % $this->_BIG_BLOCK_SIZE) ? 1 : 0));
153 39
        $iCnt = count($raList);
154 39
        $iBdCnt = $this->_BIG_BLOCK_SIZE / OLE::OLE_PPS_SIZE;
155 39
        $iPPScnt = (floor($iCnt / $iBdCnt) + (($iCnt % $iBdCnt) ? 1 : 0));
156
157 39
        return [$iSBDcnt, $iBBcnt, $iPPScnt];
158
    }
159
160
    /**
161
     * Helper function for caculating a magic value for block sizes.
162
     *
163
     * @param int $i2 The argument
164
     *
165
     * @see save()
166
     *
167
     * @return float
168
     */
169
    private static function adjust2($i2)
170
    {
171
        $iWk = log($i2) / log(2);
172
173
        return ($iWk > floor($iWk)) ? floor($iWk) + 1 : $iWk;
174
    }
175
176
    /**
177
     * Save OLE header.
178
     *
179
     * @param int $iSBDcnt
180
     * @param int $iBBcnt
181
     * @param int $iPPScnt
182
     */
183 39
    public function _saveHeader($iSBDcnt, $iBBcnt, $iPPScnt)
184
    {
185 39
        $FILE = $this->_FILEH_;
186
187
        // Calculate Basic Setting
188 39
        $iBlCnt = $this->_BIG_BLOCK_SIZE / OLE::OLE_LONG_INT_SIZE;
189 39
        $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / OLE::OLE_LONG_INT_SIZE;
190
191 39
        $iBdExL = 0;
192 39
        $iAll = $iBBcnt + $iPPScnt + $iSBDcnt;
193 39
        $iAllW = $iAll;
194 39
        $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0);
195 39
        $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0);
196
197
        // Calculate BD count
198 39 View Code Duplication
        if ($iBdCnt > $i1stBdL) {
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...
199
            while (1) {
200
                ++$iBdExL;
201
                ++$iAllW;
202
                $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0);
203
                $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0);
204
                if ($iBdCnt <= ($iBdExL * $iBlCnt + $i1stBdL)) {
205
                    break;
206
                }
207
            }
208
        }
209
210
        // Save Header
211 39
        fwrite(
212 39
            $FILE,
213
            "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1"
214
            . "\x00\x00\x00\x00"
215
            . "\x00\x00\x00\x00"
216
            . "\x00\x00\x00\x00"
217
            . "\x00\x00\x00\x00"
218 39
            . pack('v', 0x3b)
219 39
            . pack('v', 0x03)
220 39
            . pack('v', -2)
221 39
            . pack('v', 9)
222 39
            . pack('v', 6)
223 39
            . pack('v', 0)
224 39
            . "\x00\x00\x00\x00"
225 39
            . "\x00\x00\x00\x00"
226 39
            . pack('V', $iBdCnt)
227 39
            . pack('V', $iBBcnt + $iSBDcnt) //ROOT START
228 39
            . pack('V', 0)
229 39
            . pack('V', 0x1000)
230 39
            . pack('V', $iSBDcnt ? 0 : -2) //Small Block Depot
231 39
            . pack('V', $iSBDcnt)
232
        );
233
        // Extra BDList Start, Count
234 39
        if ($iBdCnt < $i1stBdL) {
235 39
            fwrite(
236 39
                $FILE,
237 39
                pack('V', -2) // Extra BDList Start
238 39
                . pack('V', 0)// Extra BDList Count
239
            );
240
        } else {
241
            fwrite($FILE, pack('V', $iAll + $iBdCnt) . pack('V', $iBdExL));
242
        }
243
244
        // BDList
245 39
        for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; ++$i) {
246 39
            fwrite($FILE, pack('V', $iAll + $i));
247
        }
248 39
        if ($i < $i1stBdL) {
249 39
            $jB = $i1stBdL - $i;
250 39 View Code Duplication
            for ($j = 0; $j < $jB; ++$j) {
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...
251 39
                fwrite($FILE, (pack('V', -1)));
252
            }
253
        }
254 39
    }
255
256
    /**
257
     * Saving big data (PPS's with data bigger than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL).
258
     *
259
     * @param int $iStBlk
260
     * @param array &$raList Reference to array of PPS's
261
     */
262 39
    public function _saveBigData($iStBlk, &$raList)
263
    {
264 39
        $FILE = $this->_FILEH_;
265
266
        // cycle through PPS's
267 39
        $iCount = count($raList);
268 39
        for ($i = 0; $i < $iCount; ++$i) {
269 39
            if ($raList[$i]->Type != OLE::OLE_PPS_TYPE_DIR) {
270 39
                $raList[$i]->Size = $raList[$i]->getDataLen();
271 39
                if (($raList[$i]->Size >= OLE::OLE_DATA_SIZE_SMALL) || (($raList[$i]->Type == OLE::OLE_PPS_TYPE_ROOT) && isset($raList[$i]->_data))) {
272 39
                    fwrite($FILE, $raList[$i]->_data);
273
274 39 View Code Duplication
                    if ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) {
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...
275 36
                        fwrite($FILE, str_repeat("\x00", $this->_BIG_BLOCK_SIZE - ($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)));
276
                    }
277
                    // Set For PPS
278 39
                    $raList[$i]->startBlock = $iStBlk;
279
                    $iStBlk +=
280 39
                            (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) +
281 39
                                (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) ? 1 : 0));
282
                }
283
            }
284
        }
285 39
    }
286
287
    /**
288
     * get small data (PPS's with data smaller than \PhpOffice\PhpSpreadsheet\Shared\OLE::OLE_DATA_SIZE_SMALL).
289
     *
290
     * @param array &$raList Reference to array of PPS's
291
     */
292 39
    public function _makeSmallData(&$raList)
293
    {
294 39
        $sRes = '';
295 39
        $FILE = $this->_FILEH_;
296 39
        $iSmBlk = 0;
297
298 39
        $iCount = count($raList);
299 39
        for ($i = 0; $i < $iCount; ++$i) {
300
            // Make SBD, small data string
301 39
            if ($raList[$i]->Type == OLE::OLE_PPS_TYPE_FILE) {
302 39
                if ($raList[$i]->Size <= 0) {
303
                    continue;
304
                }
305 39
                if ($raList[$i]->Size < OLE::OLE_DATA_SIZE_SMALL) {
306 39
                    $iSmbCnt = floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE)
307 39
                                  + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) ? 1 : 0);
308
                    // Add to SBD
309 39
                    $jB = $iSmbCnt - 1;
310 39 View Code Duplication
                    for ($j = 0; $j < $jB; ++$j) {
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...
311 39
                        fwrite($FILE, pack('V', $j + $iSmBlk + 1));
312
                    }
313 39
                    fwrite($FILE, pack('V', -2));
314
315
                    // Add to Data String(this will be written for RootEntry)
316 39
                    $sRes .= $raList[$i]->_data;
317 39 View Code Duplication
                    if ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) {
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...
318 39
                        $sRes .= str_repeat("\x00", $this->_SMALL_BLOCK_SIZE - ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE));
319
                    }
320
                    // Set for PPS
321 39
                    $raList[$i]->startBlock = $iSmBlk;
322 39
                    $iSmBlk += $iSmbCnt;
323
                }
324
            }
325
        }
326 39
        $iSbCnt = floor($this->_BIG_BLOCK_SIZE / OLE::OLE_LONG_INT_SIZE);
327 39
        if ($iSmBlk % $iSbCnt) {
328 39
            $iB = $iSbCnt - ($iSmBlk % $iSbCnt);
329 39 View Code Duplication
            for ($i = 0; $i < $iB; ++$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...
330 39
                fwrite($FILE, pack('V', -1));
331
            }
332
        }
333
334 39
        return $sRes;
335
    }
336
337
    /**
338
     * Saves all the PPS's WKs.
339
     *
340
     * @param array $raList Reference to an array with all PPS's
341
     */
342 39
    public function _savePps(&$raList)
343
    {
344
        // Save each PPS WK
345 39
        $iC = count($raList);
346 39
        for ($i = 0; $i < $iC; ++$i) {
347 39
            fwrite($this->_FILEH_, $raList[$i]->_getPpsWk());
348
        }
349
        // Adjust for Block
350 39
        $iCnt = count($raList);
351 39
        $iBCnt = $this->_BIG_BLOCK_SIZE / OLE::OLE_PPS_SIZE;
352 39
        if ($iCnt % $iBCnt) {
353
            fwrite($this->_FILEH_, str_repeat("\x00", ($iBCnt - ($iCnt % $iBCnt)) * OLE::OLE_PPS_SIZE));
354
        }
355 39
    }
356
357
    /**
358
     * Saving Big Block Depot.
359
     *
360
     * @param int $iSbdSize
361
     * @param int $iBsize
362
     * @param int $iPpsCnt
363
     */
364 39
    public function _saveBbd($iSbdSize, $iBsize, $iPpsCnt)
365
    {
366 39
        $FILE = $this->_FILEH_;
367
        // Calculate Basic Setting
368 39
        $iBbCnt = $this->_BIG_BLOCK_SIZE / OLE::OLE_LONG_INT_SIZE;
369 39
        $i1stBdL = ($this->_BIG_BLOCK_SIZE - 0x4C) / OLE::OLE_LONG_INT_SIZE;
370
371 39
        $iBdExL = 0;
372 39
        $iAll = $iBsize + $iPpsCnt + $iSbdSize;
373 39
        $iAllW = $iAll;
374 39
        $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0);
375 39
        $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0);
376
        // Calculate BD count
377 39 View Code Duplication
        if ($iBdCnt > $i1stBdL) {
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...
378
            while (1) {
379
                ++$iBdExL;
380
                ++$iAllW;
381
                $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0);
382
                $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0);
383
                if ($iBdCnt <= ($iBdExL * $iBbCnt + $i1stBdL)) {
384
                    break;
385
                }
386
            }
387
        }
388
389
        // Making BD
390
        // Set for SBD
391 39
        if ($iSbdSize > 0) {
392 39 View Code Duplication
            for ($i = 0; $i < ($iSbdSize - 1); ++$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...
393
                fwrite($FILE, pack('V', $i + 1));
394
            }
395 39
            fwrite($FILE, pack('V', -2));
396
        }
397
        // Set for B
398 39 View Code Duplication
        for ($i = 0; $i < ($iBsize - 1); ++$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...
399 39
            fwrite($FILE, pack('V', $i + $iSbdSize + 1));
400
        }
401 39
        fwrite($FILE, pack('V', -2));
402
403
        // Set for PPS
404 39 View Code Duplication
        for ($i = 0; $i < ($iPpsCnt - 1); ++$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...
405
            fwrite($FILE, pack('V', $i + $iSbdSize + $iBsize + 1));
406
        }
407 39
        fwrite($FILE, pack('V', -2));
408
        // Set for BBD itself ( 0xFFFFFFFD : BBD)
409 39
        for ($i = 0; $i < $iBdCnt; ++$i) {
410 39
            fwrite($FILE, pack('V', 0xFFFFFFFD));
411
        }
412
        // Set for ExtraBDList
413 39
        for ($i = 0; $i < $iBdExL; ++$i) {
414
            fwrite($FILE, pack('V', 0xFFFFFFFC));
415
        }
416
        // Adjust for Block
417 39
        if (($iAllW + $iBdCnt) % $iBbCnt) {
418 39
            $iBlock = ($iBbCnt - (($iAllW + $iBdCnt) % $iBbCnt));
419 39
            for ($i = 0; $i < $iBlock; ++$i) {
420 39
                fwrite($FILE, pack('V', -1));
421
            }
422
        }
423
        // Extra BDList
424 39
        if ($iBdCnt > $i1stBdL) {
425
            $iN = 0;
426
            $iNb = 0;
427
            for ($i = $i1stBdL; $i < $iBdCnt; $i++, ++$iN) {
428
                if ($iN >= ($iBbCnt - 1)) {
429
                    $iN = 0;
430
                    ++$iNb;
431
                    fwrite($FILE, pack('V', $iAll + $iBdCnt + $iNb));
432
                }
433
                fwrite($FILE, pack('V', $iBsize + $iSbdSize + $iPpsCnt + $i));
434
            }
435
            if (($iBdCnt - $i1stBdL) % ($iBbCnt - 1)) {
436
                $iB = ($iBbCnt - 1) - (($iBdCnt - $i1stBdL) % ($iBbCnt - 1));
437 View Code Duplication
                for ($i = 0; $i < $iB; ++$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...
438
                    fwrite($FILE, pack('V', -1));
439
                }
440
            }
441
            fwrite($FILE, pack('V', -2));
442
        }
443 39
    }
444
}
445