Change in libosmocore[master]: add osmo_escape_c_str and osmo_quote_c_str

2019-11-22 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/libosmocore/+/16160 )

Change subject: add osmo_escape_c_str and osmo_quote_c_str
..


Patch Set 3: Code-Review+1

(1 comment)

https://gerrit.osmocom.org/c/libosmocore/+/16160/3//COMMIT_MSG
Commit Message:

https://gerrit.osmocom.org/c/libosmocore/+/16160/3//COMMIT_MSG@7
PS3, Line 7: _str and osmo_quote_c_str
the actual functions don't have underscores between c and str, let's avoid that 
inconsistency.



--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/16160
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I3dfb892036e0133dd8e7e4a6a0c32a3caa9b
Gerrit-Change-Number: 16160
Gerrit-PatchSet: 3
Gerrit-Owner: neels 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge 
Gerrit-Comment-Date: Fri, 22 Nov 2019 10:46:48 +
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in libosmocore[master]: add osmo_escape_c_str and osmo_quote_c_str

2019-11-21 Thread neels
neels has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/16160 )


Change subject: add osmo_escape_c_str and osmo_quote_c_str
..

add osmo_escape_c_str and osmo_quote_c_str

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: I3dfb892036e0133dd8e7e4a6a0c32a3caa9b
---
M include/osmocom/core/utils.h
M src/utils.c
M tests/utils/utils_test.c
M tests/utils/utils_test.ok
4 files changed, 301 insertions(+), 21 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/60/16160/1

diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h
index 86d45bc..4e7037a 100644
--- a/include/osmocom/core/utils.h
+++ b/include/osmocom/core/utils.h
@@ -147,6 +147,11 @@
 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);
diff --git a/src/utils.c b/src/utils.c
index 904f6e4..8dfa7ec 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -669,14 +669,18 @@

 /*! Return the string with all non-printable characters escaped.
  * This internal function is the implementation for all osmo_escape_str* and 
osmo_quote_str* API versions.
- * It provides a return value of characters-needed, to allow producing 
un-truncated strings in all cases.
+ * It provides both the legacy (non C compatible) escaping, as well as C 
compatible string constant syntax,
+ * and it provides a return value of characters-needed, to allow producing 
un-truncated strings in all cases.
  * \param[out] buf  string buffer to write escaped characters to.
  * \param[in] bufsize  sizeof(buf).
  * \param[in] str  A string that may contain any characters.
  * \param[in] in_len  Pass -1 to print until nul char, or >= 0 to force a 
length (also past nul chars).
+ * \param[in] legacy_format  If false, return C compatible string constants 
("\x0f"), if true the legacy
+ *   escaping format ("\15"). The legacy format also 
escapes as "\a\b\f\v", while
+ *   the non-legacy format also escapes those as 
"\xNN" sequences.
  * \return Number of characters that would be written if bufsize were large 
enough excluding '\0' (like snprintf()).
  */
-static size_t _osmo_escape_str_buf(char *buf, size_t bufsize, const char *str, 
int in_len)
+static size_t _osmo_escape_str_buf(char *buf, size_t bufsize, const char *str, 
int in_len, bool legacy_format)
 {
struct osmo_strbuf sb = { .buf = buf, .len = bufsize };
int in_pos = 0;
@@ -715,19 +719,28 @@
BACKSLASH_CASE('\r', 'r');
BACKSLASH_CASE('\t', 't');
BACKSLASH_CASE('\0', '0');
-   BACKSLASH_CASE('\a', 'a');
-