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