30#define _GNU_SOURCE_WAS_NOT_DEFINED
47#if defined(__linux__) || defined(__windows__)
51#include <sys/sysinfo.h>
53#ifdef _GNU_SOURCE_WAS_NOT_DEFINED
62static char*
__internal_root_key =
"ENOFGLUCUNZJADRDMCZZSACRBKXAGSLTTFCICPALKHMWWGLVUIFYSLQJIHHXSHOZGRABFNBHGVTOMEBTVIPXZJHHEKIYWNVTWQKERROCTXFGMMLYUJSJFWLFCHQQMMUM";
70 if ((rootkey_size % 2) != 0) {
71 n_log(
LOG_ERR,
"key size %zu is not a multiple of 2", rootkey_size);
74 time_t t = time(NULL);
82 srand((
unsigned int)t);
83 for (
size_t it = 0; it < rootkey_size; it++) {
84 output->
data[it] = (char)(
'A' + rand() % 26);
100 bIsRetrieved = GetVolumeInformation(NULL, NULL, 0, &dwVolSerial, NULL, NULL, NULL, 0);
103 nstrprintf(output,
"%X", (
unsigned int)dwVolSerial);
105 n_log(
LOG_ERR,
"Could not retrieve current directory disk serial id");
110 char* curdir = getcwd(NULL, 0);
113 nstrprintf(cmd,
"df -T \"%s\" | awk '/^\\/dev/ {print $1}'", curdir);
116 N_STR* cur_disk = NULL;
121 nstrprintf(cmd,
"udevadm info --query=all --name=\"%s\" | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'",
_nstr(cur_disk));
125 N_STR* hd_serial = NULL;
131 for (
unsigned int it = 0; it < hd_serial->
written; it++) {
132 if (isspace(hd_serial->
data[it])) {
133 hd_serial->
data[it] =
'-';
140 char* curdir = getcwd(NULL, 0);
141 N_STR* hostid = NULL;
143 n_popen(
"hostid", 1024, (
void**)&hostid, &ret);
158 N_STR* cpu_id = NULL;
161#if defined(__i386__) || defined(__x86_64__)
163 unsigned int eax, ebx, ecx, edx;
165 if (__get_cpuid_max(0, NULL) >= 1 &&
166 __get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
167 unsigned int stepping = eax & 0xF;
168 unsigned int model = (eax >> 4) & 0xF;
169 unsigned int family = (eax >> 8) & 0xF;
170 unsigned int processorType = (eax >> 12) & 0x3;
172 nstrprintf(cpu_id,
"%u%u%u%u-%u%u%u%u-%u%u%u%u-%u%u",
188 (processorType >> 1) & 1,
197 static const char devname[] =
"/dev/cpu/self/cpuid";
200 uint32_t r_eax, r_ebx, r_ecx, r_edx;
205 if ((d = open(devname, O_RDONLY)) != -1) {
206 if (pread(d, ®s,
sizeof(regs), 0) ==
sizeof(regs)) {
216#if defined(__linux__) || defined(__ANDROID__)
217 FILE* f = fopen(
"/proc/cpuinfo",
"r");
221 while (fgets(line,
sizeof(line), f)) {
222 if (strncmp(line,
"model name", 10) == 0 ||
223 strncmp(line,
"Hardware", 8) == 0) {
224 char* colon = strchr(line,
':');
227 colon[strcspn(colon,
"\n")] = 0;
280 if (
key->written == 0)
return NULL;
285 size_t end_pos = params->
end_pos;
290 for (
size_t i = start_pos; i < end_pos; i++) {
292 char offset = cIsUpper ?
'A' :
'a';
293 keyIndexMod = i %
key->written;
295 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] + k) - offset), 26)) + offset);
299 for (
size_t i = start_pos; i < end_pos; i++) {
301 char offset = cIsUpper ?
'A' :
'a';
302 keyIndexMod = i %
key->written;
304 dest->
data[i] = (char)((
n_mathmod(((input->
data[i] - k) - offset), 26)) + offset);
332 if ((
key->written % 2) != 0) {
337 for (
size_t i = 0; i <
key->written; ++i) {
338 if (!isalpha(
key->data[i])) {
353 GetSystemInfo(&sysinfo);
354 size_t nb_procs = (size_t)sysinfo.dwNumberOfProcessors;
356 size_t nb_procs = (size_t)get_nprocs();
364 n_log(
LOG_ERR,
"Could not create thread pool for vigenere cypher");
369 size_t bytesteps = input->
written / nb_procs;
370 if (bytesteps == 0) bytesteps = input->
written;
373 for (
size_t it = 0; it < input->
written; it += bytesteps) {
374 end_pos = it + bytesteps;
381 params->
input = input;
389 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
395 if (end_pos < input->written) {
398 params->
input = input;
406 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool");
520 if ((question_size % 2) != 0) {
521 n_log(
LOG_ERR,
"odd number of elements for question size, %zu", question_size);
525 N_STR* output = NULL;
535 output =
new_nstr(question_size + 1);
536 for (it = 0; it < question_size; it++) {
537 int val = tmpoutput->
data[it % tmpoutput->
written];
538 while (val <
'A') val += 26;
539 while (val >
'Z') val -= 26;
540 output->
data[it] = (char)val;
542 output->
written = question_size;
546 output =
new_nstr(question_size + 1);
547 for (it = 0; it < question_size; it++) {
548 int val = rand() % 256;
549 while (val <
'A') val += 26;
550 while (val >
'Z') val -= 26;
551 output->
data[it] = (char)val;
563 while (it < encoded_data->written) {
564 if (encoded_data->
data[it] ==
'=') {
565 encoded_data->
data[it] =
'\0';
584 if (!decoded_question) {
594 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);
600 for (uint32_t it = 0; it < root_key->
written; it++) {
601 int32_t val = (root_key->
data[it % root_key->
written] -
'A') + (decoded_question->
data[it % decoded_question->
written] -
'A');
604 answer->
data[it] = (char)val;
623 if ((
key->written % 2) != 0) {
667 if ((
key->written % 2) != 0) {
712 n_log(
LOG_ERR,
"could not base64 decode: input_data %p", input_data);
717 if (!encoded_with_answer_data) {
718 n_log(
LOG_ERR,
"could not encode: input_data %p", input_data);
724 if (!decoded_question) {
733 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
760 n_log(
LOG_ERR,
"could not encode input_data %p", input_data);
766 if (!encoded_with_answer_data) {
767 n_log(
LOG_ERR,
"could not decode: input_data %p", input_data);
775 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
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
size_t length
total allocation (in bytes) of the data buffer, padding included
#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.