-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdec_b64.go
47 lines (40 loc) · 1.04 KB
/
dec_b64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package jx
import (
"github.com/segmentio/asm/base64"
"github.com/go-faster/errors"
)
// Base64 decodes base64 encoded data from string.
//
// Same as encoding/json, base64.StdEncoding or RFC 4648.
func (d *Decoder) Base64() ([]byte, error) {
if d.Next() == Null {
if err := d.Null(); err != nil {
return nil, errors.Wrap(err, "read null")
}
return nil, nil
}
return d.Base64Append([]byte{})
}
// Base64Append appends base64 encoded data from string.
//
// Same as encoding/json, base64.StdEncoding or RFC 4648.
func (d *Decoder) Base64Append(b []byte) ([]byte, error) {
if d.Next() == Null {
if err := d.Null(); err != nil {
return nil, errors.Wrap(err, "read null")
}
return b, nil
}
buf, err := d.StrBytes()
if err != nil {
return nil, errors.Wrap(err, "bytes")
}
decodedLen := base64.StdEncoding.DecodedLen(len(buf))
start := len(b)
b = append(b, make([]byte, decodedLen)...)
n, err := base64.StdEncoding.Decode(b[start:], buf)
if err != nil {
return nil, errors.Wrap(err, "decode")
}
return b[:start+n], nil
}