Fix PDF 1.7 with XRef Stream causes panic #61

This commit is contained in:
Paul van Brouwershaven
2025-02-25 18:13:10 +01:00
parent 1fb39a3ce9
commit 681997c680
4 changed files with 159 additions and 76 deletions

59
sign/pdfxref_test.go Normal file
View File

@@ -0,0 +1,59 @@
package sign
import (
"os"
"testing"
"github.com/digitorus/pdf"
)
func TestGetLastObjectIDFromXref(t *testing.T) {
testCases := []struct {
fileName string
expected uint32
}{
{"minimal.pdf", 5},
{"testfile12.pdf", 16},
{"testfile14.pdf", 15},
{"testfile16.pdf", 567},
{"testfile17.pdf", 20},
{"testfile20.pdf", 10},
{"testfile21.pdf", 16},
{"small.pdf", 7},
}
for _, tc := range testCases {
t.Run(tc.fileName, func(st *testing.T) {
//st.Parallel()
input_file, err := os.Open("../testfiles/" + tc.fileName)
if err != nil {
st.Fatalf("%s: %s", tc.fileName, err.Error())
}
defer input_file.Close()
finfo, err := input_file.Stat()
if err != nil {
st.Fatalf("%s: %s", tc.fileName, err.Error())
}
size := finfo.Size()
r, err := pdf.NewReader(input_file, size)
if err != nil {
st.Fatalf("%s: %s", tc.fileName, err.Error())
}
sc := &SignContext{
InputFile: input_file,
PDFReader: r,
}
obj, err := sc.getLastObjectIDFromXref()
if err != nil {
st.Fatalf("%s: %s", tc.fileName, err.Error())
}
if obj != tc.expected {
st.Fatalf("%s: expected object id %d, got %d", tc.fileName, tc.expected, obj)
}
})
}
}