Make GNUNET_STRINGS_absolute_time_to_string() return utf8-encoded string
[oweals/gnunet.git] / src / util / strings.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005-2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file util/strings.c
23  * @brief string functions
24  * @author Nils Durner
25  * @author Christian Grothoff
26  */
27
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", __VA_ARGS__)
38
39 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
40
41
42 /**
43  * Fill a buffer of the given size with
44  * count 0-terminated strings (given as varargs).
45  * If "buffer" is NULL, only compute the amount of
46  * space required (sum of "strlen(arg)+1").
47  *
48  * Unlike using "snprintf" with "%s", this function
49  * will add 0-terminators after each string.  The
50  * "GNUNET_string_buffer_tokenize" function can be
51  * used to parse the buffer back into individual
52  * strings.
53  *
54  * @param buffer the buffer to fill with strings, can
55  *               be NULL in which case only the necessary
56  *               amount of space will be calculated
57  * @param size number of bytes available in buffer
58  * @param count number of strings that follow
59  * @param ... count 0-terminated strings to copy to buffer
60  * @return number of bytes written to the buffer
61  *         (or number of bytes that would have been written)
62  */
63 size_t
64 GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
65 {
66   size_t needed;
67   size_t slen;
68   const char *s;
69   va_list ap;
70
71   needed = 0;
72   va_start (ap, count);
73   while (count > 0)
74   {
75     s = va_arg (ap, const char *);
76
77     slen = strlen (s) + 1;
78     if (buffer != NULL)
79     {
80       GNUNET_assert (needed + slen <= size);
81       memcpy (&buffer[needed], s, slen);
82     }
83     needed += slen;
84     count--;
85   }
86   va_end (ap);
87   return needed;
88 }
89
90
91 /**
92  * Given a buffer of a given size, find "count"
93  * 0-terminated strings in the buffer and assign
94  * the count (varargs) of type "const char**" to the
95  * locations of the respective strings in the
96  * buffer.
97  *
98  * @param buffer the buffer to parse
99  * @param size size of the buffer
100  * @param count number of strings to locate
101  * @return offset of the character after the last 0-termination
102  *         in the buffer, or 0 on error.
103  */
104 unsigned int
105 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
106                                 unsigned int count, ...)
107 {
108   unsigned int start;
109   unsigned int needed;
110   const char **r;
111   va_list ap;
112
113   needed = 0;
114   va_start (ap, count);
115   while (count > 0)
116   {
117     r = va_arg (ap, const char **);
118
119     start = needed;
120     while ((needed < size) && (buffer[needed] != '\0'))
121       needed++;
122     if (needed == size)
123     {
124       va_end (ap);
125       return 0;                 /* error */
126     }
127     *r = &buffer[start];
128     needed++;                   /* skip 0-termination */
129     count--;
130   }
131   va_end (ap);
132   return needed;
133 }
134
135
136 /**
137  * Convert a given filesize into a fancy human-readable format.
138  *
139  * @param size number of bytes
140  * @return fancy representation of the size (possibly rounded) for humans
141  */
142 char *
143 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
144 {
145   const char *unit = _( /* size unit */ "b");
146   char *ret;
147
148   if (size > 5 * 1024)
149   {
150     size = size / 1024;
151     unit = "KiB";
152     if (size > 5 * 1024)
153     {
154       size = size / 1024;
155       unit = "MiB";
156       if (size > 5 * 1024)
157       {
158         size = size / 1024;
159         unit = "GiB";
160         if (size > 5 * 1024)
161         {
162           size = size / 1024;
163           unit = "TiB";
164         }
165       }
166     }
167   }
168   ret = GNUNET_malloc (32);
169   GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
170   return ret;
171 }
172
173
174 /**
175  * Unit conversion table entry for 'convert_with_table'.
176  */
177 struct ConversionTable
178 {
179   /**
180    * Name of the unit (or NULL for end of table).
181    */
182   const char *name;
183
184   /**
185    * Factor to apply for this unit.
186    */
187   unsigned long long value;
188 };
189
190
191 /**
192  * Convert a string of the form "4 X 5 Y" into a numeric value
193  * by interpreting "X" and "Y" as units and then multiplying
194  * the numbers with the values associated with the respective
195  * unit from the conversion table.
196  *
197  * @param input input string to parse
198  * @param table table with the conversion of unit names to numbers
199  * @param output where to store the result
200  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
201  */
202 static int
203 convert_with_table (const char *input,
204                     const struct ConversionTable *table,
205                     unsigned long long *output)
206 {
207   unsigned long long ret;
208   char *in;
209   const char *tok;
210   unsigned long long last;
211   unsigned int i;
212
213   ret = 0;
214   last = 0;
215   in = GNUNET_strdup (input);
216   for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
217   {
218     do
219     {
220       i = 0;
221       while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
222         i++;
223       if (table[i].name != NULL)
224       {
225         last *= table[i].value;
226         break; /* next tok */
227       }
228       else
229       {
230         char *endptr;
231         ret += last;
232         errno = 0;
233         last = strtoull (tok, &endptr, 10);
234         if ((0 != errno) || (endptr == tok))
235         {
236           GNUNET_free (in);
237           return GNUNET_SYSERR;   /* expected number */
238         }
239         if ('\0' == endptr[0])
240           break; /* next tok */
241         else
242           tok = endptr; /* and re-check (handles times like "10s") */
243       }
244     } while (GNUNET_YES);
245   }
246   ret += last;
247   *output = ret;
248   GNUNET_free (in);
249   return GNUNET_OK;
250 }
251
252
253 /**
254  * Convert a given fancy human-readable size to bytes.
255  *
256  * @param fancy_size human readable string (i.e. 1 MB)
257  * @param size set to the size in bytes
258  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
259  */
260 int
261 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
262                                     unsigned long long *size)
263 {
264   static const struct ConversionTable table[] =
265   {
266     { "B", 1},
267     { "KiB", 1024},
268     { "kB", 1000},
269     { "MiB", 1024 * 1024},
270     { "MB", 1000 * 1000},
271     { "GiB", 1024 * 1024 * 1024},
272     { "GB", 1000 * 1000 * 1000},
273     { "TiB", 1024LL * 1024LL * 1024LL * 1024LL},
274     { "TB", 1000LL * 1000LL * 1000LL * 1024LL},
275     { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
276     { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
277     { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
278     { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
279     { NULL, 0}
280   };
281
282   return convert_with_table (fancy_size,
283                              table,
284                              size);
285 }
286
287
288 /**
289  * Convert a given fancy human-readable time to our internal
290  * representation.
291  *
292  * @param fancy_time human readable string (i.e. 1 minute)
293  * @param rtime set to the relative time
294  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
295  */
296 int
297 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
298                                        struct GNUNET_TIME_Relative *rtime)
299 {
300   static const struct ConversionTable table[] =
301   {
302     { "us", 1},
303     { "ms", 1000 },
304     { "s", 1000 * 1000LL },
305     { "\"", 1000  * 1000LL },
306     { "m", 60 * 1000  * 1000LL},
307     { "min", 60 * 1000  * 1000LL},
308     { "minutes", 60 * 1000  * 1000LL},
309     { "'", 60 * 1000  * 1000LL},
310     { "h", 60 * 60 * 1000  * 1000LL},
311     { "d", 24 * 60 * 60 * 1000LL * 1000LL},
312     { "day", 24 * 60 * 60 * 1000LL * 1000LL},
313     { "days", 24 * 60 * 60 * 1000LL * 1000LL},
314     { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL},
315     { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL},
316     { "a", 31536000000000LL /* year */ },
317     { NULL, 0}
318   };
319   int ret;
320   unsigned long long val;
321
322   if (0 == strcasecmp ("forever", fancy_time))
323   {
324     *rtime = GNUNET_TIME_UNIT_FOREVER_REL;
325     return GNUNET_OK;
326   }
327   ret = convert_with_table (fancy_time,
328                             table,
329                             &val);
330   rtime->rel_value_us = (uint64_t) val;
331   return ret;
332 }
333
334
335 /**
336  * Convert a given fancy human-readable time to our internal
337  * representation. The human-readable time is expected to be
338  * in local time, whereas the returned value will be in UTC.
339  *
340  * @param fancy_time human readable string (i.e. %Y-%m-%d %H:%M:%S)
341  * @param atime set to the absolute time
342  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
343  */
344 int
345 GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
346                                        struct GNUNET_TIME_Absolute *atime)
347 {
348   struct tm tv;
349   time_t t;
350 #if HAVE_TM_GMTOFF
351   struct tm *tp;
352 #endif
353
354   if (0 == strcasecmp ("end of time", fancy_time))
355   {
356     *atime = GNUNET_TIME_UNIT_FOREVER_ABS;
357     return GNUNET_OK;
358   }
359   memset (&tv, 0, sizeof (tv));
360   if ( (NULL == strptime (fancy_time, "%a %b %d %H:%M:%S %Y", &tv)) &&
361        (NULL == strptime (fancy_time, "%c", &tv)) &&
362        (NULL == strptime (fancy_time, "%Ec", &tv)) &&
363        (NULL == strptime (fancy_time, "%Y-%m-%d %H:%M:%S", &tv)) &&
364        (NULL == strptime (fancy_time, "%Y-%m-%d %H:%M", &tv)) &&
365        (NULL == strptime (fancy_time, "%x", &tv)) &&
366        (NULL == strptime (fancy_time, "%Ex", &tv)) &&
367        (NULL == strptime (fancy_time, "%Y-%m-%d", &tv)) &&
368        (NULL == strptime (fancy_time, "%Y-%m", &tv)) &&
369        (NULL == strptime (fancy_time, "%Y", &tv)) )
370     return GNUNET_SYSERR;
371   t = mktime (&tv);
372   atime->abs_value_us = (uint64_t) ((uint64_t) t * 1000LL * 1000LL);
373 #if HAVE_TM_GMTOFF
374   tp = localtime (&t);
375   atime->abs_value_us += 1000LL * 1000LL * tp->tm_gmtoff;
376 #elif defined LINUX
377   atime->abs_value_us -= 1000LL * 1000LL * timezone;
378 #elif defined WINDOWS
379   {
380     DWORD tzv;
381     TIME_ZONE_INFORMATION tzi;
382     tzv = GetTimeZoneInformation (&tzi);
383     if (TIME_ZONE_ID_INVALID != tzv)
384     {
385       atime->abs_value_us -= 1000LL * 1000LL * tzi.Bias * 60LL;
386     }
387   }
388 #endif
389   return GNUNET_OK;
390 }
391
392
393 /**
394  * Convert the len characters long character sequence
395  * given in input that is in the given input charset
396  * to a string in given output charset.
397  *
398  * @param input input string
399  * @param len number of bytes in @a input
400  * @param input_charset character set used for @a input
401  * @param output_charset desired character set for the return value
402  * @return the converted string (0-terminated),
403  *  if conversion fails, a copy of the orignal
404  *  string is returned.
405  */
406 char *
407 GNUNET_STRINGS_conv (const char *input,
408                      size_t len,
409                      const char *input_charset,
410                      const char *output_charset)
411 {
412   char *ret;
413   uint8_t *u8_string;
414   char *encoded_string;
415   size_t u8_string_length;
416   size_t encoded_string_length;
417
418   u8_string = u8_conv_from_encoding (input_charset,
419                                      iconveh_error,
420                                      input, len,
421                                      NULL, NULL,
422                                      &u8_string_length);
423   if (NULL == u8_string)
424   {
425     LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
426     goto fail;
427   }
428   if (0 == strcmp (output_charset, "UTF-8"))
429   {
430     ret = GNUNET_malloc (u8_string_length + 1);
431     memcpy (ret, u8_string, u8_string_length);
432     ret[u8_string_length] = '\0';
433     free (u8_string);
434     return ret;
435   }
436   encoded_string = u8_conv_to_encoding (output_charset, iconveh_error,
437                                         u8_string, u8_string_length,
438                                         NULL, NULL,
439                                         &encoded_string_length);
440   free (u8_string);
441   if (NULL == encoded_string)
442   {
443     LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
444     goto fail;
445   }
446   ret = GNUNET_malloc (encoded_string_length + 1);
447   memcpy (ret, encoded_string, encoded_string_length);
448   ret[encoded_string_length] = '\0';
449   free (encoded_string);
450   return ret;
451  fail:
452   LOG (GNUNET_ERROR_TYPE_WARNING, _("Character sets requested were `%s'->`%s'\n"),
453        "UTF-8", output_charset);
454   ret = GNUNET_malloc (len + 1);
455   memcpy (ret, input, len);
456   ret[len] = '\0';
457   return ret;
458 }
459
460
461 /**
462  * Convert the len characters long character sequence
463  * given in input that is in the given charset
464  * to UTF-8.
465  *
466  * @param input the input string (not necessarily 0-terminated)
467  * @param len the number of bytes in the @a input
468  * @param charset character set to convert from
469  * @return the converted string (0-terminated),
470  *  if conversion fails, a copy of the orignal
471  *  string is returned.
472  */
473 char *
474 GNUNET_STRINGS_to_utf8 (const char *input,
475                         size_t len,
476                         const char *charset)
477 {
478   return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
479 }
480
481
482 /**
483  * Convert the len bytes-long UTF-8 string
484  * given in input to the given charset.
485  *
486  * @param input the input string (not necessarily 0-terminated)
487  * @param len the number of bytes in the @a input
488  * @param charset character set to convert to
489  * @return the converted string (0-terminated),
490  *  if conversion fails, a copy of the orignal
491  *  string is returned.
492  */
493 char *
494 GNUNET_STRINGS_from_utf8 (const char *input,
495                           size_t len,
496                           const char *charset)
497 {
498   return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
499 }
500
501
502 /**
503  * Convert the utf-8 input string to lowercase.
504  * Output needs to be allocated appropriately.
505  *
506  * @param input input string
507  * @param output output buffer
508  */
509 void
510 GNUNET_STRINGS_utf8_tolower (const char *input,
511                              char *output)
512 {
513   uint8_t *tmp_in;
514   size_t len;
515
516   tmp_in = u8_tolower ((uint8_t*)input, strlen ((char *) input),
517                        NULL, UNINORM_NFD, NULL, &len);
518   memcpy(output, tmp_in, len);
519   output[len] = '\0';
520   free(tmp_in);
521 }
522
523
524 /**
525  * Convert the utf-8 input string to uppercase.
526  * Output needs to be allocated appropriately.
527  *
528  * @param input input string
529  * @param output output buffer
530  */
531 void
532 GNUNET_STRINGS_utf8_toupper(const char *input,
533                             char *output)
534 {
535   uint8_t *tmp_in;
536   size_t len;
537
538   tmp_in = u8_toupper ((uint8_t*)input, strlen ((char *) input),
539                        NULL, UNINORM_NFD, NULL, &len);
540   memcpy (output, tmp_in, len);
541   output[len] = '\0';
542   free (tmp_in);
543 }
544
545
546 /**
547  * Complete filename (a la shell) from abbrevition.
548  * @param fil the name of the file, may contain ~/ or
549  *        be relative to the current directory
550  * @returns the full file name,
551  *          NULL is returned on error
552  */
553 char *
554 GNUNET_STRINGS_filename_expand (const char *fil)
555 {
556   char *buffer;
557 #ifndef MINGW
558   size_t len;
559   size_t n;
560   char *fm;
561   const char *fil_ptr;
562 #else
563   char *fn;
564   long lRet;
565 #endif
566
567   if (fil == NULL)
568     return NULL;
569
570 #ifndef MINGW
571   if (fil[0] == DIR_SEPARATOR)
572     /* absolute path, just copy */
573     return GNUNET_strdup (fil);
574   if (fil[0] == '~')
575   {
576     fm = getenv ("HOME");
577     if (fm == NULL)
578     {
579       LOG (GNUNET_ERROR_TYPE_WARNING,
580            _("Failed to expand `$HOME': environment variable `HOME' not set"));
581       return NULL;
582     }
583     fm = GNUNET_strdup (fm);
584     /* do not copy '~' */
585     fil_ptr = fil + 1;
586
587     /* skip over dir seperator to be consistent */
588     if (fil_ptr[0] == DIR_SEPARATOR)
589       fil_ptr++;
590   }
591   else
592   {
593     /* relative path */
594     fil_ptr = fil;
595     len = 512;
596     fm = NULL;
597     while (1)
598     {
599       buffer = GNUNET_malloc (len);
600       if (getcwd (buffer, len) != NULL)
601       {
602         fm = buffer;
603         break;
604       }
605       if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
606       {
607         len *= 2;
608         GNUNET_free (buffer);
609         continue;
610       }
611       GNUNET_free (buffer);
612       break;
613     }
614     if (fm == NULL)
615     {
616       LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
617       buffer = getenv ("PWD");  /* alternative */
618       if (buffer != NULL)
619         fm = GNUNET_strdup (buffer);
620     }
621     if (fm == NULL)
622       fm = GNUNET_strdup ("./");        /* give up */
623   }
624   n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
625   buffer = GNUNET_malloc (n);
626   GNUNET_snprintf (buffer, n, "%s%s%s", fm,
627                    (fm[strlen (fm) - 1] ==
628                     DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
629   GNUNET_free (fm);
630   return buffer;
631 #else
632   fn = GNUNET_malloc (MAX_PATH + 1);
633
634   if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
635   {
636     SetErrnoFromWinError (lRet);
637     LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
638     return NULL;
639   }
640   /* is the path relative? */
641   if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
642   {
643     char szCurDir[MAX_PATH + 1];
644
645     lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
646     if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
647     {
648       SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
649       LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
650       return NULL;
651     }
652     buffer = GNUNET_malloc (MAX_PATH + 1);
653     GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
654     GNUNET_free (fn);
655     fn = buffer;
656   }
657
658   return fn;
659 #endif
660 }
661
662
663 /**
664  * Give relative time in human-readable fancy format.
665  * This is one of the very few calls in the entire API that is
666  * NOT reentrant!
667  *
668  * @param delta time in milli seconds
669  * @param do_round are we allowed to round a bit?
670  * @return time as human-readable string
671  */
672 const char *
673 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
674                                         int do_round)
675 {
676   static char buf[128];
677   const char *unit = _( /* time unit */ "µs");
678   uint64_t dval = delta.rel_value_us;
679
680   if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us == delta.rel_value_us)
681     return _("forever");
682   if (0 == delta.rel_value_us)
683     return _("0 ms");
684   if ( ( (GNUNET_YES == do_round) &&
685          (dval > 5 * 1000) ) ||
686        (0 == (dval % 1000) ))
687   {
688     dval = dval / 1000;
689     unit = _( /* time unit */ "ms");
690     if ( ( (GNUNET_YES == do_round) &&
691            (dval > 5 * 1000) ) ||
692          (0 == (dval % 1000) ))
693     {
694       dval = dval / 1000;
695       unit = _( /* time unit */ "s");
696       if ( ( (GNUNET_YES == do_round) &&
697              (dval > 5 * 60) ) ||
698            (0 == (dval % 60) ) )
699       {
700         dval = dval / 60;
701         unit = _( /* time unit */ "m");
702         if ( ( (GNUNET_YES == do_round) &&
703                (dval > 5 * 60) ) ||
704              (0 == (dval % 60) ))
705         {
706           dval = dval / 60;
707           unit = _( /* time unit */ "h");
708           if ( ( (GNUNET_YES == do_round) &&
709                  (dval > 5 * 24) ) ||
710                (0 == (dval % 24)) )
711           {
712             dval = dval / 24;
713             if (1 == dval)
714               unit = _( /* time unit */ "day");
715             else
716               unit = _( /* time unit */ "days");
717           }
718         }
719       }
720     }
721   }
722   GNUNET_snprintf (buf, sizeof (buf),
723                    "%llu %s", dval, unit);
724   return buf;
725 }
726
727
728 /**
729  * "asctime", except for GNUnet time.  Converts a GNUnet internal
730  * absolute time (which is in UTC) to a string in local time.
731  * Note that the returned value will be overwritten if this function
732  * is called again.
733  *
734  * @param t the absolute time to convert
735  * @return timestamp in human-readable form in local time
736  */
737 const char *
738 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
739 {
740   static char buf[255];
741   time_t tt;
742   struct tm *tp;
743
744   if (t.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
745     return _("end of time");
746   tt = t.abs_value_us / 1000LL / 1000LL;
747   tp = gmtime (&tt);
748   /* This is hacky, but i don't know a way to detect libc character encoding.
749    * Just expect utf8 from glibc these days.
750    * As for msvcrt, use the wide variant, which always returns utf16
751    * (otherwise we'd have to detect current codepage or use W32API character
752    * set conversion routines to convert to UTF8).
753    */
754 #ifndef WINDOWS
755   strftime (buf, sizeof (buf), "%a %b %d %H:%M:%S %Y", tp);
756 #else
757   {
758     static wchar_t wbuf[255];
759     uint8_t *conved;
760     size_t ssize;
761
762     wcsftime (wbuf, sizeof (wbuf) / sizeof (wchar_t),
763         L"%a %b %d %H:%M:%S %Y", tp);
764
765     ssize = sizeof (buf);
766     conved = u16_to_u8 (wbuf, sizeof (wbuf) / sizeof (wchar_t),
767         (uint8_t *) buf, &ssize);
768     if (conved != (uint8_t *) buf)
769     {
770       strncpy (buf, (char *) conved, sizeof (buf));
771       buf[255 - 1] = '\0';
772       free (conved);
773     }
774   }
775 #endif
776   return buf;
777 }
778
779
780 /**
781  * "man basename"
782  * Returns a pointer to a part of filename (allocates nothing)!
783  *
784  * @param filename filename to extract basename from
785  * @return short (base) name of the file (that is, everything following the
786  *         last directory separator in filename. If filename ends with a
787  *         directory separator, the result will be a zero-length string.
788  *         If filename has no directory separators, the result is filename
789  *         itself.
790  */
791 const char *
792 GNUNET_STRINGS_get_short_name (const char *filename)
793 {
794   const char *short_fn = filename;
795   const char *ss;
796   while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR))
797       && (ss[1] != '\0'))
798     short_fn = 1 + ss;
799   return short_fn;
800 }
801
802
803 /**
804  * Get the numeric value corresponding to a character.
805  *
806  * @param a a character
807  * @return corresponding numeric value
808  */
809 static unsigned int
810 getValue__ (unsigned char a)
811 {
812   if ((a >= '0') && (a <= '9'))
813     return a - '0';
814   if ((a >= 'A') && (a <= 'V'))
815     return (a - 'A' + 10);
816   if ((a >= 'a') && (a <= 'v'))
817     return (a - 'a' + 10);
818   return -1;
819 }
820
821
822 /**
823  * Convert binary data to ASCII encoding.  The ASCII encoding is rather
824  * GNUnet specific.  It was chosen such that it only uses characters
825  * in [0-9A-V], can be produced without complex arithmetics and uses a
826  * small number of characters.
827  * Does not append 0-terminator, but returns a pointer to the place where
828  * it should be placed, if needed.
829  *
830  * @param data data to encode
831  * @param size size of data (in bytes)
832  * @param out buffer to fill
833  * @param out_size size of the buffer. Must be large enough to hold
834  * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
835  * @return pointer to the next byte in 'out' or NULL on error.
836  */
837 char *
838 GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t out_size)
839 {
840   /**
841    * 32 characters for encoding
842    */
843   static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
844   unsigned int wpos;
845   unsigned int rpos;
846   unsigned int bits;
847   unsigned int vbit;
848   const unsigned char *udata;
849
850   GNUNET_assert (data != NULL);
851   GNUNET_assert (out != NULL);
852   udata = data;
853   if (out_size < (((size*8) + ((size*8) % 5)) % 5))
854   {
855     GNUNET_break (0);
856     return NULL;
857   }
858   vbit = 0;
859   wpos = 0;
860   rpos = 0;
861   bits = 0;
862   while ((rpos < size) || (vbit > 0))
863   {
864     if ((rpos < size) && (vbit < 5))
865     {
866       bits = (bits << 8) | udata[rpos++];   /* eat 8 more bits */
867       vbit += 8;
868     }
869     if (vbit < 5)
870     {
871       bits <<= (5 - vbit);      /* zero-padding */
872       GNUNET_assert (vbit == ((size * 8) % 5));
873       vbit = 5;
874     }
875     if (wpos >= out_size)
876     {
877       GNUNET_break (0);
878       return NULL;
879     }
880     out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
881     vbit -= 5;
882   }
883   GNUNET_assert (vbit == 0);
884   if (wpos < out_size)
885     out[wpos] = '\0';
886   return &out[wpos];
887 }
888
889
890 /**
891  * Convert ASCII encoding back to data
892  * out_size must match exactly the size of the data before it was encoded.
893  *
894  * @param enc the encoding
895  * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
896  * @param out location where to store the decoded data
897  * @param out_size size of the output buffer @a out
898  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
899  */
900 int
901 GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
902                                void *out, size_t out_size)
903 {
904   unsigned int rpos;
905   unsigned int wpos;
906   unsigned int bits;
907   unsigned int vbit;
908   int ret;
909   int shift;
910   unsigned char *uout;
911   unsigned int encoded_len = out_size * 8;
912
913   if (0 == enclen)
914   {
915     if (0 == out_size)
916       return GNUNET_OK;
917     return GNUNET_SYSERR;
918   }
919   uout = out;
920   wpos = out_size;
921   rpos = enclen;
922   if ((encoded_len % 5) > 0)
923   {
924     vbit = encoded_len % 5; /* padding! */
925     shift = 5 - vbit;
926     bits = (ret = getValue__ (enc[--rpos])) >> (5 - (encoded_len % 5));
927   }
928   else
929   {
930     vbit = 5;
931     shift = 0;
932     bits = (ret = getValue__ (enc[--rpos]));
933   }
934   if ((encoded_len + shift) / 5 != enclen)
935     return GNUNET_SYSERR;
936   if (-1 == ret)
937     return GNUNET_SYSERR;
938   while (wpos > 0)
939   {
940     if (0 == rpos)
941     {
942       GNUNET_break (0);
943       return GNUNET_SYSERR;
944     }
945     bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
946     if (-1 == ret)
947       return GNUNET_SYSERR;
948     vbit += 5;
949     if (vbit >= 8)
950     {
951       uout[--wpos] = (unsigned char) bits;
952       bits >>= 8;
953       vbit -= 8;
954     }
955   }
956   if ( (0 != rpos) ||
957        (0 != vbit) )
958     return GNUNET_SYSERR;
959   return GNUNET_OK;
960 }
961
962
963 /**
964  * Parse a path that might be an URI.
965  *
966  * @param path path to parse. Must be NULL-terminated.
967  * @param scheme_part a pointer to 'char *' where a pointer to a string that
968  *        represents the URI scheme will be stored. Can be NULL. The string is
969  *        allocated by the function, and should be freed by GNUNET_free() when
970  *        it is no longer needed.
971  * @param path_part a pointer to 'const char *' where a pointer to the path
972  *        part of the URI will be stored. Can be NULL. Points to the same block
973  *        of memory as 'path', and thus must not be freed. Might point to '\0',
974  *        if path part is zero-length.
975  * @return GNUNET_YES if it's an URI, GNUNET_NO otherwise. If 'path' is not
976  *         an URI, '* scheme_part' and '*path_part' will remain unchanged
977  *         (if they weren't NULL).
978  */
979 int
980 GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part,
981     const char **path_part)
982 {
983   size_t len;
984   int i, end;
985   int pp_state = 0;
986   const char *post_scheme_part = NULL;
987   len = strlen (path);
988   for (end = 0, i = 0; !end && i < len; i++)
989   {
990     switch (pp_state)
991     {
992     case 0:
993       if (path[i] == ':' && i > 0)
994       {
995         pp_state += 1;
996         continue;
997       }
998       if (!((path[i] >= 'A' && path[i] <= 'Z') || (path[i] >= 'a' && path[i] <= 'z')
999           || (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || path[i] == '-'
1000           || (path[i] == '.')))
1001         end = 1;
1002       break;
1003     case 1:
1004     case 2:
1005       if (path[i] == '/')
1006       {
1007         pp_state += 1;
1008         continue;
1009       }
1010       end = 1;
1011       break;
1012     case 3:
1013       post_scheme_part = &path[i];
1014       end = 1;
1015       break;
1016     default:
1017       end = 1;
1018     }
1019   }
1020   if (post_scheme_part == NULL)
1021     return GNUNET_NO;
1022   if (scheme_part)
1023   {
1024     *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
1025     memcpy (*scheme_part, path, post_scheme_part - path);
1026     (*scheme_part)[post_scheme_part - path] = '\0';
1027   }
1028   if (path_part)
1029     *path_part = post_scheme_part;
1030   return GNUNET_YES;
1031 }
1032
1033
1034 /**
1035  * Check whether @a filename is absolute or not, and if it's an URI
1036  *
1037  * @param filename filename to check
1038  * @param can_be_uri #GNUNET_YES to check for being URI, #GNUNET_NO - to
1039  *        assume it's not URI
1040  * @param r_is_uri a pointer to an int that is set to #GNUNET_YES if @a filename
1041  *        is URI and to #GNUNET_NO otherwise. Can be NULL. If @a can_be_uri is
1042  *        not #GNUNET_YES, `* r_is_uri` is set to #GNUNET_NO.
1043  * @param r_uri_scheme a pointer to a char * that is set to a pointer to URI scheme.
1044  *        The string is allocated by the function, and should be freed with
1045  *        GNUNET_free(). Can be NULL.
1046  * @return #GNUNET_YES if @a filename is absolute, #GNUNET_NO otherwise.
1047  */
1048 int
1049 GNUNET_STRINGS_path_is_absolute (const char *filename,
1050                                  int can_be_uri,
1051                                  int *r_is_uri,
1052                                  char **r_uri_scheme)
1053 {
1054 #if WINDOWS
1055   size_t len;
1056 #endif
1057   const char *post_scheme_path;
1058   int is_uri;
1059   char * uri;
1060   /* consider POSIX paths to be absolute too, even on W32,
1061    * as plibc expansion will fix them for us.
1062    */
1063   if (filename[0] == '/')
1064     return GNUNET_YES;
1065   if (can_be_uri)
1066   {
1067     is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1068     if (r_is_uri)
1069       *r_is_uri = is_uri;
1070     if (is_uri)
1071     {
1072       if (r_uri_scheme)
1073         *r_uri_scheme = uri;
1074       else
1075         GNUNET_free_non_null (uri);
1076 #if WINDOWS
1077       len = strlen(post_scheme_path);
1078       /* Special check for file:///c:/blah
1079        * We want to parse 'c:/', not '/c:/'
1080        */
1081       if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':')
1082         post_scheme_path = &post_scheme_path[1];
1083 #endif
1084       return GNUNET_STRINGS_path_is_absolute (post_scheme_path, GNUNET_NO, NULL, NULL);
1085     }
1086   }
1087   else
1088   {
1089     if (r_is_uri)
1090       *r_is_uri = GNUNET_NO;
1091   }
1092 #if WINDOWS
1093   len = strlen (filename);
1094   if (len >= 3 &&
1095       ((filename[0] >= 'A' && filename[0] <= 'Z')
1096       || (filename[0] >= 'a' && filename[0] <= 'z'))
1097       && filename[1] == ':' && (filename[2] == '/' || filename[2] == '\\'))
1098     return GNUNET_YES;
1099 #endif
1100   return GNUNET_NO;
1101 }
1102
1103 #if MINGW
1104 #define         _IFMT           0170000 /* type of file */
1105 #define         _IFLNK          0120000 /* symbolic link */
1106 #define  S_ISLNK(m)     (((m)&_IFMT) == _IFLNK)
1107 #endif
1108
1109
1110 /**
1111  * Perform @a checks on @a filename.
1112  *
1113  * @param filename file to check
1114  * @param checks checks to perform
1115  * @return #GNUNET_YES if all checks pass, #GNUNET_NO if at least one of them
1116  *         fails, #GNUNET_SYSERR when a check can't be performed
1117  */
1118 int
1119 GNUNET_STRINGS_check_filename (const char *filename,
1120                                enum GNUNET_STRINGS_FilenameCheck checks)
1121 {
1122   struct stat st;
1123   if ( (NULL == filename) || (filename[0] == '\0') )
1124     return GNUNET_SYSERR;
1125   if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1126     if (!GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
1127       return GNUNET_NO;
1128   if (0 != (checks & (GNUNET_STRINGS_CHECK_EXISTS
1129                       | GNUNET_STRINGS_CHECK_IS_DIRECTORY
1130                       | GNUNET_STRINGS_CHECK_IS_LINK)))
1131   {
1132     if (0 != STAT (filename, &st))
1133     {
1134       if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1135         return GNUNET_NO;
1136       else
1137         return GNUNET_SYSERR;
1138     }
1139   }
1140   if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1141     if (!S_ISDIR (st.st_mode))
1142       return GNUNET_NO;
1143   if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1144     if (!S_ISLNK (st.st_mode))
1145       return GNUNET_NO;
1146   return GNUNET_YES;
1147 }
1148
1149
1150 /**
1151  * Tries to convert 'zt_addr' string to an IPv6 address.
1152  * The string is expected to have the format "[ABCD::01]:80".
1153  *
1154  * @param zt_addr 0-terminated string. May be mangled by the function.
1155  * @param addrlen length of @a zt_addr (not counting 0-terminator).
1156  * @param r_buf a buffer to fill. Initially gets filled with zeroes,
1157  *        then its sin6_port, sin6_family and sin6_addr are set appropriately.
1158  * @return #GNUNET_OK if conversion succeded.
1159  *         #GNUNET_SYSERR otherwise, in which
1160  *         case the contents of @a r_buf are undefined.
1161  */
1162 int
1163 GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
1164                                 uint16_t addrlen,
1165                                 struct sockaddr_in6 *r_buf)
1166 {
1167   char zbuf[addrlen + 1];
1168   int ret;
1169   char *port_colon;
1170   unsigned int port;
1171
1172   if (addrlen < 6)
1173     return GNUNET_SYSERR;
1174   memcpy (zbuf, zt_addr, addrlen);
1175   if ('[' != zbuf[0])
1176   {
1177     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1178                 _("IPv6 address did not start with `['\n"));
1179     return GNUNET_SYSERR;
1180   }
1181   zbuf[addrlen] = '\0';
1182   port_colon = strrchr (zbuf, ':');
1183   if (NULL == port_colon)
1184   {
1185     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1186                 _("IPv6 address did contain ':' to separate port number\n"));
1187     return GNUNET_SYSERR;
1188   }
1189   if (']' != *(port_colon - 1))
1190   {
1191     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1192                 _("IPv6 address did contain ']' before ':' to separate port number\n"));
1193     return GNUNET_SYSERR;
1194   }
1195   ret = SSCANF (port_colon, ":%u", &port);
1196   if ( (1 != ret) || (port > 65535) )
1197   {
1198     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1199                 _("IPv6 address did contain a valid port number after the last ':'\n"));
1200     return GNUNET_SYSERR;
1201   }
1202   *(port_colon-1) = '\0';
1203   memset (r_buf, 0, sizeof (struct sockaddr_in6));
1204   ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1205   if (ret <= 0)
1206   {
1207     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1208                 _("Invalid IPv6 address `%s': %s\n"),
1209                 &zbuf[1],
1210                 STRERROR (errno));
1211     return GNUNET_SYSERR;
1212   }
1213   r_buf->sin6_port = htons (port);
1214   r_buf->sin6_family = AF_INET6;
1215 #if HAVE_SOCKADDR_IN_SIN_LEN
1216   r_buf->sin6_len = (u_char) sizeof (struct sockaddr_in6);
1217 #endif
1218   return GNUNET_OK;
1219 }
1220
1221
1222 /**
1223  * Tries to convert 'zt_addr' string to an IPv4 address.
1224  * The string is expected to have the format "1.2.3.4:80".
1225  *
1226  * @param zt_addr 0-terminated string. May be mangled by the function.
1227  * @param addrlen length of @a zt_addr (not counting 0-terminator).
1228  * @param r_buf a buffer to fill.
1229  * @return #GNUNET_OK if conversion succeded.
1230  *         #GNUNET_SYSERR otherwise, in which case
1231  *         the contents of @a r_buf are undefined.
1232  */
1233 int
1234 GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen,
1235                                 struct sockaddr_in *r_buf)
1236 {
1237   unsigned int temps[4];
1238   unsigned int port;
1239   unsigned int cnt;
1240
1241   if (addrlen < 9)
1242     return GNUNET_SYSERR;
1243   cnt = SSCANF (zt_addr, "%u.%u.%u.%u:%u", &temps[0], &temps[1], &temps[2], &temps[3], &port);
1244   if (5 != cnt)
1245     return GNUNET_SYSERR;
1246   for (cnt = 0; cnt < 4; cnt++)
1247     if (temps[cnt] > 0xFF)
1248       return GNUNET_SYSERR;
1249   if (port > 65535)
1250     return GNUNET_SYSERR;
1251   r_buf->sin_family = AF_INET;
1252   r_buf->sin_port = htons (port);
1253   r_buf->sin_addr.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) +
1254                                   (temps[2] << 8) + temps[3]);
1255 #if HAVE_SOCKADDR_IN_SIN_LEN
1256   r_buf->sin_len = (u_char) sizeof (struct sockaddr_in);
1257 #endif
1258   return GNUNET_OK;
1259 }
1260
1261
1262 /**
1263  * Tries to convert @a addr string to an IP (v4 or v6) address.
1264  * Will automatically decide whether to treat 'addr' as v4 or v6 address.
1265  *
1266  * @param addr a string, may not be 0-terminated.
1267  * @param addrlen number of bytes in @a addr (if addr is 0-terminated,
1268  *        0-terminator should not be counted towards addrlen).
1269  * @param r_buf a buffer to fill.
1270  * @return #GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
1271  *         case the contents of r_buf are undefined.
1272  */
1273 int
1274 GNUNET_STRINGS_to_address_ip (const char *addr,
1275                               uint16_t addrlen,
1276                               struct sockaddr_storage *r_buf)
1277 {
1278   if (addr[0] == '[')
1279     return GNUNET_STRINGS_to_address_ipv6 (addr,
1280                                            addrlen,
1281                                            (struct sockaddr_in6 *) r_buf);
1282   return GNUNET_STRINGS_to_address_ipv4 (addr,
1283                                          addrlen,
1284                                          (struct sockaddr_in *) r_buf);
1285 }
1286
1287
1288 /**
1289  * Makes a copy of argv that consists of a single memory chunk that can be
1290  * freed with a single call to GNUNET_free ();
1291  */
1292 static char *const *
1293 _make_continuous_arg_copy (int argc,
1294                            char *const *argv)
1295 {
1296   size_t argvsize = 0;
1297   int i;
1298   char **new_argv;
1299   char *p;
1300   for (i = 0; i < argc; i++)
1301     argvsize += strlen (argv[i]) + 1 + sizeof (char *);
1302   new_argv = GNUNET_malloc (argvsize + sizeof (char *));
1303   p = (char *) &new_argv[argc + 1];
1304   for (i = 0; i < argc; i++)
1305   {
1306     new_argv[i] = p;
1307     strcpy (p, argv[i]);
1308     p += strlen (argv[i]) + 1;
1309   }
1310   new_argv[argc] = NULL;
1311   return (char *const *) new_argv;
1312 }
1313
1314
1315 /**
1316  * Returns utf-8 encoded arguments.
1317  * Does nothing (returns a copy of argc and argv) on any platform
1318  * other than W32.
1319  * Returned argv has u8argv[u8argc] == NULL.
1320  * Returned argv is a single memory block, and can be freed with a single
1321  *   GNUNET_free () call.
1322  *
1323  * @param argc argc (as given by main())
1324  * @param argv argv (as given by main())
1325  * @param u8argc a location to store new argc in (though it's th same as argc)
1326  * @param u8argv a location to store new argv in
1327  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1328  */
1329 int
1330 GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
1331 {
1332 #if WINDOWS
1333   wchar_t *wcmd;
1334   wchar_t **wargv;
1335   int wargc;
1336   int i;
1337   char **split_u8argv;
1338
1339   wcmd = GetCommandLineW ();
1340   if (NULL == wcmd)
1341     return GNUNET_SYSERR;
1342   wargv = CommandLineToArgvW (wcmd, &wargc);
1343   if (NULL == wargv)
1344     return GNUNET_SYSERR;
1345
1346   split_u8argv = GNUNET_malloc (argc * sizeof (char *));
1347
1348   for (i = 0; i < wargc; i++)
1349   {
1350     size_t strl;
1351     /* Hopefully it will allocate us NUL-terminated strings... */
1352     split_u8argv[i] = (char *) u16_to_u8 (wargv[i], wcslen (wargv[i]) + 1, NULL, &strl);
1353     if (NULL == split_u8argv[i])
1354     {
1355       int j;
1356       for (j = 0; j < i; j++)
1357         free (split_u8argv[j]);
1358       GNUNET_free (split_u8argv);
1359       LocalFree (wargv);
1360       return GNUNET_SYSERR;
1361     }
1362   }
1363
1364   *u8argv = _make_continuous_arg_copy (wargc, split_u8argv);
1365   *u8argc = wargc;
1366
1367   for (i = 0; i < wargc; i++)
1368     free (split_u8argv[i]);
1369   free (split_u8argv);
1370   return GNUNET_OK;
1371 #else
1372   char *const *new_argv = (char *const *) _make_continuous_arg_copy (argc, argv);
1373   *u8argv = new_argv;
1374   *u8argc = argc;
1375   return GNUNET_OK;
1376 #endif
1377 }
1378
1379
1380 /**
1381  * Parse the given port policy.  The format is
1382  * "[!]SPORT[-DPORT]".
1383  *
1384  * @param port_policy string to parse
1385  * @param pp policy to fill in
1386  * @return #GNUNET_OK on success, #GNUNET_SYSERR if the
1387  *         @a port_policy is malformed
1388  */
1389 static int
1390 parse_port_policy (const char *port_policy,
1391                    struct GNUNET_STRINGS_PortPolicy *pp)
1392 {
1393   const char *pos;
1394   int s;
1395   int e;
1396   char eol[2];
1397
1398   pos = port_policy;
1399   if ('!' == *pos)
1400   {
1401     pp->negate_portrange = GNUNET_YES;
1402     pos++;
1403   }
1404   if (2 == sscanf (pos,
1405                    "%u-%u%1s",
1406                    &s, &e, eol))
1407   {
1408     if ( (0 == s) ||
1409          (s > 0xFFFF) ||
1410          (e < s) ||
1411          (e > 0xFFFF) )
1412     {
1413       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1414                   _("Port not in range\n"));
1415       return GNUNET_SYSERR;
1416     }
1417     pp->start_port = (uint16_t) s;
1418     pp->end_port = (uint16_t) e;
1419     return GNUNET_OK;
1420   }
1421   if (1 == sscanf (pos,
1422                    "%u%1s",
1423                    &s,
1424                    eol))
1425   {
1426     if ( (0 == s) ||
1427          (s > 0xFFFF) )
1428     {
1429       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1430                   _("Port not in range\n"));
1431       return GNUNET_SYSERR;
1432     }
1433
1434     pp->start_port = (uint16_t) s;
1435     pp->end_port = (uint16_t) s;
1436     return GNUNET_OK;
1437   }
1438   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1439               _("Malformed port policy `%s'\n"),
1440               port_policy);
1441   return GNUNET_SYSERR;
1442 }
1443
1444
1445 /**
1446  * Parse an IPv4 network policy. The argument specifies a list of
1447  * subnets. The format is
1448  * <tt>(network[/netmask][:SPORT[-DPORT]];)*</tt> (no whitespace, must
1449  * be terminated with a semicolon). The network must be given in
1450  * dotted-decimal notation. The netmask can be given in CIDR notation
1451  * (/16) or in dotted-decimal (/255.255.0.0).
1452  *
1453  * @param routeListX a string specifying the IPv4 subnets
1454  * @return the converted list, terminated with all zeros;
1455  *         NULL if the synatx is flawed
1456  */
1457 struct GNUNET_STRINGS_IPv4NetworkPolicy *
1458 GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
1459 {
1460   unsigned int count;
1461   unsigned int i;
1462   unsigned int j;
1463   unsigned int len;
1464   int cnt;
1465   unsigned int pos;
1466   unsigned int temps[8];
1467   int slash;
1468   struct GNUNET_STRINGS_IPv4NetworkPolicy *result;
1469   int colon;
1470   int end;
1471   char *routeList;
1472
1473   if (NULL == routeListX)
1474     return NULL;
1475   len = strlen (routeListX);
1476   if (0 == len)
1477     return NULL;
1478   routeList = GNUNET_strdup (routeListX);
1479   count = 0;
1480   for (i = 0; i < len; i++)
1481     if (routeList[i] == ';')
1482       count++;
1483   result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv4NetworkPolicy) * (count + 1));
1484   i = 0;
1485   pos = 0;
1486   while (i < count)
1487   {
1488     for (colon = pos; ':' != routeList[colon]; colon++)
1489       if ( (';' == routeList[colon]) ||
1490            ('\0' == routeList[colon]) )
1491         break;
1492     for (end = colon; ';' != routeList[end]; end++)
1493       if ('\0' == routeList[end])
1494         break;
1495     if ('\0' == routeList[end])
1496       break;
1497     routeList[end] = '\0';
1498     if (':' == routeList[colon])
1499     {
1500       routeList[colon] = '\0';
1501       if (GNUNET_OK != parse_port_policy (&routeList[colon + 1],
1502                                           &result[i].pp))
1503         break;
1504     }
1505     cnt =
1506         SSCANF (&routeList[pos],
1507                 "%u.%u.%u.%u/%u.%u.%u.%u",
1508                 &temps[0],
1509                 &temps[1],
1510                 &temps[2],
1511                 &temps[3],
1512                 &temps[4],
1513                 &temps[5],
1514                 &temps[6],
1515                 &temps[7]);
1516     if (8 == cnt)
1517     {
1518       for (j = 0; j < 8; j++)
1519         if (temps[j] > 0xFF)
1520         {
1521           LOG (GNUNET_ERROR_TYPE_WARNING,
1522                _("Invalid format for IP: `%s'\n"),
1523                &routeList[pos]);
1524           GNUNET_free (result);
1525           GNUNET_free (routeList);
1526           return NULL;
1527         }
1528       result[i].network.s_addr =
1529           htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
1530                  temps[3]);
1531       result[i].netmask.s_addr =
1532           htonl ((temps[4] << 24) + (temps[5] << 16) + (temps[6] << 8) +
1533                  temps[7]);
1534       pos = end + 1;
1535       i++;
1536       continue;
1537     }
1538     /* try second notation */
1539     cnt =
1540         SSCANF (&routeList[pos],
1541                 "%u.%u.%u.%u/%u",
1542                 &temps[0],
1543                 &temps[1],
1544                 &temps[2],
1545                 &temps[3],
1546                 &slash);
1547     if (5 == cnt)
1548     {
1549       for (j = 0; j < 4; j++)
1550         if (temps[j] > 0xFF)
1551         {
1552           LOG (GNUNET_ERROR_TYPE_WARNING,
1553                _("Invalid format for IP: `%s'\n"),
1554                &routeList[pos]);
1555           GNUNET_free (result);
1556           GNUNET_free (routeList);
1557           return NULL;
1558         }
1559       result[i].network.s_addr =
1560           htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
1561                  temps[3]);
1562       if ((slash <= 32) && (slash >= 0))
1563       {
1564         result[i].netmask.s_addr = 0;
1565         while (slash > 0)
1566         {
1567           result[i].netmask.s_addr =
1568               (result[i].netmask.s_addr >> 1) + 0x80000000;
1569           slash--;
1570         }
1571         result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1572         pos = end + 1;
1573         i++;
1574         continue;
1575       }
1576       else
1577       {
1578         LOG (GNUNET_ERROR_TYPE_WARNING,
1579              _("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1580              slash);
1581         GNUNET_free (result);
1582           GNUNET_free (routeList);
1583         return NULL;            /* error */
1584       }
1585     }
1586     /* try third notation */
1587     slash = 32;
1588     cnt =
1589         SSCANF (&routeList[pos],
1590                 "%u.%u.%u.%u",
1591                 &temps[0],
1592                 &temps[1],
1593                 &temps[2],
1594                 &temps[3]);
1595     if (4 == cnt)
1596     {
1597       for (j = 0; j < 4; j++)
1598         if (temps[j] > 0xFF)
1599         {
1600           LOG (GNUNET_ERROR_TYPE_WARNING,
1601                _("Invalid format for IP: `%s'\n"),
1602                &routeList[pos]);
1603           GNUNET_free (result);
1604           GNUNET_free (routeList);
1605           return NULL;
1606         }
1607       result[i].network.s_addr =
1608           htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) +
1609                  temps[3]);
1610       result[i].netmask.s_addr = 0;
1611       while (slash > 0)
1612       {
1613         result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
1614         slash--;
1615       }
1616       result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1617       pos = end + 1;
1618       i++;
1619       continue;
1620     }
1621     LOG (GNUNET_ERROR_TYPE_WARNING,
1622          _("Invalid format for IP: `%s'\n"),
1623          &routeList[pos]);
1624     GNUNET_free (result);
1625     GNUNET_free (routeList);
1626     return NULL;                /* error */
1627   }
1628   if (pos < strlen (routeList))
1629   {
1630     LOG (GNUNET_ERROR_TYPE_WARNING,
1631          _("Invalid format: `%s'\n"),
1632          &routeListX[pos]);
1633     GNUNET_free (result);
1634     GNUNET_free (routeList);
1635     return NULL;                /* oops */
1636   }
1637   GNUNET_free (routeList);
1638   return result;                /* ok */
1639 }
1640
1641
1642 /**
1643  * Parse an IPv6 network policy. The argument specifies a list of
1644  * subnets. The format is <tt>(network[/netmask[:SPORT[-DPORT]]];)*</tt>
1645  * (no whitespace, must be terminated with a semicolon). The network
1646  * must be given in colon-hex notation.  The netmask must be given in
1647  * CIDR notation (/16) or can be omitted to specify a single host.
1648  * Note that the netmask is mandatory if ports are specified.
1649  *
1650  * @param routeListX a string specifying the policy
1651  * @return the converted list, 0-terminated, NULL if the synatx is flawed
1652  */
1653 struct GNUNET_STRINGS_IPv6NetworkPolicy *
1654 GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
1655 {
1656   unsigned int count;
1657   unsigned int i;
1658   unsigned int len;
1659   unsigned int pos;
1660   int start;
1661   int slash;
1662   int ret;
1663   char *routeList;
1664   struct GNUNET_STRINGS_IPv6NetworkPolicy *result;
1665   unsigned int bits;
1666   unsigned int off;
1667   int save;
1668   int colon;
1669
1670   if (NULL == routeListX)
1671     return NULL;
1672   len = strlen (routeListX);
1673   if (0 == len)
1674     return NULL;
1675   routeList = GNUNET_strdup (routeListX);
1676   count = 0;
1677   for (i = 0; i < len; i++)
1678     if (';' == routeList[i])
1679       count++;
1680   if (';' != routeList[len - 1])
1681   {
1682     LOG (GNUNET_ERROR_TYPE_WARNING,
1683          _("Invalid network notation (does not end with ';': `%s')\n"),
1684          routeList);
1685     GNUNET_free (routeList);
1686     return NULL;
1687   }
1688
1689   result = GNUNET_malloc (sizeof (struct GNUNET_STRINGS_IPv6NetworkPolicy) * (count + 1));
1690   i = 0;
1691   pos = 0;
1692   while (i < count)
1693   {
1694     start = pos;
1695     while (';' != routeList[pos])
1696       pos++;
1697     slash = pos;
1698     while ((slash >= start) && (routeList[slash] != '/'))
1699       slash--;
1700
1701     if (slash < start)
1702     {
1703       memset (&result[i].netmask,
1704               0xFF,
1705               sizeof (struct in6_addr));
1706       slash = pos;
1707     }
1708     else
1709     {
1710       routeList[pos] = '\0';
1711       for (colon = pos; ':' != routeList[colon]; colon--)
1712         if ('/' == routeList[colon])
1713           break;
1714       if (':' == routeList[colon])
1715       {
1716         routeList[colon] = '\0';
1717         if (GNUNET_OK != parse_port_policy (&routeList[colon + 1],
1718                                             &result[i].pp))
1719         {
1720           GNUNET_free (result);
1721           GNUNET_free (routeList);
1722           return NULL;
1723         }
1724       }
1725       ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
1726       if (ret <= 0)
1727       {
1728         save = errno;
1729         if ((1 != SSCANF (&routeList[slash + 1], "%u", &bits)) || (bits > 128))
1730         {
1731           if (0 == ret)
1732             LOG (GNUNET_ERROR_TYPE_WARNING,
1733                  _("Wrong format `%s' for netmask\n"),
1734                  &routeList[slash + 1]);
1735           else
1736           {
1737             errno = save;
1738             LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1739           }
1740           GNUNET_free (result);
1741           GNUNET_free (routeList);
1742           return NULL;
1743         }
1744         off = 0;
1745         while (bits > 8)
1746         {
1747           result[i].netmask.s6_addr[off++] = 0xFF;
1748           bits -= 8;
1749         }
1750         while (bits > 0)
1751         {
1752           result[i].netmask.s6_addr[off] =
1753               (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1754           bits--;
1755         }
1756       }
1757     }
1758     routeList[slash] = '\0';
1759     ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
1760     if (ret <= 0)
1761     {
1762       if (0 == ret)
1763         LOG (GNUNET_ERROR_TYPE_WARNING,
1764              _("Wrong format `%s' for network\n"),
1765              &routeList[slash + 1]);
1766       else
1767         LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
1768                       "inet_pton");
1769       GNUNET_free (result);
1770       GNUNET_free (routeList);
1771       return NULL;
1772     }
1773     pos++;
1774     i++;
1775   }
1776   GNUNET_free (routeList);
1777   return result;
1778 }
1779
1780
1781
1782 /** ******************** Base64 encoding ***********/
1783
1784 #define FILLCHAR '='
1785 static char *cvt =
1786     "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/";
1787
1788
1789 /**
1790  * Encode into Base64.
1791  *
1792  * @param data the data to encode
1793  * @param len the length of the input
1794  * @param output where to write the output (*output should be NULL,
1795  *   is allocated)
1796  * @return the size of the output
1797  */
1798 size_t
1799 GNUNET_STRINGS_base64_encode (const char *data,
1800                               size_t len,
1801                               char **output)
1802 {
1803   size_t i;
1804   char c;
1805   size_t ret;
1806   char *opt;
1807
1808   ret = 0;
1809   opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1810   *output = opt;
1811   for (i = 0; i < len; ++i)
1812   {
1813     c = (data[i] >> 2) & 0x3f;
1814     opt[ret++] = cvt[(int) c];
1815     c = (data[i] << 4) & 0x3f;
1816     if (++i < len)
1817       c |= (data[i] >> 4) & 0x0f;
1818     opt[ret++] = cvt[(int) c];
1819     if (i < len)
1820     {
1821       c = (data[i] << 2) & 0x3f;
1822       if (++i < len)
1823         c |= (data[i] >> 6) & 0x03;
1824       opt[ret++] = cvt[(int) c];
1825     }
1826     else
1827     {
1828       ++i;
1829       opt[ret++] = FILLCHAR;
1830     }
1831     if (i < len)
1832     {
1833       c = data[i] & 0x3f;
1834       opt[ret++] = cvt[(int) c];
1835     }
1836     else
1837     {
1838       opt[ret++] = FILLCHAR;
1839     }
1840   }
1841   opt[ret++] = FILLCHAR;
1842   return ret;
1843 }
1844
1845 #define cvtfind(a)( (((a) >= 'A')&&((a) <= 'Z'))? (a)-'A'\
1846                    :(((a)>='a')&&((a)<='z')) ? (a)-'a'+26\
1847                    :(((a)>='0')&&((a)<='9')) ? (a)-'0'+52\
1848            :((a) == '+') ? 62\
1849            :((a) == '/') ? 63 : -1)
1850
1851
1852 /**
1853  * Decode from Base64.
1854  *
1855  * @param data the data to encode
1856  * @param len the length of the input
1857  * @param output where to write the output (*output should be NULL,
1858  *   is allocated)
1859  * @return the size of the output
1860  */
1861 size_t
1862 GNUNET_STRINGS_base64_decode (const char *data,
1863                               size_t len, char **output)
1864 {
1865   size_t i;
1866   char c;
1867   char c1;
1868   size_t ret = 0;
1869
1870 #define CHECK_CRLF  while (data[i] == '\r' || data[i] == '\n') {\
1871                         GNUNET_log(GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, "ignoring CR/LF\n"); \
1872                         i++; \
1873                         if (i >= len) goto END;  \
1874                 }
1875
1876   *output = GNUNET_malloc ((len * 3 / 4) + 8);
1877   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878               "base64_decode decoding len=%d\n",
1879               (int) len);
1880   for (i = 0; i < len; ++i)
1881   {
1882     CHECK_CRLF;
1883     if (FILLCHAR == data[i])
1884       break;
1885     c = (char) cvtfind (data[i]);
1886     ++i;
1887     CHECK_CRLF;
1888     c1 = (char) cvtfind (data[i]);
1889     c = (c << 2) | ((c1 >> 4) & 0x3);
1890     (*output)[ret++] = c;
1891     if (++i < len)
1892     {
1893       CHECK_CRLF;
1894       c = data[i];
1895       if (FILLCHAR == c)
1896         break;
1897       c = (char) cvtfind (c);
1898       c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1899       (*output)[ret++] = c1;
1900     }
1901     if (++i < len)
1902     {
1903       CHECK_CRLF;
1904       c1 = data[i];
1905       if (FILLCHAR == c1)
1906         break;
1907
1908       c1 = (char) cvtfind (c1);
1909       c = ((c << 6) & 0xc0) | c1;
1910       (*output)[ret++] = c;
1911     }
1912   }
1913 END:
1914   return ret;
1915 }
1916
1917
1918
1919
1920
1921 /* end of strings.c */