aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/base64
diff options
context:
space:
mode:
authorCaleb Spare <cespare@gmail.com>2016-03-13 17:59:26 -0700
committerIan Lance Taylor <iant@golang.org>2016-03-15 20:43:04 +0000
commit87151c82b68023e4224b016a6a66ead2c4b8ece7 (patch)
tree0a3a2e283eeafd6badfedcb61fc75d6240342728 /src/encoding/base64
parent95c6c5f36bb04e66d6a9523b3ad590faa6d563dd (diff)
downloadgo-87151c82b68023e4224b016a6a66ead2c4b8ece7.tar.gz
go-87151c82b68023e4224b016a6a66ead2c4b8ece7.zip
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/20649 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/encoding/base64')
-rw-r--r--src/encoding/base64/base64.go2
-rw-r--r--src/encoding/base64/base64_test.go45
2 files changed, 46 insertions, 1 deletions
diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go
index 0de9b40f85..c2116d8a34 100644
--- a/src/encoding/base64/base64.go
+++ b/src/encoding/base64/base64.go
@@ -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
diff --git a/src/encoding/base64/base64_test.go b/src/encoding/base64/base64_test.go
index eebf113212..19ddb92f64 100644
--- a/src/encoding/base64/base64_test.go
+++ b/src/encoding/base64/base64_test.go
@@ -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)