winex11.drv[4/6]: wintab: store string internally as Unicode

Mikolaj Zalewski mikolajz at google.com
Thu Aug 30 20:52:08 CDT 2007


In one place I convert such a string back to UNIXCP but that allows me
to do the conversion in an outer loop.
-------------- next part --------------
From 7437bf66293f7b169991a9cf6cacefb1c415c90d Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Miko=C5=82aj_Zalewski?= <mikolaj at zalewski.pl>
Date: Thu, 30 Aug 2007 16:19:50 -0700
Subject: [PATCH] winex11.drv: wintab: store string internally as Unicode

---
 dlls/winex11.drv/wintab.c |   71 ++++++++++++++++++++++++++++++++++-----------
 1 files changed, 54 insertions(+), 17 deletions(-)

diff --git a/dlls/winex11.drv/wintab.c b/dlls/winex11.drv/wintab.c
index d826c44..9b40ffd 100644
--- a/dlls/winex11.drv/wintab.c
+++ b/dlls/winex11.drv/wintab.c
@@ -22,8 +22,11 @@
 #include "wine/port.h"
 
 #include <stdlib.h>
+#include <stdarg.h>
 
 #include "windef.h"
+#include "winbase.h"
+#include "winnls.h"
 #include "x11drv.h"
 #include "wine/library.h"
 #include "wine/debug.h"
@@ -31,9 +34,11 @@
 
 WINE_DEFAULT_DEBUG_CHANNEL(wintab32);
 
+#define WT_MAX_NAME_LEN 256
+
 typedef struct tagWTI_CURSORS_INFO
 {
-    CHAR   NAME[256];
+    WCHAR   NAME[WT_MAX_NAME_LEN];
         /* a displayable zero-terminated string containing the name of the
          * cursor.
          */
@@ -47,7 +52,7 @@ typedef struct tagWTI_CURSORS_INFO
         /* the number of buttons on this cursor. */
     BYTE    BUTTONBITS;
         /* the number of bits of raw button data returned by the hardware.*/
-    CHAR   BTNNAMES[1024]; /* FIXME: make this dynamic */
+    WCHAR   BTNNAMES[1024]; /* FIXME: make this dynamic */
         /* a list of zero-terminated strings containing the names of the
          * cursor's buttons. The number of names in the list is the same as the
          * number of buttons on the cursor. The names are separated by a single
@@ -129,7 +134,7 @@ typedef struct tagWTI_CURSORS_INFO
 
 typedef struct tagWTI_DEVICES_INFO
 {
-    CHAR   NAME[256];
+    WCHAR   NAME[WT_MAX_NAME_LEN];
         /* a displayable null- terminated string describing the device,
          * manufacturer, and revision level.
          */
@@ -191,7 +196,7 @@ typedef struct tagWTI_DEVICES_INFO
         /* a 3-element array describing the tablet's rotation range and
          * resolution capabilities.
          */
-    CHAR   PNPID[256];
+    WCHAR   PNPID[WT_MAX_NAME_LEN];
         /* a null-terminated string containing the devices Plug and Play ID.*/
 }   WTI_DEVICES_INFO, *LPWTI_DEVICES_INFO;
 
@@ -233,7 +238,7 @@ static INT           button_state[10];
 
 #define             CURSORMAX 10
 
-static LOGCONTEXTA      gSysContext;
+static LOGCONTEXTW      gSysContext;
 static WTI_DEVICES_INFO gSysDevice;
 static WTI_CURSORS_INFO gSysCursor[CURSORMAX];
 static INT              gNumCursors;
@@ -305,8 +310,8 @@ void X11DRV_LoadTabletInfo(HWND hwnddefault)
     hwndTabletDefault = hwnddefault;
 
     /* Do base initializaion */
-    strcpy(gSysContext.lcName, "Wine Tablet Context");
-    strcpy(gSysDevice.NAME,"Wine Tablet Device");
+    MultiByteToWideChar(CP_UTF8, 0, "Wine Tablet Context", -1, gSysContext.lcName, WT_MAX_NAME_LEN);
+    MultiByteToWideChar(CP_UTF8, 0, "Wine Tablet Device", -1, gSysDevice.NAME, WT_MAX_NAME_LEN);
 
     gSysContext.lcOptions = CXO_SYSTEM;
     gSysContext.lcLocks = CXL_INSIZE | CXL_INASPECT | CXL_MARGIN |
@@ -337,7 +342,7 @@ void X11DRV_LoadTabletInfo(HWND hwnddefault)
     gSysDevice.PKTDATA =
         PK_CONTEXT | PK_STATUS | PK_SERIAL_NUMBER| PK_TIME | PK_CURSOR |
         PK_BUTTONS |  PK_X | PK_Y | PK_NORMAL_PRESSURE | PK_ORIENTATION;
-    strcpy(gSysDevice.PNPID,"non-pluginplay");
+    MultiByteToWideChar(CP_UTF8, 0, "non-pluginplay", -1, gSysDevice.PNPID, WT_MAX_NAME_LEN);
 
     wine_tsx11_lock();
 
@@ -396,7 +401,8 @@ void X11DRV_LoadTabletInfo(HWND hwnddefault)
                 continue;
             }
 
-            strcpy(cursor->NAME,target->name);
+            MultiByteToWideChar(CP_UNIXCP, 0, target->name, -1, cursor->NAME, WT_MAX_NAME_LEN);
+            cursor->NAME[WT_MAX_NAME_LEN - 1] = 0;
 
             cursor->ACTIVE = 1;
             cursor->PKTDATA = PK_TIME | PK_CURSOR | PK_BUTTONS |  PK_X | PK_Y |
@@ -408,9 +414,9 @@ void X11DRV_LoadTabletInfo(HWND hwnddefault)
             cursor->NPBTNMARKS[0] = 0 ;
             cursor->NPBTNMARKS[1] = 1 ;
             cursor->CAPABILITIES = CRC_MULTIMODE;
-            if (strcasecmp(cursor->NAME,"stylus")==0)
+            if (strcasecmp(target->name,"stylus")==0)
                 cursor->TYPE = 0x4825;
-            if (strcasecmp(cursor->NAME,"eraser")==0)
+            if (strcasecmp(target->name,"eraser")==0)
                 cursor->TYPE = 0xc85a;
 
 
@@ -490,16 +496,21 @@ void X11DRV_LoadTabletInfo(HWND hwnddefault)
                         break;
                     case ButtonClass:
                     {
-                        CHAR *ptr = cursor->BTNNAMES;
+                        WCHAR *ptr = cursor->BTNNAMES;
                         int i;
 
                         Button = (XButtonInfoPtr) any;
                         cursor->BUTTONS = Button->num_buttons;
                         for (i = 0; i < cursor->BUTTONS; i++)
                         {
-                            strcpy(ptr,cursor->NAME);
+                            lstrcpynW(ptr, cursor->NAME, 8);
                             ptr+=8;
+                            *(ptr - 1) = 0;
+                            /* avoid teoretical string overflow - see FIXME in WTI_CURSOR_INFO */
+                            if ((i*8 + 9) >= sizeof(cursor->NAME)/sizeof(cursor->NAME[0]))
+                                break;
                         }
+                        *ptr = 0;
                     }
                     break;
                 }
@@ -704,10 +715,12 @@ int X11DRV_AttachEventQueueToTablet(HWND hOwner)
     X11DRV_expect_error(data->display,Tablet_ErrorHandler,NULL);
     for (cur_loop=0; cur_loop < gNumCursors; cur_loop++)
     {
+        char   cursorNameA[WT_MAX_NAME_LEN];
         int    event_number=0;
 
+        WideCharToMultiByte(CP_UNIXCP, 0, gSysCursor[cur_loop].NAME, -1, cursorNameA, WT_MAX_NAME_LEN, NULL, NULL);
         for (loop=0; loop < num_devices; loop ++)
-            if (strcmp(devices[loop].name,gSysCursor[cur_loop].NAME)==0)
+            if (strncmp(devices[loop].name, cursorNameA, WT_MAX_NAME_LEN)==0)
                 target = &devices[loop];
 
         TRACE("Opening cursor %i id %i\n",cur_loop,(INT)target->id);
@@ -774,17 +787,40 @@ static inline int CopyTabletData(LPVOID target, LPCVOID src, INT size)
     return(size);
 }
 
+static inline int CopyTabletStringN(LPVOID target, LPWSTR str, INT buf_size)
+{
+    if (target != NULL)
+    {
+        char *buf = (char *)target;
+        WideCharToMultiByte(CP_ACP, 0, str, -1, buf, buf_size, NULL, NULL);
+        buf[buf_size - 1] = 0;
+    }
+    return buf_size;
+}
+
+static inline int CopyTabletString(LPVOID target, LPWSTR str)
+{
+    int len = lstrlenW(str) + 1;
+    return WideCharToMultiByte(CP_ACP, 0, str, len, (char *)target, target ? len : 0, NULL, NULL);
+}
+
 #define WTINFO_COPY_STRING_N(data_index, string, len) \
     if (nIndex == data_index || nIndex == 0) \
     { \
-        int size = CopyTabletData(lpBuffer, string, len); \
+        int size = CopyTabletStringN(lpBuffer, string, len); \
         total_size += size; \
         if (lpBuffer != NULL) \
             lpBuffer += size; \
     }
 
 #define WTINFO_COPY_STRING(data_index, string) \
-    WTINFO_COPY_STRING_N(data_index, string, strlen(string)+1);
+    if (nIndex == data_index || nIndex == 0) \
+    { \
+        int size = CopyTabletString(lpBuffer, string); \
+        total_size += size; \
+        if (lpBuffer != NULL) \
+            lpBuffer += size; \
+    }
 
 #define WTINFO_COPY_DATA(data_index, data) \
     if (nIndex == data_index || nIndex == 0) \
@@ -856,8 +892,9 @@ UINT X11DRV_WTInfoA(UINT wCategory, UINT nIndex, LPVOID lpOutput)
     {
         WORD impl_version = 0x100;
         WORD spec_version  = 0x101;
+        WCHAR wintabid[] = {'W','i','n','e',' ','W','i','n','t','a','b',' ','1','.','1',0};
 
-        WTINFO_COPY_STRING(IFC_WINTABID, "Wine Wintab 1.1");
+        WTINFO_COPY_STRING(IFC_WINTABID, wintabid);
         WTINFO_COPY_DATA(IFC_SPECVERSION, spec_version);
         WTINFO_COPY_DATA(IFC_IMPLVERSION, impl_version);
         WTINFO_END_CATEGORY
-- 
1.4.4.2


More information about the wine-patches mailing list