diff options
Diffstat (limited to 'src/util/strings.c')
-rw-r--r-- | src/util/strings.c | 2002 |
1 files changed, 1006 insertions, 996 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index ef9fdd693..1b4f75ab0 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -16,7 +16,7 @@ | |||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file util/strings.c | 21 | * @file util/strings.c |
22 | * @brief string functions | 22 | * @brief string functions |
@@ -34,10 +34,10 @@ | |||
34 | #include <unistr.h> | 34 | #include <unistr.h> |
35 | #include <uniconv.h> | 35 | #include <uniconv.h> |
36 | 36 | ||
37 | #define LOG(kind, ...) GNUNET_log_from (kind, "util-strings", __VA_ARGS__) | 37 | #define LOG(kind, ...) GNUNET_log_from(kind, "util-strings", __VA_ARGS__) |
38 | 38 | ||
39 | #define LOG_STRERROR(kind, syscall) \ | 39 | #define LOG_STRERROR(kind, syscall) \ |
40 | GNUNET_log_from_strerror (kind, "util-strings", syscall) | 40 | GNUNET_log_from_strerror(kind, "util-strings", syscall) |
41 | 41 | ||
42 | 42 | ||
43 | /** | 43 | /** |
@@ -62,7 +62,7 @@ | |||
62 | * (or number of bytes that would have been written) | 62 | * (or number of bytes that would have been written) |
63 | */ | 63 | */ |
64 | size_t | 64 | size_t |
65 | GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) | 65 | GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count, ...) |
66 | { | 66 | { |
67 | size_t needed; | 67 | size_t needed; |
68 | size_t slen; | 68 | size_t slen; |
@@ -70,21 +70,21 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) | |||
70 | va_list ap; | 70 | va_list ap; |
71 | 71 | ||
72 | needed = 0; | 72 | needed = 0; |
73 | va_start (ap, count); | 73 | va_start(ap, count); |
74 | while (count > 0) | 74 | while (count > 0) |
75 | { | ||
76 | s = va_arg (ap, const char *); | ||
77 | |||
78 | slen = strlen (s) + 1; | ||
79 | if (buffer != NULL) | ||
80 | { | 75 | { |
81 | GNUNET_assert (needed + slen <= size); | 76 | s = va_arg(ap, const char *); |
82 | GNUNET_memcpy (&buffer[needed], s, slen); | 77 | |
78 | slen = strlen(s) + 1; | ||
79 | if (buffer != NULL) | ||
80 | { | ||
81 | GNUNET_assert(needed + slen <= size); | ||
82 | GNUNET_memcpy(&buffer[needed], s, slen); | ||
83 | } | ||
84 | needed += slen; | ||
85 | count--; | ||
83 | } | 86 | } |
84 | needed += slen; | 87 | va_end(ap); |
85 | count--; | ||
86 | } | ||
87 | va_end (ap); | ||
88 | return needed; | 88 | return needed; |
89 | } | 89 | } |
90 | 90 | ||
@@ -97,23 +97,23 @@ GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...) | |||
97 | * @return string representing the array of @a pids | 97 | * @return string representing the array of @a pids |
98 | */ | 98 | */ |
99 | char * | 99 | char * |
100 | GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, | 100 | GNUNET_STRINGS_pp2s(const struct GNUNET_PeerIdentity *pids, |
101 | unsigned int num_pids) | 101 | unsigned int num_pids) |
102 | { | 102 | { |
103 | char *buf; | 103 | char *buf; |
104 | size_t off; | 104 | size_t off; |
105 | size_t plen = num_pids * 5 + 1; | 105 | size_t plen = num_pids * 5 + 1; |
106 | 106 | ||
107 | off = 0; | 107 | off = 0; |
108 | buf = GNUNET_malloc (plen); | 108 | buf = GNUNET_malloc(plen); |
109 | for (unsigned int i = 0; i < num_pids; i++) | 109 | for (unsigned int i = 0; i < num_pids; i++) |
110 | { | 110 | { |
111 | off += GNUNET_snprintf (&buf[off], | 111 | off += GNUNET_snprintf(&buf[off], |
112 | plen - off, | 112 | plen - off, |
113 | "%s%s", | 113 | "%s%s", |
114 | GNUNET_i2s (&pids[i]), | 114 | GNUNET_i2s(&pids[i]), |
115 | (i == num_pids - 1) ? "" : "-"); | 115 | (i == num_pids - 1) ? "" : "-"); |
116 | } | 116 | } |
117 | return buf; | 117 | return buf; |
118 | } | 118 | } |
119 | 119 | ||
@@ -132,10 +132,10 @@ GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, | |||
132 | * in the buffer, or 0 on error. | 132 | * in the buffer, or 0 on error. |
133 | */ | 133 | */ |
134 | unsigned int | 134 | unsigned int |
135 | GNUNET_STRINGS_buffer_tokenize (const char *buffer, | 135 | GNUNET_STRINGS_buffer_tokenize(const char *buffer, |
136 | size_t size, | 136 | size_t size, |
137 | unsigned int count, | 137 | unsigned int count, |
138 | ...) | 138 | ...) |
139 | { | 139 | { |
140 | unsigned int start; | 140 | unsigned int start; |
141 | unsigned int needed; | 141 | unsigned int needed; |
@@ -143,24 +143,24 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, | |||
143 | va_list ap; | 143 | va_list ap; |
144 | 144 | ||
145 | needed = 0; | 145 | needed = 0; |
146 | va_start (ap, count); | 146 | va_start(ap, count); |
147 | while (count > 0) | 147 | while (count > 0) |
148 | { | ||
149 | r = va_arg (ap, const char **); | ||
150 | |||
151 | start = needed; | ||
152 | while ((needed < size) && (buffer[needed] != '\0')) | ||
153 | needed++; | ||
154 | if (needed == size) | ||
155 | { | 148 | { |
156 | va_end (ap); | 149 | r = va_arg(ap, const char **); |
157 | return 0; /* error */ | 150 | |
151 | start = needed; | ||
152 | while ((needed < size) && (buffer[needed] != '\0')) | ||
153 | needed++; | ||
154 | if (needed == size) | ||
155 | { | ||
156 | va_end(ap); | ||
157 | return 0; /* error */ | ||
158 | } | ||
159 | *r = &buffer[start]; | ||
160 | needed++; /* skip 0-termination */ | ||
161 | count--; | ||
158 | } | 162 | } |
159 | *r = &buffer[start]; | 163 | va_end(ap); |
160 | needed++; /* skip 0-termination */ | ||
161 | count--; | ||
162 | } | ||
163 | va_end (ap); | ||
164 | return needed; | 164 | return needed; |
165 | } | 165 | } |
166 | 166 | ||
@@ -172,33 +172,33 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, | |||
172 | * @return fancy representation of the size (possibly rounded) for humans | 172 | * @return fancy representation of the size (possibly rounded) for humans |
173 | */ | 173 | */ |
174 | char * | 174 | char * |
175 | GNUNET_STRINGS_byte_size_fancy (unsigned long long size) | 175 | GNUNET_STRINGS_byte_size_fancy(unsigned long long size) |
176 | { | 176 | { |
177 | const char *unit = _ (/* size unit */ "b"); | 177 | const char *unit = _(/* size unit */ "b"); |
178 | char *ret; | 178 | char *ret; |
179 | 179 | ||
180 | if (size > 5 * 1024) | 180 | if (size > 5 * 1024) |
181 | { | ||
182 | size = size / 1024; | ||
183 | unit = "KiB"; | ||
184 | if (size > 5 * 1024) | ||
185 | { | 181 | { |
186 | size = size / 1024; | 182 | size = size / 1024; |
187 | unit = "MiB"; | 183 | unit = "KiB"; |
188 | if (size > 5 * 1024) | 184 | if (size > 5 * 1024) |
189 | { | ||
190 | size = size / 1024; | ||
191 | unit = "GiB"; | ||
192 | if (size > 5 * 1024) | ||
193 | { | 185 | { |
194 | size = size / 1024; | 186 | size = size / 1024; |
195 | unit = "TiB"; | 187 | unit = "MiB"; |
188 | if (size > 5 * 1024) | ||
189 | { | ||
190 | size = size / 1024; | ||
191 | unit = "GiB"; | ||
192 | if (size > 5 * 1024) | ||
193 | { | ||
194 | size = size / 1024; | ||
195 | unit = "TiB"; | ||
196 | } | ||
197 | } | ||
196 | } | 198 | } |
197 | } | ||
198 | } | 199 | } |
199 | } | 200 | ret = GNUNET_malloc(32); |
200 | ret = GNUNET_malloc (32); | 201 | GNUNET_snprintf(ret, 32, "%llu %s", size, unit); |
201 | GNUNET_snprintf (ret, 32, "%llu %s", size, unit); | ||
202 | return ret; | 202 | return ret; |
203 | } | 203 | } |
204 | 204 | ||
@@ -213,16 +213,16 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size) | |||
213 | * @param n the length of the string to copy, including its terminating null | 213 | * @param n the length of the string to copy, including its terminating null |
214 | * byte | 214 | * byte |
215 | * @return the length of the string that was copied, excluding the terminating | 215 | * @return the length of the string that was copied, excluding the terminating |
216 | * null byte | 216 | * null byte |
217 | */ | 217 | */ |
218 | size_t | 218 | size_t |
219 | GNUNET_strlcpy (char *dst, const char *src, size_t n) | 219 | GNUNET_strlcpy(char *dst, const char *src, size_t n) |
220 | { | 220 | { |
221 | size_t slen; | 221 | size_t slen; |
222 | 222 | ||
223 | GNUNET_assert (0 != n); | 223 | GNUNET_assert(0 != n); |
224 | slen = strnlen (src, n - 1); | 224 | slen = strnlen(src, n - 1); |
225 | memcpy (dst, src, slen); | 225 | memcpy(dst, src, slen); |
226 | dst[slen] = '\0'; | 226 | dst[slen] = '\0'; |
227 | return slen; | 227 | return slen; |
228 | } | 228 | } |
@@ -231,8 +231,7 @@ GNUNET_strlcpy (char *dst, const char *src, size_t n) | |||
231 | /** | 231 | /** |
232 | * Unit conversion table entry for 'convert_with_table'. | 232 | * Unit conversion table entry for 'convert_with_table'. |
233 | */ | 233 | */ |
234 | struct ConversionTable | 234 | struct ConversionTable { |
235 | { | ||
236 | /** | 235 | /** |
237 | * Name of the unit (or NULL for end of table). | 236 | * Name of the unit (or NULL for end of table). |
238 | */ | 237 | */ |
@@ -257,9 +256,9 @@ struct ConversionTable | |||
257 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 256 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
258 | */ | 257 | */ |
259 | static int | 258 | static int |
260 | convert_with_table (const char *input, | 259 | convert_with_table(const char *input, |
261 | const struct ConversionTable *table, | 260 | const struct ConversionTable *table, |
262 | unsigned long long *output) | 261 | unsigned long long *output) |
263 | { | 262 | { |
264 | unsigned long long ret; | 263 | unsigned long long ret; |
265 | char *in; | 264 | char *in; |
@@ -269,40 +268,41 @@ convert_with_table (const char *input, | |||
269 | 268 | ||
270 | ret = 0; | 269 | ret = 0; |
271 | last = 0; | 270 | last = 0; |
272 | in = GNUNET_strdup (input); | 271 | in = GNUNET_strdup(input); |
273 | for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " ")) | 272 | for (tok = strtok(in, " "); tok != NULL; tok = strtok(NULL, " ")) |
274 | { | ||
275 | do | ||
276 | { | 273 | { |
277 | i = 0; | 274 | do |
278 | while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok))) | ||
279 | i++; | ||
280 | if (table[i].name != NULL) | ||
281 | { | ||
282 | last *= table[i].value; | ||
283 | break; /* next tok */ | ||
284 | } | ||
285 | else | ||
286 | { | ||
287 | char *endptr; | ||
288 | ret += last; | ||
289 | errno = 0; | ||
290 | last = strtoull (tok, &endptr, 10); | ||
291 | if ((0 != errno) || (endptr == tok)) | ||
292 | { | 275 | { |
293 | GNUNET_free (in); | 276 | i = 0; |
294 | return GNUNET_SYSERR; /* expected number */ | 277 | while ((table[i].name != NULL) && (0 != strcasecmp(table[i].name, tok))) |
278 | i++; | ||
279 | if (table[i].name != NULL) | ||
280 | { | ||
281 | last *= table[i].value; | ||
282 | break; /* next tok */ | ||
283 | } | ||
284 | else | ||
285 | { | ||
286 | char *endptr; | ||
287 | ret += last; | ||
288 | errno = 0; | ||
289 | last = strtoull(tok, &endptr, 10); | ||
290 | if ((0 != errno) || (endptr == tok)) | ||
291 | { | ||
292 | GNUNET_free(in); | ||
293 | return GNUNET_SYSERR; /* expected number */ | ||
294 | } | ||
295 | if ('\0' == endptr[0]) | ||
296 | break; /* next tok */ | ||
297 | else | ||
298 | tok = endptr; /* and re-check (handles times like "10s") */ | ||
299 | } | ||
295 | } | 300 | } |
296 | if ('\0' == endptr[0]) | 301 | while (GNUNET_YES); |
297 | break; /* next tok */ | 302 | } |
298 | else | ||
299 | tok = endptr; /* and re-check (handles times like "10s") */ | ||
300 | } | ||
301 | } while (GNUNET_YES); | ||
302 | } | ||
303 | ret += last; | 303 | ret += last; |
304 | *output = ret; | 304 | *output = ret; |
305 | GNUNET_free (in); | 305 | GNUNET_free(in); |
306 | return GNUNET_OK; | 306 | return GNUNET_OK; |
307 | } | 307 | } |
308 | 308 | ||
@@ -315,26 +315,26 @@ convert_with_table (const char *input, | |||
315 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 315 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
316 | */ | 316 | */ |
317 | int | 317 | int |
318 | GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, | 318 | GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size, |
319 | unsigned long long *size) | 319 | unsigned long long *size) |
320 | { | 320 | { |
321 | static const struct ConversionTable table[] = | 321 | static const struct ConversionTable table[] = |
322 | {{"B", 1}, | 322 | { { "B", 1 }, |
323 | {"KiB", 1024}, | 323 | { "KiB", 1024 }, |
324 | {"kB", 1000}, | 324 | { "kB", 1000 }, |
325 | {"MiB", 1024 * 1024}, | 325 | { "MiB", 1024 * 1024 }, |
326 | {"MB", 1000 * 1000}, | 326 | { "MB", 1000 * 1000 }, |
327 | {"GiB", 1024 * 1024 * 1024}, | 327 | { "GiB", 1024 * 1024 * 1024 }, |
328 | {"GB", 1000 * 1000 * 1000}, | 328 | { "GB", 1000 * 1000 * 1000 }, |
329 | {"TiB", 1024LL * 1024LL * 1024LL * 1024LL}, | 329 | { "TiB", 1024LL * 1024LL * 1024LL * 1024LL }, |
330 | {"TB", 1000LL * 1000LL * 1000LL * 1024LL}, | 330 | { "TB", 1000LL * 1000LL * 1000LL * 1024LL }, |
331 | {"PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, | 331 | { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL }, |
332 | {"PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL}, | 332 | { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL }, |
333 | {"EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL}, | 333 | { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL }, |
334 | {"EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL}, | 334 | { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL }, |
335 | {NULL, 0}}; | 335 | { NULL, 0 } }; |
336 | 336 | ||
337 | return convert_with_table (fancy_size, table, size); | 337 | return convert_with_table(fancy_size, table, size); |
338 | } | 338 | } |
339 | 339 | ||
340 | 340 | ||
@@ -347,43 +347,43 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, | |||
347 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 347 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
348 | */ | 348 | */ |
349 | int | 349 | int |
350 | GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, | 350 | GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, |
351 | struct GNUNET_TIME_Relative *rtime) | 351 | struct GNUNET_TIME_Relative *rtime) |
352 | { | 352 | { |
353 | static const struct ConversionTable table[] = | 353 | static const struct ConversionTable table[] = |
354 | {{"us", 1}, | 354 | { { "us", 1 }, |
355 | {"ms", 1000}, | 355 | { "ms", 1000 }, |
356 | {"s", 1000 * 1000LL}, | 356 | { "s", 1000 * 1000LL }, |
357 | {"second", 1000 * 1000LL}, | 357 | { "second", 1000 * 1000LL }, |
358 | {"seconds", 1000 * 1000LL}, | 358 | { "seconds", 1000 * 1000LL }, |
359 | {"\"", 1000 * 1000LL}, | 359 | { "\"", 1000 * 1000LL }, |
360 | {"m", 60 * 1000 * 1000LL}, | 360 | { "m", 60 * 1000 * 1000LL }, |
361 | {"min", 60 * 1000 * 1000LL}, | 361 | { "min", 60 * 1000 * 1000LL }, |
362 | {"minute", 60 * 1000 * 1000LL}, | 362 | { "minute", 60 * 1000 * 1000LL }, |
363 | {"minutes", 60 * 1000 * 1000LL}, | 363 | { "minutes", 60 * 1000 * 1000LL }, |
364 | {"'", 60 * 1000 * 1000LL}, | 364 | { "'", 60 * 1000 * 1000LL }, |
365 | {"h", 60 * 60 * 1000 * 1000LL}, | 365 | { "h", 60 * 60 * 1000 * 1000LL }, |
366 | {"hour", 60 * 60 * 1000 * 1000LL}, | 366 | { "hour", 60 * 60 * 1000 * 1000LL }, |
367 | {"hours", 60 * 60 * 1000 * 1000LL}, | 367 | { "hours", 60 * 60 * 1000 * 1000LL }, |
368 | {"d", 24 * 60 * 60 * 1000LL * 1000LL}, | 368 | { "d", 24 * 60 * 60 * 1000LL * 1000LL }, |
369 | {"day", 24 * 60 * 60 * 1000LL * 1000LL}, | 369 | { "day", 24 * 60 * 60 * 1000LL * 1000LL }, |
370 | {"days", 24 * 60 * 60 * 1000LL * 1000LL}, | 370 | { "days", 24 * 60 * 60 * 1000LL * 1000LL }, |
371 | {"week", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, | 371 | { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL }, |
372 | {"weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, | 372 | { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL }, |
373 | {"year", 31536000000000LL /* year */}, | 373 | { "year", 31536000000000LL /* year */ }, |
374 | {"years", 31536000000000LL /* year */}, | 374 | { "years", 31536000000000LL /* year */ }, |
375 | {"a", 31536000000000LL /* year */}, | 375 | { "a", 31536000000000LL /* year */ }, |
376 | {NULL, 0}}; | 376 | { NULL, 0 } }; |
377 | int ret; | 377 | int ret; |
378 | unsigned long long val; | 378 | unsigned long long val; |
379 | 379 | ||
380 | if (0 == strcasecmp ("forever", fancy_time)) | 380 | if (0 == strcasecmp("forever", fancy_time)) |
381 | { | 381 | { |
382 | *rtime = GNUNET_TIME_UNIT_FOREVER_REL; | 382 | *rtime = GNUNET_TIME_UNIT_FOREVER_REL; |
383 | return GNUNET_OK; | 383 | return GNUNET_OK; |
384 | } | 384 | } |
385 | ret = convert_with_table (fancy_time, table, &val); | 385 | ret = convert_with_table(fancy_time, table, &val); |
386 | rtime->rel_value_us = (uint64_t) val; | 386 | rtime->rel_value_us = (uint64_t)val; |
387 | return ret; | 387 | return ret; |
388 | } | 388 | } |
389 | 389 | ||
@@ -398,33 +398,33 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, | |||
398 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | 398 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
399 | */ | 399 | */ |
400 | int | 400 | int |
401 | GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, | 401 | GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, |
402 | struct GNUNET_TIME_Absolute *atime) | 402 | struct GNUNET_TIME_Absolute *atime) |
403 | { | 403 | { |
404 | struct tm tv; | 404 | struct tm tv; |
405 | time_t t; | 405 | time_t t; |
406 | const char *eos; | 406 | const char *eos; |
407 | 407 | ||
408 | if (0 == strcasecmp ("end of time", fancy_time)) | 408 | if (0 == strcasecmp("end of time", fancy_time)) |
409 | { | 409 | { |
410 | *atime = GNUNET_TIME_UNIT_FOREVER_ABS; | 410 | *atime = GNUNET_TIME_UNIT_FOREVER_ABS; |
411 | return GNUNET_OK; | 411 | return GNUNET_OK; |
412 | } | 412 | } |
413 | eos = &fancy_time[strlen (fancy_time)]; | 413 | eos = &fancy_time[strlen(fancy_time)]; |
414 | memset (&tv, 0, sizeof (tv)); | 414 | memset(&tv, 0, sizeof(tv)); |
415 | if ((eos != strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) && | 415 | if ((eos != strptime(fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) && |
416 | (eos != strptime (fancy_time, "%c", &tv)) && | 416 | (eos != strptime(fancy_time, "%c", &tv)) && |
417 | (eos != strptime (fancy_time, "%Ec", &tv)) && | 417 | (eos != strptime(fancy_time, "%Ec", &tv)) && |
418 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && | 418 | (eos != strptime(fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && |
419 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) && | 419 | (eos != strptime(fancy_time, "%Y-%m-%d %H:%M", &tv)) && |
420 | (eos != strptime (fancy_time, "%x", &tv)) && | 420 | (eos != strptime(fancy_time, "%x", &tv)) && |
421 | (eos != strptime (fancy_time, "%Ex", &tv)) && | 421 | (eos != strptime(fancy_time, "%Ex", &tv)) && |
422 | (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) && | 422 | (eos != strptime(fancy_time, "%Y-%m-%d", &tv)) && |
423 | (eos != strptime (fancy_time, "%Y-%m", &tv)) && | 423 | (eos != strptime(fancy_time, "%Y-%m", &tv)) && |
424 | (eos != strptime (fancy_time, "%Y", &tv))) | 424 | (eos != strptime(fancy_time, "%Y", &tv))) |
425 | return GNUNET_SYSERR; | 425 | return GNUNET_SYSERR; |
426 | t = mktime (&tv); | 426 | t = mktime(&tv); |
427 | atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL); | 427 | atime->abs_value_us = (uint64_t)((uint64_t)t * 1000LL * 1000LL); |
428 | return GNUNET_OK; | 428 | return GNUNET_OK; |
429 | } | 429 | } |
430 | 430 | ||
@@ -443,10 +443,10 @@ GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, | |||
443 | * string is returned. | 443 | * string is returned. |
444 | */ | 444 | */ |
445 | char * | 445 | char * |
446 | GNUNET_STRINGS_conv (const char *input, | 446 | GNUNET_STRINGS_conv(const char *input, |
447 | size_t len, | 447 | size_t len, |
448 | const char *input_charset, | 448 | const char *input_charset, |
449 | const char *output_charset) | 449 | const char *output_charset) |
450 | { | 450 | { |
451 | char *ret; | 451 | char *ret; |
452 | uint8_t *u8_string; | 452 | uint8_t *u8_string; |
@@ -454,51 +454,51 @@ GNUNET_STRINGS_conv (const char *input, | |||
454 | size_t u8_string_length; | 454 | size_t u8_string_length; |
455 | size_t encoded_string_length; | 455 | size_t encoded_string_length; |
456 | 456 | ||
457 | u8_string = u8_conv_from_encoding (input_charset, | 457 | u8_string = u8_conv_from_encoding(input_charset, |
458 | iconveh_error, | 458 | iconveh_error, |
459 | input, | 459 | input, |
460 | len, | 460 | len, |
461 | NULL, | 461 | NULL, |
462 | NULL, | 462 | NULL, |
463 | &u8_string_length); | 463 | &u8_string_length); |
464 | if (NULL == u8_string) | 464 | if (NULL == u8_string) |
465 | { | 465 | { |
466 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); | 466 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); |
467 | goto fail; | 467 | goto fail; |
468 | } | 468 | } |
469 | if (0 == strcmp (output_charset, "UTF-8")) | 469 | if (0 == strcmp(output_charset, "UTF-8")) |
470 | { | 470 | { |
471 | ret = GNUNET_malloc (u8_string_length + 1); | 471 | ret = GNUNET_malloc(u8_string_length + 1); |
472 | GNUNET_memcpy (ret, u8_string, u8_string_length); | 472 | GNUNET_memcpy(ret, u8_string, u8_string_length); |
473 | ret[u8_string_length] = '\0'; | 473 | ret[u8_string_length] = '\0'; |
474 | free (u8_string); | 474 | free(u8_string); |
475 | return ret; | 475 | return ret; |
476 | } | 476 | } |
477 | encoded_string = u8_conv_to_encoding (output_charset, | 477 | encoded_string = u8_conv_to_encoding(output_charset, |
478 | iconveh_error, | 478 | iconveh_error, |
479 | u8_string, | 479 | u8_string, |
480 | u8_string_length, | 480 | u8_string_length, |
481 | NULL, | 481 | NULL, |
482 | NULL, | 482 | NULL, |
483 | &encoded_string_length); | 483 | &encoded_string_length); |
484 | free (u8_string); | 484 | free(u8_string); |
485 | if (NULL == encoded_string) | 485 | if (NULL == encoded_string) |
486 | { | 486 | { |
487 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding"); | 487 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding"); |
488 | goto fail; | 488 | goto fail; |
489 | } | 489 | } |
490 | ret = GNUNET_malloc (encoded_string_length + 1); | 490 | ret = GNUNET_malloc(encoded_string_length + 1); |
491 | GNUNET_memcpy (ret, encoded_string, encoded_string_length); | 491 | GNUNET_memcpy(ret, encoded_string, encoded_string_length); |
492 | ret[encoded_string_length] = '\0'; | 492 | ret[encoded_string_length] = '\0'; |
493 | free (encoded_string); | 493 | free(encoded_string); |
494 | return ret; | 494 | return ret; |
495 | fail: | 495 | fail: |
496 | LOG (GNUNET_ERROR_TYPE_WARNING, | 496 | LOG(GNUNET_ERROR_TYPE_WARNING, |
497 | _ ("Character sets requested were `%s'->`%s'\n"), | 497 | _("Character sets requested were `%s'->`%s'\n"), |
498 | "UTF-8", | 498 | "UTF-8", |
499 | output_charset); | 499 | output_charset); |
500 | ret = GNUNET_malloc (len + 1); | 500 | ret = GNUNET_malloc(len + 1); |
501 | GNUNET_memcpy (ret, input, len); | 501 | GNUNET_memcpy(ret, input, len); |
502 | ret[len] = '\0'; | 502 | ret[len] = '\0'; |
503 | return ret; | 503 | return ret; |
504 | } | 504 | } |
@@ -517,9 +517,9 @@ fail: | |||
517 | * string is returned. | 517 | * string is returned. |
518 | */ | 518 | */ |
519 | char * | 519 | char * |
520 | GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) | 520 | GNUNET_STRINGS_to_utf8(const char *input, size_t len, const char *charset) |
521 | { | 521 | { |
522 | return GNUNET_STRINGS_conv (input, len, charset, "UTF-8"); | 522 | return GNUNET_STRINGS_conv(input, len, charset, "UTF-8"); |
523 | } | 523 | } |
524 | 524 | ||
525 | 525 | ||
@@ -535,9 +535,9 @@ GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) | |||
535 | * string is returned. | 535 | * string is returned. |
536 | */ | 536 | */ |
537 | char * | 537 | char * |
538 | GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset) | 538 | GNUNET_STRINGS_from_utf8(const char *input, size_t len, const char *charset) |
539 | { | 539 | { |
540 | return GNUNET_STRINGS_conv (input, len, "UTF-8", charset); | 540 | return GNUNET_STRINGS_conv(input, len, "UTF-8", charset); |
541 | } | 541 | } |
542 | 542 | ||
543 | 543 | ||
@@ -549,20 +549,20 @@ GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset) | |||
549 | * @param output output buffer | 549 | * @param output output buffer |
550 | */ | 550 | */ |
551 | void | 551 | void |
552 | GNUNET_STRINGS_utf8_tolower (const char *input, char *output) | 552 | GNUNET_STRINGS_utf8_tolower(const char *input, char *output) |
553 | { | 553 | { |
554 | uint8_t *tmp_in; | 554 | uint8_t *tmp_in; |
555 | size_t len; | 555 | size_t len; |
556 | 556 | ||
557 | tmp_in = u8_tolower ((uint8_t *) input, | 557 | tmp_in = u8_tolower((uint8_t *)input, |
558 | strlen ((char *) input), | 558 | strlen((char *)input), |
559 | NULL, | 559 | NULL, |
560 | UNINORM_NFD, | 560 | UNINORM_NFD, |
561 | NULL, | 561 | NULL, |
562 | &len); | 562 | &len); |
563 | GNUNET_memcpy (output, tmp_in, len); | 563 | GNUNET_memcpy(output, tmp_in, len); |
564 | output[len] = '\0'; | 564 | output[len] = '\0'; |
565 | free (tmp_in); | 565 | free(tmp_in); |
566 | } | 566 | } |
567 | 567 | ||
568 | 568 | ||
@@ -574,20 +574,20 @@ GNUNET_STRINGS_utf8_tolower (const char *input, char *output) | |||
574 | * @param output output buffer | 574 | * @param output output buffer |
575 | */ | 575 | */ |
576 | void | 576 | void |
577 | GNUNET_STRINGS_utf8_toupper (const char *input, char *output) | 577 | GNUNET_STRINGS_utf8_toupper(const char *input, char *output) |
578 | { | 578 | { |
579 | uint8_t *tmp_in; | 579 | uint8_t *tmp_in; |
580 | size_t len; | 580 | size_t len; |
581 | 581 | ||
582 | tmp_in = u8_toupper ((uint8_t *) input, | 582 | tmp_in = u8_toupper((uint8_t *)input, |
583 | strlen ((char *) input), | 583 | strlen((char *)input), |
584 | NULL, | 584 | NULL, |
585 | UNINORM_NFD, | 585 | UNINORM_NFD, |
586 | NULL, | 586 | NULL, |
587 | &len); | 587 | &len); |
588 | GNUNET_memcpy (output, tmp_in, len); | 588 | GNUNET_memcpy(output, tmp_in, len); |
589 | output[len] = '\0'; | 589 | output[len] = '\0'; |
590 | free (tmp_in); | 590 | free(tmp_in); |
591 | } | 591 | } |
592 | 592 | ||
593 | 593 | ||
@@ -599,9 +599,10 @@ GNUNET_STRINGS_utf8_toupper (const char *input, char *output) | |||
599 | * NULL is returned on error | 599 | * NULL is returned on error |
600 | */ | 600 | */ |
601 | char * | 601 | char * |
602 | GNUNET_STRINGS_filename_expand (const char *fil) | 602 | GNUNET_STRINGS_filename_expand(const char *fil) |
603 | { | 603 | { |
604 | char *buffer; | 604 | char *buffer; |
605 | |||
605 | #ifndef MINGW | 606 | #ifndef MINGW |
606 | size_t len; | 607 | size_t len; |
607 | char *fm; | 608 | char *fm; |
@@ -617,90 +618,90 @@ GNUNET_STRINGS_filename_expand (const char *fil) | |||
617 | #ifndef MINGW | 618 | #ifndef MINGW |
618 | if (fil[0] == DIR_SEPARATOR) | 619 | if (fil[0] == DIR_SEPARATOR) |
619 | /* absolute path, just copy */ | 620 | /* absolute path, just copy */ |
620 | return GNUNET_strdup (fil); | 621 | return GNUNET_strdup(fil); |
621 | if (fil[0] == '~') | 622 | if (fil[0] == '~') |
622 | { | ||
623 | fm = getenv ("HOME"); | ||
624 | if (fm == NULL) | ||
625 | { | 623 | { |
626 | LOG (GNUNET_ERROR_TYPE_WARNING, | 624 | fm = getenv("HOME"); |
627 | _ ("Failed to expand `$HOME': environment variable `HOME' not set")); | 625 | if (fm == NULL) |
628 | return NULL; | 626 | { |
629 | } | 627 | LOG(GNUNET_ERROR_TYPE_WARNING, |
630 | fm = GNUNET_strdup (fm); | 628 | _("Failed to expand `$HOME': environment variable `HOME' not set")); |
631 | /* do not copy '~' */ | 629 | return NULL; |
632 | fil_ptr = fil + 1; | 630 | } |
631 | fm = GNUNET_strdup(fm); | ||
632 | /* do not copy '~' */ | ||
633 | fil_ptr = fil + 1; | ||
633 | 634 | ||
634 | /* skip over dir seperator to be consistent */ | 635 | /* skip over dir seperator to be consistent */ |
635 | if (fil_ptr[0] == DIR_SEPARATOR) | 636 | if (fil_ptr[0] == DIR_SEPARATOR) |
636 | fil_ptr++; | 637 | fil_ptr++; |
637 | } | ||
638 | else | ||
639 | { | ||
640 | /* relative path */ | ||
641 | fil_ptr = fil; | ||
642 | len = 512; | ||
643 | fm = NULL; | ||
644 | while (1) | ||
645 | { | ||
646 | buffer = GNUNET_malloc (len); | ||
647 | if (getcwd (buffer, len) != NULL) | ||
648 | { | ||
649 | fm = buffer; | ||
650 | break; | ||
651 | } | ||
652 | if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) | ||
653 | { | ||
654 | len *= 2; | ||
655 | GNUNET_free (buffer); | ||
656 | continue; | ||
657 | } | ||
658 | GNUNET_free (buffer); | ||
659 | break; | ||
660 | } | 638 | } |
661 | if (fm == NULL) | 639 | else |
662 | { | 640 | { |
663 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd"); | 641 | /* relative path */ |
664 | buffer = getenv ("PWD"); /* alternative */ | 642 | fil_ptr = fil; |
665 | if (buffer != NULL) | 643 | len = 512; |
666 | fm = GNUNET_strdup (buffer); | 644 | fm = NULL; |
645 | while (1) | ||
646 | { | ||
647 | buffer = GNUNET_malloc(len); | ||
648 | if (getcwd(buffer, len) != NULL) | ||
649 | { | ||
650 | fm = buffer; | ||
651 | break; | ||
652 | } | ||
653 | if ((errno == ERANGE) && (len < 1024 * 1024 * 4)) | ||
654 | { | ||
655 | len *= 2; | ||
656 | GNUNET_free(buffer); | ||
657 | continue; | ||
658 | } | ||
659 | GNUNET_free(buffer); | ||
660 | break; | ||
661 | } | ||
662 | if (fm == NULL) | ||
663 | { | ||
664 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "getcwd"); | ||
665 | buffer = getenv("PWD"); /* alternative */ | ||
666 | if (buffer != NULL) | ||
667 | fm = GNUNET_strdup(buffer); | ||
668 | } | ||
669 | if (fm == NULL) | ||
670 | fm = GNUNET_strdup("./"); /* give up */ | ||
667 | } | 671 | } |
668 | if (fm == NULL) | 672 | GNUNET_asprintf(&buffer, |
669 | fm = GNUNET_strdup ("./"); /* give up */ | 673 | "%s%s%s", |
670 | } | 674 | fm, |
671 | GNUNET_asprintf (&buffer, | 675 | (fm[strlen(fm) - 1] == DIR_SEPARATOR) ? "" |
672 | "%s%s%s", | 676 | : DIR_SEPARATOR_STR, |
673 | fm, | 677 | fil_ptr); |
674 | (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? "" | 678 | GNUNET_free(fm); |
675 | : DIR_SEPARATOR_STR, | ||
676 | fil_ptr); | ||
677 | GNUNET_free (fm); | ||
678 | return buffer; | 679 | return buffer; |
679 | #else | 680 | #else |
680 | fn = GNUNET_malloc (MAX_PATH + 1); | 681 | fn = GNUNET_malloc(MAX_PATH + 1); |
681 | |||
682 | if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS) | ||
683 | { | ||
684 | SetErrnoFromWinError (lRet); | ||
685 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); | ||
686 | return NULL; | ||
687 | } | ||
688 | /* is the path relative? */ | ||
689 | if ((0 != strncmp (fn + 1, ":\\", 2)) && (0 != strncmp (fn, "\\\\", 2))) | ||
690 | { | ||
691 | char szCurDir[MAX_PATH + 1]; | ||
692 | 682 | ||
693 | lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir); | 683 | if ((lRet = plibc_conv_to_win_path(fil, fn)) != ERROR_SUCCESS) |
694 | if (lRet + strlen (fn) + 1 > (MAX_PATH + 1)) | ||
695 | { | 684 | { |
696 | SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW); | 685 | SetErrnoFromWinError(lRet); |
697 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory"); | 686 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); |
698 | return NULL; | 687 | return NULL; |
699 | } | 688 | } |
700 | GNUNET_asprintf (&buffer, "%s\\%s", szCurDir, fn); | 689 | /* is the path relative? */ |
701 | GNUNET_free (fn); | 690 | if ((0 != strncmp(fn + 1, ":\\", 2)) && (0 != strncmp(fn, "\\\\", 2))) |
702 | fn = buffer; | 691 | { |
703 | } | 692 | char szCurDir[MAX_PATH + 1]; |
693 | |||
694 | lRet = GetCurrentDirectory(MAX_PATH + 1, szCurDir); | ||
695 | if (lRet + strlen(fn) + 1 > (MAX_PATH + 1)) | ||
696 | { | ||
697 | SetErrnoFromWinError(ERROR_BUFFER_OVERFLOW); | ||
698 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory"); | ||
699 | return NULL; | ||
700 | } | ||
701 | GNUNET_asprintf(&buffer, "%s\\%s", szCurDir, fn); | ||
702 | GNUNET_free(fn); | ||
703 | fn = buffer; | ||
704 | } | ||
704 | 705 | ||
705 | return fn; | 706 | return fn; |
706 | #endif | 707 | #endif |
@@ -717,47 +718,47 @@ GNUNET_STRINGS_filename_expand (const char *fil) | |||
717 | * @return time as human-readable string | 718 | * @return time as human-readable string |
718 | */ | 719 | */ |
719 | const char * | 720 | const char * |
720 | GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, | 721 | GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, |
721 | int do_round) | 722 | int do_round) |
722 | { | 723 | { |
723 | static char buf[128]; | 724 | static char buf[128]; |
724 | const char *unit = _ (/* time unit */ "µs"); | 725 | const char *unit = _(/* time unit */ "µs"); |
725 | uint64_t dval = delta.rel_value_us; | 726 | uint64_t dval = delta.rel_value_us; |
726 | 727 | ||
727 | if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) | 728 | if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) |
728 | return _ ("forever"); | 729 | return _("forever"); |
729 | if (0 == delta.rel_value_us) | 730 | if (0 == delta.rel_value_us) |
730 | return _ ("0 ms"); | 731 | return _("0 ms"); |
731 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) | 732 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) |
732 | { | ||
733 | dval = dval / 1000; | ||
734 | unit = _ (/* time unit */ "ms"); | ||
735 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) | ||
736 | { | 733 | { |
737 | dval = dval / 1000; | 734 | dval = dval / 1000; |
738 | unit = _ (/* time unit */ "s"); | 735 | unit = _(/* time unit */ "ms"); |
739 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) | 736 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) |
740 | { | ||
741 | dval = dval / 60; | ||
742 | unit = _ (/* time unit */ "m"); | ||
743 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) | ||
744 | { | 737 | { |
745 | dval = dval / 60; | 738 | dval = dval / 1000; |
746 | unit = _ (/* time unit */ "h"); | 739 | unit = _(/* time unit */ "s"); |
747 | if (((GNUNET_YES == do_round) && (dval > 5 * 24)) || | 740 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) |
748 | (0 == (dval % 24))) | 741 | { |
749 | { | 742 | dval = dval / 60; |
750 | dval = dval / 24; | 743 | unit = _(/* time unit */ "m"); |
751 | if (1 == dval) | 744 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) |
752 | unit = _ (/* time unit */ "day"); | 745 | { |
753 | else | 746 | dval = dval / 60; |
754 | unit = _ (/* time unit */ "days"); | 747 | unit = _(/* time unit */ "h"); |
755 | } | 748 | if (((GNUNET_YES == do_round) && (dval > 5 * 24)) || |
749 | (0 == (dval % 24))) | ||
750 | { | ||
751 | dval = dval / 24; | ||
752 | if (1 == dval) | ||
753 | unit = _(/* time unit */ "day"); | ||
754 | else | ||
755 | unit = _(/* time unit */ "days"); | ||
756 | } | ||
757 | } | ||
758 | } | ||
756 | } | 759 | } |
757 | } | ||
758 | } | 760 | } |
759 | } | 761 | GNUNET_snprintf(buf, sizeof(buf), "%llu %s", dval, unit); |
760 | GNUNET_snprintf (buf, sizeof (buf), "%llu %s", dval, unit); | ||
761 | return buf; | 762 | return buf; |
762 | } | 763 | } |
763 | 764 | ||
@@ -772,16 +773,16 @@ GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, | |||
772 | * @return timestamp in human-readable form in local time | 773 | * @return timestamp in human-readable form in local time |
773 | */ | 774 | */ |
774 | const char * | 775 | const char * |
775 | GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) | 776 | GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t) |
776 | { | 777 | { |
777 | static char buf[255]; | 778 | static char buf[255]; |
778 | time_t tt; | 779 | time_t tt; |
779 | struct tm *tp; | 780 | struct tm *tp; |
780 | 781 | ||
781 | if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) | 782 | if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) |
782 | return _ ("end of time"); | 783 | return _("end of time"); |
783 | tt = t.abs_value_us / 1000LL / 1000LL; | 784 | tt = t.abs_value_us / 1000LL / 1000LL; |
784 | tp = localtime (&tt); | 785 | tp = localtime(&tt); |
785 | /* This is hacky, but i don't know a way to detect libc character encoding. | 786 | /* This is hacky, but i don't know a way to detect libc character encoding. |
786 | * Just expect utf8 from glibc these days. | 787 | * Just expect utf8 from glibc these days. |
787 | * As for msvcrt, use the wide variant, which always returns utf16 | 788 | * As for msvcrt, use the wide variant, which always returns utf16 |
@@ -789,28 +790,28 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) | |||
789 | * set conversion routines to convert to UTF8). | 790 | * set conversion routines to convert to UTF8). |
790 | */ | 791 | */ |
791 | #ifndef WINDOWS | 792 | #ifndef WINDOWS |
792 | strftime (buf, sizeof (buf), "%a %b %d %H:%M:%S %Y", tp); | 793 | strftime(buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp); |
793 | #else | 794 | #else |
794 | { | 795 | { |
795 | static wchar_t wbuf[255]; | 796 | static wchar_t wbuf[255]; |
796 | uint8_t *conved; | 797 | uint8_t *conved; |
797 | size_t ssize; | 798 | size_t ssize; |
798 | 799 | ||
799 | wcsftime (wbuf, | 800 | wcsftime(wbuf, |
800 | sizeof (wbuf) / sizeof (wchar_t), | 801 | sizeof(wbuf) / sizeof(wchar_t), |
801 | L"%a %b %d %H:%M:%S %Y", | 802 | L"%a %b %d %H:%M:%S %Y", |
802 | tp); | 803 | tp); |
803 | 804 | ||
804 | ssize = sizeof (buf); | 805 | ssize = sizeof(buf); |
805 | conved = u16_to_u8 (wbuf, | 806 | conved = u16_to_u8(wbuf, |
806 | sizeof (wbuf) / sizeof (wchar_t), | 807 | sizeof(wbuf) / sizeof(wchar_t), |
807 | (uint8_t *) buf, | 808 | (uint8_t *)buf, |
808 | &ssize); | 809 | &ssize); |
809 | if (conved != (uint8_t *) buf) | 810 | if (conved != (uint8_t *)buf) |
810 | { | 811 | { |
811 | GNUNET_strlcpy (buf, (char *) conved, sizeof (buf)); | 812 | GNUNET_strlcpy(buf, (char *)conved, sizeof(buf)); |
812 | free (conved); | 813 | free(conved); |
813 | } | 814 | } |
814 | } | 815 | } |
815 | #endif | 816 | #endif |
816 | return buf; | 817 | return buf; |
@@ -829,12 +830,12 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) | |||
829 | * itself. | 830 | * itself. |
830 | */ | 831 | */ |
831 | const char * | 832 | const char * |
832 | GNUNET_STRINGS_get_short_name (const char *filename) | 833 | GNUNET_STRINGS_get_short_name(const char *filename) |
833 | { | 834 | { |
834 | const char *short_fn = filename; | 835 | const char *short_fn = filename; |
835 | const char *ss; | 836 | const char *ss; |
836 | 837 | ||
837 | while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0')) | 838 | while (NULL != (ss = strstr(short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0')) |
838 | short_fn = 1 + ss; | 839 | short_fn = 1 + ss; |
839 | return short_fn; | 840 | return short_fn; |
840 | } | 841 | } |
@@ -848,48 +849,51 @@ GNUNET_STRINGS_get_short_name (const char *filename) | |||
848 | * @return corresponding numeric value | 849 | * @return corresponding numeric value |
849 | */ | 850 | */ |
850 | static unsigned int | 851 | static unsigned int |
851 | getValue__ (unsigned char a) | 852 | getValue__(unsigned char a) |
852 | { | 853 | { |
853 | unsigned int dec; | 854 | unsigned int dec; |
854 | 855 | ||
855 | switch (a) | 856 | switch (a) |
856 | { | 857 | { |
857 | case 'O': | 858 | case 'O': |
858 | case 'o': | 859 | case 'o': |
859 | a = '0'; | 860 | a = '0'; |
860 | break; | 861 | break; |
861 | case 'i': | 862 | |
862 | case 'I': | 863 | case 'i': |
863 | case 'l': | 864 | case 'I': |
864 | case 'L': | 865 | case 'l': |
865 | a = '1'; | 866 | case 'L': |
866 | break; | 867 | a = '1'; |
868 | break; | ||
869 | |||
867 | /* also consider U to be V */ | 870 | /* also consider U to be V */ |
868 | case 'u': | 871 | case 'u': |
869 | case 'U': | 872 | case 'U': |
870 | a = 'V'; | 873 | a = 'V'; |
871 | break; | 874 | break; |
872 | default: | 875 | |
873 | break; | 876 | default: |
874 | } | 877 | break; |
878 | } | ||
875 | if ((a >= '0') && (a <= '9')) | 879 | if ((a >= '0') && (a <= '9')) |
876 | return a - '0'; | 880 | return a - '0'; |
877 | if ((a >= 'a') && (a <= 'z')) | 881 | if ((a >= 'a') && (a <= 'z')) |
878 | a = toupper (a); | 882 | a = toupper(a); |
879 | /* return (a - 'a' + 10); */ | 883 | /* return (a - 'a' + 10); */ |
880 | dec = 0; | 884 | dec = 0; |
881 | if ((a >= 'A') && (a <= 'Z')) | 885 | if ((a >= 'A') && (a <= 'Z')) |
882 | { | 886 | { |
883 | if ('I' < a) | 887 | if ('I' < a) |
884 | dec++; | 888 | dec++; |
885 | if ('L' < a) | 889 | if ('L' < a) |
886 | dec++; | 890 | dec++; |
887 | if ('O' < a) | 891 | if ('O' < a) |
888 | dec++; | 892 | dec++; |
889 | if ('U' < a) | 893 | if ('U' < a) |
890 | dec++; | 894 | dec++; |
891 | return (a - 'A' + 10 - dec); | 895 | return(a - 'A' + 10 - dec); |
892 | } | 896 | } |
893 | return -1; | 897 | return -1; |
894 | } | 898 | } |
895 | 899 | ||
@@ -907,10 +911,10 @@ getValue__ (unsigned char a) | |||
907 | * @return pointer to the next byte in @a out or NULL on error. | 911 | * @return pointer to the next byte in @a out or NULL on error. |
908 | */ | 912 | */ |
909 | char * | 913 | char * |
910 | GNUNET_STRINGS_data_to_string (const void *data, | 914 | GNUNET_STRINGS_data_to_string(const void *data, |
911 | size_t size, | 915 | size_t size, |
912 | char *out, | 916 | char *out, |
913 | size_t out_size) | 917 | size_t out_size) |
914 | { | 918 | { |
915 | /** | 919 | /** |
916 | * 32 characters for encoding | 920 | * 32 characters for encoding |
@@ -924,36 +928,36 @@ GNUNET_STRINGS_data_to_string (const void *data, | |||
924 | 928 | ||
925 | udata = data; | 929 | udata = data; |
926 | if (out_size < (size * 8 + 4) / 5) | 930 | if (out_size < (size * 8 + 4) / 5) |
927 | { | 931 | { |
928 | GNUNET_break (0); | 932 | GNUNET_break(0); |
929 | return NULL; | 933 | return NULL; |
930 | } | 934 | } |
931 | vbit = 0; | 935 | vbit = 0; |
932 | wpos = 0; | 936 | wpos = 0; |
933 | rpos = 0; | 937 | rpos = 0; |
934 | bits = 0; | 938 | bits = 0; |
935 | while ((rpos < size) || (vbit > 0)) | 939 | while ((rpos < size) || (vbit > 0)) |
936 | { | ||
937 | if ((rpos < size) && (vbit < 5)) | ||
938 | { | ||
939 | bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */ | ||
940 | vbit += 8; | ||
941 | } | ||
942 | if (vbit < 5) | ||
943 | { | ||
944 | bits <<= (5 - vbit); /* zero-padding */ | ||
945 | GNUNET_assert (vbit == ((size * 8) % 5)); | ||
946 | vbit = 5; | ||
947 | } | ||
948 | if (wpos >= out_size) | ||
949 | { | 940 | { |
950 | GNUNET_break (0); | 941 | if ((rpos < size) && (vbit < 5)) |
951 | return NULL; | 942 | { |
943 | bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */ | ||
944 | vbit += 8; | ||
945 | } | ||
946 | if (vbit < 5) | ||
947 | { | ||
948 | bits <<= (5 - vbit); /* zero-padding */ | ||
949 | GNUNET_assert(vbit == ((size * 8) % 5)); | ||
950 | vbit = 5; | ||
951 | } | ||
952 | if (wpos >= out_size) | ||
953 | { | ||
954 | GNUNET_break(0); | ||
955 | return NULL; | ||
956 | } | ||
957 | out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; | ||
958 | vbit -= 5; | ||
952 | } | 959 | } |
953 | out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31]; | 960 | GNUNET_assert(0 == vbit); |
954 | vbit -= 5; | ||
955 | } | ||
956 | GNUNET_assert (0 == vbit); | ||
957 | if (wpos < out_size) | 961 | if (wpos < out_size) |
958 | out[wpos] = '\0'; | 962 | out[wpos] = '\0'; |
959 | return &out[wpos]; | 963 | return &out[wpos]; |
@@ -971,7 +975,7 @@ GNUNET_STRINGS_data_to_string (const void *data, | |||
971 | * @return freshly allocated, null-terminated string | 975 | * @return freshly allocated, null-terminated string |
972 | */ | 976 | */ |
973 | char * | 977 | char * |
974 | GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size) | 978 | GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size) |
975 | { | 979 | { |
976 | char *str_buf; | 980 | char *str_buf; |
977 | size_t len = size * 8; | 981 | size_t len = size * 8; |
@@ -980,13 +984,13 @@ GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size) | |||
980 | if (len % 5 > 0) | 984 | if (len % 5 > 0) |
981 | len += 5 - len % 5; | 985 | len += 5 - len % 5; |
982 | len /= 5; | 986 | len /= 5; |
983 | str_buf = GNUNET_malloc (len + 1); | 987 | str_buf = GNUNET_malloc(len + 1); |
984 | end = GNUNET_STRINGS_data_to_string (buf, size, str_buf, len); | 988 | end = GNUNET_STRINGS_data_to_string(buf, size, str_buf, len); |
985 | if (NULL == end) | 989 | if (NULL == end) |
986 | { | 990 | { |
987 | GNUNET_free (str_buf); | 991 | GNUNET_free(str_buf); |
988 | return NULL; | 992 | return NULL; |
989 | } | 993 | } |
990 | *end = '\0'; | 994 | *end = '\0'; |
991 | return str_buf; | 995 | return str_buf; |
992 | } | 996 | } |
@@ -1003,10 +1007,10 @@ GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size) | |||
1003 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding | 1007 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding |
1004 | */ | 1008 | */ |
1005 | int | 1009 | int |
1006 | GNUNET_STRINGS_string_to_data (const char *enc, | 1010 | GNUNET_STRINGS_string_to_data(const char *enc, |
1007 | size_t enclen, | 1011 | size_t enclen, |
1008 | void *out, | 1012 | void *out, |
1009 | size_t out_size) | 1013 | size_t out_size) |
1010 | { | 1014 | { |
1011 | unsigned int rpos; | 1015 | unsigned int rpos; |
1012 | unsigned int wpos; | 1016 | unsigned int wpos; |
@@ -1018,48 +1022,48 @@ GNUNET_STRINGS_string_to_data (const char *enc, | |||
1018 | unsigned int encoded_len = out_size * 8; | 1022 | unsigned int encoded_len = out_size * 8; |
1019 | 1023 | ||
1020 | if (0 == enclen) | 1024 | if (0 == enclen) |
1021 | { | 1025 | { |
1022 | if (0 == out_size) | 1026 | if (0 == out_size) |
1023 | return GNUNET_OK; | 1027 | return GNUNET_OK; |
1024 | return GNUNET_SYSERR; | 1028 | return GNUNET_SYSERR; |
1025 | } | 1029 | } |
1026 | uout = out; | 1030 | uout = out; |
1027 | wpos = out_size; | 1031 | wpos = out_size; |
1028 | rpos = enclen; | 1032 | rpos = enclen; |
1029 | if ((encoded_len % 5) > 0) | 1033 | if ((encoded_len % 5) > 0) |
1030 | { | 1034 | { |
1031 | vbit = encoded_len % 5; /* padding! */ | 1035 | vbit = encoded_len % 5; /* padding! */ |
1032 | shift = 5 - vbit; | 1036 | shift = 5 - vbit; |
1033 | bits = (ret = getValue__ (enc[--rpos])) >> shift; | 1037 | bits = (ret = getValue__(enc[--rpos])) >> shift; |
1034 | } | 1038 | } |
1035 | else | 1039 | else |
1036 | { | 1040 | { |
1037 | vbit = 5; | 1041 | vbit = 5; |
1038 | shift = 0; | 1042 | shift = 0; |
1039 | bits = (ret = getValue__ (enc[--rpos])); | 1043 | bits = (ret = getValue__(enc[--rpos])); |
1040 | } | 1044 | } |
1041 | if ((encoded_len + shift) / 5 != enclen) | 1045 | if ((encoded_len + shift) / 5 != enclen) |
1042 | return GNUNET_SYSERR; | 1046 | return GNUNET_SYSERR; |
1043 | if (-1 == ret) | 1047 | if (-1 == ret) |
1044 | return GNUNET_SYSERR; | 1048 | return GNUNET_SYSERR; |
1045 | while (wpos > 0) | 1049 | while (wpos > 0) |
1046 | { | ||
1047 | if (0 == rpos) | ||
1048 | { | 1050 | { |
1049 | GNUNET_break (0); | 1051 | if (0 == rpos) |
1050 | return GNUNET_SYSERR; | 1052 | { |
1051 | } | 1053 | GNUNET_break(0); |
1052 | bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits; | 1054 | return GNUNET_SYSERR; |
1053 | if (-1 == ret) | 1055 | } |
1054 | return GNUNET_SYSERR; | 1056 | bits = ((ret = getValue__(enc[--rpos])) << vbit) | bits; |
1055 | vbit += 5; | 1057 | if (-1 == ret) |
1056 | if (vbit >= 8) | 1058 | return GNUNET_SYSERR; |
1057 | { | 1059 | vbit += 5; |
1058 | uout[--wpos] = (unsigned char) bits; | 1060 | if (vbit >= 8) |
1059 | bits >>= 8; | 1061 | { |
1060 | vbit -= 8; | 1062 | uout[--wpos] = (unsigned char)bits; |
1063 | bits >>= 8; | ||
1064 | vbit -= 8; | ||
1065 | } | ||
1061 | } | 1066 | } |
1062 | } | ||
1063 | if ((0 != rpos) || (0 != vbit)) | 1067 | if ((0 != rpos) || (0 != vbit)) |
1064 | return GNUNET_SYSERR; | 1068 | return GNUNET_SYSERR; |
1065 | return GNUNET_OK; | 1069 | return GNUNET_OK; |
@@ -1083,57 +1087,61 @@ GNUNET_STRINGS_string_to_data (const char *enc, | |||
1083 | * (if they weren't NULL). | 1087 | * (if they weren't NULL). |
1084 | */ | 1088 | */ |
1085 | int | 1089 | int |
1086 | GNUNET_STRINGS_parse_uri (const char *path, | 1090 | GNUNET_STRINGS_parse_uri(const char *path, |
1087 | char **scheme_part, | 1091 | char **scheme_part, |
1088 | const char **path_part) | 1092 | const char **path_part) |
1089 | { | 1093 | { |
1090 | size_t len; | 1094 | size_t len; |
1091 | size_t i; | 1095 | size_t i; |
1092 | int end; | 1096 | int end; |
1093 | int pp_state = 0; | 1097 | int pp_state = 0; |
1094 | const char *post_scheme_part = NULL; | 1098 | const char *post_scheme_part = NULL; |
1095 | len = strlen (path); | 1099 | |
1096 | for (end = 0, i = 0; ! end && i < len; i++) | 1100 | len = strlen(path); |
1097 | { | 1101 | for (end = 0, i = 0; !end && i < len; i++) |
1098 | switch (pp_state) | ||
1099 | { | 1102 | { |
1100 | case 0: | 1103 | switch (pp_state) |
1101 | if ((path[i] == ':') && (i > 0)) | 1104 | { |
1102 | { | 1105 | case 0: |
1103 | pp_state += 1; | 1106 | if ((path[i] == ':') && (i > 0)) |
1104 | continue; | 1107 | { |
1105 | } | 1108 | pp_state += 1; |
1106 | if (! ((path[i] >= 'A' && path[i] <= 'Z') || | 1109 | continue; |
1107 | (path[i] >= 'a' && path[i] <= 'z') || | 1110 | } |
1108 | (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || | 1111 | if (!((path[i] >= 'A' && path[i] <= 'Z') || |
1109 | path[i] == '-' || (path[i] == '.'))) | 1112 | (path[i] >= 'a' && path[i] <= 'z') || |
1110 | end = 1; | 1113 | (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || |
1111 | break; | 1114 | path[i] == '-' || (path[i] == '.'))) |
1112 | case 1: | 1115 | end = 1; |
1113 | case 2: | 1116 | break; |
1114 | if (path[i] == '/') | 1117 | |
1115 | { | 1118 | case 1: |
1116 | pp_state += 1; | 1119 | case 2: |
1117 | continue; | 1120 | if (path[i] == '/') |
1118 | } | 1121 | { |
1119 | end = 1; | 1122 | pp_state += 1; |
1120 | break; | 1123 | continue; |
1121 | case 3: | 1124 | } |
1122 | post_scheme_part = &path[i]; | 1125 | end = 1; |
1123 | end = 1; | 1126 | break; |
1124 | break; | 1127 | |
1125 | default: | 1128 | case 3: |
1126 | end = 1; | 1129 | post_scheme_part = &path[i]; |
1130 | end = 1; | ||
1131 | break; | ||
1132 | |||
1133 | default: | ||
1134 | end = 1; | ||
1135 | } | ||
1127 | } | 1136 | } |
1128 | } | ||
1129 | if (post_scheme_part == NULL) | 1137 | if (post_scheme_part == NULL) |
1130 | return GNUNET_NO; | 1138 | return GNUNET_NO; |
1131 | if (scheme_part) | 1139 | if (scheme_part) |
1132 | { | 1140 | { |
1133 | *scheme_part = GNUNET_malloc (post_scheme_part - path + 1); | 1141 | *scheme_part = GNUNET_malloc(post_scheme_part - path + 1); |
1134 | GNUNET_memcpy (*scheme_part, path, post_scheme_part - path); | 1142 | GNUNET_memcpy(*scheme_part, path, post_scheme_part - path); |
1135 | (*scheme_part)[post_scheme_part - path] = '\0'; | 1143 | (*scheme_part)[post_scheme_part - path] = '\0'; |
1136 | } | 1144 | } |
1137 | if (path_part) | 1145 | if (path_part) |
1138 | *path_part = post_scheme_part; | 1146 | *path_part = post_scheme_part; |
1139 | return GNUNET_YES; | 1147 | return GNUNET_YES; |
@@ -1155,10 +1163,10 @@ GNUNET_STRINGS_parse_uri (const char *path, | |||
1155 | * @return #GNUNET_YES if @a filename is absolute, #GNUNET_NO otherwise. | 1163 | * @return #GNUNET_YES if @a filename is absolute, #GNUNET_NO otherwise. |
1156 | */ | 1164 | */ |
1157 | int | 1165 | int |
1158 | GNUNET_STRINGS_path_is_absolute (const char *filename, | 1166 | GNUNET_STRINGS_path_is_absolute(const char *filename, |
1159 | int can_be_uri, | 1167 | int can_be_uri, |
1160 | int *r_is_uri, | 1168 | int *r_is_uri, |
1161 | char **r_uri_scheme) | 1169 | char **r_uri_scheme) |
1162 | { | 1170 | { |
1163 | #if WINDOWS | 1171 | #if WINDOWS |
1164 | size_t len; | 1172 | size_t len; |
@@ -1172,37 +1180,37 @@ GNUNET_STRINGS_path_is_absolute (const char *filename, | |||
1172 | if (filename[0] == '/') | 1180 | if (filename[0] == '/') |
1173 | return GNUNET_YES; | 1181 | return GNUNET_YES; |
1174 | if (can_be_uri) | 1182 | if (can_be_uri) |
1175 | { | ||
1176 | is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path); | ||
1177 | if (r_is_uri) | ||
1178 | *r_is_uri = is_uri; | ||
1179 | if (is_uri) | ||
1180 | { | 1183 | { |
1181 | if (r_uri_scheme) | 1184 | is_uri = GNUNET_STRINGS_parse_uri(filename, &uri, &post_scheme_path); |
1182 | *r_uri_scheme = uri; | 1185 | if (r_is_uri) |
1183 | else | 1186 | *r_is_uri = is_uri; |
1184 | GNUNET_free_non_null (uri); | 1187 | if (is_uri) |
1188 | { | ||
1189 | if (r_uri_scheme) | ||
1190 | *r_uri_scheme = uri; | ||
1191 | else | ||
1192 | GNUNET_free_non_null(uri); | ||
1185 | #if WINDOWS | 1193 | #if WINDOWS |
1186 | len = strlen (post_scheme_path); | 1194 | len = strlen(post_scheme_path); |
1187 | /* Special check for file:///c:/blah | 1195 | /* Special check for file:///c:/blah |
1188 | * We want to parse 'c:/', not '/c:/' | 1196 | * We want to parse 'c:/', not '/c:/' |
1189 | */ | 1197 | */ |
1190 | if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':') | 1198 | if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':') |
1191 | post_scheme_path = &post_scheme_path[1]; | 1199 | post_scheme_path = &post_scheme_path[1]; |
1192 | #endif | 1200 | #endif |
1193 | return GNUNET_STRINGS_path_is_absolute (post_scheme_path, | 1201 | return GNUNET_STRINGS_path_is_absolute(post_scheme_path, |
1194 | GNUNET_NO, | 1202 | GNUNET_NO, |
1195 | NULL, | 1203 | NULL, |
1196 | NULL); | 1204 | NULL); |
1205 | } | ||
1197 | } | 1206 | } |
1198 | } | ||
1199 | else | 1207 | else |
1200 | { | 1208 | { |
1201 | if (r_is_uri) | 1209 | if (r_is_uri) |
1202 | *r_is_uri = GNUNET_NO; | 1210 | *r_is_uri = GNUNET_NO; |
1203 | } | 1211 | } |
1204 | #if WINDOWS | 1212 | #if WINDOWS |
1205 | len = strlen (filename); | 1213 | len = strlen(filename); |
1206 | if (len >= 3 && | 1214 | if (len >= 3 && |
1207 | ((filename[0] >= 'A' && filename[0] <= 'Z') || | 1215 | ((filename[0] >= 'A' && filename[0] <= 'Z') || |
1208 | (filename[0] >= 'a' && filename[0] <= 'z')) && | 1216 | (filename[0] >= 'a' && filename[0] <= 'z')) && |
@@ -1215,7 +1223,7 @@ GNUNET_STRINGS_path_is_absolute (const char *filename, | |||
1215 | #if MINGW | 1223 | #if MINGW |
1216 | #define _IFMT 0170000 /* type of file */ | 1224 | #define _IFMT 0170000 /* type of file */ |
1217 | #define _IFLNK 0120000 /* symbolic link */ | 1225 | #define _IFLNK 0120000 /* symbolic link */ |
1218 | #define S_ISLNK(m) (((m) &_IFMT) == _IFLNK) | 1226 | #define S_ISLNK(m) (((m) & _IFMT) == _IFLNK) |
1219 | #endif | 1227 | #endif |
1220 | 1228 | ||
1221 | 1229 | ||
@@ -1228,32 +1236,33 @@ GNUNET_STRINGS_path_is_absolute (const char *filename, | |||
1228 | * fails, #GNUNET_SYSERR when a check can't be performed | 1236 | * fails, #GNUNET_SYSERR when a check can't be performed |
1229 | */ | 1237 | */ |
1230 | int | 1238 | int |
1231 | GNUNET_STRINGS_check_filename (const char *filename, | 1239 | GNUNET_STRINGS_check_filename(const char *filename, |
1232 | enum GNUNET_STRINGS_FilenameCheck checks) | 1240 | enum GNUNET_STRINGS_FilenameCheck checks) |
1233 | { | 1241 | { |
1234 | struct stat st; | 1242 | struct stat st; |
1243 | |||
1235 | if ((NULL == filename) || (filename[0] == '\0')) | 1244 | if ((NULL == filename) || (filename[0] == '\0')) |
1236 | return GNUNET_SYSERR; | 1245 | return GNUNET_SYSERR; |
1237 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) | 1246 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) |
1238 | if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL)) | 1247 | if (!GNUNET_STRINGS_path_is_absolute(filename, GNUNET_NO, NULL, NULL)) |
1239 | return GNUNET_NO; | 1248 | return GNUNET_NO; |
1240 | if (0 != (checks & | 1249 | if (0 != (checks & |
1241 | (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY | | 1250 | (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY | |
1242 | GNUNET_STRINGS_CHECK_IS_LINK))) | 1251 | GNUNET_STRINGS_CHECK_IS_LINK))) |
1243 | { | ||
1244 | if (0 != stat (filename, &st)) | ||
1245 | { | 1252 | { |
1246 | if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) | 1253 | if (0 != stat(filename, &st)) |
1247 | return GNUNET_NO; | 1254 | { |
1248 | else | 1255 | if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) |
1249 | return GNUNET_SYSERR; | 1256 | return GNUNET_NO; |
1257 | else | ||
1258 | return GNUNET_SYSERR; | ||
1259 | } | ||
1250 | } | 1260 | } |
1251 | } | ||
1252 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) | 1261 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) |
1253 | if (! S_ISDIR (st.st_mode)) | 1262 | if (!S_ISDIR(st.st_mode)) |
1254 | return GNUNET_NO; | 1263 | return GNUNET_NO; |
1255 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) | 1264 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) |
1256 | if (! S_ISLNK (st.st_mode)) | 1265 | if (!S_ISLNK(st.st_mode)) |
1257 | return GNUNET_NO; | 1266 | return GNUNET_NO; |
1258 | return GNUNET_YES; | 1267 | return GNUNET_YES; |
1259 | } | 1268 | } |
@@ -1272,9 +1281,9 @@ GNUNET_STRINGS_check_filename (const char *filename, | |||
1272 | * case the contents of @a r_buf are undefined. | 1281 | * case the contents of @a r_buf are undefined. |
1273 | */ | 1282 | */ |
1274 | int | 1283 | int |
1275 | GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, | 1284 | GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, |
1276 | uint16_t addrlen, | 1285 | uint16_t addrlen, |
1277 | struct sockaddr_in6 *r_buf) | 1286 | struct sockaddr_in6 *r_buf) |
1278 | { | 1287 | { |
1279 | char zbuf[addrlen + 1]; | 1288 | char zbuf[addrlen + 1]; |
1280 | int ret; | 1289 | int ret; |
@@ -1284,51 +1293,51 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, | |||
1284 | 1293 | ||
1285 | if (addrlen < 6) | 1294 | if (addrlen < 6) |
1286 | return GNUNET_SYSERR; | 1295 | return GNUNET_SYSERR; |
1287 | GNUNET_memcpy (zbuf, zt_addr, addrlen); | 1296 | GNUNET_memcpy(zbuf, zt_addr, addrlen); |
1288 | if ('[' != zbuf[0]) | 1297 | if ('[' != zbuf[0]) |
1289 | { | 1298 | { |
1290 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1299 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, |
1291 | _ ("IPv6 address did not start with `['\n")); | 1300 | _("IPv6 address did not start with `['\n")); |
1292 | return GNUNET_SYSERR; | 1301 | return GNUNET_SYSERR; |
1293 | } | 1302 | } |
1294 | zbuf[addrlen] = '\0'; | 1303 | zbuf[addrlen] = '\0'; |
1295 | port_colon = strrchr (zbuf, ':'); | 1304 | port_colon = strrchr(zbuf, ':'); |
1296 | if (NULL == port_colon) | 1305 | if (NULL == port_colon) |
1297 | { | 1306 | { |
1298 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1307 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, |
1299 | _ ("IPv6 address did contain ':' to separate port number\n")); | 1308 | _("IPv6 address did contain ':' to separate port number\n")); |
1300 | return GNUNET_SYSERR; | 1309 | return GNUNET_SYSERR; |
1301 | } | 1310 | } |
1302 | if (']' != *(port_colon - 1)) | 1311 | if (']' != *(port_colon - 1)) |
1303 | { | 1312 | { |
1304 | GNUNET_log ( | 1313 | GNUNET_log( |
1305 | GNUNET_ERROR_TYPE_WARNING, | 1314 | GNUNET_ERROR_TYPE_WARNING, |
1306 | _ ("IPv6 address did contain ']' before ':' to separate port number\n")); | 1315 | _("IPv6 address did contain ']' before ':' to separate port number\n")); |
1307 | return GNUNET_SYSERR; | 1316 | return GNUNET_SYSERR; |
1308 | } | 1317 | } |
1309 | ret = sscanf (port_colon, ":%u%1s", &port, dummy); | 1318 | ret = sscanf(port_colon, ":%u%1s", &port, dummy); |
1310 | if ((1 != ret) || (port > 65535)) | 1319 | if ((1 != ret) || (port > 65535)) |
1311 | { | 1320 | { |
1312 | GNUNET_log ( | 1321 | GNUNET_log( |
1313 | GNUNET_ERROR_TYPE_WARNING, | 1322 | GNUNET_ERROR_TYPE_WARNING, |
1314 | _ ("IPv6 address did contain a valid port number after the last ':'\n")); | 1323 | _("IPv6 address did contain a valid port number after the last ':'\n")); |
1315 | return GNUNET_SYSERR; | 1324 | return GNUNET_SYSERR; |
1316 | } | 1325 | } |
1317 | *(port_colon - 1) = '\0'; | 1326 | *(port_colon - 1) = '\0'; |
1318 | memset (r_buf, 0, sizeof (struct sockaddr_in6)); | 1327 | memset(r_buf, 0, sizeof(struct sockaddr_in6)); |
1319 | ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr); | 1328 | ret = inet_pton(AF_INET6, &zbuf[1], &r_buf->sin6_addr); |
1320 | if (ret <= 0) | 1329 | if (ret <= 0) |
1321 | { | 1330 | { |
1322 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1331 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, |
1323 | _ ("Invalid IPv6 address `%s': %s\n"), | 1332 | _("Invalid IPv6 address `%s': %s\n"), |
1324 | &zbuf[1], | 1333 | &zbuf[1], |
1325 | strerror (errno)); | 1334 | strerror(errno)); |
1326 | return GNUNET_SYSERR; | 1335 | return GNUNET_SYSERR; |
1327 | } | 1336 | } |
1328 | r_buf->sin6_port = htons (port); | 1337 | r_buf->sin6_port = htons(port); |
1329 | r_buf->sin6_family = AF_INET6; | 1338 | r_buf->sin6_family = AF_INET6; |
1330 | #if HAVE_SOCKADDR_IN_SIN_LEN | 1339 | #if HAVE_SOCKADDR_IN_SIN_LEN |
1331 | r_buf->sin6_len = (u_char) sizeof (struct sockaddr_in6); | 1340 | r_buf->sin6_len = (u_char)sizeof(struct sockaddr_in6); |
1332 | #endif | 1341 | #endif |
1333 | return GNUNET_OK; | 1342 | return GNUNET_OK; |
1334 | } | 1343 | } |
@@ -1346,9 +1355,9 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, | |||
1346 | * the contents of @a r_buf are undefined. | 1355 | * the contents of @a r_buf are undefined. |
1347 | */ | 1356 | */ |
1348 | int | 1357 | int |
1349 | GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | 1358 | GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, |
1350 | uint16_t addrlen, | 1359 | uint16_t addrlen, |
1351 | struct sockaddr_in *r_buf) | 1360 | struct sockaddr_in *r_buf) |
1352 | { | 1361 | { |
1353 | unsigned int temps[4]; | 1362 | unsigned int temps[4]; |
1354 | unsigned int port; | 1363 | unsigned int port; |
@@ -1357,14 +1366,14 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | |||
1357 | 1366 | ||
1358 | if (addrlen < 9) | 1367 | if (addrlen < 9) |
1359 | return GNUNET_SYSERR; | 1368 | return GNUNET_SYSERR; |
1360 | cnt = sscanf (zt_addr, | 1369 | cnt = sscanf(zt_addr, |
1361 | "%u.%u.%u.%u:%u%1s", | 1370 | "%u.%u.%u.%u:%u%1s", |
1362 | &temps[0], | 1371 | &temps[0], |
1363 | &temps[1], | 1372 | &temps[1], |
1364 | &temps[2], | 1373 | &temps[2], |
1365 | &temps[3], | 1374 | &temps[3], |
1366 | &port, | 1375 | &port, |
1367 | dummy); | 1376 | dummy); |
1368 | if (5 != cnt) | 1377 | if (5 != cnt) |
1369 | return GNUNET_SYSERR; | 1378 | return GNUNET_SYSERR; |
1370 | for (cnt = 0; cnt < 4; cnt++) | 1379 | for (cnt = 0; cnt < 4; cnt++) |
@@ -1373,11 +1382,11 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | |||
1373 | if (port > 65535) | 1382 | if (port > 65535) |
1374 | return GNUNET_SYSERR; | 1383 | return GNUNET_SYSERR; |
1375 | r_buf->sin_family = AF_INET; | 1384 | r_buf->sin_family = AF_INET; |
1376 | r_buf->sin_port = htons (port); | 1385 | r_buf->sin_port = htons(port); |
1377 | r_buf->sin_addr.s_addr = | 1386 | r_buf->sin_addr.s_addr = |
1378 | htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]); | 1387 | htonl((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]); |
1379 | #if HAVE_SOCKADDR_IN_SIN_LEN | 1388 | #if HAVE_SOCKADDR_IN_SIN_LEN |
1380 | r_buf->sin_len = (u_char) sizeof (struct sockaddr_in); | 1389 | r_buf->sin_len = (u_char)sizeof(struct sockaddr_in); |
1381 | #endif | 1390 | #endif |
1382 | return GNUNET_OK; | 1391 | return GNUNET_OK; |
1383 | } | 1392 | } |
@@ -1395,17 +1404,17 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | |||
1395 | * case the contents of @a r_buf are undefined. | 1404 | * case the contents of @a r_buf are undefined. |
1396 | */ | 1405 | */ |
1397 | int | 1406 | int |
1398 | GNUNET_STRINGS_to_address_ip (const char *addr, | 1407 | GNUNET_STRINGS_to_address_ip(const char *addr, |
1399 | uint16_t addrlen, | 1408 | uint16_t addrlen, |
1400 | struct sockaddr_storage *r_buf) | 1409 | struct sockaddr_storage *r_buf) |
1401 | { | 1410 | { |
1402 | if (addr[0] == '[') | 1411 | if (addr[0] == '[') |
1403 | return GNUNET_STRINGS_to_address_ipv6 (addr, | 1412 | return GNUNET_STRINGS_to_address_ipv6(addr, |
1404 | addrlen, | 1413 | addrlen, |
1405 | (struct sockaddr_in6 *) r_buf); | 1414 | (struct sockaddr_in6 *)r_buf); |
1406 | return GNUNET_STRINGS_to_address_ipv4 (addr, | 1415 | return GNUNET_STRINGS_to_address_ipv4(addr, |
1407 | addrlen, | 1416 | addrlen, |
1408 | (struct sockaddr_in *) r_buf); | 1417 | (struct sockaddr_in *)r_buf); |
1409 | } | 1418 | } |
1410 | 1419 | ||
1411 | 1420 | ||
@@ -1419,49 +1428,49 @@ GNUNET_STRINGS_to_address_ip (const char *addr, | |||
1419 | * @return 0 on error, otherwise number of bytes in @a sa | 1428 | * @return 0 on error, otherwise number of bytes in @a sa |
1420 | */ | 1429 | */ |
1421 | size_t | 1430 | size_t |
1422 | GNUNET_STRINGS_parse_socket_addr (const char *addr, | 1431 | GNUNET_STRINGS_parse_socket_addr(const char *addr, |
1423 | uint8_t *af, | 1432 | uint8_t *af, |
1424 | struct sockaddr **sa) | 1433 | struct sockaddr **sa) |
1425 | { | 1434 | { |
1426 | char *cp = GNUNET_strdup (addr); | 1435 | char *cp = GNUNET_strdup(addr); |
1427 | 1436 | ||
1428 | *af = AF_UNSPEC; | 1437 | *af = AF_UNSPEC; |
1429 | if ('[' == *addr) | 1438 | if ('[' == *addr) |
1430 | { | ||
1431 | /* IPv6 */ | ||
1432 | *sa = GNUNET_malloc (sizeof (struct sockaddr_in6)); | ||
1433 | if (GNUNET_OK != | ||
1434 | GNUNET_STRINGS_to_address_ipv6 (cp, | ||
1435 | strlen (cp), | ||
1436 | (struct sockaddr_in6 *) *sa)) | ||
1437 | { | 1439 | { |
1438 | GNUNET_free (*sa); | 1440 | /* IPv6 */ |
1439 | *sa = NULL; | 1441 | *sa = GNUNET_malloc(sizeof(struct sockaddr_in6)); |
1440 | GNUNET_free (cp); | 1442 | if (GNUNET_OK != |
1441 | return 0; | 1443 | GNUNET_STRINGS_to_address_ipv6(cp, |
1444 | strlen(cp), | ||
1445 | (struct sockaddr_in6 *)*sa)) | ||
1446 | { | ||
1447 | GNUNET_free(*sa); | ||
1448 | *sa = NULL; | ||
1449 | GNUNET_free(cp); | ||
1450 | return 0; | ||
1451 | } | ||
1452 | *af = AF_INET6; | ||
1453 | GNUNET_free(cp); | ||
1454 | return sizeof(struct sockaddr_in6); | ||
1442 | } | 1455 | } |
1443 | *af = AF_INET6; | ||
1444 | GNUNET_free (cp); | ||
1445 | return sizeof (struct sockaddr_in6); | ||
1446 | } | ||
1447 | else | 1456 | else |
1448 | { | ||
1449 | /* IPv4 */ | ||
1450 | *sa = GNUNET_malloc (sizeof (struct sockaddr_in)); | ||
1451 | if (GNUNET_OK != | ||
1452 | GNUNET_STRINGS_to_address_ipv4 (cp, | ||
1453 | strlen (cp), | ||
1454 | (struct sockaddr_in *) *sa)) | ||
1455 | { | 1457 | { |
1456 | GNUNET_free (*sa); | 1458 | /* IPv4 */ |
1457 | *sa = NULL; | 1459 | *sa = GNUNET_malloc(sizeof(struct sockaddr_in)); |
1458 | GNUNET_free (cp); | 1460 | if (GNUNET_OK != |
1459 | return 0; | 1461 | GNUNET_STRINGS_to_address_ipv4(cp, |
1462 | strlen(cp), | ||
1463 | (struct sockaddr_in *)*sa)) | ||
1464 | { | ||
1465 | GNUNET_free(*sa); | ||
1466 | *sa = NULL; | ||
1467 | GNUNET_free(cp); | ||
1468 | return 0; | ||
1469 | } | ||
1470 | *af = AF_INET; | ||
1471 | GNUNET_free(cp); | ||
1472 | return sizeof(struct sockaddr_in); | ||
1460 | } | 1473 | } |
1461 | *af = AF_INET; | ||
1462 | GNUNET_free (cp); | ||
1463 | return sizeof (struct sockaddr_in); | ||
1464 | } | ||
1465 | } | 1474 | } |
1466 | 1475 | ||
1467 | 1476 | ||
@@ -1470,24 +1479,25 @@ GNUNET_STRINGS_parse_socket_addr (const char *addr, | |||
1470 | * freed with a single call to GNUNET_free(); | 1479 | * freed with a single call to GNUNET_free(); |
1471 | */ | 1480 | */ |
1472 | static char *const * | 1481 | static char *const * |
1473 | _make_continuous_arg_copy (int argc, char *const *argv) | 1482 | _make_continuous_arg_copy(int argc, char *const *argv) |
1474 | { | 1483 | { |
1475 | size_t argvsize = 0; | 1484 | size_t argvsize = 0; |
1476 | int i; | 1485 | int i; |
1477 | char **new_argv; | 1486 | char **new_argv; |
1478 | char *p; | 1487 | char *p; |
1488 | |||
1479 | for (i = 0; i < argc; i++) | 1489 | for (i = 0; i < argc; i++) |
1480 | argvsize += strlen (argv[i]) + 1 + sizeof (char *); | 1490 | argvsize += strlen(argv[i]) + 1 + sizeof(char *); |
1481 | new_argv = GNUNET_malloc (argvsize + sizeof (char *)); | 1491 | new_argv = GNUNET_malloc(argvsize + sizeof(char *)); |
1482 | p = (char *) &new_argv[argc + 1]; | 1492 | p = (char *)&new_argv[argc + 1]; |
1483 | for (i = 0; i < argc; i++) | 1493 | for (i = 0; i < argc; i++) |
1484 | { | 1494 | { |
1485 | new_argv[i] = p; | 1495 | new_argv[i] = p; |
1486 | strcpy (p, argv[i]); | 1496 | strcpy(p, argv[i]); |
1487 | p += strlen (argv[i]) + 1; | 1497 | p += strlen(argv[i]) + 1; |
1488 | } | 1498 | } |
1489 | new_argv[argc] = NULL; | 1499 | new_argv[argc] = NULL; |
1490 | return (char *const *) new_argv; | 1500 | return (char *const *)new_argv; |
1491 | } | 1501 | } |
1492 | 1502 | ||
1493 | 1503 | ||
@@ -1506,10 +1516,10 @@ _make_continuous_arg_copy (int argc, char *const *argv) | |||
1506 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 1516 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure |
1507 | */ | 1517 | */ |
1508 | int | 1518 | int |
1509 | GNUNET_STRINGS_get_utf8_args (int argc, | 1519 | GNUNET_STRINGS_get_utf8_args(int argc, |
1510 | char *const *argv, | 1520 | char *const *argv, |
1511 | int *u8argc, | 1521 | int *u8argc, |
1512 | char *const **u8argv) | 1522 | char *const **u8argv) |
1513 | { | 1523 | { |
1514 | #if WINDOWS | 1524 | #if WINDOWS |
1515 | wchar_t *wcmd; | 1525 | wchar_t *wcmd; |
@@ -1518,42 +1528,42 @@ GNUNET_STRINGS_get_utf8_args (int argc, | |||
1518 | int i; | 1528 | int i; |
1519 | char **split_u8argv; | 1529 | char **split_u8argv; |
1520 | 1530 | ||
1521 | wcmd = GetCommandLineW (); | 1531 | wcmd = GetCommandLineW(); |
1522 | if (NULL == wcmd) | 1532 | if (NULL == wcmd) |
1523 | return GNUNET_SYSERR; | 1533 | return GNUNET_SYSERR; |
1524 | wargv = CommandLineToArgvW (wcmd, &wargc); | 1534 | wargv = CommandLineToArgvW(wcmd, &wargc); |
1525 | if (NULL == wargv) | 1535 | if (NULL == wargv) |
1526 | return GNUNET_SYSERR; | 1536 | return GNUNET_SYSERR; |
1527 | 1537 | ||
1528 | split_u8argv = GNUNET_malloc (argc * sizeof (char *)); | 1538 | split_u8argv = GNUNET_malloc(argc * sizeof(char *)); |
1529 | 1539 | ||
1530 | for (i = 0; i < wargc; i++) | 1540 | for (i = 0; i < wargc; i++) |
1531 | { | ||
1532 | size_t strl; | ||
1533 | /* Hopefully it will allocate us NUL-terminated strings... */ | ||
1534 | split_u8argv[i] = | ||
1535 | (char *) u16_to_u8 (wargv[i], wcslen (wargv[i]) + 1, NULL, &strl); | ||
1536 | if (NULL == split_u8argv[i]) | ||
1537 | { | 1541 | { |
1538 | int j; | 1542 | size_t strl; |
1539 | for (j = 0; j < i; j++) | 1543 | /* Hopefully it will allocate us NUL-terminated strings... */ |
1540 | free (split_u8argv[j]); | 1544 | split_u8argv[i] = |
1541 | GNUNET_free (split_u8argv); | 1545 | (char *)u16_to_u8(wargv[i], wcslen(wargv[i]) + 1, NULL, &strl); |
1542 | LocalFree (wargv); | 1546 | if (NULL == split_u8argv[i]) |
1543 | return GNUNET_SYSERR; | 1547 | { |
1548 | int j; | ||
1549 | for (j = 0; j < i; j++) | ||
1550 | free(split_u8argv[j]); | ||
1551 | GNUNET_free(split_u8argv); | ||
1552 | LocalFree(wargv); | ||
1553 | return GNUNET_SYSERR; | ||
1554 | } | ||
1544 | } | 1555 | } |
1545 | } | ||
1546 | 1556 | ||
1547 | *u8argv = _make_continuous_arg_copy (wargc, split_u8argv); | 1557 | *u8argv = _make_continuous_arg_copy(wargc, split_u8argv); |
1548 | *u8argc = wargc; | 1558 | *u8argc = wargc; |
1549 | 1559 | ||
1550 | for (i = 0; i < wargc; i++) | 1560 | for (i = 0; i < wargc; i++) |
1551 | free (split_u8argv[i]); | 1561 | free(split_u8argv[i]); |
1552 | free (split_u8argv); | 1562 | free(split_u8argv); |
1553 | return GNUNET_OK; | 1563 | return GNUNET_OK; |
1554 | #else | 1564 | #else |
1555 | char *const *new_argv = | 1565 | char *const *new_argv = |
1556 | (char *const *) _make_continuous_arg_copy (argc, argv); | 1566 | (char *const *)_make_continuous_arg_copy(argc, argv); |
1557 | *u8argv = new_argv; | 1567 | *u8argv = new_argv; |
1558 | *u8argc = argc; | 1568 | *u8argc = argc; |
1559 | return GNUNET_OK; | 1569 | return GNUNET_OK; |
@@ -1571,8 +1581,8 @@ GNUNET_STRINGS_get_utf8_args (int argc, | |||
1571 | * @a port_policy is malformed | 1581 | * @a port_policy is malformed |
1572 | */ | 1582 | */ |
1573 | static int | 1583 | static int |
1574 | parse_port_policy (const char *port_policy, | 1584 | parse_port_policy(const char *port_policy, |
1575 | struct GNUNET_STRINGS_PortPolicy *pp) | 1585 | struct GNUNET_STRINGS_PortPolicy *pp) |
1576 | { | 1586 | { |
1577 | const char *pos; | 1587 | const char *pos; |
1578 | int s; | 1588 | int s; |
@@ -1581,36 +1591,36 @@ parse_port_policy (const char *port_policy, | |||
1581 | 1591 | ||
1582 | pos = port_policy; | 1592 | pos = port_policy; |
1583 | if ('!' == *pos) | 1593 | if ('!' == *pos) |
1584 | { | ||
1585 | pp->negate_portrange = GNUNET_YES; | ||
1586 | pos++; | ||
1587 | } | ||
1588 | if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol)) | ||
1589 | { | ||
1590 | if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF)) | ||
1591 | { | 1594 | { |
1592 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n")); | 1595 | pp->negate_portrange = GNUNET_YES; |
1593 | return GNUNET_SYSERR; | 1596 | pos++; |
1594 | } | 1597 | } |
1595 | pp->start_port = (uint16_t) s; | 1598 | if (2 == sscanf(pos, "%u-%u%1s", &s, &e, eol)) |
1596 | pp->end_port = (uint16_t) e; | ||
1597 | return GNUNET_OK; | ||
1598 | } | ||
1599 | if (1 == sscanf (pos, "%u%1s", &s, eol)) | ||
1600 | { | ||
1601 | if ((0 == s) || (s > 0xFFFF)) | ||
1602 | { | 1599 | { |
1603 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n")); | 1600 | if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF)) |
1604 | return GNUNET_SYSERR; | 1601 | { |
1602 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("Port not in range\n")); | ||
1603 | return GNUNET_SYSERR; | ||
1604 | } | ||
1605 | pp->start_port = (uint16_t)s; | ||
1606 | pp->end_port = (uint16_t)e; | ||
1607 | return GNUNET_OK; | ||
1605 | } | 1608 | } |
1609 | if (1 == sscanf(pos, "%u%1s", &s, eol)) | ||
1610 | { | ||
1611 | if ((0 == s) || (s > 0xFFFF)) | ||
1612 | { | ||
1613 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, _("Port not in range\n")); | ||
1614 | return GNUNET_SYSERR; | ||
1615 | } | ||
1606 | 1616 | ||
1607 | pp->start_port = (uint16_t) s; | 1617 | pp->start_port = (uint16_t)s; |
1608 | pp->end_port = (uint16_t) s; | 1618 | pp->end_port = (uint16_t)s; |
1609 | return GNUNET_OK; | 1619 | return GNUNET_OK; |
1610 | } | 1620 | } |
1611 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1621 | GNUNET_log(GNUNET_ERROR_TYPE_WARNING, |
1612 | _ ("Malformed port policy `%s'\n"), | 1622 | _("Malformed port policy `%s'\n"), |
1613 | port_policy); | 1623 | port_policy); |
1614 | return GNUNET_SYSERR; | 1624 | return GNUNET_SYSERR; |
1615 | } | 1625 | } |
1616 | 1626 | ||
@@ -1628,7 +1638,7 @@ parse_port_policy (const char *port_policy, | |||
1628 | * NULL if the synatx is flawed | 1638 | * NULL if the synatx is flawed |
1629 | */ | 1639 | */ |
1630 | struct GNUNET_STRINGS_IPv4NetworkPolicy * | 1640 | struct GNUNET_STRINGS_IPv4NetworkPolicy * |
1631 | GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | 1641 | GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX) |
1632 | { | 1642 | { |
1633 | unsigned int count; | 1643 | unsigned int count; |
1634 | unsigned int i; | 1644 | unsigned int i; |
@@ -1646,164 +1656,164 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | |||
1646 | 1656 | ||
1647 | if (NULL == routeListX) | 1657 | if (NULL == routeListX) |
1648 | return NULL; | 1658 | return NULL; |
1649 | len = strlen (routeListX); | 1659 | len = strlen(routeListX); |
1650 | if (0 == len) | 1660 | if (0 == len) |
1651 | return NULL; | 1661 | return NULL; |
1652 | routeList = GNUNET_strdup (routeListX); | 1662 | routeList = GNUNET_strdup(routeListX); |
1653 | count = 0; | 1663 | count = 0; |
1654 | for (i = 0; i < len; i++) | 1664 | for (i = 0; i < len; i++) |
1655 | if (routeList[i] == ';') | 1665 | if (routeList[i] == ';') |
1656 | count++; | 1666 | count++; |
1657 | result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv4NetworkPolicy) * | 1667 | result = GNUNET_malloc(sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy) * |
1658 | (count + 1)); | 1668 | (count + 1)); |
1659 | i = 0; | 1669 | i = 0; |
1660 | pos = 0; | 1670 | pos = 0; |
1661 | while (i < count) | 1671 | while (i < count) |
1662 | { | ||
1663 | for (colon = pos; ':' != routeList[colon]; colon++) | ||
1664 | if ((';' == routeList[colon]) || ('\0' == routeList[colon])) | ||
1665 | break; | ||
1666 | for (end = colon; ';' != routeList[end]; end++) | ||
1667 | if ('\0' == routeList[end]) | ||
1668 | break; | ||
1669 | if ('\0' == routeList[end]) | ||
1670 | break; | ||
1671 | routeList[end] = '\0'; | ||
1672 | if (':' == routeList[colon]) | ||
1673 | { | 1672 | { |
1674 | routeList[colon] = '\0'; | 1673 | for (colon = pos; ':' != routeList[colon]; colon++) |
1675 | if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp)) | 1674 | if ((';' == routeList[colon]) || ('\0' == routeList[colon])) |
1675 | break; | ||
1676 | for (end = colon; ';' != routeList[end]; end++) | ||
1677 | if ('\0' == routeList[end]) | ||
1678 | break; | ||
1679 | if ('\0' == routeList[end]) | ||
1676 | break; | 1680 | break; |
1677 | } | 1681 | routeList[end] = '\0'; |
1678 | cnt = sscanf (&routeList[pos], | 1682 | if (':' == routeList[colon]) |
1679 | "%u.%u.%u.%u/%u.%u.%u.%u%1s", | ||
1680 | &temps[0], | ||
1681 | &temps[1], | ||
1682 | &temps[2], | ||
1683 | &temps[3], | ||
1684 | &temps[4], | ||
1685 | &temps[5], | ||
1686 | &temps[6], | ||
1687 | &temps[7], | ||
1688 | dummy); | ||
1689 | if (8 == cnt) | ||
1690 | { | ||
1691 | for (j = 0; j < 8; j++) | ||
1692 | if (temps[j] > 0xFF) | ||
1693 | { | 1683 | { |
1694 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1684 | routeList[colon] = '\0'; |
1695 | _ ("Invalid format for IP: `%s'\n"), | 1685 | if (GNUNET_OK != parse_port_policy(&routeList[colon + 1], &result[i].pp)) |
1696 | &routeList[pos]); | 1686 | break; |
1697 | GNUNET_free (result); | ||
1698 | GNUNET_free (routeList); | ||
1699 | return NULL; | ||
1700 | } | 1687 | } |
1701 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + | 1688 | cnt = sscanf(&routeList[pos], |
1702 | (temps[2] << 8) + temps[3]); | 1689 | "%u.%u.%u.%u/%u.%u.%u.%u%1s", |
1703 | result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16) + | 1690 | &temps[0], |
1704 | (temps[6] << 8) + temps[7]); | 1691 | &temps[1], |
1705 | pos = end + 1; | 1692 | &temps[2], |
1706 | i++; | 1693 | &temps[3], |
1707 | continue; | 1694 | &temps[4], |
1708 | } | 1695 | &temps[5], |
1709 | /* try second notation */ | 1696 | &temps[6], |
1710 | cnt = sscanf (&routeList[pos], | 1697 | &temps[7], |
1711 | "%u.%u.%u.%u/%u%1s", | 1698 | dummy); |
1712 | &temps[0], | 1699 | if (8 == cnt) |
1713 | &temps[1], | ||
1714 | &temps[2], | ||
1715 | &temps[3], | ||
1716 | &slash, | ||
1717 | dummy); | ||
1718 | if (5 == cnt) | ||
1719 | { | ||
1720 | for (j = 0; j < 4; j++) | ||
1721 | if (temps[j] > 0xFF) | ||
1722 | { | 1700 | { |
1723 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1701 | for (j = 0; j < 8; j++) |
1724 | _ ("Invalid format for IP: `%s'\n"), | 1702 | if (temps[j] > 0xFF) |
1725 | &routeList[pos]); | 1703 | { |
1726 | GNUNET_free (result); | 1704 | LOG(GNUNET_ERROR_TYPE_WARNING, |
1727 | GNUNET_free (routeList); | 1705 | _("Invalid format for IP: `%s'\n"), |
1728 | return NULL; | 1706 | &routeList[pos]); |
1707 | GNUNET_free(result); | ||
1708 | GNUNET_free(routeList); | ||
1709 | return NULL; | ||
1710 | } | ||
1711 | result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) + | ||
1712 | (temps[2] << 8) + temps[3]); | ||
1713 | result[i].netmask.s_addr = htonl((temps[4] << 24) + (temps[5] << 16) + | ||
1714 | (temps[6] << 8) + temps[7]); | ||
1715 | pos = end + 1; | ||
1716 | i++; | ||
1717 | continue; | ||
1729 | } | 1718 | } |
1730 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + | 1719 | /* try second notation */ |
1731 | (temps[2] << 8) + temps[3]); | 1720 | cnt = sscanf(&routeList[pos], |
1732 | if ((slash <= 32) && (slash >= 0)) | 1721 | "%u.%u.%u.%u/%u%1s", |
1733 | { | 1722 | &temps[0], |
1734 | result[i].netmask.s_addr = 0; | 1723 | &temps[1], |
1735 | while (slash > 0) | 1724 | &temps[2], |
1725 | &temps[3], | ||
1726 | &slash, | ||
1727 | dummy); | ||
1728 | if (5 == cnt) | ||
1736 | { | 1729 | { |
1737 | result[i].netmask.s_addr = | 1730 | for (j = 0; j < 4; j++) |
1738 | (result[i].netmask.s_addr >> 1) + 0x80000000; | 1731 | if (temps[j] > 0xFF) |
1739 | slash--; | 1732 | { |
1733 | LOG(GNUNET_ERROR_TYPE_WARNING, | ||
1734 | _("Invalid format for IP: `%s'\n"), | ||
1735 | &routeList[pos]); | ||
1736 | GNUNET_free(result); | ||
1737 | GNUNET_free(routeList); | ||
1738 | return NULL; | ||
1739 | } | ||
1740 | result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) + | ||
1741 | (temps[2] << 8) + temps[3]); | ||
1742 | if ((slash <= 32) && (slash >= 0)) | ||
1743 | { | ||
1744 | result[i].netmask.s_addr = 0; | ||
1745 | while (slash > 0) | ||
1746 | { | ||
1747 | result[i].netmask.s_addr = | ||
1748 | (result[i].netmask.s_addr >> 1) + 0x80000000; | ||
1749 | slash--; | ||
1750 | } | ||
1751 | result[i].netmask.s_addr = htonl(result[i].netmask.s_addr); | ||
1752 | pos = end + 1; | ||
1753 | i++; | ||
1754 | continue; | ||
1755 | } | ||
1756 | else | ||
1757 | { | ||
1758 | LOG(GNUNET_ERROR_TYPE_WARNING, | ||
1759 | _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), | ||
1760 | slash); | ||
1761 | GNUNET_free(result); | ||
1762 | GNUNET_free(routeList); | ||
1763 | return NULL; /* error */ | ||
1764 | } | ||
1740 | } | 1765 | } |
1741 | result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); | 1766 | /* try third notation */ |
1742 | pos = end + 1; | 1767 | slash = 32; |
1743 | i++; | 1768 | cnt = sscanf(&routeList[pos], |
1744 | continue; | 1769 | "%u.%u.%u.%u%1s", |
1745 | } | 1770 | &temps[0], |
1746 | else | 1771 | &temps[1], |
1747 | { | 1772 | &temps[2], |
1748 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1773 | &temps[3], |
1749 | _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), | 1774 | dummy); |
1750 | slash); | 1775 | if (4 == cnt) |
1751 | GNUNET_free (result); | ||
1752 | GNUNET_free (routeList); | ||
1753 | return NULL; /* error */ | ||
1754 | } | ||
1755 | } | ||
1756 | /* try third notation */ | ||
1757 | slash = 32; | ||
1758 | cnt = sscanf (&routeList[pos], | ||
1759 | "%u.%u.%u.%u%1s", | ||
1760 | &temps[0], | ||
1761 | &temps[1], | ||
1762 | &temps[2], | ||
1763 | &temps[3], | ||
1764 | dummy); | ||
1765 | if (4 == cnt) | ||
1766 | { | ||
1767 | for (j = 0; j < 4; j++) | ||
1768 | if (temps[j] > 0xFF) | ||
1769 | { | 1776 | { |
1770 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1777 | for (j = 0; j < 4; j++) |
1771 | _ ("Invalid format for IP: `%s'\n"), | 1778 | if (temps[j] > 0xFF) |
1772 | &routeList[pos]); | 1779 | { |
1773 | GNUNET_free (result); | 1780 | LOG(GNUNET_ERROR_TYPE_WARNING, |
1774 | GNUNET_free (routeList); | 1781 | _("Invalid format for IP: `%s'\n"), |
1775 | return NULL; | 1782 | &routeList[pos]); |
1783 | GNUNET_free(result); | ||
1784 | GNUNET_free(routeList); | ||
1785 | return NULL; | ||
1786 | } | ||
1787 | result[i].network.s_addr = htonl((temps[0] << 24) + (temps[1] << 16) + | ||
1788 | (temps[2] << 8) + temps[3]); | ||
1789 | result[i].netmask.s_addr = 0; | ||
1790 | while (slash > 0) | ||
1791 | { | ||
1792 | result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000; | ||
1793 | slash--; | ||
1794 | } | ||
1795 | result[i].netmask.s_addr = htonl(result[i].netmask.s_addr); | ||
1796 | pos = end + 1; | ||
1797 | i++; | ||
1798 | continue; | ||
1776 | } | 1799 | } |
1777 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + | 1800 | LOG(GNUNET_ERROR_TYPE_WARNING, |
1778 | (temps[2] << 8) + temps[3]); | 1801 | _("Invalid format for IP: `%s'\n"), |
1779 | result[i].netmask.s_addr = 0; | 1802 | &routeList[pos]); |
1780 | while (slash > 0) | 1803 | GNUNET_free(result); |
1781 | { | 1804 | GNUNET_free(routeList); |
1782 | result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000; | 1805 | return NULL; /* error */ |
1783 | slash--; | ||
1784 | } | ||
1785 | result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); | ||
1786 | pos = end + 1; | ||
1787 | i++; | ||
1788 | continue; | ||
1789 | } | 1806 | } |
1790 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1807 | if (pos < strlen(routeList)) |
1791 | _ ("Invalid format for IP: `%s'\n"), | 1808 | { |
1792 | &routeList[pos]); | 1809 | LOG(GNUNET_ERROR_TYPE_WARNING, |
1793 | GNUNET_free (result); | 1810 | _("Invalid format: `%s'\n"), |
1794 | GNUNET_free (routeList); | 1811 | &routeListX[pos]); |
1795 | return NULL; /* error */ | 1812 | GNUNET_free(result); |
1796 | } | 1813 | GNUNET_free(routeList); |
1797 | if (pos < strlen (routeList)) | 1814 | return NULL; /* oops */ |
1798 | { | 1815 | } |
1799 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1816 | GNUNET_free(routeList); |
1800 | _ ("Invalid format: `%s'\n"), | ||
1801 | &routeListX[pos]); | ||
1802 | GNUNET_free (result); | ||
1803 | GNUNET_free (routeList); | ||
1804 | return NULL; /* oops */ | ||
1805 | } | ||
1806 | GNUNET_free (routeList); | ||
1807 | return result; /* ok */ | 1817 | return result; /* ok */ |
1808 | } | 1818 | } |
1809 | 1819 | ||
@@ -1820,7 +1830,7 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | |||
1820 | * @return the converted list, 0-terminated, NULL if the synatx is flawed | 1830 | * @return the converted list, 0-terminated, NULL if the synatx is flawed |
1821 | */ | 1831 | */ |
1822 | struct GNUNET_STRINGS_IPv6NetworkPolicy * | 1832 | struct GNUNET_STRINGS_IPv6NetworkPolicy * |
1823 | GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | 1833 | GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX) |
1824 | { | 1834 | { |
1825 | unsigned int count; | 1835 | unsigned int count; |
1826 | unsigned int i; | 1836 | unsigned int i; |
@@ -1839,110 +1849,110 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1839 | 1849 | ||
1840 | if (NULL == routeListX) | 1850 | if (NULL == routeListX) |
1841 | return NULL; | 1851 | return NULL; |
1842 | len = strlen (routeListX); | 1852 | len = strlen(routeListX); |
1843 | if (0 == len) | 1853 | if (0 == len) |
1844 | return NULL; | 1854 | return NULL; |
1845 | routeList = GNUNET_strdup (routeListX); | 1855 | routeList = GNUNET_strdup(routeListX); |
1846 | count = 0; | 1856 | count = 0; |
1847 | for (i = 0; i < len; i++) | 1857 | for (i = 0; i < len; i++) |
1848 | if (';' == routeList[i]) | 1858 | if (';' == routeList[i]) |
1849 | count++; | 1859 | count++; |
1850 | if (';' != routeList[len - 1]) | 1860 | if (';' != routeList[len - 1]) |
1851 | { | 1861 | { |
1852 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1862 | LOG(GNUNET_ERROR_TYPE_WARNING, |
1853 | _ ("Invalid network notation (does not end with ';': `%s')\n"), | 1863 | _("Invalid network notation (does not end with ';': `%s')\n"), |
1854 | routeList); | 1864 | routeList); |
1855 | GNUNET_free (routeList); | 1865 | GNUNET_free(routeList); |
1856 | return NULL; | 1866 | return NULL; |
1857 | } | 1867 | } |
1858 | 1868 | ||
1859 | result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv6NetworkPolicy) * | 1869 | result = GNUNET_malloc(sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy) * |
1860 | (count + 1)); | 1870 | (count + 1)); |
1861 | i = 0; | 1871 | i = 0; |
1862 | pos = 0; | 1872 | pos = 0; |
1863 | while (i < count) | 1873 | while (i < count) |
1864 | { | ||
1865 | start = pos; | ||
1866 | while (';' != routeList[pos]) | ||
1867 | pos++; | ||
1868 | slash = pos; | ||
1869 | while ((slash >= start) && (routeList[slash] != '/')) | ||
1870 | slash--; | ||
1871 | |||
1872 | if (slash < start) | ||
1873 | { | 1874 | { |
1874 | memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); | 1875 | start = pos; |
1876 | while (';' != routeList[pos]) | ||
1877 | pos++; | ||
1875 | slash = pos; | 1878 | slash = pos; |
1876 | } | 1879 | while ((slash >= start) && (routeList[slash] != '/')) |
1877 | else | 1880 | slash--; |
1878 | { | 1881 | |
1879 | routeList[pos] = '\0'; | 1882 | if (slash < start) |
1880 | for (colon = pos; ':' != routeList[colon]; colon--) | ||
1881 | if ('/' == routeList[colon]) | ||
1882 | break; | ||
1883 | if (':' == routeList[colon]) | ||
1884 | { | ||
1885 | routeList[colon] = '\0'; | ||
1886 | if (GNUNET_OK != | ||
1887 | parse_port_policy (&routeList[colon + 1], &result[i].pp)) | ||
1888 | { | 1883 | { |
1889 | GNUNET_free (result); | 1884 | memset(&result[i].netmask, 0xFF, sizeof(struct in6_addr)); |
1890 | GNUNET_free (routeList); | 1885 | slash = pos; |
1891 | return NULL; | ||
1892 | } | 1886 | } |
1893 | } | 1887 | else |
1894 | ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask); | 1888 | { |
1889 | routeList[pos] = '\0'; | ||
1890 | for (colon = pos; ':' != routeList[colon]; colon--) | ||
1891 | if ('/' == routeList[colon]) | ||
1892 | break; | ||
1893 | if (':' == routeList[colon]) | ||
1894 | { | ||
1895 | routeList[colon] = '\0'; | ||
1896 | if (GNUNET_OK != | ||
1897 | parse_port_policy(&routeList[colon + 1], &result[i].pp)) | ||
1898 | { | ||
1899 | GNUNET_free(result); | ||
1900 | GNUNET_free(routeList); | ||
1901 | return NULL; | ||
1902 | } | ||
1903 | } | ||
1904 | ret = inet_pton(AF_INET6, &routeList[slash + 1], &result[i].netmask); | ||
1905 | if (ret <= 0) | ||
1906 | { | ||
1907 | save = errno; | ||
1908 | if ((1 != sscanf(&routeList[slash + 1], "%u%1s", &bits, dummy)) || | ||
1909 | (bits > 128)) | ||
1910 | { | ||
1911 | if (0 == ret) | ||
1912 | LOG(GNUNET_ERROR_TYPE_WARNING, | ||
1913 | _("Wrong format `%s' for netmask\n"), | ||
1914 | &routeList[slash + 1]); | ||
1915 | else | ||
1916 | { | ||
1917 | errno = save; | ||
1918 | LOG_STRERROR(GNUNET_ERROR_TYPE_WARNING, "inet_pton"); | ||
1919 | } | ||
1920 | GNUNET_free(result); | ||
1921 | GNUNET_free(routeList); | ||
1922 | return NULL; | ||
1923 | } | ||
1924 | off = 0; | ||
1925 | while (bits > 8) | ||
1926 | { | ||
1927 | result[i].netmask.s6_addr[off++] = 0xFF; | ||
1928 | bits -= 8; | ||
1929 | } | ||
1930 | while (bits > 0) | ||
1931 | { | ||
1932 | result[i].netmask.s6_addr[off] = | ||
1933 | (result[i].netmask.s6_addr[off] >> 1) + 0x80; | ||
1934 | bits--; | ||
1935 | } | ||
1936 | } | ||
1937 | } | ||
1938 | routeList[slash] = '\0'; | ||
1939 | ret = inet_pton(AF_INET6, &routeList[start], &result[i].network); | ||
1895 | if (ret <= 0) | 1940 | if (ret <= 0) |
1896 | { | ||
1897 | save = errno; | ||
1898 | if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) || | ||
1899 | (bits > 128)) | ||
1900 | { | 1941 | { |
1901 | if (0 == ret) | 1942 | if (0 == ret) |
1902 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1943 | LOG(GNUNET_ERROR_TYPE_WARNING, |
1903 | _ ("Wrong format `%s' for netmask\n"), | 1944 | _("Wrong format `%s' for network\n"), |
1904 | &routeList[slash + 1]); | 1945 | &routeList[slash + 1]); |
1905 | else | 1946 | else |
1906 | { | 1947 | LOG_STRERROR(GNUNET_ERROR_TYPE_ERROR, "inet_pton"); |
1907 | errno = save; | 1948 | GNUNET_free(result); |
1908 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton"); | 1949 | GNUNET_free(routeList); |
1909 | } | ||
1910 | GNUNET_free (result); | ||
1911 | GNUNET_free (routeList); | ||
1912 | return NULL; | 1950 | return NULL; |
1913 | } | 1951 | } |
1914 | off = 0; | 1952 | pos++; |
1915 | while (bits > 8) | 1953 | i++; |
1916 | { | ||
1917 | result[i].netmask.s6_addr[off++] = 0xFF; | ||
1918 | bits -= 8; | ||
1919 | } | ||
1920 | while (bits > 0) | ||
1921 | { | ||
1922 | result[i].netmask.s6_addr[off] = | ||
1923 | (result[i].netmask.s6_addr[off] >> 1) + 0x80; | ||
1924 | bits--; | ||
1925 | } | ||
1926 | } | ||
1927 | } | ||
1928 | routeList[slash] = '\0'; | ||
1929 | ret = inet_pton (AF_INET6, &routeList[start], &result[i].network); | ||
1930 | if (ret <= 0) | ||
1931 | { | ||
1932 | if (0 == ret) | ||
1933 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
1934 | _ ("Wrong format `%s' for network\n"), | ||
1935 | &routeList[slash + 1]); | ||
1936 | else | ||
1937 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); | ||
1938 | GNUNET_free (result); | ||
1939 | GNUNET_free (routeList); | ||
1940 | return NULL; | ||
1941 | } | 1954 | } |
1942 | pos++; | 1955 | GNUNET_free(routeList); |
1943 | i++; | ||
1944 | } | ||
1945 | GNUNET_free (routeList); | ||
1946 | return result; | 1956 | return result; |
1947 | } | 1957 | } |
1948 | 1958 | ||
@@ -1965,58 +1975,58 @@ static char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |||
1965 | * @return the size of the output | 1975 | * @return the size of the output |
1966 | */ | 1976 | */ |
1967 | size_t | 1977 | size_t |
1968 | GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output) | 1978 | GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output) |
1969 | { | 1979 | { |
1970 | const char *data = in; | 1980 | const char *data = in; |
1971 | size_t ret; | 1981 | size_t ret; |
1972 | char *opt; | 1982 | char *opt; |
1973 | 1983 | ||
1974 | ret = 0; | 1984 | ret = 0; |
1975 | opt = GNUNET_malloc (2 + (len * 4 / 3) + 8); | 1985 | opt = GNUNET_malloc(2 + (len * 4 / 3) + 8); |
1976 | for (size_t i = 0; i < len; ++i) | 1986 | for (size_t i = 0; i < len; ++i) |
1977 | { | ||
1978 | char c; | ||
1979 | |||
1980 | c = (data[i] >> 2) & 0x3f; | ||
1981 | opt[ret++] = cvt[(int) c]; | ||
1982 | c = (data[i] << 4) & 0x3f; | ||
1983 | if (++i < len) | ||
1984 | c |= (data[i] >> 4) & 0x0f; | ||
1985 | opt[ret++] = cvt[(int) c]; | ||
1986 | if (i < len) | ||
1987 | { | 1987 | { |
1988 | c = (data[i] << 2) & 0x3f; | 1988 | char c; |
1989 | |||
1990 | c = (data[i] >> 2) & 0x3f; | ||
1991 | opt[ret++] = cvt[(int)c]; | ||
1992 | c = (data[i] << 4) & 0x3f; | ||
1989 | if (++i < len) | 1993 | if (++i < len) |
1990 | c |= (data[i] >> 6) & 0x03; | 1994 | c |= (data[i] >> 4) & 0x0f; |
1991 | opt[ret++] = cvt[(int) c]; | 1995 | opt[ret++] = cvt[(int)c]; |
1992 | } | 1996 | if (i < len) |
1993 | else | 1997 | { |
1994 | { | 1998 | c = (data[i] << 2) & 0x3f; |
1995 | ++i; | 1999 | if (++i < len) |
1996 | opt[ret++] = FILLCHAR; | 2000 | c |= (data[i] >> 6) & 0x03; |
1997 | } | 2001 | opt[ret++] = cvt[(int)c]; |
1998 | if (i < len) | 2002 | } |
1999 | { | 2003 | else |
2000 | c = data[i] & 0x3f; | 2004 | { |
2001 | opt[ret++] = cvt[(int) c]; | 2005 | ++i; |
2002 | } | 2006 | opt[ret++] = FILLCHAR; |
2003 | else | 2007 | } |
2004 | { | 2008 | if (i < len) |
2005 | opt[ret++] = FILLCHAR; | 2009 | { |
2010 | c = data[i] & 0x3f; | ||
2011 | opt[ret++] = cvt[(int)c]; | ||
2012 | } | ||
2013 | else | ||
2014 | { | ||
2015 | opt[ret++] = FILLCHAR; | ||
2016 | } | ||
2006 | } | 2017 | } |
2007 | } | ||
2008 | *output = opt; | 2018 | *output = opt; |
2009 | return ret; | 2019 | return ret; |
2010 | } | 2020 | } |
2011 | 2021 | ||
2012 | #define cvtfind(a) \ | 2022 | #define cvtfind(a) \ |
2013 | ((((a) >= 'A') && ((a) <= 'Z')) \ | 2023 | ((((a) >= 'A') && ((a) <= 'Z')) \ |
2014 | ? (a) - 'A' \ | 2024 | ? (a) - 'A' \ |
2015 | : (((a) >= 'a') && ((a) <= 'z')) \ | 2025 | : (((a) >= 'a') && ((a) <= 'z')) \ |
2016 | ? (a) - 'a' + 26 \ | 2026 | ? (a) - 'a' + 26 \ |
2017 | : (((a) >= '0') && ((a) <= '9')) \ | 2027 | : (((a) >= '0') && ((a) <= '9')) \ |
2018 | ? (a) - '0' + 52 \ | 2028 | ? (a) - '0' + 52 \ |
2019 | : ((a) == '+') ? 62 : ((a) == '/') ? 63 : -1) | 2029 | : ((a) == '+') ? 62 : ((a) == '/') ? 63 : -1) |
2020 | 2030 | ||
2021 | 2031 | ||
2022 | /** | 2032 | /** |
@@ -2029,61 +2039,61 @@ GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output) | |||
2029 | * @return the size of the output | 2039 | * @return the size of the output |
2030 | */ | 2040 | */ |
2031 | size_t | 2041 | size_t |
2032 | GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **out) | 2042 | GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out) |
2033 | { | 2043 | { |
2034 | char *output; | 2044 | char *output; |
2035 | size_t ret = 0; | 2045 | size_t ret = 0; |
2036 | 2046 | ||
2037 | #define CHECK_CRLF \ | 2047 | #define CHECK_CRLF \ |
2038 | while (data[i] == '\r' || data[i] == '\n') \ | 2048 | while (data[i] == '\r' || data[i] == '\n') \ |
2039 | { \ | 2049 | { \ |
2040 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \ | 2050 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \ |
2041 | "ignoring CR/LF\n"); \ | 2051 | "ignoring CR/LF\n"); \ |
2042 | i++; \ | 2052 | i++; \ |
2043 | if (i >= len) \ | 2053 | if (i >= len) \ |
2044 | goto END; \ | 2054 | goto END; \ |
2045 | } | 2055 | } |
2046 | 2056 | ||
2047 | output = GNUNET_malloc ((len * 3 / 4) + 8); | 2057 | output = GNUNET_malloc((len * 3 / 4) + 8); |
2048 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 2058 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, |
2049 | "base64_decode decoding len=%d\n", | 2059 | "base64_decode decoding len=%d\n", |
2050 | (int) len); | 2060 | (int)len); |
2051 | for (size_t i = 0; i < len; ++i) | 2061 | for (size_t i = 0; i < len; ++i) |
2052 | { | ||
2053 | char c; | ||
2054 | char c1; | ||
2055 | |||
2056 | CHECK_CRLF; | ||
2057 | if (FILLCHAR == data[i]) | ||
2058 | break; | ||
2059 | c = (char) cvtfind (data[i]); | ||
2060 | ++i; | ||
2061 | CHECK_CRLF; | ||
2062 | c1 = (char) cvtfind (data[i]); | ||
2063 | c = (c << 2) | ((c1 >> 4) & 0x3); | ||
2064 | output[ret++] = c; | ||
2065 | if (++i < len) | ||
2066 | { | 2062 | { |
2063 | char c; | ||
2064 | char c1; | ||
2065 | |||
2067 | CHECK_CRLF; | 2066 | CHECK_CRLF; |
2068 | c = data[i]; | 2067 | if (FILLCHAR == data[i]) |
2069 | if (FILLCHAR == c) | ||
2070 | break; | 2068 | break; |
2071 | c = (char) cvtfind (c); | 2069 | c = (char)cvtfind(data[i]); |
2072 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); | 2070 | ++i; |
2073 | output[ret++] = c1; | ||
2074 | } | ||
2075 | if (++i < len) | ||
2076 | { | ||
2077 | CHECK_CRLF; | 2071 | CHECK_CRLF; |
2078 | c1 = data[i]; | 2072 | c1 = (char)cvtfind(data[i]); |
2079 | if (FILLCHAR == c1) | 2073 | c = (c << 2) | ((c1 >> 4) & 0x3); |
2080 | break; | ||
2081 | |||
2082 | c1 = (char) cvtfind (c1); | ||
2083 | c = ((c << 6) & 0xc0) | c1; | ||
2084 | output[ret++] = c; | 2074 | output[ret++] = c; |
2075 | if (++i < len) | ||
2076 | { | ||
2077 | CHECK_CRLF; | ||
2078 | c = data[i]; | ||
2079 | if (FILLCHAR == c) | ||
2080 | break; | ||
2081 | c = (char)cvtfind(c); | ||
2082 | c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); | ||
2083 | output[ret++] = c1; | ||
2084 | } | ||
2085 | if (++i < len) | ||
2086 | { | ||
2087 | CHECK_CRLF; | ||
2088 | c1 = data[i]; | ||
2089 | if (FILLCHAR == c1) | ||
2090 | break; | ||
2091 | |||
2092 | c1 = (char)cvtfind(c1); | ||
2093 | c = ((c << 6) & 0xc0) | c1; | ||
2094 | output[ret++] = c; | ||
2095 | } | ||
2085 | } | 2096 | } |
2086 | } | ||
2087 | END: | 2097 | END: |
2088 | *out = output; | 2098 | *out = output; |
2089 | return ret; | 2099 | return ret; |