aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-storage-blob-go/2018-03-28/azblob/zc_uuid.go
blob: 1fc7e89cf29e402d96540124332b69d4d18aefd4 (plain) (blame)
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
package azblob

import (
    "crypto/rand"
    "fmt"
    "strconv"
)

// The UUID reserved variants.
const (
    reservedNCS       byte = 0x80
    reservedRFC4122   byte = 0x40
    reservedMicrosoft byte = 0x20
    reservedFuture    byte = 0x00
)

// A UUID representation compliant with specification in RFC 4122 document.
type uuid [16]byte

// NewUUID returns a new uuid using RFC 4122 algorithm.
func newUUID() (u uuid) {
    u = uuid{}
    // Set all bits to randomly (or pseudo-randomly) chosen values.
    _, err := rand.Read(u[:])
    if err != nil {
        panic("ran.Read failed")
    }
    u[8] = (u[8] | reservedRFC4122) & 0x7F // u.setVariant(ReservedRFC4122)

    var version byte = 4
    u[6] = (u[6] & 0xF) | (version << 4) // u.setVersion(4)
    return
}

// String returns an unparsed version of the generated UUID sequence.
func (u uuid) String() string {
    return fmt.Sprintf("%x-%x-%x-%x-%x", u[0:4], u[4:6], u[6:8], u[8:10], u[10:])
}

// ParseUUID parses a string formatted as "003020100-0504-0706-0809-0a0b0c0d0e0f"
// or "{03020100-0504-0706-0809-0a0b0c0d0e0f}" into a UUID.
func parseUUID(uuidStr string) uuid {
    char := func(hexString string) byte {
        i, _ := strconv.ParseUint(hexString, 16, 8)
        return byte(i)
    }
    if uuidStr[0] == '{' {
        uuidStr = uuidStr[1:] // Skip over the '{'
    }
    // 03020100 - 05 04 - 07 06 - 08 09 - 0a 0b 0c 0d 0e 0f
    //             1 11 1 11 11 1 12 22 2 22 22 22 33 33 33
    // 01234567 8 90 12 3 45 67 8 90 12 3 45 67 89 01 23 45
    uuidVal := uuid{
        char(uuidStr[0:2]),
        char(uuidStr[2:4]),
        char(uuidStr[4:6]),
        char(uuidStr[6:8]),

        char(uuidStr[9:11]),
        char(uuidStr[11:13]),

        char(uuidStr[14:16]),
        char(uuidStr[16:18]),

        char(uuidStr[19:21]),
        char(uuidStr[21:23]),

        char(uuidStr[24:26]),
        char(uuidStr[26:28]),
        char(uuidStr[28:30]),
        char(uuidStr[30:32]),
        char(uuidStr[32:34]),
        char(uuidStr[34:36]),
    }
    return uuidVal
}

func (u uuid) bytes() []byte {
    return u[:]
}