aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/Pl_RC4.cc
blob: f98c8744c4d7cc6c9c229500d5490046eccfeb89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <qpdf/Pl_RC4.hh>
#include <qpdf/QUtil.hh>

Pl_RC4::Pl_RC4(char const* identifier, Pipeline* next,
	       unsigned char const* key_data, int key_len,
	       int out_bufsize) :
    Pipeline(identifier, next),
    out_bufsize(out_bufsize),
    rc4(key_data, key_len)
{
    this->outbuf = new unsigned char[out_bufsize];
}

Pl_RC4::~Pl_RC4()
{
    if (this->outbuf)
    {
	delete [] this->outbuf;
	this->outbuf = 0;
    }
}

void
Pl_RC4::write(unsigned char* data, int len)
{
    if (this->outbuf == 0)
    {
	throw std::logic_error(
	    this->identifier +
	    ": Pl_RC4: write() called after finish() called");
    }

    int bytes_left = len;
    unsigned char* p = data;

    while (bytes_left > 0)
    {
	int bytes = (bytes_left < this->out_bufsize ? bytes_left : out_bufsize);
	bytes_left -= bytes;
	rc4.process(p, bytes, outbuf);
	p += bytes;
	getNext()->write(outbuf, bytes);
    }
}

void
Pl_RC4::finish()
{
    if (this->outbuf)
    {
	delete [] this->outbuf;
	this->outbuf = 0;
    }
    this->getNext()->finish();
}