diff mbox series

[RESEND,RFC,v2,2/4] lib/charset: add u16_strlcat() function

Message ID 20220225013257.15674-3-masahisa.kojima@linaro.org
State Superseded
Headers show
Series enable menu-driven boot device selection | expand

Commit Message

Masahisa Kojima Feb. 25, 2022, 1:32 a.m. UTC
Provide u16 string version of strlcat().

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
---
Changes in v2:
- implement u16_strlcat(with the destination buffer size in argument)
  instead of u16_strcat

 include/charset.h | 15 +++++++++++++++
 lib/charset.c     | 20 ++++++++++++++++++++
 2 files changed, 35 insertions(+)

Comments

Simon Glass March 12, 2022, 2:24 a.m. UTC | #1
Hi Masahisa,

On Thu, 24 Feb 2022 at 18:31, Masahisa Kojima
<masahisa.kojima@linaro.org> wrote:
>
> Provide u16 string version of strlcat().
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> ---
> Changes in v2:
> - implement u16_strlcat(with the destination buffer size in argument)
>   instead of u16_strcat
>
>  include/charset.h | 15 +++++++++++++++
>  lib/charset.c     | 20 ++++++++++++++++++++
>  2 files changed, 35 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>

nits below

>
> diff --git a/include/charset.h b/include/charset.h
> index b93d023092..dc5fc275ec 100644
> --- a/include/charset.h
> +++ b/include/charset.h
> @@ -259,6 +259,21 @@ 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 src string to the dest string, overwriting the terminating
> + * null word at the end of dest, and then adds a terminating null word.

add

> + * It will append at most size - u16_strlen(dst) - 1 bytes, NUL-terminating the result.
> + *
> + * @dest:              destination buffer (null terminated)
> + * @src:               source buffer (null terminated)
> + * @size:              destination buffer size in bytes
> + * Return:             total size of the created string in bytes.
> + *                     If return value >= size, truncation occurred.
> + */
> +size_t u16_strlcat(u16 *dest, const u16 *src, size_t size);
> +
>  /**
>   * utf16_to_utf8() - Convert an utf16 string to utf8
>   *
> diff --git a/lib/charset.c b/lib/charset.c
> index f44c58d9d8..f15d5df19a 100644
> --- a/lib/charset.c
> +++ b/lib/charset.c
> @@ -428,6 +428,26 @@ u16 *u16_strdup(const void *src)
>         return new;
>  }
>
> +size_t u16_strlcat(u16 *dest, const u16 *src, size_t size)
> +{
> +       size_t dstrlen = u16_strnlen(dest, size >> 1);
> +       size_t dlen = dstrlen * sizeof(u16);
> +       size_t len = u16_strlen(src) * sizeof(u16);
> +       size_t ret = dlen + len;
> +
> +       if (dlen >= size)
> +               return ret;
> +
> +       dest += dstrlen;
> +       size -= dlen;
> +       if (len >= size)
> +               len = size - sizeof(u16);
> +
> +       memcpy(dest, src, len);
> +       dest[len >> 1] = u'\0';

blank line before final return

> +       return ret;
> +}
> +
>  /* Convert UTF-16 to UTF-8.  */
>  uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
>  {
> --
> 2.17.1
>

Regards,
Simon
Masahisa Kojima March 14, 2022, 1:30 a.m. UTC | #2
Hi Simon,

On Sat, 12 Mar 2022 at 11:24, Simon Glass <sjg@chromium.org> wrote:
>
> Hi Masahisa,
>
> On Thu, 24 Feb 2022 at 18:31, Masahisa Kojima
> <masahisa.kojima@linaro.org> wrote:
> >
> > Provide u16 string version of strlcat().
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > ---
> > Changes in v2:
> > - implement u16_strlcat(with the destination buffer size in argument)
> >   instead of u16_strcat
> >
> >  include/charset.h | 15 +++++++++++++++
> >  lib/charset.c     | 20 ++++++++++++++++++++
> >  2 files changed, 35 insertions(+)
>
> Reviewed-by: Simon Glass <sjg@chromium.org>
>
> nits below
>
> >
> > diff --git a/include/charset.h b/include/charset.h
> > index b93d023092..dc5fc275ec 100644
> > --- a/include/charset.h
> > +++ b/include/charset.h
> > @@ -259,6 +259,21 @@ 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 src string to the dest string, overwriting the terminating
> > + * null word at the end of dest, and then adds a terminating null word.
>
> add
>
> > + * It will append at most size - u16_strlen(dst) - 1 bytes, NUL-terminating the result.
> > + *
> > + * @dest:              destination buffer (null terminated)
> > + * @src:               source buffer (null terminated)
> > + * @size:              destination buffer size in bytes
> > + * Return:             total size of the created string in bytes.
> > + *                     If return value >= size, truncation occurred.
> > + */
> > +size_t u16_strlcat(u16 *dest, const u16 *src, size_t size);
> > +
> >  /**
> >   * utf16_to_utf8() - Convert an utf16 string to utf8
> >   *
> > diff --git a/lib/charset.c b/lib/charset.c
> > index f44c58d9d8..f15d5df19a 100644
> > --- a/lib/charset.c
> > +++ b/lib/charset.c
> > @@ -428,6 +428,26 @@ u16 *u16_strdup(const void *src)
> >         return new;
> >  }
> >
> > +size_t u16_strlcat(u16 *dest, const u16 *src, size_t size)
> > +{
> > +       size_t dstrlen = u16_strnlen(dest, size >> 1);
> > +       size_t dlen = dstrlen * sizeof(u16);
> > +       size_t len = u16_strlen(src) * sizeof(u16);
> > +       size_t ret = dlen + len;
> > +
> > +       if (dlen >= size)
> > +               return ret;
> > +
> > +       dest += dstrlen;
> > +       size -= dlen;
> > +       if (len >= size)
> > +               len = size - sizeof(u16);
> > +
> > +       memcpy(dest, src, len);
> > +       dest[len >> 1] = u'\0';
>
> blank line before final return

OK.
Thank you for your review.

Regards,
Masahisa Kojima

>
> > +       return ret;
> > +}
> > +
> >  /* Convert UTF-16 to UTF-8.  */
> >  uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
> >  {
> > --
> > 2.17.1
> >
>
> Regards,
> Simon
diff mbox series

Patch

diff --git a/include/charset.h b/include/charset.h
index b93d023092..dc5fc275ec 100644
--- a/include/charset.h
+++ b/include/charset.h
@@ -259,6 +259,21 @@  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 src string to the dest string, overwriting the terminating
+ * null word at the end of dest, and then adds a terminating null word.
+ * It will append at most size - u16_strlen(dst) - 1 bytes, NUL-terminating the result.
+ *
+ * @dest:		destination buffer (null terminated)
+ * @src:		source buffer (null terminated)
+ * @size:		destination buffer size in bytes
+ * Return:		total size of the created string in bytes.
+ *			If return value >= size, truncation occurred.
+ */
+size_t u16_strlcat(u16 *dest, const u16 *src, size_t size);
+
 /**
  * utf16_to_utf8() - Convert an utf16 string to utf8
  *
diff --git a/lib/charset.c b/lib/charset.c
index f44c58d9d8..f15d5df19a 100644
--- a/lib/charset.c
+++ b/lib/charset.c
@@ -428,6 +428,26 @@  u16 *u16_strdup(const void *src)
 	return new;
 }
 
+size_t u16_strlcat(u16 *dest, const u16 *src, size_t size)
+{
+	size_t dstrlen = u16_strnlen(dest, size >> 1);
+	size_t dlen = dstrlen * sizeof(u16);
+	size_t len = u16_strlen(src) * sizeof(u16);
+	size_t ret = dlen + len;
+
+	if (dlen >= size)
+		return ret;
+
+	dest += dstrlen;
+	size -= dlen;
+	if (len >= size)
+		len = size - sizeof(u16);
+
+	memcpy(dest, src, len);
+	dest[len >> 1] = u'\0';
+	return ret;
+}
+
 /* Convert UTF-16 to UTF-8.  */
 uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size)
 {