From 8a7eed50dbd7fc05a1c3bbf302ef8e42a5698a98 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Thu, 21 Nov 2019 00:12:10 +0100 Subject: add osmo_escape_cstr and osmo_quote_cstr Provide string escaping that - returns the required buffer size, so it can be used with OSMO_STRBUF_APPEND(). - uses C compatible string constant escaping sequences. This is intended as a replacement for all previous osmo_escape_str* and osmo_quote_str* API. It pains me that I didn't get them right the first nor the second time: - The buffer functions do not return the chars needed, which is required for allocating sufficient memory in the *_c versions of the functions. - Because of that, these functions are accurately usable for OSMO_STRBUF_APPEND(), producing truncated strings, for example when dumping a GSUP message. - They do not use the C equivalent string constant escaping: for some reason I thought "\15" would be valid, but it should be "\x0f". If I could, I would completely drop those mislead implementations ... but backwards compat prohibits that. A previous patch already provided internal static functions that accurately return the required buffer size. Enhance these to also support C compatible string escaping, and use them as implementation of the new functions: osmo_escape_cstr_buf() osmo_escape_cstr_c() osmo_quote_cstr_buf() osmo_quote_cstr_c() In the tests for these, also test C string equivalence. Naming: from API versions, it would be kind of logical to call them osmo_escape_str_buf3() and osmo_escape_str_c2(). Since these anyway return a different escaping, it makes sense to me to have distinct names instead. Quasi missing are variants of the non-C-compatible weird legacy escaping that return the required buffer size, but I refrain from adding those, because we have enough API cruft as it is. Just always use these new cstr variants. Change-Id: I3dfb892036e01000033dd8e7e4a6a0c32a3caa9b --- include/osmocom/core/utils.h | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'include') diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 1abbe989..01c4de6e 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -148,6 +148,11 @@ bool osmo_identifier_valid(const char *str); bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars); void osmo_identifier_sanitize_buf(char *str, const char *sep_chars, char replace_with); +size_t osmo_escape_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len); +char *osmo_escape_cstr_c(void *ctx, const char *str, int in_len); +size_t osmo_quote_cstr_buf(char *buf, size_t bufsize, const char *str, int in_len); +char *osmo_quote_cstr_c(void *ctx, const char *str, int in_len); + const char *osmo_escape_str(const char *str, int len); char *osmo_escape_str_buf2(char *buf, size_t bufsize, const char *str, int in_len); const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize); -- cgit v1.2.3