summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2018-02-16 23:25:27 +0100
committerJay Berkenbilt <ejb@ql.org>2018-02-19 03:06:27 +0100
commitd0e99f195a987c483bbb6c5449cf39bee34e08a1 (patch)
treecead8acd60cd14fd5d904ed380c750540cb361f3 /include
parentc2e16827b69f3d3ac3721cfcd608b87f28e2a13f (diff)
downloadqpdf-d0e99f195a987c483bbb6c5449cf39bee34e08a1.tar.zst
More robust handling of type errors
Give objects descriptions and context so it is possible to issue warnings instead of fatal errors for attempts to access objects of the wrong type.
Diffstat (limited to 'include')
-rw-r--r--include/qpdf/QPDF.hh1
-rw-r--r--include/qpdf/QPDFObject.hh21
-rw-r--r--include/qpdf/QPDFObjectHandle.hh32
3 files changed, 49 insertions, 5 deletions
diff --git a/include/qpdf/QPDF.hh b/include/qpdf/QPDF.hh
index 70bfac3e..7da150f1 100644
--- a/include/qpdf/QPDF.hh
+++ b/include/qpdf/QPDF.hh
@@ -703,7 +703,6 @@ class QPDF
PointerHolder<InputSource> input, int objid, int generation,
qpdf_offset_t stream_offset);
QPDFTokenizer::Token readToken(PointerHolder<InputSource>,
- bool allow_bad = false,
size_t max_len = 0);
QPDFObjectHandle readObjectAtOffset(
diff --git a/include/qpdf/QPDFObject.hh b/include/qpdf/QPDFObject.hh
index 8d479b3c..da54c027 100644
--- a/include/qpdf/QPDFObject.hh
+++ b/include/qpdf/QPDFObject.hh
@@ -23,6 +23,7 @@
#define __QPDFOBJECT_HH__
#include <qpdf/DLL.h>
+#include <qpdf/PointerHolder.hh>
#include <string>
@@ -32,6 +33,7 @@ class QPDFObjectHandle;
class QPDFObject
{
public:
+ QPDFObject();
// Objects derived from QPDFObject are accessible through
// QPDFObjectHandle. Each object returns a unique type code that
@@ -84,8 +86,27 @@ class QPDFObject
};
friend class ObjAccessor;
+ virtual void setDescription(QPDF*, std::string const&);
+ bool getDescription(QPDF*&, std::string&);
+ bool hasDescription();
+
protected:
virtual void releaseResolved() {}
+
+ private:
+ QPDFObject(QPDFObject const&);
+ QPDFObject& operator=(QPDFObject const&);
+ class Members
+ {
+ friend class QPDFObject;
+ public:
+ ~Members();
+ private:
+ Members();
+ QPDF* owning_qpdf;
+ std::string object_description;
+ };
+ PointerHolder<Members> m;
};
#endif // __QPDFOBJECT_HH__
diff --git a/include/qpdf/QPDFObjectHandle.hh b/include/qpdf/QPDFObjectHandle.hh
index d12fe87d..53b219ce 100644
--- a/include/qpdf/QPDFObjectHandle.hh
+++ b/include/qpdf/QPDFObjectHandle.hh
@@ -398,6 +398,21 @@ class QPDFObjectHandle
QPDF_DLL
static QPDFObjectHandle newReserved(QPDF* qpdf);
+ // Provide an owning qpdf and object description. The library does
+ // this automatically with objects that are read from from the
+ // input PDF and with objects that are created programmatically
+ // and inserted into the QPDF by adding them to an array or a
+ // dictionary or creating a new indirect object. Most end user
+ // code will not need to call this. If an object has an owning
+ // qpdf and object description, it enables qpdf to give warnings
+ // with proper context in some cases where it would otherwise
+ // raise exceptions.
+ QPDF_DLL
+ void setObjectDescription(QPDF* owning_qpdf,
+ std::string const& object_description);
+ QPDF_DLL
+ bool hasObjectDescription();
+
// Accessor methods. If an accessor method that is valid for only
// a particular object type is called on an object of the wrong
// type, an exception is thrown.
@@ -498,7 +513,7 @@ class QPDFObjectHandle
// Replace value of key, adding it if it does not exist
QPDF_DLL
- void replaceKey(std::string const& key, QPDFObjectHandle const&);
+ void replaceKey(std::string const& key, QPDFObjectHandle);
// Remove key, doing nothing if key does not exist
QPDF_DLL
void removeKey(std::string const& key);
@@ -769,7 +784,10 @@ class QPDFObjectHandle
};
friend class ReleaseResolver;
- // Convenience routine: Throws if the assumption is violated.
+ // Convenience routine: Throws if the assumption is violated. Your
+ // code will be better if you call one of the isType methods and
+ // handle the case of the type being wrong, but these can be
+ // convenient if you have already verified the type.
QPDF_DLL
void assertInitialized() const;
@@ -832,10 +850,16 @@ class QPDFObjectHandle
QPDF* qpdf, int objid, int generation,
QPDFObjectHandle stream_dict, qpdf_offset_t offset, size_t length);
- void assertType(char const* type_name, bool istype) const;
+ void typeWarning(char const* expected_type,
+ std::string const& warning);
+ void objectWarning(std::string const& warning);
+ void assertType(char const* type_name, bool istype);
void dereference();
void makeDirectInternal(std::set<int>& visited);
void releaseResolved();
+ static void setObjectDescriptionFromInput(
+ QPDFObjectHandle, QPDF*, std::string const&,
+ PointerHolder<InputSource>, qpdf_offset_t);
static QPDFObjectHandle parseInternal(
PointerHolder<InputSource> input,
std::string const& object_description,
@@ -868,7 +892,7 @@ class QPDFObjectHandle
bool initialized;
- QPDF* qpdf; // 0 for direct object
+ QPDF* qpdf;
int objid; // 0 for direct object
int generation;
PointerHolder<QPDFObject> obj;