Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Shivers is a Discord bot made for hobby and educational purposes
* `include`: Header files of everything

## Code
This project follows GNU99 standard and tries not to use external libraries, so includes some libraries that developed to use in this project.
This project follows C23 standard and tries not to use external libraries, so includes some libraries that developed to use in this project.

## License
This project is licensed under [BSD-3-Clause](./LICENSE) license.
2 changes: 0 additions & 2 deletions include/json.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <stdbool.h>

#include <utils.h>

enum JSONType {
Expand Down
32 changes: 15 additions & 17 deletions include/network.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <stdbool.h>

#if defined(_WIN32)
#include <winsock2.h>
#include <windows.h>
Expand All @@ -21,6 +19,10 @@ struct URL {
unsigned short port;
};

typedef enum HTTPStatusCode {
HTTP_NO_CONTENT = 204
} HTTPStatusCode;

struct Header {
char *name, *value;
};
Expand Down Expand Up @@ -78,17 +80,18 @@ void free_url(struct URL url);
char *percent_encode(const char *data);

#if defined(_WIN32)
unsigned long s_read(SSL *ssl, SOCKET sockfd, char *buffer, unsigned long size);
unsigned long s_write(SSL *ssl, SOCKET sockfd, char *buffer, unsigned long size);

void close_socket(SOCKET sockfd, SSL *ssl);
typedef SOCKET socket_t;
#define CLOSE_SOCKET(sockfd) closesocket(sockfd)
#elif defined(__linux__)
unsigned long s_read(SSL *ssl, int sockfd, char *buffer, unsigned long size);
unsigned long s_write(SSL *ssl, int sockfd, char *buffer, unsigned long size);

void close_socket(int sockfd, SSL *ssl);
typedef int socket_t;
#define CLOSE_SOCKET(sockfd) close(sockfd)
#endif

unsigned long s_read(SSL *ssl, socket_t sockfd, char *buffer, unsigned long size);
unsigned long s_write(SSL *ssl, socket_t sockfd, char *buffer, unsigned long size);

void close_socket(socket_t sockfd, SSL *ssl);

unsigned long combine_bytes(unsigned char *bytes, unsigned long byte_count);
struct Header get_header(struct Header *headers, const unsigned int header_size, const char *name);
struct hostent *resolve_hostname(char *hostname);
Expand Down Expand Up @@ -118,12 +121,7 @@ struct WebsocketMethods {
};

struct Websocket {
#if defined(_WIN32)
SOCKET sockfd;
#elif defined(__linux__)
int sockfd;
#endif

socket_t sockfd;
fd_set readfds, writefds;
struct timeval tv;
SSL *ssl;
Expand All @@ -139,4 +137,4 @@ struct Websocket {
struct Websocket create_websocket(const char *url, const struct WebsocketMethods methods);
void connect_websocket(struct Websocket *websocket);
void close_websocket(struct Websocket *websocket, const short code, const char *reason);
void send_websocket_message(struct Websocket *websocket, const char *message);
int send_websocket_message(struct Websocket *websocket, const char *message);
2 changes: 2 additions & 0 deletions include/shivers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <png.h> // IWYU pragma: export
#include <utils.h> // IWYU pragma: export

#include <assert.h> // IWYU pragma: export
#include <ctype.h> // IWYU pragma: export
#include <errno.h> // IWYU pragma: export
#include <math.h> // IWYU pragma: export
Expand All @@ -33,6 +34,7 @@
#include <zlib.h> // IWYU pragma: export

#if defined(__WIN32__)
#include <psapi.h> // IWYU pragma: export
#include <sys/timeb.h> // IWYU pragma: export
#include <winsock2.h> // IWYU pragma: export
#include <windows.h> // IWYU pragma: export
Expand Down
45 changes: 35 additions & 10 deletions include/utils.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
#pragma once

#include <stdbool.h>

#include <sys/time.h>
#include <stdint.h>

// To guarantee code execution
#define ASSERT(actual, op, expected) ({ \
typeof(actual) __actual_val = (actual); \
typeof(expected) __expected_val = (expected); \
assert(__actual_val op __expected_val); \
__actual_val; \
})

#define SPRINTF_S(__s, __format, ...) ASSERT(sprintf((__s), (__format), ##__VA_ARGS__), >, 0)

#if defined(__linux__)
#define SLEEP(ms) do { \
struct timespec req; \
req.tv_sec = ms / 1000; \
req.tv_nsec = (ms % 1000) * 1000000L; \
} while (0)
#elif defined(__WIN32)
#define SLEEP(ms) Sleep(ms)
#endif

struct SplitData {
char *data;
Expand All @@ -24,32 +42,39 @@ struct Split {
unsigned int size;
};

struct Join {
typedef struct Join {
char *data;
unsigned int length;
};
} Join;

struct Split split(const char *text, const unsigned int length, const char *separator);
void split_free(struct Split value);

void *allocate(void *value, const long old_count, const unsigned long new_count, const unsigned char size);

unsigned long join(const struct Join *value, char *source, unsigned short size, const char *separator);
unsigned long calculate_join(const struct Join *value, unsigned short size, const char *separator);
uint64_t join(const Join *value, char *source, uint16_t size, const char *separator);
uint64_t calculate_join(const Join *value, uint16_t size, const char *separator);

void strtolower(char *source, const char *dest);
void strtoupper(char *source, const char *dest);
bool streq(const char *str1, const char *str2);
void strreplace(char **source, char *target, char *replacement);
char *ltrim(const char *src);

unsigned long ahtoi(const char *data);
int atoi_s(const char *str, short length);
// Converts hex string to integer, -1 on failure
int64_t ahtoi(const char *data);

// Converts string to integer safely -1 on failure
int64_t atoi_s(const char *str, int16_t length);


char *base64_encode(const char *data, const unsigned long data_length);
#define BASE64_ENCODE_ERROR ("base64_encode(): cannot decode key")

int char_at(char *data, const char ch);

unsigned long long get_timestamp();
int64_t get_timestamp();
#define GET_TIMESTAMP_ERROR ("get_timestamp(): Cannot get current time")

void throw(const char *format, ...);

Expand Down
4 changes: 2 additions & 2 deletions libs/database/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ void database_push(char *key, void *value, const enum JSONType type) {

if (array.exist) {
char json_key[(array.element->size / 10) + 4];
sprintf(json_key, "[%u]", array.element->size);
SPRINTF_S(json_key, "[%u]", array.element->size);

json_set_val(array.element, json_key, value, type);
} else {
char json_key[5 + strlen(key)];
sprintf(json_key, "%s.[0]", key);
SPRINTF_S(json_key, "%s.[0]", key);

json_set_val(data, json_key, value, type);
}
Expand Down
Loading