github.com/likebike/go--@v0.0.0-20190911215757-0bd925d16e96/go/src/encoding/base32/base32.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package base32 implements base32 encoding as specified by RFC 4648.
     6  package base32
     7  
     8  import (
     9  	"bytes"
    10  	"io"
    11  	"strconv"
    12  	"strings"
    13  )
    14  
    15  /*
    16   * Encodings
    17   */
    18  
    19  // An Encoding is a radix 32 encoding/decoding scheme, defined by a
    20  // 32-character alphabet. The most common is the "base32" encoding
    21  // introduced for SASL GSSAPI and standardized in RFC 4648.
    22  // The alternate "base32hex" encoding is used in DNSSEC.
    23  type Encoding struct {
    24  	encode    string
    25  	decodeMap [256]byte
    26  	padChar   rune
    27  }
    28  
    29  const (
    30  	StdPadding rune = '=' // Standard padding character
    31  	NoPadding  rune = -1  // No padding
    32  )
    33  
    34  const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
    35  const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
    36  
    37  // NewEncoding returns a new Encoding defined by the given alphabet,
    38  // which must be a 32-byte string.
    39  func NewEncoding(encoder string) *Encoding {
    40  	e := new(Encoding)
    41  	e.encode = encoder
    42  	e.padChar = StdPadding
    43  
    44  	for i := 0; i < len(e.decodeMap); i++ {
    45  		e.decodeMap[i] = 0xFF
    46  	}
    47  	for i := 0; i < len(encoder); i++ {
    48  		e.decodeMap[encoder[i]] = byte(i)
    49  	}
    50  	return e
    51  }
    52  
    53  // StdEncoding is the standard base32 encoding, as defined in
    54  // RFC 4648.
    55  var StdEncoding = NewEncoding(encodeStd)
    56  
    57  // HexEncoding is the ``Extended Hex Alphabet'' defined in RFC 4648.
    58  // It is typically used in DNS.
    59  var HexEncoding = NewEncoding(encodeHex)
    60  
    61  var removeNewlinesMapper = func(r rune) rune {
    62  	if r == '\r' || r == '\n' {
    63  		return -1
    64  	}
    65  	return r
    66  }
    67  
    68  // WithPadding creates a new encoding identical to enc except
    69  // with a specified padding character, or NoPadding to disable padding.
    70  // The padding character must not be '\r' or '\n', must not
    71  // be contained in the encoding's alphabet and must be a rune equal or
    72  // below '\xff'.
    73  func (enc Encoding) WithPadding(padding rune) *Encoding {
    74  	if padding == '\r' || padding == '\n' || padding > 0xff {
    75  		panic("invalid padding")
    76  	}
    77  
    78  	for i := 0; i < len(enc.encode); i++ {
    79  		if rune(enc.encode[i]) == padding {
    80  			panic("padding contained in alphabet")
    81  		}
    82  	}
    83  
    84  	enc.padChar = padding
    85  	return &enc
    86  }
    87  
    88  /*
    89   * Encoder
    90   */
    91  
    92  // Encode encodes src using the encoding enc, writing
    93  // EncodedLen(len(src)) bytes to dst.
    94  //
    95  // The encoding pads the output to a multiple of 8 bytes,
    96  // so Encode is not appropriate for use on individual blocks
    97  // of a large data stream. Use NewEncoder() instead.
    98  func (enc *Encoding) Encode(dst, src []byte) {
    99  	if len(src) == 0 {
   100  		return
   101  	}
   102  
   103  	for len(src) > 0 {
   104  		var b [8]byte
   105  
   106  		// Unpack 8x 5-bit source blocks into a 5 byte
   107  		// destination quantum
   108  		switch len(src) {
   109  		default:
   110  			b[7] = src[4] & 0x1F
   111  			b[6] = src[4] >> 5
   112  			fallthrough
   113  		case 4:
   114  			b[6] |= (src[3] << 3) & 0x1F
   115  			b[5] = (src[3] >> 2) & 0x1F
   116  			b[4] = src[3] >> 7
   117  			fallthrough
   118  		case 3:
   119  			b[4] |= (src[2] << 1) & 0x1F
   120  			b[3] = (src[2] >> 4) & 0x1F
   121  			fallthrough
   122  		case 2:
   123  			b[3] |= (src[1] << 4) & 0x1F
   124  			b[2] = (src[1] >> 1) & 0x1F
   125  			b[1] = (src[1] >> 6) & 0x1F
   126  			fallthrough
   127  		case 1:
   128  			b[1] |= (src[0] << 2) & 0x1F
   129  			b[0] = src[0] >> 3
   130  		}
   131  
   132  		// Encode 5-bit blocks using the base32 alphabet
   133  		size := len(dst)
   134  		if size >= 8 {
   135  			// Common case, unrolled for extra performance
   136  			dst[0] = enc.encode[b[0]]
   137  			dst[1] = enc.encode[b[1]]
   138  			dst[2] = enc.encode[b[2]]
   139  			dst[3] = enc.encode[b[3]]
   140  			dst[4] = enc.encode[b[4]]
   141  			dst[5] = enc.encode[b[5]]
   142  			dst[6] = enc.encode[b[6]]
   143  			dst[7] = enc.encode[b[7]]
   144  		} else {
   145  			for i := 0; i < size; i++ {
   146  				dst[i] = enc.encode[b[i]]
   147  			}
   148  		}
   149  
   150  		// Pad the final quantum
   151  		if len(src) < 5 {
   152  			if enc.padChar == NoPadding {
   153  				break
   154  			}
   155  
   156  			dst[7] = byte(enc.padChar)
   157  			if len(src) < 4 {
   158  				dst[6] = byte(enc.padChar)
   159  				dst[5] = byte(enc.padChar)
   160  				if len(src) < 3 {
   161  					dst[4] = byte(enc.padChar)
   162  					if len(src) < 2 {
   163  						dst[3] = byte(enc.padChar)
   164  						dst[2] = byte(enc.padChar)
   165  					}
   166  				}
   167  			}
   168  
   169  			break
   170  		}
   171  
   172  		src = src[5:]
   173  		dst = dst[8:]
   174  	}
   175  }
   176  
   177  // EncodeToString returns the base32 encoding of src.
   178  func (enc *Encoding) EncodeToString(src []byte) string {
   179  	buf := make([]byte, enc.EncodedLen(len(src)))
   180  	enc.Encode(buf, src)
   181  	return string(buf)
   182  }
   183  
   184  type encoder struct {
   185  	err  error
   186  	enc  *Encoding
   187  	w    io.Writer
   188  	buf  [5]byte    // buffered data waiting to be encoded
   189  	nbuf int        // number of bytes in buf
   190  	out  [1024]byte // output buffer
   191  }
   192  
   193  func (e *encoder) Write(p []byte) (n int, err error) {
   194  	if e.err != nil {
   195  		return 0, e.err
   196  	}
   197  
   198  	// Leading fringe.
   199  	if e.nbuf > 0 {
   200  		var i int
   201  		for i = 0; i < len(p) && e.nbuf < 5; i++ {
   202  			e.buf[e.nbuf] = p[i]
   203  			e.nbuf++
   204  		}
   205  		n += i
   206  		p = p[i:]
   207  		if e.nbuf < 5 {
   208  			return
   209  		}
   210  		e.enc.Encode(e.out[0:], e.buf[0:])
   211  		if _, e.err = e.w.Write(e.out[0:8]); e.err != nil {
   212  			return n, e.err
   213  		}
   214  		e.nbuf = 0
   215  	}
   216  
   217  	// Large interior chunks.
   218  	for len(p) >= 5 {
   219  		nn := len(e.out) / 8 * 5
   220  		if nn > len(p) {
   221  			nn = len(p)
   222  			nn -= nn % 5
   223  		}
   224  		e.enc.Encode(e.out[0:], p[0:nn])
   225  		if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil {
   226  			return n, e.err
   227  		}
   228  		n += nn
   229  		p = p[nn:]
   230  	}
   231  
   232  	// Trailing fringe.
   233  	for i := 0; i < len(p); i++ {
   234  		e.buf[i] = p[i]
   235  	}
   236  	e.nbuf = len(p)
   237  	n += len(p)
   238  	return
   239  }
   240  
   241  // Close flushes any pending output from the encoder.
   242  // It is an error to call Write after calling Close.
   243  func (e *encoder) Close() error {
   244  	// If there's anything left in the buffer, flush it out
   245  	if e.err == nil && e.nbuf > 0 {
   246  		e.enc.Encode(e.out[0:], e.buf[0:e.nbuf])
   247  		e.nbuf = 0
   248  		_, e.err = e.w.Write(e.out[0:8])
   249  	}
   250  	return e.err
   251  }
   252  
   253  // NewEncoder returns a new base32 stream encoder. Data written to
   254  // the returned writer will be encoded using enc and then written to w.
   255  // Base32 encodings operate in 5-byte blocks; when finished
   256  // writing, the caller must Close the returned encoder to flush any
   257  // partially written blocks.
   258  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   259  	return &encoder{enc: enc, w: w}
   260  }
   261  
   262  // EncodedLen returns the length in bytes of the base32 encoding
   263  // of an input buffer of length n.
   264  func (enc *Encoding) EncodedLen(n int) int {
   265  	if enc.padChar == NoPadding {
   266  		return (n*8 + 4) / 5
   267  	}
   268  	return (n + 4) / 5 * 8
   269  }
   270  
   271  /*
   272   * Decoder
   273   */
   274  
   275  type CorruptInputError int64
   276  
   277  func (e CorruptInputError) Error() string {
   278  	return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10)
   279  }
   280  
   281  // decode is like Decode but returns an additional 'end' value, which
   282  // indicates if end-of-message padding was encountered and thus any
   283  // additional data is an error. This method assumes that src has been
   284  // stripped of all supported whitespace ('\r' and '\n').
   285  func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
   286  	olen := len(src)
   287  	for len(src) > 0 && !end {
   288  		// Decode quantum using the base32 alphabet
   289  		var dbuf [8]byte
   290  		dlen := 8
   291  
   292  		for j := 0; j < 8; {
   293  
   294  			// We have reached the end and are missing padding
   295  			if len(src) == 0 && enc.padChar != NoPadding {
   296  				return n, false, CorruptInputError(olen - len(src) - j)
   297  			}
   298  
   299  			// We have reached the end and are not expecing any padding
   300  			if len(src) == 0 && enc.padChar == NoPadding {
   301  				dlen, end = j, true
   302  				break
   303  			}
   304  
   305  			in := src[0]
   306  			src = src[1:]
   307  			if in == byte(enc.padChar) && j >= 2 && len(src) < 8 {
   308  				// We've reached the end and there's padding
   309  				if len(src)+j < 8-1 {
   310  					// not enough padding
   311  					return n, false, CorruptInputError(olen)
   312  				}
   313  				for k := 0; k < 8-1-j; k++ {
   314  					if len(src) > k && src[k] != byte(enc.padChar) {
   315  						// incorrect padding
   316  						return n, false, CorruptInputError(olen - len(src) + k - 1)
   317  					}
   318  				}
   319  				dlen, end = j, true
   320  				// 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not
   321  				// valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing
   322  				// the five valid padding lengths, and Section 9 "Illustrations and
   323  				// Examples" for an illustration for how the 1st, 3rd and 6th base32
   324  				// src bytes do not yield enough information to decode a dst byte.
   325  				if dlen == 1 || dlen == 3 || dlen == 6 {
   326  					return n, false, CorruptInputError(olen - len(src) - 1)
   327  				}
   328  				break
   329  			}
   330  			dbuf[j] = enc.decodeMap[in]
   331  			if dbuf[j] == 0xFF {
   332  				return n, false, CorruptInputError(olen - len(src) - 1)
   333  			}
   334  			j++
   335  		}
   336  
   337  		// Pack 8x 5-bit source blocks into 5 byte destination
   338  		// quantum
   339  		switch dlen {
   340  		case 8:
   341  			dst[4] = dbuf[6]<<5 | dbuf[7]
   342  			fallthrough
   343  		case 7:
   344  			dst[3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
   345  			fallthrough
   346  		case 5:
   347  			dst[2] = dbuf[3]<<4 | dbuf[4]>>1
   348  			fallthrough
   349  		case 4:
   350  			dst[1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
   351  			fallthrough
   352  		case 2:
   353  			dst[0] = dbuf[0]<<3 | dbuf[1]>>2
   354  		}
   355  
   356  		if !end {
   357  			dst = dst[5:]
   358  		}
   359  
   360  		switch dlen {
   361  		case 2:
   362  			n += 1
   363  		case 4:
   364  			n += 2
   365  		case 5:
   366  			n += 3
   367  		case 7:
   368  			n += 4
   369  		case 8:
   370  			n += 5
   371  		}
   372  	}
   373  	return n, end, nil
   374  }
   375  
   376  // Decode decodes src using the encoding enc. It writes at most
   377  // DecodedLen(len(src)) bytes to dst and returns the number of bytes
   378  // written. If src contains invalid base32 data, it will return the
   379  // number of bytes successfully written and CorruptInputError.
   380  // New line characters (\r and \n) are ignored.
   381  func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
   382  	src = bytes.Map(removeNewlinesMapper, src)
   383  	n, _, err = enc.decode(dst, src)
   384  	return
   385  }
   386  
   387  // DecodeString returns the bytes represented by the base32 string s.
   388  func (enc *Encoding) DecodeString(s string) ([]byte, error) {
   389  	s = strings.Map(removeNewlinesMapper, s)
   390  	dbuf := make([]byte, enc.DecodedLen(len(s)))
   391  	n, _, err := enc.decode(dbuf, []byte(s))
   392  	return dbuf[:n], err
   393  }
   394  
   395  type decoder struct {
   396  	err    error
   397  	enc    *Encoding
   398  	r      io.Reader
   399  	end    bool       // saw end of message
   400  	buf    [1024]byte // leftover input
   401  	nbuf   int
   402  	out    []byte // leftover decoded output
   403  	outbuf [1024 / 8 * 5]byte
   404  }
   405  
   406  func readEncodedData(r io.Reader, buf []byte, min int) (n int, err error) {
   407  	for n < min && err == nil {
   408  		var nn int
   409  		nn, err = r.Read(buf[n:])
   410  		n += nn
   411  	}
   412  	if n < min && n > 0 && err == io.EOF {
   413  		err = io.ErrUnexpectedEOF
   414  	}
   415  	return
   416  }
   417  
   418  func (d *decoder) Read(p []byte) (n int, err error) {
   419  	// Use leftover decoded output from last read.
   420  	if len(d.out) > 0 {
   421  		n = copy(p, d.out)
   422  		d.out = d.out[n:]
   423  		if len(d.out) == 0 {
   424  			return n, d.err
   425  		}
   426  		return n, nil
   427  	}
   428  
   429  	if d.err != nil {
   430  		return 0, d.err
   431  	}
   432  
   433  	// Read a chunk.
   434  	nn := len(p) / 5 * 8
   435  	if nn < 8 {
   436  		nn = 8
   437  	}
   438  	if nn > len(d.buf) {
   439  		nn = len(d.buf)
   440  	}
   441  
   442  	nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], 8-d.nbuf)
   443  	d.nbuf += nn
   444  	if d.nbuf < 8 {
   445  		return 0, d.err
   446  	}
   447  
   448  	// Decode chunk into p, or d.out and then p if p is too small.
   449  	nr := d.nbuf / 8 * 8
   450  	nw := d.nbuf / 8 * 5
   451  	if nw > len(p) {
   452  		nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr])
   453  		d.out = d.outbuf[0:nw]
   454  		n = copy(p, d.out)
   455  		d.out = d.out[n:]
   456  	} else {
   457  		n, d.end, err = d.enc.decode(p, d.buf[0:nr])
   458  	}
   459  	d.nbuf -= nr
   460  	for i := 0; i < d.nbuf; i++ {
   461  		d.buf[i] = d.buf[i+nr]
   462  	}
   463  
   464  	if err != nil && (d.err == nil || d.err == io.EOF) {
   465  		d.err = err
   466  	}
   467  
   468  	if len(d.out) > 0 {
   469  		// We cannot return all the decoded bytes to the caller in this
   470  		// invocation of Read, so we return a nil error to ensure that Read
   471  		// will be called again.  The error stored in d.err, if any, will be
   472  		// returned with the last set of decoded bytes.
   473  		return n, nil
   474  	}
   475  
   476  	return n, d.err
   477  }
   478  
   479  type newlineFilteringReader struct {
   480  	wrapped io.Reader
   481  }
   482  
   483  func (r *newlineFilteringReader) Read(p []byte) (int, error) {
   484  	n, err := r.wrapped.Read(p)
   485  	for n > 0 {
   486  		offset := 0
   487  		for i, b := range p[0:n] {
   488  			if b != '\r' && b != '\n' {
   489  				if i != offset {
   490  					p[offset] = b
   491  				}
   492  				offset++
   493  			}
   494  		}
   495  		if err != nil || offset > 0 {
   496  			return offset, err
   497  		}
   498  		// Previous buffer entirely whitespace, read again
   499  		n, err = r.wrapped.Read(p)
   500  	}
   501  	return n, err
   502  }
   503  
   504  // NewDecoder constructs a new base32 stream decoder.
   505  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   506  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   507  }
   508  
   509  // DecodedLen returns the maximum length in bytes of the decoded data
   510  // corresponding to n bytes of base32-encoded data.
   511  func (enc *Encoding) DecodedLen(n int) int {
   512  	if enc.padChar == NoPadding {
   513  		return n * 5 / 8
   514  	}
   515  
   516  	return n / 8 * 5
   517  }