1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/librazor/path.c Fri Oct 17 10:13:48 2014 +0100
1.3 @@ -0,0 +1,193 @@
1.4 +/*
1.5 + * Copyright (C) 2014 J. Ali Harlow <ali@juiblex.co.uk>
1.6 + *
1.7 + * This program is free software; you can redistribute it and/or modify
1.8 + * it under the terms of the GNU General Public License as published by
1.9 + * the Free Software Foundation; either version 2 of the License, or
1.10 + * (at your option) any later version.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License along
1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1.20 + */
1.21 +
1.22 +#include "config.h"
1.23 +#include <stdlib.h>
1.24 +#include <string.h>
1.25 +#include "razor.h"
1.26 +#include "razor-internal.h"
1.27 +
1.28 +/**
1.29 + * razor_path_add_root:
1.30 + *
1.31 + * Adds a root to a path. path must be an absolute pathname. In POSIX
1.32 + * environments this is equivalent to the concationation of root and path.
1.33 + * In Microsoft Windows an adjustment may need to be made for a drive letter
1.34 + * in path (which will be dropped).
1.35 + *
1.36 + * Returns: The new pathname.
1.37 + **/
1.38 +RAZOR_EXPORT char *razor_path_add_root(const char *path, const char *root)
1.39 +{
1.40 + if (root && *root)
1.41 + return razor_concat(root, SKIP_DRIVE_LETTER(path), NULL);
1.42 + else
1.43 + return strdup(path);
1.44 +}
1.45 +
1.46 +#if 0
1.47 +
1.48 +/*
1.49 + * This should work, but for some reason PathCreateFromUrlW()
1.50 + * treats the percent-encoded bytes as being in CP 850 rather
1.51 + * than UTF-8 as expected even if we set the codepage.
1.52 + */
1.53 +RAZOR_EXPORT char *razor_path_from_url(const char *url)
1.54 +{
1.55 + UINT saved_cp;
1.56 + HRESULT result;
1.57 + DWORD len = MAX_PATH;
1.58 + wchar_t *url16;
1.59 + wchar_t path16[MAX_PATH];
1.60 + char *path;
1.61 +
1.62 + url16 = razor_utf8_to_utf16(url, -1);
1.63 +
1.64 + saved_cp = GetConsoleCP();
1.65 + SetConsoleCP(CP_UTF8);
1.66 +
1.67 + result = PathCreateFromUrlW(url16, path16, &len, NULL);
1.68 +
1.69 + SetConsoleCP(saved_cp);
1.70 +
1.71 + if (result == S_OK)
1.72 + path = razor_utf16_to_utf8(path16, len);
1.73 + else
1.74 + path = NULL;
1.75 +
1.76 + free(url16);
1.77 +
1.78 + return path;
1.79 +}
1.80 +
1.81 +#else
1.82 +
1.83 +#ifdef MSWIN_API
1.84 +static int is_ascii_letter(char c)
1.85 +{
1.86 + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
1.87 +}
1.88 +#endif
1.89 +
1.90 +static int xdigit_value(char c)
1.91 +{
1.92 + if (c >= '0' && c <= '9')
1.93 + return c - '0';
1.94 + else
1.95 + return (c | 0x20) - 'a' + 10;
1.96 +}
1.97 +
1.98 +static int valid_unicode(unsigned unicode)
1.99 +{
1.100 + /*
1.101 + * Within the U+0000..U+10FFFF range defined by RFC3629
1.102 + * but not in the U+D800..U+DFFF range prohibited in UTF-8.
1.103 + */
1.104 + return unicode < 0xD800 || (unicode >= 0xE000 && unicode < 0x110000);
1.105 +}
1.106 +
1.107 +RAZOR_EXPORT char *razor_path_from_url(const char *url)
1.108 +{
1.109 + int continuation_bytes = 0;
1.110 + char *path, *p;
1.111 + unsigned char c;
1.112 + unsigned unicode;
1.113 +
1.114 + if (strncmp(url, "file://", 7) == 0)
1.115 + url += 7;
1.116 + else
1.117 + return NULL;
1.118 +
1.119 + if (strncmp(url, "localhost/", 10) == 0)
1.120 + url += 9;
1.121 + else if (strncmp(url, "/", 1) != 0)
1.122 + return NULL;
1.123 +
1.124 +#ifdef MSWIN_API
1.125 + /*
1.126 + * Under MS-Windows, file:///c:/xxx maps to c:/xxx
1.127 + * Note that PathCreateFromUrl converts / to \ as well.
1.128 + */
1.129 + if (is_ascii_letter(url[1]) && url[2] == ':' && url[3] == '/')
1.130 + url++;
1.131 +#endif
1.132 +
1.133 + p = path = malloc(strlen(url) + 1);
1.134 +
1.135 + while(*url) {
1.136 + if (*url >= 0x7F || *url < 0x20) {
1.137 + free(path);
1.138 + return NULL;
1.139 + } else if (*url != '%') {
1.140 + if (continuation_bytes) {
1.141 + free(path);
1.142 + return NULL;
1.143 + } else
1.144 + *p++ = *url++;
1.145 + } else if (isxdigit(url[1]) && isxdigit(url[2])) {
1.146 + c = xdigit_value(url[1]) * 16 + xdigit_value(url[2]);
1.147 + if (c == '/') {
1.148 + free(path);
1.149 + return NULL;
1.150 + } else if (!continuation_bytes) {
1.151 + if (c >= 0xF5 || c == 0xC0 || c == 0xC1) {
1.152 + free(path);
1.153 + return NULL;
1.154 + } else if (c >= 0xF0) {
1.155 + unicode = c & 7;
1.156 + continuation_bytes = 3;
1.157 + } else if (c >= 0xE0) {
1.158 + unicode = c & 3;
1.159 + continuation_bytes = 2;
1.160 + } else if (c >= 0xC0) {
1.161 + unicode = c & 1;
1.162 + continuation_bytes = 1;
1.163 + }
1.164 + } else if ((c & 0xC0) != 0x80) {
1.165 + free(path);
1.166 + return NULL;
1.167 + } else {
1.168 + unicode <<= 6;
1.169 + unicode |= (c & 0x3F);
1.170 +
1.171 + if (!--continuation_bytes &&
1.172 + !valid_unicode(unicode)) {
1.173 + free(path);
1.174 + return NULL;
1.175 + }
1.176 + }
1.177 +
1.178 + *p++ = c;
1.179 + url += 3;
1.180 + } else {
1.181 + free(path);
1.182 + return NULL;
1.183 + }
1.184 + }
1.185 +
1.186 + if (continuation_bytes) {
1.187 + free(path);
1.188 + return NULL;
1.189 + }
1.190 +
1.191 + *p++ = '\0';
1.192 +
1.193 + return realloc(path, p - path);
1.194 +}
1.195 +
1.196 +#endif /* 0 */
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/librazor/test-pfu.c Fri Oct 17 10:13:48 2014 +0100
2.3 @@ -0,0 +1,134 @@
2.4 +/*
2.5 + * Copyright (C) 2014 J. Ali Harlow <ali@juiblex.co.uk>
2.6 + *
2.7 + * This program is free software; you can redistribute it and/or modify
2.8 + * it under the terms of the GNU General Public License as published by
2.9 + * the Free Software Foundation; either version 2 of the License, or
2.10 + * (at your option) any later version.
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License along
2.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
2.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2.20 + */
2.21 +
2.22 +#include "config.h"
2.23 +
2.24 +#include <stdlib.h>
2.25 +#include <stdio.h>
2.26 +#include <string.h>
2.27 +#ifdef MSWIN_API
2.28 +#include <windows.h>
2.29 +#endif
2.30 +#include "razor.h"
2.31 +
2.32 +int is_ascii_letter(char c)
2.33 +{
2.34 + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
2.35 +}
2.36 +
2.37 +int is_slash(char c)
2.38 +{
2.39 + return c == '/' || c == '\\';
2.40 +}
2.41 +
2.42 +const char *mswin_path(const char *path)
2.43 +{
2.44 + if (path == NULL)
2.45 + return NULL;
2.46 +
2.47 + if (path[0] == '/' && is_ascii_letter(path[1]) && path[2] == ':' &&
2.48 + path[3] == '/')
2.49 + path++;
2.50 +
2.51 + return path;
2.52 +}
2.53 +
2.54 +int path_cmp(const char *p1, const char *p2)
2.55 +{
2.56 +#ifdef MSWIN_API
2.57 + while(*p1 && *p2) {
2.58 + if (*p1 == *p2 || is_slash(*p1) && is_slash(*p2)) {
2.59 + p1++;
2.60 + p2++;
2.61 + } else
2.62 + break;
2.63 + }
2.64 +
2.65 + return *p1 || *p2;
2.66 +#else
2.67 + return strcmp(p1, p2);
2.68 +#endif
2.69 +}
2.70 +
2.71 +int test_pfu(const char *url, const char *path)
2.72 +{
2.73 + char *s;
2.74 + int r;
2.75 +
2.76 + s = razor_path_from_url(url);
2.77 +
2.78 +#ifdef MSWIN_API
2.79 + path = mswin_path(path);
2.80 +#endif
2.81 +
2.82 + if (s && path)
2.83 + r = path_cmp(s, path);
2.84 + else
2.85 + r = (s != path);
2.86 +
2.87 + if (r) {
2.88 + fprintf(stderr, "Fail: razor_path_from_url(\"%s\")", url);
2.89 + if (s)
2.90 + fprintf(stderr, " returns \"%s\", expected", s);
2.91 + else
2.92 + fprintf(stderr, " returns NULL, expected");
2.93 + if (path)
2.94 + fprintf(stderr, " \"%s\"\n", path);
2.95 + else
2.96 + fprintf(stderr, " NULL\n");
2.97 + }
2.98 +
2.99 + free(s);
2.100 +
2.101 + return r;
2.102 +}
2.103 +
2.104 +#ifdef MSWIN_API
2.105 +UINT saved_cp;
2.106 +
2.107 +void cleanup_on_exit(void)
2.108 +{
2.109 + SetConsoleOutputCP(saved_cp);
2.110 +}
2.111 +#endif
2.112 +
2.113 +int main(int argc, char *argv[])
2.114 +{
2.115 + int r = 0;
2.116 +
2.117 +#ifdef MSWIN_API
2.118 + atexit(cleanup_on_exit);
2.119 + saved_cp = GetConsoleOutputCP();
2.120 + SetConsoleOutputCP(CP_UTF8);
2.121 +#endif
2.122 +
2.123 + r |= test_pfu("file://localhost/etc/fstab", "/etc/fstab");
2.124 + r |= test_pfu("file:///etc/fstab", "/etc/fstab");
2.125 + r |= test_pfu("file://localhost/c:/WINDOWS/clock.avi",
2.126 + "/c:/WINDOWS/clock.avi");
2.127 + r |= test_pfu("file:///c:/WINDOWS/clock.avi",
2.128 + "/c:/WINDOWS/clock.avi");
2.129 + r |= test_pfu("file:///path/to/the%20file.txt",
2.130 + "/path/to/the file.txt");
2.131 + r |= test_pfu("file:///home/s%C3%A9bastien", "/home/sébastien");
2.132 + r |= test_pfu("file:///home/luk%C3%A1%C5%A1", "/home/lukáš");
2.133 + r |= test_pfu("file:///var/log/22%20%e0%b8%aa%e0%b8%b4%e0%b8%87%e0%b8%ab%e0%b8%b2%e0%b8%84%e0%b8%a1%202014",
2.134 + "/var/log/22 สิงหาคม 2014");
2.135 +
2.136 + exit(r ? 1 : 0);
2.137 +}