forked from Harrm/scale-codec
-
Notifications
You must be signed in to change notification settings - Fork 7
Feature: custom config for coding and jam-codec compatibility #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
097383c
feature: custom confing for en-/decoding and jam-codec compatibility
xDimon 46f1c35
fix: mac CI
xDimon 0fcd42f
fix: review issues
xDimon 34da865
fix: review issue
xDimon a478d4d
fix: review issue
xDimon 8202755
fix: broken build
xDimon 1d6a6ec
fix: typo
xDimon 03360e1
fix: typo
xDimon 11e08f1
fix: review issue
xDimon 2d47f19
fix: review issue
xDimon ce7f144
bump
xDimon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| /** | ||
| * Copyright Quadrivium LLC | ||
| * All Rights Reserved | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <concepts> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| #include <boost/multiprecision/cpp_int.hpp> | ||
|
|
||
| #include <scale/detail/fixed_width_integer.hpp> | ||
| #include <scale/outcome/outcome_throw.hpp> | ||
| #include <scale/scale_error.hpp> | ||
| #include <scale/types.hpp> | ||
|
|
||
| namespace scale::detail { | ||
|
|
||
| /// min integer encoded by 2 bytes | ||
| constexpr static size_t kMinUint16 = (1ul << 6u); | ||
| /// min integer encoded by 4 bytes | ||
| constexpr static size_t kMinUint32 = (1ul << 14u); | ||
| /// min integer encoded as multibyte | ||
| constexpr static size_t kMinBigInteger = (1ul << 30u); | ||
|
|
||
| /// Returns the compact encoded length for the given value. | ||
| size_t compactLen(std::unsigned_integral auto val) { | ||
| if (val < kMinUint16) return 1; | ||
| if (val < kMinUint32) return 2; | ||
| if (val < kMinBigInteger) return 4; | ||
| size_t counter = 1; | ||
| while ((val >>= 8)) ++counter; | ||
| return counter; | ||
| } | ||
|
|
||
| /** | ||
| * Encodes any integer type to compact-integer representation | ||
| * @tparam T integer type | ||
| * @tparam S output stream type | ||
| * @param value integer value | ||
| * @return byte array representation of value as compact-integer | ||
| */ | ||
| template <typename T, typename S> | ||
| requires(std::integral<T> or std::is_same_v<T, CompactInteger>) | ||
| void encodeCompactInteger(T integer, S &s) { | ||
| boost::multiprecision::cpp_int value{integer}; | ||
|
|
||
| // cannot encode negative numbers | ||
| // there is no description how to encode compact negative numbers | ||
| if (value < 0) { | ||
| raise(EncodeError::NEGATIVE_COMPACT_INTEGER); | ||
| } | ||
|
|
||
| if (value < kMinUint16) { | ||
| uint8_t v = (value.convert_to<uint8_t>() << 2u) | 0b00; | ||
| return encodeInteger(v, s); | ||
| } | ||
|
|
||
| else if (value < kMinUint32) { | ||
| // only values from [kMinUint16, kMinUint32) can be put here | ||
| uint16_t v = (value.convert_to<uint16_t>() << 2u) | 0b01; | ||
| return encodeInteger(v, s); | ||
| } | ||
|
|
||
| else if (value < kMinBigInteger) { | ||
| // only values from [kMinUint32, kMinBigInteger) can be put here | ||
| uint32_t v = (value.convert_to<uint32_t>() << 2u) | 0b10; | ||
| return encodeInteger(v, s); | ||
| } | ||
|
|
||
| // number of bytes required to represent value | ||
| size_t significant_bytes_n = msb(value) / 8 + 1; | ||
|
|
||
| if (significant_bytes_n > 67) { | ||
| raise(EncodeError::COMPACT_INTEGER_TOO_BIG); | ||
| } | ||
|
|
||
| // The upper 6 bits of the header are used to encode the number of bytes | ||
| // required to store the big integer. The value stored in these 6 bits | ||
| // ranges from 0 to 63 (2^6 - 1). However, the actual byte count starts | ||
| // from 4, so we subtract 4 from the byte count before encoding it. | ||
| // This makes the range of byte counts for storing big integers 4 to 67. | ||
| // To construct the final header, the upper 6 bits are shifted left by | ||
| // 2 positions (equivalent to multiplying by 4). | ||
| // The lower 2 bits (minor bits) store the encoding option, which in this | ||
| // case is 0b11 (decimal value 3). The final header is formed by adding 3 | ||
| // to the result of the previous operations. | ||
| uint8_t header = ((significant_bytes_n - 4) << 2u) | 0b11; | ||
|
|
||
| s << header; | ||
|
|
||
| for (auto v = value; v != 0; v >>= 8) { | ||
| // push back the least significant byte | ||
| s << static_cast<uint8_t>(v & 0xff); | ||
| } | ||
| } | ||
|
|
||
| template <typename T, typename S> | ||
| requires std::is_same_v<T, CompactInteger> | ||
| T decodeCompactInteger(S &stream) { | ||
| auto first_byte = stream.nextByte(); | ||
|
|
||
| const uint8_t flag = (first_byte)&0b00000011u; | ||
|
|
||
| size_t number = 0u; | ||
|
|
||
| switch (flag) { | ||
| case 0b00u: { | ||
| number = static_cast<size_t>(first_byte >> 2u); | ||
| break; | ||
| } | ||
|
|
||
| case 0b01u: { | ||
| auto second_byte = stream.nextByte(); | ||
|
|
||
| number = (static_cast<size_t>((first_byte)&0b11111100u) | ||
| + static_cast<size_t>(second_byte) * 256u) | ||
| >> 2u; | ||
| if ((number >> 6) == 0) { | ||
| raise(DecodeError::REDUNDANT_COMPACT_ENCODING); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case 0b10u: { | ||
| number = first_byte; | ||
| size_t multiplier = 256u; | ||
| if (!stream.hasMore(3u)) { | ||
| raise(DecodeError::NOT_ENOUGH_DATA); | ||
| } | ||
|
|
||
| for (auto i = 0u; i < 3u; ++i) { | ||
| // we assured that there are 3 more bytes, | ||
| // no need to make checks in a loop | ||
| number += (stream.nextByte()) * multiplier; | ||
| multiplier = multiplier << 8u; | ||
| } | ||
| number = number >> 2u; | ||
| if ((number >> 14) == 0) { | ||
| raise(DecodeError::REDUNDANT_COMPACT_ENCODING); | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| case 0b11: { | ||
| auto bytes_count = ((first_byte) >> 2u) + 4u; | ||
| if (!stream.hasMore(bytes_count)) { | ||
| raise(DecodeError::NOT_ENOUGH_DATA); | ||
| } | ||
|
|
||
| CompactInteger multiplier{1u}; | ||
| CompactInteger value = 0; | ||
| // we assured that there are m more bytes, | ||
| // no need to make checks in a loop | ||
| for (auto i = 0u; i < bytes_count; ++i) { | ||
| value += (stream.nextByte()) * multiplier; | ||
| multiplier *= 256u; | ||
| } | ||
| if (value.is_zero()) { | ||
| raise(DecodeError::REDUNDANT_COMPACT_ENCODING); | ||
| } | ||
| auto bits = msb(value) + 1; | ||
| if (bits <= 30 or (bits + 7) / 8 < bytes_count) { | ||
| raise(DecodeError::REDUNDANT_COMPACT_ENCODING); | ||
| } | ||
| return value; // special case | ||
| } | ||
|
|
||
| default: | ||
| UNREACHABLE | ||
| } | ||
|
|
||
| return CompactInteger{number}; | ||
| } | ||
|
|
||
| /** | ||
| * Decodes any integer type from compact-integer representation | ||
| * @tparam T integer type | ||
| * @tparam S input stream type | ||
| * @param value integer value | ||
| * @return value according compact-integer representation | ||
| */ | ||
| template <typename T, typename S> | ||
| requires std::unsigned_integral<T> | ||
| T decodeCompactInteger(S &s) { | ||
| auto integer = decodeCompactInteger<CompactInteger>(s); | ||
| if (not integer.is_zero() | ||
| and msb(integer) >= std::numeric_limits<T>::digits) { | ||
| raise(DecodeError::DECODED_VALUE_OVERFLOWS_TARGET); | ||
| } | ||
| return static_cast<T>(integer); | ||
| } | ||
|
|
||
|
|
||
| } // namespace scale::detail |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * Copyright Quadrivium LLC | ||
| * All Rights Reserved | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <array> | ||
| #include <concepts> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| #include <scale/outcome/outcome_throw.hpp> | ||
| #include <scale/scale_error.hpp> | ||
| #include <scale/types.hpp> | ||
|
|
||
| namespace scale::detail { | ||
|
|
||
| /** | ||
| * Encodes any integer type to jam-compact-integer representation | ||
| * @tparam T integer type | ||
| * @tparam S output stream type | ||
| * @param value integer value | ||
| * @return byte array representation of value as jam-compact-integer | ||
| */ | ||
| template <typename T, typename S> | ||
| requires(std::unsigned_integral<T> or std::is_same_v<T, CompactInteger>) | ||
| void encodeJamCompactInteger(T integer, S &s) { | ||
| size_t value; | ||
|
|
||
| if constexpr (std::is_same_v<T, CompactInteger>) { | ||
| // cannot encode negative numbers | ||
| // there is no description how to encode compact negative numbers | ||
| if (integer < 0) { | ||
| raise(EncodeError::NEGATIVE_COMPACT_INTEGER); | ||
| } | ||
| if (integer.is_zero()) { | ||
| value = 0; | ||
| } else { | ||
| if (msb(integer) >= std::numeric_limits<size_t>::digits) { | ||
| raise(EncodeError::VALUE_TOO_BIG_FOR_COMPACT_REPRESENTATION); | ||
| } | ||
| value = integer.template convert_to<size_t>(); | ||
| } | ||
| } else { | ||
| value = static_cast<size_t>(integer); | ||
| } | ||
|
|
||
| if (value < 0x80) { | ||
| s << static_cast<uint8_t>(value); | ||
| return; | ||
| } | ||
|
|
||
| std::array<uint8_t, sizeof(size_t) + 1> bytes; | ||
| uint8_t &prefix = bytes[0] = 0; | ||
| size_t len = 1; | ||
|
|
||
| for (decltype(value) i = value; i != 0; i >>= 8) { | ||
| // minimal value in prefix | ||
| if (i <= (static_cast<uint8_t>(~prefix) >> 1)) { | ||
| prefix |= i; | ||
| break; | ||
| } | ||
| prefix = (prefix >> 1) | 0x80; | ||
| bytes[len++] = static_cast<uint8_t>(i & 0xff); | ||
| } | ||
|
|
||
| for (auto byte : bytes) { | ||
| s << byte; | ||
| if (--len == 0) break; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Decodes any integer type from jam-compact-integer representation | ||
| * @tparam T integer type | ||
| * @tparam S input stream type | ||
| * @param value integer value | ||
| * @return value according jam-compact-integer representation | ||
| */ | ||
| template <typename T> | ||
| requires std::unsigned_integral<T> or std::is_same_v<T, CompactInteger> | ||
| T decodeJamCompactInteger(auto &s) { | ||
| uint8_t byte; | ||
|
|
||
| s >> byte; | ||
|
|
||
| if (byte == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| uint8_t len_bits = byte; | ||
| uint8_t val_bits = byte; | ||
| uint8_t val_mask = 0xff; | ||
|
|
||
| size_t value = 0; | ||
|
|
||
| for (uint8_t i = 0; i < 8; ++i) { | ||
| val_mask >>= 1; | ||
| val_bits &= val_mask; | ||
|
|
||
| if ((len_bits & static_cast<uint8_t>(0x80)) == 0) { // no more significant bytes | ||
| value |= static_cast<size_t>(val_bits) << (8 * i); | ||
| break; | ||
| } | ||
| len_bits <<= 1; | ||
|
|
||
| s >> byte; | ||
| value |= static_cast<size_t>(byte) << (8 * i); | ||
| } | ||
| if (val_bits == 0 and (byte & ~val_mask) == 0) { | ||
| raise(DecodeError::REDUNDANT_COMPACT_ENCODING); | ||
| } | ||
|
|
||
| if constexpr (not std::is_same_v<T, size_t> | ||
| and not std::is_same_v<T, CompactInteger>) { | ||
| if (value > std::numeric_limits<T>::max()) { | ||
| raise(DecodeError::DECODED_VALUE_OVERFLOWS_TARGET); | ||
| } | ||
| } | ||
|
|
||
| return static_cast<T>(value); | ||
| } | ||
|
|
||
| } // namespace scale::detail | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.