Skip to content

Commit

Permalink
Follow README.md name rules (prevent collisions)
Browse files Browse the repository at this point in the history
?`cxx/Macros.hxx`:
	+`SUSUWU_POSIX`
	+`SUSUWU_WIN32`
	+`SUSUWU_HAS_FEATURE`
	+`SUSUWU_HAS_ATTRIBUTE`
	-[duplicate SUSUWU_NORETURN code]

?`cxx/ClassSys.*xx`:
	`:%s/_POSIX_VERSION/SUSUWU_POSIX/`
	`:%s/__WIN32__/SUSUWU_WIN32/`

?`cxx/ClassSys.*xx`:
	`:%s/SUSUWU_PRINT(ERROR/SUSUWU_ERROR/`: _Windows_ warning (about <shlobj.h>::ERROR) fix.
?`cxx/ClassSys.cxx`, ?`cxx/ClassSha2.cxx`:
	`:%s/SUSUWU_PRINT(WARNING/SUSUWU_WARNING/`: Ditto, for possible systems which `#define WARNING`.

?`cxx/ClassSha2.*xx`:
	`:%s/sha1/classSha1/`
	`:%s/sha256/classSha256/`
	`:%s/sha512/classSha512/`
	`:%s/sha2/classSha2/`
	`:%s/Sha2/ClassSha2/`
	`:%s/classClassSha/classSha/`
	`:%s/ClassClassSha/ClassSha/`
?`cxx/ClassResultList.hxx`, ?`cxx/VirusAnalysis.cxx`, ?`cxx/AssistantCns.cxx`:
	`:%s/sha2/classSha2/`

?`cxx/AssistantCns.*xx`:
	`:%s/questionsResponsesFromHosts/assistantCnsDownloadHosts/`,
	`:%s/questionsResponsesFromXhtml/assistantCnsProcessXhtml/`,
	`:%s/assistantParse/assistantCnsProcess/`.

?`posts/VirusAnalysis.md`: Include all this.

Some has to do with issue #18 (support more tools), as other systems have tools with possible collisions, which this improves.

?`README.md`:
	?`Targets:`: Include default member inits with
requirements for C++11.
	Is followup to: d7370e8 (?`cxx/*.*x`: undo C++14 code, support `-std=c++11`)
  • Loading branch information
SwuduSusuwu committed Nov 17, 2024
1 parent d7370e8 commit bf1fec0
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 210 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Dual licenses: choose [_Creative Commons_](https://creativecommons.org/share-you
[`./cxx/`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/) _C++_ implementations of posts (for now is just neural system pure virtual template ([`./cxx/ClassCns.hxx`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/ClassCns.hxx)) + antivirus([`./cxx/VirusAnalysis.cxx`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/VirusAnalysis.cxx)) + assistant([`./cxx/AssistantCns.cxx`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/AssistantCns.cxx)), with lots of [issues](https://github.com/SwuduSusuwu/SubStack/issues) which you can contribute to, or can request that more resources go to).

# How to use this
Targets: Windows/Linux/Android/OSX/iOS; minimum [_C++11_](https://gcc.gnu.org/projects/cxx-status.html#cxx11) (all `CXX` with `201102 <= __cplusplus`,) due to use of `auto`, `decltype`, `for(value: list) {}`, `nullptr`, `__func__`).
Targets: Windows/Linux/Android/OSX/iOS; minimum [_C++11_](https://gcc.gnu.org/projects/cxx-status.html#cxx11) (all `CXX` with `201102 <= __cplusplus`,) due to use of `auto`, `class {bool defaultMemberInit = true};`, `decltype`, `for(value: list) {}`, `nullptr`, `__func__`).

Usage: [`./build.sh [OPTIONS]`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/build.sh) produces objects (`./obj/*.o`, for distribution into other tools,) plus [_Executable and Linkable Format_](https://wikipedia.org/wiki/Executable_and_Linkable_Format) (`./bin/a.out`, to do examples/[unit tests](https://wikipedia.org/wiki/Unit_test#Agile) which prove how effective functions execute,) both of which you can redirect with `export OBJDIR=___` (or `export BINDIR=___`.)
- [`./cxx/main.hxx`](https://github.com/SwuduSusuwu/SubStack/blob/trunk/cxx/main.hxx) has constants to use to interpret `a.out`'s return values.
Expand Down
32 changes: 16 additions & 16 deletions cxx/AssistantCns.cxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* Dual licenses: choose "Creative Commons" or "Apache 2" (allows all uses) */
#ifndef INCLUDES_cxx_AssistantCns_cxx
#define INCLUDES_cxx_AssistantCns_cxx
#include "AssistantCns.hxx" /* assistantParseQuestion assistantParseResponses assistantParseUrls */
#include "AssistantCns.hxx" /* assistantCnsProcessQuestion assistantCnsProcessResponses assistantCnsProcessUrls */
#include "ClassCns.hxx" /* Cns CnsMode execvex */
#include "ClassPortableExecutable.hxx" /* FileBytecode FilePath */
#include "ClassResultList.hxx" /* explodeToList listMaxSize listHasValue ResultList ResultListBytecode resultListDumpTo resultListProduceHashes */
#include "ClassSha2.hxx" /* sha2 */
#include "ClassSha2.hxx" /* classSha2 */
#include "ClassSys.hxx" /* execvex */
#include "Macros.hxx" /* SUSUWU_NOTICE_EXECUTEVERBOSE */
#include <cassert> /* assert */
Expand Down Expand Up @@ -49,7 +49,7 @@ const bool assistantCnsTests() {
assert(3 == responsesOrNull.hashes.size());
SUSUWU_NOTICE_EXECUTEVERBOSE(resultListDumpTo(questionsOrNull, std::cout, true, true, false));
SUSUWU_NOTICE_EXECUTEVERBOSE((resultListDumpTo(responsesOrNull, std::cout, false, false, false), std::cout << std::endl));
questionsResponsesFromHosts(questionsOrNull, responsesOrNull);
assistantCnsDownloadHosts(questionsOrNull, responsesOrNull);
produceAssistantCns(questionsOrNull, responsesOrNull, assistantCns);
return true;
}
Expand All @@ -69,22 +69,22 @@ void produceAssistantCns(const ResultList &questionsOrNull, const ResultList &re
cns.setupSynapses(inputsToOutputs);
}

void questionsResponsesFromHosts(ResultList &questionsOrNull, ResultList &responsesOrNull, const std::vector<FilePath> &hosts) {
void assistantCnsDownloadHosts(ResultList &questionsOrNull, ResultList &responsesOrNull, const std::vector<FilePath> &hosts) {
for(const auto &host : hosts) {
execvex("wget '" + host + "/robots.txt' -Orobots.txt");
execvex("wget '" + host + "' -Oindex.xhtml");
questionsOrNull.signatures.push_back(host);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, "index.xhtml");
assistantCnsProcessXhtml(questionsOrNull, responsesOrNull, "index.xhtml");
}
}
void questionsResponsesFromXhtml(ResultList &questionsOrNull, ResultList &responsesOrNull, const FilePath &localXhtml) {
auto noRobots = assistantParseUrls("robots.txt");
auto question = assistantParseQuestion(localXhtml);
void assistantCnsProcessXhtml(ResultList &questionsOrNull, ResultList &responsesOrNull, const FilePath &localXhtml) {
auto noRobots = assistantCnsProcessUrls("robots.txt");
auto question = assistantCnsProcessQuestion(localXhtml);
if(!question.empty()) {
auto questionSha2 = sha2(question);
auto questionSha2 = classSha2(question);
if(listHasValue(questionsOrNull.hashes, questionSha2)) { /* TODO */ } else {
decltype(question) response = "";
auto responses = assistantParseResponses(localXhtml);
auto responses = assistantCnsProcessResponses(localXhtml);
if(!responses.empty()) {
questionsOrNull.hashes.insert(questionSha2);
questionsOrNull.bytecodes.push_back(question);
Expand All @@ -95,28 +95,28 @@ void questionsResponsesFromXhtml(ResultList &questionsOrNull, ResultList &respon
}
response += responseIt;
}
auto responseSha2 = sha2(response);
auto responseSha2 = classSha2(response);
if(listHasValue(responsesOrNull.hashes, responseSha2)) { /* TODO */ } else {
responsesOrNull.hashes.insert(responseSha2);
responsesOrNull.bytecodes.push_back(response);
}
}
}
}
auto urls = assistantParseUrls(localXhtml);
auto urls = assistantCnsProcessUrls(localXhtml);
for(const auto &url : urls) {
if(!listHasValue(questionsOrNull.signatures, url) && !listHasValue(noRobots, url)) {
execvex("wget '" + url + "' -O" + localXhtml);
questionsOrNull.signatures.push_back(url);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, localXhtml);
assistantCnsProcessXhtml(questionsOrNull, responsesOrNull, localXhtml);
}
}
}
#ifdef BOOST_VERSION
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#endif /* BOOST_VERSION */
const std::vector<FilePath> assistantParseUrls(const FilePath &localXhtml) {
const std::vector<FilePath> assistantCnsProcessUrls(const FilePath &localXhtml) {
const std::vector<FilePath> urls;
#ifdef BOOST_VERSION
boost::property_tree::ptree pt;
Expand All @@ -130,8 +130,8 @@ const std::vector<FilePath> assistantParseUrls(const FilePath &localXhtml) {
#endif /* else !BOOST_VERSION */
return urls;
}
const FileBytecode assistantParseQuestion(const FilePath &localXhtml) {return "";} /* TODO */
const std::vector<FileBytecode> assistantParseResponses(const FilePath &localXhtml) {return {};} /* TODO */
const FileBytecode assistantCnsProcessQuestion(const FilePath &localXhtml) {return "";} /* TODO */
const std::vector<FileBytecode> assistantCnsProcessResponses(const FilePath &localXhtml) {return {};} /* TODO */

const std::string assistantCnsProcess(const Cns &cns, const FileBytecode &bytecode) {
return cns.processToString(bytecode);
Expand Down
14 changes: 7 additions & 7 deletions cxx/AssistantCns.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace Susuwu {
extern Cns assistantCns;
extern std::string assistantCnsResponseDelimiter;

/* if (with example inputs) these functions (`questionsResponsesFromHosts()` `produceAssistantCns()`) pass, `return true;`
/* if (with example inputs) these functions (`assistantCnsDownloadHosts()` `produceAssistantCns()`) pass, `return true;`
* @throw std::bad_alloc
* @throw std::logic_error
* @pre @code assistantCns.hasImplementation() @endcode */
const bool assistantCnsTests();
static const bool assistantCnsTestsNoexcept() SUSUWU_NOEXCEPT {return templateCatchAll(assistantCnsTests, "assistantCnsTests()");}

/* Universal Resources Locators of hosts which `questionsResponsesFromHosts()` uses
/* Universal Resources Locators of hosts which `assistantCnsDownloadHosts()` uses
* Wikipedia is a special case; has compressed downloads of databases ( https://wikipedia.org/wiki/Wikipedia:Database_download )
* Github is a special case; has compressed downloads of repositories ( https://docs.github.com/en/get-started/start-your-journey/downloading-files-from-github )
*/
Expand All @@ -34,11 +34,11 @@ extern std::vector<FilePath> assistantCnsDefaultHosts;
* If no responses, `0 == responsesOrNull.bytecodes[x].size()` (ignore).
* `questionsOrNull.signatures[x] = Universal Resource Locator`
* @code sha2(ResultList.bytecodes[x]) == ResultList.hashes[x] @endcode */
void questionsResponsesFromHosts(ResultList &questionsOrNull, ResultList &responsesOrNull, const std::vector<FilePath> &hosts = assistantCnsDefaultHosts);
void questionsResponsesFromXhtml(ResultList &questionsOrNull, ResultList &responsesOrNull, const FilePath &localXhtml = "index.xhtml");
const std::vector<FilePath> assistantParseUrls(const FilePath &localXhtml = "index.xhtml"); /* TODO: for XML/XHTML could just use [ https://www.boost.io/libraries/regex/ https://github.com/boostorg/regex ] or [ https://www.boost.org/doc/libs/1_85_0/doc/html/property_tree/parsers.html#property_tree.parsers.xml_parser https://github.com/boostorg/property_tree/blob/develop/doc/xml_parser.qbk ] */
const FileBytecode assistantParseQuestion(const FilePath &localXhtml = "index.xhtml"); /* TODO: regex or XML parser */
const std::vector<FileBytecode> assistantParseResponses(const FilePath &localXhtml = "index.xhtml"); /* TODO: regex or XML parser */
void assistantCnsDownloadHosts(ResultList &questionsOrNull, ResultList &responsesOrNull, const std::vector<FilePath> &hosts = assistantCnsDefaultHosts);
void assistantCnsProcessXhtml(ResultList &questionsOrNull, ResultList &responsesOrNull, const FilePath &localXhtml = "index.xhtml");
const std::vector<FilePath> assistantCnsProcessUrls(const FilePath &localXhtml = "index.xhtml"); /* TODO: for XML/XHTML could just use [ https://www.boost.io/libraries/regex/ https://github.com/boostorg/regex ] or [ https://www.boost.org/doc/libs/1_85_0/doc/html/property_tree/parsers.html#property_tree.parsers.xml_parser https://github.com/boostorg/property_tree/blob/develop/doc/xml_parser.qbk ] */
const FileBytecode assistantCnsProcessQuestion(const FilePath &localXhtml = "index.xhtml"); /* TODO: regex or XML parser */
const std::vector<FileBytecode> assistantCnsProcessResponses(const FilePath &localXhtml = "index.xhtml"); /* TODO: regex or XML parser */

/* @pre `questionsOrNull` maps to `responsesOrNull`,
* `0 == questionsOrNull.bytecodes[x].size()` for new assistant synthesis (empty question has responses),
Expand Down
4 changes: 2 additions & 2 deletions cxx/ClassResultList.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#define INCLUDES_cxx_ClassResultList_hxx
//#include "ClassObject.hxx" /* Object */ /* TODO: fix "Initialization of non-aggregate type" */
#include "ClassPortableExecutable.hxx" /* FilePath FileBytecode FileHash */
#include "ClassSha2.hxx" /* sha2 */
#include "ClassSha2.hxx" /* classSha2 */
#include "ClassSys.hxx" /* classSysHexOs */
#include "Macros.hxx" /* SUSUWU_NOEXCEPT SUSUWU_PREFER_CSTR */
#include <algorithm> /* std::search std::find std::set_intersection */
Expand Down Expand Up @@ -92,7 +92,7 @@ template<class List, class List2>
* @post @code !hashes.empty() @endcode */
void listToHashes(const List &list /* ResultList::bytecodes or ResultList::hex*/, List2 &hashes /* ResultList::hashess */) {
for(const auto &value : list) {
hashes.insert(sha2(value));
hashes.insert(classSha2(value));
}
}
/* Usage: if `ResultList` was not produced with `.hashes` */
Expand Down
32 changes: 16 additions & 16 deletions cxx/ClassSha2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "ClassPortableExecutable.hxx" /* FileBytecode FileHash */
#include "ClassSha2.hxx"
#include "ClassSys.hxx" /* classSysHexStr classSysUSecondClock templateCatchAll */
#include "Macros.hxx" /* SUSUWU_NOEXCEPT SUSUWU_NOTICE_EXECUTE SUSUWU_PRINT SUSUWU_INFO SUSUWU_NOTICE */
#include "Macros.hxx" /* SUSUWU_NOEXCEPT SUSUWU_NOTICE_EXECUTE SUSUWU_INFO SUSUWU_NOTICE SUSUWU_WARNING */
#include <climits> /* CHAR_BIT */
#include <stdexcept> /* std::runtime_error */
#include <string> /* std::to_string */
Expand All @@ -13,26 +13,26 @@ extern "C" {
#include "../c/rfc6234/sha.h" /* SHA256HashSize, SHA256Context, SHA256Reset, SHA256Input, SHA256Result, SHA1*, SHA512* */
}
namespace Susuwu {
Sha2 sha2 = sha256;
/* const */ FileHash /* 128 bits, not null-terminated */ sha1(const FileBytecode &bytecode) {
ClassSha2 classSha2 = classSha256;
/* const */ FileHash /* 128 bits, not null-terminated */ classSha1(const FileBytecode &bytecode) {
FileHash result;
SHA1Context context;
result.resize(SHA1HashSize);
SHA1Reset(&context); /* If `undefined symbol: SHA1Reset`, affix `sha1.o` to `${LD_FLAGS}` */
SHA1Reset(&context); /* If `undefined symbol: SHA1Reset`, affix `classSha1.o` to `${LD_FLAGS}` */
SHA1Input(&context, reinterpret_cast<const unsigned char *>(&bytecode[0]), bytecode.size());
SHA1Result(&context, reinterpret_cast<unsigned char *>(&result[0]));
return result;
}
/* const */ FileHash /* 256 bits, not null-terminated */ sha256(const FileBytecode &bytecode) {
/* const */ FileHash /* 256 bits, not null-terminated */ classSha256(const FileBytecode &bytecode) {
FileHash result;
SHA256Context context;
result.resize(SHA256HashSize);
SHA256Reset(&context); /* If `undefined symbol: SHA256Reset`, affix `sha224-256.o` to `${LD_FLAGS}` */
SHA256Reset(&context); /* If `undefined symbol: SHA256Reset`, affix `classSha224-256.o` to `${LD_FLAGS}` */
SHA256Input(&context, reinterpret_cast<const unsigned char *>(&bytecode[0]), bytecode.size());
SHA256Result(&context, reinterpret_cast<unsigned char *>(&result[0]));
return result;
}
/* const */ FileHash /* 512 bits, not null-terminated */ sha512(const FileBytecode &bytecode) {
/* const */ FileHash /* 512 bits, not null-terminated */ classSha512(const FileBytecode &bytecode) {
FileHash result;
SHA512Context context;
result.resize(SHA512HashSize);
Expand All @@ -46,24 +46,24 @@ const bool classSha2Tests() { /* is just to test glue code (which wraps rfc6234)
const char nulls[65536 /* 65536 == 2^16 == 64kb */] = {0};
std::string nullStr(nulls, &nulls[65536]);
const ClassSysUSeconds tsDrift = classSysUSecondClock(), ts2Drift = classSysUSecondClock() - tsDrift, ts = classSysUSecondClock();
const FileHash hash = sha2(nullStr);
const FileHash hash = classSha2(nullStr);
const ClassSysUSeconds ts2 = classSysUSecondClock() - ts2Drift;
const std::string hashStrCompute = "0x" + classSysHexStr(hash);
const std::string hashStrTrue = "0xde2f256064a0af797747c2b97505dc0b9f3df0de4f489eac731c23ae9ca9cc31";
if(ts == ts2) {
SUSUWU_PRINT(WARNING, "0 ms (0 μs) to compute `sha2(std::string(nulls, &nulls[65536])) == " + hashStrCompute + "` = inf mbps");
SUSUWU_WARNING("0 ms (0 μs) to compute `classSha2(std::string(nulls, &nulls[65536])) == " + hashStrCompute + "` = inf mbps");
} else {
SUSUWU_INFO(std::to_string((ts2 - ts) / 1000) + " ms (" + std::to_string(ts2 - ts) + " μs) to compute `sha2(std::string(nulls, &nulls[65536])) == " + hashStrCompute + "` = " + std::to_string(float(65536) * CHAR_BIT /* to bits */ / (ts2 - ts) /* to bpμs */ * 1000000 /* to bps */ / (1 << 20) /* to mbps */) + "mbps");
SUSUWU_INFO(std::to_string((ts2 - ts) / 1000) + " ms (" + std::to_string(ts2 - ts) + " μs) to compute `classSha2(std::string(nulls, &nulls[65536])) == " + hashStrCompute + "` = " + std::to_string(float(65536) * CHAR_BIT /* to bits */ / (ts2 - ts) /* to bpμs */ * 1000000 /* to bps */ / (1 << 20) /* to mbps */) + "mbps");
}
SUSUWU_NOTICE("This `sha2()` is from `./build.sh --debug`: `./build.sh --release` has 2x this throughput");
SUSUWU_NOTICE("This `classSha2()` is from `./build.sh --debug`: `./build.sh --release` has 2x this throughput");
if(0 == hash.size()) {
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "`0 == sha2(std::string()).size();"));
} else if(hashStrTrue.size() != hashStrCompute.size() && sha256 == sha2) {
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "`sha2 = sha256;`, but `(" + std::to_string(hash.size()) + " == sha2(std::string()).size())`"));
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "`0 == classSha2(std::string()).size();"));
} else if(hashStrTrue.size() != hashStrCompute.size() && classSha256 == classSha2) {
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "`classSha2 = classSha256;`, but `(" + std::to_string(hash.size()) + " == sha2(std::string()).size())`"));
} else if(hashStrTrue.size() != hashStrCompute.size()) {
SUSUWU_INFO("`(sha256 != sha2)`, `(" + std::to_string(hash.size()) + " == sha2(std::string()).size())`");
SUSUWU_INFO("`(classSha256 != classSha2)`, `(" + std::to_string(hash.size()) + " == sha2(std::string()).size())`");
} else if(hashStrTrue != hashStrCompute) {
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "sha2(char nulls[65535] = {0}) did not compute " + hashStrTrue));
throw std::runtime_error(SUSUWU_ERRSTR(ERROR, "classSha2(char nulls[65535] = {0}) did not compute " + hashStrTrue));
}
return true;
}
Expand Down
10 changes: 5 additions & 5 deletions cxx/ClassSha2.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
#include "Macros.hxx" /* SUSUWU_NOEXCEPT */
namespace Susuwu {
/* Uses https://www.rfc-editor.org/rfc/rfc6234#section-8.2.2 */
/* const */ FileHash /* 128 bits, not null-terminated */ sha1(const FileBytecode &bytecode);
/* const */ FileHash /* 256 bits, not null-terminated */ sha256(const FileBytecode &bytecode);
/* const */ FileHash /* 512 bits, not null-terminated */ sha512(const FileBytecode &bytecode);
typedef FileHash (*Sha2)(const FileBytecode &bytecode);
extern Sha2 sha2/* = sha256 */; /* To compress, apps can execute `sha2 = sha1;`. To double hash sizes, execute `sha2 = sha512;`. (Notice: this does not recompute hashes which exist) */
/* const */ FileHash /* 128 bits, not null-terminated */ classSha1(const FileBytecode &bytecode);
/* const */ FileHash /* 256 bits, not null-terminated */ classSha256(const FileBytecode &bytecode);
/* const */ FileHash /* 512 bits, not null-terminated */ classSha512(const FileBytecode &bytecode);
typedef FileHash (*ClassSha2)(const FileBytecode &bytecode);
extern ClassSha2 classSha2/* = classSha256 */; /* To compress, apps can execute `sha2 = classSha1;`. To double hash sizes, execute `sha2 = classSha512;`. (Notice: this does not recompute hashes which exist) */
const bool classSha2Tests();
const bool classSha2TestsNoexcept() SUSUWU_NOEXCEPT;
}; /* namespace Susuwu */
Expand Down
Loading

0 comments on commit bf1fec0

Please sign in to comment.