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 for i := 0; i < len(p); i++ {
225 e.buf[i] = p[i]
226 }
227 e.nbuf = len(p)
228 n += len(p)
229 return
230 }
231
232
233
234 func (e *encoder) Close() error {
235
236 if e.err == nil && e.nbuf > 0 {
237 e.enc.Encode(e.out[0:], e.buf[0:e.nbuf])
238 encodedLen := e.enc.EncodedLen(e.nbuf)
239 e.nbuf = 0
240 _, e.err = e.w.Write(e.out[0:encodedLen])
241 }
242 return e.err
243 }
244
245
246
247
248
249
250 func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
251 return &encoder{enc: enc, w: w}
252 }
253
254
255
256 func (enc *Encoding) EncodedLen(n int) int {
257 if enc.padChar == NoPadding {
258 return (n*8 + 4) / 5
259 }
260 return (n + 4) / 5 * 8
261 }
262
263
266
267 type CorruptInputError int64
268
269 func (e CorruptInputError) Error() string {
270 return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10)
271 }
272
273
274
275
276
277 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
278
279 _ = enc.decodeMap
280
281 dsti := 0
282 olen := len(src)
283
284 for len(src) > 0 && !end {
285
286 var dbuf [8]byte
287 dlen := 8
288
289 for j := 0; j < 8; {
290
291 if len(src) == 0 {
292 if enc.padChar != NoPadding {
293
294 return n, false, CorruptInputError(olen - len(src) - j)
295 }
296
297 dlen, end = j, true
298 break
299 }
300 in := src[0]
301 src = src[1:]
302 if in == byte(enc.padChar) && j >= 2 && len(src) < 8 {
303
304 if len(src)+j < 8-1 {
305
306 return n, false, CorruptInputError(olen)
307 }
308 for k := 0; k < 8-1-j; k++ {
309 if len(src) > k && src[k] != byte(enc.padChar) {
310
311 return n, false, CorruptInputError(olen - len(src) + k - 1)
312 }
313 }
314 dlen, end = j, true
315
316
317
318
319
320 if dlen == 1 || dlen == 3 || dlen == 6 {
321 return n, false, CorruptInputError(olen - len(src) - 1)
322 }
323 break
324 }
325 dbuf[j] = enc.decodeMap[in]
326 if dbuf[j] == 0xFF {
327 return n, false, CorruptInputError(olen - len(src) - 1)
328 }
329 j++
330 }
331
332
333
334 switch dlen {
335 case 8:
336 dst[dsti+4] = dbuf[6]<<5 | dbuf[7]
337 n++
338 fallthrough
339 case 7:
340 dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
341 n++
342 fallthrough
343 case 5:
344 dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1
345 n++
346 fallthrough
347 case 4:
348 dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
349 n++
350 fallthrough
351 case 2:
352 dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2
353 n++
354 }
355 dsti += 5
356 }
357 return n, end, nil
358 }
359
360
361
362
363
364
365 func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
366 buf := make([]byte, len(src))
367 l := stripNewlines(buf, src)
368 n, _, err = enc.decode(dst, buf[:l])
369 return
370 }
371
372
373 func (enc *Encoding) DecodeString(s string) ([]byte, error) {
374 buf := []byte(s)
375 l := stripNewlines(buf, buf)
376 n, _, err := enc.decode(buf, buf[:l])
377 return buf[:n], err
378 }
379
380 type decoder struct {
381 err error
382 enc *Encoding
383 r io.Reader
384 end bool
385 buf [1024]byte
386 nbuf int
387 out []byte
388 outbuf [1024 / 8 * 5]byte
389 }
390
391 func readEncodedData(r io.Reader, buf []byte, min int, expectsPadding bool) (n int, err error) {
392 for n < min && err == nil {
393 var nn int
394 nn, err = r.Read(buf[n:])
395 n += nn
396 }
397
398 if n < min && n > 0 && err == io.EOF {
399 err = io.ErrUnexpectedEOF
400 }
401
402
403
404 if expectsPadding && min < 8 && n == 0 && err == io.EOF {
405 err = io.ErrUnexpectedEOF
406 }
407 return
408 }
409
410 func (d *decoder) Read(p []byte) (n int, err error) {
411
412 if len(d.out) > 0 {
413 n = copy(p, d.out)
414 d.out = d.out[n:]
415 if len(d.out) == 0 {
416 return n, d.err
417 }
418 return n, nil
419 }
420
421 if d.err != nil {
422 return 0, d.err
423 }
424
425
426 nn := len(p) / 5 * 8
427 if nn < 8 {
428 nn = 8
429 }
430 if nn > len(d.buf) {
431 nn = len(d.buf)
432 }
433
434
435 var min int
436 var expectsPadding bool
437 if d.enc.padChar == NoPadding {
438 min = 1
439 expectsPadding = false
440 } else {
441 min = 8 - d.nbuf
442 expectsPadding = true
443 }
444
445 nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], min, expectsPadding)
446 d.nbuf += nn
447 if d.nbuf < min {
448 return 0, d.err
449 }
450
451
452 var nr int
453 if d.enc.padChar == NoPadding {
454 nr = d.nbuf
455 } else {
456 nr = d.nbuf / 8 * 8
457 }
458 nw := d.enc.DecodedLen(d.nbuf)
459
460 if nw > len(p) {
461 nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr])
462 d.out = d.outbuf[0:nw]
463 n = copy(p, d.out)
464 d.out = d.out[n:]
465 } else {
466 n, d.end, err = d.enc.decode(p, d.buf[0:nr])
467 }
468 d.nbuf -= nr
469 for i := 0; i < d.nbuf; i++ {
470 d.buf[i] = d.buf[i+nr]
471 }
472
473 if err != nil && (d.err == nil || d.err == io.EOF) {
474 d.err = err
475 }
476
477 if len(d.out) > 0 {
478
479
480
481
482 return n, nil
483 }
484
485 return n, d.err
486 }
487
488 type newlineFilteringReader struct {
489 wrapped io.Reader
490 }
491
492
493
494 func stripNewlines(dst, src []byte) int {
495 offset := 0
496 for _, b := range src {
497 if b == '\r' || b == '\n' {
498 continue
499 }
500 dst[offset] = b
501 offset++
502 }
503 return offset
504 }
505
506 func (r *newlineFilteringReader) Read(p []byte) (int, error) {
507 n, err := r.wrapped.Read(p)
508 for n > 0 {
509 s := p[0:n]
510 offset := stripNewlines(s, s)
511 if err != nil || offset > 0 {
512 return offset, err
513 }
514
515 n, err = r.wrapped.Read(p)
516 }
517 return n, err
518 }
519
520
521 func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
522 return &decoder{enc: enc, r: &newlineFilteringReader{r}}
523 }
524
525
526
527 func (enc *Encoding) DecodedLen(n int) int {
528 if enc.padChar == NoPadding {
529 return n * 5 / 8
530 }
531
532 return n / 8 * 5
533 }
534
View as plain text