This repository has been archived by the owner on Mar 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
unarchiver.go
146 lines (129 loc) · 4.36 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
package nskeyedarchiver
import (
"fmt"
"reflect"
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) {
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 object, ok := isArrayObject(objectRef.(map[string]interface{}), objects); ok {
extractObjects, err := extractObjects(toUidList(object[nsObjects].([]interface{})), objects)
if err != nil {
return nil, err
}
returnValue[i] = extractObjects
continue
}
if object, ok := isDictionaryObject(objectRef.(map[string]interface{}), objects); ok {
dictionary, err := extractDictionary(object, objects)
if err != nil {
return nil, err
}
returnValue[i] = dictionary
continue
}
objectType := reflect.TypeOf(objectRef).String()
return nil, fmt.Errorf("Unknown object type:%s", objectType)
}
return returnValue, 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 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)
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.(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
}