29#define _GNU_SOURCE_WAS_NOT_DEFINED
46#if defined(__linux__) || defined(__windows__)
50#include <sys/sysinfo.h>
52#ifdef _GNU_SOURCE_WAS_NOT_DEFINED
61static char*
__internal_root_key =
"ENOFGLUCUNZJADRDMCZZSACRBKXAGSLTTFCICPALKHMWWGLVUIFYSLQJIHHXSHOZGRABFNBHGVTOMEBTVIPXZJHHEKIYWNVTWQKERROCTXFGMMLYUJSJFWLFCHQQMMUM";
69 if ((rootkey_size % 2) != 0) {
70 n_log(
LOG_ERR,
"key size %zu is not a multiple of 2", rootkey_size);
73 time_t t = time(NULL);
81 srand((
unsigned int)t);
82 for (
size_t it = 0; it < rootkey_size; it++) {
83 output->
data[it] = (char)(
'A' + rand() % 26);
99 bIsRetrieved = GetVolumeInformation(NULL, NULL, 0, &dwVolSerial, NULL, NULL, NULL, 0);
102 nstrprintf(output,
"%X", (
unsigned int)dwVolSerial);
104 n_log(
LOG_ERR,
"Could not retrieve current directory disk serial id");
109 char* curdir = getcwd(NULL, 0);
112 nstrprintf(cmd,
"df -T \"%s\" | awk '/^\\/dev/ {print $1}'", curdir);
115 N_STR* cur_disk = NULL;
120 nstrprintf(cmd,
"udevadm info --query=all --name=\"%s\" | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'",
_nstr(cur_disk));
124 N_STR* hd_serial = NULL;
130 for (
unsigned int it = 0; it < hd_serial->
written; it++) {
131 if (isspace(hd_serial->
data[it])) {
132 hd_serial->
data[it] =
'-';
139 char* curdir = getcwd(NULL, 0);
140 N_STR* hostid = NULL;
142 n_popen(
"hostid", 1024, (
void**)&hostid, &ret);
157 N_STR* cpu_id = NULL;
160#if defined(__i386__) || defined(__x86_64__)
162 unsigned int eax, ebx, ecx, edx;
164 if (__get_cpuid_max(0, NULL) >= 1 &&
165 __get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
166 unsigned int stepping = eax & 0xF;
167 unsigned int model = (eax >> 4) & 0xF;
168 unsigned int family = (eax >> 8) & 0xF;
169 unsigned int processorType = (eax >> 12) & 0x3;
171 nstrprintf(cpu_id,
"%u%u%u%u-%u%u%u%u-%u%u%u%u-%u%u",
187 (processorType >> 1) & 1,
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, ®s,
sizeof(regs), 0) ==
sizeof(regs)) {
215#if defined(__linux__) || defined(__ANDROID__)
216 FILE* f = fopen(
"/proc/cpuinfo",
"r");
220 while (fgets(line,
sizeof(line), f)) {
221 if (strncmp(line,
"model name", 10) == 0 ||
222 strncmp(line,
"Hardware", 8) == 0) {
223 char* colon = strchr(line,
':');
226 colon[strcspn(colon,
"\n")] = 0;
279 if (
key->written == 0)
return NULL;
284 size_t end_pos = params->
end_pos;
289 for (
size_t i = start_pos; i < end_pos; i++) {
291 char offset = cIsUpper ?
'A' :
'a';
292 keyIndexMod = i %
key->written;
294 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] + k) - offset), 26)) + offset);
298 for (
size_t i = start_pos; i < end_pos; i++) {
300 char offset = cIsUpper ?
'A' :
'a';
301 keyIndexMod = i %
key->written;
303 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] - k) - offset), 26)) + offset);
331 if ((
key->written % 2) != 0) {
336 for (
size_t i = 0; i <
key->written; ++i) {
337 if (!isalpha(
key->data[i])) {
352 GetSystemInfo(&sysinfo);
353 size_t nb_procs = (size_t)sysinfo.dwNumberOfProcessors;
355 size_t nb_procs = (size_t)get_nprocs();
363 n_log(
LOG_ERR,
"Could not create thread pool for vigenere cypher");
368 size_t bytesteps = input->
written / nb_procs;
369 if (bytesteps == 0) bytesteps = input->
written;
372 for (
size_t it = 0; it < input->
written; it += bytesteps) {
373 end_pos = it + bytesteps;
380 params->
input = input;
388 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
394 if (end_pos < input->written) {
397 params->
input = input;
405 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
519 if ((question_size % 2) != 0) {
520 n_log(
LOG_ERR,
"odd number of elements for question size, %zu", question_size);
524 N_STR* output = NULL;
534 output =
new_nstr(question_size + 1);
535 for (it = 0; it < question_size; it++) {
536 int val = tmpoutput->
data[it % tmpoutput->
written];
537 while (val <
'A') val += 26;
538 while (val >
'Z') val -= 26;
539 output->
data[it] = (char)val;
541 output->
written = question_size;
545 output =
new_nstr(question_size + 1);
546 for (it = 0; it < question_size; it++) {
547 int val = rand() % 256;
548 while (val <
'A') val += 26;
549 while (val >
'Z') val -= 26;
550 output->
data[it] = (char)val;
562 while (it < encoded_data->written) {
563 if (encoded_data->
data[it] ==
'=') {
564 encoded_data->
data[it] =
'\0';
583 if (!decoded_question) {
593 n_log(
LOG_ERR,
"question key of size %zu is not a multiple of root key of size %zu", decoded_question->
written, root_key->
written);
599 for (uint32_t it = 0; it < root_key->
written; it++) {
600 int32_t val = (root_key->
data[it % root_key->
written] -
'A') + (decoded_question->
data[it % decoded_question->
written] -
'A');
603 answer->
data[it] = (char)val;
622 if ((
key->written % 2) != 0) {
666 if ((
key->written % 2) != 0) {
711 n_log(
LOG_ERR,
"could not base64 decode: input_data %p", input_data);
716 if (!encoded_with_answer_data) {
717 n_log(
LOG_ERR,
"could not encode: input_data %p", input_data);
723 if (!decoded_question) {
732 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
759 n_log(
LOG_ERR,
"could not encode input_data %p", input_data);
765 if (!encoded_with_answer_data) {
766 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
774 n_log(
LOG_ERR,
"could not base64 decode: input_data %p", input_data);
THREAD_POOL * thread_pool
#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.
int n_popen(char *cmd, size_t read_buf_size, void **nstr_output, int *ret)
launch a command and return output and status
char n_tolower(char c)
convert char to lower case
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)
convert char to upper case
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 wait_for_threaded_pool(THREAD_POOL *thread_pool)
Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without pol...
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, not queued
Structure of a thread 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.