forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFUUID.c
503 lines (424 loc) · 17 KB
/
CFUUID.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
/* CFUUID.c
Copyright (c) 1999-2015, Apple Inc. All rights reserved.
Responsibility: David Smith
*/
#include <CoreFoundation/CFUUID.h>
#include "CFInternal.h"
#include "uuid/uuid.h"
#if __HAS_DISPATCH__
#include <dispatch/dispatch.h>
static CFMutableDictionaryRef _uniquedUUIDs = NULL;
CF_INLINE void LOCKED(dispatch_block_t work) {
static dispatch_once_t guard;
static dispatch_queue_t CFUUIDGlobalDataLock;
dispatch_once(&guard, ^{
dispatch_queue_attr_t dqattr = dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, qos_class_main(), 0);
CFUUIDGlobalDataLock = dispatch_queue_create("com.apple.CFUUID", dqattr);
});
dispatch_sync(CFUUIDGlobalDataLock, work);
}
#else
// Platforms without dispatch
static CFMutableDictionaryRef _uniquedUUIDs = NULL;
static CFLock_t _uniquedUUIDsLock = CFLockInit;
CF_INLINE void LOCKED(void (^work)(void)) {
__CFLock(&_uniquedUUIDsLock);
work();
__CFUnlock(&_uniquedUUIDsLock);
}
#endif
struct __CFUUID {
CFRuntimeBase _base;
CFUUIDBytes _bytes;
};
typedef struct __CFUUID __CFUUID_t;
static Boolean __CFisEqualUUIDBytes(const void *ptr1, const void *ptr2) {
CFUUIDBytes *p1 = (CFUUIDBytes *)ptr1;
CFUUIDBytes *p2 = (CFUUIDBytes *)ptr2;
return (((p1->byte0 == p2->byte0) && (p1->byte1 == p2->byte1) && (p1->byte2 == p2->byte2) && (p1->byte3 == p2->byte3) && (p1->byte4 == p2->byte4) && (p1->byte5 == p2->byte5) && (p1->byte6 == p2->byte6) && (p1->byte7 == p2->byte7) && (p1->byte8 == p2->byte8) && (p1->byte9 == p2->byte9) && (p1->byte10 == p2->byte10) && (p1->byte11 == p2->byte11) && (p1->byte12 == p2->byte12) && (p1->byte13 == p2->byte13) && (p1->byte14 == p2->byte14) && (p1->byte15 == p2->byte15)) ? true : false);
}
static CFHashCode __CFhashUUIDBytes(const void *ptr) {
return CFHashBytes((uint8_t *)ptr, 16);
}
/*
* GC implementation of a weak set specifically designed for UUID
*/
#define MALLOC(x) CFAllocatorAllocate(kCFAllocatorSystemDefault, x, 0)
#define FREE(x) CFAllocatorDeallocate(kCFAllocatorSystemDefault, x)
#define HASH(x) CFHashBytes((uint8_t *)x, 16)
#define READWEAK(location) objc_read_weak((id *)location)
#define WRITEWEAK(location, value) objc_assign_weak((id)value, (id *)location)
typedef struct {
unsigned long count, size;
__CFUUID_t **weakPtrs;
} _UUIDWeakSet_t;
static _UUIDWeakSet_t _UUIDWeakSet;
static void grow_has_lock(void);
// enter if not already present
static void enter_has_lock(__CFUUID_t *candidate) {
if (!candidate) return;
_UUIDWeakSet_t *table = &_UUIDWeakSet;
if (!table->size) grow_has_lock();
unsigned long int hashValue = HASH(&candidate->_bytes) & (table->size-1);
__CFUUID_t *result = table->weakPtrs[hashValue];
while (1) {
if (result == (void *)0x1 || result == NULL) {
table->weakPtrs[hashValue] = NULL; // so that we don't try to unregister 0x1
WRITEWEAK(&table->weakPtrs[hashValue], (void *)candidate);
++table->count;
break;
}
if (result) result = (__CFUUID_t *)READWEAK(&table->weakPtrs[hashValue]);
if (result) {
// see if it is equal to candidate
if (__CFisEqualUUIDBytes(&result->_bytes, &candidate->_bytes)) {
// keep first one. There is a race if two threads both fail to find
// a candidate uuid then both try decide to create and enter one.
// Under non-GC one of them simply leaks.
break;
}
} else {
// was zeroed by collector. Use this slot.
continue;
}
// move on
if (++hashValue >= table->size) hashValue = 0;
result = table->weakPtrs[hashValue];
}
}
static void *find_has_lock(const CFUUIDBytes *bytes) {
if (!bytes) return NULL;
_UUIDWeakSet_t *table = &_UUIDWeakSet;
if (!table->size) return NULL; // no entries
unsigned long int hashValue = HASH(bytes) & (table->size-1);
__CFUUID_t *result = table->weakPtrs[hashValue];
while (1) {
if (result == (void *)0x1) break;
if (result) result = (__CFUUID_t *)READWEAK(&table->weakPtrs[hashValue]);
if (result) {
// see if it is equal to bytes
if (__CFisEqualUUIDBytes(&result->_bytes, bytes)) return result;
}
// move on
if (++hashValue >= table->size) hashValue = 0;
result = table->weakPtrs[hashValue];
}
return NULL;
}
static void grow_has_lock() {
_UUIDWeakSet_t *table = &_UUIDWeakSet;
if (table->size == 0) {
table->size = 16;
table->weakPtrs = (__CFUUID_t **)MALLOC(sizeof(__CFUUID_t *)*table->size);
for (int i = 0; i < table->size; ++i) table->weakPtrs[i] = (__CFUUID_t *)0x1;
table->count = 0;
return;
}
table->count = 0;
table->size = table->size*2;
__CFUUID_t **oldPtrs = table->weakPtrs;
table->weakPtrs = (__CFUUID_t **)MALLOC(sizeof(__CFUUID_t *)*table->size);
for (int i = 0; i < table->size; ++i) table->weakPtrs[i] = (__CFUUID_t *)0x1;
for (int i = 0; i < table->size / 2; ++i) {
if (oldPtrs[i] == (__CFUUID_t *)0x1) continue; // available field, ignore
if (oldPtrs[i] == NULL) continue; // zero'ed by collector, ignore
enter_has_lock((__CFUUID_t *)READWEAK(&oldPtrs[i])); // read, then enter (but enter must check for NULL)
WRITEWEAK(&oldPtrs[i], NULL); // unregister
}
FREE(oldPtrs);
}
/***** end of weak set */
static void __CFUUIDAddUniqueUUIDHasLock(CFUUIDRef uuid) {
CFDictionaryKeyCallBacks __CFUUIDBytesDictionaryKeyCallBacks = {0, NULL, NULL, NULL, __CFisEqualUUIDBytes, __CFhashUUIDBytes};
CFDictionaryValueCallBacks __CFnonRetainedUUIDDictionaryValueCallBacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
if (kCFUseCollectableAllocator) {
enter_has_lock((__CFUUID_t *)uuid);
if (_UUIDWeakSet.count > (3 * _UUIDWeakSet.size / 4)) grow_has_lock();
} else {
if (!_uniquedUUIDs) _uniquedUUIDs = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &__CFUUIDBytesDictionaryKeyCallBacks, &__CFnonRetainedUUIDDictionaryValueCallBacks);
CFDictionarySetValue(_uniquedUUIDs, &(uuid->_bytes), uuid);
}
}
static void __CFUUIDRemoveUniqueUUIDHasLock(CFUUIDRef uuid) {
if (_uniquedUUIDs) CFDictionaryRemoveValue(_uniquedUUIDs, &(uuid->_bytes));
}
static CFUUIDRef __CFUUIDGetUniquedUUIDHasLock(const CFUUIDBytes *bytes) {
CFUUIDRef uuid = NULL;
if (kCFUseCollectableAllocator) {
uuid = (CFUUIDRef)find_has_lock(bytes);
} else if (_uniquedUUIDs) {
uuid = (CFUUIDRef)CFDictionaryGetValue(_uniquedUUIDs, bytes);
}
return uuid;
}
static void __CFUUIDDeallocate(CFTypeRef cf) {
if (kCFUseCollectableAllocator) return;
__CFUUID_t *uuid = (__CFUUID_t *)cf;
LOCKED(^{
__CFUUIDRemoveUniqueUUIDHasLock(uuid);
});
}
static CFStringRef __CFUUIDCopyDescription(CFTypeRef cf) {
CFStringRef uuidStr = CFUUIDCreateString(CFGetAllocator(cf), (CFUUIDRef)cf);
CFStringRef desc = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFUUID %p> %@"), cf, uuidStr);
CFRelease(uuidStr);
return desc;
}
static CFStringRef __CFUUIDCopyFormattingDescription(CFTypeRef cf, CFDictionaryRef formatOptions) {
return CFUUIDCreateString(CFGetAllocator(cf), (CFUUIDRef)cf);
}
static CFTypeID __kCFUUIDTypeID = _kCFRuntimeNotATypeID;
static const CFRuntimeClass __CFUUIDClass = {
0,
"CFUUID",
NULL, // init
NULL, // copy
__CFUUIDDeallocate,
NULL, // equal
NULL, // hash
__CFUUIDCopyFormattingDescription,
__CFUUIDCopyDescription
};
CFTypeID CFUUIDGetTypeID(void) {
static dispatch_once_t initOnce = 0;
dispatch_once(&initOnce, ^{ __kCFUUIDTypeID = _CFRuntimeRegisterClass(&__CFUUIDClass); });
return __kCFUUIDTypeID;
}
static CFUUIDRef __CFUUIDCreateWithBytesPrimitive(CFAllocatorRef allocator, CFUUIDBytes bytes, Boolean isConst) {
__block __CFUUID_t *uuid = NULL;
LOCKED(^{
uuid = (__CFUUID_t *)__CFUUIDGetUniquedUUIDHasLock(&bytes);
if (!uuid) {
size_t size;
size = sizeof(__CFUUID_t) - sizeof(CFRuntimeBase);
uuid = (__CFUUID_t *)_CFRuntimeCreateInstance(kCFUseCollectableAllocator ? kCFAllocatorSystemDefault : allocator, CFUUIDGetTypeID(), size, NULL);
if (!uuid) return;
uuid->_bytes = bytes;
__CFUUIDAddUniqueUUIDHasLock(uuid);
} else if (!isConst) {
CFRetain(uuid);
}
});
return (CFUUIDRef)uuid;
}
#if DEPLOYMENT_TARGET_WINDOWS
#include <Rpc.h>
#elif DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
#if DEPLOYMENT_RUNTIME_SWIFT
#include "uuid/uuid.h"
#else
#include <uuid/uuid.h>
#endif
#endif
CFUUIDRef CFUUIDCreate(CFAllocatorRef alloc) {
/* Create a new bytes struct and then call the primitive. */
__block CFUUIDBytes bytes;
__block uint32_t retval = 0;
LOCKED(^{
#if DEPLOYMENT_TARGET_WINDOWS
UUID u;
long rStatus = UuidCreate(&u);
if (RPC_S_OK != rStatus && RPC_S_UUID_LOCAL_ONLY != rStatus) retval = 1;
memmove(&bytes, &u, sizeof(bytes));
#elif DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
static Boolean useV1UUIDs = false, checked = false;
uuid_t uuid;
if (!checked) {
const char *value = __CFgetenv("CFUUIDVersionNumber");
if (value) {
if (1 == strtoul_l(value, NULL, 0, NULL)) useV1UUIDs = true;
}
checked = true;
}
if (useV1UUIDs) uuid_generate_time(uuid); else uuid_generate_random(uuid);
memcpy((void *)&bytes, uuid, sizeof(uuid));
#else
retval = 1;
#endif
});
return (retval == 0) ? __CFUUIDCreateWithBytesPrimitive(alloc, bytes, false) : NULL;
}
CFUUIDRef CFUUIDCreateWithBytes(CFAllocatorRef alloc, uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6, uint8_t byte7, uint8_t byte8, uint8_t byte9, uint8_t byte10, uint8_t byte11, uint8_t byte12, uint8_t byte13, uint8_t byte14, uint8_t byte15) {
CFUUIDBytes bytes;
// CodeWarrior can't handle the structure assignment of bytes, so we must explode this - REW, 10/8/99
bytes.byte0 = byte0;
bytes.byte1 = byte1;
bytes.byte2 = byte2;
bytes.byte3 = byte3;
bytes.byte4 = byte4;
bytes.byte5 = byte5;
bytes.byte6 = byte6;
bytes.byte7 = byte7;
bytes.byte8 = byte8;
bytes.byte9 = byte9;
bytes.byte10 = byte10;
bytes.byte11 = byte11;
bytes.byte12 = byte12;
bytes.byte13 = byte13;
bytes.byte14 = byte14;
bytes.byte15 = byte15;
return __CFUUIDCreateWithBytesPrimitive(alloc, bytes, false);
}
static void _intToHexChars(UInt32 in, UniChar *out, int digits) {
int shift;
UInt32 d;
while (--digits >= 0) {
shift = digits << 2;
d = 0x0FL & (in >> shift);
if (d <= 9) {
*out++ = (UniChar)'0' + d;
} else {
*out++ = (UniChar)'A' + (d - 10);
}
}
}
static uint8_t _byteFromHexChars(UniChar *in) {
uint8_t result = 0;
UniChar c;
uint8_t d;
CFIndex i;
for (i=0; i<2; i++) {
c = in[i];
if ((c >= (UniChar)'0') && (c <= (UniChar)'9')) {
d = c - (UniChar)'0';
} else if ((c >= (UniChar)'a') && (c <= (UniChar)'f')) {
d = c - ((UniChar)'a' - 10);
} else if ((c >= (UniChar)'A') && (c <= (UniChar)'F')) {
d = c - ((UniChar)'A' - 10);
} else {
return 0;
}
result = (result << 4) | d;
}
return result;
}
CF_INLINE Boolean _isHexChar(UniChar c) {
return ((((c >= (UniChar)'0') && (c <= (UniChar)'9')) || ((c >= (UniChar)'a') && (c <= (UniChar)'f')) || ((c >= (UniChar)'A') && (c <= (UniChar)'F'))) ? true : false);
}
#define READ_A_BYTE(into) if (i+1 < len) { \
(into) = _byteFromHexChars(&(chars[i])); \
i+=2; \
}
CFUUIDRef CFUUIDCreateFromString(CFAllocatorRef alloc, CFStringRef uuidStr) {
/* Parse the string into a bytes struct and then call the primitive. */
CFUUIDBytes bytes;
UniChar chars[100];
CFIndex len;
CFIndex i = 0;
if (uuidStr == NULL) return NULL;
len = CFStringGetLength(uuidStr);
if (len > 100) {
len = 100;
} else if (len == 0) {
return NULL;
}
CFStringGetCharacters(uuidStr, CFRangeMake(0, len), chars);
memset((void *)&bytes, 0, sizeof(bytes));
/* Skip initial random stuff */
while (!_isHexChar(chars[i]) && i < len) i++;
READ_A_BYTE(bytes.byte0);
READ_A_BYTE(bytes.byte1);
READ_A_BYTE(bytes.byte2);
READ_A_BYTE(bytes.byte3);
i++;
READ_A_BYTE(bytes.byte4);
READ_A_BYTE(bytes.byte5);
i++;
READ_A_BYTE(bytes.byte6);
READ_A_BYTE(bytes.byte7);
i++;
READ_A_BYTE(bytes.byte8);
READ_A_BYTE(bytes.byte9);
i++;
READ_A_BYTE(bytes.byte10);
READ_A_BYTE(bytes.byte11);
READ_A_BYTE(bytes.byte12);
READ_A_BYTE(bytes.byte13);
READ_A_BYTE(bytes.byte14);
READ_A_BYTE(bytes.byte15);
return __CFUUIDCreateWithBytesPrimitive(alloc, bytes, false);
}
CFStringRef CFUUIDCreateString(CFAllocatorRef alloc, CFUUIDRef uuid) {
CFMutableStringRef str = CFStringCreateMutable(alloc, 0);
UniChar buff[12];
// First segment (4 bytes, 8 digits + 1 dash)
_intToHexChars(uuid->_bytes.byte0, buff, 2);
_intToHexChars(uuid->_bytes.byte1, &(buff[2]), 2);
_intToHexChars(uuid->_bytes.byte2, &(buff[4]), 2);
_intToHexChars(uuid->_bytes.byte3, &(buff[6]), 2);
buff[8] = (UniChar)'-';
CFStringAppendCharacters(str, buff, 9);
// Second segment (2 bytes, 4 digits + 1 dash)
_intToHexChars(uuid->_bytes.byte4, buff, 2);
_intToHexChars(uuid->_bytes.byte5, &(buff[2]), 2);
buff[4] = (UniChar)'-';
CFStringAppendCharacters(str, buff, 5);
// Third segment (2 bytes, 4 digits + 1 dash)
_intToHexChars(uuid->_bytes.byte6, buff, 2);
_intToHexChars(uuid->_bytes.byte7, &(buff[2]), 2);
buff[4] = (UniChar)'-';
CFStringAppendCharacters(str, buff, 5);
// Fourth segment (2 bytes, 4 digits + 1 dash)
_intToHexChars(uuid->_bytes.byte8, buff, 2);
_intToHexChars(uuid->_bytes.byte9, &(buff[2]), 2);
buff[4] = (UniChar)'-';
CFStringAppendCharacters(str, buff, 5);
// Fifth segment (6 bytes, 12 digits)
_intToHexChars(uuid->_bytes.byte10, buff, 2);
_intToHexChars(uuid->_bytes.byte11, &(buff[2]), 2);
_intToHexChars(uuid->_bytes.byte12, &(buff[4]), 2);
_intToHexChars(uuid->_bytes.byte13, &(buff[6]), 2);
_intToHexChars(uuid->_bytes.byte14, &(buff[8]), 2);
_intToHexChars(uuid->_bytes.byte15, &(buff[10]), 2);
CFStringAppendCharacters(str, buff, 12);
return str;
}
CFUUIDRef CFUUIDGetConstantUUIDWithBytes(CFAllocatorRef alloc, uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4, uint8_t byte5, uint8_t byte6, uint8_t byte7, uint8_t byte8, uint8_t byte9, uint8_t byte10, uint8_t byte11, uint8_t byte12, uint8_t byte13, uint8_t byte14, uint8_t byte15) {
CFUUIDBytes bytes;
// CodeWarrior can't handle the structure assignment of bytes, so we must explode this - REW, 10/8/99
bytes.byte0 = byte0;
bytes.byte1 = byte1;
bytes.byte2 = byte2;
bytes.byte3 = byte3;
bytes.byte4 = byte4;
bytes.byte5 = byte5;
bytes.byte6 = byte6;
bytes.byte7 = byte7;
bytes.byte8 = byte8;
bytes.byte9 = byte9;
bytes.byte10 = byte10;
bytes.byte11 = byte11;
bytes.byte12 = byte12;
bytes.byte13 = byte13;
bytes.byte14 = byte14;
bytes.byte15 = byte15;
return __CFUUIDCreateWithBytesPrimitive(alloc, bytes, true);
}
CFUUIDBytes CFUUIDGetUUIDBytes(CFUUIDRef uuid) {
return uuid->_bytes;
}
CF_EXPORT CFUUIDRef CFUUIDCreateFromUUIDBytes(CFAllocatorRef alloc, CFUUIDBytes bytes) {
return __CFUUIDCreateWithBytesPrimitive(alloc, bytes, false);
}
#undef READ_A_BYTE
#if DEPLOYMENT_RUNTIME_SWIFT
void _cf_uuid_clear(_cf_uuid_t uu) { uuid_clear(uu); }
int _cf_uuid_compare(const _cf_uuid_t uu1, const _cf_uuid_t uu2) { return uuid_compare(uu1, uu2); }
void _cf_uuid_copy(_cf_uuid_t dst, const _cf_uuid_t src) { uuid_copy(dst, src); }
void _cf_uuid_generate(_cf_uuid_t out) { uuid_generate(out); }
void _cf_uuid_generate_random(_cf_uuid_t out) { uuid_generate_random(out); }
void _cf_uuid_generate_time(_cf_uuid_t out) { uuid_generate_time(out); }
int _cf_uuid_is_null(const _cf_uuid_t uu) { return uuid_is_null(uu); }
int _cf_uuid_parse(const _cf_uuid_string_t in, _cf_uuid_t uu) { return uuid_parse(in, uu); }
void _cf_uuid_unparse(const _cf_uuid_t uu, _cf_uuid_string_t out) { uuid_unparse(uu, out); }
void _cf_uuid_unparse_lower(const _cf_uuid_t uu, _cf_uuid_string_t out) { uuid_unparse_lower(uu, out); }
void _cf_uuid_unparse_upper(const _cf_uuid_t uu, _cf_uuid_string_t out) { uuid_unparse_upper(uu, out); }
#endif