/src/qpdf/libqpdf/RC4_native.cc
Line | Count | Source (jump to first uncovered line) |
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 | 20.7M | { |
10 | 20.7M | unsigned char t; |
11 | | |
12 | 20.7M | t = a; |
13 | 20.7M | a = b; |
14 | 20.7M | b = t; |
15 | 20.7M | } |
16 | | |
17 | | RC4_native::RC4_native(unsigned char const* key_data, int key_len) |
18 | 41.2k | { |
19 | 41.2k | if (key_len == -1) { |
20 | 0 | key_len = QIntC::to_int(strlen(reinterpret_cast<char const*>(key_data))); |
21 | 0 | } |
22 | | |
23 | 10.6M | for (int i = 0; i < 256; ++i) { |
24 | 10.5M | key.state[i] = static_cast<unsigned char>(i); |
25 | 10.5M | } |
26 | 41.2k | key.x = 0; |
27 | 41.2k | key.y = 0; |
28 | | |
29 | 41.2k | int i1 = 0; |
30 | 41.2k | int i2 = 0; |
31 | 10.6M | for (int i = 0; i < 256; ++i) { |
32 | 10.5M | i2 = (key_data[i1] + key.state[i] + i2) % 256; |
33 | 10.5M | swap_byte(key.state[i], key.state[i2]); |
34 | 10.5M | i1 = (i1 + 1) % key_len; |
35 | 10.5M | } |
36 | 41.2k | } |
37 | | |
38 | | void |
39 | | RC4_native::process(unsigned char const* in_data, size_t len, unsigned char* out_data) |
40 | 41.3k | { |
41 | 10.2M | for (size_t i = 0; i < len; ++i) { |
42 | 10.1M | key.x = static_cast<unsigned char>((key.x + 1) % 256); |
43 | 10.1M | key.y = static_cast<unsigned char>((key.state[key.x] + key.y) % 256); |
44 | 10.1M | swap_byte(key.state[key.x], key.state[key.y]); |
45 | 10.1M | int xor_index = (key.state[key.x] + key.state[key.y]) % 256; |
46 | 10.1M | out_data[i] = in_data[i] ^ key.state[xor_index]; |
47 | 10.1M | } |
48 | 41.3k | } |