This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
forked from pdfcpu/pdfcpu
/
validateInfo.go
141 lines (104 loc) · 2.96 KB
/
validateInfo.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
package pdfcpu
import (
"github.com/hhrutter/pdfcpu/pkg/log"
"github.com/pkg/errors"
)
func memberOf(s string, list []string) bool {
for _, v := range list {
if s == v {
return true
}
}
return false
}
func intMemberOf(i int, list []int) bool {
for _, v := range list {
if i == v {
return true
}
}
return false
}
func validateCreationDate(xRefTable *XRefTable, o PDFObject) (err error) {
if xRefTable.ValidationMode == ValidationRelaxed {
_, err = validateString(xRefTable, o, nil)
} else {
_, err = validateDateObject(xRefTable, o, V10)
}
return err
}
func handleDefault(xRefTable *XRefTable, o PDFObject) (err error) {
if xRefTable.ValidationMode == ValidationStrict {
_, err = xRefTable.DereferenceStringOrHexLiteral(o, V10, nil)
} else {
_, err = xRefTable.Dereference(o)
}
return err
}
func validateDocumentInfoDict(xRefTable *XRefTable, obj PDFObject) (hasModDate bool, err error) {
// Document info object is optional.
dict, err := xRefTable.DereferenceDict(obj)
if err != nil || dict == nil {
return false, err
}
for k, v := range dict.Dict {
switch k {
// text string, opt, since V1.1
case "Title":
_, err = xRefTable.DereferenceStringOrHexLiteral(v, V11, nil)
// text string, optional
case "Author":
_, err = xRefTable.DereferenceStringOrHexLiteral(v, V10, nil)
// text string, optional, since V1.1
case "Subject":
_, err = xRefTable.DereferenceStringOrHexLiteral(v, V11, nil)
// text string, optional, since V1.1
case "Keywords":
_, err = xRefTable.DereferenceStringOrHexLiteral(v, V11, nil)
// text string, optional
case "Creator":
_, err = xRefTable.DereferenceStringOrHexLiteral(v, V10, nil)
// text string, optional
case "Producer":
_, err = xRefTable.DereferenceStringOrHexLiteral(v, V10, nil)
// date, optional
case "CreationDate":
err = validateCreationDate(xRefTable, v)
// date, required if PieceInfo is present in document catalog.
case "ModDate":
hasModDate = true
_, err = validateDateObject(xRefTable, v, V10)
// name, optional, since V1.3
case "Trapped":
validate := func(s string) bool { return memberOf(s, []string{"True", "False", "Unknown"}) }
_, err = xRefTable.DereferenceName(v, V13, validate)
// text string, optional
default:
err = handleDefault(xRefTable, v)
}
if err != nil {
return false, err
}
}
return hasModDate, nil
}
func validateDocumentInfoObject(xRefTable *XRefTable) error {
// Document info object is optional.
if xRefTable.Info == nil {
return nil
}
log.Debug.Println("*** validateDocumentInfoObject begin ***")
hasModDate, err := validateDocumentInfoDict(xRefTable, *xRefTable.Info)
if err != nil {
return err
}
hasPieceInfo, err := xRefTable.CatalogHasPieceInfo()
if err != nil {
return err
}
if hasPieceInfo && !hasModDate {
return errors.Errorf("validateDocumentInfoObject: missing required entry \"ModDate\"")
}
log.Debug.Println("*** validateDocumentInfoObject end ***")
return nil
}