Skip to content

Commit

Permalink
Separate enigma code from frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
muppet2011ad committed Jun 21, 2021
1 parent e1fba9b commit 7f5df7b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 49 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
46 changes: 1 addition & 45 deletions enigma.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#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;
Expand Down
28 changes: 28 additions & 0 deletions enigma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef enigma_h

#define enigma_h

#include "rotor.h"
#include "readlines.h"
#include <stdio.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]);

#endif
25 changes: 25 additions & 0 deletions frontend.c
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 7f5df7b

Please sign in to comment.