LCOV - code coverage report
Current view: top level - src/libavutil - rc4.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 28 96.4 %
Date: 2017-01-21 09:32:20 Functions: 3 3 100.0 %

          Line data    Source code
       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 "avutil.h"
      24             : #include "common.h"
      25             : #include "mem.h"
      26             : #include "rc4.h"
      27             : 
      28         542 : AVRC4 *av_rc4_alloc(void)
      29             : {
      30         542 :     return av_mallocz(sizeof(struct AVRC4));
      31             : }
      32             : 
      33        1084 : int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) {
      34             :     int i, j;
      35             :     uint8_t y;
      36        1084 :     uint8_t *state = r->state;
      37        1084 :     int keylen = key_bits >> 3;
      38        1084 :     if (key_bits & 7)
      39           0 :         return AVERROR(EINVAL);
      40      278588 :     for (i = 0; i < 256; i++)
      41      277504 :         state[i] = i;
      42        1084 :     y = 0;
      43             :     // j is i % keylen
      44      278588 :     for (j = 0, i = 0; i < 256; i++, j++) {
      45      277504 :         if (j == keylen) j = 0;
      46      277504 :         y += state[i] + key[j];
      47      277504 :         FFSWAP(uint8_t, state[i], state[y]);
      48             :     }
      49        1084 :     r->x = 1;
      50        1084 :     r->y = state[1];
      51        1084 :     return 0;
      52             : }
      53             : 
      54        1084 : void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
      55        1084 :     uint8_t x = r->x, y = r->y;
      56        1084 :     uint8_t *state = r->state;
      57      404031 :     while (count-- > 0) {
      58      401863 :         uint8_t sum = state[x] + state[y];
      59      401863 :         FFSWAP(uint8_t, state[x], state[y]);
      60      401863 :         *dst++ = src ? *src++ ^ state[sum] : state[sum];
      61      401863 :         x++;
      62      401863 :         y += state[x];
      63             :     }
      64        1084 :     r->x = x; r->y = y;
      65        1084 : }

Generated by: LCOV version 1.12