Skip to content

Commit

Permalink
bplist: teach the parser about unusual OffsetIntSize values (#78)
Browse files Browse the repository at this point in the history
This is only a partial fix which works for the value of 3 and for any
value < 8, but won't help in other cases. Supporting values < 16 should
also be possible, but I didn't bother with it. Also this doesn't touch
`bplist_generator` and hence won't make use of non-standard values for
generating plists (while those values could probably give a benefit of
slightly reduced plist size).

We confirmed that this is supported by the CoreFoundation bplist parser.

Closes #77
  • Loading branch information
MarSoft authored May 1, 2023
1 parent 663ca47 commit e03e84e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bplist_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ func (p *bplistParser) parseSizedInteger(off offset, nbytes int) (lo uint64, hi
case 16:
lo, hi = binary.BigEndian.Uint64(p.buffer[off+8:]), binary.BigEndian.Uint64(p.buffer[off:])
default:
panic(errors.New("illegal integer size"))
if nbytes > 8 {
panic(errors.New("illegal integer size"))
}
lo, hi = binary.BigEndian.Uint64(p.buffer[off-(8-offset(nbytes)):]) & ((1<<offset(nbytes*8))-1), 0
}
newOffset = off + offset(nbytes)
return
Expand Down
37 changes: 37 additions & 0 deletions bplist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,40 @@ func TestBplistLatin1ToUTF16(t *testing.T) {
return
}
}

func TestBplistNonPowerOfTwoOffsetIntSizes(t *testing.T) {
bplist := []byte{
'b', 'p', 'l', 'i', 's', 't', '0', '0',

// Array (2 entries)
0xA2,
0x01, 0x02,

// 0xFFFFFFFFFFFFFF80 (MinInt8, sign extended)
0x13, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80,

// 0x7F (MaxInt8)
0x10, 0x7f,

// Offset table (each entry is 3 bytes)
0x00, 0x00, 0x08,
0x00, 0x00, 0x0b,
0x00, 0x00, 0x14,

// Trailer
0x00, 0x00, 0x00, 0x00, 0x00,
0x00,
0x03,
0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16,
}

buf := bytes.NewReader(bplist)
d := newBplistParser(buf)
_, err := d.parseDocument()
if err != nil {
t.Error("Unexpected error", err)
}
}

0 comments on commit e03e84e

Please sign in to comment.