librazor/path.c
author J. Ali Harlow <ali@juiblex.co.uk>
Thu Nov 13 10:44:53 2014 +0000 (2014-11-13)
changeset 462 94d7459828ba
child 475 008c75a5e08d
permissions -rw-r--r--
Add razor_install_prefix_iterator_create()
     1 /*
     2  * Copyright (C) 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include "config.h"
    20 #include <stdlib.h>
    21 #include <string.h>
    22 #include "razor.h"
    23 #include "razor-internal.h"
    24 
    25 /**
    26  * razor_path_add_root:
    27  *
    28  * Adds a root to a path. path must be an absolute pathname. In POSIX
    29  * environments this is equivalent to the concationation of root and path.
    30  * In Microsoft Windows an adjustment may need to be made for a drive letter
    31  * in path (which will be dropped).
    32  *
    33  * Returns: The new pathname.
    34  **/
    35 RAZOR_EXPORT char *razor_path_add_root(const char *path, const char *root)
    36 {
    37 	if (root && *root)
    38 		return razor_concat(root, SKIP_DRIVE_LETTER(path), NULL);
    39 	else
    40 		return strdup(path);
    41 }
    42 
    43 #if 0
    44 
    45 /*
    46  * This should work, but for some reason PathCreateFromUrlW()
    47  * treats the percent-encoded bytes as being in CP 850 rather
    48  * than UTF-8 as expected even if we set the codepage.
    49  */
    50 RAZOR_EXPORT char *razor_path_from_url(const char *url)
    51 {
    52 	UINT saved_cp;
    53 	HRESULT result;
    54 	DWORD len = MAX_PATH;
    55 	wchar_t *url16;
    56 	wchar_t path16[MAX_PATH];
    57 	char *path;
    58 
    59 	url16 = razor_utf8_to_utf16(url, -1);
    60 
    61 	saved_cp = GetConsoleCP();
    62 	SetConsoleCP(CP_UTF8);
    63 
    64 	result = PathCreateFromUrlW(url16, path16, &len, NULL);
    65 
    66 	SetConsoleCP(saved_cp);
    67 
    68 	if (result == S_OK)
    69 		path = razor_utf16_to_utf8(path16, len);
    70 	else
    71 		path = NULL;
    72 
    73 	free(url16);
    74 
    75 	return path;
    76 }
    77 
    78 #else
    79 
    80 #ifdef MSWIN_API
    81 static int is_ascii_letter(char c)
    82 {
    83 	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    84 }
    85 #endif
    86 
    87 static int xdigit_value(char c)
    88 {
    89 	if (c >= '0' && c <= '9')
    90 		return c - '0';
    91 	else
    92 		return (c | 0x20) - 'a' + 10;
    93 }
    94 
    95 static int valid_unicode(unsigned unicode)
    96 {
    97 	/*
    98 	 * Within the U+0000..U+10FFFF range defined by RFC3629
    99 	 * but not in the U+D800..U+DFFF range prohibited in UTF-8.
   100 	 */
   101 	return unicode < 0xD800 || (unicode >= 0xE000 && unicode < 0x110000);
   102 }
   103 
   104 RAZOR_EXPORT char *razor_path_from_url(const char *url)
   105 {
   106 	int continuation_bytes = 0;
   107 	char *path, *p;
   108 	unsigned char c;
   109 	unsigned unicode;
   110 
   111 	if (strncmp(url, "file://", 7) == 0)
   112 		url += 7;
   113 	else
   114 		return NULL;
   115 
   116 	if (strncmp(url, "localhost/", 10) == 0)
   117 		url += 9;
   118 	else if (strncmp(url, "/", 1) != 0)
   119 		return NULL;
   120 
   121 #ifdef MSWIN_API
   122 	/*
   123 	 * Under MS-Windows, file:///c:/xxx maps to c:/xxx
   124 	 * Note that PathCreateFromUrl converts / to \ as well.
   125 	 */
   126 	if (is_ascii_letter(url[1]) && url[2] == ':' && url[3] == '/')
   127 		url++;
   128 #endif
   129 
   130 	p = path = malloc(strlen(url) + 1);
   131 
   132 	while(*url) {
   133 		if (*url >= 0x7F || *url < 0x20) {
   134 			free(path);
   135 			return NULL;
   136 		} else if (*url != '%') {
   137 			if (continuation_bytes) {
   138 				free(path);
   139 				return NULL;
   140 			} else
   141 				*p++ = *url++;
   142 		} else if (isxdigit(url[1]) && isxdigit(url[2])) {
   143 			c = xdigit_value(url[1]) * 16 + xdigit_value(url[2]);
   144 			if (c == '/') {
   145 				free(path);
   146 				return NULL;
   147 			} else if (!continuation_bytes) {
   148 				if (c >= 0xF5 || c == 0xC0 || c == 0xC1) {
   149 					free(path);
   150 					return NULL;
   151 				} else if (c >= 0xF0) {
   152 					unicode = c & 7;
   153 					continuation_bytes = 3;
   154 				} else if (c >= 0xE0) {
   155 					unicode = c & 3;
   156 					continuation_bytes = 2;
   157 				} else if (c >= 0xC0) {
   158 					unicode = c & 1;
   159 					continuation_bytes = 1;
   160 				}
   161 			} else if ((c & 0xC0) != 0x80) {
   162 				free(path);
   163 				return NULL;
   164 			} else {
   165 				unicode <<= 6;
   166 				unicode |= (c & 0x3F);
   167 
   168 				if (!--continuation_bytes &&
   169 				    !valid_unicode(unicode)) {
   170 					free(path);
   171 					return NULL;
   172 				}
   173 			}
   174 
   175 			*p++ = c;
   176 			url += 3;
   177 		} else {
   178 			free(path);
   179 			return NULL;
   180 		}
   181 	}
   182 
   183 	if (continuation_bytes) {
   184 		free(path);
   185 		return NULL;
   186 	}
   187 
   188 	*p++ = '\0';
   189 
   190 	return realloc(path, p - path);
   191 }
   192 
   193 #endif	/* 0 */