-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
219 lines (198 loc) · 5.71 KB
/
codec.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (C) 2013-2018 by Maxim Bublis <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package uuid
import (
"errors"
"fmt"
"github.com/flexstack/uuid/base58"
)
// FromBytes returns a UUID generated from the raw byte slice input.
// It will return an error if the slice isn't 16 bytes long.
func FromBytes(input []byte) (UUID, error) {
u := UUID{}
err := u.UnmarshalBinary(input)
return u, err
}
// FromBytesOrNil returns a UUID generated from the raw byte slice input.
// Same behavior as FromBytes(), but returns uuid.Nil instead of an error.
func FromBytesOrNil(input []byte) UUID {
uuid, err := FromBytes(input)
if err != nil {
return Nil
}
return uuid
}
var errInvalidFormat = errors.New("uuid: invalid UUID format")
var hexLookupTable = func() [256]byte {
var table [256]byte
for i := range table {
table[i] = 255 // Default to 255 for invalid characters
}
// Valid hexadecimal characters
for i := '0'; i <= '9'; i++ {
table[i] = byte(i - '0')
}
for i := 'a'; i <= 'f'; i++ {
table[i] = byte(i - 'a' + 10)
}
for i := 'A'; i <= 'F'; i++ {
table[i] = byte(i - 'A' + 10)
}
return table
}()
var canonicalByteRange = [16]byte{
0, 2, 4, 6,
9, 11,
14, 16,
19, 21,
24, 26, 28, 30, 32, 34,
}
var hashByteRange = [16]byte{
0, 2, 4, 6,
8, 10,
12, 14,
16, 18,
20, 22, 24, 26, 28, 30,
}
// Parse parses the UUID stored in the string text. Parsing and supported
// formats are the same as UnmarshalText.
func (u *UUID) Parse(s string) error {
switch len(s) {
case 22: // base58
if err := base58.UnmarshalString(u[:], s); err != nil {
return err
}
return nil
case 32: // hash
for i := 0; i < 32; i += 2 {
v1 := hexLookupTable[s[i]]
v2 := hexLookupTable[s[i+1]]
if v1|v2 == 255 {
return errInvalidFormat
}
u[i/2] = (v1 << 4) | v2
}
return nil
case 36: // canonical
if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
return fmt.Errorf("uuid: incorrect UUID format in string %q", s)
}
for i, x := range canonicalByteRange {
v1 := hexLookupTable[s[x]]
v2 := hexLookupTable[s[x+1]]
if v1|v2 == 255 {
return errInvalidFormat
}
u[i] = (v1 << 4) | v2
}
return nil
default:
return fmt.Errorf("uuid: incorrect UUID length %d in string %q", len(s), s)
}
}
// FromString returns a UUID parsed from the input string.
// Input is expected in a form accepted by UnmarshalText.
func FromString(text string) (UUID, error) {
var u UUID
err := u.Parse(text)
return u, err
}
// FromStringOrNil returns a UUID parsed from the input string.
// Same behavior as FromString(), but returns uuid.Nil instead of an error.
func FromStringOrNil(input string) UUID {
uuid, err := FromString(input)
if err != nil {
return Nil
}
return uuid
}
// MarshalText implements the encoding.TextMarshaler interface.
// Creates a string representation of the UUID in the format specified by
// DefaultFormat.
func (u UUID) MarshalText() ([]byte, error) {
switch DefaultFormat {
case FormatCanonical:
var buf [36]byte
encodeCanonical(buf[:], u)
return buf[:], nil
case FormatHash:
var buf [32]byte
encodeHash(buf[:], u)
return buf[:], nil
default:
var buf [22]byte
copy(buf[:], base58.Encode(u[:]))
return buf[:], nil
}
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
// Following formats are supported:
//
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8" (canonical)
// "6ba7b8109dad11d180b400c04fd430c8" (hash)
// "1C9z3nFjeJ44HMBeuqGNxt" (base58)
func (u *UUID) UnmarshalText(b []byte) error {
switch len(b) {
case 22: // base58
if err := base58.UnmarshalBytes(u[:], b); err != nil {
return err
}
return nil
case 32: // hash
for i := 0; i < 32; i += 2 {
v1 := hexLookupTable[b[i]]
v2 := hexLookupTable[b[i+1]]
if v1|v2 == 255 {
return errInvalidFormat
}
u[i/2] = (v1 << 4) | v2
}
return nil
case 36: // canonical
if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
return fmt.Errorf("uuid: incorrect UUID format in string %q", b)
}
for i, x := range canonicalByteRange {
v1 := hexLookupTable[b[x]]
v2 := hexLookupTable[b[x+1]]
if v1|v2 == 255 {
return errInvalidFormat
}
u[i] = (v1 << 4) | v2
}
return nil
default:
return fmt.Errorf("uuid: incorrect UUID length %d in string %q", len(b), b)
}
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (u UUID) MarshalBinary() ([]byte, error) {
return u[:], nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
// It will return an error if the slice isn't 16 bytes long.
func (u *UUID) UnmarshalBinary(data []byte) error {
if len(data) != Size {
return fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
}
copy(u[:], data)
return nil
}