WHY2/src/lib/misc.c

357 lines
8.7 KiB
C
Raw Normal View History

#define _XOPEN_SOURCE 500
2022-05-01 16:23:07 +02:00
#include <why2/misc.h>
2022-04-22 18:39:48 +02:00
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <ftw.h>
2022-04-22 18:39:48 +02:00
#include <curl/curl.h>
#include <json-c/json.h>
#include <git2.h>
2022-04-22 18:39:48 +02:00
2022-05-01 16:23:07 +02:00
#include <why2/flags.h>
2022-04-22 18:39:48 +02:00
double multiply_cb(int a, int b) { return a * b; }
double subtract_cb(int a, int b) { return a - b; }
double sum_cb(int a, int b) { return a + b; }
double doMathematicalOperation(int a, int b, double (*op)(int, int))
{
return op(a, b);
}
int unlink_cb(const char *fpath, UNUSED const struct stat *sb, UNUSED int typeflag, UNUSED struct FTW *ftwbuf)
{
int rv = remove(fpath);
if (rv) perror(fpath);
return rv;
}
int removeDirectory(char *path)
{
return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
}
char *replaceWord(char *string, char *old, char *new) //CODE FROM: https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word
{
char *result;
int i, cnt = 0;
int newLen = strlen(new);
int oldLen = strlen(old);
for (i = 0; string[i] != '\0'; i++)
{
if (strstr(&string[i], old) == &string[i])
{
cnt++;
i += oldLen - 1;
}
}
result = (char*) malloc(i + cnt * (newLen - oldLen) + 1);
i = 0;
while (*string)
{
// compare the substring with the result
if (strstr(string, old) == string)
{
strcpy(&result[i], new);
i += newLen;
string += oldLen;
}
else result[i++] = *string++;
}
result[i] = '\0';
return result;
}
2022-07-22 18:22:42 +02:00
_Bool checkVersion()
2022-04-22 18:39:48 +02:00
{
2022-07-10 18:26:41 +02:00
if (getFlags().noCheck) return SUCCESS;
//FILE-CHECK VARIABLES
int notFoundBuffer = 0;
2022-04-22 18:39:48 +02:00
//CURL VARIABLES
CURL *curl = curl_easy_init();
2022-07-24 16:43:03 +02:00
FILE *fileBuffer = fopen(VERSIONS_NAME, "w+");
2022-04-22 18:39:48 +02:00
//GET versions.json
curl_easy_setopt(curl, CURLOPT_URL, VERSIONS_URL);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fileBuffer);
//DOWNLOAD versions.json
curl_easy_perform(curl);
//CLEANUP
curl_easy_cleanup(curl);
fclose(fileBuffer);
while (access(VERSIONS_NAME, R_OK) != 0)
{
notFoundBuffer++;
if (notFoundBuffer == NOT_FOUND_TRIES)
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "%s'%s' not found! Exiting...\n", CLEAR_SCREEN, VERSIONS_NAME);
return DOWNLOAD_FAILED;
}
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) printf("%s'%s' not found (%dx)! Trying again in a second.\n", CLEAR_SCREEN, VERSIONS_NAME, notFoundBuffer);
sleep(1);
}
2022-04-22 18:39:48 +02:00
//JSON VARIABLES
char *buffer;
2022-04-22 18:39:48 +02:00
struct json_object *parsedJson;
struct json_object *active;
int bufferSize;
2022-04-22 18:39:48 +02:00
//COUNT LENGTH OF buffer AND STORE IT IN bufferSize
fileBuffer = fopen(VERSIONS_NAME, "r");
fseek(fileBuffer, 0, SEEK_END);
bufferSize = ftell(fileBuffer);
rewind(fileBuffer); //REWIND fileBuffer (NO SHIT)
//SET LENGTH OF buffer
buffer = malloc(bufferSize + 1);
//FIX buffer
strcpy(buffer, "");
2022-04-22 18:39:48 +02:00
//LOAD jsonFile
fread(buffer, bufferSize, 1, fileBuffer);
2022-04-22 18:39:48 +02:00
//CHECK FOR TEXT IN buffer
if (strcmp(buffer, "") == 0)
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "You probably aren't connected to internet! This release could be unsafe!\n\n");
//WAIT FOR 5 SECONDS
sleep(5);
}
2022-04-22 18:39:48 +02:00
//CLEANUP
fclose(fileBuffer);
//GET
parsedJson = json_tokener_parse(buffer);
json_object_object_get_ex(parsedJson, "active", &active);
if (strcmp(VERSION, json_object_get_string(active)) != 0)
{
//UPDATE
2022-07-10 18:26:41 +02:00
if (getFlags().update)
{
//CHECK FOR ROOT PERMISSIONS
if (getuid() != 0)
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "You need to be root to update!\t[I DO NOT RECOMMEND USING THIS]\n");
return UPDATE_FAILED;
}
//VARIABLES
git_repository *repo = NULL;
int exitCode;
char *installCommand;
int installCode;
//MESSAGE
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) printf("Your WHY2 version is outdated!\nUpdating...\t[BETA]\n\n");
//CHECK IF WHY2 REPO ISN'T ALREADY FOUND IN 'UPDATE_NAME'
if (access(UPDATE_NAME, F_OK) == 0)
{
removeDirectory(UPDATE_NAME);
}
git_libgit2_init(); //START GIT2
exitCode = git_clone(&repo, UPDATE_URL, UPDATE_NAME, NULL); //CLONE
git_libgit2_shutdown(); //STOP GIT2
//CHECK FOR ERRORS
if (exitCode != 0)
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "Updating failed! (cloning)\n");
return UPDATE_FAILED;
}
//COUNT installCommand LENGTH & ALLOCATE IT
installCommand = replaceWord(UPDATE_COMMAND, "{DIR}", UPDATE_NAME);
installCode = system(installCommand); //INSTALL
//REMOVE versions.json - OTHERWISE WILL CAUSE SEGFAULT IN NEXT RUN
remove(VERSIONS_NAME);
free(installCommand);
//CHECK FOR ERRORS
if (installCode != 0)
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "Updating failed! (installing)\n");
return UPDATE_FAILED;
}
goto deallocation; //GREAT SUCCESS!
}
//COUNT VERSIONS BEHIND
int versionsIndex = -1;
int versionsBuffer = 0;
struct json_object *deprecated;
json_object_object_get_ex(parsedJson, "deprecated", &deprecated);
//COUNT versionsIndex
for (int i = 0; i < (int) json_object_array_length(deprecated); i++)
{
//IT'S A MATCH, BABY :D
if (strcmp(json_object_get_string(json_object_array_get_idx(deprecated, i)), VERSION) == 0)
{
versionsIndex = i;
break;
}
}
//versions.json DOESN'T CONTAIN VERSION (THIS WILL NOT HAPPEN IF YOU WILL NOT EDIT IT)
if (versionsIndex == -1)
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) printf("Version %s not found! Check your flags.\n\n", VERSION);
2022-05-26 19:50:58 +02:00
free(deprecated);
goto deallocation;
}
//COUNT versionsBuffer
versionsBuffer = json_object_array_length(deprecated) - versionsIndex;
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "This release could be unsafe! You're %d versions behind! (%s/%s)\n\n", versionsBuffer, VERSION, json_object_get_string(active));
2022-04-22 18:39:48 +02:00
//WAIT FOR 5 SECONDS
free(deprecated);
2022-04-22 18:39:48 +02:00
sleep(5);
}
deallocation:
//DEALLOCATION
free(parsedJson);
free(active);
free(buffer);
return SUCCESS;
2022-04-22 18:39:48 +02:00
}
void generateTextKeyChain(char *key, int *textKeyChain, int textKeyChainSize)
{
int numberBuffer;
int numberBuffer2;
double (*cb)(int, int);
for (int i = 0; i < textKeyChainSize; i++)
{
numberBuffer = i;
2022-05-03 18:37:28 +02:00
//CHECK, IF numberBuffer ISN'T GREATER THAN keyLength AND CUT UNUSED LENGTH
while (numberBuffer >= (int) getKeyLength())
{
2022-05-03 18:37:28 +02:00
numberBuffer -= getKeyLength();
}
numberBuffer2 = getKeyLength() - (numberBuffer + (i < textKeyChainSize));
//FILL textKeyChain
if ((numberBuffer + 1) % 3 == 0)
{
cb = multiply_cb;
2022-06-10 19:48:45 +02:00
}
else if ((numberBuffer + 1) % 2 == 0)
{
cb = subtract_cb;
2022-06-10 19:48:45 +02:00
}
else
{
cb = sum_cb;
}
textKeyChain[textKeyChainSize - (i + 1)] = doMathematicalOperation(key[numberBuffer], key[numberBuffer2], cb);
}
}
void deallocateOutput(outputFlags flags)
{
free(flags.outputText);
free(flags.usedKey);
}
2022-07-22 18:22:42 +02:00
_Bool checkKey(char *key)
{
if (strlen(key) < getKeyLength())
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "Key must be at least %lu characters long!\n", getKeyLength());
return INVALID_KEY;
}
return SUCCESS;
}
2022-07-22 18:22:42 +02:00
_Bool checkText(char *text)
{
if (strcmp(text, "") == 0)
{
2022-07-10 18:26:41 +02:00
if (!getFlags().noOutput) fprintf(stderr, "No text to encrypt!\n");
return INVALID_TEXT;
}
return SUCCESS;
}
unsigned long countIntLength(int number)
{
int returning = 1;
int buffer = 10;
//CHECK FOR NEGATIVE NUMBER
if (number < 0)
{
returning++;
number *= -1;
}
//COUNT LENGTH
while (buffer <= number)
{
buffer *= 10;
returning++;
}
return returning;
}
unsigned long countUnusedKeySize(char *text, char *key)
{
unsigned long returning = 0;
if (strlen(key) / 2 > strlen(text))
{
returning = strlen(key) - 2 * strlen(text);
}
return returning;
}
unsigned long compareTimeMicro(struct timeval startTime, struct timeval finishTime)
{
return (finishTime.tv_sec - startTime.tv_sec) * 1000000 + finishTime.tv_usec - startTime.tv_usec;
}