-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathuuid.go
More file actions
435 lines (374 loc) · 12.2 KB
/
uuid.go
File metadata and controls
435 lines (374 loc) · 12.2 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package bluetooth
// This file implements 16-bit and 128-bit UUIDs as defined in the Bluetooth
// specification.
import (
"errors"
"unsafe"
)
// UUID is a single UUID as used in the Bluetooth stack. It is represented as a
// [4]uint32 instead of a [16]byte for efficiency.
type UUID struct {
id [4]uint32
}
var errInvalidUUID = errors.New("bluetooth: failed to parse UUID")
// NewUUID returns a new UUID based on the 128-bit (or 16-byte) input.
func NewUUID(uuid [16]byte) UUID {
uu := UUID{}
uu.id[0] = uint32(uuid[15]) | uint32(uuid[14])<<8 | uint32(uuid[13])<<16 | uint32(uuid[12])<<24
uu.id[1] = uint32(uuid[11]) | uint32(uuid[10])<<8 | uint32(uuid[9])<<16 | uint32(uuid[8])<<24
uu.id[2] = uint32(uuid[7]) | uint32(uuid[6])<<8 | uint32(uuid[5])<<16 | uint32(uuid[4])<<24
uu.id[3] = uint32(uuid[3]) | uint32(uuid[2])<<8 | uint32(uuid[1])<<16 | uint32(uuid[0])<<24
return uu
}
// New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID.
//
// Note: only use registered UUIDs. See
// https://www.bluetooth.com/specifications/gatt/services/ for a list.
func New16BitUUID(shortUUID uint16) UUID {
// https://stackoverflow.com/questions/36212020/how-can-i-convert-a-bluetooth-16-bit-service-uu-into-a-128-bit-uu
var uu UUID
uu.id[0] = 0x5F9B34FB
uu.id[1] = 0x80000080
uu.id[2] = 0x00001000
uu.id[3] = uint32(shortUUID)
return uu
}
// New32BitUUID returns a new 128-bit UUID based on a 32-bit UUID.
//
// Note: only use registered UUIDs. See
// https://www.bluetooth.com/specifications/gatt/services/ for a list.
func New32BitUUID(shortUUID uint32) UUID {
// https://stackoverflow.com/questions/36212020/how-can-i-convert-a-bluetooth-16-bit-service-uuid-into-a-128-bit-uuid
var uu UUID
uu.id[0] = 0x5F9B34FB
uu.id[1] = 0x80000080
uu.id[2] = 0x00001000
uu.id[3] = shortUUID
return uu
}
// Replace16BitComponent returns a new UUID where bits 16..32 have been replaced
// with the bits given in the argument. These bits are the same bits that vary
// in the 16-bit compressed UUID form.
//
// This is especially useful for the Nordic SoftDevice, because it is able to
// store custom UUIDs more efficiently when only these bits vary between them.
func (uuid UUID) Replace16BitComponent(component uint16) UUID {
uuid.id[3] &^= 0x0000ffff // clear the new component bits
uuid.id[3] |= uint32(component) // set the component bits
return uuid
}
// Is16Bit returns whether this UUID is a 16-bit BLE UUID.
func (uuid UUID) Is16Bit() bool {
return uuid.Is32Bit() && uuid.id[3] == uint32(uint16(uuid.id[3]))
}
// Is32Bit returns whether this UUID is a 32-bit or 16-bit BLE UUID.
func (uuid UUID) Is32Bit() bool {
return uuid.id[0] == 0x5F9B34FB && uuid.id[1] == 0x80000080 && uuid.id[2] == 0x00001000
}
// Get16Bit returns the 16-bit version of this UUID. This is only valid if it
// actually is a 16-bit UUID, see Is16Bit.
func (uuid UUID) Get16Bit() uint16 {
// Note: using a Get* function as a getter because method names can't start
// with a number.
return uint16(uuid.id[3])
}
// Get32Bit returns the 32-bit version of this UUID. This is only valid if it
// actually is a 32-bit UUID, see Is32Bit.
func (uuid UUID) Get32Bit() uint32 {
// Note: using a Get* function as a getter because method names can't start
// with a number.
return uuid.id[3]
}
// BytesBytesBigEndian returns a 16-byte array containing the raw UUID in
// network order. The result from BytesBigEndian may be used as the input for
// NewUUID.
func (uuid UUID) BytesBigEndian() [16]byte {
return [16]byte{
0: byte(uuid.id[3] >> 24),
1: byte(uuid.id[3] >> 16),
2: byte(uuid.id[3] >> 8),
3: byte(uuid.id[3]),
4: byte(uuid.id[2] >> 24),
5: byte(uuid.id[2] >> 16),
6: byte(uuid.id[2] >> 8),
7: byte(uuid.id[2]),
8: byte(uuid.id[1] >> 24),
9: byte(uuid.id[1] >> 16),
10: byte(uuid.id[1] >> 8),
11: byte(uuid.id[1]),
12: byte(uuid.id[0] >> 24),
13: byte(uuid.id[0] >> 16),
14: byte(uuid.id[0] >> 8),
15: byte(uuid.id[0]),
}
}
// bytes returns a 16-byte array containing the raw UUID in little-endian
// order.
func (uuid UUID) bytes() [16]byte {
return [16]byte{
0: byte(uuid.id[0]),
1: byte(uuid.id[0] >> 8),
2: byte(uuid.id[0] >> 16),
3: byte(uuid.id[0] >> 24),
4: byte(uuid.id[1]),
5: byte(uuid.id[1] >> 8),
6: byte(uuid.id[1] >> 16),
7: byte(uuid.id[1] >> 24),
8: byte(uuid.id[2]),
9: byte(uuid.id[2] >> 8),
10: byte(uuid.id[2] >> 16),
11: byte(uuid.id[2] >> 24),
12: byte(uuid.id[3]),
13: byte(uuid.id[3] >> 8),
14: byte(uuid.id[3] >> 16),
15: byte(uuid.id[3] >> 24),
}
}
// AppendBinary appends the bytes of the uuid in little-endian order to the
// given byte slice b.
func (uuid UUID) AppendBinary(b []byte) ([]byte, error) {
id := uuid.bytes()
return append(b, id[:]...), nil
}
// MarshalBinary marshals the uuid into and byte slice in little-endian order
// and returns the slice. It will not return an error.
func (uuid UUID) MarshalBinary() (data []byte, err error) {
return uuid.AppendBinary(make([]byte, 0, 16))
}
// ParseUUID parses the given UUID
//
// Expected formats:
//
// 00001234-0000-1000-8000-00805f9b34fb
// 00001234
// 1234
//
// If the UUID cannot be parsed, an error is returned.
// It will always successfully parse UUIDs generated by UUID.String().
func ParseUUID(s string) (uuid UUID, err error) {
err = (&uuid).UnmarshalText([]byte(s))
return
}
// UnmarshalText unmarshals a text representation of a UUID.
//
// Expected formats:
//
// 00001234-0000-1000-8000-00805f9b34fb
// 00001234
// 1234
//
// If the UUID cannot be parsed, an error is returned.
// It will always successfully parse UUIDs generated by UUID.String().
// This method is an adaptation of hex.Decode idea of using a reverse hex table.
func (u *UUID) UnmarshalText(s []byte) error {
switch len(s) {
case 36:
return u.unmarshalText128(s)
case 8:
return u.unmarshalText32(s)
case 4:
return u.unmarshalText16(s)
default:
return errInvalidUUID
}
}
// Using the reverseHexTable rebuild the UUID from the string s represented in bytes
// This implementation is the inverse of MarshalText and reaches performance parity
func (u *UUID) unmarshalText128(s []byte) error {
var j uint8
for i := 3; i >= 0; i-- {
// Skip hyphens
if s[j] == '-' {
j++
}
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]]) << 28
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]]) << 24
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]]) << 20
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]]) << 16
j++
// skip hypens
if s[j] == '-' {
j++
}
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]]) << 12
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]]) << 8
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]]) << 4
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[i] |= uint32(reverseHexTable[s[j]])
j++
}
return nil
}
// Using the reverseHexTable rebuild the UUID from the string s represented in bytes
// This implementation is the inverse of MarshalText and reaches performance pairity
func (u *UUID) unmarshalText32(s []byte) error {
u.id[0] = 0x5F9B34FB
u.id[1] = 0x80000080
u.id[2] = 0x00001000
var j uint8 = 0
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 28
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 24
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 20
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 16
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 12
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 8
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 4
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]])
j++
return nil
}
// Using the reverseHexTable rebuild the UUID from the string s represented in bytes
// This implementation is the inverse of MarshalText and reaches performance pairity
func (u *UUID) unmarshalText16(s []byte) error {
u.id[0] = 0x5F9B34FB
u.id[1] = 0x80000080
u.id[2] = 0x00001000
var j uint8 = 0
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 12
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 8
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]]) << 4
j++
if reverseHexTable[s[j]] == 255 {
return errInvalidUUID
}
u.id[3] |= uint32(reverseHexTable[s[j]])
j++
return nil
}
// This table is structured such that an ascii byte can be directly used as the array key
// to directly look up the decoded uint8 value. This is a similar solution to
// the reverseHexTable found in the hex package. 255 is a sentinel value
// indicating an invalid hex byte.
var reverseHexTable = [256]uint8{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
}
// String returns a human-readable version of this UUID, such as
// 00001234-0000-1000-8000-00805f9b34fb.
func (u UUID) String() string {
buf, _ := u.AppendText(make([]byte, 0, 36))
// pulled from the guts of string builder
return unsafe.String(unsafe.SliceData(buf), 36)
}
const hexDigitLower = "0123456789abcdef"
// AppendText converts and appends the uuid onto the given byte slice
// representing a human-readable version of this UUID, such as
// 00001234-0000-1000-8000-00805f9b34fb.
func (u UUID) AppendText(buf []byte) ([]byte, error) {
for i := 3; i >= 0; i-- {
// Insert a hyphen at the correct locations.
// position 4 and 8
if i != 3 && i != 0 {
buf = append(buf, '-')
}
buf = append(buf, hexDigitLower[byte(u.id[i]>>24)>>4])
buf = append(buf, hexDigitLower[byte(u.id[i]>>24)&0xF])
buf = append(buf, hexDigitLower[byte(u.id[i]>>16)>>4])
buf = append(buf, hexDigitLower[byte(u.id[i]>>16)&0xF])
// Insert a hyphen at the correct locations.
// position 6 and 10
if i == 2 || i == 1 {
buf = append(buf, '-')
}
buf = append(buf, hexDigitLower[byte(u.id[i]>>8)>>4])
buf = append(buf, hexDigitLower[byte(u.id[i]>>8)&0xF])
buf = append(buf, hexDigitLower[byte(u.id[i])>>4])
buf = append(buf, hexDigitLower[byte(u.id[i])&0xF])
}
return buf, nil
}
// MarshalText returns the converted uuid as a bytle slice
// representing a human-readable version, such as
// 00001234-0000-1000-8000-00805f9b34fb.
func (u UUID) MarshalText() ([]byte, error) {
return u.AppendText(make([]byte, 0, 36))
}
var ErrInvalidBinaryUUID = errors.New("bluetooth: failed to unmarshal the given binary UUID")
// UnmarshalBinary copies the given uuid bytes in little-endian order onto itself.
func (u *UUID) UnmarshalBinary(uuid []byte) error {
if len(uuid) != 16 {
return ErrInvalidBinaryUUID
}
u.id[0] = uint32(uuid[0]) | uint32(uuid[1])<<8 | uint32(uuid[2])<<16 | uint32(uuid[3])<<24
u.id[1] = uint32(uuid[4]) | uint32(uuid[5])<<8 | uint32(uuid[6])<<16 | uint32(uuid[7])<<24
u.id[2] = uint32(uuid[8]) | uint32(uuid[9])<<8 | uint32(uuid[10])<<16 | uint32(uuid[11])<<24
u.id[3] = uint32(uuid[12]) | uint32(uuid[13])<<8 | uint32(uuid[14])<<16 | uint32(uuid[15])<<24
return nil
}