[libcamera-devel,v3] android: jpeg: exif: Sanitize ASCII strings with utils::toAscii()

Message ID 20201006054459.19742-1-email@uajain.com
State Accepted
Commit 6feebf297ead7b13495d01d9dffb5a09aab82af9
Headers show
Series
  • [libcamera-devel,v3] android: jpeg: exif: Sanitize ASCII strings with utils::toAscii()
Related show

Commit Message

Umang Jain Oct. 6, 2020, 5:44 a.m. UTC
Use the newly introduced utils::toAscii() utility to remove all
non-ASCII characters for EXIF_FORMAT_ASCII strings.

Signed-off-by: Umang Jain <email@uajain.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
---
 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.
---

Comments

Niklas Söderlund Oct. 7, 2020, 12:52 p.m. UTC | #1
Hi Umang,

Thanks for your work.

On 2020-10-06 11:14:59 +0530, Umang Jain wrote:
> Use the newly introduced utils::toAscii() utility to remove all
> non-ASCII characters for EXIF_FORMAT_ASCII strings.
> 
> Signed-off-by: Umang Jain <email@uajain.com>
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  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);
>  }
>  
> -- 
> 2.26.2
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

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);
 }