Jelajahi Sumber

* Fix bug ID #32025

git-svn-id: trunk@36704 -
michael 8 tahun lalu
induk
melakukan
31d9d345e1

+ 2 - 1
packages/fcl-image/examples/drawing.pp

@@ -1,7 +1,8 @@
 {$mode objfpc}{$h+}
 program Drawing;
 
-uses cwstring,classes, sysutils, FPImage, FPCanvas, FPImgCanv, FPWritePNG, FPReadPNG;
+uses 
+  {$IFDEF UNIX}cwstring,{$ENDIF} classes, sysutils, FPImage, FPCanvas, FPImgCanv, FPWritePNG, FPReadPNG;
 
 const
   MyColor : TFPColor = (Red: $7FFF; Green: $0000; Blue: $FFFF; Alpha: alphaOpaque);

+ 1 - 1
packages/fcl-image/examples/textout.pp

@@ -3,7 +3,7 @@
 program textout;
 
 uses
-  cwstring,classes, sysutils, FPImage, FPCanvas, FPImgCanv, ftFont, FPWritePNG, freetype;
+  {$IFDEF UNIX}cwstring, {$ENDIF} classes, sysutils, FPImage, FPCanvas, FPImgCanv, ftFont, FPWritePNG, freetype;
 
 const
   MyColor : TFPColor = (Red: $7FFF; Green: $0000; Blue: $FFFF; Alpha: alphaOpaque);

+ 0 - 1
packages/fcl-image/src/fpimage.inc

@@ -124,7 +124,6 @@ begin
             try
               if CheckContents (str) then
                 try
-                  str.Position := startPos;
                   FStream := str;
                   FImage := self;
                   InternalRead (str, self);

+ 15 - 8
packages/fcl-image/src/fpreadbmp.pp

@@ -224,7 +224,7 @@ begin
 end;
 
 procedure TFPReaderBMP.InternalRead(Stream:TStream; Img:TFPCustomImage);
-
+// NOTE: Assumes that BMP header already has been read
 Var
   Row, i, pallen : Integer;
   BadCompression : boolean;
@@ -504,16 +504,23 @@ begin
 end;
 
 function  TFPReaderBMP.InternalCheck (Stream:TStream) : boolean;
-
+// NOTE: Does not rewind the stream!
 var
   BFH:TBitMapFileHeader;
+  n: Int64;
 begin
-  stream.Read(BFH,SizeOf(BFH));
-  {$IFDEF ENDIAN_BIG}
-  SwapBMPFileHeader(BFH);
-  {$ENDIF}
-  With BFH do
-    Result:=(bfType=BMmagic); // Just check magic number
+  Result:=False;
+  if Stream=nil then
+    exit;
+  n:=SizeOf(BFH);
+  Result:=Stream.Read(BFH,n)=n;
+  if Result then 
+    begin
+   {$IFDEF ENDIAN_BIG}
+    SwapBMPFileHeader(BFH);
+   {$ENDIF}
+    Result := BFH.bfType = BMmagic; // Just check magic number
+    end;
 end;
 
 initialization

+ 13 - 7
packages/fcl-image/src/fpreadgif.pas

@@ -473,17 +473,23 @@ begin
 end;
 
 function TFPReaderGif.InternalCheck(Stream: TStream): boolean;
+
 var
   OldPos: Int64;
+  n: Int64;
+  
 begin
+  Result:=False;
+  if Stream = nil then
+    exit;
+  OldPos:=Stream.Position;
   try
-    OldPos:=Stream.Position;
-    Stream.Read(FHeader,SizeOf(FHeader));
-    Result:=(FHeader.Signature = 'GIF') and
-            ((FHeader.Version = '87a') or (FHeader.Version = '89a'));
-    Stream.Position:=OldPos;
-  except
-    Result:=False;
+    n := SizeOf(FHeader);
+    Result:=(Stream.Read(FHeader,n)=n)
+            and (FHeader.Signature = 'GIF') 
+            and ((FHeader.Version = '87a') or (FHeader.Version = '89a'));
+  finally
+    Stream.Position := OldPos;
   end;
 end;
 

+ 19 - 1
packages/fcl-image/src/fpreadpcx.pas

@@ -301,10 +301,28 @@ begin
 end;
 
 function TFPReaderPCX.InternalCheck(Stream: TStream): boolean;
+var
+  hdr: TPcxHeader;
+  n: Integer;
+  oldPos: Int64;
 begin
-  Result := True;
+  Result:=False;
+  if Stream = nil then
+    exit;
+  oldPos := Stream.Position;
+  try
+    n:=SizeOf(hdr);
+    Result:=(Stream.Read(hdr, n)=n)
+            and (hdr.FileID in [$0A, $0C]) 
+            and (hdr.ColorPlanes in [1, 3, 4]) 
+            and (hdr.Version in [0, 2, 3, 5])
+            and (hdr.PaletteType in [1, 2]);
+  finally
+    Stream.Position := oldPos;
+  end;
 end;
 
+
 initialization
   ImageHandlers.RegisterImageReader('PCX Format', 'pcx', TFPReaderPCX);
 end.

+ 23 - 21
packages/fcl-image/src/fpreadpng.pp

@@ -805,6 +805,7 @@ begin
     raise PNGImageException.Create('Critical chunk '+chunk.readtype+' not recognized');
 end;
 
+// NOTE: It is assumed that signature and IDHDR chunk already have been read.
 procedure TFPReaderPNG.InternalRead (Str:TStream; Img:TFPCustomImage);
 begin
   {$ifdef FPC_Debug_Image}
@@ -872,33 +873,34 @@ begin
   Result.Y := Height;
 end;
 
+// NOTE: Stream does not rewind here!
 function  TFPReaderPNG.InternalCheck (Str:TStream) : boolean;
 var SigCheck : array[0..7] of byte;
     r : integer;
 begin
-  try
-    // Check Signature
-    Str.Read(SigCheck, SizeOf(SigCheck));
-    for r := 0 to 7 do
+  Result:=False;
+  if Str=Nil then 
+    exit;
+  // Check Signature
+  if Str.Read(SigCheck, SizeOf(SigCheck)) <> SizeOf(SigCheck) then
+    Exit;
+  for r := 0 to 7 do
     begin
-      If SigCheck[r] <> Signature[r] then
-        Exit(false);
+    If SigCheck[r] <> Signature[r] then
+      Exit;
+    end;
+  // Check IHDR
+  ReadChunk;
+  move (chunk.data^, FHeader, sizeof(Header));
+  with header do
+    begin
+    {$IFDEF ENDIAN_LITTLE}
+    Width := swap(width);
+    height := swap (height);
+    {$ENDIF}
+    result :=(width > 0) and (height > 0) and (compression = 0)
+              and (filter = 0) and (Interlace in [0,1]);
     end;
-    // Check IHDR
-    ReadChunk;
-    move (chunk.data^, FHeader, sizeof(Header));
-    with header do
-      begin
-      {$IFDEF ENDIAN_LITTLE}
-      Width := swap(width);
-      height := swap (height);
-      {$ENDIF}
-      result := (width > 0) and (height > 0) and (compression = 0)
-                and (filter = 0) and (Interlace in [0,1]);
-      end;
-  except
-    result := false;
-  end;
 end;
 
 initialization

+ 23 - 5
packages/fcl-image/src/fpreadpnm.pp

@@ -48,15 +48,33 @@ type
 
 implementation
 
-function TFPReaderPNM.InternalCheck(Stream:TStream):boolean;
+const
+  WhiteSpaces=[#9,#10,#13,#32];
+  {Whitespace (TABs, CRs, LFs, blanks) are separators in the PNM Headers}
 
+{ The magic number at the beginning of a pnm file is 'P1', 'P2', ..., 'P7'
+  followed by a WhiteSpace character }
+function TFPReaderPNM.InternalCheck(Stream:TStream):boolean;
+var
+  hdr: array[0..2] of char;
+  oldPos: Int64;
+  n: Integer;
 begin
-  InternalCheck:=True;
+  Result:=False;
+  if Stream = nil then
+    exit;
+  oldPos := Stream.Position;
+  try
+    n := SizeOf(hdr);
+    Result:=(Stream.Read(hdr[0], n) = n)
+            and (hdr[0] = 'P') 
+            and (hdr[1] in ['1'..'7']) 
+            and (hdr[2] in WhiteSpaces);
+  finally
+    Stream.Position := oldPos;
+  end;
 end;
 
-const
-  WhiteSpaces=[#9,#10,#13,#32]; {Whitespace (TABs, CRs, LFs, blanks) are separators in the PNM Headers}
-
 function DropWhiteSpaces(Stream : TStream) :Char;
 
 begin

+ 11 - 6
packages/fcl-image/src/fpreadpsd.pas

@@ -588,14 +588,19 @@ end;
 function TFPReaderPSD.InternalCheck(Stream: TStream): boolean;
 var
   OldPos: Int64;
+  n: Integer;
+  
 begin
+  Result:=False;
+  if Stream=Nil then 
+    exit;
+  OldPos := Stream.Position;
   try
-    OldPos:=Stream.Position;
-    Stream.Read(FHeader,SizeOf(FHeader));
-    Result:=(FHeader.Signature = '8BPS');
-    Stream.Position:=OldPos;
-  except
-    Result:=False;
+    n := SizeOf(FHeader);
+    Result:=(Stream.Read(FHeader, n) = n) 
+            and (FHeader.Signature = '8BPS')
+  finally
+    Stream.Position := OldPos;
   end;
 end;
 

+ 1 - 1
packages/fcl-image/src/fpreadxpm.pp

@@ -39,7 +39,7 @@ type
 implementation
 
 const
-  WhiteSpace = ' '#8#10#13;
+  WhiteSpace = ' '#9#10#13;
 
 constructor TFPReaderXPM.create;
 begin