blob: e07d15b0c8012864d757f72a339a3b577b19e76b [file] [log] [blame]
Jane Liu53aafa92017-07-12 19:55:02 -04001// Copyright 2017 PDFium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "public/fpdf_attachment.h"
6
7#include "core/fpdfapi/parser/cpdf_document.h"
8#include "core/fpdfdoc/cpdf_filespec.h"
9#include "core/fpdfdoc/cpdf_nametree.h"
10#include "fpdfsdk/fsdk_define.h"
11
12DLLEXPORT int STDCALL FPDFDoc_GetAttachmentCount(FPDF_DOCUMENT document) {
13 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
14 if (!pDoc)
15 return 0;
16
17 return CPDF_NameTree(pDoc, "EmbeddedFiles").GetCount();
18}
19
20DLLEXPORT unsigned long STDCALL
21FPDFDoc_GetAttachmentName(FPDF_DOCUMENT document,
22 int index,
23 void* buffer,
24 unsigned long buflen) {
25 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
26 if (!pDoc || index < 0)
27 return 0;
28
29 CPDF_NameTree nameTree(pDoc, "EmbeddedFiles");
30 if (static_cast<size_t>(index) >= nameTree.GetCount())
31 return 0;
32
33 CFX_ByteString csName;
34 CPDF_Object* pFile = nameTree.LookupValueAndName(index, &csName);
35 if (!pFile)
36 return 0;
37
Jane Liuc8a17e52017-07-13 10:37:59 -040038 CFX_ByteString name = CPDF_FileSpec(pFile).GetFileName().UTF16LE_Encode();
39 unsigned long len = name.GetLength();
Jane Liu53aafa92017-07-12 19:55:02 -040040 if (buffer && buflen >= len)
Jane Liuc8a17e52017-07-13 10:37:59 -040041 memcpy(buffer, name.c_str(), len);
Jane Liu53aafa92017-07-12 19:55:02 -040042
43 return len;
44}