From patchwork Tue Oct 6 05:44:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 9965 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 B9783C3B5D for ; Tue, 6 Oct 2020 05:45:08 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 40FCC63B27; Tue, 6 Oct 2020 07:45:08 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="OjO7sjqL"; dkim-atps=neutral Received: from mail.uajain.com (static.126.159.217.95.clients.your-server.de [95.217.159.126]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 8773A60358 for ; Tue, 6 Oct 2020 07:45:05 +0200 (CEST) From: Umang Jain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=uajain.com; s=mail; t=1601963104; bh=OGyM51V1KvPCJ9N+RUFBNlmSeJBTy26osGdrXT+YqxQ=; h=From:To:Cc:Subject; b=OjO7sjqL/Mnk3xkUbM6YbfreX50EsysWstwpcy4pU0PHqnAxU8GbcyWkTQAnvjQIc sYDW14v8KS/HWnHFItRjAyXYVpaMEm8xwUG3w26mE5ZuVOR32Q/WJWUf9khZRSyWIT GYTFutF7LrENydXj3fSJWm+yq07K5w8whkmVgJAGOpdSNEET2Kem3LdeQw8OJiKJ56 mWufSnRcgNJEXcHLnnU1ptZBZHbVdWDT8MYCWG8YGQO0R9eAiRQ3VN60bh0sB1lWHT FgkuQrbmyJ0seSRhJ5ulB6KXvZamoCYodHyHau5LYD28ScHkhYiipkuSzdq+uqQJHN 1df39WHDw3cFA== To: libcamera-devel@lists.libcamera.org Date: Tue, 6 Oct 2020 11:14:59 +0530 Message-Id: <20201006054459.19742-1-email@uajain.com> Mime-Version: 1.0 Subject: [libcamera-devel] [PATCH v3] android: jpeg: exif: Sanitize ASCII strings with utils::toAscii() 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" Use the newly introduced utils::toAscii() utility to remove all non-ASCII characters for EXIF_FORMAT_ASCII strings. Signed-off-by: Umang Jain Reviewed-by: Kieran Bingham Reviewed-by: Niklas Söderlund --- src/android/jpeg/exif.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) --- Changes in v3: - Carry out small optimizing suggested by Laurent. Changes in v2: - Set new length for sanitized string. --- diff --git a/src/android/jpeg/exif.cpp b/src/android/jpeg/exif.cpp index 3fd5d55..e4a116d 100644 --- a/src/android/jpeg/exif.cpp +++ b/src/android/jpeg/exif.cpp @@ -8,6 +8,7 @@ #include "exif.h" #include "libcamera/internal/log.h" +#include "libcamera/internal/utils.h" using namespace libcamera; @@ -171,15 +172,31 @@ void Exif::setRational(ExifIfd ifd, ExifTag tag, ExifRational item) void Exif::setString(ExifIfd ifd, ExifTag tag, ExifFormat format, const std::string &item) { - /* Pad 1 extra byte for null-terminated string in ASCII format. */ - size_t length = format == EXIF_FORMAT_ASCII ? - item.length() + 1 : item.length(); + std::string ascii; + size_t length; + const char *str; + + if (format == EXIF_FORMAT_ASCII) { + ascii = utils::toAscii(item); + str = ascii.c_str(); + + /* Pad 1 extra byte to null-terminate the ASCII string. */ + length = ascii.length() + 1; + } else { + str = item.c_str(); + + /* + * Strings stored in different formats (EXIF_FORMAT_UNDEFINED) + * are not null-terminated. + */ + length = item.length(); + } ExifEntry *entry = createEntry(ifd, tag, format, length, length); if (!entry) return; - memcpy(entry->data, item.c_str(), length); + memcpy(entry->data, str, length); exif_entry_unref(entry); }