Skip to content

Commit

Permalink
@cxx/Class{Cns,PortableExecutable}.hxx : Object
Browse files Browse the repository at this point in the history
  (inherits from Java-ish `Object : Class` superclass which b903266 introduces )

Although lots of member functions for this superclass, `./build.sh --debug` used to produce `a.out` with `size=2966752` but now is just `size=3022144` (size increase is just 2%).

@cxx/ClassResultList
  Add `TODO: fix "Initialization of non-aggregate type with an initializer list"` for `ResultList : Object`

This is for #10 (work-in-progress)

@cxx/VirusAnalysis.md /* Include this, plus some tab fixes */
  • Loading branch information
SwuduSusuwu committed Jul 4, 2024
1 parent b903266 commit 11327e3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
4 changes: 3 additions & 1 deletion cxx/ClassCns.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once
#ifndef INCLUDES_cxx_ClassCns_hxx
#define INCLUDES_cxx_ClassCns_hxx
#include "ClassObject.hxx" /* Object */
#include <cassert> /* assert */
#include <cstddef> /* size_t */
#include <string> /* std::string */
Expand All @@ -24,8 +25,9 @@ typedef enum CnsMode : char {
* @pre @code (-1 != access(argv[0], X_OK) @endcode */
const int execves(/* const std::string &pathname, -- `execve` requires `&pathname == &argv[0]` */ const std::vector<const std::string> &argvS = {}, const std::vector<const std::string> &envpS = {});
static const int execvex(const std::string &toSh) {return execves({"/bin/sh", "-c", toSh});}
typedef class Cns {
typedef class Cns : Object {
public:
const std::string getName() {return "Susuwu::class Cns";}
virtual ~Cns() = default;
virtual const bool hasImplementation() const {return typeid(Cns) != typeid(this);}
virtual const bool isInitialized() const {return initialized;}
Expand Down
4 changes: 3 additions & 1 deletion cxx/ClassPortableExecutable.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
#pragma once
#ifndef INCLUDES_cxx_ClassPortableExecutable_hxx
#define INCLUDES_cxx_ClassPortableExecutable_hxx
#include "ClassObject.hxx" /* Object */
#include <string> /* std::string */
namespace Susuwu {
typedef std::string FilePath; /* TODO: `std::char_traits<unsigned char>`, `std::basic_string<unsigned char>("string literal")` */
typedef FilePath FileBytecode; /* Uses `std::string` for bytecode (versus `std::vector`) because:
* "If you are going to use the data in a string like fashon then you should opt for std::string as using a std::vector may confuse subsequent maintainers. If on the other hand most of the data manipulation looks like plain maths or vector like then a std::vector is more appropriate." -- https://stackoverflow.com/a/1556294/24473928
*/
typedef FilePath FileHash; /* TODO: `std::unordered_set<std::basic_string<unsigned char>>` */
typedef class PortableExecutable {
typedef class PortableExecutable : Object{
/* TODO: union of actual Portable Executable (Microsoft) + ELF (Linux) specifications */
public:
const std::string getName() {return "Susuwu::class PortableExecutable";}
FilePath path; /* Suchas "C:\Program.exe" or "/usr/bin/library.so" */
FileBytecode bytecode; /* compiled programs; bytecode */
std::string hex; /* `hexdump(path)`, hexadecimal, for C string functions */
Expand Down
5 changes: 3 additions & 2 deletions cxx/ClassResultList.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#pragma once
#ifndef INCLUDES_cxx_ClassResultList_hxx
#define INCLUDES_cxx_ClassResultList_hxx
#include "ClassPortableExecutable.hxx" /* FilePath FileBytecode FileHash*/
//#include "ClassObject.hxx" /* Object */ /* TODO: fix "Initialization of non-aggregate type" */
#include "ClassPortableExecutable.hxx" /* FilePath FileBytecode FileHash */
#include <algorithm> /* std::search std::find std::set_intersection */
#include <cstddef> /* size_t */
#if PREFERENCE_IS_CSTR
Expand All @@ -15,7 +16,7 @@ namespace Susuwu {
typedef FileHash ResultListHash;
typedef FileBytecode ResultListBytecode; /* Should have structure of FileBytecode, but is not just for files, can use for UTF8/webpages, so have a new type for this */
typedef FilePath ResultListSignature; /* TODO: `typedef ResultListBytecode ResultListSignature; ResultListSignature("string literal");` */
typedef struct ResultList { /* Lists of files (or pages) */
typedef struct ResultList /* : Object */ { /* Lists of files (or pages) */
std::unordered_set<ResultListHash> hashes; /* Unique checksums of files (or pages), to avoid duplicates, plus to do fast checks for existance */
std::vector<ResultListSignature> signatures; /* Smallest substrings (or regexes, or Universal Resource Locator) unique to this, has uses close to `hashes` but can match if files have small differences */
std::vector<ResultListBytecode> bytecodes; /* Whole files (or webpages); uses lots of space, just populate this for signature synthesis (or training CNS). */
Expand Down
50 changes: 26 additions & 24 deletions posts/VirusAnalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ typedef FilePath FileBytecode; /* Uses `std::string` for bytecode (versus `std::
* "If you are going to use the data in a string like fashon then you should opt for std::string as using a std::vector may confuse subsequent maintainers. If on the other hand most of the data manipulation looks like plain maths or vector like then a std::vector is more appropriate." -- https://stackoverflow.com/a/1556294/24473928
*/
typedef FilePath FileHash; /* TODO: `std::unordered_set<std::basic_string<unsigned char>>` */
typedef class PortableExecutable {
typedef class PortableExecutable : Object {
/* TODO: union of actual Portable Executable (Microsoft) + ELF (Linux) specifications */
public:
const std::string getName() {return "Susuwu::class PortableExecutable";}
FilePath path; /* Suchas "C:\Program.exe" or "/usr/bin/library.so" */
FileBytecode bytecode; /* compiled programs; bytecode */
std::string hex; /* `hexdump(path)`, hexadecimal, for C string functions */
Expand All @@ -39,7 +40,7 @@ public:
typedef FileHash ResultListHash;
typedef FileBytecode ResultListBytecode; /* Should have structure of FileBytecode, but is not just for files, can use for UTF8/webpages, so have a new type for this */
typedef FilePath ResultListSignature; /* TODO: `typedef ResultListBytecode ResultListSignature; ResultListSignature("string literal");` */
typedef struct ResultList { /* Lists of files (or pages) */
typedef struct ResultList /* : Object */ { /* Lists of files (or pages) */
std::unordered_set<ResultListHash> hashes; /* Unique checksums of files (or pages), to avoid duplicates, plus to do fast checks for existance */
std::vector<ResultListSignature> signatures; /* Smallest substrings (or regexes, or Universal Resource Locator) unique to this, has uses close to `hashes` but can match if files have small differences */
std::vector<ResultListBytecode> bytecodes; /* Whole files (or webpages); uses lots of space, just populate this for signature synthesis (or training CNS). */
Expand Down Expand Up @@ -165,8 +166,9 @@ typedef enum CnsMode : char {
* @pre @code (-1 != access(argv[0], X_OK) @endcode */
const int execves(/* const std::string &pathname, -- `execve` requires `&pathname == &argv[0]` */ const std::vector<const std::string> &argvS = {}, const std::vector<const std::string> &envpS = {});
static const int execvex(const std::string &toSh) {return execves({"/bin/sh", "-c", toSh});}
typedef class Cns {
typedef class Cns : Object{
public:
const std::string getName() {return "Susuwu::class Cns";}
virtual ~Cns() = default;
virtual const bool hasImplementation() const {return typeid(Cns) != typeid(this);}
virtual const bool isInitialized() const {return initialized;}
Expand Down Expand Up @@ -321,37 +323,37 @@ initial_range = (-0.5, 0.5)
# Create layersOfNeurons+1 hierarchical layers of sizes = neuronsPerLayer, and outputNeurons for last
self_organizing_network = SelfOrganizingNetwork(
input_size=input_size,
layer_sizes=layer_sizes,
input_percents=input_percents,
learning_rates=learning_rate,
boost_factors=boost_factor,
node_counts=node_count,
winner_counts=winner_count,
initial_ranges=initial_range)
input_size=input_size,
layer_sizes=layer_sizes,
input_percents=input_percents,
learning_rates=learning_rate,
boost_factors=boost_factor,
node_counts=node_count,
winner_counts=winner_count,
initial_ranges=initial_range)
# Create a set of sparse samples
samples = []");
foreach(inputsToOutputs as sample) { /* TODO: templates not allowed for virtual functions with C++ ( https://stackoverflow.com/a/78440416/24473928 ), so must produce codes for each combination of inputMode+outputMode */
PyRun_SimpleString("samples.append(" + sample.first() +" -> " + sample.last() + ")");
}
PyRun_SimpleString("for i in range(200):
self_organizing_network.train(samples)
self_organizing_network.train(samples)
");
#else /* else !USE_PYRUN */
PyObject *module = PyImport_ImportModule("hsom")
if(NULL == module) {throw "'hsom' module not found";}
PyObject *module = PyImport_ImportModule("hsom")
if(NULL == module) {throw "'hsom' module not found";}
PyObject *selfOrganizingNetwork = PyObject_GetAttrString(module,(char*)"SelfOrganizingNetwork"); /* or "PyObject *pDict = PyModule_GetDict(module); PyObject *selfOrganizingNetwork = PyDict_GetItemString(pDict, (char*)"SelfOrganizingNetwork");" */
if(NULL == selfOrganizingNetwork || !PyCallable_Check(selfOrganizingNetwork)) {throw "'SelfOrganizingNetwork' object not found";}
double result = PyObject_CallFunction(selfOrganizingNetwork, "d", 2.0); /* or "PyObject *pValue=Py_BuildValue("(z)",(char*)"args"); PyObject *pResult=PyObject_CallObject(selfOrganizingNetwork, pValue); if(NULL == pResult) {throw "PyObject_CallObject failed";} double result = PyInt_AsLong(pResult)); Py_DECREF(pValue);" */
Py_DECREF(module);
~HsomCns() {
if(NULL == selfOrganizingNetwork || !PyCallable_Check(selfOrganizingNetwork)) {throw "'SelfOrganizingNetwork' object not found";}
double result = PyObject_CallFunction(selfOrganizingNetwork, "d", 2.0); /* or "PyObject *pValue=Py_BuildValue("(z)",(char*)"args"); PyObject *pResult=PyObject_CallObject(selfOrganizingNetwork, pValue); if(NULL == pResult) {throw "PyObject_CallObject failed";} double result = PyInt_AsLong(pResult)); Py_DECREF(pValue);" */
Py_DECREF(module);
~HsomCns() {
#if PYTHON3
Py_FinalizeEx();
Py_FinalizeEx();
#else /* else !PYTHON */
Py_Finalize();
Py_Finalize();
#endif /* PYTHON3 else */
}
}
#endif /* USE_PYRUN else */
} HsomCns;
#endif /* USE_HSOM_CNS */
Expand Down Expand Up @@ -802,7 +804,7 @@ void questionsResponsesFromHosts(ResultList &questionsOrNull, ResultList &respon
for(auto host : hosts) {
execvex("wget '" + host + "/robots.txt' -Orobots.txt");
execvex("wget '" + host + "' -Oindex.xhtml");
questionsOrNull.signatures.push_back(host);
questionsOrNull.signatures.push_back(host);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, "index.xhtml");
}
}
Expand Down Expand Up @@ -830,7 +832,7 @@ void questionsResponsesFromXhtml(ResultList &questionsOrNull, ResultList &respon
for(auto url : urls) {
if(!listHasValue(questionsOrNull.signatures, url) && !listHasValue(noRobots, url)) {
execvex("wget '" + url + "' -O" + xhtmlFile);
questionsOrNull.signatures.push_back(url);
questionsOrNull.signatures.push_back(url);
questionsResponsesFromXhtml(questionsOrNull, responsesOrNull, xhtmlFile);
}
}
Expand Down Expand Up @@ -878,7 +880,7 @@ void assistantCnsLoopProcess(const Cns &cns) {
if(bytecode == previous && responses.size() > 1 + nthResponse) {
++nthResponse; /* Similar to "suggestions" for next questions, but just uses previous question to give new responses */
} else {
nthResponse = 0;
nthResponse = 0;
}
#endif /* IGNORE_PAST_MESSAGES */
std::cout << responses.at(nthResponse);
Expand Down

0 comments on commit 11327e3

Please sign in to comment.