Nothing Special   »   [go: up one dir, main page]

Skip to content

Commit

Permalink
Improve reading corrupted files
Browse files Browse the repository at this point in the history
  • Loading branch information
hhrutter committed Jun 15, 2024
1 parent c342327 commit a467f3c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
26 changes: 18 additions & 8 deletions pkg/pdfcpu/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,25 @@ func imageStub(
thumb, imgMask bool,
objNr int) (*model.Image, error) {

w := sd.IntEntry("Width")
if w == nil {
obj, ok := sd.Find("Width")
if !ok {
return nil, errors.Errorf("pdfcpu: missing image width obj#%d", objNr)
}
i, err := ctx.DereferenceInteger(obj)
if err != nil {
return nil, err
}
w := i.Value()

h := sd.IntEntry("Height")
if h == nil {
obj, ok = sd.Find("Height")
if !ok {
return nil, errors.Errorf("pdfcpu: missing image height obj#%d", objNr)
}
i, err = ctx.DereferenceInteger(obj)
if err != nil {
return nil, err
}
h := i.Value()

cs, err := ColorSpaceString(ctx, sd)
if err != nil {
Expand Down Expand Up @@ -271,7 +281,7 @@ func imageStub(
interpol = true
}

i, err := StreamLength(ctx, sd)
size, err := StreamLength(ctx, sd)
if err != nil {
return nil, err
}
Expand All @@ -288,13 +298,13 @@ func imageStub(
IsImgMask: imgMask,
HasImgMask: mask,
HasSMask: sMask,
Width: *w,
Height: *h,
Width: w,
Height: h,
Cs: cs,
Comp: comp,
Bpc: bpc,
Interpol: interpol,
Size: i,
Size: size,
Filter: filters,
DecodeParms: s,
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/pdfcpu/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,10 @@ func parseAndLoad(c context.Context, ctx *model.Context, line string, offset *in
Offset: offset,
Generation: generation}

ctx.Table[*objNr] = &entry
if !ctx.XRefTable.Exists(*objNr) {
ctx.Table[*objNr] = &entry
}

o, err := ParseObjectWithContext(c, ctx, *entry.Offset, *objNr, *entry.Generation)
if err != nil {
return err
Expand Down
21 changes: 19 additions & 2 deletions pkg/pdfcpu/writeImage.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,25 @@ func pdfImage(xRefTable *model.XRefTable, sd *types.StreamDict, thumb bool, objN

bpc := *sd.IntEntry("BitsPerComponent")

w := *sd.IntEntry("Width")
h := *sd.IntEntry("Height")
obj, ok := sd.Find("Width")
if !ok {
return nil, errors.Errorf("pdfcpu: missing image width obj#%d", objNr)
}
i, err := xRefTable.DereferenceInteger(obj)
if err != nil {
return nil, err
}
w := i.Value()

obj, ok = sd.Find("Height")
if !ok {
return nil, errors.Errorf("pdfcpu: missing image height obj#%d", objNr)
}
i, err = xRefTable.DereferenceInteger(obj)
if err != nil {
return nil, err
}
h := i.Value()

decode := decodeArr(sd.ArrayEntry("Decode"))

Expand Down

0 comments on commit a467f3c

Please sign in to comment.