This repository was archived by the owner on Apr 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrockford.go
More file actions
145 lines (122 loc) · 2.86 KB
/
crockford.go
File metadata and controls
145 lines (122 loc) · 2.86 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
package typeid
import (
"encoding/binary"
"fmt"
"github.com/google/uuid"
)
// Crockford base32 alphabet (lowercase). Excludes i, l, o, u.
const alphabet = "0123456789abcdefghjkmnpqrstvwxyz"
// decode maps every ASCII byte to its 5-bit value (0–31), or 0xFF if invalid.
var decode = func() (t [256]byte) {
for i := range t {
t[i] = 0xFF
}
for i, c := range alphabet {
t[c] = byte(i)
if c >= 'a' && c <= 'z' {
t[c-32] = byte(i)
}
}
return
}()
func decodeChar(c byte) (byte, error) {
v := decode[c]
if v == 0xFF {
return 0, fmt.Errorf("typeid: invalid base32 character %q", c)
}
return v, nil
}
// UUID encoding (128 bits -> 26 chars, appended to dst)
func appendBase32UUID(dst []byte, u uuid.UUID) []byte {
hi := binary.BigEndian.Uint64(u[:8])
lo := binary.BigEndian.Uint64(u[8:])
var buf [uuidSuffixLen]byte
for i := 25; i >= 14; i-- {
buf[i] = alphabet[lo&0x1F]
lo >>= 5
}
// char 13 straddles hi/lo: 4 remaining lo bits + 1 bit from hi
buf[13] = alphabet[(lo&0x0F)|((hi&0x01)<<4)]
hi >>= 1
for i := 12; i >= 1; i-- {
buf[i] = alphabet[hi&0x1F]
hi >>= 5
}
buf[0] = alphabet[hi&0x07]
return append(dst, buf[:]...)
}
// UUID decoding (26 chars -> 128 bits)
func decodeBase32UUID(s string) ([16]byte, error) {
if len(s) != uuidSuffixLen {
return [16]byte{}, fmt.Errorf("typeid: invalid suffix length %d", len(s))
}
v, err := decodeChar(s[0])
if err != nil {
return [16]byte{}, err
}
if v > 7 {
return [16]byte{}, ErrOverflowBase32
}
hi := uint64(v)
for i := 1; i <= 12; i++ {
v, err = decodeChar(s[i])
if err != nil {
return [16]byte{}, err
}
hi = (hi << 5) | uint64(v)
}
// char 13 straddle: top 1 bit → hi, bottom 4 bits → lo
v, err = decodeChar(s[13])
if err != nil {
return [16]byte{}, err
}
hi = (hi << 1) | uint64(v>>4)
lo := uint64(v & 0x0F)
for i := 14; i <= 25; i++ {
v, err = decodeChar(s[i])
if err != nil {
return [16]byte{}, err
}
lo = (lo << 5) | uint64(v)
}
var out [16]byte
binary.BigEndian.PutUint64(out[:8], hi)
binary.BigEndian.PutUint64(out[8:], lo)
return out, nil
}
// Int64 encoding (63 bits -> 13 chars, appended to dst)
func appendBase32Int64(dst []byte, n int64) []byte {
u := uint64(n)
var buf [int64SuffixLen]byte
for i := 12; i >= 1; i-- {
buf[i] = alphabet[u&0x1F]
u >>= 5
}
buf[0] = alphabet[u&0x07]
return append(dst, buf[:]...)
}
// Int64 decoding (13 chars -> 63 bits)
func decodeBase32Int64(s string) (int64, error) {
if len(s) != int64SuffixLen {
return 0, fmt.Errorf("typeid: invalid suffix length %d", len(s))
}
v, err := decodeChar(s[0])
if err != nil {
return 0, err
}
if v > 7 {
return 0, ErrOverflowBase32
}
val := uint64(v)
for i := 1; i < int64SuffixLen; i++ {
v, err = decodeChar(s[i])
if err != nil {
return 0, err
}
val = (val << 5) | uint64(v)
}
if val > 1<<63-1 {
return 0, ErrOverflowInt64
}
return int64(val), nil
}