]> git.sesse.net Git - vlc/blobdiff - modules/demux/mp4/drms.c
A bit of vlc/libvlc cleanup:
[vlc] / modules / demux / mp4 / drms.c
index 69409321cccdc0c4c31d342258e1490e9bafcfd5..5a8abaf5a1aa0100d39d43743b52805aa253ebd7 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * drms.c: DRMS
  *****************************************************************************
- * Copyright (C) 2004 VideoLAN
+ * Copyright (C) 2004 the VideoLAN team
  * $Id$
  *
  * Authors: Jon Lech Johansen <jon-vl@nanocrew.net>
@@ -19,7 +19,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #include <stdlib.h>                                      /* malloc(), free() */
 #   include <stdio.h>
 #endif
 
-#include <vlc/vlc.h>
+#ifdef __LIBVLC__
+#   include <vlc/vlc.h>
+#   include <vlc_md5.h>
+#   include "libmp4.h"
+#   include <vlc_charset.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>
@@ -54,7 +64,7 @@
 #   include <limits.h>
 #endif
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
 #   include <mach/mach.h>
 #   include <IOKit/IOKitLib.h>
 #   include <CoreFoundation/CFNumber.h>
@@ -67,8 +77,7 @@
 #include "drms.h"
 #include "drmstables.h"
 
-#include "libmp4.h"
-
+#if !defined( UNDER_CE )
 /*****************************************************************************
  * aes_s: AES keys structure
  *****************************************************************************
@@ -81,6 +90,9 @@ struct aes_s
     uint32_t pp_dec_keys[ AES_KEY_COUNT + 1 ][ 4 ];
 };
 
+#ifdef __LIBVLC__
+# define Digest DigestMD5
+#else
 /*****************************************************************************
  * md5_s: MD5 message structure
  *****************************************************************************
@@ -93,6 +105,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
@@ -107,18 +120,6 @@ struct shuffle_s
     uint32_t p_bordel[ 16 ];
 };
 
-/*****************************************************************************
- * shuffle_ext_s: extended shuffle structure
- *****************************************************************************
- * This structure stores the static information needed to shuffle data using
- * a custom algorithm.
- *****************************************************************************/
-struct shuffle_ext_s
-{
-    uint32_t * p_bordel;
-    uint32_t i_cmd, i_jc, i_tmp;
-};
-
 #define SWAP( a, b ) { (a) ^= (b); (b) ^= (a); (a) ^= (b); }
 
 /*****************************************************************************
@@ -145,23 +146,28 @@ struct drms_s
 static void InitAES       ( struct aes_s *, uint32_t * );
 static void DecryptAES    ( struct aes_s *, uint32_t *, const uint32_t * );
 
+#ifndef __LIBVLC__
 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 *, uint32_t );
 static void DoShuffle     ( struct shuffle_s *, uint32_t *, uint32_t );
 
 static uint32_t FirstPass ( uint32_t * );
-static void SecondPass    ( struct shuffle_ext_s * );
-static uint32_t ThirdPass ( uint32_t * );
-static void FourthPass    ( uint32_t *, uint32_t );
-static void FifthPass     ( uint32_t * );
-static void BigShuffle1   ( struct shuffle_ext_s * );
-static void BigShuffle2   ( struct shuffle_ext_s * );
+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 );
@@ -279,6 +285,15 @@ void drms_decrypt( void *_p_drms, uint32_t *p_buffer, uint32_t i_bytes )
 
 /*****************************************************************************
  * drms_init: initialise a DRMS structure
+ *****************************************************************************
+ * Return values:
+ *  0: success
+ * -1: unimplemented
+ * -2: invalid argument
+ * -3: could not get system key
+ * -4: could not get SCI data
+ * -5: no user key found in SCI data
+ * -6: invalid user key
  *****************************************************************************/
 int drms_init( void *_p_drms, uint32_t i_type,
                uint8_t *p_info, uint32_t i_len )
@@ -291,7 +306,7 @@ int drms_init( void *_p_drms, uint32_t i_type,
         case FOURCC_user:
             if( i_len < sizeof(p_drms->i_user) )
             {
-                i_ret = -1;
+                i_ret = -2;
                 break;
             }
 
@@ -301,7 +316,7 @@ int drms_init( void *_p_drms, uint32_t i_type,
         case FOURCC_key:
             if( i_len < sizeof(p_drms->i_key) )
             {
-                i_ret = -1;
+                i_ret = -2;
                 break;
             }
 
@@ -311,7 +326,7 @@ int drms_init( void *_p_drms, uint32_t i_type,
         case FOURCC_iviv:
             if( i_len < sizeof(p_drms->p_key) )
             {
-                i_ret = -1;
+                i_ret = -2;
                 break;
             }
 
@@ -319,11 +334,11 @@ int drms_init( void *_p_drms, uint32_t i_type,
             break;
 
         case FOURCC_name:
-            p_drms->p_name = strdup( p_info );
+            p_drms->p_name = (uint8_t*) strdup( (char *)p_info );
 
             if( p_drms->p_name == NULL )
             {
-                i_ret = -1;
+                i_ret = -2;
             }
             break;
 
@@ -334,19 +349,28 @@ int drms_init( void *_p_drms, uint32_t i_type,
 
             if( i_len < 64 )
             {
-                i_ret = -1;
+                i_ret = -2;
                 break;
             }
 
             InitMD5( &md5 );
-            AddMD5( &md5, p_drms->p_name, strlen( p_drms->p_name ) );
+            AddMD5( &md5, p_drms->p_name, strlen( (char *)p_drms->p_name ) );
             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
+            {
+                i_ret = GetUserKey( p_drms, p_drms->p_key );
+                if( i_ret )
+                {
+                    break;
+                }
             }
 
             InitAES( &p_drms->aes, p_drms->p_key );
@@ -358,7 +382,7 @@ int drms_init( void *_p_drms, uint32_t i_type,
 
             if( p_priv[ 0 ] != 0x6e757469 ) /* itun */
             {
-                i_ret = -1;
+                i_ret = -6;
                 break;
             }
 
@@ -480,6 +504,7 @@ static void DecryptAES( struct aes_s *p_aes,
     }
 }
 
+#ifndef __LIBVLC__
 /*****************************************************************************
  * InitMD5: initialise an MD5 message
  *****************************************************************************
@@ -666,6 +691,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
@@ -677,8 +703,8 @@ 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)(*";
+    static char const p_secret2[] = "____v8rhvsaAvOKM____FfUH%798=[;."
+                                    "____f8677680a634____ba87fnOIf)(*";
     unsigned int i;
 
     p_shuffle->i_version = i_version;
@@ -690,8 +716,8 @@ static void InitShuffle( struct shuffle_s *p_shuffle, uint32_t *p_sys_key,
         int32_t i_hash;
 
         InitMD5( &md5 );
-        AddMD5( &md5, (uint8_t *)p_sys_key, 16 );
-        AddMD5( &md5, (uint8_t *)p_secret1, 4 );
+        AddMD5( &md5, (const uint8_t *)p_sys_key, 16 );
+        AddMD5( &md5, (const uint8_t *)p_secret1, 4 );
         EndMD5( &md5 );
 
         p_secret1[ 3 ]++;
@@ -703,10 +729,10 @@ static void InitShuffle( struct shuffle_s *p_shuffle, uint32_t *p_sys_key,
     }
 
     /* Fill p_bordel with completely meaningless initial values. */
+    memcpy( p_shuffle->p_bordel, p_secret2, 64 );
     for( i = 0; i < 4; i++ )
     {
         p_shuffle->p_bordel[ 4 * i ] = U32_AT(p_sys_key + i);
-        memcpy( p_shuffle->p_bordel + 4 * i + 1, p_secret2 + 12 * i, 12 );
         REVERSE( p_shuffle->p_bordel + 4 * i + 1, 3 );
     }
 }
@@ -727,7 +753,7 @@ static void DoShuffle( struct shuffle_s *p_shuffle,
 
     static uint32_t i_secret = 0;
 
-    static uint32_t p_secret1[] =
+    static uint32_t p_secret3[] =
     {
         0xAAAAAAAA, 0x01757700, 0x00554580, 0x01724500, 0x00424580,
         0x01427700, 0x00000080, 0xC1D59D01, 0x80144981, 0x815C8901,
@@ -738,17 +764,17 @@ static void DoShuffle( struct shuffle_s *p_shuffle,
         0x00000080, 0x55555555
     };
 
-    static char p_secret2[] =
+    static char p_secret4[] =
         "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++ )
+        REVERSE( p_secret3, sizeof(p_secret3)/sizeof(p_secret3[ 0 ]) );
+        for( ; p_secret4[ 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 ]);
+            p_secret4[ i_secret ] = ROT13(p_secret4[ i_secret ]);
         }
         i_secret++; /* include zero terminator */
     }
@@ -798,11 +824,11 @@ static void DoShuffle( struct shuffle_s *p_shuffle,
     {
         p_big_bordel[ i ] = U32_AT(p_bordel + i);
     }
-    AddMD5( &md5, (uint8_t *)p_big_bordel, 64 );
+    AddMD5( &md5, (const 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 );
+        AddMD5( &md5, (const uint8_t *)p_secret3, sizeof(p_secret3) );
+        AddMD5( &md5, (const uint8_t *)p_secret4, i_secret );
     }
     EndMD5( &md5 );
 
@@ -821,51 +847,21 @@ static void DoShuffle( struct shuffle_s *p_shuffle,
 static void DoExtShuffle( uint32_t * p_bordel )
 {
     uint32_t i_ret;
-    struct shuffle_ext_s exs;
-    exs.p_bordel = p_bordel;
-
-    exs.i_tmp = FirstPass( p_bordel );
 
-    SecondPass( &exs );
+    i_ret = FirstPass( p_bordel );
 
-    i_ret = ThirdPass( p_bordel );
+    SecondPass( p_bordel, i_ret );
 
-    FourthPass( p_bordel, i_ret );
+    ThirdPass( p_bordel );
 
-    FifthPass( p_bordel );
+    FourthPass( p_bordel );
 }
 
 static uint32_t FirstPass( uint32_t * p_bordel )
 {
-    uint32_t i, j;
-    uint32_t i_cmd, i_ret;
+    uint32_t i, i_cmd, i_ret = 5;
 
-    i_ret = 5;
-    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;
-    }
+    TinyShuffle1( p_bordel );
 
     for( ; ; )
     {
@@ -884,9 +880,7 @@ static uint32_t FirstPass( uint32_t * p_bordel )
 
             if( (p_bordel[ 1 ] + p_bordel[ 2 ]) >= 0x7D0 )
             {
-                i_cmd = ((p_bordel[ 3 ] ^ 0x567F) >> 2) & 7;
-
-                switch( i_cmd )
+                switch( ((p_bordel[ 3 ] ^ 0x567F) >> 2) & 7 )
                 {
                     case 0:
                         for( i = 0; i < 3; i++ )
@@ -1012,305 +1006,150 @@ break2:
             break;
     }
 
-    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;
-    }
+    TinyShuffle2( p_bordel );
 
     return i_ret;
 }
 
-static void SecondPass( struct shuffle_ext_s * p_exs )
+static void SecondPass( uint32_t * p_bordel, uint32_t i_tmp )
 {
-    uint32_t i;
-
-    p_exs->i_cmd = p_exs->p_bordel[ 6 ] + 0x194B;
-    if( p_exs->p_bordel[ 6 ] > 0x2710 )
-    {
-        p_exs->i_cmd >>= 1;
-    }
-
-    switch( p_exs->i_cmd & 3 )
-    {
-        case 1:
-            p_exs->p_bordel[ 3 ] += 0x19FE;
-            break;
-        case 2:
-            p_exs->p_bordel[ 7 ] -= p_exs->p_bordel[ 3 ] >> 2;
-            /* no break */
-        case 0:
-            p_exs->p_bordel[ 5 ] ^= 0x248A;
-            break;
-    }
+    uint32_t i, i_cmd, i_jc = 5;
 
-    p_exs->i_jc = 5;
+    TinyShuffle3( p_bordel );
 
-    for( i = 0, p_exs->i_cmd = 0; i < 16; i++ )
+    for( i = 0, i_cmd = 0; i < 16; i++ )
     {
-        if( p_exs->p_bordel[ i ] > p_exs->p_bordel[ p_exs->i_cmd ] )
+        if( p_bordel[ i ] > p_bordel[ i_cmd ] )
         {
-            p_exs->i_cmd = i;
+            i_cmd = i;
         }
     }
 
-    switch( p_exs->i_cmd )
+    switch( i_cmd )
     {
         case 0:
-            if( p_exs->p_bordel[ 1 ] < p_exs->p_bordel[ 8 ] )
+            if( p_bordel[ 1 ] < p_bordel[ 8 ] )
             {
-                p_exs->p_bordel[ 5 ] += 1;
+                p_bordel[ 5 ] += 1;
             }
             break;
         case 4:
-            if( (p_exs->p_bordel[ 9 ] & 0x7777) == 0x3333 )
+            if( (p_bordel[ 9 ] & 0x7777) == 0x3333 )
             {
-                p_exs->p_bordel[ 5 ] -= 1;
+                p_bordel[ 5 ] -= 1;
             }
             else
             {
-                p_exs->i_jc--;
-                if( p_exs->p_bordel[ 1 ] < p_exs->p_bordel[ 8 ] )
+                i_jc--;
+                if( p_bordel[ 1 ] < p_bordel[ 8 ] )
                 {
-                    p_exs->p_bordel[ 5 ] += 1;
+                    p_bordel[ 5 ] += 1;
                 }
                 break;
             }
             /* no break */
         case 7:
-            p_exs->p_bordel[ 2 ] -= 1;
-            p_exs->p_bordel[ 1 ] -= p_exs->p_bordel[ 5 ];
-            TinyShuffle1( p_exs->p_bordel );
+            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_exs->p_bordel[ 4 ] -= 1;
-            p_exs->p_bordel[ 5 ] += 1;
-            p_exs->p_bordel[ 6 ] -= 1;
-            p_exs->p_bordel[ 7 ] += 1;
+            p_bordel[ 4 ] -= 1;
+            p_bordel[ 5 ] += 1;
+            p_bordel[ 6 ] -= 1;
+            p_bordel[ 7 ] += 1;
             break;
         default:
-            p_exs->p_bordel[ 15 ] ^= 0x18547EFF;
+            p_bordel[ 15 ] ^= 0x18547EFF;
             break;
     }
 
-    BigShuffle1( p_exs );
-}
-
-static void BigShuffle1( struct shuffle_ext_s * p_exs )
-{
-    uint32_t i, j;
-
-    for( i = 0; i < 3; i++ )
+    for( i = 3; i--; )
     {
-        p_exs->i_cmd = p_exs->p_bordel[ 12 ] + p_exs->p_bordel[ 13 ] + p_exs->p_bordel[ 6 ];
-        p_exs->i_cmd -= (((uint32_t)(((uint64_t)p_exs->i_cmd * 0x0CCCCCCCD) >> 32)) >> 2) * 5;
-
-        if( p_exs->i_cmd > 4 )
-        {
-            break;
-        }
-
-        switch( p_exs->i_cmd )
+        switch( ( p_bordel[ 12 ] + p_bordel[ 13 ] + p_bordel[ 6 ] ) % 5 )
         {
             case 0:
-                p_exs->p_bordel[ 12 ] -= 1;
+                p_bordel[ 12 ] -= 1;
                 /* no break */
             case 1:
-                p_exs->p_bordel[ 12 ] -= 1;
-                p_exs->p_bordel[ 13 ] += 1;
+                p_bordel[ 12 ] -= 1;
+                p_bordel[ 13 ] += 1;
                 break;
             case 2:
-                p_exs->p_bordel[ 13 ] += 4;
+                p_bordel[ 13 ] += 4;
                 /* no break */
             case 3:
-                p_exs->p_bordel[ 12 ] -= 1;
+                p_bordel[ 12 ] -= 1;
                 break;
             case 4:
-                if( p_exs->i_jc )
-                {
-                    p_exs->i_jc--;
-                    p_exs->p_bordel[ 5 ] += 1;
-                    p_exs->p_bordel[ 6 ] -= 1;
-                    p_exs->p_bordel[ 7 ] += 1;
-                    BigShuffle1( p_exs );
-                    return;
-                }
+                i_jc--;
+                p_bordel[ 5 ] += 1;
+                p_bordel[ 6 ] -= 1;
+                p_bordel[ 7 ] += 1;
+                i = 3; /* Restart the whole loop */
                 break;
         }
     }
 
-    for( i = 0, j = 0; i < 16; i++ )
-    {
-        if( p_exs->p_bordel[ i ] < p_exs->p_bordel[ j ] )
-        {
-            j = i;
-        }
-    }
-
-    if( (p_exs->p_bordel[ j ] % (j + 1)) > 10 )
-    {
-        p_exs->p_bordel[ 1 ] -= 1;
-
-        p_exs->p_bordel[ 2 ] += 0x13;
-        p_exs->p_bordel[ 12 ] += 1;
-    }
-
-    BigShuffle2( p_exs );
-}
-
-static void BigShuffle2( struct shuffle_ext_s * p_exs )
-{
-    uint32_t i;
-
-    p_exs->p_bordel[ 2 ] &= 0x7F3F;
+    TinyShuffle4( p_bordel );
 
-    for( i = 0; i < 5; i++ )
+    for( ; ; )
     {
-        p_exs->i_cmd = (p_exs->p_bordel[ 2 ] + 0xA) + i;
-        p_exs->i_cmd -= (((uint32_t)(((uint64_t)p_exs->i_cmd * 0x0CCCCCCCD) >> 32)) >> 2) * 5;
+        TinyShuffle5( p_bordel );
 
-        if( p_exs->i_cmd > 4 )
-        {
-            continue;
-        }
-
-        switch( p_exs->i_cmd )
+        switch( ( p_bordel[ 2 ] * 2 + 15 ) % 5 )
         {
             case 0:
-                p_exs->p_bordel[ 12 ] &= p_exs->p_bordel[ 2 ];
-                /* no break */
-            case 1:
-                p_exs->p_bordel[ 3 ] ^= p_exs->p_bordel[ 15 ];
+                if( ( p_bordel[ 3 ] + i_tmp ) <=
+                    ( p_bordel[ 1 ] + p_bordel[ 15 ] ) )
+                {
+                    p_bordel[ 3 ] += 1;
+                }
                 break;
-            case 2:
-                p_exs->p_bordel[ 15 ] += 0x576;
-                /* no break */
-            case 3:
-                p_exs->p_bordel[ 7 ] -= 0x2D;
-                /* no break */
             case 4:
-                p_exs->p_bordel[ 1 ] <<= 1;
+                p_bordel[ 10 ] -= 0x13;
+                break;
+            case 3:
+                p_bordel[ 5 ] >>= 2;
                 break;
         }
-    }
 
-    p_exs->i_cmd = (p_exs->p_bordel[ 2 ] * 2) + 15;
-    p_exs->i_cmd -= (((uint32_t)(((uint64_t)p_exs->i_cmd * 0x0CCCCCCCD) >> 32)) >> 2) * 5;
-
-    switch( p_exs->i_cmd )
-    {
-        case 0:
-            if( ( p_exs->p_bordel[ 3 ] + p_exs->i_tmp ) <=
-                ( p_exs->p_bordel[ 1 ] + p_exs->p_bordel[ 15 ] ) )
-            {
-                p_exs->p_bordel[ 3 ] += 1;
-            }
-            break;
-        case 3:
-            p_exs->p_bordel[ 5 ] >>= 2;
-            break;
-        case 4:
-            p_exs->p_bordel[ 10 ] -= 0x13;
-            break;
-    }
-
-    p_exs->i_cmd = ((p_exs->p_bordel[ 2 ] * 2) + 10) >> 1;
-    if( p_exs->p_bordel[ 2 ] & 1 )
-    {
-        if( p_exs->i_jc )
+        if( !( p_bordel[ 2 ] & 1 ) || i_jc == 0 )
         {
-            p_exs->i_jc--;
-            p_exs->p_bordel[ 2 ] += 0x13;
-            p_exs->p_bordel[ 12 ] += 1;
-            BigShuffle2( p_exs );
-            return;
+            break;
         }
-    }
 
-    p_exs->i_cmd -= p_exs->p_bordel[ 2 ];
-    p_exs->i_cmd -= (((uint32_t)(((uint64_t)p_exs->i_cmd * 0x0CCCCCCCD) >> 32)) >> 3) * 10;
-
-    switch( p_exs->i_cmd )
-    {
-        case 0:
-            for( i = 0; i < 5; i++ )
-            {
-                if( ( p_exs->p_bordel[ 1 ] & p_exs->p_bordel[ 2 ] ) >
-                    ( p_exs->p_bordel[ 7 ] & p_exs->p_bordel[ 12 ] ) )
-                {
-                    p_exs->p_bordel[ 2 ] += 1;
-                    p_exs->p_bordel[ 7 ] ^= p_exs->p_bordel[ 2 ];
-                }
-            }
-        case 1:
-            p_exs->p_bordel[ 3 ] -= 1;
-            p_exs->p_bordel[ 4 ] |= 0x400000;
-            break;
-        case 2:
-            if( p_exs->p_bordel[ 13 ] >= p_exs->p_bordel[ 3 ] )
-            {
-                if( p_exs->i_jc )
-                {
-                    p_exs->i_jc--;
-                    p_exs->p_bordel[ 5 ] += 1;
-                    p_exs->p_bordel[ 6 ] -= 1;
-                    p_exs->p_bordel[ 7 ] += 1;
-                    BigShuffle1( p_exs );
-                }
-                return;
-            }
-            else
-            {
-                p_exs->p_bordel[ 5 ] += 3;
-            }
-            break;
-        case 3:
-            p_exs->p_bordel[ 1 ] ^= (p_exs->p_bordel[ 5 ] + p_exs->p_bordel[ 15 ]);
-            break;
-        case 4:
-            p_exs->p_bordel[ 14 ] += 1;
-            break;
-        case 5:
-            p_exs->p_bordel[ 2 ] &= 0x10076000;
-            break;
-        case 6:
-            p_exs->p_bordel[ 1 ] -= p_exs->p_bordel[ 5 ];
-            /* no break */
-        case 7:
-            TinyShuffle1( p_exs->p_bordel );
-            break;
-        case 8:
-            p_exs->p_bordel[ 7 ] ^= ((p_exs->p_bordel[ 1 ] + p_exs->p_bordel[ 5 ]) - p_exs->p_bordel[ 8 ]);
-            break;
-        case 9:
-            if( (p_exs->p_bordel[ 1 ] ^ p_exs->p_bordel[ 10 ]) > 0x6000 )
-            {
-                p_exs->p_bordel[ 11 ] += 1;
-            }
-            break;
+        i_jc--;
+        p_bordel[ 2 ] += 0x13;
+        p_bordel[ 12 ] += 1;
     }
+
+    p_bordel[ 2 ] &= 0x10076000;
 }
 
-static uint32_t ThirdPass( uint32_t * p_bordel )
+static void ThirdPass( uint32_t * p_bordel )
 {
-    uint32_t i_cmd, i_ret = 5;
+    uint32_t i_cmd;
 
     i_cmd = ((p_bordel[ 7 ] + p_bordel[ 14 ] + 10) >> 1) - p_bordel[ 14 ];
-    i_cmd -= (((uint32_t)(((uint64_t)i_cmd * 0x0CCCCCCCD) >> 32)) >> 3) * 10;
+    i_cmd = i_cmd % 10;
 
     switch( i_cmd )
     {
@@ -1321,16 +1160,16 @@ static uint32_t ThirdPass( uint32_t * p_bordel )
             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:
-            while( i_cmd )
-            {
-                i_cmd -= 1;
-                p_bordel[ i_cmd ] += p_bordel[ i_cmd + 3 ];
-            }
-            i_ret--;
-            TinyShuffle2( p_bordel );
-            return i_ret;
+            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 )
             {
@@ -1348,8 +1187,7 @@ static uint32_t ThirdPass( uint32_t * p_bordel )
             break;
         case 8:
             p_bordel[ 0 ] -= p_bordel[ 11 ] & p_bordel[ 15 ];
-            i_ret--;
-            return i_ret;
+            return; /* jc = 4 */
         case 9:
             p_bordel[ 6 ] >>= (p_bordel[ 14 ] & 3);
             break;
@@ -1357,29 +1195,18 @@ static uint32_t ThirdPass( uint32_t * p_bordel )
 
     SWAP( p_bordel[ 0 ], p_bordel[ 10 ] );
 
-    TinyShuffle2( p_bordel );
+    TinyShuffle6( p_bordel );
 
-    return i_ret;
+    return; /* jc = 5 */
 }
 
-static void FourthPass( uint32_t * p_bordel, uint32_t i_jc )
+static void FourthPass( uint32_t * p_bordel )
 {
     uint32_t i, j;
-    uint32_t i_cmd;
-
-    i = (((p_bordel[ 9 ] + p_bordel[ 15 ] + 12) >> 2) - p_bordel[ 4 ]) & 7;
 
-    while( i-- )
-    {
-        SWAP( p_bordel[ i ], p_bordel[ i + 3 ] );
-    }
+    TinyShuffle7( p_bordel );
 
-    SWAP( p_bordel[ 10 ], p_bordel[ 1 ] );
-
-    i_cmd = p_bordel[ 5 ];
-    i_cmd -= (((uint32_t)(((uint64_t)i_cmd * 0x0CCCCCCCD) >> 32)) >> 2) * 5;
-
-    switch( i_cmd )
+    switch( p_bordel[ 5 ] % 5)
     {
         case 0:
             p_bordel[ 0 ] += 1;
@@ -1388,26 +1215,18 @@ static void FourthPass( uint32_t * p_bordel, uint32_t i_jc )
             p_bordel[ 11 ] ^= (p_bordel[ 3 ] + p_bordel[ 6 ] + p_bordel[ 8 ]);
             break;
         case 3:
-            for( i = 4; i < 15; i++ )
+            for( i = 4; i < 15 && (p_bordel[ i ] & 5) == 0; i++ )
             {
-                if( p_bordel[ i ] & 5 )
-                {
-                    break;
-                }
-
                 SWAP( p_bordel[ i ], p_bordel[ 15 - i ] );
             }
             break;
         case 4:
-            if( i_jc )
-            {
-                p_bordel[ 12 ] -= 1;
-                p_bordel[ 13 ] += 1;
-                p_bordel[ 2 ] -= 0x64;
-                p_bordel[ 3 ] += 0x64;
-                return;
-            }
-            break;
+            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++ )
@@ -1418,108 +1237,256 @@ static void FourthPass( uint32_t * p_bordel, uint32_t i_jc )
         }
     }
 
-    i_cmd = p_bordel[ j ];
-    i_cmd -= (((uint32_t)(((uint64_t)i_cmd * 0x51EB851F) >> 32)) >> 5) * 0x64;
-
-    switch( i_cmd )
+    switch( p_bordel[ j ] % 100 )
     {
-        case 0x0:
-            SWAP( p_bordel[ j ], p_bordel[ 0 ] );
+        case 0:
+            SWAP( p_bordel[ 0 ], p_bordel[ j ] );
             break;
-        case 0x8:
+        case 8:
             p_bordel[ 1 ] >>= 1;
             p_bordel[ 2 ] <<= 1;
             p_bordel[ 14 ] >>= 3;
             p_bordel[ 15 ] <<= 4;
             break;
-        case 0x39:
+        case 57:
             p_bordel[ j ] += p_bordel[ 13 ];
             break;
-        case 0x4c:
-            if( i_jc )
-            {
-                p_bordel[ 1 ] += 0x20E;
-                p_bordel[ 5 ] += 0x223D;
-                p_bordel[ 13 ] -= 0x576;
-                p_bordel[ 15 ] += 0x576;
-            }
-            break;
-        case 0x5b:
+        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 0x63:
+        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 FifthPass( uint32_t * p_bordel )
+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;
-    uint32_t i_cmd;
 
-    i = (p_bordel[ 0 ] & p_bordel[ 6 ]) & 0xF;
+    p_bordel[ 2 ] &= 0x7F3F;
 
-    i_cmd = p_bordel[ i ];
-    i_cmd -= ((((uint32_t)(((uint64_t)i_cmd * 0x10624DD3) >> 32)) >> 6) * 0x3E8);
+    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;
+        }
+    }
+}
 
-    switch( i_cmd )
+static void TinyShuffle6( uint32_t * p_bordel )
+{
+    uint32_t i, j;
+
+    for( i = 0; i < 8; i++ )
     {
-        case 0x7:
+        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 0x13:
+        case 19:
             p_bordel[ 15 ] &= 0x5555;
             break;
-        case 0x5d:
+        case 93:
             p_bordel[ i ] ^= p_bordel[ 15 ];
             break;
-        case 0x64:
-            SWAP( p_bordel[ 0 ], p_bordel[ 1 ] );
-            SWAP( p_bordel[ 1 ], p_bordel[ 7 ] );
+        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[ 8 ], p_bordel[ 5 ] );
-            SWAP( p_bordel[ 6 ], p_bordel[ 1 ] );
-            SWAP( p_bordel[ 3 ], p_bordel[ 0 ] );
+            SWAP( p_bordel[ 5 ], p_bordel[ 8 ] );
+            SWAP( p_bordel[ 6 ], p_bordel[ 7 ] );
             SWAP( p_bordel[ 13 ], p_bordel[ 14 ] );
             break;
-        case 0x149:
+        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 0x237:
+        case 567:
             p_bordel[ 12 ] -= p_bordel[ i ];
             p_bordel[ 13 ] += p_bordel[ i ];
             break;
-        case 0x264:
+        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 0x2f2:
+        case 754:
             i = __MIN( i, 12 );
             p_bordel[ i + 1 ] >>= 1;
             p_bordel[ i + 2 ] <<= 4;
             p_bordel[ i + 3 ] >>= 3;
             break;
-        case 0x309:
+        case 777:
             p_bordel[ 1 ] += 0x20E;
             p_bordel[ 5 ] += 0x223D;
             p_bordel[ 13 ] -= 0x576;
             p_bordel[ 15 ] += 0x576;
             break;
-        case 0x3d5:
+        case 981:
             if( (p_bordel[ i ] ^ 0x8765F441) < 0x2710 )
             {
                 SWAP( p_bordel[ 0 ], p_bordel[ 1 ] );
@@ -1532,47 +1499,6 @@ static void FifthPass( uint32_t * p_bordel )
     }
 }
 
-static void TinyShuffle1( uint32_t * p_bordel )
-{
-    uint32_t i;
-
-    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;
-        }
-    }
-}
-
-static void TinyShuffle2( uint32_t * p_bordel )
-{
-    uint32_t i;
-
-    for( i = 0; i < 8; i++ )
-    {
-        if( p_bordel[ 3 ] & 0x7514 )
-        {
-            SWAP( p_bordel[ i ], p_bordel[ i + 5 ] );
-        }
-        else
-        {
-            SWAP( p_bordel[ i ], p_bordel[ i + 7 ] );
-        }
-    }
-}
-
 /*****************************************************************************
  * GetSystemKey: get the system key
  *****************************************************************************
@@ -1580,8 +1506,8 @@ static void TinyShuffle2( uint32_t * p_bordel )
  *****************************************************************************/
 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";
+    static char const p_secret5[ 8 ] = "YuaFlafu";
+    static char const p_secret6[ 8 ] = "zPif98ga";
     struct md5_s md5;
     int64_t i_ipod_id;
     uint32_t p_system_hash[ 4 ];
@@ -1596,21 +1522,21 @@ static int GetSystemKey( uint32_t *p_sys_key, vlc_bool_t b_ipod )
     /* Combine our system info hash with additional secret data. The resulting
      * MD5 hash will be our system key. */
     InitMD5( &md5 );
-    AddMD5( &md5, p_secret1, 8 );
+    AddMD5( &md5, (const uint8_t*)p_secret5, 8 );
 
     if( !b_ipod )
     {
-        AddMD5( &md5, (uint8_t *)p_system_hash, 6 );
-        AddMD5( &md5, (uint8_t *)p_system_hash, 6 );
-        AddMD5( &md5, (uint8_t *)p_system_hash, 6 );
-        AddMD5( &md5, p_secret2, 8 );
+        AddMD5( &md5, (const uint8_t *)p_system_hash, 6 );
+        AddMD5( &md5, (const uint8_t *)p_system_hash, 6 );
+        AddMD5( &md5, (const uint8_t *)p_system_hash, 6 );
+        AddMD5( &md5, (const uint8_t *)p_secret6, 8 );
     }
     else
     {
         i_ipod_id = U64_AT(&i_ipod_id);
-        AddMD5( &md5, (uint8_t *)&i_ipod_id, sizeof(i_ipod_id) );
-        AddMD5( &md5, (uint8_t *)&i_ipod_id, sizeof(i_ipod_id) );
-        AddMD5( &md5, (uint8_t *)&i_ipod_id, sizeof(i_ipod_id) );
+        AddMD5( &md5, (const uint8_t *)&i_ipod_id, sizeof(i_ipod_id) );
+        AddMD5( &md5, (const uint8_t *)&i_ipod_id, sizeof(i_ipod_id) );
+        AddMD5( &md5, (const uint8_t *)&i_ipod_id, sizeof(i_ipod_id) );
     }
 
     EndMD5( &md5 );
@@ -1655,7 +1581,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 = utf8_fopen( psz_path, "wb" );
         if( file != NULL )
         {
             i_ret = fwrite( p_user_key, sizeof(uint32_t),
@@ -1683,7 +1609,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 = utf8_fopen( psz_path, "rb" );
     if( file != NULL )
     {
         i_ret = fread( p_user_key, sizeof(uint32_t),
@@ -1703,21 +1629,21 @@ static int ReadUserKey( void *_p_drms, uint32_t *p_user_key )
  *****************************************************************************/
 static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
 {
-    static char const p_secret[] = "mUfnpognadfgf873";
+    static char const p_secret7[] = "mUfnpognadfgf873";
     struct drms_s *p_drms = (struct drms_s *)_p_drms;
     struct aes_s aes;
     struct shuffle_s shuffle;
     uint32_t i, y;
-    uint32_t *p_sci_data;
+    uint32_t *p_sci_data = NULL;
     uint32_t i_user, i_key;
     uint32_t p_sys_key[ 4 ];
-    uint32_t i_sci_size, i_blocks, i_remaining;
+    uint32_t i_sci_size = 0, i_blocks, i_remaining;
     uint32_t *p_sci0, *p_sci1, *p_buffer;
     uint32_t p_sci_key[ 4 ];
     char *psz_ipod;
-    int i_ret = -1;
+    int i_ret = -5;
 
-    if( !ReadUserKey( p_drms, p_user_key ) )
+    if( ReadUserKey( p_drms, p_user_key ) == 0 )
     {
         REVERSE( p_user_key, 4 );
         return 0;
@@ -1727,12 +1653,12 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
 
     if( GetSystemKey( p_sys_key, psz_ipod ? VLC_TRUE : VLC_FALSE ) )
     {
-        return -1;
+        return -3;
     }
 
     if( GetSCIData( psz_ipod, &p_sci_data, &i_sci_size ) )
     {
-        return -1;
+        return -4;
     }
 
     /* Phase 1: unscramble the SCI data using the system key and shuffle
@@ -1746,9 +1672,10 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
     /* Decrypt and shuffle our data at the same time */
     InitAES( &aes, p_sys_key );
     REVERSE( p_sys_key, 4 );
+    REVERSE( p_sci_data, 1 );
     InitShuffle( &shuffle, p_sys_key, p_sci_data[ 0 ] );
 
-    memcpy( p_sci_key, p_secret, 16 );
+    memcpy( p_sci_key, p_secret7, 16 );
     REVERSE( p_sci_key, 4 );
 
     while( i_blocks-- )
@@ -1773,9 +1700,8 @@ static int GetUserKey( void *_p_drms, uint32_t *p_user_key )
 
     if( i_remaining >= 4 )
     {
-        i_remaining /= 4;
-        REVERSE( p_buffer, i_remaining );
-        DoShuffle( &shuffle, p_buffer, i_remaining );
+        REVERSE( p_buffer, i_remaining / 4 );
+        DoShuffle( &shuffle, p_buffer, i_remaining / 4 );
     }
 
     /* Phase 2: look for the user key in the generated data. I must admit I
@@ -1847,32 +1773,41 @@ static int GetSCIData( char *psz_ipod, uint32_t **pp_sci,
 {
     FILE *file;
     char *psz_path = NULL;
-    char p_tmp[ PATH_MAX ];
+    char p_tmp[ 4 * PATH_MAX ];
     int i_ret = -1;
 
     if( psz_ipod == NULL )
     {
 #ifdef WIN32
-        char *p_filename = "\\Apple Computer\\iTunes\\SC Info\\SC Info.sidb";
+        const wchar_t *wfile =
+                L"\\Apple Computer\\iTunes\\SC Info\\SC Info.sidb";
         typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
-                                                   LPSTR );
+                                                   LPWSTR );
         HINSTANCE shfolder_dll = NULL;
         SHGETFOLDERPATH dSHGetFolderPath = NULL;
+        wchar_t wpath[PATH_MAX];
 
         if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
         {
             dSHGetFolderPath =
                 (SHGETFOLDERPATH)GetProcAddress( shfolder_dll,
-                                                 _T("SHGetFolderPathA") );
+                                                 _T("SHGetFolderPathW") );
         }
 
         if( dSHGetFolderPath != NULL &&
             SUCCEEDED( dSHGetFolderPath( NULL, CSIDL_COMMON_APPDATA,
-                                         NULL, 0, p_tmp ) ) )
+                                         NULL, 0, wpath ) ) )
         {
-            strncat( p_tmp, p_filename, min( strlen( p_filename ),
-                     (sizeof(p_tmp)/sizeof(p_tmp[0]) - 1) -
-                     strlen( p_tmp ) ) );
+            if (wcslen( wpath ) + wcslen( wfile ) >= PATH_MAX )
+            {
+                return -1;
+            }
+            wcscat( wpath, wfile );
+
+            psz_path = FromWide( wpath );
+            strncpy( p_tmp, psz_path, sizeof( p_tmp ) - 1 );
+            p_tmp[sizeof( p_tmp ) - 1] = '\0';
+            free( psz_path );
             psz_path = p_tmp;
         }
 
@@ -1887,7 +1822,7 @@ static int GetSCIData( char *psz_ipod, uint32_t **pp_sci,
 #define ISCINFO "iSCInfo"
         if( strstr( psz_ipod, ISCINFO ) == NULL )
         {
-            snprintf( p_tmp, sizeof(p_tmp)/sizeof(p_tmp[0]) - 1,
+            snprintf( p_tmp, sizeof(p_tmp) - 1,
                       "%s/iPod_Control/iTunes/" ISCINFO "2", psz_ipod );
             psz_path = p_tmp;
         }
@@ -1902,12 +1837,12 @@ static int GetSCIData( char *psz_ipod, uint32_t **pp_sci,
         return -1;
     }
 
-    file = fopen( psz_path, "r" );
+    file = utf8_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 )
@@ -1975,7 +1910,7 @@ static int HashSystemInfo( uint32_t *p_system_hash )
 
     GetVolumeInformation( _T("C:\\"), NULL, 0, &i_serial,
                           NULL, NULL, NULL, 0 );
-    AddMD5( &md5, (uint8_t *)&i_serial, 4 );
+    AddMD5( &md5, (const uint8_t *)&i_serial, 4 );
 
     for( i = 0; i < sizeof(p_reg_keys) / sizeof(p_reg_keys[ 0 ]); i++ )
     {
@@ -2000,7 +1935,7 @@ static int HashSystemInfo( uint32_t *p_system_hash )
                                  NULL, NULL, p_reg_buf,
                                  &i_size ) == ERROR_SUCCESS )
             {
-                AddMD5( &md5, (uint8_t *)p_reg_buf, i_size );
+                AddMD5( &md5, (const uint8_t *)p_reg_buf, i_size );
             }
 
             free( p_reg_buf );
@@ -2039,25 +1974,35 @@ static int GetiPodID( int64_t *p_ipod_id )
         return 0;
     }
 
-#ifdef SYS_DARWIN
+#ifdef __APPLE__
     CFTypeRef value;
     mach_port_t port;
     io_object_t device;
     io_iterator_t iterator;
-    CFMutableDictionaryRef matching_dic;
+    CFMutableDictionaryRef match_dic;
+    CFMutableDictionaryRef smatch_dic;
 
     if( IOMasterPort( MACH_PORT_NULL, &port ) == KERN_SUCCESS )
     {
-        if( ( matching_dic = IOServiceMatching( "IOFireWireUnit" ) ) != NULL )
+        smatch_dic = IOServiceMatching( "IOFireWireUnit" );
+        match_dic = CFDictionaryCreateMutable( kCFAllocatorDefault, 0,
+                                           &kCFTypeDictionaryKeyCallBacks,
+                                           &kCFTypeDictionaryValueCallBacks );
+
+        if( smatch_dic != NULL && match_dic != NULL )
         {
-            CFDictionarySetValue( matching_dic,
+            CFDictionarySetValue( smatch_dic,
                                   CFSTR("FireWire Vendor Name"),
                                   CFSTR(VENDOR_NAME) );
-            CFDictionarySetValue( matching_dic,
+            CFDictionarySetValue( smatch_dic,
                                   CFSTR("FireWire Product Name"),
                                   CFSTR(PROD_NAME) );
 
-            if( IOServiceGetMatchingServices( port, matching_dic,
+            CFDictionarySetValue( match_dic,
+                                  CFSTR(kIOPropertyMatchKey),
+                                  smatch_dic );
+
+            if( IOServiceGetMatchingServices( port, match_dic,
                                               &iterator ) == KERN_SUCCESS )
             {
                 while( ( device = IOIteratorNext( iterator ) ) != NULL )
@@ -2092,7 +2037,7 @@ static int GetiPodID( int64_t *p_ipod_id )
         mach_port_deallocate( mach_task_self(), port );
     }
 
-#elif HAVE_SYSFS_LIBSYSFS_H
+#elif defined (HAVE_SYSFS_LIBSYSFS_H)
     struct sysfs_bus *bus = NULL;
     struct dlist *devlist = NULL;
     struct dlist *attributes = NULL;
@@ -2122,7 +2067,7 @@ static int GetiPodID( int64_t *p_ipod_id )
                             break;
                         }
                     }
-                }
+               }
 
                 if( !i_ret ) break;
             }
@@ -2135,3 +2080,11 @@ static int GetiPodID( int64_t *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 ) */