From 9d02d7280e5146dea479d52f81943c8bac13995d Mon Sep 17 00:00:00 2001 From: modeco80 Date: Tue, 22 Aug 2023 19:29:53 -0400 Subject: [PATCH] build matches file at end of execution --- src/fprint.hpp | 35 +++++++++++++++++++++++++++++++---- src/worker.cpp | 24 +++++++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/fprint.hpp b/src/fprint.hpp index 816c114..90eb65d 100644 --- a/src/fprint.hpp +++ b/src/fprint.hpp @@ -1,13 +1,13 @@ #pragma once -#include #include #include #include +#include #ifdef _WIN32 -#define _WIN32_LEAN_AND_MEAN -#include + #define _WIN32_LEAN_AND_MEAN + #include #endif namespace swbf { @@ -32,8 +32,35 @@ namespace swbf { std::FILE* file; }; + template + struct OsIterator { + using iterator_category = std::output_iterator_tag; + using value_type = void; + using difference_type = std::ptrdiff_t; + using pointer = void; + using reference = void; + + OsIterator(Os& file) : os(&file) {} + OsIterator& operator*() { return *this; } + OsIterator& operator++() { return *this; } + OsIterator& operator++(int) { return *this; } + + OsIterator& operator=(const char& val) { + os->put(val); + return *this; + } + + private: + Os* os; + }; + + template + inline void fprint(Stream& file, std::string_view format, Args&&... args) { + std::vformat_to(OsIterator(file), format, std::make_format_args(args...)); + } + /// Poor Man's C++23 - template + template inline void fprint(std::FILE* file, std::string_view format, Args&&... args) { #ifdef _WIN32 // windows vt handling is DOG slow. don't know why lmao diff --git a/src/worker.cpp b/src/worker.cpp index 0246702..4d14ac0 100644 --- a/src/worker.cpp +++ b/src/worker.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include "ansi.hpp" #include "fprint.hpp" #include "scramble.hpp" @@ -23,6 +25,8 @@ namespace swbf { std::uint32_t codeHash; bool done = false; + std::vector matches; + void DisplayProgress() { if(!done) swbf::fprint(stdout, "{}Thread {:2}: Trying code {}{} {}({:08x}){}", swbf::ansi::Line(threadIndex), threadIndex, @@ -94,7 +98,8 @@ namespace swbf { // There was a match! if(hash == options.targetHash) - swbf::fprint(stderr, "match: {} ({:08x})\n", test_buffer, hash); + DisplayData().matches.push_back(test_buffer); + //swbf::fprint(stderr, "match: {} ({:08x})\n", test_buffer, hash); CopyDisplayData(); @@ -132,6 +137,23 @@ namespace swbf { } pool.join(); // just in case! + + // build the matches file + fprint(stdout, "Writing matches file..."); + std::ofstream ofs("matches.txt"); + + if(options.exact) + fprint(ofs, "Matches for hash {:8x} for length {}:\n", options.targetHash, options.startLength, options.endLength); + else + fprint(ofs, "Matches for hash {:8x} for lengths {}..{}:\n", options.targetHash, options.startLength, options.endLength); + + for(auto& td : infoData) { + for(auto& match : td.matches) { + fprint(ofs, "{}\n", match); + } + + td.matches.clear(); + } } } // namespace swbf