changeset 245:1d8012046612 trunk

[svn] Convert UTF-16 (windows-1252) into something useful. Original patch by incomp@#audacious, severe cleanups by myself. :)
author nenolod
date Wed, 30 Nov 2005 14:56:57 -0800
parents 38892498a565
children acae8fa925c3
files audacious/util.c
diffstat 1 files changed, 29 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/audacious/util.c	Wed Nov 30 00:02:54 2005 -0800
+++ b/audacious/util.c	Wed Nov 30 14:56:57 2005 -0800
@@ -19,6 +19,8 @@
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
+#define WEIRD_UTF_16_PLAYLIST_ENCODING
+
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
 #endif
@@ -394,6 +396,9 @@
     gchar *buffer, *ret_buffer = NULL;
     gint found_section = 0, off = 0, len = 0;
     gsize filesize;
+    gchar *outbuf;
+    unsigned char x[] = { 0xff, 0xfe, 0x00 };
+    int counter;
 
     if (!filename)
         return NULL;
@@ -401,6 +406,30 @@
     if (!g_file_get_contents(filename, &buffer, &filesize, NULL))
         return NULL;
 
+    /*
+     * Convert UTF-16 into something useful. Original implementation
+     * by incomp@#audacious. Cleanups \nenolod
+     */
+    if (!memcmp(&buffer[0],&x,2)) {
+        outbuf = g_malloc (filesize);	/* it's safe to waste memory. */
+
+        for (counter = 2; counter < filesize; counter += 2)
+            if (!memcmp(&buffer[counter+1], &x[2], 1))
+                outbuf[(counter-2)/2] = buffer[counter];
+            else
+		return NULL;
+
+        outbuf[(counter-2)/2] = '\0';
+
+        if ((filesize - 2) / 2 == (counter - 2) / 2) {
+            g_free(buffer);
+            buffer = outbuf;
+        } else {
+            g_free(outbuf);
+	    return NULL;	/* XXX wrong encoding */
+        }
+    }
+
     while (!ret_buffer && off < filesize) {
         while (off < filesize &&
                (buffer[off] == '\r' || buffer[off] == '\n' ||