| 1 | package plist |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "io" |
| 6 | "reflect" |
| 7 | "runtime" |
| 8 | ) |
| 9 | |
| 10 | type parser interface { |
| 11 | parseDocument() (cfValue, error) |
| 12 | } |
| 13 | |
| 14 | // A Decoder reads a property list from an input stream. |
| 15 | type Decoder struct { |
| 16 | // the format of the most-recently-decoded property list |
| 17 | Format int |
| 18 | |
| 19 | reader io.ReadSeeker |
| 20 | lax bool |
| 21 | } |
| 22 | |
| 23 | // Decode works like Unmarshal, except it reads the decoder stream to find property list elements. |
| 24 | // |
| 25 | // After Decoding, the Decoder's Format field will be set to one of the plist format constants. |
| 26 | func (p *Decoder) Decode(v interface{}) (err error) { |
| 27 | defer func() { |
| 28 | if r := recover(); r != nil { |
| 29 | if _, ok := r.(runtime.Error); ok { |
| 30 | panic(r) |
| 31 | } |
| 32 | err = r.(error) |
| 33 | } |
| 34 | }() |
| 35 | |
| 36 | header := make([]byte, 6) |
| 37 | p.reader.Read(header) |
| 38 | p.reader.Seek(0, 0) |
| 39 | |
| 40 | var parser parser |
| 41 | var pval cfValue |
| 42 | if bytes.Equal(header, []byte("bplist")) { |
| 43 | parser = newBplistParser(p.reader) |
| 44 | pval, err = parser.parseDocument() |
| 45 | if err != nil { |
| 46 | // Had a bplist header, but still got an error: we have to die here. |
| 47 | return err |
| 48 | } |
| 49 | p.Format = BinaryFormat |
| 50 | } else { |
| 51 | parser = newXMLPlistParser(p.reader) |
| 52 | pval, err = parser.parseDocument() |
| 53 | if _, ok := err.(invalidPlistError); ok { |
| 54 | // Rewind: the XML parser might have exhausted the file. |
| 55 | p.reader.Seek(0, 0) |
| 56 | // We don't use parser here because we want the textPlistParser type |
| 57 | tp := newTextPlistParser(p.reader) |
| 58 | pval, err = tp.parseDocument() |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | p.Format = tp.format |
| 63 | if p.Format == OpenStepFormat { |
| 64 | // OpenStep property lists can only store strings, |
| 65 | // so we have to turn on lax mode here for the unmarshal step later. |
| 66 | p.lax = true |
| 67 | } |
| 68 | } else { |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | p.Format = XMLFormat |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | p.unmarshal(pval, reflect.ValueOf(v)) |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | // NewDecoder returns a Decoder that reads property list elements from a stream reader, r. |
| 81 | // NewDecoder requires a Seekable stream for the purposes of file type detection. |
| 82 | func NewDecoder(r io.ReadSeeker) *Decoder { |
| 83 | return &Decoder{Format: InvalidFormat, reader: r, lax: false} |
| 84 | } |
| 85 | |
| 86 | // Unmarshal parses a property list document and stores the result in the value pointed to by v. |
| 87 | // |
| 88 | // Unmarshal uses the inverse of the type encodings that Marshal uses, allocating heap-borne types as necessary. |
| 89 | // |
| 90 | // When given a nil pointer, Unmarshal allocates a new value for it to point to. |
| 91 | // |
| 92 | // To decode property list values into an interface value, Unmarshal decodes the property list into the concrete value contained |
| 93 | // in the interface value. If the interface value is nil, Unmarshal stores one of the following in the interface value: |
| 94 | // |
| 95 | // string, bool, uint64, float64 |
| 96 | // plist.UID for "CoreFoundation Keyed Archiver UIDs" (convertible to uint64) |
| 97 | // []byte, for plist data |
| 98 | // []interface{}, for plist arrays |
| 99 | // map[string]interface{}, for plist dictionaries |
| 100 | // |
| 101 | // If a property list value is not appropriate for a given value type, Unmarshal aborts immediately and returns an error. |
| 102 | // |
| 103 | // As Go does not support 128-bit types, and we don't want to pretend we're giving the user integer types (as opposed to |
| 104 | // secretly passing them structs), Unmarshal will drop the high 64 bits of any 128-bit integers encoded in binary property lists. |
| 105 | // (This is important because CoreFoundation serializes some large 64-bit values as 128-bit values with an empty high half.) |
| 106 | // |
| 107 | // When Unmarshal encounters an OpenStep property list, it will enter a relaxed parsing mode: OpenStep property lists can only store |
| 108 | // plain old data as strings, so we will attempt to recover integer, floating-point, boolean and date values wherever they are necessary. |
| 109 | // (for example, if Unmarshal attempts to unmarshal an OpenStep property list into a time.Time, it will try to parse the string it |
| 110 | // receives as a time.) |
| 111 | // |
| 112 | // Unmarshal returns the detected property list format and an error, if any. |
| 113 | func Unmarshal(data []byte, v interface{}) (format int, err error) { |
| 114 | r := bytes.NewReader(data) |
| 115 | dec := NewDecoder(r) |
| 116 | err = dec.Decode(v) |
| 117 | format = dec.Format |
| 118 | return |
| 119 | } |