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