MediaWiki master
RiffExtractor.php
Go to the documentation of this file.
1<?php
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
96 public static function extractUInt32( $string ) {
97 return unpack( 'V', $string )[1];
98 }
99}
static findChunks( $file, $maxChunks=-1)
static extractUInt32( $string)
Extract a little-endian uint32 from a 4 byte string.
static findChunksFromFile( $filename, $maxChunks=-1)