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 <why2/flags.h>
#include <why2/memory.h>
#include <why2/misc.h>
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;
int numberBuffer = 1;
int usedTextDeallocationBuffer = 0;
char *textBuffer = malloc(1);
char *textBuffer = why2_malloc(1);
int textKeyChainLength;
int *textKeyChain;
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)
returningText = calloc(numberBuffer + 1, sizeof(char));
textKeyChain = malloc(sizeof(int) * numberBuffer);
encryptedTextKeyChain = malloc(sizeof(int) * numberBuffer);
returningText = why2_calloc(numberBuffer + 1, sizeof(char));
textKeyChain = why2_malloc(sizeof(int) * numberBuffer);
encryptedTextKeyChain = why2_malloc(sizeof(int) * numberBuffer);
textKeyChainLength = numberBuffer;
//LOAD textKeyChain
@ -145,10 +146,10 @@ why2_output_flags why2_decrypt_text(char *text, char *keyNew)
};
//DEALLOCATION
free(textKeyChain);
free(encryptedTextKeyChain);
free(textBuffer);
free(usedText - usedTextDeallocationBuffer);
why2_free(textKeyChain);
why2_free(encryptedTextKeyChain);
why2_free(textBuffer);
why2_free(usedText - usedTextDeallocationBuffer);
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 <why2/flags.h>
#include <why2/memory.h>
#include <why2/misc.h>
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
char *key = NULL;
char *returningText;
char *textBuffer = malloc(1);
int *textKeyChain = malloc(sizeof(int) * strlen(text));
char *textBuffer = why2_malloc(1);
int *textKeyChain = why2_malloc(sizeof(int) * strlen(text));
int numberBuffer = 0;
if (keyNew != NULL)
@ -69,7 +70,7 @@ why2_output_flags why2_encrypt_text(char *text, char *keyNew)
why2_set_key_length(strlen(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());
}
@ -90,7 +91,7 @@ why2_output_flags why2_encrypt_text(char *text, char *keyNew)
}
//ALLOCATE returningText (WITH THE SEPARATORS)
returningText = calloc(numberBuffer + strlen(text), sizeof(char));
returningText = why2_calloc(numberBuffer + strlen(text), sizeof(char));
//LOAD returningText
for (int i = 0; i < (int) strlen(text); i++)
@ -127,8 +128,8 @@ why2_output_flags why2_encrypt_text(char *text, char *keyNew)
};
//DEALLOCATION
free(textKeyChain);
free(textBuffer);
why2_free(textKeyChain);
why2_free(textBuffer);
return output;
}

View File

@ -20,6 +20,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdlib.h>
#include <why2/memory.h>
//CONSTS (this is just local)
#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)
{
char *emptyText = malloc(1); //TEXT
char *emptyText = why2_malloc(1); //TEXT
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++)
{
emptyKey[i] = 'x';

View File

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

View File

@ -30,6 +30,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <git2.h>
#include <why2/flags.h>
#include <why2/memory.h>
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;
while (*string)
@ -137,7 +138,7 @@ enum WHY2_EXIT_CODES why2_check_version(void)
rewind(fileBuffer); //REWIND fileBuffer (NO SHIT)
//SET LENGTH OF buffer
buffer = calloc(bufferSize + 1, sizeof(char));
buffer = why2_calloc(bufferSize + 1, sizeof(char));
//LOAD jsonFile
(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
sleep(5);
free(buffer);
why2_free(buffer);
fclose(fileBuffer);
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(WHY2_VERSIONS_NAME);
free(installCommand);
why2_free(installCommand);
//CHECK FOR ERRORS
if (installCode != 0)
@ -264,7 +265,7 @@ enum WHY2_EXIT_CODES why2_check_version(void)
//DEALLOCATION
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;
}
@ -307,8 +308,8 @@ void why2_generate_text_key_chain(char *key, int *textKeyChain, int textKeyChain
void why2_deallocate_output(why2_output_flags flags)
{
free(flags.outputText);
free(flags.usedKey);
why2_free(flags.outputText);
why2_free(flags.usedKey);
flags.elapsedTime = 0;
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/encrypter.h>
#include <why2/memory.h>
#include <why2/misc.h>
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);
int buffer = 1;
int file;
char *filePath = malloc(strlen(directoryPath) + 1 + strlen(WHY2_LOG_FORMAT) + 1);
char *dateBuffer = malloc(strlen(WHY2_LOG_FORMAT_START) + 1);
char *latestBuffer = malloc(strlen(WHY2_WRITE_DIR) + strlen(WHY2_LOG_LATEST) + 2);
char *filePath = why2_malloc(strlen(directoryPath) + 1 + strlen(WHY2_LOG_FORMAT) + 1);
char *dateBuffer = why2_malloc(strlen(WHY2_LOG_FORMAT_START) + 1);
char *latestBuffer = why2_malloc(strlen(WHY2_WRITE_DIR) + strlen(WHY2_LOG_LATEST) + 2);
char *latestFilePath = NULL;
DIR *dir;
@ -87,9 +88,9 @@ why2_log_file why2_init_logger(char *directoryPath)
deallocation:
//DEALLOCATION
free(dateBuffer);
free(latestBuffer);
free(latestFilePath);
why2_free(dateBuffer);
why2_free(latestBuffer);
why2_free(latestFilePath);
closedir(dir);
return (why2_log_file)
@ -132,19 +133,19 @@ void why2_write_log(int loggerFile, char *logMessage)
//DEALLOCATION
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?
{
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
(void) (write(loggerFile, buffer, strlen(buffer)) + 1); //TODO: Try to create some function for processing exit value
//DEALLOCATION
free(buffer);
free(message);
why2_free(buffer);
why2_free(message);
}

View File

@ -26,7 +26,7 @@ int main(void)
{
//VARIABLES
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;
int exitCode = 0;
@ -74,7 +74,7 @@ int main(void)
}
//DEALLOCATION
free(usedKey);
why2_free(usedKey);
why2_deallocate_logger(logger);
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/flags.h>
#include <why2/memory.h>
#include <why2/misc.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)
{
close(logger.file);
free(logger.fileName);
why2_free(logger.fileName);
logger.fileName = NULL;
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++)
{
free(output.decryptedText[i]);
why2_free(output.decryptedText[i]);
}
output.length = 0;
free(output.decryptedText);
why2_free(output.decryptedText);
}
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)
//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
(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
content = calloc(lines + 1, sizeof(char*));
contentDecrypted = calloc(lines + 1, sizeof(char*));
content = why2_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
{
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
{
@ -107,7 +108,7 @@ why2_decrypted_output why2_decrypt_logger(why2_log_file logger)
}
//DEALLOCATION
free(rawContent);
why2_free(rawContent);
fclose(file);
why2_deallocate_decrypted_output((why2_decrypted_output) { content, lines }); //fuck the system lmao