...
Run Format

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  	"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    [32]byte
    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  	if len(encoder) != 32 {
    41  		panic("encoding alphabet is not 32-bytes long")
    42  	}
    43  
    44  	e := new(Encoding)
    45  	copy(e.encode[:], encoder)
    46  	e.padChar = StdPadding
    47  
    48  	for i := 0; i < len(e.decodeMap); i++ {
    49  		e.decodeMap[i] = 0xFF
    50  	}
    51  	for i := 0; i < len(encoder); i++ {
    52  		e.decodeMap[encoder[i]] = byte(i)
    53  	}
    54  	return e
    55  }
    56  
    57  // StdEncoding is the standard base32 encoding, as defined in
    58  // RFC 4648.
    59  var StdEncoding = NewEncoding(encodeStd)
    60  
    61  // HexEncoding is the ``Extended Hex Alphabet'' defined in RFC 4648.
    62  // It is typically used in DNS.
    63  var HexEncoding = NewEncoding(encodeHex)
    64  
    65  var removeNewlinesMapper = func(r rune) rune {
    66  	if r == '\r' || r == '\n' {
    67  		return -1
    68  	}
    69  	return r
    70  }
    71  
    72  // WithPadding creates a new encoding identical to enc except
    73  // with a specified padding character, or NoPadding to disable padding.
    74  // The padding character must not be '\r' or '\n', must not
    75  // be contained in the encoding's alphabet and must be a rune equal or
    76  // below '\xff'.
    77  func (enc Encoding) WithPadding(padding rune) *Encoding {
    78  	if padding == '\r' || padding == '\n' || padding > 0xff {
    79  		panic("invalid padding")
    80  	}
    81  
    82  	for i := 0; i < len(enc.encode); i++ {
    83  		if rune(enc.encode[i]) == padding {
    84  			panic("padding contained in alphabet")
    85  		}
    86  	}
    87  
    88  	enc.padChar = padding
    89  	return &enc
    90  }
    91  
    92  /*
    93   * Encoder
    94   */
    95  
    96  // Encode encodes src using the encoding enc, writing
    97  // EncodedLen(len(src)) bytes to dst.
    98  //
    99  // The encoding pads the output to a multiple of 8 bytes,
   100  // so Encode is not appropriate for use on individual blocks
   101  // of a large data stream. Use NewEncoder() instead.
   102  func (enc *Encoding) Encode(dst, src []byte) {
   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]&31]
   137  			dst[1] = enc.encode[b[1]&31]
   138  			dst[2] = enc.encode[b[2]&31]
   139  			dst[3] = enc.encode[b[3]&31]
   140  			dst[4] = enc.encode[b[4]&31]
   141  			dst[5] = enc.encode[b[5]&31]
   142  			dst[6] = enc.encode[b[6]&31]
   143  			dst[7] = enc.encode[b[7]&31]
   144  		} else {
   145  			for i := 0; i < size; i++ {
   146  				dst[i] = enc.encode[b[i]&31]
   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  		encodedLen := e.enc.EncodedLen(e.nbuf)
   248  		e.nbuf = 0
   249  		_, e.err = e.w.Write(e.out[0:encodedLen])
   250  	}
   251  	return e.err
   252  }
   253  
   254  // NewEncoder returns a new base32 stream encoder. Data written to
   255  // the returned writer will be encoded using enc and then written to w.
   256  // Base32 encodings operate in 5-byte blocks; when finished
   257  // writing, the caller must Close the returned encoder to flush any
   258  // partially written blocks.
   259  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   260  	return &encoder{enc: enc, w: w}
   261  }
   262  
   263  // EncodedLen returns the length in bytes of the base32 encoding
   264  // of an input buffer of length n.
   265  func (enc *Encoding) EncodedLen(n int) int {
   266  	if enc.padChar == NoPadding {
   267  		return (n*8 + 4) / 5
   268  	}
   269  	return (n + 4) / 5 * 8
   270  }
   271  
   272  /*
   273   * Decoder
   274   */
   275  
   276  type CorruptInputError int64
   277  
   278  func (e CorruptInputError) Error() string {
   279  	return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10)
   280  }
   281  
   282  // decode is like Decode but returns an additional 'end' value, which
   283  // indicates if end-of-message padding was encountered and thus any
   284  // additional data is an error. This method assumes that src has been
   285  // stripped of all supported whitespace ('\r' and '\n').
   286  func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
   287  	olen := len(src)
   288  	for len(src) > 0 && !end {
   289  		// Decode quantum using the base32 alphabet
   290  		var dbuf [8]byte
   291  		dlen := 8
   292  
   293  		for j := 0; j < 8; {
   294  
   295  			// We have reached the end and are missing padding
   296  			if len(src) == 0 && enc.padChar != NoPadding {
   297  				return n, false, CorruptInputError(olen - len(src) - j)
   298  			}
   299  
   300  			// We have reached the end and are not expecing any padding
   301  			if len(src) == 0 && enc.padChar == NoPadding {
   302  				dlen, end = j, true
   303  				break
   304  			}
   305  
   306  			in := src[0]
   307  			src = src[1:]
   308  			if in == byte(enc.padChar) && j >= 2 && len(src) < 8 {
   309  				// We've reached the end and there's padding
   310  				if len(src)+j < 8-1 {
   311  					// not enough padding
   312  					return n, false, CorruptInputError(olen)
   313  				}
   314  				for k := 0; k < 8-1-j; k++ {
   315  					if len(src) > k && src[k] != byte(enc.padChar) {
   316  						// incorrect padding
   317  						return n, false, CorruptInputError(olen - len(src) + k - 1)
   318  					}
   319  				}
   320  				dlen, end = j, true
   321  				// 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not
   322  				// valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing
   323  				// the five valid padding lengths, and Section 9 "Illustrations and
   324  				// Examples" for an illustration for how the 1st, 3rd and 6th base32
   325  				// src bytes do not yield enough information to decode a dst byte.
   326  				if dlen == 1 || dlen == 3 || dlen == 6 {
   327  					return n, false, CorruptInputError(olen - len(src) - 1)
   328  				}
   329  				break
   330  			}
   331  			dbuf[j] = enc.decodeMap[in]
   332  			if dbuf[j] == 0xFF {
   333  				return n, false, CorruptInputError(olen - len(src) - 1)
   334  			}
   335  			j++
   336  		}
   337  
   338  		// Pack 8x 5-bit source blocks into 5 byte destination
   339  		// quantum
   340  		switch dlen {
   341  		case 8:
   342  			dst[4] = dbuf[6]<<5 | dbuf[7]
   343  			fallthrough
   344  		case 7:
   345  			dst[3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
   346  			fallthrough
   347  		case 5:
   348  			dst[2] = dbuf[3]<<4 | dbuf[4]>>1
   349  			fallthrough
   350  		case 4:
   351  			dst[1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
   352  			fallthrough
   353  		case 2:
   354  			dst[0] = dbuf[0]<<3 | dbuf[1]>>2
   355  		}
   356  
   357  		if !end {
   358  			dst = dst[5:]
   359  		}
   360  
   361  		switch dlen {
   362  		case 2:
   363  			n += 1
   364  		case 4:
   365  			n += 2
   366  		case 5:
   367  			n += 3
   368  		case 7:
   369  			n += 4
   370  		case 8:
   371  			n += 5
   372  		}
   373  	}
   374  	return n, end, nil
   375  }
   376  
   377  // Decode decodes src using the encoding enc. It writes at most
   378  // DecodedLen(len(src)) bytes to dst and returns the number of bytes
   379  // written. If src contains invalid base32 data, it will return the
   380  // number of bytes successfully written and CorruptInputError.
   381  // New line characters (\r and \n) are ignored.
   382  func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
   383  	src = bytes.Map(removeNewlinesMapper, src)
   384  	n, _, err = enc.decode(dst, src)
   385  	return
   386  }
   387  
   388  // DecodeString returns the bytes represented by the base32 string s.
   389  func (enc *Encoding) DecodeString(s string) ([]byte, error) {
   390  	s = strings.Map(removeNewlinesMapper, s)
   391  	dbuf := make([]byte, enc.DecodedLen(len(s)))
   392  	n, _, err := enc.decode(dbuf, []byte(s))
   393  	return dbuf[:n], err
   394  }
   395  
   396  type decoder struct {
   397  	err    error
   398  	enc    *Encoding
   399  	r      io.Reader
   400  	end    bool       // saw end of message
   401  	buf    [1024]byte // leftover input
   402  	nbuf   int
   403  	out    []byte // leftover decoded output
   404  	outbuf [1024 / 8 * 5]byte
   405  }
   406  
   407  func readEncodedData(r io.Reader, buf []byte, min int, expectsPadding bool) (n int, err error) {
   408  	for n < min && err == nil {
   409  		var nn int
   410  		nn, err = r.Read(buf[n:])
   411  		n += nn
   412  	}
   413  	// data was read, less than min bytes could be read
   414  	if n < min && n > 0 && err == io.EOF {
   415  		err = io.ErrUnexpectedEOF
   416  	}
   417  	// no data was read, the buffer already contains some data
   418  	// when padding is disabled this is not an error, as the message can be of
   419  	// any length
   420  	if expectsPadding && min < 8 && n == 0 && err == io.EOF {
   421  		err = io.ErrUnexpectedEOF
   422  	}
   423  	return
   424  }
   425  
   426  func (d *decoder) Read(p []byte) (n int, err error) {
   427  	// Use leftover decoded output from last read.
   428  	if len(d.out) > 0 {
   429  		n = copy(p, d.out)
   430  		d.out = d.out[n:]
   431  		if len(d.out) == 0 {
   432  			return n, d.err
   433  		}
   434  		return n, nil
   435  	}
   436  
   437  	if d.err != nil {
   438  		return 0, d.err
   439  	}
   440  
   441  	// Read a chunk.
   442  	nn := len(p) / 5 * 8
   443  	if nn < 8 {
   444  		nn = 8
   445  	}
   446  	if nn > len(d.buf) {
   447  		nn = len(d.buf)
   448  	}
   449  
   450  	// Minimum amount of bytes that needs to be read each cycle
   451  	var min int
   452  	var expectsPadding bool
   453  	if d.enc.padChar == NoPadding {
   454  		min = 1
   455  		expectsPadding = false
   456  	} else {
   457  		min = 8 - d.nbuf
   458  		expectsPadding = true
   459  	}
   460  
   461  	nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], min, expectsPadding)
   462  	d.nbuf += nn
   463  	if d.nbuf < min {
   464  		return 0, d.err
   465  	}
   466  
   467  	// Decode chunk into p, or d.out and then p if p is too small.
   468  	var nr int
   469  	if d.enc.padChar == NoPadding {
   470  		nr = d.nbuf
   471  	} else {
   472  		nr = d.nbuf / 8 * 8
   473  	}
   474  	nw := d.enc.DecodedLen(d.nbuf)
   475  
   476  	if nw > len(p) {
   477  		nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr])
   478  		d.out = d.outbuf[0:nw]
   479  		n = copy(p, d.out)
   480  		d.out = d.out[n:]
   481  	} else {
   482  		n, d.end, err = d.enc.decode(p, d.buf[0:nr])
   483  	}
   484  	d.nbuf -= nr
   485  	for i := 0; i < d.nbuf; i++ {
   486  		d.buf[i] = d.buf[i+nr]
   487  	}
   488  
   489  	if err != nil && (d.err == nil || d.err == io.EOF) {
   490  		d.err = err
   491  	}
   492  
   493  	if len(d.out) > 0 {
   494  		// We cannot return all the decoded bytes to the caller in this
   495  		// invocation of Read, so we return a nil error to ensure that Read
   496  		// will be called again.  The error stored in d.err, if any, will be
   497  		// returned with the last set of decoded bytes.
   498  		return n, nil
   499  	}
   500  
   501  	return n, d.err
   502  }
   503  
   504  type newlineFilteringReader struct {
   505  	wrapped io.Reader
   506  }
   507  
   508  func (r *newlineFilteringReader) Read(p []byte) (int, error) {
   509  	n, err := r.wrapped.Read(p)
   510  	for n > 0 {
   511  		offset := 0
   512  		for i, b := range p[0:n] {
   513  			if b != '\r' && b != '\n' {
   514  				if i != offset {
   515  					p[offset] = b
   516  				}
   517  				offset++
   518  			}
   519  		}
   520  		if err != nil || offset > 0 {
   521  			return offset, err
   522  		}
   523  		// Previous buffer entirely whitespace, read again
   524  		n, err = r.wrapped.Read(p)
   525  	}
   526  	return n, err
   527  }
   528  
   529  // NewDecoder constructs a new base32 stream decoder.
   530  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   531  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   532  }
   533  
   534  // DecodedLen returns the maximum length in bytes of the decoded data
   535  // corresponding to n bytes of base32-encoded data.
   536  func (enc *Encoding) DecodedLen(n int) int {
   537  	if enc.padChar == NoPadding {
   538  		return n * 5 / 8
   539  	}
   540  
   541  	return n / 8 * 5
   542  }
   543  

View as plain text