From 13ccc1b3bd4bf2cc769603331ce74de1ff6bbdd8 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 20 Sep 2024 21:45:51 +0900 Subject: [PATCH] Use stdint.h types instead of int, long, etc. --- src/arg_parse.c | 6 +++--- src/arg_parse.h | 7 +++++-- src/big_endian.c | 4 ++-- src/big_endian.h | 5 ++++- src/config.c | 18 +++++++++--------- src/http.c | 12 ++++++------ src/http.h | 3 ++- src/http_template.c | 7 ++++--- src/main.c | 13 +++++++------ src/tcp_socket.c | 2 +- src/tcp_socket.h | 5 ++++- src/test.c | 5 +++-- 12 files changed, 50 insertions(+), 37 deletions(-) diff --git a/src/arg_parse.c b/src/arg_parse.c index 7472be5..7250e36 100644 --- a/src/arg_parse.c +++ b/src/arg_parse.c @@ -32,7 +32,7 @@ void print_usage(void) { puts(" --enable-reload-config-on-change"); } -Args parse_args(int argc, char **argv) { +Args parse_args(int32_t argc, char **argv) { --argc; ++argv; @@ -44,9 +44,9 @@ Args parse_args(int argc, char **argv) { while (argc > 0) { if ((strcmp(argv[0], "-p") == 0 || strcmp(argv[0], "--port") == 0) && argc > 1) { - int value = atoi(argv[1]); + int32_t value = atoi(argv[1]); if (value >= 0 && value <= 0xFFFF) { - args.port = (unsigned short) value; + args.port = (uint16_t) value; } --argc; ++argv; diff --git a/src/arg_parse.h b/src/arg_parse.h index 5943192..8f56fcd 100644 --- a/src/arg_parse.h +++ b/src/arg_parse.h @@ -17,6 +17,9 @@ #ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_ARG_PARSE_H_ #define SEODISPARATE_COM_C_SIMPLE_HTTP_ARG_PARSE_H_ +// Standard library includes. +#include + // Third party includes. #include @@ -25,8 +28,8 @@ typedef struct Args { // xxxx xxx1 - disable peer addr print. // xxxx xx0x - disable listen on config file for reloading. // xxxx xx1x - enable listen on config file for reloading. - unsigned short flags; - unsigned short port; + uint16_t flags; + uint16_t port; // Does not need to be free'd, this should point to a string in argv. const char *config_file; // Needs to be free'd. diff --git a/src/big_endian.c b/src/big_endian.c index 7056f75..cfab8d7 100644 --- a/src/big_endian.c +++ b/src/big_endian.c @@ -18,14 +18,14 @@ int is_big_endian(void) { union { - int i; + int32_t i; char c[4]; } bint = {0x01020304}; return bint.c[0] == 1 ? 1 : 0; } -unsigned short u16_be_swap(unsigned short value) { +uint16_t u16_be_swap(uint16_t value) { if (is_big_endian()) { return value; } else { diff --git a/src/big_endian.h b/src/big_endian.h index 2cb3c3e..5dfd381 100644 --- a/src/big_endian.h +++ b/src/big_endian.h @@ -17,9 +17,12 @@ #ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_BIG_ENDIAN_H_ #define SEODISPARATE_COM_C_SIMPLE_HTTP_BIG_ENDIAN_H_ +// Standard library includes. +#include + int is_big_endian(void); -unsigned short u16_be_swap(unsigned short value); +uint16_t u16_be_swap(uint16_t value); #endif diff --git a/src/config.c b/src/config.c index 82cfc93..5af1ffe 100644 --- a/src/config.c +++ b/src/config.c @@ -237,7 +237,7 @@ typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredIter { int c_simple_http_required_iter_fn(void *data, void *ud) { C_SIMPLE_HTTP_INTERNAL_RequiredIter *req_iter_struct = ud; - unsigned int data_str_length = (unsigned int)strlen(data) + 1; + uint32_t data_str_length = (uint32_t)strlen(data) + 1; if (simple_archiver_hash_map_get( req_iter_struct->hash_map, data, @@ -267,7 +267,7 @@ typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredCheck { int c_simple_http_check_required_iter_fn(void *path_void_str, void *ud) { C_SIMPLE_HTTP_INTERNAL_RequiredCheck *req = ud; - unsigned int path_void_str_len = (unsigned int)strlen(path_void_str) + 1; + uint32_t path_void_str_len = (uint32_t)strlen(path_void_str) + 1; C_SIMPLE_HTTP_HashMapWrapper *wrapper = simple_archiver_hash_map_get( req->map_of_paths_and_their_vars, @@ -303,8 +303,8 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config( fprintf(stderr, "ERROR: separating_key argument is NULL!\n"); return config; } - const unsigned int separating_key_size = - (unsigned int)strlen(separating_key) + 1; + const uint32_t separating_key_size = + (uint32_t)strlen(separating_key) + 1; config.hash_map = simple_archiver_hash_map_init(); @@ -320,21 +320,21 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config( } unsigned char key_buf[C_SIMPLE_HTTP_CONFIG_BUF_SIZE]; unsigned char value_buf[C_SIMPLE_HTTP_CONFIG_BUF_SIZE]; - unsigned int key_idx = 0; - unsigned int value_idx = 0; + uint32_t key_idx = 0; + uint32_t value_idx = 0; __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) char *current_separating_key_value = NULL; - unsigned int current_separating_key_value_size = 0; + uint32_t current_separating_key_value_size = 0; // xxx0 - reading key // xxx1 - reading value // 00xx - reading value is not quoted // 01xx - reading value is single quoted // 10xx - reading value is double quoted - unsigned int state = 0; + uint32_t state = 0; unsigned char single_quote_count = 0; unsigned char double_quote_count = 0; - int c; + int32_t c; while (feof(f) == 0) { c = fgetc(f); diff --git a/src/http.c b/src/http.c index a3062cc..f79797d 100644 --- a/src/http.c +++ b/src/http.c @@ -55,7 +55,7 @@ const char *c_simple_http_response_code_error_to_response( char *c_simple_http_request_response( const char *request, - unsigned int size, + uint32_t size, const C_SIMPLE_HTTP_HTTPTemplates *templates, size_t *out_size, enum C_SIMPLE_HTTP_ResponseCode *out_response_code) { @@ -63,9 +63,9 @@ char *c_simple_http_request_response( *out_size = 0; } // parse first line. - unsigned int idx = 0; + uint32_t idx = 0; char request_type[REQUEST_TYPE_BUFFER_SIZE] = {0}; - unsigned int request_type_idx = 0; + uint32_t request_type_idx = 0; for (; idx < size && request[idx] != ' ' && request[idx] != '\n' @@ -93,7 +93,7 @@ char *c_simple_http_request_response( ++idx) {} // parse request path. char request_path[REQUEST_PATH_BUFFER_SIZE] = {0}; - unsigned int request_path_idx = 0; + uint32_t request_path_idx = 0; for (; idx < size && request[idx] != ' ' && request[idx] != '\n' @@ -121,7 +121,7 @@ char *c_simple_http_request_response( ++idx) {} // parse request http protocol. char request_proto[REQUEST_PROTO_BUFFER_SIZE] = {0}; - unsigned int request_proto_idx = 0; + uint32_t request_proto_idx = 0; for (; idx < size && request[idx] != ' ' && request[idx] != '\n' @@ -222,7 +222,7 @@ SDArchiverHashMap *c_simple_http_request_to_headers_map( // xxxx xx00 - Beginning of line. // xxxx xx01 - Reached end of header key. // xxxx xx10 - Non-header line. - unsigned int state = 0; + uint32_t state = 0; size_t idx = 0; size_t header_key_idx = 0; __attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) diff --git a/src/http.h b/src/http.h index 766214b..d050b27 100644 --- a/src/http.h +++ b/src/http.h @@ -19,6 +19,7 @@ // Standard library includes. #include +#include // Third party includes. #include @@ -44,7 +45,7 @@ const char *c_simple_http_response_code_error_to_response( /// If the request is not valid, or 404, then the buffer will be NULL. char *c_simple_http_request_response( const char *request, - unsigned int size, + uint32_t size, const C_SIMPLE_HTTP_HTTPTemplates *templates, size_t *out_size, enum C_SIMPLE_HTTP_ResponseCode *out_response_code diff --git a/src/http_template.c b/src/http_template.c index 1192bcb..510ee8b 100644 --- a/src/http_template.c +++ b/src/http_template.c @@ -19,6 +19,7 @@ // Standard library includes. #include #include +#include // Third party includes. #include @@ -104,7 +105,7 @@ char *c_simple_http_path_to_generated( fprintf(stderr, "ERROR: Path string is too large!\n"); return NULL; } - unsigned int path_len = (unsigned int)path_len_size_t; + uint32_t path_len = (uint32_t)path_len_size_t; C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map = simple_archiver_hash_map_get(templates->hash_map, path, path_len); if (!wrapped_hash_map) { @@ -164,7 +165,7 @@ char *c_simple_http_path_to_generated( // xxxx xxx0 - Initial state, no delimeter reached. // xxxx xxx1 - Three left-curly-brace delimeters reached. - unsigned int state = 0; + uint32_t state = 0; for (; idx < html_buf_size; ++idx) { if ((state & 1) == 0) { @@ -216,7 +217,7 @@ char *c_simple_http_path_to_generated( simple_archiver_hash_map_get( wrapped_hash_map->hash_map, var, - (unsigned int)var_size + 1); + (uint32_t)var_size + 1); if (value_c_str) { if (c_simple_http_internal_ends_with_FILE(var) == 0) { // Load from file specified by "value_c_str". diff --git a/src/main.c b/src/main.c index 4b2f276..035c24d 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,7 @@ #include #include #include +#include // Linux/Unix includes. #include @@ -161,9 +162,9 @@ int main(int argc, char **argv) { socklen_t socket_len; // xxxx xxx1 - config needs to be reloaded. - unsigned int flags = 0; + uint32_t flags = 0; size_t config_try_reload_ticks_count = 0; - unsigned int config_try_reload_attempts = 0; + uint32_t config_try_reload_attempts = 0; while (C_SIMPLE_HTTP_KEEP_RUNNING) { nanosleep(&sleep_time, NULL); @@ -307,7 +308,7 @@ int main(int argc, char **argv) { // Received connection, handle it. if ((args.flags & 1) == 0) { printf("Peer connected: addr is "); - for (unsigned int idx = 0; idx < 16; ++idx) { + for (uint32_t idx = 0; idx < 16; ++idx) { if (idx % 2 == 0 && idx > 0) { printf(":"); } @@ -328,7 +329,7 @@ int main(int argc, char **argv) { } #ifndef NDEBUG // DEBUG print received buf. - for (unsigned int idx = 0; + for (uint32_t idx = 0; idx < C_SIMPLE_HTTP_RECV_BUF_SIZE && idx < read_ret; ++idx) { if ((recv_buf[idx] >= 0x20 && recv_buf[idx] <= 0x7E) @@ -355,7 +356,7 @@ int main(int argc, char **argv) { enum C_SIMPLE_HTTP_ResponseCode response_code; const char *response = c_simple_http_request_response( (const char*)recv_buf, - (unsigned int)read_ret, + (uint32_t)read_ret, &parsed_config, &response_size, &response_code); @@ -368,7 +369,7 @@ int main(int argc, char **argv) { size_t content_length_buf_size = 0; memcpy(content_length_buf, "Content-Length: ", 16); content_length_buf_size = 16; - int written = 0; + int32_t written = 0; snprintf( content_length_buf + content_length_buf_size, 127 - content_length_buf_size, diff --git a/src/tcp_socket.c b/src/tcp_socket.c index e8ee1fc..7fc8fa9 100644 --- a/src/tcp_socket.c +++ b/src/tcp_socket.c @@ -30,7 +30,7 @@ // Local includes. #include "big_endian.h" -int create_tcp_socket(unsigned short port) { +int create_tcp_socket(uint16_t port) { struct sockaddr_in6 ipv6_addr; memset(&ipv6_addr, 0, sizeof(struct sockaddr_in6)); ipv6_addr.sin6_family = AF_INET6; diff --git a/src/tcp_socket.h b/src/tcp_socket.h index 4a8680d..607fb64 100644 --- a/src/tcp_socket.h +++ b/src/tcp_socket.h @@ -17,9 +17,12 @@ #ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_TCP_SOCKET_H_ #define SEODISPARATE_COM_C_SIMPLE_HTTP_TCP_SOCKET_H_ +// Standard library includes. +#include + #define C_SIMPLE_HTTP_TCP_SOCKET_BACKLOG 64 -int create_tcp_socket(unsigned short port); +int create_tcp_socket(uint16_t port); void cleanup_tcp_socket(int *tcp_socket); diff --git a/src/test.c b/src/test.c index 3007767..66c4d0e 100644 --- a/src/test.c +++ b/src/test.c @@ -2,6 +2,7 @@ #include #include #include +#include // Local includes. #include "config.h" @@ -12,8 +13,8 @@ #include #include -static int checks_checked = 0; -static int checks_passed = 0; +static int32_t checks_checked = 0; +static int32_t checks_passed = 0; #define RETURN() \ do { \