Commit 87151c82 authored by Caleb Spare's avatar Caleb Spare Committed by Ian Lance Taylor

encoding/base64: correct DecodedLen overestimate for unpadded encodings

While we're at it, add tests for EncodedLen and DecodedLen.

Fixes #14803.

Change-Id: I200c72cf11c51669b8d9f70c6e57ece359f7ae61
Reviewed-on: https://go-review.googlesource.com/20649Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 95c6c5f3
......@@ -459,7 +459,7 @@ func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
func (enc *Encoding) DecodedLen(n int) int {
if enc.padChar == NoPadding {
// Unpadded data may end with partial block of 2-3 characters.
return (n*6 + 7) / 8
return n * 6 / 8
}
// Padded base64 should always be a multiple of 4 characters in length.
return n / 4 * 3
......
......@@ -234,6 +234,51 @@ func TestDecodeCorrupt(t *testing.T) {
}
}
func TestEncodedLen(t *testing.T) {
for _, tt := range []struct {
enc *Encoding
n int
want int
}{
{RawStdEncoding, 0, 0},
{RawStdEncoding, 1, 2},
{RawStdEncoding, 2, 3},
{RawStdEncoding, 3, 4},
{RawStdEncoding, 7, 10},
{StdEncoding, 0, 0},
{StdEncoding, 1, 4},
{StdEncoding, 2, 4},
{StdEncoding, 3, 4},
{StdEncoding, 4, 8},
{StdEncoding, 7, 12},
} {
if got := tt.enc.EncodedLen(tt.n); got != tt.want {
t.Errorf("EncodedLen(%d): got %d, want %d", tt.n, got, tt.want)
}
}
}
func TestDecodedLen(t *testing.T) {
for _, tt := range []struct {
enc *Encoding
n int
want int
}{
{RawStdEncoding, 0, 0},
{RawStdEncoding, 2, 1},
{RawStdEncoding, 3, 2},
{RawStdEncoding, 4, 3},
{RawStdEncoding, 10, 7},
{StdEncoding, 0, 0},
{StdEncoding, 4, 3},
{StdEncoding, 8, 6},
} {
if got := tt.enc.DecodedLen(tt.n); got != tt.want {
t.Errorf("DecodedLen(%d): got %d, want %d", tt.n, got, tt.want)
}
}
}
func TestBig(t *testing.T) {
n := 3*1000 + 1
raw := make([]byte, n)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment