Parser.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2021-2022, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtrVector.h>
  8. #include <AK/SourceLocation.h>
  9. #include <AK/WeakPtr.h>
  10. #include <LibPDF/Object.h>
  11. #include <LibPDF/Operator.h>
  12. #include <LibPDF/Reader.h>
  13. #include <LibPDF/XRefTable.h>
  14. namespace PDF {
  15. class Document;
  16. class Parser final : public RefCounted<Parser> {
  17. public:
  18. enum class LinearizationResult {
  19. NotLinearized,
  20. Linearized,
  21. };
  22. static PDFErrorOr<Vector<Operator>> parse_operators(Document*, ReadonlyBytes);
  23. Parser(Document*, ReadonlyBytes);
  24. [[nodiscard]] ALWAYS_INLINE RefPtr<DictObject> const& trailer() const { return m_trailer; }
  25. void set_document(WeakPtr<Document> const&);
  26. // Parses the header and initializes the xref table and trailer
  27. PDFErrorOr<void> initialize();
  28. PDFErrorOr<Value> parse_object_with_index(u32 index);
  29. // Specialized version of parse_dict which aborts early if the dict being parsed
  30. // is not a page object
  31. PDFErrorOr<RefPtr<DictObject>> conditionally_parse_page_tree_node(u32 object_index);
  32. private:
  33. struct LinearizationDictionary {
  34. u32 length_of_file { 0 };
  35. u32 primary_hint_stream_offset { 0 };
  36. u32 primary_hint_stream_length { 0 };
  37. u32 overflow_hint_stream_offset { 0 };
  38. u32 overflow_hint_stream_length { 0 };
  39. u32 first_page_object_number { 0 };
  40. u32 offset_of_first_page_end { 0 };
  41. u16 number_of_pages { 0 };
  42. u32 offset_of_main_xref_table { 0 };
  43. u32 first_page { 0 }; // The page to initially open (I think, the spec isn't all that clear here)
  44. };
  45. struct PageOffsetHintTable {
  46. u32 least_number_of_objects_in_a_page { 0 };
  47. u32 location_of_first_page_object { 0 };
  48. u16 bits_required_for_object_number { 0 };
  49. u32 least_length_of_a_page { 0 };
  50. u16 bits_required_for_page_length { 0 };
  51. u32 least_offset_of_any_content_stream { 0 };
  52. u16 bits_required_for_content_stream_offsets { 0 };
  53. u32 least_content_stream_length { 0 };
  54. u16 bits_required_for_content_stream_length { 0 };
  55. u16 bits_required_for_number_of_shared_obj_refs { 0 };
  56. u16 bits_required_for_greatest_shared_obj_identifier { 0 };
  57. u16 bits_required_for_fraction_numerator { 0 };
  58. u16 shared_object_reference_fraction_denominator { 0 };
  59. };
  60. struct PageOffsetHintTableEntry {
  61. u32 objects_in_page_number { 0 };
  62. u32 page_length_number { 0 };
  63. u32 number_of_shared_objects { 0 };
  64. Vector<u32> shared_object_identifiers {};
  65. Vector<u32> shared_object_location_numerators {};
  66. u32 page_content_stream_offset_number { 0 };
  67. u32 page_content_stream_length_number { 0 };
  68. };
  69. friend struct AK::Formatter<LinearizationDictionary>;
  70. friend struct AK::Formatter<PageOffsetHintTable>;
  71. friend struct AK::Formatter<PageOffsetHintTableEntry>;
  72. explicit Parser(ReadonlyBytes);
  73. PDFErrorOr<void> parse_header();
  74. PDFErrorOr<LinearizationResult> initialize_linearization_dict();
  75. PDFErrorOr<void> initialize_linearized_xref_table();
  76. PDFErrorOr<void> initialize_non_linearized_xref_table();
  77. PDFErrorOr<void> initialize_hint_tables();
  78. PDFErrorOr<PageOffsetHintTable> parse_page_offset_hint_table(ReadonlyBytes hint_stream_bytes);
  79. Vector<PageOffsetHintTableEntry> parse_all_page_offset_hint_table_entries(PageOffsetHintTable const&, ReadonlyBytes hint_stream_bytes);
  80. PDFErrorOr<NonnullRefPtr<XRefTable>> parse_xref_table();
  81. PDFErrorOr<NonnullRefPtr<DictObject>> parse_file_trailer();
  82. bool navigate_to_before_eof_marker();
  83. bool navigate_to_after_startxref();
  84. String parse_comment();
  85. PDFErrorOr<Value> parse_value();
  86. PDFErrorOr<Value> parse_possible_indirect_value_or_ref();
  87. PDFErrorOr<NonnullRefPtr<IndirectValue>> parse_indirect_value(u32 index, u32 generation);
  88. PDFErrorOr<NonnullRefPtr<IndirectValue>> parse_indirect_value();
  89. PDFErrorOr<Value> parse_number();
  90. PDFErrorOr<NonnullRefPtr<NameObject>> parse_name();
  91. NonnullRefPtr<StringObject> parse_string();
  92. String parse_literal_string();
  93. String parse_hex_string();
  94. PDFErrorOr<NonnullRefPtr<ArrayObject>> parse_array();
  95. PDFErrorOr<NonnullRefPtr<DictObject>> parse_dict();
  96. PDFErrorOr<NonnullRefPtr<StreamObject>> parse_stream(NonnullRefPtr<DictObject> dict);
  97. PDFErrorOr<Vector<Operator>> parse_operators();
  98. void push_reference(Reference const& ref) { m_current_reference_stack.append(ref); }
  99. void pop_reference() { m_current_reference_stack.take_last(); }
  100. bool matches_eol() const;
  101. bool matches_whitespace() const;
  102. bool matches_number() const;
  103. bool matches_delimiter() const;
  104. bool matches_regular_character() const;
  105. bool consume_eol();
  106. bool consume_whitespace();
  107. char consume();
  108. void consume(int amount);
  109. bool consume(char);
  110. Error error(
  111. String const& message
  112. #ifdef PDF_DEBUG
  113. ,
  114. SourceLocation loc = SourceLocation::current()
  115. #endif
  116. ) const;
  117. Reader m_reader;
  118. WeakPtr<Document> m_document;
  119. RefPtr<XRefTable> m_xref_table;
  120. RefPtr<DictObject> m_trailer;
  121. Optional<LinearizationDictionary> m_linearization_dictionary;
  122. Vector<Reference> m_current_reference_stack;
  123. bool m_disable_encryption { false };
  124. };
  125. };