These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | /** |
||
3 | * Extractor for the Resource Interchange File Format |
||
4 | * |
||
5 | * This program is free software; you can redistribute it and/or modify |
||
6 | * it under the terms of the GNU General Public License as published by |
||
7 | * the Free Software Foundation; either version 2 of the License, or |
||
8 | * (at your option) any later version. |
||
9 | * |
||
10 | * This program is distributed in the hope that it will be useful, |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | * GNU General Public License for more details. |
||
14 | * |
||
15 | * You should have received a copy of the GNU General Public License along |
||
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
||
18 | * http://www.gnu.org/copyleft/gpl.html |
||
19 | * |
||
20 | * @file |
||
21 | * @author Bryan Tong Minh |
||
22 | * @ingroup Media |
||
23 | */ |
||
24 | |||
25 | class RiffExtractor { |
||
26 | public static function findChunksFromFile( $filename, $maxChunks = -1 ) { |
||
27 | $file = fopen( $filename, 'rb' ); |
||
28 | $info = self::findChunks( $file, $maxChunks ); |
||
29 | fclose( $file ); |
||
30 | return $info; |
||
31 | } |
||
32 | |||
33 | public static function findChunks( $file, $maxChunks = -1 ) { |
||
34 | $riff = fread( $file, 4 ); |
||
35 | if ( $riff !== 'RIFF' ) { |
||
36 | return false; |
||
37 | } |
||
38 | |||
39 | // Next four bytes are fileSize |
||
40 | $fileSize = fread( $file, 4 ); |
||
41 | if ( !$fileSize || strlen( $fileSize ) != 4 ) { |
||
42 | return false; |
||
43 | } |
||
44 | |||
45 | // Next four bytes are the FourCC |
||
46 | $fourCC = fread( $file, 4 ); |
||
47 | if ( !$fourCC || strlen( $fourCC ) != 4 ) { |
||
48 | return false; |
||
49 | } |
||
50 | |||
51 | // Create basic info structure |
||
52 | $info = [ |
||
53 | 'fileSize' => self::extractUInt32( $fileSize ), |
||
54 | 'fourCC' => $fourCC, |
||
55 | 'chunks' => [], |
||
56 | ]; |
||
57 | $numberOfChunks = 0; |
||
58 | |||
59 | // Find out the chunks |
||
60 | while ( !feof( $file ) && !( $numberOfChunks >= $maxChunks && $maxChunks >= 0 ) ) { |
||
61 | $chunkStart = ftell( $file ); |
||
62 | |||
63 | $chunkFourCC = fread( $file, 4 ); |
||
64 | if ( !$chunkFourCC || strlen( $chunkFourCC ) != 4 ) { |
||
65 | return $info; |
||
66 | } |
||
67 | |||
68 | $chunkSize = fread( $file, 4 ); |
||
69 | if ( !$chunkSize || strlen( $chunkSize ) != 4 ) { |
||
70 | return $info; |
||
71 | } |
||
72 | $intChunkSize = self::extractUInt32( $chunkSize ); |
||
73 | |||
74 | // Add chunk info to the info structure |
||
75 | $info['chunks'][] = [ |
||
76 | 'fourCC' => $chunkFourCC, |
||
77 | 'start' => $chunkStart, |
||
78 | 'size' => $intChunkSize |
||
79 | ]; |
||
80 | |||
81 | // Uneven chunks have padding bytes |
||
82 | $padding = $intChunkSize % 2; |
||
83 | // Seek to the next chunk |
||
84 | fseek( $file, $intChunkSize + $padding, SEEK_CUR ); |
||
85 | |||
86 | } |
||
87 | |||
88 | return $info; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Extract a little-endian uint32 from a 4 byte string |
||
93 | * @param string $string 4-byte string |
||
94 | * @return int |
||
95 | */ |
||
96 | public static function extractUInt32( $string ) { |
||
97 | return unpack( 'V', $string )[1]; |
||
98 | } |
||
99 | }; |
||
100 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.