diff options
Diffstat (limited to 'src/util/strings.c')
-rw-r--r-- | src/util/strings.c | 693 |
1 files changed, 330 insertions, 363 deletions
diff --git a/src/util/strings.c b/src/util/strings.c index 8cd591288..ef9fdd693 100644 --- a/src/util/strings.c +++ b/src/util/strings.c | |||
@@ -34,9 +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) GNUNET_log_from_strerror (kind, "util-strings", syscall) | 39 | #define LOG_STRERROR(kind, syscall) \ |
40 | GNUNET_log_from_strerror (kind, "util-strings", syscall) | ||
40 | 41 | ||
41 | 42 | ||
42 | /** | 43 | /** |
@@ -105,15 +106,13 @@ GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, | |||
105 | 106 | ||
106 | off = 0; | 107 | off = 0; |
107 | buf = GNUNET_malloc (plen); | 108 | buf = GNUNET_malloc (plen); |
108 | for (unsigned int i = 0; | 109 | for (unsigned int i = 0; i < num_pids; i++) |
109 | i < num_pids; | ||
110 | i++) | ||
111 | { | 110 | { |
112 | off += GNUNET_snprintf (&buf[off], | 111 | off += GNUNET_snprintf (&buf[off], |
113 | plen - off, | 112 | plen - off, |
114 | "%s%s", | 113 | "%s%s", |
115 | GNUNET_i2s (&pids[i]), | 114 | GNUNET_i2s (&pids[i]), |
116 | (i == num_pids -1) ? "" : "-"); | 115 | (i == num_pids - 1) ? "" : "-"); |
117 | } | 116 | } |
118 | return buf; | 117 | return buf; |
119 | } | 118 | } |
@@ -133,8 +132,10 @@ GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, | |||
133 | * in the buffer, or 0 on error. | 132 | * in the buffer, or 0 on error. |
134 | */ | 133 | */ |
135 | unsigned int | 134 | unsigned int |
136 | GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, | 135 | GNUNET_STRINGS_buffer_tokenize (const char *buffer, |
137 | unsigned int count, ...) | 136 | size_t size, |
137 | unsigned int count, | ||
138 | ...) | ||
138 | { | 139 | { |
139 | unsigned int start; | 140 | unsigned int start; |
140 | unsigned int needed; | 141 | unsigned int needed; |
@@ -153,10 +154,10 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, | |||
153 | if (needed == size) | 154 | if (needed == size) |
154 | { | 155 | { |
155 | va_end (ap); | 156 | va_end (ap); |
156 | return 0; /* error */ | 157 | return 0; /* error */ |
157 | } | 158 | } |
158 | *r = &buffer[start]; | 159 | *r = &buffer[start]; |
159 | needed++; /* skip 0-termination */ | 160 | needed++; /* skip 0-termination */ |
160 | count--; | 161 | count--; |
161 | } | 162 | } |
162 | va_end (ap); | 163 | va_end (ap); |
@@ -173,7 +174,7 @@ GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, | |||
173 | char * | 174 | char * |
174 | GNUNET_STRINGS_byte_size_fancy (unsigned long long size) | 175 | GNUNET_STRINGS_byte_size_fancy (unsigned long long size) |
175 | { | 176 | { |
176 | const char *unit = _( /* size unit */ "b"); | 177 | const char *unit = _ (/* size unit */ "b"); |
177 | char *ret; | 178 | char *ret; |
178 | 179 | ||
179 | if (size > 5 * 1024) | 180 | if (size > 5 * 1024) |
@@ -215,7 +216,7 @@ GNUNET_STRINGS_byte_size_fancy (unsigned long long size) | |||
215 | * null byte | 216 | * null byte |
216 | */ | 217 | */ |
217 | size_t | 218 | size_t |
218 | GNUNET_strlcpy(char *dst, const char *src, size_t n) | 219 | GNUNET_strlcpy (char *dst, const char *src, size_t n) |
219 | { | 220 | { |
220 | size_t slen; | 221 | size_t slen; |
221 | 222 | ||
@@ -257,8 +258,8 @@ struct ConversionTable | |||
257 | */ | 258 | */ |
258 | static int | 259 | static int |
259 | convert_with_table (const char *input, | 260 | convert_with_table (const char *input, |
260 | const struct ConversionTable *table, | 261 | const struct ConversionTable *table, |
261 | unsigned long long *output) | 262 | unsigned long long *output) |
262 | { | 263 | { |
263 | unsigned long long ret; | 264 | unsigned long long ret; |
264 | char *in; | 265 | char *in; |
@@ -290,7 +291,7 @@ convert_with_table (const char *input, | |||
290 | if ((0 != errno) || (endptr == tok)) | 291 | if ((0 != errno) || (endptr == tok)) |
291 | { | 292 | { |
292 | GNUNET_free (in); | 293 | GNUNET_free (in); |
293 | return GNUNET_SYSERR; /* expected number */ | 294 | return GNUNET_SYSERR; /* expected number */ |
294 | } | 295 | } |
295 | if ('\0' == endptr[0]) | 296 | if ('\0' == endptr[0]) |
296 | break; /* next tok */ | 297 | break; /* next tok */ |
@@ -318,26 +319,22 @@ GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, | |||
318 | unsigned long long *size) | 319 | unsigned long long *size) |
319 | { | 320 | { |
320 | static const struct ConversionTable table[] = | 321 | static const struct ConversionTable table[] = |
321 | { | 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 | |||
338 | return convert_with_table (fancy_size, | ||
339 | table, | ||
340 | size); | ||
341 | } | 338 | } |
342 | 339 | ||
343 | 340 | ||
@@ -354,31 +351,29 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, | |||
354 | struct GNUNET_TIME_Relative *rtime) | 351 | struct GNUNET_TIME_Relative *rtime) |
355 | { | 352 | { |
356 | static const struct ConversionTable table[] = | 353 | static const struct ConversionTable table[] = |
357 | { | 354 | {{"us", 1}, |
358 | { "us", 1}, | 355 | {"ms", 1000}, |
359 | { "ms", 1000 }, | 356 | {"s", 1000 * 1000LL}, |
360 | { "s", 1000 * 1000LL }, | 357 | {"second", 1000 * 1000LL}, |
361 | { "second", 1000 * 1000LL }, | 358 | {"seconds", 1000 * 1000LL}, |
362 | { "seconds", 1000 * 1000LL }, | 359 | {"\"", 1000 * 1000LL}, |
363 | { "\"", 1000 * 1000LL }, | 360 | {"m", 60 * 1000 * 1000LL}, |
364 | { "m", 60 * 1000 * 1000LL}, | 361 | {"min", 60 * 1000 * 1000LL}, |
365 | { "min", 60 * 1000 * 1000LL}, | 362 | {"minute", 60 * 1000 * 1000LL}, |
366 | { "minute", 60 * 1000 * 1000LL}, | 363 | {"minutes", 60 * 1000 * 1000LL}, |
367 | { "minutes", 60 * 1000 * 1000LL}, | 364 | {"'", 60 * 1000 * 1000LL}, |
368 | { "'", 60 * 1000 * 1000LL}, | 365 | {"h", 60 * 60 * 1000 * 1000LL}, |
369 | { "h", 60 * 60 * 1000 * 1000LL}, | 366 | {"hour", 60 * 60 * 1000 * 1000LL}, |
370 | { "hour", 60 * 60 * 1000 * 1000LL}, | 367 | {"hours", 60 * 60 * 1000 * 1000LL}, |
371 | { "hours", 60 * 60 * 1000 * 1000LL}, | 368 | {"d", 24 * 60 * 60 * 1000LL * 1000LL}, |
372 | { "d", 24 * 60 * 60 * 1000LL * 1000LL}, | 369 | {"day", 24 * 60 * 60 * 1000LL * 1000LL}, |
373 | { "day", 24 * 60 * 60 * 1000LL * 1000LL}, | 370 | {"days", 24 * 60 * 60 * 1000LL * 1000LL}, |
374 | { "days", 24 * 60 * 60 * 1000LL * 1000LL}, | 371 | {"week", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, |
375 | { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, | 372 | {"weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, |
376 | { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL}, | 373 | {"year", 31536000000000LL /* year */}, |
377 | { "year", 31536000000000LL /* year */ }, | 374 | {"years", 31536000000000LL /* year */}, |
378 | { "years", 31536000000000LL /* year */ }, | 375 | {"a", 31536000000000LL /* year */}, |
379 | { "a", 31536000000000LL /* year */ }, | 376 | {NULL, 0}}; |
380 | { NULL, 0} | ||
381 | }; | ||
382 | int ret; | 377 | int ret; |
383 | unsigned long long val; | 378 | unsigned long long val; |
384 | 379 | ||
@@ -387,9 +382,7 @@ GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, | |||
387 | *rtime = GNUNET_TIME_UNIT_FOREVER_REL; | 382 | *rtime = GNUNET_TIME_UNIT_FOREVER_REL; |
388 | return GNUNET_OK; | 383 | return GNUNET_OK; |
389 | } | 384 | } |
390 | ret = convert_with_table (fancy_time, | 385 | ret = convert_with_table (fancy_time, table, &val); |
391 | table, | ||
392 | &val); | ||
393 | rtime->rel_value_us = (uint64_t) val; | 386 | rtime->rel_value_us = (uint64_t) val; |
394 | return ret; | 387 | return ret; |
395 | } | 388 | } |
@@ -412,24 +405,23 @@ GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, | |||
412 | time_t t; | 405 | time_t t; |
413 | const char *eos; | 406 | const char *eos; |
414 | 407 | ||
415 | if (0 == strcasecmp ("end of time", | 408 | if (0 == strcasecmp ("end of time", fancy_time)) |
416 | fancy_time)) | ||
417 | { | 409 | { |
418 | *atime = GNUNET_TIME_UNIT_FOREVER_ABS; | 410 | *atime = GNUNET_TIME_UNIT_FOREVER_ABS; |
419 | return GNUNET_OK; | 411 | return GNUNET_OK; |
420 | } | 412 | } |
421 | eos = &fancy_time[strlen (fancy_time)]; | 413 | eos = &fancy_time[strlen (fancy_time)]; |
422 | memset (&tv, 0, sizeof (tv)); | 414 | memset (&tv, 0, sizeof (tv)); |
423 | 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)) && |
424 | (eos != strptime (fancy_time, "%c", &tv)) && | 416 | (eos != strptime (fancy_time, "%c", &tv)) && |
425 | (eos != strptime (fancy_time, "%Ec", &tv)) && | 417 | (eos != strptime (fancy_time, "%Ec", &tv)) && |
426 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && | 418 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) && |
427 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) && | 419 | (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) && |
428 | (eos != strptime (fancy_time, "%x", &tv)) && | 420 | (eos != strptime (fancy_time, "%x", &tv)) && |
429 | (eos != strptime (fancy_time, "%Ex", &tv)) && | 421 | (eos != strptime (fancy_time, "%Ex", &tv)) && |
430 | (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) && | 422 | (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) && |
431 | (eos != strptime (fancy_time, "%Y-%m", &tv)) && | 423 | (eos != strptime (fancy_time, "%Y-%m", &tv)) && |
432 | (eos != strptime (fancy_time, "%Y", &tv)) ) | 424 | (eos != strptime (fancy_time, "%Y", &tv))) |
433 | return GNUNET_SYSERR; | 425 | return GNUNET_SYSERR; |
434 | t = mktime (&tv); | 426 | t = mktime (&tv); |
435 | atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL); | 427 | atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL); |
@@ -452,9 +444,9 @@ GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, | |||
452 | */ | 444 | */ |
453 | char * | 445 | char * |
454 | GNUNET_STRINGS_conv (const char *input, | 446 | GNUNET_STRINGS_conv (const char *input, |
455 | size_t len, | 447 | size_t len, |
456 | const char *input_charset, | 448 | const char *input_charset, |
457 | const char *output_charset) | 449 | const char *output_charset) |
458 | { | 450 | { |
459 | char *ret; | 451 | char *ret; |
460 | uint8_t *u8_string; | 452 | uint8_t *u8_string; |
@@ -463,10 +455,12 @@ GNUNET_STRINGS_conv (const char *input, | |||
463 | size_t encoded_string_length; | 455 | size_t encoded_string_length; |
464 | 456 | ||
465 | u8_string = u8_conv_from_encoding (input_charset, | 457 | u8_string = u8_conv_from_encoding (input_charset, |
466 | iconveh_error, | 458 | iconveh_error, |
467 | input, len, | 459 | input, |
468 | NULL, NULL, | 460 | len, |
469 | &u8_string_length); | 461 | NULL, |
462 | NULL, | ||
463 | &u8_string_length); | ||
470 | if (NULL == u8_string) | 464 | if (NULL == u8_string) |
471 | { | 465 | { |
472 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); | 466 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding"); |
@@ -480,10 +474,13 @@ GNUNET_STRINGS_conv (const char *input, | |||
480 | free (u8_string); | 474 | free (u8_string); |
481 | return ret; | 475 | return ret; |
482 | } | 476 | } |
483 | encoded_string = u8_conv_to_encoding (output_charset, iconveh_error, | 477 | encoded_string = u8_conv_to_encoding (output_charset, |
484 | u8_string, u8_string_length, | 478 | iconveh_error, |
485 | NULL, NULL, | 479 | u8_string, |
486 | &encoded_string_length); | 480 | u8_string_length, |
481 | NULL, | ||
482 | NULL, | ||
483 | &encoded_string_length); | ||
487 | free (u8_string); | 484 | free (u8_string); |
488 | if (NULL == encoded_string) | 485 | if (NULL == encoded_string) |
489 | { | 486 | { |
@@ -495,10 +492,11 @@ GNUNET_STRINGS_conv (const char *input, | |||
495 | ret[encoded_string_length] = '\0'; | 492 | ret[encoded_string_length] = '\0'; |
496 | free (encoded_string); | 493 | free (encoded_string); |
497 | return ret; | 494 | return ret; |
498 | fail: | 495 | fail: |
499 | LOG (GNUNET_ERROR_TYPE_WARNING, | 496 | LOG (GNUNET_ERROR_TYPE_WARNING, |
500 | _("Character sets requested were `%s'->`%s'\n"), | 497 | _ ("Character sets requested were `%s'->`%s'\n"), |
501 | "UTF-8", output_charset); | 498 | "UTF-8", |
499 | output_charset); | ||
502 | ret = GNUNET_malloc (len + 1); | 500 | ret = GNUNET_malloc (len + 1); |
503 | GNUNET_memcpy (ret, input, len); | 501 | GNUNET_memcpy (ret, input, len); |
504 | ret[len] = '\0'; | 502 | ret[len] = '\0'; |
@@ -519,9 +517,7 @@ GNUNET_STRINGS_conv (const char *input, | |||
519 | * string is returned. | 517 | * string is returned. |
520 | */ | 518 | */ |
521 | char * | 519 | char * |
522 | GNUNET_STRINGS_to_utf8 (const char *input, | 520 | GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset) |
523 | size_t len, | ||
524 | const char *charset) | ||
525 | { | 521 | { |
526 | return GNUNET_STRINGS_conv (input, len, charset, "UTF-8"); | 522 | return GNUNET_STRINGS_conv (input, len, charset, "UTF-8"); |
527 | } | 523 | } |
@@ -539,9 +535,7 @@ GNUNET_STRINGS_to_utf8 (const char *input, | |||
539 | * string is returned. | 535 | * string is returned. |
540 | */ | 536 | */ |
541 | char * | 537 | char * |
542 | GNUNET_STRINGS_from_utf8 (const char *input, | 538 | GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset) |
543 | size_t len, | ||
544 | const char *charset) | ||
545 | { | 539 | { |
546 | return GNUNET_STRINGS_conv (input, len, "UTF-8", charset); | 540 | return GNUNET_STRINGS_conv (input, len, "UTF-8", charset); |
547 | } | 541 | } |
@@ -555,17 +549,20 @@ GNUNET_STRINGS_from_utf8 (const char *input, | |||
555 | * @param output output buffer | 549 | * @param output output buffer |
556 | */ | 550 | */ |
557 | void | 551 | void |
558 | GNUNET_STRINGS_utf8_tolower (const char *input, | 552 | GNUNET_STRINGS_utf8_tolower (const char *input, char *output) |
559 | char *output) | ||
560 | { | 553 | { |
561 | uint8_t *tmp_in; | 554 | uint8_t *tmp_in; |
562 | size_t len; | 555 | size_t len; |
563 | 556 | ||
564 | tmp_in = u8_tolower ((uint8_t*)input, strlen ((char *) input), | 557 | tmp_in = u8_tolower ((uint8_t *) input, |
565 | NULL, UNINORM_NFD, NULL, &len); | 558 | strlen ((char *) input), |
566 | GNUNET_memcpy(output, tmp_in, len); | 559 | NULL, |
560 | UNINORM_NFD, | ||
561 | NULL, | ||
562 | &len); | ||
563 | GNUNET_memcpy (output, tmp_in, len); | ||
567 | output[len] = '\0'; | 564 | output[len] = '\0'; |
568 | free(tmp_in); | 565 | free (tmp_in); |
569 | } | 566 | } |
570 | 567 | ||
571 | 568 | ||
@@ -577,14 +574,17 @@ GNUNET_STRINGS_utf8_tolower (const char *input, | |||
577 | * @param output output buffer | 574 | * @param output output buffer |
578 | */ | 575 | */ |
579 | void | 576 | void |
580 | GNUNET_STRINGS_utf8_toupper(const char *input, | 577 | GNUNET_STRINGS_utf8_toupper (const char *input, char *output) |
581 | char *output) | ||
582 | { | 578 | { |
583 | uint8_t *tmp_in; | 579 | uint8_t *tmp_in; |
584 | size_t len; | 580 | size_t len; |
585 | 581 | ||
586 | tmp_in = u8_toupper ((uint8_t*)input, strlen ((char *) input), | 582 | tmp_in = u8_toupper ((uint8_t *) input, |
587 | NULL, UNINORM_NFD, NULL, &len); | 583 | strlen ((char *) input), |
584 | NULL, | ||
585 | UNINORM_NFD, | ||
586 | NULL, | ||
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); |
@@ -624,7 +624,7 @@ GNUNET_STRINGS_filename_expand (const char *fil) | |||
624 | if (fm == NULL) | 624 | if (fm == NULL) |
625 | { | 625 | { |
626 | LOG (GNUNET_ERROR_TYPE_WARNING, | 626 | LOG (GNUNET_ERROR_TYPE_WARNING, |
627 | _("Failed to expand `$HOME': environment variable `HOME' not set")); | 627 | _ ("Failed to expand `$HOME': environment variable `HOME' not set")); |
628 | return NULL; | 628 | return NULL; |
629 | } | 629 | } |
630 | fm = GNUNET_strdup (fm); | 630 | fm = GNUNET_strdup (fm); |
@@ -660,20 +660,20 @@ GNUNET_STRINGS_filename_expand (const char *fil) | |||
660 | } | 660 | } |
661 | if (fm == NULL) | 661 | if (fm == NULL) |
662 | { | 662 | { |
663 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, | 663 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd"); |
664 | "getcwd"); | 664 | buffer = getenv ("PWD"); /* alternative */ |
665 | buffer = getenv ("PWD"); /* alternative */ | ||
666 | if (buffer != NULL) | 665 | if (buffer != NULL) |
667 | fm = GNUNET_strdup (buffer); | 666 | fm = GNUNET_strdup (buffer); |
668 | } | 667 | } |
669 | if (fm == NULL) | 668 | if (fm == NULL) |
670 | fm = GNUNET_strdup ("./"); /* give up */ | 669 | fm = GNUNET_strdup ("./"); /* give up */ |
671 | } | 670 | } |
672 | GNUNET_asprintf (&buffer, | 671 | GNUNET_asprintf (&buffer, |
673 | "%s%s%s", | 672 | "%s%s%s", |
674 | fm, | 673 | fm, |
675 | (fm[strlen (fm) - 1] == | 674 | (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? "" |
676 | DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr); | 675 | : DIR_SEPARATOR_STR, |
676 | fil_ptr); | ||
677 | GNUNET_free (fm); | 677 | GNUNET_free (fm); |
678 | return buffer; | 678 | return buffer; |
679 | #else | 679 | #else |
@@ -682,29 +682,22 @@ GNUNET_STRINGS_filename_expand (const char *fil) | |||
682 | if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS) | 682 | if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS) |
683 | { | 683 | { |
684 | SetErrnoFromWinError (lRet); | 684 | SetErrnoFromWinError (lRet); |
685 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, | 685 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path"); |
686 | "plibc_conv_to_win_path"); | ||
687 | return NULL; | 686 | return NULL; |
688 | } | 687 | } |
689 | /* is the path relative? */ | 688 | /* is the path relative? */ |
690 | if ( (0 != strncmp (fn + 1, ":\\", 2)) && | 689 | if ((0 != strncmp (fn + 1, ":\\", 2)) && (0 != strncmp (fn, "\\\\", 2))) |
691 | (0 != strncmp (fn, "\\\\", 2)) ) | ||
692 | { | 690 | { |
693 | char szCurDir[MAX_PATH + 1]; | 691 | char szCurDir[MAX_PATH + 1]; |
694 | 692 | ||
695 | lRet = GetCurrentDirectory (MAX_PATH + 1, | 693 | lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir); |
696 | szCurDir); | ||
697 | if (lRet + strlen (fn) + 1 > (MAX_PATH + 1)) | 694 | if (lRet + strlen (fn) + 1 > (MAX_PATH + 1)) |
698 | { | 695 | { |
699 | SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW); | 696 | SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW); |
700 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, | 697 | LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory"); |
701 | "GetCurrentDirectory"); | ||
702 | return NULL; | 698 | return NULL; |
703 | } | 699 | } |
704 | GNUNET_asprintf (&buffer, | 700 | GNUNET_asprintf (&buffer, "%s\\%s", szCurDir, fn); |
705 | "%s\\%s", | ||
706 | szCurDir, | ||
707 | fn); | ||
708 | GNUNET_free (fn); | 701 | GNUNET_free (fn); |
709 | fn = buffer; | 702 | fn = buffer; |
710 | } | 703 | } |
@@ -725,56 +718,46 @@ GNUNET_STRINGS_filename_expand (const char *fil) | |||
725 | */ | 718 | */ |
726 | const char * | 719 | const char * |
727 | GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, | 720 | GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, |
728 | int do_round) | 721 | int do_round) |
729 | { | 722 | { |
730 | static char buf[128]; | 723 | static char buf[128]; |
731 | const char *unit = _( /* time unit */ "µs"); | 724 | const char *unit = _ (/* time unit */ "µs"); |
732 | uint64_t dval = delta.rel_value_us; | 725 | uint64_t dval = delta.rel_value_us; |
733 | 726 | ||
734 | if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) | 727 | if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us) |
735 | return _("forever"); | 728 | return _ ("forever"); |
736 | if (0 == delta.rel_value_us) | 729 | if (0 == delta.rel_value_us) |
737 | return _("0 ms"); | 730 | return _ ("0 ms"); |
738 | if ( ( (GNUNET_YES == do_round) && | 731 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) |
739 | (dval > 5 * 1000) ) || | ||
740 | (0 == (dval % 1000) )) | ||
741 | { | 732 | { |
742 | dval = dval / 1000; | 733 | dval = dval / 1000; |
743 | unit = _( /* time unit */ "ms"); | 734 | unit = _ (/* time unit */ "ms"); |
744 | if ( ( (GNUNET_YES == do_round) && | 735 | if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000))) |
745 | (dval > 5 * 1000) ) || | ||
746 | (0 == (dval % 1000) )) | ||
747 | { | 736 | { |
748 | dval = dval / 1000; | 737 | dval = dval / 1000; |
749 | unit = _( /* time unit */ "s"); | 738 | unit = _ (/* time unit */ "s"); |
750 | if ( ( (GNUNET_YES == do_round) && | 739 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) |
751 | (dval > 5 * 60) ) || | ||
752 | (0 == (dval % 60) ) ) | ||
753 | { | 740 | { |
754 | dval = dval / 60; | 741 | dval = dval / 60; |
755 | unit = _( /* time unit */ "m"); | 742 | unit = _ (/* time unit */ "m"); |
756 | if ( ( (GNUNET_YES == do_round) && | 743 | if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60))) |
757 | (dval > 5 * 60) ) || | 744 | { |
758 | (0 == (dval % 60) )) | 745 | dval = dval / 60; |
759 | { | 746 | unit = _ (/* time unit */ "h"); |
760 | dval = dval / 60; | 747 | if (((GNUNET_YES == do_round) && (dval > 5 * 24)) || |
761 | unit = _( /* time unit */ "h"); | 748 | (0 == (dval % 24))) |
762 | if ( ( (GNUNET_YES == do_round) && | 749 | { |
763 | (dval > 5 * 24) ) || | 750 | dval = dval / 24; |
764 | (0 == (dval % 24)) ) | 751 | if (1 == dval) |
765 | { | 752 | unit = _ (/* time unit */ "day"); |
766 | dval = dval / 24; | 753 | else |
767 | if (1 == dval) | 754 | unit = _ (/* time unit */ "days"); |
768 | unit = _( /* time unit */ "day"); | 755 | } |
769 | else | 756 | } |
770 | unit = _( /* time unit */ "days"); | ||
771 | } | ||
772 | } | ||
773 | } | 757 | } |
774 | } | 758 | } |
775 | } | 759 | } |
776 | GNUNET_snprintf (buf, sizeof (buf), | 760 | GNUNET_snprintf (buf, sizeof (buf), "%llu %s", dval, unit); |
777 | "%llu %s", dval, unit); | ||
778 | return buf; | 761 | return buf; |
779 | } | 762 | } |
780 | 763 | ||
@@ -796,7 +779,7 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) | |||
796 | struct tm *tp; | 779 | struct tm *tp; |
797 | 780 | ||
798 | if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) | 781 | if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) |
799 | return _("end of time"); | 782 | return _ ("end of time"); |
800 | tt = t.abs_value_us / 1000LL / 1000LL; | 783 | tt = t.abs_value_us / 1000LL / 1000LL; |
801 | tp = localtime (&tt); | 784 | tp = localtime (&tt); |
802 | /* This is hacky, but i don't know a way to detect libc character encoding. | 785 | /* This is hacky, but i don't know a way to detect libc character encoding. |
@@ -813,12 +796,16 @@ GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t) | |||
813 | uint8_t *conved; | 796 | uint8_t *conved; |
814 | size_t ssize; | 797 | size_t ssize; |
815 | 798 | ||
816 | wcsftime (wbuf, sizeof (wbuf) / sizeof (wchar_t), | 799 | wcsftime (wbuf, |
817 | L"%a %b %d %H:%M:%S %Y", tp); | 800 | sizeof (wbuf) / sizeof (wchar_t), |
801 | L"%a %b %d %H:%M:%S %Y", | ||
802 | tp); | ||
818 | 803 | ||
819 | ssize = sizeof (buf); | 804 | ssize = sizeof (buf); |
820 | conved = u16_to_u8 (wbuf, sizeof (wbuf) / sizeof (wchar_t), | 805 | conved = u16_to_u8 (wbuf, |
821 | (uint8_t *) buf, &ssize); | 806 | sizeof (wbuf) / sizeof (wchar_t), |
807 | (uint8_t *) buf, | ||
808 | &ssize); | ||
822 | if (conved != (uint8_t *) buf) | 809 | if (conved != (uint8_t *) buf) |
823 | { | 810 | { |
824 | GNUNET_strlcpy (buf, (char *) conved, sizeof (buf)); | 811 | GNUNET_strlcpy (buf, (char *) conved, sizeof (buf)); |
@@ -847,8 +834,7 @@ GNUNET_STRINGS_get_short_name (const char *filename) | |||
847 | const char *short_fn = filename; | 834 | const char *short_fn = filename; |
848 | const char *ss; | 835 | const char *ss; |
849 | 836 | ||
850 | while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) | 837 | while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0')) |
851 | && (ss[1] != '\0')) | ||
852 | short_fn = 1 + ss; | 838 | short_fn = 1 + ss; |
853 | return short_fn; | 839 | return short_fn; |
854 | } | 840 | } |
@@ -890,7 +876,7 @@ getValue__ (unsigned char a) | |||
890 | return a - '0'; | 876 | return a - '0'; |
891 | if ((a >= 'a') && (a <= 'z')) | 877 | if ((a >= 'a') && (a <= 'z')) |
892 | a = toupper (a); | 878 | a = toupper (a); |
893 | /* return (a - 'a' + 10); */ | 879 | /* return (a - 'a' + 10); */ |
894 | dec = 0; | 880 | dec = 0; |
895 | if ((a >= 'A') && (a <= 'Z')) | 881 | if ((a >= 'A') && (a <= 'Z')) |
896 | { | 882 | { |
@@ -950,12 +936,12 @@ GNUNET_STRINGS_data_to_string (const void *data, | |||
950 | { | 936 | { |
951 | if ((rpos < size) && (vbit < 5)) | 937 | if ((rpos < size) && (vbit < 5)) |
952 | { | 938 | { |
953 | bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */ | 939 | bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */ |
954 | vbit += 8; | 940 | vbit += 8; |
955 | } | 941 | } |
956 | if (vbit < 5) | 942 | if (vbit < 5) |
957 | { | 943 | { |
958 | bits <<= (5 - vbit); /* zero-padding */ | 944 | bits <<= (5 - vbit); /* zero-padding */ |
959 | GNUNET_assert (vbit == ((size * 8) % 5)); | 945 | GNUNET_assert (vbit == ((size * 8) % 5)); |
960 | vbit = 5; | 946 | vbit = 5; |
961 | } | 947 | } |
@@ -985,8 +971,7 @@ GNUNET_STRINGS_data_to_string (const void *data, | |||
985 | * @return freshly allocated, null-terminated string | 971 | * @return freshly allocated, null-terminated string |
986 | */ | 972 | */ |
987 | char * | 973 | char * |
988 | GNUNET_STRINGS_data_to_string_alloc (const void *buf, | 974 | GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size) |
989 | size_t size) | ||
990 | { | 975 | { |
991 | char *str_buf; | 976 | char *str_buf; |
992 | size_t len = size * 8; | 977 | size_t len = size * 8; |
@@ -1018,8 +1003,10 @@ GNUNET_STRINGS_data_to_string_alloc (const void *buf, | |||
1018 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding | 1003 | * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding |
1019 | */ | 1004 | */ |
1020 | int | 1005 | int |
1021 | GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, | 1006 | GNUNET_STRINGS_string_to_data (const char *enc, |
1022 | void *out, size_t out_size) | 1007 | size_t enclen, |
1008 | void *out, | ||
1009 | size_t out_size) | ||
1023 | { | 1010 | { |
1024 | unsigned int rpos; | 1011 | unsigned int rpos; |
1025 | unsigned int wpos; | 1012 | unsigned int wpos; |
@@ -1073,8 +1060,7 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, | |||
1073 | vbit -= 8; | 1060 | vbit -= 8; |
1074 | } | 1061 | } |
1075 | } | 1062 | } |
1076 | if ( (0 != rpos) || | 1063 | if ((0 != rpos) || (0 != vbit)) |
1077 | (0 != vbit) ) | ||
1078 | return GNUNET_SYSERR; | 1064 | return GNUNET_SYSERR; |
1079 | return GNUNET_OK; | 1065 | return GNUNET_OK; |
1080 | } | 1066 | } |
@@ -1098,8 +1084,8 @@ GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, | |||
1098 | */ | 1084 | */ |
1099 | int | 1085 | int |
1100 | GNUNET_STRINGS_parse_uri (const char *path, | 1086 | GNUNET_STRINGS_parse_uri (const char *path, |
1101 | char **scheme_part, | 1087 | char **scheme_part, |
1102 | const char **path_part) | 1088 | const char **path_part) |
1103 | { | 1089 | { |
1104 | size_t len; | 1090 | size_t len; |
1105 | size_t i; | 1091 | size_t i; |
@@ -1107,19 +1093,20 @@ GNUNET_STRINGS_parse_uri (const char *path, | |||
1107 | int pp_state = 0; | 1093 | int pp_state = 0; |
1108 | const char *post_scheme_part = NULL; | 1094 | const char *post_scheme_part = NULL; |
1109 | len = strlen (path); | 1095 | len = strlen (path); |
1110 | for (end = 0, i = 0; !end && i < len; i++) | 1096 | for (end = 0, i = 0; ! end && i < len; i++) |
1111 | { | 1097 | { |
1112 | switch (pp_state) | 1098 | switch (pp_state) |
1113 | { | 1099 | { |
1114 | case 0: | 1100 | case 0: |
1115 | if ( (path[i] == ':') && (i > 0) ) | 1101 | if ((path[i] == ':') && (i > 0)) |
1116 | { | 1102 | { |
1117 | pp_state += 1; | 1103 | pp_state += 1; |
1118 | continue; | 1104 | continue; |
1119 | } | 1105 | } |
1120 | if (!((path[i] >= 'A' && path[i] <= 'Z') || (path[i] >= 'a' && path[i] <= 'z') | 1106 | if (! ((path[i] >= 'A' && path[i] <= 'Z') || |
1121 | || (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || path[i] == '-' | 1107 | (path[i] >= 'a' && path[i] <= 'z') || |
1122 | || (path[i] == '.'))) | 1108 | (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || |
1109 | path[i] == '-' || (path[i] == '.'))) | ||
1123 | end = 1; | 1110 | end = 1; |
1124 | break; | 1111 | break; |
1125 | case 1: | 1112 | case 1: |
@@ -1178,7 +1165,7 @@ GNUNET_STRINGS_path_is_absolute (const char *filename, | |||
1178 | #endif | 1165 | #endif |
1179 | const char *post_scheme_path; | 1166 | const char *post_scheme_path; |
1180 | int is_uri; | 1167 | int is_uri; |
1181 | char * uri; | 1168 | char *uri; |
1182 | /* consider POSIX paths to be absolute too, even on W32, | 1169 | /* consider POSIX paths to be absolute too, even on W32, |
1183 | * as plibc expansion will fix them for us. | 1170 | * as plibc expansion will fix them for us. |
1184 | */ | 1171 | */ |
@@ -1196,14 +1183,17 @@ GNUNET_STRINGS_path_is_absolute (const char *filename, | |||
1196 | else | 1183 | else |
1197 | GNUNET_free_non_null (uri); | 1184 | GNUNET_free_non_null (uri); |
1198 | #if WINDOWS | 1185 | #if WINDOWS |
1199 | len = strlen(post_scheme_path); | 1186 | len = strlen (post_scheme_path); |
1200 | /* Special check for file:///c:/blah | 1187 | /* Special check for file:///c:/blah |
1201 | * We want to parse 'c:/', not '/c:/' | 1188 | * We want to parse 'c:/', not '/c:/' |
1202 | */ | 1189 | */ |
1203 | if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':') | 1190 | if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':') |
1204 | post_scheme_path = &post_scheme_path[1]; | 1191 | post_scheme_path = &post_scheme_path[1]; |
1205 | #endif | 1192 | #endif |
1206 | return GNUNET_STRINGS_path_is_absolute (post_scheme_path, GNUNET_NO, NULL, NULL); | 1193 | return GNUNET_STRINGS_path_is_absolute (post_scheme_path, |
1194 | GNUNET_NO, | ||
1195 | NULL, | ||
1196 | NULL); | ||
1207 | } | 1197 | } |
1208 | } | 1198 | } |
1209 | else | 1199 | else |
@@ -1214,18 +1204,18 @@ GNUNET_STRINGS_path_is_absolute (const char *filename, | |||
1214 | #if WINDOWS | 1204 | #if WINDOWS |
1215 | len = strlen (filename); | 1205 | len = strlen (filename); |
1216 | if (len >= 3 && | 1206 | if (len >= 3 && |
1217 | ((filename[0] >= 'A' && filename[0] <= 'Z') | 1207 | ((filename[0] >= 'A' && filename[0] <= 'Z') || |
1218 | || (filename[0] >= 'a' && filename[0] <= 'z')) | 1208 | (filename[0] >= 'a' && filename[0] <= 'z')) && |
1219 | && filename[1] == ':' && (filename[2] == '/' || filename[2] == '\\')) | 1209 | filename[1] == ':' && (filename[2] == '/' || filename[2] == '\\')) |
1220 | return GNUNET_YES; | 1210 | return GNUNET_YES; |
1221 | #endif | 1211 | #endif |
1222 | return GNUNET_NO; | 1212 | return GNUNET_NO; |
1223 | } | 1213 | } |
1224 | 1214 | ||
1225 | #if MINGW | 1215 | #if MINGW |
1226 | #define _IFMT 0170000 /* type of file */ | 1216 | #define _IFMT 0170000 /* type of file */ |
1227 | #define _IFLNK 0120000 /* symbolic link */ | 1217 | #define _IFLNK 0120000 /* symbolic link */ |
1228 | #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK) | 1218 | #define S_ISLNK(m) (((m) &_IFMT) == _IFLNK) |
1229 | #endif | 1219 | #endif |
1230 | 1220 | ||
1231 | 1221 | ||
@@ -1239,19 +1229,19 @@ GNUNET_STRINGS_path_is_absolute (const char *filename, | |||
1239 | */ | 1229 | */ |
1240 | int | 1230 | int |
1241 | GNUNET_STRINGS_check_filename (const char *filename, | 1231 | GNUNET_STRINGS_check_filename (const char *filename, |
1242 | enum GNUNET_STRINGS_FilenameCheck checks) | 1232 | enum GNUNET_STRINGS_FilenameCheck checks) |
1243 | { | 1233 | { |
1244 | struct stat st; | 1234 | struct stat st; |
1245 | if ( (NULL == filename) || (filename[0] == '\0') ) | 1235 | if ((NULL == filename) || (filename[0] == '\0')) |
1246 | return GNUNET_SYSERR; | 1236 | return GNUNET_SYSERR; |
1247 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) | 1237 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)) |
1248 | if (!GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL)) | 1238 | if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL)) |
1249 | return GNUNET_NO; | 1239 | return GNUNET_NO; |
1250 | if (0 != (checks & (GNUNET_STRINGS_CHECK_EXISTS | 1240 | if (0 != (checks & |
1251 | | GNUNET_STRINGS_CHECK_IS_DIRECTORY | 1241 | (GNUNET_STRINGS_CHECK_EXISTS | GNUNET_STRINGS_CHECK_IS_DIRECTORY | |
1252 | | GNUNET_STRINGS_CHECK_IS_LINK))) | 1242 | GNUNET_STRINGS_CHECK_IS_LINK))) |
1253 | { | 1243 | { |
1254 | if (0 != STAT (filename, &st)) | 1244 | if (0 != stat (filename, &st)) |
1255 | { | 1245 | { |
1256 | if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) | 1246 | if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS)) |
1257 | return GNUNET_NO; | 1247 | return GNUNET_NO; |
@@ -1260,10 +1250,10 @@ GNUNET_STRINGS_check_filename (const char *filename, | |||
1260 | } | 1250 | } |
1261 | } | 1251 | } |
1262 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) | 1252 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)) |
1263 | if (!S_ISDIR (st.st_mode)) | 1253 | if (! S_ISDIR (st.st_mode)) |
1264 | return GNUNET_NO; | 1254 | return GNUNET_NO; |
1265 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) | 1255 | if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK)) |
1266 | if (!S_ISLNK (st.st_mode)) | 1256 | if (! S_ISLNK (st.st_mode)) |
1267 | return GNUNET_NO; | 1257 | return GNUNET_NO; |
1268 | return GNUNET_YES; | 1258 | return GNUNET_YES; |
1269 | } | 1259 | } |
@@ -1283,8 +1273,8 @@ GNUNET_STRINGS_check_filename (const char *filename, | |||
1283 | */ | 1273 | */ |
1284 | int | 1274 | int |
1285 | GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, | 1275 | GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, |
1286 | uint16_t addrlen, | 1276 | uint16_t addrlen, |
1287 | struct sockaddr_in6 *r_buf) | 1277 | struct sockaddr_in6 *r_buf) |
1288 | { | 1278 | { |
1289 | char zbuf[addrlen + 1]; | 1279 | char zbuf[addrlen + 1]; |
1290 | int ret; | 1280 | int ret; |
@@ -1298,7 +1288,7 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, | |||
1298 | if ('[' != zbuf[0]) | 1288 | if ('[' != zbuf[0]) |
1299 | { | 1289 | { |
1300 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1290 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1301 | _("IPv6 address did not start with `['\n")); | 1291 | _ ("IPv6 address did not start with `['\n")); |
1302 | return GNUNET_SYSERR; | 1292 | return GNUNET_SYSERR; |
1303 | } | 1293 | } |
1304 | zbuf[addrlen] = '\0'; | 1294 | zbuf[addrlen] = '\0'; |
@@ -1306,34 +1296,33 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, | |||
1306 | if (NULL == port_colon) | 1296 | if (NULL == port_colon) |
1307 | { | 1297 | { |
1308 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1298 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1309 | _("IPv6 address did contain ':' to separate port number\n")); | 1299 | _ ("IPv6 address did contain ':' to separate port number\n")); |
1310 | return GNUNET_SYSERR; | 1300 | return GNUNET_SYSERR; |
1311 | } | 1301 | } |
1312 | if (']' != *(port_colon - 1)) | 1302 | if (']' != *(port_colon - 1)) |
1313 | { | 1303 | { |
1314 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1304 | GNUNET_log ( |
1315 | _("IPv6 address did contain ']' before ':' to separate port number\n")); | 1305 | GNUNET_ERROR_TYPE_WARNING, |
1306 | _ ("IPv6 address did contain ']' before ':' to separate port number\n")); | ||
1316 | return GNUNET_SYSERR; | 1307 | return GNUNET_SYSERR; |
1317 | } | 1308 | } |
1318 | ret = SSCANF (port_colon, | 1309 | ret = sscanf (port_colon, ":%u%1s", &port, dummy); |
1319 | ":%u%1s", | 1310 | if ((1 != ret) || (port > 65535)) |
1320 | &port, | ||
1321 | dummy); | ||
1322 | if ( (1 != ret) || (port > 65535) ) | ||
1323 | { | 1311 | { |
1324 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1312 | GNUNET_log ( |
1325 | _("IPv6 address did contain a valid port number after the last ':'\n")); | 1313 | GNUNET_ERROR_TYPE_WARNING, |
1314 | _ ("IPv6 address did contain a valid port number after the last ':'\n")); | ||
1326 | return GNUNET_SYSERR; | 1315 | return GNUNET_SYSERR; |
1327 | } | 1316 | } |
1328 | *(port_colon-1) = '\0'; | 1317 | *(port_colon - 1) = '\0'; |
1329 | memset (r_buf, 0, sizeof (struct sockaddr_in6)); | 1318 | memset (r_buf, 0, sizeof (struct sockaddr_in6)); |
1330 | ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr); | 1319 | ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr); |
1331 | if (ret <= 0) | 1320 | if (ret <= 0) |
1332 | { | 1321 | { |
1333 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1322 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1334 | _("Invalid IPv6 address `%s': %s\n"), | 1323 | _ ("Invalid IPv6 address `%s': %s\n"), |
1335 | &zbuf[1], | 1324 | &zbuf[1], |
1336 | STRERROR (errno)); | 1325 | strerror (errno)); |
1337 | return GNUNET_SYSERR; | 1326 | return GNUNET_SYSERR; |
1338 | } | 1327 | } |
1339 | r_buf->sin6_port = htons (port); | 1328 | r_buf->sin6_port = htons (port); |
@@ -1358,8 +1347,8 @@ GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, | |||
1358 | */ | 1347 | */ |
1359 | int | 1348 | int |
1360 | GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | 1349 | GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, |
1361 | uint16_t addrlen, | 1350 | uint16_t addrlen, |
1362 | struct sockaddr_in *r_buf) | 1351 | struct sockaddr_in *r_buf) |
1363 | { | 1352 | { |
1364 | unsigned int temps[4]; | 1353 | unsigned int temps[4]; |
1365 | unsigned int port; | 1354 | unsigned int port; |
@@ -1368,14 +1357,14 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | |||
1368 | 1357 | ||
1369 | if (addrlen < 9) | 1358 | if (addrlen < 9) |
1370 | return GNUNET_SYSERR; | 1359 | return GNUNET_SYSERR; |
1371 | cnt = SSCANF (zt_addr, | 1360 | cnt = sscanf (zt_addr, |
1372 | "%u.%u.%u.%u:%u%1s", | 1361 | "%u.%u.%u.%u:%u%1s", |
1373 | &temps[0], | 1362 | &temps[0], |
1374 | &temps[1], | 1363 | &temps[1], |
1375 | &temps[2], | 1364 | &temps[2], |
1376 | &temps[3], | 1365 | &temps[3], |
1377 | &port, | 1366 | &port, |
1378 | dummy); | 1367 | dummy); |
1379 | if (5 != cnt) | 1368 | if (5 != cnt) |
1380 | return GNUNET_SYSERR; | 1369 | return GNUNET_SYSERR; |
1381 | for (cnt = 0; cnt < 4; cnt++) | 1370 | for (cnt = 0; cnt < 4; cnt++) |
@@ -1385,8 +1374,8 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | |||
1385 | return GNUNET_SYSERR; | 1374 | return GNUNET_SYSERR; |
1386 | r_buf->sin_family = AF_INET; | 1375 | r_buf->sin_family = AF_INET; |
1387 | r_buf->sin_port = htons (port); | 1376 | r_buf->sin_port = htons (port); |
1388 | r_buf->sin_addr.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + | 1377 | r_buf->sin_addr.s_addr = |
1389 | (temps[2] << 8) + temps[3]); | 1378 | htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]); |
1390 | #if HAVE_SOCKADDR_IN_SIN_LEN | 1379 | #if HAVE_SOCKADDR_IN_SIN_LEN |
1391 | r_buf->sin_len = (u_char) sizeof (struct sockaddr_in); | 1380 | r_buf->sin_len = (u_char) sizeof (struct sockaddr_in); |
1392 | #endif | 1381 | #endif |
@@ -1407,8 +1396,8 @@ GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, | |||
1407 | */ | 1396 | */ |
1408 | int | 1397 | int |
1409 | GNUNET_STRINGS_to_address_ip (const char *addr, | 1398 | GNUNET_STRINGS_to_address_ip (const char *addr, |
1410 | uint16_t addrlen, | 1399 | uint16_t addrlen, |
1411 | struct sockaddr_storage *r_buf) | 1400 | struct sockaddr_storage *r_buf) |
1412 | { | 1401 | { |
1413 | if (addr[0] == '[') | 1402 | if (addr[0] == '[') |
1414 | return GNUNET_STRINGS_to_address_ipv6 (addr, | 1403 | return GNUNET_STRINGS_to_address_ipv6 (addr, |
@@ -1431,8 +1420,8 @@ GNUNET_STRINGS_to_address_ip (const char *addr, | |||
1431 | */ | 1420 | */ |
1432 | size_t | 1421 | size_t |
1433 | GNUNET_STRINGS_parse_socket_addr (const char *addr, | 1422 | GNUNET_STRINGS_parse_socket_addr (const char *addr, |
1434 | uint8_t *af, | 1423 | uint8_t *af, |
1435 | struct sockaddr **sa) | 1424 | struct sockaddr **sa) |
1436 | { | 1425 | { |
1437 | char *cp = GNUNET_strdup (addr); | 1426 | char *cp = GNUNET_strdup (addr); |
1438 | 1427 | ||
@@ -1442,9 +1431,9 @@ GNUNET_STRINGS_parse_socket_addr (const char *addr, | |||
1442 | /* IPv6 */ | 1431 | /* IPv6 */ |
1443 | *sa = GNUNET_malloc (sizeof (struct sockaddr_in6)); | 1432 | *sa = GNUNET_malloc (sizeof (struct sockaddr_in6)); |
1444 | if (GNUNET_OK != | 1433 | if (GNUNET_OK != |
1445 | GNUNET_STRINGS_to_address_ipv6 (cp, | 1434 | GNUNET_STRINGS_to_address_ipv6 (cp, |
1446 | strlen (cp), | 1435 | strlen (cp), |
1447 | (struct sockaddr_in6 *) *sa)) | 1436 | (struct sockaddr_in6 *) *sa)) |
1448 | { | 1437 | { |
1449 | GNUNET_free (*sa); | 1438 | GNUNET_free (*sa); |
1450 | *sa = NULL; | 1439 | *sa = NULL; |
@@ -1460,9 +1449,9 @@ GNUNET_STRINGS_parse_socket_addr (const char *addr, | |||
1460 | /* IPv4 */ | 1449 | /* IPv4 */ |
1461 | *sa = GNUNET_malloc (sizeof (struct sockaddr_in)); | 1450 | *sa = GNUNET_malloc (sizeof (struct sockaddr_in)); |
1462 | if (GNUNET_OK != | 1451 | if (GNUNET_OK != |
1463 | GNUNET_STRINGS_to_address_ipv4 (cp, | 1452 | GNUNET_STRINGS_to_address_ipv4 (cp, |
1464 | strlen (cp), | 1453 | strlen (cp), |
1465 | (struct sockaddr_in *) *sa)) | 1454 | (struct sockaddr_in *) *sa)) |
1466 | { | 1455 | { |
1467 | GNUNET_free (*sa); | 1456 | GNUNET_free (*sa); |
1468 | *sa = NULL; | 1457 | *sa = NULL; |
@@ -1481,8 +1470,7 @@ GNUNET_STRINGS_parse_socket_addr (const char *addr, | |||
1481 | * freed with a single call to GNUNET_free(); | 1470 | * freed with a single call to GNUNET_free(); |
1482 | */ | 1471 | */ |
1483 | static char *const * | 1472 | static char *const * |
1484 | _make_continuous_arg_copy (int argc, | 1473 | _make_continuous_arg_copy (int argc, char *const *argv) |
1485 | char *const *argv) | ||
1486 | { | 1474 | { |
1487 | size_t argvsize = 0; | 1475 | size_t argvsize = 0; |
1488 | int i; | 1476 | int i; |
@@ -1519,9 +1507,9 @@ _make_continuous_arg_copy (int argc, | |||
1519 | */ | 1507 | */ |
1520 | int | 1508 | int |
1521 | GNUNET_STRINGS_get_utf8_args (int argc, | 1509 | GNUNET_STRINGS_get_utf8_args (int argc, |
1522 | char *const *argv, | 1510 | char *const *argv, |
1523 | int *u8argc, | 1511 | int *u8argc, |
1524 | char *const **u8argv) | 1512 | char *const **u8argv) |
1525 | { | 1513 | { |
1526 | #if WINDOWS | 1514 | #if WINDOWS |
1527 | wchar_t *wcmd; | 1515 | wchar_t *wcmd; |
@@ -1543,7 +1531,8 @@ GNUNET_STRINGS_get_utf8_args (int argc, | |||
1543 | { | 1531 | { |
1544 | size_t strl; | 1532 | size_t strl; |
1545 | /* Hopefully it will allocate us NUL-terminated strings... */ | 1533 | /* Hopefully it will allocate us NUL-terminated strings... */ |
1546 | split_u8argv[i] = (char *) u16_to_u8 (wargv[i], wcslen (wargv[i]) + 1, NULL, &strl); | 1534 | split_u8argv[i] = |
1535 | (char *) u16_to_u8 (wargv[i], wcslen (wargv[i]) + 1, NULL, &strl); | ||
1547 | if (NULL == split_u8argv[i]) | 1536 | if (NULL == split_u8argv[i]) |
1548 | { | 1537 | { |
1549 | int j; | 1538 | int j; |
@@ -1563,7 +1552,8 @@ GNUNET_STRINGS_get_utf8_args (int argc, | |||
1563 | free (split_u8argv); | 1552 | free (split_u8argv); |
1564 | return GNUNET_OK; | 1553 | return GNUNET_OK; |
1565 | #else | 1554 | #else |
1566 | char *const *new_argv = (char *const *) _make_continuous_arg_copy (argc, argv); | 1555 | char *const *new_argv = |
1556 | (char *const *) _make_continuous_arg_copy (argc, argv); | ||
1567 | *u8argv = new_argv; | 1557 | *u8argv = new_argv; |
1568 | *u8argc = argc; | 1558 | *u8argc = argc; |
1569 | return GNUNET_OK; | 1559 | return GNUNET_OK; |
@@ -1595,35 +1585,22 @@ parse_port_policy (const char *port_policy, | |||
1595 | pp->negate_portrange = GNUNET_YES; | 1585 | pp->negate_portrange = GNUNET_YES; |
1596 | pos++; | 1586 | pos++; |
1597 | } | 1587 | } |
1598 | if (2 == sscanf (pos, | 1588 | if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol)) |
1599 | "%u-%u%1s", | ||
1600 | &s, | ||
1601 | &e, | ||
1602 | eol)) | ||
1603 | { | 1589 | { |
1604 | if ( (0 == s) || | 1590 | if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF)) |
1605 | (s > 0xFFFF) || | ||
1606 | (e < s) || | ||
1607 | (e > 0xFFFF) ) | ||
1608 | { | 1591 | { |
1609 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1592 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n")); |
1610 | _("Port not in range\n")); | ||
1611 | return GNUNET_SYSERR; | 1593 | return GNUNET_SYSERR; |
1612 | } | 1594 | } |
1613 | pp->start_port = (uint16_t) s; | 1595 | pp->start_port = (uint16_t) s; |
1614 | pp->end_port = (uint16_t) e; | 1596 | pp->end_port = (uint16_t) e; |
1615 | return GNUNET_OK; | 1597 | return GNUNET_OK; |
1616 | } | 1598 | } |
1617 | if (1 == sscanf (pos, | 1599 | if (1 == sscanf (pos, "%u%1s", &s, eol)) |
1618 | "%u%1s", | ||
1619 | &s, | ||
1620 | eol)) | ||
1621 | { | 1600 | { |
1622 | if ( (0 == s) || | 1601 | if ((0 == s) || (s > 0xFFFF)) |
1623 | (s > 0xFFFF) ) | ||
1624 | { | 1602 | { |
1625 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1603 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n")); |
1626 | _("Port not in range\n")); | ||
1627 | return GNUNET_SYSERR; | 1604 | return GNUNET_SYSERR; |
1628 | } | 1605 | } |
1629 | 1606 | ||
@@ -1632,7 +1609,7 @@ parse_port_policy (const char *port_policy, | |||
1632 | return GNUNET_OK; | 1609 | return GNUNET_OK; |
1633 | } | 1610 | } |
1634 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | 1611 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, |
1635 | _("Malformed port policy `%s'\n"), | 1612 | _ ("Malformed port policy `%s'\n"), |
1636 | port_policy); | 1613 | port_policy); |
1637 | return GNUNET_SYSERR; | 1614 | return GNUNET_SYSERR; |
1638 | } | 1615 | } |
@@ -1666,7 +1643,7 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | |||
1666 | int end; | 1643 | int end; |
1667 | char *routeList; | 1644 | char *routeList; |
1668 | char dummy[2]; | 1645 | char dummy[2]; |
1669 | 1646 | ||
1670 | if (NULL == routeListX) | 1647 | if (NULL == routeListX) |
1671 | return NULL; | 1648 | return NULL; |
1672 | len = strlen (routeListX); | 1649 | len = strlen (routeListX); |
@@ -1677,14 +1654,14 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | |||
1677 | for (i = 0; i < len; i++) | 1654 | for (i = 0; i < len; i++) |
1678 | if (routeList[i] == ';') | 1655 | if (routeList[i] == ';') |
1679 | count++; | 1656 | count++; |
1680 | result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv4NetworkPolicy) * (count + 1)); | 1657 | result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv4NetworkPolicy) * |
1658 | (count + 1)); | ||
1681 | i = 0; | 1659 | i = 0; |
1682 | pos = 0; | 1660 | pos = 0; |
1683 | while (i < count) | 1661 | while (i < count) |
1684 | { | 1662 | { |
1685 | for (colon = pos; ':' != routeList[colon]; colon++) | 1663 | for (colon = pos; ':' != routeList[colon]; colon++) |
1686 | if ( (';' == routeList[colon]) || | 1664 | if ((';' == routeList[colon]) || ('\0' == routeList[colon])) |
1687 | ('\0' == routeList[colon]) ) | ||
1688 | break; | 1665 | break; |
1689 | for (end = colon; ';' != routeList[end]; end++) | 1666 | for (end = colon; ';' != routeList[end]; end++) |
1690 | if ('\0' == routeList[end]) | 1667 | if ('\0' == routeList[end]) |
@@ -1695,76 +1672,70 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | |||
1695 | if (':' == routeList[colon]) | 1672 | if (':' == routeList[colon]) |
1696 | { | 1673 | { |
1697 | routeList[colon] = '\0'; | 1674 | routeList[colon] = '\0'; |
1698 | if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], | 1675 | if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp)) |
1699 | &result[i].pp)) | ||
1700 | break; | 1676 | break; |
1701 | } | 1677 | } |
1702 | cnt = | 1678 | cnt = sscanf (&routeList[pos], |
1703 | SSCANF (&routeList[pos], | 1679 | "%u.%u.%u.%u/%u.%u.%u.%u%1s", |
1704 | "%u.%u.%u.%u/%u.%u.%u.%u%1s", | 1680 | &temps[0], |
1705 | &temps[0], | 1681 | &temps[1], |
1706 | &temps[1], | 1682 | &temps[2], |
1707 | &temps[2], | 1683 | &temps[3], |
1708 | &temps[3], | 1684 | &temps[4], |
1709 | &temps[4], | 1685 | &temps[5], |
1710 | &temps[5], | 1686 | &temps[6], |
1711 | &temps[6], | 1687 | &temps[7], |
1712 | &temps[7], | 1688 | dummy); |
1713 | dummy); | ||
1714 | if (8 == cnt) | 1689 | if (8 == cnt) |
1715 | { | 1690 | { |
1716 | for (j = 0; j < 8; j++) | 1691 | for (j = 0; j < 8; j++) |
1717 | if (temps[j] > 0xFF) | 1692 | if (temps[j] > 0xFF) |
1718 | { | 1693 | { |
1719 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1694 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1720 | _("Invalid format for IP: `%s'\n"), | 1695 | _ ("Invalid format for IP: `%s'\n"), |
1721 | &routeList[pos]); | 1696 | &routeList[pos]); |
1722 | GNUNET_free (result); | 1697 | GNUNET_free (result); |
1723 | GNUNET_free (routeList); | 1698 | GNUNET_free (routeList); |
1724 | return NULL; | 1699 | return NULL; |
1725 | } | 1700 | } |
1726 | result[i].network.s_addr = | 1701 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + |
1727 | htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + | 1702 | (temps[2] << 8) + temps[3]); |
1728 | temps[3]); | 1703 | result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16) + |
1729 | result[i].netmask.s_addr = | 1704 | (temps[6] << 8) + temps[7]); |
1730 | htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) + | ||
1731 | temps[7]); | ||
1732 | pos = end + 1; | 1705 | pos = end + 1; |
1733 | i++; | 1706 | i++; |
1734 | continue; | 1707 | continue; |
1735 | } | 1708 | } |
1736 | /* try second notation */ | 1709 | /* try second notation */ |
1737 | cnt = | 1710 | cnt = sscanf (&routeList[pos], |
1738 | SSCANF (&routeList[pos], | 1711 | "%u.%u.%u.%u/%u%1s", |
1739 | "%u.%u.%u.%u/%u%1s", | 1712 | &temps[0], |
1740 | &temps[0], | 1713 | &temps[1], |
1741 | &temps[1], | 1714 | &temps[2], |
1742 | &temps[2], | 1715 | &temps[3], |
1743 | &temps[3], | 1716 | &slash, |
1744 | &slash, | 1717 | dummy); |
1745 | dummy); | ||
1746 | if (5 == cnt) | 1718 | if (5 == cnt) |
1747 | { | 1719 | { |
1748 | for (j = 0; j < 4; j++) | 1720 | for (j = 0; j < 4; j++) |
1749 | if (temps[j] > 0xFF) | 1721 | if (temps[j] > 0xFF) |
1750 | { | 1722 | { |
1751 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1723 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1752 | _("Invalid format for IP: `%s'\n"), | 1724 | _ ("Invalid format for IP: `%s'\n"), |
1753 | &routeList[pos]); | 1725 | &routeList[pos]); |
1754 | GNUNET_free (result); | 1726 | GNUNET_free (result); |
1755 | GNUNET_free (routeList); | 1727 | GNUNET_free (routeList); |
1756 | return NULL; | 1728 | return NULL; |
1757 | } | 1729 | } |
1758 | result[i].network.s_addr = | 1730 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + |
1759 | htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + | 1731 | (temps[2] << 8) + temps[3]); |
1760 | temps[3]); | ||
1761 | if ((slash <= 32) && (slash >= 0)) | 1732 | if ((slash <= 32) && (slash >= 0)) |
1762 | { | 1733 | { |
1763 | result[i].netmask.s_addr = 0; | 1734 | result[i].netmask.s_addr = 0; |
1764 | while (slash > 0) | 1735 | while (slash > 0) |
1765 | { | 1736 | { |
1766 | result[i].netmask.s_addr = | 1737 | result[i].netmask.s_addr = |
1767 | (result[i].netmask.s_addr >> 1) + 0x80000000; | 1738 | (result[i].netmask.s_addr >> 1) + 0x80000000; |
1768 | slash--; | 1739 | slash--; |
1769 | } | 1740 | } |
1770 | result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); | 1741 | result[i].netmask.s_addr = htonl (result[i].netmask.s_addr); |
@@ -1775,38 +1746,36 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | |||
1775 | else | 1746 | else |
1776 | { | 1747 | { |
1777 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1748 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1778 | _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), | 1749 | _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."), |
1779 | slash); | 1750 | slash); |
1780 | GNUNET_free (result); | 1751 | GNUNET_free (result); |
1781 | GNUNET_free (routeList); | 1752 | GNUNET_free (routeList); |
1782 | return NULL; /* error */ | 1753 | return NULL; /* error */ |
1783 | } | 1754 | } |
1784 | } | 1755 | } |
1785 | /* try third notation */ | 1756 | /* try third notation */ |
1786 | slash = 32; | 1757 | slash = 32; |
1787 | cnt = | 1758 | cnt = sscanf (&routeList[pos], |
1788 | SSCANF (&routeList[pos], | 1759 | "%u.%u.%u.%u%1s", |
1789 | "%u.%u.%u.%u%1s", | 1760 | &temps[0], |
1790 | &temps[0], | 1761 | &temps[1], |
1791 | &temps[1], | 1762 | &temps[2], |
1792 | &temps[2], | 1763 | &temps[3], |
1793 | &temps[3], | 1764 | dummy); |
1794 | dummy); | ||
1795 | if (4 == cnt) | 1765 | if (4 == cnt) |
1796 | { | 1766 | { |
1797 | for (j = 0; j < 4; j++) | 1767 | for (j = 0; j < 4; j++) |
1798 | if (temps[j] > 0xFF) | 1768 | if (temps[j] > 0xFF) |
1799 | { | 1769 | { |
1800 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1770 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1801 | _("Invalid format for IP: `%s'\n"), | 1771 | _ ("Invalid format for IP: `%s'\n"), |
1802 | &routeList[pos]); | 1772 | &routeList[pos]); |
1803 | GNUNET_free (result); | 1773 | GNUNET_free (result); |
1804 | GNUNET_free (routeList); | 1774 | GNUNET_free (routeList); |
1805 | return NULL; | 1775 | return NULL; |
1806 | } | 1776 | } |
1807 | result[i].network.s_addr = | 1777 | result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) + |
1808 | htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + | 1778 | (temps[2] << 8) + temps[3]); |
1809 | temps[3]); | ||
1810 | result[i].netmask.s_addr = 0; | 1779 | result[i].netmask.s_addr = 0; |
1811 | while (slash > 0) | 1780 | while (slash > 0) |
1812 | { | 1781 | { |
@@ -1819,23 +1788,23 @@ GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX) | |||
1819 | continue; | 1788 | continue; |
1820 | } | 1789 | } |
1821 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1790 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1822 | _("Invalid format for IP: `%s'\n"), | 1791 | _ ("Invalid format for IP: `%s'\n"), |
1823 | &routeList[pos]); | 1792 | &routeList[pos]); |
1824 | GNUNET_free (result); | 1793 | GNUNET_free (result); |
1825 | GNUNET_free (routeList); | 1794 | GNUNET_free (routeList); |
1826 | return NULL; /* error */ | 1795 | return NULL; /* error */ |
1827 | } | 1796 | } |
1828 | if (pos < strlen (routeList)) | 1797 | if (pos < strlen (routeList)) |
1829 | { | 1798 | { |
1830 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1799 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1831 | _("Invalid format: `%s'\n"), | 1800 | _ ("Invalid format: `%s'\n"), |
1832 | &routeListX[pos]); | 1801 | &routeListX[pos]); |
1833 | GNUNET_free (result); | 1802 | GNUNET_free (result); |
1834 | GNUNET_free (routeList); | 1803 | GNUNET_free (routeList); |
1835 | return NULL; /* oops */ | 1804 | return NULL; /* oops */ |
1836 | } | 1805 | } |
1837 | GNUNET_free (routeList); | 1806 | GNUNET_free (routeList); |
1838 | return result; /* ok */ | 1807 | return result; /* ok */ |
1839 | } | 1808 | } |
1840 | 1809 | ||
1841 | 1810 | ||
@@ -1867,7 +1836,7 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1867 | int save; | 1836 | int save; |
1868 | int colon; | 1837 | int colon; |
1869 | char dummy[2]; | 1838 | char dummy[2]; |
1870 | 1839 | ||
1871 | if (NULL == routeListX) | 1840 | if (NULL == routeListX) |
1872 | return NULL; | 1841 | return NULL; |
1873 | len = strlen (routeListX); | 1842 | len = strlen (routeListX); |
@@ -1881,13 +1850,14 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1881 | if (';' != routeList[len - 1]) | 1850 | if (';' != routeList[len - 1]) |
1882 | { | 1851 | { |
1883 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1852 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1884 | _("Invalid network notation (does not end with ';': `%s')\n"), | 1853 | _ ("Invalid network notation (does not end with ';': `%s')\n"), |
1885 | routeList); | 1854 | routeList); |
1886 | GNUNET_free (routeList); | 1855 | GNUNET_free (routeList); |
1887 | return NULL; | 1856 | return NULL; |
1888 | } | 1857 | } |
1889 | 1858 | ||
1890 | result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv6NetworkPolicy) * (count + 1)); | 1859 | result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv6NetworkPolicy) * |
1860 | (count + 1)); | ||
1891 | i = 0; | 1861 | i = 0; |
1892 | pos = 0; | 1862 | pos = 0; |
1893 | while (i < count) | 1863 | while (i < count) |
@@ -1901,9 +1871,7 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1901 | 1871 | ||
1902 | if (slash < start) | 1872 | if (slash < start) |
1903 | { | 1873 | { |
1904 | memset (&result[i].netmask, | 1874 | memset (&result[i].netmask, 0xFF, sizeof (struct in6_addr)); |
1905 | 0xFF, | ||
1906 | sizeof (struct in6_addr)); | ||
1907 | slash = pos; | 1875 | slash = pos; |
1908 | } | 1876 | } |
1909 | else | 1877 | else |
@@ -1915,8 +1883,8 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1915 | if (':' == routeList[colon]) | 1883 | if (':' == routeList[colon]) |
1916 | { | 1884 | { |
1917 | routeList[colon] = '\0'; | 1885 | routeList[colon] = '\0'; |
1918 | if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], | 1886 | if (GNUNET_OK != |
1919 | &result[i].pp)) | 1887 | parse_port_policy (&routeList[colon + 1], &result[i].pp)) |
1920 | { | 1888 | { |
1921 | GNUNET_free (result); | 1889 | GNUNET_free (result); |
1922 | GNUNET_free (routeList); | 1890 | GNUNET_free (routeList); |
@@ -1927,15 +1895,12 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1927 | if (ret <= 0) | 1895 | if (ret <= 0) |
1928 | { | 1896 | { |
1929 | save = errno; | 1897 | save = errno; |
1930 | if ( (1 != SSCANF (&routeList[slash + 1], | 1898 | if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) || |
1931 | "%u%1s", | 1899 | (bits > 128)) |
1932 | &bits, | ||
1933 | dummy)) || | ||
1934 | (bits > 128) ) | ||
1935 | { | 1900 | { |
1936 | if (0 == ret) | 1901 | if (0 == ret) |
1937 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1902 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1938 | _("Wrong format `%s' for netmask\n"), | 1903 | _ ("Wrong format `%s' for netmask\n"), |
1939 | &routeList[slash + 1]); | 1904 | &routeList[slash + 1]); |
1940 | else | 1905 | else |
1941 | { | 1906 | { |
@@ -1955,7 +1920,7 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1955 | while (bits > 0) | 1920 | while (bits > 0) |
1956 | { | 1921 | { |
1957 | result[i].netmask.s6_addr[off] = | 1922 | result[i].netmask.s6_addr[off] = |
1958 | (result[i].netmask.s6_addr[off] >> 1) + 0x80; | 1923 | (result[i].netmask.s6_addr[off] >> 1) + 0x80; |
1959 | bits--; | 1924 | bits--; |
1960 | } | 1925 | } |
1961 | } | 1926 | } |
@@ -1966,11 +1931,10 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1966 | { | 1931 | { |
1967 | if (0 == ret) | 1932 | if (0 == ret) |
1968 | LOG (GNUNET_ERROR_TYPE_WARNING, | 1933 | LOG (GNUNET_ERROR_TYPE_WARNING, |
1969 | _("Wrong format `%s' for network\n"), | 1934 | _ ("Wrong format `%s' for network\n"), |
1970 | &routeList[slash + 1]); | 1935 | &routeList[slash + 1]); |
1971 | else | 1936 | else |
1972 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, | 1937 | LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton"); |
1973 | "inet_pton"); | ||
1974 | GNUNET_free (result); | 1938 | GNUNET_free (result); |
1975 | GNUNET_free (routeList); | 1939 | GNUNET_free (routeList); |
1976 | return NULL; | 1940 | return NULL; |
@@ -1983,12 +1947,12 @@ GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX) | |||
1983 | } | 1947 | } |
1984 | 1948 | ||
1985 | 1949 | ||
1986 | |||
1987 | /** ******************** Base64 encoding ***********/ | 1950 | /** ******************** Base64 encoding ***********/ |
1988 | 1951 | ||
1989 | #define FILLCHAR '=' | 1952 | #define FILLCHAR '=' |
1990 | static char *cvt = | 1953 | static char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
1991 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; | 1954 | "abcdefghijklmnopqrstuvwxyz" |
1955 | "0123456789+/"; | ||
1992 | 1956 | ||
1993 | 1957 | ||
1994 | /** | 1958 | /** |
@@ -2001,9 +1965,7 @@ static char *cvt = | |||
2001 | * @return the size of the output | 1965 | * @return the size of the output |
2002 | */ | 1966 | */ |
2003 | size_t | 1967 | size_t |
2004 | GNUNET_STRINGS_base64_encode (const void *in, | 1968 | GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output) |
2005 | size_t len, | ||
2006 | char **output) | ||
2007 | { | 1969 | { |
2008 | const char *data = in; | 1970 | const char *data = in; |
2009 | size_t ret; | 1971 | size_t ret; |
@@ -2047,11 +2009,14 @@ GNUNET_STRINGS_base64_encode (const void *in, | |||
2047 | return ret; | 2009 | return ret; |
2048 | } | 2010 | } |
2049 | 2011 | ||
2050 | #define cvtfind(a)( (((a) >= 'A')&&((a) <= 'Z'))? (a)-'A'\ | 2012 | #define cvtfind(a) \ |
2051 | :(((a)>='a')&&((a)<='z')) ? (a)-'a'+26\ | 2013 | ((((a) >= 'A') && ((a) <= 'Z')) \ |
2052 | :(((a)>='0')&&((a)<='9')) ? (a)-'0'+52\ | 2014 | ? (a) - 'A' \ |
2053 | :((a) == '+') ? 62\ | 2015 | : (((a) >= 'a') && ((a) <= 'z')) \ |
2054 | :((a) == '/') ? 63 : -1) | 2016 | ? (a) - 'a' + 26 \ |
2017 | : (((a) >= '0') && ((a) <= '9')) \ | ||
2018 | ? (a) - '0' + 52 \ | ||
2019 | : ((a) == '+') ? 62 : ((a) == '/') ? 63 : -1) | ||
2055 | 2020 | ||
2056 | 2021 | ||
2057 | /** | 2022 | /** |
@@ -2064,18 +2029,20 @@ GNUNET_STRINGS_base64_encode (const void *in, | |||
2064 | * @return the size of the output | 2029 | * @return the size of the output |
2065 | */ | 2030 | */ |
2066 | size_t | 2031 | size_t |
2067 | GNUNET_STRINGS_base64_decode (const char *data, | 2032 | GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **out) |
2068 | size_t len, | ||
2069 | void **out) | ||
2070 | { | 2033 | { |
2071 | char *output; | 2034 | char *output; |
2072 | size_t ret = 0; | 2035 | size_t ret = 0; |
2073 | 2036 | ||
2074 | #define CHECK_CRLF while (data[i] == '\r' || data[i] == '\n') {\ | 2037 | #define CHECK_CRLF \ |
2075 | GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, "ignoring CR/LF\n"); \ | 2038 | while (data[i] == '\r' || data[i] == '\n') \ |
2076 | i++; \ | 2039 | { \ |
2077 | if (i >= len) goto END; \ | 2040 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \ |
2078 | } | 2041 | "ignoring CR/LF\n"); \ |
2042 | i++; \ | ||
2043 | if (i >= len) \ | ||
2044 | goto END; \ | ||
2045 | } | ||
2079 | 2046 | ||
2080 | output = GNUNET_malloc ((len * 3 / 4) + 8); | 2047 | output = GNUNET_malloc ((len * 3 / 4) + 8); |
2081 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 2048 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |