-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
955 lines (720 loc) · 24.2 KB
/
action.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/*
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 (
"strings"
pdf "github.com/pirogom/pdfcpu/pkg/pdfcpu"
"github.com/pkg/errors"
)
func validateGoToActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.2 Go-To Actions
required := REQUIRED
if xRefTable.ValidationMode == pdf.ValidationRelaxed {
required = OPTIONAL
}
// D, required, name, byte string or array
return validateDestinationEntry(xRefTable, d, dictName, "D", required, pdf.V10)
}
func validateGoToRActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.3 Remote Go-To Actions
// F, required, file specification
_, err := validateFileSpecEntry(xRefTable, d, dictName, "F", REQUIRED, pdf.V11)
if err != nil {
return err
}
// D, required, name, byte string or array
err = validateDestinationEntry(xRefTable, d, dictName, "D", REQUIRED, pdf.V10)
if err != nil {
return err
}
// NewWindow, optional, boolean, since V1.2
_, err = validateBooleanEntry(xRefTable, d, dictName, "NewWindow", OPTIONAL, pdf.V12, nil)
return err
}
func validateTargetDictEntry(xRefTable *pdf.XRefTable, d pdf.Dict, dictName, entryName string, required bool, sinceVersion pdf.Version) error {
// table 202
d1, err := validateDictEntry(xRefTable, d, dictName, entryName, required, sinceVersion, nil)
if err != nil || d1 == nil {
return err
}
dictName = "targetDict"
// R, required, name
_, err = validateNameEntry(xRefTable, d1, dictName, "R", REQUIRED, pdf.V10, func(s string) bool { return s == "P" || s == "C" })
if err != nil {
return err
}
// N, optional, byte string
_, err = validateStringEntry(xRefTable, d1, dictName, "N", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// P, optional, integer or byte string
err = validateIntOrStringEntry(xRefTable, d1, dictName, "P", OPTIONAL, pdf.V10)
if err != nil {
return err
}
// A, optional, integer or text string
err = validateIntOrStringEntry(xRefTable, d1, dictName, "A", OPTIONAL, pdf.V10)
if err != nil {
return err
}
// T, optional, target dict
return validateTargetDictEntry(xRefTable, d1, dictName, "T", OPTIONAL, pdf.V10)
}
func validateGoToEActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.4 Embedded Go-To Actions
// F, optional, file specification
f, err := validateFileSpecEntry(xRefTable, d, dictName, "F", OPTIONAL, pdf.V11)
if err != nil {
return err
}
// D, required, name, byte string or array
err = validateDestinationEntry(xRefTable, d, dictName, "D", REQUIRED, pdf.V10)
if err != nil {
return err
}
// NewWindow, optional, boolean, since V1.2
_, err = validateBooleanEntry(xRefTable, d, dictName, "NewWindow", OPTIONAL, pdf.V12, nil)
if err != nil {
return err
}
// T, required unless entry F is present, target dict
return validateTargetDictEntry(xRefTable, d, dictName, "T", f == nil, pdf.V10)
}
func validateWinDict(xRefTable *pdf.XRefTable, d pdf.Dict) error {
// see table 204
dictName := "winDict"
// F, required, byte string
_, err := validateStringEntry(xRefTable, d, dictName, "F", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
// D, optional, byte string
_, err = validateStringEntry(xRefTable, d, dictName, "D", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// O, optional, ASCII string
_, err = validateStringEntry(xRefTable, d, dictName, "O", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// P, optional, byte string
_, err = validateStringEntry(xRefTable, d, dictName, "P", OPTIONAL, pdf.V10, nil)
return err
}
func validateLaunchActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.5
// F, optional, file specification
_, err := validateFileSpecEntry(xRefTable, d, dictName, "F", OPTIONAL, pdf.V11)
if err != nil {
return err
}
// Win, optional, dict
d1, err := validateDictEntry(xRefTable, d, dictName, "Win", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
if d1 != nil {
err = validateWinDict(xRefTable, d1)
}
// Mac, optional, undefined dict
// Unix, optional, undefined dict
return err
}
func validateDestinationThreadEntry(xRefTable *pdf.XRefTable, d pdf.Dict, dictName, entryName string, required bool, sinceVersion pdf.Version) error {
// The destination thread (table 205)
o, err := validateEntry(xRefTable, d, dictName, entryName, required, sinceVersion)
if err != nil || o == nil {
return err
}
switch o.(type) {
case pdf.Dict, pdf.StringLiteral, pdf.Integer:
// an indRef to a thread dictionary
// or an index of the thread within the roots Threads array
// or the title of the thread as specified in its thread info dict
default:
return errors.Errorf("validateDestinationThreadEntry: dict=%s entry=%s invalid type", dictName, entryName)
}
return nil
}
func validateDestinationBeadEntry(xRefTable *pdf.XRefTable, d pdf.Dict, dictName, entryName string, required bool, sinceVersion pdf.Version) error {
// The bead in the destination thread (table 205)
o, err := validateEntry(xRefTable, d, dictName, entryName, required, sinceVersion)
if err != nil || o == nil {
return err
}
switch o.(type) {
case pdf.Dict, pdf.Integer:
// an indRef to a bead dictionary of a thread in the current file
// or an index of the thread within its thread
default:
return errors.Errorf("validateDestinationBeadEntry: dict=%s entry=%s invalid type", dictName, entryName)
}
return nil
}
func validateThreadActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
//see 12.6.4.6
// F, optional, file specification
_, err := validateFileSpecEntry(xRefTable, d, dictName, "F", OPTIONAL, pdf.V11)
if err != nil {
return err
}
// D, required, indRef to thread dict, integer or text string.
err = validateDestinationThreadEntry(xRefTable, d, dictName, "D", REQUIRED, pdf.V10)
if err != nil {
return err
}
// B, optional, indRef to bead dict or integer.
return validateDestinationBeadEntry(xRefTable, d, dictName, "B", OPTIONAL, pdf.V10)
}
func hasURIForChecking(xRefTable *pdf.XRefTable, s string) bool {
for _, links := range xRefTable.URIs {
for uri := range links {
if uri == s {
return true
}
}
}
return false
}
func validateURIActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.7
// URI, required, string
uri, err := validateStringEntry(xRefTable, d, dictName, "URI", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
// Record URIs for link checking.
if xRefTable.ValidateLinks && uri != nil &&
strings.HasPrefix(*uri, "http") && !hasURIForChecking(xRefTable, *uri) {
if len(xRefTable.URIs[xRefTable.CurPage]) == 0 {
xRefTable.URIs[xRefTable.CurPage] = map[string]string{}
}
xRefTable.URIs[xRefTable.CurPage][*uri] = ""
}
// IsMap, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "IsMap", OPTIONAL, pdf.V10, nil)
return err
}
func validateSoundDictEntry(xRefTable *pdf.XRefTable, d pdf.Dict, dictName, entryName string, required bool, sinceVersion pdf.Version) error {
sd, err := validateStreamDictEntry(xRefTable, d, dictName, entryName, required, sinceVersion, nil)
if err != nil || sd == nil {
return err
}
dictName = "soundDict"
// Type, optional, name
_, err = validateNameEntry(xRefTable, sd.Dict, dictName, "Type", OPTIONAL, pdf.V10, func(s string) bool { return s == "Sound" })
if err != nil {
return err
}
// R, required, number - sampling rate
_, err = validateNumberEntry(xRefTable, sd.Dict, dictName, "R", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// C, required, integer - # of sound channels
_, err = validateIntegerEntry(xRefTable, sd.Dict, dictName, "C", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// B, required, integer - bits per sample value per channel
_, err = validateIntegerEntry(xRefTable, sd.Dict, dictName, "B", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// E, optional, name - encoding format
validateSampleDataEncoding := func(s string) bool {
return pdf.MemberOf(s, []string{"Raw", "Signed", "muLaw", "ALaw"})
}
_, err = validateNameEntry(xRefTable, sd.Dict, dictName, "E", OPTIONAL, pdf.V10, validateSampleDataEncoding)
return err
}
func validateSoundActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.8
// Sound, required, stream dict
err := validateSoundDictEntry(xRefTable, d, dictName, "Sound", REQUIRED, pdf.V10)
if err != nil {
return err
}
// Volume, optional, number: -1.0 .. +1.0
_, err = validateNumberEntry(xRefTable, d, dictName, "Volume", OPTIONAL, pdf.V10, func(f float64) bool { return -1.0 <= f && f <= 1.0 })
if err != nil {
return err
}
// Synchronous, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "Synchronous", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// Repeat, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "Repeat", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// Mix, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "Mix", OPTIONAL, pdf.V10, nil)
return err
}
func validateMovieStartOrDurationEntry(xRefTable *pdf.XRefTable, d pdf.Dict, dictName, entryName string, required bool, sinceVersion pdf.Version) error {
o, err := validateEntry(xRefTable, d, dictName, entryName, required, sinceVersion)
if err != nil || o == nil {
return err
}
switch o := o.(type) {
case pdf.Integer, pdf.StringLiteral:
// no further processing
case pdf.Array:
if len(o) != 2 {
return errors.New("pdfcpu: validateMovieStartOrDurationEntry: array length <> 2")
}
}
return nil
}
func validateMovieActivationDict(xRefTable *pdf.XRefTable, d pdf.Dict) error {
dictName := "movieActivationDict"
// Start, optional
err := validateMovieStartOrDurationEntry(xRefTable, d, dictName, "Start", OPTIONAL, pdf.V10)
if err != nil {
return err
}
// Duration, optional
err = validateMovieStartOrDurationEntry(xRefTable, d, dictName, "Duration", OPTIONAL, pdf.V10)
if err != nil {
return err
}
// Rate, optional, number
_, err = validateNumberEntry(xRefTable, d, dictName, "Rate", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// Volume, optional, number
_, err = validateNumberEntry(xRefTable, d, dictName, "Volume", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// ShowControls, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "ShowControls", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// Mode, optional, name
validatePlayMode := func(s string) bool {
return pdf.MemberOf(s, []string{"Once", "Open", "Repeat", "Palindrome"})
}
_, err = validateNameEntry(xRefTable, d, dictName, "Mode", OPTIONAL, pdf.V10, validatePlayMode)
if err != nil {
return err
}
// Synchronous, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "Synchronous", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
// FWScale, optional, array of 2 positive integers
_, err = validateIntegerArrayEntry(xRefTable, d, dictName, "FWScale", OPTIONAL, pdf.V10, func(a pdf.Array) bool { return len(a) == 2 })
if err != nil {
return err
}
// FWPosition, optional, array of 2 numbers [0.0 .. 1.0]
_, err = validateNumberArrayEntry(xRefTable, d, dictName, "FWPosition", OPTIONAL, pdf.V10, func(a pdf.Array) bool { return len(a) == 2 })
return err
}
func validateMovieActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.9
// is a movie activation dict
err := validateMovieActivationDict(xRefTable, d)
if err != nil {
return err
}
// Needs either Annotation or T entry but not both.
// T, text string
_, err = validateStringEntry(xRefTable, d, dictName, "T", OPTIONAL, pdf.V10, nil)
if err == nil {
return nil
}
// Annotation, indRef of movie annotation dict
ir, err := validateIndRefEntry(xRefTable, d, dictName, "Annotation", REQUIRED, pdf.V10)
if err != nil || ir == nil {
return err
}
d, err = xRefTable.DereferenceDict(*ir)
if err != nil || d == nil {
return errors.New("pdfcpu: validateMovieActionDict: missing required entry \"T\" or \"Annotation\"")
}
_, err = validateNameEntry(xRefTable, d, "annotDict", "Subtype", REQUIRED, pdf.V10, func(s string) bool { return s == "Movie" })
return err
}
func validateHideActionDictEntryT(xRefTable *pdf.XRefTable, o pdf.Object) error {
switch o := o.(type) {
case pdf.StringLiteral:
// Ensure UTF16 correctness.
_, err := pdf.StringLiteralToString(o)
if err != nil {
return err
}
case pdf.Dict:
// annotDict, Check for required name Subtype
_, err := validateNameEntry(xRefTable, o, "annotDict", "Subtype", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
case pdf.Array:
// mixed array of annotationDict indRefs and strings
for _, v := range o {
o, err := xRefTable.Dereference(v)
if err != nil {
return err
}
if o == nil {
continue
}
switch o := o.(type) {
case pdf.StringLiteral:
// Ensure UTF16 correctness.
_, err = pdf.StringLiteralToString(o)
if err != nil {
return err
}
case pdf.Dict:
// annotDict, Check for required name Subtype
_, err = validateNameEntry(xRefTable, o, "annotDict", "Subtype", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
}
}
default:
return errors.Errorf("validateHideActionDict: invalid entry \"T\"")
}
return nil
}
func validateHideActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.10
// T, required, dict, text string or array
o, found := d.Find("T")
if !found || o == nil {
return errors.New("pdfcpu: validateHideActionDict: missing required entry \"T\"")
}
o, err := xRefTable.Dereference(o)
if err != nil || o == nil {
return err
}
err = validateHideActionDictEntryT(xRefTable, o)
if err != nil {
return err
}
// H, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "H", OPTIONAL, pdf.V10, nil)
return err
}
func validateNamedActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.11
validate := func(s string) bool {
if pdf.MemberOf(s, []string{"NextPage", "PrevPage", "FirstPage", "Lastpage"}) {
return true
}
// Some known non standard named actions
if pdf.MemberOf(s, []string{"GoToPage", "GoBack", "GoForward", "Find", "Print", "Quit", "FullScreen"}) {
return true
}
return false
}
_, err := validateNameEntry(xRefTable, d, dictName, "N", REQUIRED, pdf.V10, validate)
return err
}
func validateSubmitFormActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.7.5.2
// F, required, URL specification
_, err := validateURLSpecEntry(xRefTable, d, dictName, "F", REQUIRED, pdf.V10)
if err != nil {
return err
}
// Fields, optional, array
// Each element of the array shall be either an indirect reference to a field dictionary
// or (PDF 1.3) a text string representing the fully qualified name of a field.
a, err := validateArrayEntry(xRefTable, d, dictName, "Fields", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
for _, v := range a {
switch v.(type) {
case pdf.StringLiteral, pdf.IndirectRef:
// no further processing
default:
return errors.New("pdfcpu: validateSubmitFormActionDict: unknown Fields entry")
}
}
// Flags, optional, integer
_, err = validateIntegerEntry(xRefTable, d, dictName, "Flags", OPTIONAL, pdf.V10, nil)
return err
}
func validateResetFormActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.7.5.3
// Fields, optional, array
// Each element of the array shall be either an indirect reference to a field dictionary
// or (PDF 1.3) a text string representing the fully qualified name of a field.
a, err := validateArrayEntry(xRefTable, d, dictName, "Fields", OPTIONAL, pdf.V10, nil)
if err != nil {
return err
}
for _, v := range a {
switch v.(type) {
case pdf.StringLiteral, pdf.IndirectRef:
// no further processing
default:
return errors.New("pdfcpu: validateResetFormActionDict: unknown Fields entry")
}
}
// Flags, optional, integer
_, err = validateIntegerEntry(xRefTable, d, dictName, "Flags", OPTIONAL, pdf.V10, nil)
return err
}
func validateImportDataActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.7.5.4
// F, required, file specification
_, err := validateFileSpecEntry(xRefTable, d, dictName, "F", OPTIONAL, pdf.V11)
return err
}
func validateJavaScript(xRefTable *pdf.XRefTable, d pdf.Dict, dictName, entryName string, required bool) error {
o, err := validateEntry(xRefTable, d, dictName, entryName, required, pdf.V13)
if err != nil || o == nil {
return err
}
switch o := o.(type) {
case pdf.StringLiteral:
// Ensure UTF16 correctness.
_, err = pdf.StringLiteralToString(o)
case pdf.HexLiteral:
// Ensure UTF16 correctness.
_, err = pdf.HexLiteralToString(o)
case pdf.StreamDict:
// no further processing
default:
err = errors.Errorf("validateJavaScript: invalid type\n")
}
return err
}
func validateJavaScriptActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.16
// JS, required, text string or stream
return validateJavaScript(xRefTable, d, dictName, "JS", REQUIRED)
}
func validateSetOCGStateActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.12
// State, required, array
_, err := validateArrayEntry(xRefTable, d, dictName, "State", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
// PreserveRB, optional, boolean
_, err = validateBooleanEntry(xRefTable, d, dictName, "PreserveRB", OPTIONAL, pdf.V10, nil)
return err
}
func validateRenditionActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.13
// OP or JS need to be present.
// OP, integer
op, err := validateIntegerEntry(xRefTable, d, dictName, "OP", OPTIONAL, pdf.V15, func(i int) bool { return 0 <= i && i <= 4 })
if err != nil {
return err
}
// JS, text string or stream
err = validateJavaScript(xRefTable, d, dictName, "JS", op == nil)
if err != nil {
return err
}
// R, required for OP 0 and 4, rendition object dict
required := func(op *pdf.Integer) bool {
if op == nil {
return false
}
v := op.Value()
return v == 0 || v == 4
}(op)
d1, err := validateDictEntry(xRefTable, d, dictName, "R", required, pdf.V15, nil)
if err != nil {
return err
}
if d1 != nil {
err = validateRenditionDict(xRefTable, d1, pdf.V15)
if err != nil {
return err
}
}
// AN, required for any OP 0..4, indRef of screen annotation dict
d1, err = validateDictEntry(xRefTable, d, dictName, "AN", op != nil, pdf.V10, nil)
if err != nil {
return err
}
if d1 != nil {
_, err = validateNameEntry(xRefTable, d1, dictName, "Subtype", REQUIRED, pdf.V10, func(s string) bool { return s == "Screen" })
if err != nil {
return err
}
}
return nil
}
func validateTransActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.14
// Trans, required, transitionDict
d1, err := validateDictEntry(xRefTable, d, dictName, "Trans", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
return validateTransitionDict(xRefTable, d1)
}
func validateGoTo3DViewActionDict(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error {
// see 12.6.4.15
// TA, required, target annotation
d1, err := validateDictEntry(xRefTable, d, dictName, "TA", REQUIRED, pdf.V16, nil)
if err != nil {
return err
}
_, err = validateAnnotationDict(xRefTable, d1)
if err != nil {
return err
}
// V, required, the view to use: 3DViewDict or integer or text string or name
// TODO Validation.
_, err = validateEntry(xRefTable, d, dictName, "V", REQUIRED, pdf.V16)
return err
}
func validateActionDictCore(xRefTable *pdf.XRefTable, n *pdf.Name, d pdf.Dict) error {
for k, v := range map[string]struct {
validate func(xRefTable *pdf.XRefTable, d pdf.Dict, dictName string) error
sinceVersion pdf.Version
}{
"GoTo": {validateGoToActionDict, pdf.V10},
"GoToR": {validateGoToRActionDict, pdf.V10},
"GoToE": {validateGoToEActionDict, pdf.V16},
"Launch": {validateLaunchActionDict, pdf.V10},
"Thread": {validateThreadActionDict, pdf.V10},
"URI": {validateURIActionDict, pdf.V10},
"Sound": {validateSoundActionDict, pdf.V12},
"Movie": {validateMovieActionDict, pdf.V12},
"Hide": {validateHideActionDict, pdf.V12},
"Named": {validateNamedActionDict, pdf.V12},
"SubmitForm": {validateSubmitFormActionDict, pdf.V10},
"ResetForm": {validateResetFormActionDict, pdf.V12},
"ImportData": {validateImportDataActionDict, pdf.V12},
"JavaScript": {validateJavaScriptActionDict, pdf.V13},
"SetOCGState": {validateSetOCGStateActionDict, pdf.V15},
"Rendition": {validateRenditionActionDict, pdf.V15},
"Trans": {validateTransActionDict, pdf.V15},
"GoTo3DView": {validateGoTo3DViewActionDict, pdf.V16},
} {
if n.Value() == k {
err := xRefTable.ValidateVersion(k, v.sinceVersion)
if err != nil {
return err
}
return v.validate(xRefTable, d, k)
}
}
return errors.Errorf("validateActionDictCore: unsupported action type: %s\n", *n)
}
func validateActionDict(xRefTable *pdf.XRefTable, d pdf.Dict) error {
dictName := "actionDict"
// Type, optional, name
allowedTypes := []string{"Action"}
if xRefTable.ValidationMode == pdf.ValidationRelaxed {
allowedTypes = []string{"A", "Action"}
}
_, err := validateNameEntry(xRefTable, d, dictName, "Type", OPTIONAL, pdf.V10, func(s string) bool { return pdf.MemberOf(s, allowedTypes) })
if err != nil {
return err
}
// S, required, name, action Type
s, err := validateNameEntry(xRefTable, d, dictName, "S", REQUIRED, pdf.V10, nil)
if err != nil {
return err
}
err = validateActionDictCore(xRefTable, s, d)
if err != nil {
return err
}
if o, ok := d.Find("Next"); ok {
// either optional action dict
d, err := xRefTable.DereferenceDict(o)
if err == nil {
return validateActionDict(xRefTable, d)
}
// or optional array of action dicts
a, err := xRefTable.DereferenceArray(o)
if err != nil {
return err
}
for _, v := range a {
d, err := xRefTable.DereferenceDict(v)
if err != nil {
return err
}
if d == nil {
continue
}
err = validateActionDict(xRefTable, d)
if err != nil {
return err
}
}
}
return nil
}
func validateRootAdditionalActions(xRefTable *pdf.XRefTable, rootDict pdf.Dict, required bool, sinceVersion pdf.Version) error {
return validateAdditionalActions(xRefTable, rootDict, "rootDict", "AA", required, sinceVersion, "root")
}
func validateAdditionalActions(xRefTable *pdf.XRefTable, dict pdf.Dict, dictName, entryName string, required bool, sinceVersion pdf.Version, source string) error {
d, err := validateDictEntry(xRefTable, dict, dictName, entryName, required, sinceVersion, nil)
if err != nil || d == nil {
return err
}
validateAdditionalAction := func(s, source string) bool {
switch source {
case "root":
if pdf.MemberOf(s, []string{"WC", "WS", "DS", "WP", "DP"}) {
return true
}
case "page":
if pdf.MemberOf(s, []string{"O", "C"}) {
return true
}
case "fieldOrAnnot":
// A terminal acro field may be merged with a widget annotation.
fieldOptions := []string{"K", "F", "V", "C"}
annotOptions := []string{"E", "X", "D", "U", "Fo", "Bl", "PO", "PC", "PV", "Pl"}
options := append(fieldOptions, annotOptions...)
if pdf.MemberOf(s, options) {
return true
}
}
return false
}
for k, v := range d {
if !validateAdditionalAction(k, source) {
return errors.Errorf("validateAdditionalActions: action %s not allowed for source %s", k, source)
}
d, err := xRefTable.DereferenceDict(v)
if err != nil {
return err
}
if d == nil {
continue
}
err = validateActionDict(xRefTable, d)
if err != nil {
return err
}
}
return nil
}