summaryrefslogtreecommitdiff
path: root/python/typelibrary.py
blob: 8a34092853a0884a9e0d7e0a809d43f5889163d3 (plain)
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
# Copyright (c) 2015-2019 Vector 35 Inc
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import struct
import traceback
import ctypes
import abc
import numbers

# Binary Ninja components
from binaryninja import _binaryninjacore as core
import binaryninja
from binaryninja import log
from binaryninja import types
from binaryninja import metadata
from binaryninja import platform
from binaryninja import architecture

# 2-3 compatibility
from binaryninja import range
from binaryninja import with_metaclass

class TypeLibrary(object):
    def __init__(self, handle):
        self.handle = core.handle_of_type(handle, core.BNTypeLibrary)

    def __del__(self):
        core.BNFreeTypeLibrary(self.handle)

    def __repr__(self):
        return "<typelib '{}':{}>".format(self.name, self.arch.name)

    @classmethod
    def new(cls, arch, name):
        handle = core.BNNewTypeLibrary(arch.handle, name)
        return TypeLibrary(handle)

    @classmethod
    def load_from_file(cls, path):
        handle = core.BNLoadTypeLibraryFromFile(path)
        if handle is None:
            return None
        return TypeLibrary(handle)

    def write_to_file(self, path):
        core.BNWriteTypeLibraryToFile(self.handle, path)

    @classmethod
    def from_name(cls, arch, name):
        handle = core.BNLookupTypeLibraryByName(arch.handle, name)
        if handle is None:
            return None
        return TypeLibrary(handle)

    @classmethod
    def from_guid(cls, arch, guid):
        handle = core.BNLookupTypeLibraryByGuid(arch.handle, guid)
        if handle is None:
            return None
        return TypeLibrary(handle)

    @property
    def arch(self):
        arch = core.BNGetTypeLibraryArchitecture(self.handle)
        if arch is None:
            return None
        return binaryninja.architecture.CoreArchitecture._from_cache(handle=arch)

    @property
    def name(self):
        name = core.BNGetTypeLibraryName(self.handle)
        return name

    @name.setter
    def name(self, value):
        core.BNSetTypeLibraryName(self.handle, value)

    @property
    def dependency_name(self):
        return core.BNGetTypeLibraryDependencyName(self.handle)

    @dependency_name.setter
    def dependency_name(self, value):
        core.BNSetTypeLibraryDependencyName(self.handle, value)

    @property
    def guid(self):
        return core.BNGetTypeLibraryGuid(self.handle)

    @guid.setter
    def guid(self, value):
        core.BNSetTypeLibraryGuid(self.handle, value)

    @property
    def alternate_names(self):
        count = ctypes.c_ulonglong(0)
        result = []
        names = core.BNGetTypeLibraryAlternateNames(self.handle, count)
        for i in range(0, count.value):
            result.append(names[i])
        core.BNFreeStringList(names, count.value)
        return result

    def add_alternate_name(self, name):
        core.BNAddTypeLibraryAlternateName(self.handle, name)

    @property
    def platform_names(self):
        count = ctypes.c_ulonglong(0)
        result = []
        platforms = core.BNGetTypeLibraryPlatforms(self.handle, count)
        for i in range(0, count.value):
            result.append(platforms[i])
        core.BNFreeStringList(platforms, count.value)
        return result

    def add_platform(self, plat):
        if not isinstance(plat, platform.Platform):
            raise ValueError("plat must be a Platform object")
        core.BNAddTypeLibraryPlatform(self.handle, plat.handle)

    def clear_platforms(self):
        core.BNClearTypeLibraryPlatforms(self.handle)

    def finalize(self):
        core.BNFinalizeTypeLibrary(self.handle)

    def query_metadata(self, key):
        md_handle = core.BNTypeLibraryQueryMetadata(self.handle, key)
        if md_handle is None:
            return None
        return metadata.Metadata(handle=md_handle).value

    def store_metadata(self, key, md):
        if not isinstance(md, metadata.Metadata):
            md = metadata.Metadata(md)
        core.BNTypeLibraryStoreMetadata(self.handle, key, md.handle)

    def remove_metadata(self, key):
        core.BNTypeLibraryRemoveMetadata(self.handle, key)

    def add_named_object(self, name, t):
        if not isinstance(name, types.QualifiedName):
            name = types.QualifiedName(name)
        if not isinstance(t, types.Type):
            raise ValueError("t must be a Type")
        core.BNAddTypeLibraryNamedObject(self.handle, name._get_core_struct(), t.handle)

    def add_named_type(self, name, t):
        if not isinstance(name, types.QualifiedName):
            name = types.QualifiedName(name)
        if not isinstance(t, types.Type):
            raise ValueError("t must be a Type")
        core.BNAddTypeLibraryNamedType(self.handle, name._get_core_struct(), t.handle)

    def get_named_object(self, name):
        if not isinstance(name, types.QualifiedName):
            name = types.QualifiedName(name)
        t = core.BNGetTypeLibraryNamedObject(self.handle, name._get_core_struct())
        if t is None:
            return None
        return types.Type(t)

    def get_named_type(self, name):
        if not isinstance(name, types.QualifiedName):
            name = types.QualifiedName(name)
        t = core.BNGetTypeLibraryNamedType(self.handle, name._get_core_struct())
        if t is None:
            return None
        return types.Type(t)

    @property
    def named_objects(self):
        count = ctypes.c_ulonglong(0)
        result = {}
        named_types = core.BNGetTypeLibraryNamedObjects(self.handle, count)
        for i in range(0, count.value):
            name = types.QualifiedName._from_core_struct(named_types[i].name)
            result[name] = types.Type(core.BNNewTypeReference(named_types[i].type))
        core.BNFreeQualifiedNameAndTypeArray(named_types, count.value)
        return result

    @property
    def named_types(self):
        count = ctypes.c_ulonglong(0)
        result = {}
        named_types = core.BNGetTypeLibraryNamedTypes(self.handle, count)
        for i in range(0, count.value):
            name = types.QualifiedName._from_core_struct(named_types[i].name)
            result[name] = types.Type(core.BNNewTypeReference(named_types[i].type))
        core.BNFreeQualifiedNameAndTypeArray(named_types, count.value)
        return result