Parser.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2021, Matthew Olsson <mattco@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtrVector.h>
  8. #include <LibPDF/Command.h>
  9. #include <LibPDF/Object.h>
  10. #include <LibPDF/Reader.h>
  11. #include <LibPDF/XRefTable.h>
  12. namespace PDF {
  13. class Document;
  14. class Parser final : public RefCounted<Parser> {
  15. public:
  16. static Vector<Command> parse_graphics_commands(ReadonlyBytes const&);
  17. Parser(Badge<Document>, ReadonlyBytes const&);
  18. [[nodiscard]] ALWAYS_INLINE RefPtr<DictObject> const& trailer() const { return m_trailer; }
  19. void set_document(RefPtr<Document> const& document) { m_document = document; }
  20. // Parses the header and initializes the xref table and trailer
  21. bool initialize();
  22. Value parse_object_with_index(u32 index);
  23. // Specialized version of parse_dict which aborts early if the dict being parsed
  24. // is not a page object. A null RefPtr return indicates that the dict at this index
  25. // is not a page tree node, whereas ok == false indicates a malformed PDF file and
  26. // should cause an abort of the current operation.
  27. RefPtr<DictObject> conditionally_parse_page_tree_node(u32 object_index, bool& ok);
  28. private:
  29. struct LinearizationDictionary {
  30. u32 length_of_file { 0 };
  31. u32 primary_hint_stream_offset { 0 };
  32. u32 primary_hint_stream_length { 0 };
  33. u32 overflow_hint_stream_offset { 0 };
  34. u32 overflow_hint_stream_length { 0 };
  35. u32 first_page_object_number { 0 };
  36. u32 offset_of_first_page_end { 0 };
  37. u16 number_of_pages { 0 };
  38. u32 offset_of_main_xref_table { 0 };
  39. u32 first_page { 0 }; // The page to initially open (I think, the spec isn't all that clear here)
  40. };
  41. struct PageOffsetHintTable {
  42. u32 least_number_of_objects_in_a_page { 0 };
  43. u32 location_of_first_page_object { 0 };
  44. u16 bits_required_for_object_number { 0 };
  45. u32 least_length_of_a_page { 0 };
  46. u16 bits_required_for_page_length { 0 };
  47. u32 least_offset_of_any_content_stream { 0 };
  48. u16 bits_required_for_content_stream_offsets { 0 };
  49. u32 least_content_stream_length { 0 };
  50. u16 bits_required_for_content_stream_length { 0 };
  51. u16 bits_required_for_number_of_shared_obj_refs { 0 };
  52. u16 bits_required_for_greatest_shared_obj_identifier { 0 };
  53. u16 bits_required_for_fraction_numerator { 0 };
  54. u16 shared_object_reference_fraction_denominator { 0 };
  55. };
  56. struct PageOffsetHintTableEntry {
  57. u32 objects_in_page_number { 0 };
  58. u32 page_length_number { 0 };
  59. u32 number_of_shared_objects { 0 };
  60. Vector<u32> shared_object_identifiers {};
  61. Vector<u32> shared_object_location_numerators {};
  62. u32 page_content_stream_offset_number { 0 };
  63. u32 page_content_stream_length_number { 0 };
  64. };
  65. friend struct AK::Formatter<LinearizationDictionary>;
  66. friend struct AK::Formatter<PageOffsetHintTable>;
  67. friend struct AK::Formatter<PageOffsetHintTableEntry>;
  68. explicit Parser(ReadonlyBytes const&);
  69. bool parse_header();
  70. bool initialize_linearization_dict();
  71. bool initialize_linearized_xref_table();
  72. bool initialize_non_linearized_xref_table();
  73. bool initialize_hint_tables();
  74. Optional<PageOffsetHintTable> parse_page_offset_hint_table(ReadonlyBytes const& hint_stream_bytes);
  75. Optional<Vector<PageOffsetHintTableEntry>> parse_all_page_offset_hint_table_entries(PageOffsetHintTable const&, ReadonlyBytes const& hint_stream_bytes);
  76. RefPtr<XRefTable> parse_xref_table();
  77. RefPtr<DictObject> parse_file_trailer();
  78. bool navigate_to_before_eof_marker();
  79. bool navigate_to_after_startxref();
  80. // If the PDF is linearized, the first object will be the linearization
  81. // parameter dictionary, and it will always occur within the first 1024 bytes.
  82. // We do a very sloppy and context-free search for this object. A return value
  83. // of true does not necessarily mean this PDF is linearized, but a return value
  84. // of false does mean this PDF is not linearized.
  85. // FIXME: false doesn't guarantee non-linearization, but we VERIFY the result!
  86. bool sloppy_is_linearized();
  87. String parse_comment();
  88. Value parse_value();
  89. Value parse_possible_indirect_value_or_ref();
  90. RefPtr<IndirectValue> parse_indirect_value(int index, int generation);
  91. RefPtr<IndirectValue> parse_indirect_value();
  92. Value parse_number();
  93. RefPtr<NameObject> parse_name();
  94. RefPtr<StringObject> parse_string();
  95. String parse_literal_string();
  96. String parse_hex_string();
  97. RefPtr<ArrayObject> parse_array();
  98. RefPtr<DictObject> parse_dict();
  99. RefPtr<StreamObject> parse_stream(NonnullRefPtr<DictObject> dict);
  100. Vector<Command> parse_graphics_commands();
  101. bool matches_eol() const;
  102. bool matches_whitespace() const;
  103. bool matches_number() const;
  104. bool matches_delimiter() const;
  105. bool matches_regular_character() const;
  106. bool consume_eol();
  107. bool consume_whitespace();
  108. char consume();
  109. void consume(int amount);
  110. bool consume(char);
  111. Reader m_reader;
  112. RefPtr<Document> m_document;
  113. RefPtr<XRefTable> m_xref_table;
  114. RefPtr<DictObject> m_trailer;
  115. Optional<LinearizationDictionary> m_linearization_dictionary;
  116. };
  117. };