]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/drms.c
Digest => DigestMD5
[vlc] / modules / demux / mp4 / drms.c
index 2cdf1defff99383652d5f493a971b88a66428717..711eb3d33739c69b5b7dbf3094d9dd33aa2f503b 100644 (file)
@@ -2,7 +2,7 @@
  * drms.c: DRMS
  *****************************************************************************
  * Copyright (C) 2004 VideoLAN
- * $Id: drms.c,v 1.11 2004/01/23 20:58:52 jlj Exp $
+ * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
  *          Sam Hocevar <sam@zoy.org>
 #   include <stdio.h>
 #endif
 
-#include <vlc/vlc.h>
+#ifdef __VLC__
+#   include <vlc/vlc.h>
+#   include "vlc_md5.h"
+#   include "libmp4.h"
+#else
+#   include "drmsvl.h"
+#endif
 
 #ifdef HAVE_ERRNO_H
 #   include <errno.h>
 #endif
 
 #ifdef WIN32
+#   if !defined( UNDER_CE )
+#       include <direct.h>
+#   endif
 #   include <tchar.h>
 #   include <shlobj.h>
 #   include <windows.h>
 #   include <limits.h>
 #endif
 
+#ifdef SYS_DARWIN
+#   include <mach/mach.h>
+#   include <IOKit/IOKitLib.h>
+#   include <CoreFoundation/CFNumber.h>
+#endif
+
 #ifdef HAVE_SYSFS_LIBSYSFS_H
-#    include <sysfs/libsysfs.h>
+#   include <sysfs/libsysfs.h>
 #endif
 
 #include "drms.h"
 #include "drmstables.h"
 
-#include "libmp4.h"
-
+#if !defined( UNDER_CE )
 /*****************************************************************************
  * aes_s: AES keys structure
  *****************************************************************************
@@ -75,6 +89,9 @@ struct aes_s
     uint32_t pp_dec_keys[ AES_KEY_COUNT + 1 ][ 4 ];
 };
 
+#ifdef __VLC__
+# define Digest DigestMD5
+#else
 /*****************************************************************************
  * md5_s: MD5 message structure
  *****************************************************************************
@@ -87,6 +104,7 @@ struct md5_s
     uint32_t p_digest[4]; /* The MD5 digest */
     uint32_t p_data[16];  /* Buffer to cache non-aligned writes */
 };
+#endif
 
 /*****************************************************************************
  * shuffle_s: shuffle structure
@@ -96,10 +114,13 @@ struct md5_s
  *****************************************************************************/
 struct shuffle_s
 {
+    uint32_t i_version;
     uint32_t p_commands[ 20 ];
     uint32_t p_bordel[ 16 ];
 };
 
+#define SWAP( a, b ) { (a) ^= (b); (b) ^= (a); (a) ^= (b); }
+
 /*****************************************************************************
  * drms_s: DRMS structure
  *****************************************************************************
@@ -124,13 +145,29 @@ struct drms_s
 static void InitAES       ( struct aes_s *, uint32_t * );
 static void DecryptAES    ( struct aes_s *, uint32_t *, const uint32_t * );
 
+#ifndef __VLC__
 static void InitMD5       ( struct md5_s * );
 static void AddMD5        ( struct md5_s *, const uint8_t *, uint32_t );
 static void EndMD5        ( struct md5_s * );
 static void Digest        ( struct md5_s *, uint32_t * );
+#endif
 
-static void InitShuffle   ( struct shuffle_s *, uint32_t * );
-static void DoShuffle     ( struct shuffle_s *, uint32_t * );
+static void InitShuffle   ( struct shuffle_s *, uint32_t *, uint32_t );
+static void DoShuffle     ( struct shuffle_s *, uint32_t *, uint32_t );
+
+static uint32_t FirstPass ( uint32_t * );
+static void SecondPass    ( uint32_t *, uint32_t );
+static void ThirdPass     ( uint32_t * );
+static void FourthPass    ( uint32_t * );
+static void TinyShuffle1  ( uint32_t * );
+static void TinyShuffle2  ( uint32_t * );
+static void TinyShuffle3  ( uint32_t * );
+static void TinyShuffle4  ( uint32_t * );
+static void TinyShuffle5  ( uint32_t * );
+static void TinyShuffle6  ( uint32_t * );
+static void TinyShuffle7  ( uint32_t * );
+static void TinyShuffle8  ( uint32_t * );
+static void DoExtShuffle  ( uint32_t * );
 
 static int GetSystemKey   ( uint32_t *, vlc_bool_t );
 static int WriteUserKey   ( void *, uint32_t * );
@@ -139,7 +176,7 @@ static int GetUserKey     ( void *, uint32_t * );
 
 static int GetSCIData     ( char *, uint32_t **, uint32_t * );
 static int HashSystemInfo ( uint32_t * );
-static int GetiPodID      ( long long * );
+static int GetiPodID      ( int64_t * );
 
 #ifdef WORDS_BIGENDIAN
 /*****************************************************************************
@@ -311,10 +348,19 @@ int drms_init( void *_p_drms, uint32_t i_type,
             AddMD5( &md5, p_drms->p_iviv, 16 );
             EndMD5( &md5 );
 
-            if( GetUserKey( p_drms, p_drms->p_key ) )
+            if( p_drms->i_user == 0 && p_drms->i_key == 0 )
             {
-                i_ret = -1;
-                break;
+                static char const p_secret[] = "tr1-th3n.y00_by3";
+                memcpy( p_drms->p_key, p_secret, 16 );
+                REVERSE( p_drms->p_key, 4 );
+            }
+            else
+            {
+                if( GetUserKey( p_drms, p_drms->p_key ) )
+                {
+                    i_ret = -1;
+                    break;
+                }
             }
 
             InitAES( &p_drms->aes, p_drms->p_key );
@@ -448,6 +494,7 @@ static void DecryptAES( struct aes_s *p_aes,
     }
 }
 
+#ifndef __VLC__
 /*****************************************************************************
  * InitMD5: initialise an MD5 message
  *****************************************************************************
@@ -634,6 +681,7 @@ static void Digest( struct md5_s *p_md5, uint32_t *p_input )
     p_md5->p_digest[ 2 ] += c;
     p_md5->p_digest[ 3 ] += d;
 }
+#endif
 
 /*****************************************************************************
  * InitShuffle: initialise a shuffle structure
@@ -641,13 +689,16 @@ static void Digest( struct md5_s *p_md5, uint32_t *p_input )
  * This function initialises tables in the p_shuffle structure that will be
  * used later by DoShuffle. The only external parameter is p_sys_key.
  *****************************************************************************/
-static void InitShuffle( struct shuffle_s *p_shuffle, uint32_t *p_sys_key )
+static void InitShuffle( struct shuffle_s *p_shuffle, uint32_t *p_sys_key,
+                         uint32_t i_version )
 {
     char p_secret1[] = "Tv!*";
     static char const p_secret2[] = "v8rhvsaAvOKMFfUH%798=[;."
                                     "f8677680a634ba87fnOIf)(*";
     unsigned int i;
 
+    p_shuffle->i_version = i_version;
+
     /* Fill p_commands using the key and a secret seed */
     for( i = 0; i < 20; i++ )
     {
@@ -677,18 +728,47 @@ static void InitShuffle( struct shuffle_s *p_shuffle, uint32_t *p_sys_key )
 }
 
 /*****************************************************************************
- * DoShuffle: shuffle 16 byte buffer
+ * DoShuffle: shuffle buffer
  *****************************************************************************
  * This is so ugly and uses so many MD5 checksums that it is most certainly
  * one-way, though why it needs to be so complicated is beyond me.
  *****************************************************************************/
-static void DoShuffle( struct shuffle_s *p_shuffle, uint32_t *p_buffer )
+static void DoShuffle( struct shuffle_s *p_shuffle,
+                       uint32_t *p_buffer, uint32_t i_size )
 {
     struct md5_s md5;
     uint32_t p_big_bordel[ 16 ];
     uint32_t *p_bordel = p_shuffle->p_bordel;
     unsigned int i;
 
+    static uint32_t i_secret = 0;
+
+    static uint32_t p_secret1[] =
+    {
+        0xAAAAAAAA, 0x01757700, 0x00554580, 0x01724500, 0x00424580,
+        0x01427700, 0x00000080, 0xC1D59D01, 0x80144981, 0x815C8901,
+        0x80544981, 0x81D45D01, 0x00000080, 0x81A3BB03, 0x00A2AA82,
+        0x01A3BB03, 0x0022A282, 0x813BA202, 0x00000080, 0x6D575737,
+        0x4A5275A5, 0x6D525725, 0x4A5254A5, 0x6B725437, 0x00000080,
+        0xD5DDB938, 0x5455A092, 0x5D95A013, 0x4415A192, 0xC5DD393A,
+        0x00000080, 0x55555555
+    };
+
+    static char p_secret2[] =
+        "pbclevtug (p) Nccyr Pbzchgre, Vap.  Nyy Evtugf Erfreirq.";
+
+    if( i_secret == 0 )
+    {
+        REVERSE( p_secret1, sizeof(p_secret1)/sizeof(p_secret1[ 0 ]) );
+        for( ; p_secret2[ i_secret ] != '\0'; i_secret++ )
+        {
+#define ROT13(c) (((c)>='A'&&(c)<='Z')?(((c)-'A'+13)%26)+'A':\
+                  ((c)>='a'&&(c)<='z')?(((c)-'a'+13)%26)+'a':c)
+            p_secret2[ i_secret ] = ROT13(p_secret2[ i_secret ]);
+        }
+        i_secret++; /* include zero terminator */
+    }
+
     /* Using the MD5 hash of a memory block is probably not one-way enough
      * for the iTunes people. This function randomises p_bordel depending on
      * the values in p_commands to make things even more messy in p_bordel. */
@@ -722,6 +802,11 @@ static void DoShuffle( struct shuffle_s *p_shuffle, uint32_t *p_buffer )
         }
     }
 
+    if( p_shuffle->i_version == 0x01000300 )
+    {
+        DoExtShuffle( p_bordel );
+    }
+
     /* Convert our newly randomised p_bordel to big endianness and take
      * its MD5 hash. */
     InitMD5( &md5 );
@@ -730,15 +815,680 @@ static void DoShuffle( struct shuffle_s *p_shuffle, uint32_t *p_buffer )
         p_big_bordel[ i ] = U32_AT(p_bordel + i);
     }
     AddMD5( &md5, (uint8_t *)p_big_bordel, 64 );
+    if( p_shuffle->i_version == 0x01000300 )
+    {
+        AddMD5( &md5, (uint8_t *)p_secret1, sizeof(p_secret1) );
+        AddMD5( &md5, (uint8_t *)p_secret2, i_secret );
+    }
     EndMD5( &md5 );
 
     /* XOR our buffer with the computed checksum */
-    for( i = 0; i < 4; i++ )
+    for( i = 0; i < i_size; i++ )
     {
         p_buffer[ i ] ^= md5.p_digest[ i ];
     }
 }
 
+/*****************************************************************************
+ * DoExtShuffle: extended shuffle
+ *****************************************************************************
+ * This is even uglier.
+ *****************************************************************************/
+static void DoExtShuffle( uint32_t * p_bordel )
+{
+    uint32_t i_ret;
+
+    i_ret = FirstPass( p_bordel );
+
+    SecondPass( p_bordel, i_ret );
+
+    ThirdPass( p_bordel );
+
+    FourthPass( p_bordel );
+}
+
+static uint32_t FirstPass( uint32_t * p_bordel )
+{
+    uint32_t i, i_cmd, i_ret = 5;
+
+    TinyShuffle1( p_bordel );
+
+    for( ; ; )
+    {
+        for( ; ; )
+        {
+            p_bordel[ 1 ] += 0x10000000;
+            p_bordel[ 3 ] += 0x12777;
+
+            if( (p_bordel[ 10 ] & 1) && i_ret )
+            {
+                i_ret--;
+                p_bordel[ 1 ] -= p_bordel[ 2 ];
+                p_bordel[ 11 ] += p_bordel[ 12 ];
+                break;
+            }
+
+            if( (p_bordel[ 1 ] + p_bordel[ 2 ]) >= 0x7D0 )
+            {
+                switch( ((p_bordel[ 3 ] ^ 0x567F) >> 2) & 7 )
+                {
+                    case 0:
+                        for( i = 0; i < 3; i++ )
+                        {
+                            if( p_bordel[ i + 10 ] > 0x4E20 )
+                            {
+                                p_bordel[ i + 1 ] += p_bordel[ i + 2 ];
+                            }
+                        }
+                        break;
+                    case 4:
+                        p_bordel[ 1 ] -= p_bordel[ 2 ];
+                        /* no break */
+                    case 3:
+                        p_bordel[ 11 ] += p_bordel[ 12 ];
+                        break;
+                    case 6:
+                        p_bordel[ 3 ] ^= p_bordel[ 4 ];
+                        /* no break */
+                    case 8:
+                        p_bordel[ 13 ] &= p_bordel[ 14 ];
+                        /* no break */
+                    case 1:
+                        p_bordel[ 0 ] |= p_bordel[ 1 ];
+                        if( i_ret )
+                        {
+                            return i_ret;
+                        }
+                        break;
+                }
+
+                break;
+            }
+        }
+
+        for( i = 0, i_cmd = 0; i < 16; i++ )
+        {
+            if( p_bordel[ i ] < p_bordel[ i_cmd ] )
+            {
+                i_cmd = i;
+            }
+        }
+
+        if( i_ret && i_cmd != 5 )
+        {
+            i_ret--;
+        }
+        else
+        {
+            if( i_cmd == 5 )
+            {
+                p_bordel[ 8 ] &= p_bordel[ 6 ] >> 1;
+                p_bordel[ 3 ] <<= 1;
+            }
+
+            for( i = 0; i < 3; i++ )
+            {
+                p_bordel[ 11 ] += 1;
+                if( p_bordel[ 11 ] & 5 )
+                {
+                    p_bordel[ 8 ] += p_bordel[ 9 ];
+                }
+                else if( i_ret )
+                {
+                    i_ret--;
+                    i_cmd = 3;
+                    goto break2;
+                }
+            }
+
+            i_cmd = (p_bordel[ 15 ] + 0x93) >> 3;
+            if( p_bordel[ 15 ] & 0x100 )
+            {
+                i_cmd ^= 0xDEAD;
+            }
+        }
+
+        switch( i_cmd & 3 )
+        {
+            case 0:
+                while( p_bordel[ 11 ] & 1 )
+                {
+                    p_bordel[ 11 ] >>= 1;
+                    p_bordel[ 12 ] += 1;
+                }
+                /* no break */
+            case 2:
+                p_bordel[ 14 ] -= 0x19FE;
+                break;
+            case 3:
+                if( i_ret )
+                {
+                    i_ret--;
+                    p_bordel[ 5 ] += 5;
+                    continue;
+                }
+                break;
+        }
+
+        i_cmd = ((p_bordel[ 3 ] + p_bordel[ 4 ] + 10) >> 1) - p_bordel[ 4 ];
+        break;
+    }
+break2:
+
+    switch( i_cmd & 3 )
+    {
+        case 0:
+            p_bordel[ 14 ] >>= 1;
+            break;
+        case 1:
+            p_bordel[ 5 ] <<= 2;
+            break;
+        case 2:
+            p_bordel[ 12 ] |= 5;
+            break;
+        case 3:
+            p_bordel[ 15 ] &= 0x55;
+            if( i_ret )
+            {
+                p_bordel[ 2 ] &= 0xB62FC;
+                return i_ret;
+            }
+            break;
+    }
+
+    TinyShuffle2( p_bordel );
+
+    return i_ret;
+}
+
+static void SecondPass( uint32_t * p_bordel, uint32_t i_tmp )
+{
+    uint32_t i, i_cmd, i_jc = 5;
+
+    TinyShuffle3( p_bordel );
+
+    for( i = 0, i_cmd = 0; i < 16; i++ )
+    {
+        if( p_bordel[ i ] > p_bordel[ i_cmd ] )
+        {
+            i_cmd = i;
+        }
+    }
+
+    switch( i_cmd )
+    {
+        case 0:
+            if( p_bordel[ 1 ] < p_bordel[ 8 ] )
+            {
+                p_bordel[ 5 ] += 1;
+            }
+            break;
+        case 4:
+            if( (p_bordel[ 9 ] & 0x7777) == 0x3333 )
+            {
+                p_bordel[ 5 ] -= 1;
+            }
+            else
+            {
+                i_jc--;
+                if( p_bordel[ 1 ] < p_bordel[ 8 ] )
+                {
+                    p_bordel[ 5 ] += 1;
+                }
+                break;
+            }
+            /* no break */
+        case 7:
+            p_bordel[ 2 ] -= 1;
+            p_bordel[ 1 ] -= p_bordel[ 5 ];
+            for( i = 0; i < 3; i++ )
+            {
+                switch( p_bordel[ 1 ] & 3 )
+                {
+                    case 0:
+                        p_bordel[ 1 ] += 1;
+                        /* no break */
+                    case 1:
+                        p_bordel[ 3 ] -= 8;
+                        break;
+                    case 2:
+                        p_bordel[ 13 ] &= 0xFEFEFEF7;
+                        break;
+                    case 3:
+                        p_bordel[ 8 ] |= 0x80080011;
+                        break;
+                }
+            }
+            return;
+        case 10:
+            p_bordel[ 4 ] -= 1;
+            p_bordel[ 5 ] += 1;
+            p_bordel[ 6 ] -= 1;
+            p_bordel[ 7 ] += 1;
+            break;
+        default:
+            p_bordel[ 15 ] ^= 0x18547EFF;
+            break;
+    }
+
+    for( i = 3; i--; )
+    {
+        switch( ( p_bordel[ 12 ] + p_bordel[ 13 ] + p_bordel[ 6 ] ) % 5 )
+        {
+            case 0:
+                p_bordel[ 12 ] -= 1;
+                /* no break */
+            case 1:
+                p_bordel[ 12 ] -= 1;
+                p_bordel[ 13 ] += 1;
+                break;
+            case 2:
+                p_bordel[ 13 ] += 4;
+                /* no break */
+            case 3:
+                p_bordel[ 12 ] -= 1;
+                break;
+            case 4:
+                i_jc--;
+                p_bordel[ 5 ] += 1;
+                p_bordel[ 6 ] -= 1;
+                p_bordel[ 7 ] += 1;
+                i = 3; /* Restart the whole loop */
+                break;
+        }
+    }
+
+    TinyShuffle4( p_bordel );
+
+    for( ; ; )
+    {
+        TinyShuffle5( p_bordel );
+
+        switch( ( p_bordel[ 2 ] * 2 + 15 ) % 5 )
+        {
+            case 0:
+                if( ( p_bordel[ 3 ] + i_tmp ) <=
+                    ( p_bordel[ 1 ] + p_bordel[ 15 ] ) )
+                {
+                    p_bordel[ 3 ] += 1;
+                }
+                break;
+            case 4:
+                p_bordel[ 10 ] -= 0x13;
+                break;
+            case 3:
+                p_bordel[ 5 ] >>= 2;
+                break;
+        }
+
+        if( !( p_bordel[ 2 ] & 1 ) || i_jc == 0 )
+        {
+            break;
+        }
+
+        i_jc--;
+        p_bordel[ 2 ] += 0x13;
+        p_bordel[ 12 ] += 1;
+    }
+
+    p_bordel[ 2 ] &= 0x10076000;
+}
+
+static void ThirdPass( uint32_t * p_bordel )
+{
+    uint32_t i_cmd;
+
+    i_cmd = ((p_bordel[ 7 ] + p_bordel[ 14 ] + 10) >> 1) - p_bordel[ 14 ];
+    i_cmd = i_cmd % 10;
+
+    switch( i_cmd )
+    {
+        case 0:
+            p_bordel[ 1 ] <<= 1;
+            p_bordel[ 2 ] <<= 2;
+            p_bordel[ 3 ] <<= 3;
+            break;
+        case 6:
+            p_bordel[ i_cmd + 3 ] &= 0x5EDE36B;
+            p_bordel[ 5 ] += p_bordel[ 8 ];
+            p_bordel[ 4 ] += p_bordel[ 7 ];
+            p_bordel[ 3 ] += p_bordel[ 6 ];
+            p_bordel[ 2 ] += p_bordel[ 5 ];
+            /* no break */
+        case 2:
+            p_bordel[ 1 ] += p_bordel[ 4 ];
+            p_bordel[ 0 ] += p_bordel[ 3 ];
+            TinyShuffle6( p_bordel );
+            return; /* jc = 4 */
+        case 3:
+            if( (p_bordel[ 11 ] & p_bordel[ 2 ]) > 0x211B )
+            {
+                p_bordel[ 6 ] += 1;
+            }
+            break;
+        case 4:
+            p_bordel[ 7 ] += 1;
+            /* no break */
+        case 5:
+            p_bordel[ 9 ] ^= p_bordel[ 2 ];
+            break;
+        case 7:
+            p_bordel[ 2 ] ^= (p_bordel[ 1 ] & p_bordel[ 13 ]);
+            break;
+        case 8:
+            p_bordel[ 0 ] -= p_bordel[ 11 ] & p_bordel[ 15 ];
+            return; /* jc = 4 */
+        case 9:
+            p_bordel[ 6 ] >>= (p_bordel[ 14 ] & 3);
+            break;
+    }
+
+    SWAP( p_bordel[ 0 ], p_bordel[ 10 ] );
+
+    TinyShuffle6( p_bordel );
+
+    return; /* jc = 5 */
+}
+
+static void FourthPass( uint32_t * p_bordel )
+{
+    uint32_t i, j;
+
+    TinyShuffle7( p_bordel );
+
+    switch( p_bordel[ 5 ] % 5)
+    {
+        case 0:
+            p_bordel[ 0 ] += 1;
+            break;
+        case 2:
+            p_bordel[ 11 ] ^= (p_bordel[ 3 ] + p_bordel[ 6 ] + p_bordel[ 8 ]);
+            break;
+        case 3:
+            for( i = 4; i < 15 && (p_bordel[ i ] & 5) == 0; i++ )
+            {
+                SWAP( p_bordel[ i ], p_bordel[ 15 - i ] );
+            }
+            break;
+        case 4:
+            p_bordel[ 12 ] -= 1;
+            p_bordel[ 13 ] += 1;
+            p_bordel[ 2 ] -= 0x64;
+            p_bordel[ 3 ] += 0x64;
+            TinyShuffle8( p_bordel );
+            return;
+    }
+
+    for( i = 0, j = 0; i < 16; i++ )
+    {
+        if( p_bordel[ i ] > p_bordel[ j ] )
+        {
+            j = i;
+        }
+    }
+
+    switch( p_bordel[ j ] % 100 )
+    {
+        case 0:
+            SWAP( p_bordel[ 0 ], p_bordel[ j ] );
+            break;
+        case 8:
+            p_bordel[ 1 ] >>= 1;
+            p_bordel[ 2 ] <<= 1;
+            p_bordel[ 14 ] >>= 3;
+            p_bordel[ 15 ] <<= 4;
+            break;
+        case 57:
+            p_bordel[ j ] += p_bordel[ 13 ];
+            break;
+        case 76:
+            p_bordel[ 1 ] += 0x20E;
+            p_bordel[ 5 ] += 0x223D;
+            p_bordel[ 13 ] -= 0x576;
+            p_bordel[ 15 ] += 0x576;
+            return;
+        case 91:
+            p_bordel[ 2 ] -= 0x64;
+            p_bordel[ 3 ] += 0x64;
+            p_bordel[ 12 ] -= 1;
+            p_bordel[ 13 ] += 1;
+            break;
+        case 99:
+            p_bordel[ 0 ] += 1;
+            p_bordel[ j ] += p_bordel[ 13 ];
+            break;
+    }
+
+    TinyShuffle8( p_bordel );
+}
+
+/*****************************************************************************
+ * TinyShuffle[12345678]: tiny shuffle subroutines
+ *****************************************************************************
+ * These standalone functions are little helpers for the shuffling process.
+ *****************************************************************************/
+static void TinyShuffle1( uint32_t * p_bordel )
+{
+    uint32_t i_cmd = (p_bordel[ 5 ] + 10) >> 2;
+
+    if( p_bordel[ 5 ] > 0x7D0 )
+    {
+        i_cmd -= 0x305;
+    }
+
+    switch( i_cmd & 3 )
+    {
+        case 0:
+            p_bordel[ 5 ] += 5;
+            break;
+        case 1:
+            p_bordel[ 4 ] -= 1;
+            break;
+        case 2:
+            if( p_bordel[ 4 ] & 5 )
+            {
+                p_bordel[ 1 ] ^= 0x4D;
+            }
+            /* no break */
+        case 3:
+            p_bordel[ 12 ] += 5;
+            break;
+    }
+}
+
+static void TinyShuffle2( uint32_t * p_bordel )
+{
+    uint32_t i, j;
+
+    for( i = 0, j = 0; i < 16; i++ )
+    {
+        if( (p_bordel[ i ] & 0x777) > (p_bordel[ j ] & 0x777) )
+        {
+            j = i;
+        }
+    }
+
+    if( j > 5 )
+    {
+        for( ; j < 15; j++ )
+        {
+            p_bordel[ j ] += p_bordel[ j + 1 ];
+        }
+    }
+    else
+    {
+        p_bordel[ 2 ] &= 0xB62FC;
+    }
+}
+
+static void TinyShuffle3( uint32_t * p_bordel )
+{
+    uint32_t i_cmd = p_bordel[ 6 ] + 0x194B;
+
+    if( p_bordel[ 6 ] > 0x2710 )
+    {
+        i_cmd >>= 1;
+    }
+
+    switch( i_cmd & 3 )
+    {
+        case 1:
+            p_bordel[ 3 ] += 0x19FE;
+            break;
+        case 2:
+            p_bordel[ 7 ] -= p_bordel[ 3 ] >> 2;
+            /* no break */
+        case 0:
+            p_bordel[ 5 ] ^= 0x248A;
+            break;
+    }
+}
+
+static void TinyShuffle4( uint32_t * p_bordel )
+{
+    uint32_t i, j;
+
+    for( i = 0, j = 0; i < 16; i++ )
+    {
+        if( p_bordel[ i ] < p_bordel[ j ] )
+        {
+            j = i;
+        }
+    }
+
+    if( (p_bordel[ j ] % (j + 1)) > 10 )
+    {
+        p_bordel[ 1 ] -= 1;
+        p_bordel[ 2 ] += 0x13;
+        p_bordel[ 12 ] += 1;
+    }
+}
+
+static void TinyShuffle5( uint32_t * p_bordel )
+{
+    uint32_t i;
+
+    p_bordel[ 2 ] &= 0x7F3F;
+
+    for( i = 0; i < 5; i++ )
+    {
+        switch( ( p_bordel[ 2 ] + 10 + i ) % 5 )
+        {
+            case 0:
+                p_bordel[ 12 ] &= p_bordel[ 2 ];
+                /* no break */
+            case 1:
+                p_bordel[ 3 ] ^= p_bordel[ 15 ];
+                break;
+            case 2:
+                p_bordel[ 15 ] += 0x576;
+                /* no break */
+            case 3:
+                p_bordel[ 7 ] -= 0x2D;
+                /* no break */
+            case 4:
+                p_bordel[ 1 ] <<= 1;
+                break;
+        }
+    }
+}
+
+static void TinyShuffle6( uint32_t * p_bordel )
+{
+    uint32_t i, j;
+
+    for( i = 0; i < 8; i++ )
+    {
+        j = p_bordel[ 3 ] & 0x7514 ? 5 : 7;
+        SWAP( p_bordel[ i ], p_bordel[ i + j ] );
+    }
+}
+
+static void TinyShuffle7( uint32_t * p_bordel )
+{
+    uint32_t i;
+
+    i = (((p_bordel[ 9 ] + p_bordel[ 15 ] + 12) >> 2) - p_bordel[ 4 ]) & 7;
+
+    while( i-- )
+    {
+        SWAP( p_bordel[ i ], p_bordel[ i + 3 ] );
+    }
+
+    SWAP( p_bordel[ 1 ], p_bordel[ 10 ] );
+}
+
+static void TinyShuffle8( uint32_t * p_bordel )
+{
+    uint32_t i;
+
+    i = (p_bordel[ 0 ] & p_bordel[ 6 ]) & 0xF;
+
+    switch( p_bordel[ i ] % 1000 )
+    {
+        case 7:
+            if( (p_bordel[ i ] & 0x777) > (p_bordel[ 7 ] & 0x5555) )
+            {
+                p_bordel[ i ] ^= p_bordel[ 5 ] & p_bordel[ 3 ];
+            }
+            break;
+        case 19:
+            p_bordel[ 15 ] &= 0x5555;
+            break;
+        case 93:
+            p_bordel[ i ] ^= p_bordel[ 15 ];
+            break;
+        case 100:
+            SWAP( p_bordel[ 0 ], p_bordel[ 3 ] );
+            SWAP( p_bordel[ 1 ], p_bordel[ 6 ] );
+            SWAP( p_bordel[ 3 ], p_bordel[ 6 ] );
+            SWAP( p_bordel[ 4 ], p_bordel[ 9 ] );
+            SWAP( p_bordel[ 5 ], p_bordel[ 8 ] );
+            SWAP( p_bordel[ 6 ], p_bordel[ 7 ] );
+            SWAP( p_bordel[ 13 ], p_bordel[ 14 ] );
+            break;
+        case 329:
+            p_bordel[ i ] += p_bordel[ 1 ] ^ 0x80080011;
+            p_bordel[ i ] += p_bordel[ 2 ] ^ 0xBEEFDEAD;
+            p_bordel[ i ] += p_bordel[ 3 ] ^ 0x8765F444;
+            p_bordel[ i ] += p_bordel[ 4 ] ^ 0x78145326;
+            break;
+        case 567:
+            p_bordel[ 12 ] -= p_bordel[ i ];
+            p_bordel[ 13 ] += p_bordel[ i ];
+            break;
+        case 612:
+            p_bordel[ i ] += p_bordel[ 1 ];
+            p_bordel[ i ] -= p_bordel[ 7 ];
+            p_bordel[ i ] -= p_bordel[ 8 ];
+            p_bordel[ i ] += p_bordel[ 9 ];
+            p_bordel[ i ] += p_bordel[ 13 ];
+            break;
+        case 754:
+            i = __MIN( i, 12 );
+            p_bordel[ i + 1 ] >>= 1;
+            p_bordel[ i + 2 ] <<= 4;
+            p_bordel[ i + 3 ] >>= 3;
+            break;
+        case 777:
+            p_bordel[ 1 ] += 0x20E;
+            p_bordel[ 5 ] += 0x223D;
+            p_bordel[ 13 ] -= 0x576;
+            p_bordel[ 15 ] += 0x576;
+            break;
+        case 981:
+            if( (p_bordel[ i ] ^ 0x8765F441) < 0x2710 )
+            {
+                SWAP( p_bordel[ 0 ], p_bordel[ 1 ] );
+            }
+            else
+            {
+                SWAP( p_bordel[ 1 ], p_bordel[ 11 ] );
+            }
+            break;
+    }
+}
+
 /*****************************************************************************
  * GetSystemKey: get the system key
  *****************************************************************************
@@ -749,7 +1499,7 @@ static int GetSystemKey( uint32_t *p_sys_key, vlc_bool_t b_ipod )
     static char const p_secret1[ 8 ] = "YuaFlafu";
     static char const p_secret2[ 8 ] = "zPif98ga";
     struct md5_s md5;
-    long long i_ipod_id;
+    int64_t i_ipod_id;
     uint32_t p_system_hash[ 4 ];
 
     /* Compute the MD5 hash of our system info */
@@ -821,7 +1571,7 @@ static int WriteUserKey( void *_p_drms, uint32_t *p_user_key )
         snprintf( psz_path, PATH_MAX - 1, "%s/" DRMS_DIRNAME "/%08X.%03d",
                   p_drms->psz_homedir, p_drms->i_user, p_drms->i_key );
 
-        file = fopen( psz_path, "w" );
+        file = fopen( psz_path, "wb" );
         if( file != NULL )
         {
             i_ret = fwrite( p_user_key, sizeof(uint32_t),
@@ -849,7 +1599,7 @@ static int ReadUserKey( void *_p_drms, uint32_t *p_user_key )
               "%s/" DRMS_DIRNAME "/%08X.%03d", p_drms->psz_homedir,
               p_drms->i_user, p_drms->i_key );
 
-    file = fopen( psz_path, "r" );
+    file = fopen( psz_path, "rb" );
     if( file != NULL )
     {
         i_ret = fread( p_user_key, sizeof(uint32_t),
@@ -875,8 +1625,9 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
     struct shuffle_s shuffle;
     uint32_t i, y;
     uint32_t *p_sci_data;
+    uint32_t i_user, i_key;
     uint32_t p_sys_key[ 4 ];
-    uint32_t i_sci_size, i_blocks;
+    uint32_t i_sci_size, i_blocks, i_remaining;
     uint32_t *p_sci0, *p_sci1, *p_buffer;
     uint32_t p_sci_key[ 4 ];
     char *psz_ipod;
@@ -905,12 +1656,14 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
 
     /* Skip the first 4 bytes (some sort of header). Decrypt the rest. */
     i_blocks = (i_sci_size - 4) / 16;
+    i_remaining = (i_sci_size - 4) - (i_blocks * 16);
     p_buffer = p_sci_data + 1;
 
     /* Decrypt and shuffle our data at the same time */
     InitAES( &aes, p_sys_key );
     REVERSE( p_sys_key, 4 );
-    InitShuffle( &shuffle, p_sys_key );
+    REVERSE( p_sci_data, 1 );
+    InitShuffle( &shuffle, p_sys_key, p_sci_data[ 0 ] );
 
     memcpy( p_sci_key, p_secret, 16 );
     REVERSE( p_sci_key, 4 );
@@ -927,7 +1680,7 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
         memcpy( p_sci_key, p_buffer, 16 );
 
         /* Shuffle the decrypted data using a custom routine */
-        DoShuffle( &shuffle, p_tmp );
+        DoShuffle( &shuffle, p_tmp, 4 );
 
         /* Copy this block back to p_buffer */
         memcpy( p_buffer, p_tmp, 16 );
@@ -935,6 +1688,13 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
         p_buffer += 4;
     }
 
+    if( i_remaining >= 4 )
+    {
+        i_remaining /= 4;
+        REVERSE( p_buffer, i_remaining );
+        DoShuffle( &shuffle, p_buffer, i_remaining );
+    }
+
     /* Phase 2: look for the user key in the generated data. I must admit I
      *          do not understand what is going on here, because it almost
      *          looks like we are browsing data that makes sense, even though
@@ -970,15 +1730,16 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
             continue;
         }
 
-        REVERSE( p_sci0, 1 );
-        REVERSE( p_sci1, 1 );
-        if( U32_AT( p_sci0 ) == p_drms->i_user &&
-            ( ( U32_AT( p_sci1 ) == p_drms->i_key ) ||
-              ( !p_drms->i_key ) || ( p_sci1 == (p_sci0 + 18) ) ) )
+        i_user = U32_AT( p_sci0 );
+        i_key = U32_AT( p_sci1 );
+        REVERSE( &i_user, 1 );
+        REVERSE( &i_key, 1 );
+        if( i_user == p_drms->i_user && ( ( i_key == p_drms->i_key ) ||
+            ( !p_drms->i_key && ( p_sci1 == (p_sci0 + 18) ) ) ) )
         {
-            REVERSE( p_sci1 + 1, 4 );
             memcpy( p_user_key, p_sci1 + 1, 16 );
-            WriteUserKey( p_drms, p_user_key );
+            REVERSE( p_sci1 + 1, 4 );
+            WriteUserKey( p_drms, p_sci1 + 1 );
             i_ret = 0;
             break;
         }
@@ -1044,7 +1805,7 @@ static int GetSCIData( char *psz_ipod, uint32_t **pp_sci,
         if( strstr( psz_ipod, ISCINFO ) == NULL )
         {
             snprintf( p_tmp, sizeof(p_tmp)/sizeof(p_tmp[0]) - 1,
-                      "%s/iPod_Control/iTunes/" ISCINFO, psz_ipod );
+                      "%s/iPod_Control/iTunes/" ISCINFO "2", psz_ipod );
             psz_path = p_tmp;
         }
         else
@@ -1058,12 +1819,12 @@ static int GetSCIData( char *psz_ipod, uint32_t **pp_sci,
         return -1;
     }
 
-    file = fopen( psz_path, "r" );
+    file = fopen( psz_path, "rb" );
     if( file != NULL )
     {
         struct stat st;
 
-        if( !fstat( fileno( file ), &st ) )
+        if( !fstat( fileno( file ), &st ) && st.st_size >= 4 )
         {
             *pp_sci = malloc( st.st_size );
             if( *pp_sci != NULL )
@@ -1099,8 +1860,6 @@ static int HashSystemInfo( uint32_t *p_system_hash )
     struct md5_s md5;
     int i_ret = 0;
 
-    InitMD5( &md5 );
-
 #ifdef WIN32
     HKEY i_key;
     unsigned int i;
@@ -1126,6 +1885,8 @@ static int HashSystemInfo( uint32_t *p_system_hash )
         }
     };
 
+    InitMD5( &md5 );
+
     AddMD5( &md5, "cache-control", 13 );
     AddMD5( &md5, "Ethernet", 8 );
 
@@ -1166,6 +1927,7 @@ static int HashSystemInfo( uint32_t *p_system_hash )
     }
 
 #else
+    InitMD5( &md5 );
     i_ret = -1;
 #endif
 
@@ -1180,10 +1942,13 @@ static int HashSystemInfo( uint32_t *p_system_hash )
  *****************************************************************************
  * This function gets the iPod ID.
  *****************************************************************************/
-static int GetiPodID( long long *p_ipod_id )
+static int GetiPodID( int64_t *p_ipod_id )
 {
     int i_ret = -1;
 
+#define PROD_NAME   "iPod"
+#define VENDOR_NAME "Apple Computer, Inc."
+
     char *psz_ipod_id = getenv( "IPODID" );
     if( psz_ipod_id != NULL )
     {
@@ -1191,7 +1956,70 @@ static int GetiPodID( long long *p_ipod_id )
         return 0;
     }
 
-#ifdef HAVE_SYSFS_LIBSYSFS_H
+#ifdef SYS_DARWIN
+    CFTypeRef value;
+    mach_port_t port;
+    io_object_t device;
+    io_iterator_t iterator;
+    CFMutableDictionaryRef match_dic;
+    CFMutableDictionaryRef smatch_dic;
+
+    if( IOMasterPort( MACH_PORT_NULL, &port ) == KERN_SUCCESS )
+    {
+        smatch_dic = IOServiceMatching( "IOFireWireUnit" );
+        match_dic = CFDictionaryCreateMutable( kCFAllocatorDefault, 0,
+                                           &kCFTypeDictionaryKeyCallBacks,
+                                           &kCFTypeDictionaryValueCallBacks );
+
+        if( smatch_dic != NULL && match_dic != NULL )
+        {
+            CFDictionarySetValue( smatch_dic,
+                                  CFSTR("FireWire Vendor Name"),
+                                  CFSTR(VENDOR_NAME) );
+            CFDictionarySetValue( smatch_dic,
+                                  CFSTR("FireWire Product Name"),
+                                  CFSTR(PROD_NAME) );
+
+            CFDictionarySetValue( match_dic,
+                                  CFSTR(kIOPropertyMatchKey),
+                                  smatch_dic );
+
+            if( IOServiceGetMatchingServices( port, match_dic,
+                                              &iterator ) == KERN_SUCCESS )
+            {
+                while( ( device = IOIteratorNext( iterator ) ) != NULL )
+                {
+                    value = IORegistryEntryCreateCFProperty( device,
+                        CFSTR("GUID"), kCFAllocatorDefault, kNilOptions );
+
+                    if( value != NULL )
+                    {
+                        if( CFGetTypeID( value ) == CFNumberGetTypeID() )
+                        {
+                            int64_t i_ipod_id;
+                            CFNumberGetValue( (CFNumberRef)value,
+                                              kCFNumberLongLongType,
+                                              &i_ipod_id );
+                            *p_ipod_id = i_ipod_id;
+                            i_ret = 0;
+                        }
+
+                        CFRelease( value );
+                    }
+
+                    IOObjectRelease( device );
+
+                    if( !i_ret ) break;
+                }
+
+                IOObjectRelease( iterator );
+            }
+        }
+
+        mach_port_deallocate( mach_task_self(), port );
+    }
+
+#elif HAVE_SYSFS_LIBSYSFS_H
     struct sysfs_bus *bus = NULL;
     struct dlist *devlist = NULL;
     struct dlist *attributes = NULL;
@@ -1213,7 +2041,8 @@ static int GetiPodID( long long *p_ipod_id )
                                          struct sysfs_attribute )
                     {
                         if( ( strcmp( curattr->name, "model_name" ) == 0 ) &&
-                            ( strncmp( curattr->value, "iPod", 4 ) == 0 ) )
+                            ( strncmp( curattr->value, PROD_NAME,
+                                       sizeof(PROD_NAME) ) == 0 ) )
                         {
                             *p_ipod_id = strtoll( curdev->name, NULL, 16 );
                             i_ret = 0;
@@ -1233,3 +2062,11 @@ static int GetiPodID( long long *p_ipod_id )
     return i_ret;
 }
 
+#else /* !defined( UNDER_CE ) */
+
+void *drms_alloc( char *psz_homedir ){ return 0; }
+void drms_free( void *a ){}
+void drms_decrypt( void *a, uint32_t *b, uint32_t c  ){}
+int drms_init( void *a, uint32_t b, uint8_t *c, uint32_t d ){ return -1; }
+
+#endif /* defined( UNDER_CE ) */