2021-01-04 21:01:09 +00:00
|
|
|
// Copyright (c) 2005-2021 Jay Berkenbilt
|
2008-04-29 12:55:25 +00:00
|
|
|
//
|
2017-09-14 16:44:31 +00:00
|
|
|
// This file is part of qpdf.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// Versions of qpdf prior to version 7 were released under the terms
|
|
|
|
// of version 2.0 of the Artistic License. At your option, you may
|
|
|
|
// continue to consider qpdf to be licensed under those terms. Please
|
|
|
|
// see the manual for additional information.
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2018-08-12 18:07:22 +00:00
|
|
|
#ifndef QPDF_HH
|
|
|
|
#define QPDF_HH
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-06-21 17:47:41 +00:00
|
|
|
#include <qpdf/DLL.h>
|
|
|
|
#include <qpdf/Types.h>
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <list>
|
2010-10-01 11:02:35 +00:00
|
|
|
#include <iostream>
|
2019-02-13 09:37:13 +00:00
|
|
|
#include <vector>
|
2020-12-23 11:12:49 +00:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
#include <qpdf/QIntC.hh>
|
2015-11-20 11:58:06 +00:00
|
|
|
#include <qpdf/QPDFExc.hh>
|
2019-02-13 09:38:38 +00:00
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
2013-06-14 15:22:04 +00:00
|
|
|
#include <qpdf/QPDFObjGen.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/QPDFXRefEntry.hh>
|
|
|
|
#include <qpdf/QPDFObjectHandle.hh>
|
|
|
|
#include <qpdf/QPDFTokenizer.hh>
|
2020-12-23 11:12:49 +00:00
|
|
|
#include <qpdf/QPDFStreamFilter.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
#include <qpdf/Buffer.hh>
|
2012-07-21 09:37:14 +00:00
|
|
|
#include <qpdf/InputSource.hh>
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
class QPDF_Stream;
|
|
|
|
class BitStream;
|
|
|
|
class BitWriter;
|
|
|
|
|
2009-10-21 00:27:24 +00:00
|
|
|
class QPDF
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
public:
|
2009-10-24 04:47:17 +00:00
|
|
|
// Get the current version of the QPDF software
|
|
|
|
QPDF_DLL
|
|
|
|
static std::string const& QPDFVersion();
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDF();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
~QPDF();
|
|
|
|
|
|
|
|
// Associate a file with a QPDF object and do initial parsing of
|
|
|
|
// the file. PDF objects are not read until they are needed. A
|
2009-10-04 23:55:02 +00:00
|
|
|
// QPDF object may be associated with only one file in its
|
2008-04-29 12:55:25 +00:00
|
|
|
// lifetime. This method must be called before any methods that
|
|
|
|
// potentially ask for information about the PDF file are called.
|
|
|
|
// Prior to calling this, the only methods that are allowed are
|
2009-09-27 16:01:45 +00:00
|
|
|
// those that set parameters. If the input file is not
|
|
|
|
// encrypted,either a null password or an empty password can be
|
|
|
|
// used. If the file is encrypted, either the user password or
|
2018-01-14 15:17:17 +00:00
|
|
|
// the owner password may be supplied. The method
|
|
|
|
// setPasswordIsHexKey may be called prior to calling this method
|
|
|
|
// or any of the other process methods to force the password to be
|
|
|
|
// interpreted as a raw encryption key. See comments on
|
|
|
|
// setPasswordIsHexKey for more information.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 14:39:04 +00:00
|
|
|
void processFile(char const* filename, char const* password = 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2012-06-21 11:59:56 +00:00
|
|
|
// Parse a PDF from a stdio FILE*. The FILE must be open in
|
|
|
|
// binary mode and must be seekable. It may be open read only.
|
|
|
|
// This works exactly like processFile except that the PDF file is
|
2012-06-23 22:23:06 +00:00
|
|
|
// read from an already opened FILE*. If close_file is true, the
|
|
|
|
// file will be closed at the end. Otherwise, the caller is
|
|
|
|
// responsible for closing the file.
|
2012-06-21 11:59:56 +00:00
|
|
|
QPDF_DLL
|
2012-06-23 22:23:06 +00:00
|
|
|
void processFile(char const* description, FILE* file,
|
|
|
|
bool close_file, char const* password = 0);
|
2012-06-21 11:59:56 +00:00
|
|
|
|
2010-10-01 10:20:38 +00:00
|
|
|
// Parse a PDF file loaded into a memory buffer. This works
|
|
|
|
// exactly like processFile except that the PDF file is in memory
|
|
|
|
// instead of on disk. The description appears in any warning or
|
|
|
|
// error message in place of the file name.
|
|
|
|
QPDF_DLL
|
|
|
|
void processMemoryFile(char const* description,
|
|
|
|
char const* buf, size_t length,
|
|
|
|
char const* password = 0);
|
|
|
|
|
2012-09-23 21:42:26 +00:00
|
|
|
// Parse a PDF file loaded from a custom InputSource. If you have
|
|
|
|
// your own method of retrieving a PDF file, you can subclass
|
|
|
|
// InputSource and use this method.
|
|
|
|
QPDF_DLL
|
|
|
|
void processInputSource(PointerHolder<InputSource>,
|
|
|
|
char const* password = 0);
|
|
|
|
|
2019-08-31 19:10:29 +00:00
|
|
|
// Close or otherwise release the input source. Once this has been
|
|
|
|
// called, no other methods of qpdf can be called safely except
|
|
|
|
// for getWarnings and anyWarnings(). After this has been called,
|
|
|
|
// it is safe to perform operations on the input file such as
|
|
|
|
// deleting or renaming it.
|
|
|
|
QPDF_DLL
|
|
|
|
void closeInputSource();
|
|
|
|
|
2018-01-14 15:17:17 +00:00
|
|
|
// For certain forensic or investigatory purposes, it may
|
|
|
|
// sometimes be useful to specify the encryption key directly,
|
|
|
|
// even though regular PDF applications do not provide a way to do
|
2022-01-30 13:28:37 +00:00
|
|
|
// this. Calling setPasswordIsHexKey(true) before calling any of
|
2018-01-14 15:17:17 +00:00
|
|
|
// the process methods will bypass the normal encryption key
|
|
|
|
// computation or recovery mechanisms and interpret the bytes in
|
|
|
|
// the password as a hex-encoded encryption key. Note that we
|
|
|
|
// hex-encode the key because it may contain null bytes and
|
|
|
|
// therefore can't be represented in a char const*.
|
|
|
|
QPDF_DLL
|
|
|
|
void setPasswordIsHexKey(bool);
|
|
|
|
|
2012-06-22 03:06:48 +00:00
|
|
|
// Create a QPDF object for an empty PDF. This PDF has no pages
|
|
|
|
// or objects other than a minimal trailer, a document catalog,
|
|
|
|
// and a /Pages tree containing zero pages. Pages and other
|
|
|
|
// objects can be added to the file in the normal way, and the
|
|
|
|
// trailer and document catalog can be mutated. Calling this
|
|
|
|
// method is equivalent to calling processFile on an equivalent
|
2012-06-23 02:07:58 +00:00
|
|
|
// PDF file. See the pdf-create.cc example for a demonstration of
|
|
|
|
// how to use this method to create a PDF file from scratch.
|
2012-06-22 03:06:48 +00:00
|
|
|
QPDF_DLL
|
|
|
|
void emptyPDF();
|
|
|
|
|
2020-12-23 11:12:49 +00:00
|
|
|
// From 10.1: register a new filter implementation for a specific
|
|
|
|
// stream filter. You can add your own implementations for new
|
|
|
|
// filter types or override existing ones provided by the library.
|
|
|
|
// Registered stream filters are used for decoding only as you can
|
|
|
|
// override encoding with stream data providers. For example, you
|
2022-01-30 13:28:37 +00:00
|
|
|
// could use this method to add support for one of the other filter
|
2020-12-23 11:12:49 +00:00
|
|
|
// types by using additional third-party libraries that qpdf does
|
|
|
|
// not presently use. The standard filters are implemented using
|
|
|
|
// QPDFStreamFilter classes.
|
|
|
|
QPDF_DLL
|
|
|
|
static void registerStreamFilter(
|
|
|
|
std::string const& filter_name,
|
|
|
|
std::function<std::shared_ptr<QPDFStreamFilter> ()> factory);
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Parameter settings
|
|
|
|
|
2010-10-01 11:02:35 +00:00
|
|
|
// By default, warning messages are issued to std::cerr and output
|
|
|
|
// messages printed by certain check calls are issued to
|
|
|
|
// std::cout. This method allows you to specify alternative
|
|
|
|
// streams for this purpose. Note that no normal QPDF operations
|
|
|
|
// generate output to std::cout, so for applications that just
|
|
|
|
// wish to avoid creating output and don't call any check
|
|
|
|
// functions, calling setSuppressWarnings(true) is sufficient.
|
|
|
|
// Applications that wish to present check or warning information
|
|
|
|
// to users may replace the output and error streams to capture
|
|
|
|
// the output and errors for other use. A null value for either
|
|
|
|
// stream will cause QPDF to use std::cout or std::cerr as
|
|
|
|
// appropriate.
|
|
|
|
QPDF_DLL
|
|
|
|
void setOutputStreams(std::ostream* out_stream, std::ostream* err_stream);
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// If true, ignore any cross-reference streams in a hybrid file
|
|
|
|
// (one that contains both cross-reference streams and
|
|
|
|
// cross-reference tables). This can be useful for testing to
|
|
|
|
// ensure that a hybrid file would work with an older reader.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
void setIgnoreXRefStreams(bool);
|
|
|
|
|
2010-10-01 11:02:35 +00:00
|
|
|
// By default, any warnings are issued to std::cerr or the error
|
|
|
|
// stream specified in a call to setOutputStreams as they are
|
2022-01-30 13:28:37 +00:00
|
|
|
// encountered. If this method is called with a true value, reporting of
|
2008-04-29 12:55:25 +00:00
|
|
|
// warnings is suppressed. You may still retrieve warnings by
|
|
|
|
// calling getWarnings.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
void setSuppressWarnings(bool);
|
|
|
|
|
|
|
|
// By default, QPDF will try to recover if it finds certain types
|
|
|
|
// of errors in PDF files. If turned off, it will throw an
|
|
|
|
// exception on the first such problem it finds without attempting
|
|
|
|
// recovery.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
void setAttemptRecovery(bool);
|
|
|
|
|
2019-01-11 03:11:38 +00:00
|
|
|
// Tell other QPDF objects that streams copied from this QPDF need
|
|
|
|
// to be fully copied when copyForeignObject is called on them.
|
|
|
|
// Calling setIgnoreXRefStreams(true) on a QPDF object makes it
|
|
|
|
// possible for the object and its input source to disappear
|
|
|
|
// before streams copied from it are written with the destination
|
|
|
|
// QPDF object. Confused? Ordinarily, if you are going to copy
|
|
|
|
// objects from a source QPDF object to a destination QPDF object
|
|
|
|
// using copyForeignObject or addPage, the source object's input
|
|
|
|
// source must stick around until after the destination PDF is
|
|
|
|
// written. If you call this method on the source QPDF object, it
|
|
|
|
// sends a signal to the destination object that it must fully
|
|
|
|
// copy the stream data when copyForeignObject. It will do this by
|
|
|
|
// making a copy in RAM. Ordinarily the stream data is copied
|
|
|
|
// lazily to avoid unnecessary duplication of the stream data.
|
|
|
|
// Note that the stream data is copied into RAM only once
|
|
|
|
// regardless of how many objects the stream is copied into. The
|
|
|
|
// result is that, if you called setImmediateCopyFrom(true) on a
|
|
|
|
// given QPDF object prior to copying any of its streams, you do
|
|
|
|
// not need to keep it or its input source around after copying
|
|
|
|
// its objects to another QPDF. This is true even if the source
|
|
|
|
// streams use StreamDataProvider. Note that this method is called
|
|
|
|
// on the QPDF object you are copying FROM, not the one you are
|
|
|
|
// copying to. The reasoning for this is that there's no reason a
|
|
|
|
// given QPDF may not get objects copied to it from a variety of
|
|
|
|
// other objects, some transient and some not. Since what's
|
|
|
|
// relevant is whether the source QPDF is transient, the method
|
|
|
|
// must be called on the source QPDF, not the destination one.
|
2022-01-30 13:28:37 +00:00
|
|
|
// This method will make a copy of the stream in RAM, so be
|
2019-01-11 03:11:38 +00:00
|
|
|
// sure you have enough memory to simultaneously hold all the
|
|
|
|
// streams you're copying.
|
|
|
|
QPDF_DLL
|
|
|
|
void setImmediateCopyFrom(bool);
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Other public methods
|
|
|
|
|
|
|
|
// Return the list of warnings that have been issued so far and
|
|
|
|
// clear the list. This method may be called even if processFile
|
|
|
|
// throws an exception. Note that if setSuppressWarnings was not
|
|
|
|
// called or was called with a false value, any warnings retrieved
|
2010-10-01 11:02:35 +00:00
|
|
|
// here will have already been output.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-10-20 00:24:44 +00:00
|
|
|
std::vector<QPDFExc> getWarnings();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-08-31 19:10:29 +00:00
|
|
|
// Indicate whether any warnings have been issued so far. Does not
|
|
|
|
// clear the list of warnings.
|
|
|
|
QPDF_DLL
|
|
|
|
bool anyWarnings() const;
|
|
|
|
|
2022-01-30 13:28:37 +00:00
|
|
|
// Indicate the number of warnings that have been issued since the last
|
|
|
|
// call to getWarnings. Does not clear the list of warnings.
|
2021-03-01 22:33:34 +00:00
|
|
|
QPDF_DLL
|
|
|
|
size_t numWarnings() const;
|
|
|
|
|
2019-01-12 14:14:20 +00:00
|
|
|
// Return an application-scoped unique ID for this QPDF object.
|
2022-01-30 13:28:37 +00:00
|
|
|
// This is not a globally unique ID. It is constructed using a
|
2019-01-12 14:14:20 +00:00
|
|
|
// timestamp and a random number and is intended to be unique
|
|
|
|
// among QPDF objects that are created by a single run of an
|
|
|
|
// application. While it's very likely that these are actually
|
|
|
|
// globally unique, it is not recommended to use them for
|
|
|
|
// long-term purposes.
|
|
|
|
QPDF_DLL
|
|
|
|
unsigned long long getUniqueId() const;
|
|
|
|
|
2021-01-16 23:35:04 +00:00
|
|
|
// Issue a warning on behalf of this QPDF object. It will be
|
|
|
|
// emitted with other warnings, following warning suppression
|
|
|
|
// rules, and it will be available with getWarnings().
|
|
|
|
QPDF_DLL
|
|
|
|
void warn(QPDFExc const& e);
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string getFilename() const;
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string getPDFVersion() const;
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2012-12-27 20:17:39 +00:00
|
|
|
int getExtensionLevel();
|
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle getTrailer();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle getRoot();
|
2019-09-28 13:07:00 +00:00
|
|
|
QPDF_DLL
|
|
|
|
std::map<QPDFObjGen, QPDFXRefEntry> getXRefTable();
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Install this object handle as an indirect object and return an
|
|
|
|
// indirect reference to it.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle makeIndirectObject(QPDFObjectHandle);
|
|
|
|
|
|
|
|
// Retrieve an object by object ID and generation. Returns an
|
|
|
|
// indirect reference to it.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2013-06-14 15:58:37 +00:00
|
|
|
QPDFObjectHandle getObjectByObjGen(QPDFObjGen const&);
|
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle getObjectByID(int objid, int generation);
|
|
|
|
|
2011-08-10 16:42:48 +00:00
|
|
|
// Replace the object with the given object id with the given
|
2021-02-25 11:34:03 +00:00
|
|
|
// object. The object handle passed in must be a direct object,
|
2011-08-10 16:42:48 +00:00
|
|
|
// though it may contain references to other indirect objects
|
2021-02-25 11:34:03 +00:00
|
|
|
// within it. Prior to qpdf 10.2.1, after calling this method,
|
|
|
|
// existing QPDFObjectHandle instances that pointed to the
|
|
|
|
// original object still pointed to the original object, resulting
|
|
|
|
// in confusing and incorrect behavior. This was fixed in 10.2.1,
|
|
|
|
// so existing QPDFObjectHandle objects will start pointing to the
|
|
|
|
// newly replaced object. Note that replacing an object with
|
|
|
|
// QPDFObjectHandle::newNull() effectively removes the object from
|
|
|
|
// the file since a non-existent object is treated as a null
|
|
|
|
// object. To replace a reserved object, call replaceReserved
|
2012-07-08 18:19:19 +00:00
|
|
|
// instead.
|
2011-08-10 16:42:48 +00:00
|
|
|
QPDF_DLL
|
2013-06-14 15:58:37 +00:00
|
|
|
void replaceObject(QPDFObjGen const& og, QPDFObjectHandle);
|
|
|
|
QPDF_DLL
|
2011-08-10 16:42:48 +00:00
|
|
|
void replaceObject(int objid, int generation, QPDFObjectHandle);
|
|
|
|
|
2021-02-25 11:34:03 +00:00
|
|
|
// Swap two objects given by ID. Prior to qpdf 10.2.1, existing
|
|
|
|
// QPDFObjectHandle instances that reference them objects not
|
|
|
|
// notice the swap, but this was fixed in 10.2.1.
|
2011-08-10 16:42:48 +00:00
|
|
|
QPDF_DLL
|
2013-06-14 15:58:37 +00:00
|
|
|
void swapObjects(QPDFObjGen const& og1, QPDFObjGen const& og2);
|
|
|
|
QPDF_DLL
|
2011-08-10 16:42:48 +00:00
|
|
|
void swapObjects(int objid1, int generation1,
|
|
|
|
int objid2, int generation2);
|
|
|
|
|
2012-07-08 18:19:19 +00:00
|
|
|
// Replace a reserved object. This is a wrapper around
|
|
|
|
// replaceObject but it guarantees that the underlying object is a
|
|
|
|
// reserved object. After this call, reserved will be a reference
|
|
|
|
// to replacement.
|
|
|
|
QPDF_DLL
|
|
|
|
void
|
|
|
|
replaceReserved(QPDFObjectHandle reserved,
|
|
|
|
QPDFObjectHandle replacement);
|
|
|
|
|
2019-01-07 02:18:36 +00:00
|
|
|
// Copy an object from another QPDF to this one. Starting with
|
|
|
|
// qpdf version 8.3.0, it is no longer necessary to keep the
|
|
|
|
// original QPDF around after the call to copyForeignObject as
|
|
|
|
// long as the source of any copied stream data is still
|
|
|
|
// available. Usually this means you just have to keep the input
|
|
|
|
// file around, not the QPDF object. The exception to this is if
|
|
|
|
// you copy a stream that gets its data from a
|
|
|
|
// QPDFObjectHandle::StreamDataProvider. In this case only, the
|
|
|
|
// original stream's QPDF object must stick around because the
|
|
|
|
// QPDF object is itself the source of the original stream data.
|
|
|
|
// For a more in-depth discussion, please see the TODO file.
|
2019-01-11 12:46:53 +00:00
|
|
|
// Starting in 8.4.0, you can call setImmediateCopyFrom(true) on
|
2019-01-11 03:11:38 +00:00
|
|
|
// the SOURCE QPDF object (the one you're copying FROM). If you do
|
|
|
|
// this prior to copying any of its objects, then neither the
|
|
|
|
// source QPDF object nor its input source needs to stick around
|
|
|
|
// at all regardless of the source. The cost is that the stream
|
|
|
|
// data is copied into RAM at the time copyForeignObject is
|
|
|
|
// called. See setImmediateCopyFrom for more information.
|
2017-07-29 13:25:25 +00:00
|
|
|
//
|
2019-01-07 02:18:36 +00:00
|
|
|
// The return value of this method is an indirect reference to the
|
|
|
|
// copied object in this file. This method is intended to be used
|
2019-01-29 02:53:55 +00:00
|
|
|
// to copy non-page objects. To copy page objects, pass the
|
|
|
|
// foreign page object directly to addPage (or addPageAt). If you
|
|
|
|
// copy objects that contain references to pages, you should copy
|
|
|
|
// the pages first using addPage(At). Otherwise references to the
|
|
|
|
// pages that have not been copied will be replaced with nulls. It
|
|
|
|
// is possible to use copyForeignObject on page objects if you are
|
|
|
|
// not going to use them as pages. Doing so copies the object
|
|
|
|
// normally but does not update the page structure. For example,
|
|
|
|
// it is a valid use case to use copyForeignObject for a page that
|
|
|
|
// you are going to turn into a form XObject, though you can also
|
|
|
|
// use QPDFPageObjectHelper::getFormXObjectForPage for that
|
|
|
|
// purpose.
|
2022-01-30 13:28:37 +00:00
|
|
|
//
|
2012-07-11 19:29:41 +00:00
|
|
|
// When copying objects with this method, object structure will be
|
|
|
|
// preserved, so all indirectly referenced indirect objects will
|
|
|
|
// be copied as well. This includes any circular references that
|
|
|
|
// may exist. The QPDF object keeps a record of what has already
|
|
|
|
// been copied, so shared objects will not be copied multiple
|
|
|
|
// times. This also means that if you mutate an object that has
|
|
|
|
// already been copied and try to copy it again, it won't work
|
|
|
|
// since the modified object will not be recopied. Therefore, you
|
|
|
|
// should do all mutation on the original file that you are going
|
|
|
|
// to do before you start copying its objects to a new file.
|
|
|
|
QPDF_DLL
|
|
|
|
QPDFObjectHandle copyForeignObject(QPDFObjectHandle foreign);
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Encryption support
|
|
|
|
|
2012-12-30 02:31:21 +00:00
|
|
|
enum encryption_method_e { e_none, e_unknown, e_rc4, e_aes, e_aesv3 };
|
2012-12-26 21:04:54 +00:00
|
|
|
class EncryptionData
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2012-12-26 21:04:54 +00:00
|
|
|
public:
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// This class holds data read from the encryption dictionary.
|
2008-05-05 02:22:40 +00:00
|
|
|
EncryptionData(int V, int R, int Length_bytes, int P,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string const& O, std::string const& U,
|
2012-12-26 22:10:25 +00:00
|
|
|
std::string const& OE, std::string const& UE,
|
|
|
|
std::string const& Perms,
|
2009-10-17 03:14:47 +00:00
|
|
|
std::string const& id1, bool encrypt_metadata) :
|
2008-04-29 12:55:25 +00:00
|
|
|
V(V),
|
|
|
|
R(R),
|
|
|
|
Length_bytes(Length_bytes),
|
|
|
|
P(P),
|
|
|
|
O(O),
|
|
|
|
U(U),
|
2012-12-26 22:10:25 +00:00
|
|
|
OE(OE),
|
|
|
|
UE(UE),
|
|
|
|
Perms(Perms),
|
2009-10-17 03:14:47 +00:00
|
|
|
id1(id1),
|
|
|
|
encrypt_metadata(encrypt_metadata)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-12-26 21:04:54 +00:00
|
|
|
int getV() const;
|
|
|
|
int getR() const;
|
|
|
|
int getLengthBytes() const;
|
|
|
|
int getP() const;
|
|
|
|
std::string const& getO() const;
|
|
|
|
std::string const& getU() const;
|
2012-12-26 22:10:25 +00:00
|
|
|
std::string const& getOE() const;
|
|
|
|
std::string const& getUE() const;
|
|
|
|
std::string const& getPerms() const;
|
2012-12-26 21:04:54 +00:00
|
|
|
std::string const& getId1() const;
|
|
|
|
bool getEncryptMetadata() const;
|
|
|
|
|
|
|
|
void setO(std::string const&);
|
|
|
|
void setU(std::string const&);
|
2012-12-26 22:10:25 +00:00
|
|
|
void setV5EncryptionParameters(std::string const& O,
|
|
|
|
std::string const& OE,
|
|
|
|
std::string const& U,
|
|
|
|
std::string const& UE,
|
|
|
|
std::string const& Perms);
|
2012-12-26 21:04:54 +00:00
|
|
|
|
|
|
|
private:
|
2020-04-03 16:04:54 +00:00
|
|
|
EncryptionData(EncryptionData const&) = delete;
|
|
|
|
EncryptionData& operator=(EncryptionData const&) = delete;
|
2012-12-26 21:04:54 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
int V;
|
|
|
|
int R;
|
|
|
|
int Length_bytes;
|
2008-05-05 02:22:40 +00:00
|
|
|
int P;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string O;
|
|
|
|
std::string U;
|
2012-12-26 22:10:25 +00:00
|
|
|
std::string OE;
|
|
|
|
std::string UE;
|
|
|
|
std::string Perms;
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string id1;
|
2009-10-17 03:14:47 +00:00
|
|
|
bool encrypt_metadata;
|
2008-04-29 12:55:25 +00:00
|
|
|
};
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 03:11:29 +00:00
|
|
|
bool isEncrypted() const;
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool isEncrypted(int& R, int& P);
|
|
|
|
|
2012-07-16 01:15:24 +00:00
|
|
|
QPDF_DLL
|
|
|
|
bool isEncrypted(int& R, int& P, int& V,
|
|
|
|
encryption_method_e& stream_method,
|
|
|
|
encryption_method_e& string_method,
|
|
|
|
encryption_method_e& file_method);
|
|
|
|
|
2019-08-24 13:34:48 +00:00
|
|
|
QPDF_DLL
|
|
|
|
bool ownerPasswordMatched() const;
|
|
|
|
|
|
|
|
QPDF_DLL
|
|
|
|
bool userPasswordMatched() const;
|
|
|
|
|
2009-09-27 20:05:38 +00:00
|
|
|
// Encryption permissions -- not enforced by QPDF
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowAccessibility();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowExtractAll();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowPrintLowRes();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowPrintHighRes();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowModifyAssembly();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowModifyForm();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowModifyAnnotation();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowModifyOther();
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 20:05:38 +00:00
|
|
|
bool allowModifyAll();
|
|
|
|
|
2009-09-27 03:11:29 +00:00
|
|
|
// Helper function to trim padding from user password. Calling
|
|
|
|
// trim_user_password on the result of getPaddedUserPassword gives
|
|
|
|
// getTrimmedUserPassword's result.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
static void trim_user_password(std::string& user_password);
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
static std::string compute_data_key(
|
2009-10-17 03:14:47 +00:00
|
|
|
std::string const& encryption_key, int objid, int generation,
|
2012-12-30 02:31:21 +00:00
|
|
|
bool use_aes, int encryption_V, int encryption_R);
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
static std::string compute_encryption_key(
|
|
|
|
std::string const& password, EncryptionData const& data);
|
|
|
|
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
static void compute_encryption_O_U(
|
|
|
|
char const* user_password, char const* owner_password,
|
2009-10-17 18:54:51 +00:00
|
|
|
int V, int R, int key_len, int P, bool encrypt_metadata,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::string const& id1,
|
|
|
|
std::string& O, std::string& U);
|
2012-12-30 02:31:21 +00:00
|
|
|
QPDF_DLL
|
|
|
|
static void compute_encryption_parameters_V5(
|
|
|
|
char const* user_password, char const* owner_password,
|
|
|
|
int V, int R, int key_len, int P, bool encrypt_metadata,
|
|
|
|
std::string const& id1,
|
|
|
|
std::string& encryption_key,
|
|
|
|
std::string& O, std::string& U,
|
|
|
|
std::string& OE, std::string& UE, std::string& Perms);
|
2013-01-04 01:45:53 +00:00
|
|
|
// Return the full user password as stored in the PDF file. For
|
|
|
|
// files encrypted with 40-bit or 128-bit keys, the user password
|
|
|
|
// can be recovered when the file is opened using the owner
|
|
|
|
// password. This is not possible with newer encryption formats.
|
|
|
|
// If you are attempting to recover the user password in a
|
2009-09-27 03:11:29 +00:00
|
|
|
// user-presentable form, call getTrimmedUserPassword() instead.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 03:11:29 +00:00
|
|
|
std::string const& getPaddedUserPassword() const;
|
2013-01-04 01:45:53 +00:00
|
|
|
// Return human-readable form of user password subject to same
|
|
|
|
// limitations as getPaddedUserPassword().
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2009-09-27 03:11:29 +00:00
|
|
|
std::string getTrimmedUserPassword() const;
|
2012-12-30 02:31:21 +00:00
|
|
|
// Return the previously computed or retrieved encryption key for
|
|
|
|
// this file
|
|
|
|
QPDF_DLL
|
|
|
|
std::string getEncryptionKey() const;
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Linearization support
|
|
|
|
|
|
|
|
// Returns true iff the file starts with a linearization parameter
|
|
|
|
// dictionary. Does no additional validation.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
bool isLinearized();
|
|
|
|
|
2019-06-23 13:10:42 +00:00
|
|
|
// Performs various sanity checks on a linearized file. Return
|
|
|
|
// true if no errors or warnings. Otherwise, return false and
|
2010-10-01 11:02:35 +00:00
|
|
|
// output errors and warnings to std::cout or the output stream
|
2019-06-23 13:10:42 +00:00
|
|
|
// specified in a call to setOutputStreams. It is recommended for
|
|
|
|
// linearization errors to be treated as warnings.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
bool checkLinearization();
|
|
|
|
|
|
|
|
// Calls checkLinearization() and, if possible, prints normalized
|
2010-10-01 11:02:35 +00:00
|
|
|
// contents of some of the hints tables to std::cout or the output
|
|
|
|
// stream specified in a call to setOutputStreams. Normalization
|
2008-04-29 12:55:25 +00:00
|
|
|
// includes adding min values to delta values and adjusting
|
|
|
|
// offsets based on the location and size of the primary hint
|
|
|
|
// stream.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
void showLinearizationData();
|
|
|
|
|
|
|
|
// Shows the contents of the cross-reference table
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
void showXRefTable();
|
|
|
|
|
2019-01-04 15:17:33 +00:00
|
|
|
// Detect all indirect references to objects that don't exist and
|
|
|
|
// resolve them by replacing them with null, which is how the PDF
|
|
|
|
// spec says to interpret such dangling references. This method is
|
|
|
|
// called automatically if you try to add any new objects, if you
|
|
|
|
// call getAllObjects, and before a file is written. The qpdf
|
|
|
|
// object caches whether it has run this to avoid running it
|
|
|
|
// multiple times. You can pass true to force it to run again if
|
|
|
|
// you have explicitly added new objects that may have additional
|
|
|
|
// dangling references.
|
|
|
|
QPDF_DLL
|
|
|
|
void fixDanglingReferences(bool force = false);
|
|
|
|
|
2018-06-22 18:52:03 +00:00
|
|
|
// Return the approximate number of indirect objects. It is
|
|
|
|
// approximate because not all objects in the file are preserved
|
2019-01-04 15:17:33 +00:00
|
|
|
// in all cases, and gaps in object numbering are not preserved.
|
2018-06-22 18:52:03 +00:00
|
|
|
QPDF_DLL
|
|
|
|
size_t getObjectCount();
|
|
|
|
|
2017-07-28 23:18:57 +00:00
|
|
|
// Returns a list of indirect objects for every object in the xref
|
2017-08-22 18:19:08 +00:00
|
|
|
// table. Useful for discovering objects that are not otherwise
|
2017-07-28 23:18:57 +00:00
|
|
|
// referenced.
|
|
|
|
QPDF_DLL
|
|
|
|
std::vector<QPDFObjectHandle> getAllObjects();
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Optimization support -- see doc/optimization. Implemented in
|
|
|
|
// QPDF_optimization.cc
|
|
|
|
|
|
|
|
// The object_stream_data map maps from a "compressed" object to
|
2020-12-25 14:50:55 +00:00
|
|
|
// the object stream that contains it. This enables optimize to
|
2008-04-29 12:55:25 +00:00
|
|
|
// populate the object <-> user maps with only uncompressed
|
2020-12-25 14:50:55 +00:00
|
|
|
// objects. If allow_changes is false, an exception will be thrown
|
|
|
|
// if any changes are made during the optimization process. This
|
|
|
|
// is available so that the test suite can make sure that a
|
|
|
|
// linearized file is already optimized. When called in this way,
|
|
|
|
// optimize() still populates the object <-> user maps. The
|
|
|
|
// optional skip_stream_parameters parameter, if present, is
|
|
|
|
// called for each stream object. The function should return 2 if
|
|
|
|
// optimization should discard /Length, /Filter, and /DecodeParms;
|
|
|
|
// 1 if it should discard /Length, and 0 if it should preserve all
|
|
|
|
// keys. This is used by QPDFWriter to avoid creation of dangling
|
|
|
|
// objects for stream dictionary keys it will be regenerating.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
void optimize(std::map<int, int> const& object_stream_data,
|
|
|
|
bool allow_changes = true);
|
2020-12-25 14:50:55 +00:00
|
|
|
// ABI: make function optional and merge overloaded versions
|
|
|
|
QPDF_DLL
|
|
|
|
void optimize(
|
|
|
|
std::map<int, int> const& object_stream_data,
|
|
|
|
bool allow_changes,
|
|
|
|
std::function<int(QPDFObjectHandle&)> skip_stream_parameters);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-01-29 01:13:10 +00:00
|
|
|
// Traverse page tree return all /Page objects. It also detects
|
|
|
|
// and resolves cases in which the same /Page object is
|
|
|
|
// duplicated. For efficiency, this method returns a const
|
|
|
|
// reference to an internal vector of pages. Calls to addPage,
|
|
|
|
// addPageAt, and removePage safely update this, but directly
|
2021-12-04 15:27:25 +00:00
|
|
|
// manipulation of the pages tree or pushing inheritable objects
|
2019-01-29 01:13:10 +00:00
|
|
|
// to the page level may invalidate it. See comments for
|
|
|
|
// updateAllPagesCache() for additional notes. Newer code should
|
|
|
|
// use QPDFPageDocumentHelper::getAllPages instead. The decision
|
|
|
|
// to expose this internal cache was arguably incorrect, but it is
|
2018-06-18 19:05:53 +00:00
|
|
|
// being left here for compatibility. It is, however, completely
|
|
|
|
// safe to use this for files that you are not modifying.
|
2009-10-21 01:45:13 +00:00
|
|
|
QPDF_DLL
|
2008-04-29 12:55:25 +00:00
|
|
|
std::vector<QPDFObjectHandle> const& getAllPages();
|
|
|
|
|
2021-11-02 22:12:39 +00:00
|
|
|
// These methods, given a page object or its object/generation
|
|
|
|
// number, returns the 0-based index into the array returned by
|
|
|
|
// getAllPages() for that page. An exception is thrown if the page
|
|
|
|
// is not found.
|
|
|
|
QPDF_DLL
|
|
|
|
int findPage(QPDFObjGen const& og);
|
|
|
|
QPDF_DLL
|
|
|
|
int findPage(QPDFObjectHandle& page);
|
|
|
|
|
2012-06-21 14:42:18 +00:00
|
|
|
// This method synchronizes QPDF's cache of the page structure
|
|
|
|
// with the actual /Pages tree. If you restrict changes to the
|
|
|
|
// /Pages tree, including addition, removal, or replacement of
|
|
|
|
// pages or changes to any /Pages objects, to calls to these page
|
|
|
|
// handling APIs, you never need to call this method. If you
|
|
|
|
// modify /Pages structures directly, you must call this method
|
|
|
|
// afterwards. This method updates the internal list of pages, so
|
|
|
|
// after calling this method, any previous references returned by
|
2012-07-11 19:29:41 +00:00
|
|
|
// getAllPages() will be valid again. It also resets any state
|
|
|
|
// about having pushed inherited attributes in /Pages objects down
|
|
|
|
// to the pages, so if you add any inheritable attributes to a
|
|
|
|
// /Pages object, you should also call this method.
|
2012-06-18 20:46:19 +00:00
|
|
|
QPDF_DLL
|
2012-06-21 14:42:18 +00:00
|
|
|
void updateAllPagesCache();
|
2012-06-18 20:46:19 +00:00
|
|
|
|
2018-06-18 19:05:53 +00:00
|
|
|
// Legacy handling API. These methods are not going anywhere, and
|
|
|
|
// you should feel free to continue using them if it simplifies
|
|
|
|
// your code. Newer code should make use of QPDFPageDocumentHelper
|
|
|
|
// instead as future page handling methods will be added there.
|
|
|
|
// The functionality and specification of these legacy methods is
|
|
|
|
// identical to the identically named methods there, except that
|
|
|
|
// these versions use QPDFObjectHandle instead of
|
|
|
|
// QPDFPageObjectHelper, so please see comments in that file for
|
2021-03-04 19:46:07 +00:00
|
|
|
// descriptions. There are subtleties you need to know about, so
|
|
|
|
// please look at the comments there.
|
2012-07-22 00:50:42 +00:00
|
|
|
QPDF_DLL
|
2012-06-22 15:50:02 +00:00
|
|
|
void pushInheritedAttributesToPage();
|
2012-06-18 23:53:10 +00:00
|
|
|
QPDF_DLL
|
|
|
|
void addPage(QPDFObjectHandle newpage, bool first);
|
|
|
|
QPDF_DLL
|
|
|
|
void addPageAt(QPDFObjectHandle newpage, bool before,
|
2012-06-21 14:42:18 +00:00
|
|
|
QPDFObjectHandle refpage);
|
2012-06-18 23:53:10 +00:00
|
|
|
QPDF_DLL
|
2012-06-21 14:42:18 +00:00
|
|
|
void removePage(QPDFObjectHandle page);
|
2018-06-18 19:05:53 +00:00
|
|
|
// End legacy page helpers
|
2012-06-18 23:53:10 +00:00
|
|
|
|
2013-07-10 15:07:40 +00:00
|
|
|
// Writer class is restricted to QPDFWriter so that only it can
|
|
|
|
// call certain methods.
|
|
|
|
class Writer
|
|
|
|
{
|
|
|
|
friend class QPDFWriter;
|
|
|
|
private:
|
|
|
|
|
|
|
|
static void getLinearizedParts(
|
|
|
|
QPDF& qpdf,
|
|
|
|
std::map<int, int> const& object_stream_data,
|
|
|
|
std::vector<QPDFObjectHandle>& part4,
|
|
|
|
std::vector<QPDFObjectHandle>& part6,
|
|
|
|
std::vector<QPDFObjectHandle>& part7,
|
|
|
|
std::vector<QPDFObjectHandle>& part8,
|
|
|
|
std::vector<QPDFObjectHandle>& part9)
|
|
|
|
{
|
|
|
|
qpdf.getLinearizedParts(object_stream_data,
|
|
|
|
part4, part6, part7, part8, part9);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void generateHintStream(
|
|
|
|
QPDF& qpdf,
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
|
|
|
std::map<int, int> const& obj_renumber,
|
|
|
|
PointerHolder<Buffer>& hint_stream,
|
|
|
|
int& S, int& O)
|
|
|
|
{
|
|
|
|
return qpdf.generateHintStream(xref, lengths, obj_renumber,
|
|
|
|
hint_stream, S, O);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void getObjectStreamData(QPDF& qpdf, std::map<int, int>& omap)
|
|
|
|
{
|
|
|
|
qpdf.getObjectStreamData(omap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<QPDFObjGen> getCompressibleObjGens(QPDF& qpdf)
|
|
|
|
{
|
|
|
|
return qpdf.getCompressibleObjGens();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Resolver class is restricted to QPDFObjectHandle so that only
|
|
|
|
// it can resolve indirect references.
|
|
|
|
class Resolver
|
|
|
|
{
|
|
|
|
friend class QPDFObjectHandle;
|
|
|
|
private:
|
|
|
|
static PointerHolder<QPDFObject> resolve(
|
|
|
|
QPDF* qpdf, int objid, int generation)
|
|
|
|
{
|
|
|
|
return qpdf->resolve(objid, generation);
|
|
|
|
}
|
2021-02-25 11:34:03 +00:00
|
|
|
static bool objectChanged(
|
|
|
|
QPDF* qpdf, QPDFObjGen const& og, PointerHolder<QPDFObject>& oph)
|
|
|
|
{
|
|
|
|
return qpdf->objectChanged(og, oph);
|
|
|
|
}
|
2008-04-29 12:55:25 +00:00
|
|
|
};
|
|
|
|
friend class Resolver;
|
|
|
|
|
2021-02-21 11:35:53 +00:00
|
|
|
// StreamCopier class is restricted to QPDFObjectHandle so it can
|
|
|
|
// copy stream data.
|
|
|
|
class StreamCopier
|
|
|
|
{
|
|
|
|
friend class QPDFObjectHandle;
|
|
|
|
private:
|
|
|
|
static void copyStreamData(QPDF* qpdf,
|
|
|
|
QPDFObjectHandle const& dest,
|
|
|
|
QPDFObjectHandle const& src)
|
|
|
|
{
|
|
|
|
qpdf->copyStreamData(dest, src);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
friend class Resolver;
|
|
|
|
|
2019-08-21 16:50:36 +00:00
|
|
|
// ParseGuard class allows QPDFObjectHandle to detect re-entrant
|
|
|
|
// resolution
|
|
|
|
class ParseGuard
|
|
|
|
{
|
|
|
|
friend class QPDFObjectHandle;
|
|
|
|
private:
|
|
|
|
ParseGuard(QPDF* qpdf) :
|
|
|
|
qpdf(qpdf)
|
|
|
|
{
|
|
|
|
if (qpdf)
|
|
|
|
{
|
|
|
|
qpdf->inParse(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~ParseGuard()
|
|
|
|
{
|
|
|
|
if (qpdf)
|
|
|
|
{
|
|
|
|
qpdf->inParse(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QPDF* qpdf;
|
|
|
|
};
|
|
|
|
friend class ParseGuard;
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Pipe class is restricted to QPDF_Stream
|
|
|
|
class Pipe
|
|
|
|
{
|
|
|
|
friend class QPDF_Stream;
|
|
|
|
private:
|
2017-07-28 03:42:27 +00:00
|
|
|
static bool pipeStreamData(QPDF* qpdf, int objid, int generation,
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t offset, size_t length,
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle dict,
|
2017-09-12 19:48:08 +00:00
|
|
|
Pipeline* pipeline,
|
|
|
|
bool suppress_warnings,
|
|
|
|
bool will_retry)
|
2008-04-29 12:55:25 +00:00
|
|
|
{
|
2017-07-28 03:42:27 +00:00
|
|
|
return qpdf->pipeStreamData(
|
|
|
|
objid, generation, offset, length, dict, pipeline,
|
2017-09-12 19:48:08 +00:00
|
|
|
suppress_warnings, will_retry);
|
2008-04-29 12:55:25 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
friend class Pipe;
|
|
|
|
|
|
|
|
private:
|
2009-10-24 04:47:17 +00:00
|
|
|
static std::string qpdf_version;
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
class ObjCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ObjCache() :
|
|
|
|
end_before_space(0),
|
|
|
|
end_after_space(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
ObjCache(PointerHolder<QPDFObject> object,
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t end_before_space,
|
|
|
|
qpdf_offset_t end_after_space) :
|
2008-04-29 12:55:25 +00:00
|
|
|
object(object),
|
|
|
|
end_before_space(end_before_space),
|
|
|
|
end_after_space(end_after_space)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerHolder<QPDFObject> object;
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t end_before_space;
|
|
|
|
qpdf_offset_t end_after_space;
|
2008-04-29 12:55:25 +00:00
|
|
|
};
|
|
|
|
|
2012-07-11 19:29:41 +00:00
|
|
|
class ObjCopier
|
|
|
|
{
|
|
|
|
public:
|
2013-06-14 15:22:04 +00:00
|
|
|
std::map<QPDFObjGen, QPDFObjectHandle> object_map;
|
2012-07-11 19:29:41 +00:00
|
|
|
std::vector<QPDFObjectHandle> to_copy;
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen> visiting;
|
2012-07-11 19:29:41 +00:00
|
|
|
};
|
|
|
|
|
2019-01-07 02:18:36 +00:00
|
|
|
class EncryptionParameters
|
|
|
|
{
|
|
|
|
friend class QPDF;
|
|
|
|
public:
|
|
|
|
EncryptionParameters();
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool encrypted;
|
|
|
|
bool encryption_initialized;
|
|
|
|
int encryption_V;
|
|
|
|
int encryption_R;
|
|
|
|
bool encrypt_metadata;
|
|
|
|
std::map<std::string, encryption_method_e> crypt_filters;
|
|
|
|
encryption_method_e cf_stream;
|
|
|
|
encryption_method_e cf_string;
|
|
|
|
encryption_method_e cf_file;
|
|
|
|
std::string provided_password;
|
|
|
|
std::string user_password;
|
|
|
|
std::string encryption_key;
|
|
|
|
std::string cached_object_encryption_key;
|
|
|
|
int cached_key_objid;
|
|
|
|
int cached_key_generation;
|
2019-08-24 13:34:48 +00:00
|
|
|
bool user_password_matched;
|
|
|
|
bool owner_password_matched;
|
2019-01-07 02:18:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ForeignStreamData
|
|
|
|
{
|
|
|
|
friend class QPDF;
|
|
|
|
public:
|
|
|
|
ForeignStreamData(
|
|
|
|
PointerHolder<EncryptionParameters> encp,
|
|
|
|
PointerHolder<InputSource> file,
|
|
|
|
int foreign_objid,
|
|
|
|
int foreign_generation,
|
|
|
|
qpdf_offset_t offset,
|
|
|
|
size_t length,
|
|
|
|
QPDFObjectHandle local_dict);
|
|
|
|
|
|
|
|
private:
|
|
|
|
PointerHolder<EncryptionParameters> encp;
|
|
|
|
PointerHolder<InputSource> file;
|
|
|
|
int foreign_objid;
|
|
|
|
int foreign_generation;
|
|
|
|
qpdf_offset_t offset;
|
|
|
|
size_t length;
|
|
|
|
QPDFObjectHandle local_dict;
|
|
|
|
};
|
|
|
|
|
2012-07-11 19:29:41 +00:00
|
|
|
class CopiedStreamDataProvider: public QPDFObjectHandle::StreamDataProvider
|
|
|
|
{
|
|
|
|
public:
|
2019-01-07 02:18:36 +00:00
|
|
|
CopiedStreamDataProvider(QPDF& destination_qpdf);
|
2012-07-11 19:29:41 +00:00
|
|
|
virtual ~CopiedStreamDataProvider()
|
|
|
|
{
|
|
|
|
}
|
2020-04-05 03:35:35 +00:00
|
|
|
virtual bool provideStreamData(
|
|
|
|
int objid, int generation, Pipeline* pipeline,
|
|
|
|
bool suppress_warnings, bool will_retry) override;
|
2013-06-14 15:22:04 +00:00
|
|
|
void registerForeignStream(QPDFObjGen const& local_og,
|
2012-07-11 19:29:41 +00:00
|
|
|
QPDFObjectHandle foreign_stream);
|
2019-01-07 02:18:36 +00:00
|
|
|
void registerForeignStream(QPDFObjGen const& local_og,
|
|
|
|
PointerHolder<ForeignStreamData>);
|
2012-07-11 19:29:41 +00:00
|
|
|
|
|
|
|
private:
|
2019-01-07 02:18:36 +00:00
|
|
|
QPDF& destination_qpdf;
|
2013-06-14 15:22:04 +00:00
|
|
|
std::map<QPDFObjGen, QPDFObjectHandle> foreign_streams;
|
2019-01-07 02:18:36 +00:00
|
|
|
std::map<QPDFObjGen,
|
|
|
|
PointerHolder<ForeignStreamData> > foreign_stream_data;
|
2012-07-11 19:29:41 +00:00
|
|
|
};
|
|
|
|
|
2012-07-21 13:00:06 +00:00
|
|
|
class StringDecrypter: public QPDFObjectHandle::StringDecrypter
|
|
|
|
{
|
|
|
|
friend class QPDF;
|
|
|
|
|
|
|
|
public:
|
|
|
|
StringDecrypter(QPDF* qpdf, int objid, int gen);
|
|
|
|
virtual ~StringDecrypter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void decryptString(std::string& val);
|
|
|
|
|
|
|
|
private:
|
|
|
|
QPDF* qpdf;
|
|
|
|
int objid;
|
|
|
|
int gen;
|
|
|
|
};
|
|
|
|
|
2017-07-26 09:03:38 +00:00
|
|
|
class ResolveRecorder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ResolveRecorder(QPDF* qpdf, QPDFObjGen const& og) :
|
|
|
|
qpdf(qpdf),
|
|
|
|
og(og)
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
qpdf->m->resolving.insert(og);
|
2017-07-26 09:03:38 +00:00
|
|
|
}
|
|
|
|
virtual ~ResolveRecorder()
|
|
|
|
{
|
2017-08-22 01:33:44 +00:00
|
|
|
this->qpdf->m->resolving.erase(og);
|
2017-07-26 09:03:38 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
QPDF* qpdf;
|
|
|
|
QPDFObjGen og;
|
|
|
|
};
|
|
|
|
friend class ResolveRecorder;
|
|
|
|
|
2010-10-01 10:20:38 +00:00
|
|
|
void parse(char const* password);
|
2019-08-21 16:50:36 +00:00
|
|
|
void inParse(bool);
|
2008-04-29 12:55:25 +00:00
|
|
|
void setTrailer(QPDFObjectHandle obj);
|
2012-06-21 23:32:21 +00:00
|
|
|
void read_xref(qpdf_offset_t offset);
|
2008-04-29 12:55:25 +00:00
|
|
|
void reconstruct_xref(QPDFExc& e);
|
2017-08-10 23:37:05 +00:00
|
|
|
bool parse_xrefFirst(std::string const& line,
|
|
|
|
int& obj, int& num, int& bytes);
|
|
|
|
bool parse_xrefEntry(std::string const& line,
|
|
|
|
qpdf_offset_t& f1, int& f2, char& type);
|
2012-06-23 19:08:21 +00:00
|
|
|
qpdf_offset_t read_xrefTable(qpdf_offset_t offset);
|
|
|
|
qpdf_offset_t read_xrefStream(qpdf_offset_t offset);
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t processXRefStream(
|
|
|
|
qpdf_offset_t offset, QPDFObjectHandle& xref_stream);
|
|
|
|
void insertXrefEntry(int obj, int f0, qpdf_offset_t f1, int f2,
|
2009-02-21 02:31:32 +00:00
|
|
|
bool overwrite = false);
|
2009-10-19 23:09:19 +00:00
|
|
|
void setLastObjectDescription(std::string const& description,
|
|
|
|
int objid, int generation);
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle readObject(
|
2010-09-24 19:10:08 +00:00
|
|
|
PointerHolder<InputSource>, std::string const& description,
|
2009-10-19 23:09:19 +00:00
|
|
|
int objid, int generation, bool in_object_stream);
|
2012-06-20 15:20:57 +00:00
|
|
|
size_t recoverStreamLength(
|
2010-09-24 19:10:08 +00:00
|
|
|
PointerHolder<InputSource> input, int objid, int generation,
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t stream_offset);
|
2017-08-09 01:44:37 +00:00
|
|
|
QPDFTokenizer::Token readToken(PointerHolder<InputSource>,
|
2017-08-22 14:24:19 +00:00
|
|
|
size_t max_len = 0);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
QPDFObjectHandle readObjectAtOffset(
|
2009-10-19 23:09:19 +00:00
|
|
|
bool attempt_recovery,
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t offset, std::string const& description,
|
2008-04-29 12:55:25 +00:00
|
|
|
int exp_objid, int exp_generation,
|
|
|
|
int& act_objid, int& act_generation);
|
2021-02-25 11:34:03 +00:00
|
|
|
bool objectChanged(QPDFObjGen const& og, PointerHolder<QPDFObject>& oph);
|
2008-04-29 12:55:25 +00:00
|
|
|
PointerHolder<QPDFObject> resolve(int objid, int generation);
|
|
|
|
void resolveObjectsInStream(int obj_stream_number);
|
2019-01-04 16:50:02 +00:00
|
|
|
void stopOnError(std::string const& message);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// Calls finish() on the pipeline when done but does not delete it
|
2017-07-28 03:42:27 +00:00
|
|
|
bool pipeStreamData(int objid, int generation,
|
2012-06-21 23:32:21 +00:00
|
|
|
qpdf_offset_t offset, size_t length,
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle dict,
|
2017-07-28 03:42:27 +00:00
|
|
|
Pipeline* pipeline,
|
2017-09-12 19:48:08 +00:00
|
|
|
bool suppress_warnings,
|
|
|
|
bool will_retry);
|
2019-01-07 02:18:36 +00:00
|
|
|
bool pipeForeignStreamData(
|
|
|
|
PointerHolder<ForeignStreamData>,
|
2020-04-05 03:35:35 +00:00
|
|
|
Pipeline*, bool suppress_warnings, bool will_retry);
|
2019-01-06 15:34:52 +00:00
|
|
|
static bool pipeStreamData(PointerHolder<QPDF::EncryptionParameters> encp,
|
|
|
|
PointerHolder<InputSource> file,
|
|
|
|
QPDF& qpdf_for_warning,
|
|
|
|
int objid, int generation,
|
|
|
|
qpdf_offset_t offset, size_t length,
|
|
|
|
QPDFObjectHandle dict,
|
|
|
|
Pipeline* pipeline,
|
|
|
|
bool suppress_warnings,
|
|
|
|
bool will_retry);
|
2012-06-18 23:53:10 +00:00
|
|
|
|
2013-07-10 15:07:40 +00:00
|
|
|
// For QPDFWriter:
|
|
|
|
|
|
|
|
// Get lists of all objects in order according to the part of a
|
|
|
|
// linearized file that they belong to.
|
|
|
|
void getLinearizedParts(
|
|
|
|
std::map<int, int> const& object_stream_data,
|
|
|
|
std::vector<QPDFObjectHandle>& part4,
|
|
|
|
std::vector<QPDFObjectHandle>& part6,
|
|
|
|
std::vector<QPDFObjectHandle>& part7,
|
|
|
|
std::vector<QPDFObjectHandle>& part8,
|
|
|
|
std::vector<QPDFObjectHandle>& part9);
|
|
|
|
|
|
|
|
void generateHintStream(std::map<int, QPDFXRefEntry> const& xref,
|
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
|
|
|
std::map<int, int> const& obj_renumber,
|
|
|
|
PointerHolder<Buffer>& hint_stream,
|
|
|
|
int& S, int& O);
|
|
|
|
|
|
|
|
// Map object to object stream that contains it
|
|
|
|
void getObjectStreamData(std::map<int, int>&);
|
|
|
|
|
|
|
|
// Get a list of objects that would be permitted in an object
|
|
|
|
// stream.
|
|
|
|
std::vector<QPDFObjGen> getCompressibleObjGens();
|
|
|
|
|
2012-06-18 23:53:10 +00:00
|
|
|
// methods to support page handling
|
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
void getAllPagesInternal(QPDFObjectHandle cur_pages,
|
2019-01-30 03:04:52 +00:00
|
|
|
std::vector<QPDFObjectHandle>& result,
|
|
|
|
std::set<QPDFObjGen>& visited,
|
|
|
|
std::set<QPDFObjGen>& seen);
|
2012-06-21 14:42:18 +00:00
|
|
|
void insertPage(QPDFObjectHandle newpage, int pos);
|
2012-06-18 23:53:10 +00:00
|
|
|
void flattenPagesTree();
|
2012-06-21 14:42:18 +00:00
|
|
|
void insertPageobjToPage(QPDFObjectHandle const& obj, int pos,
|
|
|
|
bool check_duplicate);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
|
|
|
// methods to support encryption -- implemented in QPDF_encryption.cc
|
2019-01-06 15:34:52 +00:00
|
|
|
static encryption_method_e interpretCF(
|
|
|
|
PointerHolder<EncryptionParameters> encp, QPDFObjectHandle);
|
2008-04-29 12:55:25 +00:00
|
|
|
void initializeEncryption();
|
2019-01-06 15:34:52 +00:00
|
|
|
static std::string getKeyForObject(
|
|
|
|
PointerHolder<EncryptionParameters> encp,
|
|
|
|
int objid, int generation, bool use_aes);
|
2008-04-29 12:55:25 +00:00
|
|
|
void decryptString(std::string&, int objid, int generation);
|
2012-12-30 02:31:21 +00:00
|
|
|
static std::string compute_encryption_key_from_password(
|
|
|
|
std::string const& password, EncryptionData const& data);
|
|
|
|
static std::string recover_encryption_key_with_password(
|
|
|
|
std::string const& password, EncryptionData const& data);
|
|
|
|
static std::string recover_encryption_key_with_password(
|
|
|
|
std::string const& password, EncryptionData const& data,
|
|
|
|
bool& perms_valid);
|
2019-01-06 15:34:52 +00:00
|
|
|
static void decryptStream(
|
|
|
|
PointerHolder<EncryptionParameters> encp,
|
|
|
|
PointerHolder<InputSource> file,
|
|
|
|
QPDF& qpdf_for_warning, Pipeline*& pipeline,
|
|
|
|
int objid, int generation,
|
2021-02-06 21:25:10 +00:00
|
|
|
QPDFObjectHandle& stream_dict,
|
2009-10-17 18:54:51 +00:00
|
|
|
std::vector<PointerHolder<Pipeline> >& heap);
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2019-01-29 02:53:55 +00:00
|
|
|
// Methods to support object copying
|
2012-07-11 19:29:41 +00:00
|
|
|
void reserveObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier,
|
|
|
|
bool top);
|
|
|
|
QPDFObjectHandle replaceForeignIndirectObjects(
|
|
|
|
QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top);
|
2021-02-21 10:56:52 +00:00
|
|
|
void copyStreamData(
|
|
|
|
QPDFObjectHandle dest_stream, QPDFObjectHandle src_stream);
|
2012-07-11 19:29:41 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// Linearization Hint table structures.
|
|
|
|
// Naming conventions:
|
|
|
|
|
|
|
|
// HSomething is the Something Hint Table or table header
|
|
|
|
// HSomethingEntry is an entry in the Something table
|
|
|
|
|
|
|
|
// delta_something + min_something = something
|
|
|
|
// nbits_something = number of bits required for something
|
|
|
|
|
|
|
|
// something_offset is the pre-adjusted offset in the file. If >=
|
|
|
|
// H0_offset, H0_length must be added to get an actual file
|
|
|
|
// offset.
|
|
|
|
|
|
|
|
// PDF 1.4: Table F.4
|
|
|
|
struct HPageOffsetEntry
|
|
|
|
{
|
|
|
|
HPageOffsetEntry() :
|
|
|
|
delta_nobjects(0),
|
|
|
|
delta_page_length(0),
|
|
|
|
nshared_objects(0),
|
|
|
|
delta_content_offset(0),
|
|
|
|
delta_content_length(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int delta_nobjects; // 1
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t delta_page_length; // 2
|
2008-04-29 12:55:25 +00:00
|
|
|
int nshared_objects; // 3
|
|
|
|
// vectors' sizes = nshared_objects
|
|
|
|
std::vector<int> shared_identifiers; // 4
|
|
|
|
std::vector<int> shared_numerators; // 5
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t delta_content_offset; // 6
|
|
|
|
qpdf_offset_t delta_content_length; // 7
|
2008-04-29 12:55:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// PDF 1.4: Table F.3
|
|
|
|
struct HPageOffset
|
|
|
|
{
|
|
|
|
HPageOffset() :
|
|
|
|
min_nobjects(0),
|
|
|
|
first_page_offset(0),
|
|
|
|
nbits_delta_nobjects(0),
|
|
|
|
min_page_length(0),
|
|
|
|
nbits_delta_page_length(0),
|
|
|
|
min_content_offset(0),
|
|
|
|
nbits_delta_content_offset(0),
|
|
|
|
min_content_length(0),
|
|
|
|
nbits_delta_content_length(0),
|
|
|
|
nbits_nshared_objects(0),
|
|
|
|
nbits_shared_identifier(0),
|
|
|
|
nbits_shared_numerator(0),
|
|
|
|
shared_denominator(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int min_nobjects; // 1
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t first_page_offset; // 2
|
2008-04-29 12:55:25 +00:00
|
|
|
int nbits_delta_nobjects; // 3
|
|
|
|
int min_page_length; // 4
|
|
|
|
int nbits_delta_page_length; // 5
|
|
|
|
int min_content_offset; // 6
|
|
|
|
int nbits_delta_content_offset; // 7
|
|
|
|
int min_content_length; // 8
|
|
|
|
int nbits_delta_content_length; // 9
|
|
|
|
int nbits_nshared_objects; // 10
|
|
|
|
int nbits_shared_identifier; // 11
|
|
|
|
int nbits_shared_numerator; // 12
|
|
|
|
int shared_denominator; // 13
|
|
|
|
// vector size is npages
|
|
|
|
std::vector<HPageOffsetEntry> entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
// PDF 1.4: Table F.6
|
|
|
|
struct HSharedObjectEntry
|
|
|
|
{
|
|
|
|
HSharedObjectEntry() :
|
|
|
|
delta_group_length(0),
|
|
|
|
signature_present(0),
|
|
|
|
nobjects_minus_one(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Item 3 is a 128-bit signature (unsupported by Acrobat)
|
|
|
|
int delta_group_length; // 1
|
|
|
|
int signature_present; // 2 -- always 0
|
|
|
|
int nobjects_minus_one; // 4 -- always 0
|
|
|
|
};
|
|
|
|
|
|
|
|
// PDF 1.4: Table F.5
|
|
|
|
struct HSharedObject
|
|
|
|
{
|
|
|
|
HSharedObject() :
|
|
|
|
first_shared_obj(0),
|
|
|
|
first_shared_offset(0),
|
|
|
|
nshared_first_page(0),
|
|
|
|
nshared_total(0),
|
|
|
|
nbits_nobjects(0),
|
|
|
|
min_group_length(0),
|
|
|
|
nbits_delta_group_length(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int first_shared_obj; // 1
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t first_shared_offset; // 2
|
2008-04-29 12:55:25 +00:00
|
|
|
int nshared_first_page; // 3
|
|
|
|
int nshared_total; // 4
|
|
|
|
int nbits_nobjects; // 5
|
|
|
|
int min_group_length; // 6
|
|
|
|
int nbits_delta_group_length; // 7
|
|
|
|
// vector size is nshared_total
|
|
|
|
std::vector<HSharedObjectEntry> entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
// PDF 1.4: Table F.9
|
|
|
|
struct HGeneric
|
|
|
|
{
|
|
|
|
HGeneric() :
|
|
|
|
first_object(0),
|
|
|
|
first_object_offset(0),
|
|
|
|
nobjects(0),
|
|
|
|
group_length(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int first_object; // 1
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t first_object_offset; // 2
|
2008-04-29 12:55:25 +00:00
|
|
|
int nobjects; // 3
|
|
|
|
int group_length; // 4
|
|
|
|
};
|
|
|
|
|
|
|
|
// Other linearization data structures
|
|
|
|
|
|
|
|
// Initialized from Linearization Parameter dictionary
|
|
|
|
struct LinParameters
|
|
|
|
{
|
|
|
|
LinParameters() :
|
|
|
|
file_size(0),
|
|
|
|
first_page_object(0),
|
|
|
|
first_page_end(0),
|
|
|
|
npages(0),
|
|
|
|
xref_zero_offset(0),
|
|
|
|
first_page(0),
|
|
|
|
H_offset(0),
|
|
|
|
H_length(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:26:28 +00:00
|
|
|
qpdf_offset_t file_size; // /L
|
|
|
|
int first_page_object; // /O
|
|
|
|
qpdf_offset_t first_page_end; // /E
|
|
|
|
int npages; // /N
|
|
|
|
qpdf_offset_t xref_zero_offset; // /T
|
|
|
|
int first_page; // /P
|
|
|
|
qpdf_offset_t H_offset; // offset of primary hint stream
|
|
|
|
qpdf_offset_t H_length; // length of primary hint stream
|
2008-04-29 12:55:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Computed hint table value data structures. These tables
|
|
|
|
// contain the computed values on which the hint table values are
|
|
|
|
// based. They exclude things like number of bits and store
|
|
|
|
// actual values instead of mins and deltas. File offsets are
|
|
|
|
// also absolute rather than being offset by the size of the
|
|
|
|
// primary hint table. We populate the hint table structures from
|
|
|
|
// these during writing and compare the hint table values with
|
|
|
|
// these during validation. We ignore some values for various
|
|
|
|
// reasons described in the code. Those values are omitted from
|
|
|
|
// these structures. Note also that object numbers are object
|
|
|
|
// numbers from the input file, not the output file.
|
|
|
|
|
|
|
|
// Naming convention: CHSomething is analogous to HSomething
|
|
|
|
// above. "CH" is computed hint.
|
|
|
|
|
|
|
|
struct CHPageOffsetEntry
|
|
|
|
{
|
|
|
|
CHPageOffsetEntry() :
|
|
|
|
nobjects(0),
|
|
|
|
nshared_objects(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int nobjects;
|
|
|
|
int nshared_objects;
|
|
|
|
// vectors' sizes = nshared_objects
|
|
|
|
std::vector<int> shared_identifiers;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CHPageOffset
|
|
|
|
{
|
|
|
|
// vector size is npages
|
|
|
|
std::vector<CHPageOffsetEntry> entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CHSharedObjectEntry
|
|
|
|
{
|
|
|
|
CHSharedObjectEntry(int object) :
|
|
|
|
object(object)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int object;
|
|
|
|
};
|
|
|
|
|
|
|
|
// PDF 1.4: Table F.5
|
|
|
|
struct CHSharedObject
|
|
|
|
{
|
|
|
|
CHSharedObject() :
|
|
|
|
first_shared_obj(0),
|
|
|
|
nshared_first_page(0),
|
|
|
|
nshared_total(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int first_shared_obj;
|
|
|
|
int nshared_first_page;
|
|
|
|
int nshared_total;
|
|
|
|
// vector size is nshared_total
|
|
|
|
std::vector<CHSharedObjectEntry> entries;
|
|
|
|
};
|
|
|
|
|
|
|
|
// No need for CHGeneric -- HGeneric is fine as is.
|
|
|
|
|
|
|
|
|
|
|
|
// Data structures to support optimization -- implemented in
|
|
|
|
// QPDF_optimization.cc
|
|
|
|
|
|
|
|
class ObjUser
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum user_e
|
|
|
|
{
|
|
|
|
ou_bad,
|
|
|
|
ou_page,
|
|
|
|
ou_thumb,
|
|
|
|
ou_trailer_key,
|
|
|
|
ou_root_key,
|
|
|
|
ou_root
|
|
|
|
};
|
|
|
|
|
|
|
|
// type is set to ou_bad
|
|
|
|
ObjUser();
|
|
|
|
|
|
|
|
// type must be ou_root
|
|
|
|
ObjUser(user_e type);
|
|
|
|
|
|
|
|
// type must be one of ou_page or ou_thumb
|
|
|
|
ObjUser(user_e type, int pageno);
|
|
|
|
|
|
|
|
// type must be one of ou_trailer_key or ou_root_key
|
|
|
|
ObjUser(user_e type, std::string const& key);
|
|
|
|
|
|
|
|
bool operator<(ObjUser const&) const;
|
|
|
|
|
|
|
|
user_e ou_type;
|
|
|
|
int pageno; // if ou_page;
|
|
|
|
std::string key; // if ou_trailer_key or ou_root_key
|
|
|
|
};
|
|
|
|
|
2017-08-06 12:42:01 +00:00
|
|
|
class PatternFinder: public InputSource::Finder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PatternFinder(QPDF& qpdf, bool (QPDF::*checker)()) :
|
|
|
|
qpdf(qpdf),
|
|
|
|
checker(checker)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~PatternFinder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual bool check()
|
|
|
|
{
|
|
|
|
return (this->qpdf.*checker)();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QPDF& qpdf;
|
|
|
|
bool (QPDF::*checker)();
|
|
|
|
};
|
|
|
|
|
2017-08-06 02:34:25 +00:00
|
|
|
// Methods to support pattern finding
|
|
|
|
bool findHeader();
|
2017-08-05 18:54:07 +00:00
|
|
|
bool findStartxref();
|
2017-08-10 01:14:48 +00:00
|
|
|
bool findEndstream();
|
2017-08-06 02:34:25 +00:00
|
|
|
|
2008-04-29 12:55:25 +00:00
|
|
|
// methods to support linearization checking -- implemented in
|
|
|
|
// QPDF_linearization.cc
|
|
|
|
void readLinearizationData();
|
2019-06-23 13:10:42 +00:00
|
|
|
bool checkLinearizationInternal();
|
2008-04-29 12:55:25 +00:00
|
|
|
void dumpLinearizationDataInternal();
|
2012-06-21 23:32:21 +00:00
|
|
|
QPDFObjectHandle readHintStream(
|
|
|
|
Pipeline&, qpdf_offset_t offset, size_t length);
|
2008-04-29 12:55:25 +00:00
|
|
|
void readHPageOffset(BitStream);
|
|
|
|
void readHSharedObject(BitStream);
|
|
|
|
void readHGeneric(BitStream, HGeneric&);
|
2013-02-24 02:46:21 +00:00
|
|
|
qpdf_offset_t maxEnd(ObjUser const& ou);
|
2013-06-14 15:22:04 +00:00
|
|
|
qpdf_offset_t getLinearizationOffset(QPDFObjGen const&);
|
2008-04-29 12:55:25 +00:00
|
|
|
QPDFObjectHandle getUncompressedObject(
|
|
|
|
QPDFObjectHandle&, std::map<int, int> const& object_stream_data);
|
|
|
|
int lengthNextN(int first_object, int n,
|
|
|
|
std::list<std::string>& errors);
|
|
|
|
void checkHPageOffset(std::list<std::string>& errors,
|
|
|
|
std::list<std::string>& warnings,
|
|
|
|
std::vector<QPDFObjectHandle> const& pages,
|
|
|
|
std::map<int, int>& idx_to_obj);
|
|
|
|
void checkHSharedObject(std::list<std::string>& warnings,
|
|
|
|
std::list<std::string>& errors,
|
|
|
|
std::vector<QPDFObjectHandle> const& pages,
|
|
|
|
std::map<int, int>& idx_to_obj);
|
|
|
|
void checkHOutlines(std::list<std::string>& warnings);
|
|
|
|
void dumpHPageOffset();
|
|
|
|
void dumpHSharedObject();
|
|
|
|
void dumpHGeneric(HGeneric&);
|
2019-06-21 03:35:23 +00:00
|
|
|
qpdf_offset_t adjusted_offset(qpdf_offset_t offset);
|
2013-06-14 15:22:04 +00:00
|
|
|
QPDFObjectHandle objGenToIndirect(QPDFObjGen const&);
|
2008-04-29 12:55:25 +00:00
|
|
|
void calculateLinearizationData(
|
|
|
|
std::map<int, int> const& object_stream_data);
|
|
|
|
void pushOutlinesToPart(
|
|
|
|
std::vector<QPDFObjectHandle>& part,
|
2013-06-14 15:22:04 +00:00
|
|
|
std::set<QPDFObjGen>& lc_outlines,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::map<int, int> const& object_stream_data);
|
|
|
|
int outputLengthNextN(
|
|
|
|
int in_object, int n,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::map<int, int> const& obj_renumber);
|
|
|
|
void calculateHPageOffset(
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::map<int, int> const& obj_renumber);
|
|
|
|
void calculateHSharedObject(
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::map<int, int> const& obj_renumber);
|
|
|
|
void calculateHOutline(
|
|
|
|
std::map<int, QPDFXRefEntry> const& xref,
|
2012-06-24 19:26:28 +00:00
|
|
|
std::map<int, qpdf_offset_t> const& lengths,
|
2008-04-29 12:55:25 +00:00
|
|
|
std::map<int, int> const& obj_renumber);
|
|
|
|
void writeHPageOffset(BitWriter&);
|
|
|
|
void writeHSharedObject(BitWriter&);
|
|
|
|
void writeHGeneric(BitWriter&, HGeneric&);
|
|
|
|
|
|
|
|
|
|
|
|
// Methods to support optimization
|
|
|
|
|
2012-06-22 16:52:13 +00:00
|
|
|
void pushInheritedAttributesToPage(bool allow_changes,
|
|
|
|
bool warn_skipped_keys);
|
2012-06-22 15:50:02 +00:00
|
|
|
void pushInheritedAttributesToPageInternal(
|
2015-02-21 23:22:32 +00:00
|
|
|
QPDFObjectHandle,
|
|
|
|
std::map<std::string, std::vector<QPDFObjectHandle> >&,
|
|
|
|
std::vector<QPDFObjectHandle>& all_pages,
|
|
|
|
bool allow_changes, bool warn_skipped_keys,
|
|
|
|
std::set<QPDFObjGen>& visited);
|
2020-12-25 14:50:55 +00:00
|
|
|
void updateObjectMaps(
|
|
|
|
ObjUser const& ou, QPDFObjectHandle oh,
|
|
|
|
std::function<int(QPDFObjectHandle&)> skip_stream_parameters);
|
|
|
|
void updateObjectMapsInternal(
|
|
|
|
ObjUser const& ou, QPDFObjectHandle oh,
|
|
|
|
std::function<int(QPDFObjectHandle&)> skip_stream_parameters,
|
|
|
|
std::set<QPDFObjGen>& visited, bool top,
|
|
|
|
int depth);
|
2008-04-29 12:55:25 +00:00
|
|
|
void filterCompressedObjects(std::map<int, int> const& object_stream_data);
|
|
|
|
|
2019-06-21 03:35:23 +00:00
|
|
|
// Type conversion helper methods
|
|
|
|
template<typename T> static qpdf_offset_t toO(T const& i)
|
|
|
|
{
|
|
|
|
return QIntC::to_offset(i);
|
|
|
|
}
|
|
|
|
template<typename T> static size_t toS(T const& i)
|
|
|
|
{
|
|
|
|
return QIntC::to_size(i);
|
|
|
|
}
|
|
|
|
template<typename T> static int toI(T const& i)
|
|
|
|
{
|
|
|
|
return QIntC::to_int(i);
|
|
|
|
}
|
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
class Members
|
|
|
|
{
|
|
|
|
friend class QPDF;
|
2019-02-13 09:42:07 +00:00
|
|
|
friend class ResolveRecorder;
|
2017-08-22 01:33:44 +00:00
|
|
|
|
|
|
|
public:
|
2018-08-05 00:08:06 +00:00
|
|
|
QPDF_DLL
|
2017-08-22 01:33:44 +00:00
|
|
|
~Members();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Members();
|
|
|
|
Members(Members const&);
|
|
|
|
|
2019-01-06 15:07:23 +00:00
|
|
|
unsigned long long unique_id;
|
2017-08-22 01:33:44 +00:00
|
|
|
QPDFTokenizer tokenizer;
|
|
|
|
PointerHolder<InputSource> file;
|
|
|
|
std::string last_object_description;
|
2018-01-14 15:17:17 +00:00
|
|
|
bool provided_password_is_hex_key;
|
2017-08-22 01:33:44 +00:00
|
|
|
bool ignore_xref_streams;
|
|
|
|
bool suppress_warnings;
|
|
|
|
std::ostream* out_stream;
|
|
|
|
std::ostream* err_stream;
|
|
|
|
bool attempt_recovery;
|
2019-01-06 14:58:16 +00:00
|
|
|
PointerHolder<EncryptionParameters> encp;
|
2017-08-22 01:33:44 +00:00
|
|
|
std::string pdf_version;
|
|
|
|
std::map<QPDFObjGen, QPDFXRefEntry> xref_table;
|
|
|
|
std::set<int> deleted_objects;
|
|
|
|
std::map<QPDFObjGen, ObjCache> obj_cache;
|
|
|
|
std::set<QPDFObjGen> resolving;
|
|
|
|
QPDFObjectHandle trailer;
|
|
|
|
std::vector<QPDFObjectHandle> all_pages;
|
|
|
|
std::map<QPDFObjGen, int> pageobj_to_pages_pos;
|
|
|
|
bool pushed_inherited_attributes_to_pages;
|
|
|
|
std::vector<QPDFExc> warnings;
|
2019-01-06 15:07:23 +00:00
|
|
|
std::map<unsigned long long, ObjCopier> object_copiers;
|
2017-08-22 01:33:44 +00:00
|
|
|
PointerHolder<QPDFObjectHandle::StreamDataProvider> copied_streams;
|
|
|
|
// copied_stream_data_provider is owned by copied_streams
|
|
|
|
CopiedStreamDataProvider* copied_stream_data_provider;
|
|
|
|
bool reconstructed_xref;
|
2019-01-04 15:17:33 +00:00
|
|
|
bool fixed_dangling_refs;
|
2019-01-11 03:11:38 +00:00
|
|
|
bool immediate_copy_from;
|
2019-08-21 16:50:36 +00:00
|
|
|
bool in_parse;
|
2019-09-28 13:07:00 +00:00
|
|
|
bool parsed;
|
2020-10-22 19:19:48 +00:00
|
|
|
std::set<int> resolved_object_streams;
|
2021-02-25 11:34:03 +00:00
|
|
|
bool ever_replaced_objects;
|
2017-08-22 01:33:44 +00:00
|
|
|
|
|
|
|
// Linearization data
|
|
|
|
qpdf_offset_t first_xref_item_offset; // actual value from file
|
|
|
|
bool uncompressed_after_compressed;
|
|
|
|
|
|
|
|
// Linearization parameter dictionary and hint table data: may be
|
|
|
|
// read from file or computed prior to writing a linearized file
|
|
|
|
QPDFObjectHandle lindict;
|
|
|
|
LinParameters linp;
|
|
|
|
HPageOffset page_offset_hints;
|
|
|
|
HSharedObject shared_object_hints;
|
|
|
|
HGeneric outline_hints;
|
|
|
|
|
|
|
|
// Computed linearization data: used to populate above tables
|
|
|
|
// during writing and to compare with them during validation.
|
|
|
|
// c_ means computed.
|
|
|
|
LinParameters c_linp;
|
|
|
|
CHPageOffset c_page_offset_data;
|
|
|
|
CHSharedObject c_shared_object_data;
|
|
|
|
HGeneric c_outline_data;
|
|
|
|
|
|
|
|
// Object ordering data for linearized files: initialized by
|
|
|
|
// calculateLinearizationData(). Part numbers refer to the PDF
|
|
|
|
// 1.4 specification.
|
|
|
|
std::vector<QPDFObjectHandle> part4;
|
|
|
|
std::vector<QPDFObjectHandle> part6;
|
|
|
|
std::vector<QPDFObjectHandle> part7;
|
|
|
|
std::vector<QPDFObjectHandle> part8;
|
|
|
|
std::vector<QPDFObjectHandle> part9;
|
|
|
|
|
|
|
|
// Optimization data
|
|
|
|
std::map<ObjUser, std::set<QPDFObjGen> > obj_user_to_objects;
|
|
|
|
std::map<QPDFObjGen, std::set<ObjUser> > object_to_obj_users;
|
|
|
|
};
|
2008-04-29 12:55:25 +00:00
|
|
|
|
2017-08-22 01:33:44 +00:00
|
|
|
// Keep all member variables inside the Members object, which we
|
|
|
|
// dynamically allocate. This makes it possible to add new private
|
|
|
|
// members without breaking binary compatibility.
|
|
|
|
PointerHolder<Members> m;
|
2008-04-29 12:55:25 +00:00
|
|
|
};
|
|
|
|
|
2018-08-12 18:07:22 +00:00
|
|
|
#endif // QPDF_HH
|