]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/base32, encoding/base64: a small stack-space optimization.
authorNigel Tao <nigeltao@golang.org>
Tue, 12 Mar 2013 00:24:24 +0000 (11:24 +1100)
committerNigel Tao <nigeltao@golang.org>
Tue, 12 Mar 2013 00:24:24 +0000 (11:24 +1100)
R=dsymonds, dave
CC=golang-dev
https://golang.org/cl/7568045

src/pkg/encoding/base32/base32.go
src/pkg/encoding/base64/base64.go

index 738a960d7f2baac4db4f36238865116304490ced..6c5d8d3a9e9c50ae7526e7a3180750c0670e06a7 100644 (file)
@@ -230,7 +230,7 @@ func (e CorruptInputError) Error() string {
 // indicates if end-of-message padding was encountered and thus any
 // additional data is an error.
 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
-       osrc := src
+       olen := len(src)
        for len(src) > 0 && !end {
                // Decode quantum using the base32 alphabet
                var dbuf [8]byte
@@ -238,7 +238,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
 
                for j := 0; j < 8; {
                        if len(src) == 0 {
-                               return n, false, CorruptInputError(len(osrc) - len(src) - j)
+                               return n, false, CorruptInputError(olen - len(src) - j)
                        }
                        in := src[0]
                        src = src[1:]
@@ -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
                                if len(src)+j < 8-1 {
                                        // not enough padding
-                                       return n, false, CorruptInputError(len(osrc))
+                                       return n, false, CorruptInputError(olen)
                                }
                                for k := 0; k < 8-1-j; k++ {
                                        if len(src) > k && src[k] != '=' {
                                                // 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
@@ -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
                                // src bytes do not yield enough information to decode a dst byte.
                                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
                        }
                        dbuf[j] = enc.decodeMap[in]
                        if dbuf[j] == 0xFF {
-                               return n, false, CorruptInputError(len(osrc) - len(src) - 1)
+                               return n, false, CorruptInputError(olen - len(src) - 1)
                        }
                        j++
                }
index 8ccd9127e11ce269021073f31668fe822c796774..26dd7f7b99f687952e7931d6318f5380a5dd14d4 100644 (file)
@@ -210,7 +210,7 @@ func (e CorruptInputError) Error() string {
 // indicates if end-of-message padding was encountered and thus any
 // additional data is an error.
 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
-       osrc := src
+       olen := len(src)
        for len(src) > 0 && !end {
                // Decode quantum using the base64 alphabet
                var dbuf [4]byte
@@ -218,7 +218,7 @@ func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
 
                for j := 0; j < 4; {
                        if len(src) == 0 {
-                               return n, false, CorruptInputError(len(osrc) - len(src) - j)
+                               return n, false, CorruptInputError(olen - len(src) - j)
                        }
                        in := src[0]
                        src = src[1:]
@@ -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
                                if len(src)+j < 4-1 {
                                        // not enough padding
-                                       return n, false, CorruptInputError(len(osrc))
+                                       return n, false, CorruptInputError(olen)
                                }
                                if len(src) > 0 && src[0] != '=' {
                                        // incorrect padding
-                                       return n, false, CorruptInputError(len(osrc) - len(src) - 1)
+                                       return n, false, CorruptInputError(olen - len(src) - 1)
                                }
                                dlen, end = j, true
                                break
                        }
                        dbuf[j] = enc.decodeMap[in]
                        if dbuf[j] == 0xFF {
-                               return n, false, CorruptInputError(len(osrc) - len(src) - 1)
+                               return n, false, CorruptInputError(olen - len(src) - 1)
                        }
                        j++
                }