51b1c77
diff --git a/src/itdb_itunesdb.c b/src/itdb_itunesdb.c
51b1c77
index c41e758..3f352e1 100644
51b1c77
--- a/src/itdb_itunesdb.c
51b1c77
+++ b/src/itdb_itunesdb.c
51b1c77
@@ -1286,6 +1286,45 @@ static gint32 get_mhod_type (FContents *cts, glong seek, guint32 *ml)
51b1c77
     return type;
51b1c77
 }
51b1c77
 
51b1c77
+static char *extract_mhod_string (FContents *cts, glong seek)
51b1c77
+{
51b1c77
+    gunichar2 *entry_utf16;
51b1c77
+    char *entry_utf8;
51b1c77
+    gint string_type;
51b1c77
+    gsize len;
51b1c77
+
51b1c77
+    /* type of string: 0x02: UTF8, 0x01 or 0x00: UTF16 LE */
51b1c77
+    string_type = get32lint (cts, seek);
51b1c77
+    len = get32lint (cts, seek+4);   /* length of string */
51b1c77
+    g_return_val_if_fail (len < G_MAXUINT - 2, NULL);
51b1c77
+    if (string_type != 0x02) {
51b1c77
+	/* UTF-16 string */
51b1c77
+	entry_utf16 = g_new0 (gunichar2, (len+2)/2);
51b1c77
+	if (seek_get_n_bytes (cts, (gchar *)entry_utf16, seek+16, len)) {
51b1c77
+	    fixup_little_utf16 (entry_utf16);
51b1c77
+	    entry_utf8= g_utf16_to_utf8 (entry_utf16, -1, NULL, NULL, NULL);
51b1c77
+	    g_free (entry_utf16);
51b1c77
+	} else { 
51b1c77
+	    g_free (entry_utf16);
51b1c77
+	    return NULL;
51b1c77
+	}
51b1c77
+    } else {
51b1c77
+	/* UTF-8 string */
51b1c77
+	entry_utf8 = g_new0 (gchar, len+1);
51b1c77
+	if (!seek_get_n_bytes (cts, entry_utf8, seek+16, len)) {
51b1c77
+	    g_free (entry_utf8);
51b1c77
+	    return NULL;
51b1c77
+	}
51b1c77
+    }
51b1c77
+
51b1c77
+    if (g_utf8_validate (entry_utf8, -1, NULL)) {
51b1c77
+	return entry_utf8;
51b1c77
+    } else {
51b1c77
+	g_free (entry_utf8);
51b1c77
+	return NULL;
51b1c77
+    }
51b1c77
+}
51b1c77
+
51b1c77
 /* Returns the contents of the mhod at position @mhod_seek. This can
51b1c77
    be a simple string or something more complicated as in the case for
51b1c77
    Itdb_SPLPREF OR Itdb_SPLRULES.
51b1c77
@@ -1303,12 +1342,10 @@ static gint32 get_mhod_type (FContents *cts, glong seek, guint32 *ml)
51b1c77
 
51b1c77
 static MHODData get_mhod (FImport *fimp, glong mhod_seek, guint32 *ml)
51b1c77
 {
51b1c77
-  gunichar2 *entry_utf16 = NULL;
51b1c77
   MHODData result;
51b1c77
   gint32 xl;
51b1c77
   guint32 mhod_len;
51b1c77
   gint32 header_length;
51b1c77
-  guint32 string_type;
51b1c77
   gulong seek;
51b1c77
   FContents *cts;
51b1c77
   
51b1c77
@@ -1384,34 +1421,9 @@ static MHODData get_mhod (FImport *fimp, glong mhod_seek, guint32 *ml)
51b1c77
   case MHOD_ID_SORT_ALBUMARTIST:
51b1c77
   case MHOD_ID_SORT_COMPOSER:
51b1c77
   case MHOD_ID_SORT_TVSHOW:
51b1c77
-      /* type of string: 0x02: UTF8, 0x01 or 0x00: UTF16 LE */
51b1c77
-      string_type = get32lint (cts, seek);
51b1c77
-      xl = get32lint (cts, seek+4);   /* length of string */
51b1c77
-      g_return_val_if_fail (xl < G_MAXUINT - 2, result);
51b1c77
-      if (string_type != 0x02)
51b1c77
-      {
51b1c77
-	  entry_utf16 = g_new0 (gunichar2, (xl+2)/2);
51b1c77
-	  if (seek_get_n_bytes (cts, (gchar *)entry_utf16, seek+16, xl))
51b1c77
-	  {
51b1c77
-	      fixup_little_utf16 (entry_utf16);
51b1c77
-	      result.data.string = g_utf16_to_utf8 (entry_utf16, -1,
51b1c77
-						    NULL, NULL, NULL);
51b1c77
-	      g_free (entry_utf16);
51b1c77
-	  }
51b1c77
-	  else
51b1c77
-	  {   /* error */
51b1c77
-	      g_free (entry_utf16);
51b1c77
-	      return result;  /* *ml==-1, result.valid==FALSE */
51b1c77
-	  }
51b1c77
-      }
51b1c77
-      else
51b1c77
-      {
51b1c77
-	  result.data.string = g_new0 (gchar, xl+1);
51b1c77
-	  if (!seek_get_n_bytes (cts, result.data.string, seek+16, xl))
51b1c77
-	  {   /* error */
51b1c77
-	      g_free (entry_utf16);
51b1c77
-	      return result;  /* *ml==-1, result.valid==FALSE */
51b1c77
-	  }
51b1c77
+      result.data.string = extract_mhod_string (cts, seek);
51b1c77
+      if (result.data.string == NULL) {
51b1c77
+	  return result;
51b1c77
       }
51b1c77
       break;
51b1c77
   case MHOD_ID_PODCASTURL:
078984a
--- libgpod-0.7.2/src/itdb_itunesdb.c.old	2009-10-19 16:53:13.000000000 +0100
078984a
+++ libgpod-0.7.2/src/itdb_itunesdb.c	2009-10-19 16:53:57.000000000 +0100
078984a
@@ -1423,6 +1423,7 @@ static MHODData get_mhod (FImport *fimp,
078984a
   case MHOD_ID_SORT_TVSHOW:
078984a
       result.data.string = extract_mhod_string (cts, seek);
078984a
       if (result.data.string == NULL) {
078984a
+	  *ml = mhod_len;
078984a
 	  return result;
078984a
       }
078984a
       break;