Reverted r113177 per CR
[lhc/web/wiklou.git] / includes / media / GIFMetadataExtractor.php
index a3d3ced..90bc726 100644 (file)
@@ -50,7 +50,7 @@ class GIFMetadataExtractor {
                        throw new Exception( "File $filename does not exist" );
                }
 
-               $fh = fopen( $filename, 'r' );
+               $fh = fopen( $filename, 'rb' );
 
                if ( !$fh ) {
                        throw new Exception( "Unable to open file $filename" );
@@ -95,6 +95,7 @@ class GIFMetadataExtractor {
                                self::skipBlock( $fh ); 
                        } elseif ( $buf == self::$gif_extension_sep ) {
                                $buf = fread( $fh, 1 );
+                               if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
                                $extension_code = unpack( 'C', $buf );
                                $extension_code = $extension_code[1];
 
@@ -105,6 +106,7 @@ class GIFMetadataExtractor {
                                        fread( $fh, 1 ); // Transparency, disposal method, user input
 
                                        $buf = fread( $fh, 2 ); // Delay, in hundredths of seconds.
+                                       if ( strlen( $buf ) < 2 ) throw new Exception( "Ran out of input" );
                                        $delay = unpack( 'v', $buf );
                                        $delay = $delay[1];
                                        $duration += $delay * 0.01;
@@ -112,6 +114,7 @@ class GIFMetadataExtractor {
                                        fread( $fh, 1 ); // Transparent colour index
 
                                        $term = fread( $fh, 1 ); // Should be a terminator
+                                       if ( strlen( $term ) < 1 ) throw new Exception( "Ran out of input" );
                                        $term = unpack( 'C', $term );
                                        $term = $term[1];
                                        if ($term != 0 ) {
@@ -126,14 +129,14 @@ class GIFMetadataExtractor {
 
                                        // The standard says this should be ASCII, however its unclear if
                                        // thats true in practise. Check to see if its valid utf-8, if so
-                                       // assume its that, otherwise assume its iso-8859-1
+                                       // assume its that, otherwise assume its windows-1252 (iso-8859-1)
                                        $dataCopy = $data;
                                        // quickIsNFCVerify has the side effect of replacing any invalid characters
                                        UtfNormal::quickIsNFCVerify( $dataCopy );
 
                                        if ( $dataCopy !== $data ) {
                                                wfSuppressWarnings();
-                                               $data = iconv( 'ISO-8859-1', 'UTF-8', $data );
+                                               $data = iconv( 'windows-1252', 'UTF-8', $data );
                                                wfRestoreWarnings();
                                        }
 
@@ -150,6 +153,7 @@ class GIFMetadataExtractor {
                                        // Application extension (Netscape info about the animated gif)
                                        // or XMP (or theoretically any other type of extension block)
                                        $blockLength = fread( $fh, 1 );
+                                       if ( strlen( $blockLength ) < 1 ) throw new Exception( "Ran out of input" );
                                        $blockLength = unpack( 'C', $blockLength );
                                        $blockLength = $blockLength[1];
                                        $data = fread( $fh, $blockLength );
@@ -172,6 +176,7 @@ class GIFMetadataExtractor {
                                                
                                                // Unsigned little-endian integer, loop count or zero for "forever"
                                                $loopData = fread( $fh, 2 );
+                                               if ( strlen( $loopData ) < 2 ) throw new Exception( "Ran out of input" );
                                                $loopData = unpack( 'v', $loopData );
                                                $loopCount = $loopData[1];
                                                
@@ -209,6 +214,7 @@ class GIFMetadataExtractor {
                        } elseif ( $buf == self::$gif_term ) {
                                break;
                        } else {
+                               if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
                                $byte = unpack( 'C', $buf );
                                $byte = $byte[1];
                                throw new Exception( "At position: ".ftell($fh). ", Unknown byte ".$byte );
@@ -242,6 +248,7 @@ class GIFMetadataExtractor {
         * @return int
         */
        static function decodeBPP( $data ) {
+               if ( strlen( $data ) < 1 ) throw new Exception( "Ran out of input" );
                $buf = unpack( 'C', $data );
                $buf = $buf[1];
                $bpp = ( $buf & 7 ) + 1;
@@ -259,6 +266,7 @@ class GIFMetadataExtractor {
        static function skipBlock( $fh ) {
                while ( !feof( $fh ) ) {
                        $buf = fread( $fh, 1 );
+                       if ( strlen( $buf ) < 1 ) throw new Exception( "Ran out of input" );
                        $block_len = unpack( 'C', $buf );
                        $block_len = $block_len[1];
                        if ($block_len == 0) {
@@ -278,7 +286,7 @@ class GIFMetadataExtractor {
         *  sub-blocks in the returned value. Normally this is false,
         *  except XMP is weird and does a hack where you need to keep
         *  these length bytes.
-        * @return The data.
+        * @return string The data.
         */
        static function readBlock( $fh, $includeLengths = false ) {
                $data = '';