build matches file at end of execution

This commit is contained in:
Lily Tsuru 2023-08-22 19:29:53 -04:00
parent 55937b0bba
commit 9d02d7280e
2 changed files with 54 additions and 5 deletions

View File

@ -1,9 +1,9 @@
#pragma once #pragma once
#include <iterator>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <format> #include <format>
#include <iterator>
#ifdef _WIN32 #ifdef _WIN32
#define _WIN32_LEAN_AND_MEAN #define _WIN32_LEAN_AND_MEAN
@ -32,6 +32,33 @@ namespace swbf {
std::FILE* file; std::FILE* file;
}; };
template <class Os>
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 <class Stream, class... Args>
inline void fprint(Stream& file, std::string_view format, Args&&... args) {
std::vformat_to(OsIterator<Stream>(file), format, std::make_format_args(args...));
}
/// Poor Man's C++23 /// Poor Man's C++23
template <class... Args> template <class... Args>
inline void fprint(std::FILE* file, std::string_view format, Args&&... args) { inline void fprint(std::FILE* file, std::string_view format, Args&&... args) {

View File

@ -7,6 +7,8 @@
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <fstream>
#include "ansi.hpp" #include "ansi.hpp"
#include "fprint.hpp" #include "fprint.hpp"
#include "scramble.hpp" #include "scramble.hpp"
@ -23,6 +25,8 @@ namespace swbf {
std::uint32_t codeHash; std::uint32_t codeHash;
bool done = false; bool done = false;
std::vector<std::string> matches;
void DisplayProgress() { void DisplayProgress() {
if(!done) if(!done)
swbf::fprint(stdout, "{}Thread {:2}: Trying code {}{} {}({:08x}){}", swbf::ansi::Line(threadIndex), threadIndex, swbf::fprint(stdout, "{}Thread {:2}: Trying code {}{} {}({:08x}){}", swbf::ansi::Line(threadIndex), threadIndex,
@ -94,7 +98,8 @@ namespace swbf {
// There was a match! // There was a match!
if(hash == options.targetHash) 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(); CopyDisplayData();
@ -132,6 +137,23 @@ namespace swbf {
} }
pool.join(); // just in case! 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 } // namespace swbf