Dont parse twice in test, fix some bugs with special pdfs
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
||||
.idea
|
||||
testfiles/*.pdf
|
||||
testfiles/*.pdf.broken
|
||||
testfiles/*.pdf.tmp
|
||||
|
@@ -14,9 +14,16 @@ import (
|
||||
func findFirstPage(parent pdf.Value) (pdf.Value, error) {
|
||||
value_type := parent.Key("Type").String()
|
||||
if value_type == "/Pages" {
|
||||
recurse_parent, recurse_err := findFirstPage(parent.Key("Kids").Index(0))
|
||||
|
||||
for i := 0; i < parent.Key("Kids").Len(); i++ {
|
||||
recurse_parent, recurse_err := findFirstPage(parent.Key("Kids").Index(i))
|
||||
if recurse_err == nil {
|
||||
return recurse_parent, recurse_err
|
||||
}
|
||||
}
|
||||
|
||||
return parent, errors.New("Could not find first page.")
|
||||
}
|
||||
|
||||
if value_type == "/Page" {
|
||||
return parent, nil
|
||||
@@ -83,6 +90,10 @@ func writePartFromSourceFileToTargetFile(input_file *os.File, output_file *os.Fi
|
||||
// Track read/written bytes so we know when we're done.
|
||||
read_bytes := int64(0)
|
||||
|
||||
if length <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Create a buffer for the chunks.
|
||||
buf := make([]byte, max_chunk_length)
|
||||
for {
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -20,15 +19,14 @@ func (context *SignContext) createCatalog() (catalog string, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
if !found_pages {
|
||||
return "", errors.New("Didn't find pages in PDF trailer Root.")
|
||||
}
|
||||
|
||||
rootPtr := root.GetPtr()
|
||||
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
||||
|
||||
if found_pages {
|
||||
pages := root.Key("Pages").GetPtr()
|
||||
catalog += " /Pages " + strconv.Itoa(int(pages.GetID())) + " " + strconv.Itoa(int(pages.GetGen())) + " R"
|
||||
}
|
||||
|
||||
catalog += " /AcroForm <<"
|
||||
catalog += " /Fields [" + strconv.Itoa(int(context.VisualSignData.ObjectId)) + " 0 R]"
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package sign
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
@@ -21,13 +20,11 @@ func (context *SignContext) createVisualSignature() (visual_signature string, er
|
||||
}
|
||||
}
|
||||
|
||||
if !found_pages {
|
||||
return "", errors.New("Didn't find pages in PDF trailer Root.")
|
||||
}
|
||||
|
||||
rootPtr := root.GetPtr()
|
||||
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
||||
|
||||
if found_pages {
|
||||
first_page, err := findFirstPage(root.Key("Pages"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -36,6 +33,7 @@ func (context *SignContext) createVisualSignature() (visual_signature string, er
|
||||
first_page_ptr := first_page.GetPtr()
|
||||
|
||||
visual_signature += " /P " + strconv.Itoa(int(first_page_ptr.GetID())) + " " + strconv.Itoa(int(first_page_ptr.GetGen())) + " R"
|
||||
}
|
||||
|
||||
visual_signature += " /F 132"
|
||||
visual_signature += " /FT /Sig"
|
||||
|
@@ -27,6 +27,7 @@ func (context *SignContext) writeXref() error {
|
||||
}
|
||||
|
||||
func (context *SignContext) writeXrefTable() error {
|
||||
// @todo: maybe we need a prev here too.
|
||||
xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount, 10)
|
||||
new_xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount+4, 10)
|
||||
|
||||
|
@@ -123,30 +123,11 @@ func TestSignPDF(t *testing.T) {
|
||||
for _, f := range files {
|
||||
ext := filepath.Ext(f.Name())
|
||||
if ext != ".pdf" {
|
||||
fmt.Printf("Skipping file %s", f.Name())
|
||||
fmt.Printf("Skipping file %s\n", f.Name())
|
||||
continue
|
||||
}
|
||||
|
||||
input_file, err := os.Open("../testfiles/" + f.Name())
|
||||
if err != nil {
|
||||
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
finfo, err := input_file.Stat()
|
||||
if err != nil {
|
||||
input_file.Close()
|
||||
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||
return
|
||||
}
|
||||
size := finfo.Size()
|
||||
|
||||
_, err = pdf.NewReader(input_file, size)
|
||||
if err != nil {
|
||||
input_file.Close()
|
||||
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Printf("Signing file %s\n", f.Name())
|
||||
|
||||
err = SignFile("../testfiles/"+f.Name(), "../testfiles/"+f.Name()+".tmp", SignData{
|
||||
Signature: SignDataSignature{
|
||||
@@ -174,11 +155,8 @@ func TestSignPDF(t *testing.T) {
|
||||
defer os.Remove("../testfiles/"+f.Name()+".tmp")
|
||||
|
||||
if err != nil {
|
||||
input_file.Close()
|
||||
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
input_file.Close()
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user