/*****************************************************************************
* 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
*
* 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.
*****************************************************************************/
/*
* 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"
/*****************************************************************************
* csa_New:
*****************************************************************************/
-csa_t *csa_New()
+csa_t *csa_New( void )
{
csa_t *c = malloc( sizeof( csa_t ) );
memset( c, 0, sizeof( csa_t ) );
/*****************************************************************************
* 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;
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 );
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];
}
}
}
/*****************************************************************************
* 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;
/* 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;
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];
}
}
}