From 7f5df7b999d247198e0d6fa15e2d1ffd36318842 Mon Sep 17 00:00:00 2001 From: James Townsend Date: Mon, 21 Jun 2021 14:15:01 +0100 Subject: [PATCH] Separate enigma code from frontend --- Makefile | 11 +++++++---- enigma.c | 46 +--------------------------------------------- enigma.h | 28 ++++++++++++++++++++++++++++ frontend.c | 25 +++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 49 deletions(-) create mode 100644 enigma.h create mode 100644 frontend.c diff --git a/Makefile b/Makefile index ed340e3..87ff7be 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,16 @@ OPTS := -Wall -pedantic -std=c11 -all: enigma +all: frontend build/%.o: %.c - gcc $(OPTS) -c $< -g -o $@ + gcc $(OPTS) -c $< -g -fpic -o $@ -enigma: build/enigma.o build/readlines.o build/rotor.o - gcc build/enigma.o build/readlines.o build/rotor.o -o build/enigma +build/libenigma.so: build/enigma.o build/readlines.o build/rotor.o + gcc build/enigma.o build/readlines.o build/rotor.o -shared -o build/libenigma.so + +frontend: build/libenigma.so + gcc -Lbuild -Wl,-rpath=build -o build/enigma frontend.c -lenigma make clean: rm -f build/*.o build/*.so build/enigma \ No newline at end of file diff --git a/enigma.c b/enigma.c index 36e77c3..89935fa 100644 --- a/enigma.c +++ b/enigma.c @@ -1,50 +1,6 @@ #include #include -#include "readlines.h" -#include "rotor.h" - -struct enigma_structure { - rotor rotors[3]; - rotor reflector; - char plugboard[27]; // Used to perform substitution - char pairs[30]; // Not needed for enigma functionality, but useful to output plugboard config -}; - -typedef struct enigma_structure *enigma; - -void keypress_rotate(rotor rotors[3]); -char *encode_message(char *message, rotor rotors[3], rotor reflector, char plugboard[26]); -char p_sub(char c, char p[26]); -void input(char *string,int length); -enigma create_enigma_from_file(FILE *config_file, r_template templates[], int num_templates); -void destroy_engima(enigma e); -void display_config(enigma e); -r_template *load_templates_from_file(FILE* f, int *num_rotors); -enigma create_enigma(r_template rotors[3], r_template reflector, char positions[3], char ring_settings[3], char plugboard[27], char pairs[30]); - -int main() { - FILE* file = fopen("rotors", "r"); - int num_rotors = 0; - r_template *templates = load_templates_from_file(file, &num_rotors); - fclose(file); - - FILE *config_file = fopen("config", "r"); - enigma e = create_enigma_from_file(config_file, templates, num_rotors); - fclose(config_file); - free(templates); - - display_config(e); - - printf("Enter message to be encoded: "); - char buffer[BUFFER_SIZE]; - input(buffer, BUFFER_SIZE); - - char *result = encode_message(buffer, e->rotors, e->reflector, e->plugboard); - - printf("Result: %s\n", result); - destroy_engima(e); - free(result); -} +#include "enigma.h" r_template *load_templates_from_file(FILE* f, int *num_rotors) { int arr_size = LINES_ARR_LEN; diff --git a/enigma.h b/enigma.h new file mode 100644 index 0000000..66bc276 --- /dev/null +++ b/enigma.h @@ -0,0 +1,28 @@ +#ifndef enigma_h + +#define enigma_h + +#include "rotor.h" +#include "readlines.h" +#include + +struct enigma_structure { + rotor rotors[3]; + rotor reflector; + char plugboard[27]; // Used to perform substitution + char pairs[30]; // Not needed for enigma functionality, but useful to output plugboard config +}; + +typedef struct enigma_structure *enigma; + +void keypress_rotate(rotor rotors[3]); +char *encode_message(char *message, rotor rotors[3], rotor reflector, char plugboard[26]); +char p_sub(char c, char p[26]); +void input(char *string,int length); +enigma create_enigma_from_file(FILE *config_file, r_template templates[], int num_templates); +void destroy_engima(enigma e); +void display_config(enigma e); +r_template *load_templates_from_file(FILE* f, int *num_rotors); +enigma create_enigma(r_template rotors[3], r_template reflector, char positions[3], char ring_settings[3], char plugboard[27], char pairs[30]); + +#endif \ No newline at end of file diff --git a/frontend.c b/frontend.c new file mode 100644 index 0000000..fecb240 --- /dev/null +++ b/frontend.c @@ -0,0 +1,25 @@ +#include "enigma.h" + +int main() { + FILE* file = fopen("rotors", "r"); + int num_rotors = 0; + r_template *templates = load_templates_from_file(file, &num_rotors); + fclose(file); + + FILE *config_file = fopen("config", "r"); + enigma e = create_enigma_from_file(config_file, templates, num_rotors); + fclose(config_file); + free(templates); + + display_config(e); + + printf("Enter message to be encoded: "); + char buffer[BUFFER_SIZE]; + input(buffer, BUFFER_SIZE); + + char *result = encode_message(buffer, e->rotors, e->reflector, e->plugboard); + + printf("Result: %s\n", result); + destroy_engima(e); + free(result); +} \ No newline at end of file