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
--- /dev/null
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#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);
+}
--- /dev/null
+xor(char* buf1, char* buf2, char* buf3, int length) {
+ int i;
+ for(i=0; i<length; i++)
+ buf3[i] = buf1[i] ^ buf2[i];
+}