Skip to content

Prep for release 0.1.5 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions code/logic/fossil/io/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,68 @@ int fossil_io_validate_input_buffer(const char *buf, size_t size);
*/
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream);

/**
* @brief Validates if the input string is a valid integer.
*
* @param input The input string to validate.
* @param output Pointer to an integer where the parsed value will be stored if valid.
* @return true if the input is a valid integer, false otherwise.
*/
int fossil_io_validate_is_int(const char *input, int *output);

/**
* @brief Validates if the input string is a valid float.
*
* @param input The input string to validate.
* @param output Pointer to a float where the parsed value will be stored if valid.
* @return true if the input is a valid float, false otherwise.
*/
int fossil_io_validate_is_float(const char *input, float *output);

/**
* @brief Validates if the input string contains only alphanumeric characters.
*
* @param input The input string to validate.
* @return true if the input is alphanumeric, false otherwise.
*/
int fossil_io_validate_is_alnum(const char *input);

/**
* @brief Validates if the input string is a valid email address.
*
* @param input The input string to validate.
* @return true if the input is a valid email address, false otherwise.
*/
int fossil_io_validate_is_email(const char *input);

/**
* @brief Validates if the input string does not exceed the specified maximum length.
*
* @param input The input string to validate.
* @param max_length The maximum allowed length of the input string.
* @return true if the input length is within the specified limit, false otherwise.
*/
int fossil_io_validate_is_length(const char *input, size_t max_length);

/**
* @brief Sanitizes the input string and stores the sanitized result in the output buffer.
*
* @param input The input string to sanitize.
* @param output The buffer where the sanitized string will be stored.
* @param output_size The size of the output buffer.
* @return A fossil_io_validate_error_t indicating the result of the sanitization process.
*/
int fossil_io_validate_sanitize_string(const char *input, char *output, size_t output_size);

/**
* @brief Reads a secure line of input into the provided buffer.
*
* @param buffer The buffer where the input will be stored.
* @param buffer_size The size of the buffer.
* @return A fossil_io_validate_error_t indicating the result of the input reading process.
*/
int fossil_io_validate_read_secure_line(char *buffer, size_t buffer_size);

#ifdef __cplusplus
}

Expand Down Expand Up @@ -149,6 +211,115 @@ namespace fossil {
return fossil_io_gets_utf8(buf, size, input_stream);
}

/**
* Reads formatted input from the standard input stream.
*
* @param format The format string specifying how the input should be interpreted.
* @param ... Additional arguments for storing the input values.
* @return On success, the number of input items successfully matched and assigned is returned.
* On failure, EOF is returned.
*/
static int scanf(const char *format, ...) {
va_list args;
va_start(args, format);
int result = fossil_io_scanf(format, args);
va_end(args);
return result;
}

/**
* Reads formatted input from the specified input stream.
*
* @param input_stream Pointer to the input stream to read from.
* @param format The format string specifying how the input should be interpreted.
* @param ... Additional arguments for storing the input values.
* @return On success, the number of input items successfully matched and assigned is returned.
* On failure, EOF is returned.
*/
static int fscanf(FILE *input_stream, const char *format, ...) {
va_list args;
va_start(args, format);
int result = fossil_io_fscanf(input_stream, format, args);
va_end(args);
return result;
}

/**
* @brief Validates if the input string is a valid integer.
*
* @param input The input string to validate.
* @param output Pointer to an integer where the parsed value will be stored if valid.
* @return true if the input is a valid integer, false otherwise.
*/
static int validate_is_int(const char *input, int *output) {
return fossil_io_validate_is_int(input, output);
}

/**
* @brief Validates if the input string is a valid float.
*
* @param input The input string to validate.
* @param output Pointer to a float where the parsed value will be stored if valid.
* @return true if the input is a valid float, false otherwise.
*/
static int validate_is_float(const char *input, float *output) {
return fossil_io_validate_is_float(input, output);
}

/**
* @brief Validates if the input string contains only alphanumeric characters.
*
* @param input The input string to validate.
* @return true if the input is alphanumeric, false otherwise.
*/
static int validate_is_alnum(const char *input) {
return fossil_io_validate_is_alnum(input);
}

/**
* @brief Validates if the input string is a valid email address.
*
* @param input The input string to validate.
* @return true if the input is a valid email address, false otherwise.
*/
static int validate_is_email(const char *input) {
return fossil_io_validate_is_email(input);
}

/**
* @brief Validates if the input string does not exceed the specified maximum length.
*
* @param input The input string to validate.
* @param max_length The maximum allowed length of the input string.
* @return true if the input length is within the specified limit, false otherwise.
*/
static int validate_is_length(const char *input, size_t max_length) {
return fossil_io_validate_is_length(input, max_length);
}

/**
* @brief Sanitizes the input string and stores the sanitized result in the output buffer.
*
* @param input The input string to sanitize.
* @param output The buffer where the sanitized string will be stored.
* @param output_size The size of the output buffer.
* @return A fossil_io_validate_error_t indicating the result of the sanitization process.
*/
static int validate_sanitize_string(const char *input, char *output, size_t output_size) {
return fossil_io_validate_sanitize_string(input, output, output_size);
}

/**
* @brief Reads a secure line of input into the provided buffer.
*
* @param buffer The buffer where the input will be stored.
* @param buffer_size The size of the buffer.
* @return A fossil_io_validate_error_t indicating the result of the input reading process.
*/
static int validate_read_secure_line(char *buffer, size_t buffer_size) {
return fossil_io_validate_read_secure_line(buffer, buffer_size);
}

};

}
Expand Down
121 changes: 120 additions & 1 deletion code/logic/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@
#include "fossil/io/soap.h"
#include "fossil/io/output.h"

#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <limits.h>

#ifdef __WIN32
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif

// Function to trim leading and trailing spaces from a string
void fossil_io_trim(char *str) {
Expand Down Expand Up @@ -155,3 +166,111 @@ char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {

return buf;
}

int fossil_io_validate_is_int(const char *input, int *output) {
if (input == NULL || output == NULL) {
return 0;
}

char *endptr;
long value = strtol(input, &endptr, 10);

if (*endptr != '\0' || value < INT_MIN || value > INT_MAX) {
return 0;
}

*output = (int)value;
return 1;
}

int fossil_io_validate_is_float(const char *input, float *output) {
if (input == NULL || output == NULL) {
return 0;
}

char *endptr;
float value = strtof(input, &endptr);

if (*endptr != '\0') {
return 0;
}

*output = value;
return 1;
}

int fossil_io_validate_is_alnum(const char *input) {
if (input == NULL) {
return 0;
}

while (*input != '\0') {
if (!isalnum(*input)) {
return 0;
}
input++;
}

return 1;
}

int fossil_io_validate_is_email(const char *input) {
if (input == NULL) {
return 0;
}

// Check for the presence of an '@' character
const char *at = strchr(input, '@');
if (at == NULL) {
return 0;
}

// Check for the presence of a '.' character after the '@' character
const char *dot = strchr(at, '.');
if (dot == NULL) {
return 0;
}

return 1;
}

int fossil_io_validate_is_length(const char *input, size_t max_length) {
if (input == NULL) {
return 0;
}

return strlen(input) <= max_length;
}

int fossil_io_validate_sanitize_string(const char *input, char *output, size_t output_size) {
if (input == NULL || output == NULL || output_size == 0) {
return 0;
}

// Copy the input string to the output buffer
strncpy(output, input, output_size);

// Sanitize the output buffer
fossil_soap_sanitize(output);

return 1;
}

int fossil_io_validate_read_secure_line(char *buffer, size_t buffer_size) {
if (buffer == NULL || buffer_size == 0) {
return 0;
}

// Read a line of input from the user
if (fgets(buffer, buffer_size, stdin) == NULL) {
return 0;
}

// Remove the newline character from the input
size_t len = strlen(buffer);
if (len > 0 && buffer[len - 1] == '\n') {
buffer[len - 1] = '\0';
}

return 1;
}
Loading