-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
unarchiver.go
215 lines (191 loc) · 6.1 KB
/
unarchiver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package nskeyedarchiver
import (
"fmt"
log "github.com/sirupsen/logrus"
plist "howett.net/plist"
)
//Unarchive extracts NSKeyedArchiver Plists, either in XML or Binary format, and returns an array of the archived objects converted to usable Go Types.
// Primitives will be extracted just like regular Plist primitives (string, float64, int64, []uint8 etc.).
// NSArray, NSMutableArray, NSSet and NSMutableSet will transformed into []interface{}
// NSDictionary and NSMutableDictionary will be transformed into map[string] interface{}. I might add non string keys later.
func Unarchive(xml []byte) ([]interface{}, error) {
SetupDecoders()
plist, err := plistFromBytes(xml)
if err != nil {
return nil, err
}
nsKeyedArchiverData := plist.(map[string]interface{})
err = verifyCorrectArchiver(nsKeyedArchiverData)
if err != nil {
return nil, err
}
return extractObjectsFromTop(nsKeyedArchiverData[topKey].(map[string]interface{}), nsKeyedArchiverData[objectsKey].([]interface{}))
}
func extractObjectsFromTop(top map[string]interface{}, objects []interface{}) ([]interface{}, error) {
objectCount := len(top)
if root, ok := top["root"]; ok {
return extractObjects([]plist.UID{root.(plist.UID)}, objects)
}
objectRefs := make([]plist.UID, objectCount)
//convert the Dictionary with the objectReferences into a flat list of UIDs, so we can reuse the extractObjects function later
for i := 0; i < objectCount; i++ {
objectIndex := top[fmt.Sprintf("$%d", i)].(plist.UID)
objectRefs[i] = objectIndex
}
return extractObjects(objectRefs, objects)
}
func extractObjects(objectRefs []plist.UID, objects []interface{}) ([]interface{}, error) {
objectCount := len(objectRefs)
returnValue := make([]interface{}, objectCount)
for i := 0; i < objectCount; i++ {
objectIndex := objectRefs[i]
objectRef := objects[objectIndex]
if object, ok := isPrimitiveObject(objectRef); ok {
returnValue[i] = object
continue
}
//if this crashes, I forgot a primitive type
nonPrimitiveObjectRef := objectRef.(map[string]interface{})
if object, ok := isArrayObject(nonPrimitiveObjectRef, objects); ok {
extractObjects, err := extractObjects(toUidList(object[nsObjects].([]interface{})), objects)
if err != nil {
return nil, err
}
returnValue[i] = extractObjects
continue
}
if object, ok := isDictionaryObject(nonPrimitiveObjectRef, objects); ok {
dictionary, err := extractDictionary(object, objects)
if err != nil {
return nil, err
}
returnValue[i] = dictionary
continue
}
if object, ok := isNSMutableDataObject(nonPrimitiveObjectRef, objects); ok {
returnValue[i] = object[nsDataKey]
continue
}
if object, ok := isNSMutableString(nonPrimitiveObjectRef, objects); ok {
returnValue[i] = object[nsStringKey]
continue
}
obj, err := decodeNonstandardObject(nonPrimitiveObjectRef, objects)
if err != nil {
return nil, err
}
returnValue[i] = obj
}
return returnValue, nil
}
func decodeNonstandardObject(object map[string]interface{}, objects []interface{}) (interface{}, error) {
className, err := resolveClass(object[class], objects)
if err != nil {
return nil, err
}
factory := decodableClasses[className]
if factory == nil {
return nil, fmt.Errorf("Unknown class:%s", className)
}
return factory(object, objects), nil
}
func isArrayObject(object map[string]interface{}, objects []interface{}) (map[string]interface{}, bool) {
className, err := resolveClass(object[class], objects)
if err != nil {
return nil, false
}
if className == nsArray || className == nsMutableArray || className == nsSet || className == nsMutableSet {
return object, true
}
return object, false
}
func isDictionaryObject(object map[string]interface{}, objects []interface{}) (map[string]interface{}, bool) {
className, err := resolveClass(object[class], objects)
if err != nil {
return nil, false
}
if className == nsDictionary || className == nsMutableDictionary {
return object, true
}
return object, false
}
func isNSMutableDataObject(object map[string]interface{}, objects []interface{}) (map[string]interface{}, bool) {
className, err := resolveClass(object[class], objects)
if err != nil {
return nil, false
}
if className == nsMutableData {
return object, true
}
return object, false
}
func isNSMutableString(object map[string]interface{}, objects []interface{}) (map[string]interface{}, bool) {
className, err := resolveClass(object[class], objects)
if err != nil {
return nil, false
}
if className == nsMutableString {
return object, true
}
return object, false
}
func extractDictionary(object map[string]interface{}, objects []interface{}) (map[string]interface{}, error) {
keyRefs := toUidList(object[nsKeys].([]interface{}))
keys, err := extractObjects(keyRefs, objects)
if err != nil {
return nil, err
}
valueRefs := toUidList(object[nsObjects].([]interface{}))
values, err := extractObjects(valueRefs, objects)
if err != nil {
return nil, err
}
mapSize := len(keys)
result := make(map[string]interface{}, mapSize)
if mapSize == 0 {
return result, nil
}
if _, ok := keys[0].(string); !ok {
log.Warn("non string key dict found, lazy decoding by converting keys to strings :-), fix later")
for i := 0; i < mapSize; i++ {
key := keys[i].(uint64)
result[fmt.Sprintf("uint64{%d}", key)] = values[i]
}
return result, nil
}
for i := 0; i < mapSize; i++ {
result[keys[i].(string)] = values[i]
}
return result, nil
}
func resolveClass(classInfo interface{}, objects []interface{}) (string, error) {
if v, ok := classInfo.(plist.UID); ok {
classDict := objects[v].(map[string]interface{})
return classDict[className].(string), nil
}
return "", fmt.Errorf("Could not find class for %s", classInfo)
}
func isPrimitiveObject(object interface{}) (interface{}, bool) {
if v, ok := object.(int32); ok {
return v, ok
}
if v, ok := object.(int); ok {
return v, ok
}
if v, ok := object.(uint64); ok {
return v, ok
}
if v, ok := object.(float64); ok {
return v, ok
}
if v, ok := object.(bool); ok {
return v, ok
}
if v, ok := object.(string); ok {
return v, ok
}
if v, ok := object.([]uint8); ok {
return v, ok
}
return object, false
}