[MERGED] libosmocore[master]: utils: add osmo_escape_str()
Harald Welte has submitted this change and it was merged. Change subject: utils: add osmo_escape_str() .. utils: add osmo_escape_str() To report invalid characters in identifiers, it is desirable to escape any weird characters. Otherwise we might print stray newlines or control characters in the log output. ctrl_test.c already uses a print_escaped() function, which will be replaced by osmo_escape_str() in a subsequent patch. control_cmd.c will use osmo_escape_str() to log invalid identifiers. Change-Id: Ic685eb63dead3967d01aaa4f1e9899e5461ca49a --- 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, 167 insertions(+), 0 deletions(-) Approvals: Harald Welte: Looks good to me, approved Jenkins Builder: Verified diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 0973b4c..72266ae 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -120,4 +120,7 @@ bool osmo_identifier_valid(const char *str); bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars); +const char *osmo_escape_str(const char *str, int 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 6cc823e..109aac0 100644 --- a/src/utils.c +++ b/src/utils.c @@ -467,4 +467,91 @@ return osmo_separated_identifiers_valid(str, NULL); } +/*! Return the string with all non-printable characters escaped. + * \param[in] str A string that may contain any characters. + * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. + * \param[inout] buf string buffer to write escaped characters to. + * \param[in] bufsize size of \a buf. + * \returns buf containing an escaped representation, possibly truncated, or str itself. + */ +const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize) +{ + int in_pos = 0; + int next_unprintable = 0; + int out_pos = 0; + char *out = buf; + /* -1 to leave space for a final \0 */ + int out_len = bufsize-1; + + if (!str) + return "(null)"; + + if (in_len < 0) + in_len = strlen(str); + + while (in_pos < in_len) { + for (next_unprintable = in_pos; +next_unprintable < in_len && isprint((int)str[next_unprintable]) +&& str[next_unprintable] != '"' +&& str[next_unprintable] != '\\'; +next_unprintable++); + + if (next_unprintable == in_len + && in_pos == 0) + return str; + + while (in_pos < next_unprintable && out_pos < out_len) + out[out_pos++] = str[in_pos++]; + + if (out_pos == out_len || in_pos == in_len) + goto done; + + switch (str[next_unprintable]) { +#define BACKSLASH_CASE(c, repr) \ + case c: \ + if (out_pos > out_len-2) \ + goto done; \ + out[out_pos++] = '\\'; \ + out[out_pos++] = repr; \ + break + + BACKSLASH_CASE('\n', 'n'); + BACKSLASH_CASE('\r', 'r'); + BACKSLASH_CASE('\t', 't'); + BACKSLASH_CASE('\0', '0'); + BACKSLASH_CASE('\a', 'a'); + BACKSLASH_CASE('\b', 'b'); + BACKSLASH_CASE('\v', 'v'); + BACKSLASH_CASE('\f', 'f'); + BACKSLASH_CASE('\\', '\\'); + BACKSLASH_CASE('"', '"'); +#undef BACKSLASH_CASE + + default: + out_pos += snprintf([out_pos], out_len - out_pos, "\\%u", (unsigned char)str[in_pos]); + if (out_pos > out_len) { + out_pos = out_len; + goto done; + } + break; + } + in_pos ++; + } + +done: + out[out_pos] = '\0'; + return out; +} + +/*! Return the string with all non-printable characters escaped. + * Call osmo_escape_str_buf() with a static buffer. + * \param[in] str A string that may contain any characters. + * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. + * \returns buf containing an escaped representation, possibly truncated, or str itself. + */ +const char *osmo_escape_str(const char *str, int in_len) +{ + return osmo_escape_str_buf(str, in_len, namebuf, sizeof(namebuf)); +} + /*! @} */ diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c index e6d7ae8..b4f7cd3 100644 --- a/tests/utils/utils_test.c +++ b/tests/utils/utils_test.c @@ -323,6 +323,53 @@ } } +static void
libosmocore[master]: utils: add osmo_escape_str()
Patch Set 3: Code-Review+2 -- To view, visit https://gerrit.osmocom.org/5429 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: Ic685eb63dead3967d01aaa4f1e9899e5461ca49a Gerrit-PatchSet: 3 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Neels HofmeyrGerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-HasComments: No
[PATCH] libosmocore[master]: utils: add osmo_escape_str()
Hello Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/5429 to look at the new patch set (#3). utils: add osmo_escape_str() To report invalid characters in identifiers, it is desirable to escape any weird characters. Otherwise we might print stray newlines or control characters in the log output. ctrl_test.c already uses a print_escaped() function, which will be replaced by osmo_escape_str() in a subsequent patch. control_cmd.c will use osmo_escape_str() to log invalid identifiers. Change-Id: Ic685eb63dead3967d01aaa4f1e9899e5461ca49a --- 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, 167 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/29/5429/3 diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 0973b4c..72266ae 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -120,4 +120,7 @@ bool osmo_identifier_valid(const char *str); bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars); +const char *osmo_escape_str(const char *str, int 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 6cc823e..109aac0 100644 --- a/src/utils.c +++ b/src/utils.c @@ -467,4 +467,91 @@ return osmo_separated_identifiers_valid(str, NULL); } +/*! Return the string with all non-printable characters escaped. + * \param[in] str A string that may contain any characters. + * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. + * \param[inout] buf string buffer to write escaped characters to. + * \param[in] bufsize size of \a buf. + * \returns buf containing an escaped representation, possibly truncated, or str itself. + */ +const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize) +{ + int in_pos = 0; + int next_unprintable = 0; + int out_pos = 0; + char *out = buf; + /* -1 to leave space for a final \0 */ + int out_len = bufsize-1; + + if (!str) + return "(null)"; + + if (in_len < 0) + in_len = strlen(str); + + while (in_pos < in_len) { + for (next_unprintable = in_pos; +next_unprintable < in_len && isprint((int)str[next_unprintable]) +&& str[next_unprintable] != '"' +&& str[next_unprintable] != '\\'; +next_unprintable++); + + if (next_unprintable == in_len + && in_pos == 0) + return str; + + while (in_pos < next_unprintable && out_pos < out_len) + out[out_pos++] = str[in_pos++]; + + if (out_pos == out_len || in_pos == in_len) + goto done; + + switch (str[next_unprintable]) { +#define BACKSLASH_CASE(c, repr) \ + case c: \ + if (out_pos > out_len-2) \ + goto done; \ + out[out_pos++] = '\\'; \ + out[out_pos++] = repr; \ + break + + BACKSLASH_CASE('\n', 'n'); + BACKSLASH_CASE('\r', 'r'); + BACKSLASH_CASE('\t', 't'); + BACKSLASH_CASE('\0', '0'); + BACKSLASH_CASE('\a', 'a'); + BACKSLASH_CASE('\b', 'b'); + BACKSLASH_CASE('\v', 'v'); + BACKSLASH_CASE('\f', 'f'); + BACKSLASH_CASE('\\', '\\'); + BACKSLASH_CASE('"', '"'); +#undef BACKSLASH_CASE + + default: + out_pos += snprintf([out_pos], out_len - out_pos, "\\%u", (unsigned char)str[in_pos]); + if (out_pos > out_len) { + out_pos = out_len; + goto done; + } + break; + } + in_pos ++; + } + +done: + out[out_pos] = '\0'; + return out; +} + +/*! Return the string with all non-printable characters escaped. + * Call osmo_escape_str_buf() with a static buffer. + * \param[in] str A string that may contain any characters. + * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. + * \returns buf containing an escaped representation, possibly truncated, or str itself. + */ +const char *osmo_escape_str(const char *str, int in_len) +{ + return osmo_escape_str_buf(str, in_len, namebuf, sizeof(namebuf)); +} + /*! @} */ diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c index e6d7ae8..b4f7cd3 100644 --- a/tests/utils/utils_test.c +++ b/tests/utils/utils_test.c @@ -323,6 +323,53 @@ } } +static void str_escape_test(void) +{ + int i; + int
libosmocore[master]: utils: add osmo_escape_str()
Patch Set 2: (1 comment) https://gerrit.osmocom.org/#/c/5429/2/src/utils.c File src/utils.c: Line 498: whitespace -- To view, visit https://gerrit.osmocom.org/5429 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: Ic685eb63dead3967d01aaa4f1e9899e5461ca49a Gerrit-PatchSet: 2 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Neels HofmeyrGerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-HasComments: Yes
[PATCH] libosmocore[master]: utils: add osmo_escape_str()
Review at https://gerrit.osmocom.org/5429 utils: add osmo_escape_str() To report invalid characters in identifiers, it is desirable to escape any weird characters. Otherwise we might print stray newlines or control characters in the log output. ctrl_test.c already uses a print_escaped() function, which will be replaced by osmo_escape_str() in a subsequent patch. control_cmd.c will use osmo_escape_str() to log invalid identifiers. Change-Id: Ic685eb63dead3967d01aaa4f1e9899e5461ca49a --- 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, 167 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/29/5429/1 diff --git a/include/osmocom/core/utils.h b/include/osmocom/core/utils.h index 0973b4c..72266ae 100644 --- a/include/osmocom/core/utils.h +++ b/include/osmocom/core/utils.h @@ -120,4 +120,7 @@ bool osmo_identifier_valid(const char *str); bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars); +const char *osmo_escape_str(const char *str, int 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 d64bb57..0dd2142 100644 --- a/src/utils.c +++ b/src/utils.c @@ -467,4 +467,91 @@ return osmo_separated_identifiers_valid(str, NULL); } +/*! Return the string with all non-printable characters escaped. + * \param[in] str A string that may contain any characters. + * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. + * \param[inout] buf string buffer to write escaped characters to. + * \param[in] bufsize size of \a buf. + * \returns buf containing an escaped representation, possibly truncated, or str itself. + */ +const char *osmo_escape_str_buf(const char *str, int in_len, char *buf, size_t bufsize) +{ + int in_pos = 0; + int next_unprintable = 0; + int out_pos = 0; + char *out = buf; + /* -1 to leave space for a final \0 */ + int out_len = bufsize-1; + + if (!str) + return "(null)"; + + if (in_len < 0) + in_len = strlen(str); + + while (in_pos < in_len) { + for (next_unprintable = in_pos; +next_unprintable < in_len && isprint(str[next_unprintable]) +&& str[next_unprintable] != '"' +&& str[next_unprintable] != '\\'; +next_unprintable++); + + if (next_unprintable == in_len + && in_pos == 0) + return str; + + while (in_pos < next_unprintable && out_pos < out_len) + out[out_pos++] = str[in_pos++]; + + if (out_pos == out_len || in_pos == in_len) + goto done; + + switch (str[next_unprintable]) { +#define BACKSLASH_CASE(c, repr) \ + case c: \ + if (out_pos > out_len-2) \ + goto done; \ + out[out_pos++] = '\\'; \ + out[out_pos++] = repr; \ + break + + BACKSLASH_CASE('\n', 'n'); + BACKSLASH_CASE('\r', 'r'); + BACKSLASH_CASE('\t', 't'); + BACKSLASH_CASE('\0', '0'); + BACKSLASH_CASE('\a', 'a'); + BACKSLASH_CASE('\b', 'b'); + BACKSLASH_CASE('\v', 'v'); + BACKSLASH_CASE('\f', 'f'); + BACKSLASH_CASE('\\', '\\'); + BACKSLASH_CASE('"', '"'); +#undef BACKSLASH_CASE + + default: + out_pos += snprintf([out_pos], out_len - out_pos, "\\%u", (unsigned char)str[in_pos]); + if (out_pos > out_len) { + out_pos = out_len; + goto done; + } + break; + } + in_pos ++; + } + +done: + out[out_pos] = '\0'; + return out; +} + +/*! Return the string with all non-printable characters escaped. + * Call osmo_escape_str_buf() with a static buffer. + * \param[in] str A string that may contain any characters. + * \param[in] len Pass -1 to print until nul char, or >= 0 to force a length. + * \returns buf containing an escaped representation, possibly truncated, or str itself. + */ +const char *osmo_escape_str(const char *str, int in_len) +{ + return osmo_escape_str_buf(str, in_len, namebuf, sizeof(namebuf)); +} + /*! @} */ diff --git a/tests/utils/utils_test.c b/tests/utils/utils_test.c index e6d7ae8..b4f7cd3 100644 --- a/tests/utils/utils_test.c +++ b/tests/utils/utils_test.c @@ -323,6 +323,53 @@ } } +static void str_escape_test(void) +{ + int i; + int j; + uint8_t in_buf[32]; + char out_buf[11]; + const char *printable =