[PATCH 0/4] efi_loader: simplify string handling

* convert u16_strlen() into a macro * use u16_size() instead of u16_strlen() to simplify code
Heinrich Schuchardt (4): lib: convert u16_strlen() into a macro lib: simplify u16_strdup() efi_loader: EFI_HII_STRING_PROTOCOL.GetString() efi_loader: simplify efi_serialize_load_option()
include/charset.h | 26 ++++++++++++++------------ lib/charset.c | 14 +------------- lib/efi_loader/efi_hii.c | 2 +- lib/efi_loader/efi_load_options.c | 2 +- 4 files changed, 17 insertions(+), 27 deletions(-)

The function u16_strlen() can be implemented as call to u16_strnlen().
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com --- include/charset.h | 26 ++++++++++++++------------ lib/charset.c | 12 ------------ 2 files changed, 14 insertions(+), 24 deletions(-)
diff --git a/include/charset.h b/include/charset.h index b93d023092..38908e08f0 100644 --- a/include/charset.h +++ b/include/charset.h @@ -200,18 +200,6 @@ int u16_strncmp(const u16 *s1, const u16 *s2, size_t n); */ #define u16_strcmp(s1, s2) u16_strncmp((s1), (s2), SIZE_MAX)
-/** - * u16_strlen - count non-zero words - * - * This function matches wsclen() if the -fshort-wchar compiler flag is set. - * In the EFI context we explicitly need a function handling u16 strings. - * - * @in: null terminated u16 string - * Return: number of non-zero words. - * This is not the number of utf-16 letters! - */ -size_t u16_strlen(const void *in); - /** * u16_strsize() - count size of u16 string in bytes including the null * character @@ -236,6 +224,20 @@ size_t u16_strsize(const void *in); */ size_t u16_strnlen(const u16 *in, size_t count);
+/** + * u16_strlen - count non-zero words + * + * This function matches wsclen() if the -fshort-wchar compiler flag is set. + * In the EFI context we explicitly need a function handling u16 strings. + * + * @in: null terminated u16 string + * Return: number of non-zero words. + * This is not the number of utf-16 letters! + */ +size_t u16_strlen(const void *in); + +#define u16_strlen(in) u16_strnlen(in, SIZE_MAX) + /** * u16_strcpy() - copy u16 string * diff --git a/lib/charset.c b/lib/charset.c index f44c58d9d8..91cbe87509 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -375,18 +375,6 @@ int u16_strncmp(const u16 *s1, const u16 *s2, size_t n) return ret; }
-size_t u16_strlen(const void *in) -{ - const char *pos = in; - size_t ret; - - for (; pos[0] || pos[1]; pos += 2) - ; - ret = pos - (char *)in; - ret >>= 1; - return ret; -} - size_t __efi_runtime u16_strnlen(const u16 *in, size_t count) { size_t i;

Use u16_strsize() instead of duplicating it.
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com --- lib/charset.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/charset.c b/lib/charset.c index 91cbe87509..de201cf3b9 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -407,7 +407,7 @@ u16 *u16_strdup(const void *src)
if (!src) return NULL; - len = (u16_strlen(src) + 1) * sizeof(u16); + len = u16_strsize(src); new = malloc(len); if (!new) return NULL;

Use u16_strsize().
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com --- lib/efi_loader/efi_hii.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/efi_loader/efi_hii.c b/lib/efi_loader/efi_hii.c index 9f87e95e32..75ff58aafa 100644 --- a/lib/efi_loader/efi_hii.c +++ b/lib/efi_loader/efi_hii.c @@ -900,7 +900,7 @@ get_string(const struct efi_hii_string_protocol *this,
str = stbl->strings[string_id - 1].string; if (str) { - len = (u16_strlen(str) + 1) * sizeof(u16); + len = u16_strsize(str); if (*string_size < len) { *string_size = len;

Use u16_strsize().
Signed-off-by: Heinrich Schuchardt heinrich.schuchardt@canonical.com --- lib/efi_loader/efi_load_options.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/efi_loader/efi_load_options.c b/lib/efi_loader/efi_load_options.c index 68cd85ba2e..71454f0fc6 100644 --- a/lib/efi_loader/efi_load_options.c +++ b/lib/efi_loader/efi_load_options.c @@ -113,7 +113,7 @@ unsigned long efi_serialize_load_option(struct efi_load_option *lo, u8 **data) unsigned long size; u8 *p;
- label_len = (u16_strlen(lo->label) + 1) * sizeof(u16); + label_len = u16_strsize(lo->label);
/* total size */ size = sizeof(lo->attributes);
participants (1)
-
Heinrich Schuchardt