Commit 96e0c0c7 authored by Nigel Tao's avatar Nigel Tao

encoding/base32, encoding/base64: a small stack-space optimization.

R=dsymonds, dave
CC=golang-dev
https://golang.org/cl/7568045
parent 2f2271db
...@@ -230,7 +230,7 @@ func (e CorruptInputError) Error() string { ...@@ -230,7 +230,7 @@ func (e CorruptInputError) Error() string {
// indicates if end-of-message padding was encountered and thus any // indicates if end-of-message padding was encountered and thus any
// additional data is an error. // additional data is an error.
func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
osrc := src olen := len(src)
for len(src) > 0 && !end { for len(src) > 0 && !end {
// Decode quantum using the base32 alphabet // Decode quantum using the base32 alphabet
var dbuf [8]byte var dbuf [8]byte
...@@ -238,7 +238,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { ...@@ -238,7 +238,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
for j := 0; j < 8; { for j := 0; j < 8; {
if len(src) == 0 { if len(src) == 0 {
return n, false, CorruptInputError(len(osrc) - len(src) - j) return n, false, CorruptInputError(olen - len(src) - j)
} }
in := src[0] in := src[0]
src = src[1:] src = src[1:]
...@@ -250,12 +250,12 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { ...@@ -250,12 +250,12 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
// We've reached the end and there's padding // We've reached the end and there's padding
if len(src)+j < 8-1 { if len(src)+j < 8-1 {
// not enough padding // not enough padding
return n, false, CorruptInputError(len(osrc)) return n, false, CorruptInputError(olen)
} }
for k := 0; k < 8-1-j; k++ { for k := 0; k < 8-1-j; k++ {
if len(src) > k && src[k] != '=' { if len(src) > k && src[k] != '=' {
// incorrect padding // incorrect padding
return n, false, CorruptInputError(len(osrc) - len(src) + k - 1) return n, false, CorruptInputError(olen - len(src) + k - 1)
} }
} }
dlen, end = j, true dlen, end = j, true
...@@ -265,13 +265,13 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { ...@@ -265,13 +265,13 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
// Examples" for an illustration for how the the 1st, 3rd and 6th base32 // Examples" for an illustration for how the the 1st, 3rd and 6th base32
// src bytes do not yield enough information to decode a dst byte. // src bytes do not yield enough information to decode a dst byte.
if dlen == 1 || dlen == 3 || dlen == 6 { if dlen == 1 || dlen == 3 || dlen == 6 {
return n, false, CorruptInputError(len(osrc) - len(src) - 1) return n, false, CorruptInputError(olen - len(src) - 1)
} }
break break
} }
dbuf[j] = enc.decodeMap[in] dbuf[j] = enc.decodeMap[in]
if dbuf[j] == 0xFF { if dbuf[j] == 0xFF {
return n, false, CorruptInputError(len(osrc) - len(src) - 1) return n, false, CorruptInputError(olen - len(src) - 1)
} }
j++ j++
} }
......
...@@ -210,7 +210,7 @@ func (e CorruptInputError) Error() string { ...@@ -210,7 +210,7 @@ func (e CorruptInputError) Error() string {
// indicates if end-of-message padding was encountered and thus any // indicates if end-of-message padding was encountered and thus any
// additional data is an error. // additional data is an error.
func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
osrc := src olen := len(src)
for len(src) > 0 && !end { for len(src) > 0 && !end {
// Decode quantum using the base64 alphabet // Decode quantum using the base64 alphabet
var dbuf [4]byte var dbuf [4]byte
...@@ -218,7 +218,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { ...@@ -218,7 +218,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
for j := 0; j < 4; { for j := 0; j < 4; {
if len(src) == 0 { if len(src) == 0 {
return n, false, CorruptInputError(len(osrc) - len(src) - j) return n, false, CorruptInputError(olen - len(src) - j)
} }
in := src[0] in := src[0]
src = src[1:] src = src[1:]
...@@ -230,18 +230,18 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { ...@@ -230,18 +230,18 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
// We've reached the end and there's padding // We've reached the end and there's padding
if len(src)+j < 4-1 { if len(src)+j < 4-1 {
// not enough padding // not enough padding
return n, false, CorruptInputError(len(osrc)) return n, false, CorruptInputError(olen)
} }
if len(src) > 0 && src[0] != '=' { if len(src) > 0 && src[0] != '=' {
// incorrect padding // incorrect padding
return n, false, CorruptInputError(len(osrc) - len(src) - 1) return n, false, CorruptInputError(olen - len(src) - 1)
} }
dlen, end = j, true dlen, end = j, true
break break
} }
dbuf[j] = enc.decodeMap[in] dbuf[j] = enc.decodeMap[in]
if dbuf[j] == 0xFF { if dbuf[j] == 0xFF {
return n, false, CorruptInputError(len(osrc) - len(src) - 1) return n, false, CorruptInputError(olen - len(src) - 1)
} }
j++ j++
} }
......
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