[ox/mc] Fix serialization of negative numbers in non-64 bit values
This commit is contained in:
parent
c460e0f9e0
commit
9db10ec4a1
7
deps/ox/src/ox/mc/intops.hpp
vendored
7
deps/ox/src/ox/mc/intops.hpp
vendored
@ -64,13 +64,14 @@ struct McInt {
|
|||||||
|
|
||||||
template<typename I>
|
template<typename I>
|
||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
constexpr McInt encodeInteger(I input) noexcept {
|
constexpr McInt encodeInteger(I pInput) noexcept {
|
||||||
|
auto const input = ox::ResizedInt_t<I, 64>{pInput};
|
||||||
McInt out;
|
McInt out;
|
||||||
const auto inputNegative = is_signed_v<I> && input < 0;
|
const auto inputNegative = is_signed_v<I> && input < 0;
|
||||||
// move input to uint64_t to allow consistent bit manipulation, and to avoid
|
// move input to uint64_t to allow consistent bit manipulation, and to avoid
|
||||||
// overflow concerns
|
// overflow concerns
|
||||||
uint64_t val = 0;
|
uint64_t val = 0;
|
||||||
ox_memcpy(&val, &input, sizeof(I));
|
ox_memcpy(&val, &input, sizeof(input));
|
||||||
if (val) {
|
if (val) {
|
||||||
// bits needed to represent number factoring in space possibly
|
// bits needed to represent number factoring in space possibly
|
||||||
// needed for signed bit
|
// needed for signed bit
|
||||||
@ -93,7 +94,7 @@ constexpr McInt encodeInteger(I input) noexcept {
|
|||||||
}
|
}
|
||||||
if (bytes == 9) {
|
if (bytes == 9) {
|
||||||
out.data[0] = bytesIndicator;
|
out.data[0] = bytesIndicator;
|
||||||
ox_memcpy(&out.data[1], &leVal, sizeof(I));
|
ox_memcpy(&out.data[1], &leVal, 8);
|
||||||
if (inputNegative) {
|
if (inputNegative) {
|
||||||
out.data[1] |= 0b1000'0000;
|
out.data[1] |= 0b1000'0000;
|
||||||
}
|
}
|
||||||
|
3
deps/ox/src/ox/mc/test/tests.cpp
vendored
3
deps/ox/src/ox/mc/test/tests.cpp
vendored
@ -267,6 +267,9 @@ std::map<ox::StringView, ox::Error(*)()> tests = {
|
|||||||
return OxError(0);
|
return OxError(0);
|
||||||
};
|
};
|
||||||
oxAssert(check(uint32_t(14)), "Decode of 14 failed.");
|
oxAssert(check(uint32_t(14)), "Decode of 14 failed.");
|
||||||
|
oxAssert(check(int8_t(-1)), "Decode of -1 failed.");
|
||||||
|
oxAssert(check(int16_t(-1)), "Decode of -1 failed.");
|
||||||
|
oxAssert(check(int32_t(-1)), "Decode of -1 failed.");
|
||||||
oxAssert(check(int64_t(-1)), "Decode of -1 failed.");
|
oxAssert(check(int64_t(-1)), "Decode of -1 failed.");
|
||||||
oxAssert(check(int64_t(-2)), "Decode of -2 failed.");
|
oxAssert(check(int64_t(-2)), "Decode of -2 failed.");
|
||||||
oxAssert(check(int64_t(-127)), "Decode of -127 failed.");
|
oxAssert(check(int64_t(-127)), "Decode of -127 failed.");
|
||||||
|
Loading…
Reference in New Issue
Block a user