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/>.
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
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
40 percent_plus_escape (const char *string
)
46 for (length
=1, s
=string
; *s
; s
++)
48 if (*s
== '+' || *s
== '\"' || *s
== '%'
49 || *(const unsigned char *)s
< 0x20)
55 buffer
= p
= xtrymalloc (length
);
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
);