From f0307891048688780525c1f43a4a04ccf788ffb3 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Mon, 7 Mar 2022 17:55:11 -0500 Subject: [PATCH] Rename bits_include.cc to qpdf/bits_functions.hh --- libqpdf/BitStream.cc | 4 ++-- libqpdf/BitWriter.cc | 4 ++-- libqpdf/bits_include.cc | 155 ----------------------------------------------------------------------------------------------------------------------------------------------------------- libqpdf/qpdf/bits_functions.hh | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ libtests/bits.cc | 4 ++-- libtests/build.mk | 2 +- 6 files changed, 160 insertions(+), 162 deletions(-) delete mode 100644 libqpdf/bits_include.cc create mode 100644 libqpdf/qpdf/bits_functions.hh diff --git a/libqpdf/BitStream.cc b/libqpdf/BitStream.cc index 3b8e702..d0e991d 100644 --- a/libqpdf/BitStream.cc +++ b/libqpdf/BitStream.cc @@ -2,9 +2,9 @@ #include -// See comments in bits_include.cc +// See comments in bits_functions.hh #define BITS_READ 1 -#include "bits_include.cc" +#include BitStream::BitStream(unsigned char const* p, size_t nbytes) : start(p), diff --git a/libqpdf/BitWriter.cc b/libqpdf/BitWriter.cc index 81c47db..9d32473 100644 --- a/libqpdf/BitWriter.cc +++ b/libqpdf/BitWriter.cc @@ -1,8 +1,8 @@ #include -// See comments in bits_include.cc +// See comments in bits_functions.hh #define BITS_WRITE 1 -#include "bits_include.cc" +#include BitWriter::BitWriter(Pipeline* pl) : pl(pl), diff --git a/libqpdf/bits_include.cc b/libqpdf/bits_include.cc deleted file mode 100644 index 0f52b4e..0000000 --- a/libqpdf/bits_include.cc +++ /dev/null @@ -1,155 +0,0 @@ -// This file is #included in other source files. - -#ifndef __BITS_CC__ -#define __BITS_CC__ - -#include -#include -#include -#include -#include - -// These functions may be run at places where the function call -// overhead from test coverage testing would be too high. Therefore, -// we make the test coverage cases conditional upon a preprocessor -// symbol. BitStream.cc includes this file without defining the -// symbol, and the specially designed test code that fully exercises -// this code includes with the symbol defined. - -#ifdef BITS_READ -static unsigned long long -read_bits(unsigned char const*& p, size_t& bit_offset, - size_t& bits_available, size_t bits_wanted) -{ - // View p as a stream of bits: - - // 76543210 76543210 .... - - // bit_offset is the bit number within the first byte that marks - // the first bit that we would read. - - if (bits_wanted > bits_available) - { - throw std::runtime_error( - "overflow reading bit stream: wanted = " + - QUtil::uint_to_string(bits_wanted) + "; available = " + - QUtil::uint_to_string(bits_available)); - } - if (bits_wanted > 32) - { - throw std::out_of_range("read_bits: too many bits requested"); - } - - unsigned long result = 0; -#ifdef BITS_TESTING - if (bits_wanted == 0) - { - QTC::TC("libtests", "bits zero bits wanted"); - } -#endif - while (bits_wanted > 0) - { - // Grab bits from the first byte clearing anything before - // bit_offset. - unsigned char byte = static_cast( - *p & ((1U << (bit_offset + 1U)) - 1U)); - - // There are bit_offset + 1 bits available in the first byte. - size_t to_copy = std::min(bits_wanted, bit_offset + 1); - size_t leftover = (bit_offset + 1) - to_copy; - -#ifdef BITS_TESTING - QTC::TC("libtests", "bits bit_offset", - ((bit_offset == 0) ? 0 : - (bit_offset == 7) ? 1 : - 2)); - QTC::TC("libtests", "bits leftover", (leftover > 0) ? 1 : 0); -#endif - - // Right shift so that all the bits we want are right justified. - byte = static_cast(byte >> leftover); - - // Copy the bits into result - result <<= to_copy; - result |= byte; - - // Update pointers - if (leftover) - { - bit_offset = leftover - 1; - } - else - { - bit_offset = 7; - ++p; - } - bits_wanted -= to_copy; - bits_available -= to_copy; - -#ifdef BITS_TESTING - QTC::TC("libtests", "bits iterations", - ((bits_wanted > 8) ? 0 : - (bits_wanted > 0) ? 1 : - 2)); -#endif - } - - return result; -} -#endif - -#ifdef BITS_WRITE -static void -write_bits(unsigned char& ch, size_t& bit_offset, - unsigned long long val, size_t bits, Pipeline* pipeline) -{ - if (bits > 32) - { - throw std::out_of_range("write_bits: too many bits requested"); - } - - // bit_offset + 1 is the number of bits left in ch -#ifdef BITS_TESTING - if (bits == 0) - { - QTC::TC("libtests", "bits write zero bits"); - } -#endif - while (bits > 0) - { - size_t bits_to_write = std::min(bits, bit_offset + 1); - unsigned char newval = static_cast( - (val >> (bits - bits_to_write)) & ((1U << bits_to_write) - 1)); - size_t bits_left_in_ch = bit_offset + 1 - bits_to_write; - newval = static_cast(newval << bits_left_in_ch); - ch |= newval; - if (bits_left_in_ch == 0) - { -#ifdef BITS_TESTING - QTC::TC("libtests", "bits write pipeline"); -#endif - pipeline->write(&ch, 1); - bit_offset = 7; - ch = 0; - } - else - { -#ifdef BITS_TESTING - QTC::TC("libtests", "bits write leftover"); -#endif - bit_offset -= bits_to_write; - } - bits -= bits_to_write; -#ifdef BITS_TESTING - QTC::TC("libtests", "bits write iterations", - ((bits > 8) ? 0 : - (bits > 0) ? 1 : - 2)); -#endif - } - -} -#endif - - -#endif // __BITS_CC__ diff --git a/libqpdf/qpdf/bits_functions.hh b/libqpdf/qpdf/bits_functions.hh new file mode 100644 index 0000000..c00a0bc --- /dev/null +++ b/libqpdf/qpdf/bits_functions.hh @@ -0,0 +1,153 @@ +#ifndef __BITS_FUNCTIONS_HH__ +#define __BITS_FUNCTIONS_HH__ + +#include +#include +#include +#include +#include + +// This file is #included by specific source files, which must define +// certain preprocessor symbols. These functions may be run at places +// where the function call overhead from test coverage testing would +// be too high. Therefore, we make the test coverage cases conditional +// upon a preprocessor symbol. Library code includes this file without +// BITS_TESTING, and the specially designed test code that fully +// exercises this code includes with the symbol defined. + +#ifdef BITS_READ +static unsigned long long +read_bits(unsigned char const*& p, size_t& bit_offset, + size_t& bits_available, size_t bits_wanted) +{ + // View p as a stream of bits: + + // 76543210 76543210 .... + + // bit_offset is the bit number within the first byte that marks + // the first bit that we would read. + + if (bits_wanted > bits_available) + { + throw std::runtime_error( + "overflow reading bit stream: wanted = " + + QUtil::uint_to_string(bits_wanted) + "; available = " + + QUtil::uint_to_string(bits_available)); + } + if (bits_wanted > 32) + { + throw std::out_of_range("read_bits: too many bits requested"); + } + + unsigned long result = 0; +#ifdef BITS_TESTING + if (bits_wanted == 0) + { + QTC::TC("libtests", "bits zero bits wanted"); + } +#endif + while (bits_wanted > 0) + { + // Grab bits from the first byte clearing anything before + // bit_offset. + unsigned char byte = static_cast( + *p & ((1U << (bit_offset + 1U)) - 1U)); + + // There are bit_offset + 1 bits available in the first byte. + size_t to_copy = std::min(bits_wanted, bit_offset + 1); + size_t leftover = (bit_offset + 1) - to_copy; + +#ifdef BITS_TESTING + QTC::TC("libtests", "bits bit_offset", + ((bit_offset == 0) ? 0 : + (bit_offset == 7) ? 1 : + 2)); + QTC::TC("libtests", "bits leftover", (leftover > 0) ? 1 : 0); +#endif + + // Right shift so that all the bits we want are right justified. + byte = static_cast(byte >> leftover); + + // Copy the bits into result + result <<= to_copy; + result |= byte; + + // Update pointers + if (leftover) + { + bit_offset = leftover - 1; + } + else + { + bit_offset = 7; + ++p; + } + bits_wanted -= to_copy; + bits_available -= to_copy; + +#ifdef BITS_TESTING + QTC::TC("libtests", "bits iterations", + ((bits_wanted > 8) ? 0 : + (bits_wanted > 0) ? 1 : + 2)); +#endif + } + + return result; +} +#endif + +#ifdef BITS_WRITE +static void +write_bits(unsigned char& ch, size_t& bit_offset, + unsigned long long val, size_t bits, Pipeline* pipeline) +{ + if (bits > 32) + { + throw std::out_of_range("write_bits: too many bits requested"); + } + + // bit_offset + 1 is the number of bits left in ch +#ifdef BITS_TESTING + if (bits == 0) + { + QTC::TC("libtests", "bits write zero bits"); + } +#endif + while (bits > 0) + { + size_t bits_to_write = std::min(bits, bit_offset + 1); + unsigned char newval = static_cast( + (val >> (bits - bits_to_write)) & ((1U << bits_to_write) - 1)); + size_t bits_left_in_ch = bit_offset + 1 - bits_to_write; + newval = static_cast(newval << bits_left_in_ch); + ch |= newval; + if (bits_left_in_ch == 0) + { +#ifdef BITS_TESTING + QTC::TC("libtests", "bits write pipeline"); +#endif + pipeline->write(&ch, 1); + bit_offset = 7; + ch = 0; + } + else + { +#ifdef BITS_TESTING + QTC::TC("libtests", "bits write leftover"); +#endif + bit_offset -= bits_to_write; + } + bits -= bits_to_write; +#ifdef BITS_TESTING + QTC::TC("libtests", "bits write iterations", + ((bits > 8) ? 0 : + (bits > 0) ? 1 : + 2)); +#endif + } + +} +#endif + +#endif // __BITS_FUNCTIONS_HH__ diff --git a/libtests/bits.cc b/libtests/bits.cc index 81b4456..2c3dc34 100644 --- a/libtests/bits.cc +++ b/libtests/bits.cc @@ -7,11 +7,11 @@ #include #include -// See comments in bits_include.cc +// See comments in bits_functions.hh #define BITS_TESTING 1 #define BITS_READ 1 #define BITS_WRITE 1 -#include "../libqpdf/bits_include.cc" +#include static void print_values(long long byte_offset, size_t bit_offset, diff --git a/libtests/build.mk b/libtests/build.mk index 0b9f6fa..6d3371f 100644 --- a/libtests/build.mk +++ b/libtests/build.mk @@ -38,7 +38,7 @@ $(TARGETS_libtests): $(TARGETS_libqpdf) $(TARGETS_qpdf) INCLUDES_libtests = include libqpdf -TC_SRCS_libtests = $(wildcard libqpdf/*.cc) $(wildcard libtests/*.cc) +TC_SRCS_libtests = $(wildcard libqpdf/*.cc) $(wildcard libtests/*.cc) libqpdf/qpdf/bits_functions.hh # ----- -- libgit2 0.21.4