2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/BitStream.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// See comments in bits.cc
|
|
|
|
#define BITS_READ 1
|
|
|
|
#include "bits.icc"
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
BitStream::BitStream(unsigned char const* p, size_t nbytes) :
|
2008-04-29 12:55:25 +00:00
|
|
|
start(p),
|
|
|
|
nbytes(nbytes)
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BitStream::reset()
|
|
|
|
{
|
|
|
|
p = start;
|
|
|
|
bit_offset = 7;
|
2019-06-21 03:35:23 +00:00
|
|
|
if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8)
|
2013-10-05 09:51:54 +00:00
|
|
|
{
|
|
|
|
throw std::runtime_error("array too large for bitstream");
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
bits_available = 8 * nbytes;
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
unsigned long long
|
2019-06-21 03:35:23 +00:00
|
|
|
BitStream::getBits(size_t nbits)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
return read_bits(this->p, this->bit_offset,
|
|
|
|
this->bits_available, nbits);
|
|
|
|
}
|
|
|
|
|
2018-01-13 23:13:18 +00:00
|
|
|
long long
|
2019-06-21 03:35:23 +00:00
|
|
|
BitStream::getBitsSigned(size_t nbits)
|
2018-01-13 23:13:18 +00:00
|
|
|
{
|
|
|
|
unsigned long long bits = read_bits(this->p, this->bit_offset,
|
|
|
|
this->bits_available, nbits);
|
|
|
|
long long result = 0;
|
2019-06-21 03:35:23 +00:00
|
|
|
if (static_cast<long long>(bits) > 1LL << (nbits - 1))
|
2018-01-13 23:13:18 +00:00
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
result = static_cast<long long>(bits -(1ULL << nbits));
|
2018-01-13 23:13:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = static_cast<long long>(bits);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
int
|
|
|
|
BitStream::getBitsInt(size_t nbits)
|
|
|
|
{
|
|
|
|
return static_cast<int>(
|
|
|
|
QIntC::to_uint(
|
|
|
|
read_bits(this->p, this->bit_offset,
|
|
|
|
this->bits_available, nbits)));
|
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
BitStream::skipToNextByte()
|
|
|
|
{
|
|
|
|
if (bit_offset != 7)
|
|
|
|
{
|
2019-06-21 03:35:23 +00:00
|
|
|
size_t bits_to_skip = bit_offset + 1;
|
2008-04-29 12:55:25 +00:00
|
|
|
if (bits_available < bits_to_skip)
|
|
|
|
{
|
2009-09-26 18:36:04 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: overflow skipping to next byte in bitstream");
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
bit_offset = 7;
|
|
|
|
++p;
|
|
|
|
bits_available -= bits_to_skip;
|
|
|
|
}
|
|
|
|
}
|