| 1 | // Copyright (C) 2013-2018 by Maxim Bublis <[email protected]> |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining |
| 4 | // a copy of this software and associated documentation files (the |
| 5 | // "Software"), to deal in the Software without restriction, including |
| 6 | // without limitation the rights to use, copy, modify, merge, publish, |
| 7 | // distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | // permit persons to whom the Software is furnished to do so, subject to |
| 9 | // the following conditions: |
| 10 | // |
| 11 | // The above copyright notice and this permission notice shall be |
| 12 | // included in all copies or substantial portions of the Software. |
| 13 | // |
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 18 | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 19 | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 20 | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | package uuid |
| 23 | |
| 24 | // FromBytes returns a UUID generated from the raw byte slice input. |
| 25 | // It will return an error if the slice isn't 16 bytes long. |
| 26 | func FromBytes(input []byte) (UUID, error) { |
| 27 | u := UUID{} |
| 28 | err := u.UnmarshalBinary(input) |
| 29 | return u, err |
| 30 | } |
| 31 | |
| 32 | // FromBytesOrNil returns a UUID generated from the raw byte slice input. |
| 33 | // Same behavior as FromBytes(), but returns uuid.Nil instead of an error. |
| 34 | func FromBytesOrNil(input []byte) UUID { |
| 35 | // The logic here is duplicated from UnmarshalBinary as there is unnecessary |
| 36 | // overhead generating errors which would be checked and discarded. |
| 37 | if len(input) != Size { |
| 38 | return Nil |
| 39 | } |
| 40 | |
| 41 | uuid := UUID{} |
| 42 | copy(uuid[:], input) |
| 43 | |
| 44 | return uuid |
| 45 | } |
| 46 | |
| 47 | func fromHexChar(c byte) byte { |
| 48 | switch { |
| 49 | case '0' <= c && c <= '9': |
| 50 | return c - '0' |
| 51 | case 'a' <= c && c <= 'f': |
| 52 | return c - 'a' + 10 |
| 53 | case 'A' <= c && c <= 'F': |
| 54 | return c - 'A' + 10 |
| 55 | } |
| 56 | return 255 |
| 57 | } |
| 58 | |
| 59 | // parseBytes parses UUID text representation from a byte slice and populates |
| 60 | // the provided UUID reference. It centralizes the parsing logic so that both |
| 61 | // Parse (string input) and UnmarshalText ([]byte input) can delegate to this |
| 62 | // single implementation, eliminating code duplication. |
| 63 | // |
| 64 | // Supported formats and ABNF grammar are documented on UnmarshalText; refer |
| 65 | // there for full details. This helper simply enforces those rules. |
| 66 | func parseBytes(b []byte, u *UUID) error { |
| 67 | // Fast-path: ensure we don't accidentally mutate the caller's slice. |
| 68 | // We will only reslice, never modify the underlying bytes. |
| 69 | switch len(b) { |
| 70 | case 32: // hash |
| 71 | case 36: // canonical |
| 72 | case 34, 38: |
| 73 | if b[0] != '{' || b[len(b)-1] != '}' { |
| 74 | return ErrInvalidBraces |
| 75 | } |
| 76 | b = b[1 : len(b)-1] |
| 77 | case 41, 45: |
| 78 | if string(b[:9]) != "urn:uuid:" { |
| 79 | return ErrInvalidURNPrefix |
| 80 | } |
| 81 | b = b[9:] |
| 82 | default: |
| 83 | return ErrIncorrectLength |
| 84 | } |
| 85 | |
| 86 | // canonical (36 chars with dashes at fixed positions) |
| 87 | if len(b) == 36 { |
| 88 | if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' { |
| 89 | return ErrInvalidDashes |
| 90 | } |
| 91 | for i, x := range [16]byte{ |
| 92 | 0, 2, 4, 6, |
| 93 | 9, 11, |
| 94 | 14, 16, |
| 95 | 19, 21, |
| 96 | 24, 26, 28, 30, 32, 34, |
| 97 | } { |
| 98 | v1 := fromHexChar(b[x]) |
| 99 | v2 := fromHexChar(b[x+1]) |
| 100 | if v1|v2 == 255 { |
| 101 | return ErrInvalidFormat |
| 102 | } |
| 103 | u[i] = (v1 << 4) | v2 |
| 104 | } |
| 105 | return nil |
| 106 | } |
| 107 | |
| 108 | // hash-like (32 hex chars, no dashes) |
| 109 | for i := 0; i < 32; i += 2 { |
| 110 | v1 := fromHexChar(b[i]) |
| 111 | v2 := fromHexChar(b[i+1]) |
| 112 | if v1|v2 == 255 { |
| 113 | return ErrInvalidFormat |
| 114 | } |
| 115 | u[i/2] = (v1 << 4) | v2 |
| 116 | } |
| 117 | return nil |
| 118 | } |
| 119 | |
| 120 | // Parse parses the UUID stored in the string text. Parsing and supported |
| 121 | // formats are the same as UnmarshalText. |
| 122 | func (u *UUID) Parse(s string) error { |
| 123 | return parseBytes([]byte(s), u) |
| 124 | } |
| 125 | |
| 126 | // FromString returns a UUID parsed from the input string. |
| 127 | // Input is expected in a form accepted by UnmarshalText. |
| 128 | func FromString(text string) (UUID, error) { |
| 129 | var u UUID |
| 130 | err := u.Parse(text) |
| 131 | return u, err |
| 132 | } |
| 133 | |
| 134 | // FromStringOrNil returns a UUID parsed from the input string. |
| 135 | // Same behavior as FromString(), but returns uuid.Nil instead of an error. |
| 136 | func FromStringOrNil(input string) UUID { |
| 137 | uuid, err := FromString(input) |
| 138 | if err != nil { |
| 139 | return Nil |
| 140 | } |
| 141 | return uuid |
| 142 | } |
| 143 | |
| 144 | // MarshalText implements the encoding.TextMarshaler interface. |
| 145 | // The encoding is the same as returned by the String() method. |
| 146 | func (u UUID) MarshalText() ([]byte, error) { |
| 147 | var buf [36]byte |
| 148 | encodeCanonical(buf[:], u) |
| 149 | return buf[:], nil |
| 150 | } |
| 151 | |
| 152 | // Following formats are supported: |
| 153 | // |
| 154 | // "6ba7b810-9dad-11d1-80b4-00c04fd430c8", |
| 155 | // "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}", |
| 156 | // "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" |
| 157 | // "6ba7b8109dad11d180b400c04fd430c8", |
| 158 | // "{6ba7b8109dad11d180b400c04fd430c8}", |
| 159 | // "urn:uuid:6ba7b8109dad11d180b400c04fd430c8" |
| 160 | // |
| 161 | // ABNF for supported UUID text representation follows: |
| 162 | // |
| 163 | // URN := "urn" |
| 164 | // UUID-NID := "uuid" |
| 165 | // |
| 166 | // hexdig := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | |
| 167 | // "a" | "b" | "c" | "d" | "e" | "f" | |
| 168 | // "A" | "B" | "C" | "D" | "E" | "F" |
| 169 | // |
| 170 | // hexoct := hexdig hexdig |
| 171 | // 2hexoct := hexoct hexoct |
| 172 | // 4hexoct := 2hexoct 2hexoct |
| 173 | // 6hexoct := 4hexoct 2hexoct |
| 174 | // 12hexoct := 6hexoct 6hexoct |
| 175 | // |
| 176 | // hashlike := 12hexoct |
| 177 | // canonical := 4hexoct "-" 2hexoct "-" 2hexoct "-" 2hexoct "-" 6hexoct |
| 178 | // |
| 179 | // plain := canonical | hashlike |
| 180 | // braced := "{" plain "}" |
| 181 | // urn := URN ":" UUID-NID ":" plain |
| 182 | // |
| 183 | // uuid := canonical | hashlike | braced | urn |
| 184 | // |
| 185 | // The function delegates validation to internal parseBytes(). |
| 186 | func (u *UUID) UnmarshalText(b []byte) error { |
| 187 | return parseBytes(b, u) |
| 188 | } |
| 189 | |
| 190 | // MarshalBinary implements the encoding.BinaryMarshaler interface. |
| 191 | func (u UUID) MarshalBinary() ([]byte, error) { |
| 192 | return u.Bytes(), nil |
| 193 | } |
| 194 | |
| 195 | // UnmarshalBinary implements the encoding.BinaryUnmarshaler interface. |
| 196 | // It will return an error if the slice isn't 16 bytes long. |
| 197 | func (u *UUID) UnmarshalBinary(data []byte) error { |
| 198 | if len(data) != Size { |
| 199 | return ErrIncorrectByteLength |
| 200 | } |
| 201 | copy(u[:], data) |
| 202 | |
| 203 | return nil |
| 204 | } |