10#define _GNU_SOURCE_WAS_NOT_DEFINED
27#if defined(__linux__) || defined(__windows__)
31#include <sys/sysinfo.h>
33#ifdef _GNU_SOURCE_WAS_NOT_DEFINED
42static char*
__internal_root_key =
"ENOFGLUCUNZJADRDMCZZSACRBKXAGSLTTFCICPALKHMWWGLVUIFYSLQJIHHXSHOZGRABFNBHGVTOMEBTVIPXZJHHEKIYWNVTWQKERROCTXFGMMLYUJSJFWLFCHQQMMUM";
50 if ((rootkey_size % 2) != 0) {
51 n_log(
LOG_ERR,
"key size %d is not a multiple of 2", rootkey_size);
55 time_t t = time(NULL);
60 srand((
unsigned int)t);
61 for (
size_t it = 0; it < rootkey_size; it++) {
62 output->
data[it] = (char)(
'A' + rand() % 26);
78 bIsRetrieved = GetVolumeInformation(NULL, NULL, 0, &dwVolSerial, NULL, NULL, NULL, 0);
81 nstrprintf(output,
"%X", (
unsigned int)dwVolSerial);
83 n_log(
LOG_ERR,
"Could not retrieve current directory disk serial id");
88 char* curdir = getcwd(NULL, 0);
91 nstrprintf(cmd,
"df -T \"%s\" | awk '/^\\/dev/ {print $1}'", curdir);
94 N_STR* cur_disk = NULL;
96 n_popen(
_nstr(cmd), 1024, (
void**)&cur_disk, &ret);
99 nstrprintf(cmd,
"udevadm info --query=all --name=%s | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'",
_nstr(cur_disk));
100 n_popen(
_nstr(cmd), 1024, (
void**)&output, &ret);
103 N_STR* hd_serial = NULL;
109 for (
unsigned int it = 0; it < hd_serial->
written; it++) {
110 if (isspace(hd_serial->
data[it])) {
111 hd_serial->
data[it] =
'-';
118 char* curdir = getcwd(NULL, 0);
119 N_STR* hostid = NULL;
121 n_popen(
"hostid", 1024, (
void**)&hostid, &ret);
134 N_STR* cpu_id = NULL;
137 unsigned int a[4] = {0, 0, 0, 0};
139 if (__get_cpuid_max(0, NULL) >= 1) {
140 __get_cpuid(1, &a[0], &a[1], &a[2], &a[3]);
147 memset(&binaryNum, 0,
sizeof(binaryNum));
149 unsigned int n = a[3];
153 binaryNum[i] = n % 2;
161 binaryNum[i] = n % 2;
169 binaryNum[i] = n % 2;
177 binaryNum[i] = n % 2;
190 nstrprintf(cpu_id,
"%d%d%d%d-%d%d%d%d-%d%d%d%d-%d%d", binaryNum[3], binaryNum[2], binaryNum[1], binaryNum[0],
191 binaryNum[7], binaryNum[6], binaryNum[5], binaryNum[4],
192 binaryNum[11], binaryNum[10], binaryNum[9], binaryNum[8],
193 binaryNum[13], binaryNum[12]);
196 static const char devname[] =
"/dev/cpu/self/cpuid";
199 uint32_t r_eax, r_ebx, r_ecx, r_edx;
204 if ((d = open(devname, O_RDONLY)) != -1) {
205 if (pread(d, rp,
sizeof(*rp), 0) ==
sizeof(*rp)) {
206 s = (
char*)&rp->r_ebx;
253 size_t end_pos = params->
end_pos;
256 size_t keyIndexMod = 0;
258 for (
size_t i = start_pos; i < end_pos; i++) {
260 char offset = cIsUpper ?
'A' :
'a';
261 keyIndexMod = i % key->written;
262 int k = (cIsUpper ?
n_toupper(key->data[keyIndexMod]) :
n_tolower(key->data[keyIndexMod])) - offset;
263 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] + k) - offset), 26)) + offset);
267 for (
size_t i = start_pos; i < end_pos; i++) {
269 char offset = cIsUpper ?
'A' :
'a';
270 keyIndexMod = i % key->written;
271 int k = (cIsUpper ?
n_toupper(key->data[keyIndexMod]) :
n_tolower(key->data[keyIndexMod])) - offset;
272 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] - k) - offset), 26)) + offset);
299 if ((key->written % 2) != 0) {
300 n_log(
LOG_ERR,
"odd number of elements in root key %s, %d",
_nstr(key), key->written);
304 for (
size_t i = 0; i < key->written; ++i) {
305 if (!isalpha(key->data[i])) {
306 n_log(
LOG_ERR,
"key contain bad char '%c'", key->data[i]);
320 GetSystemInfo(&sysinfo);
321 size_t nb_procs = (size_t)sysinfo.dwNumberOfProcessors;
323 size_t nb_procs = (size_t)get_nprocs();
331 size_t bytesteps = input->
written / nb_procs;
332 if (bytesteps == 0) bytesteps = input->
written;
335 for (
size_t it = 0; it < input->
written; it += bytesteps) {
336 end_pos = it + bytesteps;
343 params->
input = input;
351 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
356 if (end_pos < input->written) {
359 params->
input = input;
367 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
480 if ((question_size % 2) != 0) {
481 n_log(
LOG_ERR,
"odd number of elements for question size, %d", question_size);
485 N_STR* output = NULL;
495 output =
new_nstr(question_size + 1);
496 for (it = 0; it < question_size; it++) {
497 int val = tmpoutput->
data[it % tmpoutput->
written];
498 while (val <
'A') val += 26;
499 while (val >
'Z') val -= 26;
500 output->
data[it] = (char)val;
502 output->
written = question_size;
506 output =
new_nstr(question_size + 1);
507 for (it = 0; it < question_size; it++) {
508 int val = rand() % 256;
509 while (val <
'A') val += 26;
510 while (val >
'Z') val -= 26;
511 output->
data[it] = (char)val;
519 while (it < encoded_data->written) {
520 if (encoded_data->
data[it] ==
'=') {
521 encoded_data->
data[it] =
'\0';
546 n_log(
LOG_ERR,
"question key of size %d is not a multiple of root key of size %d", decoded_question->
written, root_key->
written);
552 for (uint32_t it = 0; it < root_key->
written; it++) {
553 int32_t val = (root_key->
data[it % root_key->
written] -
'A') + (decoded_question->
data[it % decoded_question->
written] -
'A');
556 answer->
data[it] = (char)val;
575 if ((key->written % 2) != 0) {
576 n_log(
LOG_ERR,
"odd number of elements in key %s, %d",
_nstr(key), key->written);
618 if ((key->written % 2) != 0) {
619 n_log(
LOG_ERR,
"odd number of elements in key %s , %d",
_nstr(key), key->written);
662 n_log(
LOG_ERR,
"could not base64 decode: input_data %p", input_data);
667 if (!encoded_with_answer_data) {
668 n_log(
LOG_ERR,
"could not encode: input_data %p", input_data);
676 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
703 n_log(
LOG_ERR,
"could not encode input_data %p", input_data);
709 if (!encoded_with_answer_data) {
710 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
718 n_log(
LOG_ERR,
"could not base64 decode: input_data %p", input_data);
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define _str(__PTR)
define true
#define Free(__ptr)
Free Handler to get errors.
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
char n_tolower(char c)
is_alpha
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
bool n_isupper(char c)
test if char c is uppercase
char n_toupper(char c)
is_alpha
bool n_islower(char c)
test if char c is lowercase
N_STR * n_base64_encode(N_STR *input)
encode a N_STR *string
N_STR * n_vigenere_encode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere encode a file using key
int n_vigenere_decode_file_qa(N_STR *in, N_STR *out, N_STR *question, N_STR *answer)
directly vigenere decode a file using question and answer
N_STR * n_vigenere_quick_encode(N_STR *decoded_data)
quick encode data
N_STR * n_vigenere_decode(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
#define n_mathmod(__a, __b)
The % operator returns a result that adopts the sign of the dividend, a true mathematical modulus ado...
N_STR * n_vigenere_get_answer(N_STR *root_key, N_STR *question)
get an answer from a root key and a question
int n_vigenere_encode_file_qa(N_STR *in, N_STR *out, N_STR *question, N_STR *answer)
directly vigenere encode a file using question and answer
N_STR * n_vigenere_decode_qa(N_STR *input_data, N_STR *question, N_STR *answer)
directly vigenere decode a file using key
N_STR * n_vigenere_encode(N_STR *string, N_STR *key)
encode input using vigenere cypher and key
int n_vigenere_encode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere encode a file using key
int n_vigenere_decode_file(N_STR *in, N_STR *out, N_STR *key)
directly vigenere decode a file using key
N_STR * n_vigenere_get_rootkey(size_t rootkey_size)
get a rootkey randomly generated
N_STR * n_vigenere_get_question(size_t question_size)
get a question generated from the current machine hardware (disk&cpu)
N_STR * n_vigenere_quick_decode(N_STR *encoded_data)
quick decode data
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
size_t written
size of the written data inside the string
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
int nstr_to_file(N_STR *str, char *filename)
Write a N_STR content into a file.
N_STR * nstrdup(N_STR *str)
Duplicate a N_STR.
#define nstrprintf_cat(__nstr_var, __format,...)
Macro to quickly allocate and sprintf and cat to a N_STR.
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
N_STR * new_nstr(NSTRBYTE size)
create a new N_STR string
#define nstrprintf(__nstr_var, __format,...)
Macro to quickly allocate and sprintf to N_STR.
N_STR * file_to_nstr(char *filename)
Load a whole file into a N_STR.
A box including a string and his lenght.
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
int add_threaded_process(THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
add a function and params to a thread pool
int refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
int destroy_threaded_pool(THREAD_POOL **pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
#define DIRECT_PROC
processing mode for added func, direct start
int wait_for_threaded_pool(THREAD_POOL *thread_pool, unsigned int delay)
Wait for all the launched process in the thread pool to terminate.
Structure of a trhead pool.
base64 encoding and decoding functions using N_STR
Common headers and low-level functions & define.
static char * __internal_root_key
static internal root key, just to obfuscate text so we do not care if is is public
N_STR * n_vigenere_cypher(N_STR *input, N_STR *key, bool encipher, bool in_place)
encode or decode input using vigenere cypher
bool encipher
1 encoding, 0 decoding
void * n_vigenere_cypher_thread(void *ptr)
encode or decode threaded func using params from ptr
N_STR * n_vigenere_decode_in_place(N_STR *string, N_STR *key)
decode input using vigenere cypher and key
N_STR * n_get_current_dir_hd_serial()
get the serial of the current running dir hosting disk
size_t start_pos
input starting point
N_STR * key
encoding/decoding key
N_STR * n_vigenere_encode_in_place(N_STR *string, N_STR *key)
encode string in place using vigenere cypher and key
size_t end_pos
output starting point
N_STR * n_get_cpu_id()
get the CPU id
structure of a n_vigenere_cypher_thread param
vigenere encoding and decoding functions using N_STR/files