Fixed bug 2567 - x11: Local dropped files are not URI-decoded
authorSam Lantinga <slouken@libsdl.org>
Sun, 15 Jun 2014 18:31:30 -0700
changeset 8870cd1113741ade
parent 8869 553e9f7cd10e
child 8871 e7abd1102676
Fixed bug 2567 - x11: Local dropped files are not URI-decoded

Melker Narikka

Local files that are dropped onto a window under X11
are not going through a URI decoding step, resulting in the following
in my test application:

Dropped file /home/meklu/Pictures/Screenshot%20from%202013-10-30%2014:04:50.png
Couldn't load /home/meklu/Pictures/Screenshot%20from%202013-10-30%2014:04:50.png

Expected result:

Dropped file /home/meklu/Pictures/Screenshot from 2013-10-30 14:04:50.png
Loaded /home/meklu/Pictures/Screenshot from 2013-10-30 14:04:50.png successfully

I've attached a patch that fixes the issue by doing URI decoding in-place on
the file string buffer.
src/video/x11/SDL_x11events.c
     1.1 --- a/src/video/x11/SDL_x11events.c	Sun Jun 15 18:09:39 2014 -0700
     1.2 +++ b/src/video/x11/SDL_x11events.c	Sun Jun 15 18:31:30 2014 -0700
     1.3 @@ -174,6 +174,79 @@
     1.4      return SDL_FALSE;
     1.5  }
     1.6  
     1.7 +/* Decodes URI escape sequences in string buf of len bytes
     1.8 +   (excluding the terminating NULL byte) in-place. Since
     1.9 +   URI-encoded characters take three times the space of
    1.10 +   normal characters, this should not be an issue.
    1.11 +
    1.12 +   Returns the number of decoded bytes that wound up in
    1.13 +   the buffer, excluding the terminating NULL byte.
    1.14 +
    1.15 +   The buffer is guaranteed to be NULL-terminated but
    1.16 +   may contain embedded NULL bytes.
    1.17 +
    1.18 +   On error, -1 is returned.
    1.19 + */
    1.20 +int X11_URIDecode(char *buf, int len) {
    1.21 +    int ri, wi, di;
    1.22 +    char decode = '\0';
    1.23 +    if (buf == NULL || len < 0) {
    1.24 +        errno = EINVAL;
    1.25 +        return -1;
    1.26 +    }
    1.27 +    if (len == 0) {
    1.28 +        len = SDL_strlen(buf);
    1.29 +    }
    1.30 +    for (ri = 0, wi = 0, di = 0; ri < len && wi < len; ri += 1) {
    1.31 +        if (di == 0) {
    1.32 +            /* start decoding */
    1.33 +            if (buf[ri] == '%') {
    1.34 +                decode = '\0';
    1.35 +                di += 1;
    1.36 +                continue;
    1.37 +            }
    1.38 +            /* normal write */
    1.39 +            buf[wi] = buf[ri];
    1.40 +            wi += 1;
    1.41 +            continue;
    1.42 +        } else if (di == 1 || di == 2) {
    1.43 +            char off = '\0';
    1.44 +            char isa = buf[ri] >= 'a' && buf[ri] <= 'f';
    1.45 +            char isA = buf[ri] >= 'A' && buf[ri] <= 'F';
    1.46 +            char isn = buf[ri] >= '0' && buf[ri] <= '9';
    1.47 +            if (!(isa || isA || isn)) {
    1.48 +                /* not a hexadecimal */
    1.49 +                int sri;
    1.50 +                for (sri = ri - di; sri <= ri; sri += 1) {
    1.51 +                    buf[wi] = buf[sri];
    1.52 +                    wi += 1;
    1.53 +                }
    1.54 +                di = 0;
    1.55 +                continue;
    1.56 +            }
    1.57 +            /* itsy bitsy magicsy */
    1.58 +            if (isn) {
    1.59 +                off = 0 - '0';
    1.60 +            } else if (isa) {
    1.61 +                off = 10 - 'a';
    1.62 +            } else if (isA) {
    1.63 +                off = 10 - 'A';
    1.64 +            }
    1.65 +            decode |= (buf[ri] + off) << (2 - di) * 4;
    1.66 +            if (di == 2) {
    1.67 +                buf[wi] = decode;
    1.68 +                wi += 1;
    1.69 +                di = 0;
    1.70 +            } else {
    1.71 +                di += 1;
    1.72 +            }
    1.73 +            continue;
    1.74 +        }
    1.75 +    }
    1.76 +    buf[wi] = '\0';
    1.77 +    return wi;
    1.78 +}
    1.79 +
    1.80  /* Convert URI to local filename
    1.81     return filename if possible, else NULL
    1.82  */
    1.83 @@ -202,6 +275,8 @@
    1.84      }
    1.85      if ( local ) {
    1.86        file = uri;
    1.87 +      /* Convert URI escape sequences to real characters */
    1.88 +      X11_URIDecode(file, 0);
    1.89        if ( uri[1] == '/' ) {
    1.90            file++;
    1.91        } else {