mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 16:30:07 +08:00
Some checks failed
Test / test (1.20, macos-13) (push) Has been cancelled
Test / test (1.20, macos-latest) (push) Has been cancelled
Test / test (1.20, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.20, ubuntu-latest) (push) Has been cancelled
Test / test (1.20, windows-latest) (push) Has been cancelled
Test / test (1.21, macos-13) (push) Has been cancelled
Test / test (1.21, macos-latest) (push) Has been cancelled
Test / test (1.21, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.21, ubuntu-latest) (push) Has been cancelled
Test / test (1.21, windows-latest) (push) Has been cancelled
Test / test (1.22, macos-13) (push) Has been cancelled
Test / test (1.22, macos-latest) (push) Has been cancelled
Test / test (1.22, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.22, ubuntu-latest) (push) Has been cancelled
Test / test (1.22, windows-latest) (push) Has been cancelled
Test / test (1.23, macos-13) (push) Has been cancelled
Test / test (1.23, macos-latest) (push) Has been cancelled
Test / test (1.23, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.23, ubuntu-latest) (push) Has been cancelled
Test / test (1.23, windows-latest) (push) Has been cancelled
Test / test (1.24, macos-13) (push) Has been cancelled
Test / test (1.24, macos-latest) (push) Has been cancelled
Test / test (1.24, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.24, ubuntu-latest) (push) Has been cancelled
Test / test (1.24, windows-latest) (push) Has been cancelled
Trigger CMFA Update / trigger-CMFA-update (push) Has been cancelled
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package convert
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
encRaw = base64.RawStdEncoding
|
|
enc = base64.StdEncoding
|
|
)
|
|
|
|
// DecodeBase64 try to decode content from the given bytes,
|
|
// which can be in base64.RawStdEncoding, base64.StdEncoding or just plaintext.
|
|
func DecodeBase64(buf []byte) []byte {
|
|
result, err := tryDecodeBase64(buf)
|
|
if err != nil {
|
|
return buf
|
|
}
|
|
return result
|
|
}
|
|
|
|
func tryDecodeBase64(buf []byte) ([]byte, error) {
|
|
dBuf := make([]byte, encRaw.DecodedLen(len(buf)))
|
|
n, err := encRaw.Decode(dBuf, buf)
|
|
if err != nil {
|
|
n, err = enc.Decode(dBuf, buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return dBuf[:n], nil
|
|
}
|
|
|
|
func urlSafe(data string) string {
|
|
return strings.NewReplacer("+", "-", "/", "_").Replace(data)
|
|
}
|
|
|
|
func decodeUrlSafe(data string) string {
|
|
dcBuf, err := base64.RawURLEncoding.DecodeString(data)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return string(dcBuf)
|
|
}
|
|
|
|
func TryDecodeBase64(s string) (decoded []byte, err error) {
|
|
if len(s)%4 == 0 {
|
|
if decoded, err = base64.StdEncoding.DecodeString(s); err == nil {
|
|
return
|
|
}
|
|
if decoded, err = base64.URLEncoding.DecodeString(s); err == nil {
|
|
return
|
|
}
|
|
} else {
|
|
if decoded, err = base64.RawStdEncoding.DecodeString(s); err == nil {
|
|
return
|
|
}
|
|
if decoded, err = base64.RawURLEncoding.DecodeString(s); err == nil {
|
|
return
|
|
}
|
|
}
|
|
return nil, fmt.Errorf("invalid base64-encoded string")
|
|
}
|