Merge "Make line breaks in <blockquote> behave like <div> (bug 6200)."
[mediawiki.git] / includes / media / GIFMetadataExtractor.php
blob887afa3f13c5da10eb140f9da3c8e22cf7043505
1 <?php
2 /**
3 * GIF frame counter.
5 * Originally written in Perl by Steve Sanbeg.
6 * Ported to PHP by Andrew Garrett
7 * Deliberately not using MWExceptions to avoid external dependencies, encouraging
8 * redistribution.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
26 * @ingroup Media
29 /**
30 * GIF frame counter.
32 * @ingroup Media
34 class GIFMetadataExtractor {
35 static $gif_frame_sep;
36 static $gif_extension_sep;
37 static $gif_term;
39 const VERSION = 1;
41 // Each sub-block is less than or equal to 255 bytes.
42 // Most of the time its 255 bytes, except for in XMP
43 // blocks, where it's usually between 32-127 bytes each.
44 const MAX_SUBBLOCKS = 262144; // 5mb divided by 20.
46 /**
47 * @throws Exception
48 * @param $filename string
49 * @return array
51 static function getMetadata( $filename ) {
52 self::$gif_frame_sep = pack( "C", ord( "," ) );
53 self::$gif_extension_sep = pack( "C", ord( "!" ) );
54 self::$gif_term = pack( "C", ord( ";" ) );
56 $frameCount = 0;
57 $duration = 0.0;
58 $isLooped = false;
59 $xmp = "";
60 $comment = array();
62 if ( !$filename ) {
63 throw new Exception( "No file name specified" );
64 } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
65 throw new Exception( "File $filename does not exist" );
68 $fh = fopen( $filename, 'rb' );
70 if ( !$fh ) {
71 throw new Exception( "Unable to open file $filename" );
74 // Check for the GIF header
75 $buf = fread( $fh, 6 );
76 if ( !( $buf == 'GIF87a' || $buf == 'GIF89a' ) ) {
77 throw new Exception( "Not a valid GIF file; header: $buf" );
80 // Skip over width and height.
81 fread( $fh, 4 );
83 // Read BPP
84 $buf = fread( $fh, 1 );
85 $bpp = self::decodeBPP( $buf );
87 // Skip over background and aspect ratio
88 fread( $fh, 2 );
90 // Skip over the GCT
91 self::readGCT( $fh, $bpp );
93 while ( !feof( $fh ) ) {
94 $buf = fread( $fh, 1 );
96 if ( $buf == self::$gif_frame_sep ) {
97 // Found a frame
98 $frameCount++;
100 ## Skip bounding box
101 fread( $fh, 8 );
103 ## Read BPP
104 $buf = fread( $fh, 1 );
105 $bpp = self::decodeBPP( $buf );
107 ## Read GCT
108 self::readGCT( $fh, $bpp );
109 fread( $fh, 1 );
110 self::skipBlock( $fh );
111 } elseif ( $buf == self::$gif_extension_sep ) {
112 $buf = fread( $fh, 1 );
113 if ( strlen( $buf ) < 1 ) {
114 throw new Exception( "Ran out of input" );
116 $extension_code = unpack( 'C', $buf );
117 $extension_code = $extension_code[1];
119 if ( $extension_code == 0xF9 ) {
120 // Graphics Control Extension.
121 fread( $fh, 1 ); // Block size
123 fread( $fh, 1 ); // Transparency, disposal method, user input
125 $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
126 if ( strlen( $buf ) < 2 ) {
127 throw new Exception( "Ran out of input" );
129 $delay = unpack( 'v', $buf );
130 $delay = $delay[1];
131 $duration += $delay * 0.01;
133 fread( $fh, 1 ); // Transparent colour index
135 $term = fread( $fh, 1 ); // Should be a terminator
136 if ( strlen( $term ) < 1 ) {
137 throw new Exception( "Ran out of input" );
139 $term = unpack( 'C', $term );
140 $term = $term[1];
141 if ( $term != 0 ) {
142 throw new Exception( "Malformed Graphics Control Extension block" );
144 } elseif ( $extension_code == 0xFE ) {
145 // Comment block(s).
146 $data = self::readBlock( $fh );
147 if ( $data === "" ) {
148 throw new Exception( 'Read error, zero-length comment block' );
151 // The standard says this should be ASCII, however its unclear if
152 // thats true in practise. Check to see if its valid utf-8, if so
153 // assume its that, otherwise assume its windows-1252 (iso-8859-1)
154 $dataCopy = $data;
155 // quickIsNFCVerify has the side effect of replacing any invalid characters
156 UtfNormal::quickIsNFCVerify( $dataCopy );
158 if ( $dataCopy !== $data ) {
159 wfSuppressWarnings();
160 $data = iconv( 'windows-1252', 'UTF-8', $data );
161 wfRestoreWarnings();
164 $commentCount = count( $comment );
165 if ( $commentCount === 0
166 || $comment[$commentCount - 1] !== $data )
168 // Some applications repeat the same comment on each
169 // frame of an animated GIF image, so if this comment
170 // is identical to the last, only extract once.
171 $comment[] = $data;
173 } elseif ( $extension_code == 0xFF ) {
174 // Application extension (Netscape info about the animated gif)
175 // or XMP (or theoretically any other type of extension block)
176 $blockLength = fread( $fh, 1 );
177 if ( strlen( $blockLength ) < 1 ) {
178 throw new Exception( "Ran out of input" );
180 $blockLength = unpack( 'C', $blockLength );
181 $blockLength = $blockLength[1];
182 $data = fread( $fh, $blockLength );
184 if ( $blockLength != 11 ) {
185 wfDebug( __METHOD__ . " GIF application block with wrong length\n" );
186 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
187 self::skipBlock( $fh );
188 continue;
191 // NETSCAPE2.0 (application name for animated gif)
192 if ( $data == 'NETSCAPE2.0' ) {
193 $data = fread( $fh, 2 ); // Block length and introduction, should be 03 01
195 if ( $data != "\x03\x01" ) {
196 throw new Exception( "Expected \x03\x01, got $data" );
199 // Unsigned little-endian integer, loop count or zero for "forever"
200 $loopData = fread( $fh, 2 );
201 if ( strlen( $loopData ) < 2 ) {
202 throw new Exception( "Ran out of input" );
204 $loopData = unpack( 'v', $loopData );
205 $loopCount = $loopData[1];
207 if ( $loopCount != 1 ) {
208 $isLooped = true;
211 // Read out terminator byte
212 fread( $fh, 1 );
213 } elseif ( $data == 'XMP DataXMP' ) {
214 // application name for XMP data.
215 // see pg 18 of XMP spec part 3.
217 $xmp = self::readBlock( $fh, true );
219 if ( substr( $xmp, -257, 3 ) !== "\x01\xFF\xFE"
220 || substr( $xmp, -4 ) !== "\x03\x02\x01\x00" )
222 // this is just a sanity check.
223 throw new Exception( "XMP does not have magic trailer!" );
226 // strip out trailer.
227 $xmp = substr( $xmp, 0, -257 );
229 } else {
230 // unrecognized extension block
231 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
232 self::skipBlock( $fh );
233 continue;
235 } else {
236 self::skipBlock( $fh );
238 } elseif ( $buf == self::$gif_term ) {
239 break;
240 } else {
241 if ( strlen( $buf ) < 1 ) {
242 throw new Exception( "Ran out of input" );
244 $byte = unpack( 'C', $buf );
245 $byte = $byte[1];
246 throw new Exception( "At position: " . ftell( $fh ) . ", Unknown byte " . $byte );
250 return array(
251 'frameCount' => $frameCount,
252 'looped' => $isLooped,
253 'duration' => $duration,
254 'xmp' => $xmp,
255 'comment' => $comment,
260 * @param $fh
261 * @param $bpp
262 * @return void
264 static function readGCT( $fh, $bpp ) {
265 if ( $bpp > 0 ) {
266 for ( $i = 1; $i <= pow( 2, $bpp ); ++$i ) {
267 fread( $fh, 3 );
273 * @param $data
274 * @throws Exception
275 * @return int
277 static function decodeBPP( $data ) {
278 if ( strlen( $data ) < 1 ) {
279 throw new Exception( "Ran out of input" );
281 $buf = unpack( 'C', $data );
282 $buf = $buf[1];
283 $bpp = ( $buf & 7 ) + 1;
284 $buf >>= 7;
286 $have_map = $buf & 1;
288 return $have_map ? $bpp : 0;
292 * @param $fh
293 * @throws Exception
295 static function skipBlock( $fh ) {
296 while ( !feof( $fh ) ) {
297 $buf = fread( $fh, 1 );
298 if ( strlen( $buf ) < 1 ) {
299 throw new Exception( "Ran out of input" );
301 $block_len = unpack( 'C', $buf );
302 $block_len = $block_len[1];
303 if ( $block_len == 0 ) {
304 return;
306 fread( $fh, $block_len );
311 * Read a block. In the GIF format, a block is made up of
312 * several sub-blocks. Each sub block starts with one byte
313 * saying how long the sub-block is, followed by the sub-block.
314 * The entire block is terminated by a sub-block of length
315 * 0.
316 * @param $fh FileHandle
317 * @param $includeLengths Boolean Include the length bytes of the
318 * sub-blocks in the returned value. Normally this is false,
319 * except XMP is weird and does a hack where you need to keep
320 * these length bytes.
321 * @throws Exception
322 * @return string The data.
324 static function readBlock( $fh, $includeLengths = false ) {
325 $data = '';
326 $subLength = fread( $fh, 1 );
327 $blocks = 0;
329 while ( $subLength !== "\0" ) {
330 $blocks++;
331 if ( $blocks > self::MAX_SUBBLOCKS ) {
332 throw new Exception( "MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
334 if ( feof( $fh ) ) {
335 throw new Exception( "Read error: Unexpected EOF." );
337 if ( $includeLengths ) {
338 $data .= $subLength;
341 $data .= fread( $fh, ord( $subLength ) );
342 $subLength = fread( $fh, 1 );
344 return $data;