commit 9d7852f261413d756d5c4b72d98c8c85b3e8857f
Author: Christophe Fergeau <[email protected]>
Date: Sun Aug 23 13:40:15 2009 +0200
move compression code to its own file
src/Makefile.am | 1 +
src/itdb_itunesdb.c | 165 ++------------------------------------------
src/itdb_zlib.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++
src/itdb_zlib.h | 40 +++++++++++
4 files changed, 237 insertions(+), 158 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 9cfdbd4..5051224 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -18,6 +18,7 @@ libgpod_la_SOURCES = \
itdb_thumb.c \
itdb_track.c \
itdb_tzinfo.c \
+ itdb_zlib.c \
ithumb-writer.c \
pixmaps.c \
sha1.c
diff --git a/src/itdb_itunesdb.c b/src/itdb_itunesdb.c
index 25ef508..4fe6363 100644
--- a/src/itdb_itunesdb.c
+++ b/src/itdb_itunesdb.c
@@ -110,6 +110,7 @@
#include "db-artwork-parser.h"
#include "itdb_device.h"
#include "itdb_private.h"
+#include "itdb_zlib.h"
#include <errno.h>
#include <fcntl.h>
@@ -123,7 +124,6 @@
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
-#include <zlib.h>
#define ITUNESDB_DEBUG 0
#define ITUNESDB_MHIT_DEBUG 0
@@ -2799,74 +2799,12 @@ static gboolean parse_playlists (FImport *fimp, glong
mhsd_seek)
return TRUE;
}
-#define CHUNK 16384
-static int zlib_inflate(gchar *outbuf, gchar *zdata, gsize compressed_size,
gsize *uncompressed_size)
-{
- int ret;
- guint32 inpos = 0;
- guint32 outpos = 0;
- unsigned have;
- z_stream strm;
- unsigned char out[CHUNK];
-
- /* allocate inflate state */
- strm.zalloc = Z_NULL;
- strm.zfree = Z_NULL;
- strm.opaque = Z_NULL;
- strm.avail_in = 0;
- strm.next_in = Z_NULL;
- ret = inflateInit(&strm);
- if (ret != Z_OK)
- return ret;
-
- *uncompressed_size = 0;
-
- /* decompress until deflate stream ends or end of file */
- do {
- strm.avail_in = CHUNK;
- if (inpos+strm.avail_in > compressed_size) {
- strm.avail_in = compressed_size - inpos;
- }
- strm.next_in = (unsigned char*)zdata+inpos;
- inpos+=strm.avail_in;
-
- /* run inflate() on input until output buffer not full */
- do {
- strm.avail_out = CHUNK;
- if (outbuf) {
- strm.next_out = (unsigned char*)(outbuf + outpos);
- } else {
- strm.next_out = out;
- }
- ret = inflate(&strm, Z_NO_FLUSH);
- g_assert(ret != Z_STREAM_ERROR); /* state not clobbered */
- switch (ret) {
- case Z_NEED_DICT:
- ret = Z_DATA_ERROR; /* and fall through */
- case Z_DATA_ERROR:
- case Z_MEM_ERROR:
- (void)inflateEnd(&strm);
- return ret;
- }
- have = CHUNK - strm.avail_out;
- *uncompressed_size += have;
- if (outbuf) {
- outpos += have;
- }
- } while (strm.avail_out == 0);
-
- /* done when inflate() says it's done */
- } while (ret != Z_STREAM_END);
-
- /* clean up and return */
- (void)inflateEnd(&strm);
- return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
-}
-
-static gboolean check_decompress_fimp (FImport *fimp)
+static gboolean parse_fimp (FImport *fimp)
{
+ glong seek=0;
FContents *cts;
+ glong mhsd_1, mhsd_2, mhsd_3;
g_return_val_if_fail (fimp, FALSE);
g_return_val_if_fail (fimp->itdb, FALSE);
@@ -2875,6 +2813,7 @@ static gboolean check_decompress_fimp (FImport *fimp)
cts = fimp->fcontents;
+ /* Check if it looks like an iTunesDB */
if (!check_header_seek (cts, "mhbd", 0))
{
fcontents_set_reversed (cts, TRUE);
@@ -2893,53 +2832,7 @@ static gboolean check_decompress_fimp (FImport *fimp)
}
}
- /* only proceed with decompression if it's a compressed file */
- if ((*(guint32*)(cts->contents+12) == 2) && (*(guint32*)(cts->contents+16)
>= 0x28)) {
- /* get some values */
- guint32 headerSize = *(guint32*)(cts->contents+4);
- guint32 cSize = *(guint32*)(cts->contents+8);
- size_t uSize = 0;
-
- if (zlib_inflate(NULL, cts->contents+headerSize, cSize-headerSize,
&uSize) == 0) {
- gchar *new_contents;
- printf("allocating %"G_GSIZE_FORMAT"\n", uSize+headerSize);
- new_contents = (gchar*)g_malloc(uSize+headerSize);
- memcpy(new_contents, cts->contents, headerSize);
- printf("decompressing\n");
- if (zlib_inflate(new_contents+headerSize, cts->contents+headerSize,
cSize-headerSize, &uSize) == 0) {
- /* update FContents structure */
- g_free(cts->contents);
- cts->contents = new_contents;
- cts->length = uSize+headerSize;
- printf("uncompressed size: %"G_GSIZE_FORMAT"\n", cts->length);
- }
- } else {
- g_set_error (&fimp->error,
- ITDB_FILE_ERROR,
- ITDB_FILE_ERROR_CORRUPT,
- _("iTunesCDB '%s' could not be decompressed"),
- cts->filename);
- return FALSE;
- }
- }
-
- return TRUE;
-}
-
-static gboolean parse_fimp (FImport *fimp)
-{
- glong seek=0;
- FContents *cts;
- glong mhsd_1, mhsd_2, mhsd_3;
-
- g_return_val_if_fail (fimp, FALSE);
- g_return_val_if_fail (fimp->itdb, FALSE);
- g_return_val_if_fail (fimp->fcontents, FALSE);
- g_return_val_if_fail (fimp->fcontents->filename, FALSE);
-
- check_decompress_fimp(fimp);
-
- cts = fimp->fcontents;
+ itdb_zlib_check_decompress_fimp(fimp);
/* get the positions of the various mhsd */
/* type 1: track list */
@@ -5431,50 +5324,6 @@ static void prepare_itdb_for_write (FExport *fexp)
}
}
-static gboolean maybe_compress_itdb (FExport *fexp)
-{
- Itdb_iTunesDB *itdb;
- WContents *cts;
-
- itdb = fexp->itdb;
- cts = fexp->wcontents;
-
- if (itdb->version < 0x28) {
- guint32 header_len;
- uLongf compressed_len;
- guint32 uncompressed_len;
- gchar *new_contents;
- int status;
-
- printf("target DB needs compression\n");
-
- header_len = *(guint32*)(cts->contents+4);
- uncompressed_len = *(guint32*)(cts->contents+8) - header_len;
- compressed_len = compressBound (uncompressed_len);
-
- new_contents = g_malloc (header_len + compressed_len);
- memcpy (new_contents, cts->contents, header_len);
- status = compress2 ((guchar*)new_contents + header_len, &compressed_len,
- (guchar*)cts->contents + header_len,
uncompressed_len, 1);
- if (status != Z_OK) {
- g_free (new_contents);
- g_set_error (&fexp->error,
- ITDB_FILE_ERROR,
- ITDB_FILE_ERROR_ITDB_CORRUPT,
- _("Error compressing iTunesCDB file!\n"));
- return FALSE;
- }
-
- g_free(cts->contents);
- /* update mhbd size */
- *(guint32*)(new_contents+8) = compressed_len + header_len;
- cts->contents = new_contents;
- cts->pos = compressed_len + header_len;
- printf("compressed size: %ld\n", cts->pos);
- }
-
- return TRUE;
-}
/**
* itdb_write_file:
@@ -5538,7 +5387,7 @@ gboolean itdb_write_file (Itdb_iTunesDB *itdb, const
gchar *filename,
{
if (write_mhsd_albums (fexp)) {
fix_header (cts, mhbd_seek);
- if (maybe_compress_itdb (fexp)) {
+ if (itdb_zlib_check_compress_fexp (fexp)) {
/* Set checksum (ipods require it starting from
* iPod Classic and fat Nanos)
*/
diff --git a/src/itdb_zlib.c b/src/itdb_zlib.c
new file mode 100644
index 0000000..888ec10
--- /dev/null
+++ b/src/itdb_zlib.c
@@ -0,0 +1,189 @@
+/*
+| Copyright (C) 2009 Nikias Bassen <[email protected]>
+| Part of the gtkpod project.
+|
+| URL: http://www.gtkpod.org/
+| URL: http://gtkpod.sourceforge.net/
+|
+| The code contained in this file is free software; you can redistribute
+| it and/or modify it under the terms of the GNU Lesser General Public
+| License as published by the Free Software Foundation; either version
+| 2.1 of the License, or (at your option) any later version.
+|
+| This file 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
+| Lesser General Public License for more details.
+|
+| You should have received a copy of the GNU Lesser General Public
+| License along with this code; if not, write to the Free Software
+| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+|
+| iTunes and iPod are trademarks of Apple
+|
+| This product is not supported/written/published by Apple!
+|
+| $Id$
+*/
+#include <config.h>
+#include <string.h>
+#include <zlib.h>
+
+#include <glib/gi18n-lib.h>
+
+#include "itdb_zlib.h"
+#include "itdb_private.h"
+
+#define CHUNK 16384
+
+static int zlib_inflate(gchar *outbuf, gchar *zdata, gsize compressed_size,
gsize *uncompressed_size)
+{
+ int ret;
+ guint32 inpos = 0;
+ guint32 outpos = 0;
+ unsigned have;
+ z_stream strm;
+ unsigned char out[CHUNK];
+
+ /* allocate inflate state */
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.avail_in = 0;
+ strm.next_in = Z_NULL;
+ ret = inflateInit(&strm);
+ if (ret != Z_OK)
+ return ret;
+
+ *uncompressed_size = 0;
+
+ /* decompress until deflate stream ends or end of file */
+ do {
+ strm.avail_in = CHUNK;
+ if (inpos+strm.avail_in > compressed_size) {
+ strm.avail_in = compressed_size - inpos;
+ }
+ strm.next_in = (unsigned char*)zdata+inpos;
+ inpos+=strm.avail_in;
+
+ /* run inflate() on input until output buffer not full */
+ do {
+ strm.avail_out = CHUNK;
+ if (outbuf) {
+ strm.next_out = (unsigned char*)(outbuf + outpos);
+ } else {
+ strm.next_out = out;
+ }
+ ret = inflate(&strm, Z_NO_FLUSH);
+ g_assert(ret != Z_STREAM_ERROR); /* state not clobbered */
+ switch (ret) {
+ case Z_NEED_DICT:
+ ret = Z_DATA_ERROR; /* and fall through */
+ case Z_DATA_ERROR:
+ case Z_MEM_ERROR:
+ (void)inflateEnd(&strm);
+ return ret;
+ }
+ have = CHUNK - strm.avail_out;
+ *uncompressed_size += have;
+ if (outbuf) {
+ outpos += have;
+ }
+ } while (strm.avail_out == 0);
+
+ /* done when inflate() says it's done */
+ } while (ret != Z_STREAM_END);
+
+ /* clean up and return */
+ (void)inflateEnd(&strm);
+ return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
+}
+
+gboolean itdb_zlib_check_decompress_fimp (FImport *fimp)
+{
+ FContents *cts;
+
+ g_return_val_if_fail (fimp, FALSE);
+ g_return_val_if_fail (fimp->itdb, FALSE);
+ g_return_val_if_fail (fimp->fcontents, FALSE);
+ g_return_val_if_fail (fimp->fcontents->filename, FALSE);
+
+ cts = fimp->fcontents;
+
+ /* only proceed with decompression if it's a compressed file */
+ if ((*(guint32*)(cts->contents+12) == 2) && (*(guint32*)(cts->contents+16)
>= 0x28)) {
+ /* get some values */
+ guint32 headerSize = *(guint32*)(cts->contents+4);
+ guint32 cSize = *(guint32*)(cts->contents+8);
+ size_t uSize = 0;
+
+ if (zlib_inflate(NULL, cts->contents+headerSize, cSize-headerSize,
&uSize) == 0) {
+ gchar *new_contents;
+ g_print("allocating %"G_GSIZE_FORMAT"\n", uSize+headerSize);
+ new_contents = (gchar*)g_malloc(uSize+headerSize);
+ memcpy(new_contents, cts->contents, headerSize);
+ g_print("decompressing\n");
+ if (zlib_inflate(new_contents+headerSize, cts->contents+headerSize,
cSize-headerSize, &uSize) == 0) {
+ /* update FContents structure */
+ g_free(cts->contents);
+ cts->contents = new_contents;
+ cts->length = uSize+headerSize;
+ g_print("uncompressed size: %"G_GSIZE_FORMAT"\n", cts->length);
+ }
+ } else {
+ g_set_error (&fimp->error,
+ ITDB_FILE_ERROR,
+ ITDB_FILE_ERROR_CORRUPT,
+ _("iTunesCDB '%s' could not be decompressed"),
+ cts->filename);
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
+gboolean itdb_zlib_check_compress_fexp (FExport *fexp)
+{
+ Itdb_iTunesDB *itdb;
+ WContents *cts;
+
+ itdb = fexp->itdb;
+ cts = fexp->wcontents;
+
+ if (itdb->version < 0x28) {
+ guint32 header_len;
+ uLongf compressed_len;
+ guint32 uncompressed_len;
+ gchar *new_contents;
+ int status;
+
+ g_print("target DB needs compression\n");
+
+ header_len = *(guint32*)(cts->contents+4);
+ uncompressed_len = *(guint32*)(cts->contents+8) - header_len;
+ compressed_len = compressBound (uncompressed_len);
+
+ new_contents = g_malloc (header_len + compressed_len);
+ memcpy (new_contents, cts->contents, header_len);
+ status = compress2 ((guchar*)new_contents + header_len, &compressed_len,
+ (guchar*)cts->contents + header_len,
uncompressed_len, 1);
+ if (status != Z_OK) {
+ g_free (new_contents);
+ g_set_error (&fexp->error,
+ ITDB_FILE_ERROR,
+ ITDB_FILE_ERROR_ITDB_CORRUPT,
+ _("Error compressing iTunesCDB file!\n"));
+ return FALSE;
+ }
+
+ g_free(cts->contents);
+ /* update mhbd size */
+ *(guint32*)(new_contents+8) = compressed_len + header_len;
+ cts->contents = new_contents;
+ cts->pos = compressed_len + header_len;
+ g_print("compressed size: %ld\n", cts->pos);
+ }
+
+ return TRUE;
+}
diff --git a/src/itdb_zlib.h b/src/itdb_zlib.h
new file mode 100644
index 0000000..e716d92
--- /dev/null
+++ b/src/itdb_zlib.h
@@ -0,0 +1,40 @@
+/*
+| Copyright (C) 2009 Christophe Fergeau <[email protected]>
+| Part of the gtkpod project.
+|
+| URL: http://www.gtkpod.org/
+| URL: http://gtkpod.sourceforge.net/
+|
+| The code contained in this file is free software; you can redistribute
+| it and/or modify it under the terms of the GNU Lesser General Public
+| License as published by the Free Software Foundation; either version
+| 2.1 of the License, or (at your option) any later version.
+|
+| This file 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
+| Lesser General Public License for more details.
+|
+| You should have received a copy of the GNU Lesser General Public
+| License along with this code; if not, write to the Free Software
+| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+|
+| iTunes and iPod are trademarks of Apple
+|
+| This product is not supported/written/published by Apple!
+|
+| $Id$
+*/
+#ifndef __ITDB_ZLIB_H__
+#define __ITDB_ZLIB_H__
+
+G_BEGIN_DECLS
+#include <glib.h>
+#include <itdb_private.h>
+
+G_GNUC_INTERNAL gboolean itdb_zlib_check_decompress_fimp (FImport *fimp);
+G_GNUC_INTERNAL gboolean itdb_zlib_check_compress_fexp (FExport *fexp);
+
+G_END_DECLS
+
+#endif
------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2