lib/charset: add u16_strlcat() function
authorMasahisa Kojima <masahisa.kojima@linaro.org>
Thu, 28 Apr 2022 08:09:34 +0000 (17:09 +0900)
committerHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tue, 3 May 2022 19:39:22 +0000 (21:39 +0200)
Provide u16 string version of strlcat().

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
include/charset.h
lib/charset.c

index 38908e0..20abfbe 100644 (file)
@@ -262,6 +262,20 @@ u16 *u16_strcpy(u16 *dest, const u16 *src);
 u16 *u16_strdup(const void *src);
 
 /**
+ * u16_strlcat() - Append a length-limited, %NUL-terminated string to another
+ *
+ * Append the source string @src to the destination string @dest, overwriting
+ * null word at the end of @dest adding  a terminating null word.
+ *
+ * @dest:              zero terminated u16 destination string
+ * @src:               zero terminated u16 source string
+ * @count:             size of buffer in u16 words including taling 0x0000
+ * Return:             required size including trailing 0x0000 in u16 words
+ *                     If return value >= count, truncation occurred.
+ */
+size_t u16_strlcat(u16 *dest, const u16 *src, size_t size);
+
+/**
  * utf16_to_utf8() - Convert an utf16 string to utf8
  *
  * Converts 'size' characters of the utf16 string 'src' to utf8
index de201cf..bece498 100644 (file)
@@ -416,6 +416,22 @@ u16 *u16_strdup(const void *src)
        return new;
 }
 
+size_t u16_strlcat(u16 *dest, const u16 *src, size_t count)
+{
+       size_t destlen = u16_strlen(dest);
+       size_t srclen = u16_strlen(src);
+       size_t ret = destlen + srclen + 1;
+
+       if (destlen >= count)
+               return ret;
+       if (ret > count)
+               srclen -= ret - count;
+       memcpy(&dest[destlen], src, 2 * srclen);
+       dest[destlen + srclen] = 0x0000;
+
+       return ret;
+}
+
 /* Convert UTF-16 to UTF-8.  */
 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
 {