View difference between Paste ID: RN5g7g9u and LtRfGZDi
SHOW: | | - or go back to the newest paste.
1-
1+
2
 * decode a percent-encoded C string with optional path normalization
3
 *
4
 * The buffer pointed to by @dst must be at least strlen(@src) bytes.
5
 * Decoding stops at the first character from @src that decodes to null.
6
 *
7-
 * Path normalization will remove redundant slashes and slash+dot sequences,
7+
8-
 * as well as removing path components when slash+dot+dot is found. It will
8+
9-
 * keep the root slash (if one was present) and will stop normalization
9+
10-
 * at the first questionmark found (so query parameters won't be normalized).
10+
11
 * @legalese        BSD licensed (http://opensource.org/licenses/BSD-2-Clause)
12
 */
13
ptrdiff_t urldecode(char* dst, const char* src)
14
{
15
    char* org_dst = dst;
16
    char ch, a, b;
17
    do {
18
        ch = *src++;
19
        if (ch == '%' && isxdigit(a = src[0]) && isxdigit(b = src[1])) {
20
            if (a < 'A') a -= '0';
21
            else if(a < 'a') a -= 'A' - 10;
22
            else a -= 'a' - 10;
23
            if (b < 'A') b -= '0';
24
            else if(b < 'a') b -= 'A' - 10;
25
            else b -= 'a' - 10;
26
            ch = 16 * a + b;
27
            src += 2;
28
        }
29
        *dst++ = ch;
30
    } while(ch);
31
    return (dst - org_dst) - 1;
32
}