-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathFileSpec.cc
305 lines (263 loc) · 8.75 KB
/
FileSpec.cc
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//========================================================================
//
// FileSpec.cc
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2008-2009 Carlos Garcia Campos <[email protected]>
// Copyright (C) 2009 Kovid Goyal <[email protected]>
// Copyright (C) 2012, 2017-2021, 2024 Albert Astals Cid <[email protected]>
// Copyright (C) 2012 Hib Eris <[email protected]>
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <[email protected]>. Work sponsored by the LiMux project of the city of Munich
// Copyright (C) 2018 Adam Reichold <[email protected]>
// Copyright (C) 2019 Christian Persch <[email protected]>
// Copyright (C) 2024, 2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
//========================================================================
//
// Most of the code from Link.cc and PSOutputDev.cc
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
#include <config.h>
#include "FileSpec.h"
#include "XRef.h"
#include "goo/gfile.h"
EmbFile::EmbFile(Object &&efStream)
{
m_size = -1;
m_createDate = nullptr;
m_modDate = nullptr;
m_checksum = nullptr;
m_mimetype = nullptr;
m_objStr = std::move(efStream);
if (m_objStr.isStream()) {
// dataDict corresponds to Table 3.41 in the PDF1.6 spec.
Dict *dataDict = m_objStr.streamGetDict();
// subtype is normally the mimetype
Object subtypeName = dataDict->lookup("Subtype");
if (subtypeName.isName()) {
m_mimetype = std::make_unique<GooString>(subtypeName.getName());
}
// paramDict corresponds to Table 3.42 in the PDF1.6 spec
Object paramDict = dataDict->lookup("Params");
if (paramDict.isDict()) {
Object paramObj = paramDict.dictLookup("ModDate");
if (paramObj.isString()) {
m_modDate = paramObj.getString()->copy();
}
paramObj = paramDict.dictLookup("CreationDate");
if (paramObj.isString()) {
m_createDate = paramObj.getString()->copy();
}
paramObj = paramDict.dictLookup("Size");
if (paramObj.isInt()) {
m_size = paramObj.getInt();
}
paramObj = paramDict.dictLookup("CheckSum");
if (paramObj.isString()) {
m_checksum = paramObj.getString()->copy();
}
}
}
}
EmbFile::~EmbFile() = default;
bool EmbFile::save(const std::string &path)
{
FILE *f;
bool ret;
if (!(f = openFile(path.c_str(), "wb"))) {
return false;
}
ret = save2(f);
fclose(f);
return ret;
}
bool EmbFile::save2(FILE *f)
{
int c;
if (unlikely(!m_objStr.isStream())) {
return false;
}
m_objStr.streamReset();
while ((c = m_objStr.streamGetChar()) != EOF) {
fputc(c, f);
}
return true;
}
FileSpec::FileSpec(const Object *fileSpecA)
{
ok = true;
embFile = nullptr;
fileSpec = fileSpecA->copy();
Object obj1 = getFileSpecName(fileSpecA);
if (!obj1.isString()) {
ok = false;
error(errSyntaxError, -1, "Invalid FileSpec");
return;
}
fileName = obj1.getString()->copy();
if (fileSpec.isDict()) {
obj1 = fileSpec.dictLookup("EF");
if (obj1.isDict()) {
fileStream = obj1.dictLookupNF("F").copy();
if (!fileStream.isRef()) {
ok = false;
fileStream.setToNull();
error(errSyntaxError, -1, "Invalid FileSpec: Embedded file stream is not an indirect reference");
return;
}
}
obj1 = fileSpec.dictLookup("Desc");
if (obj1.isString()) {
desc = obj1.getString()->copy();
}
}
}
FileSpec::~FileSpec() = default;
EmbFile *FileSpec::getEmbeddedFile()
{
if (!ok || !fileSpec.isDict()) {
return nullptr;
}
if (embFile) {
return embFile.get();
}
XRef *xref = fileSpec.getDict()->getXRef();
embFile = std::make_unique<EmbFile>(fileStream.fetch(xref));
return embFile.get();
}
Object FileSpec::newFileSpecObject(XRef *xref, GooFile *file, const std::string &fileName)
{
Object paramsDict = Object(new Dict(xref));
paramsDict.dictSet("Size", Object(file->size()));
// No Subtype in the embedded file stream dictionary for now
Object streamDict = Object(new Dict(xref));
streamDict.dictSet("Length", Object(file->size()));
streamDict.dictSet("Params", std::move(paramsDict));
FileStream *fStream = new FileStream(file, 0, false, file->size(), std::move(streamDict));
fStream->setNeedsEncryptionOnSave(true);
Stream *stream = fStream;
const Ref streamRef = xref->addIndirectObject(Object(stream));
Dict *efDict = new Dict(xref);
efDict->set("F", Object(streamRef));
Dict *fsDict = new Dict(xref);
fsDict->set("Type", Object(objName, "Filespec"));
fsDict->set("UF", Object(std::make_unique<GooString>(fileName)));
fsDict->set("EF", Object(efDict));
return Object(fsDict);
}
GooString *FileSpec::getFileNameForPlatform()
{
if (platformFileName) {
return platformFileName.get();
}
Object obj1 = getFileSpecNameForPlatform(&fileSpec);
if (obj1.isString()) {
platformFileName = obj1.getString()->copy();
}
return platformFileName.get();
}
Object getFileSpecName(const Object *fileSpec)
{
if (fileSpec->isString()) {
return fileSpec->copy();
}
if (fileSpec->isDict()) {
Object fileName = fileSpec->dictLookup("UF");
if (fileName.isString()) {
return fileName;
}
fileName = fileSpec->dictLookup("F");
if (fileName.isString()) {
return fileName;
}
fileName = fileSpec->dictLookup("DOS");
if (fileName.isString()) {
return fileName;
}
fileName = fileSpec->dictLookup("Mac");
if (fileName.isString()) {
return fileName;
}
fileName = fileSpec->dictLookup("Unix");
if (fileName.isString()) {
return fileName;
}
}
return Object();
}
Object getFileSpecNameForPlatform(const Object *fileSpec)
{
if (fileSpec->isString()) {
return fileSpec->copy();
}
Object fileName;
if (fileSpec->isDict()) {
fileName = fileSpec->dictLookup("UF");
if (!fileName.isString()) {
fileName = fileSpec->dictLookup("F");
if (!fileName.isString()) {
#ifdef _WIN32
const char *platform = "DOS";
#else
const char *platform = "Unix";
#endif
fileName = fileSpec->dictLookup(platform);
if (!fileName.isString()) {
error(errSyntaxError, -1, "Illegal file spec");
return Object();
}
}
}
} else {
error(errSyntaxError, -1, "Illegal file spec");
return Object();
}
// system-dependent path manipulation
#ifdef _WIN32
int i, j;
std::unique_ptr<GooString> name = fileName.getString()->copy();
// "//...." --> "\...."
// "/x/...." --> "x:\...."
// "/server/share/...." --> "\\server\share\...."
// convert escaped slashes to slashes and unescaped slashes to backslashes
i = 0;
if (name->getChar(0) == '/') {
if (name->getLength() >= 2 && name->getChar(1) == '/') {
name->del(0);
i = 0;
} else if (name->getLength() >= 2 && ((name->getChar(1) >= 'a' && name->getChar(1) <= 'z') || (name->getChar(1) >= 'A' && name->getChar(1) <= 'Z')) && (name->getLength() == 2 || name->getChar(2) == '/')) {
name->setChar(0, name->getChar(1));
name->setChar(1, ':');
i = 2;
} else {
for (j = 2; j < name->getLength(); ++j) {
if (name->getChar(j - 1) != '\\' && name->getChar(j) == '/') {
break;
}
}
if (j < name->getLength()) {
name->setChar(0, '\\');
name->insert(0, '\\');
i = 2;
}
}
}
for (; i < name->getLength(); ++i) {
if (name->getChar(i) == '/') {
name->setChar(i, '\\');
} else if (name->getChar(i) == '\\' && i + 1 < name->getLength() && name->getChar(i + 1) == '/') {
name->del(i);
}
}
fileName = Object(std::move(name));
#endif /* _WIN32 */
return fileName;
}