1 /* stringhelp.c - standard string helper functions
2 * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007,
3 * 2008, 2009, 2010 Free Software Foundation, Inc.
4 * Copyright (C) 2014 Werner Koch
5 * Copyright (C) 2015 g10 Code GmbH
7 * This file is part of GnuPG.
9 * GnuPG is free software; you can redistribute it and/or modify it
10 * under the terms of either
12 * - the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at
14 * your option) any later version.
18 * - the GNU General Public License as published by the Free
19 * Software Foundation; either version 2 of the License, or (at
20 * your option) any later version.
22 * or both in parallel, as here.
24 * GnuPG is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
29 * You should have received a copies of the GNU General Public License
30 * and the GNU Lesser General Public License along with this program;
31 * if not, see <http://www.gnu.org/licenses/>.
44 #include <sys/types.h>
45 #ifdef HAVE_W32_SYSTEM
46 # ifdef HAVE_WINSOCK2_H
47 # include <winsock2.h>
54 #include "common-defs.h"
57 #include "stringhelp.h"
59 #define tohex_lower(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'a'))
61 /* Sometimes we want to avoid mixing slashes and backslashes on W32
62 and prefer backslashes. There is usual no problem with mixing
63 them, however a very few W32 API calls can't grok plain slashes.
64 Printing filenames with mixed slashes also looks a bit strange.
65 This function has no effext on POSIX. */
67 change_slashes (char *name)
69 #ifdef HAVE_DOSISH_SYSTEM
72 if (strchr (name, '\\'))
78 #endif /*HAVE_DOSISH_SYSTEM*/
84 * Check whether STRING starts with KEYWORD. The keyword is
85 * delimited by end of string, a space or a tab. Returns NULL if not
86 * found or a pointer into STRING to the next non-space character
87 * after the KEYWORD (which may be end of string).
90 has_leading_keyword (const char *string, const char *keyword)
92 size_t n = strlen (keyword);
94 if (!strncmp (string, keyword, n)
95 && (!string[n] || string[n] == ' ' || string[n] == '\t'))
98 while (*string == ' ' || *string == '\t')
100 return (char*)string;
107 * Look for the substring SUB in buffer and return a pointer to that
108 * substring in BUFFER or NULL if not found.
109 * Comparison is case-insensitive.
112 memistr (const void *buffer, size_t buflen, const char *sub)
114 const unsigned char *buf = buffer;
115 const unsigned char *t = (const unsigned char *)buffer;
116 const unsigned char *s = (const unsigned char *)sub;
119 for ( ; n ; t++, n-- )
121 if ( toupper (*t) == toupper (*s) )
123 for ( buf=t++, buflen = n--, s++;
124 n && toupper (*t) == toupper (*s); t++, s++, n-- )
127 return (const char*)buf;
129 s = (const unsigned char *)sub ;
137 ascii_memistr ( const void *buffer, size_t buflen, const char *sub )
139 const unsigned char *buf = buffer;
140 const unsigned char *t = (const unsigned char *)buf;
141 const unsigned char *s = (const unsigned char *)sub;
144 for ( ; n ; t++, n-- )
146 if (ascii_toupper (*t) == ascii_toupper (*s) )
148 for ( buf=t++, buflen = n--, s++;
149 n && ascii_toupper (*t) == ascii_toupper (*s); t++, s++, n-- )
152 return (const char*)buf;
153 t = (const unsigned char *)buf;
154 s = (const unsigned char *)sub ;
161 /* This function is similar to strncpy(). However it won't copy more
162 than N - 1 characters and makes sure that a '\0' is appended. With
163 N given as 0, nothing will happen. With DEST given as NULL, memory
164 will be allocated using xmalloc (i.e. if it runs out of core
165 the function terminates). Returns DES or a pointer to the
169 mem2str( char *dest , const void *src , size_t n )
176 dest = xmalloc( n ) ;
179 for(n--; n && *s; n-- )
189 * remove leading and trailing white spaces
192 trim_spaces( char *str )
194 char *string, *p, *mark;
197 /* find first non space character */
198 for( p=string; *p && isspace( *(byte*)p ) ; p++ )
200 /* move characters */
201 for( (mark = NULL); (*string = *p); string++, p++ )
202 if( isspace( *(byte*)p ) ) {
209 *mark = '\0' ; /* remove trailing spaces */
215 * remove trailing white spaces
218 trim_trailing_spaces( char *string )
222 for( mark = NULL, p = string; *p; p++ ) {
223 if( isspace( *(byte*)p ) ) {
238 trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
243 for(mark=NULL, p=line, n=0; n < len; n++, p++ ) {
244 if( strchr(trimchars, *p ) ) {
260 * remove trailing white spaces and return the length of the buffer
263 trim_trailing_ws( byte *line, unsigned len )
265 return trim_trailing_chars( line, len, " \t\r\n" );
269 length_sans_trailing_chars (const unsigned char *line, size_t len,
270 const char *trimchars )
272 const unsigned char *p, *mark;
275 for( mark=NULL, p=line, n=0; n < len; n++, p++ )
277 if (strchr (trimchars, *p ))
292 * Return the length of line ignoring trailing white-space.
295 length_sans_trailing_ws (const unsigned char *line, size_t len)
297 return length_sans_trailing_chars (line, len, " \t\r\n");
303 * Extract from a given path the filename component. This function
304 * terminates the process on memory shortage.
307 make_basename(const char *filepath, const char *inputpath)
310 return riscos_make_basename(filepath, inputpath);
314 (void)inputpath; /* Only required for riscos. */
316 if ( !(p=strrchr(filepath, '/')) )
317 #ifdef HAVE_DOSISH_SYSTEM
318 if ( !(p=strrchr(filepath, '\\')) )
320 #ifdef HAVE_DRIVE_LETTERS
321 if ( !(p=strrchr(filepath, ':')) )
324 return xstrdup(filepath);
334 * Extract from a given filename the path prepended to it. If there
335 * isn't a path prepended to the filename, a dot is returned ('.').
336 * This function terminates the process on memory shortage.
339 make_dirname(const char *filepath)
345 if ( !(p=strrchr(filepath, '/')) )
346 #ifdef HAVE_DOSISH_SYSTEM
347 if ( !(p=strrchr(filepath, '\\')) )
349 #ifdef HAVE_DRIVE_LETTERS
350 if ( !(p=strrchr(filepath, ':')) )
356 dirname_length = p-filepath;
357 dirname = xmalloc(dirname_length+1);
358 strncpy(dirname, filepath, dirname_length);
359 dirname[dirname_length] = 0;
367 get_pwdir (int xmode, const char *name)
371 struct passwd *pwd = NULL;
376 /* Fixme: We should use getpwnam_r if available. */
377 pwd = getpwnam (name);
383 /* Fixme: We should use getpwuid_r if available. */
384 pwd = getpwuid (getuid());
390 result = xstrdup (pwd->pw_dir);
392 result = xtrystrdup (pwd->pw_dir);
394 #else /*!HAVE_PWD_H*/
395 /* No support at all. */
398 #endif /*HAVE_PWD_H*/
403 /* xmode 0 := Return NULL on error
404 1 := Terminate on error
405 2 := Make sure that name is absolute; return NULL on error
406 3 := Make sure that name is absolute; terminate on error
409 do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
411 const char *argv[32];
415 char *home_buffer = NULL;
416 char *name, *home, *p;
419 want_abs = !!(xmode & 2);
422 n = strlen (first_part) + 1;
424 while ( (argv[argc] = va_arg (arg_ptr, const char *)) )
426 n += strlen (argv[argc]) + 1;
427 if (argc >= DIM (argv)-1)
431 gpg_err_set_errno (EINVAL);
439 if (*first_part == '~')
441 if (first_part[1] == '/' || !first_part[1])
443 /* This is the "~/" or "~" case. */
444 home = getenv("HOME");
446 home = home_buffer = get_pwdir (xmode, NULL);
452 /* This is the "~username/" or "~username" case. */
456 user = xstrdup (first_part+1);
459 user = xtrystrdup (first_part+1);
463 p = strchr (user, '/');
466 skip = 1 + strlen (user);
468 home = home_buffer = get_pwdir (xmode, user);
481 name = xtrymalloc (n);
490 p = stpcpy (stpcpy (name, home), first_part + skip);
492 p = stpcpy (name, first_part);
495 for (argc=0; argv[argc]; argc++)
497 /* Avoid a leading double slash if the first part was "/". */
498 if (!argc && name[0] == '/' && !name[1])
499 p = stpcpy (p, argv[argc]);
501 p = stpcpy (stpcpy (p, "/"), argv[argc]);
506 #ifdef HAVE_DRIVE_LETTERS
507 p = strchr (name, ':');
516 #ifdef HAVE_DRIVE_LETTERS
521 home = gnupg_getcwd ();
526 fprintf (stderr, "\nfatal: getcwd failed: %s\n",
533 n = strlen (home) + 1 + strlen (name) + 1;
535 home_buffer = xmalloc (n);
538 home_buffer = xtrymalloc (n);
547 else /* Windows case. */
549 memcpy (home_buffer, p, p - name + 1);
550 p = home_buffer + (p - name + 1);
553 /* Avoid a leading double slash if the cwd is "/". */
554 if (home[0] == '/' && !home[1])
555 strcpy (stpcpy (p, "/"), name);
557 strcpy (stpcpy (stpcpy (p, home), "/"), name);
561 /* Let's do a simple compression to catch the most common
562 case of using "." for gpg's --homedir option. */
564 if (n > 2 && name[n-2] == '/' && name[n-1] == '.')
568 return change_slashes (name);
571 /* Construct a filename from the NULL terminated list of parts. Tilde
572 expansion is done for the first argument. This function terminates
573 the process on memory shortage. */
575 make_filename (const char *first_part, ... )
580 va_start (arg_ptr, first_part);
581 result = do_make_filename (1, first_part, arg_ptr);
586 /* Construct a filename from the NULL terminated list of parts. Tilde
587 expansion is done for the first argument. This function may return
590 make_filename_try (const char *first_part, ... )
595 va_start (arg_ptr, first_part);
596 result = do_make_filename (0, first_part, arg_ptr);
601 /* Construct an absolute filename from the NULL terminated list of
602 parts. Tilde expansion is done for the first argument. This
603 function terminates the process on memory shortage. */
605 make_absfilename (const char *first_part, ... )
610 va_start (arg_ptr, first_part);
611 result = do_make_filename (3, first_part, arg_ptr);
616 /* Construct an absolute filename from the NULL terminated list of
617 parts. Tilde expansion is done for the first argument. This
618 function may return NULL on error. */
620 make_absfilename_try (const char *first_part, ... )
625 va_start (arg_ptr, first_part);
626 result = do_make_filename (2, first_part, arg_ptr);
633 /* Compare whether the filenames are identical. This is a
634 special version of strcmp() taking the semantics of filenames in
635 account. Note that this function works only on the supplied names
636 without considering any context like the current directory. See
637 also same_file_p(). */
639 compare_filenames (const char *a, const char *b)
641 #ifdef HAVE_DOSISH_SYSTEM
642 for ( ; *a && *b; a++, b++ )
645 && (toupper (*(const unsigned char*)a)
646 != toupper (*(const unsigned char*)b) )
647 && !((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/')))
650 if ((*a == '/' && *b == '\\') || (*a == '\\' && *b == '/'))
653 return (toupper (*(const unsigned char*)a)
654 - toupper (*(const unsigned char*)b));
661 /* Convert 2 hex characters at S to a byte value. Return this value
662 or -1 if there is an error. */
664 hextobyte (const char *s)
668 if ( *s >= '0' && *s <= '9' )
670 else if ( *s >= 'A' && *s <= 'F' )
671 c = 16 * (10 + *s - 'A');
672 else if ( *s >= 'a' && *s <= 'f' )
673 c = 16 * (10 + *s - 'a');
677 if ( *s >= '0' && *s <= '9' )
679 else if ( *s >= 'A' && *s <= 'F' )
681 else if ( *s >= 'a' && *s <= 'f' )
689 /* Create a string from the buffer P_ARG of length N which is suitable
690 for printing. Caller must release the created string using xfree.
691 This function terminates the process on memory shortage. */
693 sanitize_buffer (const void *p_arg, size_t n, int delim)
695 const unsigned char *p = p_arg;
696 size_t save_n, buflen;
697 const unsigned char *save_p;
700 /* First count length. */
701 for (save_n = n, save_p = p, buflen=1 ; n; n--, p++ )
703 if ( *p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\'))
705 if ( *p=='\n' || *p=='\r' || *p=='\f'
706 || *p=='\v' || *p=='\b' || !*p )
716 /* And now make the string */
717 d = buffer = xmalloc( buflen );
718 for ( ; n; n--, p++ )
720 if (*p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\')) {
724 else if( *p == '\r' )
726 else if( *p == '\f' )
728 else if( *p == '\v' )
730 else if( *p == '\b' )
735 sprintf(d, "x%02x", *p );
747 /* Given a string containing an UTF-8 encoded text, return the number
748 of characters in this string. It differs from strlen in that it
749 only counts complete UTF-8 characters. Note, that this function
750 does not take combined characters into account. */
752 utf8_charcount (const char *s)
757 if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */
764 /****************************************************
765 ********** W32 specific functions ****************
766 ****************************************************/
768 #ifdef HAVE_W32_SYSTEM
770 w32_strerror (int ec)
772 static char strerr[256];
775 ec = (int)GetLastError ();
776 #ifdef HAVE_W32CE_SYSTEM
777 /* There is only a wchar_t FormatMessage. It does not make much
778 sense to play the conversion game; we print only the code. */
779 snprintf (strerr, sizeof strerr, "ec=%d", (int)GetLastError ());
781 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, ec,
782 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
783 strerr, DIM (strerr)-1, NULL);
787 #endif /*HAVE_W32_SYSTEM*/
790 /****************************************************
791 ******** Locale insensitive ctype functions ********
792 ****************************************************/
793 /* FIXME: replace them by a table lookup and macros */
795 ascii_isupper (int c)
797 return c >= 'A' && c <= 'Z';
801 ascii_islower (int c)
803 return c >= 'a' && c <= 'z';
807 ascii_toupper (int c)
809 if (c >= 'a' && c <= 'z')
815 ascii_tolower (int c)
817 if (c >= 'A' && c <= 'Z')
822 /* Lowercase all ASCII characters in S. */
824 ascii_strlwr (char *s)
829 if (isascii (*p) && *p >= 'A' && *p <= 'Z')
836 ascii_strcasecmp( const char *a, const char *b )
841 for (; *a && *b; a++, b++) {
842 if (*a != *b && ascii_toupper(*a) != ascii_toupper(*b))
845 return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
849 ascii_strncasecmp (const char *a, const char *b, size_t n)
851 const unsigned char *p1 = (const unsigned char *)a;
852 const unsigned char *p2 = (const unsigned char *)b;
853 unsigned char c1, c2;
860 c1 = ascii_tolower (*p1);
861 c2 = ascii_tolower (*p2);
863 if ( !--n || c1 == '\0')
876 ascii_memcasecmp (const void *a_arg, const void *b_arg, size_t n )
878 const char *a = a_arg;
879 const char *b = b_arg;
883 for ( ; n; n--, a++, b++ )
885 if( *a != *b && ascii_toupper (*a) != ascii_toupper (*b) )
886 return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
892 ascii_strcmp( const char *a, const char *b )
897 for (; *a && *b; a++, b++) {
901 return *a == *b? 0 : (*(signed char *)a - *(signed char *)b);
906 ascii_memcasemem (const void *haystack, size_t nhaystack,
907 const void *needle, size_t nneedle)
911 return (void*)haystack; /* finding an empty needle is really easy */
912 if (nneedle <= nhaystack)
914 const char *a = haystack;
915 const char *b = a + nhaystack - nneedle;
919 if ( !ascii_memcasecmp (a, needle, nneedle) )
926 /*********************************************
927 ********** missing string functions *********
928 *********************************************/
932 stpcpy(char *a,const char *b)
943 /* Find the first occurrence in S of any character in ACCEPT.
944 Code taken from glibc-2.6/string/strpbrk.c (LGPLv2.1+) and modified. */
946 strpbrk (const char *s, const char *accept)
950 const char *a = accept;
959 #endif /*!HAVE_STRPBRK*/
963 /* Code taken from glibc-2.2.1/sysdeps/generic/strsep.c. */
965 strsep (char **stringp, const char *delim)
973 /* A frequent case is when the delimiter string contains only one
974 character. Here we don't need to call the expensive 'strpbrk'
975 function and instead work using 'strchr'. */
976 if (delim[0] == '\0' || delim[1] == '\0')
986 else if (*begin == '\0')
989 end = strchr (begin + 1, ch);
993 /* Find the end of the token. */
994 end = strpbrk (begin, delim);
998 /* Terminate the token and set *STRINGP past NUL character. */
1003 /* No more delimiters; this is the last token. */
1008 #endif /*HAVE_STRSEP*/
1023 #ifndef HAVE_STRCASECMP
1025 strcasecmp( const char *a, const char *b )
1027 for( ; *a && *b; a++, b++ ) {
1028 if( *a != *b && toupper(*a) != toupper(*b) )
1031 return *(const byte*)a - *(const byte*)b;
1037 * mingw32/cpd has a memicmp()
1039 #ifndef HAVE_MEMICMP
1041 memicmp( const char *a, const char *b, size_t n )
1043 for( ; n; n--, a++, b++ )
1044 if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) )
1045 return *(const byte *)a - *(const byte*)b;
1051 #ifndef HAVE_MEMRCHR
1053 memrchr (const void *buffer, int c, size_t n)
1055 const unsigned char *p = buffer;
1057 for (p += n; n ; n--)
1062 #endif /*HAVE_MEMRCHR*/
1065 /* Percent-escape the string STR by replacing colons with '%3a'. If
1066 EXTRA is not NULL all characters in EXTRA are also escaped. */
1068 do_percent_escape (const char *str, const char *extra, int die)
1076 for (i=j=0; str[i]; i++)
1077 if (str[i] == ':' || str[i] == '%' || (extra && strchr (extra, str[i])))
1080 ptr = xmalloc (i + 2 * j + 1);
1083 ptr = xtrymalloc (i + 2 * j + 1);
1096 else if (*str == '%')
1102 else if (extra && strchr (extra, *str))
1105 ptr[i++] = tohex_lower ((*str>>4)&15);
1106 ptr[i++] = tohex_lower (*str&15);
1117 /* Percent-escape the string STR by replacing colons with '%3a'. If
1118 EXTRA is not NULL all characters in EXTRA are also escaped. This
1119 function terminates the process on memory shortage. */
1121 percent_escape (const char *str, const char *extra)
1123 return do_percent_escape (str, extra, 1);
1126 /* Same as percent_escape but return NULL instead of exiting on memory
1129 try_percent_escape (const char *str, const char *extra)
1131 return do_percent_escape (str, extra, 0);
1137 do_strconcat (const char *s1, va_list arg_ptr)
1139 const char *argv[48];
1146 needed = strlen (s1);
1147 while (((argv[argc] = va_arg (arg_ptr, const char *))))
1149 needed += strlen (argv[argc]);
1150 if (argc >= DIM (argv)-1)
1152 gpg_err_set_errno (EINVAL);
1158 buffer = xtrymalloc (needed);
1161 for (p = buffer, argc=0; argv[argc]; argc++)
1162 p = stpcpy (p, argv[argc]);
1168 /* Concatenate the string S1 with all the following strings up to a
1169 NULL. Returns a malloced buffer with the new string or NULL on a
1170 malloc error or if too many arguments are given. */
1172 strconcat (const char *s1, ...)
1178 result = xtrystrdup ("");
1181 va_start (arg_ptr, s1);
1182 result = do_strconcat (s1, arg_ptr);
1188 /* Same as strconcat but terminate the process with an error message
1189 if something goes wrong. */
1191 xstrconcat (const char *s1, ...)
1197 result = xstrdup ("");
1200 va_start (arg_ptr, s1);
1201 result = do_strconcat (s1, arg_ptr);
1206 if (errno == EINVAL)
1207 fputs ("\nfatal: too many args for xstrconcat\n", stderr);
1209 fputs ("\nfatal: out of memory\n", stderr);
1215 /* Split a string into fields at DELIM. REPLACEMENT is the character
1216 to replace the delimiter with (normally: '\0' so that each field is
1217 NUL terminated). The caller is responsible for freeing the result.
1218 Note: this function modifies STRING! If you need the original
1219 value, then you should pass a copy to this function.
1221 If malloc fails, this function returns NULL. */
1223 strsplit (char *string, char delim, char replacement, int *count)
1229 /* First, count the number of fields. */
1230 for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
1233 result = xtrycalloc (sizeof (*result), (fields + 1));
1239 for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
1241 result[fields ++] = t + 1;
1252 /* Tokenize STRING using the set of delimiters in DELIM. Leading
1253 * spaces and tabs are removed from all tokens. The caller must xfree
1256 * Returns: A malloced and NULL delimited array with the tokens. On
1257 * memory error NULL is returned and ERRNO is set.
1260 strtokenize (const char *string, const char *delim)
1266 char *p, *px, *pend;
1269 /* Count the number of fields. */
1270 for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim))
1272 fields++; /* Add one for the terminating NULL. */
1274 /* Allocate an array for all fields, a terminating NULL, and space
1275 for a copy of the string. */
1276 bytes = fields * sizeof *result;
1277 if (bytes / sizeof *result != fields)
1279 gpg_err_set_errno (ENOMEM);
1282 n = strlen (string) + 1;
1286 gpg_err_set_errno (ENOMEM);
1289 result = xtrymalloc (bytes);
1292 buffer = (char*)(result + fields);
1294 /* Copy and parse the string. */
1295 strcpy (buffer, string);
1296 for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1)
1301 for (px = pend - 1; px >= p && spacep (px); px--)
1307 for (px = p + strlen (p) - 1; px >= p && spacep (px); px--)
1312 assert ((char*)(result + n + 1) == buffer);