Fix for 687474, Damaged pdf files won't display with Ghostscript

Alex Cherepanov <[email protected]> Tue, 25 May 2004 20:10:06 -0400
Newsgroups gmane.comp.printing.ghostscript.patches
Organization Coscript Software
Message-ID <[email protected]>
Search the whole PDF file for the last occurrence of %%EOF, because
there are files that have large amount of garbage at the end.
For efficiency search the last 1024 bytes first.
Fix bug 687474 from customer 580.

_______________________________________________
gs-code-review mailing list
[email protected]
http://www.ghostscript.com/mailman/listinfo/gs-code-review
pdf_main.ps.diff (text/plain, 3.1 KB)
Index: gs/lib/pdf_main.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/pdf_main.ps,v
retrieving revision 1.86
diff -b -u -r1.86 pdf_main.ps
--- a/gs/lib/pdf_main.ps	1 Apr 2004 06:14:00 -0000	1.86
+++ b/gs/lib/pdf_main.ps	25 May 2004 23:52:42 -0000
@@ -491,32 +491,49 @@
    currentdict end
  } bind def
 
+% Look for [\r\n]%%EO from the current position of the file.
+% Return the position of %%EO if found or -1 .
+/findeof {  % <file> find_eof <file> <position>
+  -1 exch
+  {
+    dup bytesavailable 4 lt { exit } if
+    dup 0 (%%EO) /SubFileDecode filter flushfile
+    dup dup fileposition 5 sub setfileposition
+    dup 5 string readstring not { pop exit } if
+    dup (\r%%EO) eq exch (\n%%EO) eq or {
+      dup fileposition 4 sub
+      3 1 roll exch pop
+    } if
+  } loop
+  exch
+} bind def
+
 % Skip backward over the %%EOF at the end of the PDF file, and read
 % the preceding startxref line.  The PDF specification unambiguously
 % requires that the %%EOF appear on a line by itself, and that the
 % startxref and the following position value appear on separate lines;
-% however, some applications (including, apparently, Acrobat Distiller
-% on the Macintosh) may add up to 2K of garbage after the %%EOF, and some
-% other applications also truncate the %%EOF to %%EO, and/or put the
+% however, some applications truncate the %%EOF to %%EO, and/or put the
 % startxref and the following value on the same line.
-% A file reported from Distiller 3.02b for AIX 4.1.1 has 2076 bytes
-% of garbage. The tolerance is increased to 4K to cover future
-% applications and bigger allocation units.
+% There seems to be no limit on the amount of garbage that can be
+% appended to the PDF file. Current record (60K) belongs to 
+% PDF-Out (v 2.0 - 35). We start the search for %%EO from the last 1024
+% bytes and continue from the beginning of the file.
 /findxref {		% - findxref <xrefpos>
-  PDFfile dup dup 0 setfileposition bytesavailable
+  PDFfile dup dup dup 0 setfileposition bytesavailable
   dup /PDFfilelen exch def
-	% Find the last %%EOF string (within 4096 bytes)
-  4096 sub PDFoffset .max
-  2 copy setfileposition
-  PDFfilelen exch sub string 1 index exch readstring pop {
-    (\015%%EO) search {		% Adobe can handle truncated key string
-      pop pop			% if found, keep searching 'post' string
-    } {
-      (\012%%EO) search
-	{ pop pop } { exit } ifelse	% exit if neither string found
-    } ifelse
-  } loop
-  PDFfilelen exch length sub 4 sub PDFoffset .max exch 1 index setfileposition
+	% Find the last %%EOF string (within 1024 bytes)
+  1024 sub PDFoffset .max
+  setfileposition findeof                  % search the last 1024 bytes
+  dup 0 le {
+    pop
+    dup PDFoffset setfileposition findeof  % search from the beginnibg
+    dup 0 le {
+       (   **** Error: Cannot find a %%EOF marker anywhere in the file.\n)
+       pdfformaterror
+       /findxref cvx /syntaxerror signalerror
+    } if
+  } if
+  dup 3 1 roll setfileposition
 	% Stack: eofpos
 	% Check for whether this is, in fact, a valid PDF file.
   dup PDFfilelen exch sub dup dup 7 gt exch 5 lt or {