Blame SOURCES/0001-wayland-Convert-URI-to-local-path-for-DropFile.patch

310ebd
From 9a2bbd8acbebcde56dd2f89ebca3b196c2b38914 Mon Sep 17 00:00:00 2001
310ebd
From: Ethan Lee <flibitijibibo@gmail.com>
310ebd
Date: Wed, 12 Jan 2022 13:01:05 -0500
310ebd
Subject: [PATCH 01/11] wayland: Convert URI to local path for DropFile
310ebd
310ebd
---
310ebd
 src/video/wayland/SDL_waylandevents.c | 149 ++++++++++++++++++++++----
310ebd
 1 file changed, 129 insertions(+), 20 deletions(-)
310ebd
310ebd
diff --git a/src/video/wayland/SDL_waylandevents.c b/src/video/wayland/SDL_waylandevents.c
310ebd
index dfa62ec69..bf2aeea5b 100644
310ebd
--- a/src/video/wayland/SDL_waylandevents.c
310ebd
+++ b/src/video/wayland/SDL_waylandevents.c
310ebd
@@ -1312,36 +1312,145 @@ data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
310ebd
 {
310ebd
 }
310ebd
 
310ebd
+/* Decodes URI escape sequences in string buf of len bytes
310ebd
+   (excluding the terminating NULL byte) in-place. Since
310ebd
+   URI-encoded characters take three times the space of
310ebd
+   normal characters, this should not be an issue.
310ebd
+
310ebd
+   Returns the number of decoded bytes that wound up in
310ebd
+   the buffer, excluding the terminating NULL byte.
310ebd
+
310ebd
+   The buffer is guaranteed to be NULL-terminated but
310ebd
+   may contain embedded NULL bytes.
310ebd
+
310ebd
+   On error, -1 is returned.
310ebd
+
310ebd
+   FIXME: This was shamelessly copied from SDL_x11events.c
310ebd
+ */
310ebd
+static int Wayland_URIDecode(char *buf, int len) {
310ebd
+    int ri, wi, di;
310ebd
+    char decode = '\0';
310ebd
+    if (buf == NULL || len < 0) {
310ebd
+        errno = EINVAL;
310ebd
+        return -1;
310ebd
+    }
310ebd
+    if (len == 0) {
310ebd
+        len = SDL_strlen(buf);
310ebd
+    }
310ebd
+    for (ri = 0, wi = 0, di = 0; ri < len && wi < len; ri += 1) {
310ebd
+        if (di == 0) {
310ebd
+            /* start decoding */
310ebd
+            if (buf[ri] == '%') {
310ebd
+                decode = '\0';
310ebd
+                di += 1;
310ebd
+                continue;
310ebd
+            }
310ebd
+            /* normal write */
310ebd
+            buf[wi] = buf[ri];
310ebd
+            wi += 1;
310ebd
+            continue;
310ebd
+        } else if (di == 1 || di == 2) {
310ebd
+            char off = '\0';
310ebd
+            char isa = buf[ri] >= 'a' && buf[ri] <= 'f';
310ebd
+            char isA = buf[ri] >= 'A' && buf[ri] <= 'F';
310ebd
+            char isn = buf[ri] >= '0' && buf[ri] <= '9';
310ebd
+            if (!(isa || isA || isn)) {
310ebd
+                /* not a hexadecimal */
310ebd
+                int sri;
310ebd
+                for (sri = ri - di; sri <= ri; sri += 1) {
310ebd
+                    buf[wi] = buf[sri];
310ebd
+                    wi += 1;
310ebd
+                }
310ebd
+                di = 0;
310ebd
+                continue;
310ebd
+            }
310ebd
+            /* itsy bitsy magicsy */
310ebd
+            if (isn) {
310ebd
+                off = 0 - '0';
310ebd
+            } else if (isa) {
310ebd
+                off = 10 - 'a';
310ebd
+            } else if (isA) {
310ebd
+                off = 10 - 'A';
310ebd
+            }
310ebd
+            decode |= (buf[ri] + off) << (2 - di) * 4;
310ebd
+            if (di == 2) {
310ebd
+                buf[wi] = decode;
310ebd
+                wi += 1;
310ebd
+                di = 0;
310ebd
+            } else {
310ebd
+                di += 1;
310ebd
+            }
310ebd
+            continue;
310ebd
+        }
310ebd
+    }
310ebd
+    buf[wi] = '\0';
310ebd
+    return wi;
310ebd
+}
310ebd
+
310ebd
+/* Convert URI to local filename
310ebd
+   return filename if possible, else NULL
310ebd
+
310ebd
+   FIXME: This was shamelessly copied from SDL_x11events.c
310ebd
+*/
310ebd
+static char* Wayland_URIToLocal(char* uri) {
310ebd
+    char *file = NULL;
310ebd
+    SDL_bool local;
310ebd
+
310ebd
+    if (SDL_memcmp(uri,"file:/",6) == 0) uri += 6;      /* local file? */
310ebd
+    else if (SDL_strstr(uri,":/") != NULL) return file; /* wrong scheme */
310ebd
+
310ebd
+    local = uri[0] != '/' || (uri[0] != '\0' && uri[1] == '/');
310ebd
+
310ebd
+    /* got a hostname? */
310ebd
+    if (!local && uri[0] == '/' && uri[2] != '/') {
310ebd
+      char* hostname_end = SDL_strchr(uri+1, '/');
310ebd
+      if (hostname_end != NULL) {
310ebd
+          char hostname[ 257 ];
310ebd
+          if (gethostname(hostname, 255) == 0) {
310ebd
+            hostname[ 256 ] = '\0';
310ebd
+            if (SDL_memcmp(uri+1, hostname, hostname_end - (uri+1)) == 0) {
310ebd
+                uri = hostname_end + 1;
310ebd
+                local = SDL_TRUE;
310ebd
+            }
310ebd
+          }
310ebd
+      }
310ebd
+    }
310ebd
+    if (local) {
310ebd
+      file = uri;
310ebd
+      /* Convert URI escape sequences to real characters */
310ebd
+      Wayland_URIDecode(file, 0);
310ebd
+      if (uri[1] == '/') {
310ebd
+          file++;
310ebd
+      } else {
310ebd
+          file--;
310ebd
+      }
310ebd
+    }
310ebd
+    return file;
310ebd
+}
310ebd
+
310ebd
 static void
310ebd
 data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
310ebd
 {
310ebd
     SDL_WaylandDataDevice *data_device = data;
310ebd
-    void *buffer = NULL;
310ebd
-    size_t length = 0;
310ebd
-
310ebd
-    const char *current_uri = NULL;
310ebd
-    const char *last_char = NULL;
310ebd
-    char *current_char = NULL;
310ebd
 
310ebd
     if (data_device->drag_offer != NULL) {
310ebd
         /* TODO: SDL Support more mime types */
310ebd
-        buffer = Wayland_data_offer_receive(data_device->drag_offer,
310ebd
-                                            &length, FILE_MIME, SDL_FALSE);
310ebd
-
310ebd
-        /* uri-list */
310ebd
-        current_uri = (const char *)buffer;
310ebd
-        last_char = (const char *)buffer + length;
310ebd
-        for (current_char = buffer; current_char < last_char; ++current_char) {
310ebd
-            if (*current_char == '\n' || *current_char == 0) {
310ebd
-                if (*current_uri != 0 && *current_uri != '#') {
310ebd
-                    *current_char = 0;
310ebd
-                    SDL_SendDropFile(NULL, current_uri);
310ebd
+        size_t length;
310ebd
+        void *buffer = Wayland_data_offer_receive(data_device->drag_offer,
310ebd
+                                                  &length, FILE_MIME, SDL_FALSE);
310ebd
+        if (buffer) {
310ebd
+            char *saveptr = NULL;
310ebd
+            char *token = SDL_strtokr((char *) buffer, "\r\n", &saveptr);
310ebd
+            while (token != NULL) {
310ebd
+                char *fn = Wayland_URIToLocal(token);
310ebd
+                if (fn) {
310ebd
+                    SDL_SendDropFile(NULL, fn); /* FIXME: Window? */
310ebd
                 }
310ebd
-                current_uri = (const char *)current_char + 1;
310ebd
+                token = SDL_strtokr(NULL, "\r\n", &saveptr);
310ebd
             }
310ebd
+            SDL_SendDropComplete(NULL); /* FIXME: Window? */
310ebd
+            SDL_free(buffer);
310ebd
         }
310ebd
-
310ebd
-        SDL_free(buffer);
310ebd
     }
310ebd
 }
310ebd
 
310ebd
-- 
310ebd
2.34.1
310ebd