From 85561c7ff0bc4d285b36893a75559e61eb8c5e43 Mon Sep 17 00:00:00 2001 From: Jacob Boerema Date: Sun, 26 May 2024 14:46:11 -0400 Subject: [PATCH] libgimp: fix #6126 Invalid charset: InvalidCharsetId Some apps that write EXIF metadata, forgot to add the charset to certain tags that require it. The main case is Exif.Photo.UserComment. This caused us to show a warning about an invalid charset, in addition to not showing it in our Metdata Viewer. We fix this by reading the raw data for that tag when we encounter the above error. The raw data is then validated as utf-8 and converted to a string if valid. We then resave this tag to our metadata to force it to have the correct charset; that way we don't have to do any checking in other places in our code. Note: there are a few other tags that also use a charset. We may have to check those too, eventually. --- libgimp/gimpimagemetadata.c | 48 ++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/libgimp/gimpimagemetadata.c b/libgimp/gimpimagemetadata.c index b2f69b91b8..b4651076c6 100644 --- a/libgimp/gimpimagemetadata.c +++ b/libgimp/gimpimagemetadata.c @@ -140,7 +140,53 @@ gimp_image_metadata_load_finish (GimpImage *image, } else if (comment) { - comment = gimp_image_metadata_interpret_comment (comment); + if (g_str_has_prefix (comment, "charset=InvalidCharsetId ")) + { + GBytes *bytes = NULL; + + /* The Exif metadata writer forgot to add the charset. + * Read the raw data and assume it's UTF-8. */ + g_printerr ("Invalid charset for tag %s. Using raw data.\n", + "Exif.Photo.UserComment"); + + bytes = gexiv2_metadata_try_get_tag_raw (GEXIV2_METADATA (metadata), + "Exif.Photo.UserComment", + NULL); + if (bytes) + { + gsize size, strsize; + const gchar *data; + gchar *raw_comment; + + data = g_bytes_get_data (bytes, &size); + raw_comment = g_new (gchar, size + 1 ); + strsize = g_strlcpy (raw_comment, data, size + 1); + g_bytes_unref (bytes); + g_free (comment); + + if (raw_comment && strsize > 0 && + g_utf8_validate (raw_comment, size, NULL)) + { + comment = raw_comment; + } + else + { + g_free (raw_comment); + comment = NULL; + } + + /* Fix the tag in our metadata too, that way we don't have to + * check for this in other places. */ + gexiv2_metadata_try_set_tag_string (GEXIV2_METADATA (metadata), + "Exif.Photo.UserComment", + comment, + NULL); + } + } + else + { + comment = gimp_image_metadata_interpret_comment (comment); + } } if (! comment)