From 9e861cf8d3bbef5a0f67b0290f123115cdbae568 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 22 Mar 2023 08:28:17 +0000 Subject: [PATCH] fpspreadsheet: Support reading of BIFF2 files with BOF record of BIFF8 - there seem to exist some malformed files of this kind which can be read by Excel. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@8768 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/read_write/excel2demo/excel2write.lpi | 1 + components/fpspreadsheet/source/common/xlsbiff2.pas | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/components/fpspreadsheet/examples/read_write/excel2demo/excel2write.lpi b/components/fpspreadsheet/examples/read_write/excel2demo/excel2write.lpi index 9aa1f63cc..954cfbd3d 100644 --- a/components/fpspreadsheet/examples/read_write/excel2demo/excel2write.lpi +++ b/components/fpspreadsheet/examples/read_write/excel2demo/excel2write.lpi @@ -53,6 +53,7 @@ + diff --git a/components/fpspreadsheet/source/common/xlsbiff2.pas b/components/fpspreadsheet/source/common/xlsbiff2.pas index c2e980385..efb645fee 100644 --- a/components/fpspreadsheet/source/common/xlsbiff2.pas +++ b/components/fpspreadsheet/source/common/xlsbiff2.pas @@ -194,6 +194,7 @@ const INT_EXCEL_ID_BOOLERROR = $0005; INT_EXCEL_ID_ROW = $0008; INT_EXCEL_ID_BOF = $0009; + INT_EXCEL_ID_BOF_BIFF8 = $0809; {%H-}INT_EXCEL_ID_INDEX = $000B; INT_EXCEL_ID_FORMAT = $001E; INT_EXCEL_ID_FORMATCOUNT = $001F; @@ -340,6 +341,8 @@ class function TsSpreadBIFF2Reader.CheckFileFormat(AStream: TStream): Boolean; const BIFF2_HEADER: packed array[0..3] of byte = ( $09,$00, $04,$00); // they are common to all BIFF2 files that I've seen + BIFF8_BOF: packed array[0..1] of byte = ( + $09,$08); // some files begin with the BOF of BIFF8 (don't know whether there are BIFF5 BOFs as well...) var P: Int64; buf: packed array[0..3] of byte = (0, 0, 0, 0); @@ -352,10 +355,7 @@ begin n := AStream.Read(buf, SizeOf(buf)); if n < Length(BIFF2_HEADER) then exit; - for n:=0 to High(buf) do - if buf[n] <> BIFF2_HEADER[n] then - exit; - Result := true; + Result := CompareMem(@buf[0], @BIFF2_HEADER, 4) or CompareMem(@buf[0], @BIFF8_BOF, 2); finally AStream.Position := P; end; @@ -656,6 +656,10 @@ begin case RecordType of INT_EXCEL_ID_BLANK : ReadBlank(AStream); INT_EXCEL_ID_BOF : BOFFound := true; + INT_EXCEL_ID_BOF_BIFF8 : begin + BOFFound := true; + FWorkbook.AddErrorMsg('Incorrect file header.'); + end; INT_EXCEL_ID_BOOLERROR : ReadBool(AStream); INT_EXCEL_ID_BOTTOMMARGIN : ReadMargin(AStream, 3); INT_EXCEL_ID_CODEPAGE : ReadCodePage(AStream);