From 1a3c7a8282049630a180d91b2e0b2ac82d8b876f Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Wed, 8 Dec 2021 21:33:09 +0100 Subject: Go: add Updater.Dereference() --- pdf/pdf.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pdf/pdf.go b/pdf/pdf.go index cf7f9a8..05905c0 100644 --- a/pdf/pdf.go +++ b/pdf/pdf.go @@ -794,6 +794,14 @@ func (u *Updater) Get(n, generation uint) (Object, error) { } } +// Derefence dereferences Reference objects, and passes the other kinds through. +func (u *Updater) Dereference(o Object) (Object, error) { + if o.Kind != Reference { + return o, nil + } + return u.Get(o.N, o.Generation) +} + // Allocate allocates a new object number. func (u *Updater) Allocate() uint { n := u.xrefSize @@ -904,15 +912,15 @@ func NewDate(ts time.Time) Object { // GetFirstPage retrieves the first page of the given page (sub)tree reference, // or returns a Nil object if unsuccessful. -func (u *Updater) GetFirstPage(nodeN, nodeGeneration uint) Object { - obj, err := u.Get(nodeN, nodeGeneration) +func (u *Updater) GetFirstPage(node Object) Object { + obj, err := u.Dereference(node) if err != nil || obj.Kind != Dict { return New(Nil) } // Out of convenience; these aren't filled normally. - obj.N = nodeN - obj.Generation = nodeGeneration + obj.N = node.N + obj.Generation = node.Generation if typ, ok := obj.Dict["Type"]; !ok || typ.Kind != Name { return New(Nil) @@ -932,7 +940,7 @@ func (u *Updater) GetFirstPage(nodeN, nodeGeneration uint) Object { } // XXX: Nothing prevents us from recursing in an evil circular graph. - return u.GetFirstPage(kids.Array[0].N, kids.Array[0].Generation) + return u.GetFirstPage(kids.Array[0]) } // ----------------------------------------------------------------------------- @@ -1126,7 +1134,7 @@ func Sign(document []byte, key crypto.PrivateKey, certs []*x509.Certificate, if !ok || rootRef.Kind != Reference { return nil, errors.New("trailer does not contain a reference to Root") } - root, err := pdf.Get(rootRef.N, rootRef.Generation) + root, err := pdf.Dereference(rootRef) if err != nil { return nil, fmt.Errorf("Root dictionary retrieval failed: %s", err) } @@ -1183,7 +1191,7 @@ func Sign(document []byte, key crypto.PrivateKey, certs []*x509.Certificate, if !ok || pagesRef.Kind != Reference { return nil, errors.New("invalid Pages reference") } - page := pdf.GetFirstPage(pagesRef.N, pagesRef.Generation) + page := pdf.GetFirstPage(pagesRef) if page.Kind != Dict { return nil, errors.New("invalid or unsupported page tree") } -- cgit v1.2.3-70-g09d2