gv (the successor to Tim Theisen's ghostview) by default can uncompress gziped
files on the fly. This patch allows to also uncompress bzip2-compressed files
on the fly.

Compressed input can't be read from a pipe, the input must be seekable. The
name of the compressed file is irrelevant, detection is by magic file ID. gv
can't read from stdin.

gv only allows to specify one program to handle the decompression, it might
make more sense to extend this to two, but it's also possible to use a short
shell script to handle both bzip2 and gzip decompression, which is what
gv_uncompress does.

Volker Kuhlmann <VolkerKuhlmann@gmx.de>, 22 Aug 2003


--- gv-3.5.8/source/ps.c.orig	2003-08-22 15:08:02.000000000 +1200
+++ gv-3.5.8/source/ps.c	2003-08-22 16:41:41.000000000 +1200
@@ -408,9 +408,18 @@
     BEGINMESSAGE(psscan)
 
     if (cmd_uncompress) {
-      char b[2];
+      char b[4];
+      /*
       if (!(fread(b, sizeof(char),2, *fileP) == 2)
 	  || b[0] != '\037' || (b[1] != '\235' && b[1] != '\213')) {
+      */
+      /* 0x1F9D, 0x1F8B = gzip */
+      #define is_gzip (b[0] == '\037' && (b[1] == '\235' || b[1] == '\213'))
+      /* "BZ" = bzip2; should we skip the test in b[3]? */
+      #define is_bzip2 (b[0] == 'B' && b[1] == 'Z' \
+      	    	    	&& (b[3] >= 48) && (b[3] <=57) )
+      if ( (fread(b, sizeof(char),4, *fileP) != 4) /* I/O error */
+	  || (!is_gzip && !is_bzip2) ) {
         rewind(*fileP);
 	cmd_uncompress=NULL;
       }
--- gv-3.5.8/source/file.c.orig	2003-08-22 15:08:02.000000000 +1200
+++ gv-3.5.8/source/file.c	2003-08-22 15:10:05.000000000 +1200
@@ -376,6 +376,7 @@
   while (*ext) { *ext = tolower(*ext); ext++; }
   if      (!strncmp(mext,".gz",3))  ext = "";
   else if (!strncmp(mext,".z",2))   ext = "";
+  else if (!strncmp(mext,".bz2",4)) ext = "";
   else if (!strncmp(mext,".ps",3))  ext = ".ps";
   else if (!strncmp(mext,".pdf",4)) ext = ".pdf";
   else                              ext = e;
--- gv-3.5.8/source/Imakefile.orig	2003-08-22 17:18:29.000000000 +1200
+++ gv-3.5.8/source/Imakefile	2003-08-22 17:17:09.000000000 +1200
@@ -320,7 +320,8 @@
 	@echo "GV.gsSafer:		True" >> $(GV_INTERN_RES_DAT)
 	@echo "GV.gsQuiet:		True" >> $(GV_INTERN_RES_DAT)
 	@echo "GV.gsArguments:" >> $(GV_INTERN_RES_DAT)
-	@echo "GV.uncompressCommand:	gzip -d -c %s > %s" >> $(GV_INTERN_RES_DAT)
+#	@echo "GV.uncompressCommand:	gzip -d -c %s > %s" >> $(GV_INTERN_RES_DAT)
+	@echo "GV.uncompressCommand:	gv_uncompress %s %s" >> $(GV_INTERN_RES_DAT)
 	@echo "GV.printCommand:	$(PRINT_COMMAND)" >> $(GV_INTERN_RES_DAT)
 
 clean::
