~akirad/cinecutie/trunk

« back to all changes in this revision

Viewing changes to quicktime/ffmpeg/libavutil/rc4.c

  • Committer: Paolo Rampino
  • Date: 2010-02-17 19:46:21 UTC
  • Revision ID: git-v1:c39ff77ffa6ae08441c12e7d7f54e3897ddde7f1
Initial Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * RC4 encryption/decryption/pseudo-random number generator
 
3
 * Copyright (c) 2007 Reimar Doeffinger
 
4
 *
 
5
 * loosely based on LibTomCrypt by Tom St Denis
 
6
 *
 
7
 * This file is part of FFmpeg.
 
8
 *
 
9
 * FFmpeg is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Lesser General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2.1 of the License, or (at your option) any later version.
 
13
 *
 
14
 * FFmpeg is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Lesser General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with FFmpeg; if not, write to the Free Software
 
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
22
 */
 
23
#include "common.h"
 
24
#include "rc4.h"
 
25
 
 
26
void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
 
27
    int i, j;
 
28
    uint8_t x, y;
 
29
    uint8_t state[256];
 
30
    for (i = 0; i < 256; i++)
 
31
        state[i] = i;
 
32
    y = 0;
 
33
    // j is i % keylen
 
34
    for (j = 0, i = 0; i < 256; i++, j++) {
 
35
        if (j == keylen) j = 0;
 
36
        y += state[i] + key[j];
 
37
        FFSWAP(uint8_t, state[i], state[y]);
 
38
    }
 
39
    // state initialized, now do the real encryption
 
40
    x = 1; y = state[1];
 
41
    while (datalen-- > 0) {
 
42
        uint8_t sum = state[x] + state[y];
 
43
        FFSWAP(uint8_t, state[x], state[y]);
 
44
        *data++ ^= state[sum];
 
45
        x++;
 
46
        y += state[x];
 
47
    }
 
48
}