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.
This commit is contained in:
Jacob Boerema 2024-05-26 14:46:11 -04:00
parent 3ad39c3866
commit 85561c7ff0
1 changed files with 47 additions and 1 deletions

View File

@ -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)