| | |
| | |
| | |
| |
|
| | |
| | package base32 |
| |
|
| | import ( |
| | "io" |
| | "slices" |
| | "strconv" |
| | ) |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | type Encoding struct { |
| | encode [32]byte |
| | decodeMap [256]uint8 |
| | padChar rune |
| | } |
| |
|
| | const ( |
| | StdPadding rune = '=' |
| | NoPadding rune = -1 |
| | ) |
| |
|
| | const ( |
| | decodeMapInitialize = "" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| | "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" |
| | invalidIndex = '\xff' |
| | ) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func NewEncoding(encoder string) *Encoding { |
| | if len(encoder) != 32 { |
| | panic("encoding alphabet is not 32-bytes long") |
| | } |
| |
|
| | e := new(Encoding) |
| | e.padChar = StdPadding |
| | copy(e.encode[:], encoder) |
| | copy(e.decodeMap[:], decodeMapInitialize) |
| |
|
| | for i := 0; i < len(encoder); i++ { |
| | |
| | |
| | |
| | switch { |
| | case encoder[i] == '\n' || encoder[i] == '\r': |
| | panic("encoding alphabet contains newline character") |
| | case e.decodeMap[encoder[i]] != invalidIndex: |
| | panic("encoding alphabet includes duplicate symbols") |
| | } |
| | e.decodeMap[encoder[i]] = uint8(i) |
| | } |
| | return e |
| | } |
| |
|
| | |
| | var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567") |
| |
|
| | |
| | |
| | var HexEncoding = NewEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUV") |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | func (enc Encoding) WithPadding(padding rune) *Encoding { |
| | switch { |
| | case padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff: |
| | panic("invalid padding") |
| | case padding != NoPadding && enc.decodeMap[byte(padding)] != invalidIndex: |
| | panic("padding contained in alphabet") |
| | } |
| | enc.padChar = padding |
| | return &enc |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | func (enc *Encoding) Encode(dst, src []byte) { |
| | if len(src) == 0 { |
| | return |
| | } |
| | |
| | |
| | |
| | _ = enc.encode |
| |
|
| | di, si := 0, 0 |
| | n := (len(src) / 5) * 5 |
| | for si < n { |
| | |
| | |
| | hi := uint32(src[si+0])<<24 | uint32(src[si+1])<<16 | uint32(src[si+2])<<8 | uint32(src[si+3]) |
| | lo := hi<<8 | uint32(src[si+4]) |
| |
|
| | dst[di+0] = enc.encode[(hi>>27)&0x1F] |
| | dst[di+1] = enc.encode[(hi>>22)&0x1F] |
| | dst[di+2] = enc.encode[(hi>>17)&0x1F] |
| | dst[di+3] = enc.encode[(hi>>12)&0x1F] |
| | dst[di+4] = enc.encode[(hi>>7)&0x1F] |
| | dst[di+5] = enc.encode[(hi>>2)&0x1F] |
| | dst[di+6] = enc.encode[(lo>>5)&0x1F] |
| | dst[di+7] = enc.encode[(lo)&0x1F] |
| |
|
| | si += 5 |
| | di += 8 |
| | } |
| |
|
| | |
| | remain := len(src) - si |
| | if remain == 0 { |
| | return |
| | } |
| |
|
| | |
| | val := uint32(0) |
| | switch remain { |
| | case 4: |
| | val |= uint32(src[si+3]) |
| | dst[di+6] = enc.encode[val<<3&0x1F] |
| | dst[di+5] = enc.encode[val>>2&0x1F] |
| | fallthrough |
| | case 3: |
| | val |= uint32(src[si+2]) << 8 |
| | dst[di+4] = enc.encode[val>>7&0x1F] |
| | fallthrough |
| | case 2: |
| | val |= uint32(src[si+1]) << 16 |
| | dst[di+3] = enc.encode[val>>12&0x1F] |
| | dst[di+2] = enc.encode[val>>17&0x1F] |
| | fallthrough |
| | case 1: |
| | val |= uint32(src[si+0]) << 24 |
| | dst[di+1] = enc.encode[val>>22&0x1F] |
| | dst[di+0] = enc.encode[val>>27&0x1F] |
| | } |
| |
|
| | |
| | if enc.padChar != NoPadding { |
| | nPad := (remain * 8 / 5) + 1 |
| | for i := nPad; i < 8; i++ { |
| | dst[di+i] = byte(enc.padChar) |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | func (enc *Encoding) AppendEncode(dst, src []byte) []byte { |
| | n := enc.EncodedLen(len(src)) |
| | dst = slices.Grow(dst, n) |
| | enc.Encode(dst[len(dst):][:n], src) |
| | return dst[:len(dst)+n] |
| | } |
| |
|
| | |
| | func (enc *Encoding) EncodeToString(src []byte) string { |
| | buf := make([]byte, enc.EncodedLen(len(src))) |
| | enc.Encode(buf, src) |
| | return string(buf) |
| | } |
| |
|
| | type encoder struct { |
| | err error |
| | enc *Encoding |
| | w io.Writer |
| | buf [5]byte |
| | nbuf int |
| | out [1024]byte |
| | } |
| |
|
| | func (e *encoder) Write(p []byte) (n int, err error) { |
| | if e.err != nil { |
| | return 0, e.err |
| | } |
| |
|
| | |
| | if e.nbuf > 0 { |
| | var i int |
| | for i = 0; i < len(p) && e.nbuf < 5; i++ { |
| | e.buf[e.nbuf] = p[i] |
| | e.nbuf++ |
| | } |
| | n += i |
| | p = p[i:] |
| | if e.nbuf < 5 { |
| | return |
| | } |
| | e.enc.Encode(e.out[0:], e.buf[0:]) |
| | if _, e.err = e.w.Write(e.out[0:8]); e.err != nil { |
| | return n, e.err |
| | } |
| | e.nbuf = 0 |
| | } |
| |
|
| | |
| | for len(p) >= 5 { |
| | nn := len(e.out) / 8 * 5 |
| | if nn > len(p) { |
| | nn = len(p) |
| | nn -= nn % 5 |
| | } |
| | e.enc.Encode(e.out[0:], p[0:nn]) |
| | if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil { |
| | return n, e.err |
| | } |
| | n += nn |
| | p = p[nn:] |
| | } |
| |
|
| | |
| | copy(e.buf[:], p) |
| | e.nbuf = len(p) |
| | n += len(p) |
| | return |
| | } |
| |
|
| | |
| | |
| | func (e *encoder) Close() error { |
| | |
| | if e.err == nil && e.nbuf > 0 { |
| | e.enc.Encode(e.out[0:], e.buf[0:e.nbuf]) |
| | encodedLen := e.enc.EncodedLen(e.nbuf) |
| | e.nbuf = 0 |
| | _, e.err = e.w.Write(e.out[0:encodedLen]) |
| | } |
| | return e.err |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser { |
| | return &encoder{enc: enc, w: w} |
| | } |
| |
|
| | |
| | |
| | func (enc *Encoding) EncodedLen(n int) int { |
| | if enc.padChar == NoPadding { |
| | return n/5*8 + (n%5*8+4)/5 |
| | } |
| | return (n + 4) / 5 * 8 |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | type CorruptInputError int64 |
| |
|
| | func (e CorruptInputError) Error() string { |
| | return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { |
| | |
| | _ = enc.decodeMap |
| |
|
| | dsti := 0 |
| | olen := len(src) |
| |
|
| | for len(src) > 0 && !end { |
| | |
| | var dbuf [8]byte |
| | dlen := 8 |
| |
|
| | for j := 0; j < 8; { |
| |
|
| | if len(src) == 0 { |
| | if enc.padChar != NoPadding { |
| | |
| | return n, false, CorruptInputError(olen - len(src) - j) |
| | } |
| | |
| | dlen, end = j, true |
| | break |
| | } |
| | in := src[0] |
| | src = src[1:] |
| | if in == byte(enc.padChar) && j >= 2 && len(src) < 8 { |
| | |
| | if len(src)+j < 8-1 { |
| | |
| | return n, false, CorruptInputError(olen) |
| | } |
| | for k := 0; k < 8-1-j; k++ { |
| | if len(src) > k && src[k] != byte(enc.padChar) { |
| | |
| | return n, false, CorruptInputError(olen - len(src) + k - 1) |
| | } |
| | } |
| | dlen, end = j, true |
| | |
| | |
| | |
| | |
| | |
| | if dlen == 1 || dlen == 3 || dlen == 6 { |
| | return n, false, CorruptInputError(olen - len(src) - 1) |
| | } |
| | break |
| | } |
| | dbuf[j] = enc.decodeMap[in] |
| | if dbuf[j] == 0xFF { |
| | return n, false, CorruptInputError(olen - len(src) - 1) |
| | } |
| | j++ |
| | } |
| |
|
| | |
| | |
| | switch dlen { |
| | case 8: |
| | dst[dsti+4] = dbuf[6]<<5 | dbuf[7] |
| | n++ |
| | fallthrough |
| | case 7: |
| | dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3 |
| | n++ |
| | fallthrough |
| | case 5: |
| | dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1 |
| | n++ |
| | fallthrough |
| | case 4: |
| | dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4 |
| | n++ |
| | fallthrough |
| | case 2: |
| | dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2 |
| | n++ |
| | } |
| | dsti += 5 |
| | } |
| | return n, end, nil |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | func (enc *Encoding) Decode(dst, src []byte) (n int, err error) { |
| | buf := make([]byte, len(src)) |
| | l := stripNewlines(buf, src) |
| | n, _, err = enc.decode(dst, buf[:l]) |
| | return |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) { |
| | |
| | n := len(src) |
| | for n > 0 && rune(src[n-1]) == enc.padChar { |
| | n-- |
| | } |
| | n = decodedLen(n, NoPadding) |
| |
|
| | dst = slices.Grow(dst, n) |
| | n, err := enc.Decode(dst[len(dst):][:n], src) |
| | return dst[:len(dst)+n], err |
| | } |
| |
|
| | |
| | |
| | |
| | func (enc *Encoding) DecodeString(s string) ([]byte, error) { |
| | buf := []byte(s) |
| | l := stripNewlines(buf, buf) |
| | n, _, err := enc.decode(buf, buf[:l]) |
| | return buf[:n], err |
| | } |
| |
|
| | type decoder struct { |
| | err error |
| | enc *Encoding |
| | r io.Reader |
| | end bool |
| | buf [1024]byte |
| | nbuf int |
| | out []byte |
| | outbuf [1024 / 8 * 5]byte |
| | } |
| |
|
| | func readEncodedData(r io.Reader, buf []byte, min int, expectsPadding bool) (n int, err error) { |
| | for n < min && err == nil { |
| | var nn int |
| | nn, err = r.Read(buf[n:]) |
| | n += nn |
| | } |
| | |
| | if n < min && n > 0 && err == io.EOF { |
| | err = io.ErrUnexpectedEOF |
| | } |
| | |
| | |
| | |
| | if expectsPadding && min < 8 && n == 0 && err == io.EOF { |
| | err = io.ErrUnexpectedEOF |
| | } |
| | return |
| | } |
| |
|
| | func (d *decoder) Read(p []byte) (n int, err error) { |
| | |
| | if len(d.out) > 0 { |
| | n = copy(p, d.out) |
| | d.out = d.out[n:] |
| | if len(d.out) == 0 { |
| | return n, d.err |
| | } |
| | return n, nil |
| | } |
| |
|
| | if d.err != nil { |
| | return 0, d.err |
| | } |
| |
|
| | |
| | nn := (len(p) + 4) / 5 * 8 |
| | if nn < 8 { |
| | nn = 8 |
| | } |
| | if nn > len(d.buf) { |
| | nn = len(d.buf) |
| | } |
| |
|
| | |
| | var min int |
| | var expectsPadding bool |
| | if d.enc.padChar == NoPadding { |
| | min = 1 |
| | expectsPadding = false |
| | } else { |
| | min = 8 - d.nbuf |
| | expectsPadding = true |
| | } |
| |
|
| | nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], min, expectsPadding) |
| | d.nbuf += nn |
| | if d.nbuf < min { |
| | return 0, d.err |
| | } |
| | if nn > 0 && d.end { |
| | return 0, CorruptInputError(0) |
| | } |
| |
|
| | |
| | var nr int |
| | if d.enc.padChar == NoPadding { |
| | nr = d.nbuf |
| | } else { |
| | nr = d.nbuf / 8 * 8 |
| | } |
| | nw := d.enc.DecodedLen(d.nbuf) |
| |
|
| | if nw > len(p) { |
| | nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr]) |
| | d.out = d.outbuf[0:nw] |
| | n = copy(p, d.out) |
| | d.out = d.out[n:] |
| | } else { |
| | n, d.end, err = d.enc.decode(p, d.buf[0:nr]) |
| | } |
| | d.nbuf -= nr |
| | for i := 0; i < d.nbuf; i++ { |
| | d.buf[i] = d.buf[i+nr] |
| | } |
| |
|
| | if err != nil && (d.err == nil || d.err == io.EOF) { |
| | d.err = err |
| | } |
| |
|
| | if len(d.out) > 0 { |
| | |
| | |
| | |
| | |
| | return n, nil |
| | } |
| |
|
| | return n, d.err |
| | } |
| |
|
| | type newlineFilteringReader struct { |
| | wrapped io.Reader |
| | } |
| |
|
| | |
| | |
| | func stripNewlines(dst, src []byte) int { |
| | offset := 0 |
| | for _, b := range src { |
| | if b == '\r' || b == '\n' { |
| | continue |
| | } |
| | dst[offset] = b |
| | offset++ |
| | } |
| | return offset |
| | } |
| |
|
| | func (r *newlineFilteringReader) Read(p []byte) (int, error) { |
| | n, err := r.wrapped.Read(p) |
| | for n > 0 { |
| | s := p[0:n] |
| | offset := stripNewlines(s, s) |
| | if err != nil || offset > 0 { |
| | return offset, err |
| | } |
| | |
| | n, err = r.wrapped.Read(p) |
| | } |
| | return n, err |
| | } |
| |
|
| | |
| | func NewDecoder(enc *Encoding, r io.Reader) io.Reader { |
| | return &decoder{enc: enc, r: &newlineFilteringReader{r}} |
| | } |
| |
|
| | |
| | |
| | func (enc *Encoding) DecodedLen(n int) int { |
| | return decodedLen(n, enc.padChar) |
| | } |
| |
|
| | func decodedLen(n int, padChar rune) int { |
| | if padChar == NoPadding { |
| | return n/8*5 + n%8*5/8 |
| | } |
| | return n / 8 * 5 |
| | } |
| |
|