github.com/slayercat/go@v0.0.0-20170428012452-c51559813f61/src/encoding/base64/base64.go (about)

     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  	"io"
    10  	"strconv"
    11  )
    12  
    13  /*
    14   * Encodings
    15   */
    16  
    17  // An Encoding is a radix 64 encoding/decoding scheme, defined by a
    18  // 64-character alphabet. The most common encoding is the "base64"
    19  // encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
    20  // (RFC 1421).  RFC 4648 also defines an alternate encoding, which is
    21  // the standard encoding with - and _ substituted for + and /.
    22  type Encoding struct {
    23  	encode    [64]byte
    24  	decodeMap [256]byte
    25  	padChar   rune
    26  	strict    bool
    27  }
    28  
    29  const (
    30  	StdPadding rune = '=' // Standard padding character
    31  	NoPadding  rune = -1  // No padding
    32  )
    33  
    34  const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    35  const encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
    36  
    37  // NewEncoding returns a new padded Encoding defined by the given alphabet,
    38  // which must be a 64-byte string that does not contain the padding character
    39  // or CR / LF ('\r', '\n').
    40  // The resulting Encoding uses the default padding character ('='),
    41  // which may be changed or disabled via WithPadding.
    42  func NewEncoding(encoder string) *Encoding {
    43  	if len(encoder) != 64 {
    44  		panic("encoding alphabet is not 64-bytes long")
    45  	}
    46  	for i := 0; i < len(encoder); i++ {
    47  		if encoder[i] == '\n' || encoder[i] == '\r' {
    48  			panic("encoding alphabet contains newline character")
    49  		}
    50  	}
    51  
    52  	e := new(Encoding)
    53  	e.padChar = StdPadding
    54  	copy(e.encode[:], encoder)
    55  
    56  	for i := 0; i < len(e.decodeMap); i++ {
    57  		e.decodeMap[i] = 0xFF
    58  	}
    59  	for i := 0; i < len(encoder); i++ {
    60  		e.decodeMap[encoder[i]] = byte(i)
    61  	}
    62  	return e
    63  }
    64  
    65  // WithPadding creates a new encoding identical to enc except
    66  // with a specified padding character, or NoPadding to disable padding.
    67  // The padding character must not be '\r' or '\n', must not
    68  // be contained in the encoding's alphabet and must be a rune equal or
    69  // below '\xff'.
    70  func (enc Encoding) WithPadding(padding rune) *Encoding {
    71  	if padding == '\r' || padding == '\n' || padding > 0xff {
    72  		panic("invalid padding")
    73  	}
    74  
    75  	for i := 0; i < len(enc.encode); i++ {
    76  		if rune(enc.encode[i]) == padding {
    77  			panic("padding contained in alphabet")
    78  		}
    79  	}
    80  
    81  	enc.padChar = padding
    82  	return &enc
    83  }
    84  
    85  // Strict creates a new encoding identical to enc except with
    86  // strict decoding enabled. In this mode, the decoder requires that
    87  // trailing padding bits are zero, as described in RFC 4648 section 3.5.
    88  func (enc Encoding) Strict() *Encoding {
    89  	enc.strict = true
    90  	return &enc
    91  }
    92  
    93  // StdEncoding is the standard base64 encoding, as defined in
    94  // RFC 4648.
    95  var StdEncoding = NewEncoding(encodeStd)
    96  
    97  // URLEncoding is the alternate base64 encoding defined in RFC 4648.
    98  // It is typically used in URLs and file names.
    99  var URLEncoding = NewEncoding(encodeURL)
   100  
   101  // RawStdEncoding is the standard raw, unpadded base64 encoding,
   102  // as defined in RFC 4648 section 3.2.
   103  // This is the same as StdEncoding but omits padding characters.
   104  var RawStdEncoding = StdEncoding.WithPadding(NoPadding)
   105  
   106  // RawURLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
   107  // It is typically used in URLs and file names.
   108  // This is the same as URLEncoding but omits padding characters.
   109  var RawURLEncoding = URLEncoding.WithPadding(NoPadding)
   110  
   111  /*
   112   * Encoder
   113   */
   114  
   115  // Encode encodes src using the encoding enc, writing
   116  // EncodedLen(len(src)) bytes to dst.
   117  //
   118  // The encoding pads the output to a multiple of 4 bytes,
   119  // so Encode is not appropriate for use on individual blocks
   120  // of a large data stream. Use NewEncoder() instead.
   121  func (enc *Encoding) Encode(dst, src []byte) {
   122  	if len(src) == 0 {
   123  		return
   124  	}
   125  
   126  	di, si := 0, 0
   127  	n := (len(src) / 3) * 3
   128  	for si < n {
   129  		// Convert 3x 8bit source bytes into 4 bytes
   130  		val := uint(src[si+0])<<16 | uint(src[si+1])<<8 | uint(src[si+2])
   131  
   132  		dst[di+0] = enc.encode[val>>18&0x3F]
   133  		dst[di+1] = enc.encode[val>>12&0x3F]
   134  		dst[di+2] = enc.encode[val>>6&0x3F]
   135  		dst[di+3] = enc.encode[val&0x3F]
   136  
   137  		si += 3
   138  		di += 4
   139  	}
   140  
   141  	remain := len(src) - si
   142  	if remain == 0 {
   143  		return
   144  	}
   145  	// Add the remaining small block
   146  	val := uint(src[si+0]) << 16
   147  	if remain == 2 {
   148  		val |= uint(src[si+1]) << 8
   149  	}
   150  
   151  	dst[di+0] = enc.encode[val>>18&0x3F]
   152  	dst[di+1] = enc.encode[val>>12&0x3F]
   153  
   154  	switch remain {
   155  	case 2:
   156  		dst[di+2] = enc.encode[val>>6&0x3F]
   157  		if enc.padChar != NoPadding {
   158  			dst[di+3] = byte(enc.padChar)
   159  		}
   160  	case 1:
   161  		if enc.padChar != NoPadding {
   162  			dst[di+2] = byte(enc.padChar)
   163  			dst[di+3] = byte(enc.padChar)
   164  		}
   165  	}
   166  }
   167  
   168  // EncodeToString returns the base64 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  [3]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 < 3; i++ {
   193  			e.buf[e.nbuf] = p[i]
   194  			e.nbuf++
   195  		}
   196  		n += i
   197  		p = p[i:]
   198  		if e.nbuf < 3 {
   199  			return
   200  		}
   201  		e.enc.Encode(e.out[:], e.buf[:])
   202  		if _, e.err = e.w.Write(e.out[:4]); e.err != nil {
   203  			return n, e.err
   204  		}
   205  		e.nbuf = 0
   206  	}
   207  
   208  	// Large interior chunks.
   209  	for len(p) >= 3 {
   210  		nn := len(e.out) / 4 * 3
   211  		if nn > len(p) {
   212  			nn = len(p)
   213  			nn -= nn % 3
   214  		}
   215  		e.enc.Encode(e.out[:], p[:nn])
   216  		if _, e.err = e.w.Write(e.out[0 : nn/3*4]); 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[:], e.buf[:e.nbuf])
   238  		_, e.err = e.w.Write(e.out[:e.enc.EncodedLen(e.nbuf)])
   239  		e.nbuf = 0
   240  	}
   241  	return e.err
   242  }
   243  
   244  // NewEncoder returns a new base64 stream encoder. Data written to
   245  // the returned writer will be encoded using enc and then written to w.
   246  // Base64 encodings operate in 4-byte blocks; when finished
   247  // writing, the caller must Close the returned encoder to flush any
   248  // partially written blocks.
   249  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   250  	return &encoder{enc: enc, w: w}
   251  }
   252  
   253  // EncodedLen returns the length in bytes of the base64 encoding
   254  // of an input buffer of length n.
   255  func (enc *Encoding) EncodedLen(n int) int {
   256  	if enc.padChar == NoPadding {
   257  		return (n*8 + 5) / 6 // minimum # chars at 6 bits per char
   258  	}
   259  	return (n + 2) / 3 * 4 // minimum # 4-char quanta, 3 bytes each
   260  }
   261  
   262  /*
   263   * Decoder
   264   */
   265  
   266  type CorruptInputError int64
   267  
   268  func (e CorruptInputError) Error() string {
   269  	return "illegal base64 data at input byte " + strconv.FormatInt(int64(e), 10)
   270  }
   271  
   272  // decode is like Decode but returns an additional 'end' value, which
   273  // indicates if end-of-message padding or a partial quantum was encountered
   274  // and thus any additional data is an error.
   275  func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
   276  	si := 0
   277  
   278  	for si < len(src) && !end {
   279  		// Decode quantum using the base64 alphabet
   280  		var dbuf [4]byte
   281  		dinc, dlen := 3, 4
   282  
   283  		for j := 0; j < len(dbuf); j++ {
   284  			if len(src) == si {
   285  				switch {
   286  				case j == 0:
   287  					return n, false, nil
   288  				case j == 1, enc.padChar != NoPadding:
   289  					return n, false, CorruptInputError(si - j)
   290  				}
   291  				dinc, dlen, end = j-1, j, true
   292  				break
   293  			}
   294  			in := src[si]
   295  
   296  			si++
   297  
   298  			out := enc.decodeMap[in]
   299  			if out != 0xFF {
   300  				dbuf[j] = out
   301  				continue
   302  			}
   303  
   304  			if in == '\n' || in == '\r' {
   305  				j--
   306  				continue
   307  			}
   308  			if rune(in) == enc.padChar {
   309  				// We've reached the end and there's padding
   310  				switch j {
   311  				case 0, 1:
   312  					// incorrect padding
   313  					return n, false, CorruptInputError(si - 1)
   314  				case 2:
   315  					// "==" is expected, the first "=" is already consumed.
   316  					// skip over newlines
   317  					for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
   318  						si++
   319  					}
   320  					if si == len(src) {
   321  						// not enough padding
   322  						return n, false, CorruptInputError(len(src))
   323  					}
   324  					if rune(src[si]) != enc.padChar {
   325  						// incorrect padding
   326  						return n, false, CorruptInputError(si - 1)
   327  					}
   328  
   329  					si++
   330  				}
   331  				// skip over newlines
   332  				for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
   333  					si++
   334  				}
   335  				if si < len(src) {
   336  					// trailing garbage
   337  					err = CorruptInputError(si)
   338  				}
   339  				dinc, dlen, end = 3, j, true
   340  				break
   341  			}
   342  			return n, false, CorruptInputError(si - 1)
   343  		}
   344  
   345  		// Convert 4x 6bit source bytes into 3 bytes
   346  		val := uint(dbuf[0])<<18 | uint(dbuf[1])<<12 | uint(dbuf[2])<<6 | uint(dbuf[3])
   347  		dbuf[2], dbuf[1], dbuf[0] = byte(val>>0), byte(val>>8), byte(val>>16)
   348  		switch dlen {
   349  		case 4:
   350  			dst[2] = dbuf[2]
   351  			dbuf[2] = 0
   352  			fallthrough
   353  		case 3:
   354  			dst[1] = dbuf[1]
   355  			if enc.strict && dbuf[2] != 0 {
   356  				return n, end, CorruptInputError(si - 1)
   357  			}
   358  			dbuf[1] = 0
   359  			fallthrough
   360  		case 2:
   361  			dst[0] = dbuf[0]
   362  			if enc.strict && (dbuf[1] != 0 || dbuf[2] != 0) {
   363  				return n, end, CorruptInputError(si - 2)
   364  			}
   365  		}
   366  		dst = dst[dinc:]
   367  		n += dlen - 1
   368  	}
   369  
   370  	return n, end, err
   371  }
   372  
   373  // Decode decodes src using the encoding enc. It writes at most
   374  // DecodedLen(len(src)) bytes to dst and returns the number of bytes
   375  // written. If src contains invalid base64 data, it will return the
   376  // number of bytes successfully written and CorruptInputError.
   377  // New line characters (\r and \n) are ignored.
   378  func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
   379  	n, _, err = enc.decode(dst, src)
   380  	return
   381  }
   382  
   383  // DecodeString returns the bytes represented by the base64 string s.
   384  func (enc *Encoding) DecodeString(s string) ([]byte, error) {
   385  	dbuf := make([]byte, enc.DecodedLen(len(s)))
   386  	n, _, err := enc.decode(dbuf, []byte(s))
   387  	return dbuf[:n], err
   388  }
   389  
   390  type decoder struct {
   391  	err     error
   392  	readErr error // error from r.Read
   393  	enc     *Encoding
   394  	r       io.Reader
   395  	end     bool       // saw end of message
   396  	buf     [1024]byte // leftover input
   397  	nbuf    int
   398  	out     []byte // leftover decoded output
   399  	outbuf  [1024 / 4 * 3]byte
   400  }
   401  
   402  func (d *decoder) Read(p []byte) (n int, err error) {
   403  	// Use leftover decoded output from last read.
   404  	if len(d.out) > 0 {
   405  		n = copy(p, d.out)
   406  		d.out = d.out[n:]
   407  		return n, nil
   408  	}
   409  
   410  	if d.err != nil {
   411  		return 0, d.err
   412  	}
   413  
   414  	// This code assumes that d.r strips supported whitespace ('\r' and '\n').
   415  
   416  	// Refill buffer.
   417  	for d.nbuf < 4 && d.readErr == nil {
   418  		nn := len(p) / 3 * 4
   419  		if nn < 4 {
   420  			nn = 4
   421  		}
   422  		if nn > len(d.buf) {
   423  			nn = len(d.buf)
   424  		}
   425  		nn, d.readErr = d.r.Read(d.buf[d.nbuf:nn])
   426  		d.nbuf += nn
   427  	}
   428  
   429  	if d.nbuf < 4 {
   430  		if d.enc.padChar == NoPadding && d.nbuf > 0 {
   431  			// Decode final fragment, without padding.
   432  			var nw int
   433  			nw, _, d.err = d.enc.decode(d.outbuf[:], d.buf[:d.nbuf])
   434  			d.nbuf = 0
   435  			d.end = true
   436  			d.out = d.outbuf[:nw]
   437  			n = copy(p, d.out)
   438  			d.out = d.out[n:]
   439  			if n > 0 || len(p) == 0 && len(d.out) > 0 {
   440  				return n, nil
   441  			}
   442  			if d.err != nil {
   443  				return 0, d.err
   444  			}
   445  		}
   446  		d.err = d.readErr
   447  		if d.err == io.EOF && d.nbuf > 0 {
   448  			d.err = io.ErrUnexpectedEOF
   449  		}
   450  		return 0, d.err
   451  	}
   452  
   453  	// Decode chunk into p, or d.out and then p if p is too small.
   454  	nr := d.nbuf / 4 * 4
   455  	nw := d.nbuf / 4 * 3
   456  	if nw > len(p) {
   457  		nw, d.end, d.err = d.enc.decode(d.outbuf[:], d.buf[:nr])
   458  		d.out = d.outbuf[:nw]
   459  		n = copy(p, d.out)
   460  		d.out = d.out[n:]
   461  	} else {
   462  		n, d.end, d.err = d.enc.decode(p, d.buf[:nr])
   463  	}
   464  	d.nbuf -= nr
   465  	copy(d.buf[:d.nbuf], d.buf[nr:])
   466  	return n, d.err
   467  }
   468  
   469  type newlineFilteringReader struct {
   470  	wrapped io.Reader
   471  }
   472  
   473  func (r *newlineFilteringReader) Read(p []byte) (int, error) {
   474  	n, err := r.wrapped.Read(p)
   475  	for n > 0 {
   476  		offset := 0
   477  		for i, b := range p[:n] {
   478  			if b != '\r' && b != '\n' {
   479  				if i != offset {
   480  					p[offset] = b
   481  				}
   482  				offset++
   483  			}
   484  		}
   485  		if offset > 0 {
   486  			return offset, err
   487  		}
   488  		// Previous buffer entirely whitespace, read again
   489  		n, err = r.wrapped.Read(p)
   490  	}
   491  	return n, err
   492  }
   493  
   494  // NewDecoder constructs a new base64 stream decoder.
   495  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   496  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   497  }
   498  
   499  // DecodedLen returns the maximum length in bytes of the decoded data
   500  // corresponding to n bytes of base64-encoded data.
   501  func (enc *Encoding) DecodedLen(n int) int {
   502  	if enc.padChar == NoPadding {
   503  		// Unpadded data may end with partial block of 2-3 characters.
   504  		return n * 6 / 8
   505  	}
   506  	// Padded base64 should always be a multiple of 4 characters in length.
   507  	return n / 4 * 3
   508  }