main: refacto + care nullbyte in char *

This commit is contained in:
theBigBlase 2023-10-06 22:13:13 -04:00
parent c744b597c4
commit 76ee7c2fab
2 changed files with 22 additions and 13 deletions

View file

@ -5,24 +5,24 @@ OJB = $(SRC:.c=.o)
OUT = build OUT = build
CC = /usr/bin/gcc CC = /usr/bin/gcc
CFLAGS = -ansi -Wall -fsanitize=address -std=c99 -O0 CFLAGS = -ansi -Wall -std=c99 -O0
CFDEBUG = -ansi -Wall -g -std=c99 CFDEBUG = -ansi -Wall -fsanitize=address -g -std=c99
RM = /bin/rm -fr RM = /bin/rm -fr
TEST = -D TEST_TECH TEST_F = -D TEST_TECH
PART = -D PART_ TEST = -D TEST_
EVE = -D EVE_ EVE = -D EVE_
%.o: %.c %.o: %.c
$(CC) -c $(CFLAGS) $(CC) -c $(CFLAGS)
build: build:
$(CC) $(SRC) $(PART)$(part) $(EVE)$(eve) $(CFLAGS) -o $(OUT)/main.o $(CC) $(SRC) $(TEST)$(test) $(EVE)$(eve) $(CFLAGS) -o $(OUT)/main.o
build_test: build_test:
$(CC) $(SRC) $(CFLAGS) $(TEST) -o $(OUT)/main.o $(CC) $(SRC) $(CFLAGS) $(TEST_F) -o $(OUT)/main.o
debug: debug:
$(CC) $(SRC) $(PART)$(part) $(CFDEBUG) -o $(OUT)/main.o $(CC) $(SRC) $(TEST)$(test) $(CFDEBUG) -o $(OUT)/main.o
clean: clean:
$(RM) $(OBJ) $(OUT)/main.o $(RM) $(OBJ) $(OUT)/main.o

View file

@ -45,27 +45,29 @@ main(int argc, char *argv[])
#ifdef TEST_TECH #ifdef TEST_TECH
char *test = "yay"; char *test = "yay";
printf("Testing char_arr_to_uint32 and uint32_to_char_arr: \n"); printf("Testing char_arr_to_uint32 and uint32_to_char_arr: \n");
printf("%s\n", uint32_to_char_arr(char_arr_to_uint32(test))); char *res = uint32_to_char_arr(char_arr_to_uint32(test));
printf("%s\n", );
free(res);
#endif #endif
printf("\n--- MSG ---\nas 0x: "); printf("\n--- MSG ---\nas 0x: ");
#ifdef PART_ZERO #ifdef TEST_ZERO
msg = 0; msg = 0;
printf("0x00000000\n"); printf("0x00000000\n");
#endif #endif
#ifdef PART_MAX #ifdef TEST_MAX
msg = UINT32_MAX; msg = UINT32_MAX;
printf("0xffffffff\n"); printf("0xffffffff\n");
#endif #endif
#ifdef PART_MID #ifdef TEST_MID
printf("0x0000ffff\n"); printf("0x0000ffff\n");
/* is UINT16_MAX, but keeping this way for corrector convenience */ /* is UINT16_MAX, but keeping this way for corrector convenience */
msg = UINT32_MAX & UINT16_MAX; msg = UINT32_MAX & UINT16_MAX;
#endif #endif
#ifdef PART_NAME #ifdef TEST_NAME
msg = *(uint32_t *)"bob"; msg = *(uint32_t *)"bob";
printf("%u\n", msg); printf("%u\n", msg);
#endif #endif
@ -83,10 +85,12 @@ main(int argc, char *argv[])
printf("--- VERIF ---\n%d\n", printf("--- VERIF ---\n%d\n",
verif(key, msg, tag)); verif(key, msg, tag));
printf("\n--- ALICE ---\n"); printf("\n--- ALICE ---\n");
printf("sends msg: %u\nsends tag: %u\n", msg, tag); printf("sends msg: %u\nsends tag: %u\n", msg, tag);
printf("knows key: %u\n", key); printf("knows key: %u\n", key);
printf("\n--- EVE ---\n"); printf("\n--- EVE ---\n");
printf("recieves msg: %u\nrecieves tag: %u\n", msg, tag); printf("recieves msg: %u\nrecieves tag: %u\n", msg, tag);
struct eve tmp = { msg, tag }; struct eve tmp = { msg, tag };
@ -94,11 +98,14 @@ main(int argc, char *argv[])
msg = tmp.msg, tag = tmp.tag; msg = tmp.msg, tag = tmp.tag;
printf("sends msg: %u\nsends tag: %u\n", msg, tag); printf("sends msg: %u\nsends tag: %u\n", msg, tag);
printf("\n--- BOB ---\n"); printf("\n--- BOB ---\n");
printf("recieves msg: %u\nrecieves tag: %u\n", msg, tag); printf("recieves msg: %u\nrecieves tag: %u\n", msg, tag);
printf("knows key: %u\n", key); printf("knows key: %u\n", key);
char res = verif(msg, tag, key); char res = verif(msg, tag, key);
printf("verifies: %s\n", res ? "TRUE" : "FALSE"); printf("verifies: %s\n", res ? "TRUE" : "FALSE");
if (res) { if (res) {
msg_char = uint32_to_char_arr(msg); msg_char = uint32_to_char_arr(msg);
printf("msg as char *: %s\n", msg_char); printf("msg as char *: %s\n", msg_char);
@ -160,11 +167,13 @@ char_arr_to_uint32(char *arr)
static char * static char *
uint32_to_char_arr(uint32_t n) uint32_to_char_arr(uint32_t n)
{ {
char *res = calloc(4, 1); char *res = calloc(5, 1);
res[0] = MID_BITS(n, 0, 8); res[0] = MID_BITS(n, 0, 8);
res[1] = MID_BITS(n, 8, 16); res[1] = MID_BITS(n, 8, 16);
res[2] = MID_BITS(n, 16, 24); res[2] = MID_BITS(n, 16, 24);
res[3] = MID_BITS(n, 24, 32); res[3] = MID_BITS(n, 24, 32);
/* care for terminating nullbyte ! */
res[4] = '\0';
return res; return res;
} }