MediaWiki master
RiffExtractor.php
Go to the documentation of this file.
1<?php
7namespace Wikimedia;
8
20 public static function findChunksFromFile( $filename, $maxChunks = -1 ) {
21 $file = fopen( $filename, 'rb' );
22 $info = self::findChunks( $file, $maxChunks );
23 fclose( $file );
24 return $info;
25 }
26
32 public static function findChunks( $file, $maxChunks = -1 ) {
33 $riff = fread( $file, 4 );
34 if ( $riff !== 'RIFF' ) {
35 return false;
36 }
37
38 // Next four bytes are fileSize
39 $fileSize = fread( $file, 4 );
40 if ( !$fileSize || strlen( $fileSize ) != 4 ) {
41 return false;
42 }
43
44 // Next four bytes are the FourCC
45 $fourCC = fread( $file, 4 );
46 if ( !$fourCC || strlen( $fourCC ) != 4 ) {
47 return false;
48 }
49
50 // Create basic info structure
51 $info = [
52 'fileSize' => self::extractUInt32( $fileSize ),
53 'fourCC' => $fourCC,
54 'chunks' => [],
55 ];
56 $numberOfChunks = 0;
57
58 // Find out the chunks
59 while ( !feof( $file ) && !( $numberOfChunks >= $maxChunks && $maxChunks >= 0 ) ) {
60 $chunkStart = ftell( $file );
61
62 $chunkFourCC = fread( $file, 4 );
63 if ( !$chunkFourCC || strlen( $chunkFourCC ) != 4 ) {
64 return $info;
65 }
66
67 $chunkSize = fread( $file, 4 );
68 if ( !$chunkSize || strlen( $chunkSize ) != 4 ) {
69 return $info;
70 }
71 $intChunkSize = self::extractUInt32( $chunkSize );
72
73 // Add chunk info to the info structure
74 $info['chunks'][] = [
75 'fourCC' => $chunkFourCC,
76 'start' => $chunkStart,
77 'size' => $intChunkSize
78 ];
79
80 // Uneven chunks have padding bytes
81 $padding = $intChunkSize % 2;
82 // Seek to the next chunk
83 fseek( $file, $intChunkSize + $padding, SEEK_CUR );
84
85 }
86
87 return $info;
88 }
89
95 public static function extractUInt32( $string ) {
96 return unpack( 'V', $string )[1];
97 }
98}
99
101class_alias( RiffExtractor::class, 'RiffExtractor' );
Extractor for the Resource Interchange File Format.
static findChunks( $file, $maxChunks=-1)
static findChunksFromFile( $filename, $maxChunks=-1)
static extractUInt32( $string)
Extract a little-endian uint32 from a 4 byte string.