From 53aafa99985e93c527ea2803400f250025cc7f01 Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Wed, 12 Jul 2017 19:55:02 -0400 Subject: Basic APIs and test for retrieving embedded attachment count and name 1. Added APIs for retrieving embedded attachment count and file name. * Added an embedder test testing them. Bug=pdfium:174 Change-Id: I181b8e0b81495d8a7fd8c3f79dbbc0f907f5e3fd Reviewed-on: https://pdfium-review.googlesource.com/7490 Commit-Queue: Jane Liu Reviewed-by: dsinclair --- BUILD.gn | 3 ++ fpdfsdk/fpdfattachment.cpp | 47 +++++++++++++++++++++++++++++ fpdfsdk/fpdfattachment_embeddertest.cpp | 22 ++++++++++++++ fpdfsdk/fpdfview_c_api_test.c | 7 ++++- public/fpdf_attachment.h | 44 +++++++++++++++++++++++++++ testing/resources/embedded_attachments.pdf | Bin 0 -> 10447 bytes 6 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 fpdfsdk/fpdfattachment.cpp create mode 100644 fpdfsdk/fpdfattachment_embeddertest.cpp create mode 100644 public/fpdf_attachment.h create mode 100644 testing/resources/embedded_attachments.pdf diff --git a/BUILD.gn b/BUILD.gn index fff58b3afd..ff0d905fff 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -136,6 +136,7 @@ static_library("pdfium") { "fpdfsdk/fpdf_sysfontinfo.cpp", "fpdfsdk/fpdf_transformpage.cpp", "fpdfsdk/fpdfannot.cpp", + "fpdfsdk/fpdfattachment.cpp", "fpdfsdk/fpdfdoc.cpp", "fpdfsdk/fpdfeditimg.cpp", "fpdfsdk/fpdfeditpage.cpp", @@ -156,6 +157,7 @@ static_library("pdfium") { "fpdfsdk/pdfsdk_fieldaction.h", "public/cpp/fpdf_deleters.h", "public/fpdf_annot.h", + "public/fpdf_attachment.h", "public/fpdf_dataavail.h", "public/fpdf_doc.h", "public/fpdf_edit.h", @@ -2005,6 +2007,7 @@ test("pdfium_embeddertests") { "fpdfsdk/fpdf_flatten_embeddertest.cpp", "fpdfsdk/fpdf_structtree_embeddertest.cpp", "fpdfsdk/fpdfannot_embeddertest.cpp", + "fpdfsdk/fpdfattachment_embeddertest.cpp", "fpdfsdk/fpdfdoc_embeddertest.cpp", "fpdfsdk/fpdfedit_embeddertest.cpp", "fpdfsdk/fpdfext_embeddertest.cpp", 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(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 buf(len); + EXPECT_EQ(12u, FPDFDoc_GetAttachmentName(document(), 0, buf.data(), len)); + EXPECT_STREQ(L"1.txt", + GetPlatformWString(reinterpret_cast(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); diff --git a/public/fpdf_attachment.h b/public/fpdf_attachment.h new file mode 100644 index 0000000000..07fdff5a9e --- /dev/null +++ b/public/fpdf_attachment.h @@ -0,0 +1,44 @@ +// 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_ATTACHMENT_H_ +#define PUBLIC_FPDF_ATTACHMENT_H_ + +// NOLINTNEXTLINE(build/include) +#include "fpdfview.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// Experimental API. +// Get the number of embedded files in |document|. +// +// document - handle to a document. +// +// Returns the number of embedded files in |document|. +DLLEXPORT int STDCALL FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document); + +// Experimental API. +// Get the name of the embedded file at |index| in |document|. |buffer| is +// only modified if |buflen| is longer than the length of the file name. On +// errors, |buffer| is unmodified and the returned length is 0. +// +// document - handle to a document. +// index - the index of the requested embedded file. +// buffer - buffer for holding the file name, encoded in UTF16-LE. +// buflen - length of the buffer. +// +// Returns the length of the file name. +DLLEXPORT unsigned long STDCALL +FPDFDoc_GetAttachmentName(FPDF_DOCUMENT document, + int index, + void* buffer, + unsigned long buflen); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus + +#endif // PUBLIC_FPDF_ATTACHMENT_H_ diff --git a/testing/resources/embedded_attachments.pdf b/testing/resources/embedded_attachments.pdf new file mode 100644 index 0000000000..1e8fecd3dc Binary files /dev/null and b/testing/resources/embedded_attachments.pdf differ -- cgit v1.2.3