From e0c0ac68d94906459d2bcfee883021bfeea4d777 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Tue, 9 Nov 2021 23:19:51 -0300 Subject: [PATCH] Update Makefile and continue refactoring code Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- Makefile | 33 +++++++++++++++------------------ include/challenges.h | 7 +------ include/math.h | 12 ++++++++++++ include/random.h | 8 -------- include/server.h | 1 - src/challenges.c | 6 ------ src/{random.c => math.c} | 8 +++++++- 7 files changed, 35 insertions(+), 40 deletions(-) create mode 100644 include/math.h delete mode 100644 include/random.h rename src/{random.c => math.c} (69%) diff --git a/Makefile b/Makefile index b2be4fa..b9fb844 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,28 @@ CC = gcc CCFLAGS = -Wall -std=c99 -pedantic -g -CLIENT=client -SERVER=server -ERRORS=errors.o -CHALLENGES=challenges.o -RANDOM=random.o -SOCKETS=sockets.o +SERVER_SOURCES=src/server.c src/errors.c src/challenges.c src/math.c src/sockets.c +CLIENT_SOURCES=src/client.c src/errors.c src/sockets.c +SERVER_LIBS=include/errors.h include/challenges.h include/server.h include/challengesLib.h include/sockets.h include/math.h +CLIENT_LIBS=include/errors.h include/client.h include/sockets.h +SERVER_OBJ=server +CLIENT_OBJ=client TMP := $(shell mktemp) -all: $(ERRORS) $(CHALLENGES) $(RANDOM) $(SOCKETS) $(CLIENT) $(SERVER) +all: $(CLIENT_OBJ) $(SERVER_OBJ) -$(SERVER): src/server.c src/challenges.c src/errors.c src/sockets.c include/errors.h include/challenges.h include/server.h include/challengesLib.h include/sockets.h - $(CC) $(CCFLAGS) src/server.c -o server $(CHALLENGES) $(ERRORS) $(RANDOM) $(SOCKETS) -lm - objcopy --add-section .RUN_ME="$(TMP)" --set-section-flags .mydata=noload,readonly server - strip --strip-debug server +$(SERVER_OBJ): $(SERVER_SOURCES) $(SERVER_LIBS) + $(CC) $(CCFLAGS) -o $(SERVER_OBJ) $(SERVER_SOURCES) -lm + objcopy --add-section .RUN_ME="$(TMP)" --set-section-flags .mydata=noload,readonly $(SERVER_OBJ) + strip --strip-debug $(SERVER_OBJ) rm "$(TMP)" -$(CLIENT): src/client.c src/errors.c src/sockets.c include/errors.h include/client.h include/sockets.h - $(CC) $(CCFLAGS) src/client.c -o client $(ERRORS) $(SOCKETS) - -%.o: src/%.c - $(CC) $(CCFLAGS) -I./include -c $< +$(CLIENT_OBJ): $(CLIENT_SOURCES) $(CLIENT_LIBS) + $(CC) $(CCFLAGS) -o $(CLIENT_OBJ) $(CLIENT_SOURCES) clean: - rm -f $(ERRORS) $(CHALLENGES) $(CLIENT) $(SERVER) $(RANDOM) $(SOCKETS) + rm -f $(CLIENT_OBJ) $(SERVER_OBJ) test: pvs-studio-analyzer trace -- make @@ -34,6 +31,6 @@ test: cppcheck --quiet --enable=all --force --inconclusive . cleanTest: - rm -rf PVS-Studio.log report.tasks strace_out + rm -f PVS-Studio.log report.tasks strace_out .PHONY: all clean test cleanTest diff --git a/include/challenges.h b/include/challenges.h index b54792d..ad0ef0b 100644 --- a/include/challenges.h +++ b/include/challenges.h @@ -7,15 +7,10 @@ #include #include #include -#include #include "errors.h" -#include "random.h" +#include "math.h" -#define MAX_LEN 100 #define MAX_NORMALS 1000 -#define UPPER 2 -#define LOWER 1 -#define M_PI 3.14159265358979323846 #define MAGIC_VAL 0x12345678 typedef struct challenge_t { diff --git a/include/math.h b/include/math.h new file mode 100644 index 0000000..36b8417 --- /dev/null +++ b/include/math.h @@ -0,0 +1,12 @@ +#ifndef MATH_H +#define MATH_H + +#include +#include + +#define M_PI 3.14159265358979323846 + +double getUniform(uint32_t max); +void boxMuller(double * normal); + +#endif \ No newline at end of file diff --git a/include/random.h b/include/random.h deleted file mode 100644 index dc80db5..0000000 --- a/include/random.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef MATH_H -#define MATH_H - -#include - -double getUniform(uint32_t max); - -#endif \ No newline at end of file diff --git a/include/server.h b/include/server.h index 663a83e..c276378 100644 --- a/include/server.h +++ b/include/server.h @@ -14,7 +14,6 @@ #include "challengesLib.h" #include "sockets.h" -#define MAX_LEN 100 #define TIME_SLEEP 2 void startChallenge(); diff --git a/src/challenges.c b/src/challenges.c index bf12ab6..1bf526c 100644 --- a/src/challenges.c +++ b/src/challenges.c @@ -87,12 +87,6 @@ void gdbChallenge() { gdbme(); } -void boxMuller(double * normal) { - double random1 = getUniform(1); - double random2 = getUniform(1); - *normal = sqrt(-2 * log(random1)) * cos(2 * M_PI * random2); -} - void normalChallenge() { for (int i = 0; i < MAX_NORMALS; i++) { double normal = 0; diff --git a/src/random.c b/src/math.c similarity index 69% rename from src/random.c rename to src/math.c index c54c9b6..ddccb7d 100644 --- a/src/random.c +++ b/src/math.c @@ -1,6 +1,6 @@ // This is a personal academic project. Dear PVS-Studio, please check it. // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com -#include "../include/random.h" +#include "../include/math.h" static uint32_t m_z = 362436069; static uint32_t m_w = 521288629; @@ -14,4 +14,10 @@ uint32_t getUint() { double getUniform(uint32_t max) { uint32_t u = getUint(); return (u + 1.0) * 2.328306435454494e-10 * max; +} + +void boxMuller(double * normal) { + double random1 = getUniform(1); + double random2 = getUniform(1); + *normal = sqrt(-2 * log(random1)) * cos(2 * M_PI * random2); } \ No newline at end of file