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