summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/fpdfattachment.cpp47
-rw-r--r--fpdfsdk/fpdfattachment_embeddertest.cpp22
-rw-r--r--fpdfsdk/fpdfview_c_api_test.c7
3 files changed, 75 insertions, 1 deletions
diff --git a/fpdfsdk/fpdfattachment.cpp b/fpdfsdk/fpdfattachment.cpp
new file mode 100644
index 0000000000..d8713bb2e1
--- /dev/null
+++ b/fpdfsdk/fpdfattachment.cpp
@@ -0,0 +1,47 @@
+// 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_attachment.h"
+
+#include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfdoc/cpdf_filespec.h"
+#include "core/fpdfdoc/cpdf_nametree.h"
+#include "fpdfsdk/fsdk_define.h"
+
+DLLEXPORT int STDCALL FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document) {
+ CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
+ if (!pDoc)
+ return 0;
+
+ return CPDF_NameTree(pDoc, "EmbeddedFiles").GetCount();
+}
+
+DLLEXPORT unsigned long STDCALL
+FPDFDoc_GetAttachmentName(FPDF_DOCUMENT document,
+ int index,
+ void* buffer,
+ unsigned long buflen) {
+ CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
+ if (!pDoc || index < 0)
+ return 0;
+
+ CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
+ if (static_cast<size_t>(index) >= nameTree.GetCount())
+ return 0;
+
+ CFX_ByteString csName;
+ CPDF_Object* pFile = nameTree.LookupValueAndName(index, &csName);
+ if (!pFile)
+ return 0;
+
+ CFX_WideString name;
+ CPDF_FileSpec filespec(pFile);
+ filespec.GetFileName(&name);
+ CFX_ByteString encodedName = name.UTF16LE_Encode();
+ unsigned long len = encodedName.GetLength();
+ if (buffer && buflen >= len)
+ memcpy(buffer, encodedName.c_str(), len);
+
+ return len;
+}
diff --git a/fpdfsdk/fpdfattachment_embeddertest.cpp b/fpdfsdk/fpdfattachment_embeddertest.cpp
new file mode 100644
index 0000000000..2cbda8a429
--- /dev/null
+++ b/fpdfsdk/fpdfattachment_embeddertest.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_attachment.h"
+#include "testing/embedder_test.h"
+
+class FPDFAttachmentEmbeddertest : public EmbedderTest {};
+
+TEST_F(FPDFAttachmentEmbeddertest, ExtractAttachments) {
+ // Open a file with two attachments.
+ ASSERT_TRUE(OpenDocument("embedded_attachments.pdf"));
+ EXPECT_EQ(2, FPDFDoc_GetAttachmentCount(document()));
+
+ // Check that the name of the first attachment is correct.
+ unsigned long len = FPDFDoc_GetAttachmentName(document(), 0, nullptr, 0);
+ std::vector<char> buf(len);
+ EXPECT_EQ(12u, FPDFDoc_GetAttachmentName(document(), 0, buf.data(), len));
+ EXPECT_STREQ(L"1.txt",
+ GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
+ .c_str());
+}
diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c
index 1c066dd295..2fcaf7455f 100644
--- a/fpdfsdk/fpdfview_c_api_test.c
+++ b/fpdfsdk/fpdfview_c_api_test.c
@@ -10,6 +10,7 @@
#include "fpdfsdk/fpdfview_c_api_test.h"
#include "public/fpdf_annot.h"
+#include "public/fpdf_attachment.h"
#include "public/fpdf_dataavail.h"
#include "public/fpdf_doc.h"
#include "public/fpdf_edit.h"
@@ -34,7 +35,7 @@ fnptr g_c_api_test_fnptr = NULL; // Extern, so can't know it doesn't change.
// Function to call from gtest harness to ensure linker resolution.
int CheckPDFiumCApi() {
- //fpdf_annot.h
+ // fpdf_annot.h
CHK(FPDFAnnot_IsSupportedSubtype);
CHK(FPDFPage_CreateAnnot);
CHK(FPDFPage_GetAnnotCount);
@@ -61,6 +62,10 @@ int CheckPDFiumCApi() {
CHK(FPDFAnnot_GetFlags);
CHK(FPDFAnnot_SetFlags);
+ // fpdf_attachment.h
+ CHK(FPDFDoc_GetAttachmentCount);
+ CHK(FPDFDoc_GetAttachmentName);
+
// fpdf_dataavail.h
CHK(FPDFAvail_Create);
CHK(FPDFAvail_Destroy);