EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Signing PDF files with extra data before %PDF header

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#32550
Posted: 03/09/2015 08:38:25
by Petr Stransky (Standard support level)
Joined: 09/24/2012
Posts: 19

I did receive a PDF file that our product based on SBB has not been able to sign. Upon closer inspection I found that the file has some extra data before the %PDF header (I do not know the origin of the file so I do not know why):
Code
<!--ERROR--><pre style="font-size: 10pt;"><b>Warning%PDF-1.3


When I try to sign it using SBB it fails in PDFDocument.Open with Unexpected character exception. This seems to be reasonable, the error message is clear and it is according to PDF specification:
Quote
7.5.2File Header
The first line of a PDF file shall be a header consisting of the 5 characters %PDF– followed by a version number of the form 1.N, where N is a digit between 0 and 7.


If I remove the extra data from the file by hand it signs correctly. I do not blame SBB for refusing to sign this file however Adober Acrobat XI and some competing products do work with that file even with that extra data before %PDF header. So I tried to implement the funcionality in our code before passing the PDF stream to SBB with seeking (fPDFSigner is our simple wrapper class around SBB PDF signing components):
Code
(fPDFSigner.pdfStream as TMemoryStream).LoadFromFile(fileName);
fPDFSigner.pdfStream.Seek($35,soFromBeginning);
fPDFSigner.LoadPDF;

With this the LoadPDF fails at fPDFDocument.Open(fPDFStream) with Stream read error, probably because the code in SBB seems to expect the stream to have position=0 (SBPDF.pas 3921):
Code
procedure TElPDFDocument.Open(Stream: TElStream);
begin


  Reset();
  // copying file contents to the temporary stream
  CreateTemporaryStream;
  FTempStream.Size := Stream.Size;
  FTempStream.Position := 0;
  FTempStream.CopyFrom(Stream, Stream.Size);
  .....

so SBB actualy tries to read after the end of the stream. When I change it like:
Code
  FTempStream.Size := Stream.Size-Stream.Position;
  FTempStream.Position := 0;
  FTempStream.CopyFrom(Stream, Stream.Size-Stream.Position);

everyting works fine and the file is signed correctly.

I would like to know:
1) what is the Eldos opinion about such PDF files
2) if the error when loading from stream with position <> 0 is considered a bug and would be fixed or if I am missing something
#32553
Posted: 03/09/2015 09:35:13
by Dmytro Bogatskyy (EldoS Corp.)

Thank you for contacting us.

Quote
1) what is the Eldos opinion about such PDF files

The PDF standard clearly states that:
Quote

The first line of a PDF file shall be a header consisting of the 5 characters %PDF– followed by a version number of the form M.N where M and N are integers separated by a PERIOD (2Eh). The characters PDF are preceded by a PERCENT SIGN (25h) and follow by a MINUS SIGN (2Dh).

We have some heuristic methods (workarounds) that fix document structure errors, but not this case.
Quote
2) if the error when loading from stream with position <> 0 is considered a bug and would be fixed or if I am missing something

Yes, it will be fixed in the same way as you posted above.
Thank you for pointing this out.
#32554
Posted: 03/09/2015 09:51:19
by Petr Stransky (Standard support level)
Joined: 09/24/2012
Posts: 19

Thanks for the prompt answer.

Reply

Statistics

Topic viewed 505 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!