Source file src/encoding/base32/base32.go

     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  	"io"
    10  	"strconv"
    11  )
    12  
    13  /*
    14   * Encodings
    15   */
    16  
    17  // An Encoding is a radix 32 encoding/decoding scheme, defined by a
    18  // 32-character alphabet. The most common is the "base32" encoding
    19  // introduced for SASL GSSAPI and standardized in RFC 4648.
    20  // The alternate "base32hex" encoding is used in DNSSEC.
    21  type Encoding struct {
    22  	encode    [32]byte
    23  	decodeMap [256]byte
    24  	padChar   rune
    25  }
    26  
    27  const (
    28  	StdPadding rune = '=' // Standard padding character
    29  	NoPadding  rune = -1  // No padding
    30  )
    31  
    32  const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
    33  const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
    34  
    35  // NewEncoding returns a new Encoding defined by the given alphabet,
    36  // which must be a 32-byte string.
    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  // StdEncoding is the standard base32 encoding, as defined in
    56  // RFC 4648.
    57  var StdEncoding = NewEncoding(encodeStd)
    58  
    59  // HexEncoding is the “Extended Hex Alphabet” defined in RFC 4648.
    60  // It is typically used in DNS.
    61  var HexEncoding = NewEncoding(encodeHex)
    62  
    63  // WithPadding creates a new encoding identical to enc except
    64  // with a specified padding character, or NoPadding to disable padding.
    65  // The padding character must not be '\r' or '\n', must not
    66  // be contained in the encoding's alphabet and must be a rune equal or
    67  // below '\xff'.
    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  /*
    84   * Encoder
    85   */
    86  
    87  // Encode encodes src using the encoding enc, writing
    88  // EncodedLen(len(src)) bytes to dst.
    89  //
    90  // The encoding pads the output to a multiple of 8 bytes,
    91  // so Encode is not appropriate for use on individual blocks
    92  // of a large data stream. Use NewEncoder() instead.
    93  func (enc *Encoding) Encode(dst, src []byte) {
    94  	for len(src) > 0 {
    95  		var b [8]byte
    96  
    97  		// Unpack 8x 5-bit source blocks into a 5 byte
    98  		// destination quantum
    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  		// Encode 5-bit blocks using the base32 alphabet
   124  		size := len(dst)
   125  		if size >= 8 {
   126  			// Common case, unrolled for extra performance
   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  		// Pad the final quantum
   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  // EncodeToString returns the base32 encoding of src.
   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    // buffered data waiting to be encoded
   180  	nbuf int        // number of bytes in buf
   181  	out  [1024]byte // output buffer
   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  	// Leading fringe.
   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  	// Large interior chunks.
   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  	// Trailing fringe.
   224  	copy(e.buf[:], p)
   225  	e.nbuf = len(p)
   226  	n += len(p)
   227  	return
   228  }
   229  
   230  // Close flushes any pending output from the encoder.
   231  // It is an error to call Write after calling Close.
   232  func (e *encoder) Close() error {
   233  	// If there's anything left in the buffer, flush it out
   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  // NewEncoder returns a new base32 stream encoder. Data written to
   244  // the returned writer will be encoded using enc and then written to w.
   245  // Base32 encodings operate in 5-byte blocks; when finished
   246  // writing, the caller must Close the returned encoder to flush any
   247  // partially written blocks.
   248  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   249  	return &encoder{enc: enc, w: w}
   250  }
   251  
   252  // EncodedLen returns the length in bytes of the base32 encoding
   253  // of an input buffer of length n.
   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  /*
   262   * Decoder
   263   */
   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  // decode is like Decode but returns an additional 'end' value, which
   272  // indicates if end-of-message padding was encountered and thus any
   273  // additional data is an error. This method assumes that src has been
   274  // stripped of all supported whitespace ('\r' and '\n').
   275  func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
   276  	// Lift the nil check outside of the loop.
   277  	_ = enc.decodeMap
   278  
   279  	dsti := 0
   280  	olen := len(src)
   281  
   282  	for len(src) > 0 && !end {
   283  		// Decode quantum using the base32 alphabet
   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  					// We have reached the end and are missing padding
   292  					return n, false, CorruptInputError(olen - len(src) - j)
   293  				}
   294  				// We have reached the end and are not expecting any padding
   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  				// We've reached the end and there's padding
   302  				if len(src)+j < 8-1 {
   303  					// not enough padding
   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  						// incorrect padding
   309  						return n, false, CorruptInputError(olen - len(src) + k - 1)
   310  					}
   311  				}
   312  				dlen, end = j, true
   313  				// 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not
   314  				// valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing
   315  				// the five valid padding lengths, and Section 9 "Illustrations and
   316  				// Examples" for an illustration for how the 1st, 3rd and 6th base32
   317  				// src bytes do not yield enough information to decode a dst byte.
   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  		// Pack 8x 5-bit source blocks into 5 byte destination
   331  		// quantum
   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  // Decode decodes src using the encoding enc. It writes at most
   359  // DecodedLen(len(src)) bytes to dst and returns the number of bytes
   360  // written. If src contains invalid base32 data, it will return the
   361  // number of bytes successfully written and CorruptInputError.
   362  // New line characters (\r and \n) are ignored.
   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  // DecodeString returns the bytes represented by the base32 string s.
   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       // saw end of message
   383  	buf    [1024]byte // leftover input
   384  	nbuf   int
   385  	out    []byte // leftover decoded output
   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  	// data was read, less than min bytes could be read
   396  	if n < min && n > 0 && err == io.EOF {
   397  		err = io.ErrUnexpectedEOF
   398  	}
   399  	// no data was read, the buffer already contains some data
   400  	// when padding is disabled this is not an error, as the message can be of
   401  	// any length
   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  	// Use leftover decoded output from last read.
   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  	// Read a chunk.
   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  	// Minimum amount of bytes that needs to be read each cycle
   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  	if nn > 0 && d.end {
   449  		return 0, CorruptInputError(0)
   450  	}
   451  
   452  	// Decode chunk into p, or d.out and then p if p is too small.
   453  	var nr int
   454  	if d.enc.padChar == NoPadding {
   455  		nr = d.nbuf
   456  	} else {
   457  		nr = d.nbuf / 8 * 8
   458  	}
   459  	nw := d.enc.DecodedLen(d.nbuf)
   460  
   461  	if nw > len(p) {
   462  		nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr])
   463  		d.out = d.outbuf[0:nw]
   464  		n = copy(p, d.out)
   465  		d.out = d.out[n:]
   466  	} else {
   467  		n, d.end, err = d.enc.decode(p, d.buf[0:nr])
   468  	}
   469  	d.nbuf -= nr
   470  	for i := 0; i < d.nbuf; i++ {
   471  		d.buf[i] = d.buf[i+nr]
   472  	}
   473  
   474  	if err != nil && (d.err == nil || d.err == io.EOF) {
   475  		d.err = err
   476  	}
   477  
   478  	if len(d.out) > 0 {
   479  		// We cannot return all the decoded bytes to the caller in this
   480  		// invocation of Read, so we return a nil error to ensure that Read
   481  		// will be called again.  The error stored in d.err, if any, will be
   482  		// returned with the last set of decoded bytes.
   483  		return n, nil
   484  	}
   485  
   486  	return n, d.err
   487  }
   488  
   489  type newlineFilteringReader struct {
   490  	wrapped io.Reader
   491  }
   492  
   493  // stripNewlines removes newline characters and returns the number
   494  // of non-newline characters copied to dst.
   495  func stripNewlines(dst, src []byte) int {
   496  	offset := 0
   497  	for _, b := range src {
   498  		if b == '\r' || b == '\n' {
   499  			continue
   500  		}
   501  		dst[offset] = b
   502  		offset++
   503  	}
   504  	return offset
   505  }
   506  
   507  func (r *newlineFilteringReader) Read(p []byte) (int, error) {
   508  	n, err := r.wrapped.Read(p)
   509  	for n > 0 {
   510  		s := p[0:n]
   511  		offset := stripNewlines(s, s)
   512  		if err != nil || offset > 0 {
   513  			return offset, err
   514  		}
   515  		// Previous buffer entirely whitespace, read again
   516  		n, err = r.wrapped.Read(p)
   517  	}
   518  	return n, err
   519  }
   520  
   521  // NewDecoder constructs a new base32 stream decoder.
   522  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   523  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   524  }
   525  
   526  // DecodedLen returns the maximum length in bytes of the decoded data
   527  // corresponding to n bytes of base32-encoded data.
   528  func (enc *Encoding) DecodedLen(n int) int {
   529  	if enc.padChar == NoPadding {
   530  		return n * 5 / 8
   531  	}
   532  
   533  	return n / 8 * 5
   534  }
   535  

View as plain text