...

Source file src/encoding/base64/base64.go

Documentation: encoding/base64

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

View as plain text