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