-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathenc.go
78 lines (68 loc) · 1.79 KB
/
enc.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// © Broadcom. All Rights Reserved.
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: Apache-2.0
package util
import (
"bytes"
"compress/gzip"
"encoding/base64"
"io"
)
// Base64Decode returns a byte slice decoded from a base64 byte slice.
func Base64Decode(src []byte) ([]byte, error) {
b64 := base64.StdEncoding
dst := make([]byte, b64.DecodedLen(len(src)))
n, err := b64.Decode(dst, src)
if err != nil {
return nil, err
}
return dst[:n], nil
}
// TryToDecodeBase64Gzip base64-decodes the provided data until the
// DecodeString function fails. If the result is gzipped, then it is
// decompressed and returned. Otherwise the decoded data is returned.
//
// This function will also return the original data as a string if it was
// neither base64 encoded or gzipped.
func TryToDecodeBase64Gzip(data []byte) (string, error) {
if len(data) == 0 {
return "", nil
}
gzipOrPlainText := data
for {
decoded, err := Base64Decode(data)
if err != nil {
break
}
data = decoded
gzipOrPlainText = data
}
r, err := gzip.NewReader(bytes.NewReader(gzipOrPlainText))
if err != nil {
//nolint:nilerr
return string(gzipOrPlainText), nil
}
plainText, err := io.ReadAll(r)
if err != nil {
return "", err
}
return string(plainText), nil
}
// EncodeGzipBase64 compresses the input string using gzip and then encodes it
// using base64.
func EncodeGzipBase64(s string) (string, error) {
var zbuf bytes.Buffer
zw := gzip.NewWriter(&zbuf)
if _, err := zw.Write([]byte(s)); err != nil {
return "", err
}
// Flush before closing to ensure all data is written.
if err := zw.Flush(); err != nil {
return "", err
}
if err := zw.Close(); err != nil {
return "", err
}
b64 := base64.StdEncoding.EncodeToString(zbuf.Bytes())
return b64, nil
}