57 self::$gifFrameSep = pack(
"C", ord(
"," ) );
58 self::$gifExtensionSep = pack(
"C", ord(
"!" ) );
59 self::$gifTerm = pack(
"C", ord(
";" ) );
68 throw new Exception(
"No file name specified" );
69 } elseif ( !file_exists( $filename ) || is_dir( $filename ) ) {
70 throw new Exception(
"File $filename does not exist" );
73 $fh = fopen( $filename,
'rb' );
76 throw new Exception(
"Unable to open file $filename" );
80 $buf = fread( $fh, 6 );
81 if ( !( $buf ==
'GIF87a' || $buf ==
'GIF89a' ) ) {
82 throw new Exception(
"Not a valid GIF file; header: $buf" );
89 $buf = fread( $fh, 1 );
98 while ( !feof( $fh ) ) {
99 $buf = fread( $fh, 1 );
101 if ( $buf == self::$gifFrameSep ) {
105 # # Skip bounding box
109 $buf = fread( $fh, 1 );
116 } elseif ( $buf == self::$gifExtensionSep ) {
117 $buf = fread( $fh, 1 );
118 if ( strlen( $buf ) < 1 ) {
119 throw new Exception(
"Ran out of input" );
121 $extension_code = unpack(
'C', $buf )[1];
123 if ( $extension_code == 0xF9 ) {
129 $buf = fread( $fh, 2 );
130 if ( strlen( $buf ) < 2 ) {
131 throw new Exception(
"Ran out of input" );
133 $delay = unpack(
'v', $buf )[1];
134 $duration += $delay * 0.01;
138 $term = fread( $fh, 1 );
139 if ( strlen(
$term ) < 1 ) {
140 throw new Exception(
"Ran out of input" );
144 throw new Exception(
"Malformed Graphics Control Extension block" );
146 } elseif ( $extension_code == 0xFE ) {
149 if ( $data ===
"" ) {
150 throw new Exception(
'Read error, zero-length comment block' );
158 UtfNormal\Validator::quickIsNFCVerify( $dataCopy );
160 if ( $dataCopy !== $data ) {
161 MediaWiki\suppressWarnings();
162 $data = iconv(
'windows-1252',
'UTF-8', $data );
163 MediaWiki\restoreWarnings();
166 $commentCount =
count( $comment );
167 if ( $commentCount === 0
168 || $comment[$commentCount - 1] !== $data
175 } elseif ( $extension_code == 0xFF ) {
178 $blockLength = fread( $fh, 1 );
179 if ( strlen( $blockLength ) < 1 ) {
180 throw new Exception(
"Ran out of input" );
182 $blockLength = unpack(
'C', $blockLength )[1];
183 $data = fread( $fh, $blockLength );
185 if ( $blockLength != 11 ) {
186 wfDebug( __METHOD__ .
" GIF application block with wrong length\n" );
187 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
193 if ( $data ==
'NETSCAPE2.0' ) {
194 $data = fread( $fh, 2 );
196 if ( $data !=
"\x03\x01" ) {
197 throw new Exception(
"Expected \x03\x01, got $data" );
201 $loopData = fread( $fh, 2 );
202 if ( strlen( $loopData ) < 2 ) {
203 throw new Exception(
"Ran out of input" );
205 $loopCount = unpack(
'v', $loopData )[1];
207 if ( $loopCount != 1 ) {
213 } elseif ( $data ==
'XMP DataXMP' ) {
219 if ( substr( $xmp, -257, 3 ) !==
"\x01\xFF\xFE"
220 || substr( $xmp, -4 ) !==
"\x03\x02\x01\x00"
223 throw new Exception(
"XMP does not have magic trailer!" );
227 $xmp = substr( $xmp, 0, -257 );
230 fseek( $fh, -( $blockLength + 1 ), SEEK_CUR );
237 } elseif ( $buf == self::$gifTerm ) {
240 if ( strlen( $buf ) < 1 ) {
241 throw new Exception(
"Ran out of input" );
243 $byte = unpack(
'C', $buf )[1];
244 throw new Exception(
"At position: " . ftell( $fh ) .
", Unknown byte " . $byte );
249 'frameCount' => $frameCount,
250 'looped' => $isLooped,
251 'duration' => $duration,
253 'comment' => $comment,
264 $max = pow( 2, $bpp );
265 for ( $i = 1; $i <= $max; ++$i ) {
277 if ( strlen( $data ) < 1 ) {
278 throw new Exception(
"Ran out of input" );
280 $buf = unpack(
'C', $data )[1];
281 $bpp = ( $buf & 7 ) + 1;
284 $have_map = $buf & 1;
286 return $have_map ? $bpp : 0;
294 while ( !feof( $fh ) ) {
295 $buf = fread( $fh, 1 );
296 if ( strlen( $buf ) < 1 ) {
297 throw new Exception(
"Ran out of input" );
299 $block_len = unpack(
'C', $buf )[1];
300 if ( $block_len == 0 ) {
303 fread( $fh, $block_len );
321 static function readBlock( $fh, $includeLengths =
false ) {
323 $subLength = fread( $fh, 1 );
326 while ( $subLength !==
"\0" ) {
328 if ( $blocks > self::MAX_SUBBLOCKS ) {
329 throw new Exception(
"MAX_SUBBLOCKS exceeded (over $blocks sub-blocks)" );
332 throw new Exception(
"Read error: Unexpected EOF." );
334 if ( $includeLengths ) {
338 $data .= fread( $fh, ord( $subLength ) );
339 $subLength = fread( $fh, 1 );