/* stringhelp.c - standard string helper functions
* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007,
* 2008, 2009, 2010 Free Software Foundation, Inc.
+ * Copyright (C) 2014 Werner Koch
+ * Copyright (C) 2015 g10 Code GmbH
*
- * This file is part of JNLIB, which is a subsystem of GnuPG.
+ * This file is part of GnuPG.
*
- * JNLIB is free software; you can redistribute it and/or modify it
+ * GnuPG is free software; you can redistribute it and/or modify it
* under the terms of either
*
* - the GNU Lesser General Public License as published by the Free
*
* or both in parallel, as here.
*
- * JNLIB is distributed in the hope that it will be useful, but
+ * GnuPG is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_W32_SYSTEM
+# ifdef HAVE_WINSOCK2_H
+# include <winsock2.h>
+# endif
# include <windows.h>
#endif
+#include <assert.h>
-#include "libjnlib-config.h"
+#include "util.h"
+#include "common-defs.h"
#include "utf8conv.h"
+#include "sysutils.h"
#include "stringhelp.h"
-
#define tohex_lower(n) ((n) < 10 ? ((n) + '0') : (((n) - 10) + 'a'))
/* Sometimes we want to avoid mixing slashes and backslashes on W32
/*
+ * Check whether STRING starts with KEYWORD. The keyword is
+ * delimited by end of string, a space or a tab. Returns NULL if not
+ * found or a pointer into STRING to the next non-space character
+ * after the KEYWORD (which may be end of string).
+ */
+char *
+has_leading_keyword (const char *string, const char *keyword)
+{
+ size_t n = strlen (keyword);
+
+ if (!strncmp (string, keyword, n)
+ && (!string[n] || string[n] == ' ' || string[n] == '\t'))
+ {
+ string += n;
+ while (*string == ' ' || *string == '\t')
+ string++;
+ return (char*)string;
+ }
+ return NULL;
+}
+
+
+/*
* Look for the substring SUB in buffer and return a pointer to that
* substring in BUFFER or NULL if not found.
* Comparison is case-insensitive.
/* This function is similar to strncpy(). However it won't copy more
than N - 1 characters and makes sure that a '\0' is appended. With
N given as 0, nothing will happen. With DEST given as NULL, memory
- will be allocated using jnlib_xmalloc (i.e. if it runs out of core
+ will be allocated using xmalloc (i.e. if it runs out of core
the function terminates). Returns DES or a pointer to the
allocated memory.
*/
if( n ) {
if( !dest )
- dest = jnlib_xmalloc( n ) ;
+ dest = xmalloc( n ) ;
d = dest;
s = src ;
for(n--; n && *s; n-- )
if ( !(p=strrchr(filepath, ':')) )
#endif
{
- return jnlib_xstrdup(filepath);
+ return xstrdup(filepath);
}
- return jnlib_xstrdup(p+1);
+ return xstrdup(p+1);
#endif
}
if ( !(p=strrchr(filepath, ':')) )
#endif
{
- return jnlib_xstrdup(".");
+ return xstrdup(".");
}
dirname_length = p-filepath;
- dirname = jnlib_xmalloc(dirname_length+1);
+ dirname = xmalloc(dirname_length+1);
strncpy(dirname, filepath, dirname_length);
dirname[dirname_length] = 0;
if (pwd)
{
if (xmode)
- result = jnlib_xstrdup (pwd->pw_dir);
+ result = xstrdup (pwd->pw_dir);
else
- result = jnlib_strdup (pwd->pw_dir);
+ result = xtrystrdup (pwd->pw_dir);
}
#else /*!HAVE_PWD_H*/
/* No support at all. */
return result;
}
+
+/* xmode 0 := Return NULL on error
+ 1 := Terminate on error
+ 2 := Make sure that name is absolute; return NULL on error
+ 3 := Make sure that name is absolute; terminate on error
+ */
static char *
do_make_filename (int xmode, const char *first_part, va_list arg_ptr)
{
int skip = 1;
char *home_buffer = NULL;
char *name, *home, *p;
+ int want_abs;
+
+ want_abs = !!(xmode & 2);
+ xmode &= 1;
n = strlen (first_part) + 1;
argc = 0;
{
if (xmode)
BUG ();
- jnlib_set_errno (EINVAL);
+ gpg_err_set_errno (EINVAL);
return NULL;
}
argc++;
char *user;
if (xmode)
- user = jnlib_xstrdup (first_part+1);
+ user = xstrdup (first_part+1);
else
{
- user = jnlib_strdup (first_part+1);
+ user = xtrystrdup (first_part+1);
if (!user)
return NULL;
}
skip = 1 + strlen (user);
home = home_buffer = get_pwdir (xmode, user);
- jnlib_free (user);
+ xfree (user);
if (home)
n += strlen (home);
else
}
if (xmode)
- name = jnlib_xmalloc (n);
+ name = xmalloc (n);
else
{
- name = jnlib_malloc (n);
+ name = xtrymalloc (n);
if (!name)
{
- jnlib_free (home_buffer);
+ xfree (home_buffer);
return NULL;
}
}
else
p = stpcpy (name, first_part);
- jnlib_free (home_buffer);
-
+ xfree (home_buffer);
for (argc=0; argv[argc]; argc++)
- p = stpcpy (stpcpy (p, "/"), argv[argc]);
+ {
+ /* Avoid a leading double slash if the first part was "/". */
+ if (!argc && name[0] == '/' && !name[1])
+ p = stpcpy (p, argv[argc]);
+ else
+ p = stpcpy (stpcpy (p, "/"), argv[argc]);
+ }
+ if (want_abs)
+ {
+#ifdef HAVE_DRIVE_LETTERS
+ p = strchr (name, ':');
+ if (p)
+ p++;
+ else
+ p = name;
+#else
+ p = name;
+#endif
+ if (*p != '/'
+#ifdef HAVE_DRIVE_LETTERS
+ && *p != '\\'
+#endif
+ )
+ {
+ home = gnupg_getcwd ();
+ if (!home)
+ {
+ if (xmode)
+ {
+ fprintf (stderr, "\nfatal: getcwd failed: %s\n",
+ strerror (errno));
+ exit(2);
+ }
+ xfree (name);
+ return NULL;
+ }
+ n = strlen (home) + 1 + strlen (name) + 1;
+ if (xmode)
+ home_buffer = xmalloc (n);
+ else
+ {
+ home_buffer = xtrymalloc (n);
+ if (!home_buffer)
+ {
+ xfree (name);
+ return NULL;
+ }
+ }
+ if (p == name)
+ p = home_buffer;
+ else /* Windows case. */
+ {
+ memcpy (home_buffer, p, p - name + 1);
+ p = home_buffer + (p - name + 1);
+ }
+
+ /* Avoid a leading double slash if the cwd is "/". */
+ if (home[0] == '/' && !home[1])
+ strcpy (stpcpy (p, "/"), name);
+ else
+ strcpy (stpcpy (stpcpy (p, home), "/"), name);
+
+ xfree (name);
+ name = home_buffer;
+ /* Let's do a simple compression to catch the most common
+ case of using "." for gpg's --homedir option. */
+ n = strlen (name);
+ if (n > 2 && name[n-2] == '/' && name[n-1] == '.')
+ name[n-2] = 0;
+ }
+ }
return change_slashes (name);
}
return result;
}
+/* Construct an absolute filename from the NULL terminated list of
+ parts. Tilde expansion is done for the first argument. This
+ function terminates the process on memory shortage. */
+char *
+make_absfilename (const char *first_part, ... )
+{
+ va_list arg_ptr;
+ char *result;
+
+ va_start (arg_ptr, first_part);
+ result = do_make_filename (3, first_part, arg_ptr);
+ va_end (arg_ptr);
+ return result;
+}
+
+/* Construct an absolute filename from the NULL terminated list of
+ parts. Tilde expansion is done for the first argument. This
+ function may return NULL on error. */
+char *
+make_absfilename_try (const char *first_part, ... )
+{
+ va_list arg_ptr;
+ char *result;
+
+ va_start (arg_ptr, first_part);
+ result = do_make_filename (2, first_part, arg_ptr);
+ va_end (arg_ptr);
+ return result;
+}
+
\f
/* Compare whether the filenames are identical. This is a
}
-/* Print a BUFFER to stream FP while replacing all control characters
- and the characters DELIM and DELIM2 with standard C escape
- sequences. Returns the number of characters printed. */
-size_t
-print_sanitized_buffer2 (FILE *fp, const void *buffer, size_t length,
- int delim, int delim2)
-{
- const unsigned char *p = buffer;
- size_t count = 0;
-
- for (; length; length--, p++, count++)
- {
- if (*p < 0x20
- || *p == 0x7f
- || *p == delim
- || *p == delim2
- || ((delim || delim2) && *p=='\\'))
- {
- putc ('\\', fp);
- count++;
- if (*p == '\n')
- {
- putc ('n', fp);
- count++;
- }
- else if (*p == '\r')
- {
- putc ('r', fp);
- count++;
- }
- else if (*p == '\f')
- {
- putc ('f', fp);
- count++;
- }
- else if (*p == '\v')
- {
- putc ('v', fp);
- count++;
- }
- else if (*p == '\b')
- {
- putc ('b', fp);
- count++;
- }
- else if (!*p)
- {
- putc('0', fp);
- count++;
- }
- else
- {
- fprintf (fp, "x%02x", *p);
- count += 3;
- }
- }
- else
- {
- putc (*p, fp);
- count++;
- }
- }
-
- return count;
-}
-
-/* Same as print_sanitized_buffer2 but with just one delimiter. */
-size_t
-print_sanitized_buffer (FILE *fp, const void *buffer, size_t length,
- int delim)
-{
- return print_sanitized_buffer2 (fp, buffer, length, delim, 0);
-}
-
-
-size_t
-print_sanitized_utf8_buffer (FILE *fp, const void *buffer,
- size_t length, int delim)
-{
- const char *p = buffer;
- size_t i;
-
- /* We can handle plain ascii simpler, so check for it first. */
- for (i=0; i < length; i++ )
- {
- if ( (p[i] & 0x80) )
- break;
- }
- if (i < length)
- {
- char *buf = utf8_to_native (p, length, delim);
- /*(utf8 conversion already does the control character quoting)*/
- i = strlen (buf);
- fputs (buf, fp);
- jnlib_free (buf);
- return i;
- }
- else
- return print_sanitized_buffer (fp, p, length, delim);
-}
-
-
-size_t
-print_sanitized_string2 (FILE *fp, const char *string, int delim, int delim2)
-{
- return string? print_sanitized_buffer2 (fp, string, strlen (string),
- delim, delim2):0;
-}
-
-size_t
-print_sanitized_string (FILE *fp, const char *string, int delim)
-{
- return string? print_sanitized_buffer (fp, string, strlen (string), delim):0;
-}
-
-size_t
-print_sanitized_utf8_string (FILE *fp, const char *string, int delim)
-{
- return string? print_sanitized_utf8_buffer (fp,
- string, strlen (string),
- delim) : 0;
-}
-
/* Create a string from the buffer P_ARG of length N which is suitable
for printing. Caller must release the created string using xfree.
This function terminates the process on memory shortage. */
p = save_p;
n = save_n;
/* And now make the string */
- d = buffer = jnlib_xmalloc( buflen );
+ d = buffer = xmalloc( buflen );
for ( ; n; n--, p++ )
{
if (*p < 0x20 || *p == 0x7f || *p == delim || (delim && *p=='\\')) {
return c;
}
+/* Lowercase all ASCII characters in S. */
+char *
+ascii_strlwr (char *s)
+{
+ char *p = s;
+
+ for (p=s; *p; p++ )
+ if (isascii (*p) && *p >= 'A' && *p <= 'Z')
+ *p |= 0x20;
+
+ return s;
+}
int
ascii_strcasecmp( const char *a, const char *b )
}
#endif
+#ifndef HAVE_STRPBRK
+/* Find the first occurrence in S of any character in ACCEPT.
+ Code taken from glibc-2.6/string/strpbrk.c (LGPLv2.1+) and modified. */
+char *
+strpbrk (const char *s, const char *accept)
+{
+ while (*s != '\0')
+ {
+ const char *a = accept;
+ while (*a != '\0')
+ if (*a++ == *s)
+ return (char *) s;
+ ++s;
+ }
+
+ return NULL;
+}
+#endif /*!HAVE_STRPBRK*/
+
+
#ifndef HAVE_STRSEP
/* Code taken from glibc-2.2.1/sysdeps/generic/strsep.c. */
char *
return NULL;
/* A frequent case is when the delimiter string contains only one
- character. Here we don't need to call the expensive `strpbrk'
- function and instead work using `strchr'. */
+ character. Here we don't need to call the expensive 'strpbrk'
+ function and instead work using 'strchr'. */
if (delim[0] == '\0' || delim[1] == '\0')
{
char ch = delim[0];
if (str[i] == ':' || str[i] == '%' || (extra && strchr (extra, str[i])))
j++;
if (die)
- ptr = jnlib_xmalloc (i + 2 * j + 1);
+ ptr = xmalloc (i + 2 * j + 1);
else
{
- ptr = jnlib_malloc (i + 2 * j + 1);
+ ptr = xtrymalloc (i + 2 * j + 1);
if (!ptr)
return NULL;
}
needed += strlen (argv[argc]);
if (argc >= DIM (argv)-1)
{
- jnlib_set_errno (EINVAL);
+ gpg_err_set_errno (EINVAL);
return NULL;
}
argc++;
}
needed++;
- buffer = jnlib_malloc (needed);
+ buffer = xtrymalloc (needed);
if (buffer)
{
for (p = buffer, argc=0; argv[argc]; argc++)
char *result;
if (!s1)
- result = jnlib_strdup ("");
+ result = xtrystrdup ("");
else
{
va_start (arg_ptr, s1);
char *result;
if (!s1)
- result = jnlib_xstrdup ("");
+ result = xstrdup ("");
else
{
va_start (arg_ptr, s1);
}
return result;
}
+
+/* Split a string into fields at DELIM. REPLACEMENT is the character
+ to replace the delimiter with (normally: '\0' so that each field is
+ NUL terminated). The caller is responsible for freeing the result.
+ Note: this function modifies STRING! If you need the original
+ value, then you should pass a copy to this function.
+
+ If malloc fails, this function returns NULL. */
+char **
+strsplit (char *string, char delim, char replacement, int *count)
+{
+ int fields = 1;
+ char *t;
+ char **result;
+
+ /* First, count the number of fields. */
+ for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
+ fields ++;
+
+ result = xtrycalloc ((fields + 1), sizeof (*result));
+ if (! result)
+ return NULL;
+
+ result[0] = string;
+ fields = 1;
+ for (t = strchr (string, delim); t; t = strchr (t + 1, delim))
+ {
+ result[fields ++] = t + 1;
+ *t = replacement;
+ }
+
+ if (count)
+ *count = fields;
+
+ return result;
+}
+
+
+/* Tokenize STRING using the set of delimiters in DELIM. Leading
+ * spaces and tabs are removed from all tokens. The caller must xfree
+ * the result.
+ *
+ * Returns: A malloced and NULL delimited array with the tokens. On
+ * memory error NULL is returned and ERRNO is set.
+ */
+char **
+strtokenize (const char *string, const char *delim)
+{
+ const char *s;
+ size_t fields;
+ size_t bytes, n;
+ char *buffer;
+ char *p, *px, *pend;
+ char **result;
+
+ /* Count the number of fields. */
+ for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim))
+ fields++;
+ fields++; /* Add one for the terminating NULL. */
+
+ /* Allocate an array for all fields, a terminating NULL, and space
+ for a copy of the string. */
+ bytes = fields * sizeof *result;
+ if (bytes / sizeof *result != fields)
+ {
+ gpg_err_set_errno (ENOMEM);
+ return NULL;
+ }
+ n = strlen (string) + 1;
+ bytes += n;
+ if (bytes < n)
+ {
+ gpg_err_set_errno (ENOMEM);
+ return NULL;
+ }
+ result = xtrymalloc (bytes);
+ if (!result)
+ return NULL;
+ buffer = (char*)(result + fields);
+
+ /* Copy and parse the string. */
+ strcpy (buffer, string);
+ for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1)
+ {
+ *pend = 0;
+ while (spacep (p))
+ p++;
+ for (px = pend - 1; px >= p && spacep (px); px--)
+ *px = 0;
+ result[n++] = p;
+ }
+ while (spacep (p))
+ p++;
+ for (px = p + strlen (p) - 1; px >= p && spacep (px); px--)
+ *px = 0;
+ result[n++] = p;
+ result[n] = NULL;
+
+ assert ((char*)(result + n + 1) == buffer);
+
+ return result;
+}