KLDR almost done, now rly

This commit is contained in:
Shr3dd3r 2023-08-25 06:29:29 +03:00
parent 786460d41b
commit c1c1970d04
7 changed files with 178 additions and 249 deletions

View File

@ -1,9 +1,9 @@
# libstadium makefile
CC = g++
CFLAGS_DEFAULT = -Wall -Wpedantic -Wno-uninitialized -std=c++23 -static-libgcc -static-libstdc++ -static
CFLAGS_DEBUG = -Og -fanalyzer -ggdb
CFLAGS_RELEASE = -Ofast -fdevirtualize-speculatively -fdata-sections -ffunction-sections -Wl,-gc-sections -Wl,-strip-all -Wl,-strip-discarded -flto -s
CFLAGS_DEFAULT = -march=native -std=c++23
CFLAGS_DEBUG = -Wall -Werror -Wno-uninitialized -O0 -fanalyzer -ggdb -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment
CFLAGS_RELEASE = -static-libgcc -static-libstdc++ -static -Ofast -fdevirtualize-speculatively -fdata-sections -ffunction-sections -Wl,-gc-sections -Wl,-strip-all -Wl,-strip-discarded -flto -s -D_FORTIFY_SOURCE=2
SOURCES = src/stadium.hpp src/stadium.cpp src/kldr.hpp
SOURCES_TEST = src/test.cpp
@ -12,7 +12,7 @@ OUTPUT_LIB = libstadium.so
OUTPUT_TEST_BIN = libstadiumtest
default: clean debug_test
default: clean release_test
debug_test: $(SOURCES) $(SOURCES_TEST)
@ -23,7 +23,7 @@ debug: $(SOURCES)
echo "NYI"
release_test: $(SOURCES)
echo "NYI"
$(CC) $(CFLAGS_DEFAULT) $(CFLAGS_RELEASE) $(SOURCES) $(SOURCES_TEST) -o $(OUTPUT_TEST_BIN)
release: $(SOURCES)
echo "NYI"

BIN
libstadiumtest Executable file

Binary file not shown.

View File

@ -1,102 +0,0 @@
/*
* kldr.cpp
* Copyright (c) 2023 Cyclone Team. Licensed under GNU GPLv3-only terms.
*
*/
#include "kldr.hpp"
namespace Stadium {
namespace Base {
// Destructor
template <typename KeyT, typename LengthT>
KLDRArray<KeyT, LengthT>::~KLDRArray () {
// Freeing all pointers if vector is not empty
for (size_t i = 0; i < this->Values.size(); i++) {
operator delete(this->Values[i]);
}
}
// Print all contents of array to stdout
template <typename KeyT, typename LengthT>
void KLDRArray<KeyT, LengthT>::Print () {
for (size_t i = 0; i < this->Keys.size(); i++) {
std::cout << "Key: " << std::hex << std::setw(sizeof(KeyT) * 2) << this->Keys[i] << "; ";
std::cout << "Length: " << std::dec << this->Lengths[i] << "; ";
std::cout << "Value: " << std::hex << std::setfill('0') << std::setw(2);
for (LengthT j = 0; j < this->Lengths[i]; j++)
std::cout << this->Values[j] << " ";
std::cout << std::dec << std::endl;
}
}
// Add new cell to array
template <typename KeyT, typename LengthT>
void KLDRArray<KeyT, LengthT>::Add (KeyT key, LengthT length, void* data) {
this->Keys.push_back(key);
this->Lengths.push_back(length);
void* newData = operator new(length); // Yes, allocating memory for void pointer really looks like this
std::copy(data, data + length, newData); // ATTENTION: may be we should not copy, but rather use pointer to already allocated mem
this->Values.push_back(std::move(newData)); // NOTICE: move or not to move...
// NOTICE: there is `std::is_pod<SomeType>()`, so may be we can use one more template to make this a little more safe
}
// Get just value from array by key
template <typename KeyT, typename LengthT>
void* KLDRArray<KeyT, LengthT>::Get (KeyT key) {
for (size_t i = 0; i < this->Keys.size(); i++) {
if (this->Keys[i] == key) {
return this->Values[i];
}
}
throw std::invalid_argument("invalid KLDRArray key");
}
// Get value and length from array by key
template <typename KeyT, typename LengthT>
void* KLDRArray<KeyT, LengthT>::Get (KeyT key, LengthT* length) {
for (size_t i = 0; i < this->Keys.size(); i++) {
if (this->Keys[i] == key) {
*length = this->Lengths[i];
return this->Values[i];
}
}
throw std::invalid_argument("invalid KLDRArray key");
}
// Delete cell from array by key
template <typename KeyT, typename LengthT>
void KLDRArray<KeyT, LengthT>::Del (KeyT key) {
for (size_t i = 0; i < this->Keys.size(); i++) {
if (this->Keys[i] == key) {
this->Keys.erase(this->Keys.begin() + i);
this->Lengths.erase(this->Lengths.begin() + i);
this->Values.erase(this->Values.begin() + i);
return;
}
}
throw std::invalid_argument("invalid KLDRArray key");
}
// Shuffle array
template <typename KeyT, typename LengthT>
void KLDRArray<KeyT, LengthT>::Shuffle () {
std::random_device seed;
std::mt19937 gen{seed()};
std::uniform_int_distribution<size_t> dist{0, this->Keys.size()};
size_t pickedIndex = 0;
for (size_t i = 0; i < this->Keys.size(); i++) {
do
pickedIndex = dist(gen);
while (pickedIndex == i);
std::swap(this->Keys[i], this->Keys[pickedIndex]);
std::swap(this->Lengths[i], this->Length[pickedIndex]);
std::swap(this->Values[i], this->Values[pickedIndex]);
}
}
}
}

View File

@ -1,128 +0,0 @@
/*
* kldr.hpp
* Copyright (c) 2023 Cyclone Team. Licensed under GNU GPLv3-only terms.
*
*/
#ifndef LIBSTADIUM_KLDR_HPP
#define LIBSTADIUM_KLDR_HPP
#include <cstdio>
#include <iomanip>
#include <vector>
#include <random>
#include <exception>
namespace Stadium {
namespace Base {
// NOTICE: This can be more optimized
template <typename KeyT = uint8_t, typename LengthT = uint16_t>
class KLDRArray {
protected:
std::vector<KeyT> Keys;
std::vector<LengthT> Lengths;
std::vector<void*> Values; // We should use void* as it is more abstract, BUT! Then we need to deal with schizophrenical errors, like segfault when valid void pointer is freed
public:
//KLDRArray (); // TODO: check used types
// Destructor
~KLDRArray () {
// Freeing all pointers if vector is not empty
for (size_t i = 0; i < this->Keys.size(); i++) {
operator delete(this->Values[i]);
}
}
// Print all contents of array to stdout
void Print () {
printf("Elements amount: %lu\n", this->Keys.size());
for (size_t i = 0; i < this->Keys.size(); i++) {
printf("Key: 0x%.2X; ", (KeyT)this->Keys[i]);
printf("Length: %u; ", (LengthT)this->Lengths[i]);
printf("Value: ");
for (LengthT j = 0; j < this->Lengths[i]; j++)
printf("%.2X ", ((uint8_t*)this->Values[i])[j]);
printf("\n");
// std::cout << "Key: " << std::hex << std::setw(sizeof(KeyT) * 2) << this->Keys[i] << "; ";
// std::cout << "Length: " << std::dec << this->Lengths[i] << "; ";
// std::cout << "Value: " << std::hex;
// for (LengthT j = 0; j < this->Lengths[i]; j++)
// std::cout << std::setfill('0') << std::setw(2) << this->Values[j] << " ";
// std::cout << std::dec << std::endl;
}
}
// Add new cell to array
// WARNING: pointer supplied as "data" may be invalid after this function call
void Add (KeyT key, LengthT length, void* data) {
this->Keys.push_back(key);
this->Lengths.push_back(length);
//void* newData = operator new(length); // Yes, allocating memory for void pointer really looks like this
//std::copy(data, data + length, newData);
this->Values.push_back(std::move(data)); // NOTICE: move or not to move...
// NOTICE: there is `std::is_pod<SomeType>()`, so may be we can use one more template to make this a little more safe
}
// Get just value from array by key
void* Get (KeyT key) {
for (size_t i = 0; i < this->Keys.size(); i++) {
if (this->Keys[i] == key) {
return this->Values[i];
}
}
throw std::invalid_argument("invalid KLDRArray key");
}
// Get value and length from array by key
void* Get (KeyT key, LengthT* length) {
for (size_t i = 0; i < this->Keys.size(); i++) {
if (this->Keys[i] == key) {
*length = this->Lengths[i];
return this->Values[i];
}
}
throw std::invalid_argument("invalid KLDRArray key");
}
// Delete cell from array by key
void Del (KeyT key) {
for (size_t i = 0; i < this->Keys.size(); i++) {
if (this->Keys[i] == key) {
this->Keys.erase(this->Keys.begin() + i);
this->Lengths.erase(this->Lengths.begin() + i);
this->Values.erase(this->Values.begin() + i);
return;
}
}
throw std::invalid_argument("invalid KLDRArray key");
}
// Shuffle array
void Shuffle () {
std::random_device seed;
std::mt19937 gen{seed()};
std::uniform_int_distribution<size_t> dist{0, this->Keys.size()};
size_t pickedIndex = 0;
for (size_t i = 0; i < this->Keys.size(); i++) {
do
pickedIndex = dist(gen);
while (pickedIndex == i);
std::swap(this->Keys[i], this->Keys[pickedIndex]);
std::swap(this->Lengths[i], this->Length[pickedIndex]);
std::swap(this->Values[i], this->Values[pickedIndex]);
}
}
};
}
}
#endif

View File

@ -20,7 +20,33 @@
namespace Stadium {
namespace Base {
// NOTICE: This can be more optimized
enum KLDRDefaultKeys : uint8_t {
Data = 0x01,
ObjectID = 0x02,
SrcEventAuthor = 0x03,
PrevEvent = 0x04,
NextEvent = 0x05,
BatchNumber = 0x06,
Path = 0x07,
Power = 0x08,
// Crypto-related
CryptoAlgos = 0x11,
CryptoKeyID = 0x12,
SignedDataHash = 0x13
};
/*
* KLDR stands for "Key-Length-Data-Repeat"
* Schematically, binary array with KLDR-formatted data looks like this.
* Single cell:
* [key: 1/2 bytes][length of data: 1/2/4 bytes][data: length of data bytes]
* Whole array:
* [cell 1][cell 2]...[cell n]
*
* Suffix "F" in function name stands for "Fast", i.e. it is unsafe, but fast version.
*
* This can be more optimized for performance with cost of more memory usage.
*/
template <typename KeyT = uint8_t, typename LengthT = uint16_t>
class KLDRArray {
protected:
@ -29,7 +55,7 @@ class KLDRArray {
std::vector<void*> Values;
public:
//KLDRArray (); // TODO: check used types
//KLDRArray (); // TODO: check used types and/or parse given char array/vector
// Destructor
~KLDRArray () {
@ -39,12 +65,77 @@ class KLDRArray {
}
}
// Write data to PREALLOCATED char array
// WARNING: unsafe and probably UB (may be i test this later but who knows)
void AsArrayF (char* arr) {
char* ptr = arr;
for (size_t i = 0; i < this->CellsAmount(); i++) {
std::copy((char*)&this->Keys[i], (char*)(&this->Keys[i] + sizeof(KeyT)), ptr);
ptr += sizeof(KeyT);
std::copy((char*)&this->Lengths[i], (char*)(&this->Lengths[i] + sizeof(LengthT)), ptr);
ptr += sizeof(LengthT);
std::copy((char*)this->Values[i], ((char*)this->Values[i]) + this->Lengths[i], ptr);
ptr += this->Lengths[i];
}
}
// Return contents as char vector
std::vector<char> AsArray () {
std::vector<char> result;
for (size_t i = 0; i < this->CellsAmount(); i++) {
for (uint j = 0; j < sizeof(KeyT); j++)
result.push_back(((char*)(&this->Keys[i]))[j]);
for (uint j = 0; j < sizeof(LengthT); j++)
result.push_back(((char*)(&this->Lengths[i]))[j]);
for (uint j = 0; j < this->Lengths[i]; j++)
result.push_back(((char*)this->Values[i])[j]);
}
return result;
}
// Print all contents of array to stdout
void Print () {
printf("Elements amount: %lu\n", this->Keys.size());
for (size_t i = 0; i < this->Keys.size(); i++) {
printf("Key: 0x%.2X; ", (KeyT)this->Keys[i]);
printf("Length: %u; ", (LengthT)this->Lengths[i]);
printf("Elements amount: %lu\n", this->CellsAmount());
for (size_t i = 0; i < this->CellsAmount(); i++) {
printf("Key: ");
switch (this->Keys[i]) {
case KLDRDefaultKeys::Data:
printf("Data");
break;
case KLDRDefaultKeys::ObjectID:
printf("ObjectID");
break;
case KLDRDefaultKeys::SrcEventAuthor:
printf("SrcEventAuthor");
break;
case KLDRDefaultKeys::PrevEvent:
printf("PrevEvent");
break;
case KLDRDefaultKeys::NextEvent:
printf("NextEvent");
break;
case KLDRDefaultKeys::BatchNumber:
printf("BatchNumber");
break;
case KLDRDefaultKeys::Path:
printf("Path");
break;
case KLDRDefaultKeys::Power:
printf("Power");
break;
case KLDRDefaultKeys::CryptoAlgos:
printf("CryptoAlgos");
break;
case KLDRDefaultKeys::CryptoKeyID:
printf("CryptoKeyID");
break;
case KLDRDefaultKeys::SignedDataHash:
printf("SignedDataHash");
break;
default:
printf("0x%.2X", (KeyT)this->Keys[i]);
}
printf("; Length: %u; ", (LengthT)this->Lengths[i]);
printf("Value: ");
for (LengthT j = 0; j < this->Lengths[i]; j++)
printf("%.2X ", ((uint8_t*)this->Values[i])[j]);
@ -52,6 +143,29 @@ class KLDRArray {
}
}
// Get amount of cells in array
inline size_t CellsAmount () {
return this->Keys.size();
}
// Get size of all data in array as if they in packed condition
size_t FlatSize () {
size_t result = this->Keys.size() * sizeof(KeyT) + this->Lengths.size() * sizeof(LengthT);
for (size_t i = 0; i < this->CellsAmount(); i++)
result += this->Lengths[i];
return result;
}
// If there are specified key
bool KeyExists (KeyT key) {
for (size_t i = 0; i < this->CellsAmount(); i++) {
if (this->Keys[i] == key) {
return true;
}
}
return false;
}
// Add new cell to array, fast version without keys check
void AddF (KeyT key, LengthT length, void* data) {
this->Keys.push_back(key);
@ -63,7 +177,7 @@ class KLDRArray {
}
void Add (KeyT key, LengthT length, void* data) {
for (size_t i = 0; i < this->Keys.size(); i++) {
for (size_t i = 0; i < this->CellsAmount(); i++) {
if (this->Keys[i] == key) {
throw std::invalid_argument("supplied key already exist");
}
@ -73,7 +187,7 @@ class KLDRArray {
// Get just value from array by key
void* Get (KeyT key) {
for (size_t i = 0; i < this->Keys.size(); i++) {
for (size_t i = 0; i < this->CellsAmount(); i++) {
if (this->Keys[i] == key) {
return this->Values[i];
}
@ -83,7 +197,7 @@ class KLDRArray {
// Get value and length from array by key
void* Get (KeyT key, LengthT* length) {
for (size_t i = 0; i < this->Keys.size(); i++) {
for (size_t i = 0; i < this->CellsAmount(); i++) {
if (this->Keys[i] == key) {
*length = this->Lengths[i];
return this->Values[i];
@ -94,7 +208,7 @@ class KLDRArray {
// Delete cell from array by key
void Del (KeyT key) {
for (size_t i = 0; i < this->Keys.size(); i++) {
for (size_t i = 0; i < this->CellsAmount(); i++) {
if (this->Keys[i] == key) {
this->Keys.erase(this->Keys.begin() + i);
this->Lengths.erase(this->Lengths.begin() + i);
@ -107,7 +221,7 @@ class KLDRArray {
// Shuffle array
void Shuffle () {
size_t elements = this->Keys.size();
size_t elements = this->CellsAmount();
std::random_device seed;
std::mt19937 gen{seed()};
std::uniform_int_distribution<size_t> dist{0, elements-1};

View File

@ -1,5 +1,6 @@
#include <cstdio>
#include "stadium.hpp"
#include "utils.hpp"
int main () {
@ -9,14 +10,19 @@ int main () {
char str3[] = "fisting\n";
char str4[] = "IS 300$!!!!!!!!!!!!!!!!!!\n";
char str5[] = "a)\n";
arr.Add(0x01, sizeof(str1), (void*)&str1);
arr.Add(0x02, sizeof(str2), (void*)&str2);
arr.Add(Stadium::Base::KLDRDefaultKeys::Data, sizeof(str1), (void*)&str1);
arr.Add(Stadium::Base::KLDRDefaultKeys::ObjectID, sizeof(str2), (void*)&str2);
arr.Add(0xF4, sizeof(str3), (void*)&str3);
arr.Add(0x6a, sizeof(str4), (void*)&str4);
arr.Add(0x6c, sizeof(str5), (void*)&str5);
arr.Add(Stadium::Base::KLDRDefaultKeys::SignedDataHash, sizeof(str5), (void*)&str5);
arr.Print();
std::cout << "Shuffling!" << std::endl;
arr.Shuffle();
arr.Print();
size_t arr_sz = arr.FlatSize();
std::cout << "Calculated flat binary array size: " << arr_sz << std::endl;
std::cout << "Its contents:" << std::endl;
Stadium::Utils::PrintArray(arr.AsArray());
return 0;
}

39
src/utils.hpp Normal file
View File

@ -0,0 +1,39 @@
/*
* utils.hpp
* Copyright (c) 2023 Cyclone Team. Licensed under GNU GPLv3-only terms.
*
*/
#ifndef LIBSTADIUM_UTILS_HPP
#define LIBSTADIUM_UTILS_HPP
#include <iostream>
#include <vector>
namespace Stadium {
namespace Utils {
void PrintArray (std::vector<char> arr) {
printf("Length: %lu\n", arr.size());
for (size_t i = 0; i < arr.size(); i++)
printf("%.2X ", (uint8_t)arr[i]);
printf("\n");
}
void PrintArray (char* arr, size_t len) {
printf("Length: %lu\n", len);
for (size_t i = 0; i < len; i++)
printf("%.2X ", (uint8_t)arr[i]);
printf("\n");
}
}
}
#endif