typo fixes.
[gnupg.git] / common / percent.c
bloba0c78ec7b86810d3ae722272eca12cb39c815c4e
1 /* percent.c - Percent escaping
2 * Copyright (C) 2008 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <errno.h>
23 #include <ctype.h>
25 #include "util.h"
28 /* Create a newly alloced string from STRING with all spaces and
29 control characters converted to plus signs or %xx sequences. The
30 function returns the new string or NULL in case of a malloc
31 failure.
33 Note that we also escape the quote character to work around a bug
34 in the mingw32 runtime which does not correcty handle command line
35 quoting. We correctly double the quote mark when calling a program
36 (i.e. gpg-protect-tool), but the pre-main code does not notice the
37 double quote as an escaped quote. We do this also on POSIX systems
38 for consistency. */
39 char *
40 percent_plus_escape (const char *string)
42 char *buffer, *p;
43 const char *s;
44 size_t length;
46 for (length=1, s=string; *s; s++)
48 if (*s == '+' || *s == '\"' || *s == '%'
49 || *(const unsigned char *)s < 0x20)
50 length += 3;
51 else
52 length++;
55 buffer = p = xtrymalloc (length);
56 if (!buffer)
57 return NULL;
59 for (s=string; *s; s++)
61 if (*s == '+' || *s == '\"' || *s == '%'
62 || *(const unsigned char *)s < 0x20)
64 snprintf (p, 4, "%%%02X", *(unsigned char *)s);
65 p += 3;
67 else if (*s == ' ')
68 *p++ = '+';
69 else
70 *p++ = *s;
72 *p = 0;
74 return buffer;