Coverage Report

Created: 2025-12-14 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/RC4_native.cc
Line
Count
Source
1
#include <qpdf/RC4_native.hh>
2
3
#include <qpdf/QIntC.hh>
4
5
#include <string.h>
6
7
static void
8
swap_byte(unsigned char& a, unsigned char& b)
9
1.76G
{
10
1.76G
    unsigned char t;
11
12
1.76G
    t = a;
13
1.76G
    a = b;
14
1.76G
    b = t;
15
1.76G
}
16
17
RC4_native::RC4_native(unsigned char const* key_data, int key_len)
18
1.08M
{
19
1.08M
    if (key_len == -1) {
20
0
        key_len = QIntC::to_int(strlen(reinterpret_cast<char const*>(key_data)));
21
0
    }
22
23
278M
    for (int i = 0; i < 256; ++i) {
24
277M
        key.state[i] = static_cast<unsigned char>(i);
25
277M
    }
26
1.08M
    key.x = 0;
27
1.08M
    key.y = 0;
28
29
1.08M
    int i1 = 0;
30
1.08M
    int i2 = 0;
31
278M
    for (int i = 0; i < 256; ++i) {
32
277M
        i2 = (key_data[i1] + key.state[i] + i2) % 256;
33
277M
        swap_byte(key.state[i], key.state[i2]);
34
277M
        i1 = (i1 + 1) % key_len;
35
277M
    }
36
1.08M
}
37
38
void
39
RC4_native::process(unsigned char const* in_data, size_t len, unsigned char* out_data)
40
1.10M
{
41
1.48G
    for (size_t i = 0; i < len; ++i) {
42
1.48G
        key.x = static_cast<unsigned char>((key.x + 1) % 256);
43
1.48G
        key.y = static_cast<unsigned char>((key.state[key.x] + key.y) % 256);
44
1.48G
        swap_byte(key.state[key.x], key.state[key.y]);
45
1.48G
        int xor_index = (key.state[key.x] + key.state[key.y]) % 256;
46
1.48G
        out_data[i] = in_data[i] ^ key.state[xor_index];
47
1.48G
    }
48
1.10M
}