Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
RiffExtractor
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 findChunksFromFile
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 findChunks
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
182
 extractUInt32
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
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
25class 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}