-
Notifications
You must be signed in to change notification settings - Fork 0
/
interoplib.c
282 lines (226 loc) · 13.1 KB
/
interoplib.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "interoplib.h"
#include <stdarg.h>
/*********************************************************************/
#define NOTIFICATIONCENTER_GLOBALSENDER ("Global")
/*********************************************************************/
#define BASE16_LOWERCHARS ("0123456789abcdef")
#define BASE16_UPPERCHARS ("0123456789ABCDEF")
/*********************************************************************/
bool String_ConvertToHex(const uint8_t *Binary, int32_t BinarySize, bool Lower, char *Hex, int32_t MaxHex) {
char *Base16Symbols = NULL;
if (Lower == true)
Base16Symbols = BASE16_LOWERCHARS;
else
Base16Symbols = BASE16_UPPERCHARS;
while ((MaxHex > 2) && (BinarySize > 0)) {
Hex[0] = Base16Symbols[*Binary >> 4];
Hex[1] = Base16Symbols[*Binary & 0xF];
Hex += 2;
MaxHex -= 2;
Binary += 1;
BinarySize -= 1;
}
if (MaxHex > 1 && BinarySize > 0)
*Hex++ = Base16Symbols[*Binary >> 4];
if (MaxHex == 0)
return false;
*Hex = 0;
return BinarySize == 0;
}
/*********************************************************************/
static const uint8_t Base64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*********************************************************************/
bool Base64_Encode(const uint8_t *Source, int32_t SourceLength, char *Target, int32_t MaxTarget,
int32_t *TargetLength) {
int32_t i = 0, n = 0;
uint8_t b1, b2, b3, b4, b5, b6, b7;
*TargetLength = 0;
for (i = 0; i < SourceLength && n + 2 <= MaxTarget; i += 3) {
b1 = Source[i], b2 = 0, b3 = 0;
if ((i + 1) < SourceLength)
b2 = Source[i + 1];
if ((i + 2) < SourceLength)
b3 = Source[i + 2];
b4 = b1 >> 2;
b5 = ((b1 & 0x3) << 4) | (b2 >> 4);
b6 = ((b2 & 0xf) << 2) | (b3 >> 6);
b7 = b3 & 0x3f;
Target[n++] = Base64Alphabet[b4];
Target[n++] = Base64Alphabet[b5];
if ((i + 1) < SourceLength) {
if (n >= MaxTarget)
break;
Target[n++] = Base64Alphabet[b6];
} else if (n < MaxTarget)
Target[n++] = '=';
if ((i + 2) < SourceLength) {
if (n >= MaxTarget)
break;
Target[n++] = Base64Alphabet[b7];
} else if (n < MaxTarget)
Target[n++] = '=';
}
*TargetLength = n;
if (n < MaxTarget)
Target[n] = 0;
if (i < SourceLength)
return false;
return true;
}
int32_t Base64_CalculateEncodeSize(int32_t source_length) {
// Round up to the nearest multiple of 4 and include NULL terminator
return ((source_length + 2) / 3 * 4) + 1;
}
/*********************************************************************/
typedef void *(*Class_ConvertFromInstanceIdCallback)(const char *InstanceId);
typedef char *(*Class_ConvertToInstanceIdCallback)(void *Pointer);
typedef bool (*Class_TrackInstanceCallback)(void *Pointer, const char *InstanceId);
typedef bool (*Class_UntrackInstanceCallback)(void *Pointer);
typedef echandle (*Dictionary_CreateCallback)(void);
typedef int32_t (*Dictionary_ReleaseCallback)(echandle *DictionaryHandle);
typedef bool (*NotificationCenter_AddInstanceObserverCallback)(const char *Type, const char *Notification,
const char *Sender, void *UserPtr,
NotificationCenter_ObserverCallback Callback);
typedef bool (*NotificationCenter_RemoveInstanceObserverCallback)(const char *Type, const char *Notification,
const char *Sender, void *UserPtr,
NotificationCenter_ObserverCallback Callback);
typedef bool (*NotificationCenter_FireCallback)(const char *Type, const char *Notification, const char *Sender,
echandle DictionaryHandle);
typedef bool (*NotificationCenter_FireWithJSONCallback)(const char *Type, const char *Notification, const char *Sender,
const char *Format, ...);
typedef bool (*NotificationCenter_FireWithJSONVCallback)(const char *Type, const char *Notification, const char *Sender,
const char *Format, va_list ArgumentList);
typedef bool (*NotificationCenter_FireAfterDelayCallback)(const char *Type, const char *Notification,
const char *Sender, int32_t DelayMS,
echandle DictionaryHandle);
typedef bool (*NotificationCenter_FireAfterDelayWithJSONCallback)(const char *Type, const char *Notification,
const char *Sender, int32_t DelayMS,
const char *Format, ...);
typedef bool (*NotificationCenter_FireAfterDelayWithJSONVCallback)(const char *Type, const char *Notification,
const char *Sender, int32_t DelayMS,
const char *Format, va_list ArgumentList);
typedef bool (*Interop_GenerateInstanceIdCallback)(char *String, int32_t MaxString);
typedef struct GlobalInteropLibStruct {
Class_ConvertFromInstanceIdCallback Class_ConvertFromInstanceId;
Class_ConvertToInstanceIdCallback Class_ConvertToInstanceId;
Class_TrackInstanceCallback Class_TrackInstance;
Class_UntrackInstanceCallback Class_UntrackInstance;
Dictionary_CreateCallback Dictionary_Create;
Dictionary_ReleaseCallback Dictionary_Release;
NotificationCenter_AddInstanceObserverCallback NotificationCenter_AddInstanceObserver;
NotificationCenter_RemoveInstanceObserverCallback NotificationCenter_RemoveInstanceObserver;
NotificationCenter_FireCallback NotificationCenter_Fire;
NotificationCenter_FireWithJSONCallback NotificationCenter_FireWithJSON;
NotificationCenter_FireWithJSONVCallback NotificationCenter_FireWithJSONV;
NotificationCenter_FireAfterDelayCallback NotificationCenter_FireAfterDelay;
NotificationCenter_FireAfterDelayWithJSONCallback NotificationCenter_FireAfterDelayWithJSON;
NotificationCenter_FireAfterDelayWithJSONVCallback NotificationCenter_FireAfterDelayWithJSONV;
Interop_GenerateInstanceIdCallback Interop_GenerateInstanceId;
} GlobalInteropLibStruct;
GlobalInteropLibStruct GlobalInteropLib;
/*********************************************************************/
void *Class_ConvertFromInstanceId(const char *InstanceId) {
return GlobalInteropLib.Class_ConvertFromInstanceId(InstanceId);
}
char *Class_ConvertToInstanceId(void *Pointer) {
return GlobalInteropLib.Class_ConvertToInstanceId(Pointer);
}
bool Class_TrackInstance(void *Pointer, const char *InstanceId) {
return GlobalInteropLib.Class_TrackInstance(Pointer, InstanceId);
}
bool Class_UntrackInstance(void *Pointer) {
return GlobalInteropLib.Class_UntrackInstance(Pointer);
}
echandle Dictionary_Create(void) {
return GlobalInteropLib.Dictionary_Create();
}
int32_t Dictionary_Release(echandle *DictionaryHandle) {
return GlobalInteropLib.Dictionary_Release(DictionaryHandle);
}
bool Interop_GenerateInstanceId(char *String, int32_t MaxString);
bool NotificationCenter_AddInstanceObserver(const char *Type, const char *Notification, const char *Sender,
void *UserPtr, NotificationCenter_ObserverCallback Callback) {
return GlobalInteropLib.NotificationCenter_AddInstanceObserver(Type, Notification, Sender, UserPtr, Callback);
}
bool NotificationCenter_AddObserver(const char *Type, const char *Notification, void *UserPtr,
NotificationCenter_ObserverCallback Callback) {
return NotificationCenter_AddInstanceObserver(Type, Notification, NOTIFICATIONCENTER_GLOBALSENDER, UserPtr,
Callback);
}
bool NotificationCenter_RemoveInstanceObserver(const char *Type, const char *Notification, const char *Sender,
void *UserPtr, NotificationCenter_ObserverCallback Callback) {
return GlobalInteropLib.NotificationCenter_RemoveInstanceObserver(Type, Notification, Sender, UserPtr, Callback);
}
bool NotificationCenter_RemoveObserver(const char *Type, const char *Notification, void *UserPtr,
NotificationCenter_ObserverCallback Callback) {
return NotificationCenter_RemoveInstanceObserver(Type, Notification, NOTIFICATIONCENTER_GLOBALSENDER, UserPtr,
Callback);
}
bool NotificationCenter_Fire(const char *Type, const char *Notification, const char *Sender,
echandle DictionaryHandle) {
return GlobalInteropLib.NotificationCenter_Fire(Type, Notification, Sender, DictionaryHandle);
}
bool NotificationCenter_FireWithJSON(const char *Type, const char *Notification, const char *Sender, const char *Format,
...) {
va_list ArgumentList;
bool RetVal = false;
va_start(ArgumentList, Format);
RetVal = GlobalInteropLib.NotificationCenter_FireWithJSONV(Type, Notification, Sender, Format, ArgumentList);
va_end(ArgumentList);
return RetVal;
}
bool NotificationCenter_FireAfterDelay(const char *Type, const char *Notification, const char *Sender, int32_t DelayMS,
echandle DictionaryHandle) {
return GlobalInteropLib.NotificationCenter_FireAfterDelay(Type, Notification, Sender, DelayMS, DictionaryHandle);
}
bool NotificationCenter_FireAfterDelayWithJSON(const char *Type, const char *Notification, const char *Sender,
int32_t DelayMS, const char *Format, ...) {
va_list ArgumentList;
bool RetVal = false;
va_start(ArgumentList, Format);
RetVal = GlobalInteropLib.NotificationCenter_FireAfterDelayWithJSONV(Type, Notification, Sender, DelayMS, Format,
ArgumentList);
va_end(ArgumentList);
return RetVal;
}
bool Interop_GenerateInstanceId(char *String, int32_t MaxString) {
return GlobalInteropLib.Interop_GenerateInstanceId(String, MaxString);
}
/*********************************************************************/
bool Interop_SetOverride(const char *Key, void *Value) {
if (strcmp(Key, "Class_ConvertFromInstanceId") == 0)
GlobalInteropLib.Class_ConvertFromInstanceId = (Class_ConvertFromInstanceIdCallback)Value;
else if (strcmp(Key, "Class_ConvertToInstanceId") == 0)
GlobalInteropLib.Class_ConvertToInstanceId = (Class_ConvertToInstanceIdCallback)Value;
else if (strcmp(Key, "Class_TrackInstance") == 0)
GlobalInteropLib.Class_TrackInstance = (Class_TrackInstanceCallback)Value;
else if (strcmp(Key, "Class_UntrackInstance") == 0)
GlobalInteropLib.Class_UntrackInstance = (Class_UntrackInstanceCallback)Value;
else if (strcmp(Key, "Dictionary_Create") == 0)
GlobalInteropLib.Dictionary_Create = (Dictionary_CreateCallback)Value;
else if (strcmp(Key, "Dictionary_Release") == 0)
GlobalInteropLib.Dictionary_Release = (Dictionary_ReleaseCallback)Value;
else if (strcmp(Key, "NotificationCenter_AddInstanceObserver") == 0)
GlobalInteropLib.NotificationCenter_AddInstanceObserver = (NotificationCenter_AddInstanceObserverCallback)Value;
else if (strcmp(Key, "NotificationCenter_RemoveInstanceObserver") == 0)
GlobalInteropLib.NotificationCenter_RemoveInstanceObserver =
(NotificationCenter_RemoveInstanceObserverCallback)Value;
else if (strcmp(Key, "NotificationCenter_Fire") == 0)
GlobalInteropLib.NotificationCenter_Fire = (NotificationCenter_FireCallback)Value;
else if (strcmp(Key, "NotificationCenter_FireWithJSON") == 0)
GlobalInteropLib.NotificationCenter_FireWithJSON = (NotificationCenter_FireWithJSONCallback)Value;
else if (strcmp(Key, "NotificationCenter_FireWithJSONV") == 0)
GlobalInteropLib.NotificationCenter_FireWithJSONV = (NotificationCenter_FireWithJSONVCallback)Value;
else if (strcmp(Key, "NotificationCenter_FireAfterDelay") == 0)
GlobalInteropLib.NotificationCenter_FireAfterDelay = (NotificationCenter_FireAfterDelayCallback)Value;
else if (strcmp(Key, "NotificationCenter_FireAfterDelayWithJSON") == 0)
GlobalInteropLib.NotificationCenter_FireAfterDelayWithJSON =
(NotificationCenter_FireAfterDelayWithJSONCallback)Value;
else if (strcmp(Key, "NotificationCenter_FireAfterDelayWithJSONV") == 0)
GlobalInteropLib.NotificationCenter_FireAfterDelayWithJSONV =
(NotificationCenter_FireAfterDelayWithJSONVCallback)Value;
else if (strcmp(Key, "Interop_GenerateInstanceId") == 0)
GlobalInteropLib.Interop_GenerateInstanceId = (Interop_GenerateInstanceIdCallback)Value;
return true;
}
/*********************************************************************/