mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
rebase: vendor files required for kmip
Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
66
vendor/github.com/gemalto/kmip-go/internal/kmiputil/hex_values.go
generated
vendored
Normal file
66
vendor/github.com/gemalto/kmip-go/internal/kmiputil/hex_values.go
generated
vendored
Normal file
@ -0,0 +1,66 @@
|
||||
package kmiputil
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
|
||||
"github.com/ansel1/merry"
|
||||
)
|
||||
|
||||
var ErrInvalidHexString = merry.New("invalid hex string")
|
||||
|
||||
func DecodeUint32(b []byte) uint32 {
|
||||
// pad to 4 bytes with leading zeros
|
||||
return binary.BigEndian.Uint32(pad(b, 4))
|
||||
}
|
||||
|
||||
func DecodeUint64(b []byte) uint64 {
|
||||
// pad to 8 bytes with leading zeros
|
||||
return binary.BigEndian.Uint64(pad(b, 8))
|
||||
}
|
||||
|
||||
func pad(b []byte, l int) []byte {
|
||||
if len(b) < l {
|
||||
b2 := make([]byte, l)
|
||||
copy(b2[l-len(b):], b)
|
||||
b = b2
|
||||
}
|
||||
|
||||
return b
|
||||
}
|
||||
|
||||
// ParseHexValue attempts to parse a string formatted as a hex value
|
||||
// as described in the KMIP Profiles spec, in the "Hex representations" section.
|
||||
//
|
||||
// If the string doesn't start with the required prefix "0x", it is assumed the string
|
||||
// is not a hex representation, and nil, nil is returned.
|
||||
//
|
||||
// An ErrInvalidHexString is returned if the hex parsing fails.
|
||||
// If the max argument is >0, ErrInvalidHexString is returned if the number of bytes parsed
|
||||
// is greater than max, ignoring leading zeros. All bytes parsed are returned (including
|
||||
// leading zeros).
|
||||
func ParseHexValue(s string, max int) ([]byte, error) {
|
||||
if !strings.HasPrefix(s, "0x") {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
b, err := hex.DecodeString(s[2:])
|
||||
if err != nil {
|
||||
return nil, merry.WithCause(ErrInvalidHexString, err).Append(err.Error())
|
||||
}
|
||||
|
||||
if max > 0 {
|
||||
l := len(b)
|
||||
// minus leading zeros
|
||||
for i := 0; i < len(b) && b[i] == 0; i++ {
|
||||
l--
|
||||
}
|
||||
|
||||
if l > max {
|
||||
return nil, merry.Appendf(ErrInvalidHexString, "must be %v bytes", max)
|
||||
}
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
62
vendor/github.com/gemalto/kmip-go/internal/kmiputil/names.go
generated
vendored
Normal file
62
vendor/github.com/gemalto/kmip-go/internal/kmiputil/names.go
generated
vendored
Normal file
@ -0,0 +1,62 @@
|
||||
package kmiputil
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
var (
|
||||
nonWordAtWordBoundary = regexp.MustCompile(`(\W)([a-zA-Z][a-z])`)
|
||||
startingDigits = regexp.MustCompile(`^([\d]+)(.*)`)
|
||||
)
|
||||
|
||||
// NormalizeName converts a string into the CamelCase format required for the XML and JSON encoding
|
||||
// of KMIP values. It should be used for tag names, type names, and enumeration value names.
|
||||
// Implementation of 5.4.1.1 and 5.5.1.1 from the KMIP Profiles specification.
|
||||
func NormalizeName(s string) string {
|
||||
// 1. Replace round brackets ‘(‘, ‘)’ with spaces
|
||||
s = strings.Map(func(r rune) rune {
|
||||
switch r {
|
||||
case '(', ')':
|
||||
return ' '
|
||||
}
|
||||
|
||||
return r
|
||||
}, s)
|
||||
|
||||
// 2. If a non-word char (not alpha, digit or underscore) is followed by a letter (either upper or lower case) then a lower case letter, replace the non-word char with space
|
||||
s = nonWordAtWordBoundary.ReplaceAllString(s, " $2")
|
||||
|
||||
// 3. Replace remaining non-word chars (except whitespace) with underscore.
|
||||
s = strings.Map(func(r rune) rune {
|
||||
switch {
|
||||
case r >= 'a' && r <= 'z':
|
||||
case r >= 'A' && r <= 'Z':
|
||||
case r >= '0' && r <= '9':
|
||||
case r == '_':
|
||||
case r == ' ':
|
||||
default:
|
||||
return '_'
|
||||
}
|
||||
|
||||
return r
|
||||
}, s)
|
||||
|
||||
words := strings.Split(s, " ")
|
||||
|
||||
for i, w := range words {
|
||||
if i == 0 {
|
||||
// 4. If the first word begins with a digit, move all digits at start of first word to end of first word
|
||||
w = startingDigits.ReplaceAllString(w, `$2$1`)
|
||||
}
|
||||
|
||||
// 5. Capitalize the first letter of each word
|
||||
words[i] = cases.Title(language.AmericanEnglish, cases.NoLower).String(w)
|
||||
}
|
||||
|
||||
// 6. Concatenate all words with spaces removed
|
||||
return strings.Join(words, "")
|
||||
}
|
Reference in New Issue
Block a user