2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-09 23:00:57 +00:00
qpdf/libqpdf/BitStream.cc
Jay Berkenbilt 77e889495f Update some code manually to get better formatting results
Add comments to force line breaks, parenthesize function arguments
that are contatenated strings, etc. -- these kinds of changes improve
clang-format's results and also cause emacs cc-mode to match
clang-format. After this type of change, most of the time, when
clang-format and emacs disagree, clang-format is better.
2022-04-05 14:56:19 -04:00

70 lines
1.6 KiB
C++

#include <qpdf/BitStream.hh>
#include <qpdf/QIntC.hh>
// See comments in bits_functions.hh
#define BITS_READ 1
#include <qpdf/bits_functions.hh>
BitStream::BitStream(unsigned char const* p, size_t nbytes) :
start(p),
nbytes(nbytes)
{
reset();
}
void
BitStream::reset()
{
p = start;
bit_offset = 7;
if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8) {
throw std::runtime_error("array too large for bitstream");
}
bits_available = 8 * nbytes;
}
unsigned long long
BitStream::getBits(size_t nbits)
{
return read_bits(this->p, this->bit_offset, this->bits_available, nbits);
}
long long
BitStream::getBitsSigned(size_t nbits)
{
unsigned long long bits =
read_bits(this->p, this->bit_offset, this->bits_available, nbits);
long long result = 0;
if (static_cast<long long>(bits) > 1LL << (nbits - 1)) {
result = static_cast<long long>(bits - (1ULL << nbits));
} else {
result = static_cast<long long>(bits);
}
return result;
}
int
BitStream::getBitsInt(size_t nbits)
{
return static_cast<int>(
// line-break
QIntC::to_uint(
read_bits(this->p, this->bit_offset, this->bits_available, nbits)));
}
void
BitStream::skipToNextByte()
{
if (bit_offset != 7) {
size_t bits_to_skip = bit_offset + 1;
if (bits_available < bits_to_skip) {
throw std::logic_error(
"INTERNAL ERROR: overflow skipping to next byte in bitstream");
}
bit_offset = 7;
++p;
bits_available -= bits_to_skip;
}
}