From 22c61ea45154fba52408c6eb26773b6a56dae351 Mon Sep 17 00:00:00 2001 From: modeco80 Date: Mon, 21 Aug 2023 23:45:10 -0400 Subject: [PATCH] replace printing code with somehting hopefully better? --- src/main.cpp | 70 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index bf8d5c2..fbf6ab9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,19 +34,19 @@ std::string RandomString(std::uint32_t length, TRandomAlgo& rng) { constexpr std::uint32_t SwsfScramble(std::string_view code) { std::uint32_t tally {}; - auto cycle = [&tally](char c) { - tally = c + (tally * 5); - }; + auto cycle = [&tally](char c) { tally = c + (tally * 5); }; - cycle('c'); cycle('o'); cycle('d'); cycle('e'); cycle('_'); + cycle('c'); + cycle('o'); + cycle('d'); + cycle('e'); + cycle('_'); for(auto* p = code.data(); *p; ++p) cycle(tolower(*p)); return tally; } -// using ChannelType = concurrent_channel; - // These is not atomic since no other threads update it // there is no real point to making it atomic, other than // @@ -95,35 +95,57 @@ struct BruteThreadState { void DisplayProgress(const std::string& code, std::uint32_t hash) { std::lock_guard lk(print_lock); - std::cout << Line(line) << std::format("Thread {:2}: Trying code {}{} {}({:08x}){}", line, Color(172), code, Color(166), hash, Reset()) << std::flush; + struct FputcIterator { + using iterator_category = std::output_iterator_tag; + using value_type = void; + using difference_type = std::ptrdiff_t; + using pointer = void; + using reference = void; + + FputcIterator(std::FILE* file) : file(file) {} + FputcIterator& operator*() { return *this; } + FputcIterator& operator++() { return *this; } + FputcIterator& operator++(int) { return *this; } + + FputcIterator& operator=(const char& val) { + fputc(val, file); + return *this; + } + + private: + std::FILE* file; + }; + + std::format_to(FputcIterator(stdout), "{}Thread {:2}: Trying code {}{} {}({:08x}){}", Line(line), line, Color(172), code, Color(166), hash, + Reset()); + std::fflush(stdout); } void BruteForce(char prefix) { - //for(std::uint32_t i = 8; i <= code_length; ++i) { - std::string test_buffer(code_length, 'a'); - test_buffer[0] = prefix; + // for(std::uint32_t i = 8; i <= code_length; ++i) { + std::string test_buffer(code_length, 'a'); + test_buffer[0] = prefix; - - // test all possible combinations - while(true) { - //std::string test_buffer = RandomString(code_length, rng); - auto hash = SwsfScramble(test_buffer); - if( hash == code_hash) { - std::cerr << std::format("match: {} ({:08x})\n", test_buffer, hash); - } - - DisplayProgress(test_buffer, hash); - - if(!NextCode(test_buffer, 1)) - break; + // test all possible combinations + while(true) { + // std::string test_buffer = RandomString(code_length, rng); + auto hash = SwsfScramble(test_buffer); + if(hash == code_hash) { + std::cerr << std::format("match: {} ({:08x})\n", test_buffer, hash); } + + DisplayProgress(test_buffer, hash); + + if(!NextCode(test_buffer, 1)) + break; + } //} } private: unsigned line; std::random_device rd; - //swbf::Xoshiro256ss rng{rd}; + // swbf::Xoshiro256ss rng{rd}; }; int main() {