| 1 |
package plist |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"io/ioutil" |
| 10 |
"math" |
| 11 |
"runtime" |
| 12 |
"time" |
| 13 |
"unicode/utf16" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
signedHighBits = 0xFFFFFFFFFFFFFFFF |
| 18 |
) |
| 19 |
|
| 20 |
type offset uint64 |
| 21 |
|
| 22 |
type bplistParser struct { |
| 23 |
buffer []byte |
| 24 |
|
| 25 |
reader io.ReadSeeker |
| 26 |
version int |
| 27 |
objects []cfValue |
| 28 |
trailer bplistTrailer |
| 29 |
trailerOffset uint64 |
| 30 |
|
| 31 |
containerStack []offset |
| 32 |
} |
| 33 |
|
| 34 |
func (p *bplistParser) validateDocumentTrailer() { |
| 35 |
if p.trailer.OffsetTableOffset >= p.trailerOffset { |
| 36 |
panic(fmt.Errorf("offset table beyond beginning of trailer (0x%x, trailer@0x%x)", p.trailer.OffsetTableOffset, p.trailerOffset)) |
| 37 |
} |
| 38 |
|
| 39 |
if p.trailer.OffsetTableOffset < 9 { |
| 40 |
panic(fmt.Errorf("offset table begins inside header (0x%x)", p.trailer.OffsetTableOffset)) |
| 41 |
} |
| 42 |
|
| 43 |
if p.trailerOffset > (p.trailer.NumObjects*uint64(p.trailer.OffsetIntSize))+p.trailer.OffsetTableOffset { |
| 44 |
panic(errors.New("garbage between offset table and trailer")) |
| 45 |
} |
| 46 |
|
| 47 |
if p.trailer.OffsetTableOffset+(uint64(p.trailer.OffsetIntSize)*p.trailer.NumObjects) > p.trailerOffset { |
| 48 |
panic(errors.New("offset table isn't long enough to address every object")) |
| 49 |
} |
| 50 |
|
| 51 |
maxObjectRef := uint64(1) << (8 * p.trailer.ObjectRefSize) |
| 52 |
if p.trailer.NumObjects > maxObjectRef { |
| 53 |
panic(fmt.Errorf("more objects (%v) than object ref size (%v bytes) can support", p.trailer.NumObjects, p.trailer.ObjectRefSize)) |
| 54 |
} |
| 55 |
|
| 56 |
if p.trailer.OffsetIntSize < uint8(8) && (uint64(1)<<(8*p.trailer.OffsetIntSize)) <= p.trailer.OffsetTableOffset { |
| 57 |
panic(errors.New("offset size isn't big enough to address entire file")) |
| 58 |
} |
| 59 |
|
| 60 |
if p.trailer.TopObject >= p.trailer.NumObjects { |
| 61 |
panic(fmt.Errorf("top object #%d is out of range (only %d exist)", p.trailer.TopObject, p.trailer.NumObjects)) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func (p *bplistParser) parseDocument() (pval cfValue, parseError error) { |
| 66 |
defer func() { |
| 67 |
if r := recover(); r != nil { |
| 68 |
if _, ok := r.(runtime.Error); ok { |
| 69 |
panic(r) |
| 70 |
} |
| 71 |
|
| 72 |
parseError = plistParseError{"binary", r.(error)} |
| 73 |
} |
| 74 |
}() |
| 75 |
|
| 76 |
p.buffer, _ = ioutil.ReadAll(p.reader) |
| 77 |
|
| 78 |
l := len(p.buffer) |
| 79 |
if l < 40 { |
| 80 |
panic(errors.New("not enough data")) |
| 81 |
} |
| 82 |
|
| 83 |
if !bytes.Equal(p.buffer[0:6], []byte{'b', 'p', 'l', 'i', 's', 't'}) { |
| 84 |
panic(errors.New("incomprehensible magic")) |
| 85 |
} |
| 86 |
|
| 87 |
p.version = int(((p.buffer[6] - '0') * 10) + (p.buffer[7] - '0')) |
| 88 |
|
| 89 |
if p.version > 1 { |
| 90 |
panic(fmt.Errorf("unexpected version %d", p.version)) |
| 91 |
} |
| 92 |
|
| 93 |
p.trailerOffset = uint64(l - 32) |
| 94 |
p.trailer = bplistTrailer{ |
| 95 |
SortVersion: p.buffer[p.trailerOffset+5], |
| 96 |
OffsetIntSize: p.buffer[p.trailerOffset+6], |
| 97 |
ObjectRefSize: p.buffer[p.trailerOffset+7], |
| 98 |
NumObjects: binary.BigEndian.Uint64(p.buffer[p.trailerOffset+8:]), |
| 99 |
TopObject: binary.BigEndian.Uint64(p.buffer[p.trailerOffset+16:]), |
| 100 |
OffsetTableOffset: binary.BigEndian.Uint64(p.buffer[p.trailerOffset+24:]), |
| 101 |
} |
| 102 |
|
| 103 |
p.validateDocumentTrailer() |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
p.objects = make([]cfValue, p.trailer.NumObjects) |
| 113 |
|
| 114 |
pval = p.objectAtIndex(p.trailer.TopObject) |
| 115 |
return |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (p *bplistParser) parseSizedInteger(off offset, nbytes int) (lo uint64, hi uint64, newOffset offset) { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
switch nbytes { |
| 125 |
case 1: |
| 126 |
lo, hi = uint64(p.buffer[off]), 0 |
| 127 |
case 2: |
| 128 |
lo, hi = uint64(binary.BigEndian.Uint16(p.buffer[off:])), 0 |
| 129 |
case 4: |
| 130 |
lo, hi = uint64(binary.BigEndian.Uint32(p.buffer[off:])), 0 |
| 131 |
case 8: |
| 132 |
lo = binary.BigEndian.Uint64(p.buffer[off:]) |
| 133 |
if p.buffer[off]&0x80 != 0 { |
| 134 |
|
| 135 |
hi = signedHighBits |
| 136 |
} |
| 137 |
case 16: |
| 138 |
lo, hi = binary.BigEndian.Uint64(p.buffer[off+8:]), binary.BigEndian.Uint64(p.buffer[off:]) |
| 139 |
default: |
| 140 |
if nbytes > 8 { |
| 141 |
panic(errors.New("illegal integer size")) |
| 142 |
} |
| 143 |
lo, hi = binary.BigEndian.Uint64(p.buffer[off-(8-offset(nbytes)):]) & ((1<<offset(nbytes*8))-1), 0 |
| 144 |
} |
| 145 |
newOffset = off + offset(nbytes) |
| 146 |
return |
| 147 |
} |
| 148 |
|
| 149 |
func (p *bplistParser) parseObjectRefAtOffset(off offset) (uint64, offset) { |
| 150 |
oid, _, next := p.parseSizedInteger(off, int(p.trailer.ObjectRefSize)) |
| 151 |
return oid, next |
| 152 |
} |
| 153 |
|
| 154 |
func (p *bplistParser) parseOffsetAtOffset(off offset) (offset, offset) { |
| 155 |
parsedOffset, _, next := p.parseSizedInteger(off, int(p.trailer.OffsetIntSize)) |
| 156 |
return offset(parsedOffset), next |
| 157 |
} |
| 158 |
|
| 159 |
func (p *bplistParser) objectAtIndex(index uint64) cfValue { |
| 160 |
if index >= p.trailer.NumObjects { |
| 161 |
panic(fmt.Errorf("invalid object#%d (max %d)", index, p.trailer.NumObjects)) |
| 162 |
} |
| 163 |
|
| 164 |
if pval := p.objects[index]; pval != nil { |
| 165 |
return pval |
| 166 |
} |
| 167 |
|
| 168 |
off, _ := p.parseOffsetAtOffset(offset(p.trailer.OffsetTableOffset + (index * uint64(p.trailer.OffsetIntSize)))) |
| 169 |
if off > offset(p.trailer.OffsetTableOffset-1) { |
| 170 |
panic(fmt.Errorf("object#%d starts beyond beginning of object table (0x%x, table@0x%x)", index, off, p.trailer.OffsetTableOffset)) |
| 171 |
} |
| 172 |
|
| 173 |
pval := p.parseTagAtOffset(off) |
| 174 |
p.objects[index] = pval |
| 175 |
return pval |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
func (p *bplistParser) pushNestedObject(off offset) { |
| 180 |
for _, v := range p.containerStack { |
| 181 |
if v == off { |
| 182 |
p.panicNestedObject(off) |
| 183 |
} |
| 184 |
} |
| 185 |
p.containerStack = append(p.containerStack, off) |
| 186 |
} |
| 187 |
|
| 188 |
func (p *bplistParser) panicNestedObject(off offset) { |
| 189 |
ids := "" |
| 190 |
for _, v := range p.containerStack { |
| 191 |
ids += fmt.Sprintf("0x%x > ", v) |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
panic(fmt.Errorf("self-referential collection@0x%x (%s0x%x) cannot be deserialized", off, ids, off)) |
| 196 |
} |
| 197 |
|
| 198 |
func (p *bplistParser) popNestedObject() { |
| 199 |
p.containerStack = p.containerStack[:len(p.containerStack)-1] |
| 200 |
} |
| 201 |
|
| 202 |
func (p *bplistParser) parseTagAtOffset(off offset) cfValue { |
| 203 |
tag := p.buffer[off] |
| 204 |
|
| 205 |
switch tag & 0xF0 { |
| 206 |
case bpTagNull: |
| 207 |
switch tag & 0x0F { |
| 208 |
case bpTagBoolTrue, bpTagBoolFalse: |
| 209 |
return cfBoolean(tag == bpTagBoolTrue) |
| 210 |
} |
| 211 |
case bpTagInteger: |
| 212 |
lo, hi, _ := p.parseIntegerAtOffset(off) |
| 213 |
return &cfNumber{ |
| 214 |
signed: hi == signedHighBits, |
| 215 |
value: lo, |
| 216 |
} |
| 217 |
case bpTagReal: |
| 218 |
nbytes := 1 << (tag & 0x0F) |
| 219 |
switch nbytes { |
| 220 |
case 4: |
| 221 |
bits := binary.BigEndian.Uint32(p.buffer[off+1:]) |
| 222 |
return &cfReal{wide: false, value: float64(math.Float32frombits(bits))} |
| 223 |
case 8: |
| 224 |
bits := binary.BigEndian.Uint64(p.buffer[off+1:]) |
| 225 |
return &cfReal{wide: true, value: math.Float64frombits(bits)} |
| 226 |
} |
| 227 |
panic(errors.New("illegal float size")) |
| 228 |
case bpTagDate: |
| 229 |
bits := binary.BigEndian.Uint64(p.buffer[off+1:]) |
| 230 |
val := math.Float64frombits(bits) |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
val += 978307200 |
| 235 |
|
| 236 |
sec, fsec := math.Modf(val) |
| 237 |
time := time.Unix(int64(sec), int64(fsec*float64(time.Second))).In(time.UTC) |
| 238 |
return cfDate(time) |
| 239 |
case bpTagData: |
| 240 |
data := p.parseDataAtOffset(off) |
| 241 |
return cfData(data) |
| 242 |
case bpTagASCIIString: |
| 243 |
str := p.parseASCIIStringAtOffset(off) |
| 244 |
return cfString(str) |
| 245 |
case bpTagUTF16String: |
| 246 |
str := p.parseUTF16StringAtOffset(off) |
| 247 |
return cfString(str) |
| 248 |
case bpTagUID: |
| 249 |
lo, _, _ := p.parseSizedInteger(off+1, int(tag&0xF)+1) |
| 250 |
return cfUID(lo) |
| 251 |
case bpTagDictionary: |
| 252 |
return p.parseDictionaryAtOffset(off) |
| 253 |
case bpTagArray: |
| 254 |
return p.parseArrayAtOffset(off) |
| 255 |
} |
| 256 |
panic(fmt.Errorf("unexpected atom 0x%2.02x at offset 0x%x", tag, off)) |
| 257 |
} |
| 258 |
|
| 259 |
func (p *bplistParser) parseIntegerAtOffset(off offset) (uint64, uint64, offset) { |
| 260 |
tag := p.buffer[off] |
| 261 |
return p.parseSizedInteger(off+1, 1<<(tag&0xF)) |
| 262 |
} |
| 263 |
|
| 264 |
func (p *bplistParser) countForTagAtOffset(off offset) (uint64, offset) { |
| 265 |
tag := p.buffer[off] |
| 266 |
cnt := uint64(tag & 0x0F) |
| 267 |
if cnt == 0xF { |
| 268 |
cnt, _, off = p.parseIntegerAtOffset(off + 1) |
| 269 |
return cnt, off |
| 270 |
} |
| 271 |
return cnt, off + 1 |
| 272 |
} |
| 273 |
|
| 274 |
func (p *bplistParser) parseDataAtOffset(off offset) []byte { |
| 275 |
len, start := p.countForTagAtOffset(off) |
| 276 |
if start+offset(len) > offset(p.trailer.OffsetTableOffset) { |
| 277 |
panic(fmt.Errorf("data@0x%x too long (%v bytes, max is %v)", off, len, p.trailer.OffsetTableOffset-uint64(start))) |
| 278 |
} |
| 279 |
return p.buffer[start : start+offset(len)] |
| 280 |
} |
| 281 |
|
| 282 |
func (p *bplistParser) parseASCIIStringAtOffset(off offset) string { |
| 283 |
len, start := p.countForTagAtOffset(off) |
| 284 |
if start+offset(len) > offset(p.trailer.OffsetTableOffset) { |
| 285 |
panic(fmt.Errorf("ascii string@0x%x too long (%v bytes, max is %v)", off, len, p.trailer.OffsetTableOffset-uint64(start))) |
| 286 |
} |
| 287 |
|
| 288 |
return zeroCopy8BitString(p.buffer, int(start), int(len)) |
| 289 |
} |
| 290 |
|
| 291 |
func (p *bplistParser) parseUTF16StringAtOffset(off offset) string { |
| 292 |
len, start := p.countForTagAtOffset(off) |
| 293 |
bytes := len * 2 |
| 294 |
if start+offset(bytes) > offset(p.trailer.OffsetTableOffset) { |
| 295 |
panic(fmt.Errorf("utf16 string@0x%x too long (%v bytes, max is %v)", off, bytes, p.trailer.OffsetTableOffset-uint64(start))) |
| 296 |
} |
| 297 |
|
| 298 |
u16s := make([]uint16, len) |
| 299 |
for i := offset(0); i < offset(len); i++ { |
| 300 |
u16s[i] = binary.BigEndian.Uint16(p.buffer[start+(i*2):]) |
| 301 |
} |
| 302 |
runes := utf16.Decode(u16s) |
| 303 |
return string(runes) |
| 304 |
} |
| 305 |
|
| 306 |
func (p *bplistParser) parseObjectListAtOffset(off offset, count uint64) []cfValue { |
| 307 |
if off+offset(count*uint64(p.trailer.ObjectRefSize)) > offset(p.trailer.OffsetTableOffset) { |
| 308 |
panic(fmt.Errorf("list@0x%x length (%v) puts its end beyond the offset table at 0x%x", off, count, p.trailer.OffsetTableOffset)) |
| 309 |
} |
| 310 |
objects := make([]cfValue, count) |
| 311 |
|
| 312 |
next := off |
| 313 |
var oid uint64 |
| 314 |
for i := uint64(0); i < count; i++ { |
| 315 |
oid, next = p.parseObjectRefAtOffset(next) |
| 316 |
objects[i] = p.objectAtIndex(oid) |
| 317 |
} |
| 318 |
|
| 319 |
return objects |
| 320 |
} |
| 321 |
|
| 322 |
func (p *bplistParser) parseDictionaryAtOffset(off offset) *cfDictionary { |
| 323 |
p.pushNestedObject(off) |
| 324 |
defer p.popNestedObject() |
| 325 |
|
| 326 |
|
| 327 |
cnt, start := p.countForTagAtOffset(off) |
| 328 |
objects := p.parseObjectListAtOffset(start, cnt*2) |
| 329 |
|
| 330 |
keys := make([]string, cnt) |
| 331 |
for i := uint64(0); i < cnt; i++ { |
| 332 |
if str, ok := objects[i].(cfString); ok { |
| 333 |
keys[i] = string(str) |
| 334 |
} else { |
| 335 |
panic(fmt.Errorf("dictionary@0x%x contains non-string key at index %d", off, i)) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
return &cfDictionary{ |
| 340 |
keys: keys, |
| 341 |
values: objects[cnt:], |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
func (p *bplistParser) parseArrayAtOffset(off offset) *cfArray { |
| 346 |
p.pushNestedObject(off) |
| 347 |
defer p.popNestedObject() |
| 348 |
|
| 349 |
|
| 350 |
cnt, start := p.countForTagAtOffset(off) |
| 351 |
return &cfArray{p.parseObjectListAtOffset(start, cnt)} |
| 352 |
} |
| 353 |
|
| 354 |
func newBplistParser(r io.ReadSeeker) *bplistParser { |
| 355 |
return &bplistParser{reader: r} |
| 356 |
} |