Compare commits

..

2 Commits

Author SHA1 Message Date
da87b9a8a0 change message size cap (was 128) to INT64_MAX 2022-11-19 18:06:47 +00:00
1a8f4bbd90 fix whitespace formatting 2022-11-19 16:55:05 +00:00
4 changed files with 20 additions and 15 deletions

View File

@ -12,6 +12,7 @@
#include "dyninput.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@ -24,11 +25,12 @@ int main()
,stdout
);
char buf[4];
int64_t maxsize = INT64_MAX;
fgets(buf, 3, stdin);
int choice = strtol(buf, NULL, 10);
fputs("Enter your message text: ", stdout);
char* msg = dyninput_str(INT64_MAX);
char* msg = dyninput_str(maxsize);
if (!msg)
{
puts("ERROR: String input failed! Exiting...");
@ -41,18 +43,18 @@ int main()
if (choice == 1)
{
caesar_encrypt(msg, 128, key);
caesar_encrypt(msg, maxsize, key);
puts("\nYour encrypted message is:");
for (int i = 0; i < 128 && msg[i]; i++)
for (int i = 0; i < maxsize && msg[i]; i++)
{
putchar(msg[i]);
}
}
else if (choice == 2)
{
caesar_decrypt(msg, 128, key);
caesar_decrypt(msg, maxsize, key);
puts("\nYour decrypted message is:");
for (int i = 0; i < 128 && msg[i]; i++)
for (int i = 0; i < maxsize && msg[i]; i++)
{
putchar(msg[i]);
}

View File

@ -9,11 +9,13 @@
*/
#include "crypt.h"
#include <stdbool.h>
#include <stdint.h>
/*
* For built in C mod operator (%) -1 % 26 == -1
* For built in C mod operator (%) -1 % 26 == -1
* For the requirements of this algorithm -1 mod 26 == 25 is required
* The below functions accomplish this
*/
@ -33,8 +35,8 @@ inline int fast_mod(int a, int b)
/*
* 65 - 90 = ASCII 'A' - 'Z'
* 97 - 122 = ASCII 'a' - 'z'
* 65 - 90 = ASCII 'A' - 'Z'
* 97 - 122 = ASCII 'a' - 'z'
*/
inline bool isupper(char c)
@ -43,9 +45,9 @@ inline bool isupper(char c)
}
void caesar_encrypt(char* str, int size, int key)
void caesar_encrypt(char* str, int64_t size, int key)
{
for (int i = 0; i < size && str[i] != '\n'; i++)
for (int64_t i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ')
{
bool upper = isupper(str[i]);
@ -56,9 +58,9 @@ void caesar_encrypt(char* str, int size, int key)
}
void caesar_decrypt(char* str, int size, int key)
void caesar_decrypt(char* str, int64_t size, int key)
{
for (int i = 0; i < size && str[i] != '\n'; i++)
for (int64_t i = 0; i < size && str[i] != '\n'; i++)
if (str[i] != ' ')
{
bool upper = isupper(str[i]);

View File

@ -9,8 +9,9 @@
*/
#pragma once
#include <stdint.h>
void caesar_encrypt(char* str, int size, int key);
void caesar_encrypt(char* str, int64_t size, int key);
void caesar_decrypt(char* str, int size, int key);
void caesar_decrypt(char* str, int64_t size, int key);

View File

@ -36,7 +36,7 @@ char* dyninput_str(int_least64_t maxsize)
c = (char)getchar();
if (i + 1 == size)
{
char* tmp = realloc(str, size + 1);
char* tmp = realloc(str, size + 1);
if (!tmp)
{
puts("Error (re)allocating memory!");