Mystro256 7793bd7
commit 8f29de5231e348d356b5f3962225362e8fd87ba9
Mystro256 e523d93
Author: Tim Kosse <tim.kosse@filezilla-project.org>
Mystro256 e523d93
Date:   Mon Sep 14 11:42:38 2015 +0200
Mystro256 e523d93
Mystro256 e523d93
    Escape filenames in wxFileDataObject::GetDataSize/GetDataHere
Mystro256 e523d93
    
Mystro256 e523d93
    On wxGTK, wxFileDataObject::SetData calls g_filename_from_uri which
Mystro256 e523d93
    percent-decodes URIs. No corresponding percent-encoding was done in
Mystro256 e523d93
    wxFileDataObject::GetDataSize/GetDataHere. Use g_filename_to_uri instead in so
Mystro256 e523d93
    that filenames are properly escaped.
Mystro256 e523d93
    
Mystro256 e523d93
    This commit also fixes the data being truncated if it contains non-ASCII
Mystro256 e523d93
    characters on wide-character builds, see the memcpy arguments in the original
Mystro256 e523d93
    code.
Mystro256 e523d93
Mystro256 e523d93
diff --git a/src/gtk/dataobj.cpp b/src/gtk/dataobj.cpp
Mystro256 7793bd7
index 9a39607..2d1f43c 100644
Mystro256 e523d93
--- a/src/gtk/dataobj.cpp
Mystro256 e523d93
+++ b/src/gtk/dataobj.cpp
Mystro256 e523d93
@@ -235,16 +235,21 @@ wxTextDataObject::GetAllFormats(wxDataFormat *formats,
Mystro256 e523d93
 
Mystro256 e523d93
 bool wxFileDataObject::GetDataHere(void *buf) const
Mystro256 e523d93
 {
Mystro256 e523d93
-    wxString filenames;
Mystro256 e523d93
+    char* out = reinterpret_cast<char*>(buf);
Mystro256 e523d93
 
Mystro256 e523d93
     for (size_t i = 0; i < m_filenames.GetCount(); i++)
Mystro256 e523d93
     {
Mystro256 e523d93
-        filenames += wxT("file:");
Mystro256 e523d93
-        filenames += m_filenames[i];
Mystro256 e523d93
-        filenames += wxT("\r\n");
Mystro256 e523d93
+        char* uri = g_filename_to_uri(m_filenames[i].mbc_str(), 0, 0);
Mystro256 e523d93
+        if (uri)
Mystro256 e523d93
+        {
Mystro256 e523d93
+            size_t const len = strlen(uri);
Mystro256 e523d93
+            strcpy(out, uri);
Mystro256 e523d93
+            out += len;
Mystro256 e523d93
+            *(out++) = '\r';
Mystro256 e523d93
+            *(out++) = '\n';
Mystro256 e523d93
+        }
Mystro256 e523d93
     }
Mystro256 e523d93
-
Mystro256 e523d93
-    memcpy( buf, filenames.mbc_str(), filenames.length() + 1 );
Mystro256 e523d93
+    *out = 0;
Mystro256 e523d93
 
Mystro256 e523d93
     return true;
Mystro256 e523d93
 }
Mystro256 e523d93
@@ -255,9 +260,11 @@ size_t wxFileDataObject::GetDataSize() const
Mystro256 e523d93
 
Mystro256 e523d93
     for (size_t i = 0; i < m_filenames.GetCount(); i++)
Mystro256 e523d93
     {
Mystro256 e523d93
-        // This is junk in UTF-8
Mystro256 e523d93
-        res += m_filenames[i].length();
Mystro256 e523d93
-        res += 5 + 2; // "file:" (5) + "\r\n" (2)
Mystro256 e523d93
+        char* uri = g_filename_to_uri(m_filenames[i].mbc_str(), 0, 0);
Mystro256 e523d93
+        if (uri) {
Mystro256 e523d93
+            res += strlen(uri) + 2; // Including "\r\n"
Mystro256 e523d93
+            g_free(uri);
Mystro256 e523d93
+        }
Mystro256 e523d93
     }
Mystro256 e523d93
 
Mystro256 e523d93
     return res + 1;