| 1 |
// Copyright 2016 Google Inc. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package uuid |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/rand" |
| 10 |
"encoding/hex" |
| 11 |
"errors" |
| 12 |
"fmt" |
| 13 |
"io" |
| 14 |
"strings" |
| 15 |
) |
| 16 |
|
| 17 |
// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC |
| 18 |
// 4122. |
| 19 |
type UUID [16]byte |
| 20 |
|
| 21 |
// A Version represents a UUID's version. |
| 22 |
type Version byte |
| 23 |
|
| 24 |
// A Variant represents a UUID's variant. |
| 25 |
type Variant byte |
| 26 |
|
| 27 |
// Constants returned by Variant. |
| 28 |
const ( |
| 29 |
Invalid = Variant(iota) // Invalid UUID |
| 30 |
RFC4122 // The variant specified in RFC4122 |
| 31 |
Reserved // Reserved, NCS backward compatibility. |
| 32 |
Microsoft // Reserved, Microsoft Corporation backward compatibility. |
| 33 |
Future // Reserved for future definition. |
| 34 |
) |
| 35 |
|
| 36 |
var rander = rand.Reader // random function |
| 37 |
|
| 38 |
// Parse decodes s into a UUID or returns an error. Both the UUID form of |
| 39 |
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and |
| 40 |
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded. |
| 41 |
func Parse(s string) (UUID, error) { |
| 42 |
var uuid UUID |
| 43 |
if len(s) != 36 { |
| 44 |
if len(s) != 36+9 { |
| 45 |
return uuid, fmt.Errorf("invalid UUID length: %d", len(s)) |
| 46 |
} |
| 47 |
if strings.ToLower(s[:9]) != "urn:uuid:" { |
| 48 |
return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9]) |
| 49 |
} |
| 50 |
s = s[9:] |
| 51 |
} |
| 52 |
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' { |
| 53 |
return uuid, errors.New("invalid UUID format") |
| 54 |
} |
| 55 |
for i, x := range [16]int{ |
| 56 |
0, 2, 4, 6, |
| 57 |
9, 11, |
| 58 |
14, 16, |
| 59 |
19, 21, |
| 60 |
24, 26, 28, 30, 32, 34} { |
| 61 |
v, ok := xtob(s[x], s[x+1]) |
| 62 |
if !ok { |
| 63 |
return uuid, errors.New("invalid UUID format") |
| 64 |
} |
| 65 |
uuid[i] = v |
| 66 |
} |
| 67 |
return uuid, nil |
| 68 |
} |
| 69 |
|
| 70 |
// ParseBytes is like Parse, except it parses a byte slice instead of a string. |
| 71 |
func ParseBytes(b []byte) (UUID, error) { |
| 72 |
var uuid UUID |
| 73 |
if len(b) != 36 { |
| 74 |
if len(b) != 36+9 { |
| 75 |
return uuid, fmt.Errorf("invalid UUID length: %d", len(b)) |
| 76 |
} |
| 77 |
if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) { |
| 78 |
return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9]) |
| 79 |
} |
| 80 |
b = b[9:] |
| 81 |
} |
| 82 |
if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' { |
| 83 |
return uuid, errors.New("invalid UUID format") |
| 84 |
} |
| 85 |
for i, x := range [16]int{ |
| 86 |
0, 2, 4, 6, |
| 87 |
9, 11, |
| 88 |
14, 16, |
| 89 |
19, 21, |
| 90 |
24, 26, 28, 30, 32, 34} { |
| 91 |
v, ok := xtob(b[x], b[x+1]) |
| 92 |
if !ok { |
| 93 |
return uuid, errors.New("invalid UUID format") |
| 94 |
} |
| 95 |
uuid[i] = v |
| 96 |
} |
| 97 |
return uuid, nil |
| 98 |
} |
| 99 |
|
| 100 |
// FromBytes creates a new UUID from a byte slice. Returns an error if the slice |
| 101 |
// does not have a length of 16. The bytes are copied from the slice. |
| 102 |
func FromBytes(b []byte) (uuid UUID, err error) { |
| 103 |
err = uuid.UnmarshalBinary(b) |
| 104 |
return uuid, err |
| 105 |
} |
| 106 |
|
| 107 |
// Must returns uuid if err is nil and panics otherwise. |
| 108 |
func Must(uuid UUID, err error) UUID { |
| 109 |
if err != nil { |
| 110 |
panic(err) |
| 111 |
} |
| 112 |
return uuid |
| 113 |
} |
| 114 |
|
| 115 |
// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
| 116 |
// , or "" if uuid is invalid. |
| 117 |
func (uuid UUID) String() string { |
| 118 |
var buf [36]byte |
| 119 |
encodeHex(buf[:], uuid) |
| 120 |
return string(buf[:]) |
| 121 |
} |
| 122 |
|
| 123 |
// URN returns the RFC 2141 URN form of uuid, |
| 124 |
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. |
| 125 |
func (uuid UUID) URN() string { |
| 126 |
var buf [36 + 9]byte |
| 127 |
copy(buf[:], "urn:uuid:") |
| 128 |
encodeHex(buf[9:], uuid) |
| 129 |
return string(buf[:]) |
| 130 |
} |
| 131 |
|
| 132 |
func encodeHex(dst []byte, uuid UUID) { |
| 133 |
hex.Encode(dst[:], uuid[:4]) |
| 134 |
dst[8] = '-' |
| 135 |
hex.Encode(dst[9:13], uuid[4:6]) |
| 136 |
dst[13] = '-' |
| 137 |
hex.Encode(dst[14:18], uuid[6:8]) |
| 138 |
dst[18] = '-' |
| 139 |
hex.Encode(dst[19:23], uuid[8:10]) |
| 140 |
dst[23] = '-' |
| 141 |
hex.Encode(dst[24:], uuid[10:]) |
| 142 |
} |
| 143 |
|
| 144 |
// Variant returns the variant encoded in uuid. |
| 145 |
func (uuid UUID) Variant() Variant { |
| 146 |
switch { |
| 147 |
case (uuid[8] & 0xc0) == 0x80: |
| 148 |
return RFC4122 |
| 149 |
case (uuid[8] & 0xe0) == 0xc0: |
| 150 |
return Microsoft |
| 151 |
case (uuid[8] & 0xe0) == 0xe0: |
| 152 |
return Future |
| 153 |
default: |
| 154 |
return Reserved |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Version returns the version of uuid. |
| 159 |
func (uuid UUID) Version() Version { |
| 160 |
return Version(uuid[6] >> 4) |
| 161 |
} |
| 162 |
|
| 163 |
func (v Version) String() string { |
| 164 |
if v > 15 { |
| 165 |
return fmt.Sprintf("BAD_VERSION_%d", v) |
| 166 |
} |
| 167 |
return fmt.Sprintf("VERSION_%d", v) |
| 168 |
} |
| 169 |
|
| 170 |
func (v Variant) String() string { |
| 171 |
switch v { |
| 172 |
case RFC4122: |
| 173 |
return "RFC4122" |
| 174 |
case Reserved: |
| 175 |
return "Reserved" |
| 176 |
case Microsoft: |
| 177 |
return "Microsoft" |
| 178 |
case Future: |
| 179 |
return "Future" |
| 180 |
case Invalid: |
| 181 |
return "Invalid" |
| 182 |
} |
| 183 |
return fmt.Sprintf("BadVariant%d", int(v)) |
| 184 |
} |
| 185 |
|
| 186 |
// SetRand sets the random number generator to r, which implements io.Reader. |
| 187 |
// If r.Read returns an error when the package requests random data then |
| 188 |
// a panic will be issued. |
| 189 |
// |
| 190 |
// Calling SetRand with nil sets the random number generator to the default |
| 191 |
// generator. |
| 192 |
func SetRand(r io.Reader) { |
| 193 |
if r == nil { |
| 194 |
rander = rand.Reader |
| 195 |
return |
| 196 |
} |
| 197 |
rander = r |
| 198 |
} |