implemented why2_malloc & why2_calloc & why2_free

This commit is contained in:
Václav Šmejkal 2023-01-29 20:39:52 +01:00
parent e075c022fa
commit 6cce28c65a
Signed by: ENGO150
GPG Key ID: 4A57E86482968843
8 changed files with 55 additions and 48 deletions

View File

@ -24,6 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <sys/time.h> #include <sys/time.h>
#include <why2/flags.h> #include <why2/flags.h>
#include <why2/memory.h>
#include <why2/misc.h> #include <why2/misc.h>
why2_output_flags why2_decrypt_text(char *text, char *keyNew) why2_output_flags why2_decrypt_text(char *text, char *keyNew)
@ -61,7 +62,7 @@ why2_output_flags why2_decrypt_text(char *text, char *keyNew)
char *returningText; char *returningText;
int numberBuffer = 1; int numberBuffer = 1;
int usedTextDeallocationBuffer = 0; int usedTextDeallocationBuffer = 0;
char *textBuffer = malloc(1); char *textBuffer = why2_malloc(1);
int textKeyChainLength; int textKeyChainLength;
int *textKeyChain; int *textKeyChain;
char *key = strdup(keyNew); //COPY keyNew TO key char *key = strdup(keyNew); //COPY keyNew TO key
@ -75,9 +76,9 @@ why2_output_flags why2_decrypt_text(char *text, char *keyNew)
} }
//SET LENGTH (numberBuffer) //SET LENGTH (numberBuffer)
returningText = calloc(numberBuffer + 1, sizeof(char)); returningText = why2_calloc(numberBuffer + 1, sizeof(char));
textKeyChain = malloc(sizeof(int) * numberBuffer); textKeyChain = why2_malloc(sizeof(int) * numberBuffer);
encryptedTextKeyChain = malloc(sizeof(int) * numberBuffer); encryptedTextKeyChain = why2_malloc(sizeof(int) * numberBuffer);
textKeyChainLength = numberBuffer; textKeyChainLength = numberBuffer;
//LOAD textKeyChain //LOAD textKeyChain
@ -145,10 +146,10 @@ why2_output_flags why2_decrypt_text(char *text, char *keyNew)
}; };
//DEALLOCATION //DEALLOCATION
free(textKeyChain); why2_free(textKeyChain);
free(encryptedTextKeyChain); why2_free(encryptedTextKeyChain);
free(textBuffer); why2_free(textBuffer);
free(usedText - usedTextDeallocationBuffer); why2_free(usedText - usedTextDeallocationBuffer);
return output; return output;
} }

View File

@ -24,6 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <sys/time.h> #include <sys/time.h>
#include <why2/flags.h> #include <why2/flags.h>
#include <why2/memory.h>
#include <why2/misc.h> #include <why2/misc.h>
why2_output_flags why2_encrypt_text(char *text, char *keyNew) why2_output_flags why2_encrypt_text(char *text, char *keyNew)
@ -51,8 +52,8 @@ why2_output_flags why2_encrypt_text(char *text, char *keyNew)
//VARIABLES //VARIABLES
char *key = NULL; char *key = NULL;
char *returningText; char *returningText;
char *textBuffer = malloc(1); char *textBuffer = why2_malloc(1);
int *textKeyChain = malloc(sizeof(int) * strlen(text)); int *textKeyChain = why2_malloc(sizeof(int) * strlen(text));
int numberBuffer = 0; int numberBuffer = 0;
if (keyNew != NULL) if (keyNew != NULL)
@ -69,7 +70,7 @@ why2_output_flags why2_encrypt_text(char *text, char *keyNew)
why2_set_key_length(strlen(key)); why2_set_key_length(strlen(key));
} else //LOAD KEY } else //LOAD KEY
{ {
key = malloc(why2_get_key_length() + 1); key = why2_malloc(why2_get_key_length() + 1);
why2_generate_key(key, why2_get_key_length()); why2_generate_key(key, why2_get_key_length());
} }
@ -90,7 +91,7 @@ why2_output_flags why2_encrypt_text(char *text, char *keyNew)
} }
//ALLOCATE returningText (WITH THE SEPARATORS) //ALLOCATE returningText (WITH THE SEPARATORS)
returningText = calloc(numberBuffer + strlen(text), sizeof(char)); returningText = why2_calloc(numberBuffer + strlen(text), sizeof(char));
//LOAD returningText //LOAD returningText
for (int i = 0; i < (int) strlen(text); i++) for (int i = 0; i < (int) strlen(text); i++)
@ -127,8 +128,8 @@ why2_output_flags why2_encrypt_text(char *text, char *keyNew)
}; };
//DEALLOCATION //DEALLOCATION
free(textKeyChain); why2_free(textKeyChain);
free(textBuffer); why2_free(textBuffer);
return output; return output;
} }

View File

@ -20,6 +20,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdlib.h> #include <stdlib.h>
#include <why2/memory.h>
//CONSTS (this is just local) //CONSTS (this is just local)
#define DEFAULT_FLAGS (why2_input_flags) { 0, 0, 0 } #define DEFAULT_FLAGS (why2_input_flags) { 0, 0, 0 }
@ -55,10 +57,10 @@ why2_input_flags why2_get_flags(void)
why2_output_flags why2_no_output(enum WHY2_EXIT_CODES exitCode) why2_output_flags why2_no_output(enum WHY2_EXIT_CODES exitCode)
{ {
char *emptyText = malloc(1); //TEXT char *emptyText = why2_malloc(1); //TEXT
emptyText[0] = '\0'; emptyText[0] = '\0';
char *emptyKey = malloc(why2_get_key_length() + 1); //KEY char *emptyKey = why2_malloc(why2_get_key_length() + 1); //KEY
for (int i = 0; i < (int) why2_get_key_length(); i++) for (int i = 0; i < (int) why2_get_key_length(); i++)
{ {
emptyKey[i] = 'x'; emptyKey[i] = 'x';

View File

@ -33,7 +33,7 @@ int main(void)
char *textBuffer; char *textBuffer;
char *keyBuffer; char *keyBuffer;
char *statusBuffer; char *statusBuffer;
char *outputBuffer = malloc(1); //THIS IS TEMP ALLOCATION char *outputBuffer = why2_malloc(1); //THIS IS TEMP ALLOCATION
int exitCode = 0; int exitCode = 0;
unsigned long timeBuffer; unsigned long timeBuffer;
FILE *outputStreamBuffer = stdout; FILE *outputStreamBuffer = stdout;
@ -111,10 +111,10 @@ int main(void)
); );
//DEALLOCATION //DEALLOCATION
free(textBuffer); why2_free(textBuffer);
free(keyBuffer); why2_free(keyBuffer);
free(statusBuffer); why2_free(statusBuffer);
free(outputBuffer); why2_free(outputBuffer);
why2_deallocate_output(encrypted); why2_deallocate_output(encrypted);
return exitCode; return exitCode;

View File

@ -30,6 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <git2.h> #include <git2.h>
#include <why2/flags.h> #include <why2/flags.h>
#include <why2/memory.h>
why2_bool seedSet = 0; //DO NOT FUCKING TOUCH THIS!!! why2_bool seedSet = 0; //DO NOT FUCKING TOUCH THIS!!!
@ -68,7 +69,7 @@ char *replaceWord(char *string, char *old, char *new) //CODE FROM: https://www.g
} }
} }
result = (char*) malloc(i + cnt * (newLen - oldLen) + 1); result = (char*) why2_malloc(i + cnt * (newLen - oldLen) + 1);
i = 0; i = 0;
while (*string) while (*string)
@ -137,7 +138,7 @@ enum WHY2_EXIT_CODES why2_check_version(void)
rewind(fileBuffer); //REWIND fileBuffer (NO SHIT) rewind(fileBuffer); //REWIND fileBuffer (NO SHIT)
//SET LENGTH OF buffer //SET LENGTH OF buffer
buffer = calloc(bufferSize + 1, sizeof(char)); buffer = why2_calloc(bufferSize + 1, sizeof(char));
//LOAD jsonFile //LOAD jsonFile
(void) (fread(buffer, bufferSize, 1, fileBuffer) + 1); //TODO: Try to create some function for processing exit value (void) (fread(buffer, bufferSize, 1, fileBuffer) + 1); //TODO: Try to create some function for processing exit value
@ -152,7 +153,7 @@ enum WHY2_EXIT_CODES why2_check_version(void)
//WAIT FOR 5 SECONDS //WAIT FOR 5 SECONDS
sleep(5); sleep(5);
free(buffer); why2_free(buffer);
fclose(fileBuffer); fclose(fileBuffer);
return WHY2_SUCCESS; return WHY2_SUCCESS;
} }
@ -212,7 +213,7 @@ enum WHY2_EXIT_CODES why2_check_version(void)
//REMOVE versions.json - OTHERWISE WILL CAUSE SEGFAULT IN NEXT RUN //REMOVE versions.json - OTHERWISE WILL CAUSE SEGFAULT IN NEXT RUN
remove(WHY2_VERSIONS_NAME); remove(WHY2_VERSIONS_NAME);
free(installCommand); why2_free(installCommand);
//CHECK FOR ERRORS //CHECK FOR ERRORS
if (installCode != 0) if (installCode != 0)
@ -264,7 +265,7 @@ enum WHY2_EXIT_CODES why2_check_version(void)
//DEALLOCATION //DEALLOCATION
json_object_put(parsedJson); //THIS FREES EVERY json_object - AT LEAST JSON-C'S DOCUMENTATION SAYS THAT json_object_put(parsedJson); //THIS FREES EVERY json_object - AT LEAST JSON-C'S DOCUMENTATION SAYS THAT
free(buffer); why2_free(buffer);
return WHY2_SUCCESS; return WHY2_SUCCESS;
} }
@ -307,8 +308,8 @@ void why2_generate_text_key_chain(char *key, int *textKeyChain, int textKeyChain
void why2_deallocate_output(why2_output_flags flags) void why2_deallocate_output(why2_output_flags flags)
{ {
free(flags.outputText); why2_free(flags.outputText);
free(flags.usedKey); why2_free(flags.usedKey);
flags.elapsedTime = 0; flags.elapsedTime = 0;
flags.exitCode = WHY2_SUCCESS; flags.exitCode = WHY2_SUCCESS;

View File

@ -30,6 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/logger/flags.h> #include <why2/logger/flags.h>
#include <why2/encrypter.h> #include <why2/encrypter.h>
#include <why2/memory.h>
#include <why2/misc.h> #include <why2/misc.h>
why2_log_file why2_init_logger(char *directoryPath) why2_log_file why2_init_logger(char *directoryPath)
@ -41,9 +42,9 @@ why2_log_file why2_init_logger(char *directoryPath)
struct tm tm = *localtime(&timeL); struct tm tm = *localtime(&timeL);
int buffer = 1; int buffer = 1;
int file; int file;
char *filePath = malloc(strlen(directoryPath) + 1 + strlen(WHY2_LOG_FORMAT) + 1); char *filePath = why2_malloc(strlen(directoryPath) + 1 + strlen(WHY2_LOG_FORMAT) + 1);
char *dateBuffer = malloc(strlen(WHY2_LOG_FORMAT_START) + 1); char *dateBuffer = why2_malloc(strlen(WHY2_LOG_FORMAT_START) + 1);
char *latestBuffer = malloc(strlen(WHY2_WRITE_DIR) + strlen(WHY2_LOG_LATEST) + 2); char *latestBuffer = why2_malloc(strlen(WHY2_WRITE_DIR) + strlen(WHY2_LOG_LATEST) + 2);
char *latestFilePath = NULL; char *latestFilePath = NULL;
DIR *dir; DIR *dir;
@ -87,9 +88,9 @@ why2_log_file why2_init_logger(char *directoryPath)
deallocation: deallocation:
//DEALLOCATION //DEALLOCATION
free(dateBuffer); why2_free(dateBuffer);
free(latestBuffer); why2_free(latestBuffer);
free(latestFilePath); why2_free(latestFilePath);
closedir(dir); closedir(dir);
return (why2_log_file) return (why2_log_file)
@ -132,19 +133,19 @@ void why2_write_log(int loggerFile, char *logMessage)
//DEALLOCATION //DEALLOCATION
why2_deallocate_output(encrypted); why2_deallocate_output(encrypted);
free(logMessageUsed); //I COULD DO THIS SMART SOMEHOW, BUT I AM TOO LAZY FOR THAT SHIT why2_free(logMessageUsed); //I COULD DO THIS SMART SOMEHOW, BUT I AM TOO LAZY FOR THAT SHIT
} else //FUCK ENCRYPTION, LET'S DO IT; WHY WOULD WE EVEN USE WHY2-CORE? HUH? } else //FUCK ENCRYPTION, LET'S DO IT; WHY WOULD WE EVEN USE WHY2-CORE? HUH?
{ {
message = logMessageUsed; message = logMessageUsed;
} }
buffer = malloc(strlen(WHY2_WRITE_FORMAT) + strlen(message) + 2); //ALLOCATE buffer = why2_malloc(strlen(WHY2_WRITE_FORMAT) + strlen(message) + 2); //ALLOCATE
sprintf(buffer, WHY2_WRITE_FORMATTING, tm.tm_hour, tm.tm_min, tm.tm_sec, message); //LOAD MESSAGE sprintf(buffer, WHY2_WRITE_FORMATTING, tm.tm_hour, tm.tm_min, tm.tm_sec, message); //LOAD MESSAGE
(void) (write(loggerFile, buffer, strlen(buffer)) + 1); //TODO: Try to create some function for processing exit value (void) (write(loggerFile, buffer, strlen(buffer)) + 1); //TODO: Try to create some function for processing exit value
//DEALLOCATION //DEALLOCATION
free(buffer); why2_free(buffer);
free(message); why2_free(message);
} }

View File

@ -26,7 +26,7 @@ int main(void)
{ {
//VARIABLES //VARIABLES
why2_log_file logger = why2_init_logger(WHY2_WRITE_DIR); //INITIALIZE LOGGER FILE why2_log_file logger = why2_init_logger(WHY2_WRITE_DIR); //INITIALIZE LOGGER FILE
char *usedKey = malloc(why2_get_key_length() + 1); char *usedKey = why2_malloc(why2_get_key_length() + 1);
why2_decrypted_output decrypted; why2_decrypted_output decrypted;
int exitCode = 0; int exitCode = 0;
@ -74,7 +74,7 @@ int main(void)
} }
//DEALLOCATION //DEALLOCATION
free(usedKey); why2_free(usedKey);
why2_deallocate_logger(logger); why2_deallocate_logger(logger);
why2_deallocate_decrypted_output(decrypted); why2_deallocate_decrypted_output(decrypted);

View File

@ -25,6 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <why2/decrypter.h> #include <why2/decrypter.h>
#include <why2/flags.h> #include <why2/flags.h>
#include <why2/memory.h>
#include <why2/misc.h> #include <why2/misc.h>
#include <why2/logger/flags.h> #include <why2/logger/flags.h>
@ -32,7 +33,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
void why2_deallocate_logger(why2_log_file logger) void why2_deallocate_logger(why2_log_file logger)
{ {
close(logger.file); close(logger.file);
free(logger.fileName); why2_free(logger.fileName);
logger.fileName = NULL; logger.fileName = NULL;
logger.file = INVALID_FILE; logger.file = INVALID_FILE;
@ -42,11 +43,11 @@ void why2_deallocate_decrypted_output(why2_decrypted_output output)
{ {
for (unsigned long i = 0; i < output.length; i++) for (unsigned long i = 0; i < output.length; i++)
{ {
free(output.decryptedText[i]); why2_free(output.decryptedText[i]);
} }
output.length = 0; output.length = 0;
free(output.decryptedText); why2_free(output.decryptedText);
} }
why2_decrypted_output why2_decrypt_logger(why2_log_file logger) why2_decrypted_output why2_decrypt_logger(why2_log_file logger)
@ -67,7 +68,7 @@ why2_decrypted_output why2_decrypt_logger(why2_log_file logger)
rewind(file); //REWIND file (NO SHIT) rewind(file); //REWIND file (NO SHIT)
//ALLOCATE rawContent //ALLOCATE rawContent
rawContent = calloc(rawContentL + 1, sizeof(char)); //CALLOC WILL BE USED FOR CLEANING AFTER ALLOCATION rawContent = why2_calloc(rawContentL + 1, sizeof(char)); //CALLOC WILL BE USED FOR CLEANING AFTER ALLOCATION
//LOAD rawContent //LOAD rawContent
(void) (fread(rawContent, rawContentL, 1, file) + 1); //TODO: Try to create some function for processing exit value (void) (fread(rawContent, rawContentL, 1, file) + 1); //TODO: Try to create some function for processing exit value
@ -78,14 +79,14 @@ why2_decrypted_output why2_decrypt_logger(why2_log_file logger)
} }
//ALLOCATE content & contentDecrypted //ALLOCATE content & contentDecrypted
content = calloc(lines + 1, sizeof(char*)); content = why2_calloc(lines + 1, sizeof(char*));
contentDecrypted = calloc(lines + 1, sizeof(char*)); contentDecrypted = why2_calloc(lines + 1, sizeof(char*));
for (int i = 0; i < rawContentL; i++) //LOAD rawContent AND SPLIT TO content for (int i = 0; i < rawContentL; i++) //LOAD rawContent AND SPLIT TO content
{ {
if (rawContent[i] == '\n') //END OF ONE LINE if (rawContent[i] == '\n') //END OF ONE LINE
{ {
content[contentIndexBuffer] = calloc((i - (startingAtBuffer + strlen(WHY2_WRITE_FORMAT)) + 2), sizeof(char)); //ALLOCATE ELEMENT content[contentIndexBuffer] = why2_calloc((i - (startingAtBuffer + strlen(WHY2_WRITE_FORMAT)) + 2), sizeof(char)); //ALLOCATE ELEMENT
for (int j = startingAtBuffer + strlen(WHY2_WRITE_FORMAT); j <= i; j++) //LOAD CONTENT OF EACH LINE for (int j = startingAtBuffer + strlen(WHY2_WRITE_FORMAT); j <= i; j++) //LOAD CONTENT OF EACH LINE
{ {
@ -107,7 +108,7 @@ why2_decrypted_output why2_decrypt_logger(why2_log_file logger)
} }
//DEALLOCATION //DEALLOCATION
free(rawContent); why2_free(rawContent);
fclose(file); fclose(file);
why2_deallocate_decrypted_output((why2_decrypted_output) { content, lines }); //fuck the system lmao why2_deallocate_decrypted_output((why2_decrypted_output) { content, lines }); //fuck the system lmao