2 This file is part of GNUnet.
3 (C) 2005, 2006 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
22 * @file util/strings.c
23 * @brief string functions
25 * @author Christian Grothoff
32 #include "gnunet_common.h"
33 #include "gnunet_strings_lib.h"
35 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
41 * Fill a buffer of the given size with
42 * count 0-terminated strings (given as varargs).
43 * If "buffer" is NULL, only compute the amount of
44 * space required (sum of "strlen(arg)+1").
46 * Unlike using "snprintf" with "%s", this function
47 * will add 0-terminators after each string. The
48 * "GNUNET_string_buffer_tokenize" function can be
49 * used to parse the buffer back into individual
52 * @param buffer the buffer to fill with strings, can
53 * be NULL in which case only the necessary
54 * amount of space will be calculated
55 * @param size number of bytes available in buffer
56 * @param count number of strings that follow
57 * @param ... count 0-terminated strings to copy to buffer
58 * @return number of bytes written to the buffer
59 * (or number of bytes that would have been written)
62 GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...)
73 s = va_arg (ap, const char *);
75 slen = strlen (s) + 1;
78 GNUNET_assert (needed + slen <= size);
79 memcpy (&buffer[needed], s, slen);
90 * Given a buffer of a given size, find "count"
91 * 0-terminated strings in the buffer and assign
92 * the count (varargs) of type "const char**" to the
93 * locations of the respective strings in the
96 * @param buffer the buffer to parse
97 * @param size size of the buffer
98 * @param count number of strings to locate
99 * @return offset of the character after the last 0-termination
100 * in the buffer, or 0 on error.
103 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
104 unsigned int count, ...)
112 va_start (ap, count);
115 r = va_arg (ap, const char **);
118 while ((needed < size) && (buffer[needed] != '\0'))
123 return 0; /* error */
126 needed++; /* skip 0-termination */
135 * Convert a given filesize into a fancy human-readable format.
137 * @param size number of bytes
138 * @return fancy representation of the size (possibly rounded) for humans
141 GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
143 const char *unit = _( /* size unit */ "b");
166 ret = GNUNET_malloc (32);
167 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
173 * Unit conversion table entry for 'convert_with_table'.
175 struct ConversionTable
178 * Name of the unit (or NULL for end of table).
183 * Factor to apply for this unit.
185 unsigned long long value;
190 * Convert a string of the form "4 X 5 Y" into a numeric value
191 * by interpreting "X" and "Y" as units and then multiplying
192 * the numbers with the values associated with the respective
193 * unit from the conversion table.
195 * @param input input string to parse
196 * @param table table with the conversion of unit names to numbers
197 * @param output where to store the result
198 * @return GNUNET_OK on success, GNUNET_SYSERR on error
201 convert_with_table (const char *input,
202 const struct ConversionTable *table,
203 unsigned long long *output)
205 unsigned long long ret;
208 unsigned long long last;
213 in = GNUNET_strdup (input);
214 for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
217 while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
219 if (table[i].name != NULL)
220 last *= table[i].value;
225 if (1 != SSCANF (tok, "%llu", &last))
228 return GNUNET_SYSERR; /* expected number */
240 * Convert a given fancy human-readable size to bytes.
242 * @param fancy_size human readable string (i.e. 1 MB)
243 * @param size set to the size in bytes
244 * @return GNUNET_OK on success, GNUNET_SYSERR on error
247 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
248 unsigned long long *size)
250 static const struct ConversionTable table[] =
255 { "MiB", 1024 * 1024},
256 { "MB", 1000 * 1000},
257 { "GiB", 1024 * 1024 * 1024},
258 { "GB", 1000 * 1000 * 1000},
259 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL},
260 { "TB", 1000LL * 1000LL * 1000LL * 1024LL},
261 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
262 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL},
263 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL},
264 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL},
268 return convert_with_table (fancy_size,
275 * Convert a given fancy human-readable time to our internal
278 * @param fancy_time human readable string (i.e. 1 minute)
279 * @param rtime set to the relative time
280 * @return GNUNET_OK on success, GNUNET_SYSERR on error
283 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
284 struct GNUNET_TIME_Relative *rtime)
286 static const struct ConversionTable table[] =
292 { "minutes", 60 * 1000},
294 { "h", 60 * 60 * 1000},
295 { "d", 24 * 60 * 60 * 1000},
296 { "a", 31536000000 /* year */ },
300 unsigned long long val;
302 ret = convert_with_table (fancy_time,
305 rtime->rel_value = (uint64_t) val;
310 * Convert the len characters long character sequence
311 * given in input that is in the given input charset
312 * to a string in given output charset.
313 * @return the converted string (0-terminated),
314 * if conversion fails, a copy of the orignal
315 * string is returned.
318 GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, const char *output_charset)
322 #if ENABLE_NLS && HAVE_ICONV
329 cd = iconv_open (output_charset, input_charset);
330 if (cd == (iconv_t) - 1)
332 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_open");
333 LOG (GNUNET_ERROR_TYPE_WARNING, _("Character sets requested were `%s'->`%s'\n"),
334 input_charset, output_charset);
335 ret = GNUNET_malloc (len + 1);
336 memcpy (ret, input, len);
340 tmpSize = 3 * len + 4;
341 tmp = GNUNET_malloc (tmpSize);
345 #if FREEBSD || DARWIN || WINDOWS
346 (const char **) &input,
350 &len, &itmp, &finSize) == SIZE_MAX)
352 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv");
355 ret = GNUNET_malloc (len + 1);
356 memcpy (ret, input, len);
360 ret = GNUNET_malloc (tmpSize - finSize + 1);
361 memcpy (ret, tmp, tmpSize - finSize);
362 ret[tmpSize - finSize] = '\0';
364 if (0 != iconv_close (cd))
365 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "iconv_close");
368 ret = GNUNET_malloc (len + 1);
369 memcpy (ret, input, len);
377 * Convert the len characters long character sequence
378 * given in input that is in the given charset
380 * @return the converted string (0-terminated),
381 * if conversion fails, a copy of the orignal
382 * string is returned.
385 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
387 return GNUNET_STRINGS_conv (input, len, charset, "UTF-8");
391 * Convert the len bytes-long UTF-8 string
392 * given in input to the given charset.
394 * @return the converted string (0-terminated),
395 * if conversion fails, a copy of the orignal
396 * string is returned.
399 GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
401 return GNUNET_STRINGS_conv (input, len, "UTF-8", charset);
407 * Complete filename (a la shell) from abbrevition.
408 * @param fil the name of the file, may contain ~/ or
409 * be relative to the current directory
410 * @returns the full file name,
411 * NULL is returned on error
414 GNUNET_STRINGS_filename_expand (const char *fil)
432 if (fil[0] == DIR_SEPARATOR)
433 /* absolute path, just copy */
434 return GNUNET_strdup (fil);
437 fm = getenv ("HOME");
440 LOG (GNUNET_ERROR_TYPE_WARNING,
441 _("Failed to expand `$HOME': environment variable `HOME' not set"));
444 fm = GNUNET_strdup (fm);
445 /* do not copy '~' */
448 /* skip over dir seperator to be consistent */
449 if (fil_ptr[0] == DIR_SEPARATOR)
460 buffer = GNUNET_malloc (len);
461 if (getcwd (buffer, len) != NULL)
466 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
469 GNUNET_free (buffer);
472 GNUNET_free (buffer);
477 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "getcwd");
478 buffer = getenv ("PWD"); /* alternative */
480 fm = GNUNET_strdup (buffer);
483 fm = GNUNET_strdup ("./"); /* give up */
485 n = strlen (fm) + 1 + strlen (fil_ptr) + 1;
486 buffer = GNUNET_malloc (n);
487 GNUNET_snprintf (buffer, n, "%s%s%s", fm,
488 (fm[strlen (fm) - 1] ==
489 DIR_SEPARATOR) ? "" : DIR_SEPARATOR_STR, fil_ptr);
493 fn = GNUNET_malloc (MAX_PATH + 1);
495 if ((lRet = plibc_conv_to_win_path (fil, fn)) != ERROR_SUCCESS)
497 SetErrnoFromWinError (lRet);
498 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "plibc_conv_to_win_path");
501 /* is the path relative? */
502 if ((strncmp (fn + 1, ":\\", 2) != 0) && (strncmp (fn, "\\\\", 2) != 0))
504 char szCurDir[MAX_PATH + 1];
506 lRet = GetCurrentDirectory (MAX_PATH + 1, szCurDir);
507 if (lRet + strlen (fn) + 1 > (MAX_PATH + 1))
509 SetErrnoFromWinError (ERROR_BUFFER_OVERFLOW);
510 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "GetCurrentDirectory");
513 buffer = GNUNET_malloc (MAX_PATH + 1);
514 GNUNET_snprintf (buffer, MAX_PATH + 1, "%s\\%s", szCurDir, fn);
525 * Give relative time in human-readable fancy format.
527 * @param delta time in milli seconds
528 * @return time as human-readable string
531 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta)
533 const char *unit = _( /* time unit */ "ms");
535 uint64_t dval = delta.rel_value;
537 if (delta.rel_value == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
538 return GNUNET_strdup (_("eternity"));
542 unit = _( /* time unit */ "s");
546 unit = _( /* time unit */ "m");
550 unit = _( /* time unit */ "h");
554 unit = _( /* time unit */ " days");
559 GNUNET_asprintf (&ret, "%llu %s", dval, unit);
565 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
566 * return value does not include the newline character.
568 * @param t time to convert
569 * @return absolute time in human-readable format
572 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
577 if (t.abs_value == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
578 return GNUNET_strdup (_("end of time"));
579 tt = t.abs_value / 1000;
581 ret = ctime_r (&tt, GNUNET_malloc (32));
583 ret = GNUNET_strdup (ctime (&tt));
585 ret[strlen (ret) - 1] = '\0';
592 * Returns a pointer to a part of filename (allocates nothing)!
594 * @param filename filename to extract basename from
595 * @return short (base) name of the file (that is, everything following the
596 * last directory separator in filename. If filename ends with a
597 * directory separator, the result will be a zero-length string.
598 * If filename has no directory separators, the result is filename
602 GNUNET_STRINGS_get_short_name (const char *filename)
604 const char *short_fn = filename;
606 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR))
614 * Get the numeric value corresponding to a character.
616 * @param a a character
617 * @return corresponding numeric value
620 getValue__ (unsigned char a)
622 if ((a >= '0') && (a <= '9'))
624 if ((a >= 'A') && (a <= 'V'))
625 return (a - 'A' + 10);
631 * Convert binary data to ASCII encoding. The ASCII encoding is rather
632 * GNUnet specific. It was chosen such that it only uses characters
633 * in [0-9A-V], can be produced without complex arithmetics and uses a
634 * small number of characters.
635 * Does not append 0-terminator, but returns a pointer to the place where
636 * it should be placed, if needed.
638 * @param data data to encode
639 * @param size size of data (in bytes)
640 * @param out buffer to fill
641 * @param out_size size of the buffer. Must be large enough to hold
642 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5 bytes
643 * @return pointer to the next byte in 'out' or NULL on error.
646 GNUNET_STRINGS_data_to_string (const unsigned char *data, size_t size, char *out, size_t out_size)
649 * 32 characters for encoding
651 static char *encTable__ = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
657 GNUNET_assert (data != NULL);
658 GNUNET_assert (out != NULL);
659 if (out_size < (((size*8) + ((size*8) % 5)) % 5))
668 while ((rpos < size) || (vbit > 0))
670 if ((rpos < size) && (vbit < 5))
672 bits = (bits << 8) | data[rpos++]; /* eat 8 more bits */
677 bits <<= (5 - vbit); /* zero-padding */
678 GNUNET_assert (vbit == ((size * 8) % 5));
681 if (wpos >= out_size)
686 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
689 if (wpos != out_size)
694 GNUNET_assert (vbit == 0);
700 * Convert ASCII encoding back to data
701 * out_size must match exactly the size of the data before it was encoded.
703 * @param enc the encoding
704 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
705 * @param out location where to store the decoded data
706 * @param out_size sizeof the output buffer
707 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
710 GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
711 unsigned char *out, size_t out_size)
719 int encoded_len = out_size * 8;
720 if (encoded_len % 5 > 0)
722 vbit = encoded_len % 5; /* padding! */
730 if ((encoded_len + shift) / 5 != enclen)
731 return GNUNET_SYSERR;
735 bits = (ret = getValue__ (enc[--rpos])) >> (5 - encoded_len % 5);
737 return GNUNET_SYSERR;
740 GNUNET_assert (rpos > 0);
741 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
743 return GNUNET_SYSERR;
747 out[--wpos] = (unsigned char) bits;
752 GNUNET_assert (rpos == 0);
753 GNUNET_assert (vbit == 0);
759 * Parse a path that might be an URI.
761 * @param path path to parse. Must be NULL-terminated.
762 * @param scheme_part a pointer to 'char *' where a pointer to a string that
763 * represents the URI scheme will be stored. Can be NULL. The string is
764 * allocated by the function, and should be freed by GNUNET_free() when
765 * it is no longer needed.
766 * @param path_part a pointer to 'const char *' where a pointer to the path
767 * part of the URI will be stored. Can be NULL. Points to the same block
768 * of memory as 'path', and thus must not be freed. Might point to '\0',
769 * if path part is zero-length.
770 * @return GNUNET_YES if it's an URI, GNUNET_NO otherwise. If 'path' is not
771 * an URI, '* scheme_part' and '*path_part' will remain unchanged
772 * (if they weren't NULL).
775 GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part,
776 const char **path_part)
781 const char *post_scheme_part = NULL;
783 for (end = 0, i = 0; !end && i < len; i++)
788 if (path[i] == ':' && i > 0)
793 if (!((path[i] >= 'A' && path[i] <= 'Z') || (path[i] >= 'a' && path[i] <= 'z')
794 || (path[i] >= '0' && path[i] <= '9') || path[i] == '+' || path[i] == '-'
795 || (path[i] == '.')))
808 post_scheme_part = &path[i];
815 if (post_scheme_part == NULL)
819 *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
820 memcpy (*scheme_part, path, post_scheme_part - path);
821 (*scheme_part)[post_scheme_part - path] = '\0';
824 *path_part = post_scheme_part;
830 * Check whether 'filename' is absolute or not, and if it's an URI
832 * @param filename filename to check
833 * @param can_be_uri GNUNET_YES to check for being URI, GNUNET_NO - to
834 * assume it's not URI
835 * @param r_is_uri a pointer to an int that is set to GNUNET_YES if 'filename'
836 * is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is
837 * not GNUNET_YES, *r_is_uri is set to GNUNET_NO.
838 * @param r_uri_scheme a pointer to a char * that is set to a pointer to URI scheme.
839 * The string is allocated by the function, and should be freed with
840 * GNUNET_free (). Can be NULL.
841 * @return GNUNET_YES if 'filename' is absolute, GNUNET_NO otherwise.
844 GNUNET_STRINGS_path_is_absolute (const char *filename, int can_be_uri,
845 int *r_is_uri, char **r_uri_scheme)
850 const char *post_scheme_path;
853 /* consider POSIX paths to be absolute too, even on W32,
854 * as plibc expansion will fix them for us.
856 if (filename[0] == '/')
860 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
868 GNUNET_free_non_null (uri);
870 len = strlen(post_scheme_path);
871 /* Special check for file:///c:/blah
872 * We want to parse 'c:/', not '/c:/'
874 if (post_scheme_path[0] == '/' && len >= 3 && post_scheme_path[2] == ':')
875 post_scheme_path = &post_scheme_path[1];
877 return GNUNET_STRINGS_path_is_absolute (post_scheme_path, GNUNET_NO, NULL, NULL);
884 *r_is_uri = GNUNET_NO;
887 len = strlen (filename);
889 ((filename[0] >= 'A' && filename[0] <= 'Z')
890 || (filename[0] >= 'a' && filename[0] <= 'z'))
891 && filename[1] == ':' && (filename[2] == '/' || filename[2] == '\\'))
898 #define _IFMT 0170000 /* type of file */
899 #define _IFLNK 0120000 /* symbolic link */
900 #define S_ISLNK(m) (((m)&_IFMT) == _IFLNK)
904 * Perform 'checks' on 'filename'
906 * @param filename file to check
907 * @param checks checks to perform
908 * @return GNUNET_YES if all checks pass, GNUNET_NO if at least one of them
909 * fails, GNUNET_SYSERR when a check can't be performed
912 GNUNET_STRINGS_check_filename (const char *filename,
913 enum GNUNET_STRINGS_FilenameCheck checks)
916 if (filename == NULL || filename[0] == '\0')
917 return GNUNET_SYSERR;
918 if (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE)
919 if (!GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
921 if (checks & (GNUNET_STRINGS_CHECK_EXISTS
922 | GNUNET_STRINGS_CHECK_IS_DIRECTORY
923 | GNUNET_STRINGS_CHECK_IS_LINK))
925 if (STAT (filename, &st))
927 if (checks & GNUNET_STRINGS_CHECK_EXISTS)
930 return GNUNET_SYSERR;
933 if (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY)
934 if (!S_ISDIR (st.st_mode))
936 if (checks & GNUNET_STRINGS_CHECK_IS_LINK)
937 if (!S_ISLNK (st.st_mode))
942 #define MAX_IPV6_ADDRLEN 47
943 #define MAX_IPV4_ADDRLEN 21
944 #define MAX_IP_ADDRLEN MAX_IPV6_ADDRLEN
948 * Tries to convert 'zt_addr' string to an IPv6 address.
950 * @param zt_addr 0-terminated string. May be mangled by the function.
951 * @param addrlen length of zt_addr (not counting 0-terminator).
952 * @param r_buf a buffer to fill. Initially gets filled with zeroes,
953 * then its sin6_port, sin6_family and sin6_addr are set appropriately.
954 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
955 * case the contents of r_buf are undefined.
958 GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
960 struct sockaddr_in6 *r_buf)
967 return GNUNET_SYSERR;
969 port_colon = strrchr (zt_addr, ':');
970 if (port_colon == NULL)
971 return GNUNET_SYSERR;
972 ret = SSCANF (port_colon, ":%u", &port);
973 if (ret != 1 || port > 65535)
974 return GNUNET_SYSERR;
975 port_colon[0] = '\0';
976 memset (r_buf, 0, sizeof (struct sockaddr_in6));
977 ret = inet_pton (AF_INET6, zt_addr, &r_buf->sin6_addr);
979 return GNUNET_SYSERR;
980 r_buf->sin6_port = htonl (port);
981 r_buf->sin6_family = AF_INET6;
987 * Tries to convert 'zt_addr' string to an IPv4 address.
989 * @param zt_addr 0-terminated string. May be mangled by the function.
990 * @param addrlen length of zt_addr (not counting 0-terminator).
991 * @param r_buf a buffer to fill.
992 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case
993 * the contents of r_buf are undefined.
996 GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen,
997 struct sockaddr_in *r_buf)
999 unsigned int temps[5];
1004 return GNUNET_SYSERR;
1006 cnt = SSCANF (zt_addr, "%u.%u.%u.%u:%u", &temps[0], &temps[1], &temps[2], &temps[3], &port);
1008 return GNUNET_SYSERR;
1010 for (cnt = 0; cnt < 4; cnt++)
1011 if (temps[cnt] > 0xFF)
1012 return GNUNET_SYSERR;
1014 return GNUNET_SYSERR;
1016 r_buf->sin_family = AF_INET;
1017 r_buf->sin_port = htonl (port);
1018 r_buf->sin_addr.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16) +
1019 (temps[2] << 8) + temps[3]);
1024 * Tries to convert 'addr' string to an IP (v4 or v6) address.
1025 * IPv6 address must have its address part enclosed in '()' parens
1027 * Will automatically decide whether to treat 'addr' as v4 or v6 address.
1029 * @param addr a string, may not be 0-terminated.
1030 * @param addrlen number of bytes in addr (if addr is 0-terminated,
1031 * 0-terminator should not be counted towards addrlen).
1032 * @param r_buf a buffer to fill.
1033 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
1034 * case the contents of r_buf are undefined.
1037 GNUNET_STRINGS_to_address_ip (const char *addr,
1039 struct sockaddr_storage *r_buf)
1042 char zt_addr[MAX_IP_ADDRLEN + 1];
1043 uint16_t zt_len = addrlen <= MAX_IP_ADDRLEN ? addrlen : MAX_IP_ADDRLEN;
1046 return GNUNET_SYSERR;
1048 memset (zt_addr, 0, MAX_IP_ADDRLEN + 1);
1049 strncpy (zt_addr, addr, zt_len);
1051 /* For URIs we use '(' and ')' instead of '[' and ']'. Do the substitution
1052 * now, as GNUNET_STRINGS_to_address_ipv6() takes a proper []-enclosed IPv6
1055 if (zt_addr[0] == '(')
1057 for (i = 0; i < zt_len; i++)
1071 return GNUNET_STRINGS_to_address_ipv6 (zt_addr, zt_len, (struct sockaddr_in6 *) r_buf);
1073 return GNUNET_STRINGS_to_address_ipv4 (zt_addr, zt_len, (struct sockaddr_in *) r_buf);
1076 /* end of strings.c */