util/charset: Add utf16_len_n()
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Wed, 8 Nov 2023 23:43:07 +0000 (12:43 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 15 Nov 2023 22:07:36 +0000 (22:07 +0000)
This function returns the length in bytes — at most ‘n’ — of a UTF‐16
string excluding the null terminator.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/util/charset/charset.h
lib/util/charset/util_unistr.c

index 0f3c6750a4fa5b82e1818992962f248d87169013..195655ba1e59a1103b74ee5f0ecd2e34b5393ffc 100644 (file)
@@ -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
index 66620fb543290255a1d36000558c8368d09072ee..6eb3f50269186a57a0dea4a1ceb405678bad69e8 100644 (file)
@@ -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;