Initial Commit + Ready2Use Login System
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
#include "dynmem.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
// Reads a file and stores it in a buffer (automatically opens, reads and closes the file)
|
||||
//@warning "readl" only reads the first line! Possible side effects may include slow performance if done multiple times
|
||||
//@param filepath Path to the file
|
||||
//@param buffer Buffer to store the file
|
||||
//@param size Size parameter specifies the number of bytes to read
|
||||
int readl(const char *filepath, void *buffer, size_t size)
|
||||
{
|
||||
int fd;
|
||||
#ifdef _WIN32
|
||||
fd = _open(filepath, _O_RDONLY | _O_BINARY); // Open file in binary mode
|
||||
#else
|
||||
fd = open(filepath, O_RDONLY); // Files are opened in binary mode by default on Unix-like systems
|
||||
#endif
|
||||
if (fd == -1)
|
||||
{
|
||||
return fd;
|
||||
}
|
||||
|
||||
size_t bytesRead = read(fd, buffer, size);
|
||||
if (bytesRead == -1)
|
||||
{
|
||||
return bytesRead;
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
_close(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Writes a buffer to a file (automatically opens, writes and closes the file)
|
||||
//@warning "writel" only writes to the first line! Possible side effects may include slow performance if done multiple times
|
||||
//@param filepath Path to the file
|
||||
//@param buffer Buffer to write to the file
|
||||
//@param size Size parameter specifies the number of bytes to write
|
||||
void writel(const char *filepath, void *buffer, size_t size)
|
||||
{
|
||||
int fd;
|
||||
#ifdef _WIN32
|
||||
fd = _open(filepath, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, _S_IREAD | _S_IWRITE); // Open file in binary mode
|
||||
#else
|
||||
fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); // Files are opened in binary mode by default on Unix-like systems
|
||||
#endif
|
||||
if (fd == -1)
|
||||
{
|
||||
fprintf(stderr, "Failed to open file %s\n", filepath);
|
||||
return;
|
||||
}
|
||||
|
||||
size_t bytesWritten = write(fd, buffer, size);
|
||||
if (bytesWritten == -1)
|
||||
{
|
||||
fprintf(stderr, "Failed to write to file %s\n", filepath);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
_close(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif
|
||||
}
|
||||
|
||||
void emcryptText(int *output, char *password)
|
||||
{
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
output[i] = (int)password[i] * 2;
|
||||
}
|
||||
}
|
||||
|
||||
void decryptText(char *output, int *password)
|
||||
{
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
output[i] = (char)(password[i] / 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// Desenvolvido por: Afonso Sousa
|
||||
// Versão: 1.1
|
||||
// Data: 2024-11-05
|
||||
// Descrição: Biblioteca para alocação de memória dinâmica em C e leitura de ficheiros (low-level)
|
||||
#ifndef DYNMEM_H
|
||||
#define DYNMEM_H
|
||||
|
||||
// Bibliotecas para o malloc e ficheiros
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#ifdef _WIN32
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include "dynmem.c"
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
||||
Dynamic memory allocation for C
|
||||
|
||||
Uso:
|
||||
#include "dynmem.h"
|
||||
|
||||
int *vector = create(int);
|
||||
if (vector == NULL)
|
||||
{
|
||||
fprintf(stderr, "Falha na alocação de memoria para o vector\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
vector = size(vector, 1);
|
||||
add(vector, 0, 10);
|
||||
|
||||
printf("[%d]: %d\n", 0, vector[0]);
|
||||
|
||||
free(vector);
|
||||
*/
|
||||
|
||||
#define add(ptr, index, value) ptr[index] = value //@param ptr Pointer to add the value @param index Index of the value to add @param value Value to add
|
||||
#define remove(ptr, index) ptr[index] = 0 //@param ptr Pointer to remove the value @param index Index of the value to remove
|
||||
|
||||
#ifdef __INTELLISENSE__
|
||||
#define create(type) ((type *)malloc(sizeof(type))) //@param type Type of the vector @example int *vector = create(int);
|
||||
#define size(ptr, size) ((void *)realloc((ptr), sizeof(*(ptr)) * (size))) //@param ptr Pointer to extend @param size New size of the pointer
|
||||
#else
|
||||
#define create(type) ((type *)malloc(sizeof(type))) //@param type Type of the vector @example int *vector = create(int);
|
||||
#define size(ptr, size) ((__typeof__(ptr))realloc((ptr), sizeof(*(ptr)) * (size))) //@param ptr Pointer to extend @param size New size of the pointer
|
||||
#endif
|
||||
|
||||
int readl(const char *filepath, void *buffer, size_t size); //@param filepath Path to the file @param buffer Buffer to store the file @param size Size parameter specifies the number of bytes to read
|
||||
void writel(const char *filepath, void *buffer, size_t size); //@param filepath Path to the file @param buffer Buffer to write to the file @param size Size parameter specifies the number of bytes to write
|
||||
|
||||
void emcryptText(int *output, char *password); //@param output Output buffer to store the encrypted text @param password Password to encrypt the text
|
||||
|
||||
void decryptText(char *output, int *password); //@param output Output buffer to store the decrypted text @param password Password to decrypt the text
|
||||
|
||||
#endif // DYNMEM_H
|
||||
@@ -0,0 +1,108 @@
|
||||
|
||||
#ifndef HERROR_H
|
||||
#define HERROR_H
|
||||
|
||||
#include <setjmp.h>
|
||||
|
||||
// Macro to start a try block (exception handling context)
|
||||
// @warning Variables `_ex_buf` and `_ex_code` are created in the current scope. Avoid name collisions.
|
||||
// @warning Ensure every `try` is followed by `catch` and `end_try` to prevent scope issues.
|
||||
#define try \
|
||||
do \
|
||||
{ \
|
||||
jmp_buf _ex_buf; /* Buffer to store the environment for long jump */ \
|
||||
volatile int _ex_code; /* Variable to store the exception code */ \
|
||||
_ex_code = setjmp(_ex_buf); /* Save the calling environment for later use by longjmp */ \
|
||||
if (_ex_code == 0) /* If no exception has occurred, execute the try block */
|
||||
|
||||
// Macro to define a catch block for a specific exception code
|
||||
// @param exception The exception code to catch (must match the value passed to `throw`)
|
||||
// @warning Variables declared inside `try` may not be accessible here due to block scope limitations.
|
||||
#define catch(exception) else if (_ex_code == (exception))
|
||||
|
||||
// Macro to throw an exception, unwinding execution to the nearest `try` block
|
||||
// @param exception The exception code to propagate (non-zero integer)
|
||||
// @warning Undefined behavior if called outside a `try` block or after `end_try`.
|
||||
#define throw(exception) longjmp(_ex_buf, (exception))
|
||||
|
||||
// Macro to terminate the try-catch block
|
||||
// @warning Required to close the `do-while` structure. Do not omit.
|
||||
#define end_try \
|
||||
} \
|
||||
while (0)
|
||||
|
||||
/**
|
||||
* @enum Exception
|
||||
* @brief Represents different types of exceptions that can occur in the application.
|
||||
*
|
||||
* This enumeration defines various exception types that the application can handle.
|
||||
* Each enumerator represents a specific kind of exception that might be encountered
|
||||
* during the execution of the program.
|
||||
*/
|
||||
enum Exception
|
||||
{
|
||||
// General exceptions
|
||||
INVALID_ARGUMENT = 1, // Invalid parameter passed to function
|
||||
INDEX_OUT_OF_BOUNDS, // Array/collection access out of valid range
|
||||
NULL_POINTER, // Unexpected null pointer encountered
|
||||
DIVISION_BY_ZERO, // Attempted division by zero
|
||||
MEMORY_ALLOC_FAILURE, // Failed to allocate dynamic memory
|
||||
|
||||
// I/O related exceptions
|
||||
FILE_NOT_FOUND = 100, // File could not be located
|
||||
FILE_READ_ERROR, // Error occurred while reading from file
|
||||
FILE_WRITE_ERROR, // Error occurred while writing to file
|
||||
PERMISSION_DENIED, // Insufficient permission for operation
|
||||
IO_OPERATION_FAILED, // General I/O operation failure
|
||||
|
||||
// Network related exceptions
|
||||
SOCKET_CREATION_FAILED = 200, // Failed to create a network socket
|
||||
CONNECTION_FAILED, // Network connection attempt failed
|
||||
CONNECTION_TIMEOUT, // Connection timed out
|
||||
HOST_UNREACHABLE, // Remote host unreachable
|
||||
DATA_TRANSMISSION_ERROR, // Error transmitting/receiving data
|
||||
|
||||
// Data processing exceptions
|
||||
PARSE_ERROR = 300, // Error parsing data (e.g., JSON, XML, etc.)
|
||||
ENCODING_ERROR, // Error encoding data
|
||||
DECODING_ERROR, // Error decoding data
|
||||
UNSUPPORTED_FORMAT, // Unsupported file or data format
|
||||
DATA_OVERFLOW, // Overflow during arithmetic or data operation
|
||||
DATA_UNDERFLOW, // Underflow during arithmetic or data operation
|
||||
DATA_CORRUPTION, // Data integrity check failed
|
||||
|
||||
// Multithreading/concurrency exceptions
|
||||
THREAD_CREATION_FAILED = 400, // Failed to create a thread
|
||||
THREAD_JOIN_FAILED, // Failed to join a thread
|
||||
MUTEX_LOCK_FAILED, // Failed to acquire a mutex lock
|
||||
DEADLOCK_DETECTED, // Deadlock condition detected
|
||||
THREAD_POOL_EXHAUSTED, // No available threads in a thread pool
|
||||
|
||||
// Resource management exceptions
|
||||
RESOURCE_NOT_AVAILABLE = 500, // Resource not available for use
|
||||
RESOURCE_LEAK_DETECTED, // Resource was not properly released
|
||||
HANDLE_INVALID, // Invalid resource handle
|
||||
CONFIGURATION_ERROR, // Invalid or missing configuration
|
||||
|
||||
// Custom application exceptions
|
||||
FEATURE_NOT_IMPLEMENTED = 600, // Feature is not implemented yet
|
||||
OPERATION_ABORTED, // Operation was aborted by the user or system
|
||||
TIMEOUT_ERROR, // Operation timed out
|
||||
INVALID_STATE, // Invalid state for operation
|
||||
UNSUPPORTED_OPERATION, // Operation not supported in current context
|
||||
VERSION_MISMATCH, // Version mismatch detected
|
||||
|
||||
// Security-related exceptions
|
||||
AUTHENTICATION_FAILED = 700, // User authentication failed
|
||||
AUTHORIZATION_FAILED, // User not authorized for operation
|
||||
DATA_ENCRYPTION_FAILED, // Data encryption failed
|
||||
DATA_DECRYPTION_FAILED, // Data decryption failed
|
||||
INSECURE_OPERATION, // Operation deemed insecure
|
||||
TOKEN_EXPIRED, // Security token expired or invalid
|
||||
ACCESS_VIOLATION, // Unauthorized memory access or system access
|
||||
|
||||
// Reserved for user-defined exceptions
|
||||
USER_DEFINED_START = 1000 // Start of user-defined exceptions
|
||||
};
|
||||
|
||||
#endif // HERROR_H
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <termios.h>
|
||||
#include "herror.h"
|
||||
#include "dynmem.h"
|
||||
|
||||
int prompPassword(char *password)
|
||||
{
|
||||
struct termios oldt, newt;
|
||||
|
||||
printf("Please enter your unique password:");
|
||||
try
|
||||
{
|
||||
// Disable echo
|
||||
tcgetattr(STDIN_FILENO, &oldt);
|
||||
newt = oldt;
|
||||
newt.c_lflag &= ~(ECHO);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
|
||||
do
|
||||
{
|
||||
scanf("%s", password);
|
||||
} while (password[0] == '\0');
|
||||
|
||||
// Restore echo
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
|
||||
}
|
||||
catch (MEMORY_ALLOC_FAILURE)
|
||||
{
|
||||
printf("Memory allocation failure\n");
|
||||
return 1;
|
||||
}
|
||||
end_try;
|
||||
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *prompNormalRequest(char *message)
|
||||
{
|
||||
char *temp = create(char);
|
||||
|
||||
temp = size(temp, 256);
|
||||
|
||||
printf("%s\n", message);
|
||||
scanf("%s", temp);
|
||||
|
||||
return temp;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef UI_H
|
||||
#define UI_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <termios.h>
|
||||
#include "herror.h"
|
||||
#include "dynmem.h"
|
||||
|
||||
#include "ui.c"
|
||||
|
||||
int pomrpPassword(char *password);
|
||||
char *prompNormalRequest(char *message);
|
||||
|
||||
#endif // !UI_H
|
||||
Reference in New Issue
Block a user