Source file src/encoding/base32/base32.go

Documentation: encoding/base32

     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  	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  // Close flushes any pending output from the encoder.
   233  // It is an error to call Write after calling Close.
   234  func (e *encoder) Close() error {
   235  	// If there's anything left in the buffer, flush it out
   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  // NewEncoder returns a new base32 stream encoder. Data written to
   246  // the returned writer will be encoded using enc and then written to w.
   247  // Base32 encodings operate in 5-byte blocks; when finished
   248  // writing, the caller must Close the returned encoder to flush any
   249  // partially written blocks.
   250  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   251  	return &encoder{enc: enc, w: w}
   252  }
   253  
   254  // EncodedLen returns the length in bytes of the base32 encoding
   255  // of an input buffer of length n.
   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  /*
   264   * Decoder
   265   */
   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  // decode is like Decode but returns an additional 'end' value, which
   274  // indicates if end-of-message padding was encountered and thus any
   275  // additional data is an error. This method assumes that src has been
   276  // stripped of all supported whitespace ('\r' and '\n').
   277  func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
   278  	// Lift the nil check outside of the loop.
   279  	_ = enc.decodeMap
   280  
   281  	dsti := 0
   282  	olen := len(src)
   283  
   284  	for len(src) > 0 && !end {
   285  		// Decode quantum using the base32 alphabet
   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  					// We have reached the end and are missing padding
   294  					return n, false, CorruptInputError(olen - len(src) - j)
   295  				}
   296  				// We have reached the end and are not expecting any padding
   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  				// We've reached the end and there's padding
   304  				if len(src)+j < 8-1 {
   305  					// not enough padding
   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  						// incorrect padding
   311  						return n, false, CorruptInputError(olen - len(src) + k - 1)
   312  					}
   313  				}
   314  				dlen, end = j, true
   315  				// 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not
   316  				// valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing
   317  				// the five valid padding lengths, and Section 9 "Illustrations and
   318  				// Examples" for an illustration for how the 1st, 3rd and 6th base32
   319  				// src bytes do not yield enough information to decode a dst byte.
   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  		// Pack 8x 5-bit source blocks into 5 byte destination
   333  		// quantum
   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  // Decode decodes src using the encoding enc. It writes at most
   361  // DecodedLen(len(src)) bytes to dst and returns the number of bytes
   362  // written. If src contains invalid base32 data, it will return the
   363  // number of bytes successfully written and CorruptInputError.
   364  // New line characters (\r and \n) are ignored.
   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  // DecodeString returns the bytes represented by the base32 string s.
   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       // saw end of message
   385  	buf    [1024]byte // leftover input
   386  	nbuf   int
   387  	out    []byte // leftover decoded output
   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  	// data was read, less than min bytes could be read
   398  	if n < min && n > 0 && err == io.EOF {
   399  		err = io.ErrUnexpectedEOF
   400  	}
   401  	// no data was read, the buffer already contains some data
   402  	// when padding is disabled this is not an error, as the message can be of
   403  	// any length
   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  	// Use leftover decoded output from last read.
   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  	// Read a chunk.
   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  	// Minimum amount of bytes that needs to be read each cycle
   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  	// Decode chunk into p, or d.out and then p if p is too small.
   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  		// We cannot return all the decoded bytes to the caller in this
   479  		// invocation of Read, so we return a nil error to ensure that Read
   480  		// will be called again.  The error stored in d.err, if any, will be
   481  		// returned with the last set of decoded bytes.
   482  		return n, nil
   483  	}
   484  
   485  	return n, d.err
   486  }
   487  
   488  type newlineFilteringReader struct {
   489  	wrapped io.Reader
   490  }
   491  
   492  // stripNewlines removes newline characters and returns the number
   493  // of non-newline characters copied to dst.
   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  		// Previous buffer entirely whitespace, read again
   515  		n, err = r.wrapped.Read(p)
   516  	}
   517  	return n, err
   518  }
   519  
   520  // NewDecoder constructs a new base32 stream decoder.
   521  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   522  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   523  }
   524  
   525  // DecodedLen returns the maximum length in bytes of the decoded data
   526  // corresponding to n bytes of base32-encoded data.
   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