-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase62.go
261 lines (221 loc) · 4.97 KB
/
base62.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
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
package base62
import (
"bytes"
"errors"
"fmt"
"io"
)
var masks = []uint{0x00, 0x01, 0x03, 0x07, 0xf, 0x1f, 0x3f, 0x7f, 0xff}
var encoding = newEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
type enc struct {
chr string
pos map[byte]uint
}
// TODO(knorton): Add an output buffer
type encoder struct {
w io.Writer
// fragment of bits left from the last write
buf uint
// number of bits left in buf
num uint
// This represents a carry state in the encoder where a single bit is
// being pushed into the next 6-bit word. The least significant bit
// indicates the bit value of the carry and the next significant bit
// indicates whether there is a carry at all. Examples:
// 0x0 - no carry
// 0x3 - carry of 1
// 0x2 - carry of 0
car uint
}
type decoder struct {
r io.Reader
rb [1024]byte
// fragment of written bits
buf uint
// bits written into buf
num uint
}
func newEncoding(chr string) *enc {
p := map[byte]uint{}
for i := 0; i < len(chr); i++ {
p[chr[i]] = uint(i)
}
return &enc{chr, p}
}
// Combine an bits of av with bn bits of bv to get a word with an + bn bits.
func concat(av, an, bv, bn uint) uint {
return ((av & masks[an]) << bn) | (bv & masks[bn])
}
// Read n bits from p.
func readBits(e *encoder, n uint, p []byte) (uint, uint, []byte) {
// log.Printf("Read %x bits\n", n)
var rv uint = 0
var rn uint = 0
for n > 0 {
// can we finish this read from buf?
if e.num >= n {
// read upper n bits of buf
v := masks[n] & (e.buf >> (e.num - n))
e.num -= n
rv <<= n
rv |= v
rn += n
return rv, rn, p
}
// can we make this read at all?
if len(p) == 0 {
return 0, 0, p
}
// read what is left in buf and bring in more bits from p
v := masks[e.num] & e.buf
rv |= v
rn += e.num
n -= e.num
e.num = 8
e.buf = uint(p[0])
p = p[1:]
}
panic("unreachable")
}
// Encode a 6-bit word into a character, emit it on the writer and return
// the carry state.
func encode6Bits(e *encoder, v uint) (uint, error) {
chr := encoding.chr
if v < 60 {
if _, err := e.w.Write([]byte{chr[v]}); err != nil {
return 0, err
}
return 0, nil
}
if v < 62 {
if _, err := e.w.Write([]byte{chr[60]}); err != nil {
return 0, err
}
} else {
if _, err := e.w.Write([]byte{chr[61]}); err != nil {
return 0, err
}
}
return (v & 1) | 2, nil
}
func (e *encoder) Write(p []byte) (int, error) {
c := len(p)
if c == 0 {
return 0, nil
}
var v uint
var n uint
var err error
for {
if e.car&2 == 0 {
v, n, p = readBits(e, 6, p)
if n == 0 {
return c, nil
}
e.car, err = encode6Bits(e, v)
if err != nil {
return c - len(p), err
}
} else {
v, n, p = readBits(e, 5, p)
if n == 0 {
return c, nil
}
e.car, err = encode6Bits(e, concat(e.car&1, 1, v, 5))
if err != nil {
return c - len(p), err
}
}
}
panic("unreachable")
}
func (e *encoder) Close() error {
if e.num == 0 && e.car&2 == 0 {
return nil
}
if e.car&2 == 0 {
if _, err := encode6Bits(e, e.buf&masks[e.num]<<(6-e.num)); err != nil {
return err
}
} else {
if _, err := encode6Bits(e, concat(e.car&1, 1, e.buf&masks[e.num]<<(5-e.num), 5)); err != nil {
return err
}
}
return nil
}
func writeBits(d *decoder, r *[]byte, w *[]byte, v uint, n uint) {
// how many bits are left in our 1-byte buffer
l := 8 - d.num
// will we fill up buf in this write?
if n >= l {
(*w)[0] = byte(d.buf | (v>>(n-l))&masks[l])
d.buf = 0
d.num = 0
n -= l
l = 8
*r = (*r)[1:]
*w = (*w)[1:]
}
if n == 0 {
return
}
// put what is left in buf
d.buf |= (masks[n] & v) << (l - n)
d.num += n
}
func (d *decoder) Read(p []byte) (int, error) {
nn := len(p)
nr := nn
if nr > len(d.rb) {
nr = len(d.rb)
}
q := d.rb[:nr]
n, err := io.ReadAtLeast(d.r, q, 1)
if err != nil && err != io.EOF {
return 0, err
}
for i := 0; i < n; i++ {
c := d.rb[i]
pos, ok := encoding.pos[c]
if !ok {
return 0, errors.New(fmt.Sprintf("illegal character %d\n", c))
}
switch pos {
case 60:
writeBits(d, &q, &p, 0x1e, 5)
case 61:
writeBits(d, &q, &p, 0x1f, 5)
default:
writeBits(d, &q, &p, pos, 6)
}
}
return nn - len(p), err
}
func NewEncoder(w io.Writer) io.WriteCloser {
return &encoder{w: w}
}
func NewDecoder(r io.Reader) io.Reader {
return &decoder{r: r}
}
func DecodedLen(n int) int {
return n / 2
}
func EncodedLen(n int) int {
return n * 2
}
func EncodeToString(data []byte) string {
var buf bytes.Buffer
e := NewEncoder(&buf)
e.Write(data)
e.Close()
return buf.String()
}
func DecodeString(data string) ([]byte, error) {
var res bytes.Buffer
d := NewDecoder(bytes.NewBufferString(data))
if _, err := io.Copy(&res, d); err != nil {
return nil, err
}
return res.Bytes(), nil
}