/usr/include/podofo/base/PdfParser.h is in libpodofo-dev 0.9.0-1.2+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | /***************************************************************************
* Copyright (C) 2005 by Dominik Seichter *
* domseichter@web.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _PDF_PARSER_H_
#define _PDF_PARSER_H_
#include "PdfDefines.h"
#include "PdfTokenizer.h"
#include "PdfVecObjects.h"
#define W_ARRAY_SIZE 3
#define W_MAX_BYTES 4
namespace PoDoFo {
typedef std::map<int,PdfObject*> TMapObjects;
typedef TMapObjects::iterator TIMapObjects;
typedef TMapObjects::const_iterator TCIMapObjects;
class PdfEncrypt;
class PdfString;
/**
* PdfParser reads a PDF file into memory.
* The file can be modified in memory and written back using
* the PdfWriter class.
* Most PDF features are supported
*/
class PODOFO_API PdfParser : public PdfTokenizer {
friend class PdfDocument;
friend class PdfWriter;
public:
struct TXRefEntry {
inline TXRefEntry() : lOffset(0), lGeneration(0), cUsed('\x00'), bParsed(false) { }
pdf_long lOffset;
long lGeneration;
char cUsed;
bool bParsed;
};
typedef std::vector<TXRefEntry> TVecOffsets;
typedef TVecOffsets::iterator TIVecOffsets;
typedef TVecOffsets::const_iterator TCIVecOffsets;
/** Create a new PdfParser object
* You have to open a PDF file using ParseFile later.
* \param pVecObjects vector to write the parsed PdfObjects to
*
* \see ParseFile
*/
PdfParser( PdfVecObjects* pVecObjects );
/** Create a new PdfParser object and open a PDF file and parse
* it into memory.
*
* \param pVecObjects vector to write the parsed PdfObjects to
* \param pszFilename filename of the file which is going to be parsed
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* \see SetPassword
*/
PdfParser( PdfVecObjects* pVecObjects, const char* pszFilename, bool bLoadOnDemand = true );
#ifdef _WIN32
#if defined(_MSC_VER) && _MSC_VER <= 1200 // nicht für Visualstudio 6
#else
/** Create a new PdfParser object and open a PDF file and parse
* it into memory.
*
* \param pVecObjects vector to write the parsed PdfObjects to
* \param pszFilename filename of the file which is going to be parsed
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* This is an overloaded member function to allow working
* with unicode characters. On Unix systes you can also path
* UTF-8 to the const char* overload.
*
* \see SetPassword
*/
PdfParser( PdfVecObjects* pVecObjects, const wchar_t* pszFilename, bool bLoadOnDemand = true );
#endif
#endif // _WIN32
/** Create a new PdfParser object and open a PDF file and parse
* it into memory.
*
* \param pVecObjects vector to write the parsed PdfObjects to
* \param pBuffer buffer containing a PDF file in memory
* \param lLen length of the buffer containing the PDF file
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* \see SetPassword
*/
PdfParser( PdfVecObjects* pVecObjects, const char* pBuffer, long lLen, bool bLoadOnDemand = true );
/** Create a new PdfParser object and open a PDF file and parse
* it into memory.
*
* \param pVecObjects vector to write the parsed PdfObjects to
* \param rDevice read from this PdfRefCountedInputDevice
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* \see SetPassword
*/
PdfParser( PdfVecObjects* pVecObjects, const PdfRefCountedInputDevice & rDevice,
bool bLoadOnDemand = true );
/** Delete the PdfParser and all PdfObjects
*/
virtual ~PdfParser();
/** Open a PDF file and parse it.
*
* \param pszFilename filename of the file which is going to be parsed
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* \see SetPassword
*/
void ParseFile( const char* pszFilename, bool bLoadOnDemand = true );
#ifdef _WIN32
/** Open a PDF file and parse it.
*
* \param pszFilename filename of the file which is going to be parsed
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* This is an overloaded member function to allow working
* with unicode characters. On Unix systes you can also path
* UTF-8 to the const char* overload.
*
* \see SetPassword
*/
void ParseFile( const wchar_t* pszFilename, bool bLoadOnDemand = true );
#endif // _WIN32
/** Open a PDF file and parse it.
*
* \param pBuffer buffer containing a PDF file in memory
* \param lLen length of the buffer containing the PDF file
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* \see SetPassword
*/
void ParseFile( const char* pBuffer, long lLen, bool bLoadOnDemand = true );
/** Open a PDF file and parse it.
*
* \param rDevice the input device to read from
* \param bLoadOnDemand If true all objects will be read from the file at
* the time they are accesed first.
* If false all objects will be read immediately.
* This is faster if you do not need the complete PDF
* file in memory.
*
*
* This might throw a PdfError( ePdfError_InvalidPassword ) exception
* if a password is required to read this PDF.
* Call SetPassword with the correct password in this case.
*
* \see SetPassword
*/
void ParseFile( const PdfRefCountedInputDevice & rDevice, bool bLoadOnDemand = true );
/** Quick method to detect secured PDF files, i.e.
* a PDF with an /Encrypt key in the trailer directory.
*
* \returns true if document is secured, false otherwise
*/
bool QuickEncryptedCheck( const char* pszFilename );
/**
* Retrieve the number of incremental updates that
* have been applied to the last parsed PDF file.
*
* 0 means no update has been applied.
*
* \returns the number of incremental updates to the parsed PDF.
*/
inline int GetNumberOfIncrementalUpdates() const;
/** Get a reference to the sorted internal objects vector.
* \returns the internal objects vector.
*/
inline const PdfVecObjects* GetObjects() const;
/** Get the file format version of the pdf
* \returns the file format version as enum
*/
inline EPdfVersion GetPdfVersion() const;
/** Get the file format version of the pdf
* \returns the file format version as string
*/
const char* GetPdfVersionString() const;
/** Get the trailer dictionary
* which can be written unmodified to a pdf file.
*/
inline const PdfObject* GetTrailer() const;
/** \returns true if this PdfParser loads all objects on demand at
* the time they are accessed for the first time.
* The default is to load all object immediately.
* In this case false is returned.
*/
inline bool GetLoadOnDemand() const;
/** \returns whether the parsed document contains linearization tables
*/
bool IsLinearized() const { return m_pLinearization != NULL; }
/** \returns the length of the file
*/
size_t GetFileSize() const { return m_nFileSize; }
/**
* \returns true if this PdfWriter creates an encrypted PDF file
*/
bool GetEncrypted() const { return (m_pEncrypt != NULL); }
/**
* \returns the parsers encryption object or NULL if the read PDF file was not encrypted
*/
const PdfEncrypt* GetEncrypt() const { return m_pEncrypt; }
/**
* Takes the encryption object fro mthe parser. The internal handle will be set
* to NULL and the ownership of the object is given to the caller.
*
* Only call this if you need access to the encryption object
* before deleting the parser.
*
* \returns the parsers encryption object or NULL if the read PDF file was not encrypted
*/
inline PdfEncrypt* TakeEncrypt();
/** If you try to open an encrypted PDF file, which requires
* a password to open, PoDoFo will throw a PdfError( ePdfError_InvalidPassword )
* exception.
*
* If you got such an exception, you have to set a password
* which should be used for opening the PDF.
*
* The usual way will be to ask the user for the password
* and set the password using this method.
*
* PdfParser will immediately continue to read the PDF file.
*
* \param sPassword a user or owner password which can be used to open an encrypted PDF file
* If the password is invalid, a PdfError( ePdfError_InvalidPassword ) exception is thrown!
*/
void SetPassword( const std::string & sPassword );
/**
* \returns true if strict parsing mode is enabled
*
* \see SetStringParsing
*/
inline bool IsStrictParsing() const;
/**
* Enable/disable strict parsing mode.
* Strict parsing is by default disabled.
*
* If you enable strict parsing, PoDoFo will fail
* on a few more common PDF failures. Please not
* that PoDoFo's parser is by default very strict
* already and does not recover from e.g. wrong XREF
* tables.
*
* \param bStrict new setting for strict parsing mode.
*/
inline void SetStrictParsing( bool bStrict );
/**
* \return if broken objects are ignored while parsing
*/
inline bool GetIgnoreBrokenObjects();
/**
* Specify if the parser should ignore broken
* objects, i.e. XRef entries that do not point
* to valid objects.
*
* Default is to not ignore broken objects and
* throw an exception if one is found.
*
* \param bBroken if true broken objects will be ignored
*/
inline void SetIgnoreBrokenObjects( bool bBroken );
protected:
/** Searches backwards from the end of the file
* and tries to find a token.
* The current file is positioned right after the token.
*
* \param pszToken a token to find
* \param lRange range in bytes in which to search
* begining at the end of the file
*/
void FindToken( const char* pszToken, const long lRange );
// Peter Petrov 23 December 2008
/** Searches backwards from the specified position of the file
* and tries to find a token.
* The current file is positioned right after the token.
*
* \param pszToken a token to find
* \param lRange range in bytes in which to search
* begining at the specified position of the file
* \param searchEnd specifies position
*/
void FindToken2( const char* pszToken, const long lRange, size_t searchEnd );
/** Reads the xref sections and the trailers of the file
* in the correct order in the memory
* and takes care for linearized pdf files.
*/
void ReadDocumentStructure();
/** Checks wether this pdf is linearized or not.
* Initializes the linearization directory on sucess.
*/
void HasLinearizationDict();
/** Merge the information of this trailer object
* in the parsers main trailer object.
* \param pTrailer take the keys to merge from this dictionary.
*/
void MergeTrailer( const PdfObject* pTrailer );
/** Read the trailer directory at the end of the file.
*/
void ReadTrailer();
/** Looks for a startxref entry at the current file position
* and saves its byteoffset to pXRefOffset.
* \param pXRefOffset store the byte offset of the xref section into this variable.
*/
void ReadXRef( pdf_long* pXRefOffset );
/** Reads the xref table from a pdf file.
* If there is no xref table, ReadXRefStreamContents() is called.
* \param lOffset read the table from this offset
* \param bPositionAtEnd if true the xref table is not read, but the
* file stream is positioned directly
* after the table, which allows reading
* a following trailer dictionary.
*/
void ReadXRefContents( pdf_long lOffset, bool bPositionAtEnd = false );
/** Read a xref subsection
*
* Throws ePdfError_NoXref if the number of objects read was not
* the number specified by the subsection header (as passed in
* `nNumObjects').
*
* \param nFirstObject object number of the first object
* \param nNumObjects how many objects should be read from this section
*/
void ReadXRefSubsection( long long & nFirstObject, long long & nNumObjects );
/** Reads a xref stream contens object
* \param lOffset read the stream from this offset
* \param bReadOnlyTrailer only the trailer is skipped over, the contents
* of the xref stream are not parsed
*/
void ReadXRefStreamContents( pdf_long lOffset, bool bReadOnlyTrailer );
/** Reads all objects from the pdf into memory
* from the offsets listed in m_vecOffsets.
*
* If required an encryption object is setup first.
*
* The actual reading happens in ReadObjectsInternal()
* either if no encryption is required or a correct
* encryption object was initialized from SetPassword.
*/
void ReadObjects();
/** Reads all objects from the pdf into memory
* from the offsets listed in m_vecOffsets.
*
* Requires a correctly setup PdfEncrypt object
* with correct password.
*
* This method is called from ReadObjects
* or SetPassword.
*
* \see ReadObjects
* \see SetPassword
*/
void ReadObjectsInternal();
/** Read the object with index nIndex from the object stream nObjNo
* and push it on the objects vector m_vecOffsets.
*
* All objects are read from this stream and the stream object
* is free'd from memory. Further calls who try to read from the
* same stream simply do nothing.
*
* \param nObjNo object number of the stream object
* \param nIndex index of the object which should be parsed
*
*/
void ReadObjectFromStream( int nObjNo, int nIndex );
/** Checks the magic number at the start of the pdf file
* and sets the m_ePdfVersion member to the correct version
* of the pdf file.
*
* \returns true if this is a pdf file, otherwise false
*/
bool IsPdfFile();
void ReadNextTrailer();
/** Checks for the existence of the %%EOF marker at the end of the file
* When strict mode is off it will also attempt to setup the parser to ignore
* any garbage after the last %%EOF marker
* Simply raises an error if there is a problem with the marker
*
*/
void CheckEOFMarker();
private:
/** Free all internal data structures
*/
void Clear();
/** Initializes all private members
* with their initial values.
*/
void Init();
/** Small helper method to retrieve the document id from the trailer
*
* \returns the document id of this PDF document
*/
const PdfString & GetDocumentId();
/** Determines the correct version of the PDF
* from the document catalog (if available).
* as, PDF > 1.4 allows updating the version.
*
* If no catalog dictionary is present or no /Version
* key is available, the version from the file header will
* be used.
*/
void UpdateDocumentVersion();
private:
EPdfVersion m_ePdfVersion;
bool m_bLoadOnDemand;
pdf_long m_nXRefOffset;
long m_nFirstObject;
long m_nNumObjects;
pdf_long m_nXRefLinearizedOffset;
size_t m_nFileSize;
pdf_long m_lLastEOFOffset;
TVecOffsets m_offsets;
PdfVecObjects* m_vecObjects;
PdfObject* m_pTrailer;
PdfObject* m_pLinearization;
PdfEncrypt* m_pEncrypt;
bool m_xrefSizeUnknown;
std::set<int> m_setObjectStreams;
bool m_bStrictParsing;
bool m_bIgnoreBrokenObjects;
int m_nIncrementalUpdates;
};
// -----------------------------------------------------
//
// -----------------------------------------------------
bool PdfParser::GetLoadOnDemand() const
{
return m_bLoadOnDemand;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
EPdfVersion PdfParser::GetPdfVersion() const
{
return m_ePdfVersion;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
int PdfParser::GetNumberOfIncrementalUpdates() const
{
return m_nIncrementalUpdates;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
const PdfVecObjects* PdfParser::GetObjects() const
{
return m_vecObjects;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
const PdfObject* PdfParser::GetTrailer() const
{
return m_pTrailer;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
PdfEncrypt* PdfParser::TakeEncrypt()
{
PdfEncrypt* pEncrypt = m_pEncrypt;
m_pEncrypt = NULL;
return pEncrypt;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
bool PdfParser::IsStrictParsing() const
{
return m_bStrictParsing;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
void PdfParser::SetStrictParsing( bool bStrict )
{
m_bStrictParsing = bStrict;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
bool PdfParser::GetIgnoreBrokenObjects()
{
return m_bIgnoreBrokenObjects;
}
// -----------------------------------------------------
//
// -----------------------------------------------------
void PdfParser::SetIgnoreBrokenObjects( bool bBroken )
{
m_bIgnoreBrokenObjects = bBroken;
}
};
#endif // _PDF_PARSER_H_
|