File: CFUUID.c

package info (click to toggle)
gnustep-corebase 0.1.1%2B20230710-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,608 kB
  • sloc: ansic: 25,359; objc: 4,347; cpp: 75; xml: 49; makefile: 24; sh: 7
file content (349 lines) | stat: -rw-r--r-- 9,245 bytes parent folder | download
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
/* CFUUID.c
   
   Copyright (C) 2011 Free Software Foundation, Inc.
   
   Written by: Stefan Bidigaray
   Date: May, 2011
   
   This file is part of GNUstep CoreBase Library.
   
   This library is free software; you can redisibute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is disibuted in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#include "CoreFoundation/CFRuntime.h"
#include "CoreFoundation/CFBase.h"
#include "CoreFoundation/CFSet.h"
#include "CoreFoundation/CFString.h"
#include "CoreFoundation/CFUUID.h"
#include "GSPrivate.h"

/* Some of the code in CFUUID is based on Etoile's ETUUID class.
   Copyright (C) 2007 Yen-Ju Check <yjchenx AT gmail DOT com> */

#include <stdio.h>
#include <stdlib.h>
/* On *BSD and Linux we have a srandomdev() function which seeds the random 
 * number generator with entropy collected from a variety of sources. On other
 * platforms we don't, so we use some less random data based on the current 
 * time and pid to seed the random number generator.
 */
#if defined(__FreeBSD__) \
    || defined(__OpenBSD__) \
    || defined(__DragonFly__) \
    || defined(__APPLE__)
  #define INITRANDOM() srandomdev()
#elif defined(_WIN32)
  #define INITRANDOM() CFsrandomdev()
  #define UNICODE
  #include <windows.h>
  #include <stdlib.h>
BOOLEAN (APIENTRY *RtlGenRandom)(PVOID, ULONG);

static void CFsrandomdev (void)
{
  HMODULE lib = LoadLibraryW (L"advapi32.dll");
  RtlGenRandom = (BOOLEAN(APIENTRY*)(PVOID,ULONG))GetProcAddress (lib,
    "SystemFunction036");
}
#else
  #include <sys/time.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <time.h>
  #define INITRANDOM() CFsrandomdev()
  #if defined(__linux__) || defined(__GNU__)
    #include <fcntl.h>
    #include <errno.h>
    #include <unistd.h>
static void CFsrandomdev(void)
{
  int fd;
  unsigned int seed = 0;
  size_t len = sizeof(seed);
  Boolean hasSeed = false;
  
  fd = open("/dev/random", O_RDONLY | O_NONBLOCK, 0);
  if (fd >= 0) 
    {
      if (errno != EWOULDBLOCK)
        {
          if (read(fd, &seed, len) == (ssize_t)len)
            hasSeed = true;
        }
      close(fd);
    }
  
  if (hasSeed == false) 
    {
      struct timeval tv;
      
      gettimeofday(&tv, NULL);
      seed = ((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ time(NULL));
    }
  
  srandom(seed);
}
  #else
static void CFsrandomdev(void)
{
  struct timeval tv;
  unsigned int seed = 0;
  
  gettimeofday(&tv, NULL);
  seed = ((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ time(NULL));
  
  srandom(seed);
}
  #endif
#endif

static CFTypeID _kCFUUIDTypeID = 0;
static GSMutex _kCFUUIDLock;
static CFMutableSetRef _kCFUUIDConstants = NULL;

struct __CFUUID
{
  CFRuntimeBase _parent;
  CFUUIDBytes   _bytes;
};

static Boolean
CFUUIDBytesEqual (const void *b1, const void *b2)
{
  CFUUIDBytes *bytes1 = (CFUUIDBytes*)b1;
  CFUUIDBytes *bytes2 = (CFUUIDBytes*)b2;
  if (bytes1->byte0 == bytes2->byte0
      && bytes1->byte1 == bytes2->byte1
      && bytes1->byte2 == bytes2->byte2
      && bytes1->byte3 == bytes2->byte3
      && bytes1->byte4 == bytes2->byte4
      && bytes1->byte5 == bytes2->byte5
      && bytes1->byte6 == bytes2->byte6
      && bytes1->byte7 == bytes2->byte7
      && bytes1->byte8 == bytes2->byte8
      && bytes1->byte9 == bytes2->byte9
      && bytes1->byte10 == bytes2->byte10
      && bytes1->byte11 == bytes2->byte11
      && bytes1->byte12 == bytes2->byte12
      && bytes1->byte13 == bytes2->byte13
      && bytes1->byte14 == bytes2->byte14
      && bytes1->byte15 == bytes2->byte15)
    return true;
  
  return false;
}

static CFHashCode
CFUUIDBytesHash (const void *bytes)
{
  return GSHashBytes (bytes, 16);
}

static Boolean
CFUUIDEqual (CFTypeRef cf1, CFTypeRef cf2)
{
  CFUUIDBytes bytes1 = ((CFUUIDRef)cf1)->_bytes;
  CFUUIDBytes bytes2 = ((CFUUIDRef)cf2)->_bytes;
  
  return CFUUIDBytesEqual (&bytes1, &bytes2);
}

static CFHashCode
CFUUIDHash (CFTypeRef cf)
{
  return GSHashBytes ((const void*)&((CFUUIDRef)cf)->_bytes, 16);
}

static CFStringRef
CFUUIDCopyFormattingDescription (CFTypeRef cf, CFDictionaryRef formatOptions)
{
  return CFUUIDCreateString(NULL, cf);
}

static const CFRuntimeClass CFUUIDClass =
{
  0,
  "CFUUID",
  NULL, /* init */
  NULL, /* copy */
  NULL, /* dealloc */
  CFUUIDEqual, /* equal */
  CFUUIDHash, /* hash */
  CFUUIDCopyFormattingDescription, /* copyFormattingDesc */
  NULL  /* copyDebugDesc */
};

void CFUUIDInitialize (void)
{
  INITRANDOM();
  _kCFUUIDTypeID = _CFRuntimeRegisterClass (&CFUUIDClass);
  GSMutexInitialize (&_kCFUUIDLock);
}



CFUUIDRef
CFUUIDCreate (CFAllocatorRef alloc)
{
  CFUUIDBytes bytes;
  
#if defined(_WIN32)
  RtlGenRandom ((void*)&bytes, sizeof(bytes));
#else
  CFIndex i;
  for (i = 0 ; i < 16 ; ++i)
    {
      long r = random ();
      ((UInt8*)&bytes)[i] = (UInt8)r;
    }
#endif
  /* Set version */
  bytes.byte6 &= 0x0F;
  bytes.byte6 |= 0x40;
  /* Bit 6 must be 0 and 7 must be 1 */
  bytes.byte8 &= 0xBF;
  
  return CFUUIDCreateFromUUIDBytes (alloc, bytes);
}

CFUUIDRef
CFUUIDCreateFromString (CFAllocatorRef alloc, CFStringRef uuidStr)
{
  CFUUIDBytes bytes;
  char data[36]; /* 36 chars */
  if (!CFStringGetCString (uuidStr, data, 36, kCFStringEncodingASCII))
    return NULL;
  
  sscanf (data, "%4hx%4hx-%4hx-%4hx-%4hx-%4hx%4hx%4hx",
    (UInt16*)&bytes.byte1,(UInt16*)&bytes.byte3, (UInt16*)&bytes.byte5,
    (UInt16*)&bytes.byte7, (UInt16*)&bytes.byte9, (UInt16*)&bytes.byte11,
    (UInt16*)&bytes.byte13, (UInt16*)&bytes.byte15);
  
  return CFUUIDCreateFromUUIDBytes (alloc, bytes);
}

CFUUIDRef
CFUUIDCreateFromUUIDBytes (CFAllocatorRef alloc, CFUUIDBytes bytes)
{
  struct __CFUUID *new;
  
  new = (struct __CFUUID *)_CFRuntimeCreateInstance (alloc, _kCFUUIDTypeID,
    sizeof(struct __CFUUID) - sizeof(CFRuntimeBase), NULL);
  if (new)
    new->_bytes = bytes;
  
  return (CFUUIDRef)new;
}

CFUUIDRef
CFUUIDCreateWithBytes (CFAllocatorRef alloc, UInt8 byte0, UInt8 byte1,
  UInt8 byte2, UInt8 byte3, UInt8 byte4, UInt8 byte5, UInt8 byte6, UInt8 byte7,
  UInt8 byte8, UInt8 byte9, UInt8 byte10, UInt8 byte11, UInt8 byte12,
  UInt8 byte13, UInt8 byte14, UInt8 byte15)
{
  CFUUIDBytes uuidBytes;
  uuidBytes.byte0 = byte0;
  uuidBytes.byte1 = byte1;
  uuidBytes.byte2 = byte2;
  uuidBytes.byte3 = byte3;
  uuidBytes.byte4 = byte4;
  uuidBytes.byte5 = byte5;
  uuidBytes.byte6 = byte6;
  uuidBytes.byte7 = byte7;
  uuidBytes.byte8 = byte8;
  uuidBytes.byte9 = byte9;
  uuidBytes.byte10 = byte10;
  uuidBytes.byte11 = byte11;
  uuidBytes.byte12 = byte12;
  uuidBytes.byte13 = byte13;
  uuidBytes.byte14 = byte14;
  uuidBytes.byte15 = byte15;
  
  return CFUUIDCreateFromUUIDBytes (alloc, uuidBytes);
}

CFStringRef
CFUUIDCreateString (CFAllocatorRef alloc, CFUUIDRef uuid)
{
  CFStringRef ret;
  CFUUIDBytes bytes = uuid->_bytes;
  
  ret = CFStringCreateWithFormat (alloc, NULL, CFSTR("%2hhx%2hhx%2hhx%2hhx-"
    "%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx"),
      bytes.byte0, bytes.byte1, bytes.byte2, bytes.byte3,
      bytes.byte4, bytes.byte5, bytes.byte6, bytes.byte7,
      bytes.byte8, bytes.byte9, bytes.byte10, bytes.byte11,
      bytes.byte12, bytes.byte13, bytes.byte14, bytes.byte15);
  
  return ret;
}

CFUUIDRef
CFUUIDGetConstantUUIDWithBytes (CFAllocatorRef alloc, UInt8 byte0, UInt8 byte1,
  UInt8 byte2, UInt8 byte3, UInt8 byte4, UInt8 byte5, UInt8 byte6, UInt8 byte7,
  UInt8 byte8, UInt8 byte9, UInt8 byte10, UInt8 byte11, UInt8 byte12,
  UInt8 byte13, UInt8 byte14, UInt8 byte15)
{
  CFUUIDRef uuid;
  CFUUIDBytes uuidBytes;
  CFSetCallBacks cb = {0, NULL, NULL, NULL, CFUUIDBytesEqual, CFUUIDBytesHash};
  
  uuidBytes.byte0 = byte0;
  uuidBytes.byte1 = byte1;
  uuidBytes.byte2 = byte2;
  uuidBytes.byte3 = byte3;
  uuidBytes.byte4 = byte4;
  uuidBytes.byte5 = byte5;
  uuidBytes.byte6 = byte6;
  uuidBytes.byte7 = byte7;
  uuidBytes.byte8 = byte8;
  uuidBytes.byte9 = byte9;
  uuidBytes.byte10 = byte10;
  uuidBytes.byte11 = byte11;
  uuidBytes.byte12 = byte12;
  uuidBytes.byte13 = byte13;
  uuidBytes.byte14 = byte14;
  uuidBytes.byte15 = byte15;
  
  GSMutexLock (&_kCFUUIDLock);
  if (_kCFUUIDConstants == NULL)
    _kCFUUIDConstants = CFSetCreateMutable (NULL, 0, &cb);
  
  if (!CFSetGetValueIfPresent(_kCFUUIDConstants, &uuidBytes,
      (const void**)&uuid))
    {
      uuid = CFUUIDCreateFromUUIDBytes (NULL, uuidBytes);
      CFSetAddValue (_kCFUUIDConstants, (const void*)uuid);
      CFRelease (uuid);
    }
  GSMutexUnlock (&_kCFUUIDLock);
  
  return uuid;
}

CFUUIDBytes
CFUUIDGetUUIDBytes (CFUUIDRef uuid)
{
  return uuid->_bytes;
}

CFTypeID
CFUUIDGetTypeID (void)
{
  return _kCFUUIDTypeID;
}