<commit_before>/*****************************************************************************
 * libcsa.c: CSA scrambler/descrambler
 *****************************************************************************
 * Copyright (C) 2004 <NAME>
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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.
 *****************************************************************************/

/*
 * XXX: A great part is just a copy/past of deCSA but I can't find the
 * 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"

struct csa_t
{
    /* odd and even keys */
    uint8_t o_ck[8];
    uint8_t e_ck[8];

    uint8_t o_kk[57];
    uint8_t e_kk[57];

    /* cypher state */
    int     A[11];
    int     B[11];
    int     X, Y, Z;
    int     D, E, F;
    int     p, q, r;
};

static void csa_ComputeKey( uint8_t kk[57], uint8_t ck[8] );

static void csa_StreamCypher( csa_t *c, int b_init, uint8_t *ck, uint8_t *sb, uint8_t *cb );

static void csa_BlockDecypher( uint8_t kk[57], uint8_t ib[8], uint8_t bd[8] );
static void csa_BlockCypher( uint8_t kk[57], uint8_t bd[8], uint8_t ib[8] );

/*****************************************************************************
 * csa_New:
 *****************************************************************************/
csa_t *csa_New()
{
    csa_t *c = malloc( sizeof( csa_t ) );
    memset( c, 0, sizeof( csa_t ) );

    return c;
}

/*****************************************************************************
 * csa_Delete:
 *****************************************************************************/
void   csa_Delete( csa_t *c )
{
    free( c );
}

/*****************************************************************************
 * csa_SetCW:
 *****************************************************************************/
void csa_SetCW( csa_t *c, uint8_t o_ck[8], uint8_t e_ck[8] )
{
    memcpy( c->o_ck, o_ck, 8 );
    csa_ComputeKey( c->o_kk, o_ck );

    memcpy( c->e_ck, e_ck, 8 );
    csa_ComputeKey( c->e_kk, e_ck );
}

/*****************************************************************************
 * csa_Decrypt:
 *****************************************************************************/
void csa_Decrypt( csa_t *c, uint8_t *pkt )
{
    uint8_t *ck;
    uint8_t *kk;

    uint8_t  ib[8], stream[8], block[8];

    int     i_hdr, i_residue;
    int     i, j, n;

    /* transport scrambling control */
    if( (pkt[3]&0x80) == 0 )
    {
        /* not scrambled */
        return;
    }
    if( pkt[3]&0x40 )
    {
        ck = c->o_ck;
        kk = c->o_kk;
    }
    else
    {
        ck = c->e_ck;
        kk = c->e_kk;
    }

    /* clear transport scrambling control */
    pkt[3] &= 0x3f;

    i_hdr = 4;
    if( pkt[3]&0x20 )
    {
        /* skip adaption field */
        i_hdr += pkt[4] + 1;
    }
<commit_msg> * csa: decrypt only when payload >= 8 bytes.<commit_after>/*****************************************************************************
 * libcsa.c: CSA scrambler/descrambler
 *****************************************************************************
 * Copyright (C) 2004 <NAME>
 * $Id$
 *
 * Authors: <NAME> <<EMAIL>>
 *
 * 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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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.
 *****************************************************************************/

/*
 * XXX: A great part is just a copy/past of deCSA but I can't find the
 * 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"

struct csa_t
{
    /* odd and even keys */
    uint8_t o_ck[8];
    uint8_t e_ck[8];

    uint8_t o_kk[57];
    uint8_t e_kk[57];

    /* cypher state */
    int     A[11];
    int     B[11];
    int     X, Y, Z;
    int     D, E, F;
    int     p, q, r;
};

static void csa_ComputeKey( uint8_t kk[57], uint8_t ck[8] );

static void csa_StreamCypher( csa_t *c, int b_init, uint8_t *ck, uint8_t *sb, uint8_t *cb );

static void csa_BlockDecypher( uint8_t kk[57], uint8_t ib[8], uint8_t bd[8] );
static void csa_BlockCypher( uint8_t kk[57], uint8_t bd[8], uint8_t ib[8] );

/*****************************************************************************
 * csa_New:
 *****************************************************************************/
csa_t *csa_New()
{
    csa_t *c = malloc( sizeof( csa_t ) );
    memset( c, 0, sizeof( csa_t ) );

    return c;
}

/*****************************************************************************
 * csa_Delete:
 *****************************************************************************/
void   csa_Delete( csa_t *c )
{
    free( c );
}

/*****************************************************************************
 * csa_SetCW:
 *****************************************************************************/
void csa_SetCW( csa_t *c, uint8_t o_ck[8], uint8_t e_ck[8] )
{
    memcpy( c->o_ck, o_ck, 8 );
    csa_ComputeKey( c->o_kk, o_ck );

    memcpy( c->e_ck, e_ck, 8 );
    csa_ComputeKey( c->e_kk, e_ck );
}

/*****************************************************************************
 * csa_Decrypt:
 *****************************************************************************/
void csa_Decrypt( csa_t *c, uint8_t *pkt )
{
    uint8_t *ck;
    uint8_t *kk;

    uint8_t  ib[8], stream[8], block[8];

    int     i_hdr, i_residue;
    int     i, j, n;

    /* transport scrambling control */
    if( (pkt[3]&0x80) == 0 )
    {
        /* not scrambled */
        return;
    }
    if( pkt[3]&0x40 )
    {
        ck = c->o_ck;
        kk = c->o_kk;
    }
    else
    {
        ck = c->e_ck;
        kk = c->e_kk;
    }

    /* clear transport scrambling control */
    pkt[3] &= 0x3f;

    i_hdr = 4;
    if( pkt[3]&0x20 )
    {
        /* skip adaption field */
        i_hdr += pkt[4] + 1;
    }

    if( 188 - i_hdr < 8 )
        return;

{'fraction_non_alphanumeric': 0.3324523577688135, 'fraction_numerical': 0.024064543051884824, 'mean_word_length': 3.4741754822650903, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1965327', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2239, 'n_words': 678}