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
CC = /usr/bin/gcc
CFLAGS = -ansi -Wall -fsanitize=address -std=c99 -O0
CFDEBUG = -ansi -Wall -g -std=c99
CFLAGS = -ansi -Wall -std=c99 -O0
CFDEBUG = -ansi -Wall -fsanitize=address -g -std=c99
RM = /bin/rm -fr
TEST = -D TEST_TECH
PART = -D PART_
TEST_F = -D TEST_TECH
TEST = -D TEST_
EVE = -D EVE_
%.o: %.c
$(CC) -c $(CFLAGS)
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:
$(CC) $(SRC) $(CFLAGS) $(TEST) -o $(OUT)/main.o
$(CC) $(SRC) $(CFLAGS) $(TEST_F) -o $(OUT)/main.o
debug:
$(CC) $(SRC) $(PART)$(part) $(CFDEBUG) -o $(OUT)/main.o
$(CC) $(SRC) $(TEST)$(test) $(CFDEBUG) -o $(OUT)/main.o
clean:
$(RM) $(OBJ) $(OUT)/main.o

View file

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