All of lore.kernel.org
 help / color / mirror / Atom feed
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
To: Masahisa Kojima <masahisa.kojima@linaro.org>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
	Simon Glass <sjg@chromium.org>,
	Takahiro Akashi <takahiro.akashi@linaro.org>,
	Francois Ozog <francois.ozog@linaro.org>,
	Mark Kettenis <mark.kettenis@xs4all.nl>,
	u-boot@lists.denx.de
Subject: Re: [PATCH v4 02/11] lib/charset: add u16_strlcat() function
Date: Sat, 16 Apr 2022 09:32:57 +0200	[thread overview]
Message-ID: <8a5e3553-bf9a-635a-2482-67a83c15023e@gmx.de> (raw)
In-Reply-To: <20220324135443.1571-3-masahisa.kojima@linaro.org>

On 3/24/22 14:54, Masahisa Kojima wrote:
> Provide u16 string version of strlcat().
>
> Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> Reviewed-by: Simon Glass <sjg@chromium.org>
> ---
> Changes in v4:
> - add blank line above the return statement
>
> Changes in v2:
> - implement u16_strlcat(with the destination buffer size in argument)
>    instead of u16_strcat
>
>   include/charset.h | 15 +++++++++++++++
>   lib/charset.c     | 21 +++++++++++++++++++++
>   2 files changed, 36 insertions(+)
>
> 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.

Why "- 1"?

If size is even, we append up to size - u16_strlen(dst) - 2 bytes. The
two extra bytes used for 0x0000.
If size is odd, we append up to size - u16_strlen(dst) - 3 bytes leaving
one byte of the buffer unused.

> + *
> + * @dest:		destination buffer (null terminated)
> + * @src:		source buffer (null terminated)
> + * @size:		destination buffer size in bytes

s/$/ including the trailing 0x0000/

> + * 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..47997eca7d 100644
> --- a/lib/charset.c
> +++ b/lib/charset.c
> @@ -428,6 +428,27 @@ u16 *u16_strdup(const void *src)
>   	return new;
>   }
>
> +size_t u16_strlcat(u16 *dest, const u16 *src, size_t size)
> +{

If you start the function with

     size >>= 1;

or

     size /= sizeof(u16);

this might simplify the code.

> +	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;

This misses the  trailing 0x0000.

Best regards

Heinrich

> +
> +	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)
>   {


  parent reply	other threads:[~2022-04-16  7:33 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-24 13:54 [PATCH v4 00/11] enable menu-driven boot device selection Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 01/11] bootmenu: fix menu API error handling Masahisa Kojima
2022-03-30  8:55   ` Ilias Apalodimas
2022-03-24 13:54 ` [PATCH v4 02/11] lib/charset: add u16_strlcat() function Masahisa Kojima
2022-04-02  7:14   ` Heinrich Schuchardt
2022-04-04 14:50     ` Masahisa Kojima
2022-04-16  7:32   ` Heinrich Schuchardt [this message]
2022-04-18  7:47     ` Masahisa Kojima
2022-04-28  7:45       ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 03/11] test: unit test for u16_strlcat() Masahisa Kojima
2022-04-02  7:47   ` Heinrich Schuchardt
2022-04-04 14:54     ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 04/11] menu: always show the menu regardless of the number of entry Masahisa Kojima
2022-04-02  7:56   ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 05/11] efi_loader: export efi_locate_device_handle() Masahisa Kojima
2022-04-01  5:43   ` Ilias Apalodimas
2022-03-24 13:54 ` [PATCH v4 06/11] efi_loader: bootmgr: add booting from removable media Masahisa Kojima
2022-03-30 19:13   ` Ilias Apalodimas
2022-03-31  0:51     ` Masahisa Kojima
2022-03-31  6:25       ` Ilias Apalodimas
2022-04-02  6:12   ` Heinrich Schuchardt
2022-04-04  6:48     ` Masahisa Kojima
2022-04-04 21:54       ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 07/11] bootmenu: add UEFI and disto_boot entries Masahisa Kojima
2022-04-01  6:08   ` Ilias Apalodimas
2022-04-02  6:33   ` Heinrich Schuchardt
2022-04-04  8:10     ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 08/11] bootmenu: factor out the user input handling Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 09/11] efi_loader: add menu-driven UEFI Boot Variable maintenance Masahisa Kojima
2022-03-31  8:31   ` Ilias Apalodimas
2022-04-14  9:25     ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 10/11] bootmenu: add removable media entries Masahisa Kojima
2022-03-31  8:48   ` Ilias Apalodimas
2022-03-31 10:18     ` Masahisa Kojima
2022-04-04 22:00     ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 11/11] doc:bootmenu: add UEFI boot variable and distro boot support Masahisa Kojima
2022-04-02  5:51   ` Heinrich Schuchardt
2022-03-25  1:20 ` [PATCH v4 00/11] enable menu-driven boot device selection Takahiro Akashi
2022-03-25  6:57   ` Masahisa Kojima
2022-04-02  5:48 ` Heinrich Schuchardt
2022-04-04  6:10   ` Masahisa Kojima
2022-04-16  6:46 ` Heinrich Schuchardt
2022-04-28  7:35   ` Masahisa Kojima

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8a5e3553-bf9a-635a-2482-67a83c15023e@gmx.de \
    --to=xypron.glpk@gmx.de \
    --cc=francois.ozog@linaro.org \
    --cc=ilias.apalodimas@linaro.org \
    --cc=mark.kettenis@xs4all.nl \
    --cc=masahisa.kojima@linaro.org \
    --cc=sjg@chromium.org \
    --cc=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.