Add FPDFCatalog_IsTagged to public API.

The new fpdf_catalog.h will contains functions to access entries in
the catalog (root) dict in a PDF.

Bug: chromium:768986
Change-Id: I6e1d4a479d6f8742981e89f07bab98ee96dc3763
Reviewed-on: https://pdfium-review.googlesource.com/15970
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 7355dca..3de2d32 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -138,6 +138,7 @@
     "fpdfsdk/fpdf_transformpage.cpp",
     "fpdfsdk/fpdfannot.cpp",
     "fpdfsdk/fpdfattachment.cpp",
+    "fpdfsdk/fpdfcatalog.cpp",
     "fpdfsdk/fpdfdoc.cpp",
     "fpdfsdk/fpdfeditimg.cpp",
     "fpdfsdk/fpdfeditpage.cpp",
@@ -159,6 +160,7 @@
     "public/cpp/fpdf_deleters.h",
     "public/fpdf_annot.h",
     "public/fpdf_attachment.h",
+    "public/fpdf_catalog.h",
     "public/fpdf_dataavail.h",
     "public/fpdf_doc.h",
     "public/fpdf_edit.h",
@@ -1927,6 +1929,7 @@
     "core/fxcrt/weak_ptr_unittest.cpp",
     "core/fxcrt/widestring_unittest.cpp",
     "core/fxge/dib/cstretchengine_unittest.cpp",
+    "fpdfsdk/fpdfcatalog_unittest.cpp",
     "fpdfsdk/fpdfdoc_unittest.cpp",
     "fpdfsdk/fpdfeditimg_unittest.cpp",
     "testing/unit_test_main.cpp",
diff --git a/fpdfsdk/fpdfcatalog.cpp b/fpdfsdk/fpdfcatalog.cpp
new file mode 100644
index 0000000..493e5ae
--- /dev/null
+++ b/fpdfsdk/fpdfcatalog.cpp
@@ -0,0 +1,22 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "public/fpdf_catalog.h"
+
+#include "core/fpdfapi/parser/cpdf_document.h"
+#include "fpdfsdk/fsdk_define.h"
+
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
+FPDFCatalog_IsTagged(FPDF_DOCUMENT document) {
+  CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
+  if (!pDoc)
+    return false;
+
+  const CPDF_Dictionary* pCatalog = pDoc->GetRoot();
+  if (!pCatalog)
+    return false;
+
+  const CPDF_Dictionary* pMarkInfo = pCatalog->GetDictFor("MarkInfo");
+  return pMarkInfo && pMarkInfo->GetIntegerFor("Marked") != 0;
+}
diff --git a/fpdfsdk/fpdfcatalog_unittest.cpp b/fpdfsdk/fpdfcatalog_unittest.cpp
new file mode 100644
index 0000000..13c0634
--- /dev/null
+++ b/fpdfsdk/fpdfcatalog_unittest.cpp
@@ -0,0 +1,95 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "public/fpdf_catalog.h"
+
+#include "core/fpdfapi/cpdf_modulemgr.h"
+#include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_number.h"
+#include "core/fpdfapi/parser/cpdf_parser.h"
+#include "core/fpdfapi/parser/cpdf_string.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
+
+#ifdef PDF_ENABLE_XFA
+#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
+#endif  // PDF_ENABLE_XFA
+
+class CPDF_TestDocument : public CPDF_Document {
+ public:
+  CPDF_TestDocument() : CPDF_Document(nullptr) {}
+
+  void SetRoot(CPDF_Dictionary* root) {
+    m_pRootDict = root;
+    GetRoot();
+  }
+};
+
+#ifdef PDF_ENABLE_XFA
+class CPDF_TestXFAContext : public CPDFXFA_Context {
+ public:
+  CPDF_TestXFAContext()
+      : CPDFXFA_Context(pdfium::MakeUnique<CPDF_TestDocument>()) {}
+
+  void SetRoot(CPDF_Dictionary* root) {
+    reinterpret_cast<CPDF_TestDocument*>(GetPDFDoc())->SetRoot(root);
+  }
+
+  CPDF_IndirectObjectHolder* GetHolder() { return GetPDFDoc(); }
+};
+using CPDF_TestPdfDocument = CPDF_TestXFAContext;
+#else   // PDF_ENABLE_XFA
+using CPDF_TestPdfDocument = CPDF_TestDocument;
+#endif  // PDF_ENABLE_XFA
+
+class PDFCatalogTest : public testing::Test {
+ public:
+  void SetUp() override {
+    CPDF_ModuleMgr::Get()->Init();
+
+    m_pDoc = pdfium::MakeUnique<CPDF_TestPdfDocument>();
+
+    // Setup the root directory.
+    m_pRootObj = pdfium::MakeUnique<CPDF_Dictionary>();
+  }
+
+  void TearDown() override {
+    m_pDoc.reset();
+    CPDF_ModuleMgr::Destroy();
+  }
+
+ protected:
+  std::unique_ptr<CPDF_TestPdfDocument> m_pDoc;
+  std::unique_ptr<CPDF_Dictionary> m_pRootObj;
+};
+
+TEST_F(PDFCatalogTest, IsTagged) {
+  // Null doc
+  EXPECT_FALSE(FPDFCatalog_IsTagged(nullptr));
+
+  // No root
+  m_pDoc->SetRoot(nullptr);
+  EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+  // Empty root
+  m_pDoc->SetRoot(m_pRootObj.get());
+  EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+  // Root with other key
+  m_pRootObj->SetNewFor<CPDF_String>("OTHER_KEY", "other value", false);
+  EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+  // Root with empty MarkInfo
+  CPDF_Dictionary* markInfoDict =
+      m_pRootObj->SetNewFor<CPDF_Dictionary>("MarkInfo");
+  EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+  // MarkInfo present but Marked is 0
+  markInfoDict->SetNewFor<CPDF_Number>("Marked", 0);
+  EXPECT_FALSE(FPDFCatalog_IsTagged(m_pDoc.get()));
+
+  // MarkInfo present and Marked is 1, PDF is considered tagged.
+  markInfoDict->SetNewFor<CPDF_Number>("Marked", 1);
+  EXPECT_TRUE(FPDFCatalog_IsTagged(m_pDoc.get()));
+}
diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c
index 211d7e0..bf1a0c4 100644
--- a/fpdfsdk/fpdfview_c_api_test.c
+++ b/fpdfsdk/fpdfview_c_api_test.c
@@ -11,6 +11,7 @@
 
 #include "public/fpdf_annot.h"
 #include "public/fpdf_attachment.h"
+#include "public/fpdf_catalog.h"
 #include "public/fpdf_dataavail.h"
 #include "public/fpdf_doc.h"
 #include "public/fpdf_edit.h"
@@ -78,6 +79,9 @@
     CHK(FPDFAttachment_SetFile);
     CHK(FPDFAttachment_GetFile);
 
+    // fpdf_catalog.h
+    CHK(FPDFCatalog_IsTagged);
+
     // fpdf_dataavail.h
     CHK(FPDFAvail_Create);
     CHK(FPDFAvail_Destroy);
diff --git a/public/fpdf_catalog.h b/public/fpdf_catalog.h
new file mode 100644
index 0000000..48d2775
--- /dev/null
+++ b/public/fpdf_catalog.h
@@ -0,0 +1,34 @@
+// Copyright 2017 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef PUBLIC_FPDF_CATALOG_H_
+#define PUBLIC_FPDF_CATALOG_H_
+
+// NOLINTNEXTLINE(build/include)
+#include "fpdfview.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif  // __cplusplus
+
+/**
+ * Experimental API.
+ *
+ * Determine if |document| represents a tagged PDF.
+ *
+ * For the definition of tagged PDF, See (see 10.7 "Tagged PDF" in PDF
+ * Reference 1.7).
+ *
+ *   document - handle to a document.
+ *
+ * Returns |true| iff |document| is a tagged PDF.
+ */
+FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
+FPDFCatalog_IsTagged(FPDF_DOCUMENT document);
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif  // __cplusplus
+
+#endif  // PUBLIC_FPDF_CATALOG_H_