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  	"math"
    11  	"slices"
    12  	"strconv"
    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   // mapping of symbol index to symbol byte value
    25  	decodeMap [256]uint8 // mapping of symbol byte value to symbol index
    26  	padChar   rune
    27  }
    28  
    29  const (
    30  	StdPadding rune = '=' // Standard padding character
    31  	NoPadding  rune = -1  // No padding
    32  )
    33  
    34  const (
    35  	decodeMapInitialize = "" +
    36  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    37  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    38  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    39  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    40  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    41  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    42  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    43  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    44  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    45  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    46  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    47  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    48  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    49  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    50  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
    51  		"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
    52  	invalidIndex = '\xff'
    53  )
    54  
    55  // NewEncoding returns a new padded Encoding defined by the given alphabet,
    56  // which must be a 32-byte string that contains unique byte values and
    57  // does not contain the padding character or CR / LF ('\r', '\n').
    58  // The alphabet is treated as a sequence of byte values
    59  // without any special treatment for multi-byte UTF-8.
    60  // The resulting Encoding uses the default padding character ('='),
    61  // which may be changed or disabled via [Encoding.WithPadding].
    62  func NewEncoding(encoder string) *Encoding {
    63  	if len(encoder) != 32 {
    64  		panic("encoding alphabet is not 32-bytes long")
    65  	}
    66  
    67  	e := new(Encoding)
    68  	e.padChar = StdPadding
    69  	copy(e.encode[:], encoder)
    70  	copy(e.decodeMap[:], decodeMapInitialize)
    71  
    72  	for i := 0; i < len(encoder); i++ {
    73  		// Note: While we document that the alphabet cannot contain
    74  		// the padding character, we do not enforce it since we do not know
    75  		// if the caller intends to switch the padding from StdPadding later.
    76  		switch {
    77  		case encoder[i] == '\n' || encoder[i] == '\r':
    78  			panic("encoding alphabet contains newline character")
    79  		case e.decodeMap[encoder[i]] != invalidIndex:
    80  			panic("encoding alphabet includes duplicate symbols")
    81  		}
    82  		e.decodeMap[encoder[i]] = uint8(i)
    83  	}
    84  	return e
    85  }
    86  
    87  // StdEncoding is the standard base32 encoding, as defined in RFC 4648.
    88  var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")
    89  
    90  // HexEncoding is the “Extended Hex Alphabet” defined in RFC 4648.
    91  // It is typically used in DNS.
    92  var HexEncoding = NewEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUV")
    93  
    94  // WithPadding creates a new encoding identical to enc except
    95  // with a specified padding character, or NoPadding to disable padding.
    96  // The padding character must not be '\r' or '\n',
    97  // must not be contained in the encoding's alphabet,
    98  // must not be negative, and must be a rune equal or below '\xff'.
    99  // Padding characters above '\x7f' are encoded as their exact byte value
   100  // rather than using the UTF-8 representation of the codepoint.
   101  func (enc Encoding) WithPadding(padding rune) *Encoding {
   102  	switch {
   103  	case padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff:
   104  		panic("invalid padding")
   105  	case padding != NoPadding && enc.decodeMap[byte(padding)] != invalidIndex:
   106  		panic("padding contained in alphabet")
   107  	}
   108  	enc.padChar = padding
   109  	return &enc
   110  }
   111  
   112  /*
   113   * Encoder
   114   */
   115  
   116  // Encode encodes src using the encoding enc,
   117  // writing [Encoding.EncodedLen](len(src)) bytes to dst.
   118  //
   119  // The encoding pads the output to a multiple of 8 bytes,
   120  // so Encode is not appropriate for use on individual blocks
   121  // of a large data stream. Use [NewEncoder] instead.
   122  func (enc *Encoding) Encode(dst, src []byte) {
   123  	if len(src) == 0 {
   124  		return
   125  	}
   126  	// enc is a pointer receiver, so the use of enc.encode within the hot
   127  	// loop below means a nil check at every operation. Lift that nil check
   128  	// outside of the loop to speed up the encoder.
   129  	_ = enc.encode
   130  
   131  	for len(src) >= 5 {
   132  		// Combining two 32 bit loads allows the same code to be used
   133  		// for 32 and 64 bit platforms.
   134  		hi := uint32(src[0])<<24 | uint32(src[1])<<16 | uint32(src[2])<<8 | uint32(src[3])
   135  		lo := hi<<8 | uint32(src[4])
   136  
   137  		_ = dst[7] // Eliminate bounds checks below.
   138  		dst[0] = enc.encode[(hi>>27)&0x1F]
   139  		dst[1] = enc.encode[(hi>>22)&0x1F]
   140  		dst[2] = enc.encode[(hi>>17)&0x1F]
   141  		dst[3] = enc.encode[(hi>>12)&0x1F]
   142  		dst[4] = enc.encode[(hi>>7)&0x1F]
   143  		dst[5] = enc.encode[(hi>>2)&0x1F]
   144  		dst[6] = enc.encode[(lo>>5)&0x1F]
   145  		dst[7] = enc.encode[(lo)&0x1F]
   146  
   147  		src = src[5:]
   148  		dst = dst[8:]
   149  	}
   150  
   151  	// Add the remaining small block
   152  	if len(src) == 0 {
   153  		return
   154  	}
   155  
   156  	// Encode the remaining bytes in reverse order.
   157  	val := uint32(0)
   158  	switch len(src) {
   159  	case 4:
   160  		val |= uint32(src[3])
   161  		dst[6] = enc.encode[val<<3&0x1F]
   162  		dst[5] = enc.encode[val>>2&0x1F]
   163  		fallthrough
   164  	case 3:
   165  		val |= uint32(src[2]) << 8
   166  		dst[4] = enc.encode[val>>7&0x1F]
   167  		fallthrough
   168  	case 2:
   169  		val |= uint32(src[1]) << 16
   170  		dst[3] = enc.encode[val>>12&0x1F]
   171  		dst[2] = enc.encode[val>>17&0x1F]
   172  		fallthrough
   173  	case 1:
   174  		val |= uint32(src[0]) << 24
   175  		dst[1] = enc.encode[val>>22&0x1F]
   176  		dst[0] = enc.encode[val>>27&0x1F]
   177  	}
   178  
   179  	// Pad the final quantum
   180  	if enc.padChar != NoPadding {
   181  		nPad := (len(src) * 8 / 5) + 1
   182  		for i := nPad; i < 8; i++ {
   183  			dst[i] = byte(enc.padChar)
   184  		}
   185  	}
   186  }
   187  
   188  // AppendEncode appends the base32 encoded src to dst
   189  // and returns the extended buffer.
   190  func (enc *Encoding) AppendEncode(dst, src []byte) []byte {
   191  	n := enc.EncodedLen(len(src))
   192  	dst = slices.Grow(dst, n)
   193  	enc.Encode(dst[len(dst):][:n], src)
   194  	return dst[:len(dst)+n]
   195  }
   196  
   197  // EncodeToString returns the base32 encoding of src.
   198  func (enc *Encoding) EncodeToString(src []byte) string {
   199  	buf := make([]byte, enc.EncodedLen(len(src)))
   200  	enc.Encode(buf, src)
   201  	return string(buf)
   202  }
   203  
   204  type encoder struct {
   205  	err  error
   206  	enc  *Encoding
   207  	w    io.Writer
   208  	buf  [5]byte    // buffered data waiting to be encoded
   209  	nbuf int        // number of bytes in buf
   210  	out  [1024]byte // output buffer
   211  }
   212  
   213  func (e *encoder) Write(p []byte) (n int, err error) {
   214  	if e.err != nil {
   215  		return 0, e.err
   216  	}
   217  
   218  	// Leading fringe.
   219  	if e.nbuf > 0 {
   220  		var i int
   221  		for i = 0; i < len(p) && e.nbuf < 5; i++ {
   222  			e.buf[e.nbuf] = p[i]
   223  			e.nbuf++
   224  		}
   225  		n += i
   226  		p = p[i:]
   227  		if e.nbuf < 5 {
   228  			return
   229  		}
   230  		e.enc.Encode(e.out[0:], e.buf[0:])
   231  		if _, e.err = e.w.Write(e.out[0:8]); e.err != nil {
   232  			return n, e.err
   233  		}
   234  		e.nbuf = 0
   235  	}
   236  
   237  	// Large interior chunks.
   238  	for len(p) >= 5 {
   239  		nn := len(e.out) / 8 * 5
   240  		if nn > len(p) {
   241  			nn = len(p)
   242  			nn -= nn % 5
   243  		}
   244  		e.enc.Encode(e.out[0:], p[0:nn])
   245  		if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil {
   246  			return n, e.err
   247  		}
   248  		n += nn
   249  		p = p[nn:]
   250  	}
   251  
   252  	// Trailing fringe.
   253  	copy(e.buf[:], p)
   254  	e.nbuf = len(p)
   255  	n += len(p)
   256  	return
   257  }
   258  
   259  // Close flushes any pending output from the encoder.
   260  // It is an error to call Write after calling Close.
   261  func (e *encoder) Close() error {
   262  	// If there's anything left in the buffer, flush it out
   263  	if e.err == nil && e.nbuf > 0 {
   264  		e.enc.Encode(e.out[0:], e.buf[0:e.nbuf])
   265  		encodedLen := e.enc.EncodedLen(e.nbuf)
   266  		e.nbuf = 0
   267  		_, e.err = e.w.Write(e.out[0:encodedLen])
   268  	}
   269  	return e.err
   270  }
   271  
   272  // NewEncoder returns a new base32 stream encoder. Data written to
   273  // the returned writer will be encoded using enc and then written to w.
   274  // Base32 encodings operate in 5-byte blocks; when finished
   275  // writing, the caller must Close the returned encoder to flush any
   276  // partially written blocks.
   277  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   278  	return &encoder{enc: enc, w: w}
   279  }
   280  
   281  // EncodedLen returns the length in bytes of the base32 encoding
   282  // of an input buffer of length n.
   283  // It panics if the encoded length overflows int,
   284  // which can happen only if n > [math.MaxInt]/8*5.
   285  func (enc *Encoding) EncodedLen(n int) int {
   286  	if enc.padChar == NoPadding {
   287  		if n > math.MaxInt/8*5+4 {
   288  			panic("encoded length overflows int")
   289  		}
   290  		return n/5*8 + (n%5*8+4)/5
   291  	}
   292  	if n > math.MaxInt/8*5 {
   293  		panic("encoded length overflows int")
   294  	}
   295  	return (n + 4) / 5 * 8
   296  }
   297  
   298  /*
   299   * Decoder
   300   */
   301  
   302  type CorruptInputError int64
   303  
   304  func (e CorruptInputError) Error() string {
   305  	return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10)
   306  }
   307  
   308  // decode is like Decode but returns an additional 'end' value, which
   309  // indicates if end-of-message padding was encountered and thus any
   310  // additional data is an error. This method assumes that src has been
   311  // stripped of all supported whitespace ('\r' and '\n').
   312  func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
   313  	// Lift the nil check outside of the loop.
   314  	_ = enc.decodeMap
   315  
   316  	dsti := 0
   317  	olen := len(src)
   318  
   319  	for len(src) > 0 && !end {
   320  		// Decode quantum using the base32 alphabet
   321  		var dbuf [8]byte
   322  		dlen := 8
   323  
   324  		for j := 0; j < 8; {
   325  
   326  			if len(src) == 0 {
   327  				if enc.padChar != NoPadding {
   328  					// We have reached the end and are missing padding
   329  					return n, false, CorruptInputError(olen - len(src) - j)
   330  				}
   331  				// We have reached the end and are not expecting any padding
   332  				dlen, end = j, true
   333  				break
   334  			}
   335  			in := src[0]
   336  			src = src[1:]
   337  			if in == byte(enc.padChar) && j >= 2 && len(src) < 8 {
   338  				// We've reached the end and there's padding
   339  				if len(src)+j < 8-1 {
   340  					// not enough padding
   341  					return n, false, CorruptInputError(olen)
   342  				}
   343  				for k := 0; k < 8-1-j; k++ {
   344  					if len(src) > k && src[k] != byte(enc.padChar) {
   345  						// incorrect padding
   346  						return n, false, CorruptInputError(olen - len(src) + k - 1)
   347  					}
   348  				}
   349  				dlen, end = j, true
   350  				// 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not
   351  				// valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing
   352  				// the five valid padding lengths, and Section 9 "Illustrations and
   353  				// Examples" for an illustration for how the 1st, 3rd and 6th base32
   354  				// src bytes do not yield enough information to decode a dst byte.
   355  				if dlen == 1 || dlen == 3 || dlen == 6 {
   356  					return n, false, CorruptInputError(olen - len(src) - 1)
   357  				}
   358  				break
   359  			}
   360  			dbuf[j] = enc.decodeMap[in]
   361  			if dbuf[j] == 0xFF {
   362  				return n, false, CorruptInputError(olen - len(src) - 1)
   363  			}
   364  			j++
   365  		}
   366  
   367  		// Pack 8x 5-bit source blocks into 5 byte destination
   368  		// quantum
   369  		switch dlen {
   370  		case 8:
   371  			dst[dsti+4] = dbuf[6]<<5 | dbuf[7]
   372  			n++
   373  			fallthrough
   374  		case 7:
   375  			dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
   376  			n++
   377  			fallthrough
   378  		case 5:
   379  			dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1
   380  			n++
   381  			fallthrough
   382  		case 4:
   383  			dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
   384  			n++
   385  			fallthrough
   386  		case 2:
   387  			dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2
   388  			n++
   389  		}
   390  		dsti += 5
   391  	}
   392  	return n, end, nil
   393  }
   394  
   395  // Decode decodes src using the encoding enc. It writes at most
   396  // [Encoding.DecodedLen](len(src)) bytes to dst and returns the number of bytes
   397  // written. The caller must ensure that dst is large enough to hold all
   398  // the decoded data. If src contains invalid base32 data, it will return the
   399  // number of bytes successfully written and [CorruptInputError].
   400  // Newline characters (\r and \n) are ignored.
   401  func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
   402  	buf := make([]byte, len(src))
   403  	l := stripNewlines(buf, src)
   404  	n, _, err = enc.decode(dst, buf[:l])
   405  	return
   406  }
   407  
   408  // AppendDecode appends the base32 decoded src to dst
   409  // and returns the extended buffer.
   410  // If the input is malformed, it returns the partially decoded src and an error.
   411  // New line characters (\r and \n) are ignored.
   412  func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) {
   413  	// Compute the output size without padding to avoid over allocating.
   414  	n := len(src)
   415  	for n > 0 && rune(src[n-1]) == enc.padChar {
   416  		n--
   417  	}
   418  	n = decodedLen(n, NoPadding)
   419  
   420  	dst = slices.Grow(dst, n)
   421  	n, err := enc.Decode(dst[len(dst):][:n], src)
   422  	return dst[:len(dst)+n], err
   423  }
   424  
   425  // DecodeString returns the bytes represented by the base32 string s.
   426  // If the input is malformed, it returns the partially decoded data and
   427  // [CorruptInputError]. New line characters (\r and \n) are ignored.
   428  func (enc *Encoding) DecodeString(s string) ([]byte, error) {
   429  	buf := []byte(s)
   430  	l := stripNewlines(buf, buf)
   431  	n, _, err := enc.decode(buf, buf[:l])
   432  	return buf[:n], err
   433  }
   434  
   435  type decoder struct {
   436  	err    error
   437  	enc    *Encoding
   438  	r      io.Reader
   439  	end    bool       // saw end of message
   440  	buf    [1024]byte // leftover input
   441  	nbuf   int
   442  	out    []byte // leftover decoded output
   443  	outbuf [1024 / 8 * 5]byte
   444  }
   445  
   446  func readEncodedData(r io.Reader, buf []byte, min int, expectsPadding bool) (n int, err error) {
   447  	for n < min && err == nil {
   448  		var nn int
   449  		nn, err = r.Read(buf[n:])
   450  		n += nn
   451  	}
   452  	// data was read, less than min bytes could be read
   453  	if n < min && n > 0 && err == io.EOF {
   454  		err = io.ErrUnexpectedEOF
   455  	}
   456  	// no data was read, the buffer already contains some data
   457  	// when padding is disabled this is not an error, as the message can be of
   458  	// any length
   459  	if expectsPadding && min < 8 && n == 0 && err == io.EOF {
   460  		err = io.ErrUnexpectedEOF
   461  	}
   462  	return
   463  }
   464  
   465  func (d *decoder) Read(p []byte) (n int, err error) {
   466  	// Use leftover decoded output from last read.
   467  	if len(d.out) > 0 {
   468  		n = copy(p, d.out)
   469  		d.out = d.out[n:]
   470  		if len(d.out) == 0 {
   471  			return n, d.err
   472  		}
   473  		return n, nil
   474  	}
   475  
   476  	if d.err != nil {
   477  		return 0, d.err
   478  	}
   479  
   480  	// Read a chunk.
   481  	nn := (len(p) + 4) / 5 * 8
   482  	if nn < 8 {
   483  		nn = 8
   484  	}
   485  	if nn > len(d.buf) {
   486  		nn = len(d.buf)
   487  	}
   488  
   489  	// Minimum amount of bytes that needs to be read each cycle
   490  	var min int
   491  	var expectsPadding bool
   492  	if d.enc.padChar == NoPadding {
   493  		min = 1
   494  		expectsPadding = false
   495  	} else {
   496  		min = 8 - d.nbuf
   497  		expectsPadding = true
   498  	}
   499  
   500  	nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], min, expectsPadding)
   501  	d.nbuf += nn
   502  	if d.nbuf < min {
   503  		return 0, d.err
   504  	}
   505  	if nn > 0 && d.end {
   506  		return 0, CorruptInputError(0)
   507  	}
   508  
   509  	// Decode chunk into p, or d.out and then p if p is too small.
   510  	var nr int
   511  	if d.enc.padChar == NoPadding {
   512  		nr = d.nbuf
   513  	} else {
   514  		nr = d.nbuf / 8 * 8
   515  	}
   516  	nw := d.enc.DecodedLen(d.nbuf)
   517  
   518  	if nw > len(p) {
   519  		nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr])
   520  		d.out = d.outbuf[0:nw]
   521  		n = copy(p, d.out)
   522  		d.out = d.out[n:]
   523  	} else {
   524  		n, d.end, err = d.enc.decode(p, d.buf[0:nr])
   525  	}
   526  	d.nbuf -= nr
   527  	for i := 0; i < d.nbuf; i++ {
   528  		d.buf[i] = d.buf[i+nr]
   529  	}
   530  
   531  	if err != nil && (d.err == nil || d.err == io.EOF) {
   532  		d.err = err
   533  	}
   534  
   535  	if len(d.out) > 0 {
   536  		// We cannot return all the decoded bytes to the caller in this
   537  		// invocation of Read, so we return a nil error to ensure that Read
   538  		// will be called again.  The error stored in d.err, if any, will be
   539  		// returned with the last set of decoded bytes.
   540  		return n, nil
   541  	}
   542  
   543  	return n, d.err
   544  }
   545  
   546  type newlineFilteringReader struct {
   547  	wrapped io.Reader
   548  }
   549  
   550  // stripNewlines removes newline characters and returns the number
   551  // of non-newline characters copied to dst.
   552  func stripNewlines(dst, src []byte) int {
   553  	offset := 0
   554  	for _, b := range src {
   555  		if b == '\r' || b == '\n' {
   556  			continue
   557  		}
   558  		dst[offset] = b
   559  		offset++
   560  	}
   561  	return offset
   562  }
   563  
   564  func (r *newlineFilteringReader) Read(p []byte) (int, error) {
   565  	n, err := r.wrapped.Read(p)
   566  	for n > 0 {
   567  		s := p[0:n]
   568  		offset := stripNewlines(s, s)
   569  		if err != nil || offset > 0 {
   570  			return offset, err
   571  		}
   572  		// Previous buffer entirely whitespace, read again
   573  		n, err = r.wrapped.Read(p)
   574  	}
   575  	return n, err
   576  }
   577  
   578  // NewDecoder constructs a new base32 stream decoder.
   579  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   580  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   581  }
   582  
   583  // DecodedLen returns the maximum length in bytes of the decoded data
   584  // corresponding to n bytes of base32-encoded data.
   585  func (enc *Encoding) DecodedLen(n int) int {
   586  	return decodedLen(n, enc.padChar)
   587  }
   588  
   589  func decodedLen(n int, padChar rune) int {
   590  	if padChar == NoPadding {
   591  		return n/8*5 + n%8*5/8
   592  	}
   593  	return n / 8 * 5
   594  }
   595  

View as plain text