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
|
.idea
|
||||||
testfiles/*.pdf
|
testfiles/*.pdf
|
||||||
|
testfiles/*.pdf.broken
|
||||||
|
testfiles/*.pdf.tmp
|
||||||
|
@@ -14,8 +14,15 @@ import (
|
|||||||
func findFirstPage(parent pdf.Value) (pdf.Value, error) {
|
func findFirstPage(parent pdf.Value) (pdf.Value, error) {
|
||||||
value_type := parent.Key("Type").String()
|
value_type := parent.Key("Type").String()
|
||||||
if value_type == "/Pages" {
|
if value_type == "/Pages" {
|
||||||
recurse_parent, recurse_err := findFirstPage(parent.Key("Kids").Index(0))
|
|
||||||
return recurse_parent, recurse_err
|
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" {
|
if value_type == "/Page" {
|
||||||
@@ -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.
|
// Track read/written bytes so we know when we're done.
|
||||||
read_bytes := int64(0)
|
read_bytes := int64(0)
|
||||||
|
|
||||||
|
if length <= 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Create a buffer for the chunks.
|
// Create a buffer for the chunks.
|
||||||
buf := make([]byte, max_chunk_length)
|
buf := make([]byte, max_chunk_length)
|
||||||
for {
|
for {
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package sign
|
package sign
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strconv"
|
"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()
|
rootPtr := root.GetPtr()
|
||||||
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
||||||
|
|
||||||
pages := root.Key("Pages").GetPtr()
|
if found_pages {
|
||||||
catalog += " /Pages " + strconv.Itoa(int(pages.GetID())) + " " + strconv.Itoa(int(pages.GetGen())) + " R"
|
pages := root.Key("Pages").GetPtr()
|
||||||
|
catalog += " /Pages " + strconv.Itoa(int(pages.GetID())) + " " + strconv.Itoa(int(pages.GetGen())) + " R"
|
||||||
|
}
|
||||||
|
|
||||||
catalog += " /AcroForm <<"
|
catalog += " /AcroForm <<"
|
||||||
catalog += " /Fields [" + strconv.Itoa(int(context.VisualSignData.ObjectId)) + " 0 R]"
|
catalog += " /Fields [" + strconv.Itoa(int(context.VisualSignData.ObjectId)) + " 0 R]"
|
||||||
|
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
package sign
|
package sign
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,22 +20,21 @@ 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()
|
rootPtr := root.GetPtr()
|
||||||
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
context.CatalogData.RootString = strconv.Itoa(int(rootPtr.GetID())) + " " + strconv.Itoa(int(rootPtr.GetGen())) + " R"
|
||||||
|
|
||||||
first_page, err := findFirstPage(root.Key("Pages"))
|
if found_pages {
|
||||||
if err != nil {
|
first_page, err := findFirstPage(root.Key("Pages"))
|
||||||
return "", err
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
first_page_ptr := first_page.GetPtr()
|
||||||
|
|
||||||
|
visual_signature += " /P " + strconv.Itoa(int(first_page_ptr.GetID())) + " " + strconv.Itoa(int(first_page_ptr.GetGen())) + " R"
|
||||||
}
|
}
|
||||||
|
|
||||||
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 += " /F 132"
|
||||||
visual_signature += " /FT /Sig"
|
visual_signature += " /FT /Sig"
|
||||||
visual_signature += " /T " + pdfString("Signature")
|
visual_signature += " /T " + pdfString("Signature")
|
||||||
|
@@ -27,6 +27,7 @@ func (context *SignContext) writeXref() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (context *SignContext) writeXrefTable() 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)
|
xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount, 10)
|
||||||
new_xref_size := "xref\n0 " + strconv.FormatInt(context.PDFReader.XrefInformation.ItemCount+4, 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 {
|
for _, f := range files {
|
||||||
ext := filepath.Ext(f.Name())
|
ext := filepath.Ext(f.Name())
|
||||||
if ext != ".pdf" {
|
if ext != ".pdf" {
|
||||||
fmt.Printf("Skipping file %s", f.Name())
|
fmt.Printf("Skipping file %s\n", f.Name())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
input_file, err := os.Open("../testfiles/" + f.Name())
|
fmt.Printf("Signing file %s\n", 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
|
|
||||||
}
|
|
||||||
|
|
||||||
err = SignFile("../testfiles/"+f.Name(), "../testfiles/"+f.Name()+".tmp", SignData{
|
err = SignFile("../testfiles/"+f.Name(), "../testfiles/"+f.Name()+".tmp", SignData{
|
||||||
Signature: SignDataSignature{
|
Signature: SignDataSignature{
|
||||||
@@ -174,11 +155,8 @@ func TestSignPDF(t *testing.T) {
|
|||||||
defer os.Remove("../testfiles/"+f.Name()+".tmp")
|
defer os.Remove("../testfiles/"+f.Name()+".tmp")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
input_file.Close()
|
|
||||||
t.Errorf("%s: %s", f.Name(), err.Error())
|
t.Errorf("%s: %s", f.Name(), err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
input_file.Close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user