Extract util::Xor, Add key_offset option, Add bench

This commit is contained in:
MarcoFalke
2023-06-12 11:28:59 +02:00
parent 99b3af78bd
commit 9999a49b32
4 changed files with 49 additions and 17 deletions

View File

@@ -23,6 +23,27 @@
#include <utility>
#include <vector>
namespace util {
inline void Xor(Span<std::byte> write, Span<const std::byte> key, size_t key_offset = 0)
{
if (key.size() == 0) {
return;
}
key_offset %= key.size();
for (size_t i = 0, j = key_offset; i != write.size(); i++) {
write[i] ^= key[j++];
// This potentially acts on very many bytes of data, so it's
// important that we calculate `j`, i.e. the `key` index in this
// way instead of doing a %, which would effectively be a division
// for each byte Xor'd -- much slower than need be.
if (j == key.size())
j = 0;
}
}
} // namespace util
template<typename Stream>
class OverrideStream
{
@@ -316,20 +337,7 @@ public:
*/
void Xor(const std::vector<unsigned char>& key)
{
if (key.size() == 0) {
return;
}
for (size_type i = 0, j = 0; i != size(); i++) {
vch[i] ^= std::byte{key[j++]};
// This potentially acts on very many bytes of data, so it's
// important that we calculate `j`, i.e. the `key` index in this
// way instead of doing a %, which would effectively be a division
// for each byte Xor'd -- much slower than need be.
if (j == key.size())
j = 0;
}
util::Xor(MakeWritableByteSpan(*this), MakeByteSpan(key));
}
};
@@ -469,7 +477,6 @@ public:
}
};
/** Non-refcounted RAII wrapper for FILE*
*
* Will automatically close the file when it goes out of scope if not null.