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";
51 if( ( rootkey_size % 2 ) != 0 )
53 n_log(
LOG_ERR ,
"key size %d is not a multiple of 2" , rootkey_size );
57 srand( time( NULL ) );
58 for(
size_t it = 0 ; it < rootkey_size ; it ++ )
60 output -> data[ it ] =
'A' + rand()%26 ;
62 output -> written = rootkey_size ;
74 N_STR *output = NULL ;
79 bIsRetrieved = GetVolumeInformation(NULL,NULL,0,&dwVolSerial,NULL,NULL,NULL,0);
83 nstrprintf( output ,
"%X" , (
unsigned int)dwVolSerial );
85 n_log(
LOG_ERR ,
"Could not retrieve current directory disk serial id" );
90 char *curdir = getcwd( NULL , 0 );
93 nstrprintf( cmd ,
"df -T \"%s\" | awk '/^\\/dev/ {print $1}'" , curdir );
96 N_STR *cur_disk = NULL ;
98 n_popen(
_nstr( cmd ) , 1024 , (
void **)&cur_disk , &ret );
101 nstrprintf( cmd ,
"udevadm info --query=all --name=%s | egrep '(ID_SERIAL=|ID_FS_UUID=)' | awk -F'=' '{print $2}'" ,
_nstr( cur_disk ) );
102 n_popen(
_nstr( cmd ) , 1024 , (
void **)&output , &ret );
105 N_STR *hd_serial = NULL ;
111 for(
unsigned int it = 0 ; it < hd_serial -> written ; it ++ )
113 if( isspace( hd_serial -> data[ it ] ) )
115 hd_serial -> data[ it ] =
'-' ;
122 char *curdir = getcwd( NULL , 0 );
123 N_STR *hostid = NULL ;
125 n_popen(
"hostid" , 1024 , (
void **)&hostid , &ret );
141 N_STR *cpu_id = NULL ;
145 unsigned int a[ 4 ] = { 0 , 0 , 0 , 0 };
147 if( __get_cpuid_max( 0 , NULL ) >= 1 )
149 __get_cpuid( 1 , &a[ 0 ] , &a[ 1 ] , &a[ 2 ] , &a[ 3 ] );
157 memset( &binaryNum , 0 ,
sizeof( binaryNum ) );
164 binaryNum[i] = n % 2;
173 binaryNum[i] = n % 2;
182 binaryNum[i] = n % 2;
191 binaryNum[i] = n % 2;
204 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] ,
205 binaryNum[7],binaryNum[6],binaryNum[5],binaryNum[4] ,
206 binaryNum[11],binaryNum[10],binaryNum[9],binaryNum[8] ,
207 binaryNum[13],binaryNum[12] );
210 static const char devname[] =
"/dev/cpu/self/cpuid";
213 uint32_t r_eax, r_ebx, r_ecx, r_edx;
218 if ((d = open(devname, O_RDONLY)) != -1)
220 if (pread(d, rp,
sizeof (*rp), 0) ==
sizeof (*rp))
222 s = (
char *)&rp->r_ebx;
260 N_STR *input = params -> input ;
261 N_STR *dest = params -> dest ;
262 N_STR *key = params -> key ;
269 bool encipher = params -> encipher ;
271 size_t start_pos = params -> start_pos ;
272 size_t end_pos = params -> end_pos ;
275 uint8_t keyIndexMod = 0;
278 for(
size_t i = start_pos ; i < end_pos ; i++ )
280 if( ( cIsUpper =
n_isupper( input -> data[ i ] ) ) || (
n_islower( input -> data[ i ] ) ) )
282 char offset = cIsUpper ?
'A' :
'a';
283 keyIndexMod= i % key -> written ;
284 int k = ( cIsUpper ?
n_toupper( key -> data[ keyIndexMod ] ) :
n_tolower( key -> data[ keyIndexMod ] ) ) - offset ;
285 dest -> data[ i ] = (char)( (
n_mathmod( ( ( input -> data[ i ] + k ) - offset ) , 26 ) ) + offset );
291 for( uint32_t i = start_pos ; i < end_pos ; i++ )
293 if( ( cIsUpper =
n_isupper( input -> data[ i ] ) ) || (
n_islower( input -> data[ i ] ) ) )
295 char offset = cIsUpper ?
'A' :
'a';
296 keyIndexMod= i % key -> written ;
297 int k = ( cIsUpper ?
n_toupper( key -> data[ keyIndexMod ] ) :
n_tolower( key -> data[ keyIndexMod ] ) ) - offset ;
298 dest -> data[ i ] = (char)( (
n_mathmod( ( ( input -> data[ i ] - k ) - offset ) , 26 ) ) + offset );
324 if( input -> written == 0 )
330 if( ( key -> written % 2 ) != 0 )
332 n_log(
LOG_ERR ,
"odd number of elements in root key %s, %d" ,
_nstr( key ) , key -> written );
336 for(
size_t i = 0 ; i < key -> written ; ++i )
338 if( !isalpha( key -> data[ i ] ) )
340 n_log(
LOG_ERR ,
"key contain bad char '%c'" , key -> data[ i ] );
357 GetSystemInfo(&sysinfo);
358 int nb_procs = sysinfo.dwNumberOfProcessors;
360 int nb_procs = get_nprocs();
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 )
375 end_pos = it + bytesteps ;
377 if( end_pos >= input -> written )
378 end_pos = input -> written ;
382 params -> input = input ;
383 params -> dest = dest ;
384 params -> key = key ;
385 params -> encipher = encipher ;
386 params -> start_pos = it ;
387 params -> end_pos = end_pos ;
391 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool" );
397 if( end_pos < input -> written )
401 params -> input = input ;
402 params -> dest = dest ;
403 params -> key = key ;
404 params -> encipher = encipher ;
405 params -> start_pos = end_pos ;
406 params -> end_pos = input -> written ;
410 n_log(
LOG_ERR,
"Error adding n_vigenere_cypher_thread to thread pool" );
509 return encoded_data ;
543 if( ( question_size % 2 ) != 0 )
545 n_log(
LOG_ERR ,
"odd number of elements for question size, %d" , question_size );
549 N_STR *output = NULL ;
560 output =
new_nstr( question_size + 1 );
561 for( it = 0 ; it < question_size ; it ++ )
563 output -> data[ it ] = tmpoutput -> data[ it % tmpoutput -> written ] ;
564 while( output -> data[ it ] <
'A' ) output -> data[ it ] += 26 ;
565 while( output -> data[ it ] >
'Z' ) output -> data[ it ] -= 26 ;
567 output -> written = question_size ;
573 output =
new_nstr( question_size + 1 );
574 for( it = 0 ; it < question_size ; it ++ )
576 output -> data[ it ] = rand()%255 ;
577 while( output -> data[ it ] <
'A' ) output -> data[ it ] += 26 ;
578 while( output -> data[ it ] >
'Z' ) output -> data[ it ] -= 26 ;
586 while( it < encoded_data -> written )
588 if( encoded_data -> data[ it ] ==
'=' )
590 encoded_data -> data[ it ] =
'\0' ;
594 return encoded_data ;
612 if( root_key -> written < decoded_question -> written )
614 n_log(
LOG_ERR ,
"root key of size %d is lower than question key of size %d" , root_key -> written , decoded_question -> written );
618 if( (root_key -> written % decoded_question -> written) != 0 )
620 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 );
626 for( uint32_t it = 0 ; it < root_key -> written ; it ++ )
628 int32_t val = ( root_key -> data[ it % root_key -> written ] -
'A' ) + ( decoded_question -> data[ it % decoded_question -> written ] -
'A' );
631 answer -> data[ it ] = val ;
634 answer -> written = root_key -> written ;
654 if( ( key -> written % 2 ) != 0 )
656 n_log(
LOG_ERR ,
"odd number of elements in key %s, %d" ,
_nstr( key ) , key -> written );
704 if( ( key -> written % 2 ) != 0 )
706 n_log(
LOG_ERR ,
"odd number of elements in key %s , %d" ,
_nstr( key ) , key -> written );
756 n_log(
LOG_ERR ,
"could not base64 decode: input_data %p" , input_data );
761 if( !encoded_with_answer_data )
763 n_log(
LOG_ERR ,
"could not encode: input_data %p" , input_data );
772 n_log(
LOG_ERR ,
"could not decode: input_data %p" , input_data );
776 return encoded_data ;
791 __n_assert( input_data -> data ,
return NULL );
803 n_log(
LOG_ERR ,
"could not encode input_data %p" , input_data );
806 n_log(
LOG_DEBUG ,
"encoded_data: %d/%d" , encoded_data -> written , encoded_data -> length );
809 if( !encoded_with_answer_data )
811 n_log(
LOG_ERR ,
"could not decode: input_data %p" , input_data );
814 n_log(
LOG_DEBUG ,
"encoded_with_answer_data: %d/%d" , encoded_with_answer_data -> written , encoded_with_answer_data -> length );
820 n_log(
LOG_ERR ,
"could not base64 decode: input_data %p" , input_data );
823 n_log(
LOG_DEBUG ,
"b64_decoded: %d/%d" , b64_decoded -> written , b64_decoded -> length );
#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
int n_popen(char *cmd, int read_buf_size, void **nstr_output, int *ret)
launch a command abd return output and status
#define Free(__ptr)
Free Handler to get errors.
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
FORCE_INLINE char n_tolower(char c)
is_alpha
N_STR * n_base64_decode(N_STR *bufcoded)
decode a N_STR *string
FORCE_INLINE bool n_isupper(char c)
test if char c is uppercase
FORCE_INLINE char n_toupper(char c)
is_alpha
FORCE_INLINE 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
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
#define nstrprintf(__nstr_var,...)
Macro to quickly allocate and sprintf to N_STR *.
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.
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_cat(__nstr_var,...)
Macro to quickly allocate and sprintf and cat to a 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.
int destroy_threaded_pool(THREAD_POOL **pool, int delay)
delete a thread_pool, exit the threads and free the structs
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
#define DIRECT_PROC
processing mode for added func, direct start
int wait_for_threaded_pool(THREAD_POOL *thread_pool, int delay)
Wait for all the launched process in the thread pool to terminate.
THREAD_POOL * new_thread_pool(int nbmaxthr, int nb_max_waiting)
Create a new pool of nbmaxthr threads.
Structure of a trhead pool.
base64 encoding and decoding functions using N_STR
Common headers and low-level hugly 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