Clean up internal clipboard handling

We now filter incoming data, which means we can start assuming the
clipboard data is always null terminated. This allows us to clean
up a lot of the internal handling.
diff --git a/unix/x0vncserver/XDesktop.cxx b/unix/x0vncserver/XDesktop.cxx
index 564b2d5..8be9aa3 100644
--- a/unix/x0vncserver/XDesktop.cxx
+++ b/unix/x0vncserver/XDesktop.cxx
@@ -406,7 +406,7 @@
 #endif
 }
 
-void XDesktop::clientCutText(const char* str, int len) {
+void XDesktop::clientCutText(const char* str) {
 }
 
 ScreenSet XDesktop::computeScreenLayout()
diff --git a/unix/x0vncserver/XDesktop.h b/unix/x0vncserver/XDesktop.h
index 3e85aac..840d433 100644
--- a/unix/x0vncserver/XDesktop.h
+++ b/unix/x0vncserver/XDesktop.h
@@ -56,7 +56,7 @@
   virtual void pointerEvent(const rfb::Point& pos, int buttonMask);
   KeyCode XkbKeysymToKeycode(Display* dpy, KeySym keysym);
   virtual void keyEvent(rdr::U32 keysym, rdr::U32 xtcode, bool down);
-  virtual void clientCutText(const char* str, int len);
+  virtual void clientCutText(const char* str);
   virtual unsigned int setScreenLayout(int fb_width, int fb_height,
                                        const rfb::ScreenSet& layout);
 
diff --git a/unix/xserver/hw/vnc/XserverDesktop.cc b/unix/xserver/hw/vnc/XserverDesktop.cc
index d8b3a4d..cd9b1f9 100644
--- a/unix/xserver/hw/vnc/XserverDesktop.cc
+++ b/unix/xserver/hw/vnc/XserverDesktop.cc
@@ -192,10 +192,10 @@
   server->setLEDState(state);
 }
 
-void XserverDesktop::serverCutText(const char* str, int len)
+void XserverDesktop::serverCutText(const char* str)
 {
   try {
-    server->serverCutText(str, len);
+    server->serverCutText(str);
   } catch (rdr::Exception& e) {
     vlog.error("XserverDesktop::serverCutText: %s",e.str());
   }
@@ -436,9 +436,9 @@
   vncPointerButtonAction(buttonMask);
 }
 
-void XserverDesktop::clientCutText(const char* str, int len)
+void XserverDesktop::clientCutText(const char* str)
 {
-  vncClientCutText(str, len);
+  vncClientCutText(str);
 }
 
 unsigned int XserverDesktop::setScreenLayout(int fb_width, int fb_height,
diff --git a/unix/xserver/hw/vnc/XserverDesktop.h b/unix/xserver/hw/vnc/XserverDesktop.h
index 1253935..c6c4eb1 100644
--- a/unix/xserver/hw/vnc/XserverDesktop.h
+++ b/unix/xserver/hw/vnc/XserverDesktop.h
@@ -61,7 +61,7 @@
   void refreshScreenLayout();
   void bell();
   void setLEDState(unsigned int state);
-  void serverCutText(const char* str, int len);
+  void serverCutText(const char* str);
   void setDesktopName(const char* name);
   void setCursor(int width, int height, int hotX, int hotY,
                  const unsigned char *rgbaData);
@@ -92,7 +92,7 @@
                                const char* userName);
   virtual void pointerEvent(const rfb::Point& pos, int buttonMask);
   virtual void keyEvent(rdr::U32 keysym, rdr::U32 keycode, bool down);
-  virtual void clientCutText(const char* str, int len);
+  virtual void clientCutText(const char* str);
   virtual unsigned int setScreenLayout(int fb_width, int fb_height,
                                        const rfb::ScreenSet& layout);
 
diff --git a/unix/xserver/hw/vnc/vncExtInit.cc b/unix/xserver/hw/vnc/vncExtInit.cc
index 20072f4..10143f6 100644
--- a/unix/xserver/hw/vnc/vncExtInit.cc
+++ b/unix/xserver/hw/vnc/vncExtInit.cc
@@ -285,10 +285,10 @@
     desktop[scr]->setDesktopName(desktopName);
 }
 
-void vncServerCutText(const char *text, size_t len)
+void vncServerCutText(const char *text)
 {
   for (int scr = 0; scr < vncGetScreenCount(); scr++)
-    desktop[scr]->serverCutText(text, len);
+    desktop[scr]->serverCutText(text);
 }
 
 int vncConnectClient(const char *addr)
diff --git a/unix/xserver/hw/vnc/vncExtInit.h b/unix/xserver/hw/vnc/vncExtInit.h
index 5f97f96..bdcce83 100644
--- a/unix/xserver/hw/vnc/vncExtInit.h
+++ b/unix/xserver/hw/vnc/vncExtInit.h
@@ -53,7 +53,7 @@
 
 void vncUpdateDesktopName(void);
 
-void vncServerCutText(const char *text, size_t len);
+void vncServerCutText(const char *text);
 
 int vncConnectClient(const char *addr);
 
diff --git a/unix/xserver/hw/vnc/vncSelection.c b/unix/xserver/hw/vnc/vncSelection.c
index 5ddcaf0..8f4146d 100644
--- a/unix/xserver/hw/vnc/vncSelection.c
+++ b/unix/xserver/hw/vnc/vncSelection.c
@@ -48,7 +48,6 @@
 static Window wid;
 
 static char* clientCutText;
-static int clientCutTextLen;
 
 static int vncCreateSelectionWindow(void);
 static int vncOwnSelection(Atom selection);
@@ -82,23 +81,20 @@
     FatalError("Add VNC SelectionCallback failed\n");
 }
 
-void vncClientCutText(const char* str, int len)
+void vncClientCutText(const char* str)
 {
   int rc;
 
   if (clientCutText != NULL)
     free(clientCutText);
 
-  clientCutText = malloc(len);
+  clientCutText = strdup(str);
   if (clientCutText == NULL) {
     LOG_ERROR("Could not allocate clipboard buffer");
     DeleteWindowFromAnySelections(pWindow);
     return;
   }
 
-  memcpy(clientCutText, str, len);
-  clientCutTextLen = len;
-
   if (vncGetSetPrimary()) {
     rc = vncOwnSelection(xaPRIMARY);
     if (rc != Success)
@@ -246,7 +242,7 @@
   } else if ((target == xaSTRING) || (target == xaTEXT)) {
     rc = dixChangeWindowProperty(serverClient, pWin, realProperty,
                                  XA_STRING, 8, PropModeReplace,
-                                 clientCutTextLen, clientCutText,
+                                 strlen(clientCutText), clientCutText,
                                  TRUE);
     if (rc != Success)
       return rc;
@@ -258,25 +254,22 @@
     const unsigned char* in;
     size_t in_len;
 
-    buffer = malloc(clientCutTextLen*2);
+    buffer = malloc(strlen(clientCutText)*2);
     if (buffer == NULL)
       return BadAlloc;
 
     out = buffer;
     len = 0;
     in = clientCutText;
-    in_len = clientCutTextLen;
-    while (in_len > 0) {
+    while (*in != '\0') {
       if (*in & 0x80) {
         *out++ = 0xc0 | (*in >> 6);
         *out++ = 0x80 | (*in & 0x3f);
         len += 2;
         in++;
-        in_len--;
       } else {
         *out++ = *in++;
         len++;
-        in_len--;
       }
     }
 
@@ -426,7 +419,7 @@
     if (filtered == NULL)
       return;
 
-    vncServerCutText(filtered, strlen(filtered));
+    vncServerCutText(filtered);
 
     vncStrFree(filtered);
   } else if (target == xaUTF8_STRING) {
@@ -484,7 +477,7 @@
     if (filtered == NULL)
       return;
 
-    vncServerCutText(filtered, strlen(filtered));
+    vncServerCutText(filtered);
 
     vncStrFree(filtered);
   }
diff --git a/unix/xserver/hw/vnc/vncSelection.h b/unix/xserver/hw/vnc/vncSelection.h
index 969f895..e11d4a4 100644
--- a/unix/xserver/hw/vnc/vncSelection.h
+++ b/unix/xserver/hw/vnc/vncSelection.h
@@ -1,4 +1,4 @@
-/* Copyright 2016 Pierre Ossman for Cendio AB
+/* Copyright 2016-2019 Pierre Ossman for Cendio AB
  * 
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -24,7 +24,7 @@
 
 void vncSelectionInit(void);
 
-void vncClientCutText(const char* str, int len);
+void vncClientCutText(const char* str);
 
 #ifdef __cplusplus
 }