Compare commits
9 Commits
4c9614342e
...
testing
Author | SHA1 | Date | |
---|---|---|---|
da87b9a8a0 | |||
1a8f4bbd90 | |||
bb0a1a5eef | |||
1fde10d2f5 | |||
236dbce306 | |||
4ba0eab83e | |||
51cd993b61 | |||
738bd9a99c | |||
7fe3d3c107 |
@ -12,9 +12,8 @@
|
|||||||
#include "dyninput.h"
|
#include "dyninput.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
@ -26,14 +25,17 @@ int main()
|
|||||||
,stdout
|
,stdout
|
||||||
);
|
);
|
||||||
char buf[4];
|
char buf[4];
|
||||||
|
int64_t maxsize = INT64_MAX;
|
||||||
fgets(buf, 3, stdin);
|
fgets(buf, 3, stdin);
|
||||||
int choice = strtol(buf, NULL, 10);
|
int choice = strtol(buf, NULL, 10);
|
||||||
|
|
||||||
//char* msg = calloc(128, sizeof(char));
|
|
||||||
|
|
||||||
fputs("Enter your message text: ", stdout);
|
fputs("Enter your message text: ", stdout);
|
||||||
//if (msg) fgets(msg, 128, stdin);
|
char* msg = dyninput_str(maxsize);
|
||||||
char* msg = dyninput_str(INT64_MAX);
|
if (!msg)
|
||||||
|
{
|
||||||
|
puts("ERROR: String input failed! Exiting...");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
fputs("Enter your key (shift value): ", stdout);
|
fputs("Enter your key (shift value): ", stdout);
|
||||||
fgets(buf, 4, stdin);
|
fgets(buf, 4, stdin);
|
||||||
@ -41,18 +43,18 @@ int main()
|
|||||||
|
|
||||||
if (choice == 1)
|
if (choice == 1)
|
||||||
{
|
{
|
||||||
caesar_encrypt(msg, 128, key);
|
caesar_encrypt(msg, maxsize, key);
|
||||||
puts("\nYour encrypted message is:");
|
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]);
|
putchar(msg[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (choice == 2)
|
else if (choice == 2)
|
||||||
{
|
{
|
||||||
caesar_decrypt(msg, 128, key);
|
caesar_decrypt(msg, maxsize, key);
|
||||||
puts("\nYour decrypted message is:");
|
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]);
|
putchar(msg[i]);
|
||||||
}
|
}
|
||||||
|
23
src/crypt.c
23
src/crypt.c
@ -9,8 +9,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "crypt.h"
|
#include "crypt.h"
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
|
||||||
// Which of the two below functions is faster (find out!)
|
// Which of the two below functions is faster (find out!)
|
||||||
inline int mod(int a, int b)
|
inline int mod(int a, int b)
|
||||||
@ -26,15 +34,20 @@ inline int fast_mod(int a, int b)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 65 - 90 = ASCII 'A' - 'Z'
|
||||||
|
* 97 - 122 = ASCII 'a' - 'z'
|
||||||
|
*/
|
||||||
|
|
||||||
inline bool isupper(char c)
|
inline bool isupper(char c)
|
||||||
{
|
{
|
||||||
return c >= 'A' && c <= 'Z';
|
return c >= 'A' && c <= 'Z';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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] != ' ')
|
if (str[i] != ' ')
|
||||||
{
|
{
|
||||||
bool upper = isupper(str[i]);
|
bool upper = isupper(str[i]);
|
||||||
@ -45,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] != ' ')
|
if (str[i] != ' ')
|
||||||
{
|
{
|
||||||
bool upper = isupper(str[i]);
|
bool upper = isupper(str[i]);
|
||||||
|
@ -9,8 +9,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#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);
|
||||||
|
@ -15,33 +15,44 @@
|
|||||||
|
|
||||||
char* dyninput_str(int_least64_t maxsize)
|
char* dyninput_str(int_least64_t maxsize)
|
||||||
{
|
{
|
||||||
char* str = malloc(3 * sizeof(char));
|
char* str = malloc(3 * sizeof(char));
|
||||||
/*if (!str)
|
if (!str)
|
||||||
puts("Error allocating memory!");
|
{
|
||||||
return NULL;*/
|
puts("\nError allocating memory!");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
int_least64_t size = 3, i = 0;
|
int_least64_t size = 3, i = 0;
|
||||||
char c = 0;
|
char c = 0;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
c = getchar();
|
if (size == maxsize)
|
||||||
if (i + 2 == size)
|
{
|
||||||
{
|
printf("Character limit of %lld reached!\n", maxsize);
|
||||||
char* tmp = realloc(str, size + 1);
|
break;
|
||||||
if (!tmp)
|
}
|
||||||
{
|
|
||||||
puts("Error (re)allocating memory!");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
str = tmp;
|
|
||||||
size++;
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i] = c;
|
c = (char)getchar();
|
||||||
i++;
|
if (i + 1 == size)
|
||||||
} while (c && c != '\n');
|
{
|
||||||
str[i] = '\0';
|
char* tmp = realloc(str, size + 1);
|
||||||
|
if (!tmp)
|
||||||
|
{
|
||||||
|
puts("Error (re)allocating memory!");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
str = tmp;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
return str;
|
str[i] = c;
|
||||||
|
i++;
|
||||||
|
} while (c && c != '\n');
|
||||||
|
if (str[i - 1] != '\n') str[i - 1] = '\n';
|
||||||
|
str[i] = '\0';
|
||||||
|
|
||||||
|
// Sanitise stdin
|
||||||
|
while (c != '\n') c = (char)getchar();
|
||||||
|
return str;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user