From patchwork Thu Jan 14 10:40:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Elder X-Patchwork-Id: 10857 X-Patchwork-Delegate: paul.elder@ideasonboard.com Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 9D881BD808 for ; Thu, 14 Jan 2021 10:40:50 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 59B7A680E8; Thu, 14 Jan 2021 11:40:50 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="Ri8D12IZ"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 37C3D680E3 for ; Thu, 14 Jan 2021 11:40:49 +0100 (CET) Received: from pyrite.rasen.tech (unknown [IPv6:2400:4051:61:600:2c71:1b79:d06d:5032]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id BC85B2B3; Thu, 14 Jan 2021 11:40:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1610620848; bh=2rO6jGp2Y1nu6awNfsdYgxsRDJn8UjiYQY07RH1f5Zc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ri8D12IZDtonTqcr/fAKoRrK3K3MlQCJQxGh1+TDChAIOeU4uoNBE5UZsyrenNWf2 1af/fpmPGxnW9xCSY53oExDonIOZiR0wOtuhbiZ5c6itrFzOErhEcn4lo7emujoVUB DtnYsdptIFu6oe6HlyoCz4dQA6uEm0L9ImcjqCdM= From: Paul Elder To: libcamera-devel@lists.libcamera.org Date: Thu, 14 Jan 2021 19:40:30 +0900 Message-Id: <20210114104035.302968-2-paul.elder@ideasonboard.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210114104035.302968-1-paul.elder@ideasonboard.com> References: <20210114104035.302968-1-paul.elder@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 1/6] utils: Add function to convert string to UCS-2 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" GPSProcessingMethod and UserComment in EXIF tags can be in Unicode, but are recommended to be in UCS-2. Add a function in utils to help with this. Signed-off-by: Paul Elder --- include/libcamera/internal/utils.h | 2 ++ src/libcamera/utils.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/libcamera/internal/utils.h b/include/libcamera/internal/utils.h index f08134af..aa9cc236 100644 --- a/include/libcamera/internal/utils.h +++ b/include/libcamera/internal/utils.h @@ -35,6 +35,8 @@ const char *basename(const char *path); char *secure_getenv(const char *name); std::string dirname(const std::string &path); +std::vector string_to_c16(const std::string &str, bool le); + template std::vector map_keys(const T &map) { diff --git a/src/libcamera/utils.cpp b/src/libcamera/utils.cpp index e90375ae..89cb0f73 100644 --- a/src/libcamera/utils.cpp +++ b/src/libcamera/utils.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include /** @@ -122,6 +123,35 @@ std::string dirname(const std::string &path) return path.substr(0, pos + 1); } +/** + * \brief Convert string to byte array of UCS-2 + * \param[in] str String to convert + * \param[in] le Little-endian (false for Big-endian) + * + * \return Byte array of UCS-2 representation of \a str, without null-terminator + */ +std::vector string_to_c16(const std::string &str, bool le) +{ + std::mbstate_t state{}; + char16_t c16; + const char *ptr = &str[0], *end = &str[0] + str.size(); + + std::vector ret; + while (size_t rc = mbrtoc16(&c16, ptr, end - ptr + 1, &state)) { + if (rc == static_cast(-2) || + rc == static_cast(-1)) + break; + + ret.push_back(le ? (c16 & 0xff) : ((c16 >> 8) & 0xff)); + ret.push_back(le ? ((c16 >> 8) & 0xff) : (c16 & 0xff)); + + if (rc > 0) + ptr += rc; + } + + return ret; +} + /** * \fn std::vector map_keys(const T &map) * \brief Retrieve the keys of a std::map<>