GNUnet 0.24.1-15-gab6ed22f1
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 { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
281 { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
282 { "year", 31536000000000LL /* year */ },
283 { "years", 31536000000000LL /* year */ },
284 { "a", 31536000000000LL /* year */ },
285 { NULL, 0 } };
286 int ret;
287 unsigned long long val;
288
289 if (0 == strcasecmp ("forever", fancy_time))
290 {
292 return GNUNET_OK;
293 }
294 ret = convert_with_table (fancy_time, table, &val);
295 rtime->rel_value_us = (uint64_t) val;
296 return ret;
297}
298
299
302 struct GNUNET_TIME_Absolute *atime)
303{
304 struct tm tv;
305 time_t t;
306 const char *eos;
307
308 if (0 == strcasecmp ("end of time", fancy_time))
309 {
311 return GNUNET_OK;
312 }
313 eos = &fancy_time[strlen (fancy_time)];
314 memset (&tv, 0, sizeof(tv));
315 if ((eos != strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) &&
316 (eos != strptime (fancy_time, "%c", &tv)) &&
317 (eos != strptime (fancy_time, "%Ec", &tv)) &&
318 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) &&
319 (eos != strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) &&
320 (eos != strptime (fancy_time, "%x", &tv)) &&
321 (eos != strptime (fancy_time, "%Ex", &tv)) &&
322 (eos != strptime (fancy_time, "%Y-%m-%d", &tv)) &&
323 (eos != strptime (fancy_time, "%Y-%m", &tv)) &&
324 (eos != strptime (fancy_time, "%Y", &tv)))
325 return GNUNET_SYSERR;
326 t = mktime (&tv);
327 atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL);
328 return GNUNET_OK;
329}
330
331
334 struct GNUNET_TIME_Timestamp *atime)
335{
337 if (GNUNET_OK !=
339 &atime->abs_time)))
340 {
341 return ret;
342 }
344 {
345 atime->abs_time = GNUNET_TIME_UNIT_FOREVER_TS.abs_time;
346 }
347 return GNUNET_OK;
348}
349
350
351char *
352GNUNET_STRINGS_conv (const char *input,
353 size_t len,
354 const char *input_charset,
355 const char *output_charset)
356{
357 char *ret;
358 uint8_t *u8_string;
359 char *encoded_string;
360 size_t u8_string_length;
361 size_t encoded_string_length;
362
363 u8_string = u8_conv_from_encoding (input_charset,
364 iconveh_error,
365 input,
366 len,
367 NULL,
368 NULL,
369 &u8_string_length);
370 if (NULL == u8_string)
371 {
372 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
373 goto fail;
374 }
375 if (0 == strcmp (output_charset, "UTF-8"))
376 {
377 ret = GNUNET_malloc (u8_string_length + 1);
378 GNUNET_memcpy (ret, u8_string, u8_string_length);
379 ret[u8_string_length] = '\0';
380 free (u8_string);
381 return ret;
382 }
383 encoded_string = u8_conv_to_encoding (output_charset,
384 iconveh_error,
385 u8_string,
386 u8_string_length,
387 NULL,
388 NULL,
389 &encoded_string_length);
390 free (u8_string);
391 if (NULL == encoded_string)
392 {
393 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
394 goto fail;
395 }
396 ret = GNUNET_malloc (encoded_string_length + 1);
397 GNUNET_memcpy (ret, encoded_string, encoded_string_length);
398 ret[encoded_string_length] = '\0';
399 free (encoded_string);
400 return ret;
401fail:
403 _ ("Character sets requested were `%s'->`%s'\n"),
404 "UTF-8",
405 output_charset);
406 ret = GNUNET_malloc (len + 1);
407 GNUNET_memcpy (ret, input, len);
408 ret[len] = '\0';
409 return ret;
410}
411
412
413char *
414GNUNET_STRINGS_to_utf8 (const char *input,
415 size_t len,
416 const char *charset)
417{
418 return GNUNET_STRINGS_conv (input,
419 len,
420 charset,
421 "UTF-8");
422}
423
424
425char *
426GNUNET_STRINGS_from_utf8 (const char *input,
427 size_t len,
428 const char *charset)
429{
430 return GNUNET_STRINGS_conv (input,
431 len,
432 "UTF-8",
433 charset);
434}
435
436
437char *
439{
440 uint8_t *tmp;
441 size_t len;
442 char *output;
443 tmp = u8_normalize (UNINORM_NFC,
444 (uint8_t *) input,
445 strlen ((char*) input),
446 NULL,
447 &len);
448 if (NULL == tmp)
449 return NULL;
450 output = GNUNET_malloc (len + 1);
451 GNUNET_memcpy (output, tmp, len);
452 output[len] = '\0';
453 free (tmp);
454 return output;
455}
456
457
460 char *output)
461{
462 uint8_t *tmp_in;
463 size_t len;
464
465 tmp_in = u8_tolower ((uint8_t *) input,
466 strlen ((char *) input),
467 NULL,
468 UNINORM_NFD,
469 NULL,
470 &len);
471 if (NULL == tmp_in)
472 return GNUNET_SYSERR;
473 GNUNET_memcpy (output, tmp_in, len);
474 output[len] = '\0';
475 GNUNET_free (tmp_in);
476 return GNUNET_OK;
477}
478
479
482 char *output)
483{
484 uint8_t *tmp_in;
485 size_t len;
486
487 tmp_in = u8_toupper ((uint8_t *) input,
488 strlen ((char *) input),
489 NULL,
490 UNINORM_NFD,
491 NULL,
492 &len);
493 if (NULL == tmp_in)
494 return GNUNET_SYSERR;
495 /* 0-terminator does not fit */
496 GNUNET_memcpy (output, tmp_in, len);
497 output[len] = '\0';
498 GNUNET_free (tmp_in);
499 return GNUNET_OK;
500}
501
502
503char *
505{
506 char *buffer;
507 size_t len;
508 char *fm;
509 const char *fil_ptr;
510
511 if (NULL == fil)
512 return NULL;
513
514 if (fil[0] == DIR_SEPARATOR)
515 /* absolute path, just copy */
516 return GNUNET_strdup (fil);
517 if (fil[0] == '~')
518 {
519 fm = getenv ("HOME");
520 if (fm == NULL)
521 {
523 _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
524 return NULL;
525 }
526 fm = GNUNET_strdup (fm);
527 /* do not copy '~' */
528 fil_ptr = fil + 1;
529
530 /* skip over dir separator to be consistent */
531 if (fil_ptr[0] == DIR_SEPARATOR)
532 fil_ptr++;
533 }
534 else
535 {
536 /* relative path */
537 fil_ptr = fil;
538 len = 512;
539 fm = NULL;
540 while (1)
541 {
542 buffer = GNUNET_malloc (len);
543 if (NULL != getcwd (buffer,
544 len))
545 {
546 fm = buffer;
547 break;
548 }
549 if ( (errno == ERANGE) &&
550 (len < 1024 * 1024 * 4) )
551 {
552 len *= 2;
553 GNUNET_free (buffer);
554 continue;
555 }
556 GNUNET_free (buffer);
557 break;
558 }
559 if (NULL == fm)
560 {
562 "getcwd");
563 buffer = getenv ("PWD"); /* alternative */
564 if (buffer != NULL)
565 fm = GNUNET_strdup (buffer);
566 }
567 if (NULL == fm)
568 fm = GNUNET_strdup ("./"); /* give up */
569 }
570 GNUNET_asprintf (&buffer,
571 "%s%s%s",
572 fm,
573 (fm[strlen (fm) - 1] == DIR_SEPARATOR)
574 ? ""
576 fil_ptr);
577 GNUNET_free (fm);
578 return buffer;
579}
580
581
582const char *
584 int do_round)
585{
586 static GNUNET_THREAD_LOCAL char buf[128];
587 const char *unit = /* time unit */ "µs";
588 uint64_t dval = delta.rel_value_us;
589
591 return "forever";
592 if (0 == delta.rel_value_us)
593 return "0 ms";
594 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
595 {
596 dval = dval / 1000;
597 unit = /* time unit */ "ms";
598 if (((GNUNET_YES == do_round) && (dval > 5 * 1000)) || (0 == (dval % 1000)))
599 {
600 dval = dval / 1000;
601 unit = /* time unit */ "s";
602 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
603 {
604 dval = dval / 60;
605 unit = /* time unit */ "m";
606 if (((GNUNET_YES == do_round) && (dval > 5 * 60)) || (0 == (dval % 60)))
607 {
608 dval = dval / 60;
609 unit = /* time unit */ "h";
610 if (((GNUNET_YES == do_round) && (dval > 5 * 24)) ||
611 (0 == (dval % 24)))
612 {
613 dval = dval / 24;
614 if (1 == dval)
615 unit = /* time unit */ "day";
616 else
617 unit = /* time unit */ "days";
618 }
619 }
620 }
621 }
622 }
623 GNUNET_snprintf (buf, sizeof(buf), "%llu %s",
624 (unsigned long long) dval, unit);
625 return buf;
626}
627
628
629const char *
631{
632 struct GNUNET_TIME_Absolute av;
633
634 if (t.abs_time.abs_value_us == GNUNET_TIME_UNIT_FOREVER_TS.abs_time.
637 ;
638 av.abs_value_us = t.abs_time.abs_value_us;
640}
641
642
643const char *
645{
646 static GNUNET_THREAD_LOCAL char buf[255];
647 time_t tt;
648 struct tm *tp;
649
651 return "end of time";
652 tt = t.abs_value_us / 1000LL / 1000LL;
653 tp = localtime (&tt);
654 /* This is hacky, but i don't know a way to detect libc character encoding.
655 * Just expect utf8 from glibc these days.
656 * As for msvcrt, use the wide variant, which always returns utf16
657 * (otherwise we'd have to detect current codepage or use W32API character
658 * set conversion routines to convert to UTF8).
659 */
660 strftime (buf, sizeof(buf), "%a %b %d %H:%M:%S %Y", tp);
661
662 return buf;
663}
664
665
666const char *
668{
669 const char *short_fn = filename;
670 const char *ss;
671
672 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
673 short_fn = 1 + ss;
674 return short_fn;
675}
676
677
685static unsigned int
686getValue__ (unsigned char a)
687{
688 unsigned int dec;
689
690 switch (a)
691 {
692 case 'O':
693 case 'o':
694 a = '0';
695 break;
696
697 case 'i':
698 case 'I':
699 case 'l':
700 case 'L':
701 a = '1';
702 break;
703
704 /* also consider U to be V */
705 case 'u':
706 case 'U':
707 a = 'V';
708 break;
709
710 default:
711 break;
712 }
713 if ((a >= '0') && (a <= '9'))
714 return a - '0';
715 if ((a >= 'a') && (a <= 'z'))
716 a = toupper (a);
717 /* return (a - 'a' + 10); */
718 dec = 0;
719 if ((a >= 'A') && (a <= 'Z'))
720 {
721 if ('I' < a)
722 dec++;
723 if ('L' < a)
724 dec++;
725 if ('O' < a)
726 dec++;
727 if ('U' < a)
728 dec++;
729 return(a - 'A' + 10 - dec);
730 }
731 return -1;
732}
733
734
735char *
737 size_t size,
738 char *out,
739 size_t out_size)
740{
744 static const char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
745 unsigned int wpos;
746 unsigned int rpos;
747 unsigned int bits;
748 unsigned int vbit;
749 const unsigned char *udata;
750
751 GNUNET_assert (size < SIZE_MAX / 8 - 4);
752 udata = data;
753 if (out_size < (size * 8 + 4) / 5)
754 {
755 GNUNET_break (0);
756 return NULL;
757 }
758 vbit = 0;
759 wpos = 0;
760 rpos = 0;
761 bits = 0;
762 while ((rpos < size) || (vbit > 0))
763 {
764 if ((rpos < size) && (vbit < 5))
765 {
766 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
767 vbit += 8;
768 }
769 if (vbit < 5)
770 {
771 bits <<= (5 - vbit); /* zero-padding */
772 GNUNET_assert (vbit == ((size * 8) % 5));
773 vbit = 5;
774 }
775 if (wpos >= out_size)
776 {
777 GNUNET_break (0);
778 return NULL;
779 }
780 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
781 vbit -= 5;
782 }
783 GNUNET_assert (0 == vbit);
784 if (wpos < out_size)
785 out[wpos] = '\0';
786 return &out[wpos];
787}
788
789
790char *
792{
793 char *str_buf;
794 size_t len = size * 8;
795 char *end;
796
797 if (len % 5 > 0)
798 len += 5 - len % 5;
799 len /= 5;
800 str_buf = GNUNET_malloc (len + 1);
802 size,
803 str_buf,
804 len);
805 if (NULL == end)
806 {
807 GNUNET_free (str_buf);
808 return NULL;
809 }
810 *end = '\0';
811 return str_buf;
812}
813
814
817 size_t enclen,
818 void *out,
819 size_t out_size)
820{
821 size_t rpos;
822 size_t wpos;
823 unsigned int bits;
824 unsigned int vbit;
825 int ret;
826 int shift;
827 unsigned char *uout;
828 size_t encoded_len;
829
830 if (0 == enclen)
831 {
832 if (0 == out_size)
833 return GNUNET_OK;
834 return GNUNET_SYSERR;
835 }
836 GNUNET_assert (out_size < SIZE_MAX / 8);
837 encoded_len = out_size * 8;
838 uout = out;
839 wpos = out_size;
840 rpos = enclen;
841 if ((encoded_len % 5) > 0)
842 {
843 vbit = encoded_len % 5; /* padding! */
844 shift = 5 - vbit;
845 bits = (ret = getValue__ (enc[--rpos])) >> shift;
846 }
847 else
848 {
849 vbit = 5;
850 shift = 0;
851 bits = (ret = getValue__ (enc[--rpos]));
852 }
853 if ((encoded_len + shift) / 5 != enclen)
854 return GNUNET_SYSERR;
855 if (-1 == ret)
856 return GNUNET_SYSERR;
857 while (wpos > 0)
858 {
859 if (0 == rpos)
860 {
861 GNUNET_break (0);
862 return GNUNET_SYSERR;
863 }
864 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
865 if (-1 == ret)
866 return GNUNET_SYSERR;
867 vbit += 5;
868 if (vbit >= 8)
869 {
870 uout[--wpos] = (unsigned char) bits;
871 bits >>= 8;
872 vbit -= 8;
873 }
874 }
875 if ((0 != rpos) || (0 != vbit))
876 return GNUNET_SYSERR;
877 return GNUNET_OK;
878}
879
880
883 size_t enclen,
884 void **out,
885 size_t *out_size)
886{
887 size_t size;
888 void *data;
889 int res;
890
891 size = (enclen * 5) / 8;
893 {
894 GNUNET_break_op (0);
895 return GNUNET_SYSERR;
896 }
899 enclen,
900 data,
901 size);
902 if ( (0 < size) &&
903 (GNUNET_OK != res) )
904 {
905 size--;
907 enclen,
908 data,
909 size);
910 }
911 if (GNUNET_OK != res)
912 {
913 GNUNET_break_op (0);
915 return GNUNET_SYSERR;
916 }
917 *out = data;
918 *out_size = size;
919 return GNUNET_OK;
920}
921
922
924GNUNET_STRINGS_parse_uri (const char *path,
925 char **scheme_part,
926 const char **path_part)
927{
928 size_t len;
929 size_t i;
930 int end;
931 int pp_state = 0;
932 const char *post_scheme_part = NULL;
933
934 len = strlen (path);
935 for (end = 0, i = 0; ! end && i < len; i++)
936 {
937 switch (pp_state)
938 {
939 case 0:
940 if ((path[i] == ':') && (i > 0))
941 {
942 pp_state += 1;
943 continue;
944 }
945 if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
946 ((path[i] >= 'a') && (path[i] <= 'z') ) ||
947 ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
948 (path[i] == '-') || (path[i] == '.')))
949 end = 1;
950 break;
951
952 case 1:
953 case 2:
954 if (path[i] == '/')
955 {
956 pp_state += 1;
957 continue;
958 }
959 end = 1;
960 break;
961
962 case 3:
963 post_scheme_part = &path[i];
964 end = 1;
965 break;
966
967 default:
968 end = 1;
969 }
970 }
971 if (post_scheme_part == NULL)
972 return GNUNET_NO;
973 if (scheme_part)
974 {
975 *scheme_part = GNUNET_strndup (path,
976 post_scheme_part - path);
977 }
978 if (path_part)
979 *path_part = post_scheme_part;
980 return GNUNET_YES;
981}
982
983
986 int can_be_uri,
987 int *r_is_uri,
988 char **r_uri_scheme)
989{
990 const char *post_scheme_path;
991 int is_uri;
992 char *uri;
993 /* consider POSIX paths to be absolute too, even on W32,
994 * as plibc expansion will fix them for us.
995 */
996 if (filename[0] == '/')
997 return GNUNET_YES;
998 if (can_be_uri)
999 {
1000 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1001 if (r_is_uri)
1002 *r_is_uri = is_uri;
1003 if (is_uri)
1004 {
1005 if (r_uri_scheme)
1006 *r_uri_scheme = uri;
1007 else
1008 GNUNET_free (uri);
1009
1010 return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1011 GNUNET_NO,
1012 NULL,
1013 NULL);
1014 }
1015 }
1016 else
1017 {
1018 if (r_is_uri)
1019 *r_is_uri = GNUNET_NO;
1020 }
1021
1022 return GNUNET_NO;
1023}
1024
1025
1028 enum GNUNET_STRINGS_FilenameCheck checks)
1029{
1030 struct stat st;
1031
1032 if ((NULL == filename) || (filename[0] == '\0'))
1033 return GNUNET_SYSERR;
1034 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1036 return GNUNET_NO;
1037 if (0 != (checks
1040 {
1041 if (0 != lstat (filename, &st))
1042 {
1043 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1044 return GNUNET_NO;
1045 else
1046 return GNUNET_SYSERR;
1047 }
1048 }
1049 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1050 if (! S_ISDIR (st.st_mode))
1051 return GNUNET_NO;
1052 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1053 if (! S_ISLNK (st.st_mode))
1054 return GNUNET_NO;
1055 return GNUNET_YES;
1056}
1057
1058
1061 size_t addrlen,
1062 struct sockaddr_in6 *r_buf)
1063{
1064 if (addrlen < 6)
1065 return GNUNET_SYSERR;
1066 if (addrlen > 512)
1067 return GNUNET_SYSERR; /* sanity check to protect zbuf allocation,
1068 actual limit is not precise */
1069 {
1070 char zbuf[addrlen + 1];
1071 int ret;
1072 char *port_colon;
1073 unsigned int port;
1074 char dummy[2];
1075
1076 GNUNET_memcpy (zbuf, zt_addr, addrlen);
1077 if ('[' != zbuf[0])
1078 {
1080 _ ("IPv6 address did not start with `['\n"));
1081 return GNUNET_SYSERR;
1082 }
1083 zbuf[addrlen] = '\0';
1084 port_colon = strrchr (zbuf, ':');
1085 if (NULL == port_colon)
1086 {
1088 _ ("IPv6 address did contain ':' to separate port number\n"));
1089 return GNUNET_SYSERR;
1090 }
1091 if (']' != *(port_colon - 1))
1092 {
1093 GNUNET_log (
1095 _ (
1096 "IPv6 address did contain ']' before ':' to separate port number\n"));
1097 return GNUNET_SYSERR;
1098 }
1099 ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1100 if ((1 != ret) || (port > 65535))
1101 {
1102 GNUNET_log (
1104 _ (
1105 "IPv6 address did contain a valid port number after the last ':'\n"));
1106 return GNUNET_SYSERR;
1107 }
1108 *(port_colon - 1) = '\0';
1109 memset (r_buf, 0, sizeof(struct sockaddr_in6));
1110 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1111 if (ret <= 0)
1112 {
1114 _ ("Invalid IPv6 address `%s': %s\n"),
1115 &zbuf[1],
1116 strerror (errno));
1117 return GNUNET_SYSERR;
1118 }
1119 r_buf->sin6_port = htons (port);
1120 r_buf->sin6_family = AF_INET6;
1121#if HAVE_SOCKADDR_IN_SIN_LEN
1122 r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1123#endif
1124 return GNUNET_OK;
1125 }
1126}
1127
1128
1131 size_t addrlen,
1132 struct sockaddr_in *r_buf)
1133{
1134 unsigned int temps[4];
1135 unsigned int port;
1136 unsigned int cnt;
1137 char dummy[2];
1138
1139 if (addrlen < 9)
1140 return GNUNET_SYSERR;
1141 cnt = sscanf (zt_addr,
1142 "%u.%u.%u.%u:%u%1s",
1143 &temps[0],
1144 &temps[1],
1145 &temps[2],
1146 &temps[3],
1147 &port,
1148 dummy);
1149 if (5 != cnt)
1150 return GNUNET_SYSERR;
1151 for (cnt = 0; cnt < 4; cnt++)
1152 if (temps[cnt] > 0xFF)
1153 return GNUNET_SYSERR;
1154 if (port > 65535)
1155 return GNUNET_SYSERR;
1156 r_buf->sin_family = AF_INET;
1157 r_buf->sin_port = htons (port);
1158 r_buf->sin_addr.s_addr =
1159 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1160#if HAVE_SOCKADDR_IN_SIN_LEN
1161 r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1162#endif
1163 return GNUNET_OK;
1164}
1165
1166
1169 uint16_t addrlen,
1170 struct sockaddr_storage *r_buf)
1171{
1172 if (addr[0] == '[')
1173 return GNUNET_STRINGS_to_address_ipv6 (addr,
1174 addrlen,
1175 (struct sockaddr_in6 *) r_buf);
1176 return GNUNET_STRINGS_to_address_ipv4 (addr,
1177 addrlen,
1178 (struct sockaddr_in *) r_buf);
1179}
1180
1181
1182size_t
1184 uint8_t *af,
1185 struct sockaddr **sa)
1186{
1187 *af = AF_UNSPEC;
1188 if ('[' == *addr)
1189 {
1190 /* IPv6 */
1191 *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1192 if (GNUNET_OK !=
1194 strlen (addr),
1195 (struct sockaddr_in6 *) *sa))
1196 {
1197 GNUNET_free (*sa);
1198 *sa = NULL;
1199 return 0;
1200 }
1201 *af = AF_INET6;
1202 return sizeof(struct sockaddr_in6);
1203 }
1204 else
1205 {
1206 /* IPv4 */
1207 *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1208 if (GNUNET_OK !=
1210 strlen (addr),
1211 (struct sockaddr_in *) *sa))
1212 {
1213 GNUNET_free (*sa);
1214 *sa = NULL;
1215 return 0;
1216 }
1217 *af = AF_INET;
1218 return sizeof(struct sockaddr_in);
1219 }
1220}
1221
1222
1232static enum GNUNET_GenericReturnValue
1233parse_port_policy (const char *port_policy,
1234 struct GNUNET_STRINGS_PortPolicy *pp)
1235{
1236 const char *pos;
1237 int s;
1238 int e;
1239 char eol[2];
1240
1241 pos = port_policy;
1242 if ('!' == *pos)
1243 {
1245 pos++;
1246 }
1247 if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol))
1248 {
1249 if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF))
1250 {
1251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1252 return GNUNET_SYSERR;
1253 }
1254 pp->start_port = (uint16_t) s;
1255 pp->end_port = (uint16_t) e;
1256 return GNUNET_OK;
1257 }
1258 if (1 == sscanf (pos, "%u%1s", &s, eol))
1259 {
1260 if ((0 == s) || (s > 0xFFFF))
1261 {
1262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1263 return GNUNET_SYSERR;
1264 }
1265
1266 pp->start_port = (uint16_t) s;
1267 pp->end_port = (uint16_t) s;
1268 return GNUNET_OK;
1269 }
1271 _ ("Malformed port policy `%s'\n"),
1272 port_policy);
1273 return GNUNET_SYSERR;
1274}
1275
1276
1278GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1279{
1280 size_t count;
1281 size_t len;
1282 size_t pos;
1283 unsigned int temps[8];
1285 char *routeList;
1286
1287 if (NULL == routeListX)
1288 return NULL;
1289 len = strlen (routeListX);
1290 if (0 == len)
1291 return NULL;
1292 routeList = GNUNET_strdup (routeListX);
1293 count = 0;
1294 for (size_t i = 0; i < len; i++)
1295 if (routeList[i] == ';')
1296 count++;
1297 GNUNET_assert (count < SIZE_MAX);
1298 result = GNUNET_new_array (count + 1,
1300 pos = 0;
1301 for (size_t i = 0; i < count; i++)
1302 {
1303 size_t colon;
1304 size_t end;
1305 char dummy;
1306
1307 for (colon = pos; ':' != routeList[colon]; colon++)
1308 if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1309 break;
1310 for (end = colon; ';' != routeList[end]; end++)
1311 if ('\0' == routeList[end])
1312 break;
1313 if ('\0' == routeList[end])
1314 break;
1315 routeList[end] = '\0';
1316 if (':' == routeList[colon])
1317 {
1318 routeList[colon] = '\0';
1319 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1320 break;
1321 }
1322 if (8 ==
1323 sscanf (&routeList[pos],
1324 "%u.%u.%u.%u/%u.%u.%u.%u%c",
1325 &temps[0],
1326 &temps[1],
1327 &temps[2],
1328 &temps[3],
1329 &temps[4],
1330 &temps[5],
1331 &temps[6],
1332 &temps[7],
1333 &dummy))
1334 {
1335 for (unsigned int j = 0; j < 8; j++)
1336 if (temps[j] > 0xFF)
1337 {
1339 _ ("Invalid format for IP: `%s'\n"),
1340 &routeList[pos]);
1342 GNUNET_free (routeList);
1343 return NULL;
1344 }
1345 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1346 + (temps[2] << 8) + temps[3]);
1347 result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1348 + (temps[6] << 8) + temps[7]);
1349 pos = end + 1;
1350 continue;
1351 }
1352
1353 /* try second notation */
1354 {
1355 unsigned int slash;
1356
1357 if (5 ==
1358 sscanf (&routeList[pos],
1359 "%u.%u.%u.%u/%u%c",
1360 &temps[0],
1361 &temps[1],
1362 &temps[2],
1363 &temps[3],
1364 &slash,
1365 &dummy))
1366 {
1367 for (unsigned int j = 0; j < 4; j++)
1368 if (temps[j] > 0xFF)
1369 {
1371 _ ("Invalid format for IP: `%s'\n"),
1372 &routeList[pos]);
1374 GNUNET_free (routeList);
1375 return NULL;
1376 }
1377 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1378 + (temps[2] << 8) + temps[3]);
1379 if (slash <= 32)
1380 {
1381 result[i].netmask.s_addr = 0;
1382 while (slash > 0)
1383 {
1384 result[i].netmask.s_addr =
1385 (result[i].netmask.s_addr >> 1) + 0x80000000;
1386 slash--;
1387 }
1388 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1389 pos = end + 1;
1390 continue;
1391 }
1392 else
1393 {
1395 _ (
1396 "Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1397 slash);
1399 GNUNET_free (routeList);
1400 return NULL; /* error */
1401 }
1402 }
1403 }
1404
1405 /* try third notation */
1406 if (4 ==
1407 sscanf (&routeList[pos],
1408 "%u.%u.%u.%u%c",
1409 &temps[0],
1410 &temps[1],
1411 &temps[2],
1412 &temps[3],
1413 &dummy))
1414 {
1415 for (unsigned int j = 0; j < 4; j++)
1416 if (temps[j] > 0xFF)
1417 {
1419 _ ("Invalid format for IP: `%s'\n"),
1420 &routeList[pos]);
1422 GNUNET_free (routeList);
1423 return NULL;
1424 }
1425 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1426 + (temps[2] << 8) + temps[3]);
1427 result[i].netmask.s_addr = htonl (0xffffffff); /* yeah, the htonl is useless */
1428 pos = end + 1;
1429 continue;
1430 }
1432 _ ("Invalid format for IP: `%s'\n"),
1433 &routeList[pos]);
1435 GNUNET_free (routeList);
1436 return NULL; /* error */
1437 }
1438 if (pos < strlen (routeList))
1439 {
1441 _ ("Invalid format: `%s'\n"),
1442 &routeListX[pos]);
1444 GNUNET_free (routeList);
1445 return NULL; /* oops */
1446 }
1447 GNUNET_free (routeList);
1448 return result; /* ok */
1449}
1450
1451
1453GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1454{
1455 size_t count;
1456 size_t len;
1457 size_t pos;
1458 int ret;
1459 char *routeList;
1461 unsigned int off;
1462
1463 if (NULL == routeListX)
1464 return NULL;
1465 len = strlen (routeListX);
1466 if (0 == len)
1467 return NULL;
1468 routeList = GNUNET_strdup (routeListX);
1469 count = 0;
1470 for (size_t j = 0; j < len; j++)
1471 if (';' == routeList[j])
1472 count++;
1473 if (';' != routeList[len - 1])
1474 {
1476 _ ("Invalid network notation (does not end with ';': `%s')\n"),
1477 routeList);
1478 GNUNET_free (routeList);
1479 return NULL;
1480 }
1481 GNUNET_assert (count < UINT_MAX);
1482 result = GNUNET_new_array (count + 1,
1484 pos = 0;
1485 for (size_t i = 0; i < count; i++)
1486 {
1487 size_t start;
1488 size_t slash;
1489
1490 start = pos;
1491 while (';' != routeList[pos])
1492 pos++;
1493 slash = pos;
1494 while ( (slash > start) &&
1495 (routeList[slash] != '/') )
1496 slash--;
1497 if (slash <= start)
1498 {
1499 memset (&result[i].netmask,
1500 0xFF,
1501 sizeof(struct in6_addr));
1502 slash = pos;
1503 }
1504 else
1505 {
1506 size_t colon;
1507
1508 routeList[pos] = '\0';
1509 for (colon = pos; ':' != routeList[colon]; colon--)
1510 if ('/' == routeList[colon])
1511 break;
1512 if (':' == routeList[colon])
1513 {
1514 routeList[colon] = '\0';
1515 if (GNUNET_OK !=
1516 parse_port_policy (&routeList[colon + 1],
1517 &result[i].pp))
1518 {
1520 GNUNET_free (routeList);
1521 return NULL;
1522 }
1523 }
1524 ret = inet_pton (AF_INET6,
1525 &routeList[slash + 1],
1526 &result[i].netmask);
1527 if (ret <= 0)
1528 {
1529 char dummy;
1530 unsigned int bits;
1531 int save = errno;
1532
1533 if ( (1 != sscanf (&routeList[slash + 1],
1534 "%u%c",
1535 &bits,
1536 &dummy)) ||
1537 (bits > 128))
1538 {
1539 if (0 == ret)
1540 {
1542 _ ("Wrong format `%s' for netmask\n"),
1543 &routeList[slash]);
1544 }
1545 else
1546 {
1547 errno = save;
1549 "inet_pton");
1550 }
1552 GNUNET_free (routeList);
1553 return NULL;
1554 }
1555 off = 0;
1556 while (bits > 8)
1557 {
1558 result[i].netmask.s6_addr[off++] = 0xFF;
1559 bits -= 8;
1560 }
1561 while (bits > 0)
1562 {
1563 result[i].netmask.s6_addr[off] =
1564 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1565 bits--;
1566 }
1567 }
1568 }
1569 routeList[slash] = '\0';
1570 ret = inet_pton (AF_INET6,
1571 &routeList[start],
1572 &result[i].network);
1573 if (ret <= 0)
1574 {
1575 if (0 == ret)
1577 _ ("Wrong format `%s' for network\n"),
1578 &routeList[slash + 1]);
1579 else
1581 "inet_pton");
1583 GNUNET_free (routeList);
1584 return NULL;
1585 }
1586 pos++;
1587 }
1588 GNUNET_free (routeList);
1589 return result;
1590}
1591
1592
1595#define FILLCHAR '='
1596static const char *cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1597 "abcdefghijklmnopqrstuvwxyz"
1598 "0123456789+/";
1599
1600
1601size_t
1603 size_t len,
1604 char **output)
1605{
1606 const unsigned char *data = in;
1607 size_t ret;
1608 char *opt;
1609
1610 ret = 0;
1611 GNUNET_assert (len < SIZE_MAX / 4);
1612 opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1613 for (size_t i = 0; i < len; ++i)
1614 {
1615 char c;
1616
1617 c = (data[i] >> 2) & 0x3f;
1618 opt[ret++] = cvt[(int) c];
1619 c = (data[i] << 4) & 0x3f;
1620 if (++i < len)
1621 c |= (data[i] >> 4) & 0x0f;
1622 opt[ret++] = cvt[(int) c];
1623 if (i < len)
1624 {
1625 c = (data[i] << 2) & 0x3f;
1626 if (++i < len)
1627 c |= (data[i] >> 6) & 0x03;
1628 opt[ret++] = cvt[(int) c];
1629 }
1630 else
1631 {
1632 ++i;
1633 opt[ret++] = FILLCHAR;
1634 }
1635 if (i < len)
1636 {
1637 c = data[i] & 0x3f;
1638 opt[ret++] = cvt[(int) c];
1639 }
1640 else
1641 {
1642 opt[ret++] = FILLCHAR;
1643 }
1644 }
1645 *output = opt;
1646 return ret;
1647}
1648
1649
1650size_t
1652 size_t len,
1653 char **output)
1654{
1655 char *enc;
1656 size_t pos;
1657
1659 len,
1660 output);
1661 enc = *output;
1662 /* Replace with correct characters for base64url */
1663 pos = 0;
1664 while ('\0' != enc[pos])
1665 {
1666 if ('+' == enc[pos])
1667 enc[pos] = '-';
1668 if ('/' == enc[pos])
1669 enc[pos] = '_';
1670 if ('=' == enc[pos])
1671 {
1672 enc[pos] = '\0';
1673 break;
1674 }
1675 pos++;
1676 }
1677 return strlen (enc);
1678}
1679
1680
1681#define cvtfind(a) \
1682 ((((a) >= 'A') && ((a) <= 'Z')) \
1683 ? (a) - 'A' \
1684 : (((a) >= 'a') && ((a) <= 'z')) \
1685 ? (a) - 'a' + 26 \
1686 : (((a) >= '0') && ((a) <= '9')) \
1687 ? (a) - '0' + 52 \
1688 : ((a) == '+') ? 62 : ((a) == '/') ? 63 : -1)
1689
1690
1691#define CHECK_CRLF \
1692 while ( (data[i] == '\r') || (data[i] == '\n') ) \
1693 { \
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
1695 "ignoring CR/LF\n"); \
1696 i++; \
1697 if (i >= len) { \
1698 goto END; \
1699 } \
1700 }
1701
1702
1703size_t
1705 size_t len,
1706 void **out)
1707{
1708 unsigned char *output;
1709 size_t ret = 0;
1710
1711 GNUNET_assert (len / 3 < SIZE_MAX);
1712 output = GNUNET_malloc ((len * 3 / 4) + 8);
1714 "base64_decode decoding len=%d\n",
1715 (int) len);
1716 for (size_t i = 0; i < len; ++i)
1717 {
1718 unsigned char c;
1719 unsigned char c1;
1720
1721 CHECK_CRLF;
1722 if (FILLCHAR == data[i])
1723 break;
1724 c = (unsigned char) cvtfind (data[i]);
1725 ++i;
1726 CHECK_CRLF;
1727 c1 = (unsigned char) cvtfind (data[i]);
1728 c = (c << 2) | ((c1 >> 4) & 0x3);
1729 output[ret++] = c;
1730 if (++i < len)
1731 {
1732 CHECK_CRLF;
1733 c = data[i];
1734 if (FILLCHAR == c)
1735 break;
1736 c = (unsigned char) cvtfind (c);
1737 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1738 output[ret++] = c1;
1739 }
1740 if (++i < len)
1741 {
1742 CHECK_CRLF;
1743 c1 = data[i];
1744 if (FILLCHAR == c1)
1745 break;
1746
1747 c1 = (unsigned char) cvtfind (c1);
1748 c = ((c << 6) & 0xc0) | c1;
1749 output[ret++] = c;
1750 }
1751 }
1752END:
1753 *out = output;
1754 return ret;
1755}
1756
1757
1758#undef CHECK_CRLF
1759
1760
1761size_t
1763 size_t len,
1764 void **out)
1765{
1766 char *s;
1767 int padding;
1768 size_t ret;
1769
1770 /* make enough space for padding */
1771 GNUNET_assert (len < SIZE_MAX - 3);
1772 s = GNUNET_malloc (len + 3);
1773 memcpy (s,
1774 data,
1775 len);
1776 for (size_t i = 0; i < strlen (s); i++)
1777 {
1778 if (s[i] == '-')
1779 s[i] = '+';
1780 if (s[i] == '_')
1781 s[i] = '/';
1782 }
1783 padding = len % 4;
1784 switch (padding) // Pad with trailing '='s
1785 {
1786 case 0:
1787 break; // No pad chars in this case
1788 case 2:
1789 memcpy (&s[len],
1790 "==",
1791 2);
1792 len += 2;
1793 break; // Two pad chars
1794 case 3:
1795 s[len] = '=';
1796 len++;
1797 break; // One pad char
1798 default:
1799 GNUNET_assert (0);
1800 break;
1801 }
1803 len,
1804 out);
1805 GNUNET_free (s);
1806 return ret;
1807}
1808
1809
1810size_t
1812 size_t len,
1813 char **out)
1814{
1815 const char *rpos = data;
1816 char *wpos;
1817 size_t resl = 0;
1818 *out = GNUNET_malloc (len + 1); /* output should always fit into input */
1819 wpos = *out;
1820
1821 while ( ('\0' != *rpos) &&
1822 (data + len != rpos) )
1823 {
1824 unsigned int num;
1825 switch (*rpos)
1826 {
1827 case '%':
1828 if (rpos + 3 > data + len)
1829 {
1830 GNUNET_break_op (0);
1831 GNUNET_free (*out);
1832 return 0;
1833 }
1834 if (1 != sscanf (rpos + 1,
1835 "%2x",
1836 &num))
1837 {
1838 /* Invalid URL encoding, try to continue anyway */
1839 GNUNET_break_op (0);
1840 *wpos = *rpos;
1841 wpos++;
1842 resl++;
1843 rpos++;
1844 break;
1845 }
1846 *wpos = (char) ((unsigned char) num);
1847 wpos++;
1848 resl++;
1849 rpos += 3;
1850 break;
1851 /* TODO: add bad sequence handling */
1852 /* intentional fall through! */
1853 default:
1854 *wpos = *rpos;
1855 wpos++;
1856 resl++;
1857 rpos++;
1858 }
1859 }
1860 *wpos = '\0'; /* add 0-terminator */
1861 return resl;
1862}
1863
1864
1865size_t
1867 const char data[static len],
1868 char **out)
1869{
1870 struct GNUNET_Buffer buf = { 0 };
1871 const uint8_t *i8 = (uint8_t *) data;
1872 const uint8_t *end = (uint8_t *) (data + len);
1873
1874 while (end != i8)
1875 {
1876 if (0 == *i8)
1877 {
1878 /* invalid UTF-8 (or bad @a len): fail */
1879 GNUNET_break (0);
1880 GNUNET_buffer_clear (&buf);
1881 return 0;
1882 }
1883 if (0 == (0x80 & *i8))
1884 {
1885 /* traditional ASCII */
1886 if (isalnum (*i8) ||
1887 (*i8 == '-') ||
1888 (*i8 == '_') ||
1889 (*i8 == '.') ||
1890 (*i8 == '~') )
1891 GNUNET_buffer_write (&buf,
1892 (const char*) i8,
1893 1);
1894 else if (*i8 == ' ')
1895 GNUNET_buffer_write (&buf,
1896 "+",
1897 1);
1898 else
1900 "%%%X%X",
1901 *i8 >> 4,
1902 *i8 & 15);
1903 i8++;
1904 continue;
1905 }
1906 if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
1907 {
1908 /* 2-byte value, percent-encode */
1910 "%%%X%X",
1911 *i8 >> 4,
1912 *i8 & 15);
1913 i8++;
1914 if ( (end == i8) ||
1915 (0 == *i8) )
1916 {
1917 /* invalid UTF-8 (or bad @a len): fail */
1918 GNUNET_break (0);
1919 GNUNET_buffer_clear (&buf);
1920 return 0;
1921 }
1923 "%%%X%X",
1924 *i8 >> 4,
1925 *i8 & 15);
1926 i8++;
1927 continue;
1928 }
1929 if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
1930 {
1931 /* 3-byte value, percent-encode */
1932 for (unsigned int i = 0; i<3; i++)
1933 {
1934 if ( (end == i8) ||
1935 (0 == *i8) )
1936 {
1937 /* invalid UTF-8 (or bad @a len): fail */
1938 GNUNET_break (0);
1939 GNUNET_buffer_clear (&buf);
1940 return 0;
1941 }
1943 "%%%X%X",
1944 *i8 >> 4,
1945 *i8 & 15);
1946 i8++;
1947 }
1948 continue;
1949 }
1950 if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
1951 {
1952 /* 4-byte value, percent-encode */
1953 for (unsigned int i = 0; i<4; i++)
1954 {
1955 if ( (end == i8) ||
1956 (0 == *i8) )
1957 {
1958 /* invalid UTF-8 (or bad @a len): fail */
1959 GNUNET_break (0);
1960 GNUNET_buffer_clear (&buf);
1961 return 0;
1962 }
1964 "%%%X%X",
1965 *i8 >> 4,
1966 *i8 & 15);
1967 i8++;
1968 }
1969 continue;
1970 }
1971 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
1972 + 0x04) & *i8))
1973 {
1974 /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1975 for (unsigned int i = 0; i<5; i++)
1976 {
1977 if ( (end == i8) ||
1978 (0 == *i8) )
1979 {
1980 /* invalid UTF-8 (or bad @a len): fail */
1981 GNUNET_break (0);
1982 GNUNET_buffer_clear (&buf);
1983 return 0;
1984 }
1986 "%%%X%X",
1987 *i8 >> 4,
1988 *i8 & 15);
1989 i8++;
1990 }
1991 continue;
1992 }
1993 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
1994 + 0x08 + 0x04 + 0x02)
1995 & *i8))
1996 {
1997 /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1998 for (unsigned int i = 0; i<6; i++)
1999 {
2000 if ( (end == i8) ||
2001 (0 == *i8) )
2002 {
2003 /* invalid UTF-8 (or bad @a len): fail */
2004 GNUNET_break (0);
2005 GNUNET_buffer_clear (&buf);
2006 return 0;
2007 }
2009 "%%%X%X",
2010 *i8 >> 4,
2011 *i8 & 15);
2012 i8++;
2013 }
2014 continue;
2015 }
2016 /* really, really invalid UTF-8: fail */
2017 GNUNET_break (0);
2018 GNUNET_buffer_clear (&buf);
2019 return 0;
2020 }
2021 *out = GNUNET_buffer_reap_str (&buf);
2022 return strlen (*out);
2023}
2024
2025
2039char *
2041{
2042 const char *ret;
2043 const char *dot;
2044
2045 ret = strrchr (argv0, '_');
2046 if (NULL == ret)
2047 return NULL;
2048 ret++; /* skip underscore */
2049 dot = strchr (ret,
2050 '.');
2051 if (NULL != dot)
2052 return GNUNET_strndup (ret,
2053 dot - ret);
2054 return GNUNET_strdup (ret);
2055}
2056
2057
2058/* 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 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.
static struct GNUNET_SCHEDULER_Task * t
Main task.
#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:123
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:86
#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:165
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.
@ 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:882
size_t GNUNET_STRINGS_urlencode(size_t len, const char data[static len], char **out)
url/percent encode (RFC3986).
Definition: strings.c:1866
size_t GNUNET_STRINGS_base64url_decode(const char *data, size_t len, void **out)
Decode from Base64url.
Definition: strings.c:1762
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:1183
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:736
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:414
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:791
struct GNUNET_STRINGS_IPv6NetworkPolicy * GNUNET_STRINGS_parse_ipv6_policy(const char *routeListX)
Parse an IPv6 network policy.
Definition: strings.c:1453
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:352
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:1168
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_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:504
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:1060
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_toupper(const char *input, char *output)
Convert the utf-8 input string to upper case.
Definition: strings.c:481
size_t GNUNET_STRINGS_urldecode(const char *data, size_t len, char **out)
url/percent encode (RFC3986).
Definition: strings.c:1811
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:816
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:985
size_t GNUNET_STRINGS_base64url_encode(const void *in, size_t len, char **output)
Encode into Base64url.
Definition: strings.c:1651
struct GNUNET_STRINGS_IPv4NetworkPolicy * GNUNET_STRINGS_parse_ipv4_policy(const char *routeListX)
Parse an IPv4 network policy.
Definition: strings.c:1278
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:924
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:1704
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:1130
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1602
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_tolower(const char *input, char *output)
Convert the utf-8 input string to lower case.
Definition: strings.c:459
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:426
enum GNUNET_GenericReturnValue GNUNET_STRINGS_check_filename(const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
Perform checks on filename.
Definition: strings.c:1027
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:2040
char * GNUNET_STRINGS_utf8_normalize(const char *input)
Normalize the utf-8 input string to NFC.
Definition: strings.c:438
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:667
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:333
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
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:583
bool GNUNET_TIME_absolute_is_never(struct GNUNET_TIME_Absolute abs)
Test if abs is never.
Definition: time.c:650
#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:644
#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:630
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:301
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:1596
static enum GNUNET_GenericReturnValue parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1233
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:1595
#define CHECK_CRLF
Definition: strings.c:1691
static unsigned int getValue__(unsigned char a)
Get the decoded value corresponding to a character according to Crockford Base32 encoding.
Definition: strings.c:686
#define cvtfind(a)
Definition: strings.c:1681
#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.