Fix for 687601, /undefined in PK, 2nd version

Alex Cherepanov <[email protected]> Mon, 30 Aug 2004 17:41:06 -0400
Newsgroups gmane.comp.printing.ghostscript.patches
Organization Coscript Software
Message-ID <[email protected]>
This is a 2nd version of the patch. The buffers for %stdin% have
been increased to allow 1K of looking ahead.

[Log message]
Improve automatic PDF recognition in run operator. Following PDF 1.5
Implementation Notes H.3.4.1 search first 1024 bytes for %PDF- string.
Identify file as PDF if %PDF- is found but the file doesn't start with
%! . Recognize %!PS-Adobe-N.n PDF-M.m but don't search for it yet.
Make .peekstring return "() false" instead of /rangecheck for empty
files. Increase the size of %stdin% buffers to 1024 bytes.
Fix bug 687601 from the customer 562

_______________________________________________
gs-code-review mailing list
[email protected]
http://www.ghostscript.com/mailman/listinfo/gs-code-review
687601a.diff (text/plain, 3.7 KB)
Index: gs/lib/pdf_main.ps
===================================================================
RCS file: /cvs/ghostscript/gs/lib/pdf_main.ps,v
retrieving revision 1.87
diff -b -u -r1.87 pdf_main.ps
--- gs/lib/pdf_main.ps	1 Jun 2004 21:13:04 -0000	1.87
+++ gs/lib/pdf_main.ps	30 Aug 2004 21:14:11 -0000
@@ -70,13 +70,31 @@
 /.runps /run load def
 /run {
   dup type /filetype ne { (r) file } if
-  dup ( ) .peekstring {
-    (%) eq {
-      dup (     ) .peekstring {
-	(%PDF-) eq {
+  dup 1024 string .peekstring pop
+  dup length 0 ne {
+    dup length 100 gt { % too short for PDF
+      dup 0 2 getinterval (%!) eq {
+        dup 0 9 getinterval (PS-Adobe-) eq
+        exch 13 4 getinterval (PDF-) eq and 
+      } {
+        (%PDF-) search {
+          pop pop
+          length 0 ne {
+             (%stderr) (w) file dup
+             (   **** Warning: The file doesn't start with %PDF-\n)
+             writestring flushfile
+          } if
+          //true
+        } {
+          pop //false
+        } ifelse
+      } ifelse
+    } {
+      pop //false
+    } ifelse {
 	    dup (%stdin) (r) file eq {
 	      % Copy PDF from stdin to temporary file then run it.
-	      null (w+) //systemdict /.tempfile get exec exch 3 1 roll
+        //null (w+) //systemdict /.tempfile get exec exch 3 1 roll
 	      % stack: tempname stdin tempfile
 	      64000 string
 	      {
@@ -95,13 +113,7 @@
 	      runpdf
 	    } ifelse
 	  } {
-	    cvx .runps % doesn't start with %PDF-
-	  } ifelse
-	} {
-	  pop cvx .runps % didn't read 5 characters
-	} ifelse
-    } {
-      cvx .runps % didn't start with %
+      cvx .runps
     } ifelse
   } {
     pop closefile % file was empty
Index: gs/src/zfileio.c
===================================================================
RCS file: /cvs/ghostscript/gs/src/zfileio.c,v
retrieving revision 1.17
diff -b -u -r1.17 zfileio.c
--- gs/src/zfileio.c	4 Aug 2004 19:36:13 -0000	1.17
+++ gs/src/zfileio.c	30 Aug 2004 21:14:11 -0000
@@ -702,7 +702,7 @@
 	 * at least len bytes.  However, this raises messy problems about
 	 * which allocator to use and how it should interact with restore.
 	 */
-	if (len >= s->bsize)
+	if (len >= s->bsize && s->bsize > 0)
 	    return_error(e_rangecheck);
 	switch (status) {
 	case EOFC:
Index: gs/src/ziodevs.c
===================================================================
RCS file: /cvs/ghostscript/gs/src/ziodevs.c,v
retrieving revision 1.9
diff -b -u -r1.9 ziodevs.c
--- gs/src/ziodevs.c	4 Aug 2004 19:36:13 -0000	1.9
+++ gs/src/ziodevs.c	30 Aug 2004 21:14:11 -0000
@@ -46,7 +46,7 @@
  * immediately afterwards so as not to wind up with dangling references.
  */
 
-#define STDIN_BUF_SIZE 128
+#define STDIN_BUF_SIZE 1024
 
 private iodev_proc_init(stdin_init);
 private iodev_proc_open_device(stdin_open);
Index: gs/src/ziodevsc.c
===================================================================
RCS file: /cvs/ghostscript/gs/src/ziodevsc.c,v
retrieving revision 1.7
diff -b -u -r1.7 ziodevsc.c
--- gs/src/ziodevsc.c	4 Aug 2004 19:36:13 -0000	1.7
+++ gs/src/ziodevsc.c	30 Aug 2004 21:14:11 -0000
@@ -47,7 +47,7 @@
  * immediately afterwards so as not to wind up with dangling references.
  */
 
-#define STDIN_BUF_SIZE 128
+#define STDIN_BUF_SIZE 1024
 private iodev_proc_init(stdin_init);
 private iodev_proc_open_device(stdin_open);
 const gx_io_device gs_iodev_stdin =
Index: gs/src/iminst.h
===================================================================
RCS file: /cvs/ghostscript/gs/src/iminst.h,v
retrieving revision 1.8
diff -b -u -r1.8 iminst.h
--- gs/src/iminst.h	4 Aug 2004 19:36:13 -0000	1.8
+++ gs/src/iminst.h	30 Aug 2004 21:14:11 -0000
@@ -51,7 +51,7 @@
 } gs_file_path;
 
 /* buffer sizes for stdio */
-#define STDIN_BUF_SIZE 128
+#define STDIN_BUF_SIZE 1024
 #define STDOUT_BUF_SIZE 128
 #define STDERR_BUF_SIZE 128