github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/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  	"bytes"
    10  	"io"
    11  	"strconv"
    12  	"strings"
    13  )
    14  
    15  /*
    16   * Encodings
    17   */
    18  
    19  // An Encoding is a radix 64 encoding/decoding scheme, defined by a
    20  // 64-character alphabet.  The most common encoding is the "base64"
    21  // encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
    22  // (RFC 1421).  RFC 4648 also defines an alternate encoding, which is
    23  // the standard encoding with - and _ substituted for + and /.
    24  type Encoding struct {
    25  	encode    string
    26  	decodeMap [256]byte
    27  	padChar   rune
    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.
    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  	e := new(Encoding)
    44  	e.encode = encoder
    45  	e.padChar = StdPadding
    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  // WithPadding creates a new encoding identical to enc except
    56  // with a specified padding character, or NoPadding to disable padding.
    57  func (enc Encoding) WithPadding(padding rune) *Encoding {
    58  	enc.padChar = padding
    59  	return &enc
    60  }
    61  
    62  // StdEncoding is the standard base64 encoding, as defined in
    63  // RFC 4648.
    64  var StdEncoding = NewEncoding(encodeStd)
    65  
    66  // URLEncoding is the alternate base64 encoding defined in RFC 4648.
    67  // It is typically used in URLs and file names.
    68  var URLEncoding = NewEncoding(encodeURL)
    69  
    70  // RawStdEncoding is the standard raw, unpadded base64 encoding,
    71  // as defined in RFC 4648 section 3.2.
    72  // This is the same as StdEncoding but omits padding characters.
    73  var RawStdEncoding = StdEncoding.WithPadding(NoPadding)
    74  
    75  // URLEncoding is the unpadded alternate base64 encoding defined in RFC 4648.
    76  // It is typically used in URLs and file names.
    77  // This is the same as URLEncoding but omits padding characters.
    78  var RawURLEncoding = URLEncoding.WithPadding(NoPadding)
    79  
    80  var removeNewlinesMapper = func(r rune) rune {
    81  	if r == '\r' || r == '\n' {
    82  		return -1
    83  	}
    84  	return r
    85  }
    86  
    87  /*
    88   * Encoder
    89   */
    90  
    91  // Encode encodes src using the encoding enc, writing
    92  // EncodedLen(len(src)) bytes to dst.
    93  //
    94  // The encoding pads the output to a multiple of 4 bytes,
    95  // so Encode is not appropriate for use on individual blocks
    96  // of a large data stream.  Use NewEncoder() instead.
    97  func (enc *Encoding) Encode(dst, src []byte) {
    98  	if len(src) == 0 {
    99  		return
   100  	}
   101  
   102  	for len(src) > 0 {
   103  		var b0, b1, b2, b3 byte
   104  
   105  		// Unpack 4x 6-bit source blocks into a 4 byte
   106  		// destination quantum
   107  		switch len(src) {
   108  		default:
   109  			b3 = src[2] & 0x3F
   110  			b2 = src[2] >> 6
   111  			fallthrough
   112  		case 2:
   113  			b2 |= (src[1] << 2) & 0x3F
   114  			b1 = src[1] >> 4
   115  			fallthrough
   116  		case 1:
   117  			b1 |= (src[0] << 4) & 0x3F
   118  			b0 = src[0] >> 2
   119  		}
   120  
   121  		// Encode 6-bit blocks using the base64 alphabet
   122  		dst[0] = enc.encode[b0]
   123  		dst[1] = enc.encode[b1]
   124  		if len(src) >= 3 {
   125  			dst[2] = enc.encode[b2]
   126  			dst[3] = enc.encode[b3]
   127  		} else { // Final incomplete quantum
   128  			if len(src) >= 2 {
   129  				dst[2] = enc.encode[b2]
   130  			}
   131  			if enc.padChar != NoPadding {
   132  				if len(src) < 2 {
   133  					dst[2] = byte(enc.padChar)
   134  				}
   135  				dst[3] = byte(enc.padChar)
   136  			}
   137  			break
   138  		}
   139  
   140  		src = src[3:]
   141  		dst = dst[4:]
   142  	}
   143  }
   144  
   145  // EncodeToString returns the base64 encoding of src.
   146  func (enc *Encoding) EncodeToString(src []byte) string {
   147  	buf := make([]byte, enc.EncodedLen(len(src)))
   148  	enc.Encode(buf, src)
   149  	return string(buf)
   150  }
   151  
   152  type encoder struct {
   153  	err  error
   154  	enc  *Encoding
   155  	w    io.Writer
   156  	buf  [3]byte    // buffered data waiting to be encoded
   157  	nbuf int        // number of bytes in buf
   158  	out  [1024]byte // output buffer
   159  }
   160  
   161  func (e *encoder) Write(p []byte) (n int, err error) {
   162  	if e.err != nil {
   163  		return 0, e.err
   164  	}
   165  
   166  	// Leading fringe.
   167  	if e.nbuf > 0 {
   168  		var i int
   169  		for i = 0; i < len(p) && e.nbuf < 3; i++ {
   170  			e.buf[e.nbuf] = p[i]
   171  			e.nbuf++
   172  		}
   173  		n += i
   174  		p = p[i:]
   175  		if e.nbuf < 3 {
   176  			return
   177  		}
   178  		e.enc.Encode(e.out[:], e.buf[:])
   179  		if _, e.err = e.w.Write(e.out[:4]); e.err != nil {
   180  			return n, e.err
   181  		}
   182  		e.nbuf = 0
   183  	}
   184  
   185  	// Large interior chunks.
   186  	for len(p) >= 3 {
   187  		nn := len(e.out) / 4 * 3
   188  		if nn > len(p) {
   189  			nn = len(p)
   190  			nn -= nn % 3
   191  		}
   192  		e.enc.Encode(e.out[:], p[:nn])
   193  		if _, e.err = e.w.Write(e.out[0 : nn/3*4]); e.err != nil {
   194  			return n, e.err
   195  		}
   196  		n += nn
   197  		p = p[nn:]
   198  	}
   199  
   200  	// Trailing fringe.
   201  	for i := 0; i < len(p); i++ {
   202  		e.buf[i] = p[i]
   203  	}
   204  	e.nbuf = len(p)
   205  	n += len(p)
   206  	return
   207  }
   208  
   209  // Close flushes any pending output from the encoder.
   210  // It is an error to call Write after calling Close.
   211  func (e *encoder) Close() error {
   212  	// If there's anything left in the buffer, flush it out
   213  	if e.err == nil && e.nbuf > 0 {
   214  		e.enc.Encode(e.out[:], e.buf[:e.nbuf])
   215  		_, e.err = e.w.Write(e.out[:e.enc.EncodedLen(e.nbuf)])
   216  		e.nbuf = 0
   217  	}
   218  	return e.err
   219  }
   220  
   221  // NewEncoder returns a new base64 stream encoder.  Data written to
   222  // the returned writer will be encoded using enc and then written to w.
   223  // Base64 encodings operate in 4-byte blocks; when finished
   224  // writing, the caller must Close the returned encoder to flush any
   225  // partially written blocks.
   226  func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser {
   227  	return &encoder{enc: enc, w: w}
   228  }
   229  
   230  // EncodedLen returns the length in bytes of the base64 encoding
   231  // of an input buffer of length n.
   232  func (enc *Encoding) EncodedLen(n int) int {
   233  	if enc.padChar == NoPadding {
   234  		return (n*8 + 5) / 6 // minimum # chars at 6 bits per char
   235  	}
   236  	return (n + 2) / 3 * 4 // minimum # 4-char quanta, 3 bytes each
   237  }
   238  
   239  /*
   240   * Decoder
   241   */
   242  
   243  type CorruptInputError int64
   244  
   245  func (e CorruptInputError) Error() string {
   246  	return "illegal base64 data at input byte " + strconv.FormatInt(int64(e), 10)
   247  }
   248  
   249  // decode is like Decode but returns an additional 'end' value, which
   250  // indicates if end-of-message padding or a partial quantum was encountered
   251  // and thus any additional data is an error. This method assumes that src has been
   252  // stripped of all supported whitespace ('\r' and '\n').
   253  func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
   254  	olen := len(src)
   255  	for len(src) > 0 && !end {
   256  		// Decode quantum using the base64 alphabet
   257  		var dbuf [4]byte
   258  		dinc, dlen := 3, 4
   259  
   260  		for j := range dbuf {
   261  			if len(src) == 0 {
   262  				if enc.padChar != NoPadding || j < 2 {
   263  					return n, false, CorruptInputError(olen - len(src) - j)
   264  				}
   265  				dinc, dlen, end = j-1, j, true
   266  				break
   267  			}
   268  			in := src[0]
   269  			src = src[1:]
   270  			if rune(in) == enc.padChar {
   271  				// We've reached the end and there's padding
   272  				switch j {
   273  				case 0, 1:
   274  					// incorrect padding
   275  					return n, false, CorruptInputError(olen - len(src) - 1)
   276  				case 2:
   277  					// "==" is expected, the first "=" is already consumed.
   278  					if len(src) == 0 {
   279  						// not enough padding
   280  						return n, false, CorruptInputError(olen)
   281  					}
   282  					if rune(src[0]) != enc.padChar {
   283  						// incorrect padding
   284  						return n, false, CorruptInputError(olen - len(src) - 1)
   285  					}
   286  					src = src[1:]
   287  				}
   288  				if len(src) > 0 {
   289  					// trailing garbage
   290  					err = CorruptInputError(olen - len(src))
   291  				}
   292  				dinc, dlen, end = 3, j, true
   293  				break
   294  			}
   295  			dbuf[j] = enc.decodeMap[in]
   296  			if dbuf[j] == 0xFF {
   297  				return n, false, CorruptInputError(olen - len(src) - 1)
   298  			}
   299  		}
   300  
   301  		// Pack 4x 6-bit source blocks into 3 byte destination
   302  		// quantum
   303  		switch dlen {
   304  		case 4:
   305  			dst[2] = dbuf[2]<<6 | dbuf[3]
   306  			fallthrough
   307  		case 3:
   308  			dst[1] = dbuf[1]<<4 | dbuf[2]>>2
   309  			fallthrough
   310  		case 2:
   311  			dst[0] = dbuf[0]<<2 | dbuf[1]>>4
   312  		}
   313  		dst = dst[dinc:]
   314  		n += dlen - 1
   315  	}
   316  
   317  	return n, end, err
   318  }
   319  
   320  // Decode decodes src using the encoding enc.  It writes at most
   321  // DecodedLen(len(src)) bytes to dst and returns the number of bytes
   322  // written.  If src contains invalid base64 data, it will return the
   323  // number of bytes successfully written and CorruptInputError.
   324  // New line characters (\r and \n) are ignored.
   325  func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
   326  	src = bytes.Map(removeNewlinesMapper, src)
   327  	n, _, err = enc.decode(dst, src)
   328  	return
   329  }
   330  
   331  // DecodeString returns the bytes represented by the base64 string s.
   332  func (enc *Encoding) DecodeString(s string) ([]byte, error) {
   333  	s = strings.Map(removeNewlinesMapper, s)
   334  	dbuf := make([]byte, enc.DecodedLen(len(s)))
   335  	n, _, err := enc.decode(dbuf, []byte(s))
   336  	return dbuf[:n], err
   337  }
   338  
   339  type decoder struct {
   340  	err    error
   341  	enc    *Encoding
   342  	r      io.Reader
   343  	end    bool       // saw end of message
   344  	buf    [1024]byte // leftover input
   345  	nbuf   int
   346  	out    []byte // leftover decoded output
   347  	outbuf [1024 / 4 * 3]byte
   348  }
   349  
   350  func (d *decoder) Read(p []byte) (n int, err error) {
   351  	if d.err != nil {
   352  		return 0, d.err
   353  	}
   354  
   355  	// Use leftover decoded output from last read.
   356  	if len(d.out) > 0 {
   357  		n = copy(p, d.out)
   358  		d.out = d.out[n:]
   359  		return n, nil
   360  	}
   361  
   362  	// Read a chunk.
   363  	nn := len(p) / 3 * 4
   364  	if nn < 4 {
   365  		nn = 4
   366  	}
   367  	if nn > len(d.buf) {
   368  		nn = len(d.buf)
   369  	}
   370  	nn, d.err = io.ReadAtLeast(d.r, d.buf[d.nbuf:nn], 4-d.nbuf)
   371  	d.nbuf += nn
   372  	if d.err != nil || d.nbuf < 4 {
   373  		return 0, d.err
   374  	}
   375  
   376  	// Decode chunk into p, or d.out and then p if p is too small.
   377  	nr := d.nbuf / 4 * 4
   378  	nw := d.nbuf / 4 * 3
   379  	if nw > len(p) {
   380  		nw, d.end, d.err = d.enc.decode(d.outbuf[:], d.buf[:nr])
   381  		d.out = d.outbuf[:nw]
   382  		n = copy(p, d.out)
   383  		d.out = d.out[n:]
   384  	} else {
   385  		n, d.end, d.err = d.enc.decode(p, d.buf[:nr])
   386  	}
   387  	d.nbuf -= nr
   388  	for i := 0; i < d.nbuf; i++ {
   389  		d.buf[i] = d.buf[i+nr]
   390  	}
   391  
   392  	if d.err == nil {
   393  		d.err = err
   394  	}
   395  	return n, d.err
   396  }
   397  
   398  type newlineFilteringReader struct {
   399  	wrapped io.Reader
   400  }
   401  
   402  func (r *newlineFilteringReader) Read(p []byte) (int, error) {
   403  	n, err := r.wrapped.Read(p)
   404  	for n > 0 {
   405  		offset := 0
   406  		for i, b := range p[:n] {
   407  			if b != '\r' && b != '\n' {
   408  				if i != offset {
   409  					p[offset] = b
   410  				}
   411  				offset++
   412  			}
   413  		}
   414  		if offset > 0 {
   415  			return offset, err
   416  		}
   417  		// Previous buffer entirely whitespace, read again
   418  		n, err = r.wrapped.Read(p)
   419  	}
   420  	return n, err
   421  }
   422  
   423  // NewDecoder constructs a new base64 stream decoder.
   424  func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
   425  	return &decoder{enc: enc, r: &newlineFilteringReader{r}}
   426  }
   427  
   428  // DecodedLen returns the maximum length in bytes of the decoded data
   429  // corresponding to n bytes of base64-encoded data.
   430  func (enc *Encoding) DecodedLen(n int) int {
   431  	if enc.padChar == NoPadding {
   432  		// Unpadded data may end with partial block of 2-3 characters.
   433  		return (n*6 + 7) / 8
   434  	}
   435  	// Padded base64 should always be a multiple of 4 characters in length.
   436  	return n / 4 * 3
   437  }