libs5-go/encoding/encoding.go

31 lines
606 B
Go
Raw Normal View History

package encoding
2024-01-04 13:21:19 +00:00
import "encoding/base64"
2024-01-04 13:21:19 +00:00
func UnmarshalBase64UrlJSON(data []byte) ([]byte, error) {
strData := string(data)
if len(strData) >= 2 && strData[0] == '"' && strData[len(strData)-1] == '"' {
strData = strData[1 : len(strData)-1]
}
if strData == "null" {
return nil, nil
}
decodedData, err := MultibaseDecodeString(strData)
if err != nil {
if err != ErrMultibaseEncodingNotSupported {
2024-01-04 13:21:19 +00:00
return nil, err
}
} else {
return decodedData, nil
2024-01-04 13:21:19 +00:00
}
decodedData, err = base64.RawURLEncoding.DecodeString(strData)
2024-01-04 13:21:19 +00:00
if err != nil {
return nil, err
}
return decodedData, nil
}