1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2026-03-02 04:09:18 +00:00

refactor: changed directory structure

This commit is contained in:
Falk Werner
2020-06-21 21:18:43 +02:00
parent 528a8ef501
commit 73f15b3790
43 changed files with 63 additions and 63 deletions

View File

@@ -0,0 +1,183 @@
#include "webfuse_provider/impl/util/base64.h"
static const uint8_t wfp_impl_base64_decode_table[256] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 1
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 62, 0x80, 0x80, 0x80, 63, // 2
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0x80, 0x80, 0x80, 0, 0x80, 0x80, // 3
0x80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 4
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0x80, 0x80, 0x80, 0x80, 0x80, // 5
0x80, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 6
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0x80, 0x80, 0x80, 0x80, 0x80, // 7
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 8
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 9
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // A
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // B
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // C
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // D
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // E
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // F
};
size_t wfp_impl_base64_encoded_size(size_t length)
{
return ((length + 2) / 3) * 4;
}
size_t wfp_impl_base64_encode(
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size)
{
// 0 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890123
static char const table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t const length_needed = wfp_impl_base64_encoded_size(length);
if (buffer_size < length_needed)
{
return 0;
}
size_t pos = 0;
size_t out_pos = 0;
for(; (length - pos) >= 3; pos += 3)
{
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) | (data[pos + 2] >> 6) ];
buffer[out_pos++] = table[ data[pos + 2] & 0x3f ];
}
switch((length - pos))
{
case 1:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos] & 0x03) << 4) ];
buffer[out_pos++] = '=';
buffer[out_pos++] = '=';
break;
case 2:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) ];
buffer[out_pos++] = '=';
break;
default:
break;
}
if (buffer_size > out_pos)
{
buffer[out_pos] = '\0';
}
return out_pos;
}
size_t wfp_impl_base64_decoded_size(char const * data, size_t length)
{
size_t result = 0;
if ((length > 0) && ((length % 4) == 0))
{
result = (length / 4) * 3;
if ('=' == data[length - 1])
{
result--;
if ('=' == data[length - 2])
{
result--;
}
}
}
return result;
}
size_t wfp_impl_base64_decode(
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size)
{
uint8_t const * table = wfp_impl_base64_decode_table;
size_t needed_size = wfp_impl_base64_decoded_size(data, length);
if ((0 == needed_size) || (buffer_size < needed_size))
{
return 0;
}
size_t out_pos = 0;
size_t pos = 0;
for(; pos < length - 4; pos += 4)
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
buffer[out_pos++] = (b << 4) | (c >> 2);
buffer[out_pos++] = (c << 6) | d;
}
// decode last block
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
if ('=' != data[pos + 2])
{
buffer[out_pos++] = (b << 4) | (c >> 2);
if ('=' != data[pos + 3])
{
buffer[out_pos++] = (c << 6) | d;
}
}
}
return out_pos;
}
extern bool wfp_impl_base64_isvalid(char const * data, size_t length)
{
uint8_t const * table = wfp_impl_base64_decode_table;
if ((length == 0) || ((length % 4) != 0))
{
return false;
}
size_t pos = 0;
for(; pos < (length - 2); pos++)
{
unsigned char c = (unsigned char) data[pos];
if (('=' == c) || (0x80 == table[c]))
{
return false;
}
}
if (('=' == data[pos]) && ('=' != data[pos + 1]))
{
return false;
}
for(;pos < length; pos++)
{
char c = data[pos];
if (0x80 == table[ (unsigned char) c])
{
return false;
}
}
return true;
}

View File

@@ -0,0 +1,40 @@
#ifndef WFP_UTIL_BASE64_H
#define WFP_UTILBASE64_H
#ifndef __cplusplus
#include <inttypes.h>
#include <stddef.h>
#include <stdbool.h>
#else
#include <cinttypes>
#include <cstddef>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
extern size_t wfp_impl_base64_encoded_size(size_t length);
extern size_t wfp_impl_base64_encode(
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size);
extern size_t wfp_impl_base64_decoded_size(char const * data, size_t length);
extern size_t wfp_impl_base64_decode(
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size);
extern bool wfp_impl_base64_isvalid(char const * data, size_t length);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,21 @@
#ifndef WFP_UTIL_CONTAINER_OF_H
#define WFP_UTIL_CONTAINER_OF_H
#ifndef __cplusplus
#include <stddef.h>
#else
#include <cstddef>
#endif
#ifdef __GNUC__
#define wfp_container_of(pointer, type, member) \
({ \
const typeof( ((type *)0)->member ) * __member = (pointer); \
(type *)( (char *)__member - offsetof(type, member) ); \
})
#else
#define wfp_container_of(pointer, type, member) \
(type *) (((char *) pointer) - offsetof(type, member))
#endif
#endif

View File

@@ -0,0 +1,27 @@
#include "webfuse_provider/impl/util/json_util.h"
int wfp_impl_json_get_int(json_t const * object, char const * key, int default_value)
{
int result = default_value;
json_t * holder = json_object_get(object, key);
if ((NULL != holder) && (json_is_integer(holder)))
{
result = json_integer_value(holder);
}
return result;
}
wfp_status
wfp_impl_jsonrpc_get_status(
json_t const * error)
{
wfp_status status = WFP_GOOD;
if (NULL != error)
{
status = wfp_impl_json_get_int(error, "code", WFP_BAD_FORMAT);
}
return status;
}

View File

@@ -0,0 +1,26 @@
#ifndef WFP_JSON_UTIL_H
#define WFP_JSON_UTIL_H
#include <jansson.h>
#include "webfuse_provider/status.h"
#ifdef __cplusplus
extern "C"
{
#endif
extern int
wfp_impl_json_get_int(
json_t const * object,
char const * key,
int default_value);
extern wfp_status
wfp_impl_jsonrpc_get_status(
json_t const * error);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,18 @@
#include "webfuse_provider/impl/util/lws_log.h"
#include <stdbool.h>
#include <libwebsockets.h>
#define WFP_LWSLOG_DISABLE 0
static bool wfp_impl_lwslog_is_diabled = false;
void wfp_impl_lwslog_disable(void)
{
if (!wfp_impl_lwslog_is_diabled)
{
lws_set_log_level(WFP_LWSLOG_DISABLE, NULL);
wfp_impl_lwslog_is_diabled = true;
}
}

View File

@@ -0,0 +1,15 @@
#ifndef WFP_LWS_LOG_H
#define WFP_LWS_LOG_H
#ifdef __cplusplus
extern "C"
{
#endif
extern void wfp_impl_lwslog_disable(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,56 @@
#include "webfuse_provider/impl/util/slist.h"
#include <stddef.h>
void wfp_slist_init(
struct wfp_slist * list)
{
list->head.next = NULL;
list->last = &list->head;
}
bool wfp_slist_empty(
struct wfp_slist * list)
{
return (list->last == &list->head);
}
struct wfp_slist_item * wfp_slist_first(
struct wfp_slist * list)
{
return list->head.next;
}
void wfp_slist_append(
struct wfp_slist * list,
struct wfp_slist_item * item)
{
item->next = NULL;
list->last->next = item;
list->last = item;
}
struct wfp_slist_item * wfp_slist_remove_first(
struct wfp_slist * list)
{
return wfp_slist_remove_after(list, &list->head);
}
struct wfp_slist_item * wfp_slist_remove_after(
struct wfp_slist * list,
struct wfp_slist_item * prev)
{
struct wfp_slist_item * result = prev->next;
if (NULL != result)
{
prev->next = result->next;
if (list->last == result)
{
list->last = prev;
}
}
return result;
}

View File

@@ -0,0 +1,48 @@
#ifndef WFP_UTIL_SLIST_H
#define WFP_UTIL_SLIST_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_slist_item
{
struct wfp_slist_item * next;
};
struct wfp_slist
{
struct wfp_slist_item head;
struct wfp_slist_item * last;
};
extern void wfp_slist_init(
struct wfp_slist * list);
extern bool wfp_slist_empty(
struct wfp_slist * list);
extern struct wfp_slist_item * wfp_slist_first(
struct wfp_slist * list);
extern void wfp_slist_append(
struct wfp_slist * list,
struct wfp_slist_item * item);
extern struct wfp_slist_item * wfp_slist_remove_first(
struct wfp_slist * list);
extern struct wfp_slist_item * wfp_slist_remove_after(
struct wfp_slist * list,
struct wfp_slist_item * prev);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,125 @@
#include "webfuse_provider/impl/util/url.h"
#include <stdlib.h>
#include <string.h>
struct wfp_url_protocol
{
char const * name;
size_t name_length;
int default_port;
bool use_tls;
};
static bool wfp_url_readprotocol(
struct wfp_url * url,
char const * * data)
{
static struct wfp_url_protocol const known_protocols[] =
{
{"ws://", 5, 80, false},
{"wss://", 6, 443, true}
};
static size_t const count = (sizeof(known_protocols) / sizeof(known_protocols[0]));
bool found = false;
for(size_t i = 0; (!found) && (i < count); i++)
{
struct wfp_url_protocol const * protocol = &known_protocols[i];
if (0 == strncmp(*data, protocol->name, protocol->name_length))
{
url->port = protocol->default_port;
url->use_tls = protocol->use_tls;
*data = *data + protocol->name_length;
found = true;
}
}
return found;
}
static bool wfp_url_readhost(
struct wfp_url * url,
char const * * data)
{
char * end = strpbrk(*data, ":/");
bool const result = (NULL != end);
if (result)
{
size_t length = end - *data;
url->host = strndup(*data, length);
*data = end;
}
return result;
}
static bool wfp_url_readport(
struct wfp_url * url,
char const * * data)
{
bool result;
if (':' == **data)
{
*data = *data + 1;
char * end = strchr(*data, '/');
result = (NULL != end);
if (result)
{
url->port = atoi(*data);
*data = end;
}
}
else
{
result = ('/' == **data);
}
return result;
}
static bool wfp_url_readpath(
struct wfp_url * url,
char const * * data)
{
bool const result = ('/' == **data);
url->path = strdup(*data);
*data = NULL;
return result;
}
bool wfp_url_init(
struct wfp_url * url,
char const * value)
{
memset(url, 0, sizeof(struct wfp_url));
char const * data = value;
bool const result =
wfp_url_readprotocol(url, &data) &&
wfp_url_readhost(url, &data) &&
wfp_url_readport(url, &data) &&
wfp_url_readpath(url, &data)
;
if (!result)
{
wfp_url_cleanup(url);
}
return result;
}
void wfp_url_cleanup(
struct wfp_url * url)
{
free(url->host);
free(url->path);
memset(url, 0, sizeof(struct wfp_url));
}

View File

@@ -0,0 +1,32 @@
#ifndef WFP_UTIL_URL_H
#define WFP_UTIL_URL_H
#ifndef __cplusplus
#include <stdbool.h>
#endif
#ifdef __cplusplus
extern "C"
{
#endif
struct wfp_url
{
char * host;
int port;
char * path;
bool use_tls;
};
extern bool wfp_url_init(
struct wfp_url * url,
char const * value);
extern void wfp_url_cleanup(
struct wfp_url * url);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,10 @@
#ifndef WFP_UTIL_H
#define WFP_UTIL_H
#ifdef __GNUC__
#define WFP_UNUSED_PARAM(param) param __attribute__((unused))
#else
#define WFP_UNUSED_PARAM(param)
#endif
#endif