diff options
| author | Gustav Westling <zegl@westling.xyz> | 2017-07-02 13:27:47 +0200 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2017-07-06 18:05:22 +0000 |
| commit | 9d2de778052ecbaf042d0b01c39e41db55ca6eaa (patch) | |
| tree | 68dcb9904646e83d4f9ce8d19ff8e9be64cd05c2 /src/encoding/base32/base32.go | |
| parent | f3b5a2bc1983ddb83d72e741b176993d9b800faf (diff) | |
| download | go-9d2de778052ecbaf042d0b01c39e41db55ca6eaa.tar.xz | |
encoding/base32: support custom and disabled padding when decoding
CL 38634 added support for custom (and disabled) padding characters
when encoding, but didn't update the decoding paths. This adds
decoding support.
Fixes #20854
Change-Id: I9fb1a0aaebb27f1204c9f726a780d5784eb71024
Reviewed-on: https://go-review.googlesource.com/47341
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/encoding/base32/base32.go')
| -rw-r--r-- | src/encoding/base32/base32.go | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/encoding/base32/base32.go b/src/encoding/base32/base32.go index 437b41d225..0270e8f4d4 100644 --- a/src/encoding/base32/base32.go +++ b/src/encoding/base32/base32.go @@ -279,19 +279,28 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { dlen := 8 for j := 0; j < 8; { - if len(src) == 0 { + + // We have reached the end and are missing padding + if len(src) == 0 && enc.padChar != NoPadding { return n, false, CorruptInputError(olen - len(src) - j) } + + // We have reached the end and are not expecing any padding + if len(src) == 0 && enc.padChar == NoPadding { + dlen, end = j, true + break + } + in := src[0] src = src[1:] - if in == '=' && j >= 2 && len(src) < 8 { + if in == byte(enc.padChar) && j >= 2 && len(src) < 8 { // We've reached the end and there's padding if len(src)+j < 8-1 { // not enough padding return n, false, CorruptInputError(olen) } for k := 0; k < 8-1-j; k++ { - if len(src) > k && src[k] != '=' { + if len(src) > k && src[k] != byte(enc.padChar) { // incorrect padding return n, false, CorruptInputError(olen - len(src) + k - 1) } @@ -484,4 +493,11 @@ func NewDecoder(enc *Encoding, r io.Reader) io.Reader { // DecodedLen returns the maximum length in bytes of the decoded data // corresponding to n bytes of base32-encoded data. -func (enc *Encoding) DecodedLen(n int) int { return n / 8 * 5 } +func (enc *Encoding) DecodedLen(n int) int { + if enc.padChar == NoPadding { + // +6 represents the missing padding + return (n + 6) / 8 * 5 + } + + return n / 8 * 5 +} |
