1
|
<?php
|
2
|
/*-
|
3
|
* Copyright (c) 2008 Fredrik Lindberg - http://www.shapeshifter.se
|
4
|
* All rights reserved.
|
5
|
*
|
6
|
* Redistribution and use in source and binary forms, with or without
|
7
|
* modification, are permitted provided that the following conditions
|
8
|
* are met:
|
9
|
* 1. Redistributions of source code must retain the above copyright
|
10
|
* notice, this list of conditions and the following disclaimer.
|
11
|
* 2. Redistributions in binary form must reproduce the above copyright
|
12
|
* notice, this list of conditions and the following disclaimer in the
|
13
|
* documentation and/or other materials provided with the distribution.
|
14
|
*
|
15
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
16
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
17
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
18
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
19
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
20
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
21
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
22
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
24
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25
|
*
|
26
|
*/
|
27
|
|
28
|
/*
|
29
|
* UUID (RFC4122) Generator
|
30
|
* http://tools.ietf.org/html/rfc4122
|
31
|
*
|
32
|
* Implements version 1, 3, 4 and 5
|
33
|
*/
|
34
|
class UUID {
|
35
|
/* UUID versions */
|
36
|
const UUID_TIME = 1; /* Time based UUID */
|
37
|
const UUID_NAME_MD5 = 3; /* Name based (MD5) UUID */
|
38
|
const UUID_RANDOM = 4; /* Random UUID */
|
39
|
const UUID_NAME_SHA1 = 5; /* Name based (SHA1) UUID */
|
40
|
|
41
|
/* UUID formats */
|
42
|
const FMT_FIELD = 100;
|
43
|
const FMT_STRING = 101;
|
44
|
const FMT_BINARY = 102;
|
45
|
const FMT_QWORD = 1; /* Quad-word, 128-bit (not impl.) */
|
46
|
const FMT_DWORD = 2; /* Double-word, 64-bit (not impl.) */
|
47
|
const FMT_WORD = 4; /* Word, 32-bit (not impl.) */
|
48
|
const FMT_SHORT = 8; /* Short (not impl.) */
|
49
|
const FMT_BYTE = 16; /* Byte */
|
50
|
const FMT_DEFAULT = 16;
|
51
|
|
52
|
/* Field UUID representation */
|
53
|
static private $m_uuid_field = array(
|
54
|
'time_low' => 0, /* 32-bit */
|
55
|
'time_mid' => 0, /* 16-bit */
|
56
|
'time_hi' => 0, /* 16-bit */
|
57
|
'clock_seq_hi' => 0, /* 8-bit */
|
58
|
'clock_seq_low' => 0, /* 8-bit */
|
59
|
'node' => array() /* 48-bit */
|
60
|
);
|
61
|
|
62
|
static private $m_generate = array(
|
63
|
self::UUID_TIME => "generateTime",
|
64
|
self::UUID_RANDOM => "generateRandom",
|
65
|
self::UUID_NAME_MD5 => "generateNameMD5",
|
66
|
self::UUID_NAME_SHA1 => "generateNameSHA1"
|
67
|
);
|
68
|
|
69
|
static private $m_convert = array(
|
70
|
self::FMT_FIELD => array(
|
71
|
self::FMT_BYTE => "conv_field2byte",
|
72
|
self::FMT_STRING => "conv_field2string",
|
73
|
self::FMT_BINARY => "conv_field2binary"
|
74
|
),
|
75
|
self::FMT_BYTE => array(
|
76
|
self::FMT_FIELD => "conv_byte2field",
|
77
|
self::FMT_STRING => "conv_byte2string",
|
78
|
self::FMT_BINARY => "conv_byte2binary"
|
79
|
),
|
80
|
self::FMT_STRING => array(
|
81
|
self::FMT_BYTE => "conv_string2byte",
|
82
|
self::FMT_FIELD => "conv_string2field",
|
83
|
self::FMT_BINARY => "conv_string2binary"
|
84
|
),
|
85
|
);
|
86
|
|
87
|
/* Swap byte order of a 32-bit number */
|
88
|
static private function swap32($x) {
|
89
|
return (($x & 0x000000ff) << 24) | (($x & 0x0000ff00) << 8) |
|
90
|
(($x & 0x00ff0000) >> 8) | (($x & 0xff000000) >> 24);
|
91
|
}
|
92
|
|
93
|
/* Swap byte order of a 16-bit number */
|
94
|
static private function swap16($x) {
|
95
|
return (($x & 0x00ff) << 8) | (($x & 0xff00) >> 8);
|
96
|
}
|
97
|
|
98
|
/* Auto-detect UUID format */
|
99
|
static private function detectFormat($src) {
|
100
|
if (is_string($src))
|
101
|
return self::FMT_STRING;
|
102
|
else if (is_array($src)) {
|
103
|
$len = count($src);
|
104
|
if ($len == 1 || ($len % 2) == 0)
|
105
|
return $len;
|
106
|
else
|
107
|
return (-1);
|
108
|
}
|
109
|
else
|
110
|
return self::FMT_BINARY;
|
111
|
}
|
112
|
|
113
|
/*
|
114
|
* Public API, generate a UUID of 'type' in format 'fmt' for
|
115
|
* the given namespace 'ns' and node 'node'
|
116
|
*/
|
117
|
static public function generate($type, $fmt = self::FMT_BYTE,
|
118
|
$node = "", $ns = "") {
|
119
|
$func = self::$m_generate[$type];
|
120
|
if (!isset($func))
|
121
|
return null;
|
122
|
$conv = self::$m_convert[self::FMT_FIELD][$fmt];
|
123
|
|
124
|
$uuid = self::$func($ns, $node);
|
125
|
return self::$conv($uuid);
|
126
|
}
|
127
|
|
128
|
/*
|
129
|
* Public API, convert a UUID from one format to another
|
130
|
*/
|
131
|
static public function convert($uuid, $from, $to) {
|
132
|
$conv = self::$m_convert[$from][$to];
|
133
|
if (!isset($conv))
|
134
|
return ($uuid);
|
135
|
|
136
|
return (self::$conv($uuid));
|
137
|
}
|
138
|
|
139
|
/*
|
140
|
* Generate an UUID version 4 (pseudo random)
|
141
|
*/
|
142
|
static private function generateRandom($ns, $node) {
|
143
|
$uuid = self::$m_uuid_field;
|
144
|
|
145
|
$uuid['time_hi'] = (4 << 12) | (mt_rand(0, 0x1000));
|
146
|
$uuid['clock_seq_hi'] = (1 << 7) | mt_rand(0, 128);
|
147
|
$uuid['time_low'] = mt_rand(0, 0xffffffff);
|
148
|
$uuid['time_mid'] = mt_rand(0, 0x0000ffff);
|
149
|
$uuid['clock_seq_low'] = mt_rand(0, 255);
|
150
|
for ($i = 0; $i < 6; $i++)
|
151
|
$uuid['node'][$i] = mt_rand(0, 255);
|
152
|
return ($uuid);
|
153
|
}
|
154
|
|
155
|
/*
|
156
|
* Generate UUID version 3 and 5 (name based)
|
157
|
*/
|
158
|
static private function generateName($ns, $node, $hash, $version) {
|
159
|
$ns_fmt = self::detectFormat($ns);
|
160
|
$field = self::convert($ns, $ns_fmt, self::FMT_FIELD);
|
161
|
|
162
|
/* Swap byte order to keep it in big endian on all platforms */
|
163
|
$field['time_low'] = self::swap32($field['time_low']);
|
164
|
$field['time_mid'] = self::swap16($field['time_mid']);
|
165
|
$field['time_hi'] = self::swap16($field['time_hi']);
|
166
|
|
167
|
/* Convert the namespace to binary and concatenate node */
|
168
|
$raw = self::convert($field, self::FMT_FIELD, self::FMT_BINARY);
|
169
|
$raw .= $node;
|
170
|
|
171
|
/* Hash the namespace and node and convert to a byte array */
|
172
|
$val = $hash($raw, true);
|
173
|
$tmp = unpack('C16', $val);
|
174
|
foreach (array_keys($tmp) as $key)
|
175
|
$byte[$key - 1] = $tmp[$key];
|
176
|
|
177
|
/* Convert byte array to a field array */
|
178
|
$field = self::conv_byte2field($byte);
|
179
|
|
180
|
$field['time_low'] = self::swap32($field['time_low']);
|
181
|
$field['time_mid'] = self::swap16($field['time_mid']);
|
182
|
$field['time_hi'] = self::swap16($field['time_hi']);
|
183
|
|
184
|
/* Apply version and constants */
|
185
|
$field['clock_seq_hi'] &= 0x3f;
|
186
|
$field['clock_seq_hi'] |= (1 << 7);
|
187
|
$field['time_hi'] &= 0x0fff;
|
188
|
$field['time_hi'] |= ($version << 12);
|
189
|
|
190
|
return ($field);
|
191
|
}
|
192
|
static private function generateNameMD5($ns, $node) {
|
193
|
return self::generateName($ns, $node, "md5",
|
194
|
self::UUID_NAME_MD5);
|
195
|
}
|
196
|
static private function generateNameSHA1($ns, $node) {
|
197
|
return self::generateName($ns, $node, "sha1",
|
198
|
self::UUID_NAME_SHA1);
|
199
|
}
|
200
|
|
201
|
/*
|
202
|
* Generate UUID version 1 (time based)
|
203
|
*/
|
204
|
static private function generateTime($ns, $node) {
|
205
|
$uuid = self::$m_uuid_field;
|
206
|
|
207
|
/*
|
208
|
* Get current time in 100 ns intervals. The magic value
|
209
|
* is the offset between UNIX epoch and the UUID UTC
|
210
|
* time base October 15, 1582.
|
211
|
*/
|
212
|
$tp = gettimeofday();
|
213
|
$time = ($tp['sec'] * 10000000) + ($tp['usec'] * 10) +
|
214
|
0x01B21DD213814000;
|
215
|
|
216
|
/* Work around PHP 32-bit bit-operation limits */
|
217
|
$q = intval($time / 0xffffffff);
|
218
|
$low = $time - ($q * (0xffffffff + 1));
|
219
|
$high = intval(($time - $low) / 0xffffffff);
|
220
|
|
221
|
$uuid['time_low'] = $low;
|
222
|
$uuid['time_mid'] = $high & 0x0000ffff;
|
223
|
$uuid['time_hi'] = ($high & 0x0fff) | (self::UUID_TIME << 12);
|
224
|
|
225
|
/*
|
226
|
* We don't support saved state information and generate
|
227
|
* a random clock sequence each time.
|
228
|
*/
|
229
|
$uuid['clock_seq_hi'] = (1 << 7) | mt_rand(0, 128);
|
230
|
$uuid['clock_seq_low'] = mt_rand(0, 255);
|
231
|
|
232
|
/*
|
233
|
* Node should be set to the 48-bit IEEE node identifier, but
|
234
|
* we leave it for the user to supply the node.
|
235
|
*/
|
236
|
for ($i = 0; $i < 6; $i++)
|
237
|
$uuid['node'][$i] = ord(substr($node, $i, 1));
|
238
|
|
239
|
return ($uuid);
|
240
|
}
|
241
|
|
242
|
/* Assumes correct byte order */
|
243
|
static private function conv_field2byte($src) {
|
244
|
$uuid[0] = ($src['time_low'] & 0xff000000) >> 24;
|
245
|
$uuid[1] = ($src['time_low'] & 0x00ff0000) >> 16;
|
246
|
$uuid[2] = ($src['time_low'] & 0x0000ff00) >> 8;
|
247
|
$uuid[3] = ($src['time_low'] & 0x000000ff);
|
248
|
$uuid[4] = ($src['time_mid'] & 0xff00) >> 8;
|
249
|
$uuid[5] = ($src['time_mid'] & 0x00ff);
|
250
|
$uuid[6] = ($src['time_hi'] & 0xff00) >> 8;
|
251
|
$uuid[7] = ($src['time_hi'] & 0x00ff);
|
252
|
$uuid[8] = $src['clock_seq_hi'];
|
253
|
$uuid[9] = $src['clock_seq_low'];
|
254
|
|
255
|
for ($i = 0; $i < 6; $i++)
|
256
|
$uuid[10+$i] = $src['node'][$i];
|
257
|
|
258
|
return ($uuid);
|
259
|
}
|
260
|
|
261
|
static private function conv_field2string($src) {
|
262
|
$str = sprintf(
|
263
|
'%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
|
264
|
($src['time_low']), ($src['time_mid']), ($src['time_hi']),
|
265
|
$src['clock_seq_hi'], $src['clock_seq_low'],
|
266
|
$src['node'][0], $src['node'][1], $src['node'][2],
|
267
|
$src['node'][3], $src['node'][4], $src['node'][5]);
|
268
|
return ($str);
|
269
|
}
|
270
|
|
271
|
static private function conv_field2binary($src) {
|
272
|
$byte = self::conv_field2byte($src);
|
273
|
return self::conv_byte2binary($byte);
|
274
|
}
|
275
|
|
276
|
static private function conv_byte2field($uuid) {
|
277
|
$field = self::$m_uuid_field;
|
278
|
$field['time_low'] = ($uuid[0] << 24) | ($uuid[1] << 16) |
|
279
|
($uuid[2] << 8) | $uuid[3];
|
280
|
$field['time_mid'] = ($uuid[4] << 8) | $uuid[5];
|
281
|
$field['time_hi'] = ($uuid[6] << 8) | $uuid[7];
|
282
|
$field['clock_seq_hi'] = $uuid[8];
|
283
|
$field['clock_seq_low'] = $uuid[9];
|
284
|
|
285
|
for ($i = 0; $i < 6; $i++)
|
286
|
$field['node'][$i] = $uuid[10+$i];
|
287
|
return ($field);
|
288
|
}
|
289
|
|
290
|
static public function conv_byte2string($src) {
|
291
|
$field = self::conv_byte2field($src);
|
292
|
return self::conv_field2string($field);
|
293
|
}
|
294
|
|
295
|
static private function conv_byte2binary($src) {
|
296
|
$raw = pack('C16', $src[0], $src[1], $src[2], $src[3],
|
297
|
$src[4], $src[5], $src[6], $src[7], $src[8], $src[9],
|
298
|
$src[10], $src[11], $src[12], $src[13], $src[14], $src[15]);
|
299
|
return ($raw);
|
300
|
}
|
301
|
|
302
|
static private function conv_string2field($src) {
|
303
|
$parts = sscanf($src, '%x-%x-%x-%x-%02x%02x%02x%02x%02x%02x');
|
304
|
$field = self::$m_uuid_field;
|
305
|
$field['time_low'] = ($parts[0]);
|
306
|
$field['time_mid'] = ($parts[1]);
|
307
|
$field['time_hi'] = ($parts[2]);
|
308
|
$field['clock_seq_hi'] = ($parts[3] & 0xff00) >> 8;
|
309
|
$field['clock_seq_low'] = $parts[3] & 0x00ff;
|
310
|
for ($i = 0; $i < 6; $i++)
|
311
|
$field['node'][$i] = $parts[4+$i];
|
312
|
|
313
|
return ($field);
|
314
|
}
|
315
|
|
316
|
static private function conv_string2byte($src) {
|
317
|
$field = self::conv_string2field($src);
|
318
|
return self::conv_field2byte($field);
|
319
|
}
|
320
|
|
321
|
static private function conv_string2binary($src) {
|
322
|
$byte = self::conv_string2byte($src);
|
323
|
return self::conv_byte2binary($byte);
|
324
|
}
|
325
|
}
|
326
|
|
327
|
?>
|