Debian Codegraph @

bplist_generator.go

raw symbols SCIP
1 package plist
2
3 import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7 "io"
8 "time"
9 "unicode/utf16"
10 )
11
12 func bplistMinimumIntSize(n uint64) int {
13 switch {
14 case n <= uint64(0xff):
15 return 1
16 case n <= uint64(0xffff):
17 return 2
18 case n <= uint64(0xffffffff):
19 return 4
20 default:
21 return 8
22 }
23 }
24
25 func bplistValueShouldUnique(pval cfValue) bool {
26 switch pval.(type) {
27 case cfString, *cfNumber, *cfReal, cfDate, cfData:
28 return true
29 }
30 return false
31 }
32
33 type bplistGenerator struct {
34 writer *countedWriter
35 objmap map[interface{}]uint64 // maps pValue.hash()es to object locations
36 objtable []cfValue
37 trailer bplistTrailer
38 }
39
40 func (p *bplistGenerator) flattenPlistValue(pval cfValue) {
41 key := pval.hash()
42 if bplistValueShouldUnique(pval) {
43 if _, ok := p.objmap[key]; ok {
44 return
45 }
46 }
47
48 p.objmap[key] = uint64(len(p.objtable))
49 p.objtable = append(p.objtable, pval)
50
51 switch pval := pval.(type) {
52 case *cfDictionary:
53 pval.sort()
54 for _, k := range pval.keys {
55 p.flattenPlistValue(cfString(k))
56 }
57 for _, v := range pval.values {
58 p.flattenPlistValue(v)
59 }
60 case *cfArray:
61 for _, v := range pval.values {
62 p.flattenPlistValue(v)
63 }
64 }
65 }
66
67 func (p *bplistGenerator) indexForPlistValue(pval cfValue) (uint64, bool) {
68 v, ok := p.objmap[pval.hash()]
69 return v, ok
70 }
71
72 func (p *bplistGenerator) generateDocument(root cfValue) {
73 p.objtable = make([]cfValue, 0, 16)
74 p.objmap = make(map[interface{}]uint64)
75 p.flattenPlistValue(root)
76
77 p.trailer.NumObjects = uint64(len(p.objtable))
78 p.trailer.ObjectRefSize = uint8(bplistMinimumIntSize(p.trailer.NumObjects))
79
80 p.writer.Write([]byte("bplist00"))
81
82 offtable := make([]uint64, p.trailer.NumObjects)
83 for i, pval := range p.objtable {
84 offtable[i] = uint64(p.writer.BytesWritten())
85 p.writePlistValue(pval)
86 }
87
88 p.trailer.OffsetIntSize = uint8(bplistMinimumIntSize(uint64(p.writer.BytesWritten())))
89 p.trailer.TopObject = p.objmap[root.hash()]
90 p.trailer.OffsetTableOffset = uint64(p.writer.BytesWritten())
91
92 for _, offset := range offtable {
93 p.writeSizedInt(offset, int(p.trailer.OffsetIntSize))
94 }
95
96 binary.Write(p.writer, binary.BigEndian, p.trailer)
97 }
98
99 func (p *bplistGenerator) writePlistValue(pval cfValue) {
100 if pval == nil {
101 return
102 }
103
104 switch pval := pval.(type) {
105 case *cfDictionary:
106 p.writeDictionaryTag(pval)
107 case *cfArray:
108 p.writeArrayTag(pval.values)
109 case cfString:
110 p.writeStringTag(string(pval))
111 case *cfNumber:
112 p.writeIntTag(pval.signed, pval.value)
113 case *cfReal:
114 if pval.wide {
115 p.writeRealTag(pval.value, 64)
116 } else {
117 p.writeRealTag(pval.value, 32)
118 }
119 case cfBoolean:
120 p.writeBoolTag(bool(pval))
121 case cfData:
122 p.writeDataTag([]byte(pval))
123 case cfDate:
124 p.writeDateTag(time.Time(pval))
125 case cfUID:
126 p.writeUIDTag(UID(pval))
127 default:
128 panic(fmt.Errorf("unknown plist type %t", pval))
129 }
130 }
131
132 func (p *bplistGenerator) writeSizedInt(n uint64, nbytes int) {
133 var val interface{}
134 switch nbytes {
135 case 1:
136 val = uint8(n)
137 case 2:
138 val = uint16(n)
139 case 4:
140 val = uint32(n)
141 case 8:
142 val = n
143 default:
144 panic(errors.New("illegal integer size"))
145 }
146 binary.Write(p.writer, binary.BigEndian, val)
147 }
148
149 func (p *bplistGenerator) writeBoolTag(v bool) {
150 tag := uint8(bpTagBoolFalse)
151 if v {
152 tag = bpTagBoolTrue
153 }
154 binary.Write(p.writer, binary.BigEndian, tag)
155 }
156
157 func (p *bplistGenerator) writeIntTag(signed bool, n uint64) {
158 var tag uint8
159 var val interface{}
160 switch {
161 case n <= uint64(0xff):
162 val = uint8(n)
163 tag = bpTagInteger | 0x0
164 case n <= uint64(0xffff):
165 val = uint16(n)
166 tag = bpTagInteger | 0x1
167 case n <= uint64(0xffffffff):
168 val = uint32(n)
169 tag = bpTagInteger | 0x2
170 case n > uint64(0x7fffffffffffffff) && !signed:
171 // 64-bit values are always *signed* in format 00.
172 // Any unsigned value that doesn't intersect with the signed
173 // range must be sign-extended and stored as a SInt128
174 val = n
175 tag = bpTagInteger | 0x4
176 default:
177 val = n
178 tag = bpTagInteger | 0x3
179 }
180
181 binary.Write(p.writer, binary.BigEndian, tag)
182 if tag&0xF == 0x4 {
183 // SInt128; in the absence of true 128-bit integers in Go,
184 // we'll just fake the top half. We only got here because
185 // we had an unsigned 64-bit int that didn't fit,
186 // so sign extend it with zeroes.
187 binary.Write(p.writer, binary.BigEndian, uint64(0))
188 }
189 binary.Write(p.writer, binary.BigEndian, val)
190 }
191
192 func (p *bplistGenerator) writeUIDTag(u UID) {
193 nbytes := bplistMinimumIntSize(uint64(u))
194 tag := uint8(bpTagUID | (nbytes - 1))
195
196 binary.Write(p.writer, binary.BigEndian, tag)
197 p.writeSizedInt(uint64(u), nbytes)
198 }
199
200 func (p *bplistGenerator) writeRealTag(n float64, bits int) {
201 var tag uint8 = bpTagReal | 0x3
202 var val interface{} = n
203 if bits == 32 {
204 val = float32(n)
205 tag = bpTagReal | 0x2
206 }
207
208 binary.Write(p.writer, binary.BigEndian, tag)
209 binary.Write(p.writer, binary.BigEndian, val)
210 }
211
212 func (p *bplistGenerator) writeDateTag(t time.Time) {
213 tag := uint8(bpTagDate) | 0x3
214 val := float64(t.In(time.UTC).UnixNano()) / float64(time.Second)
215 val -= 978307200 // Adjust to Apple Epoch
216
217 binary.Write(p.writer, binary.BigEndian, tag)
218 binary.Write(p.writer, binary.BigEndian, val)
219 }
220
221 func (p *bplistGenerator) writeCountedTag(tag uint8, count uint64) {
222 marker := tag
223 if count >= 0xF {
224 marker |= 0xF
225 } else {
226 marker |= uint8(count)
227 }
228
229 binary.Write(p.writer, binary.BigEndian, marker)
230
231 if count >= 0xF {
232 p.writeIntTag(false, count)
233 }
234 }
235
236 func (p *bplistGenerator) writeDataTag(data []byte) {
237 p.writeCountedTag(bpTagData, uint64(len(data)))
238 binary.Write(p.writer, binary.BigEndian, data)
239 }
240
241 func (p *bplistGenerator) writeStringTag(str string) {
242 for _, r := range str {
243 if r > 0x7F {
244 utf16Runes := utf16.Encode([]rune(str))
245 p.writeCountedTag(bpTagUTF16String, uint64(len(utf16Runes)))
246 binary.Write(p.writer, binary.BigEndian, utf16Runes)
247 return
248 }
249 }
250
251 p.writeCountedTag(bpTagASCIIString, uint64(len(str)))
252 binary.Write(p.writer, binary.BigEndian, []byte(str))
253 }
254
255 func (p *bplistGenerator) writeDictionaryTag(dict *cfDictionary) {
256 // assumption: sorted already; flattenPlistValue did this.
257 cnt := len(dict.keys)
258 p.writeCountedTag(bpTagDictionary, uint64(cnt))
259 vals := make([]uint64, cnt*2)
260 for i, k := range dict.keys {
261 // invariant: keys have already been "uniqued" (as PStrings)
262 keyIdx, ok := p.objmap[cfString(k).hash()]
263 if !ok {
264 panic(errors.New("failed to find key " + k + " in object map during serialization"))
265 }
266 vals[i] = keyIdx
267 }
268
269 for i, v := range dict.values {
270 // invariant: values have already been "uniqued"
271 objIdx, ok := p.indexForPlistValue(v)
272 if !ok {
273 panic(errors.New("failed to find value in object map during serialization"))
274 }
275 vals[i+cnt] = objIdx
276 }
277
278 for _, v := range vals {
279 p.writeSizedInt(v, int(p.trailer.ObjectRefSize))
280 }
281 }
282
283 func (p *bplistGenerator) writeArrayTag(arr []cfValue) {
284 p.writeCountedTag(bpTagArray, uint64(len(arr)))
285 for _, v := range arr {
286 objIdx, ok := p.indexForPlistValue(v)
287 if !ok {
288 panic(errors.New("failed to find value in object map during serialization"))
289 }
290
291 p.writeSizedInt(objIdx, int(p.trailer.ObjectRefSize))
292 }
293 }
294
295 func (p *bplistGenerator) Indent(i string) {
296 // There's nothing to indent.
297 }
298
299 func newBplistGenerator(w io.Writer) *bplistGenerator {
300 return &bplistGenerator{
301 writer: &countedWriter{Writer: mustWriter{w}},
302 }
303 }