dlls/wineps/afm.c

Morten Welinder terra-wine at diku.dk
Wed Feb 14 21:01:19 CST 2001


This patch fixes a ton of problems popping up with compressed afm
files (and other files not quite maching the code's expectation).
Compressed afm files still won't be handled right, but at least
things will not crash.


2001-02-14  Morten Welinder  <terra-wine at diku.dk>

	* dlls/wineps/afm.c (PSDRV_AFMGetCharMetrics): Use unsigned chars
	(since isspace is used).
	(PSDRV_AFMParse): Don't crash on missing font name.  Use unsigned
	chars.  Fix peculiar inconsistent indentation.  Don't leak a FILE.
	Catch problematic files with no line feed in them.  Don't mix
	characters and integers.  Don't overrun the buffer.


Index: afm.c
===================================================================
RCS file: /home/wine/wine/dlls/wineps/afm.c,v
retrieving revision 1.6
diff -u -r1.6 afm.c
--- afm.c	2001/02/13 20:18:47	1.6
+++ afm.c	2001/02/15 02:45:08
@@ -34,8 +34,8 @@
  */
 static void PSDRV_AFMGetCharMetrics(AFM *afm, FILE *fp)
 {
-    char line[256], valbuf[256];
-    char *cp, *item, *value, *curpos, *endpos;
+    unsigned char line[256], valbuf[256];
+    unsigned char *cp, *item, *value, *curpos, *endpos;
     int i;
     AFMMETRICS *metric;
 
@@ -63,7 +63,7 @@
 	    while(isspace(*value))
 	        value++;
 	    cp = endpos = strchr(value, ';');
-	    if (!cp) { ERR("missing ;, failed.\n"); return; }
+	    if (!cp) { ERR("missing ;, failed. [%s]\n", line); return; }
 	    while(isspace(*--cp))
 	        ;
 	    memcpy(valbuf, value, cp - value + 1);
@@ -126,11 +126,12 @@
 static AFM *PSDRV_AFMParse(char const *file)
 {
     FILE *fp;
-    char buf[256];
-    char *value;
+    unsigned char buf[256];
+    unsigned char *value;
     AFM *afm;
-    char *cp;
+    unsigned char *cp;
     int afmfile = 0; 
+    int c;
 
     TRACE("parsing '%s'\n", file);
 
@@ -146,31 +147,29 @@
     }
 
     cp = buf; 
-    while ( ( *cp = fgetc ( fp ) ) != EOF ) {
-      if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-1 ) {
-	if ( cp == buf ) 
-	  continue;
-	*(cp+1)='\0';
-      }
-      else {
-	cp ++; 
-	continue;
-      }
+    while ( ( c = fgetc ( fp ) ) != EOF ) {
+	*cp = c;
+	if ( *cp == '\r' || *cp == '\n' || cp - buf == sizeof(buf)-2 ) {
+	    if ( cp == buf ) 
+		continue;
+	    *(cp+1)='\0';
+	}
+	else {
+	    cp ++; 
+	    continue;
+	}
       
-      cp = buf + strlen(buf);
-      do {
-	  *cp = '\0';
+	cp = buf + strlen(buf);
+	do {
+	    *cp = '\0';
 	    cp--;
 	} while(cp > buf && isspace(*cp));
+
 	cp = buf; 
 
-	if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) ) {
-	  HeapFree ( PSDRV_Heap, 0, afm ); 
-	  return NULL;
-	}
-	else {
-	  afmfile = 1; 
-	}
+	if ( afmfile == 0 && strncmp ( buf, "StartFontMetrics", 16 ) )
+	    break;
+	afmfile = 1; 
 
         value = strchr(buf, ' ');
 	if(value)
@@ -276,8 +275,15 @@
     }
     fclose(fp);
 
-    if(afm->FontName == NULL)
+    if (afmfile == 0) {
+	HeapFree ( PSDRV_Heap, 0, afm ); 
+	return NULL;
+    }
+
+    if(afm->FontName == NULL) {
         WARN("%s contains no FontName.\n", file);
+	afm->FontName = HEAP_strdupA(PSDRV_Heap, 0, "nofont");
+    }
     if(afm->FullName == NULL)
         afm->FullName = HEAP_strdupA(PSDRV_Heap, 0, afm->FontName);
     if(afm->FamilyName == NULL)




More information about the wine-patches mailing list