[totem-pl-parser] lib: Add support for AMZ AmazonMP3 download files



commit da7a2779d8b460841bb1f6519d2c1fdd7287324b
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Feb 20 01:30:59 2012 +0100

    lib: Add support for AMZ AmazonMP3 download files
    
    Using original code from libamz:
    http://nenolod.net/projects/libamz/
    
    https://bugzilla.gnome.org/show_bug.cgi?id=668686

 plparse/Makefile.am           |    4 +
 plparse/totem-pl-parser-amz.c |  155 +++++++++++++++++++++++++++++++++++++++++
 plparse/totem-pl-parser-amz.h |   45 ++++++++++++
 plparse/totem-pl-parser.c     |    2 +
 4 files changed, 206 insertions(+), 0 deletions(-)
---
diff --git a/plparse/Makefile.am b/plparse/Makefile.am
index 3520bd9..5ffbfb7 100644
--- a/plparse/Makefile.am
+++ b/plparse/Makefile.am
@@ -25,6 +25,7 @@ plparser_sources =				\
 	totem-pl-parser-builtins.c		\
 	totem-disc.c				\
 	totem-pl-parser.c			\
+	totem-pl-parser-amz.c			\
 	totem-pl-parser-lines.c			\
 	totem-pl-parser-media.c			\
 	totem-pl-parser-misc.c			\
@@ -58,6 +59,7 @@ dist_libtotem_plparser_la_SOURCES =		\
 	totem-pl-parser-xspf.h			\
 	totem-pl-playlist.c			\
 	totem-pl-playlist.h			\
+	totem-pl-parser-amz.h			\
 	xmlparser.h xmlparser.c			\
 	xmllexer.h xmllexer.c			\
 	bswap.h
@@ -114,6 +116,8 @@ libtotem_plparser_mini_la_SOURCES =		\
 	totem-pl-parser-wm.h			\
 	totem-pl-parser-xspf.c			\
 	totem-pl-parser-xspf.h			\
+	totem-pl-parser-amz.h			\
+	totem-pl-parser-amz.c			\
 	totem-pl-playlist.c			\
 	totem-pl-playlist.h
 
diff --git a/plparse/totem-pl-parser-amz.c b/plparse/totem-pl-parser-amz.c
new file mode 100644
index 0000000..4e2873d
--- /dev/null
+++ b/plparse/totem-pl-parser-amz.c
@@ -0,0 +1,155 @@
+/*
+   Copyright (C) 2010 Bastien Nocera <hadess hadess net>
+
+   Copyright (c) 2010 William Pitcock <nenolod dereferenced org>
+                 for amzfile_decrypt_blob().
+
+   The Gnome Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301  USA.
+
+   Author: Bastien Nocera <hadess hadess net>
+ */
+
+#include "config.h"
+
+#ifndef TOTEM_PL_PARSER_MINI
+#include <string.h>
+#include <glib.h>
+
+#include "totem-pl-parser-mini.h"
+#include "totem-pl-parser-amz.h"
+#include "totem-pl-parser-xspf.h"
+#include "totem-pl-parser-private.h"
+
+#ifdef HAVE_LIBGCRYPT
+#include <gcrypt.h>
+
+/*
+ * LOL.
+ *
+ * These guys use DES encryption which has been broken since the 1970s.  Keys
+ * cracked in 37 seconds on a Core i7 processor running at 2.7GHz.
+ */
+static const guchar amazon_key[8] = { 0x29, 0xAB, 0x9D, 0x18, 0xB2, 0x44, 0x9E, 0x31 };
+static const guchar amazon_iv[8]  = { 0x5E, 0x72, 0xD7, 0x9A, 0x11, 0xB3, 0x4F, 0xEE };
+
+/*
+ * decrypts the underlying XSPF playlist.
+ * does not *parse* the XSPF playlist.
+ */
+static gboolean
+amzfile_decrypt_blob(gchar *indata, gsize inlen, gchar **outdata)
+{
+	gcry_cipher_hd_t hd;
+	gcry_error_t err;
+	guchar *unpackdata, *decryptdata;
+	gsize unpacklen;
+	gint i;
+
+	unpackdata = g_base64_decode(indata, &unpacklen);
+	if (unpackdata == NULL)
+	{
+		g_print("g_base64_decode failed\n");
+		return FALSE;
+	}
+
+	if (unpacklen % 8)
+		unpacklen -= (unpacklen % 8);
+
+	decryptdata = g_malloc0(unpacklen + 1);
+
+	if ((err = gcry_cipher_open(&hd, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_CBC, 0)))
+	{
+		g_print("unable to initialise gcrypt: %s", gcry_strerror(err));
+		g_free(unpackdata);
+		g_free(decryptdata);
+		return FALSE;
+	}
+
+	if ((err = gcry_cipher_setkey(hd, amazon_key, 8)))
+	{
+		g_print("unable to set key for DES block cipher: %s", gcry_strerror(err));
+		gcry_cipher_close(hd);
+		g_free(unpackdata);
+		g_free(decryptdata);
+		return FALSE;
+	}
+
+	if ((err = gcry_cipher_setiv(hd, amazon_iv, 8)))
+	{
+		g_print("unable to set initialisation vector for DES block cipher: %s", gcry_strerror(err));
+		gcry_cipher_close(hd);
+		g_free(unpackdata);
+		g_free(decryptdata);
+		return FALSE;
+	}
+
+	if ((err = gcry_cipher_decrypt(hd, decryptdata, unpacklen, unpackdata, unpacklen)))
+	{
+		g_print("unable to decrypt embedded DES-encrypted XSPF document: %s", gcry_strerror(err));
+		gcry_cipher_close(hd);
+		g_free(unpackdata);
+		g_free(decryptdata);
+		return FALSE;
+	}
+
+	g_free(unpackdata);
+	gcry_cipher_close(hd);
+
+	/* remove padding from XSPF document */
+	for (i = unpacklen; i > 0; i--)
+	{
+		if (decryptdata[i - 1] == '\n' || decryptdata[i] == '\r' || decryptdata[i - 1] >= ' ')
+			break;
+	}
+	decryptdata[i] = 0;
+
+	*outdata = (char *) decryptdata;
+	return TRUE;
+}
+#endif /* HAVE_LIBGCRYPT */
+
+TotemPlParserResult
+totem_pl_parser_add_amz (TotemPlParser *parser,
+			 GFile *file,
+			 GFile *base_file,
+			 TotemPlParseData *parse_data,
+			 gpointer data)
+{
+#ifdef HAVE_LIBGCRYPT
+	char *b64data, *contents;
+	TotemPlParserResult ret;
+	gsize b64len;
+
+	if (g_file_load_contents (file, NULL, &b64data, &b64len, NULL, NULL) == FALSE)
+		return TOTEM_PL_PARSER_RESULT_ERROR;
+
+	if (amzfile_decrypt_blob (b64data, b64len, &contents) == FALSE) {
+		g_free (b64data);
+		return TOTEM_PL_PARSER_RESULT_ERROR;
+	}
+
+	ret = totem_pl_parser_add_xspf_with_contents (parser, file, base_file,
+						      contents, parse_data);
+
+	g_free (contents);
+
+	return ret;
+#else
+	return TOTEM_PL_PARSER_RESULT_UNHANDLED;
+#endif /* HAVE_LIBGCRYPT */
+}
+
+#endif /* !TOTEM_PL_PARSER_MINI */
diff --git a/plparse/totem-pl-parser-amz.h b/plparse/totem-pl-parser-amz.h
new file mode 100644
index 0000000..0675da8
--- /dev/null
+++ b/plparse/totem-pl-parser-amz.h
@@ -0,0 +1,45 @@
+/*
+   Copyright (C) 2010 Bastien Nocera <hadess hadess net>
+
+   The Gnome Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301  USA.
+
+   Author: Bastien Nocera <hadess hadess net>
+ */
+
+#ifndef TOTEM_PL_PARSER_AMZ_H
+#define TOTEM_PL_PARSER_AMZ_H
+
+G_BEGIN_DECLS
+
+#ifndef TOTEM_PL_PARSER_MINI
+#include "totem-pl-parser.h"
+#include "totem-pl-parser-private.h"
+#include <gio/gio.h>
+#endif /* !TOTEM_PL_PARSER_MINI */
+
+#ifndef TOTEM_PL_PARSER_MINI
+
+TotemPlParserResult totem_pl_parser_add_amz (TotemPlParser *parser,
+					     GFile *file,
+					     GFile *base_file,
+					     TotemPlParseData *parse_data,
+					     gpointer data);
+
+#endif /* !TOTEM_PL_PARSER_MINI */
+
+G_END_DECLS
+
+#endif /* TOTEM_PL_PARSER_AMZ_H */
diff --git a/plparse/totem-pl-parser.c b/plparse/totem-pl-parser.c
index dc4a2e0..abc93a8 100644
--- a/plparse/totem-pl-parser.c
+++ b/plparse/totem-pl-parser.c
@@ -154,6 +154,7 @@
 #include "totem-pl-parser-misc.h"
 #include "totem-pl-parser-private.h"
 #include "totem-pl-parser-videosite.h"
+#include "totem-pl-parser-amz.h"
 
 #define READ_CHUNK_SIZE 8192
 #define RECURSE_LEVEL_MAX 4
@@ -206,6 +207,7 @@ static PlaylistTypes special_types[] = {
 	PLAYLIST_TYPE ("application/atom+xml", totem_pl_parser_add_atom, NULL, FALSE),
 	PLAYLIST_TYPE ("application/rss+xml", totem_pl_parser_add_rss, totem_pl_parser_is_rss, FALSE),
 	PLAYLIST_TYPE ("text/x-opml+xml", totem_pl_parser_add_opml, NULL, FALSE),
+	PLAYLIST_TYPE ("audio/x-amzxml", totem_pl_parser_add_amz, NULL, FALSE),
 #ifndef TOTEM_PL_PARSER_MINI
 	PLAYLIST_TYPE ("application/x-desktop", totem_pl_parser_add_desktop, NULL, TRUE),
 	PLAYLIST_TYPE ("application/x-gnome-app-info", totem_pl_parser_add_desktop, NULL, TRUE),



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]