Better code to handle stdin, error and lack of input handling

This commit is contained in:
Ignacio 2024-04-01 22:22:41 -03:00
parent d3e3123373
commit 29925395b3

101
salty.c
View File

@ -57,54 +57,45 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
} }
unsigned char* decrypted; unsigned char* decrypted;
size_t decLen; size_t decLen = 0;
// Check if input is stdin, and read the input // Check if input is stdin, and read the input
if (stdinput) { if (stdinput) {
// Begin reading into buffer // Begin reading into buffer
unsigned char buffer[BUF_SIZE]; unsigned char buffer[BUF_SIZE];
size_t inputSize = 0;
unsigned char c;
unsigned char* input = malloc(BUF_SIZE); unsigned char* input = malloc(BUF_SIZE);
if (input == NULL) { if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error."); fprintf(stderr,ERR"Memory allocation error.\n");
fclose(outFile); fclose(outFile);
return 1; return 1;
} }
int i = 0; int i = 0;
c = fgetc(stdin);
while (feof(stdin) == 0) { while(true) {
inputSize++; size_t readBytes = fread(buffer,1,BUF_SIZE,stdin);
if (i < BUF_SIZE) { decLen += readBytes;
buffer[i] = c; // Prevents reallocating to zero bytes.
i++; if (decLen > 0) {
} else { unsigned char* old = input;
char *old = input; input = realloc(input, decLen);
input = realloc(input, inputSize);
if (input == NULL) { if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error."); fprintf(stderr,ERR"Memory allocation error.\n");
fclose(outFile); fclose(outFile);
free(old); free(old);
return 1; return 1;
} }
memcpy(input + inputSize - BUF_SIZE,buffer,BUF_SIZE);
i = 0;
} }
c = fgetc(stdin); memcpy(input + decLen - readBytes,buffer,readBytes);
} if (readBytes == 0 && !ferror(stdin))
if (i > 0) { break;
char *old = input; else if (readBytes == 0 && ferror(stdin)) {
input = realloc(input, inputSize); fprintf(stderr,ERR"Error reading from stdin.\n");
if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error.");
fclose(outFile); fclose(outFile);
free(old); free(input);
return 1; return 1;
} }
memcpy(input + inputSize - i,buffer,i);
} }
decrypted = input; decrypted = input;
decLen = inputSize;
} else { } else {
// Open input file // Open input file
FILE *inFile = fopen(inputFile,"rb"); FILE *inFile = fopen(inputFile,"rb");
@ -128,6 +119,12 @@ int encryptMessage(const char* inputFile, const char* outputFile, const char* pa
fclose(inFile); fclose(inFile);
} }
if (decLen == 0) {
fprintf(stderr,ERR"No bytes read from file.\n");
fclose(outFile);
return 1;
}
size_t encLen = decLen + crypto_secretbox_MACBYTES; size_t encLen = decLen + crypto_secretbox_MACBYTES;
fprintf(stderr,OK"File is %d bytes.\n",decLen); fprintf(stderr,OK"File is %d bytes.\n",decLen);
fprintf(stderr,OK"Encoding file to %d bytes.\n", encLen); fprintf(stderr,OK"Encoding file to %d bytes.\n", encLen);
@ -190,65 +187,52 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
// Variables // Variables
unsigned char salt[SALT_SIZE]; unsigned char salt[SALT_SIZE];
unsigned char nonce[crypto_secretbox_NONCEBYTES]; unsigned char nonce[crypto_secretbox_NONCEBYTES];
unsigned char* fullInput; unsigned char* fullInput;
size_t inLen; size_t inLen = 0;
// Check if input is stdin, and read the input // Check if input is stdin, and read the input
if (stdinput) { if (stdinput) {
// Begin reading into buffer // Begin reading into buffer
unsigned char buffer[BUF_SIZE]; unsigned char buffer[BUF_SIZE];
size_t inputSize = 0;
unsigned char c;
unsigned char* input = malloc(BUF_SIZE); unsigned char* input = malloc(BUF_SIZE);
if (input == NULL) { if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error."); fprintf(stderr,ERR"Memory allocation error.");
fclose(outFile); fclose(outFile);
return 1; return 1;
} }
int i = 0; while(true) {
c = fgetc(stdin); size_t readBytes = fread(buffer,1,BUF_SIZE,stdin);
while (feof(stdin) == 0) { inLen += readBytes;
inputSize++; // Prevents reallocating to zero bytes.
if (i < BUF_SIZE) { if (inLen > 0) {
buffer[i] = c; unsigned char* old = input;
i++; input = realloc(input, inLen);
} else {
char *old = input;
input = realloc(input, inputSize);
if (input == NULL) { if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error."); fprintf(stderr,ERR"Memory allocation error.\n");
fclose(outFile); fclose(outFile);
free(old); free(old);
return 1; return 1;
} }
memcpy(input + inputSize - BUF_SIZE,buffer,BUF_SIZE);
i = 0;
} }
c = fgetc(stdin); memcpy(input + inLen - readBytes,buffer,readBytes);
} if (readBytes == 0 && !ferror(stdin))
if (i > 0) { break;
char *old = input; else if (readBytes == 0 && ferror(stdin)) {
input = realloc(input, inputSize); fprintf(stderr,ERR"Error reading from stdin.\n");
if (input == NULL) {
fprintf(stderr,ERR"Memory allocation error.");
fclose(outFile); fclose(outFile);
free(old); free(input);
return 1; return 1;
} }
memcpy(input + inputSize - i,buffer,i);
} }
fullInput = input; fullInput = input;
inLen = inputSize;
} else { } else {
FILE *inFile = fopen(inputFile,"rb"); FILE *inFile = fopen(inputFile,"rb");
// Get the size of the file and read it // Get the size of the file and read it
size_t inputSize = fileSize(inFile); inLen = fileSize(inFile);
unsigned char* input = malloc(inputSize); unsigned char* input = malloc(inLen);
if (fread(input,1,inputSize,inFile) != inputSize) { if (fread(input,1,inLen,inFile) != inLen) {
fprintf(stderr,ERR"Error reading from input file.\n"); fprintf(stderr,ERR"Error reading from input file.\n");
fclose(inFile); fclose(inFile);
fclose(outFile); fclose(outFile);
@ -257,13 +241,12 @@ int decryptMessage(const char* inputFile, const char* outputFile, const char* pa
fclose(inFile); fclose(inFile);
fullInput = input; fullInput = input;
inLen = inputSize;
} }
size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8; size_t encLen = inLen - sizeof(salt) - sizeof(nonce) - 8;
if (inLen == 0) { if (inLen == 0) {
fprintf(stderr,ERR"File size is 0. No data."); fprintf(stderr,ERR"No bytes read from file.\n");
fclose(outFile); fclose(outFile);
return 1; return 1;
} }