2016-02-19 22:30:25 +02:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
2011-08-09 00:34:29 +03:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2011-08-08 21:45:10 +03:00
|
|
|
|
|
|
|
package uuid
|
|
|
|
|
|
|
|
import (
|
2016-02-26 07:36:20 +02:00
|
|
|
"bytes"
|
2011-08-08 21:45:10 +03:00
|
|
|
"crypto/rand"
|
2015-10-11 03:32:16 +02:00
|
|
|
"encoding/hex"
|
2016-02-19 22:30:25 +02:00
|
|
|
"errors"
|
2011-08-08 21:45:10 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
|
|
|
|
// 4122.
|
2016-02-19 22:30:25 +02:00
|
|
|
type UUID [16]byte
|
2011-08-08 21:45:10 +03:00
|
|
|
|
2016-02-19 22:30:25 +02:00
|
|
|
// A Version represents a UUID's version.
|
2011-08-08 21:45:10 +03:00
|
|
|
type Version byte
|
|
|
|
|
2016-02-19 22:30:25 +02:00
|
|
|
// A Variant represents a UUID's variant.
|
2011-08-08 21:45:10 +03:00
|
|
|
type Variant byte
|
|
|
|
|
|
|
|
// Constants returned by Variant.
|
|
|
|
const (
|
2011-08-10 03:20:55 +03:00
|
|
|
Invalid = Variant(iota) // Invalid UUID
|
|
|
|
RFC4122 // The variant specified in RFC4122
|
|
|
|
Reserved // Reserved, NCS backward compatibility.
|
|
|
|
Microsoft // Reserved, Microsoft Corporation backward compatibility.
|
|
|
|
Future // Reserved for future definition.
|
2011-08-08 21:45:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var rander = rand.Reader // random function
|
|
|
|
|
2016-02-19 22:30:25 +02:00
|
|
|
// Parse decodes s into a UUID or returns an error. Both the UUID form of
|
2011-08-08 21:45:10 +03:00
|
|
|
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
|
|
|
|
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
|
2016-02-19 22:30:25 +02:00
|
|
|
func Parse(s string) (UUID, error) {
|
|
|
|
var uuid UUID
|
|
|
|
if len(s) != 36 {
|
|
|
|
if len(s) != 36+9 {
|
|
|
|
return uuid, fmt.Errorf("invalid UUID length: %d", len(s))
|
|
|
|
}
|
2011-08-08 21:45:10 +03:00
|
|
|
if strings.ToLower(s[:9]) != "urn:uuid:" {
|
2016-02-19 22:30:25 +02:00
|
|
|
return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
s = s[9:]
|
|
|
|
}
|
|
|
|
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
|
2016-02-19 22:30:25 +02:00
|
|
|
return uuid, errors.New("invalid UUID format")
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
2015-10-10 23:00:45 +02:00
|
|
|
for i, x := range [16]int{
|
2011-08-08 21:45:10 +03:00
|
|
|
0, 2, 4, 6,
|
|
|
|
9, 11,
|
|
|
|
14, 16,
|
|
|
|
19, 21,
|
|
|
|
24, 26, 28, 30, 32, 34} {
|
2016-02-26 07:36:20 +02:00
|
|
|
if v, ok := xtob(s[x], s[x+1]); !ok {
|
2016-02-19 22:30:25 +02:00
|
|
|
return uuid, errors.New("invalid UUID format")
|
2011-08-08 21:45:10 +03:00
|
|
|
} else {
|
|
|
|
uuid[i] = v
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 22:30:25 +02:00
|
|
|
return uuid, nil
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
|
2016-02-28 10:52:46 +02:00
|
|
|
// ParseBytes is like Parse, except it parses a byte slice instead of a string.
|
2016-02-19 22:30:25 +02:00
|
|
|
func ParseBytes(b []byte) (UUID, error) {
|
2016-02-26 07:36:20 +02:00
|
|
|
var uuid UUID
|
|
|
|
if len(b) != 36 {
|
|
|
|
if len(b) != 36+9 {
|
|
|
|
return uuid, fmt.Errorf("invalid UUID length: %d", len(b))
|
|
|
|
}
|
|
|
|
if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
|
|
|
|
return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
|
|
|
|
}
|
|
|
|
b = b[9:]
|
|
|
|
}
|
|
|
|
if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
|
|
|
|
return uuid, errors.New("invalid UUID format")
|
|
|
|
}
|
|
|
|
for i, x := range [16]int{
|
|
|
|
0, 2, 4, 6,
|
|
|
|
9, 11,
|
|
|
|
14, 16,
|
|
|
|
19, 21,
|
|
|
|
24, 26, 28, 30, 32, 34} {
|
|
|
|
if v, ok := xtob(b[x], b[x+1]); !ok {
|
|
|
|
return uuid, errors.New("invalid UUID format")
|
|
|
|
} else {
|
|
|
|
uuid[i] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uuid, nil
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 21:34:15 +02:00
|
|
|
// Must returns uuid if err is nil and panics otherwise.
|
|
|
|
func Must(uuid UUID, err error) UUID {
|
2016-02-19 22:30:25 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2016-02-11 22:00:09 +02:00
|
|
|
}
|
2016-02-24 21:34:15 +02:00
|
|
|
return uuid
|
2016-02-11 22:00:09 +02:00
|
|
|
}
|
|
|
|
|
2011-08-08 21:45:10 +03:00
|
|
|
// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
|
|
// , or "" if uuid is invalid.
|
|
|
|
func (uuid UUID) String() string {
|
2015-10-11 03:32:16 +02:00
|
|
|
var buf [36]byte
|
2015-10-11 05:56:32 +02:00
|
|
|
encodeHex(buf[:], uuid)
|
2015-10-11 03:32:16 +02:00
|
|
|
return string(buf[:])
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// URN returns the RFC 2141 URN form of uuid,
|
|
|
|
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
|
|
|
|
func (uuid UUID) URN() string {
|
2015-10-11 04:03:07 +02:00
|
|
|
var buf [36 + 9]byte
|
|
|
|
copy(buf[:], "urn:uuid:")
|
2015-10-11 05:56:32 +02:00
|
|
|
encodeHex(buf[9:], uuid)
|
2015-10-11 04:03:07 +02:00
|
|
|
return string(buf[:])
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
|
2015-10-11 05:56:32 +02:00
|
|
|
func encodeHex(dst []byte, uuid UUID) {
|
|
|
|
hex.Encode(dst[:], uuid[:4])
|
|
|
|
dst[8] = '-'
|
|
|
|
hex.Encode(dst[9:13], uuid[4:6])
|
|
|
|
dst[13] = '-'
|
|
|
|
hex.Encode(dst[14:18], uuid[6:8])
|
|
|
|
dst[18] = '-'
|
|
|
|
hex.Encode(dst[19:23], uuid[8:10])
|
|
|
|
dst[23] = '-'
|
|
|
|
hex.Encode(dst[24:], uuid[10:])
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
|
2016-02-19 22:30:25 +02:00
|
|
|
// Variant returns the variant encoded in uuid.
|
2011-08-08 21:45:10 +03:00
|
|
|
func (uuid UUID) Variant() Variant {
|
|
|
|
switch {
|
|
|
|
case (uuid[8] & 0xc0) == 0x80:
|
|
|
|
return RFC4122
|
|
|
|
case (uuid[8] & 0xe0) == 0xc0:
|
2011-08-10 03:20:55 +03:00
|
|
|
return Microsoft
|
2011-08-08 21:45:10 +03:00
|
|
|
case (uuid[8] & 0xe0) == 0xe0:
|
2011-08-10 03:20:55 +03:00
|
|
|
return Future
|
2011-08-08 21:45:10 +03:00
|
|
|
default:
|
2011-08-10 03:20:55 +03:00
|
|
|
return Reserved
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-19 22:30:25 +02:00
|
|
|
// Version returns the version of uuid.
|
|
|
|
func (uuid UUID) Version() Version {
|
|
|
|
return Version(uuid[6] >> 4)
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v Version) String() string {
|
|
|
|
if v > 15 {
|
|
|
|
return fmt.Sprintf("BAD_VERSION_%d", v)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("VERSION_%d", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Variant) String() string {
|
|
|
|
switch v {
|
|
|
|
case RFC4122:
|
|
|
|
return "RFC4122"
|
2011-08-10 03:20:55 +03:00
|
|
|
case Reserved:
|
|
|
|
return "Reserved"
|
|
|
|
case Microsoft:
|
|
|
|
return "Microsoft"
|
|
|
|
case Future:
|
|
|
|
return "Future"
|
|
|
|
case Invalid:
|
|
|
|
return "Invalid"
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
2011-08-10 03:20:55 +03:00
|
|
|
return fmt.Sprintf("BadVariant%d", int(v))
|
2011-08-08 21:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetRand sets the random number generator to r, which implents io.Reader.
|
|
|
|
// If r.Read returns an error when the package requests random data then
|
|
|
|
// a panic will be issued.
|
|
|
|
//
|
|
|
|
// Calling SetRand with nil sets the random number generator to the default
|
|
|
|
// generator.
|
|
|
|
func SetRand(r io.Reader) {
|
|
|
|
if r == nil {
|
|
|
|
rander = rand.Reader
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rander = r
|
|
|
|
}
|