summaryrefslogtreecommitdiffstats
path: root/src/basic/utf8.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-06-09 13:41:24 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2018-06-09 13:41:24 +0200
commit294a3121aac8ca8bdadbf4f826ec29e2702cd646 (patch)
tree969c21c101ec7c23a00cc713e067638781d09531 /src/basic/utf8.c
parentlldp: check that lldp neighbor raw data size is in expected range (diff)
downloadsystemd-294a3121aac8ca8bdadbf4f826ec29e2702cd646.tar.xz
systemd-294a3121aac8ca8bdadbf4f826ec29e2702cd646.zip
basic/utf8: add ascii_is_valid_n()
Diffstat (limited to 'src/basic/utf8.c')
-rw-r--r--src/basic/utf8.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/basic/utf8.c b/src/basic/utf8.c
index 670a98a6a9..8bef153968 100644
--- a/src/basic/utf8.c
+++ b/src/basic/utf8.c
@@ -247,6 +247,9 @@ char *utf8_escape_non_printable(const char *str) {
char *ascii_is_valid(const char *str) {
const char *p;
+ /* Check whether the string consists of valid ASCII bytes,
+ * i.e values between 0 and 127, inclusive. */
+
assert(str);
for (p = str; *p; p++)
@@ -256,6 +259,21 @@ char *ascii_is_valid(const char *str) {
return (char*) str;
}
+char *ascii_is_valid_n(const char *str, size_t len) {
+ size_t i;
+
+ /* Very similar to ascii_is_valid(), but checks exactly len
+ * bytes and rejects any NULs in that range. */
+
+ assert(str);
+
+ for (i = 0; i < len; i++)
+ if ((unsigned char) str[i] >= 128 || str[i] == 0)
+ return NULL;
+
+ return (char*) str;
+}
+
/**
* utf8_encode_unichar() - Encode single UCS-4 character as UTF-8
* @out_utf8: output buffer of at least 4 bytes or NULL