tibbs / rpms / texlive

Forked from rpms/texlive 6 years ago
Clone
860dd42
diff -up texlive-2007/texk/dviljk/c-auto.in.dviljktemp texlive-2007/texk/dviljk/c-auto.in
860dd42
--- texlive-2007/texk/dviljk/c-auto.in.dviljktemp	2006-01-17 22:41:51.000000000 +0100
860dd42
+++ texlive-2007/texk/dviljk/c-auto.in	2008-08-27 13:32:10.000000000 +0200
860dd42
@@ -1,9 +1,23 @@
860dd42
-/* c-auto.in.  Generated automatically from configure.in by autoheader.  */
860dd42
+/* c-auto.in.  Generated from configure.in by autoheader.  */
860dd42
+/* acconfig.h -- used by autoheader when generating c-auto.in.
860dd42
 
860dd42
-/* Define if type char is unsigned and you are not using gcc.  */
860dd42
-#ifndef __CHAR_UNSIGNED__
860dd42
-#undef __CHAR_UNSIGNED__
860dd42
-#endif
860dd42
+   If you're thinking of editing acconfig.h to fix a configuration
860dd42
+   problem, don't. Edit the c-auto.h file created by configure,
860dd42
+   instead.  Even better, fix configure to give the right answer.  */
860dd42
+
860dd42
+/* Define to 1 if you have the `mkdtemp' function. */
860dd42
+#undef HAVE_MKDTEMP
860dd42
+
860dd42
+/* Define to 1 if you have the `rmdir' function. */
860dd42
+#undef HAVE_RMDIR
860dd42
 
860dd42
-/* The number of bytes in a long.  */
860dd42
+/* Define to 1 if you have the `unlink' function. */
860dd42
+#undef HAVE_UNLINK
860dd42
+
860dd42
+/* The size of a `long', as computed by sizeof. */
860dd42
 #undef SIZEOF_LONG
860dd42
+
860dd42
+/* Define to 1 if type `char' is unsigned and you are not using gcc.  */
860dd42
+#ifndef __CHAR_UNSIGNED__
860dd42
+# undef __CHAR_UNSIGNED__
860dd42
+#endif
860dd42
diff -U0 texlive-2007/texk/dviljk/ChangeLog.dviljktemp texlive-2007/texk/dviljk/ChangeLog
860dd42
--- texlive-2007/texk/dviljk/ChangeLog.dviljktemp	2006-05-29 22:09:20.000000000 +0200
860dd42
+++ texlive-2007/texk/dviljk/ChangeLog	2008-08-27 13:32:10.000000000 +0200
860dd42
@@ -0,0 +1,39 @@
860dd42
+2007-07-02  Joachim Schrod  <jschrod@acm.org>
860dd42
+
860dd42
+	* dvi2xx.c (DoSpecial): Security issue: usage of tmpnam() caused
860dd42
+	tempfile race condition. I use mkdtemp() if it's available and
860dd42
+	fall back to tmpnam.
860dd42
+
860dd42
+	Special parsing of include files was inconsistent, unify it. The
860dd42
+	current parsing code still allows lots of non-sensical special
860dd42
+	commands, but at least it doesn't access unrelated variables any
860dd42
+	more.
860dd42
+
860dd42
+2007-06-28  Joachim Schrod  <jschrod@acm.org>
860dd42
+
860dd42
+	* dvi2xx.c: Fixed a whole bunch of buffer overflows: The program
860dd42
+	did not check memory bounds for any string operation. All places
860dd42
+	where strings are copied with strcpy are replaced by dynamically
860dd42
+	allocated strings (with xstrdup from kpathsea) or bounded string
860dd42
+	operations. Fixed also manual string copy operations on special
860dd42
+	strings. Fixed array buffer overflow in defpoint and fill special
860dd42
+	operations.
860dd42
+	(DoSpecial): Call of ghostscript for psfile special had also a
860dd42
+	potential buffer overflow caused by unchecked usage of sprintf.
860dd42
+	Fix core dump: Check if all required parameters of psfile special
860dd42
+	are passed.
860dd42
+
860dd42
+	Bumped version number up to 2.6p3.
860dd42
+
860dd42
+	* dvi2xx.h: Some fixed sized string arrays are pointers now, they
860dd42
+	get dynamically allocated.
860dd42
+	(GetBytes): Another buffer overflow: Check that the buffer size is
860dd42
+	sufficient to store the read bytes. That relies on the invariant
860dd42
+	that the GetBytes macro is always called with an array as argument
860dd42
+	and not with a pointer.
860dd42
+
860dd42
+	* config.h: Throw an error if kpathsea is not used. dvi2xx.c
860dd42
+	had previously already kpathsea dependencies without protecting
860dd42
+	them with #if KPATHSEA. We go that road further since upstream
860dd42
+	does not exist any more.
860dd42
+
860dd42
diff -up texlive-2007/texk/dviljk/config.h.dviljktemp texlive-2007/texk/dviljk/config.h
860dd42
--- texlive-2007/texk/dviljk/config.h.dviljktemp	2006-01-17 22:41:51.000000000 +0100
860dd42
+++ texlive-2007/texk/dviljk/config.h	2008-08-27 13:32:10.000000000 +0200
860dd42
@@ -216,12 +216,7 @@ typedef SCHAR_TYPE signed_char;
860dd42
 #endif
860dd42
 
860dd42
 #ifndef KPATHSEA
860dd42
-extern bool findfile(
860dd42
-#if NeedFunctionPrototypes
860dd42
-char path[], char n[], long4 fontmag, char name[], 
860dd42
-	      bool tfm, int level
860dd42
-#endif
860dd42
-    );
860dd42
+#error "Would need changed findfile, dviljk has changed allocation semantic of name member in tfontptr"
860dd42
 #endif
860dd42
 
860dd42
 
860dd42
@@ -444,3 +439,24 @@ typedef  FILE *FILEPTR;
860dd42
 /* If we have neither, should fall back to fprintf with fixed args.  */
860dd42
 #endif
860dd42
 #endif
860dd42
+
860dd42
+/* If unlink and rmdir are not there, we don't delete the temporary files. */
860dd42
+#ifndef HAVE_RMDIR
860dd42
+#define rmdir(dir)
860dd42
+#endif
860dd42
+#ifndef HAVE_UNLINK
860dd42
+#define unlink(file)
860dd42
+#endif
860dd42
+
860dd42
+/* If mkdtemp() does not exist, we have to use tmpnam(). */
860dd42
+#ifndef HAVE_MKDTEMP
860dd42
+#define mkdtemp(dir) (tmpnam(dir) ? \
860dd42
+		      ( mkdir(dir, 0700) == -1 ? NULL : dir ) :	\
860dd42
+		      ( errno = EINVAL, NULL ) )
860dd42
+#endif
860dd42
+
860dd42
+#ifndef KPATHSEA
860dd42
+/* FIXME: Should provide a strdup function. But currently this tree is
860dd42
+   only used in connection with kpathsea anyhow. */
860dd42
+#error "Need xstrdup and xmalloc function, e.g. from kpathsea"
860dd42
+#endif
860dd42
diff -up texlive-2007/texk/dviljk/configure.in.dviljktemp texlive-2007/texk/dviljk/configure.in
860dd42
--- texlive-2007/texk/dviljk/configure.in.dviljktemp	2006-01-17 22:41:51.000000000 +0100
860dd42
+++ texlive-2007/texk/dviljk/configure.in	2008-08-27 13:32:10.000000000 +0200
860dd42
@@ -13,6 +13,7 @@ sinclude(../kpathsea/withenable.ac)
860dd42
 dnl These tests prevent reliable cross-compilation.  Sigh.
860dd42
 AC_C_CHAR_UNSIGNED
860dd42
 AC_CHECK_SIZEOF(long)
860dd42
+AC_CHECK_FUNCS(rmdir unlink mkdtemp)
860dd42
 
860dd42
 KPSE_CONFIG_FILES([Makefile])
860dd42
 AC_OUTPUT
860dd42
diff -up texlive-2007/texk/dviljk/dvi2xx.c.dviljktemp texlive-2007/texk/dviljk/dvi2xx.c
860dd42
--- texlive-2007/texk/dviljk/dvi2xx.c.dviljktemp	2006-01-17 22:41:51.000000000 +0100
860dd42
+++ texlive-2007/texk/dviljk/dvi2xx.c	2008-08-27 13:32:10.000000000 +0200
4931932
@@ -1,5 +1,5 @@
4931932
 /* $Id: dvi2xx.c,v 2.5 1997/12/08 20:52:20 neumann Exp $ */
4931932
-#define VERSION "2.6p2 (dviljk)"
4931932
+#define VERSION "dviljk (version 2.6p3)"
4931932
 /*
4931932
 #define DEBUGGS 1
4931932
 */
4931932
@@ -173,7 +173,7 @@ char    *argv[];
4931932
   y_origin = YDEFAULTOFF; /* y-origin in dots                    */
4931932
 
4931932
   setbuf(ERR_STREAM, NULL);
4931932
-  (void) strcpy(G_progname, argv[0]);
4931932
+  G_progname = argv[0];
4931932
 #ifdef KPATHSEA
4931932
   kpse_set_progname(argv[0]);
4931932
   kpse_set_program_enabled (kpse_pk_format, MAKE_TEX_PK_BY_DEFAULT, kpse_src_compile);
4931932
@@ -2968,8 +2968,8 @@ char    *argv[];
4931932
 #endif
4931932
 {
4931932
   int     argind;            /* argument index for flags      */
4931932
-  char    curarea[STRSIZE];  /* current file area             */
4931932
-  char    curname[STRSIZE];  /* current file name             */
4931932
+  char    *curarea;	     /* current file area             */
4931932
+  char    *curname;   	     /* current file name             */
4931932
   char    *tcp, *tcp1;       /* temporary character pointers  */
4931932
   char    *this_arg;
4931932
   double  x_offset = 0.0, y_offset = 0.0;
4931932
@@ -2988,9 +2988,9 @@ char    *argv[];
4931932
 #endif
4931932
 #endif
4931932
 
4931932
-  if (argc == 2 && (strcmp (argv[1], "--version") == 0)) {
4931932
+  if (argc == 2 && EQ(argv[1], "--version")) {
4931932
     extern KPSEDLL char *kpathsea_version_string;
4931932
-    puts ("dvilj(k) 2.6");
4931932
+    puts (VERSION);
4931932
     puts (kpathsea_version_string);
4931932
     puts ("Copyright (C) 1997 Gustaf Neumann.\n\
4931932
 There is NO warranty.  You may redistribute this software\n\
4931932
@@ -3328,8 +3328,8 @@ Primary author of Dvi2xx: Gustaf Neumann
4931932
       }
4931932
     } else {
4931932
 
4931932
-      (void) strcpy(filename, tcp);
4931932
-      if (!strcmp(filename, "-")) {
4931932
+      filename = tcp;
4931932
+      if (EQ(filename, "-")) {
4931932
         EmitFileName = "-";
4931932
 #ifdef RISC_USE_OSL
4931932
         dvifp = BINOPEN("Kbd:");
4931932
@@ -3339,57 +3339,68 @@ Primary author of Dvi2xx: Gustaf Neumann
4931932
 	  AssureBinary(fileno(dvifp));
4931932
 #endif
4931932
       } else {
4931932
+	/* Since this code is used only once during startup, we don't care
4931932
+	   about free()ing the allocated strings that represent filenames.
4931932
+	   It will be more work to realize proper deallocation handling than
4931932
+	   it's worth in terms of saving a few bytes. We consider these
4931932
+	   bytes actually static memory where we don't know the size in
4931932
+	   advance and don't add them to the allocated_storage count.
4931932
+	   [27 Jun 07 -js] */
4931932
 #ifdef KPATHSEA
4931932
         /* split into directory + file name */
4931932
 	int tcplen, argvlen;
4931932
 	tcp = (char *)xbasename(argv[argind]);/* this knows about any kind of slashes */
4931932
 	tcplen = strlen(tcp);
4931932
+	if ( tcplen == 0 ) {
4931932
+	  /* This happens when the DVI file name has a trailing slash; this
4931932
+	     is not a valid name. Then we terminate the argument parsing
4931932
+	     loop, a usage message will be output below. */
4931932
+	  break;
4931932
+	}
4931932
 	argvlen = strlen(argv[argind]);
4931932
 	if (tcplen == argvlen)
4931932
-	  curarea[0] = '\0';
4931932
+	  curarea = "";
4931932
 	else {
4931932
-	  (void) strcpy(curarea, argv[argind]);
4931932
+	  curarea = xstrdup(argv[argind]);
4931932
 	  curarea[argvlen-tcplen] = '\0';
4931932
 	}
4931932
 #else
4931932
         tcp = strrchr(argv[argind], '/');
4931932
         /* split into directory + file name */
4931932
         if (tcp == NULL) {
4931932
-          curarea[0] = '\0';
4931932
+          curarea[0] = "";
4931932
           tcp = argv[argind];
4931932
         } else {
4931932
-          (void) strcpy(curarea, argv[argind]);
4931932
+	  curarea = xstrdup(argv[argind]);
4931932
           curarea[tcp-argv[argind]+1] = '\0';
4931932
           tcp += 1;
4931932
         }
4931932
 #endif
4931932
 
4931932
+        curname = (char *) xmalloc(strlen(tcp)+5);  /* + space for ".dvi" */
4931932
         (void) strcpy(curname, tcp);
4931932
         /* split into file name + extension */
4931932
-        tcp1 = strrchr(tcp, '.');
4931932
+        tcp1 = strrchr(curname, '.');
4931932
         if (tcp1 == NULL) {
4931932
-          (void) strcpy(rootname, curname);
4931932
+          rootname = xstrdup(curname);
4931932
           strcat(curname, ".dvi");
4931932
         } else {
4931932
           *tcp1 = '\0';
4931932
-          (void) strcpy(rootname, curname);
4931932
+          rootname = xstrdup(curname);
4931932
           *tcp1 = '.';
4931932
         }
4931932
 
4931932
+	filename = (char *) xmalloc(strlen(curarea)+strlen(curname)+1);
4931932
         (void) strcpy(filename, curarea);
4931932
         (void) strcat(filename, curname);
4931932
 
4931932
         if ((dvifp = BINOPEN(filename)) == FPNULL) {
4931932
           /* do not insist on .dvi */
4931932
           if (tcp1 == NULL) {
4931932
-            int l = strlen(curname);
4931932
-            if (l > 4)
4931932
-              curname[l - 4] = '\0';
4931932
-            l = strlen(filename);
4931932
-            if (l > 4)
4931932
-              filename[l - 4] = '\0';
4931932
+	    filename[strlen(filename) - 4] = '\0';
4931932
+	    dvifp = BINOPEN(filename);
4931932
           }
4931932
-          if (tcp1 != NULL || (dvifp = BINOPEN(filename)) == FPNULL) {
4931932
+          if (dvifp == FPNULL) {
4931932
 #ifdef MSC5
4931932
             Fatal("%s: can't find DVI file \"%s\"\n\n",
4931932
                   G_progname, filename);
4931932
@@ -3411,7 +3422,7 @@ Primary author of Dvi2xx: Gustaf Neumann
4931932
   y_goffset = (short) MM_TO_PXL(y_offset) + y_origin;
4931932
 
4931932
   if (dvifp == FPNULL) {
4931932
-    fprintf(ERR_STREAM,"\nThis is the DVI to %s converter version %s",
4931932
+    fprintf(ERR_STREAM,"\nThis is the DVI to %s converter %s",
4931932
             PRINTER, VERSION);
4931932
 #ifdef SEVENBIT
4931932
     fprintf(ERR_STREAM,", 7bit");
4931932
@@ -3507,13 +3518,8 @@ Primary author of Dvi2xx: Gustaf Neumann
4931932
     exit(1);
4931932
   }
4931932
   if (EQ(EmitFileName, "")) {
4931932
-    if ((EmitFileName = (char *)malloc( STRSIZE )) != NULL)
4931932
-      allocated_storage += STRSIZE;
4931932
-    else
4931932
-      Fatal("Can't allocate storage of %d bytes\n",STRSIZE);
4931932
-    (void) strcpy(EmitFileName, curname);
4931932
-    if ((tcp1 = strrchr(EmitFileName, '.')))
4931932
-      *tcp1 = '\0';
4931932
+    EmitFileName = (char *) xmalloc(strlen(rootname)+sizeof(EMITFILE_EXTENSION));
4931932
+    (void) strcpy(EmitFileName, rootname);
4931932
     strcat(EmitFileName, EMITFILE_EXTENSION);
4931932
   }
4931932
   if (G_quiet)
4931932
@@ -3698,6 +3704,8 @@ bool PFlag;
4931932
 #endif
4931932
   }
4931932
   CloseFiles();
4931932
+  if ( tmp_dir[0] != '\0' )
4931932
+    rmdir (tmp_dir);			/* ignore errors */
4931932
   exit(G_errenc);
4931932
 }
4931932
 
4931932
@@ -3895,22 +3903,21 @@ char *str;
4931932
 int  n;
4931932
 #endif
4931932
 {
4931932
-  char    spbuf[STRSIZE], xs[STRSIZE], ys[STRSIZE];
4931932
-  char    *sf = NULL, *psfile = NULL;
4931932
+  char    xs[STRSIZE], ys[STRSIZE];
4931932
+  char    *include_file = NULL;
4931932
+  enum    { VerbFile, HPFile, PSFile } file_type;
4931932
   float   x,y;
4931932
   long4   x_pos, y_pos;
4931932
   KeyWord k;
4931932
   int     i, j, j1;
4931932
   static  int   GrayScale = 10, Pattern = 1;
4931932
   static  bool  GrayFill = _TRUE;
4931932
-  static  long4 p_x[80], p_y[80];
4931932
-  int llx=0, lly=0, urx=0, ury=0, rwi=0, rhi=0;
4931932
-#ifdef WIN32
4931932
-  char    *gs_path;
4931932
-#endif
4931932
+  static  long4 p_x[MAX_SPECIAL_DEFPOINTS], p_y[MAX_SPECIAL_DEFPOINTS];
4931932
+  int llx=0, lly=0, urx=0, ury=0, rwi=0;
4931932
 
4931932
   str[n] = '\0';
4931932
-  spbuf[0] = '\0';
4931932
+  for ( i=0 ; i
4931932
+    p_x[i] = p_y[i] = -1;
4931932
 
4931932
   SetPosn(h, v);
4931932
 #ifdef __riscos
4931932
@@ -3924,41 +3931,30 @@ int  n;
4931932
     /* get all keyword-value pairs */
4931932
     /* for compatibility, single words are taken as file names */
4931932
     if ( k.vt == None && access(k.Key, 0) == 0) {
4931932
-      if ( sf
4931932
-#ifdef KPATHSEA
4931932
-           && !kpse_tex_hush ("special")
4931932
-#endif
4931932
-         )
4931932
-        Warning("More than one \\special file name given. %s ignored", sf);
4931932
-      (void) strcpy(spbuf, k.Key);
4931932
-      sf = spbuf;
4931932
-      /*
4931932
-        for (j = 1; ((sf[j]=='/' ? sf[j]='\\':sf[j]) != '\0'); j++);
4931932
-        */
4931932
-    } else if ( GetKeyVal( &k, KeyTab, NKEYS, &i ) && i != -1 )
4931932
+      if ( include_file && !kpse_tex_hush ("special") ) {
4931932
+        Warning("More than one \\special file name given. %s ignored", include_file);
4931932
+	free (include_file);
4931932
+      }
4931932
+      include_file = xstrdup(k.Key);
4931932
+      file_type = VerbFile;
4931932
+    } else if ( GetKeyVal( &k, KeyTab, NKEYS, &i ) && i != -1 ) {
4931932
       switch (i) {
4931932
       case PSFILE:
4931932
-        if (sf
4931932
-#ifdef KPATHSEA
4931932
-            && !kpse_tex_hush ("special")
4931932
-#endif
4931932
-            )
4931932
-            Warning("More than one \\special file name given. %s ignored", sf);
4931932
-        (void) strcpy(spbuf, k.Val);
4931932
-        psfile = spbuf;
4931932
-        /*
4931932
-          for (j=1; ((sf[j]=='/' ? sf[j]='\\':sf[j]) != '\0'); j++);
4931932
-          */
4931932
+        if ( include_file ) {
4931932
+	  Warning("More than one \\special file name given. %s ignored", include_file);
4931932
+	  free(include_file);
4931932
+	}
4931932
+        include_file = xstrdup(k.Val);
4931932
+	file_type = PSFile;
4931932
         break;
4931932
         
4931932
       case HPFILE:
4931932
-        if (sf)
4931932
-            Warning("More than one \\special file name given. %s ignored", sf);
4931932
-        (void) strcpy(spbuf, k.Val);
4931932
-        sf = spbuf;
4931932
-        /*
4931932
-          for (j=1; ((sf[j]=='/' ? sf[j]='\\':sf[j]) != '\0'); j++);
4931932
-          */
4931932
+        if ( include_file && !kpse_tex_hush ("special") ) {
4931932
+	  Warning("More than one \\special file name given. %s ignored", include_file);
4931932
+	  free(include_file);
4931932
+	}
4931932
+        include_file = xstrdup(k.Val);
4931932
+	file_type = HPFile;
4931932
         break;
4931932
 
4931932
       case ORIENTATION:
4931932
@@ -3978,23 +3974,24 @@ int  n;
4931932
         }
4931932
 #endif
4931932
         else
4931932
-#ifdef KPATHSEA
4931932
-           if (!kpse_tex_hush ("special"))
4931932
-#endif
4931932
           Warning( "Invalid orientation (%d)given; ignored.", k.v.i);
4931932
         break;
4931932
 
4931932
       case RESETPOINTS:
4931932
-        (void) strcpy(spbuf, k.Val);
4931932
-
4931932
-        sf = NULL;
4931932
+	for ( i=0 ; i
4931932
+	  p_x[i] = p_y[i] = -1;
4931932
         break;
4931932
 
4931932
       case DEFPOINT:
4931932
-        (void) strcpy(spbuf, k.Val);
4931932
-        i = sscanf(spbuf,"%d(%[^,],%s)",&j,xs,ys);
4931932
+	/* 254 is STRSIZE-1. cpp should be used to construct that number. */
4931932
+        i = sscanf(k.Val,"%d(%254[^,],%254s)",&j,xs,ys);
4931932
         if (i>0) {
4931932
-          x_pos = h; 
4931932
+	  if ( j < 0  ||  j >= MAX_SPECIAL_DEFPOINTS ) {
4931932
+	    Warning ("defpoint %d ignored, must be between 0 and %d",
4931932
+		     j, MAX_SPECIAL_DEFPOINTS);
4931932
+	    break;
4931932
+	  }
4931932
+          x_pos = h;
4931932
           y_pos = v;
4931932
           if (i>1) {
4931932
             if (sscanf(xs,"%fpt",&x)>0) {
4931932
@@ -4011,19 +4008,32 @@ int  n;
4931932
           p_x[j]=x_pos;
4931932
           p_y[j]=y_pos;
4931932
         } else
4931932
-#ifdef KPATHSEA
4931932
-              if (!kpse_tex_hush ("special"))
4931932
-#endif
4931932
           Warning("invalid point definition\n");
4931932
-
4931932
-        sf = NULL;
4931932
         break;
4931932
 
4931932
       case FILL:
4931932
-        (void) strcpy(spbuf, k.Val);
4931932
-        i = sscanf(spbuf,"%d/%d %s",&j,&j1,xs);
4931932
+	/* 254 is STRSIZE-1. cpp should be used to construct that number. */
4931932
+        i = sscanf(k.Val,"%d/%d %254s",&j,&j1,xs);
4931932
         if (i>1) {
4931932
 #ifdef LJ
4931932
+	  if ( j < 0 || j >= MAX_SPECIAL_DEFPOINTS ) {
4931932
+	    Warning ("fill ignored, point %d must be between 0 and %d",
4931932
+		     j, MAX_SPECIAL_DEFPOINTS);
4931932
+	    break;
4931932
+	  }
4931932
+	  if ( p_x[j] == -1 ) {
4931932
+	    Warning ("fill ignored, point %d is undefined\n", j);
4931932
+	    break;
4931932
+	  }
4931932
+	  if ( j1 < 0 || j1 >= MAX_SPECIAL_DEFPOINTS ) {
4931932
+	    Warning ("fill ignored, point %d must be between 0 and %d",
4931932
+		     j1, MAX_SPECIAL_DEFPOINTS);
4931932
+	    break;
4931932
+	  }
4931932
+	  if ( p_x[j1] == -1 ) {
4931932
+	    Warning ("fill ignored, point %d is undefined\n", j1);
4931932
+	    break;
4931932
+	  }
4931932
           SetPosn(p_x[j], p_y[j]);
4931932
           x_pos = (long4)PIXROUND(p_x[j1]-p_x[j], hconv);
4931932
           y_pos = (long4)PIXROUND(p_y[j1]-p_y[j], vconv);
4931932
@@ -4044,9 +4054,6 @@ int  n;
4931932
           GrayScale = k.v.i;
4931932
           GrayFill = _TRUE;
4931932
         } else
4931932
-#ifdef KPATHSEA
4931932
-           if (!kpse_tex_hush ("special"))
4931932
-#endif
4931932
           Warning( "Invalid gray scale (%d) given; ignored.", k.v.i);
4931932
         break;
4931932
 
4931932
@@ -4055,9 +4062,6 @@ int  n;
4931932
           Pattern = k.v.i;
4931932
           GrayFill = _FALSE;
4931932
         } else
4931932
-#ifdef KPATHSEA
4931932
-           if (!kpse_tex_hush ("special"))
4931932
-#endif
4931932
           Warning( "Invalid pattern (%d) given; ignored.", k.v.i);
4931932
         break;
4931932
 
4931932
@@ -4066,75 +4070,123 @@ int  n;
4931932
       case URX: urx = k.v.i; break;
4931932
       case URY: ury = k.v.i; break;
4931932
       case RWI: rwi = k.v.i; break;
4931932
-      case RHI: rhi = k.v.i; break;
4931932
+      case RHI:
4931932
+	if (!kpse_tex_hush ("special"))
4931932
+            Warning("Whatever rhi was good for once, it is ignored now.");
4931932
+	break;
4931932
 
4931932
       default:
4931932
-#ifdef KPATHSEA
4931932
-           if (!kpse_tex_hush ("special"))
4931932
-#endif
4931932
+	if ( !kpse_tex_hush ("special") )
4931932
         Warning("Can't handle %s=%s command; ignored.", k.Key, k.Val);
4931932
         break;
4931932
       }
4931932
-      
4931932
-    else
4931932
-#ifdef KPATHSEA
4931932
-           if (!kpse_tex_hush ("special"))
4931932
-#endif
4931932
+
4931932
+    } else if (!kpse_tex_hush ("special")) {
4931932
       Warning("Invalid keyword or value in \\special - <%s> ignored", k.Key);
4931932
+    }
4931932
+
4931932
+    free (k.Key);
4931932
+    if ( k.Val != NULL )  free(k.Val);
4931932
   }
4931932
 
4931932
-  if ( sf || psfile ) {
4931932
+  if ( include_file ) {
4931932
     last_rx = last_ry = UNKNOWN;
4931932
 #ifdef IBM3812
4931932
     PMPflush;
4931932
 #endif
4931932
-    if (sf) {
4931932
-      if (i == HPFILE) 
4931932
-        CopyHPFile( sf );
4931932
-      else 
4931932
-        CopyFile( sf );
4931932
-    }
4931932
-    else
4931932
+
4931932
 #ifdef LJ
4931932
-      if (psfile) {
4931932
+      if ( file_type == PSFile) {
4931932
         /* int height = rwi * (urx - llx) / (ury - lly);*/
4931932
         int width  = urx - llx;
4931932
         int height = ury - lly;
4931932
         char cmd[255];
4931932
-        int scale_factor    = 3000 * width / rwi;
4931932
-        int adjusted_height = height * 300/scale_factor;
4931932
-        int adjusted_llx    = llx    * 300/scale_factor;
4931932
+        char *cmd_format = "%s -q -dSIMPLE -dSAFER -dNOPAUSE -sDEVICE=%s -sOutputFile=%s %s %s showpage.ps -c quit";
4931932
+        char *gs_cmd;
4931932
+        int scale_factor, adjusted_height, adjusted_llx;
4931932
         char *printer = "ljetplus"; /* use the most stupid one */
4931932
 
4931932
-
4931932
-        char scale_file_name[255];
4931932
-        char *scale_file = tmpnam(scale_file_name);
4931932
-        char *pcl_file = tmpnam(NULL);  
4931932
+        char pcl_file[STRSIZE];
4931932
+        char scale_file[STRSIZE];
4931932
         FILEPTR scalef;
4931932
 
4931932
-        if ( (scalef = BOUTOPEN(scale_file)) == FPNULL ) {
4931932
-          Warning("Unable to open file %s for writing", scale_file );
4931932
-          return;
4931932
-        }
4931932
-        fprintf(scalef, "%.2f %.2f scale\n%d %d translate\n",  
4931932
-                300.0/scale_factor, 300.0/scale_factor,
4931932
-                0, adjusted_height == height ? 0 : ury);
4931932
-        BCLOSE( scalef );
4931932
+        if ( urx == 0 || ury == 0 || rwi == 0 ) {
4931932
+  	/* Since dvips' psfile special has a different syntax, this might
4931932
+  	   well be one of those specials, i.e., a non-dviljk special. Then
4931932
+  	   the Warning should be suppressable. */
4931932
+  	if ( !kpse_tex_hush ("special") )
4931932
+  	  Warning ("Ignoring psfile special without urx, ury and rwi attributes");
4931932
+	free (include_file);
4931932
+	return;
4931932
+      }
4931932
+      scale_factor    = 3000 * width / rwi;
4931932
+      adjusted_height = height * 300/scale_factor;
4931932
+      adjusted_llx    = llx    * 300/scale_factor;
4931932
+
4931932
+      /* We cannot use mkstemp, as we cannot pass two open file descriptors
4931932
+	 portably to Ghostscript. We don't want to use tmpnam() or tempnam()
4931932
+	 either, as they have tempfile creation race conditions. Instead we
4931932
+	 create a temporary directory with mkdtemp() -- if that's available.
4931932
+	 If not, we are thrown back to tempnam(), to get our functionality
4931932
+	 at all. We need to create the temporary directory only once per
4931932
+	 run; it will be deleted in AllDone(). */
4931932
+      if ( tmp_dir[0] == '\0' ) {
4931932
+	char * base_dir;
4931932
+	if ( (base_dir = getenv("TMPDIR")) == NULL ) {
4931932
+	  base_dir = "/tmp";
4931932
+	} else if ( strlen(base_dir) > STRSIZE - sizeof("/dviljkXXXXXX/include.pcl") ) {
4931932
+	  Warning ("TMPDIR %s is too long, using /tmp instead", base_dir);
4931932
+	  base_dir = "/tmp";
4931932
+	}
4931932
+	if ( base_dir[0] == '/'  && base_dir[1] == '\0' ) {
4931932
+	  Warning ("Feeling naughty, do we? / is no temporary directory, dude");
4931932
+	  base_dir = "/tmp";
4931932
+	}
4931932
+	strcpy (tmp_dir, base_dir);
4931932
+	strcat (tmp_dir, "/dviljkXXXXXX");
4931932
+	if ( mkdtemp(tmp_dir) == NULL ) {
4931932
+	  Warning ("Could not create temporary directory %s, errno = %d; ignoring include file special",
4931932
+		   tmp_dir, errno);
4931932
+	  return;
4931932
+	}
4931932
+      }
4931932
+      strcpy(pcl_file, tmp_dir);
4931932
+      strcat(pcl_file, "/include.pcl");
4931932
+      strcpy(scale_file, tmp_dir);
4931932
+      strcat(scale_file, "/scale.ps");
4931932
+
4931932
+      if ( (scalef = BOUTOPEN(scale_file)) == FPNULL ) {
4931932
+	Warning("Unable to open file %s for writing", scale_file );
4931932
+	free (include_file);
4931932
+	unlink(scale_file);		/* ignore error */
4931932
+	return;
4931932
+      }
4931932
+      fprintf(scalef, "%.2f %.2f scale\n%d %d translate\n",
4931932
+	      300.0/scale_factor, 300.0/scale_factor,
4931932
+	      0, adjusted_height == height ? 0 : ury);
4931932
+      BCLOSE( scalef );
4931932
 
4931932
 #ifdef WIN32
4931932
-	gs_path = getenv("GS_PATH");
4931932
-	if (!gs_path)
4931932
-	  gs_path = "gswin32c.exe";
4931932
-        sprintf(cmd,"%s -q -dSIMPLE -dSAFER -dNOPAUSE -sDEVICE=%s -sOutputFile=%s %s %s showpage.ps -c quit",
4931932
-		gs_path, printer, pcl_file, scale_file, psfile);
4931932
+      if ( (gs_cmd = getenv("GS_PATH")) == NULL )
4931932
+	gs_cmd = "gswin32c.exe";
4931932
 #else
4931932
-        sprintf(cmd,"gs -q -dSIMPLE -dSAFER -dNOPAUSE -sDEVICE=%s -sOutputFile=%s %s %s showpage.ps -c quit",
4931932
-                printer, pcl_file, scale_file, psfile);
4931932
+      gs_cmd = "gs";
4931932
 #endif
4931932
+      if ( strlen(cmd_format)-10 + strlen(gs_cmd) + strlen(printer) +
4931932
+	       strlen(pcl_file) + strlen(scale_file) + strlen(include_file) +1 >
4931932
+	   sizeof(cmd) ) {
4931932
+	Warning ("Ghostscript command for %s would be too long, skipping special", include_file);
4931932
+	free (include_file);
4931932
+	unlink(scale_file);		/* ignore errors */
4931932
+	unlink(pcl_file);
4931932
+	return;
4931932
+      }
4931932
+      sprintf(cmd, cmd_format,
4931932
+	      gs_cmd, printer, pcl_file, scale_file, include_file);
4931932
 #ifdef DEBUGGS   
4931932
         fprintf(stderr,
4931932
           "PS-file '%s' w=%d, h=%d, urx=%d, ury=%d, llx=%d, lly=%d, rwi=%d\n",
4931932
-                psfile, urx - llx, height, urx,ury,llx,lly, rwi);
4931932
+	      include_file, urx - llx, height, urx,ury,llx,lly, rwi);
4931932
         fprintf(stderr,"%s\n",cmd);
4931932
 #endif
4931932
         if (system(cmd)) {
4931932
@@ -4158,11 +4210,21 @@ int  n;
4931932
 #endif
4931932
 
4931932
           CopyHPFile( pcl_file );
4931932
-          /* unlink(pcl_file); */
4931932
-          /* unlink(scale_file); */
4931932
-        }
4931932
       }
4931932
+      unlink(scale_file);		/* ignore errors */
4931932
+      unlink(pcl_file);
4931932
+    }
4931932
+    else
4931932
 #endif /* LJ */
4931932
+
4931932
+    if ( file_type == HPFile )
4931932
+      CopyHPFile( include_file );
4931932
+    else if ( file_type == VerbFile )
4931932
+      CopyFile( include_file );
4931932
+    else
4931932
+      Warning ("This can't happen: unknown file_type value %d", file_type);
4931932
+
4931932
+    if ( include_file != NULL )  free(include_file);
4931932
   }
4931932
 }
4931932
 
4931932
@@ -4173,12 +4235,11 @@ int  n;
4931932
 /**********************************************************************/
4931932
 /*****************************  GetKeyStr  ****************************/
4931932
 /**********************************************************************/
4931932
-/* extract first keyword-value pair from string (value part may be null)
4931932
- * return pointer to remainder of string
4931932
- * return NULL if none found
4931932
+/* Extract first keyword-value pair from string (value part may be null),
4931932
+ * keyword and value are allocated and must be free by caller.
4931932
+ * Return pointer to remainder of string,
4931932
+ * return NULL if none found.
4931932
  */
4931932
-char    KeyStr[STRSIZE];
4931932
-char    ValStr[STRSIZE];
4931932
 #if NeedFunctionPrototypes
4931932
 char *GetKeyStr(char *str, KeyWord *kw )
4931932
 #else
4931932
@@ -4187,39 +4248,46 @@ char    *str;
4931932
 KeyWord *kw;
4931932
 #endif
4931932
 {
4931932
-  char    *s, *k, *v, t;
4931932
+  char *s, *start;
4931932
+  char save_char, quote_char;
4931932
   if ( !str )
4931932
     return( NULL );
4931932
   for (s = str; *s == ' '; s++)
4931932
     ;          /* skip over blanks */
4931932
   if (*s == '\0')
4931932
     return( NULL );
4931932
-  for (k = KeyStr; /* extract keyword portion */
4931932
-       *s != ' ' && *s != '\0' && *s != '=';
4931932
-       *k++ = *s++)
4931932
-    ;
4931932
-  *k = '\0';
4931932
-  kw->Key = KeyStr;
4931932
-  kw->Val = v = NULL;
4931932
+  start = s++;				/* start of keyword */
4931932
+  while ( *s != ' ' && *s != '\0' && *s != '=' )  /* locate end */
4931932
+    s++;
4931932
+  save_char = *s;
4931932
+  *s = '\0';
4931932
+  kw->Key = xstrdup(start);
4931932
+  kw->Val = NULL;
4931932
   kw->vt = None;
4931932
-  for ( ; *s == ' '; s++)
4931932
-    ;            /* skip over blanks */
4931932
-  if ( *s != '=' )         /* look for "=" */
4931932
+  if ( save_char == '\0' )		/* shortcut when we're at the end */
4931932
+    return (s);
4931932
+  *s = save_char;			/* restore keyword end char */
4931932
+  while ( *s == ' ' ) s++ ;		/* skip over blanks */
4931932
+  if ( *s != '=' )			/* no "=" means no value */
4931932
     return( s );
4931932
-  for (s++; *s == ' '; s++);      /* skip over blanks */
4931932
-  if ( *s == '\'' || *s == '\"' )  /* get string delimiter */
4931932
-    t = *s++;
4931932
+  for (s++; *s == ' '; s++)
4931932
+    ;					/* skip over blanks */
4931932
+  if ( *s == '\'' || *s == '\"' )	/* get string delimiter */
4931932
+    quote_char = *s++;
4931932
   else
4931932
-    t = ' ';
4931932
-  for (v = ValStr; /* copy value portion up to delim */
4931932
-       *s != t && *s != '\0';
4931932
-       *v++ = *s++)
4931932
-    ;
4931932
-  if ( t != ' ' && *s == t )
4931932
-    s++;
4931932
-  *v = '\0';
4931932
-  kw->Val = ValStr;
4931932
+    quote_char = ' ';
4931932
+  start = s;				/* no increment, might be "" as value */
4931932
+  while ( *s != quote_char && *s != '\0' )
4931932
+    s++;			  /* locate end of value portion */
4931932
+  save_char = *s;
4931932
+  *s = '\0';
4931932
+  kw->Val = xstrdup(start);
4931932
   kw->vt = String;
4931932
+  if ( save_char != '\0' ) {		/* save_char is now quote_char */
4931932
+    *s = save_char;
4931932
+    if ( quote_char != ' ' )		/* we had real quote chars */
4931932
+      s++;
4931932
+  }
4931932
   return( s );
4931932
 }
4931932
 
4931932
@@ -4819,13 +4887,14 @@ struct font_entry *fontptr;
4931932
      the resident fonts.  */
4931932
   if (tfm_read_info(fontptr->n, &tfm_info)
4931932
       && tfm_info.family[0]
4931932
-      && strcmp((char *)tfm_info.family, "HPAUTOTFM") == 0) {
4931932
+      && EQ((char *)tfm_info.family, "HPAUTOTFM")) {
4931932
     unsigned i;
4931932
     double factor = fontptr->s / (double)0x100000;
4931932
 
4931932
     resident_count++;
4931932
     fontptr->resident_p = _TRUE;
4931932
-    strcpy(fontptr->symbol_set, (char *)tfm_info.coding_scheme);
4931932
+    strncpy(fontptr->symbol_set, (char *)tfm_info.coding_scheme, 39);
4931932
+    fontptr->symbol_set[39] = '\0';
4931932
     fontptr->resid = tfm_info.typeface_id;
4931932
     fontptr->spacing = tfm_info.spacing;
4931932
     fontptr->style = tfm_info.style;
4931932
@@ -4878,7 +4947,7 @@ struct font_entry *fontptr;
4931932
     fontptr->resident_p = _FALSE;
4931932
 
4931932
     if (tfm_info.family[0]
4931932
-        && strcmp((char *)tfm_info.family, "UNSPECIFIED") == 0) {
4931932
+        && EQ((char *)tfm_info.family, "UNSPECIFIED")) {
4931932
       Warning("font family for %s is UNSPECIFIED; need to run dvicopy?",
4931932
               fontptr->n);
4931932
       fontptr->font_file_id = NO_FILE;
4931932
@@ -5031,10 +5100,9 @@ printf("[%ld]=%lf * %lf * %lf + 0.5 = %l
4931932
   if (tfontptr->resident_p)
4931932
     return;
4931932
 
4931932
-  if (!(resident_font_located)) {
4931932
+  if (!(resident_font_located))
4931932
 #endif
4931932
 
4931932
-#ifdef KPATHSEA
4931932
     {
4931932
       kpse_glyph_file_type font_ret;
4931932
       char *name;
4931932
@@ -5047,9 +5115,9 @@ printf("[%ld]=%lf * %lf * %lf + 0.5 = %l
4931932
       if (name)
4931932
         {
4931932
           font_found = _TRUE;
4931932
-          strcpy (tfontptr->name, name);
4931932
-          free (name);
4931932
-          
4931932
+          tfontptr->name = name;
4931932
+          allocated_storage += strlen(name)+1;
4931932
+
4931932
           if (!FILESTRCASEEQ (tfontptr->n, font_ret.name)) {
4931932
               fprintf (stderr,
4931932
                        "dvilj: Font %s not found, using %s at %d instead.\n",
4931932
@@ -5071,29 +5139,6 @@ printf("[%ld]=%lf * %lf * %lf + 0.5 = %l
4931932
             tfontptr->n, dpi);
4931932
         }
4931932
     }
4931932
-#else /* not KPATHSEA */
4931932
-    if (!(findfile(PXLpath,
4931932
-                   tfontptr->n,
4931932
-                   tfontptr->font_mag,
4931932
-                   tfontptr->name,
4931932
-                   _FALSE,
4931932
-                   0))) {
4931932
-      Warning(tfontptr->name); /* contains error messsage */
4931932
-      tfontptr->font_file_id = NO_FILE;
4931932
-#ifdef __riscos
4931932
-      MakeMetafontFile(PXLpath, tfontptr->n, tfontptr->font_mag);
4931932
-#endif
4931932
-    }
4931932
-    else {
4931932
-      font_found = _TRUE;
4931932
-      if (G_verbose)
4931932
-        fprintf(ERR_STREAM,"%d: using font <%s>\n", plusid, tfontptr->name);
4931932
-    }
4931932
-#endif /* not KPATHSEA */
4931932
-
4931932
-#ifdef LJ_RESIDENT_FONTS
4931932
-  }
4931932
-#endif
4931932
 
4931932
   tfontptr->plusid = plusid;
4931932
   plusid++;
860dd42
diff -up texlive-2007/texk/dviljk/dvi2xx.h.dviljktemp texlive-2007/texk/dviljk/dvi2xx.h
860dd42
--- texlive-2007/texk/dviljk/dvi2xx.h.dviljktemp	2006-01-17 22:41:51.000000000 +0100
860dd42
+++ texlive-2007/texk/dviljk/dvi2xx.h	2008-08-27 13:32:10.000000000 +0200
4931932
@@ -10,8 +10,8 @@
4931932
 
4931932
 #ifdef KPATHSEA
4931932
 #include <kpathsea/config.h>
4931932
+#include <kpathsea/c-std.h>
4931932
 #include <kpathsea/c-limits.h>
4931932
-#include <kpathsea/c-memstr.h>
4931932
 #include <kpathsea/magstep.h>
4931932
 #include <kpathsea/proginit.h>
4931932
 #include <kpathsea/progname.h>
4931932
@@ -24,6 +24,7 @@
4931932
 #include <string.h>
4931932
 #include <stdio.h>
4931932
 #include <stdlib.h>
4931932
+#include <unistd.h>
4931932
 #ifdef  unix
4931932
 #include <limits.h>
4931932
 #endif
4931932
@@ -41,9 +42,6 @@
4931932
 #ifdef MSC5
4931932
 #include <dos.h>     /* only for binaryopen on device  */
4931932
 #endif
4931932
-#if defined (unix) && !defined (KPATHSEA)
4931932
-#include <limits.h>
4931932
-#endif
4931932
 
4931932
 
4931932
 #include "config.h"
4931932
@@ -116,6 +114,7 @@
4931932
 #define  HUGE_SIZE  (unsigned char) 2
4931932
 #define  HUGE_CHAR_PATTERN 32767l
4931932
 #define  BYTES_PER_PIXEL_LINE 500    /* max number of bytes per pixel line */
4931932
+#define  MAX_SPECIAL_DEFPOINTS 80    /* max number of defpoint specials */
4931932
 
4931932
 
4931932
 #define PK_POST 245
4931932
@@ -281,7 +280,14 @@ char *MFMODE     = MFMODE600;
4931932
 #define VisChar(c) (unsigned char)(c)
4931932
 #endif
4931932
 
4931932
-#define GetBytes(fp,buf,n) read_multi(buf,1,n,fp) /* used to be a function */
4931932
+/* Used to be a function. buf is always an array, never a pointer.
4931932
+   Without that invariant, we would have to introduce full dynamic
4931932
+   memory management in this driver -- probably it would be easier to
4931932
+   write a new one. [27 Jun 07 -js] */
4931932
+#define GetBytes(fp,buf,n) \
4931932
+    ( sizeof(buf) != sizeof(void *) && sizeof(buf) > n ? \
4931932
+        read_multi(buf, 1, n, fp) \
4931932
+      : Fatal("Try to read %d bytes in an array of size %d", n, sizeof(buf)) )
4931932
 
4931932
 
4931932
 /**********************************************************************/
4931932
@@ -307,6 +313,7 @@ int     printf();
4931932
 int     sscanf();
4931932
 int     strcmp();
4931932
 char   *strcpy();
4931932
+char   *strncpy();
4931932
 #   ifdef MSC5
4931932
 unsigned int strlen();
4931932
 #   endif
4931932
@@ -393,7 +400,7 @@ struct font_entry {    /* font entry */
4931932
     char n[STRSIZE];          /* FNT_DEF command parameters                */
4931932
     long4    font_mag;         /* computed from FNT_DEF s and d parameters  */
4931932
     /*char psname[STRSIZE];*/ /* PostScript name of the font               */
4931932
-    char    name[STRSIZE];    /* full name of PXL file                     */
4931932
+    char    *name;	       /* full name of PXL file                     */
4931932
     FILEPTR font_file_id;      /* file identifier (NO_FILE if none)         */
4931932
 #ifdef USEPXL
4931932
     long4    magnification;    /* magnification read from PXL file          */
4931932
@@ -487,8 +494,8 @@ void    LoadAChar DVIPROTO((long4, regis
4931932
 long4   NoSignExtend DVIPROTO((FILEPTR, int));
4931932
 void    OpenFontFile DVIPROTO((void));
4931932
 long4   PixRound DVIPROTO((long4, long4));
4931932
-void    PkRaster DVIPROTO((struct char_entry *, int)); 
4931932
-void    RasterLine DVIPROTO((struct char_entry *, unsigned int, 
4931932
+void    PkRaster DVIPROTO((struct char_entry *, int));
4931932
+void    RasterLine DVIPROTO((struct char_entry *, unsigned int,
4931932
 			     unsigned int, unsigned char *));
4931932
 void    RasterChar DVIPROTO((struct char_entry *));
4931932
 void    ReadFontDef DVIPROTO((long4));
4931932
@@ -534,11 +541,12 @@ bool    LastPageSpecified = _FALSE;
4931932
 #ifndef KPATHSEA
4931932
 char   *PXLpath = FONTAREA;
4931932
 #endif
4931932
-char    G_progname[STRSIZE];     /* program name                        */
4931932
-char    filename[STRSIZE];       /* DVI file name                       */
4931932
-char    rootname[STRSIZE];       /* DVI filename without extension      */
4931932
+char   *G_progname;		 /* program name                        */
4931932
+char   *filename;	         /* DVI file name                       */
4931932
+char   *rootname;		 /* DVI filename without extension      */
4931932
 char   *HeaderFileName = "";     /* file name & path of Headerfile      */
4931932
 char   *EmitFileName = "";       /* file name & path for output         */
4931932
+char    tmp_dir[STRSIZE] = "";	 /* temporary directory for auxilliary files */
4931932
 #ifdef IBM3812
4931932
 bool    FirstAlternate = _FALSE; /* first page from alternate casette ?   */
4931932
 #endif