2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-04 03:10:52 +00:00

Use anonymous namespaces for file-private classes

This commit is contained in:
Jay Berkenbilt 2022-04-16 13:21:57 -04:00
parent 38edca820e
commit 75fe4f60c3
15 changed files with 435 additions and 362 deletions

View File

@ -6,7 +6,8 @@
qpdf 11.
* Perform code cleanup including some source-compatible but not
binary compatible changes to function signatures.
binary compatible changes to function signatures, use of anonymous
namespaces, and use of "= default" and "= delete" in declarations.
2022-04-09 Jay Berkenbilt <ejb@ql.org>

2
TODO
View File

@ -479,8 +479,6 @@ This is a list of changes to make next time there is an ABI change.
Comments appear in the code prefixed by "ABI". Always Search for ABI
in source and header files to find items not listed here.
* See where anonymous namespaces can be used to keep things private to
a source file. Search for `(class|struct)` in **/*.cc.
* Having QPDFObjectHandle setters return Class& to allow for
use of fluent interfaces. This includes array and dictionary
mutators.

View File

@ -14,12 +14,15 @@
# error "qpdf does not support libjpeg built with BITS_IN_JSAMPLE != 8"
#endif
struct qpdf_jpeg_error_mgr
namespace
{
struct qpdf_jpeg_error_mgr
{
struct jpeg_error_mgr pub;
jmp_buf jmpbuf;
std::string msg;
};
};
} // namespace
static void
error_handler(j_common_ptr cinfo)
@ -147,13 +150,16 @@ Pl_DCT::finish()
}
}
struct dct_pipeline_dest
namespace
{
struct dct_pipeline_dest
{
struct jpeg_destination_mgr pub; /* public fields */
unsigned char* buffer;
size_t size;
Pipeline* next;
};
};
} // namespace
static void
init_pipeline_destination(j_compress_ptr)

View File

@ -50,8 +50,10 @@ static char const* EMPTY_PDF = (
"110\n"
"%%EOF\n");
class InvalidInputSource: public InputSource
namespace
{
class InvalidInputSource: public InputSource
{
public:
virtual ~InvalidInputSource() = default;
virtual qpdf_offset_t
@ -99,11 +101,13 @@ class InvalidInputSource: public InputSource
throwException()
{
throw std::logic_error(
"QPDF operation attempted on a QPDF object with no input source."
"QPDF operation attempted on a QPDF object with no input "
"source."
" QPDF operations are invalid before processFile (or another"
" process method) or after closeInputSource");
}
};
};
} // namespace
QPDF::ForeignStreamData::ForeignStreamData(
std::shared_ptr<EncryptionParameters> encp,

View File

@ -543,20 +543,25 @@ QPDFAcroFormDocumentHelper::adjustInheritedFields(
}
}
class ResourceReplacer: public QPDFObjectHandle::TokenFilter
namespace
{
class ResourceReplacer: public QPDFObjectHandle::TokenFilter
{
public:
ResourceReplacer(
std::map<std::string, std::map<std::string, std::string>> const& dr_map,
std::map<std::string, std::map<std::string, std::set<size_t>>> const&
rnames);
std::map<std::string, std::map<std::string, std::string>> const&
dr_map,
std::map<
std::string,
std::map<std::string, std::set<size_t>>> const& rnames);
virtual ~ResourceReplacer() = default;
virtual void handleToken(QPDFTokenizer::Token const&) override;
private:
size_t offset;
std::map<std::string, std::map<size_t, std::string>> to_replace;
};
};
} // namespace
ResourceReplacer::ResourceReplacer(
std::map<std::string, std::map<std::string, std::string>> const& dr_map,

View File

@ -508,8 +508,10 @@ QPDFFormFieldObjectHelper::generateAppearance(QPDFAnnotationObjectHelper& aoh)
}
}
class ValueSetter: public QPDFObjectHandle::TokenFilter
namespace
{
class ValueSetter: public QPDFObjectHandle::TokenFilter
{
public:
ValueSetter(
std::string const& DA,
@ -530,7 +532,8 @@ class ValueSetter: public QPDFObjectHandle::TokenFilter
QPDFObjectHandle::Rectangle bbox;
enum { st_top, st_bmc, st_emc, st_end } state;
bool replaced;
};
};
} // namespace
ValueSetter::ValueSetter(
std::string const& DA,
@ -701,8 +704,10 @@ ValueSetter::writeAppearance()
write("ET\nQ\nEMC");
}
class TfFinder: public QPDFObjectHandle::TokenFilter
namespace
{
class TfFinder: public QPDFObjectHandle::TokenFilter
{
public:
TfFinder();
virtual ~TfFinder()
@ -721,7 +726,8 @@ class TfFinder: public QPDFObjectHandle::TokenFilter
int last_num_idx;
std::string last_name;
std::vector<std::string> DA;
};
};
} // namespace
TfFinder::TfFinder() :
tf(11.0),

View File

@ -2,8 +2,10 @@
#include <qpdf/NNTree.hh>
class NameTreeDetails: public NNTreeDetails
namespace
{
class NameTreeDetails: public NNTreeDetails
{
public:
virtual std::string const&
itemsKey() const override
@ -27,7 +29,8 @@ class NameTreeDetails: public NNTreeDetails
auto bs = b.getUTF8Value();
return ((as < bs) ? -1 : (as > bs) ? 1 : 0);
}
};
};
} // namespace
static NameTreeDetails name_tree_details;

View File

@ -3,8 +3,10 @@
#include <qpdf/NNTree.hh>
#include <qpdf/QIntC.hh>
class NumberTreeDetails: public NNTreeDetails
namespace
{
class NumberTreeDetails: public NNTreeDetails
{
public:
virtual std::string const&
itemsKey() const override
@ -28,7 +30,8 @@ class NumberTreeDetails: public NNTreeDetails
auto bs = b.getIntValue();
return ((as < bs) ? -1 : (as > bs) ? 1 : 0);
}
};
};
} // namespace
static NumberTreeDetails number_tree_details;

View File

@ -32,9 +32,12 @@
#include <stdexcept>
#include <stdlib.h>
class TerminateParsing
namespace
{
};
class TerminateParsing
{
};
} // namespace
QPDFObjectHandle::StreamDataProvider::StreamDataProvider(bool supports_retry) :
supports_retry(supports_retry)
@ -74,8 +77,10 @@ QPDFObjectHandle::StreamDataProvider::supportsRetry()
return this->supports_retry;
}
class CoalesceProvider: public QPDFObjectHandle::StreamDataProvider
namespace
{
class CoalesceProvider: public QPDFObjectHandle::StreamDataProvider
{
public:
CoalesceProvider(
QPDFObjectHandle containing_page, QPDFObjectHandle old_contents) :
@ -90,7 +95,8 @@ class CoalesceProvider: public QPDFObjectHandle::StreamDataProvider
private:
QPDFObjectHandle containing_page;
QPDFObjectHandle old_contents;
};
};
} // namespace
void
CoalesceProvider::provideStreamData(int, int, Pipeline* p)
@ -167,8 +173,10 @@ QPDFObjectHandle::ParserCallbacks::terminateParsing()
throw TerminateParsing();
}
class LastChar: public Pipeline
namespace
{
class LastChar: public Pipeline
{
public:
LastChar(Pipeline* next);
virtual ~LastChar() = default;
@ -178,7 +186,8 @@ class LastChar: public Pipeline
private:
unsigned char last_char;
};
};
} // namespace
LastChar::LastChar(Pipeline* next) :
Pipeline("lastchar", next),
@ -293,9 +302,11 @@ QPDFObjectHandle::getTypeName()
}
}
template <class T>
class QPDFObjectTypeAccessor
namespace
{
template <class T>
class QPDFObjectTypeAccessor
{
public:
static bool
check(QPDFObject* o)
@ -307,7 +318,8 @@ class QPDFObjectTypeAccessor
{
return (o && dynamic_cast<T const*>(o));
}
};
};
} // namespace
bool
QPDFObjectHandle::isBool()
@ -1435,8 +1447,10 @@ QPDFObjectHandle::replaceStreamData(
provider, filter, decode_parms);
}
class FunctionProvider: public QPDFObjectHandle::StreamDataProvider
namespace
{
class FunctionProvider: public QPDFObjectHandle::StreamDataProvider
{
public:
FunctionProvider(std::function<void(Pipeline*)> provider) :
StreamDataProvider(false),
@ -1459,8 +1473,11 @@ class FunctionProvider: public QPDFObjectHandle::StreamDataProvider
virtual bool
provideStreamData(
int, int, Pipeline* pipeline, bool suppress_warnings, bool will_retry)
override
int,
int,
Pipeline* pipeline,
bool suppress_warnings,
bool will_retry) override
{
return p2(pipeline, suppress_warnings, will_retry);
}
@ -1468,7 +1485,8 @@ class FunctionProvider: public QPDFObjectHandle::StreamDataProvider
private:
std::function<void(Pipeline*)> p1;
std::function<bool(Pipeline*, bool, bool)> p2;
};
};
} // namespace
void
QPDFObjectHandle::replaceStreamData(

View File

@ -11,8 +11,10 @@
#include <qpdf/QUtil.hh>
#include <qpdf/ResourceFinder.hh>
class ContentProvider: public QPDFObjectHandle::StreamDataProvider
namespace
{
class ContentProvider: public QPDFObjectHandle::StreamDataProvider
{
public:
ContentProvider(QPDFObjectHandle from_page) :
from_page(from_page)
@ -24,7 +26,8 @@ class ContentProvider: public QPDFObjectHandle::StreamDataProvider
private:
QPDFObjectHandle from_page;
};
};
} // namespace
void
ContentProvider::provideStreamData(int, int, Pipeline* p)
@ -39,8 +42,10 @@ ContentProvider::provideStreamData(int, int, Pipeline* p)
concat.manualFinish();
}
class InlineImageTracker: public QPDFObjectHandle::TokenFilter
namespace
{
class InlineImageTracker: public QPDFObjectHandle::TokenFilter
{
public:
InlineImageTracker(QPDF*, size_t min_size, QPDFObjectHandle resources);
virtual ~InlineImageTracker() = default;
@ -55,7 +60,8 @@ class InlineImageTracker: public QPDFObjectHandle::TokenFilter
int min_suffix;
bool any_images;
enum { st_top, st_bi } state;
};
};
} // namespace
InlineImageTracker::InlineImageTracker(
QPDF* qpdf, size_t min_size, QPDFObjectHandle resources) :

View File

@ -20,8 +20,10 @@ is_delimiter(char ch)
return (strchr(" \t\n\v\f\r()<>[]{}/%", ch) != 0);
}
class QPDFWordTokenFinder: public InputSource::Finder
namespace
{
class QPDFWordTokenFinder: public InputSource::Finder
{
public:
QPDFWordTokenFinder(
std::shared_ptr<InputSource> is, std::string const& str) :
@ -35,7 +37,8 @@ class QPDFWordTokenFinder: public InputSource::Finder
private:
std::shared_ptr<InputSource> is;
std::string str;
};
};
} // namespace
bool
QPDFWordTokenFinder::check()

View File

@ -19,8 +19,10 @@
#include <stdexcept>
class SF_Crypt: public QPDFStreamFilter
namespace
{
class SF_Crypt: public QPDFStreamFilter
{
public:
SF_Crypt() = default;
virtual ~SF_Crypt() = default;
@ -35,7 +37,8 @@ class SF_Crypt: public QPDFStreamFilter
for (auto const& key : decode_parms.getKeys()) {
if (((key == "/Type") || (key == "/Name")) &&
((!decode_parms.hasKey("/Type")) ||
decode_parms.isDictionaryOfType("/CryptFilterDecodeParms"))) {
decode_parms.isDictionaryOfType(
"/CryptFilterDecodeParms"))) {
// we handle this in decryptStream
} else {
filterable = false;
@ -50,7 +53,8 @@ class SF_Crypt: public QPDFStreamFilter
// Not used -- handled by pipeStreamData
return nullptr;
}
};
};
} // namespace
std::map<std::string, std::string> QPDF_Stream::filter_abbreviations = {
// The PDF specification provides these filter abbreviations for

View File

@ -251,8 +251,10 @@ static unsigned short mac_roman_to_unicode[] = {
0x02c7, // 0xff
};
class FileCloser
namespace
{
class FileCloser
{
public:
FileCloser(FILE* f) :
f(f)
@ -266,7 +268,8 @@ class FileCloser
private:
FILE* f;
};
};
} // namespace
template <typename T>
static std::string
@ -1052,8 +1055,10 @@ QUtil::toUTF16(unsigned long uval)
// Random data support
class RandomDataProviderProvider
namespace
{
class RandomDataProviderProvider
{
public:
RandomDataProviderProvider();
void setProvider(RandomDataProvider*);
@ -1062,7 +1067,8 @@ class RandomDataProviderProvider
private:
RandomDataProvider* default_provider;
RandomDataProvider* current_provider;
};
};
} // namespace
RandomDataProviderProvider::RandomDataProviderProvider() :
default_provider(CryptoRandomDataProvider::getInstance()),

View File

@ -31,13 +31,19 @@ SecureRandomDataProvider::getInstance()
# ifdef _WIN32
class WindowsCryptProvider
namespace
{
class WindowsCryptProvider
{
public:
WindowsCryptProvider()
{
if (!CryptAcquireContextW(
&crypt_prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
&crypt_prov,
NULL,
NULL,
PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
throw std::runtime_error(
"unable to acquire crypt context: " + getErrorMessage());
}
@ -68,10 +74,11 @@ class WindowsCryptProvider
std::string message(messageBuffer, size);
LocalFree(messageBuffer);
return (
"error number " + QUtil::int_to_string_base(errorMessageID, 16) +
": " + message);
"error number " +
QUtil::int_to_string_base(errorMessageID, 16) + ": " + message);
}
};
};
} // namespace
# endif
void

View File

@ -60,8 +60,10 @@ _qpdf_data::_qpdf_data() :
{
}
class ProgressReporter: public QPDFWriter::ProgressReporter
namespace
{
class ProgressReporter: public QPDFWriter::ProgressReporter
{
public:
ProgressReporter(void (*handler)(int, void*), void* data);
virtual ~ProgressReporter() = default;
@ -70,7 +72,8 @@ class ProgressReporter: public QPDFWriter::ProgressReporter
private:
void (*handler)(int, void*);
void* data;
};
};
} // namespace
ProgressReporter::ProgressReporter(void (*handler)(int, void*), void* data) :
handler(handler),