libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 4ec916c02cd2868e21864abc8232335282713358
parent a940c3ba581e55f238e02632a8b0b5d68cba1a9f
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
Date:   Tue,  7 Jun 2022 09:50:21 +0300

MHD_str_quote(): added new internal function

Diffstat:
Msrc/microhttpd/mhd_str.c | 41+++++++++++++++++++++++++++++++++++++++++
Msrc/microhttpd/mhd_str.h | 27++++++++++++++++++++++++++-
2 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c @@ -1474,3 +1474,44 @@ MHD_str_unquote (const char *quoted, #endif /* DAUTH_SUPPORT */ + +#if defined(DAUTH_SUPPORT) || defined(BAUTH_SUPPORT) + +size_t +MHD_str_quote (const char *unquoted, + size_t unquoted_len, + char *result, + size_t buf_size) +{ + size_t r; + size_t w; + + r = 0; + w = 0; + + if (unquoted_len > buf_size) + return 0; /* The output buffer is too small */ + + while (unquoted_len > r) + { + if (buf_size <= w) + return 0; /* The output buffer is too small */ + else + { + const char chr = unquoted[r++]; + if (('\\' == chr) || ('\"' == chr)) + { + result[w++] = '\\'; /* Escape current char */ + if (buf_size <= w) + return 0; /* The output buffer is too small */ + } + result[w++] = chr; + } + } + mhd_assert (w >= r); + mhd_assert (w <= r * 2); + return w; +} + + +#endif /* DAUTH_SUPPORT || BAUTH_SUPPORT */ diff --git a/src/microhttpd/mhd_str.h b/src/microhttpd/mhd_str.h @@ -544,7 +544,7 @@ MHD_str_equal_caseless_quoted_bin_n (const char *quoted, * DQUOTE chars, does not need to be zero-terminated * @param quoted_len the length in chars of the @a quoted string * @param[out] result the pointer to the buffer to put the result, must - * be at least @a size character long.May be modified even + * be at least @a size character long. May be modified even * if @a quoted is invalid sequence. The result is NOT * zero-terminated. * @return The number of characters written to the output buffer, @@ -558,4 +558,29 @@ MHD_str_unquote (const char *quoted, #endif /* DAUTH_SUPPORT */ +#if defined(DAUTH_SUPPORT) || defined(BAUTH_SUPPORT) + +/** + * Convert string from unquoted to quoted form as specified by + * RFC7230#section-3.2.6 and RFC7694#quoted.strings. + * + * @param unquoted the unquoted string, does not need to be zero-terminated + * @param unquoted_len the length in chars of the @a unquoted string + * @param[out] result the pointer to the buffer to put the result. May be + * modified even if function failed due to insufficient + * space. The result is NOT zero-terminated and does not + * have opening and closing DQUOTE chars. + * @param buf_size the size of the allocated memory for @a result + * @return The number of copied characters, can be up to two times more than + * @a unquoted_len, zero if @a unquoted_len is zero or if quoted + * string is larger than @a buf_size. + */ +size_t +MHD_str_quote (const char *unquoted, + size_t unquoted_len, + char *result, + size_t buf_size); + +#endif /* DAUTH_SUPPORT || BAUTH_SUPPORT */ + #endif /* MHD_STR_H */