Skip to content

Commit

Permalink
Fixing hexendec decode not going from right to left, and Name shiftin…
Browse files Browse the repository at this point in the history
…g by 128 in bits. (#6)
  • Loading branch information
GhostofCookie committed Jun 28, 2023
1 parent 03e2466 commit 2f860a3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
24 changes: 11 additions & 13 deletions include/quicr/hex_endec.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,39 +121,37 @@ class HexEndec
}

template<size_t N, typename Uint_t = uint64_t>
static constexpr std::array<Uint_t, N> Decode(std::span<uint16_t> distribution, quicr::Name name)
static constexpr std::array<Uint_t, N> Decode(const std::span<uint16_t>& distribution, quicr::Name name)
{
const auto dist_size = distribution.size();
std::array<uint64_t, N> result;
for (int i = dist_size - 1; i >= 0; --i)
std::array<Uint_t, N> result;

for (size_t i = 0; i < dist_size; ++i)
{
const auto dist = distribution[i];
result[i] = name.bits<std::uint64_t>(0, dist);
name >>= dist;
result[i] = name.bits<Uint_t>(Size - dist, dist);
name <<= dist;
}

return result;
}

template<typename Uint_t = uint64_t, typename = typename std::enable_if_t<is_valid_uint<Uint_t>::value, Uint_t>>
static inline typename std::vector<Uint_t> Decode(std::span<uint16_t> distribution, std::string_view hex)
static inline std::vector<Uint_t> Decode(const std::span<uint16_t>& distribution, std::string_view hex)
{
return Decode(distribution, quicr::Name(hex));
}

template<typename Uint_t = uint64_t>
static inline std::vector<Uint_t> Decode(std::span<uint16_t> distribution, quicr::Name name)
{
if (std::accumulate(distribution.begin(), distribution.end(), 0) > Size)
throw std::domain_error("Total bits cannot exceed specified size");

const auto dist_size = distribution.size();
std::vector<uint64_t> result(dist_size);
for (int i = dist_size - 1; i >= 0; --i)
std::vector<Uint_t> result(dist_size);
for (size_t i = 0; i < dist_size; ++i)
{
const auto dist = distribution[i];
result[i] = (name.bits<std::uint64_t>(0, dist));
name >>= dist;
result[i] = name.bits<Uint_t>(Size - dist, dist);
name <<= dist;
}

return result;
Expand Down
2 changes: 2 additions & 0 deletions include/quicr/name.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,8 @@ template<>
constexpr Name Name::bits<Name>(std::uint16_t from, std::uint16_t length) const
{
if (length == 0) return Name(uint_type(0), uint_type(0));
if (length == sizeof(Name) * 8) return *this;

return *this & (((Name(uint_type(0), uint_type(1)) << length) - 1) << from);
}

Expand Down

0 comments on commit 2f860a3

Please sign in to comment.