Skip to content

Commit

Permalink
test clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
forgotthepen committed Sep 6, 2024
1 parent a5c73a7 commit f42d837
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
15 changes: 8 additions & 7 deletions str-to-num.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,22 @@ class s2n_basic {
TOut m_data{};

public:
// use a container since TConverter::to_str() will return an array
// container for the decrypted/restored string
// using a container since TConverter::to_str() will return an array
struct str_container {
constexpr static s2n_sz_t count = str_count;
TChar data[str_count + 1]{};

constexpr void clear() {
for (s2n_sz_t idx = 0; idx < str_count; ++idx) {
// data[idx] = TChar(); // easy to detect
data[idx] += str_count ^ (idx + 1);
data[idx] *= idx + 2;
data[idx] += str_count ^ (idx + 3);
data[idx] *= idx + 3;
data[idx] ^= idx + 3;
}

data[str_count] = str_count * (data[0] + 1);
data[str_count] ^= (data[0] - 1);
data[str_count] = str_count * (data[0] + 3);
data[str_count] ^= (data[0] - 3);
}

// constant destruction is available since C++20: https://en.cppreference.com/w/cpp/language/constexpr
Expand Down Expand Up @@ -282,15 +283,15 @@ namespace s2n_cvt {
constexpr static void to_num(TChar (&dst) [StrCount], const TStr& src) noexcept {
for (s2n_sz_t src_idx = 0; src_idx < StrCount; ++src_idx) {
const auto key2 = (StrCount << (src_idx + 3)) | (src_idx + 3);
dst[src_idx] = src[src_idx] ^ static_cast<TChar>(VKey ^ src_idx) ^ static_cast<TChar>(key2);
dst[src_idx] = static_cast<TChar>(src[src_idx] ^ VKey ^ (src_idx + 12) ^ key2);
}
}

template<typename TStr, s2n_sz_t StrCount, typename TChar>
constexpr static void to_str(TChar (&dst) [StrCount + 1], const TChar (&src) [StrCount]) noexcept {
for (s2n_sz_t src_idx = 0; src_idx < StrCount; ++src_idx) {
const auto key2 = (StrCount << (src_idx + 3)) | (src_idx + 3);
dst[src_idx] = src[src_idx] ^ static_cast<TChar>(VKey ^ src_idx) ^ static_cast<TChar>(key2);
dst[src_idx] = static_cast<TChar>(src[src_idx] ^ VKey ^ (src_idx + 12) ^ key2);
}
}
};
Expand Down
15 changes: 15 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ int main()
std::cout << std::hex << "FNV 1a = 0x" << smsm33.data() << std::endl;
}

{
constexpr auto clear_me_after_use = s2n("my super secret");

{
constexpr auto actual_str_constexpr = clear_me_after_use.str();
static_assert(same_str_data(actual_str_constexpr.data, "my super secret"), "error");
}

auto actual_str = clear_me_after_use.str();
std::cout << std::hex << "actual str = [" << actual_str.data << "]" << std::endl;
actual_str.clear(); // CANNOT BE RECOVERED
// invalid use, we have to call .str() again to get a new instance
// std::cout << std::hex << "actual str = [" << actual_str.data << "]" << std::endl;
}


using tt1 = str_char_t<char (&) [4]>;
using tt2 = str_char_t<const char (&) [4]>;
Expand Down

0 comments on commit f42d837

Please sign in to comment.