blob: 88abea30d5176c6436868c087ab72293b3d81020 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// Copyright 2016 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.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "core/fpdfdoc/cpdf_filespec.h"
#include <vector>
#include "constants/stream_dict_common.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_name.h"
#include "core/fpdfapi/parser/cpdf_object.h"
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fpdfapi/parser/cpdf_string.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
#include "core/fxcrt/fx_system.h"
namespace {
// List of keys to check for the file specification string.
// Follows the same precedence order as GetFileName().
constexpr const char* kKeys[] = {"UF", "F", "DOS", "Mac", "Unix"};
#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_ || \
_FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
WideString ChangeSlashToPlatform(const wchar_t* str) {
WideString result;
while (*str) {
if (*str == '/') {
#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
result += L':';
#else
result += L'\\';
#endif
} else {
result += *str;
}
str++;
}
return result;
}
WideString ChangeSlashToPDF(const wchar_t* str) {
WideString result;
while (*str) {
if (*str == '\\' || *str == ':')
result += L'/';
else
result += *str;
str++;
}
return result;
}
#endif // _FX_PLATFORM_APPLE_ || _FX_PLATFORM_WINDOWS_
} // namespace
CPDF_FileSpec::CPDF_FileSpec(const CPDF_Object* pObj) : m_pObj(pObj) {
ASSERT(m_pObj);
}
CPDF_FileSpec::CPDF_FileSpec(CPDF_Object* pObj)
: m_pObj(pObj), m_pWritableObj(pObj) {
ASSERT(m_pObj);
}
CPDF_FileSpec::~CPDF_FileSpec() {}
WideString CPDF_FileSpec::DecodeFileName(const WideString& filepath) {
if (filepath.GetLength() <= 1)
return WideString();
#if _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
if (filepath.Left(sizeof("/Mac") - 1) == WideStringView(L"/Mac"))
return ChangeSlashToPlatform(filepath.c_str() + 1);
return ChangeSlashToPlatform(filepath.c_str());
#elif _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
if (filepath[0] != L'/')
return ChangeSlashToPlatform(filepath.c_str());
if (filepath[1] == L'/')
return ChangeSlashToPlatform(filepath.c_str() + 1);
if (filepath[2] == L'/') {
WideString result;
result += filepath[1];
result += L':';
result += ChangeSlashToPlatform(filepath.c_str() + 2);
return result;
}
WideString result;
result += L'\\';
result += ChangeSlashToPlatform(filepath.c_str());
return result;
#else
return WideString(filepath);
#endif
}
WideString CPDF_FileSpec::GetFileName() const {
WideString csFileName;
if (const CPDF_Dictionary* pDict = m_pObj->AsDictionary()) {
csFileName = pDict->GetUnicodeTextFor("UF");
if (csFileName.IsEmpty()) {
csFileName = WideString::FromLocal(
pDict->GetStringFor(pdfium::stream::kF).AsStringView());
}
if (pDict->GetStringFor("FS") == "URL")
return csFileName;
if (csFileName.IsEmpty()) {
constexpr const char* keys[] = {"DOS", "Mac", "Unix"};
for (const auto* key : keys) {
if (pDict->KeyExist(key)) {
csFileName =
WideString::FromLocal(pDict->GetStringFor(key).AsStringView());
break;
}
}
}
} else if (m_pObj->IsString()) {
csFileName = WideString::FromLocal(m_pObj->GetString().AsStringView());
}
return DecodeFileName(csFileName);
}
const CPDF_Stream* CPDF_FileSpec::GetFileStream() const {
const CPDF_Dictionary* pDict = m_pObj->AsDictionary();
if (!pDict)
return nullptr;
// Get the embedded files dictionary.
const CPDF_Dictionary* pFiles = pDict->GetDictFor("EF");
if (!pFiles)
return nullptr;
size_t end = pDict->GetStringFor("FS") == "URL" ? 2 : FX_ArraySize(kKeys);
for (size_t i = 0; i < end; ++i) {
const ByteString& key = kKeys[i];
if (!pDict->GetUnicodeTextFor(key).IsEmpty()) {
const CPDF_Stream* pStream = pFiles->GetStreamFor(key);
if (pStream)
return pStream;
}
}
return nullptr;
}
CPDF_Stream* CPDF_FileSpec::GetFileStream() {
CPDF_Dictionary* pDict = m_pWritableObj->AsDictionary();
if (!pDict)
return nullptr;
// Get the embedded files dictionary.
CPDF_Dictionary* pFiles = pDict->GetDictFor("EF");
if (!pFiles)
return nullptr;
size_t end = pDict->GetStringFor("FS") == "URL" ? 2 : FX_ArraySize(kKeys);
for (size_t i = 0; i < end; ++i) {
const ByteString& key = kKeys[i];
if (!pDict->GetUnicodeTextFor(key).IsEmpty()) {
CPDF_Stream* pStream = pFiles->GetStreamFor(key);
if (pStream)
return pStream;
}
}
return nullptr;
}
const CPDF_Dictionary* CPDF_FileSpec::GetParamsDict() const {
const CPDF_Stream* pStream = GetFileStream();
if (!pStream)
return nullptr;
const CPDF_Dictionary* pDict = pStream->GetDict();
return pDict ? pDict->GetDictFor("Params") : nullptr;
}
CPDF_Dictionary* CPDF_FileSpec::GetParamsDict() {
CPDF_Stream* pStream = GetFileStream();
if (!pStream)
return nullptr;
CPDF_Dictionary* pDict = pStream->GetDict();
return pDict ? pDict->GetDictFor("Params") : nullptr;
}
WideString CPDF_FileSpec::EncodeFileName(const WideString& filepath) {
if (filepath.GetLength() <= 1)
return WideString();
#if _FX_PLATFORM_ == _FX_PLATFORM_WINDOWS_
if (filepath[1] == L':') {
WideString result(L'/');
result += filepath[0];
if (filepath[2] != L'\\')
result += L'/';
result += ChangeSlashToPDF(filepath.c_str() + 2);
return result;
}
if (filepath[0] == L'\\' && filepath[1] == L'\\')
return ChangeSlashToPDF(filepath.c_str() + 1);
if (filepath[0] == L'\\')
return L'/' + ChangeSlashToPDF(filepath.c_str());
return ChangeSlashToPDF(filepath.c_str());
#elif _FX_PLATFORM_ == _FX_PLATFORM_APPLE_
if (filepath.Left(sizeof("Mac") - 1) == L"Mac")
return L'/' + ChangeSlashToPDF(filepath.c_str());
return ChangeSlashToPDF(filepath.c_str());
#else
return WideString(filepath);
#endif
}
void CPDF_FileSpec::SetFileName(const WideString& wsFileName) {
if (!m_pWritableObj) {
NOTREACHED();
return;
}
WideString wsStr = EncodeFileName(wsFileName);
if (m_pObj->IsString()) {
m_pWritableObj->SetString(ByteString::FromUnicode(wsStr));
} else if (CPDF_Dictionary* pDict = m_pWritableObj->AsDictionary()) {
pDict->SetNewFor<CPDF_String>(pdfium::stream::kF,
ByteString::FromUnicode(wsStr), false);
pDict->SetNewFor<CPDF_String>("UF", PDF_EncodeText(wsStr), false);
}
}
|