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
|
|
|
|
2022-03-07 22:55:11 +00:00
|
|
|
// See comments in bits_functions.hh
|
2008-04-29 12:55:25 +00:00
|
|
|
#define BITS_READ 1
|
2022-03-07 22:55:11 +00:00
|
|
|
#include <qpdf/bits_functions.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
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;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QIntC::to_uint(nbytes) > static_cast<unsigned int>(-1) / 8) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::runtime_error("array too large for bitstream");
|
2013-10-05 09:51:54 +00:00
|
|
|
}
|
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
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return read_bits(this->p, this->bit_offset, this->bits_available, nbits);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2023-05-21 17:35:09 +00:00
|
|
|
unsigned long long bits = read_bits(this->p, this->bit_offset, this->bits_available, nbits);
|
2018-01-13 23:13:18 +00:00
|
|
|
long long result = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (static_cast<long long>(bits) > 1LL << (nbits - 1)) {
|
|
|
|
result = static_cast<long long>(bits - (1ULL << nbits));
|
|
|
|
} else {
|
2018-01-13 23:13:18 +00:00
|
|
|
result = static_cast<long long>(bits);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
int
|
|
|
|
BitStream::getBitsInt(size_t nbits)
|
|
|
|
{
|
2022-04-03 20:10:27 +00:00
|
|
|
return static_cast<int>(
|
|
|
|
// line-break
|
2023-05-21 17:35:09 +00:00
|
|
|
QIntC::to_uint(read_bits(this->p, this->bit_offset, this->bits_available, nbits)));
|
2019-06-21 03:35:23 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void
|
|
|
|
BitStream::skipToNextByte()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bit_offset != 7) {
|
2022-02-08 14:18:08 +00:00
|
|
|
size_t bits_to_skip = bit_offset + 1;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (bits_available < bits_to_skip) {
|
2023-05-21 17:35:09 +00:00
|
|
|
throw std::logic_error("INTERNAL ERROR: overflow skipping to next byte in bitstream");
|
2022-02-08 14:18:08 +00:00
|
|
|
}
|
|
|
|
bit_offset = 7;
|
|
|
|
++p;
|
|
|
|
bits_available -= bits_to_skip;
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
}
|