]> git.seodisparate.com - c_simple_http/commitdiff
Use stdint.h types instead of int, long, etc.
authorStephen Seo <seo.disparate@gmail.com>
Fri, 20 Sep 2024 12:45:51 +0000 (21:45 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Fri, 20 Sep 2024 12:45:51 +0000 (21:45 +0900)
12 files changed:
src/arg_parse.c
src/arg_parse.h
src/big_endian.c
src/big_endian.h
src/config.c
src/http.c
src/http.h
src/http_template.c
src/main.c
src/tcp_socket.c
src/tcp_socket.h
src/test.c

index 7472be5c0da06e87eba06a98b178d9f420806903..7250e36d8c6731a7efb0c77e8d867a0116d61aec 100644 (file)
@@ -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;
index 5943192dd85ca83b39c7733d4d61d45536896fe4..8f56fcd652a98a42151e47e10e1dacaee842e04a 100644 (file)
@@ -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 <stdint.h>
+
 // Third party includes.
 #include <SimpleArchiver/src/data_structures/linked_list.h>
 
@@ -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.
index 7056f75c9b0eae687f5cb1c1137aaedf4a42593c..cfab8d72f38cbdbfe3d1991d3288992d20c6a4ce 100644 (file)
 
 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 {
index 2cb3c3e93c2aaa224f0a389f2bcae49b7101c8ec..5dfd3818437aa35f49b3f1f627f70118d026bb54 100644 (file)
 #ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_BIG_ENDIAN_H_
 #define SEODISPARATE_COM_C_SIMPLE_HTTP_BIG_ENDIAN_H_
 
+// Standard library includes.
+#include <stdint.h>
+
 int is_big_endian(void);
 
-unsigned short u16_be_swap(unsigned short value);
+uint16_t u16_be_swap(uint16_t value);
 
 #endif
 
index 82cfc9332b28076471d01f6518d4192295ba9614..5af1ffe644450d9daba9fbf807b5d627e8327e3d 100644 (file)
@@ -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);
index a3062cc4ed67220e8b68a5dce7a0853c20d540e7..f79797d942e1012d91ccb647eb7b9f167fd7409f 100644 (file)
@@ -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)))
index 766214b2516872a30200a8734bc6333455ad8911..d050b27bd5768bc16d982693e60b19a2a33575df 100644 (file)
@@ -19,6 +19,7 @@
 
 // Standard library includes.
 #include <stddef.h>
+#include <stdint.h>
 
 // Third party includes.
 #include <SimpleArchiver/src/data_structures/hash_map.h>
@@ -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
index 1192bcbc73e2962e10e899a0282682c80d35fdad..510ee8b83bed144dbc711322d77a5aaf32d2550a 100644 (file)
@@ -19,6 +19,7 @@
 // Standard library includes.
 #include <string.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 // Third party includes.
 #include <SimpleArchiver/src/data_structures/linked_list.h>
@@ -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".
index 4b2f2761288b67e20d572e47a6ae848471ff7c61..035c24dc2247933409fc5a8610618bf9762be4dd 100644 (file)
@@ -18,6 +18,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdint.h>
 
 // Linux/Unix includes.
 #include <sys/socket.h>
@@ -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,
index e8ee1fc78557a76768f300330aa54743b6a02af6..7fc8fa90e801df79e4d904b08231a86bd01178e5 100644 (file)
@@ -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;
index 4a8680d8002515767b0990fb7e3011ded45356ce..607fb64e971c342a64a762e7c76617890ac8274a 100644 (file)
 #ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_TCP_SOCKET_H_
 #define SEODISPARATE_COM_C_SIMPLE_HTTP_TCP_SOCKET_H_
 
+// Standard library includes.
+#include <stdint.h>
+
 #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);
 
index 300776739c8049c93648ef842ff4a4b2dc7267c1..66c4d0e0dc6e6d4392dc164122af8da379217216 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <stdint.h>
 
 // Local includes.
 #include "config.h"
@@ -12,8 +13,8 @@
 #include <SimpleArchiver/src/helpers.h>
 #include <SimpleArchiver/src/data_structures/hash_map.h>
 
-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 { \