Source file src/encoding/base64/base64.go

     1  // Copyright 2009 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 base64 implements base64 encoding as specified by RFC 4648.
     6  package base64
     7  
     8  import (
     9  	"encoding/binary"
    10  	"io"
    11  	"strconv"
    12  )
    13  
    14  /*
    15   * Encodings
    16   */
    17  
    18  // An Encoding is a radix 64 encoding/decoding scheme, defined by a
    19  // 64-character alphabet. The most common encoding is the "base64"
    20  // encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
    21  // (RFC 1421).  RFC 4648 also defines an alternate encoding, which is
    22  // the standard encoding with - and _ substituted for + and /.
    23  type Encoding struct {
    24  	encode    [64]byte
    25  	decodeMap [256]byte
    26  	padChar   rune
    27  	strict    bool
    28  }
    29  
    30  const (
    31  	StdPadding rune = '=' // Standard padding character
    32  	NoPadding  rune = -1  // No padding
    33  )
    34  
    35  const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    36  const encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
    37  
    38  // NewEncoding returns a new padded Encoding defined by the given alphabet,
    39  // which must be a 64-byte string that does not contain the padding character
    40  // or CR / LF ('\r', '\n').
    41  // The resulting Encoding uses the default padding character ('='),
    42  // which may be changed or disabled via WithPadding.
    43  func NewEncoding(encoder string) *Encoding {
    44  	if len(encoder) != 64 {
    45  		panic("encoding alphabet is not 64-bytes long")
    46  	}
    47  	for i := 0; i < len(encoder); i++ {
    48  		if encoder[i] == '\n' || encoder[i] == '\r' {
    49  			panic("encoding alphabet contains newline character")
    50  		}
    51  	}
    52  
    53  	e := new(Encoding)
    54  	e.padChar = StdPadding
    55  	copy(e.encode[:], encoder)
    56  
    57  	for i := 0; i < len(e.decodeMap); i++ {
    58  		e.decodeMap[i] = 0xFF
    59  	}
    60  	for i := 0; i < len(encoder); i++ {
    61  		e.decodeMap[encoder[i]] = byte(i)
    62  	}
    63  	return e
    64  }
    65  
    66  // WithPadding creates a new encoding identical to enc except
    67  // with a specified padding character, or NoPadding to disable padding.
    68  // The padding character must not be '\r' or '\n', must not
    69  // be contained in the encoding's alphabet and must be a rune equal or
    70  // below '\xff'.
    71  func (enc Encoding) WithPadding(padding rune) *Encoding {
    72  	if padding == '\r' || padding == '\n' || padding > 0xff {
    73  		panic("invalid padding")
    74  	}
    75  
    76  	for i := 0; i < len(enc.encode); i++ {
    77  		if rune(enc.encode[i]) == padding {
    78  			panic("padding contained in alphabet")
    79  		}
    80  	}
    81  
    82  	enc.padChar = padding
    83  	return &enc
    84  }
    85  
    86  // Strict creates a new encoding identical to enc except with
    87  // strict decoding enabled. In this mode, the decoder requires that
    88  // trailing padding bits are zero, as described in RFC 4648 section 3.5.
    89  //
    90  // Note that the input is still malleable, as new line characters
    91  // (CR and LF) are still ignored.
    92  func (enc Encoding) Strict() *Encoding {
    93  	enc.strict = true
    94  	return &enc
    95  }
    96  
    97  // StdEncoding is the standard base64 encoding, as defined in
    98  // RFC 4648.
    99  var StdEncoding = NewEncoding(encodeStd)
   100  
   101  // URLEncoding is the alternate base64 encoding defined in RFC 4648.
   102  // It is typically used in URLs and file names.
   103  var URLEncoding = NewEncoding(encodeURL)
   104  
   105  // RawStdEncoding is the standard raw, unpadded base64 encoding,
   106  // as defined in RFC 4648 section 3.2.
   107  // This is the same as StdEncoding but omits padding characters.
   108  var RawStdEncoding = StdEncoding.WithPadding(NoPadding)
   109  
   110  // RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
   111  // It is typically used in URLs and file names.
   112  // This is the same as URLEncoding but omits padding characters.
   113  var RawURLEncoding = URLEncoding.WithPadding(NoPadding)
   114  
   115  /*
   116   * Encoder
   117   */
   118  
   119  // Encode encodes src using the encoding enc, writing
   120  // EncodedLen(len(src)) bytes to dst.
   121  //
   122  // The encoding pads the output to a multiple of 4 bytes,
   123  // so Encode is not appropriate for use on individual blocks
   124  // of a large data stream. Use NewEncoder() instead.
   125  func (enc *Encoding) Encode(dst, src []byte) {
   126  	if len(src) == 0 {
   127  		return
   128  	}
   129  	// enc is a pointer receiver, so the use of enc.encode within the hot
   130  	// loop below means a nil check at every operation. Lift that nil check
   131  	// outside of the loop to speed up the encoder.
   132  	_ = enc.encode
   133  
   134  	di, si := 0, 0
   135  	n := (len(src) / 3) * 3
   136  	for si < n {
   137  		// Convert 3x 8bit source bytes into 4 bytes
   138  		val := uint(src[si+0])<<16 | uint(src[si+1])<<8 | uint(src[si+2])
   139  
   140  		dst[di+0] = enc.encode[val>>18&0x3F]
   141  		dst[di+1] = enc.encode[val>>12&0x3F]
   142  		dst[di+2] = enc.encode[val>>6&0x3F]
   143  		dst[di+3] = enc.encode[val&0x3F]
   144  
   145  		si += 3
   146  		di += 4
   147  	}
   148  
   149  	remain := len(src) - si
   150  	if remain == 0 {
   151  		return
   152  	}
   153  	// Add the remaining small block
   154  	val := uint(src[si+0]) << 16
   155  	if remain == 2 {
   156  		val |= uint(src[si+1]) << 8
   157  	}
   158  
   159  	dst[di+0] = enc.encode[val>>18&0x3F]
   160  	dst[di+1] = enc.encode[val>>12&0x3F]
   161  
   162  	switch remain {
   163  	case 2:
   164  		dst[di+2] = enc.encode[val>>6&0x3F]
   165  		if enc.padChar != NoPadding {
   166  			dst[di+3] = byte(enc.padChar)
   167  		}
   168  	case 1:
   169  		if enc.padChar != NoPadding {
   170  			dst[di+2] = byte(enc.padChar)
   171  			dst[di+3] = byte(enc.padChar)
   172  		}
   173  	}
   174  }
   175  
   176  // EncodeToString returns the base64 encoding of src.
   177  func (enc *Encoding) EncodeToString(src []byte) string {
   178  	buf := make([]byte, enc.EncodedLen(len(src)))
   179  	enc.Encode(buf, src)
   180  	return string(buf)
   181  }
   182  
   183  type encoder struct {
   184  	err  error
   185  	enc  *Encoding
   186  	w    io.Writer
   187  	buf  [3]byte    // buffered data waiting to be encoded
   188  	nbuf int        // number of bytes in buf
   189  	out  [1024]byte // output buffer
   190  }
   191  
   192  func (e *encoder) Write(p []byte) (n int, err error) {
   193  	if e.err != nil {
   194  		return 0, e.err
   195  	}
   196  
   197  	// Leading fringe.
   198  	if e.nbuf > 0 {
   199  		var i int
   200  		for i = 0; i < len(p) && e.nbuf < 3; i++ {
   201  			e.buf[e.nbuf] = p[i]
   202  			e.nbuf++
   203  		}
   204  		n += i
   205  		p = p[i:]
   206  		if e.nbuf < 3 {
   207  			return
   208  		}
   209  		e.enc.Encode(e.out[:], e.buf[:])
   210  		if _, e.err = e.w.Write(e.out[:4]); e.err != nil {
   211  			return n, e.err
   212  		}
   213  		e.nbuf = 0
   214  	}
   215  
   216  	// Large interior chunks.
   217  	for len(p) >= 3 {
   218  		nn := len(e.out) / 4 * 3
   219  		if nn > len(p) {
   220  			nn = len(p)
   221  			nn -= nn % 3
   222  		}
   223  		e.enc.Encode(e.out[:], p[:nn])
   224  		if _, e.err = e.w.Write(e.out[0 : nn/3*4]); e.err != nil {
   225  			return n, e.err
   226  		}
   227  		n += nn
   228  		p = p[nn:]
   229  	}
   230  
   231  	// Trailing fringe.
   232  	copy(e.buf[:], p)
   233  	e.nbuf = len(p)
   234  	n += len(p)
   235  	return
   236  }
   237  
   238  // Close flushes any pending output from the encoder.
   239  // It is an error to call Write after calling Close.
   240  func (e *encoder) Close() error {
   241  	// If there's anything left in the buffer, flush it out
   242  	if e.err == nil && e.nbuf > 0 {
   243  		e.enc.Encode(e.out[:], e.buf[:e.nbuf])
   244  		_, e.err = e.w.Write(e.out[:e.enc.EncodedLen(e.nbuf)])
   245  		e.nbuf = 0
   246  	}
   247  	return e.err
   248  }
   249  
   250  // NewEncoder returns a new base64 stream encoder. Data written to
   251  // the returned writer will be encoded using enc and then written to w.
   252  // Base64 encodings operate in 4-byte blocks; when finished
   253  // writing, the caller must Close the returned encoder to flush any
   254  // partially written blocks.
   255  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   256  	return &encoder{enc: enc, w: w}
   257  }
   258  
   259  // EncodedLen returns the length in bytes of the base64 encoding
   260  // of an input buffer of length n.
   261  func (enc *Encoding) EncodedLen(n int) int {
   262  	if enc.padChar == NoPadding {
   263  		return (n*8 + 5) / 6 // minimum # chars at 6 bits per char
   264  	}
   265  	return (n + 2) / 3 * 4 // minimum # 4-char quanta, 3 bytes each
   266  }
   267  
   268  /*
   269   * Decoder
   270   */
   271  
   272  type CorruptInputError int64
   273  
   274  func (e CorruptInputError) Error() string {
   275  	return "illegal base64 data at input byte " + strconv.FormatInt(int64(e), 10)
   276  }
   277  
   278  // decodeQuantum decodes up to 4 base64 bytes. The received parameters are
   279  // the destination buffer dst, the source buffer src and an index in the
   280  // source buffer si.
   281  // It returns the number of bytes read from src, the number of bytes written
   282  // to dst, and an error, if any.
   283  func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err error) {
   284  	// Decode quantum using the base64 alphabet
   285  	var dbuf [4]byte
   286  	dlen := 4
   287  
   288  	// Lift the nil check outside of the loop.
   289  	_ = enc.decodeMap
   290  
   291  	for j := 0; j < len(dbuf); j++ {
   292  		if len(src) == si {
   293  			switch {
   294  			case j == 0:
   295  				return si, 0, nil
   296  			case j == 1, enc.padChar != NoPadding:
   297  				return si, 0, CorruptInputError(si - j)
   298  			}
   299  			dlen = j
   300  			break
   301  		}
   302  		in := src[si]
   303  		si++
   304  
   305  		out := enc.decodeMap[in]
   306  		if out != 0xff {
   307  			dbuf[j] = out
   308  			continue
   309  		}
   310  
   311  		if in == '\n' || in == '\r' {
   312  			j--
   313  			continue
   314  		}
   315  
   316  		if rune(in) != enc.padChar {
   317  			return si, 0, CorruptInputError(si - 1)
   318  		}
   319  
   320  		// We've reached the end and there's padding
   321  		switch j {
   322  		case 0, 1:
   323  			// incorrect padding
   324  			return si, 0, CorruptInputError(si - 1)
   325  		case 2:
   326  			// "==" is expected, the first "=" is already consumed.
   327  			// skip over newlines
   328  			for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
   329  				si++
   330  			}
   331  			if si == len(src) {
   332  				// not enough padding
   333  				return si, 0, CorruptInputError(len(src))
   334  			}
   335  			if rune(src[si]) != enc.padChar {
   336  				// incorrect padding
   337  				return si, 0, CorruptInputError(si - 1)
   338  			}
   339  
   340  			si++
   341  		}
   342  
   343  		// skip over newlines
   344  		for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
   345  			si++
   346  		}
   347  		if si < len(src) {
   348  			// trailing garbage
   349  			err = CorruptInputError(si)
   350  		}
   351  		dlen = j
   352  		break
   353  	}
   354  
   355  	// Convert 4x 6bit source bytes into 3 bytes
   356  	val := uint(dbuf[0])<<18 | uint(dbuf[1])<<12 | uint(dbuf[2])<<6 | uint(dbuf[3])
   357  	dbuf[2], dbuf[1], dbuf[0] = byte(val>>0), byte(val>>8), byte(val>>16)
   358  	switch dlen {
   359  	case 4:
   360  		dst[2] = dbuf[2]
   361  		dbuf[2] = 0
   362  		fallthrough
   363  	case 3:
   364  		dst[1] = dbuf[1]
   365  		if enc.strict && dbuf[2] != 0 {
   366  			return si, 0, CorruptInputError(si - 1)
   367  		}
   368  		dbuf[1] = 0
   369  		fallthrough
   370  	case 2:
   371  		dst[0] = dbuf[0]
   372  		if enc.strict && (dbuf[1] != 0 || dbuf[2] != 0) {
   373  			return si, 0, CorruptInputError(si - 2)
   374  		}
   375  	}
   376  
   377  	return si, dlen - 1, err
   378  }
   379  
   380  // DecodeString returns the bytes represented by the base64 string s.
   381  func (enc *Encoding) DecodeString(s string) ([]byte, error) {
   382  	dbuf := make([]byte, enc.DecodedLen(len(s)))
   383  	n, err := enc.Decode(dbuf, []byte(s))
   384  	return dbuf[:n], err
   385  }
   386  
   387  type decoder struct {
   388  	err     error
   389  	readErr error // error from r.Read
   390  	enc     *Encoding
   391  	r       io.Reader
   392  	buf     [1024]byte // leftover input
   393  	nbuf    int
   394  	out     []byte // leftover decoded output
   395  	outbuf  [1024 / 4 * 3]byte
   396  }
   397  
   398  func (d *decoder) Read(p []byte) (n int, err error) {
   399  	// Use leftover decoded output from last read.
   400  	if len(d.out) > 0 {
   401  		n = copy(p, d.out)
   402  		d.out = d.out[n:]
   403  		return n, nil
   404  	}
   405  
   406  	if d.err != nil {
   407  		return 0, d.err
   408  	}
   409  
   410  	// This code assumes that d.r strips supported whitespace ('\r' and '\n').
   411  
   412  	// Refill buffer.
   413  	for d.nbuf < 4 && d.readErr == nil {
   414  		nn := len(p) / 3 * 4
   415  		if nn < 4 {
   416  			nn = 4
   417  		}
   418  		if nn > len(d.buf) {
   419  			nn = len(d.buf)
   420  		}
   421  		nn, d.readErr = d.r.Read(d.buf[d.nbuf:nn])
   422  		d.nbuf += nn
   423  	}
   424  
   425  	if d.nbuf < 4 {
   426  		if d.enc.padChar == NoPadding && d.nbuf > 0 {
   427  			// Decode final fragment, without padding.
   428  			var nw int
   429  			nw, d.err = d.enc.Decode(d.outbuf[:], d.buf[:d.nbuf])
   430  			d.nbuf = 0
   431  			d.out = d.outbuf[:nw]
   432  			n = copy(p, d.out)
   433  			d.out = d.out[n:]
   434  			if n > 0 || len(p) == 0 && len(d.out) > 0 {
   435  				return n, nil
   436  			}
   437  			if d.err != nil {
   438  				return 0, d.err
   439  			}
   440  		}
   441  		d.err = d.readErr
   442  		if d.err == io.EOF && d.nbuf > 0 {
   443  			d.err = io.ErrUnexpectedEOF
   444  		}
   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 / 4 * 4
   450  	nw := d.nbuf / 4 * 3
   451  	if nw > len(p) {
   452  		nw, d.err = d.enc.Decode(d.outbuf[:], d.buf[:nr])
   453  		d.out = d.outbuf[:nw]
   454  		n = copy(p, d.out)
   455  		d.out = d.out[n:]
   456  	} else {
   457  		n, d.err = d.enc.Decode(p, d.buf[:nr])
   458  	}
   459  	d.nbuf -= nr
   460  	copy(d.buf[:d.nbuf], d.buf[nr:])
   461  	return n, d.err
   462  }
   463  
   464  // Decode decodes src using the encoding enc. It writes at most
   465  // DecodedLen(len(src)) bytes to dst and returns the number of bytes
   466  // written. If src contains invalid base64 data, it will return the
   467  // number of bytes successfully written and CorruptInputError.
   468  // New line characters (\r and \n) are ignored.
   469  func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
   470  	if len(src) == 0 {
   471  		return 0, nil
   472  	}
   473  
   474  	// Lift the nil check outside of the loop. enc.decodeMap is directly
   475  	// used later in this function, to let the compiler know that the
   476  	// receiver can't be nil.
   477  	_ = enc.decodeMap
   478  
   479  	si := 0
   480  	for strconv.IntSize >= 64 && len(src)-si >= 8 && len(dst)-n >= 8 {
   481  		src2 := src[si : si+8]
   482  		if dn, ok := assemble64(
   483  			enc.decodeMap[src2[0]],
   484  			enc.decodeMap[src2[1]],
   485  			enc.decodeMap[src2[2]],
   486  			enc.decodeMap[src2[3]],
   487  			enc.decodeMap[src2[4]],
   488  			enc.decodeMap[src2[5]],
   489  			enc.decodeMap[src2[6]],
   490  			enc.decodeMap[src2[7]],
   491  		); ok {
   492  			binary.BigEndian.PutUint64(dst[n:], dn)
   493  			n += 6
   494  			si += 8
   495  		} else {
   496  			var ninc int
   497  			si, ninc, err = enc.decodeQuantum(dst[n:], src, si)
   498  			n += ninc
   499  			if err != nil {
   500  				return n, err
   501  			}
   502  		}
   503  	}
   504  
   505  	for len(src)-si >= 4 && len(dst)-n >= 4 {
   506  		src2 := src[si : si+4]
   507  		if dn, ok := assemble32(
   508  			enc.decodeMap[src2[0]],
   509  			enc.decodeMap[src2[1]],
   510  			enc.decodeMap[src2[2]],
   511  			enc.decodeMap[src2[3]],
   512  		); ok {
   513  			binary.BigEndian.PutUint32(dst[n:], dn)
   514  			n += 3
   515  			si += 4
   516  		} else {
   517  			var ninc int
   518  			si, ninc, err = enc.decodeQuantum(dst[n:], src, si)
   519  			n += ninc
   520  			if err != nil {
   521  				return n, err
   522  			}
   523  		}
   524  	}
   525  
   526  	for si < len(src) {
   527  		var ninc int
   528  		si, ninc, err = enc.decodeQuantum(dst[n:], src, si)
   529  		n += ninc
   530  		if err != nil {
   531  			return n, err
   532  		}
   533  	}
   534  	return n, err
   535  }
   536  
   537  // assemble32 assembles 4 base64 digits into 3 bytes.
   538  // Each digit comes from the decode map, and will be 0xff
   539  // if it came from an invalid character.
   540  func assemble32(n1, n2, n3, n4 byte) (dn uint32, ok bool) {
   541  	// Check that all the digits are valid. If any of them was 0xff, their
   542  	// bitwise OR will be 0xff.
   543  	if n1|n2|n3|n4 == 0xff {
   544  		return 0, false
   545  	}
   546  	return uint32(n1)<<26 |
   547  			uint32(n2)<<20 |
   548  			uint32(n3)<<14 |
   549  			uint32(n4)<<8,
   550  		true
   551  }
   552  
   553  // assemble64 assembles 8 base64 digits into 6 bytes.
   554  // Each digit comes from the decode map, and will be 0xff
   555  // if it came from an invalid character.
   556  func assemble64(n1, n2, n3, n4, n5, n6, n7, n8 byte) (dn uint64, ok bool) {
   557  	// Check that all the digits are valid. If any of them was 0xff, their
   558  	// bitwise OR will be 0xff.
   559  	if n1|n2|n3|n4|n5|n6|n7|n8 == 0xff {
   560  		return 0, false
   561  	}
   562  	return uint64(n1)<<58 |
   563  			uint64(n2)<<52 |
   564  			uint64(n3)<<46 |
   565  			uint64(n4)<<40 |
   566  			uint64(n5)<<34 |
   567  			uint64(n6)<<28 |
   568  			uint64(n7)<<22 |
   569  			uint64(n8)<<16,
   570  		true
   571  }
   572  
   573  type newlineFilteringReader struct {
   574  	wrapped io.Reader
   575  }
   576  
   577  func (r *newlineFilteringReader) Read(p []byte) (int, error) {
   578  	n, err := r.wrapped.Read(p)
   579  	for n > 0 {
   580  		offset := 0
   581  		for i, b := range p[:n] {
   582  			if b != '\r' && b != '\n' {
   583  				if i != offset {
   584  					p[offset] = b
   585  				}
   586  				offset++
   587  			}
   588  		}
   589  		if offset > 0 {
   590  			return offset, err
   591  		}
   592  		// Previous buffer entirely whitespace, read again
   593  		n, err = r.wrapped.Read(p)
   594  	}
   595  	return n, err
   596  }
   597  
   598  // NewDecoder constructs a new base64 stream decoder.
   599  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   600  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   601  }
   602  
   603  // DecodedLen returns the maximum length in bytes of the decoded data
   604  // corresponding to n bytes of base64-encoded data.
   605  func (enc *Encoding) DecodedLen(n int) int {
   606  	if enc.padChar == NoPadding {
   607  		// Unpadded data may end with partial block of 2-3 characters.
   608  		return n * 6 / 8
   609  	}
   610  	// Padded base64 should always be a multiple of 4 characters in length.
   611  	return n / 4 * 3
   612  }
   613  

View as plain text