diff options
author | Werner Koch <[email protected]> | 2018-07-02 18:24:10 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2018-07-02 18:25:30 +0000 |
commit | 58baf40af641f8cbf597e508a292e85ae94688f1 (patch) | |
tree | 4220db56a47ede8a3f6853706014e1ca2f992209 /common/percent.c | |
parent | agent: Fix segv running in --server mode (diff) | |
download | gnupg-58baf40af641f8cbf597e508a292e85ae94688f1.tar.gz gnupg-58baf40af641f8cbf597e508a292e85ae94688f1.zip |
common: New function percent_data_escape.
* common/percent.c (percent_data_escape): New.
* common/t-percent.c (test_percent_data_escape): New.
Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'common/percent.c')
-rw-r--r-- | common/percent.c | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/common/percent.c b/common/percent.c index 569c5fd99..eeb026fbe 100644 --- a/common/percent.c +++ b/common/percent.c @@ -87,6 +87,50 @@ percent_plus_escape (const char *string) } +/* Create a newly alloced string from (DATA,DATALEN) with embedded + * Nuls quoted as %00. The standard percent unescaping can be + * used to reverse this encoding. */ +char * +percent_data_escape (const void *data, size_t datalen) +{ + char *buffer, *p; + const char *s; + size_t n, length; + + for (length=1, s=data, n=datalen; n; s++, n--) + { + if (!*s || *s == '%') + length += 3; + else + length++; + } + + buffer = p = xtrymalloc (length); + if (!buffer) + return NULL; + + for (s=data, n=datalen; n; s++, n--) + { + if (!*s) + { + memcpy (p, "%00", 3); + p += 3; + } + else if (*s == '%') + { + memcpy (p, "%25", 3); + p += 3; + } + else + *p++ = *s; + } + *p = 0; + + return buffer; + +} + + /* Do the percent and plus/space unescaping from STRING to BUFFER and return the length of the valid buffer. Plus unescaping is only done if WITHPLUS is true. An escaped Nul character will be |