mirror of
https://github.com/mikefarah/yq.git
synced 2025-01-13 03:45:37 +00:00
Add support for decoding base64 strings without padding (#1604)
This commit is contained in:
parent
99b2ab610a
commit
69d3e3f3f8
@ -4,10 +4,33 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
yaml "gopkg.in/yaml.v3"
|
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 {
|
type base64Decoder struct {
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
finished bool
|
finished bool
|
||||||
@ -20,7 +43,7 @@ func NewBase64Decoder() Decoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dec *base64Decoder) Init(reader io.Reader) error {
|
func (dec *base64Decoder) Init(reader io.Reader) error {
|
||||||
dec.reader = reader
|
dec.reader = &base64Padder{Reader: reader}
|
||||||
dec.readAnything = false
|
dec.readAnything = false
|
||||||
dec.finished = false
|
dec.finished = false
|
||||||
return nil
|
return nil
|
||||||
|
@ -310,6 +310,14 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!str)::\n",
|
"D0, P[], (!!str)::\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "base64 missing padding test",
|
||||||
|
skipDoc: true,
|
||||||
|
expression: `"Y2F0cw" | @base64d`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!str)::cats\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
requiresFormat: "xml",
|
requiresFormat: "xml",
|
||||||
description: "empty xml decode",
|
description: "empty xml decode",
|
||||||
|
Loading…
Reference in New Issue
Block a user