rebase: vendor files required for kmip

Signed-off-by: Rakshith R <rar@redhat.com>
This commit is contained in:
Rakshith R
2022-08-16 15:18:06 +05:30
committed by mergify[bot]
parent 0c33a33d5c
commit e72ed593be
186 changed files with 39195 additions and 203 deletions

View 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
}

View 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, "")
}