| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#include <uriparser/UriDefsConfig.h> |
| 42 |
#if (!defined(URI_PASS_ANSI) && !defined(URI_PASS_UNICODE)) |
| 43 |
|
| 44 |
# ifdef URI_ENABLE_ANSI |
| 45 |
# define URI_PASS_ANSI 1 |
| 46 |
# include "UriFile.c" |
| 47 |
# undef URI_PASS_ANSI |
| 48 |
# endif |
| 49 |
# ifdef URI_ENABLE_UNICODE |
| 50 |
# define URI_PASS_UNICODE 1 |
| 51 |
# include "UriFile.c" |
| 52 |
# undef URI_PASS_UNICODE |
| 53 |
# endif |
| 54 |
#else |
| 55 |
# ifdef URI_PASS_ANSI |
| 56 |
# include <uriparser/UriDefsAnsi.h> |
| 57 |
# else |
| 58 |
# include <uriparser/UriDefsUnicode.h> |
| 59 |
# include <wchar.h> |
| 60 |
# endif |
| 61 |
|
| 62 |
# ifndef URI_DOXYGEN |
| 63 |
# include <uriparser/Uri.h> |
| 64 |
# endif |
| 65 |
|
| 66 |
# include <stdlib.h> |
| 67 |
|
| 68 |
static URI_INLINE int URI_FUNC(FilenameToUriString)(const URI_CHAR * filename, |
| 69 |
URI_CHAR * uriString, |
| 70 |
UriBool fromUnix) { |
| 71 |
const URI_CHAR * input = filename; |
| 72 |
const URI_CHAR * lastSep = input - 1; |
| 73 |
UriBool firstSegment = URI_TRUE; |
| 74 |
URI_CHAR * output = uriString; |
| 75 |
UriBool absolute; |
| 76 |
UriBool is_windows_network; |
| 77 |
|
| 78 |
if ((filename == NULL) || (uriString == NULL)) { |
| 79 |
return URI_ERROR_NULL; |
| 80 |
} |
| 81 |
|
| 82 |
is_windows_network = (filename[0] == _UT('\\')) && (filename[1] == _UT('\\')); |
| 83 |
absolute = fromUnix ? (filename[0] == _UT('/')) |
| 84 |
: (((filename[0] != _UT('\0')) && (filename[1] == _UT(':'))) |
| 85 |
|| is_windows_network); |
| 86 |
|
| 87 |
if (absolute) { |
| 88 |
const URI_CHAR * const prefix = fromUnix ? _UT("file://") |
| 89 |
: is_windows_network ? _UT("file:") |
| 90 |
: _UT("file:///"); |
| 91 |
const size_t prefixLen = URI_STRLEN(prefix); |
| 92 |
|
| 93 |
|
| 94 |
memcpy(uriString, prefix, prefixLen * sizeof(URI_CHAR)); |
| 95 |
output += prefixLen; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
for (;;) { |
| 100 |
if ((input[0] == _UT('\0')) || (fromUnix && input[0] == _UT('/')) |
| 101 |
|| (!fromUnix && input[0] == _UT('\\'))) { |
| 102 |
|
| 103 |
if (lastSep + 1 < input) { |
| 104 |
if (!fromUnix && absolute && (firstSegment == URI_TRUE)) { |
| 105 |
|
| 106 |
const int charsToCopy = (int)(input - (lastSep + 1)); |
| 107 |
memcpy(output, lastSep + 1, charsToCopy * sizeof(URI_CHAR)); |
| 108 |
output += charsToCopy; |
| 109 |
} else { |
| 110 |
output = URI_FUNC(EscapeEx)(lastSep + 1, input, output, URI_FALSE, |
| 111 |
URI_FALSE); |
| 112 |
} |
| 113 |
} |
| 114 |
firstSegment = URI_FALSE; |
| 115 |
} |
| 116 |
|
| 117 |
if (input[0] == _UT('\0')) { |
| 118 |
output[0] = _UT('\0'); |
| 119 |
break; |
| 120 |
} else if (fromUnix && (input[0] == _UT('/'))) { |
| 121 |
|
| 122 |
output[0] = _UT('/'); |
| 123 |
output++; |
| 124 |
lastSep = input; |
| 125 |
} else if (!fromUnix && (input[0] == _UT('\\'))) { |
| 126 |
|
| 127 |
output[0] = _UT('/'); |
| 128 |
output++; |
| 129 |
lastSep = input; |
| 130 |
} |
| 131 |
input++; |
| 132 |
} |
| 133 |
|
| 134 |
return URI_SUCCESS; |
| 135 |
} |
| 136 |
|
| 137 |
static URI_INLINE int URI_FUNC(UriStringToFilename)(const URI_CHAR * uriString, |
| 138 |
URI_CHAR * filename, UriBool toUnix) { |
| 139 |
if ((uriString == NULL) || (filename == NULL)) { |
| 140 |
return URI_ERROR_NULL; |
| 141 |
} |
| 142 |
|
| 143 |
const UriBool file_unknown_slashes = |
| 144 |
URI_STRNCMP(uriString, _UT("file:"), URI_STRLEN(_UT("file:"))) == 0; |
| 145 |
const UriBool file_one_or_more_slashes = |
| 146 |
file_unknown_slashes |
| 147 |
&& (URI_STRNCMP(uriString, _UT("file:/"), URI_STRLEN(_UT("file:/"))) == 0); |
| 148 |
const UriBool file_two_or_more_slashes = |
| 149 |
file_one_or_more_slashes |
| 150 |
&& (URI_STRNCMP(uriString, _UT("file://"), URI_STRLEN(_UT("file://"))) == 0); |
| 151 |
const UriBool file_three_or_more_slashes = |
| 152 |
file_two_or_more_slashes |
| 153 |
&& (URI_STRNCMP(uriString, _UT("file:///"), URI_STRLEN(_UT("file:///"))) == 0); |
| 154 |
|
| 155 |
const size_t charsToSkip = |
| 156 |
file_two_or_more_slashes |
| 157 |
? file_three_or_more_slashes ? toUnix |
| 158 |
|
| 159 |
? URI_STRLEN(_UT("file://")) |
| 160 |
|
| 161 |
: URI_STRLEN(_UT("file:///")) |
| 162 |
|
| 163 |
: URI_STRLEN(_UT("file://")) |
| 164 |
: ((file_one_or_more_slashes && toUnix) |
| 165 |
|
| 166 |
|
| 167 |
? URI_STRLEN(_UT("file:")) |
| 168 |
: ((!toUnix && file_unknown_slashes && !file_one_or_more_slashes) |
| 169 |
|
| 170 |
|
| 171 |
? URI_STRLEN(_UT("file:")) |
| 172 |
: 0)); |
| 173 |
const size_t charsToCopy = URI_STRLEN(uriString + charsToSkip) + 1; |
| 174 |
|
| 175 |
const UriBool is_windows_network_with_authority = |
| 176 |
(toUnix == URI_FALSE) && file_two_or_more_slashes && !file_three_or_more_slashes; |
| 177 |
|
| 178 |
URI_CHAR * const unescape_target = |
| 179 |
is_windows_network_with_authority ? (filename + 2) : filename; |
| 180 |
|
| 181 |
if (is_windows_network_with_authority) { |
| 182 |
filename[0] = '\\'; |
| 183 |
filename[1] = '\\'; |
| 184 |
} |
| 185 |
|
| 186 |
memcpy(unescape_target, uriString + charsToSkip, charsToCopy * sizeof(URI_CHAR)); |
| 187 |
URI_FUNC(UnescapeInPlaceEx)(filename, URI_FALSE, URI_BR_DONT_TOUCH); |
| 188 |
|
| 189 |
|
| 190 |
if (!toUnix) { |
| 191 |
URI_CHAR * walker = filename; |
| 192 |
while (walker[0] != _UT('\0')) { |
| 193 |
if (walker[0] == _UT('/')) { |
| 194 |
walker[0] = _UT('\\'); |
| 195 |
} |
| 196 |
walker++; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return URI_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
int URI_FUNC(UnixFilenameToUriString)(const URI_CHAR * filename, URI_CHAR * uriString) { |
| 204 |
return URI_FUNC(FilenameToUriString)(filename, uriString, URI_TRUE); |
| 205 |
} |
| 206 |
|
| 207 |
int URI_FUNC(WindowsFilenameToUriString)(const URI_CHAR * filename, |
| 208 |
URI_CHAR * uriString) { |
| 209 |
return URI_FUNC(FilenameToUriString)(filename, uriString, URI_FALSE); |
| 210 |
} |
| 211 |
|
| 212 |
int URI_FUNC(UriStringToUnixFilename)(const URI_CHAR * uriString, URI_CHAR * filename) { |
| 213 |
return URI_FUNC(UriStringToFilename)(uriString, filename, URI_TRUE); |
| 214 |
} |
| 215 |
|
| 216 |
int URI_FUNC(UriStringToWindowsFilename)(const URI_CHAR * uriString, |
| 217 |
URI_CHAR * filename) { |
| 218 |
return URI_FUNC(UriStringToFilename)(uriString, filename, URI_FALSE); |
| 219 |
} |
| 220 |
|
| 221 |
#endif |