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)
| 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. |
| 70 | func 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. |
| 89 | func getUnsignedIntegerValue(input []byte, ainfo AdditionalInfo) uint64 { |
no test coverage detected