2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-04 20:37:50 +00:00
qpdf/libqpdf/Pl_RC4.cc
Jay Berkenbilt 6c39aa8763 In shippable code, favor smart pointers (fixes #235)
Use PointerHolder in several places where manually memory allocation
and deallocation were being used. This helps to protect against memory
leaks when exceptions are thrown in surprising places.
2019-06-22 16:57:52 -04:00

49 lines
1010 B
C++

#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,
size_t out_bufsize) :
Pipeline(identifier, next),
out_bufsize(out_bufsize),
rc4(key_data, key_len)
{
this->outbuf = PointerHolder<unsigned char>(
true, new unsigned char[out_bufsize]);
}
Pl_RC4::~Pl_RC4()
{
}
void
Pl_RC4::write(unsigned char* data, size_t len)
{
if (this->outbuf.getPointer() == 0)
{
throw std::logic_error(
this->identifier +
": Pl_RC4: write() called after finish() called");
}
size_t bytes_left = len;
unsigned char* p = data;
while (bytes_left > 0)
{
size_t bytes =
(bytes_left < this->out_bufsize ? bytes_left : out_bufsize);
bytes_left -= bytes;
rc4.process(p, bytes, outbuf.getPointer());
p += bytes;
getNext()->write(outbuf.getPointer(), bytes);
}
}
void
Pl_RC4::finish()
{
this->outbuf = 0;
this->getNext()->finish();
}