Add support for decoding base64 strings without padding (#1604)

This commit is contained in:
TJ Miller 2023-03-17 20:30:35 -07:00 committed by GitHub
parent 99b2ab610a
commit 69d3e3f3f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -4,10 +4,33 @@ import (
"bytes"
"encoding/base64"
"io"
"strings"
yaml "gopkg.in/yaml.v3"
)
type base64Padder struct {
count uint64
io.Reader
}
func (c *base64Padder) pad(buf []byte) (int, error) {
pad := strings.Repeat("=", int(4-c.count%4))
n, err := strings.NewReader(pad).Read(buf)
c.count += uint64(n)
return n, err
}
func (c *base64Padder) Read(buf []byte) (int, error) {
n, err := c.Reader.Read(buf)
c.count += uint64(n)
if err == io.EOF && c.count%4 != 0 {
return c.pad(buf)
}
return n, err
}
type base64Decoder struct {
reader io.Reader
finished bool
@ -20,7 +43,7 @@ func NewBase64Decoder() Decoder {
}
func (dec *base64Decoder) Init(reader io.Reader) error {
dec.reader = reader
dec.reader = &base64Padder{Reader: reader}
dec.readAnything = false
dec.finished = false
return nil

View File

@ -310,6 +310,14 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
"D0, P[], (!!str)::\n",
},
},
{
description: "base64 missing padding test",
skipDoc: true,
expression: `"Y2F0cw" | @base64d`,
expected: []string{
"D0, P[], (!!str)::cats\n",
},
},
{
requiresFormat: "xml",
description: "empty xml decode",