Go: add Updater.Dereference()

This commit is contained in:
Přemysl Eric Janouch 2021-12-08 21:33:09 +01:00
parent d8171b9ac4
commit 1a3c7a8282
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 15 additions and 7 deletions

View File

@ -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. // Allocate allocates a new object number.
func (u *Updater) Allocate() uint { func (u *Updater) Allocate() uint {
n := u.xrefSize 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, // GetFirstPage retrieves the first page of the given page (sub)tree reference,
// or returns a Nil object if unsuccessful. // or returns a Nil object if unsuccessful.
func (u *Updater) GetFirstPage(nodeN, nodeGeneration uint) Object { func (u *Updater) GetFirstPage(node Object) Object {
obj, err := u.Get(nodeN, nodeGeneration) obj, err := u.Dereference(node)
if err != nil || obj.Kind != Dict { if err != nil || obj.Kind != Dict {
return New(Nil) return New(Nil)
} }
// Out of convenience; these aren't filled normally. // Out of convenience; these aren't filled normally.
obj.N = nodeN obj.N = node.N
obj.Generation = nodeGeneration obj.Generation = node.Generation
if typ, ok := obj.Dict["Type"]; !ok || typ.Kind != Name { if typ, ok := obj.Dict["Type"]; !ok || typ.Kind != Name {
return New(Nil) 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. // 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 { if !ok || rootRef.Kind != Reference {
return nil, errors.New("trailer does not contain a reference to Root") 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 { if err != nil {
return nil, fmt.Errorf("Root dictionary retrieval failed: %s", err) 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 { if !ok || pagesRef.Kind != Reference {
return nil, errors.New("invalid Pages reference") return nil, errors.New("invalid Pages reference")
} }
page := pdf.GetFirstPage(pagesRef.N, pagesRef.Generation) page := pdf.GetFirstPage(pagesRef)
if page.Kind != Dict { if page.Kind != Dict {
return nil, errors.New("invalid or unsupported page tree") return nil, errors.New("invalid or unsupported page tree")
} }