From 667ced56862ec5d50dd17e98aa26155cc2330ac3 Mon Sep 17 00:00:00 2001 From: Ingo Klöcker Date: Mon, 3 Aug 2026 14:52:39 +0200 Subject: Add helper to decode percent-plus encoded strings * src/conversion.c (do_decode_percent_or_percent_plus): New. (_gpgme_decode_percent_string): Move its code to new file-static do_decode_percent_or_percent_plus and call it with disabled decoding of '+'. * src/conversion.c, src/util.h (_gpgme_decode_percent_plus_string): New. -- GnuPG-bug-id: 8369 --- src/conversion.c | 54 +++++++++++++++++++++++++++++++++++++++++++++--------- src/util.h | 10 ++++++++++ 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/conversion.c b/src/conversion.c index d15d7cc9..11e03e9c 100644 --- a/src/conversion.c +++ b/src/conversion.c @@ -242,16 +242,17 @@ _gpgme_decode_c_string (const char *src, char **destp, size_t len) } -/* Decode the percent escaped string SRC and store the result in the - buffer *DESTP which is LEN bytes long. If LEN is zero, then a - large enough buffer is allocated with malloc and *DESTP is set to - the result. Currently, LEN is only used to specify if allocation +/* Do the percent and plus/space decoding of string SRC and store the + result in the buffer *DESTP which is LEN bytes long. If LEN is zero, + then a large enough buffer is allocated with malloc and *DESTP is set + to the result. Currently, LEN is only used to specify if allocation is desired or not, the caller is expected to make sure that *DESTP is large enough if LEN is not zero. If BINARY is 1, then '\0' - characters are allowed in the output. */ -gpgme_error_t -_gpgme_decode_percent_string (const char *src, char **destp, size_t len, - int binary) + characters are allowed in the output. Plus decoding is only done + if WITHPLUS is 1. */ +static gpgme_error_t +do_decode_percent_or_percent_plus (const char *src, char **destp, size_t len, + int binary, int withplus) { char *dest; @@ -277,7 +278,12 @@ _gpgme_decode_percent_string (const char *src, char **destp, size_t len, /* Convert the string. */ while (*src) { - if (*src != '%') + if (*src == '+' && withplus) + { + *(dest++) = ' '; + src++; + } + else if (*src != '%') { *(dest++) = *(src++); continue; @@ -316,6 +322,36 @@ _gpgme_decode_percent_string (const char *src, char **destp, size_t len, } +/* Decode the percent escaped string SRC and store the result in the + buffer *DESTP which is LEN bytes long. If LEN is zero, then a + large enough buffer is allocated with malloc and *DESTP is set to + the result. Currently, LEN is only used to specify if allocation + is desired or not, the caller is expected to make sure that *DESTP + is large enough if LEN is not zero. If BINARY is 1, then '\0' + characters are allowed in the output. */ +gpgme_error_t +_gpgme_decode_percent_string (const char *src, char **destp, size_t len, + int binary) +{ + return do_decode_percent_or_percent_plus (src, destp, len, binary, 0); +} + + +/* Decode the percent-plus escaped string SRC and store the result in the + buffer *DESTP which is LEN bytes long. If LEN is zero, then a + large enough buffer is allocated with malloc and *DESTP is set to + the result. Currently, LEN is only used to specify if allocation + is desired or not, the caller is expected to make sure that *DESTP + is large enough if LEN is not zero. If BINARY is 1, then '\0' + characters are allowed in the output. */ +gpgme_error_t +_gpgme_decode_percent_plus_string (const char *src, char **destp, size_t len, + int binary) +{ + return do_decode_percent_or_percent_plus (src, destp, len, binary, 1); +} + + /* Encode the string SRC with percent escaping and store the result in the buffer *DESTP which is LEN bytes long. If LEN is zero, then a large enough buffer is allocated with malloc and *DESTP is set to diff --git a/src/util.h b/src/util.h index d805373a..b7462ea5 100644 --- a/src/util.h +++ b/src/util.h @@ -131,6 +131,16 @@ gpgme_error_t _gpgme_decode_c_string (const char *src, char **destp, gpgme_error_t _gpgme_decode_percent_string (const char *src, char **destp, size_t len, int binary); +/* Decode the percent-plus escaped string SRC and store the result in the + buffer *DESTP which is LEN bytes long. If LEN is zero, then a + large enough buffer is allocated with malloc and *DESTP is set to + the result. Currently, LEN is only used to specify if allocation + is desired or not, the caller is expected to make sure that *DESTP + is large enough if LEN is not zero. If BINARY is 1, then '\0' + characters are allowed in the output. */ +gpgme_error_t _gpgme_decode_percent_plus_string (const char *src, char **destp, + size_t len, int binary); + gpgme_error_t _gpgme_encode_percent_string (const char *src, char **destp, size_t len); -- cgit