some improvments

This commit is contained in:
Adrian Zürcher
2026-01-01 11:00:23 +01:00
parent 8f313c00f0
commit ef0778c8b3
20 changed files with 305 additions and 385 deletions

View File

@@ -10,21 +10,18 @@ import (
"gitea.tecamino.com/paadi/pdfmerge/internal/pdf/common"
)
// TODO (v3): Create a new type xrefType which can be an integer and can be used for improved type checking.
// TODO (v3): Unexport these constants and rename with camelCase.
const (
// XREF_TABLE_ENTRY indicates a normal xref table entry.
XREF_TABLE_ENTRY = iota
// xRefTableEntry indicates a normal xref table entry.
xRefTableEntry = iota
// XREF_OBJECT_STREAM indicates an xref entry in an xref object stream.
XREF_OBJECT_STREAM = iota
// xRefObjectStream indicates an xref entry in an xref object stream.
xRefObjectStream = iota
)
// XrefObject defines a cross reference entry which is a map between object number (with generation number) and the
// location of the actual object, either as a file offset (xref table entry), or as a location within an xref
// stream object (xref object stream).
// TODO (v3): Unexport.
type XrefObject struct {
type xRefObject struct {
xtype int
objectNumber int
generation int
@@ -36,32 +33,28 @@ type XrefObject struct {
}
// XrefTable is a map between object number and corresponding XrefObject.
// TODO (v3): Unexport.
// TODO: Consider changing to a slice, so can maintain the object order without sorting when analyzing.
type XrefTable map[int]XrefObject
type xRefTable map[int]xRefObject
// ObjectStream represents an object stream's information which can contain multiple indirect objects.
// The information specifies the number of objects and has information about offset locations for
// each object.
// TODO (v3): Unexport.
type ObjectStream struct {
N int // TODO (v3): Unexport.
type objectStream struct {
n int
ds []byte
offsets map[int]int64
}
// ObjectStreams defines a map between object numbers (object streams only) and underlying ObjectStream information.
type ObjectStreams map[int]ObjectStream
type ObjectStreams map[int]objectStream
// ObjectCache defines a map between object numbers and corresponding PdfObject. Serves as a cache for PdfObjects that
// have already been parsed.
// TODO (v3): Unexport.
type ObjectCache map[int]PdfObject
type objectCache map[int]PdfObject
// Get an object from an object stream.
func (parser *PdfParser) lookupObjectViaOS(sobjNumber int, objNum int) (PdfObject, error) {
var bufReader *bytes.Reader
var objstm ObjectStream
var objstm objectStream
var cached bool
objstm, cached = parser.objstms[sobjNumber]
@@ -149,7 +142,7 @@ func (parser *PdfParser) lookupObjectViaOS(sobjNumber int, objNum int) (PdfObjec
offsets[int(*onum)] = int64(*firstOffset + *offset)
}
objstm = ObjectStream{N: int(*N), ds: ds, offsets: offsets}
objstm = objectStream{n: int(*N), ds: ds, offsets: offsets}
parser.objstms[sobjNumber] = objstm
} else {
// Temporarily change the reader object to this decoded buffer.
@@ -246,7 +239,7 @@ func (parser *PdfParser) lookupByNumber(objNumber int, attemptRepairs bool) (Pdf
common.Log.Trace("Lookup obj number %d", objNumber)
switch xref.xtype {
case XREF_TABLE_ENTRY:
case xRefTableEntry:
common.Log.Trace("xrefobj obj num %d", xref.objectNumber)
common.Log.Trace("xrefobj gen %d", xref.generation)
common.Log.Trace("xrefobj offset %d", xref.offset)
@@ -283,7 +276,7 @@ func (parser *PdfParser) lookupByNumber(objNumber int, attemptRepairs bool) (Pdf
return nil, false, err
}
// Empty the cache.
parser.ObjCache = ObjectCache{}
parser.ObjCache = objectCache{}
// Try looking up again and return.
return parser.lookupByNumberWrapper(objNumber, false)
}
@@ -292,7 +285,7 @@ func (parser *PdfParser) lookupByNumber(objNumber int, attemptRepairs bool) (Pdf
common.Log.Trace("Returning obj")
parser.ObjCache[objNumber] = obj
return obj, false, nil
case XREF_OBJECT_STREAM:
case xRefObjectStream:
common.Log.Trace("xref from object stream!")
common.Log.Trace(">Load via OS!")
common.Log.Trace("Object stream available in object %d/%d", xref.osObjNumber, xref.osObjIndex)
@@ -361,7 +354,7 @@ func (parser *PdfParser) Trace(obj PdfObject) (PdfObject, error) {
return o, nil
}
func printXrefTable(xrefTable XrefTable) {
func printXrefTable(xrefTable xRefTable) {
common.Log.Debug("=X=X=X=")
common.Log.Debug("Xref table:")
i := 0