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
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,6 +150,8 @@ Pl_DCT::finish()
}
}
namespace
{
struct dct_pipeline_dest
{
struct jpeg_destination_mgr pub; /* public fields */
@ -154,6 +159,7 @@ struct dct_pipeline_dest
size_t size;
Pipeline* next;
};
} // namespace
static void
init_pipeline_destination(j_compress_ptr)

View File

@ -50,6 +50,8 @@ static char const* EMPTY_PDF = (
"110\n"
"%%EOF\n");
namespace
{
class InvalidInputSource: public InputSource
{
public:
@ -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,13 +543,17 @@ QPDFAcroFormDocumentHelper::adjustInheritedFields(
}
}
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;
@ -557,6 +561,7 @@ class ResourceReplacer: public QPDFObjectHandle::TokenFilter
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,6 +508,8 @@ QPDFFormFieldObjectHelper::generateAppearance(QPDFAnnotationObjectHelper& aoh)
}
}
namespace
{
class ValueSetter: public QPDFObjectHandle::TokenFilter
{
public:
@ -531,6 +533,7 @@ class ValueSetter: public QPDFObjectHandle::TokenFilter
enum { st_top, st_bmc, st_emc, st_end } state;
bool replaced;
};
} // namespace
ValueSetter::ValueSetter(
std::string const& DA,
@ -701,6 +704,8 @@ ValueSetter::writeAppearance()
write("ET\nQ\nEMC");
}
namespace
{
class TfFinder: public QPDFObjectHandle::TokenFilter
{
public:
@ -722,6 +727,7 @@ class TfFinder: public QPDFObjectHandle::TokenFilter
std::string last_name;
std::vector<std::string> DA;
};
} // namespace
TfFinder::TfFinder() :
tf(11.0),

View File

@ -2,6 +2,8 @@
#include <qpdf/NNTree.hh>
namespace
{
class NameTreeDetails: public NNTreeDetails
{
public:
@ -28,6 +30,7 @@ class NameTreeDetails: public NNTreeDetails
return ((as < bs) ? -1 : (as > bs) ? 1 : 0);
}
};
} // namespace
static NameTreeDetails name_tree_details;

View File

@ -3,6 +3,8 @@
#include <qpdf/NNTree.hh>
#include <qpdf/QIntC.hh>
namespace
{
class NumberTreeDetails: public NNTreeDetails
{
public:
@ -29,6 +31,7 @@ class NumberTreeDetails: public NNTreeDetails
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>
namespace
{
class TerminateParsing
{
};
} // namespace
QPDFObjectHandle::StreamDataProvider::StreamDataProvider(bool supports_retry) :
supports_retry(supports_retry)
@ -74,6 +77,8 @@ QPDFObjectHandle::StreamDataProvider::supportsRetry()
return this->supports_retry;
}
namespace
{
class CoalesceProvider: public QPDFObjectHandle::StreamDataProvider
{
public:
@ -91,6 +96,7 @@ class CoalesceProvider: public QPDFObjectHandle::StreamDataProvider
QPDFObjectHandle containing_page;
QPDFObjectHandle old_contents;
};
} // namespace
void
CoalesceProvider::provideStreamData(int, int, Pipeline* p)
@ -167,6 +173,8 @@ QPDFObjectHandle::ParserCallbacks::terminateParsing()
throw TerminateParsing();
}
namespace
{
class LastChar: public Pipeline
{
public:
@ -179,6 +187,7 @@ class LastChar: public Pipeline
private:
unsigned char last_char;
};
} // namespace
LastChar::LastChar(Pipeline* next) :
Pipeline("lastchar", next),
@ -293,6 +302,8 @@ QPDFObjectHandle::getTypeName()
}
}
namespace
{
template <class T>
class QPDFObjectTypeAccessor
{
@ -308,6 +319,7 @@ class QPDFObjectTypeAccessor
return (o && dynamic_cast<T const*>(o));
}
};
} // namespace
bool
QPDFObjectHandle::isBool()
@ -1435,6 +1447,8 @@ QPDFObjectHandle::replaceStreamData(
provider, filter, decode_parms);
}
namespace
{
class FunctionProvider: public QPDFObjectHandle::StreamDataProvider
{
public:
@ -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);
}
@ -1469,6 +1486,7 @@ class FunctionProvider: public QPDFObjectHandle::StreamDataProvider
std::function<void(Pipeline*)> p1;
std::function<bool(Pipeline*, bool, bool)> p2;
};
} // namespace
void
QPDFObjectHandle::replaceStreamData(

View File

@ -11,6 +11,8 @@
#include <qpdf/QUtil.hh>
#include <qpdf/ResourceFinder.hh>
namespace
{
class ContentProvider: public QPDFObjectHandle::StreamDataProvider
{
public:
@ -25,6 +27,7 @@ class ContentProvider: public QPDFObjectHandle::StreamDataProvider
private:
QPDFObjectHandle from_page;
};
} // namespace
void
ContentProvider::provideStreamData(int, int, Pipeline* p)
@ -39,6 +42,8 @@ ContentProvider::provideStreamData(int, int, Pipeline* p)
concat.manualFinish();
}
namespace
{
class InlineImageTracker: public QPDFObjectHandle::TokenFilter
{
public:
@ -56,6 +61,7 @@ class InlineImageTracker: public QPDFObjectHandle::TokenFilter
bool any_images;
enum { st_top, st_bi } state;
};
} // namespace
InlineImageTracker::InlineImageTracker(
QPDF* qpdf, size_t min_size, QPDFObjectHandle resources) :

View File

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

View File

@ -19,6 +19,8 @@
#include <stdexcept>
namespace
{
class SF_Crypt: public QPDFStreamFilter
{
public:
@ -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;
@ -51,6 +54,7 @@ class SF_Crypt: public QPDFStreamFilter
return nullptr;
}
};
} // namespace
std::map<std::string, std::string> QPDF_Stream::filter_abbreviations = {
// The PDF specification provides these filter abbreviations for

View File

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

View File

@ -31,13 +31,19 @@ SecureRandomDataProvider::getInstance()
# ifdef _WIN32
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,6 +60,8 @@ _qpdf_data::_qpdf_data() :
{
}
namespace
{
class ProgressReporter: public QPDFWriter::ProgressReporter
{
public:
@ -71,6 +73,7 @@ class ProgressReporter: public QPDFWriter::ProgressReporter
void (*handler)(int, void*);
void* data;
};
} // namespace
ProgressReporter::ProgressReporter(void (*handler)(int, void*), void* data) :
handler(handler),