Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / libs / RiffExtractor.php
blob304b99b8a43ab4c7aece268b2080524de69fb808
1 <?php
2 /**
3 * Extractor for the Resource Interchange File Format
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.
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.
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
20 * @file
21 * @author Bryan Tong Minh
22 * @ingroup Media
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;
33 public static function findChunks( $file, $maxChunks = -1 ) {
34 $riff = fread( $file, 4 );
35 if ( $riff !== 'RIFF' ) {
36 return false;
39 // Next four bytes are fileSize
40 $fileSize = fread( $file, 4 );
41 if ( !$fileSize || strlen( $fileSize ) != 4 ) {
42 return false;
45 // Next four bytes are the FourCC
46 $fourCC = fread( $file, 4 );
47 if ( !$fourCC || strlen( $fourCC ) != 4 ) {
48 return false;
51 // Create basic info structure
52 $info = [
53 'fileSize' => self::extractUInt32( $fileSize ),
54 'fourCC' => $fourCC,
55 'chunks' => [],
57 $numberOfChunks = 0;
59 // Find out the chunks
60 while ( !feof( $file ) && !( $numberOfChunks >= $maxChunks && $maxChunks >= 0 ) ) {
61 $chunkStart = ftell( $file );
63 $chunkFourCC = fread( $file, 4 );
64 if ( !$chunkFourCC || strlen( $chunkFourCC ) != 4 ) {
65 return $info;
68 $chunkSize = fread( $file, 4 );
69 if ( !$chunkSize || strlen( $chunkSize ) != 4 ) {
70 return $info;
72 $intChunkSize = self::extractUInt32( $chunkSize );
74 // Add chunk info to the info structure
75 $info['chunks'][] = [
76 'fourCC' => $chunkFourCC,
77 'start' => $chunkStart,
78 'size' => $intChunkSize
81 // Uneven chunks have padding bytes
82 $padding = $intChunkSize % 2;
83 // Seek to the next chunk
84 fseek( $file, $intChunkSize + $padding, SEEK_CUR );
88 return $info;
91 /**
92 * Extract a little-endian uint32 from a 4 byte string
93 * @param string $string 4-byte string
94 * @return int
96 public static function extractUInt32( $string ) {
97 return unpack( 'V', $string )[1];