From: Joseph Sutton Date: Wed, 8 Nov 2023 23:43:07 +0000 (+1300) Subject: util/charset: Add utf16_len_n() X-Git-Tag: talloc-2.4.2~679 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a46746381b2bf7c336e4411430ecfc5fbb3cbb2a;p=thirdparty%2Fsamba.git util/charset: Add utf16_len_n() This function returns the length in bytes — at most ‘n’ — of a UTF‐16 string excluding the null terminator. Signed-off-by: Joseph Sutton Reviewed-by: Andrew Bartlett --- diff --git a/lib/util/charset/charset.h b/lib/util/charset/charset.h index 0f3c6750a4f..195655ba1e5 100644 --- a/lib/util/charset/charset.h +++ b/lib/util/charset/charset.h @@ -114,6 +114,12 @@ the result includes the null termination **/ size_t utf16_null_terminated_len(const void *buf); +/** +return the number of bytes occupied by a buffer in CH_UTF16 format +limited by 'n' bytes +**/ +size_t utf16_len_n(const void *src, size_t n); + /** return the number of bytes occupied by a buffer in CH_UTF16 format the result includes the null termination diff --git a/lib/util/charset/util_unistr.c b/lib/util/charset/util_unistr.c index 66620fb5432..6eb3f502691 100644 --- a/lib/util/charset/util_unistr.c +++ b/lib/util/charset/util_unistr.c @@ -212,6 +212,19 @@ size_t utf16_null_terminated_len(const void *buf) return utf16_len(buf) + 2; } +/** +return the number of bytes occupied by a buffer in CH_UTF16 format +limited by 'n' bytes +**/ +size_t utf16_len_n(const void *src, size_t n) +{ + size_t len; + + for (len = 0; (len+2 <= n) && SVAL(src, len); len += 2) ; + + return len; +} + /** return the number of bytes occupied by a buffer in CH_UTF16 format the result includes the null termination @@ -221,7 +234,7 @@ size_t utf16_null_terminated_len_n(const void *src, size_t n) { size_t len; - for (len = 0; (len+2 <= n) && SVAL(src, len); len += 2) ; + len = utf16_len_n(src, n); if (len+2 <= n) { len += 2;