From de3e831eae4131980220baaddbe3e8c4b38bebd1 Mon Sep 17 00:00:00 2001 From: "Ying-Chun Liu (PaulLiu)" Date: Wed, 13 Aug 2025 17:44:31 +0100 Subject: [PATCH] lib: uefi: add charset converting helper functions. The UEFI variable names are in UTF-16. We add several helper functions to convert US-ASCII to UTF-16. Signed-off-by: Ying-Chun Liu (PaulLiu) --- lib/uefi/charset.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++ lib/uefi/charset.h | 28 +++++++++++++++++ lib/uefi/rules.mk | 1 + 3 files changed, 102 insertions(+) create mode 100644 lib/uefi/charset.cpp create mode 100644 lib/uefi/charset.h diff --git a/lib/uefi/charset.cpp b/lib/uefi/charset.cpp new file mode 100644 index 00000000..6996a3e1 --- /dev/null +++ b/lib/uefi/charset.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "charset.h" + +size_t utf16_strlen(const char16_t *str) { + size_t len = 0; + + for (; *str; str++) { + len++; + } + + return len; +} + +int utf16_strcmp(const char16_t *s1, const char16_t *s2) { + int ret = 0; + + for (; ; s1++, s2++) { + ret = *s1 - *s2; + if (ret || !*s1) + break; + } + + return ret; +} + + +/** + * @brief convert utf-8 string to utf-16 string. + * + * This function converts utf-8 string to utf-16 string. However + * it only supports us-ascii input right now. + */ +int utf8_to_utf16(char16_t *dest, const char *src, size_t size) { + size_t i = 0; + for (; i < size - 1 && *src; i++, src++) { + dest[i] = *src; + } + + dest[i] = 0; + return 0; +} + +/** + * @brief convert utf-16 string to utf-8 string. + * + * This function converts utf-16 string to utf-8 string. However + * it only supports us-ascii output right now. + */ +int utf16_to_utf8(char *dest, const char16_t *src, size_t size) { + size_t i = 0; + for (; i < size - 1 && *src; i++, src++) { + dest[i] = *src; + } + + dest[i] = 0; + return 0; +} diff --git a/lib/uefi/charset.h b/lib/uefi/charset.h new file mode 100644 index 00000000..3154c90f --- /dev/null +++ b/lib/uefi/charset.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef __CHARSET_ +#define __CHARSET_ + +#include + +size_t utf16_strlen(const char16_t *str); +int utf16_strcmp(const char16_t *s1, const char16_t *s2); +int utf8_to_utf16(char16_t *dest, const char *src, size_t size); +int utf16_to_utf8(char *dest, const char16_t *src, size_t size); + +#endif diff --git a/lib/uefi/rules.mk b/lib/uefi/rules.mk index 6d2b66cb..b8eff87e 100644 --- a/lib/uefi/rules.mk +++ b/lib/uefi/rules.mk @@ -21,6 +21,7 @@ MODULE_SRCS += \ $(LOCAL_DIR)/events.cpp \ $(LOCAL_DIR)/io_stack.cpp \ $(LOCAL_DIR)/debug_support.cpp \ + $(LOCAL_DIR)/charset.cpp \ include make/module.mk