commit d9cfac8b388ff952dd52a908a074424dd0fcf1af
Author: Christophe Fergeau <t...@gnome.org>
Date:   Fri Dec 31 22:44:35 2010 +0100

    factor common code in hash functions
    
    All the hash functions need the idevice firewire guid as a 20-byte
    hex number. Introduce an internal itdb_device_get_hex_uuid function
    to factor this code which is common to the 3 hash implementations.

 src/itdb_device.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++----
 src/itdb_device.h |    1 +
 src/itdb_hash58.c |   50 ++++-----------------------------------------
 src/itdb_hash72.c |   54 +------------------------------------------------
 src/itdb_hashAB.c |    2 +-
 5 files changed, 61 insertions(+), 103 deletions(-)
---
diff --git a/src/itdb_device.c b/src/itdb_device.c
index f1aabad..81e7bb9 100644
--- a/src/itdb_device.c
+++ b/src/itdb_device.c
@@ -1847,6 +1847,58 @@ const char *itdb_device_get_firewire_id (const 
Itdb_Device *device)
     return g_hash_table_lookup (device->sysinfo, "FirewireGuid");
 }
 
+gchar *itdb_device_get_uuid(const Itdb_Device *device)
+{
+    return g_hash_table_lookup (device->sysinfo, "FirewireGuid");
+}
+
+static int ord_from_hex_char(const char c)
+{
+  if ('0' <= c && c <= '9')
+    return c - '0';
+  else if ('a' <= c && c <= 'f')
+    return 10 + (c - 'a');
+  else if ('A' <= c && c <= 'F')
+    return 10 + (c - 'A');
+  else
+    return -1;
+}
+
+static int
+itdb_hex_from_string(unsigned char *dest, const int array_size, const char *s)
+{
+  /* skip optional '0x' prefix */
+  if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
+    s += 2;
+
+  if (strlen(s) > array_size*2)
+    return -8;
+
+  do {
+    int low, high;
+    if((high = ord_from_hex_char(s[0])) == -1 ||
+       (low = ord_from_hex_char(s[1])) == -1)
+      return -9;
+    *dest++ = high<<4 | low;
+  } while(*(s+=2));
+  return 0;
+}
+
+gboolean itdb_device_get_hex_uuid (const Itdb_Device *device, unsigned char 
uuid[20])
+{
+    const char *uuid_str;
+    int result;
+
+    uuid_str = itdb_device_get_firewire_id (device);
+    if (uuid_str == NULL) {
+        return FALSE;
+    }
+    memset (uuid, 0, 20);
+    result = itdb_hex_from_string (uuid, 20, uuid_str);
+
+    return (result == 0);
+}
+
 ItdbChecksumType itdb_device_get_checksum_type (const Itdb_Device *device)
 {
 
@@ -2150,11 +2202,6 @@ itdb_device_supports_podcast (const Itdb_Device *device)
     }
 }
 
-gchar *itdb_device_get_uuid(const Itdb_Device *device)
-{
-    return g_hash_table_lookup (device->sysinfo, "FirewireGuid");
-}
-
 gboolean itdb_device_is_shuffle (const Itdb_Device *device)
 {
     const Itdb_IpodInfo *info;
diff --git a/src/itdb_device.h b/src/itdb_device.h
index 465d398..935dbd2 100644
--- a/src/itdb_device.h
+++ b/src/itdb_device.h
@@ -186,6 +186,7 @@ G_GNUC_INTERNAL gboolean itdb_device_is_iphone_family 
(const Itdb_Device *device
 G_GNUC_INTERNAL gboolean itdb_device_is_shuffle (const Itdb_Device *device);
 G_GNUC_INTERNAL ItdbChecksumType itdb_device_get_checksum_type (const 
Itdb_Device *device);
 G_GNUC_INTERNAL enum ItdbShadowDBVersion itdb_device_get_shadowdb_version 
(const Itdb_Device *device);
+G_GNUC_INTERNAL gboolean itdb_device_get_hex_uuid (const Itdb_Device *device, 
unsigned char uuid[20]);
 
 const Itdb_IpodInfo *
 itdb_ipod_info_from_serial (const char *serial);
diff --git a/src/itdb_hash58.c b/src/itdb_hash58.c
index 03b6cbb..4931f1f 100644
--- a/src/itdb_hash58.c
+++ b/src/itdb_hash58.c
@@ -117,38 +117,6 @@ static const unsigned char fixed[18] = {
     0x21, 0x07, 0xC1, 0xD0, 0x12, 0xB2, 0xA1, 0x07, 0x81
 };
 
-static int ord_from_hex_char(const char c)
-{
-  if ('0' <= c && c <= '9')
-    return c - '0';
-  else if ('a' <= c && c <= 'f')
-    return 10 + (c - 'a');
-  else if ('A' <= c && c <= 'F')
-    return 10 + (c - 'A');
-  else
-    return -1;
-}
-
-static int string_to_hex(unsigned char *dest, const int array_size,
-                        const char *s)
-{
-  /* skip optional '0x' prefix */
-  if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
-    s += 2;
-
-  if (strlen(s) > array_size*2)
-    return -8;
-
-  do {
-    int low, high;
-    if((high = ord_from_hex_char(s[0])) == -1 ||
-       (low = ord_from_hex_char(s[1])) == -1)
-      return -9;
-    *dest++ = high<<4 | low;
-  } while(*(s+=2));
-  return 0;
-}
-
 static int gcd(int a, int b){
     while (TRUE)
     {
@@ -169,20 +137,13 @@ static int lcm(int a, int b)
     return (a*b)/gcd(a,b);
 }
 
-static unsigned char *generate_key (const char *fwid)
+static unsigned char *generate_key (const unsigned char *firewire_id)
 {
     unsigned char *key;
     unsigned char y[16];
     GChecksum *checksum;
     gsize checksum_len;
     int i;
-    unsigned char firewire_id[20];
-    int result;
-
-    result = string_to_hex (firewire_id, sizeof (firewire_id), fwid);
-    if (result < 0) {
-       return NULL;
-    }
 
     /* take LCM of each two bytes in the FWID in turn */
     for (i=0; i<4; i++){
@@ -212,7 +173,7 @@ static unsigned char *generate_key (const char *fwid)
     return key;
 }
 
-static unsigned char *itdb_compute_hash (const char *firewire_id,
+static unsigned char *itdb_compute_hash (const unsigned char *firewire_id,
                                         const unsigned char *itdb,
                                         unsigned long size, 
                                         gsize *len)
@@ -268,7 +229,7 @@ gboolean itdb_hash58_write_hash (Itdb_Device *device,
                                 gsize itdb_len,
                                 GError **error)
 {
-    const char *fwid;
+    unsigned char firewire_id[20];
     guchar backup18[8];
     guchar backup32[20];
     unsigned char *checksum;
@@ -277,8 +238,7 @@ gboolean itdb_hash58_write_hash (Itdb_Device *device,
    
     g_assert (itdb_device_get_checksum_type (device) == ITDB_CHECKSUM_HASH58);
 
-    fwid = itdb_device_get_firewire_id (device);
-    if (fwid == NULL) {
+    if (!itdb_device_get_hex_uuid(device, firewire_id)) {
        g_set_error (error, 0, -1, "Couldn't find the iPod firewire ID");
        return FALSE;
     }
@@ -300,7 +260,7 @@ gboolean itdb_hash58_write_hash (Itdb_Device *device,
 
     header->hashing_scheme = GUINT16_FROM_LE (ITDB_CHECKSUM_HASH58);
 
-    checksum = itdb_compute_hash (fwid, itdb_data, itdb_len, &len);
+    checksum = itdb_compute_hash (firewire_id, itdb_data, itdb_len, &len);
     if (checksum == NULL) {
        g_set_error (error, 0, -1, "Failed to compute checksum");
        return FALSE;
diff --git a/src/itdb_hash72.c b/src/itdb_hash72.c
index f158e1e..e57c22f 100644
--- a/src/itdb_hash72.c
+++ b/src/itdb_hash72.c
@@ -100,56 +100,6 @@ static int hash_extract(const uint8_t signature[46],
        return 0;
 }
 
-static int ord_from_hex_char(const char c)
-{
-  if ('0' <= c && c <= '9')
-    return c - '0';
-  else if ('a' <= c && c <= 'f')
-    return 10 + (c - 'a');
-  else if ('A' <= c && c <= 'F')
-    return 10 + (c - 'A');
-  else
-    return -1;
-}
-
-static int string_to_hex(unsigned char *dest, const int array_size,
-                        const char *s)
-{
-  /* skip optional '0x' prefix */
-  if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
-    s += 2;
-
-  if (strlen(s) > array_size*2)
-    return -8;
-
-  do {
-    int low, high;
-    if((high = ord_from_hex_char(s[0])) == -1 ||
-       (low = ord_from_hex_char(s[1])) == -1)
-      return -9;
-    *dest++ = high<<4 | low;
-  } while(*(s+=2));
-  return 0;
-}
-
-static gboolean get_uuid (const Itdb_Device *device, unsigned char uuid[20])
-{
-    const char *uuid_str;
-    int result;
-
-    uuid_str = itdb_device_get_uuid (device);
-    if (uuid_str == NULL) {
-       uuid_str = itdb_device_get_firewire_id (device);
-    }
-    if (uuid_str == NULL) {
-       return FALSE;
-    }
-    memset (uuid, 0, 20);
-    result = string_to_hex (uuid, 20, uuid_str);
-
-    return (result == 0);
-}
-
 struct Hash78Info {
     unsigned char header[6];
     unsigned char uuid[20];
@@ -179,7 +129,7 @@ static gboolean write_hash_info (const Itdb_Device *device,
     const char header[] = "HASHv0";
 
     memcpy (hash_info.header, header, sizeof (header));
-    success = get_uuid (device, hash_info.uuid);
+    success = itdb_device_get_hex_uuid (device, hash_info.uuid);
     if (!success) {
        return FALSE;
     }
@@ -202,7 +152,7 @@ static struct Hash78Info *read_hash_info (const Itdb_Device 
*device)
     unsigned char uuid[20];
     struct Hash78Info *info;
 
-    if (!get_uuid (device, uuid)) {
+    if (!itdb_device_get_hex_uuid (device, uuid)) {
        return NULL;
     }
 
diff --git a/src/itdb_hashAB.c b/src/itdb_hashAB.c
index ce0f794..7b9f833 100644
--- a/src/itdb_hashAB.c
+++ b/src/itdb_hashAB.c
@@ -118,7 +118,7 @@ gboolean itdb_hashAB_compute_hash_for_sha1 (const 
Itdb_Device *device,
        }
     }
 
-    if (!get_uuid(device, uuid)) return FALSE;
+    if (!itdb_device_get_hex_uuid(device, uuid)) return FALSE;
 
     calc_hashAB(signature, sha1, uuid, rndpart);
 

------------------------------------------------------------------------------
Learn how Oracle Real Application Clusters (RAC) One Node allows customers
to consolidate database storage, standardize their database environment, and, 
should the need arise, upgrade to a full multi-node Oracle RAC database 
without downtime or disruption
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
gtkpod-cvs2 mailing list
gtkpod-cvs2@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gtkpod-cvs2

Reply via email to