Revision: 1712
          http://gtkpod.svn.sourceforge.net/gtkpod/?rev=1712&view=rev
Author:   jcsjcs
Date:     2007-10-01 08:52:05 -0700 (Mon, 01 Oct 2007)

Log Message:
-----------
        * src/itdb_itunesdb.c: integrate checksum writing into the
          creation of the iTunesDB instead of modifying the iTunesDB file
          in place (Christophe)

        * src/itdb_device.c: re-use artwork definition for iPod classic
          also for iPod Nanos 3G.

Modified Paths:
--------------
    libgpod/trunk/ChangeLog
    libgpod/trunk/src/itdb_device.c
    libgpod/trunk/src/itdb_itunesdb.c

Modified: libgpod/trunk/ChangeLog
===================================================================
--- libgpod/trunk/ChangeLog     2007-10-01 15:28:49 UTC (rev 1711)
+++ libgpod/trunk/ChangeLog     2007-10-01 15:52:05 UTC (rev 1712)
@@ -9,6 +9,13 @@
        * src/itdb_device.c: added artwork definition file (cover only)
          for iPod Nano Videos (3G). Thanks to Simon Schulz.
 
+       * src/itdb_itunesdb.c: integrate checksum writing into the
+         creation of the iTunesDB instead of modifying the iTunesDB file
+         in place (Christophe)
+
+       * src/itdb_device.c: re-use artwork definition for iPod classic
+         also for iPod Nanos 3G.
+
 2007-09-30  Christophe Fergeau  <[EMAIL PROTECTED]>
 
        * src/itdb_itunesdb.c: (itdb_write_checksum): remove debugging g_print

Modified: libgpod/trunk/src/itdb_device.c
===================================================================
--- libgpod/trunk/src/itdb_device.c     2007-10-01 15:28:49 UTC (rev 1711)
+++ libgpod/trunk/src/itdb_device.c     2007-10-01 15:52:05 UTC (rev 1712)
@@ -307,14 +307,6 @@
     {-1,                           -1,  -1,   -1, -1}
 };
 
-static const Itdb_ArtworkFormat ipod_nano3_artwork_info[] = {
-    {ITDB_THUMB_COVER_SMALL,      56,  55, 1061, THUMB_FORMAT_RGB565_LE, 
0x1810}, /*pad data to 0x1810 bytes*/
-    {ITDB_THUMB_COVER_LARGE,     320, 320, 1062, THUMB_FORMAT_RGB565_LE},
-    {ITDB_THUMB_COVER_MEDIUM,     128, 128, 1055, THUMB_FORMAT_RGB565_LE},
-    {-1,                           -1,  -1,   -1, -1}
-};
-
-
 static const Itdb_ArtworkFormat ipod_classic_1_artwork_info[] = {
     /* officially 55x55 -- verify! */
     {ITDB_THUMB_COVER_XSMALL,      56,  56, 1061, THUMB_FORMAT_RGB565_LE},
@@ -334,7 +326,10 @@
     {-1,                           -1,  -1,   -1, -1}
 };
 
+/* these seem to be identical... */
+#define ipod_nano3_artwork_info ipod_classic_1_artwork_info
 
+
 static void itdb_device_set_timezone_info (Itdb_Device *device);
 
 /* Reset or create the SysInfo hash table */

Modified: libgpod/trunk/src/itdb_itunesdb.c
===================================================================
--- libgpod/trunk/src/itdb_itunesdb.c   2007-10-01 15:28:49 UTC (rev 1711)
+++ libgpod/trunk/src/itdb_itunesdb.c   2007-10-01 15:52:05 UTC (rev 1712)
@@ -4861,6 +4861,49 @@
 }
 
 
+static gboolean write_db_checksum (FExport *fexp, GError **error)
+{
+    guint64 fwid;
+    guchar backup18[8];
+    guchar backup32[20];
+    unsigned char *itdb_data;
+    unsigned char *checksum;
+    gsize len;
+
+    fwid = itdb_device_get_firewire_id (fexp->itdb->device);
+    if (fwid == 0) {
+       g_set_error (error, 0, -1, "Couldn't find the iPod firewire ID");
+       return FALSE;
+    }
+
+    if (fexp->wcontents->pos < 0x6c) {
+       g_set_error (error, 0, -1, "iTunesDB file too small to write checksum");
+       return FALSE;
+    }
+    itdb_data = (unsigned char *)fexp->wcontents->contents;
+
+    memcpy (backup18, itdb_data+0x18, sizeof (backup18));
+    memcpy (backup32, itdb_data+0x32, sizeof (backup32));
+
+    /* Those fields must be zero'ed out for the sha1 calculation */
+    memset(itdb_data+0x18, 0, 8);
+    memset(itdb_data+0x32, 0, 20);
+    memset(itdb_data+0x58, 0, 20);
+
+    checksum = itdb_compute_hash (fwid, itdb_data, fexp->wcontents->pos, &len);
+    if (checksum == NULL) {
+       g_set_error (error, 0, -1, "Failed to compute checksum");
+       return FALSE;
+    }
+    memcpy (itdb_data+0x58, checksum, len);    
+    g_free (checksum);
+
+    memcpy (itdb_data+0x18, backup18, sizeof (backup18));
+    memcpy (itdb_data+0x32, backup32, sizeof (backup32));
+
+    return TRUE;
+}
+
 /**
  * itdb_write_file:
  * @itdb: the #Itdb_iTunesDB to save
@@ -4918,6 +4961,11 @@
            if (write_mhsd_playlists (fexp, 2))
            {
                fix_header (cts, mhbd_seek);
+
+               /* Set checksum (ipods require it starting from iPod Classic 
+                * and fat Nanos)
+                */
+               write_db_checksum (fexp, &fexp->error);
            }
        }
     }
@@ -4947,113 +4995,6 @@
     return result;
 }
 
-static unsigned char *
-calculate_db_checksum (const char *itdb_path, guint64 fwid, gsize *len)
-{
-    int fd;
-    struct stat stat_buf;
-    int result;
-    unsigned char *itdb_data;
-    unsigned char *checksum;
-
-    fd = open (itdb_path, O_RDONLY);
-    if (fd < 0) {
-       g_warning ("Couldn't open %s", itdb_path);
-       return NULL;
-    }
-
-    result = fstat (fd, &stat_buf);
-    if (result != 0) {
-       g_warning ("Couldn't stat %s", itdb_path);
-       close (fd);
-       return NULL;
-    }
-
-    if (stat_buf.st_size < 0x80) {
-       g_warning ("%s is too small", itdb_path);
-       close (fd);
-       return NULL;
-    }
-
-    itdb_data = mmap (NULL, stat_buf.st_size, 
-                     PROT_READ | PROT_WRITE, 
-                     MAP_PRIVATE, fd, 0);
-    if (itdb_data == MAP_FAILED) {
-       g_warning ("Couldn't mmap %s", itdb_path);
-       close (fd);
-       return NULL;
-    }
-
-    /* Those fields must be zero'ed out for the sha1 calculation */
-    memset(itdb_data+0x18, 0, 8);
-    memset(itdb_data+0x32, 0, 20);
-    memset(itdb_data+0x58, 0, 20);
-
-    checksum = itdb_compute_hash (fwid, itdb_data, stat_buf.st_size, len);
-
-    munmap (itdb_data, stat_buf.st_size);
-    close (fd);
-
-    return checksum;
-}
-
-static gboolean itdb_write_checksum_to_file (const char *path, 
-                                            const unsigned char *checksum,
-                                            size_t size)
-{
-    FILE *f;
-    int result;
-    size_t count;
-
-    f = fopen (path, "rb+");
-    if (f == NULL) {
-       return FALSE;
-    }
-    
-    result = fseek (f, 0x58, SEEK_SET);
-    if (result != 0) {
-       fclose (f);
-       return FALSE;
-    }
-    
-    count = fwrite (checksum, size, 1, f);
-    fclose (f);
-   
-    return (count == 1);
-}
-
-static gboolean itdb_write_checksum (Itdb_iTunesDB *db)
-{
-    guint64 fwid;
-    char *itdb_path;
-    unsigned char *checksum;
-    gsize len;
-    gboolean result;
-
-    if (db->device == NULL) {
-       return FALSE;
-    }
-
-    fwid = itdb_device_get_firewire_id (db->device);
-    if (fwid == 0) {
-       return FALSE;
-    }
-
-    itdb_path = itdb_get_itunesdb_path (itdb_get_mountpoint (db));
-    checksum = calculate_db_checksum (itdb_path, fwid, &len);
-
-    if (checksum == NULL) {
-       g_free (itdb_path);
-       return FALSE;
-    }
-
-    result = itdb_write_checksum_to_file (itdb_path, checksum, len);
-    g_free (itdb_path);
-    g_free (checksum);
-    
-    return result;
-}
-
 /**
  * itdb_write:
  * @itdb: the #Itdb_iTunesDB to write to disk
@@ -5101,14 +5042,6 @@
 
     if (result != FALSE)
     {
-       /* Set checksum (ipods require it starting from iPod Classic 
-        * and fat Nanos)
-        */
-       result = itdb_write_checksum (itdb);
-       if (!result) {
-           g_warning ("Couldn't set checksum");
-       }
-
        /* Write SysInfo file if it has changed */
        if (itdb->device->sysinfo_changed)
        {


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
gtkpod-cvs2 mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to