xref: /aosp_15_r20/external/pdfium/core/fpdfapi/parser/object_tree_traversal_util.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2023 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CORE_FPDFAPI_PARSER_OBJECT_TREE_TRAVERSAL_UTIL_H_
6 #define CORE_FPDFAPI_PARSER_OBJECT_TREE_TRAVERSAL_UTIL_H_
7 
8 #include <stdint.h>
9 
10 #include <set>
11 
12 class CPDF_Document;
13 
14 // Traverses `document` starting with its trailer, if it has one, or starting at
15 // the catalog, which always exists. The trailer should have a reference to the
16 // catalog. The traversal avoids cycles.
17 // Returns all the PDF objects (not CPDF_Objects) the traversal reached as a set
18 // of object numbers.
19 std::set<uint32_t> GetObjectsWithReferences(const CPDF_Document* document);
20 
21 // Same as GetObjectsWithReferences(), but only returns the objects with
22 // multiple references. References that would create a cycle are ignored.
23 //
24 // In this example, where (A) is the root node:
25 //
26 //     A -> B
27 //     A -> C
28 //     B -> D
29 //     C -> D
30 //
31 // GetObjectsWithMultipleReferences() returns {D}, since both (B) and (C)
32 // references to (D), and there are no cycles.
33 //
34 // In this example, where (A) is the root node:
35 //
36 //     A -> B
37 //     B -> C
38 //     C -> B
39 //
40 // GetObjectsWithMultipleReferences() returns {}, even though both (A) and (C)
41 // references (B). Since (B) -> (C) -> (B) creates a cycle, the (C) -> (B)
42 // reference does not count.
43 std::set<uint32_t> GetObjectsWithMultipleReferences(
44     const CPDF_Document* document);
45 
46 #endif  // CORE_FPDFAPI_PARSER_OBJECT_TREE_TRAVERSAL_UTIL_H_
47