wineps: escape document title

Huw D M Davies h.davies1 at physics.ox.ac.uk
Wed Nov 9 05:56:35 CST 2005


        Huw Davies <huw at codeweavers.com>
        Escape non-printable characters in the document title and also
        truncate it to 0x80 characters.
-- 
Huw Davies
huw at codeweavers.com
Index: dlls/wineps/ps.c
===================================================================
RCS file: /home/wine/wine/dlls/wineps/ps.c,v
retrieving revision 1.33
diff -u -p -r1.33 ps.c
--- dlls/wineps/ps.c	26 Aug 2005 08:53:31 -0000	1.33
+++ dlls/wineps/ps.c	9 Nov 2005 11:54:19 -0000
@@ -250,11 +250,65 @@ INT PSDRV_WriteFeature(HANDLE16 hJob, ch
     return 1;
 }
 
-
+/********************************************************
+ *         escape_title
+ *
+ * Helper for PSDRV_WriteHeader.  Escape any non-printable characters
+ * as octal.  If we've had to use an escape then surround the entire string
+ * in brackets.  Truncate string to represent at most 0x80 characters.
+ *
+ */
+static char *escape_title(LPCSTR str)
+{
+    char *ret, *cp;
+    int i, extra = 0;
+
+    if(!str)
+    {
+        ret = HeapAlloc(GetProcessHeap(), 0, 1);
+        *ret = '\0';
+        return ret;
+    }
+
+    for(i = 0; i < 0x80 && str[i]; i++)
+    {
+        if(!isprint(str[i]))
+           extra += 3;
+    }
+
+    if(!extra)
+    {
+        ret = HeapAlloc(GetProcessHeap(), 0, i + 1);
+        memcpy(ret, str, i);
+        ret[i] = '\0';
+        return ret;
+    }
+
+    extra += 2; /* two for the brackets */
+    cp = ret = HeapAlloc(GetProcessHeap(), 0, i + extra + 1);
+    *cp++ = '(';
+    for(i = 0; i < 0x80 && str[i]; i++)
+    {
+        if(!isprint(str[i]))
+        {
+            BYTE b = (BYTE)str[i];
+            *cp++ = '\\';
+            *cp++ = ((b >> 6) & 0x7) + '0';
+            *cp++ = ((b >> 3) & 0x7) + '0';
+            *cp++ = ((b)      & 0x7) + '0';
+        }
+        else
+            *cp++ = str[i];
+    }
+    *cp++ = ')';
+    *cp = '\0';
+    return ret;
+}
+       
 
 INT PSDRV_WriteHeader( PSDRV_PDEVICE *physDev, LPCSTR title )
 {
-    char *buf;
+    char *buf, *escaped_title;
     INPUTSLOT *slot;
     PAGESIZE *page;
     DUPLEX *duplex;
@@ -263,8 +317,9 @@ INT PSDRV_WriteHeader( PSDRV_PDEVICE *ph
 
     TRACE("'%s'\n", debugstr_a(title));
 
+    escaped_title = escape_title(title);
     buf = HeapAlloc( PSDRV_Heap, 0, sizeof(psheader) +
-                     (title ? strlen(title) : 0) + 30 );
+                     strlen(escaped_title) + 30 );
     if(!buf) {
         WARN("HeapAlloc failed\n");
         return 0;
@@ -278,8 +333,9 @@ INT PSDRV_WriteHeader( PSDRV_PDEVICE *ph
     ury = physDev->ImageableArea.top * 72.0 / physDev->logPixelsY;
     /* FIXME should do something better with BBox */
 
-    sprintf(buf, psheader, title ? title : "", llx, lly, urx, ury);
+    sprintf(buf, psheader, escaped_title, llx, lly, urx, ury);
 
+    HeapFree(GetProcessHeap(), 0, escaped_title);
     if( WriteSpool16( physDev->job.hJob, buf, strlen(buf) ) !=
 	                                             strlen(buf) ) {
         WARN("WriteSpool error\n");



More information about the wine-patches mailing list