From patchwork Wed Aug 9 23:14:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rob Clark X-Patchwork-Id: 800028 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="qyW/Wa4d"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3xSRx46XxTz9s4q for ; Thu, 10 Aug 2017 09:16:56 +1000 (AEST) Received: by lists.denx.de (Postfix, from userid 105) id 90182C21E01; Wed, 9 Aug 2017 23:15:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=-0.0 required=5.0 tests=FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id CAE54C21DC1; Wed, 9 Aug 2017 23:15:27 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 8A489C21D83; Wed, 9 Aug 2017 23:15:02 +0000 (UTC) Received: from mail-qk0-f196.google.com (mail-qk0-f196.google.com [209.85.220.196]) by lists.denx.de (Postfix) with ESMTPS id A3931C21C8B for ; Wed, 9 Aug 2017 23:14:58 +0000 (UTC) Received: by mail-qk0-f196.google.com with SMTP id d136so7092912qkg.3 for ; Wed, 09 Aug 2017 16:14:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ScP1OqGaaiOdD4o8vhex4d75FfCX1fJZ0D8GBzCv6sY=; b=qyW/Wa4dJAsjnPMibwn/d5TNPtU4eF+rEUSPm7IpR45hj1bAaxEcm11fl7FE7feCic ETjVX5vrscqEopbRh6wDGnydgceu7f85RuUGslzSys5O5l7aV+2S6LkZ+aypzgWQ6/cs pLDfHslTied+iP+eNHc1Hmv14jKLTq7K64hmavYY1NPMmmxP2T8NpLGHGE99wO324vjP 3GeIcRT8taqi6jZcJeeM9QE55SckQVW16LyC9UsmelCgy1lE4fxBei6pASNj66HMLmsf SnrGpWPmV8EfMAtczbYSU0o+2DEcFMX98eYBDkaMwNk4bOmOzmBAIuFLSdMYtPz+soII MIxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ScP1OqGaaiOdD4o8vhex4d75FfCX1fJZ0D8GBzCv6sY=; b=NXhudW9+rylBDMiUD5bc2z5dY1yQsm4y7FohorkcXuOitMtu/MDcy5Afr/GCJsrSAG j8zCGGvsih81VFcGAyVokhugIY6wkf10wbmrlUkiHU130wx3xZTalyJC3vmvT2rnxuce GTpBcp7poq4bF8KvHq+Uc9QX7J1CKLlul4mIvNb5YmxDQ2fdgo8LxOWAY6Mew04E1ZMN hJh9LlALnhA9MmutfFyVVj2NPFPdJnq3qxWEfGALYbKCQWhRzYORtbl7m3tqgxWsd2Kr jxtol/OA6nogi2s9OiGOElxcrgAl3eRNQTXZkJ8BRMoYZRwuiqsHegsaS2ecjcvHAaa4 HcHA== X-Gm-Message-State: AHYfb5jht4wCv0Z2RVF/k+l8LW8sVCW0yyw9LuVmJhNtTwPb9CqjFGcQ PEzHsQwuAMNHiQ7MnSI= X-Received: by 10.55.154.200 with SMTP id c191mr12708463qke.256.1502320497302; Wed, 09 Aug 2017 16:14:57 -0700 (PDT) Received: from localhost ([2601:184:4780:aac0:25f8:dd96:a084:785a]) by smtp.gmail.com with ESMTPSA id t74sm2631451qke.85.2017.08.09.16.14.55 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 09 Aug 2017 16:14:56 -0700 (PDT) From: Rob Clark To: U-Boot Mailing List Date: Wed, 9 Aug 2017 19:14:32 -0400 Message-Id: <20170809231441.22691-3-robdclark@gmail.com> X-Mailer: git-send-email 2.13.0 In-Reply-To: <20170809231441.22691-1-robdclark@gmail.com> References: <20170809231441.22691-1-robdclark@gmail.com> Subject: [U-Boot] [PATCH 2/5] lib: add some utf16 handling helpers X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" We'll eventually want these in a few places in efi_loader, and also vsprintf. Signed-off-by: Rob Clark --- include/charset.h | 55 ++++++++++++++++++++++++++++++ lib/Makefile | 1 + lib/charset.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ lib/efi_loader/efi_console.c | 17 ++-------- 4 files changed, 140 insertions(+), 14 deletions(-) create mode 100644 include/charset.h create mode 100644 lib/charset.c diff --git a/include/charset.h b/include/charset.h new file mode 100644 index 0000000000..47ff6c7af1 --- /dev/null +++ b/include/charset.h @@ -0,0 +1,55 @@ +/* + * charset conversion utils + * + * Copyright (c) 2017 Rob Clark + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#ifndef __CHARSET_H_ +#define __CHARSET_H_ + +#define MAX_UTF8_PER_UTF16 4 + +/** + * utf16_strlen() - Get the length of an utf16 string + * + * Returns the number of 16 bit characters in an utf16 string, not + * including the terminating NULL character. + * + * @in the string to measure + * @return the string length + */ +size_t utf16_strlen(uint16_t *in); + +/** + * utf16_strnlen() - Get the length of a fixed-size utf16 string. + * + * Returns the number of 16 bit characters in an utf16 string, + * not including the terminating NULL character, but at most + * 'count' number of characters. In doing this, utf16_strnlen() + * looks at only the first 'count' characters. + * + * @in the string to measure + * @count the maximum number of characters to count + * @return the string length, up to a maximum of 'count' + */ +size_t utf16_strnlen(const uint16_t *in, size_t count); + +/** + * utf16_to_utf8() - Convert an utf16 string to utf8 + * + * Converts 'size' characters of the utf16 string 'src' to utf8 + * written to the 'dest' buffer. + * + * NOTE that a single utf16 character can generate up to 4 utf8 + * characters. See MAX_UTF8_PER_UTF16. + * + * @dest the destination buffer to write the utf8 characters + * @src the source utf16 string + * @size the number of utf16 characters to convert + * @return the pointer to the first unwritten byte in 'dest' + */ +uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size); + +#endif /* __CHARSET_H_ */ diff --git a/lib/Makefile b/lib/Makefile index eacc7d6485..b88b6ebd53 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_OF_LIVE) += of_live.o obj-$(CONFIG_CMD_DHRYSTONE) += dhry/ obj-$(CONFIG_AES) += aes.o +obj-y += charset.o obj-$(CONFIG_USB_TTY) += circbuf.o obj-y += crc7.o obj-y += crc8.o diff --git a/lib/charset.c b/lib/charset.c new file mode 100644 index 0000000000..eaff2e542e --- /dev/null +++ b/lib/charset.c @@ -0,0 +1,81 @@ +/* + * charset conversion utils + * + * Copyright (c) 2017 Rob Clark + * + * SPDX-License-Identifier: GPL-2.0+ + */ + +#include +#include + +/* + * utf8/utf16 conversion mostly lifted from grub + */ + +size_t utf16_strlen(uint16_t *in) +{ + size_t i; + for (i = 0; in[i]; i++); + return i; +} + +size_t utf16_strnlen(const uint16_t *in, size_t count) +{ + size_t i; + for (i = 0; count-- && in[i]; i++); + return i; +} + +/* Convert UTF-16 to UTF-8. */ +uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size) +{ + uint32_t code_high = 0; + + while (size--) { + uint32_t code = *src++; + + if (code_high) { + if (code >= 0xDC00 && code <= 0xDFFF) { + /* Surrogate pair. */ + code = ((code_high - 0xD800) << 10) + (code - 0xDC00) + 0x10000; + + *dest++ = (code >> 18) | 0xF0; + *dest++ = ((code >> 12) & 0x3F) | 0x80; + *dest++ = ((code >> 6) & 0x3F) | 0x80; + *dest++ = (code & 0x3F) | 0x80; + } else { + /* Error... */ + *dest++ = '?'; + /* *src may be valid. Don't eat it. */ + src--; + } + + code_high = 0; + } else { + if (code <= 0x007F) { + *dest++ = code; + } else if (code <= 0x07FF) { + *dest++ = (code >> 6) | 0xC0; + *dest++ = (code & 0x3F) | 0x80; + } else if (code >= 0xD800 && code <= 0xDBFF) { + code_high = code; + continue; + } else if (code >= 0xDC00 && code <= 0xDFFF) { + /* Error... */ + *dest++ = '?'; + } else if (code < 0x10000) { + *dest++ = (code >> 12) | 0xE0; + *dest++ = ((code >> 6) & 0x3F) | 0x80; + *dest++ = (code & 0x3F) | 0x80; + } else { + *dest++ = (code >> 18) | 0xF0; + *dest++ = ((code >> 12) & 0x3F) | 0x80; + *dest++ = ((code >> 6) & 0x3F) | 0x80; + *dest++ = (code & 0x3F) | 0x80; + } + } + } + + return dest; +} diff --git a/lib/efi_loader/efi_console.c b/lib/efi_loader/efi_console.c index 5ebce4b544..3fc82b8726 100644 --- a/lib/efi_loader/efi_console.c +++ b/lib/efi_loader/efi_console.c @@ -7,6 +7,7 @@ */ #include +#include #include static bool console_size_queried; @@ -138,20 +139,8 @@ static efi_status_t EFIAPI efi_cout_reset( static void print_unicode_in_utf8(u16 c) { - char utf8[4] = { 0 }; - char *b = utf8; - - if (c < 0x80) { - *(b++) = c; - } else if (c < 0x800) { - *(b++) = 192 + c / 64; - *(b++) = 128 + c % 64; - } else { - *(b++) = 224 + c / 4096; - *(b++) = 128 + c / 64 % 64; - *(b++) = 128 + c % 64; - } - + char utf8[MAX_UTF8_PER_UTF16] = { 0 }; + utf16_to_utf8((u8 *)utf8, &c, 1); puts(utf8); }