mirror of
https://github.com/qpdf/qpdf.git
synced 2024-12-23 03:18:59 +00:00
5d4cad9c02
Significantly improve the code's use of off_t for file offsets, size_t for memory sizes, and integer types in cases where there has to be compatibility with external interfaces. Rework sections of the code that would have prevented qpdf from working on files larger than 2 (or maybe 4) GB in size.
56 lines
1.0 KiB
C++
56 lines
1.0 KiB
C++
#include <qpdf/RC4.hh>
|
|
|
|
#include <string.h>
|
|
|
|
static void swap_byte(unsigned char &a, unsigned char &b)
|
|
{
|
|
unsigned char t;
|
|
|
|
t = a;
|
|
a = b;
|
|
b = t;
|
|
}
|
|
|
|
RC4::RC4(unsigned char const* key_data, int key_len)
|
|
{
|
|
if (key_len == -1)
|
|
{
|
|
key_len = (int)strlen((char*)key_data);
|
|
}
|
|
|
|
for (int i = 0; i < 256; ++i)
|
|
{
|
|
key.state[i] = i;
|
|
}
|
|
key.x = 0;
|
|
key.y = 0;
|
|
|
|
int i1 = 0;
|
|
int i2 = 0;
|
|
for (int i = 0; i < 256; ++i)
|
|
{
|
|
i2 = (key_data[i1] + key.state[i] + i2) % 256;
|
|
swap_byte(key.state[i], key.state[i2]);
|
|
i1 = (i1 + 1) % key_len;
|
|
}
|
|
}
|
|
|
|
void
|
|
RC4::process(unsigned char *in_data, int len, unsigned char* out_data)
|
|
{
|
|
if (out_data == 0)
|
|
{
|
|
// Convert in place
|
|
out_data = in_data;
|
|
}
|
|
|
|
for (int i = 0; i < len; ++i)
|
|
{
|
|
key.x = (key.x + 1) % 256;
|
|
key.y = (key.state[key.x] + key.y) % 256;
|
|
swap_byte(key.state[key.x], key.state[key.y]);
|
|
int xor_index = (key.state[key.x] + key.state[key.y]) % 256;
|
|
out_data[i] = in_data[i] ^ key.state[xor_index];
|
|
}
|
|
}
|