MCPcopy Create free account
hub / github.com/WICG/webpackage / unsignedIntegerDeterministic

Function unsignedIntegerDeterministic

go/internal/cbor/deterministic.go:70–86  ·  view source on GitHub ↗

unsignedIntegerDeterministic calculates the value of the unsigned integer to ensure that the right amount of bytes is used in the CBOR encoding. It returns both, the length of the bytes array and the value of the unsigned integer.

(input []byte)

Source from the content-addressed store, hash-verified

68// the right amount of bytes is used in the CBOR encoding. It returns both,
69// the length of the bytes array and the value of the unsigned integer.
70func unsignedIntegerDeterministic(input []byte) (int, uint64, error) {
71 ainfo := convertToAdditionalInfo(input[0])
72
73 if ainfo == AdditionalInfoIndefinite || ainfo == AdditionalInfoReserved {
74 return -1, 0, fmt.Errorf("%v should not be in use in deterministic CBOR.", ainfo)
75 }
76
77 lengthInBytes := ainfo.getAdditionalInfoLength()
78 limit := ainfo.getAdditionalInfoValueLowerLimit()
79 value := getUnsignedIntegerValue(input, ainfo)
80
81 if value < limit {
82 return -1, value, fmt.Errorf("When following CBOR deterministic principles, %v should not be represented with %v bytes.", value, len(input)-1)
83 }
84
85 return lengthInBytes, value, nil
86}
87
88// getUnsignedIntegerValue converts the byte array containing the CBOR encoded unsigned integer into an actual value.
89func getUnsignedIntegerValue(input []byte, ainfo AdditionalInfo) uint64 {

Callers 4

deterministicRecFunction · 0.70
arrayDeterministicFunction · 0.70
mapDeterministicFunction · 0.70

Tested by

no test coverage detected