~ubuntu-branches/ubuntu/hardy/gtkpod-aac/hardy

« back to all changes in this revision

Viewing changes to src/md5.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-07-17 18:25:25 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070717182525-rhl5w4pk8lbk6pna
Tags: 0.99.10-2ubuntu1
* Resynchronise with gtkpod 0.9.10-2.
* Hack in dpatch support, since it was removed.
* Rename debian/patches/03-configure.dpatch to
  debian/patches/aac-configure.dpatch.
* Update debian/gtkpod-aac.diff.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Time-stamp: <2006-05-17 22:10:35 jcs>
2
 
|
3
 
|  Copyright (C) 2002 Corey Donohoe <atmos at atmos.org>
4
 
|  Copyright (C) 2002-2005 Jorg Schuler <jcsjcs at users sourceforge net>
5
 
|  Part of the gtkpod project.
6
 
7
 
|  URL: http://www.gtkpod.org/
8
 
|  URL: http://gtkpod.sourceforge.net/
9
 
|
10
 
|  SHA1 routine: David Puckett <niekze at yahoo.com>
11
 
|  SHA1 implemented from FIPS-160 standard
12
 
|  <http://www.itl.nist.gov/fipspubs/fip180-1.htm>
13
 
|
14
 
|  This program is free software; you can redistribute it and/or modify
15
 
|  it under the terms of the GNU General Public License as published by
16
 
|  the Free Software Foundation; either version 2 of the License, or
17
 
|  (at your option) any later version.
18
 
|
19
 
|  This program is distributed in the hope that it will be useful,
20
 
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 
|  GNU General Public License for more details.
23
 
|
24
 
|  You should have received a copy of the GNU General Public License
25
 
|  along with this program; if not, write to the Free Software
26
 
|  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
 
|
28
 
|  iTunes and iPod are trademarks of Apple
29
 
|
30
 
|  This product is not supported/written/published by Apple!
31
 
|
32
 
|  $Id: md5.c,v 1.44 2006/06/25 23:29:08 freethinkerjim Exp $
33
 
*/
34
 
 
35
 
#include <errno.h>
36
 
#include <limits.h>
37
 
#include <stdio.h>
38
 
#include <string.h>
39
 
#include <sys/stat.h>
40
 
#include <sys/types.h>
41
 
#include <unistd.h>
42
 
#include <glib.h>
43
 
#include "charset.h"
44
 
#include "md5.h"
45
 
#include "file.h"
46
 
#include "prefs.h"
47
 
#include "misc.h"
48
 
#include "misc_track.h"
49
 
#include "display.h"
50
 
#include "display_itdb.h"
51
 
 
52
 
 
53
 
typedef guint32 chunk;
54
 
union _block
55
 
{
56
 
   guint8 charblock[64];
57
 
   chunk chunkblock[16];
58
 
};
59
 
typedef union _block block;
60
 
 
61
 
union _hblock
62
 
{
63
 
   guint8 charblock[20];
64
 
   chunk chunkblock[5];
65
 
};
66
 
typedef union _hblock hblock;
67
 
 
68
 
struct _sha1
69
 
{
70
 
   block *blockdata;
71
 
   hblock *H;
72
 
};
73
 
typedef struct _sha1 sha1;
74
 
 
75
 
static guint8 *sha1_hash(const guint8 * text, guint32 len);
76
 
static void process_block_sha1(sha1 * message);
77
 
 
78
 
#if BYTE_ORDER == LITTLE_ENDIAN
79
 
static void little_endian(hblock * stupidblock, int blocks);
80
 
#endif
81
 
 
82
 
/**
83
 
 * Create and manage a string hash for files on disk
84
 
 */
85
 
 
86
 
/**
87
 
 * NR_PATH_MAX_BLOCKS
88
 
 * A seed of sorts for SHA1, if collisions occur increasing this value
89
 
 * should give more unique data to SHA1 as more of the file is read
90
 
 * This value is multiplied by PATH_MAX_MD5 to determine how many bytes are read
91
 
 */
92
 
#define NR_PATH_MAX_BLOCKS 4
93
 
#define PATH_MAX_MD5 4096
94
 
 
95
 
/* Set up or destory the md5 hash table */
96
 
void setup_md5()
97
 
{
98
 
    struct itdbs_head *itdbs_head;
99
 
 
100
 
    g_return_if_fail (gtkpod_window);
101
 
    itdbs_head = g_object_get_data (G_OBJECT (gtkpod_window),
102
 
                                    "itdbs_head");
103
 
 
104
 
    /* gets called before itdbs are set up -> fail silently */
105
 
    if (itdbs_head)
106
 
    {
107
 
        if (prefs_get_int("md5"))  /* MD5 hashing turned on */
108
 
        {
109
 
            gp_md5_hash_tracks();
110
 
        
111
 
            /* Display duplicates */
112
 
            gp_duplicate_remove(NULL, NULL);
113
 
        }
114
 
        else
115
 
            gp_md5_free_hash();
116
 
    }
117
 
}
118
 
 
119
 
/**
120
 
 * get_filesize_for_file_descriptor - get the filesize on disk for the given
121
 
 * file descriptor
122
 
 * @fp - the filepointer we want the filesize for
123
 
 * Returns - the filesize in bytes
124
 
 */
125
 
static guint32
126
 
get_filesize_for_file_descriptor(FILE *fp)
127
 
{
128
 
    off_t result = 0;
129
 
    struct stat stat_info;
130
 
    int file_no = fileno(fp);
131
 
 
132
 
    if((fstat(file_no, &stat_info) == 0))       /* returns 0 on success */
133
 
        result = (int)stat_info.st_size;
134
 
    return (guint32)result;
135
 
}
136
 
 
137
 
/**
138
 
 * md5_hash_on_file - read PATH_MAX_MD5 * NR_PATH_MAX_BLOCKS bytes
139
 
 * from the file and ask sha1 for a hash of it, convert this hash to a
140
 
 * string of hex output @fp - an open file descriptor to read from
141
 
 * Returns - A Hash String - you handle memory returned
142
 
 */
143
 
static gchar *
144
 
md5_hash_on_file(FILE * fp)
145
 
{
146
 
   gchar *result = NULL;
147
 
 
148
 
   if (fp)
149
 
   {
150
 
       int fsize = 0;
151
 
       int chunk_size = PATH_MAX_MD5 * NR_PATH_MAX_BLOCKS;
152
 
 
153
 
       fsize = get_filesize_for_file_descriptor(fp);
154
 
       if(fsize < chunk_size)
155
 
           chunk_size = fsize;
156
 
 
157
 
       if(fsize > 0)
158
 
       {
159
 
           guint32 fsize_normal;
160
 
           guint8 *hash = NULL;
161
 
           int bread = 0, x = 0, last = 0;
162
 
           guchar file_chunk[chunk_size + sizeof(int)];
163
 
 
164
 
           /* allocate the digest we're returning */
165
 
           result = g_malloc0(sizeof(gchar) * 41);
166
 
 
167
 
           /* put filesize in the first 32 bits */
168
 
           fsize_normal = GINT32_TO_LE (fsize);
169
 
           memcpy(file_chunk, &fsize_normal, sizeof(guint32));
170
 
 
171
 
           /* read chunk_size from fp */
172
 
           bread = fread(&file_chunk[sizeof(int)], sizeof(gchar),
173
 
                            chunk_size, fp);
174
 
 
175
 
           /* create hash from our data */
176
 
           hash = sha1_hash(file_chunk, (bread + sizeof(int)));
177
 
 
178
 
           /* put it in a format we like */
179
 
           for (x = 0; x < 20; x++)
180
 
               last += snprintf(&result[last], 4, "%02x", hash[x]);
181
 
 
182
 
           /* free the hash value sha1_hash gave us */
183
 
           g_free(hash);
184
 
       }
185
 
       else
186
 
       {
187
 
          gtkpod_warning(_("Hashed file is 0 bytes long\n"));
188
 
       }
189
 
   }
190
 
   return (result);
191
 
}
192
 
 
193
 
/**
194
 
 * Generate a unique hash for the Track passed in
195
 
 * @s - The Track data structure, we want to hash based on the file on disk
196
 
 * Returns - an SHA1 hash in string format, is the hex output from the hash
197
 
 */
198
 
static gchar *
199
 
md5_hash_track(Track * s)
200
 
{
201
 
   ExtraTrackData *etr;
202
 
   gchar *result = NULL;
203
 
   gchar *filename;
204
 
 
205
 
   g_return_val_if_fail (s, NULL);
206
 
   etr = s->userdata;
207
 
   g_return_val_if_fail (etr, NULL);
208
 
 
209
 
   if (etr->md5_hash != NULL)
210
 
   {
211
 
       result = g_strdup(etr->md5_hash);
212
 
   }
213
 
   else
214
 
   {
215
 
       filename = get_file_name_from_source (s, SOURCE_PREFER_LOCAL);
216
 
       if (filename)
217
 
       {
218
 
           result = md5_hash_on_filename (filename, FALSE);
219
 
           g_free(filename);
220
 
       }
221
 
   }
222
 
   return (result);
223
 
}
224
 
 
225
 
 
226
 
/* @silent: don't print any warning */
227
 
gchar *md5_hash_on_filename (gchar *name, gboolean silent)
228
 
{
229
 
    gchar *result = NULL;
230
 
 
231
 
    if (name)
232
 
    {
233
 
        FILE *fpit = fopen (name, "r");
234
 
        if (!fpit)
235
 
        {
236
 
            if (!silent)
237
 
            {
238
 
                gchar *name_utf8=charset_to_utf8 (name);
239
 
                gtkpod_warning (
240
 
                    _("Could not open '%s' to calculate MD5 checksum: %s\n"),
241
 
                    name_utf8, strerror(errno));
242
 
                g_free (name_utf8);
243
 
            }
244
 
        }
245
 
        else
246
 
        {
247
 
            result = md5_hash_on_file (fpit);
248
 
            fclose (fpit);
249
 
        }
250
 
    }
251
 
    return result;
252
 
}
253
 
 
254
 
 
255
 
/**
256
 
 * Free up the dynamically allocated memory in @itdb's hash table
257
 
 */
258
 
void md5_free_eitdb (ExtraiTunesDBData *eitdb)
259
 
{
260
 
    g_return_if_fail (eitdb);
261
 
 
262
 
    if (eitdb->md5hash)
263
 
    {
264
 
        g_hash_table_destroy (eitdb->md5hash);
265
 
        eitdb->md5hash = NULL;
266
 
    }
267
 
}
268
 
 
269
 
/**
270
 
 * Free up the dynamically allocated memory in @itdb's hash table
271
 
 */
272
 
void md5_free (iTunesDB *itdb)
273
 
{
274
 
    g_return_if_fail (itdb);
275
 
    g_return_if_fail (itdb->userdata);
276
 
 
277
 
    md5_free_eitdb (itdb->userdata);
278
 
}
279
 
 
280
 
/**
281
 
 * Check to see if a track has already been added to the ipod
282
 
 * @s - the Track we want to know about. If the track does not exist, it
283
 
 * is inserted into the hash.
284
 
 * Returns a pointer to the duplicate track.
285
 
 */
286
 
Track *md5_track_exists_insert (iTunesDB *itdb, Track * s)
287
 
{
288
 
    ExtraiTunesDBData *eitdb;
289
 
    ExtraTrackData *etr;
290
 
    gchar *val = NULL;
291
 
    Track *track = NULL;
292
 
 
293
 
    g_return_val_if_fail (itdb, NULL);
294
 
    eitdb = itdb->userdata;
295
 
    g_return_val_if_fail (eitdb, NULL);
296
 
 
297
 
    g_return_val_if_fail (s, NULL);
298
 
    etr = s->userdata;
299
 
    g_return_val_if_fail (etr, NULL);
300
 
 
301
 
    if (prefs_get_int("md5"))
302
 
    {
303
 
        if (eitdb->md5hash == NULL)
304
 
        {
305
 
            eitdb->md5hash = g_hash_table_new_full(g_str_hash,
306
 
                                                   g_str_equal,
307
 
                                                   g_free, NULL);
308
 
        }
309
 
        val = md5_hash_track (s);
310
 
        if (val != NULL)
311
 
        {
312
 
            track = g_hash_table_lookup (eitdb->md5hash, val);
313
 
            if (track)
314
 
            {
315
 
                g_free(val);
316
 
            }
317
 
            else
318
 
            {   /* if it doesn't exist we register it in the hash */
319
 
                g_free (etr->md5_hash);
320
 
                etr->md5_hash = g_strdup (val);
321
 
                /* val is used in the next line -- we don't have to
322
 
                 * g_free() it */
323
 
                g_hash_table_insert (eitdb->md5hash, val, s);
324
 
            }
325
 
        }
326
 
    }
327
 
    return track;
328
 
}
329
 
 
330
 
/**
331
 
 * Check to see if a track has already been added to the ipod
332
 
 * @s - the Track we want to know about.
333
 
 * Returns a pointer to the duplicate track.
334
 
 */
335
 
Track *md5_track_exists (iTunesDB *itdb, Track *s)
336
 
{
337
 
    ExtraiTunesDBData *eitdb;
338
 
    Track *track = NULL;
339
 
 
340
 
    g_return_val_if_fail (itdb, NULL);
341
 
    eitdb = itdb->userdata;
342
 
    g_return_val_if_fail (eitdb, NULL);
343
 
 
344
 
    if (prefs_get_int("md5") && eitdb->md5hash)
345
 
    {
346
 
        gchar *val = md5_hash_track (s);
347
 
        if (val)
348
 
        {
349
 
            track = g_hash_table_lookup (eitdb->md5hash, val);
350
 
            g_free (val);
351
 
        }
352
 
    }
353
 
    return track;
354
 
}
355
 
 
356
 
/**
357
 
 * Check to see if a track has already been added to the ipod
358
 
 * @file - the Track we want to know about.
359
 
 * Returns a pointer to the duplicate track using md5 for
360
 
 * identification. If md5 checksums are off, NULL is returned.
361
 
 */
362
 
Track *md5_file_exists (iTunesDB *itdb, gchar *file, gboolean silent)
363
 
{
364
 
    ExtraiTunesDBData *eitdb;
365
 
    Track *track = NULL;
366
 
 
367
 
    g_return_val_if_fail (file, NULL);
368
 
    g_return_val_if_fail (itdb, NULL);
369
 
    eitdb = itdb->userdata;
370
 
    g_return_val_if_fail (eitdb, NULL);
371
 
 
372
 
    if (prefs_get_int("md5") && eitdb->md5hash)
373
 
    {
374
 
        gchar *val = md5_hash_on_filename (file, silent);
375
 
        if (val)
376
 
        {
377
 
            track = g_hash_table_lookup (eitdb->md5hash, val);
378
 
            g_free (val);
379
 
        }
380
 
    }
381
 
    return track;
382
 
}
383
 
 
384
 
 
385
 
/**
386
 
 * Check to see if a track has already been added to the ipod
387
 
 * @md5 - the md5 we want to know about.
388
 
 * Returns a pointer to the duplicate track using md5 for
389
 
 * identification. If md5 checksums are off, NULL is returned.
390
 
 */
391
 
Track *md5_md5_exists (iTunesDB *itdb, gchar *md5)
392
 
{
393
 
    ExtraiTunesDBData *eitdb;
394
 
    Track *track = NULL;
395
 
 
396
 
    g_return_val_if_fail (md5, NULL);
397
 
    g_return_val_if_fail (itdb, NULL);
398
 
    eitdb = itdb->userdata;
399
 
    g_return_val_if_fail (eitdb, NULL);
400
 
 
401
 
    if (prefs_get_int("md5") && eitdb->md5hash)
402
 
    {
403
 
        track = g_hash_table_lookup (eitdb->md5hash, md5);
404
 
    }
405
 
    return track;
406
 
}
407
 
 
408
 
/**
409
 
 * Free the specified track from the ipod's unique file hash
410
 
 * @s - The Track that's being freed from the ipod
411
 
 */
412
 
void md5_track_remove (Track *s)
413
 
{
414
 
    ExtraiTunesDBData *eitdb;
415
 
 
416
 
    g_return_if_fail (s);
417
 
    g_return_if_fail (s->itdb);
418
 
    eitdb = s->itdb->userdata;
419
 
    g_return_if_fail (eitdb);
420
 
 
421
 
    if (prefs_get_int("md5") && eitdb->md5hash)
422
 
    {
423
 
        gchar *val = md5_hash_track (s);
424
 
        if (val)
425
 
        {
426
 
            Track *track = g_hash_table_lookup (eitdb->md5hash, val);
427
 
            if (track)
428
 
            {
429
 
                if (track == s) /* only remove if it's the same track */
430
 
                    g_hash_table_remove (eitdb->md5hash, val);
431
 
            }
432
 
            g_free(val);
433
 
        }
434
 
    }
435
 
}
436
 
 
437
 
/* sha1_hash - hash value the input data with a given size.
438
 
 * @text - the data we're reading to seed sha1
439
 
 * @len - the length of the data for our seed
440
 
 * Returns a unique 20 char array.
441
 
 */
442
 
static guint8 *
443
 
sha1_hash(const guint8 * text, guint32 len)
444
 
{
445
 
   chunk x;
446
 
   chunk temp_len = len;
447
 
   const guint8 *temp_text = text;
448
 
   guint8 *digest;
449
 
   sha1 *message;
450
 
 
451
 
   digest = g_malloc0(sizeof(guint8) * 21);
452
 
   message = g_malloc0(sizeof(sha1));
453
 
   message->blockdata = g_malloc0(sizeof(block));
454
 
   message->H = g_malloc(sizeof(hblock));
455
 
 
456
 
   message->H->chunkblock[0] = 0x67452301;
457
 
   message->H->chunkblock[1] = 0xefcdab89;
458
 
   message->H->chunkblock[2] = 0x98badcfe;
459
 
   message->H->chunkblock[3] = 0x10325476;
460
 
   message->H->chunkblock[4] = 0xc3d2e1f0;
461
 
   while (temp_len >= 64)
462
 
   {
463
 
      for (x = 0; x < 64; x++)
464
 
         message->blockdata->charblock[x] = temp_text[x];
465
 
#if BYTE_ORDER == LITTLE_ENDIAN
466
 
      little_endian((hblock *) message->blockdata, 16);
467
 
#endif
468
 
      process_block_sha1(message);
469
 
      temp_len -= 64;
470
 
      temp_text += 64;
471
 
   }
472
 
   for (x = 0; x < temp_len; x++)
473
 
      message->blockdata->charblock[x] = temp_text[x];
474
 
   message->blockdata->charblock[temp_len] = 0x80;
475
 
   for (x = temp_len + 1; x < 64; x++)
476
 
      message->blockdata->charblock[x] = 0x00;
477
 
#if BYTE_ORDER == LITTLE_ENDIAN
478
 
   little_endian((hblock *) message->blockdata, 16);
479
 
#endif
480
 
   if (temp_len > 54)
481
 
   {
482
 
      process_block_sha1(message);
483
 
      for (x = 0; x < 60; x++)
484
 
         message->blockdata->charblock[x] = 0x00;
485
 
   }
486
 
   message->blockdata->chunkblock[15] = len * 8;
487
 
   process_block_sha1(message);
488
 
#if BYTE_ORDER == LITTLE_ENDIAN
489
 
   little_endian(message->H, 5);
490
 
#endif
491
 
   for (x = 0; x < 20; x++)
492
 
      digest[x] = message->H->charblock[x];
493
 
   digest[20] = 0x00;
494
 
   g_free(message->blockdata);
495
 
   g_free(message->H);
496
 
   g_free(message);
497
 
   return (digest);
498
 
}
499
 
 
500
 
/*
501
 
 * process_block_sha1 - process one 512-bit block of data
502
 
 * @message - the sha1 struct we're doing working on
503
 
 */
504
 
static void
505
 
process_block_sha1(sha1 * message)
506
 
{
507
 
   chunk x;
508
 
   chunk w[80];                 /* test block */
509
 
   chunk A;                     /* A - E: used for hash calc */
510
 
   chunk B;
511
 
   chunk C;
512
 
   chunk D;
513
 
   chunk E;
514
 
   chunk T;                     /* temp guint32 */
515
 
   chunk K[] = { 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 };
516
 
 
517
 
   for (x = 0; x < 16; x++)
518
 
      w[x] = message->blockdata->chunkblock[x];
519
 
   for (x = 16; x < 80; x++)
520
 
   {
521
 
      w[x] = w[x - 3] ^ w[x - 8] ^ w[x - 14] ^ w[x - 16];
522
 
      w[x] = (w[x] << 1) | (w[x] >> 31);
523
 
   }
524
 
   A = message->H->chunkblock[0];
525
 
   B = message->H->chunkblock[1];
526
 
   C = message->H->chunkblock[2];
527
 
   D = message->H->chunkblock[3];
528
 
   E = message->H->chunkblock[4];
529
 
   for (x = 0; x < 80; x++)
530
 
   {
531
 
      T = ((A << 5) | (A >> 27)) + E + w[x] + K[x / 20];
532
 
      if (x < 20)
533
 
         T += (B & C) | ((~B) & D);
534
 
      else if (x < 40 || x >= 60)
535
 
         T += B ^ C ^ D;
536
 
      else
537
 
         T += ((C | D) & B) | (C & D);
538
 
      E = D;
539
 
      D = C;
540
 
      C = (B << 30) | (B >> 2);
541
 
      B = A;
542
 
      A = T;
543
 
   }
544
 
   message->H->chunkblock[0] += A;
545
 
   message->H->chunkblock[1] += B;
546
 
   message->H->chunkblock[2] += C;
547
 
   message->H->chunkblock[3] += D;
548
 
   message->H->chunkblock[4] += E;
549
 
}
550
 
 
551
 
#if BYTE_ORDER == LITTLE_ENDIAN
552
 
/*
553
 
 * little_endian - swap the significants bits to cater to bigendian
554
 
 * @stupidblock - the block of data we're swapping
555
 
 * @blocks - the number of blocks we're swapping
556
 
 */
557
 
static void
558
 
little_endian(hblock * stupidblock, int blocks)
559
 
{
560
 
   int x;
561
 
   for (x = 0; x < blocks; x++)
562
 
   {
563
 
        stupidblock->chunkblock[x] = (stupidblock->charblock[x * 4] << 24 | stupidblock->charblock[x * 4 + 1] << 16 | stupidblock->charblock[x * 4 +2] << 8 | stupidblock->charblock[x * 4 + 3]);
564
 
   }
565
 
}
566
 
#endif
567
 
 
568