This commit is contained in:
snarmph 2025-09-28 16:31:22 +02:00
parent 524ec0d1ce
commit 61c1060a98
16 changed files with 5043 additions and 31 deletions

64
colla.c
View file

@ -34,13 +34,6 @@
colla_modules_e colla__initialised_modules = 0;
extern void os_init(void);
extern void os_cleanup(void);
#if !COLLA_NO_NET
extern void net_init(void);
extern void net_cleanup(void);
#endif
static char *colla_fmt__stb_callback(const char *buf, void *ud, int len) {
// TODO maybe use os_write?
fflush(stdout);
@ -154,9 +147,9 @@ tstr_t tstr_init(TCHAR *str, usize optional_len) {
};
}
str16_t str16_init(u16 *str, usize optional_len) {
str16_t str16_init(char16_t *str, usize optional_len) {
if (str && !optional_len) {
optional_len = wcslen(str);
optional_len = str16_len(str);
}
return (str16_t){
.buf = str,
@ -173,6 +166,19 @@ str_t str_from_str16(arena_t *arena, str16_t src) {
return out;
}
usize str16_len(char16_t *str) {
#if COLLA_WIN
return wcslen(str);
#else
usize len = 0;
while (*str) {
str++;
len++;
}
return len;
#endif
}
str_t str_from_tstr(arena_t *arena, tstr_t src) {
#if COLLA_UNICODE
return str_from_str16(arena, src);
@ -1057,14 +1063,13 @@ static void *arena__alloc_common(const arena_alloc_desc_t *desc) {
usize new_cur = allocated + total;
if (new_cur > page_end) {
usize extra_mem = os_pad_to_page(new_cur - page_end);
usize page_size = os_get_system_info().page_size;
// TODO is this really correct?
usize num_of_pages = (extra_mem / page_size) + 1;
usize prev_page = os_pad_to_page(allocated - page_size);
usize next_page = os_pad_to_page(new_cur);
usize num_of_pages = (next_page - prev_page) / page_size;
colla_assert(num_of_pages > 0);
if (!os_commit(arena->cur, num_of_pages + 1)) {
if (!os_commit(arena->beg + prev_page, num_of_pages)) {
if (!soft_fail) {
fatal("failed to commit memory for virtual arena, tried to commit %zu pages\n", num_of_pages);
}
@ -1214,7 +1219,7 @@ usize os_file_write_buf(oshandle_t handle, buffer_t buf) {
}
buffer_t os_file_read_all(arena_t *arena, strview_t path) {
oshandle_t fp = os_file_open(path, FILEMODE_READ);
oshandle_t fp = os_file_open(path, OS_FILE_READ);
if (!os_handle_valid(fp)) {
err("could not open file: %v", path);
return (buffer_t){0};
@ -1242,7 +1247,7 @@ buffer_t os_file_read_all_fp(arena_t *arena, oshandle_t handle) {
}
str_t os_file_read_all_str(arena_t *arena, strview_t path) {
oshandle_t fp = os_file_open(path, FILEMODE_READ);
oshandle_t fp = os_file_open(path, OS_FILE_READ);
if (!os_handle_valid(fp)) {
err("could not open file %v: %v", path, os_get_error_string(os_get_last_error()));
return STR_EMPTY;
@ -1273,7 +1278,7 @@ str_t os_file_read_all_str_fp(arena_t *arena, oshandle_t handle) {
}
bool os_file_write_all(strview_t name, buffer_t buffer) {
oshandle_t fp = os_file_open(name, FILEMODE_WRITE);
oshandle_t fp = os_file_open(name, OS_FILE_WRITE);
bool result = os_file_write_all_fp(fp, buffer);
os_file_close(fp);
return result;
@ -1284,7 +1289,7 @@ bool os_file_write_all_fp(oshandle_t handle, buffer_t buffer) {
}
bool os_file_write_all_str(strview_t name, strview_t data) {
oshandle_t fp = os_file_open(name, FILEMODE_WRITE);
oshandle_t fp = os_file_open(name, OS_FILE_WRITE);
bool result = os_file_write_all_str_fp(fp, data);
os_file_close(fp);
return result;
@ -1295,7 +1300,7 @@ bool os_file_write_all_str_fp(oshandle_t handle, strview_t data) {
}
u64 os_file_time(strview_t path) {
oshandle_t fp = os_file_open(path, FILEMODE_READ);
oshandle_t fp = os_file_open(path, OS_FILE_READ);
u64 result = os_file_time_fp(fp);
os_file_close(fp);
return result;
@ -1334,7 +1339,7 @@ usize os_pad_to_page(usize byte_count) {
void ini__parse(arena_t *arena, ini_t *ini, const iniopt_t *options);
ini_t ini_parse(arena_t *arena, strview_t filename, iniopt_t *opt) {
oshandle_t fp = os_file_open(filename, FILEMODE_READ);
oshandle_t fp = os_file_open(filename, OS_FILE_READ);
ini_t out = ini_parse_fp(arena, fp, opt);
os_file_close(fp);
return out;
@ -2735,6 +2740,13 @@ http_url_t http_split_url(strview_t url) {
}
#if !COLLA_NO_NET
// HTTP /////////////////////////////
http_res_t http_request(http_request_desc_t *req) {
return http_request_cb(req, NULL, NULL);
}
// WEBSOCKETS ///////////////////////
#define WEBSOCKET_MAGIC "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
@ -2772,17 +2784,17 @@ buffer_t websocket_encode(arena_t *arena, strview_t message) {
if (message.len > UINT16_MAX) extra += sizeof(u64);
else if (message.len > UINT8_MAX) extra += sizeof(u16);
u8 *bytes = alloc(arena, u8, message.len + extra);
bytes[0] = 0b10000001;
bytes[1] = 0b10000000;
bytes[0] = 0x81; // 0b10000001
bytes[1] = 0x80; // 0b10000000;
int offset = 2;
if (message.len > UINT16_MAX) {
bytes[1] |= 0b01111111;
bytes[1] |= 0x7F; // 0b01111111;
u64 len = htonll(message.len);
memmove(bytes + 2, &len, sizeof(len));
offset += sizeof(u64);
}
else if (message.len > UINT8_MAX) {
bytes[1] |= 0b01111110;
bytes[1] |= 0x7E; // 0b01111110;
u16 len = htons((u16)message.len);
memmove(bytes + 2, &len, sizeof(len));
offset += sizeof(u16);
@ -2803,9 +2815,9 @@ str_t websocket_decode(arena_t *arena, buffer_t message) {
str_t out = STR_EMPTY;
u8 *bytes = message.data;
bool mask = bytes[1] & 0b10000000;
bool mask = bytes[1] & 0x80; // 0b10000000;
int offset = 2;
u64 msglen = bytes[1] & 0b01111111;
u64 msglen = bytes[1] & 0x7F; // 0b01111111;
// 16bit msg len
if (msglen == 126) {