Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 1 | // 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 | |
| 12 | DLLEXPORT 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 | |
| 20 | DLLEXPORT unsigned long STDCALL |
| 21 | FPDFDoc_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 Liu | c8a17e5 | 2017-07-13 10:37:59 -0400 | [diff] [blame] | 38 | CFX_ByteString name = CPDF_FileSpec(pFile).GetFileName().UTF16LE_Encode(); |
| 39 | unsigned long len = name.GetLength(); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 40 | if (buffer && buflen >= len) |
Jane Liu | c8a17e5 | 2017-07-13 10:37:59 -0400 | [diff] [blame] | 41 | memcpy(buffer, name.c_str(), len); |
Jane Liu | 53aafa9 | 2017-07-12 19:55:02 -0400 | [diff] [blame] | 42 | |
| 43 | return len; |
| 44 | } |