diff options
Diffstat (limited to 'src/encoding/base64/base64.go')
| -rw-r--r-- | src/encoding/base64/base64.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/encoding/base64/base64.go b/src/encoding/base64/base64.go index 87f6897062..5db72b91e2 100644 --- a/src/encoding/base64/base64.go +++ b/src/encoding/base64/base64.go @@ -8,6 +8,7 @@ package base64 import ( "encoding/binary" "io" + "slices" "strconv" ) @@ -191,6 +192,15 @@ func (enc *Encoding) Encode(dst, src []byte) { } } +// AppendEncode appends the base64 encoded src to dst +// and returns the extended buffer. +func (enc *Encoding) AppendEncode(dst, src []byte) []byte { + n := enc.EncodedLen(len(src)) + dst = slices.Grow(dst, n) + enc.Encode(dst[len(dst):][:n], src) + return dst[:len(dst)+n] +} + // EncodeToString returns the base64 encoding of src. func (enc *Encoding) EncodeToString(src []byte) string { buf := make([]byte, enc.EncodedLen(len(src))) @@ -395,6 +405,16 @@ func (enc *Encoding) decodeQuantum(dst, src []byte, si int) (nsi, n int, err err return si, dlen - 1, err } +// AppendDecode appends the base64 decoded src to dst +// and returns the extended buffer. +// If the input is malformed, it returns the partially decoded src and an error. +func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) { + n := enc.DecodedLen(len(src)) + dst = slices.Grow(dst, n) + n, err := enc.Decode(dst[len(dst):][:n], src) + return dst[:len(dst)+n], err +} + // DecodeString returns the bytes represented by the base64 string s. func (enc *Encoding) DecodeString(s string) ([]byte, error) { dbuf := make([]byte, enc.DecodedLen(len(s))) |
