commit 34b21c494b6b9fcc9e3c9e7d98bfea6114eefa41 from: Evan Burkey date: Wed Jul 31 21:40:16 2024 UTC cleanup const qualifiers. Add index argument to vec_push commit - c2fe01f04c367f396e0b649d18e524eaffa648b8 commit + 34b21c494b6b9fcc9e3c9e7d98bfea6114eefa41 blob - 29c64bdbce1d9efd5c8a3483d49bb07f307a2612 blob + 116107cf7edb41ba8c57145587e606f5ac7cd369 --- include/lfcrypto.h +++ include/lfcrypto.h @@ -3,15 +3,15 @@ #include -const char *b64_encode(const unsigned char *s, size_t sz); +char *b64_encode(const unsigned char *s, size_t sz); unsigned char *b64_decode(const char *s, size_t sz, size_t *decode_sz); -const char *hex_encode(const unsigned char *hex, size_t sz); +char *hex_encode(const unsigned char *hex, size_t sz); unsigned char *hex_decode(const char *orig, size_t *sz); -const char *hex_to_str(const unsigned char *hex, size_t sz); +char *hex_to_str(const unsigned char *hex, size_t sz); -const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz); -const unsigned char *repeating_key_xor_s(const char* s, const char* key); +unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz); +unsigned char *repeating_key_xor_s(const char* s, const char* key); unsigned int hamming_distance_s(const char *a, const char *b); unsigned int hamming_distance(unsigned char *a, unsigned char *b, size_t sz); blob - 9b65d98c7906ac9f315f10d4ac6f6aa8fd129fbe blob + 8afb905eb12229c1215e032fca1f8fd5d3e47465 --- include/lfstring.h +++ include/lfstring.h @@ -5,6 +5,6 @@ int find_substrings(const char* haystack, const char* needle, size_t *num_substrings, size_t **substrings); -const char* substr(const char* str, size_t idx, size_t len); +char* substr(const char* str, size_t idx, size_t len); #endif // LIBFLINT_H_STRING blob - c0c7a630137ea37ed58d234696eadaf8de52accf blob + 0c294c83921366234fafaa1e5f491b0c23a019ac --- include/lfvector.h +++ include/lfvector.h @@ -18,7 +18,7 @@ void vec_destroy(Vector *vec); int vec_insert(Vector *vec, void *data, size_t index); -int vec_push(Vector *vec, void *data); +int vec_push(Vector *vec, void *data, size_t *index); void *vec_safe_at(Vector *vec, size_t index); blob - f907d6e7a4c6e2704272a6de3579879d28511185 blob + c3a42e6a9e6707f9f558e0aa06af77fe96698e52 --- src/crypto.c +++ src/crypto.c @@ -45,7 +45,7 @@ static int resize_b64_buf(b64_buf *b, size_t sz) { return 0; } -const char *b64_encode(const unsigned char *s, size_t sz) { +char *b64_encode(const unsigned char *s, size_t sz) { int i = 0; b64_buf encbuf; size_t size = 0; @@ -236,7 +236,7 @@ unsigned char *hex_decode(const char *orig, size_t *sz return hex; } -const char *hex_encode(const unsigned char *hex, size_t sz) { +char *hex_encode(const unsigned char *hex, size_t sz) { size_t ssz = sz * 2 + 1; char *s = malloc(sizeof(char) * ssz); char *pos = s; @@ -250,7 +250,7 @@ const char *hex_encode(const unsigned char *hex, size_ return s; } -const char *hex_to_str(const unsigned char *hex, size_t sz) { +char *hex_to_str(const unsigned char *hex, size_t sz) { char *s = malloc(sizeof(char) * (sz + 1)); for (size_t i = 0; i < sz; ++i) { s[i] = (char)hex[i]; @@ -259,7 +259,7 @@ const char *hex_to_str(const unsigned char *hex, size_ return s; } -const unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz) { +unsigned char* repeating_key_xor(const unsigned char* s, size_t s_sz, const unsigned char* key, size_t k_sz) { unsigned char* r = malloc(sizeof(unsigned char) * s_sz); for (size_t i = 0, j = 0; i < s_sz; ++i) { r[i] = s[i] ^ key[j]; @@ -268,7 +268,7 @@ const unsigned char* repeating_key_xor(const unsigned return r; } -const unsigned char *repeating_key_xor_s(const char* s, const char* key) { +unsigned char *repeating_key_xor_s(const char* s, const char* key) { return repeating_key_xor((unsigned char*)s, strlen(s), (unsigned char*)key, strlen(key)); } blob - 488ad0691f02bab72076981e01e6009ae4aeace9 blob + 6cfe72d28dc88b08e240782f73007000a8b8da3c --- src/memory.c +++ src/memory.c @@ -83,7 +83,7 @@ static void *arena_resize_align(ArenaAllocator *alloca return arena_malloc_align(allocator, new_sz, align); } - if (allocator->buf <= mem && mem < allocator->buf + allocator->buf_sz) { + if (allocator->buf <= (unsigned char*)mem && (unsigned char*)mem < allocator->buf + allocator->buf_sz) { if (allocator->buf + allocator->offset_prev == old_mem) { allocator->offset_cur = allocator->offset_prev + new_sz; if (new_sz > old_sz) { blob - f277c5c80b79f670aa5c82f5cf23c90ce324a5e7 blob + 4fe56033bd86f75901823d8367d78b305597e022 --- src/string.c +++ src/string.c @@ -40,7 +40,7 @@ int find_substrings(const char* haystack, const char* return 0; } -const char* substr(const char* str, size_t idx, size_t len) { +char* substr(const char* str, size_t idx, size_t len) { size_t sz_str = strlen(str); if (sz_str < len || idx + len > sz_str) { return NULL; blob - dd8d3030cda7f370c807c1542cfa63c45f924233 blob + 8204b3eabaf2386260cd4ef18a4b561609849353 --- src/vector.c +++ src/vector.c @@ -81,7 +81,10 @@ int vec_insert(Vector *vec, void *data, size_t index) return 0; } -int vec_push(Vector *vec, void *data) { +int vec_push(Vector *vec, void *data, size_t *index) { + if (index != NULL) { + *index = vec_len(vec); + } return vec_insert(vec, data, vec_len(vec)); } blob - ed45525483916ceb313c69c4254b3aa9c84e38a0 blob + f42f6adf5495aa83ff5b501ad0b7d34bb92462b1 --- tests/tests.c +++ tests/tests.c @@ -70,9 +70,9 @@ void test_set() { int j = 2; int k = 2; - set_insert(set, (void *) &i); - set_insert(set, (void *) &j); - set_insert(set, (void *) &k); + set_insert(set, &i); + set_insert(set, &j); + set_insert(set, &k); int i2 = 1; int j2 = 4; @@ -80,8 +80,8 @@ void test_set() { Set *set2 = malloc(sizeof(Set)); set_init(set2, int_match, NULL); - set_insert(set2, (void *) &i2); - set_insert(set2, (void *) &j2); + set_insert(set2, &i2); + set_insert(set2, &j2); printf("Set 1:"); print_ll(set); @@ -202,7 +202,7 @@ void test_math() { void print_vector(Vector *vec) { for (size_t i = 0; i < vec->length; ++i) { - int t = *(int*)vec_at(vec, i); + int t = *(int *) vec_at(vec, i); printf("%d ", t); } printf("\n"); @@ -219,29 +219,31 @@ void test_vector() { int e3 = 3; int e4 = 4; - vec_push(v, &e0); + size_t idx = 0; + vec_push(v, &e0, &idx); + assert(idx == 0); assert(v->length == 1); - int *t = (int*)vec_at(v, 0); + int *t = (int *) vec_at(v, 0); assert(*t == 0); - vec_push(v, &e1); - vec_push(v, &e2); + vec_push(v, &e1, NULL); + vec_push(v, &e2, NULL); assert(v->length == 3); // test access outside bounds - t = (int*)vec_safe_at(v, 3); + t = (int *) vec_safe_at(v, 3); assert(t == NULL); printf("Before insert: "); print_vector(v); - vec_push(v, &e4); + vec_push(v, &e4, NULL); vec_insert(v, &e3, 3); printf("After insert: "); print_vector(v); - t = (int*)vec_at(v, 3); + t = (int *) vec_at(v, 3); assert(*t == e3); - t = (int*)vec_at(v, 4); + t = (int *) vec_at(v, 4); assert(*t == e4); const int *min = vec_min(v, vec_cmp_int); @@ -251,13 +253,13 @@ void test_vector() { assert(*min == e0); assert(*max == e4); - t = (int*)vec_remove(v, 1); + t = (int *) vec_remove(v, 1); assert(t != NULL); assert(*t == 1); printf("After removal: "); print_vector(v); - t = (int*)vec_remove(v, 10); + t = (int *) vec_remove(v, 10); assert(t == NULL); printf("\ncap before shrink: %zu\n", vec_cap(v)); @@ -268,13 +270,15 @@ void test_vector() { vec_destroy(v); free(v); } + void test_string() { printf("\n--- STRING TEST ---\n"); - const char* haystack = "Test one two one and also maybe two but not Gabe's least favorite number, which is not one."; - const char* needles[] = { - "one", - "two", - "Gabe" + const char *haystack = + "Test one two one and also maybe two but not Gabe's least favorite number, which is not one."; + const char *needles[] = { + "one", + "two", + "Gabe" }; size_t sub_sz = 0; @@ -286,7 +290,7 @@ void test_string() { assert(subs[1] == 13); assert(subs[2] == 87); - const char *s = substr(haystack, subs[0], strlen(needles[0])); + char *s = substr(haystack, subs[0], strlen(needles[0])); assert(strcmp(s, needles[0]) == 0); free(s); @@ -330,14 +334,14 @@ void test_crypto() { char *out2 = "YSBsb25nZXIgYmFzZTY0IHRlc3QsIGFwcGFyZW50bHk="; size_t s_sz = 0; - s = (char *)b64_decode(out2, strlen(out2), &s_sz); + s = (char *) b64_decode(out2, strlen(out2), &s_sz); assert(strcmp(s, "a longer base64 test, apparently") == 0); assert(strlen(s) == s_sz); free(s); s = hex_decode("DEADBEEF", &s_sz); unsigned char h[4] = { - 0xDE, 0xAD, 0xBE, 0xEF + 0xDE, 0xAD, 0xBE, 0xEF }; for (size_t i = 0; i < 4; ++i) { assert(s[i] == h[i]); @@ -347,7 +351,7 @@ void test_crypto() { // Odd number of characters s = hex_decode("f00f5", &s_sz); unsigned char h2[4] = { - 0x0F, 0x00, 0xF5 + 0x0F, 0x00, 0xF5 }; for (size_t i = 0; i < 3; ++i) { assert(s[i] == h2[i]); @@ -367,20 +371,20 @@ void test_crypto() { // "Sup?" unsigned char hexsup[4] = { - 0x53, 0x75, 0x70, 0x3F + 0x53, 0x75, 0x70, 0x3F }; s = hex_to_str(hexsup, 4); assert(strcmp(s, "Sup?") == 0); free(s); s = repeating_key_xor_s("TEST", "HI"); - const char *enc = hex_encode(s, 4); + char *enc = hex_encode(s, 4); assert(strcmp(enc, "1c0c1b1d") == 0); free(enc); free(s); - unsigned char ua[2] = { 0x2, 0xF }; - unsigned char ub[2] = { 0x4, 0xE }; + unsigned char ua[2] = {0x2, 0xF}; + unsigned char ub[2] = {0x4, 0xE}; unsigned int hamming = hamming_distance(ua, ub, 2); assert(hamming == 3); @@ -405,7 +409,7 @@ void test_parsing() { void tcp_test_handler(Server *s) { struct sockaddr_storage client_addr; socklen_t client_addr_sz = sizeof(client_addr); - int new_fd = accept(s->fd, (struct sockaddr *)&client_addr, &client_addr_sz); + int new_fd = accept(s->fd, (struct sockaddr *) &client_addr, &client_addr_sz); assert(new_fd != -1); assert(send(new_fd, NET_MSG, 10, 0) != -1); close(new_fd); @@ -422,7 +426,7 @@ void udp_test_handler(Server *s) { socklen_t client_addr_sz = sizeof(client_addr); char recv_buf[128]; - int r = (int)recvfrom(s->fd, recv_buf, 128, 0, (struct sockaddr*)&client_addr, &client_addr_sz); + int r = (int) recvfrom(s->fd, recv_buf, 128, 0, (struct sockaddr *) &client_addr, &client_addr_sz); assert(r > 0); assert(strcmp(recv_buf, NET_MSG) == 0); } @@ -441,7 +445,7 @@ void test_network() { sleep(1); const char *s = capture_system("echo hello | nc localhost 18632", 0); assert(strcmp(s, NET_MSG) == 0); - free((char *)s); + free((char *) s); pthread_join(srv_tid, NULL); printf("Passed TCP test\n");