-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathext_uuid.cpp
244 lines (182 loc) · 7.51 KB
/
ext_uuid.cpp
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
/*
UUID extension for HHVM.
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2005 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Hartmut Holzgraefe, Anthon Pang |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/ext/extension.h"
#include <cinttypes>
#include <uuid/uuid.h>
#define UUID_TYPE_DEFAULT 0
#define UUID_TYPE_NULL -1
#define UUID_TYPE_INVALID -42
namespace HPHP {
const StaticString s_UUID_VARIANT_NCS("UUID_VARIANT_NCS");
const int64_t k_UUID_VARIANT_NCS = UUID_VARIANT_NCS;
const StaticString s_UUID_VARIANT_DCE("UUID_VARIANT_DCE");
const int64_t k_UUID_VARIANT_DCE = UUID_VARIANT_DCE;
const StaticString s_UUID_VARIANT_MICROSOFT("UUID_VARIANT_MICROSOFT");
const int64_t k_UUID_VARIANT_MICROSOFT = UUID_VARIANT_MICROSOFT;
const StaticString s_UUID_VARIANT_OTHER("UUID_VARIANT_OTHER");
const int64_t k_UUID_VARIANT_OTHER = UUID_VARIANT_OTHER;
const StaticString s_UUID_TYPE_DEFAULT("UUID_TYPE_DEFAULT");
const int64_t k_UUID_TYPE_DEFAULT = UUID_TYPE_DEFAULT;
const StaticString s_UUID_TYPE_TIME("UUID_TYPE_TIME");
const int64_t k_UUID_TYPE_TIME = UUID_TYPE_DCE_TIME;
const StaticString s_UUID_TYPE_DCE("UUID_TYPE_DCE");
const int64_t k_UUID_TYPE_DCE = UUID_TYPE_DCE_RANDOM;
const StaticString s_UUID_TYPE_NAME("UUID_TYPE_NAME");
const int64_t k_UUID_TYPE_NAME = UUID_TYPE_DCE_TIME;
const StaticString s_UUID_TYPE_RANDOM("UUID_TYPE_RANDOM");
const int64_t k_UUID_TYPE_RANDOM = UUID_TYPE_DCE_RANDOM;
const StaticString s_UUID_TYPE_NULL("UUID_TYPE_NULL");
const int64_t k_UUID_TYPE_NULL = UUID_TYPE_NULL;
const StaticString s_UUID_TYPE_INVALID("UUID_TYPE_INVALID");
const int64_t k_UUID_TYPE_INVALID = UUID_TYPE_INVALID;
const int UUID_BUF_LEN = 37;
#define CHECK_UUID(uuid_param, u_param) \
if (uuid_parse(uuid_param.c_str(), u_param)) { \
return Variant(false); \
}
static Variant HHVM_FUNCTION(uuid_compare, const String& uuid1, const String& uuid2) {
uuid_t u1, u2;
CHECK_UUID(uuid1, u1)
CHECK_UUID(uuid2, u2)
return Variant(uuid_compare(u1, u2));
}
static String HHVM_FUNCTION(uuid_create, const Variant& uuid_type) {
uuid_t u;
char uuid_str[UUID_BUF_LEN];
int64_t uuid_type_value = UUID_TYPE_DEFAULT;
if (uuid_type.isInteger()) {
uuid_type_value = uuid_type.getInt64();
} else if (! uuid_type.isNull()) {
raise_param_type_warning("uuid_create", 2, KindOfInt64, uuid_type.getType());
uuid_type_value = UUID_TYPE_DEFAULT;
}
switch (uuid_type_value) {
case UUID_TYPE_DCE_TIME:
uuid_generate_time(u);
break;
case UUID_TYPE_DCE_RANDOM:
uuid_generate_random(u);
break;
default:
raise_warning(
"uuid_create(): Unknown/invalid UUID type '%ld' requested, using default type instead",
uuid_type_value
);
/* fall through */
case UUID_TYPE_DEFAULT:
uuid_generate(u);
break;
}
uuid_unparse(u, uuid_str);
return String(uuid_str);
}
static bool HHVM_FUNCTION(uuid_is_null, const String& uuid) {
uuid_t u;
if (uuid_parse(uuid.c_str(), u)) {
return false;
}
return (bool) uuid_is_null(u);
}
static bool HHVM_FUNCTION(uuid_is_valid, const String& uuid) {
uuid_t u;
return (bool) (0 == uuid_parse(uuid.c_str(), u));
}
static Variant HHVM_FUNCTION(uuid_mac, const String& uuid) {
uuid_t u;
char uuid_str[UUID_BUF_LEN];
CHECK_UUID(uuid, u)
if (uuid_variant(u) != UUID_VARIANT_DCE ||
uuid_type(u) != UUID_TYPE_DCE_TIME ||
(u[10] & 0x01) // fake MAC address if the multicast bit is set
) {
return Variant(false);
}
uuid_unparse(u, uuid_str);
return Variant(String((char *) (uuid_str + 24)));
}
static Variant HHVM_FUNCTION(uuid_parse, const String& uuid) {
uuid_t u;
CHECK_UUID(uuid, u)
return Variant(String((char *) &u, sizeof(uuid_t), CopyString));
}
static Variant HHVM_FUNCTION(uuid_time, const String& uuid) {
uuid_t u;
CHECK_UUID(uuid, u)
if (uuid_variant(u) != UUID_VARIANT_DCE ||
uuid_type(u) != UUID_TYPE_DCE_TIME
) {
return Variant(false);
}
return Variant(uuid_time(u, NULL));
}
static Variant HHVM_FUNCTION(uuid_type, const String& uuid) {
uuid_t u;
CHECK_UUID(uuid, u)
if (uuid_is_null(u)) {
return Variant(UUID_TYPE_NULL);
}
return Variant(uuid_type(u));
}
static Variant HHVM_FUNCTION(uuid_unparse, const String& u) {
char uuid_str[UUID_BUF_LEN];
if (u.length() != sizeof(uuid_t)) {
return Variant(false);
}
uuid_unparse((unsigned char *) u.c_str(), uuid_str);
return Variant(String(uuid_str, 36, CopyString));
}
static Variant HHVM_FUNCTION(uuid_variant, const String& uuid) {
uuid_t u;
CHECK_UUID(uuid, u)
if (uuid_is_null(u)) {
return Variant(UUID_TYPE_NULL);
}
return Variant(uuid_variant(u));
}
////////////////////////////////////////////////////////////////////////////////
class uuidExtension: public Extension {
public:
uuidExtension(): Extension("uuid", "1.0.3") {}
virtual void moduleInit() override {
Native::registerConstant<KindOfInt64>(s_UUID_VARIANT_NCS.get(), k_UUID_VARIANT_NCS);
Native::registerConstant<KindOfInt64>(s_UUID_VARIANT_DCE.get(), k_UUID_VARIANT_DCE);
Native::registerConstant<KindOfInt64>(s_UUID_VARIANT_MICROSOFT.get(), k_UUID_VARIANT_MICROSOFT);
Native::registerConstant<KindOfInt64>(s_UUID_VARIANT_OTHER.get(), k_UUID_VARIANT_OTHER);
Native::registerConstant<KindOfInt64>(s_UUID_TYPE_DEFAULT.get(), k_UUID_TYPE_DEFAULT);
Native::registerConstant<KindOfInt64>(s_UUID_TYPE_TIME.get(), k_UUID_TYPE_TIME);
Native::registerConstant<KindOfInt64>(s_UUID_TYPE_DCE.get(), k_UUID_TYPE_DCE);
Native::registerConstant<KindOfInt64>(s_UUID_TYPE_NAME.get(), k_UUID_TYPE_NAME);
Native::registerConstant<KindOfInt64>(s_UUID_TYPE_RANDOM.get(), k_UUID_TYPE_RANDOM);
Native::registerConstant<KindOfInt64>(s_UUID_TYPE_NULL.get(), k_UUID_TYPE_NULL);
Native::registerConstant<KindOfInt64>(s_UUID_TYPE_INVALID.get(), k_UUID_TYPE_INVALID);
HHVM_FE(uuid_compare);
HHVM_FE(uuid_create);
HHVM_FE(uuid_is_null);
HHVM_FE(uuid_is_valid);
HHVM_FE(uuid_mac);
HHVM_FE(uuid_parse);
HHVM_FE(uuid_time);
HHVM_FE(uuid_type);
HHVM_FE(uuid_unparse);
HHVM_FE(uuid_variant);
loadSystemlib();
}
} s_uuid_extension;
HHVM_GET_MODULE(uuid);
} /* namespace HPHP */