Replace Padder CRTP base with a concept.
This commit is contained in:
@@ -1,38 +1,16 @@
|
|||||||
#ifndef CHAOS_PADDING_PADDER_HPP
|
#ifndef CHAOS_PADDING_PADDER_HPP
|
||||||
#define CHAOS_PADDING_PADDER_HPP
|
#define CHAOS_PADDING_PADDER_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
namespace Chaos::Padding
|
namespace Chaos::Padding
|
||||||
{
|
{
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Padder
|
concept Padder = requires(uint8_t * begin, uint8_t * end)
|
||||||
{
|
{
|
||||||
public:
|
T::Pad(begin, end);
|
||||||
template<typename OutputIt>
|
{ T::ComputeUnpad(begin, end) } noexcept;
|
||||||
void Pad(OutputIt begin, OutputIt end) const
|
|
||||||
{
|
|
||||||
Impl().Pad(begin, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename InputIt>
|
|
||||||
auto ComputeUnpad(InputIt begin, InputIt end) const noexcept
|
|
||||||
{
|
|
||||||
return Impl().ComputeUnpad(begin, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
Padder() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
const T & Impl() const
|
|
||||||
{
|
|
||||||
return static_cast<const T &>(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
T & Impl()
|
|
||||||
{
|
|
||||||
return static_cast<T &>(*this);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Chaos::Padding
|
} // namespace Chaos::Padding
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
namespace Chaos::Padding
|
namespace Chaos::Padding
|
||||||
{
|
{
|
||||||
|
|
||||||
class PadderIso7816 : public Padder<PadderIso7816>
|
class PadderIso7816
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<typename OutputIt>
|
template<typename OutputIt>
|
||||||
@@ -82,6 +82,8 @@ private:
|
|||||||
using Branchless = Service::Branchless;
|
using Branchless = Service::Branchless;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(Padder<PadderIso7816>);
|
||||||
|
|
||||||
} // namespace Chaos::Padding
|
} // namespace Chaos::Padding
|
||||||
|
|
||||||
#endif // CHAOS_PADDING_PADDERISO7816_HPP
|
#endif // CHAOS_PADDING_PADDERISO7816_HPP
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
namespace Chaos::Padding
|
namespace Chaos::Padding
|
||||||
{
|
{
|
||||||
|
|
||||||
class PadderPkcs7 : public Padder<PadderPkcs7>
|
class PadderPkcs7
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<typename OutputIt>
|
template<typename OutputIt>
|
||||||
@@ -75,6 +75,8 @@ private:
|
|||||||
using Branchless = Service::Branchless;
|
using Branchless = Service::Branchless;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static_assert(Padder<PadderPkcs7>);
|
||||||
|
|
||||||
} // namespace Chaos::Padding
|
} // namespace Chaos::Padding
|
||||||
|
|
||||||
#endif // CHAOS_PADDING_PADDERPKCS7_HPP
|
#endif // CHAOS_PADDING_PADDERPKCS7_HPP
|
||||||
|
|||||||
@@ -109,8 +109,8 @@ TEST(PadIso7816Tests, PadOutIteratorUsageTest)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Impl, typename OutputIt>
|
template<Padder PadderImpl, typename OutputIt>
|
||||||
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
|
void PadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
|
||||||
{
|
{
|
||||||
padder.Pad(begin, end);
|
padder.Pad(begin, end);
|
||||||
}
|
}
|
||||||
@@ -127,7 +127,7 @@ TEST(PadIso7816Tests, PadThroughBaseTest)
|
|||||||
};
|
};
|
||||||
|
|
||||||
const PadderIso7816 padder;
|
const PadderIso7816 padder;
|
||||||
PadThroughBase(padder, fact.begin(), fact.end());
|
PadGeneric(padder, fact.begin(), fact.end());
|
||||||
|
|
||||||
ASSERT_EQ(expected, fact);
|
ASSERT_EQ(expected, fact);
|
||||||
}
|
}
|
||||||
@@ -324,8 +324,8 @@ TEST(PadIso7816Tests, UnpadErrorTest)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Impl, typename OutputIt>
|
template<Padder PadderImpl, typename OutputIt>
|
||||||
auto ComputeUnpadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
|
auto ComputeUnpadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
|
||||||
{
|
{
|
||||||
return padder.ComputeUnpad(begin, end);
|
return padder.ComputeUnpad(begin, end);
|
||||||
}
|
}
|
||||||
@@ -336,7 +336,7 @@ TEST(PadIso7816Tests, ComputeUnpadThroughBaseTest)
|
|||||||
std::array<uint8_t, 5> data = { 0x80, 0x00, 0x00, 0x00, 0x00 };
|
std::array<uint8_t, 5> data = { 0x80, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
const PadderIso7816 padder;
|
const PadderIso7816 padder;
|
||||||
auto result = ComputeUnpadThroughBase(padder, data.begin(), data.end());
|
auto result = ComputeUnpadGeneric(padder, data.begin(), data.end());
|
||||||
|
|
||||||
ASSERT_TRUE(result.IsOkay_);
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
ASSERT_EQ(5, result.PadSize_);
|
ASSERT_EQ(5, result.PadSize_);
|
||||||
|
|||||||
@@ -137,8 +137,8 @@ TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Impl, typename OutputIt>
|
template<Padder PadderImpl, typename OutputIt>
|
||||||
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
|
void PadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
|
||||||
{
|
{
|
||||||
padder.Pad(begin, end);
|
padder.Pad(begin, end);
|
||||||
}
|
}
|
||||||
@@ -153,7 +153,7 @@ TEST(PadPkcs7Tests, PadThroughBaseTest)
|
|||||||
};
|
};
|
||||||
|
|
||||||
const PadderPkcs7 padder;
|
const PadderPkcs7 padder;
|
||||||
PadThroughBase(padder, fact.begin(), fact.end());
|
PadGeneric(padder, fact.begin(), fact.end());
|
||||||
|
|
||||||
ASSERT_EQ(expected, fact);
|
ASSERT_EQ(expected, fact);
|
||||||
}
|
}
|
||||||
@@ -347,8 +347,8 @@ TEST(PadPkcs7Tests, UnpadErrorTest)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Impl, typename OutputIt>
|
template<Padder PadderImpl, typename OutputIt>
|
||||||
auto ComputeUnpadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
|
auto ComputeUnpadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
|
||||||
{
|
{
|
||||||
return padder.ComputeUnpad(begin, end);
|
return padder.ComputeUnpad(begin, end);
|
||||||
}
|
}
|
||||||
@@ -359,7 +359,7 @@ TEST(PadPkcs7Tests, ComputeUnpadThroughBaseTest)
|
|||||||
std::array<uint8_t, 5> data = { 0x05, 0x05, 0x05, 0x05, 0x05 };
|
std::array<uint8_t, 5> data = { 0x05, 0x05, 0x05, 0x05, 0x05 };
|
||||||
|
|
||||||
const PadderPkcs7 padder;
|
const PadderPkcs7 padder;
|
||||||
auto result = ComputeUnpadThroughBase(padder, data.begin(), data.end());
|
auto result = ComputeUnpadGeneric(padder, data.begin(), data.end());
|
||||||
|
|
||||||
ASSERT_TRUE(result.IsOkay_);
|
ASSERT_TRUE(result.IsOkay_);
|
||||||
ASSERT_EQ(5, result.PadSize_);
|
ASSERT_EQ(5, result.PadSize_);
|
||||||
|
|||||||
Reference in New Issue
Block a user