-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCMUUID.m
More file actions
167 lines (143 loc) · 3.83 KB
/
CMUUID.m
File metadata and controls
167 lines (143 loc) · 3.83 KB
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
//
// CMUUID.m
// WODCoach
//
// Created by Casey Marshall on 6/14/10.
// Copyright 2010 Modal Domains. All rights reserved.
//
#import "CMUUID.h"
@implementation CMUUID
- (id) initWithString: (NSString *) uuidStr
{
if (self = [super init])
{
CFUUIDRef u = CFUUIDCreateFromString(NULL, (CFStringRef) uuidStr);
bytes = CFUUIDGetUUIDBytes(u);
CFRelease(u);
}
return self;
}
- (id) initWithUUIDRef: (CFUUIDRef) uuid
{
if (self = [super init])
{
bytes = CFUUIDGetUUIDBytes(uuid);
}
return self;
}
- (id) initWithUUIDBytes: (CFUUIDBytes) uuidBytes
{
if (self = [super init])
{
bytes = uuidBytes;
}
return self;
}
- (id) initWithData: (NSData *) data
{
if (self = [super init])
{
if ([data length] < 16)
{
[self release];
return nil;
}
[data getBytes: &bytes length: sizeof(bytes)];
}
return self;
}
+ (CMUUID *) uuidWithString: (NSString *) uuidStr
{
return [[[CMUUID alloc] initWithString: uuidStr] autorelease];
}
+ (CMUUID *) uuidWithUUIDRef: (CFUUIDRef) uuid
{
return [[[CMUUID alloc] initWithUUIDRef: uuid] autorelease];
}
+ (CMUUID *) uuidWithUUIDBytes: (CFUUIDBytes) uuidBytes
{
return [[[CMUUID alloc] initWithUUIDBytes: uuidBytes] autorelease];
}
+ (CMUUID *) uuidWithData: (NSData *) data
{
return [[[CMUUID alloc] initWithData: data] autorelease];
}
+ (CMUUID *) randomUuid
{
CFUUIDBytes bytes;
SecRandomCopyBytes(kSecRandomDefault, sizeof(bytes), (UInt8 *) &bytes);
return [CMUUID uuidWithUUIDBytes: bytes];
}
+ (CMUUID *) nullUuid
{
CFUUIDBytes bytes;
memset(&bytes, 0, sizeof(bytes));
return [CMUUID uuidWithUUIDBytes: bytes];
}
- (NSString *) stringValue
{
return [NSString stringWithFormat: @"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
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];
}
- (NSComparisonResult) compareTo: (CMUUID *) that
{
CFUUIDBytes thatbytes = [that bytes];
int result = memcmp(&bytes, &thatbytes, sizeof(bytes));
if (result < 0)
return NSOrderedAscending;
if (result > 0)
return NSOrderedDescending;
return NSOrderedSame;
}
- (BOOL) isEqual: (id) object
{
if (object == nil || ![object isKindOfClass: [CMUUID class]])
return NO;
CMUUID *that = (CMUUID *) object;
CFUUIDBytes thatbytes = [that bytes];
return memcmp(&bytes, &thatbytes, sizeof(bytes)) == 0;
}
- (NSString *) description
{
return [self stringValue];
}
- (CFUUIDRef) uuid
{
return CFUUIDCreateWithBytes(NULL, 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);
}
- (CFUUIDBytes) bytes
{
CFUUIDBytes ret = bytes;
return ret;
}
- (NSData *) data
{
return [NSData dataWithBytes: (void *) &bytes
length: sizeof(bytes)];
}
- (BOOL) isNullUuid
{
return (bytes.byte0 == 0
&& bytes.byte1 == 0
&& bytes.byte2 == 0
&& bytes.byte3 == 0
&& bytes.byte4 == 0
&& bytes.byte5 == 0
&& bytes.byte6 == 0
&& bytes.byte7 == 0
&& bytes.byte8 == 0
&& bytes.byte9 == 0
&& bytes.byte10 == 0
&& bytes.byte11 == 0
&& bytes.byte12 == 0
&& bytes.byte13 == 0
&& bytes.byte14 == 0
&& bytes.byte15 == 0);
}
@end