]> git.sesse.net Git - vlc/blobdiff - modules/mux/mpeg/csa.c
Remove per-ID destination since it is not actually used/usable
[vlc] / modules / mux / mpeg / csa.c
index 16f91a1d23fe02dac6249a9d37726b0cd30f0d2d..2dc84ef4d3f9e9e304ecc90113c378d5b42cd1ba 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * libcsa.c: CSA scrambler/descrambler
  *****************************************************************************
- * Copyright (C) 2004 Laurent Aimar
+ * Copyright (C) 2004-2005 Laurent Aimar
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
+ *          Jean-Paul Saman <jpsaman #_at_# m2x.nl>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,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.
  *****************************************************************************/
 
 /*
@@ -26,7 +27,6 @@
  * author and the license. If there is a problem with it please e-mail me.
  */
 
-#include <stdlib.h>
 #include <vlc/vlc.h>
 
 #include "csa.h"
@@ -58,7 +58,7 @@ static void csa_BlockCypher( uint8_t kk[57], uint8_t bd[8], uint8_t ib[8] );
 /*****************************************************************************
  * csa_New:
  *****************************************************************************/
-csa_t *csa_New()
+csa_t *csa_New( void )
 {
     csa_t *c = malloc( sizeof( csa_t ) );
     memset( c, 0, sizeof( csa_t ) );
@@ -89,7 +89,7 @@ void csa_SetCW( csa_t *c, uint8_t o_ck[8], uint8_t e_ck[8] )
 /*****************************************************************************
  * csa_Decrypt:
  *****************************************************************************/
-void csa_Decrypt( csa_t *c, uint8_t *pkt )
+void csa_Decrypt( csa_t *c, uint8_t *pkt, int i_pkt_size )
 {
     uint8_t *ck;
     uint8_t *kk;
@@ -126,12 +126,18 @@ void csa_Decrypt( csa_t *c, uint8_t *pkt )
         i_hdr += pkt[4] + 1;
     }
 
+    if( 188 - i_hdr < 8 )
+        return;
+
     /* init csa state */
     csa_StreamCypher( c, 1, ck, &pkt[i_hdr], ib );
 
     /* */
-    n = (188 - i_hdr) / 8;
-    i_residue = (188 - i_hdr) % 8;
+    n = (i_pkt_size - i_hdr) / 8;
+    if( n < 0 )
+        return;
+        
+    i_residue = (i_pkt_size - i_hdr) % 8;    
     for( i = 1; i < n + 1; i++ )
     {
         csa_BlockDecypher( kk, ib, block );
@@ -164,7 +170,7 @@ void csa_Decrypt( csa_t *c, uint8_t *pkt )
         csa_StreamCypher( c, 0, ck, NULL, stream );
         for( j = 0; j < i_residue; j++ )
         {
-            pkt[188 - i_residue + j] ^= stream[j];
+            pkt[i_pkt_size - i_residue + j] ^= stream[j];
         }
     }
 }
@@ -172,13 +178,13 @@ void csa_Decrypt( csa_t *c, uint8_t *pkt )
 /*****************************************************************************
  * csa_Encrypt:
  *****************************************************************************/
-void csa_Encrypt( csa_t *c, uint8_t *pkt, int b_odd )
+void csa_Encrypt( csa_t *c, uint8_t *pkt, int i_pkt_size, int b_odd )
 {
     uint8_t *ck;
     uint8_t *kk;
 
     int i, j;
-    int i_hdr;
+    int i_hdr = 4; /* hdr len */
     uint8_t  ib[184/8+2][8], stream[8], block[8];
     int n, i_residue;
 
@@ -207,10 +213,10 @@ void csa_Encrypt( csa_t *c, uint8_t *pkt, int b_odd )
         /* skip adaption field */
         i_hdr += pkt[4] + 1;
     }
-    n = (188 - i_hdr) / 8;
-    i_residue = (188 - i_hdr) % 8;
+    n = (i_pkt_size - i_hdr) / 8;
+    i_residue = (i_pkt_size - i_hdr) % 8;
 
-    if( n == 0 )
+    if( n <= 0 )
     {
         pkt[3] &= 0x3f;
         return;
@@ -250,7 +256,7 @@ void csa_Encrypt( csa_t *c, uint8_t *pkt, int b_odd )
         csa_StreamCypher( c, 0, ck, NULL, stream );
         for( j = 0; j < i_residue; j++ )
         {
-            pkt[188 - i_residue + j] ^= stream[j];
+            pkt[i_pkt_size - i_residue + j] ^= stream[j];
         }
     }
 }