From patchwork Fri Oct 13 17:33:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 825652 X-Patchwork-Delegate: agraf@suse.de 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=) Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 3yDFLH58N7z9s7C for ; Sat, 14 Oct 2017 04:38:15 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 2856CC2206D; Fri, 13 Oct 2017 17:35:59 +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_MSPIKE_H2 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 98560C22065; Fri, 13 Oct 2017 17:33:46 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 07579C21F7F; Fri, 13 Oct 2017 17:33:39 +0000 (UTC) Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) by lists.denx.de (Postfix) with ESMTPS id CD6B5C22016 for ; Fri, 13 Oct 2017 17:33:38 +0000 (UTC) Received: from workstation4.fritz.box ([94.114.42.150]) by mail.gmx.com (mrgmx102 [212.227.17.174]) with ESMTPSA (Nemesis) id 0LlGoc-1dVNiR0Isg-00b5Om; Fri, 13 Oct 2017 19:33:37 +0200 From: Heinrich Schuchardt To: Alexander Graf Date: Fri, 13 Oct 2017 19:33:09 +0200 Message-Id: <20171013173314.22304-5-xypron.glpk@gmx.de> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20171013173314.22304-1-xypron.glpk@gmx.de> References: <20171013173314.22304-1-xypron.glpk@gmx.de> X-Provags-ID: V03:K0:JU6jg12IByc8CWzK5F+jdYpaAHJHofTD5xBcbrTG3TMYyAanvNg uL6A6E3kK5keYgNo1laptqEpax0UZd3TeCDp4smI6wVDTBsqWXMP7skcQZUUaPAwDMrxmtq 7s4fw49AwMP6WJDdC3HafyIxLCHvWWFSlOml726U1nP9vINre0aRo51z+wfaD67PAoyDb8S hjQtKYn7h55pe/Vs4NBPw== X-UI-Out-Filterresults: notjunk:1; V01:K0:qrv/jrRbRyw=:0jvTgHpePYMfvQH7Ljj9Rg 5E76tRAbaru+8neH42JamZeY+HIiDf+EbIs+wisafz8eNjJPkfR//rnffmqZ5gG+jCdUKp7kO ky/gOVwNctGEz9b2PAXzGZ7KBcJ+NmtZ6OicWzzBLxMW3guoYAlKQjVsqTzN2Mjz3qz9qHak+ GgPNyATJ7BR3sOCROAP2BqB3ZFFxPHPg3fnV78gIo6R+GuTgDPf03K+uN3spFTWui9ZNhGlos FXlSQ+fAMCO0a6rXiu5v6E3j4vG6p9X6XLHYPPTDHBM9mwHdCHD/k4L2n4IVbwLJJtqGx7gN9 jUlF2XDgv/t44ElxgiYHAYGYfDxL71jo671SRZV0MY1CvRbAyqtaBhJPbS+8LzIb3ZxD3TEVV TbBFvzOkn3SEg+1Tu4Z0k1tDGKllzJyTHZEnHkTuchoyH3nB64lLzHJo3g5gFREs9kGNJgXfp 1UlPIRbqrensMi+Y1PIa6FmgshpkzRNyaxhTmJ1VGMfFzft1Z71fksGkI+egc8DJafkVzCx4g hMItQYps4Sxqo2UVjHeCgkyRFUgcpAoHLvPxnjpIwRWL0+eOiDaLRNJTbvjcx9c5RvxEyZRKT N8J4kom84RuEdxv1eMY8c//CNJL4EXPTQyCIKnYFni69CyqRfYdS14h8Gv3yVuAiuRTXAtORb aQo1QzrfuYT7+t9lzA0WLXmm1earU/JeZOYRaqbOxf4rXnlADjYQT8UHD568U4FcASVBNf1KF RjiipJ6WPsKfwfw/DsI7ymjhA0BcCKk93QiTDg4k8uYwPuY7GBo5PzA74mAzGvXTeI3vXmjhl IKzquD8dF2KiqAotJldgdyr9Zv7nQ== Cc: u-boot@lists.denx.de, Heinrich Schuchardt Subject: [U-Boot] [PATCH v2 4/9] efi_loader: new function utf8_to_utf16 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" Provide a conversion function from utf8 to utf16. Add missing #include in include/charset.h. Remove superfluous #include in lib/charset.c. Signed-off-by: Heinrich Schuchardt --- v2 new patch --- include/charset.h | 15 +++++++++++++++ lib/charset.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/include/charset.h b/include/charset.h index 37a3278499..2662c2f7c9 100644 --- a/include/charset.h +++ b/include/charset.h @@ -9,6 +9,8 @@ #ifndef __CHARSET_H_ #define __CHARSET_H_ +#include + #define MAX_UTF8_PER_UTF16 3 /** @@ -62,4 +64,17 @@ uint16_t *utf16_strdup(const uint16_t *s); */ uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size); +/** + * utf8_to_utf16() - Convert an utf8 string to utf16 + * + * Converts up to 'size' characters of the utf16 string 'src' to utf8 + * written to the 'dest' buffer. Stops at 0x00. + * + * @dest the destination buffer to write the utf8 characters + * @src the source utf16 string + * @size maximum number of utf16 characters to convert + * @return the pointer to the first unwritten byte in 'dest' + */ +uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size); + #endif /* __CHARSET_H_ */ diff --git a/lib/charset.c b/lib/charset.c index ff76e88c77..8cd17ea1cb 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -6,7 +6,6 @@ * SPDX-License-Identifier: GPL-2.0+ */ -#include #include #include @@ -99,3 +98,59 @@ uint8_t *utf16_to_utf8(uint8_t *dest, const uint16_t *src, size_t size) return dest; } + +uint16_t *utf8_to_utf16(uint16_t *dest, const uint8_t *src, size_t size) +{ + while (size--) { + int extension_bytes; + uint32_t code; + + extension_bytes = 0; + if (*src <= 0x7f) { + code = *src++; + /* Exit on zero byte */ + if (!code) + size = 0; + } else if (*src <= 0xbf) { + /* Illegal code */ + code = '?'; + } else if (*src <= 0xdf) { + code = *src++ & 0x1f; + extension_bytes = 1; + } else if (*src <= 0xef) { + code = *src++ & 0x0f; + extension_bytes = 2; + } else if (*src <= 0xf7) { + code = *src++ & 0x07; + extension_bytes = 3; + } else { + /* Illegal code */ + code = '?'; + } + + for (; extension_bytes && size; --size, --extension_bytes) { + if ((*src & 0xc0) == 0x80) { + code <<= 6; + code |= *src++ & 0x3f; + } else { + /* Illegal code */ + code = '?'; + ++src; + --size; + break; + } + } + + if (code < 0x10000) { + *dest++ = code; + } else { + /* + * Simplified expression for + * (((code - 0x10000) >> 10) & 0x3ff) | 0xd800 + */ + *dest++ = (code >> 10) + 0xd7c0; + *dest++ = (code & 0x3ff) | 0xdc00; + } + } + return dest; +}