Comments (1)
Hi @al3xkr,
This is an expected (although somewhat tricky) behavior that follows from how integers are defined in the standard and stored in libcbor. Because CBOR allows 1, 2, 4, and 8 byte integers that are either positive or negative (not unsigned and signed!), there is no way of mapping the full range of negative 8B integers to any standard signed C99 type. The only way to map these values correctly to standard C99 integer types is to use uint8_t
through uint64_t
.
Specifically, CBOR negative 1B integer can represent values between -1 and -256 inclusive (see http://libcbor.readthedocs.io/en/v0.5.0/api/type_0_1.html#type-0-1), which would underflow an int8_t
(representable range -128 through 127).
Therefore cbor_get_uint8
returns uint8_t
(http://libcbor.readthedocs.io/en/v0.5.0/api/type_0_1.html#_CPPv214cbor_get_uint8PK11cbor_item_t), as opposed to int8_t
. In order to get the 'logical' value represented by the item, you can do e.g.
cbor_item_t *hkdf_alg = cbor_new_int8();
cbor_mark_negint(hkdf_alg);
int abs_i = abs(ECDH_SS_HKDF_256) - 1;
cbor_set_uint8(hkdf_alg, abs_i);
int16_t item_value = -1 * (int16_t) cbor_get_uint8(hkdf_alg) - 1;
Note that int16_t
is necessary to avoid the aforementioned underflow.
cbor.me automatically converts the displayed items to the corresponding logical values, so you see -27, but this unfortunately cannot be supported in C until we get a signed integer type that is wider than 64 bits.
Hope this helps,
Pavel
from libcbor.
Related Issues (20)
- Hi @mghicho, not really, but the current encoders can be used to encode e.g. an array or a map in a streaming manner by calling e.g. `cbor_encode_indef_array_start` and then encoding individual items and terminating, flushing buffers as needed. What would the API you have in mind look like?
- precision with embedded type in cbor_encode_indef_array_start HOT 1
- AArch32 compatiblity issues HOT 5
- Improve Bazel support HOT 2
- BUG: CBOR Key/Value Arrays are interpreted correctly but do not deallocate correctly. HOT 7
- Improve documentation on memory management
- Travis CI broken
- half-precision float NaN loss of information (nitpicky) HOT 1
- Migrate Windows CI to Circle HOT 2
- Make cbor_encode_half respect NaN data bits
- Improve streaming parser documentation, especially for error state handling
- Improve test coverage of cbor_bytestring_add_chunk failure scenarios
- Add [[nodiscard]] attribute
- Broken encoding of empty byte strings in 0.10.0 HOT 1
- FR: Vectorization HOT 1
- test_serialize_4b_bytestring / test_serialize_8b_bytestring tests fail on 32-bit systems (size_t overflow) HOT 2
- Better documentation for memory management
- Documentation references RFC 7049, which has been replaced by RFC 8949 (STD94).
- Testing for musl HOT 3
- Signaling NaN encoding broken on Windows
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libcbor.