mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-13 11:05:46 +00:00
https://github.com/XTLS/Xray-core/pull/7124#issuecomment-6110557806 https://github.com/XTLS/Xray-core/pull/7132#issuecomment-6118169645 https://github.com/XTLS/Xray-core/pull/7132#issuecomment-6126428359
30 lines
799 B
Go
30 lines
799 B
Go
package base64
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"encoding/base64"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
)
|
|
|
|
// Encode returns prefixed Base64 text, optionally compressed with gzip at the highest level.
|
|
func Encode(data []byte, zip bool) (string, error) {
|
|
if !zip {
|
|
return Prefix + base64.StdEncoding.EncodeToString(data), nil
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
writer, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
|
|
if err != nil {
|
|
return "", errors.New("failed to compress gzip").Base(err)
|
|
}
|
|
if _, err := writer.Write(data); err != nil {
|
|
return "", errors.New("failed to compress gzip").Base(err)
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
return "", errors.New("failed to compress gzip").Base(err)
|
|
}
|
|
return GzipPrefix + base64.StdEncoding.EncodeToString(buf.Bytes()), nil
|
|
}
|