forked from pdfcpu/pdfcpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileSpec.go
501 lines (374 loc) · 12.5 KB
/
fileSpec.go
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
Copyright 2018 The pdfcpu Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validate
import (
"net/url"
pdf "github.com/hamdouni/pdfcpu/pkg/pdfcpu"
"github.com/pkg/errors"
)
// See 7.11.4
func validateFileSpecString(s string) bool {
// see 7.11.2
// The standard format for representing a simple file specification in string form divides the string into component substrings
// separated by the SOLIDUS character (2Fh) (/). The SOLIDUS is a generic component separator that shall be mapped to the appropriate
// platform-specific separator when generating a platform-dependent file name. Any of the components may be empty.
// If a component contains one or more literal SOLIDI, each shall be preceded by a REVERSE SOLIDUS (5Ch) (\), which in turn shall be
// preceded by another REVERSE SOLIDUS to indicate that it is part of the string and not an escape character.
//
// EXAMPLE ( in\\/out )
// represents the file name in/out
// I have not seen an instance of a single file spec string that actually complies with this definition and uses
// the double reverse solidi in front of the solidus, because of that we simply
return true
}
func validateURLString(s string) bool {
// RFC1738 compliant URL, see 7.11.5
_, err := url.ParseRequestURI(s)
return err == nil
}
func validateEmbeddedFileStreamMacParameterDict(xRefTable *pdf.XRefTable, d pdf.Dict) error {
dictName := "embeddedFileStreamMacParameterDict"
// Subtype, optional integer
// The embedded file's file type integer encoded according to Mac OS conventions.
_, err := validateIntegerEntry(xRefTable, d, dictName, "Subtype", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// Creator, optional integer
// The embedded file's creator signature integer encoded according to Mac OS conventions.
_, err = validateIntegerEntry(xRefTable, d, dictName, "Creator", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// ResFork, optional stream dict
// The binary contents of the embedded file's resource fork.
_, err = validateStreamDictEntry(xRefTable, d, dictName, "ResFork", OPTIONAL, pdf.V10, nil)
return err
}
func validateEmbeddedFileStreamParameterDict(xRefTable *pdf.XRefTable, o pdf.Object) error {
d, err := xRefTable.DereferenceDict(o)
if err != nil || d == nil {
return err
}
dictName := "embeddedFileStreamParmDict"
// Size, optional integer
_, err = validateIntegerEntry(xRefTable, d, dictName, "Size", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// CreationDate, optional date
_, err = validateDateEntry(xRefTable, d, dictName, "CreationDate", OPTIONAL, pdf.V10)
if err != nil {
return err
}
// ModDate, optional date
_, err = validateDateEntry(xRefTable, d, dictName, "ModDate", OPTIONAL, pdf.V10)
if err != nil {
return err
}
// Mac, optional dict
macDict, err := validateDictEntry(xRefTable, d, dictName, "Mac", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
if macDict != nil {
err = validateEmbeddedFileStreamMacParameterDict(xRefTable, macDict)
if err != nil {
return err
}
}
// CheckSum, optional string
_, err = validateStringEntry(xRefTable, d, dictName, "CheckSum", OPTIONAL, pdf.V10, nil)
return err
}
func validateEmbeddedFileStreamDict(xRefTable *pdf.XRefTable, sd *pdf.StreamDict) error {
dictName := "embeddedFileStreamDict"
// Type, optional, name
_, err := validateNameEntry(xRefTable, sd.Dict, dictName, "Type", OPTIONAL, pdf.V10, func(s string) bool { return s == "EmbeddedFile" })
if err != nil {
return err
}
// Subtype, optional, name
_, err = validateNameEntry(xRefTable, sd.Dict, dictName, "Subtype", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// Params, optional, dict
// parameter dict containing additional file-specific information.
if o, found := sd.Dict.Find("Params"); found && o != nil {
err = validateEmbeddedFileStreamParameterDict(xRefTable, o)
}
return err
}
func validateFileSpecDictEntriesEFAndRFKeys(k string) bool {
return k == "F" || k == "UF" || k == "DOS" || k == "Mac" || k == "Unix"
}
func validateFileSpecDictEntryEFDict(xRefTable *pdf.XRefTable, d pdf.Dict) error {
for k, obj := range d {
if !validateFileSpecDictEntriesEFAndRFKeys(k) {
return errors.Errorf("validateFileSpecEntriesEFAndRF: invalid key: %s", k)
}
// value must be embedded file stream dict
// see 7.11.4
sd, err := validateStreamDict(xRefTable, obj)
if err != nil {
return err
}
if sd == nil {
continue
}
err = validateEmbeddedFileStreamDict(xRefTable, sd)
if err != nil {
return err
}
}
return nil
}
func validateRFDictFilesArray(xRefTable *pdf.XRefTable, a pdf.Array) error {
if len(a)%2 > 0 {
return errors.New("pdfcpu: validateRFDictFilesArray: rfDict array corrupt")
}
for k, v := range a {
if v == nil {
return errors.New("pdfcpu: validateRFDictFilesArray: rfDict, array entry nil")
}
o, err := xRefTable.Dereference(v)
if err != nil {
return err
}
if o == nil {
return errors.New("pdfcpu: validateRFDictFilesArray: rfDict, array entry nil")
}
if k%2 > 0 {
_, ok := o.(pdf.StringLiteral)
if !ok {
return errors.New("pdfcpu: validateRFDictFilesArray: rfDict, array entry corrupt")
}
} else {
// value must be embedded file stream dict
// see 7.11.4
sd, err := validateStreamDict(xRefTable, o)
if err != nil {
return err
}
err = validateEmbeddedFileStreamDict(xRefTable, sd)
if err != nil {
return err
}
}
}
return nil
}
func validateFileSpecDictEntriesEFAndRF(xRefTable *pdf.XRefTable, efDict, rfDict pdf.Dict) error {
// EF only or EF and RF
if efDict == nil {
return errors.Errorf("pdfcpu: validateFileSpecEntriesEFAndRF: missing required efDict.")
}
err := validateFileSpecDictEntryEFDict(xRefTable, efDict)
if err != nil {
return err
}
if rfDict != nil {
for k, val := range rfDict {
if _, ok := efDict.Find(k); !ok {
return errors.Errorf("pdfcpu: validateFileSpecEntriesEFAndRF: rfDict entry=%s missing corresponding efDict entry\n", k)
}
// value must be related files array.
// see 7.11.4.2
a, err := xRefTable.DereferenceArray(val)
if err != nil {
return err
}
if a == nil {
continue
}
err = validateRFDictFilesArray(xRefTable, a)
if err != nil {
return err
}
}
}
return nil
}
func validateFileSpecDictType(xRefTable *pdf.XRefTable, d pdf.Dict) error {
if d.Type() == nil || (*d.Type() != "Filespec" && (xRefTable.ValidationMode == pdf.ValidationRelaxed && *d.Type() != "F")) {
return errors.New("pdfcpu: validateFileSpecDictType: missing type: FileSpec")
}
return nil
}
func requiredF(dosFound, macFound, unixFound bool) bool {
return !dosFound && !macFound && !unixFound
}
func validateFileSpecDictEFAndRF(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// RF, optional, dict of related files arrays, since V1.3
rfDict, err := validateDictEntry(xRefTable, d, dictName, "RF", OPTIONAL, pdf.V13, nil)
if err != nil {
return err
}
// EF, required if RF present, dict of embedded file streams, since 1.3
efDict, err := validateDictEntry(xRefTable, d, dictName, "EF", rfDict != nil, pdf.V13, nil)
if err != nil {
return err
}
// Type, required if EF present, name
validate := func(s string) bool {
return s == "Filespec" || (xRefTable.ValidationMode == pdf.ValidationRelaxed && s == "F")
}
_, err = validateNameEntry(xRefTable, d, dictName, "Type", efDict != nil, pdf.V10, validate)
if err != nil {
return err
}
// if EF present, Type "FileSpec" is required
if efDict != nil {
err = validateFileSpecDictType(xRefTable, d)
if err != nil {
return err
}
err = validateFileSpecDictEntriesEFAndRF(xRefTable, efDict, rfDict)
}
return err
}
func validateFileSpecDict(xRefTable *pdf.XRefTable, d pdf.Dict) error {
dictName := "fileSpecDict"
// FS, optional, name
fsName, err := validateNameEntry(xRefTable, d, dictName, "FS", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// DOS, byte string, optional, obsolescent.
_, dosFound := d.Find("DOS")
// Mac, byte string, optional, obsolescent.
_, macFound := d.Find("Mac")
// Unix, byte string, optional, obsolescent.
_, unixFound := d.Find("Unix")
// F, file spec string
validate := validateFileSpecString
if fsName != nil && fsName.Value() == "URL" {
validate = validateURLString
}
_, err = validateStringEntry(xRefTable, d, dictName, "F", requiredF(dosFound, macFound, unixFound), pdf.V10, validate)
if err != nil {
return err
}
// UF, optional, text string
sinceVersion := pdf.V17
if xRefTable.ValidationMode == pdf.ValidationRelaxed {
sinceVersion = pdf.V14
}
_, err = validateStringEntry(xRefTable, d, dictName, "UF", OPTIONAL, sinceVersion, validateFileSpecString)
if err != nil {
return err
}
// ID, optional, array of strings
_, err = validateStringArrayEntry(xRefTable, d, dictName, "ID", OPTIONAL, pdf.V11, func(a pdf.Array) bool { return len(a) == 2 })
if err != nil {
return err
}
// V, optional, boolean, since V1.2
_, err = validateBooleanEntry(xRefTable, d, dictName, "V", OPTIONAL, pdf.V12, nil)
if err != nil {
return err
}
err = validateFileSpecDictEFAndRF(xRefTable, d, dictName)
if err != nil {
return err
}
// Desc, optional, text string, since V1.6
sinceVersion = pdf.V16
if xRefTable.ValidationMode == pdf.ValidationRelaxed {
sinceVersion = pdf.V10
}
_, err = validateStringEntry(xRefTable, d, dictName, "Desc", OPTIONAL, sinceVersion, nil)
if err != nil {
return err
}
// CI, optional, collection item dict, since V1.7
_, err = validateDictEntry(xRefTable, d, dictName, "CI", OPTIONAL, pdf.V17, nil)
return err
}
func validateFileSpecification(xRefTable *pdf.XRefTable, o pdf.Object) (pdf.Object, error) {
// See 7.11.4
o, err := xRefTable.Dereference(o)
if err != nil {
return nil, err
}
switch o := o.(type) {
case pdf.StringLiteral:
s := o.Value()
if !validateFileSpecString(s) {
return nil, errors.Errorf("pdfcpu: validateFileSpecification: invalid file spec string: %s", s)
}
case pdf.HexLiteral:
s := o.Value()
if !validateFileSpecString(s) {
return nil, errors.Errorf("pdfcpu: validateFileSpecification: invalid file spec string: %s", s)
}
case pdf.Dict:
err = validateFileSpecDict(xRefTable, o)
if err != nil {
return nil, err
}
default:
return nil, errors.Errorf("pdfcpu: validateFileSpecification: invalid type")
}
return o, nil
}
func validateURLSpecification(xRefTable *pdf.XRefTable, o pdf.Object) (pdf.Object, error) {
// See 7.11.4
d, err := xRefTable.DereferenceDict(o)
if err != nil {
return nil, err
}
if d == nil {
return nil, errors.New("pdfcpu: validateURLSpecification: missing dict")
}
dictName := "urlSpec"
// FS, required, name
_, err = validateNameEntry(xRefTable, d, dictName, "FS", REQUIRED, pdf.V10, func(s string) bool { return s == "URL" })
if err != nil {
return nil, err
}
// F, required, string, URL (Internet RFC 1738)
_, err = validateStringEntry(xRefTable, d, dictName, "F", REQUIRED, pdf.V10, validateURLString)
return o, err
}
func validateFileSpecEntry(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string, entryName string, required bool, sinceVersion pdf.Version) (pdf.Object, error) {
o, err := validateEntry(xRefTable, d, dictName, entryName, required, sinceVersion)
if err != nil || o == nil {
return nil, err
}
err = xRefTable.ValidateVersion("fileSpec", sinceVersion)
if err != nil {
return nil, err
}
return validateFileSpecification(xRefTable, o)
}
func validateURLSpecEntry(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string, entryName string, required bool, sinceVersion pdf.Version) (pdf.Object, error) {
o, err := validateEntry(xRefTable, d, dictName, entryName, required, sinceVersion)
if err != nil || o == nil {
return nil, err
}
err = xRefTable.ValidateVersion("URLSpec", sinceVersion)
if err != nil {
return nil, err
}
return validateURLSpecification(xRefTable, o)
}
func validateFileSpecificationOrFormObject(xRefTable *pdf.XRefTable, obj pdf.Object) error {
sd, ok := obj.(pdf.StreamDict)
if ok {
return validateFormStreamDict(xRefTable, &sd)
}
_, err := validateFileSpecification(xRefTable, obj)
return err
}