UUID::detectFormat()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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