From 1b885bafbad13ad13ca234a66c057188eaab5322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim-Philipp=20M=C3=BCller?= Date: Fri, 28 Apr 2006 11:37:22 +0000 Subject: [PATCH] tag: id3v2: Recognise and skip any byte order marker (BOM) in Original commit message from CVS: * gst-libs/gst/tag/id3v2frames.c: (has_utf16_bom), (parse_split_strings): Recognise and skip any byte order marker (BOM) in UTF-16 strings. --- gst-libs/gst/tag/id3v2frames.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/gst-libs/gst/tag/id3v2frames.c b/gst-libs/gst/tag/id3v2frames.c index cf56f8bd17..b97e4d0184 100644 --- a/gst-libs/gst/tag/id3v2frames.c +++ b/gst-libs/gst/tag/id3v2frames.c @@ -668,6 +668,24 @@ parse_insert_string_field (const gchar * encoding, gchar * data, gint data_size, g_array_append_val (fields, field); } +static gboolean +has_utf16_bom (gchar * data, const gchar ** p_in_encoding) +{ + guint16 marker = (GST_READ_UINT8 (data) << 8) | GST_READ_UINT8 (data + 1); + + switch (marker) { + case 0xFFFE: + *p_in_encoding = "UTF16LE"; + return TRUE; + case 0xFEFF: + *p_in_encoding = "UTF16BE"; + return TRUE; + default: + break; + } + return FALSE; +} + static void parse_split_strings (guint8 encoding, gchar * data, gint data_size, GArray ** out_fields) @@ -719,6 +737,9 @@ parse_split_strings (guint8 encoding, gchar * data, gint data_size, /* Find '\0\0' terminator */ for (text_pos = 0; text_pos < data_size - 1; text_pos += 2) { if (data[text_pos] == '\0' && data[text_pos + 1] == '\0') { + if (has_utf16_bom (data + prev, &in_encode)) { + prev += 2; /* skip BOM */ + } /* found a delimiter */ parse_insert_string_field (in_encode, data + prev, text_pos - prev + 2, fields); @@ -729,6 +750,9 @@ parse_split_strings (guint8 encoding, gchar * data, gint data_size, } if (data_size - prev > 1 && (data[prev] != 0x00 || data[prev + 1] != 0x00)) { + if (has_utf16_bom (data + prev, &in_encode)) { + prev += 2; /* skip BOM */ + } /* There were 2 or more non-null chars left, convert those too */ parse_insert_string_field (in_encode, data + prev, data_size - prev, fields);