aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2018-01-14 00:13:18 +0100
committerJay Berkenbilt <ejb@ql.org>2018-01-14 01:49:42 +0100
commitd9c90497089ae5cf00891d6febfa7f486f021833 (patch)
tree37ff785d288aa4433eba89230ab7c877b573bc91 /libqpdf
parentbf2fb239d7a39255fe122db50dd5d03f9baa25ae (diff)
downloadqpdf-d9c90497089ae5cf00891d6febfa7f486f021833.tar.zst
Add signed support to BitStream and BitWriter
Diffstat (limited to 'libqpdf')
-rw-r--r--libqpdf/BitStream.cc17
-rw-r--r--libqpdf/BitWriter.cc15
-rw-r--r--libqpdf/qpdf/BitStream.hh2
-rw-r--r--libqpdf/qpdf/BitWriter.hh2
4 files changed, 36 insertions, 0 deletions
diff --git a/libqpdf/BitStream.cc b/libqpdf/BitStream.cc
index 14eae55d..bb52af31 100644
--- a/libqpdf/BitStream.cc
+++ b/libqpdf/BitStream.cc
@@ -30,6 +30,23 @@ BitStream::getBits(int nbits)
this->bits_available, nbits);
}
+long long
+BitStream::getBitsSigned(int 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) > 1 << (nbits - 1))
+ {
+ result = static_cast<long long>(bits - (1 << nbits));
+ }
+ else
+ {
+ result = static_cast<long long>(bits);
+ }
+ return result;
+}
+
void
BitStream::skipToNextByte()
{
diff --git a/libqpdf/BitWriter.cc b/libqpdf/BitWriter.cc
index 4fb375cb..7513ac76 100644
--- a/libqpdf/BitWriter.cc
+++ b/libqpdf/BitWriter.cc
@@ -18,6 +18,21 @@ BitWriter::writeBits(unsigned long long val, unsigned int bits)
}
void
+BitWriter::writeBitsSigned(long long val, unsigned int bits)
+{
+ unsigned long long uval = 0;
+ if (val < 0)
+ {
+ uval = static_cast<unsigned long long>((1 << bits) + val);
+ }
+ else
+ {
+ uval = static_cast<unsigned long long>(val);
+ }
+ writeBits(uval, bits);
+}
+
+void
BitWriter::flush()
{
if (bit_offset < 7)
diff --git a/libqpdf/qpdf/BitStream.hh b/libqpdf/qpdf/BitStream.hh
index e45a90ee..8ea8bec4 100644
--- a/libqpdf/qpdf/BitStream.hh
+++ b/libqpdf/qpdf/BitStream.hh
@@ -15,6 +15,8 @@ class BitStream
QPDF_DLL
unsigned long long getBits(int nbits);
QPDF_DLL
+ long long getBitsSigned(int nbits);
+ QPDF_DLL
void skipToNextByte();
private:
diff --git a/libqpdf/qpdf/BitWriter.hh b/libqpdf/qpdf/BitWriter.hh
index 7e3b07a9..3d93c0b7 100644
--- a/libqpdf/qpdf/BitWriter.hh
+++ b/libqpdf/qpdf/BitWriter.hh
@@ -16,6 +16,8 @@ class BitWriter
BitWriter(Pipeline* pl);
QPDF_DLL
void writeBits(unsigned long long val, unsigned int bits);
+ QPDF_DLL
+ void writeBitsSigned(long long val, unsigned int bits);
// Force any partial byte to be written to the pipeline.
QPDF_DLL
void flush();