GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
strings.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2005-2017 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
27#include "platform.h"
28#if HAVE_ICONV
29#include <iconv.h>
30#endif
31#include "gnunet_util_lib.h"
32#include <unicase.h>
33#include <unistr.h>
34#include <uniconv.h>
35
36#define LOG(kind, ...) GNUNET_log_from (kind, "util-strings", __VA_ARGS__)
37
38#define LOG_STRERROR(kind, syscall) \
39 GNUNET_log_from_strerror (kind, "util-strings", syscall)
40
41
42size_t
44 size_t size,
45 unsigned int count, ...)
46{
47 size_t needed;
48 va_list ap;
49
50 needed = 0;
51 va_start (ap, count);
52 while (count > 0)
53 {
54 const char *s = va_arg (ap, const char *);
55 size_t slen = strlen (s) + 1;
56
57 GNUNET_assert (slen <= size - needed);
58 if (NULL != buffer)
59 GNUNET_memcpy (&buffer[needed],
60 s,
61 slen);
62 needed += slen;
63 count--;
64 }
65 va_end (ap);
66 return needed;
67}
68
69
70unsigned int
72 size_t size,
73 unsigned int count,
74 ...)
75{
76 unsigned int start;
77 unsigned int needed;
78 const char **r;
79 va_list ap;
80
81 needed = 0;
82 va_start (ap, count);
83 while (count > 0)
84 {
85 r = va_arg (ap, const char **);
86
87 start = needed;
88 while ((needed < size) && (buffer[needed] != '\0'))
89 needed++;
90 if (needed == size)
91 {
92 va_end (ap);
93 return 0; /* error */
94 }
95 *r = &buffer[start];
96 needed++; /* skip 0-termination */
97 count--;
98 }
99 va_end (ap);
100 return needed;
101}
102
103
104char *
106{
107 const char *unit = /* size unit */ "b";
108 char *ret;
109
110 if (size > 5 * 1024)
111 {
112 size = size / 1024;
113 unit = "KiB";
114 if (size > 5 * 1024)
115 {
116 size = size / 1024;
117 unit = "MiB";
118 if (size > 5 * 1024)
119 {
120 size = size / 1024;
121 unit = "GiB";
122 if (size > 5 * 1024)
123 {
124 size = size / 1024;
125 unit = "TiB";
126 }
127 }
128 }
129 }
130 ret = GNUNET_malloc (32);
131 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
132 return ret;
133}
134
135
136size_t
137GNUNET_strlcpy (char *dst,
138 const char *src,
139 size_t n)
140{
141 size_t slen;
142
143 GNUNET_assert (0 != n);
144 slen = strnlen (src, n - 1);
145 memcpy (dst, src, slen);
146 dst[slen] = '\0';
147 return slen;
148}
149
150
155{
159 const char *name;
160
164 unsigned long long value;
165};
166
167
180convert_with_table (const char *input,
181 const struct ConversionTable *table,
182 unsigned long long *output)
183{
184 unsigned long long ret;
185 char *in;
186 const char *tok;
187 unsigned long long last;
188 unsigned int i;
189 char *sptr;
190
191 ret = 0;
192 last = 0;
193 in = GNUNET_strdup (input);
194 for (tok = strtok_r (in, " ", &sptr);
195 tok != NULL;
196 tok = strtok_r (NULL, " ", &sptr))
197 {
198 do
199 {
200 i = 0;
201 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
202 i++;
203 if (table[i].name != NULL)
204 {
205 last *= table[i].value;
206 break; /* next tok */
207 }
208 else
209 {
210 char *endptr;
211 ret += last;
212 errno = 0;
213 last = strtoull (tok, &endptr, 10);
214 if ((0 != errno) || (endptr == tok))
215 {
216 GNUNET_free (in);
217 return GNUNET_SYSERR; /* expected number */
218 }
219 if ('\0' == endptr[0])
220 break; /* next tok */
221 else
222 tok = endptr; /* and re-check (handles times like "10s") */
223 }
224 }
225 while (GNUNET_YES);
226 }
227 ret += last;
228 *output = ret;
229 GNUNET_free (in);
230 return GNUNET_OK;
231}
232
233
236 unsigned long long *size)
237{
238 static const struct ConversionTable table[] =
239 { { "B", 1 },
240 { "KiB", 1024 },
241 { "kB", 1000 },
242 { "MiB", 1024 * 1024 },
243 { "MB", 1000 * 1000 },
244 { "GiB", 1024 * 1024 * 1024 },
245 { "GB", 1000 * 1000 * 1000 },
246 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
247 { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
248 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
249 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
250 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
251 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
252 { NULL, 0 } };
253
254 return convert_with_table (fancy_size, table, size);
255}
256
257
260 struct GNUNET_TIME_Relative *rtime)
261{
262 static const struct ConversionTable table[] =
263 { { "us", 1 },
264 { "ms", 1000 },
265 { "s", 1000 * 1000LL },
266 { "second", 1000 * 1000LL },
267 { "seconds", 1000 * 1000LL },
268 { "\"", 1000 * 1000LL },
269 { "m", 60 * 1000 * 1000LL },
270 { "min", 60 * 1000 * 1000LL },
271 { "minute", 60 * 1000 * 1000LL },
272 { "minutes", 60 * 1000 * 1000LL },
273 { "'", 60 * 1000 * 1000LL },
274 { "h", 60 * 60 * 1000 * 1000LL },
275 { "hour", 60 * 60 * 1000 * 1000LL },
276 { "hours", 60 * 60 * 1000 * 1000LL },
277 { "d", 24 * 60 * 60 * 1000LL * 1000LL },
278 { "day", 24 * 60 * 60 * 1000LL * 1000LL },
279 { "days", 24 * 60 * 60 * 1000LL * 1000LL },
280 { "w", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
281 { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
282 { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
283 { "year", 31536000000000LL /* year */ },
284 { "years", 31536000000000LL /* year */ },
285 { "y", 31536000000000LL /* year */ },
286 { "a", 31536000000000LL /* year */ },
287 { NULL, 0 } };
288 int ret;
289 unsigned long long val;
290
291 if (0 == strcasecmp ("forever", fancy_time))
292 {
294 return GNUNET_OK;
295 }
296 ret = convert_with_table (fancy_time, table, &val);
297 rtime->rel_value_us = (uint64_t) val;
298 return ret;
299}
300
301
304 struct GNUNET_TIME_Absolute *atime)
305{
306 struct tm tv;
307 time_t t;
308 const char *eos;
309 unsigned long long l;
310 char dummy;
311
312 if (0 == strcasecmp ("end of time",
313 fancy_time))
314 {
316 return GNUNET_OK;
317 }
318 if ( (1 == sscanf (fancy_time,
319 "%llu%c",
320 &l,
321 &dummy)) &&
322 (l > 9999) )
323 {
324 /* Time given in seconds after epoch, and not a year */
325 *atime = GNUNET_TIME_absolute_from_s (l);
326 return GNUNET_OK;
327 }
328 eos = &fancy_time[strlen (fancy_time)];
329 memset (&tv, 0, sizeof(tv));
330 if ((eos != strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) &&
331 (eos != strptime (fancy_time, "%c", &tv)) &&
332 (eos != strptime (fancy_time, "%Ec", &tv)) &&
333 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) &&
334 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) &&
335 (eos != strptime (fancy_time, "%x", &tv)) &&
336 (eos != strptime (fancy_time, "%Ex", &tv)) &&
337 (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) &&
338 (eos != strptime (fancy_time, "%Y-%m", &tv)) &&
339 (eos != strptime (fancy_time, "%Y", &tv)))
340 return GNUNET_SYSERR;
341 t = mktime (&tv);
342 if (((time_t) -1) == t)
343 {
345 "mktime");
346 return GNUNET_SYSERR;
347 }
349 return GNUNET_OK;
350}
351
352
355 struct GNUNET_TIME_Timestamp *atime)
356{
358
359 if (GNUNET_OK !=
361 &atime->abs_time)))
362 {
363 return ret;
364 }
366 {
367 atime->abs_time = GNUNET_TIME_UNIT_FOREVER_TS.abs_time;
368 }
369 return GNUNET_OK;
370}
371
372
373char *
374GNUNET_STRINGS_conv (const char *input,
375 size_t len,
376 const char *input_charset,
377 const char *output_charset)
378{
379 char *ret;
380 uint8_t *u8_string;
381 char *encoded_string;
382 size_t u8_string_length;
383 size_t encoded_string_length;
384
385 u8_string = u8_conv_from_encoding (input_charset,
386 iconveh_error,
387 input,
388 len,
389 NULL,
390 NULL,
391 &u8_string_length);
392 if (NULL == u8_string)
393 {
394 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
395 goto fail;
396 }
397 if (0 == strcmp (output_charset, "UTF-8"))
398 {
399 ret = GNUNET_malloc (u8_string_length + 1);
400 GNUNET_memcpy (ret, u8_string, u8_string_length);
401 ret[u8_string_length] = '\0';
402 free (u8_string);
403 return ret;
404 }
405 encoded_string = u8_conv_to_encoding (output_charset,
406 iconveh_error,
407 u8_string,
408 u8_string_length,
409 NULL,
410 NULL,
411 &encoded_string_length);
412 free (u8_string);
413 if (NULL == encoded_string)
414 {
415 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
416 goto fail;
417 }
418 ret = GNUNET_malloc (encoded_string_length + 1);
419 GNUNET_memcpy (ret, encoded_string, encoded_string_length);
420 ret[encoded_string_length] = '\0';
421 free (encoded_string);
422 return ret;
423fail:
425 _ ("Character sets requested were `%s'->`%s'\n"),
426 "UTF-8",
427 output_charset);
428 ret = GNUNET_malloc (len + 1);
429 GNUNET_memcpy (ret, input, len);
430 ret[len] = '\0';
431 return ret;
432}
433
434
435char *
436GNUNET_STRINGS_to_utf8 (const char *input,
437 size_t len,
438 const char *charset)
439{
440 return GNUNET_STRINGS_conv (input,
441 len,
442 charset,
443 "UTF-8");
444}
445
446
447char *
448GNUNET_STRINGS_from_utf8 (const char *input,
449 size_t len,
450 const char *charset)
451{
452 return GNUNET_STRINGS_conv (input,
453 len,
454 "UTF-8",
455 charset);
456}
457
458
459char *
461{
462 uint8_t *tmp;
463 size_t len;
464 char *output;
465 tmp = u8_normalize (UNINORM_NFC,
466 (uint8_t *) input,
467 strlen ((char*) input),
468 NULL,
469 &len);
470 if (NULL == tmp)
471 return NULL;
472 output = GNUNET_malloc (len + 1);
473 GNUNET_memcpy (output, tmp, len);
474 output[len] = '\0';
475 free (tmp);
476 return output;
477}
478
479
480char *
482{
483 uint8_t *tmp_in;
484 size_t len;
485 char *ret;
486
487 tmp_in = u8_tolower ((uint8_t *) input,
488 strlen ((char *) input),
489 NULL,
490 UNINORM_NFD,
491 NULL,
492 &len);
493
494 if (NULL == tmp_in)
495 return NULL;
496 ret = GNUNET_malloc (len + 1);
497 memcpy (ret,
498 tmp_in,
499 len);
500 free (tmp_in);
501 return ret;
502}
503
504
505char *
507{
508 uint8_t *tmp_in;
509 size_t len;
510 char *ret;
511
512 tmp_in = u8_toupper ((uint8_t *) input,
513 strlen ((char *) input),
514 NULL,
515 UNINORM_NFD,
516 NULL,
517 &len);
518
519 if (NULL == tmp_in)
520 return NULL;
521 ret = GNUNET_malloc (len + 1);
522 memcpy (ret,
523 tmp_in,
524 len);
525 free (tmp_in);
526 return ret;
527}
528
529
530char *
532{
533 char *buffer;
534 size_t len;
535 char *fm;
536 const char *fil_ptr;
537
538 if (NULL == fil)
539 return NULL;
540
541 if (fil[0] == DIR_SEPARATOR)
542 /* absolute path, just copy */
543 return GNUNET_strdup (fil);
544 if (fil[0] == '~')
545 {
546 fm = getenv ("HOME");
547 if (fm == NULL)
548 {
550 _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
551 return NULL;
552 }
553 fm = GNUNET_strdup (fm);
554 /* do not copy '~' */
555 fil_ptr = fil + 1;
556
557 /* skip over dir separator to be consistent */
558 if (fil_ptr[0] == DIR_SEPARATOR)
559 fil_ptr++;
560 }
561 else
562 {
563 /* relative path */
564 fil_ptr = fil;
565 len = 512;
566 fm = NULL;
567 while (1)
568 {
569 buffer = GNUNET_malloc (len);
570 if (NULL != getcwd (buffer,
571 len))
572 {
573 fm = buffer;
574 break;
575 }
576 if ( (errno == ERANGE) &&
577 (len < 1024 * 1024 * 4) )
578 {
579 len *= 2;
580 GNUNET_free (buffer);
581 continue;
582 }
583 GNUNET_free (buffer);
584 break;
585 }
586 if (NULL == fm)
587 {
589 "getcwd");
590 buffer = getenv ("PWD"); /* alternative */
591 if (buffer != NULL)
592 fm = GNUNET_strdup (buffer);
593 }
594 if (NULL == fm)
595 fm = GNUNET_strdup ("./"); /* give up */
596 }
597 GNUNET_asprintf (&buffer,
598 "%s%s%s",
599 fm,
600 (fm[strlen (fm) - 1] == DIR_SEPARATOR)
601 ? ""
603 fil_ptr);
604 GNUNET_free (fm);
605 return buffer;
606}
607
608
609const char *
611 int do_round)
612{
613 static GNUNET_THREAD_LOCAL char buf[128];
614 const char *unit = /* time unit */ "µs";
615 uint64_t dval = delta.rel_value_us;
616
618 return "forever";
619 if (0 == delta.rel_value_us)
620 return "0 ms";
621 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
622 {
623 dval = dval / 1000;
624 unit = /* time unit */ "ms";
625 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
626 {
627 dval = dval / 1000;
628 unit = /* time unit */ "s";
629 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
630 {
631 dval = dval / 60;
632 unit = /* time unit */ "m";
633 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
634 {
635 dval = dval / 60;
636 unit = /* time unit */ "h";
637 if (((GNUNET_YES == do_round) && (dval > 5 * 24)) ||
638 (0 == (dval % 24)))
639 {
640 dval = dval / 24;
641 if (1 == dval)
642 unit = /* time unit */ "day";
643 else
644 unit = /* time unit */ "days";
645 }
646 }
647 }
648 }
649 }
650 GNUNET_snprintf (buf, sizeof(buf), "%llu %s",
651 (unsigned long long) dval, unit);
652 return buf;
653}
654
655
656const char *
658{
659 struct GNUNET_TIME_Absolute av;
660
661 if (t.abs_time.abs_value_us == GNUNET_TIME_UNIT_FOREVER_TS.abs_time.
664 ;
665 av.abs_value_us = t.abs_time.abs_value_us;
667}
668
669
670const char *
672{
673 static GNUNET_THREAD_LOCAL char buf[255];
674 time_t tt;
675 struct tm *tp;
676
678 return "end of time";
679 tt = t.abs_value_us / 1000LL / 1000LL;
680 tp = localtime (&tt);
681 /* This is hacky, but i don't know a way to detect libc character encoding.
682 * Just expect utf8 from glibc these days.
683 * As for msvcrt, use the wide variant, which always returns utf16
684 * (otherwise we'd have to detect current codepage or use W32API character
685 * set conversion routines to convert to UTF8).
686 */
687 strftime (buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp);
688
689 return buf;
690}
691
692
693const char *
695{
696 const char *short_fn = filename;
697 const char *ss;
698
699 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
700 short_fn = 1 + ss;
701 return short_fn;
702}
703
704
712static unsigned int
713getValue__ (unsigned char a)
714{
715 unsigned int dec;
716
717 switch (a)
718 {
719 case 'O':
720 case 'o':
721 a = '0';
722 break;
723
724 case 'i':
725 case 'I':
726 case 'l':
727 case 'L':
728 a = '1';
729 break;
730
731 /* also consider U to be V */
732 case 'u':
733 case 'U':
734 a = 'V';
735 break;
736
737 default:
738 break;
739 }
740 if ((a >= '0') && (a <= '9'))
741 return a - '0';
742 if ((a >= 'a') && (a <= 'z'))
743 a = toupper (a);
744 /* return (a - 'a' + 10); */
745 dec = 0;
746 if ((a >= 'A') && (a <= 'Z'))
747 {
748 if ('I' < a)
749 dec++;
750 if ('L' < a)
751 dec++;
752 if ('O' < a)
753 dec++;
754 if ('U' < a)
755 dec++;
756 return(a - 'A' + 10 - dec);
757 }
758 return -1;
759}
760
761
762char *
764 size_t size,
765 char *out,
766 size_t out_size)
767{
771 static const char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
772 unsigned int wpos;
773 unsigned int rpos;
774 unsigned int bits;
775 unsigned int vbit;
776 const unsigned char *udata;
777
778 GNUNET_assert (size < SIZE_MAX / 8 - 4);
779 udata = data;
780 if (out_size < (size * 8 + 4) / 5)
781 {
782 GNUNET_break (0);
783 return NULL;
784 }
785 vbit = 0;
786 wpos = 0;
787 rpos = 0;
788 bits = 0;
789 while ((rpos < size) || (vbit > 0))
790 {
791 if ((rpos < size) && (vbit < 5))
792 {
793 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
794 vbit += 8;
795 }
796 if (vbit < 5)
797 {
798 bits <<= (5 - vbit); /* zero-padding */
799 GNUNET_assert (vbit == ((size * 8) % 5));
800 vbit = 5;
801 }
802 if (wpos >= out_size)
803 {
804 GNUNET_break (0);
805 return NULL;
806 }
807 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
808 vbit -= 5;
809 }
810 GNUNET_assert (0 == vbit);
811 if (wpos < out_size)
812 out[wpos] = '\0';
813 return &out[wpos];
814}
815
816
817char *
819{
820 char *str_buf;
821 size_t len = size * 8;
822 char *end;
823
824 if (len % 5 > 0)
825 len += 5 - len % 5;
826 len /= 5;
827 str_buf = GNUNET_malloc (len + 1);
829 size,
830 str_buf,
831 len);
832 if (NULL == end)
833 {
834 GNUNET_free (str_buf);
835 return NULL;
836 }
837 *end = '\0';
838 return str_buf;
839}
840
841
844 size_t enclen,
845 void *out,
846 size_t out_size)
847{
848 size_t rpos;
849 size_t wpos;
850 unsigned int bits;
851 unsigned int vbit;
852 int ret;
853 int shift;
854 unsigned char *uout;
855 size_t encoded_len;
856
857 if (0 == enclen)
858 {
859 if (0 == out_size)
860 return GNUNET_OK;
861 return GNUNET_SYSERR;
862 }
863 GNUNET_assert (out_size < SIZE_MAX / 8);
864 encoded_len = out_size * 8;
865 uout = out;
866 wpos = out_size;
867 rpos = enclen;
868 if ((encoded_len % 5) > 0)
869 {
870 vbit = encoded_len % 5; /* padding! */
871 shift = 5 - vbit;
872 bits = (ret = getValue__ (enc[--rpos])) >> shift;
873 }
874 else
875 {
876 vbit = 5;
877 shift = 0;
878 bits = (ret = getValue__ (enc[--rpos]));
879 }
880 if ((encoded_len + shift) / 5 != enclen)
881 return GNUNET_SYSERR;
882 if (-1 == ret)
883 return GNUNET_SYSERR;
884 while (wpos > 0)
885 {
886 if (0 == rpos)
887 {
888 GNUNET_break (0);
889 return GNUNET_SYSERR;
890 }
891 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
892 if (-1 == ret)
893 return GNUNET_SYSERR;
894 vbit += 5;
895 if (vbit >= 8)
896 {
897 uout[--wpos] = (unsigned char) bits;
898 bits >>= 8;
899 vbit -= 8;
900 }
901 }
902 if ((0 != rpos) || (0 != vbit))
903 return GNUNET_SYSERR;
904 return GNUNET_OK;
905}
906
907
910 size_t enclen,
911 void **out,
912 size_t *out_size)
913{
914 size_t size;
915 void *data;
916 int res;
917
918 size = (enclen * 5) / 8;
920 {
921 GNUNET_break_op (0);
922 return GNUNET_SYSERR;
923 }
926 enclen,
927 data,
928 size);
929 if ( (0 < size) &&
930 (GNUNET_OK != res) )
931 {
932 size--;
934 enclen,
935 data,
936 size);
937 }
938 if (GNUNET_OK != res)
939 {
940 GNUNET_break_op (0);
942 return GNUNET_SYSERR;
943 }
944 *out = data;
945 *out_size = size;
946 return GNUNET_OK;
947}
948
949
951GNUNET_STRINGS_parse_uri (const char *path,
952 char **scheme_part,
953 const char **path_part)
954{
955 size_t len;
956 size_t i;
957 int end;
958 int pp_state = 0;
959 const char *post_scheme_part = NULL;
960
961 len = strlen (path);
962 for (end = 0, i = 0; ! end && i < len; i++)
963 {
964 switch (pp_state)
965 {
966 case 0:
967 if ((path[i] == ':') && (i > 0))
968 {
969 pp_state += 1;
970 continue;
971 }
972 if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
973 ((path[i] >= 'a') && (path[i] <= 'z') ) ||
974 ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
975 (path[i] == '-') || (path[i] == '.')))
976 end = 1;
977 break;
978
979 case 1:
980 case 2:
981 if (path[i] == '/')
982 {
983 pp_state += 1;
984 continue;
985 }
986 end = 1;
987 break;
988
989 case 3:
990 post_scheme_part = &path[i];
991 end = 1;
992 break;
993
994 default:
995 end = 1;
996 }
997 }
998 if (post_scheme_part == NULL)
999 return GNUNET_NO;
1000 if (scheme_part)
1001 {
1002 *scheme_part = GNUNET_strndup (path,
1003 post_scheme_part - path);
1004 }
1005 if (path_part)
1006 *path_part = post_scheme_part;
1007 return GNUNET_YES;
1008}
1009
1010
1013 int can_be_uri,
1014 int *r_is_uri,
1015 char **r_uri_scheme)
1016{
1017 const char *post_scheme_path;
1018 int is_uri;
1019 char *uri;
1020 /* consider POSIX paths to be absolute too, even on W32,
1021 * as plibc expansion will fix them for us.
1022 */
1023 if (filename[0] == '/')
1024 return GNUNET_YES;
1025 if (can_be_uri)
1026 {
1027 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1028 if (r_is_uri)
1029 *r_is_uri = is_uri;
1030 if (is_uri)
1031 {
1032 if (r_uri_scheme)
1033 *r_uri_scheme = uri;
1034 else
1035 GNUNET_free (uri);
1036
1037 return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1038 GNUNET_NO,
1039 NULL,
1040 NULL);
1041 }
1042 }
1043 else
1044 {
1045 if (r_is_uri)
1046 *r_is_uri = GNUNET_NO;
1047 }
1048
1049 return GNUNET_NO;
1050}
1051
1052
1055 enum GNUNET_STRINGS_FilenameCheck checks)
1056{
1057 struct stat st;
1058
1059 if ((NULL == filename) || (filename[0] == '\0'))
1060 return GNUNET_SYSERR;
1061 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1063 return GNUNET_NO;
1064 if (0 != (checks
1067 {
1068 if (0 != lstat (filename, &st))
1069 {
1070 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1071 return GNUNET_NO;
1072 else
1073 return GNUNET_SYSERR;
1074 }
1075 }
1076 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1077 if (! S_ISDIR (st.st_mode))
1078 return GNUNET_NO;
1079 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1080 if (! S_ISLNK (st.st_mode))
1081 return GNUNET_NO;
1082 return GNUNET_YES;
1083}
1084
1085
1088 size_t addrlen,
1089 struct sockaddr_in6 *r_buf)
1090{
1091 if (addrlen < 6)
1092 return GNUNET_SYSERR;
1093 if (addrlen > 512)
1094 return GNUNET_SYSERR; /* sanity check to protect zbuf allocation,
1095 actual limit is not precise */
1096 {
1097 char zbuf[addrlen + 1];
1098 int ret;
1099 char *port_colon;
1100 unsigned int port;
1101 char dummy[2];
1102
1103 GNUNET_memcpy (zbuf, zt_addr, addrlen);
1104 if ('[' != zbuf[0])
1105 {
1107 _ ("IPv6 address did not start with `['\n"));
1108 return GNUNET_SYSERR;
1109 }
1110 zbuf[addrlen] = '\0';
1111 port_colon = strrchr (zbuf, ':');
1112 if (NULL == port_colon)
1113 {
1115 _ ("IPv6 address did contain ':' to separate port number\n"));
1116 return GNUNET_SYSERR;
1117 }
1118 if (']' != *(port_colon - 1))
1119 {
1120 GNUNET_log (
1122 _ (
1123 "IPv6 address did contain ']' before ':' to separate port number\n"));
1124 return GNUNET_SYSERR;
1125 }
1126 ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1127 if ((1 != ret) || (port > 65535))
1128 {
1129 GNUNET_log (
1131 _ (
1132 "IPv6 address did contain a valid port number after the last ':'\n"));
1133 return GNUNET_SYSERR;
1134 }
1135 *(port_colon - 1) = '\0';
1136 memset (r_buf, 0, sizeof(struct sockaddr_in6));
1137 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1138 if (ret <= 0)
1139 {
1141 _ ("Invalid IPv6 address `%s': %s\n"),
1142 &zbuf[1],
1143 strerror (errno));
1144 return GNUNET_SYSERR;
1145 }
1146 r_buf->sin6_port = htons (port);
1147 r_buf->sin6_family = AF_INET6;
1148#if HAVE_SOCKADDR_IN_SIN_LEN
1149 r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1150#endif
1151 return GNUNET_OK;
1152 }
1153}
1154
1155
1158 size_t addrlen,
1159 struct sockaddr_in *r_buf)
1160{
1161 unsigned int temps[4];
1162 unsigned int port;
1163 unsigned int cnt;
1164 char dummy[2];
1165
1166 if (addrlen < 9)
1167 return GNUNET_SYSERR;
1168 cnt = sscanf (zt_addr,
1169 "%u.%u.%u.%u:%u%1s",
1170 &temps[0],
1171 &temps[1],
1172 &temps[2],
1173 &temps[3],
1174 &port,
1175 dummy);
1176 if (5 != cnt)
1177 return GNUNET_SYSERR;
1178 for (cnt = 0; cnt < 4; cnt++)
1179 if (temps[cnt] > 0xFF)
1180 return GNUNET_SYSERR;
1181 if (port > 65535)
1182 return GNUNET_SYSERR;
1183 r_buf->sin_family = AF_INET;
1184 r_buf->sin_port = htons (port);
1185 r_buf->sin_addr.s_addr =
1186 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1187#if HAVE_SOCKADDR_IN_SIN_LEN
1188 r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1189#endif
1190 return GNUNET_OK;
1191}
1192
1193
1196 uint16_t addrlen,
1197 struct sockaddr_storage *r_buf)
1198{
1199 if (addr[0] == '[')
1200 return GNUNET_STRINGS_to_address_ipv6 (addr,
1201 addrlen,
1202 (struct sockaddr_in6 *) r_buf);
1203 return GNUNET_STRINGS_to_address_ipv4 (addr,
1204 addrlen,
1205 (struct sockaddr_in *) r_buf);
1206}
1207
1208
1209size_t
1211 uint8_t *af,
1212 struct sockaddr **sa)
1213{
1214 *af = AF_UNSPEC;
1215 if ('[' == *addr)
1216 {
1217 /* IPv6 */
1218 *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1219 if (GNUNET_OK !=
1221 strlen (addr),
1222 (struct sockaddr_in6 *) *sa))
1223 {
1224 GNUNET_free (*sa);
1225 *sa = NULL;
1226 return 0;
1227 }
1228 *af = AF_INET6;
1229 return sizeof(struct sockaddr_in6);
1230 }
1231 else
1232 {
1233 /* IPv4 */
1234 *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1235 if (GNUNET_OK !=
1237 strlen (addr),
1238 (struct sockaddr_in *) *sa))
1239 {
1240 GNUNET_free (*sa);
1241 *sa = NULL;
1242 return 0;
1243 }
1244 *af = AF_INET;
1245 return sizeof(struct sockaddr_in);
1246 }
1247}
1248
1249
1259static enum GNUNET_GenericReturnValue
1260parse_port_policy (const char *port_policy,
1261 struct GNUNET_STRINGS_PortPolicy *pp)
1262{
1263 const char *pos;
1264 int s;
1265 int e;
1266 char eol[2];
1267
1268 pos = port_policy;
1269 if ('!' == *pos)
1270 {
1272 pos++;
1273 }
1274 if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol))
1275 {
1276 if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF))
1277 {
1278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1279 return GNUNET_SYSERR;
1280 }
1281 pp->start_port = (uint16_t) s;
1282 pp->end_port = (uint16_t) e;
1283 return GNUNET_OK;
1284 }
1285 if (1 == sscanf (pos, "%u%1s", &s, eol))
1286 {
1287 if ((0 == s) || (s > 0xFFFF))
1288 {
1289 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1290 return GNUNET_SYSERR;
1291 }
1292
1293 pp->start_port = (uint16_t) s;
1294 pp->end_port = (uint16_t) s;
1295 return GNUNET_OK;
1296 }
1298 _ ("Malformed port policy `%s'\n"),
1299 port_policy);
1300 return GNUNET_SYSERR;
1301}
1302
1303
1305GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1306{
1307 size_t count;
1308 size_t len;
1309 size_t pos;
1310 unsigned int temps[8];
1312 char *routeList;
1313
1314 if (NULL == routeListX)
1315 return NULL;
1316 len = strlen (routeListX);
1317 if (0 == len)
1318 return NULL;
1319 routeList = GNUNET_strdup (routeListX);
1320 count = 0;
1321 for (size_t i = 0; i < len; i++)
1322 if (routeList[i] == ';')
1323 count++;
1324 GNUNET_assert (count < SIZE_MAX);
1325 result = GNUNET_new_array (count + 1,
1327 pos = 0;
1328 for (size_t i = 0; i < count; i++)
1329 {
1330 size_t colon;
1331 size_t end;
1332 char dummy;
1333
1334 for (colon = pos; ':' != routeList[colon]; colon++)
1335 if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1336 break;
1337 for (end = colon; ';' != routeList[end]; end++)
1338 if ('\0' == routeList[end])
1339 break;
1340 if ('\0' == routeList[end])
1341 break;
1342 routeList[end] = '\0';
1343 if (':' == routeList[colon])
1344 {
1345 routeList[colon] = '\0';
1346 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1347 break;
1348 }
1349 if (8 ==
1350 sscanf (&routeList[pos],
1351 "%u.%u.%u.%u/%u.%u.%u.%u%c",
1352 &temps[0],
1353 &temps[1],
1354 &temps[2],
1355 &temps[3],
1356 &temps[4],
1357 &temps[5],
1358 &temps[6],
1359 &temps[7],
1360 &dummy))
1361 {
1362 for (unsigned int j = 0; j < 8; j++)
1363 if (temps[j] > 0xFF)
1364 {
1366 _ ("Invalid format for IP: `%s'\n"),
1367 &routeList[pos]);
1369 GNUNET_free (routeList);
1370 return NULL;
1371 }
1372 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1373 + (temps[2] << 8) + temps[3]);
1374 result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1375 + (temps[6] << 8) + temps[7]);
1376 pos = end + 1;
1377 continue;
1378 }
1379
1380 /* try second notation */
1381 {
1382 unsigned int slash;
1383
1384 if (5 ==
1385 sscanf (&routeList[pos],
1386 "%u.%u.%u.%u/%u%c",
1387 &temps[0],
1388 &temps[1],
1389 &temps[2],
1390 &temps[3],
1391 &slash,
1392 &dummy))
1393 {
1394 for (unsigned int j = 0; j < 4; j++)
1395 if (temps[j] > 0xFF)
1396 {
1398 _ ("Invalid format for IP: `%s'\n"),
1399 &routeList[pos]);
1401 GNUNET_free (routeList);
1402 return NULL;
1403 }
1404 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1405 + (temps[2] << 8) + temps[3]);
1406 if (slash <= 32)
1407 {
1408 result[i].netmask.s_addr = 0;
1409 while (slash > 0)
1410 {
1411 result[i].netmask.s_addr =
1412 (result[i].netmask.s_addr >> 1) + 0x80000000;
1413 slash--;
1414 }
1415 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1416 pos = end + 1;
1417 continue;
1418 }
1419 else
1420 {
1422 _ (
1423 "Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1424 slash);
1426 GNUNET_free (routeList);
1427 return NULL; /* error */
1428 }
1429 }
1430 }
1431
1432 /* try third notation */
1433 if (4 ==
1434 sscanf (&routeList[pos],
1435 "%u.%u.%u.%u%c",
1436 &temps[0],
1437 &temps[1],
1438 &temps[2],
1439 &temps[3],
1440 &dummy))
1441 {
1442 for (unsigned int j = 0; j < 4; j++)
1443 if (temps[j] > 0xFF)
1444 {
1446 _ ("Invalid format for IP: `%s'\n"),
1447 &routeList[pos]);
1449 GNUNET_free (routeList);
1450 return NULL;
1451 }
1452 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1453 + (temps[2] << 8) + temps[3]);
1454 result[i].netmask.s_addr = htonl (0xffffffff); /* yeah, the htonl is useless */
1455 pos = end + 1;
1456 continue;
1457 }
1459 _ ("Invalid format for IP: `%s'\n"),
1460 &routeList[pos]);
1462 GNUNET_free (routeList);
1463 return NULL; /* error */
1464 }
1465 if (pos < strlen (routeList))
1466 {
1468 _ ("Invalid format: `%s'\n"),
1469 &routeListX[pos]);
1471 GNUNET_free (routeList);
1472 return NULL; /* oops */
1473 }
1474 GNUNET_free (routeList);
1475 return result; /* ok */
1476}
1477
1478
1480GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1481{
1482 size_t count;
1483 size_t len;
1484 size_t pos;
1485 int ret;
1486 char *routeList;
1488 unsigned int off;
1489
1490 if (NULL == routeListX)
1491 return NULL;
1492 len = strlen (routeListX);
1493 if (0 == len)
1494 return NULL;
1495 routeList = GNUNET_strdup (routeListX);
1496 count = 0;
1497 for (size_t j = 0; j < len; j++)
1498 if (';' == routeList[j])
1499 count++;
1500 if (';' != routeList[len - 1])
1501 {
1503 _ ("Invalid network notation (does not end with ';': `%s')\n"),
1504 routeList);
1505 GNUNET_free (routeList);
1506 return NULL;
1507 }
1508 GNUNET_assert (count < UINT_MAX);
1509 result = GNUNET_new_array (count + 1,
1511 pos = 0;
1512 for (size_t i = 0; i < count; i++)
1513 {
1514 size_t start;
1515 size_t slash;
1516
1517 start = pos;
1518 while (';' != routeList[pos])
1519 pos++;
1520 slash = pos;
1521 while ( (slash > start) &&
1522 (routeList[slash] != '/') )
1523 slash--;
1524 if (slash <= start)
1525 {
1526 memset (&result[i].netmask,
1527 0xFF,
1528 sizeof(struct in6_addr));
1529 slash = pos;
1530 }
1531 else
1532 {
1533 size_t colon;
1534
1535 routeList[pos] = '\0';
1536 for (colon = pos; ':' != routeList[colon]; colon--)
1537 if ('/' == routeList[colon])
1538 break;
1539 if (':' == routeList[colon])
1540 {
1541 routeList[colon] = '\0';
1542 if (GNUNET_OK !=
1543 parse_port_policy (&routeList[colon + 1],
1544 &result[i].pp))
1545 {
1547 GNUNET_free (routeList);
1548 return NULL;
1549 }
1550 }
1551 ret = inet_pton (AF_INET6,
1552 &routeList[slash + 1],
1553 &result[i].netmask);
1554 if (ret <= 0)
1555 {
1556 char dummy;
1557 unsigned int bits;
1558 int save = errno;
1559
1560 if ( (1 != sscanf (&routeList[slash + 1],
1561 "%u%c",
1562 &bits,
1563 &dummy)) ||
1564 (bits > 128))
1565 {
1566 if (0 == ret)
1567 {
1569 _ ("Wrong format `%s' for netmask\n"),
1570 &routeList[slash]);
1571 }
1572 else
1573 {
1574 errno = save;
1576 "inet_pton");
1577 }
1579 GNUNET_free (routeList);
1580 return NULL;
1581 }
1582 off = 0;
1583 while (bits > 8)
1584 {
1585 result[i].netmask.s6_addr[off++] = 0xFF;
1586 bits -= 8;
1587 }
1588 while (bits > 0)
1589 {
1590 result[i].netmask.s6_addr[off] =
1591 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1592 bits--;
1593 }
1594 }
1595 }
1596 routeList[slash] = '\0';
1597 ret = inet_pton (AF_INET6,
1598 &routeList[start],
1599 &result[i].network);
1600 if (ret <= 0)
1601 {
1602 if (0 == ret)
1604 _ ("Wrong format `%s' for network\n"),
1605 &routeList[slash + 1]);
1606 else
1608 "inet_pton");
1610 GNUNET_free (routeList);
1611 return NULL;
1612 }
1613 pos++;
1614 }
1615 GNUNET_free (routeList);
1616 return result;
1617}
1618
1619
1622#define FILLCHAR '='
1623static const char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1624 "abcdefghijklmnopqrstuvwxyz"
1625 "0123456789+/";
1626
1627
1628size_t
1630 size_t len,
1631 char **output)
1632{
1633 const unsigned char *data = in;
1634 size_t ret;
1635 char *opt;
1636
1637 ret = 0;
1638 GNUNET_assert (len < SIZE_MAX / 4);
1639 opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1640 for (size_t i = 0; i < len; ++i)
1641 {
1642 char c;
1643
1644 c = (data[i] >> 2) & 0x3f;
1645 opt[ret++] = cvt[(int) c];
1646 c = (data[i] << 4) & 0x3f;
1647 if (++i < len)
1648 c |= (data[i] >> 4) & 0x0f;
1649 opt[ret++] = cvt[(int) c];
1650 if (i < len)
1651 {
1652 c = (data[i] << 2) & 0x3f;
1653 if (++i < len)
1654 c |= (data[i] >> 6) & 0x03;
1655 opt[ret++] = cvt[(int) c];
1656 }
1657 else
1658 {
1659 ++i;
1660 opt[ret++] = FILLCHAR;
1661 }
1662 if (i < len)
1663 {
1664 c = data[i] & 0x3f;
1665 opt[ret++] = cvt[(int) c];
1666 }
1667 else
1668 {
1669 opt[ret++] = FILLCHAR;
1670 }
1671 }
1672 *output = opt;
1673 return ret;
1674}
1675
1676
1677size_t
1679 size_t len,
1680 char **output)
1681{
1682 char *enc;
1683 size_t pos;
1684
1686 len,
1687 output);
1688 enc = *output;
1689 /* Replace with correct characters for base64url */
1690 pos = 0;
1691 while ('\0' != enc[pos])
1692 {
1693 if ('+' == enc[pos])
1694 enc[pos] = '-';
1695 if ('/' == enc[pos])
1696 enc[pos] = '_';
1697 if ('=' == enc[pos])
1698 {
1699 enc[pos] = '\0';
1700 break;
1701 }
1702 pos++;
1703 }
1704 return strlen (enc);
1705}
1706
1707
1708#define cvtfind(a) \
1709 ((((a) >= 'A') && ((a) <= 'Z')) \
1710 ? (a) - 'A' \
1711 : (((a) >= 'a') && ((a) <= 'z')) \
1712 ? (a) - 'a' + 26 \
1713 : (((a) >= '0') && ((a) <= '9')) \
1714 ? (a) - '0' + 52 \
1715 : ((a) == '+') ? 62 : ((a) == '/') ? 63 : -1)
1716
1717
1718#define CHECK_CRLF \
1719 while ( (data[i] == '\r') || (data[i] == '\n') ) \
1720 { \
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
1722 "ignoring CR/LF\n"); \
1723 i++; \
1724 if (i >= len) { \
1725 goto END; \
1726 } \
1727 }
1728
1729
1730size_t
1732 size_t len,
1733 void **out)
1734{
1735 unsigned char *output;
1736 size_t ret = 0;
1737
1738 GNUNET_assert (len / 3 < SIZE_MAX);
1739 output = GNUNET_malloc ((len * 3 / 4) + 8);
1741 "base64_decode decoding len=%d\n",
1742 (int) len);
1743 for (size_t i = 0; i < len; ++i)
1744 {
1745 unsigned char c;
1746 unsigned char c1;
1747
1748 CHECK_CRLF;
1749 if (FILLCHAR == data[i])
1750 break;
1751 c = (unsigned char) cvtfind (data[i]);
1752 ++i;
1753 CHECK_CRLF;
1754 c1 = (unsigned char) cvtfind (data[i]);
1755 c = (c << 2) | ((c1 >> 4) & 0x3);
1756 output[ret++] = c;
1757 if (++i < len)
1758 {
1759 CHECK_CRLF;
1760 c = data[i];
1761 if (FILLCHAR == c)
1762 break;
1763 c = (unsigned char) cvtfind (c);
1764 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1765 output[ret++] = c1;
1766 }
1767 if (++i < len)
1768 {
1769 CHECK_CRLF;
1770 c1 = data[i];
1771 if (FILLCHAR == c1)
1772 break;
1773
1774 c1 = (unsigned char) cvtfind (c1);
1775 c = ((c << 6) & 0xc0) | c1;
1776 output[ret++] = c;
1777 }
1778 }
1779END:
1780 *out = output;
1781 return ret;
1782}
1783
1784
1785#undef CHECK_CRLF
1786
1787
1788size_t
1790 size_t len,
1791 void **out)
1792{
1793 char *s;
1794 int padding;
1795 size_t ret;
1796
1797 /* make enough space for padding */
1798 GNUNET_assert (len < SIZE_MAX - 3);
1799 s = GNUNET_malloc (len + 3);
1800 memcpy (s,
1801 data,
1802 len);
1803 for (size_t i = 0; i < strlen (s); i++)
1804 {
1805 if (s[i] == '-')
1806 s[i] = '+';
1807 if (s[i] == '_')
1808 s[i] = '/';
1809 }
1810 padding = len % 4;
1811 switch (padding) // Pad with trailing '='s
1812 {
1813 case 0:
1814 break; // No pad chars in this case
1815 case 2:
1816 memcpy (&s[len],
1817 "==",
1818 2);
1819 len += 2;
1820 break; // Two pad chars
1821 case 3:
1822 s[len] = '=';
1823 len++;
1824 break; // One pad char
1825 default:
1826 GNUNET_assert (0);
1827 break;
1828 }
1830 len,
1831 out);
1832 GNUNET_free (s);
1833 return ret;
1834}
1835
1836
1837size_t
1839 size_t len,
1840 char **out)
1841{
1842 const char *rpos = data;
1843 char *wpos;
1844 size_t resl = 0;
1845 *out = GNUNET_malloc (len + 1); /* output should always fit into input */
1846 wpos = *out;
1847
1848 while ( ('\0' != *rpos) &&
1849 (data + len != rpos) )
1850 {
1851 unsigned int num;
1852 switch (*rpos)
1853 {
1854 case '%':
1855 if (rpos + 3 > data + len)
1856 {
1857 GNUNET_break_op (0);
1858 GNUNET_free (*out);
1859 return 0;
1860 }
1861 if (1 != sscanf (rpos + 1,
1862 "%2x",
1863 &num))
1864 {
1865 /* Invalid URL encoding, try to continue anyway */
1866 GNUNET_break_op (0);
1867 *wpos = *rpos;
1868 wpos++;
1869 resl++;
1870 rpos++;
1871 break;
1872 }
1873 *wpos = (char) ((unsigned char) num);
1874 wpos++;
1875 resl++;
1876 rpos += 3;
1877 break;
1878 /* TODO: add bad sequence handling */
1879 /* intentional fall through! */
1880 default:
1881 *wpos = *rpos;
1882 wpos++;
1883 resl++;
1884 rpos++;
1885 }
1886 }
1887 *wpos = '\0'; /* add 0-terminator */
1888 return resl;
1889}
1890
1891
1892size_t
1894 const char data[static len],
1895 char **out)
1896{
1897 struct GNUNET_Buffer buf = { 0 };
1898 const uint8_t *i8 = (uint8_t *) data;
1899 const uint8_t *end = (uint8_t *) (data + len);
1900
1901 while (end != i8)
1902 {
1903 if (0 == *i8)
1904 {
1905 /* invalid UTF-8 (or bad @a len): fail */
1906 GNUNET_break (0);
1907 GNUNET_buffer_clear (&buf);
1908 return 0;
1909 }
1910 if (0 == (0x80 & *i8))
1911 {
1912 /* traditional ASCII */
1913 if (isalnum (*i8) ||
1914 (*i8 == '-') ||
1915 (*i8 == '_') ||
1916 (*i8 == '.') ||
1917 (*i8 == '~') )
1918 GNUNET_buffer_write (&buf,
1919 (const char*) i8,
1920 1);
1921 else if (*i8 == ' ')
1922 GNUNET_buffer_write (&buf,
1923 "+",
1924 1);
1925 else
1927 "%%%X%X",
1928 *i8 >> 4,
1929 *i8 & 15);
1930 i8++;
1931 continue;
1932 }
1933 if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
1934 {
1935 /* 2-byte value, percent-encode */
1937 "%%%X%X",
1938 *i8 >> 4,
1939 *i8 & 15);
1940 i8++;
1941 if ( (end == i8) ||
1942 (0 == *i8) )
1943 {
1944 /* invalid UTF-8 (or bad @a len): fail */
1945 GNUNET_break (0);
1946 GNUNET_buffer_clear (&buf);
1947 return 0;
1948 }
1950 "%%%X%X",
1951 *i8 >> 4,
1952 *i8 & 15);
1953 i8++;
1954 continue;
1955 }
1956 if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
1957 {
1958 /* 3-byte value, percent-encode */
1959 for (unsigned int i = 0; i<3; i++)
1960 {
1961 if ( (end == i8) ||
1962 (0 == *i8) )
1963 {
1964 /* invalid UTF-8 (or bad @a len): fail */
1965 GNUNET_break (0);
1966 GNUNET_buffer_clear (&buf);
1967 return 0;
1968 }
1970 "%%%X%X",
1971 *i8 >> 4,
1972 *i8 & 15);
1973 i8++;
1974 }
1975 continue;
1976 }
1977 if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
1978 {
1979 /* 4-byte value, percent-encode */
1980 for (unsigned int i = 0; i<4; i++)
1981 {
1982 if ( (end == i8) ||
1983 (0 == *i8) )
1984 {
1985 /* invalid UTF-8 (or bad @a len): fail */
1986 GNUNET_break (0);
1987 GNUNET_buffer_clear (&buf);
1988 return 0;
1989 }
1991 "%%%X%X",
1992 *i8 >> 4,
1993 *i8 & 15);
1994 i8++;
1995 }
1996 continue;
1997 }
1998 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
1999 + 0x04) & *i8))
2000 {
2001 /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
2002 for (unsigned int i = 0; i<5; i++)
2003 {
2004 if ( (end == i8) ||
2005 (0 == *i8) )
2006 {
2007 /* invalid UTF-8 (or bad @a len): fail */
2008 GNUNET_break (0);
2009 GNUNET_buffer_clear (&buf);
2010 return 0;
2011 }
2013 "%%%X%X",
2014 *i8 >> 4,
2015 *i8 & 15);
2016 i8++;
2017 }
2018 continue;
2019 }
2020 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
2021 + 0x08 + 0x04 + 0x02)
2022 & *i8))
2023 {
2024 /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
2025 for (unsigned int i = 0; i<6; i++)
2026 {
2027 if ( (end == i8) ||
2028 (0 == *i8) )
2029 {
2030 /* invalid UTF-8 (or bad @a len): fail */
2031 GNUNET_break (0);
2032 GNUNET_buffer_clear (&buf);
2033 return 0;
2034 }
2036 "%%%X%X",
2037 *i8 >> 4,
2038 *i8 & 15);
2039 i8++;
2040 }
2041 continue;
2042 }
2043 /* really, really invalid UTF-8: fail */
2044 GNUNET_break (0);
2045 GNUNET_buffer_clear (&buf);
2046 return 0;
2047 }
2048 *out = GNUNET_buffer_reap_str (&buf);
2049 return strlen (*out);
2050}
2051
2052
2066char *
2068{
2069 const char *ret;
2070 const char *dot;
2071
2072 ret = strrchr (argv0, '_');
2073 if (NULL == ret)
2074 return NULL;
2075 ret++; /* skip underscore */
2076 dot = strchr (ret,
2077 '.');
2078 if (NULL != dot)
2079 return GNUNET_strndup (ret,
2080 dot - ret);
2081 return GNUNET_strdup (ret);
2082}
2083
2084
2085/* end of strings.c */
static size_t strnlen(const char *s, size_t n)
#define S_ISLNK(m)
Definition disk.c:61
char * getenv()
static int start
Set if we are to start default services (including ARM).
Definition gnunet-arm.c:38
static int ret
Final status code.
Definition gnunet-arm.c:93
static int end
Set if we are to shutdown all services (including ARM).
Definition gnunet-arm.c:33
static uint16_t port
Port number.
Definition gnunet-bcd.c:146
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
@ END
We're done processing.
static struct GNUNET_SCHEDULER_Task * tt
Task scheduled to handle timeout.
static char * data
The data to insert into the dht.
static char * filename
static OpusDecoder * dec
OPUS decoder.
static OpusEncoder * enc
OPUS encoder.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static char * name
Name (label) of the records to list.
static struct GNUNET_SCHEDULER_Task * t
Main task.
static char * res
Currently read line or NULL on EOF.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static int result
Global testing status.
static void do_round(void *cls)
Send out PUSHes and PULLs, possibly update #view, samplers.
static void save()
Write persistent statistics to disk.
#define GNUNET_log(kind,...)
char * GNUNET_buffer_reap_str(struct GNUNET_Buffer *buf)
Clear the buffer and return the string it contained.
Definition buffer.c:89
void GNUNET_buffer_write_fstr(struct GNUNET_Buffer *buf, const char *fmt,...) __attribute__((format(printf
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition buffer.c:63
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition buffer.c:123
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc(const char *enc, size_t enclen, void **out, size_t *out_size)
Convert CrockfordBase32 encoding back to data.
Definition strings.c:909
size_t GNUNET_STRINGS_urlencode(size_t len, const char data[static len], char **out)
url/percent encode (RFC3986).
Definition strings.c:1893
size_t GNUNET_STRINGS_base64url_decode(const char *data, size_t len, void **out)
Decode from Base64url.
Definition strings.c:1789
size_t GNUNET_STRINGS_parse_socket_addr(const char *addr, uint8_t *af, struct sockaddr **sa)
Parse an address given as a string into a struct sockaddr.
Definition strings.c:1210
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition strings.c:763
char * GNUNET_STRINGS_to_utf8(const char *input, size_t len, const char *charset)
Convert the len characters long character sequence given in input that is in the given charset to UTF...
Definition strings.c:436
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition strings.c:818
struct GNUNET_STRINGS_IPv6NetworkPolicy * GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX)
Parse an IPv6 network policy.
Definition strings.c:1480
char * GNUNET_STRINGS_conv(const char *input, size_t len, const char *input_charset, const char *output_charset)
Convert the len characters long character sequence given in input that is in the given input charset ...
Definition strings.c:374
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ip(const char *addr, uint16_t addrlen, struct sockaddr_storage *r_buf)
Tries to convert addr string to an IP (v4 or v6) address.
Definition strings.c:1195
char * GNUNET_STRINGS_byte_size_fancy(unsigned long long size)
Convert a given filesize into a fancy human-readable format.
Definition strings.c:105
GNUNET_STRINGS_FilenameCheck
Flags for what we should check a file for.
char * GNUNET_STRINGS_utf8_toupper(const char *input)
Convert the utf-8 input string to upper case.
Definition strings.c:506
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition strings.c:531
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, size_t addrlen, struct sockaddr_in6 *r_buf)
Tries to convert zt_addr string to an IPv6 address.
Definition strings.c:1087
size_t GNUNET_STRINGS_urldecode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition strings.c:1838
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition strings.c:843
unsigned int GNUNET_STRINGS_buffer_tokenize(const char *buffer, size_t size, unsigned int count,...)
Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count ...
Definition strings.c:71
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it's an URI.
Definition strings.c:1012
size_t GNUNET_STRINGS_base64url_encode(const void *in, size_t len, char **output)
Encode into Base64url.
Definition strings.c:1678
struct GNUNET_STRINGS_IPv4NetworkPolicy * GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX)
Parse an IPv4 network policy.
Definition strings.c:1305
enum GNUNET_GenericReturnValue GNUNET_STRINGS_parse_uri(const char *path, char **scheme_part, const char **path_part)
Parse a path that might be an URI.
Definition strings.c:951
size_t GNUNET_STRINGS_buffer_fill(char *buffer, size_t size, unsigned int count,...)
Fill a buffer of the given size with count 0-terminated strings (given as varargs).
Definition strings.c:43
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
Convert a given fancy human-readable time to our internal representation.
Definition strings.c:259
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out)
Decode from Base64.
Definition strings.c:1731
char * GNUNET_STRINGS_utf8_tolower(const char *input)
Convert the utf-8 input string to lower case.
Definition strings.c:481
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, size_t addrlen, struct sockaddr_in *r_buf)
Tries to convert zt_addr string to an IPv4 address.
Definition strings.c:1157
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition strings.c:1629
char * GNUNET_STRINGS_from_utf8(const char *input, size_t len, const char *charset)
Convert the len bytes-long UTF-8 string given in input to the given charset.
Definition strings.c:448
enum GNUNET_GenericReturnValue GNUNET_STRINGS_check_filename(const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
Perform checks on filename.
Definition strings.c:1054
char * GNUNET_STRINGS_get_suffix_from_binary_name(const char *argv0)
Sometimes we use the binary name to determine which specific test to run.
Definition strings.c:2067
char * GNUNET_STRINGS_utf8_normalize(const char *input)
Normalize the utf-8 input string to NFC.
Definition strings.c:460
size_t GNUNET_strlcpy(char *dst, const char *src, size_t n)
Like strlcpy but portable.
Definition strings.c:137
const char * GNUNET_STRINGS_get_short_name(const char *filename)
"man basename" Returns a pointer to a part of filename (allocates nothing)!
Definition strings.c:694
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size, unsigned long long *size)
Convert a given fancy human-readable size to bytes.
Definition strings.c:235
@ GNUNET_STRINGS_CHECK_IS_ABSOLUTE
Check that the path is an absolute path.
@ GNUNET_STRINGS_CHECK_IS_DIRECTORY
Check that it is a directory.
@ GNUNET_STRINGS_CHECK_EXISTS
Check that it exists.
@ GNUNET_STRINGS_CHECK_IS_LINK
Check that it is a link.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_timestamp(const char *fancy_time, struct GNUNET_TIME_Timestamp *atime)
Convert a given fancy human-readable time to our internal representation.
Definition strings.c:354
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_from_s(uint64_t s_after_epoch)
Convert seconds after the UNIX epoch to absolute time.
Definition time.c:701
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition strings.c:610
bool GNUNET_TIME_absolute_is_never(struct GNUNET_TIME_Absolute abs)
Test if abs is never.
Definition time.c:646
#define GNUNET_TIME_UNIT_FOREVER_TS
Constant used to specify "forever".
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
Definition strings.c:671
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
const char * GNUNET_STRINGS_timestamp_to_string(struct GNUNET_TIME_Timestamp t)
Like asctime, except for GNUnet time.
Definition strings.c:657
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_absolute(const char *fancy_time, struct GNUNET_TIME_Absolute *atime)
Convert a given fancy human-readable time to our internal representation.
Definition strings.c:303
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition peer.c:56
static unsigned int size
Size of the "table".
Definition peer.c:68
#define DIR_SEPARATOR
Definition platform.h:166
#define DIR_SEPARATOR_STR
Definition platform.h:167
#define _(String)
GNU gettext support macro.
Definition platform.h:179
#define SIZE_MAX
Definition platform.h:209
#define GNUNET_THREAD_LOCAL
Definition platform.h:248
static struct GNUNET_TIME_Relative delta
Definition speedup.c:36
static const char * cvt
Definition strings.c:1623
static enum GNUNET_GenericReturnValue parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition strings.c:1260
static enum GNUNET_GenericReturnValue convert_with_table(const char *input, const struct ConversionTable *table, unsigned long long *output)
Convert a string of the form "4 X 5 Y" into a numeric value by interpreting "X" and "Y" as units and ...
Definition strings.c:180
#define FILLCHAR
******************** Base64 encoding
Definition strings.c:1622
#define CHECK_CRLF
Definition strings.c:1718
static unsigned int getValue__(unsigned char a)
Get the decoded value corresponding to a character according to Crockford Base32 encoding.
Definition strings.c:713
#define cvtfind(a)
Definition strings.c:1708
#define LOG(kind,...)
Definition strings.c:36
#define LOG_STRERROR(kind, syscall)
Definition strings.c:38
Unit conversion table entry for 'convert_with_table'.
Definition strings.c:155
const char * name
Name of the unit (or NULL for end of table).
Definition strings.c:159
unsigned long long value
Factor to apply for this unit.
Definition strings.c:164
Dynamically growing buffer.
IPV4 network in CIDR notation.
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
struct in_addr netmask
IPv4 netmask.
network in CIDR notation for IPV6.
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
struct in6_addr network
IPv6 address.
struct in6_addr netmask
IPv6 netmask.
uint16_t start_port
Starting port range (0 if none given).
int negate_portrange
GNUNET_YES if the port range should be negated ("!" in policy).
uint16_t end_port
End of port range (0 if none given).
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Time for timestamps used by GNUnet, in microseconds rounded to seconds.
struct GNUNET_TIME_Absolute abs_time
The actual value.