From 7f0383dc6d58498d501d4c1f6dd6ff7196d716e0 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Sun, 14 May 2017 00:13:27 -0700 Subject: [PATCH] Solve Set 1, Problem 2 --- Makefile | 12 ++++++++---- set1p2.test.c | 29 +++++++++++++++++++++++++++++ xor.c | 5 +++++ xor.h | 1 + 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 set1p2.test.c create mode 100644 xor.c create mode 100644 xor.h diff --git a/Makefile b/Makefile index f11b138..5bbfa62 100644 --- a/Makefile +++ b/Makefile @@ -2,11 +2,15 @@ CC=gcc ODIR=obj CFLAGS= -all: io.o io.test.o -%.test: %.o %.test.c +all: libs +libs: io.o xor.o +set1p1.test: set1p1.test.c io.o $(CC) -o $@ $? $(CFLAGS) -test: set1p2.test - ./set1p2.test +set1p2.test: set1p2.test.c io.o xor.o + $(CC) -o $@ $? $(CFLAGS) +test: set1p1.test set1p2.test + ./set1p1.test >/dev/null || echo "Set 1, Problem 1: Failed" + ./set1p2.test >/dev/null || echo "Set 1, Problem 2: Failed" clean: rm -f *.o a.out *.test %.o: %.c diff --git a/set1p2.test.c b/set1p2.test.c new file mode 100644 index 0000000..1097a80 --- /dev/null +++ b/set1p2.test.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include "io.h" +#include "xor.h" + +int main() { + char a[] = "1c0111001f010100061a024b53535009181c"; + char b[] = "686974207468652062756c6c277320657965"; + char expected_a_xor_b[] = "746865206b696420646f6e277420706c6179"; + + char a_xor_b[37]; + char a_bytes[18], b_bytes[18], ab_bytes[18]; + int a_bytes_len, b_bytes_len; + decode_hex(a, a_bytes, &a_bytes_len); + decode_hex(b, b_bytes, &b_bytes_len); + if (a_bytes_len != 18 || b_bytes_len != 18) { + printf("Wrong length."); exit(1); + } + xor(a_bytes, b_bytes, ab_bytes, a_bytes_len); + encode_hex(ab_bytes, a_bytes_len, a_xor_b); + if (strcmp(a_xor_b, expected_a_xor_b)==0) { + printf("Xor succeeded.\n"); + } else { + printf("Xor failed.\nExpected: %s\n Got: %s\n", expected_a_xor_b, a_xor_b); + exit(1); + } + exit(0); +} diff --git a/xor.c b/xor.c new file mode 100644 index 0000000..ad84b89 --- /dev/null +++ b/xor.c @@ -0,0 +1,5 @@ +xor(char* buf1, char* buf2, char* buf3, int length) { + int i; + for(i=0; i