2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-05-29 16:30:53 +00:00
qpdf/libqpdf/Pl_SHA2.cc
Jay Berkenbilt cb769c62e5 WHITESPACE ONLY -- expand tabs in source code
This comment expands all tabs using an 8-character tab-width. You
should ignore this commit when using git blame or use git blame -w.

In the early days, I used to use tabs where possible for indentation,
since emacs did this automatically. In recent years, I have switched
to only using spaces, which means qpdf source code has been a mixture
of spaces and tabs. I have avoided cleaning this up because of not
wanting gratuitous whitespaces change to cloud the output of git
blame, but I changed my mind after discussing with users who view qpdf
source code in editors/IDEs that have other tab widths by default and
in light of the fact that I am planning to start applying automatic
code formatting soon.
2022-02-08 11:51:15 -05:00

94 lines
1.8 KiB
C++

#include <qpdf/Pl_SHA2.hh>
#include <stdexcept>
#include <cstdio>
#include <qpdf/PointerHolder.hh>
#include <qpdf/QUtil.hh>
#include <qpdf/QPDFCryptoProvider.hh>
Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) :
Pipeline("sha2", next),
in_progress(false)
{
if (bits)
{
resetBits(bits);
}
}
Pl_SHA2::~Pl_SHA2()
{
}
void
Pl_SHA2::write(unsigned char* buf, size_t len)
{
if (! this->in_progress)
{
this->in_progress = true;
}
// Write in chunks in case len is too big to fit in an int.
// Assume int is at least 32 bits.
static size_t const max_bytes = 1 << 30;
size_t bytes_left = len;
unsigned char* data = buf;
while (bytes_left > 0)
{
size_t bytes = (bytes_left >= max_bytes ? max_bytes : bytes_left);
this->crypto->SHA2_update(data, bytes);
bytes_left -= bytes;
data += bytes;
}
if (this->getNext(true))
{
this->getNext()->write(buf, len);
}
}
void
Pl_SHA2::finish()
{
if (this->getNext(true))
{
this->getNext()->finish();
}
this->crypto->SHA2_finalize();
this->in_progress = false;
}
void
Pl_SHA2::resetBits(int bits)
{
if (this->in_progress)
{
throw std::logic_error(
"bit reset requested for in-progress SHA2 Pipeline");
}
this->crypto = QPDFCryptoProvider::getImpl();
this->crypto->SHA2_init(bits);
}
std::string
Pl_SHA2::getRawDigest()
{
if (this->in_progress)
{
throw std::logic_error(
"digest requested for in-progress SHA2 Pipeline");
}
return this->crypto->SHA2_digest();
}
std::string
Pl_SHA2::getHexDigest()
{
if (this->in_progress)
{
throw std::logic_error(
"digest requested for in-progress SHA2 Pipeline");
}
return QUtil::hex_encode(getRawDigest());
}