1
2
3
4
5
6 package base32
7
8 import (
9 "io"
10 "strconv"
11 )
12
13
16
17
18
19
20
21 type Encoding struct {
22 encode [32]byte
23 decodeMap [256]byte
24 padChar rune
25 }
26
27 const (
28 StdPadding rune = '='
29 NoPadding rune = -1
30 )
31
32 const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
33 const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
34
35
36
37 func NewEncoding(encoder string) *Encoding {
38 if len(encoder) != 32 {
39 panic("encoding alphabet is not 32-bytes long")
40 }
41
42 e := new(Encoding)
43 copy(e.encode[:], encoder)
44 e.padChar = StdPadding
45
46 for i := 0; i < len(e.decodeMap); i++ {
47 e.decodeMap[i] = 0xFF
48 }
49 for i := 0; i < len(encoder); i++ {
50 e.decodeMap[encoder[i]] = byte(i)
51 }
52 return e
53 }
54
55
56
57 var StdEncoding = NewEncoding(encodeStd)
58
59
60
61 var HexEncoding = NewEncoding(encodeHex)
62
63
64
65
66
67
68 func (enc Encoding) WithPadding(padding rune) *Encoding {
69 if padding == '\r' || padding == '\n' || padding > 0xff {
70 panic("invalid padding")
71 }
72
73 for i := 0; i < len(enc.encode); i++ {
74 if rune(enc.encode[i]) == padding {
75 panic("padding contained in alphabet")
76 }
77 }
78
79 enc.padChar = padding
80 return &enc
81 }
82
83
86
87
88
89
90
91
92
93 func (enc *Encoding) Encode(dst, src []byte) {
94 for len(src) > 0 {
95 var b [8]byte
96
97
98
99 switch len(src) {
100 default:
101 b[7] = src[4] & 0x1F
102 b[6] = src[4] >> 5
103 fallthrough
104 case 4:
105 b[6] |= (src[3] << 3) & 0x1F
106 b[5] = (src[3] >> 2) & 0x1F
107 b[4] = src[3] >> 7
108 fallthrough
109 case 3:
110 b[4] |= (src[2] << 1) & 0x1F
111 b[3] = (src[2] >> 4) & 0x1F
112 fallthrough
113 case 2:
114 b[3] |= (src[1] << 4) & 0x1F
115 b[2] = (src[1] >> 1) & 0x1F
116 b[1] = (src[1] >> 6) & 0x1F
117 fallthrough
118 case 1:
119 b[1] |= (src[0] << 2) & 0x1F
120 b[0] = src[0] >> 3
121 }
122
123
124 size := len(dst)
125 if size >= 8 {
126
127 dst[0] = enc.encode[b[0]&31]
128 dst[1] = enc.encode[b[1]&31]
129 dst[2] = enc.encode[b[2]&31]
130 dst[3] = enc.encode[b[3]&31]
131 dst[4] = enc.encode[b[4]&31]
132 dst[5] = enc.encode[b[5]&31]
133 dst[6] = enc.encode[b[6]&31]
134 dst[7] = enc.encode[b[7]&31]
135 } else {
136 for i := 0; i < size; i++ {
137 dst[i] = enc.encode[b[i]&31]
138 }
139 }
140
141
142 if len(src) < 5 {
143 if enc.padChar == NoPadding {
144 break
145 }
146
147 dst[7] = byte(enc.padChar)
148 if len(src) < 4 {
149 dst[6] = byte(enc.padChar)
150 dst[5] = byte(enc.padChar)
151 if len(src) < 3 {
152 dst[4] = byte(enc.padChar)
153 if len(src) < 2 {
154 dst[3] = byte(enc.padChar)
155 dst[2] = byte(enc.padChar)
156 }
157 }
158 }
159
160 break
161 }
162
163 src = src[5:]
164 dst = dst[8:]
165 }
166 }
167
168
169 func (enc *Encoding) EncodeToString(src []byte) string {
170 buf := make([]byte, enc.EncodedLen(len(src)))
171 enc.Encode(buf, src)
172 return string(buf)
173 }
174
175 type encoder struct {
176 err error
177 enc *Encoding
178 w io.Writer
179 buf [5]byte
180 nbuf int
181 out [1024]byte
182 }
183
184 func (e *encoder) Write(p []byte) (n int, err error) {
185 if e.err != nil {
186 return 0, e.err
187 }
188
189
190 if e.nbuf > 0 {
191 var i int
192 for i = 0; i < len(p) && e.nbuf < 5; i++ {
193 e.buf[e.nbuf] = p[i]
194 e.nbuf++
195 }
196 n += i
197 p = p[i:]
198 if e.nbuf < 5 {
199 return
200 }
201 e.enc.Encode(e.out[0:], e.buf[0:])
202 if _, e.err = e.w.Write(e.out[0:8]); e.err != nil {
203 return n, e.err
204 }
205 e.nbuf = 0
206 }
207
208
209 for len(p) >= 5 {
210 nn := len(e.out) / 8 * 5
211 if nn > len(p) {
212 nn = len(p)
213 nn -= nn % 5
214 }
215 e.enc.Encode(e.out[0:], p[0:nn])
216 if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil {
217 return n, e.err
218 }
219 n += nn
220 p = p[nn:]
221 }
222
223
224 copy(e.buf[:], p)
225 e.nbuf = len(p)
226 n += len(p)
227 return
228 }
229
230
231
232 func (e *encoder) Close() error {
233
234 if e.err == nil && e.nbuf > 0 {
235 e.enc.Encode(e.out[0:], e.buf[0:e.nbuf])
236 encodedLen := e.enc.EncodedLen(e.nbuf)
237 e.nbuf = 0
238 _, e.err = e.w.Write(e.out[0:encodedLen])
239 }
240 return e.err
241 }
242
243
244
245
246
247
248 func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
249 return &encoder{enc: enc, w: w}
250 }
251
252
253
254 func (enc *Encoding) EncodedLen(n int) int {
255 if enc.padChar == NoPadding {
256 return (n*8 + 4) / 5
257 }
258 return (n + 4) / 5 * 8
259 }
260
261
264
265 type CorruptInputError int64
266
267 func (e CorruptInputError) Error() string {
268 return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10)
269 }
270
271
272
273
274
275 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
276
277 _ = enc.decodeMap
278
279 dsti := 0
280 olen := len(src)
281
282 for len(src) > 0 && !end {
283
284 var dbuf [8]byte
285 dlen := 8
286
287 for j := 0; j < 8; {
288
289 if len(src) == 0 {
290 if enc.padChar != NoPadding {
291
292 return n, false, CorruptInputError(olen - len(src) - j)
293 }
294
295 dlen, end = j, true
296 break
297 }
298 in := src[0]
299 src = src[1:]
300 if in == byte(enc.padChar) && j >= 2 && len(src) < 8 {
301
302 if len(src)+j < 8-1 {
303
304 return n, false, CorruptInputError(olen)
305 }
306 for k := 0; k < 8-1-j; k++ {
307 if len(src) > k && src[k] != byte(enc.padChar) {
308
309 return n, false, CorruptInputError(olen - len(src) + k - 1)
310 }
311 }
312 dlen, end = j, true
313
314
315
316
317
318 if dlen == 1 || dlen == 3 || dlen == 6 {
319 return n, false, CorruptInputError(olen - len(src) - 1)
320 }
321 break
322 }
323 dbuf[j] = enc.decodeMap[in]
324 if dbuf[j] == 0xFF {
325 return n, false, CorruptInputError(olen - len(src) - 1)
326 }
327 j++
328 }
329
330
331
332 switch dlen {
333 case 8:
334 dst[dsti+4] = dbuf[6]<<5 | dbuf[7]
335 n++
336 fallthrough
337 case 7:
338 dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
339 n++
340 fallthrough
341 case 5:
342 dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1
343 n++
344 fallthrough
345 case 4:
346 dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
347 n++
348 fallthrough
349 case 2:
350 dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2
351 n++
352 }
353 dsti += 5
354 }
355 return n, end, nil
356 }
357
358
359
360
361
362
363 func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
364 buf := make([]byte, len(src))
365 l := stripNewlines(buf, src)
366 n, _, err = enc.decode(dst, buf[:l])
367 return
368 }
369
370
371 func (enc *Encoding) DecodeString(s string) ([]byte, error) {
372 buf := []byte(s)
373 l := stripNewlines(buf, buf)
374 n, _, err := enc.decode(buf, buf[:l])
375 return buf[:n], err
376 }
377
378 type decoder struct {
379 err error
380 enc *Encoding
381 r io.Reader
382 end bool
383 buf [1024]byte
384 nbuf int
385 out []byte
386 outbuf [1024 / 8 * 5]byte
387 }
388
389 func readEncodedData(r io.Reader, buf []byte, min int, expectsPadding bool) (n int, err error) {
390 for n < min && err == nil {
391 var nn int
392 nn, err = r.Read(buf[n:])
393 n += nn
394 }
395
396 if n < min && n > 0 && err == io.EOF {
397 err = io.ErrUnexpectedEOF
398 }
399
400
401
402 if expectsPadding && min < 8 && n == 0 && err == io.EOF {
403 err = io.ErrUnexpectedEOF
404 }
405 return
406 }
407
408 func (d *decoder) Read(p []byte) (n int, err error) {
409
410 if len(d.out) > 0 {
411 n = copy(p, d.out)
412 d.out = d.out[n:]
413 if len(d.out) == 0 {
414 return n, d.err
415 }
416 return n, nil
417 }
418
419 if d.err != nil {
420 return 0, d.err
421 }
422
423
424 nn := len(p) / 5 * 8
425 if nn < 8 {
426 nn = 8
427 }
428 if nn > len(d.buf) {
429 nn = len(d.buf)
430 }
431
432
433 var min int
434 var expectsPadding bool
435 if d.enc.padChar == NoPadding {
436 min = 1
437 expectsPadding = false
438 } else {
439 min = 8 - d.nbuf
440 expectsPadding = true
441 }
442
443 nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], min, expectsPadding)
444 d.nbuf += nn
445 if d.nbuf < min {
446 return 0, d.err
447 }
448
449
450 var nr int
451 if d.enc.padChar == NoPadding {
452 nr = d.nbuf
453 } else {
454 nr = d.nbuf / 8 * 8
455 }
456 nw := d.enc.DecodedLen(d.nbuf)
457
458 if nw > len(p) {
459 nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr])
460 d.out = d.outbuf[0:nw]
461 n = copy(p, d.out)
462 d.out = d.out[n:]
463 } else {
464 n, d.end, err = d.enc.decode(p, d.buf[0:nr])
465 }
466 d.nbuf -= nr
467 for i := 0; i < d.nbuf; i++ {
468 d.buf[i] = d.buf[i+nr]
469 }
470
471 if err != nil && (d.err == nil || d.err == io.EOF) {
472 d.err = err
473 }
474
475 if len(d.out) > 0 {
476
477
478
479
480 return n, nil
481 }
482
483 return n, d.err
484 }
485
486 type newlineFilteringReader struct {
487 wrapped io.Reader
488 }
489
490
491
492 func stripNewlines(dst, src []byte) int {
493 offset := 0
494 for _, b := range src {
495 if b == '\r' || b == '\n' {
496 continue
497 }
498 dst[offset] = b
499 offset++
500 }
501 return offset
502 }
503
504 func (r *newlineFilteringReader) Read(p []byte) (int, error) {
505 n, err := r.wrapped.Read(p)
506 for n > 0 {
507 s := p[0:n]
508 offset := stripNewlines(s, s)
509 if err != nil || offset > 0 {
510 return offset, err
511 }
512
513 n, err = r.wrapped.Read(p)
514 }
515 return n, err
516 }
517
518
519 func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
520 return &decoder{enc: enc, r: &newlineFilteringReader{r}}
521 }
522
523
524
525 func (enc *Encoding) DecodedLen(n int) int {
526 if enc.padChar == NoPadding {
527 return n * 5 / 8
528 }
529
530 return n / 8 * 5
531 }
532
View as plain text