diff options
Diffstat (limited to 'src/include')
70 files changed, 3315 insertions, 0 deletions
diff --git a/src/include/skalibs/alloc.h b/src/include/skalibs/alloc.h new file mode 100644 index 0000000..707d5c1 --- /dev/null +++ b/src/include/skalibs/alloc.h @@ -0,0 +1,16 @@ +/* ISC license. */ + +#ifndef ALLOC_H +#define ALLOC_H + +#include <skalibs/gccattributes.h> + +typedef char aligned_char gccattr_aligned ; +typedef aligned_char *aligned_char_ref, **aligned_char_ref_ref ; + +extern aligned_char_ref alloc (unsigned int) ; +extern void alloc_free (void *) ; +#define alloc_re(p, old, new) alloc_realloc(p, new) +extern int alloc_realloc (aligned_char_ref_ref, unsigned int) ; + +#endif diff --git a/src/include/skalibs/allreadwrite.h b/src/include/skalibs/allreadwrite.h new file mode 100644 index 0000000..6b2a0cf --- /dev/null +++ b/src/include/skalibs/allreadwrite.h @@ -0,0 +1,27 @@ +/* ISC license. */ + +#ifndef ALLREADWRITE_H +#define ALLREADWRITE_H + +#include <sys/types.h> +#include <sys/uio.h> +#include <skalibs/functypes.h> + +extern int sanitize_read (int) ; +extern int unsanitize_read (int) ; + +extern unsigned int allreadwrite (iofunc_t_ref, int, char *, unsigned int) ; + +extern int fd_read (int, char *, unsigned int) ; +extern int fd_write (int, char const *, unsigned int) ; + +extern int fd_recv (int, char *, unsigned int, unsigned int) ; +extern int fd_send (int, char const *, unsigned int, unsigned int) ; + +extern unsigned int allread (int, char *, unsigned int) ; +extern unsigned int allwrite (int, char const *, unsigned int) ; + +extern int fd_readv (int, struct iovec const *, unsigned int) ; +extern int fd_writev (int, struct iovec const *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/avlnode.h b/src/include/skalibs/avlnode.h new file mode 100644 index 0000000..4d13d15 --- /dev/null +++ b/src/include/skalibs/avlnode.h @@ -0,0 +1,44 @@ +/* ISC license. */ + +#ifndef AVLNODE_H +#define AVLNODE_H + +#include <skalibs/gccattributes.h> +#include <skalibs/functypes.h> + + +#define AVLNODE_MAXDEPTH 49 /* enough for 2^32 nodes in the worst case */ + +typedef int avliterfunc_t (unsigned int, unsigned int, void *) ; +typedef avliterfunc_t *avliterfunc_t_ref ; + +typedef struct avlnode_s avlnode, *avlnode_ref ; +struct avlnode_s +{ + unsigned int data ; + unsigned int child[2] ; + signed char balance : 2 ; +} ; + +#define AVLNODE_ZERO { .data = 0, .child = { (unsigned int)-1, (unsigned int)-1 }, .balance = 0 } +extern avlnode const avlnode_zero ; + +extern unsigned int avlnode_searchnode (avlnode const *, unsigned int, unsigned int, void const *, dtokfunc_t_ref, cmpfunc_t_ref, void *) ; +extern int avlnode_search (avlnode const *, unsigned int, unsigned int, void const *, unsigned int *, dtokfunc_t_ref, cmpfunc_t_ref, void *) ; +extern unsigned int avlnode_height (avlnode const *, unsigned int, unsigned int) gccattr_pure ; + +extern unsigned int avlnode_extremenode (avlnode const *, unsigned int, unsigned int, int) gccattr_pure ; +#define avlnode_minnode(s, max, r) avlnode_extremenode(s, max, (r), 0) +#define avlnode_maxnode(s, max, r) avlnode_extremenode(s, max, (r), 1) + +extern int avlnode_extreme (avlnode const *, unsigned int, unsigned int, int, unsigned int *) ; +#define avlnode_min(s, max, r, data) avlnode_extreme(s, max, (r), 0, data) +#define avlnode_max(s, max, r, data) avlnode_extreme(s, max, (r), 1, data) + +extern unsigned int avlnode_insertnode (avlnode_ref, unsigned int, unsigned int, unsigned int, dtokfunc_t_ref, cmpfunc_t_ref, void *) ; +extern unsigned int avlnode_delete (avlnode_ref, unsigned int, unsigned int *, void const *, dtokfunc_t_ref, cmpfunc_t_ref, void *) ; +#define avlnode_deletenode(s, max, r, i, dtok, f, p) avlnode_delete(s, max, r, (*(dtok))((s)[i].data), dtok, f, p) + +extern int avlnode_iter (avlnode_ref, unsigned int, unsigned int, avliterfunc_t_ref, void *) ; + +#endif diff --git a/src/include/skalibs/avltree.h b/src/include/skalibs/avltree.h new file mode 100644 index 0000000..cefd116 --- /dev/null +++ b/src/include/skalibs/avltree.h @@ -0,0 +1,55 @@ +/* ISC license. */ + +#ifndef AVLTREE_H +#define AVLTREE_H + +#include <skalibs/functypes.h> +#include <skalibs/gensetdyn.h> +#include <skalibs/avlnode.h> + +typedef struct avltree_s avltree, *avltree_ref ; +struct avltree_s +{ + gensetdyn x ; + unsigned int root ; + dtokfunc_t_ref dtok ; + cmpfunc_t_ref kcmp ; + void *external ; +} ; + +#define AVLTREE_ZERO { .x = GENSETDYN_ZERO, .root = (unsigned int)-1, .dtok = 0, .kcmp = 0, .external = 0 } +extern avltree const avltree_zero ; +#define avltree_len(t) gensetdyn_n(&(t)->x) +#define avltree_totalsize(t) ((t)->x.storage.len) +#define avltree_nodes(t) ((avlnode_ref)(t)->x.storage.s) +#define avltree_data(t, i) (avltree_nodes(t)[i].data) +#define avltree_root(t) ((t)->root) +#define avltree_setroot(t, r) ((t)->root = (r)) + +extern void avltree_free (avltree_ref) ; +extern void avltree_init (avltree_ref, unsigned int, unsigned int, unsigned int, dtokfunc_t_ref, cmpfunc_t_ref, void *) ; +#define AVLTREE_INIT(b, num, den, dtk, f, p) { .x = GENSETDYN_INIT(avlnode, (b), num, den), .root = (unsigned int)-1, .dtok = (dtk), .kcmp = (f), .external = (p) } + +#define avltree_searchnode(t, k) avlnode_searchnode(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), k, (t)->dtok, (t)->kcmp, (t)->external) +#define avltree_search(t, k, data) avlnode_search(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), k, (data), (t)->dtok, (t)->kcmp, (t)->external) + +#define avltree_height(t) avlnode_height(avltree_nodes(t), avltree_totalsize(t), avltree_root(t)) + +#define avltree_extremenode(t, h) avlnode_extremenode(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), h) +#define avltree_minnode(t) avltree_extremenode((t), 0) +#define avltree_maxnode(t) avltree_extremenode((t), 1) + +#define avltree_extreme(t, h, data) avlnode_extreme(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), (h), data) +#define avltree_min(t, data) avltree_extreme((t), 0, data) +#define avltree_max(t, data) avltree_extreme((t), 1, data) + +extern int avltree_newnode (avltree_ref, unsigned int, unsigned int *) ; +#define avltree_insertnode(t, i) avltree_setroot(t, avlnode_insertnode(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), i, (t)->dtok, (t)->kcmp, (t)->external)) +extern int avltree_insert (avltree_ref, unsigned int) ; + +#define avltree_deletenode(t, i) avltree_delete(t, (*(t)->dtok)(avltree_data(t, i))) +extern int avltree_delete (avltree_ref, void const *) ; + +#define avltree_iter(t, f, p) avlnode_iter(avltree_nodes(t), avltree_totalsize(t), avltree_root(t), f, p) + +#endif diff --git a/src/include/skalibs/avltreen.h b/src/include/skalibs/avltreen.h new file mode 100644 index 0000000..8151e9d --- /dev/null +++ b/src/include/skalibs/avltreen.h @@ -0,0 +1,87 @@ +/* ISC license. */ + +#ifndef AVLTREEN_H +#define AVLTREEN_H + +#include <skalibs/functypes.h> +#include <skalibs/genset.h> +#include <skalibs/avlnode.h> + + + /* avltreen: just the structure. Storage and freelist are outside. */ + +typedef struct avltreen_s avltreen, *avltreen_ref ; +struct avltreen_s +{ + genset x ; + unsigned int root ; + dtokfunc_t_ref dtok ; + cmpfunc_t_ref kcmp ; + void *external ; +} ; + +#define AVLTREEN_ZERO { .x = GENSET_ZERO, .root = (unsigned int)-1, .dtok = 0, .kcmp = 0, .external = 0 } +#define avltreen_totalsize(t) ((t)->x.max) +#define avltreen_len(t) genset_n(&(t)->x) +#define avltreen_nodes(t) ((avlnode_ref)(t)->x.storage) +#define avltreen_data(t, i) (avltreen_nodes(t)[i].data) +#define avltreen_root(t) ((t)->root) +#define avltreen_setroot(t, r) ((t)->root = (r)) +extern void avltreen_init (avltreen_ref, avlnode_ref, unsigned int *, unsigned int, dtokfunc_t_ref, cmpfunc_t_ref, void *) ; + +#define avltreen_searchnode(t, k) avlnode_searchnode(avltreen_nodes(t), avltreen_totalsize(t), avltreen_root(t), (k), (t)->dtok, (t)->kcmp, (t)->external) +#define avltreen_search(t, k, data) avlnode_search(avltreen_nodes(t), avltreen_totalsize(t), avltreen_root(t), k, (data), (t)->dtok, (t)->kcmp, (t)->external) + +#define avltreen_height(t) avlnode_height(avltreen_nodes(t), avltreen_totalsize(t), avltreen_root(t)) + +#define avltreen_extremenode(t, h) avlnode_extremenode(avltreen_nodes(t), avltreen_totalsize(t), avltreen_root(t), h) +#define avltreen_minnode(t) avltreen_extremenode((t), 0) +#define avltreen_maxnode(t) avltreen_extremenode((t), 1) + +#define avltreen_extreme(t, h, data) avlnode_extreme(avltreen_nodes(t), avltreen_totalsize(t), avltreen_root(t), (h), data) +#define avltreen_min(t, data) avltreen_extreme((t), 0, data) +#define avltreen_max(t, data) avltreen_extreme((t), 1, data) + +extern unsigned int avltreen_newnode (avltreen_ref, unsigned int) ; +#define avltreen_insertnode(t, i) avltreen_setroot(t, avlnode_insertnode(avltreen_nodes(t), avltreen_totalsize(t), avltreen_root(t), i, (t)->dtok, (t)->kcmp, (t)->external)) +extern int avltreen_insert (avltreen_ref, unsigned int) ; + +#define avltreen_deletenode(t, i) avltreen_delete(t, avltreen_data(t, i)) +extern int avltreen_delete (avltreen_ref, void const *) ; + +#define avltreen_iter(t, f, p) avlnode_iter(avltreen_nodes(t), avltreen_totalsize(t), avltreen_root(t), f, p) + + + /* avltreeb: everything in one place. Stack or BSS, or heap if you insist */ + +#define AVLTREEB_TYPE(size) struct { avlnode storage[size] ; unsigned int freelist[size] ; avltreen info ; } +#define avltreeb_init(t, size, dtk, f, p) avltreen_init(&(t)->info, (t)->storage, (t)->freelist, size, dtk, f, p) +#define avltreeb_totalsize(t) avltreen_totalsize(&(t)->info) +#define avltreeb_len(t) avltreen_len(&(t)->info) +#define avltreeb_nodes(t) ((avlnode_ref)(t)->storage) +#define avltreeb_data(t, i) (avltreeb_nodes(t)[i].data) +#define avltreeb_root(t) ((t)->info.root) +#define avltreeb_setroot(t, r) ((t)->info.root = (r)) + +#define avltreeb_searchnode(t, k) avlnode_searchnode(avltreeb_nodes(t), avltreeb_totalsize(t), avltreeb_root(t), (k), (t)->info.dtok, (t)->info.kcmp, (t)->info.external) +#define avltreeb_search(t, k, data) avlnode_search(avltreeb_nodes(t), avltreeb_totalsize(t), avltreeb_root(t), k, (data), (t)->info.dtok, (t)->info.kcmp, (t)->info.external) +#define avltreeb_height(t) avlnode_height(avltreeb_nodes(t), avltreeb_totalsize(t), avltreeb_root(t)) + +#define avltreeb_extremenode(t, h) avlnode_extremenode(avltreeb_nodes(t), avltreeb_totalsize(t), avltreeb_root(t), h) +#define avltreeb_minnode(t) avltreeb_extremenode((t), 0) +#define avltreeb_maxnode(t) avltreeb_extremenode((t), 1) + +#define avltreeb_extreme(t, h, data) avlnode_extremenode(avltreeb_nodes(t), avltreeb_totalsize(t), avltreeb_root(t), h, data) +#define avltreeb_min(t, data) avltreeb_extreme((t), 0, data) +#define avltreeb_max(t, data) avltreeb_extreme((t), 1, data) + +#define avltreeb_newnode(t, d) avltreen_newnode(&(t)->info, d) +#define avltreeb_insertnode(t, i) avltreeb_setroot(t, avlnode_insertnode(avltreeb_nodes(t), avltreeb_totalsize(t), avltreeb_root(t), (i), (t)->info.dtok, (t)->info.kcmp, (t)->info.external)) +#define avltreeb_insert(t, d) avltreen_insert(&(t)->info, d) + +#define avltreeb_deletenode(t, i) avltreeb_delete(t, avltreeb_data(t, i)) +#define avltreeb_delete(t, k) avltreen_delete(&(t)->info, k) + +#define avltreeb_iter(t, f, p) avlnode_iter(avltreeb_nodes(t), avltreeb_totalsize(t), avltreeb_root(t), f, p) + +#endif diff --git a/src/include/skalibs/biguint.h b/src/include/skalibs/biguint.h new file mode 100644 index 0000000..94e99a9 --- /dev/null +++ b/src/include/skalibs/biguint.h @@ -0,0 +1,46 @@ +/* ISC license. */ + +#ifndef BIGUINT_H +#define BIGUINT_H + +#include <skalibs/gccattributes.h> +#include <skalibs/uint32.h> + +extern void bu_pack (char *, uint32 const *, unsigned int) ; +extern void bu_unpack (char const *, uint32 *, unsigned int) ; +extern void bu_pack_big (char *, uint32 const *, unsigned int) ; +extern void bu_unpack_big (char const *, uint32 *, unsigned int) ; +extern unsigned int bu_fmt (char *, uint32 const *, unsigned int) ; +extern unsigned int bu_scanlen (char const *, unsigned int *) ; +extern int bu_scan (char const *, unsigned int, uint32 *, unsigned int, unsigned int) ; + +extern unsigned int bu_len (uint32 const *, unsigned int) gccattr_pure ; +extern void bu_zero (uint32 *, unsigned int) ; +extern int bu_copy (uint32 *, unsigned int, uint32 const *, unsigned int) ; +extern int bu_cmp (uint32 const *, unsigned int, uint32 const *, unsigned int) gccattr_pure ; + +#define bu_add(c, cn, a, an, b, bn) bu_addc(c, cn, a, an, b, (bn), 0) +extern int bu_addc (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int, int) ; +#define bu_sub(c, cn, a, an, b, bn) bu_subc(c, cn, a, an, b, (bn), 0) +extern int bu_subc (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int, int) ; +extern int bu_mul (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int) ; +extern int bu_div (uint32 const *, unsigned int, uint32 const *, unsigned int, uint32 *, unsigned int, uint32 *, unsigned int) ; +extern int bu_mod (uint32 *, unsigned int, uint32 const *, unsigned int) ; +extern int bu_gcd (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int) ; + +#define bu_slb(a, n) bu_slbc(a, n, 0) +extern int bu_slbc (uint32 *, unsigned int, int) ; +#define bu_srb(a, n) bu_srbc(a, n, 0) +extern int bu_srbc (uint32 *, unsigned int, int) ; + +extern int bu_addmod (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int) ; +extern int bu_submod (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int) ; +extern int bu_invmod (uint32 *, unsigned int, uint32 const *, unsigned int) ; +extern int bu_divmod (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int, uint32 const *, unsigned int) ; + +extern void bu_scan_internal (char const *, unsigned int, uint32 *) ; +extern void bu_copy_internal (uint32 *, uint32 const *, unsigned int) ; +extern void bu_div_internal (uint32 *, unsigned int, uint32 const *, unsigned int, uint32 *, unsigned int) ; +extern void bu_divmod_internal (uint32 *, uint32 *, uint32 const *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/bitarray.h b/src/include/skalibs/bitarray.h new file mode 100644 index 0000000..7cf79b3 --- /dev/null +++ b/src/include/skalibs/bitarray.h @@ -0,0 +1,33 @@ +/* ISC license. */ + +#ifndef BITARRAY_H +#define BITARRAY_H + +#include <skalibs/gccattributes.h> + +#define bitarray_div8(n) ((n) ? 1U + (((n) - 1) >> 3) : 0U) + +extern void bitarray_clearsetn (unsigned char *, unsigned int, unsigned int, int) ; +#define bitarray_clearn(s, start, len) bitarray_clearsetn(s, start, (len), 0) +#define bitarray_setn(s, start, len) bitarray_clearsetn(s, start, (len), 1) + +#define bitarray_peek(s, n) (((s)[(n)>>3] & (1U<<((n)&7))) ? 1 : 0) +#define bitarray_isset(b, n) bitarray_peek(b, n) +#define bitarray_clear(s, n) ((s)[(n)>>3] &= ~(1U << ((n) & 7))) +#define bitarray_set(s, n) ((s)[(n)>>3] |= 1U << ((n) & 7)) +#define bitarray_poke(s, n, h) ((h) ? bitarray_set(s, n) : bitarray_clear(s, n)) + +extern int bitarray_testandpoke (unsigned char *, unsigned int, int) ; +#define bitarray_testandclear(b, n) bitarray_testandpoke(b, n, 0) +#define bitarray_testandset(b, n) bitarray_testandpoke(b, n, 1) + +extern unsigned int bitarray_firstclear (unsigned char const *, unsigned int) gccattr_pure ; +extern unsigned int bitarray_firstset (unsigned char const *, unsigned int) gccattr_pure ; +#define bitarray_first(s, n, h) ((h) ? bitarray_firstset(s, n) : bitarray_firstclear(s, n)) + +extern void bitarray_not (unsigned char *, unsigned int, unsigned int) ; +extern void bitarray_and (unsigned char *, unsigned char const *, unsigned char const *, unsigned int) ; +extern void bitarray_or (unsigned char *, unsigned char const *, unsigned char const *, unsigned int) ; +extern void bitarray_xor (unsigned char *, unsigned char const *, unsigned char const *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/bufalloc.h b/src/include/skalibs/bufalloc.h new file mode 100644 index 0000000..c2779b6 --- /dev/null +++ b/src/include/skalibs/bufalloc.h @@ -0,0 +1,36 @@ +/* ISC license. */ + +#ifndef BUFALLOC_H +#define BUFALLOC_H + +#include <skalibs/gccattributes.h> +#include <skalibs/stralloc.h> + +typedef struct bufalloc bufalloc, *bufalloc_ref ; +struct bufalloc +{ + stralloc x ; + unsigned int p ; + int fd ; + int (*op) (int, char const *, unsigned int) ; +} ; + +#define BUFALLOC_ZERO { .x = STRALLOC_ZERO, .p = 0, .fd = -1, .op = 0 } +#define BUFALLOC_INIT(f, d) { .x = STRALLOC_ZERO, .p = 0, .fd = (d), .op = (f) } +extern void bufalloc_init (bufalloc_ref, int (*)(int, char const *, unsigned int), int) ; +#define bufalloc_shrink(ba) stralloc_shrink(&(ba)->x) +#define bufalloc_free(ba) stralloc_free(&(ba)->x) +#define bufalloc_put(ba, s, n) stralloc_catb(&(ba)->x, s, n) +#define bufalloc_puts(ba, s) stralloc_cats(&(ba)->x, s) +#define bufalloc_putv(ba, v, n) stralloc_catv(&(ba)->x, v, n) +#define bufalloc_fd(ba) ((ba)->fd) +extern int bufalloc_getfd (bufalloc const *) gccattr_pure ; +extern int bufalloc_flush (bufalloc_ref) ; +extern void bufalloc_clean (bufalloc_ref) ; +#define bufalloc_len(ba) ((ba)->x.len - (ba)->p) +extern unsigned int bufalloc_getlen (bufalloc const *) gccattr_pure ; +#define bufalloc_isempty(ba) ((ba)->x.len == (ba)->p) + +extern bufalloc_ref bufalloc_1, bufalloc_2 ; + +#endif diff --git a/src/include/skalibs/buffer.h b/src/include/skalibs/buffer.h new file mode 100644 index 0000000..3064c51 --- /dev/null +++ b/src/include/skalibs/buffer.h @@ -0,0 +1,134 @@ +/* ISC license. */ + +#ifndef BUFFER_H +#define BUFFER_H + +#include <skalibs/gccattributes.h> +#include <skalibs/bytestr.h> +#include <skalibs/cbuffer.h> +#include <skalibs/diuint.h> +#include <skalibs/siovec.h> + +typedef int buffer_io_func_t (int, siovec_t const *, unsigned int, void *) ; +typedef buffer_io_func_t *buffer_io_func_t_ref ; + +typedef struct buffer_s buffer, buffer_t, *buffer_ref, *buffer_t_ref ; +struct buffer_s +{ + buffer_io_func_t *op ; + int fd ; + cbuffer_t c ; + void *aux ; +} ; +#define BUFFER_ZERO { .op = 0, .fd = -1, .c = CBUFFER_ZERO, .aux = 0 } + + + /* + Circular buffers need to be 1 char bigger than the storage space, + so that the head == tail case is nonambiguous (empty). + */ + +#define BUFFER_INSIZE 8193 +#define BUFFER_OUTSIZE 8193 +#define BUFFER_ERRSIZE 1025 +#define BUFFER_INSIZE_SMALL 513 +#define BUFFER_OUTSIZE_SMALL 513 + +#define BUFFER_INIT_AUX(f, d, buf, len, data) { .op = (f), .fd = (d), .c = CBUFFER_INIT(buf, len), .aux = (data) } +#define BUFFER_INIT(f, d, buf, len) BUFFER_INIT_AUX(f, d, buf, (len), 0) +extern int buffer_init_aux (buffer *, buffer_io_func_t *, int, char *, unsigned int, void *) ; +#define buffer_init(b, f, d, buf, len) buffer_init_aux(b, f, d, buf, len, 0) + + + /* Writing */ + +extern int buffer_flush (buffer *) ; + +#define buffer_putnoflush(b, s, len) cbuffer_put(&(b)->c, s, len) +#define buffer_putvnoflush(b, v, n) cbuffer_putv(&(b)->c, v, n) +#define buffer_putsnoflush(b, s) buffer_putnoflush(b, (s), str_len(s)) + +extern int buffer_putallnoflush (buffer *, char const *, unsigned int) ; +extern int buffer_putvallnoflush (buffer *, siovec_t const *, unsigned int) ; +#define buffer_putsallnoflush(b, s) buffer_putallnoflush(b, (s), str_len(s)) + +extern int buffer_putall (buffer *, char const *, unsigned int, unsigned int *) ; +extern int buffer_putvall (buffer *, siovec_t const *, unsigned int, diuint *) ; +#define buffer_putsall(b, s, w) buffer_putall(b, s, str_len(s), w) + +#define buffer_putallflush(b, s, len, w) (buffer_putall(b, s, len, w) && buffer_flush(b)) +#define buffer_putvallflush(b, v, n, w) (buffer_putvall(b, v, n, w) && buffer_flush(b)) +#define buffer_putsallflush(b, s, w) buffer_putallflush(b, s, str_len(s), w) + +extern int buffer_put (buffer *, char const *, unsigned int) ; +extern int buffer_putv (buffer *, siovec_t const *, unsigned int) ; +#define buffer_puts(b, s) buffer_put(b, (s), str_len(s)) + +extern int buffer_putflush (buffer *, char const *, unsigned int) ; +extern int buffer_putvflush (buffer *, siovec_t const *, unsigned int) ; +#define buffer_putsflush(b, s) buffer_putflush(b, (s), str_len(s)) + +#define buffer_unput(b, n) cbuffer_unput(&(b)->c, n) +#define buffer_wpeek(b, v) cbuffer_wpeek(&(b)->c, v) +#define buffer_wseek(b, n) cbuffer_wseek(&(b)->c, n) +extern buffer_io_func_t buffer_write ; + + + /* Reading */ + +extern int buffer_fill (buffer *) ; + +#define buffer_getnofill(b, s, len) cbuffer_get(&(b)->c, s, len) +#define buffer_getvnofill(b, v, n) cbuffer_getv(&(b)->c, v, n) + +extern int buffer_getallnofill (buffer *, char *, unsigned int) ; +extern int buffer_getvallnofill (buffer *, siovec_t const *, unsigned int) ; + +extern int buffer_getall (buffer *, char *, unsigned int, unsigned int *) ; +extern int buffer_getvall (buffer *, siovec_t const *, unsigned int, diuint *) ; + +extern int buffer_get (buffer *, char *, unsigned int) ; +extern int buffer_getv (buffer *, siovec_t const *, unsigned int) ; + +#define buffer_unget(b, n) cbuffer_unget(&(b)->c, n) +#define buffer_rpeek(b, n) cbuffer_rpeek(&(b)->c, n) +#define buffer_rseek(b, n) cbuffer_rseek(&(b)->c, n) +extern buffer_io_func_t buffer_read ; + + + /* Utility */ + +#define buffer_len(b) cbuffer_len(&(b)->c) +extern unsigned int buffer_getlen (buffer const *) gccattr_pure ; +#define buffer_available(b) cbuffer_available(&(b)->c) +#define buffer_isempty(b) cbuffer_isempty(&(b)->c) +#define buffer_isfull(b) cbuffer_isfull(&(b)->c) +#define buffer_fd(b) ((b)->fd) +extern int buffer_getfd (buffer const *) gccattr_pure ; +#define buffer_isreadable(b) (!buffer_isfull(b)) +#define buffer_iswritable(b) (!buffer_isempty(b)) + + + /* Globals */ + +extern buffer_io_func_t buffer_flush1read ; + +extern buffer buffer_0_ ; +#define buffer_0 (&buffer_0_) + +extern buffer buffer_0small_ ; +#define buffer_0small (&buffer_0small_) + +extern buffer buffer_0f1_ ; +#define buffer_0f1 (&buffer_0f1_) + +extern buffer buffer_1_ ; +#define buffer_1 (&buffer_1_) + +extern buffer buffer_1small_ ; +#define buffer_1small (&buffer_1small_) + +extern buffer buffer_2_ ; +#define buffer_2 (&buffer_2_) + +#endif diff --git a/src/include/skalibs/bytestr.h b/src/include/skalibs/bytestr.h new file mode 100644 index 0000000..52e9bd6 --- /dev/null +++ b/src/include/skalibs/bytestr.h @@ -0,0 +1,65 @@ +/* ISC license. */ + +#ifndef BYTESTR_H +#define BYTESTR_H + +#include <skalibs/config.h> +#include <skalibs/gccattributes.h> + +/* for Alphas and other archs where char != 8bit */ +#define T8(x) ((x) & 0xffU) + +#ifndef SKALIBS_FLAG_REPLACE_LIBC + +#include <sys/types.h> +#include <string.h> + +#define byte_copy(to, n, from) memmove(to, (from), n) +#define byte_copyr(to, n, from) memmove(to, (from), n) +#define byte_diff(a, n, b) memcmp(a, (b), n) +#define byte_zero(p, n) memset(p, 0, n) +#define str_len(s) strlen(s) +#define str_diff(a, b) strcmp(a, b) +#define str_diffn(a, b, n) strncmp(a, b, n) +#define str_copy(to, from) strlen(strcpy(to, from)) + +#else + +extern void byte_copy (char *, unsigned int, char const *) ; +extern void byte_copyr (char *, unsigned int, char const *) ; +extern int byte_diff (char const *, unsigned int, char const *) gccattr_pure ; +extern void byte_zero (void *, unsigned int) ; +extern unsigned int str_len (char const *) gccattr_pure ; +extern int str_diff (char const *, char const *) gccattr_pure ; +extern int str_diffn (char const *, char const *, unsigned int) gccattr_pure ; +extern unsigned int str_copy (char *, char const *) ; + +#endif + + +extern unsigned int byte_chr (char const *, unsigned int, int) gccattr_pure ; +extern unsigned int byte_rchr (char const *, unsigned int, int) gccattr_pure ; +extern unsigned int byte_in (char const *, unsigned int, char const *, unsigned int) gccattr_pure ; +#define byte_equal(s, n, t) (!byte_diff((s), (n), (t))) +extern unsigned int byte_count (char const *, unsigned int, char) gccattr_pure ; + +#define str_diffb(a, n, b) str_diffn(a, (b), n) +extern unsigned int str_chr (char const *, int) gccattr_pure ; +extern unsigned int str_rchr (char const *, int) gccattr_pure ; +extern int str_start (char const *, char const *) gccattr_pure ; +#define str_equal(s, t) (!str_diff(s, t)) +extern unsigned int str_strn (char const *, unsigned int, char const *, unsigned int) gccattr_pure ; + +extern void case_lowers (char *) ; +extern void case_lowerb (char *, unsigned int) ; +extern void case_uppers (char *) ; +extern void case_upperb (char *, unsigned int) ; +#define case_equals(a, b) (!case_diffs(a, b)) +#define case_equalb(a, n, b) (!case_diffb(a, n, b)) +extern int case_diffs (char const *, char const *) gccattr_pure ; +extern int case_diffb (char const *, unsigned int, char const *) gccattr_pure ; +#define case_starts(s, t) case_startb(s, str_len(s), t) +extern int case_startb (char const *, unsigned int, char const *) gccattr_pure ; +extern unsigned int case_str (char const *, char const *) gccattr_pure ; + +#endif diff --git a/src/include/skalibs/cbuffer.h b/src/include/skalibs/cbuffer.h new file mode 100644 index 0000000..a80e3af --- /dev/null +++ b/src/include/skalibs/cbuffer.h @@ -0,0 +1,62 @@ +/* ISC license. */ + +#ifndef CBUFFER_H +#define CBUFFER_H + +#include <skalibs/gccattributes.h> +#include <skalibs/bytestr.h> +#include <skalibs/diuint.h> +#include <skalibs/siovec.h> + +typedef struct cbuffer_s cbuffer_t, *cbuffer_t_ref ; +struct cbuffer_s +{ + char *x ; + unsigned int a ; /* total length */ + unsigned int p ; /* head */ + unsigned int n ; /* tail */ +} ; +#define CBUFFER_ZERO { .x = 0, .a = 0, .p = 0, .n = 0 } + + /* + Circular buffers need to be 1 char bigger than the storage space, + so that the head == tail case is nonambiguous (empty). + */ + +#define CBUFFER_INIT(buf, len) { .x = (buf), .a = (len), .p = 0, .n = 0 } +extern int cbuffer_init (cbuffer_t *, char *, unsigned int) ; + + + /* Writing */ + +extern unsigned int cbuffer_put (cbuffer_t *, char const *, unsigned int) ; +extern unsigned int cbuffer_putv (cbuffer_t *, siovec_t const *, unsigned int) ; +#define cbuffer_puts(b, s) cbuffer_put(b, (s), str_len(s)) + +#define cbuffer_UNPUT(b, w) ((b)->n = ((b)->a + (b)->n - w) % (b)->a, w) ; +extern unsigned int cbuffer_unput (cbuffer_t *, unsigned int) ; +extern void cbuffer_wpeek (cbuffer_t *, siovec_t *) ; +#define cbuffer_WSEEK(b, w) ((b)->n = ((b)->n + (w)) % (b)->a, w) +extern unsigned int cbuffer_wseek (cbuffer_t *, unsigned int) ; + + + /* Reading */ + +extern unsigned int cbuffer_get (cbuffer_t *, char *, unsigned int) ; +extern unsigned int cbuffer_getv (cbuffer_t *, siovec_t const *, unsigned int) ; + +#define cbuffer_UNGET(b, n) ((b)->p = ((b)->a + (b)->p - n) % (b)->a, n) ; +extern unsigned int cbuffer_unget (cbuffer_t *, unsigned int) ; +extern void cbuffer_rpeek (cbuffer_t *, siovec_t *) ; +#define cbuffer_RSEEK(b, n) ((b)->p = ((b)->p + (n)) % (b)->a, n) +extern unsigned int cbuffer_rseek (cbuffer_t *, unsigned int) ; + + + /* Utility */ + +#define cbuffer_len(b) ((unsigned int)(((b)->a - (b)->p + (b)->n) % (b)->a)) +#define cbuffer_available(b) ((unsigned int)(((b)->a - (b)->n + (b)->p - 1) % (b)->a)) +#define cbuffer_isempty(b) (!cbuffer_len(b)) +#define cbuffer_isfull(b) (!cbuffer_available(b)) + +#endif diff --git a/src/include/skalibs/cdb.h b/src/include/skalibs/cdb.h new file mode 100644 index 0000000..9e9aae0 --- /dev/null +++ b/src/include/skalibs/cdb.h @@ -0,0 +1,51 @@ +/* ISC license. */ + +#ifndef CDB_H +#define CDB_H + +#include <skalibs/gccattributes.h> +#include <skalibs/uint32.h> + +#define CDB_HASHSTART 5381 +extern uint32 cdb_hashadd (uint32, unsigned char) gccattr_const ; +extern uint32 cdb_hash (char const *, unsigned int) gccattr_pure ; + +typedef struct cdb cdb_t, *cdb_t_ref ; +struct cdb +{ + char *map ; /* 0 if no map */ + int fd ; /* -1 if uninitted, negative if mapped, nonnegative if nomapped */ + uint32 size ; /* initialized if map is nonzero */ + uint32 loop ; /* number of hash slots searched under this key */ + uint32 khash ; /* initialized if loop is nonzero */ + uint32 kpos ; /* initialized if loop is nonzero */ + uint32 hpos ; /* initialized if loop is nonzero */ + uint32 hslots ; /* initialized if loop is nonzero */ + uint32 dpos ; /* initialized if cdb_findnext() returns 1 */ + uint32 dlen ; /* initialized if cdb_findnext() returns 1 */ +} ; + +#define CDB_ZERO { .map = 0, .fd = -1, .size = 0, .loop = 0, .khash = 0, .kpos = 0, .hpos = 0, .hslots = 0, .dpos = 0, .dlen = 0 } +extern struct cdb const cdb_zero ; + +extern void cdb_free (struct cdb *) ; + +#define cdb_init(c, fd) (cdb_init_map(c, (fd), 1) ? 0 : -1) +extern int cdb_init_map (struct cdb *, int fd, int) ; +extern int cdb_mapfile (struct cdb *, char const *) ; +extern int cdb_read (struct cdb *, char *, unsigned int, uint32) ; +#define cdb_findstart(c) ((c)->loop = 0) +extern int cdb_findnext (struct cdb *, char const *, unsigned int) ; +#define cdb_find(c, s, len) (cdb_findstart(c), cdb_findnext(c, s, len)) + +#define cdb_datapos(c) ((c)->dpos) +#define cdb_datalen(c) ((c)->dlen) +#define cdb_keypos(c) ((c)->kpos) +#define cdb_keylen(c) ((c)->dpos - (c)->kpos) + +#define cdb_traverse_init(c, kpos) (*(kpos) = 2048) +#define cdb_firstkey(c, kpos) (cdb_traverse_init(c, kpos), cdb_nextkey(c, kpos)) +extern int cdb_nextkey (struct cdb *, uint32 *) ; +extern int cdb_successor (struct cdb *, char const *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/cdb_make.h b/src/include/skalibs/cdb_make.h new file mode 100644 index 0000000..d0a949b --- /dev/null +++ b/src/include/skalibs/cdb_make.h @@ -0,0 +1,27 @@ +/* ISC license. */ + +#ifndef CDB_MAKE_H +#define CDB_MAKE_H + +#include <skalibs/uint32.h> +#include <skalibs/diuint32.h> +#include <skalibs/genalloc.h> +#include <skalibs/buffer.h> + +typedef struct cdb_make cdb_make, *cdb_make_ref ; +struct cdb_make +{ + char buf[BUFFER_OUTSIZE] ; + genalloc hplist ; /* diuint32 */ + buffer b ; + uint32 pos ; + int fd ; +} ; + +#define CDB_MAKE_ZERO { .buf = "", .hplist = GENALLOC_ZERO, .b = BUFFER_INIT(&buffer_write, -1, 0, 0), .pos = 2048, .fd = -1 } + +extern int cdb_make_start (struct cdb_make *, int) ; +extern int cdb_make_add (struct cdb_make *, char const *, unsigned int, char const *, unsigned int) ; +extern int cdb_make_finish (struct cdb_make *) ; + +#endif diff --git a/src/include/skalibs/datastruct.h b/src/include/skalibs/datastruct.h new file mode 100644 index 0000000..1c7d816 --- /dev/null +++ b/src/include/skalibs/datastruct.h @@ -0,0 +1,12 @@ +/* ISC license. */ + +#ifndef DATASTRUCT_H +#define DATASTRUCT_H + +#include <skalibs/genset.h> +#include <skalibs/gensetdyn.h> +#include <skalibs/avlnode.h> +#include <skalibs/avltree.h> +#include <skalibs/avltreen.h> + +#endif diff --git a/src/include/skalibs/direntry.h b/src/include/skalibs/direntry.h new file mode 100644 index 0000000..3150e51 --- /dev/null +++ b/src/include/skalibs/direntry.h @@ -0,0 +1,13 @@ +/* ISC license. */ + +#ifndef DIRENTRY_H +#define DIRENTRY_H + +#include <sys/types.h> +#include <dirent.h> + +typedef struct dirent direntry, direntry_t, *direntry_ref, *direntry_t_ref ; + +extern int dir_close (DIR *) ; + +#endif diff --git a/src/include/skalibs/diuint.h b/src/include/skalibs/diuint.h new file mode 100644 index 0000000..1c13bd8 --- /dev/null +++ b/src/include/skalibs/diuint.h @@ -0,0 +1,15 @@ +/* ISC license. */ + +#ifndef DIUINT_H +#define DIUINT_H + +typedef struct diuint diuint, *diuint_ref ; +struct diuint +{ + unsigned int left ; + unsigned int right ; +} ; + +#define DIUINT_ZERO { .left = 0, .right = 0 } + +#endif diff --git a/src/include/skalibs/diuint32.h b/src/include/skalibs/diuint32.h new file mode 100644 index 0000000..cd8ae75 --- /dev/null +++ b/src/include/skalibs/diuint32.h @@ -0,0 +1,17 @@ +/* ISC license. */ + +#ifndef DIUINT32_H +#define DIUINT32_H + +#include <skalibs/uint32.h> + +typedef struct diuint32 diuint32, *diuint32_ref ; +struct diuint32 +{ + uint32 left ; + uint32 right ; +} ; + +#define DIUINT32_ZERO { .left = 0, .right = 0 } + +#endif diff --git a/src/include/skalibs/djbtime.h b/src/include/skalibs/djbtime.h new file mode 100644 index 0000000..60a1d0b --- /dev/null +++ b/src/include/skalibs/djbtime.h @@ -0,0 +1,70 @@ +/* ISC license. */ + +#ifndef DJBTIME_H +#define DJBTIME_H + +#include <time.h> +#include <skalibs/config.h> +#include <skalibs/uint32.h> +#include <skalibs/uint64.h> +#include <skalibs/tai.h> + + +/* UTC <--> TAI conversions */ +/* sysclock can be either TAI-10 or UTC */ + +extern int utc_from_tai (uint64 *, tai_t const *) ; +extern int tai_from_utc (tai_t *, uint64) ; +extern int utc_from_sysclock (uint64 *) ; +extern int sysclock_from_utc (uint64 *) ; + + +/* NTP internal format */ + +#define NTP_OFFSET 2208988800UL +extern int ntp_from_tain (uint64 *, tain_t const *) ; +extern int tain_from_ntp (tain_t *, uint64) ; + + +/* localtime handling - replaces caltimedate functions */ +/* ltm64 can be either TAI-10 or UTC depending on --enable-right-tz */ +/* normally ltm64 is the same as sysclock, but we allow it to be different */ +/* for instance for musl TAI-10 systems */ + +typedef struct localtmn_s localtmn_t, *localtmn_t_ref ; +struct localtmn_s +{ + struct tm tm ; + uint32 nano ; +} ; + +extern int ltm64_from_tai (uint64 *, tai_t const *) ; +extern int tai_from_ltm64 (tai_t *, uint64) ; +extern int ltm64_from_utc (uint64 *) ; +extern int utc_from_ltm64 (uint64 *) ; +extern int ltm64_from_sysclock (uint64 *) ; +extern int sysclock_from_ltm64 (uint64 *) ; + +extern int localtm_from_ltm64 (struct tm *, uint64, int) ; +extern int ltm64_from_localtm (uint64 *, struct tm const *) ; +extern int localtm_from_sysclock (struct tm *, uint64, int) ; +extern int sysclock_from_localtm (uint64 *, struct tm const *) ; +extern int localtm_from_utc (struct tm *, uint64, int) ; +extern int utc_from_localtm (uint64 *, struct tm const *) ; +extern int localtm_from_tai (struct tm *, tai_t const *, int) ; +extern int tai_from_localtm (tai_t *, struct tm const *) ; + +extern int localtmn_from_tain (localtmn_t_ref, tain_t const *, int) ; +extern int tain_from_localtmn (tain_t *, localtmn_t const *) ; +extern int localtmn_from_sysclock (localtmn_t_ref, tain_t const *, int) ; +extern int sysclock_from_localtmn (tain_t *, localtmn_t const *) ; + +#define LOCALTM_FMT 21 +extern unsigned int localtm_fmt (char *, struct tm const *) ; +extern unsigned int localtm_scan (char const *, struct tm *) ; + +#define LOCALTMN_FMT 31 +extern unsigned int localtmn_fmt (char *, localtmn_t const *) ; +extern unsigned int localtmn_scan (char const *, localtmn_t_ref) ; + +#endif diff --git a/src/include/skalibs/djbunix.h b/src/include/skalibs/djbunix.h new file mode 100644 index 0000000..b5f5db4 --- /dev/null +++ b/src/include/skalibs/djbunix.h @@ -0,0 +1,149 @@ +/* ISC license. */ + +#ifndef DJBUNIX_H +#define DJBUNIX_H + +#include <sys/types.h> +#include <sys/uio.h> +#include <sys/wait.h> +#include <sys/stat.h> +#include <unistd.h> +#include <skalibs/gccattributes.h> +#include <skalibs/uint64.h> +#include <skalibs/stralloc.h> +#include <skalibs/envalloc.h> +#include <skalibs/env.h> /* compatibility */ + +#define DJBUNIX_FLAG_NB 0x01U +#define DJBUNIX_FLAG_COE 0x02U + +extern int coe (int) ; +extern int uncoe (int) ; +extern int ndelay_on (int) ; +extern int ndelay_off (int) ; +extern int pipe_internal (int *, unsigned int) ; +#define pipenb(p) pipe_internal(p, DJBUNIX_FLAG_NB) +#define pipecoe(p) pipe_internal(p, DJBUNIX_FLAG_COE) +#define pipenbcoe(p) pipe_internal(p, DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int fd_copy (int, int) ; +extern int fd_copy2 (int, int, int, int) ; +extern int fd_move (int, int) ; +extern int fd_move2 (int, int, int, int) ; +extern int fd_close (int) ; +extern int fd_chmod (int, unsigned int) ; +extern int fd_chown (int, unsigned int, unsigned int) ; +extern int fd_sync (int) ; +extern int fd_cat (int, int) ; +extern unsigned int fd_catn (int, int, unsigned int) ; +extern int fd_ensure_open (int, int) ; +#define fd_sanitize() (fd_ensure_open(0, 0) && fd_ensure_open(1, 1)) +extern int lock_ex (int) ; +extern int lock_exnb (int) ; +extern int lock_sh (int) ; +extern int lock_shnb (int) ; +extern int lock_un (int) ; +extern int open2 (char const *, unsigned int) ; +extern int open3 (char const *, unsigned int, unsigned int) ; +extern int open_read (char const *) ; +extern int open_readb (char const *) ; +extern int open_excl (char const *) ; +extern int open_append (char const *) ; +extern int open_create (char const *) ; +extern int open_trunc (char const *) ; +extern int open_write (char const *) ; +extern int socket_internal (int, int, int, unsigned int) ; +extern int socketpair_internal (int, int, int, unsigned int, int *) ; + +extern int pathexec_env (char const *, char const *) ; +extern void pathexec_r (char const *const *, char const *const *, unsigned int, char const *, unsigned int) ; +extern void pathexec_r_name (char const *, char const *const *, char const *const *, unsigned int, char const *, unsigned int) ; +extern void pathexec_fromenv (char const *const *, char const *const *, unsigned int) ; +extern void execvep (char const *, char const *const *, char const *const *, char const *) ; +extern void pathexec_run (char const *, char const *const *, char const *const *) ; +extern void pathexec0_run (char const *const *, char const *const *) ; +extern void pathexec (char const *const *) ; +extern void pathexec0 (char const *const *) ; + +#define prot_gid(gid) setgid(gid) +#define prot_uid(uid) setuid(uid) +extern int prot_readgroups (char const *, gid_t *, unsigned int) ; +extern int prot_grps (char const *) ; +extern int prot_setuidgid (char const *) ; + +extern long seek_cur (int) ; +extern int seek_set (int, long) ; +extern int seek_end (int) ; +extern int seek_trunc (int, long) ; +#define seek_begin(fd) (seek_set((fd), 0)) + +extern pid_t wait_nointr (int *) ; +extern pid_t waitpid_nointr (pid_t, int *, int) ; +#define wait_pid(pid, wstat) waitpid_nointr(pid, (wstat), 0) +#define wait_nohang(wstat) waitpid_nointr(-1, (wstat), WNOHANG) +extern pid_t wait_pid_nohang (pid_t, int *) ; +extern int wait_pids_nohang (pid_t const *, unsigned int, int *) ; +#define wait_status(w) (WIFSIGNALED(w) ? 126 : WEXITSTATUS(w)) +extern unsigned int wait_reap (void) ; +extern int waitn (pid_t *, unsigned int) ; +extern int waitn_reap (pid_t *, unsigned int) ; + +extern pid_t doublefork (void) ; + +extern int fd_chdir (int) ; + +#define absolutepath(sa, s) sarealpath(sa, s) +/* extern char *realpath (char const *, char *) ; */ +extern char *realpath_tmp (char const *, char *, stralloc *) ; +extern int sarealpath (stralloc *, char const *) ; +extern int sarealpath_tmp (stralloc *, char const *, stralloc *) ; +/* extern char *basename (char *) ; */ +extern int sabasename (stralloc *, char const *, unsigned int) ; +/* extern char *dirname (char *) ; */ +extern int sadirname (stralloc *, char const *, unsigned int) ; +extern int sagetcwd (stralloc *) ; +extern int sareadlink (stralloc *, char const *) ; +extern int sagethostname (stralloc *) ; + +extern int slurp (stralloc *, int) ; +extern int openslurpclose (stralloc *, char const *) ; +extern int openreadclose (char const *, stralloc *, unsigned int) ; +extern int openreadnclose (char const *, char *, unsigned int) ; +extern int openreadfileclose (char const *, stralloc *, unsigned int) ; + +#define openwritenclose_unsafe(f, s, n) openwritenclose_unsafe_internal(f, s, (n), 0, 0, 0) +#define openwritenclose_unsafe_sync(f, s, n) openwritenclose_unsafe_internal(f, s, (n), 0, 0, 1) +#define openwritenclose_unsafe_devino(f, s, n, dev, ino) openwritenclose_unsafe_internal(f, s, n, dev, (ino), 0) +#define openwritenclose_unsafe_devino_sync(f, s, n, dev, ino) openwritenclose_unsafe_internal(f, s, n, dev, (ino), 1) +extern int openwritenclose_unsafe_internal (char const *, char const *, unsigned int, uint64 *, uint64 *, int) ; + +#define openwritenclose_suffix(f, s, n, t) openwritenclose_suffix_internal(f, s, n, 0, 0, 0, t) +#define openwritenclose_suffix_sync(f, s, n, t) openwritenclose_suffix_internal(f, s, n, 0, 0, 1, t) +#define openwritenclose_suffix_devino(f, s, n, t, dev, ino) openwritenclose_suffix_internal(f, s, n, dev, (ino), 0, t) +#define openwritenclose_suffix_devino_sync(f, s, n, t, dev, ino) openwritenclose_suffix_internal(f, s, n, dev, (ino), 1, t) +extern int openwritenclose_suffix_internal (char const *, char const *, unsigned int, uint64 *, uint64 *, int, char const *) ; + +extern int rm_rf (char const *) ; +extern int rm_rf_tmp (char const *, stralloc *) ; +extern int rm_rf_in_tmp (stralloc *, unsigned int) ; /* caution ! */ +extern int rmstar (char const *) ; +extern int rmstar_tmp (char const *, stralloc *) ; + + + /* Simple spawn functions. 0 for no communication, 1 for a child/parent pipe. */ + +extern pid_t child_spawn0 (char const *, char const *const *, char const *const *) ; +extern pid_t child_spawn1 (char const *, char const *const *, char const *const *, int *, int) ; + + + /* + Unified function to fork a child with communication canals. + * uses posix_spawn() if available, else uses fork+exec + * requests n (the last arg) communication fds between parent and child + * if n=1, the fd is a Unix socket. If more canals are needed, you can + pass them through that socket. + * if n>=2, the fds are pipes, parent reads on even and writes on odd. + */ + +extern pid_t child_spawn (char const *, char const *const *, char const *const *, int *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/env.h b/src/include/skalibs/env.h new file mode 100644 index 0000000..ccb3aac --- /dev/null +++ b/src/include/skalibs/env.h @@ -0,0 +1,26 @@ +/* ISC license. */ + +#ifndef ENV_H +#define ENV_H + +#include <skalibs/gccattributes.h> +#include <skalibs/stralloc.h> + +extern unsigned int env_len (char const *const *) gccattr_pure ; +extern char const *env_get (char const *) gccattr_pure ; +extern char const *env_get2 (char const *const *, char const *) gccattr_pure ; +extern char const *ucspi_get (char const *) gccattr_pure ; +extern int env_addmodif (stralloc *, char const *, char const *) ; +extern int env_make (char const **, unsigned int, char const *, unsigned int) ; +extern unsigned int env_merge (char const **, unsigned int, char const *const *, unsigned int, char const *, unsigned int) ; +extern int env_string (stralloc *, char const *const *, unsigned int) ; + +#define SKALIBS_ENVDIR_VERBATIM 0x01 +#define SKALIBS_ENVDIR_NOCHOMP 0x02 +extern int envdir_internal (char const *, stralloc *, unsigned int, char) ; +#define envdir(path, sa) envdir_internal(path, (sa), 0, '\n') +#define envdir_leaveblanks(path, sa) envdir_internal(path, (sa), SKALIBS_ENVDIR_NOCHOMP, '\n') +#define envdir_verbatim_chomp(path, sa) envdir_internal(path, (sa), SKALIBS_ENVDIR_VERBATIM, '\n') +#define envdir_verbatim(path, sa) envdir_internal(path, (sa), SKALIBS_ENVDIR_VERBATIM|SKALIBS_ENVDIR_NOCHOMP, '\n') + +#endif diff --git a/src/include/skalibs/envalloc.h b/src/include/skalibs/envalloc.h new file mode 100644 index 0000000..1ebcd6b --- /dev/null +++ b/src/include/skalibs/envalloc.h @@ -0,0 +1,15 @@ +/* ISC license. */ + +#ifndef ENVALLOC_H +#define ENVALLOC_H + +#include <skalibs/genalloc.h> + +#define ENVALLOC_ZERO GENALLOC_ZERO + +extern int envalloc_make (genalloc *, unsigned int, char const *, unsigned int) ; +extern int envalloc_uniq (genalloc *, char) ; +extern int envalloc_merge (genalloc *, char const *const *, unsigned int, char const *, unsigned int) ; +extern int envalloc_0 (genalloc *) ; + +#endif diff --git a/src/include/skalibs/environ.h b/src/include/skalibs/environ.h new file mode 100644 index 0000000..6bf3041 --- /dev/null +++ b/src/include/skalibs/environ.h @@ -0,0 +1,8 @@ +/* ISC license. */ + +#ifndef ENVIRON_H +#define ENVIRON_H + +extern char **environ ; + +#endif diff --git a/src/include/skalibs/fmtscan.h b/src/include/skalibs/fmtscan.h new file mode 100644 index 0000000..ccfd160 --- /dev/null +++ b/src/include/skalibs/fmtscan.h @@ -0,0 +1,53 @@ +/* ISC license. */ + +#ifndef FMTSCAN_H +#define FMTSCAN_H + +#include <skalibs/gccattributes.h> +#include <skalibs/uint32.h> + + + /* fmt */ + + +extern unsigned char fmtscan_asc (unsigned char) gccattr_const ; + +extern unsigned int str_fmt (char *, char const *) ; +extern unsigned int strn_fmt (char *, char const *, unsigned int) ; + +#define IP4_FMT 20 +extern unsigned int ip4_fmt (char *, char const *) ; +extern unsigned int ip4_fmtu32 (char *, uint32) ; + +extern unsigned int ucharn_fmt (char *, char const *, unsigned int) ; +extern unsigned int ucharn_fmt_little (char *, char const *, unsigned int) ; + +#define IP6_FMT 40 +extern unsigned int ip6_fmt (char *, char const *) ; + +#define short_fmt(s, u) long_fmt((s), (long)(u)) +#define int_fmt(s, u) long_fmt((s), (long)(u)) +extern unsigned int long_fmt (char *, long) ; + + + /* scan */ + + +extern unsigned char fmtscan_num (unsigned char, unsigned char) gccattr_const ; + +extern unsigned int ip4_scan (char const *, char *) ; +extern unsigned int ip4_scanu32 (char const *, uint32 *) ; +extern unsigned int ip4_scanlist_u32 (uint32 *, unsigned int, char const *, unsigned int *) ; +extern unsigned int ip4_scanlist (char *, unsigned int, char const *, unsigned int *) ; +extern unsigned int ip6_scan (char const *, char *) ; +extern unsigned int ip6_scanlist (char *, unsigned int, char const *, unsigned int *) ; + +extern unsigned int ucharn_scan (char const *, char *, unsigned int) ; +extern unsigned int ucharn_scan_little (char const *, char *, unsigned int) ; +extern unsigned int ucharn_findlen (char const *) gccattr_pure ; + +extern unsigned int short_scan (char const *, short *) ; +extern unsigned int int_scan (char const *, int *) ; +extern unsigned int long_scan (char const *, long *) ; + +#endif diff --git a/src/include/skalibs/functypes.h b/src/include/skalibs/functypes.h new file mode 100644 index 0000000..15cdbcc --- /dev/null +++ b/src/include/skalibs/functypes.h @@ -0,0 +1,33 @@ +/* ISC license. */ + +#ifndef FUNCTYPES_H +#define FUNCTYPES_H + +typedef int uintcmpfunc_t (unsigned int, unsigned int, void *) ; +typedef uintcmpfunc_t *uintcmpfunc_t_ref ; + +typedef int cmpfunc_t (void const *, void const *, void *) ; +typedef cmpfunc_t *cmpfunc_t_ref ; + +typedef void *dtokfunc_t (unsigned int, void *) ; +typedef dtokfunc_t *dtokfunc_t_ref ; + +typedef int iterfunc_t (char *, void *) ; +typedef iterfunc_t *iterfunc_t_ref ; + +typedef void freefunc_t (void *) ; +typedef freefunc_t *freefunc_t_ref ; + +typedef int initfunc_t (void *) ; +typedef initfunc_t *initfunc_t_ref ; + +typedef unsigned int uinitfunc_t (void *) ; +typedef uinitfunc_t *uinitfunc_t_ref ; + +typedef int iofunc_t (int, char *, unsigned int) ; +typedef iofunc_t *iofunc_t_ref ; + +typedef unsigned int alliofunc_t (int, char *, unsigned int) ; +typedef alliofunc_t *alliofunc_t_ref ; + +#endif diff --git a/src/include/skalibs/gccattributes.h b/src/include/skalibs/gccattributes.h new file mode 100644 index 0000000..0584480 --- /dev/null +++ b/src/include/skalibs/gccattributes.h @@ -0,0 +1,61 @@ +/* ISC license. */ + +#ifndef GCCATTRIBUTES_H +#define GCCATTRIBUTES_H + +#ifdef __GNUC__ + +#define gccattr_noreturn __attribute__((__noreturn__)) +#define gccattr_noinline __attribute__((__noinline__)) +#define gccattr_inline __attribute__((__always_inline__)) +#define gccattr_const __attribute__((__const__)) +#define gccattr_unused __attribute__((__unused__)) +#define gccattr_used __attribute__((__used__)) +#define gccattr_weak __attribute__((__weak__)) +#define gccattr_aligned __attribute__((__aligned__)) + +# if (__GNUC__ >= 3) || ((__GNUC__ == 2) && (__GNUC_MINOR__ >= 96)) +#define gccattr_malloc __attribute__((__malloc__)) +#define gccattr_pure __attribute__((__pure__)) +# else +#define gccattr_malloc +#define gccattr_pure +# endif + +# if (__GNUC__ >= 3) +#define gccattr_deprecated __attribute__((__deprecated__)) +# else +#define gccattr_deprecated +# endif + +#else + +#define gccattr_noreturn +#define gccattr_noinline +#define gccattr_inline +#define gccattr_const +#define gccattr_unused +#define gccattr_used +#define gccattr_weak +#define gccattr_aligned +#define gccattr_malloc +#define gccattr_pure +#define gccattr_deprecated + +#endif + +#ifdef GCCATTR_COMPAT_0_22 +#define _a_noreturn gccattr_noreturn +#define _a_noinline gccattr_noinline +#define _a_inline gccattr_inline +#define _a_const gccattr_const +#define _a_unused gccattr_unused +#define _a_used gccattr_used +#define _a_weak gccattr_weak +#define _a_aligned gccattr_aligned +#define _a_malloc gccattr_malloc +#define _a_pure gccattr_pure +#define _a_deprecated gccattr_deprecated +#endif + +#endif diff --git a/src/include/skalibs/genalloc.h b/src/include/skalibs/genalloc.h new file mode 100644 index 0000000..1aa0613 --- /dev/null +++ b/src/include/skalibs/genalloc.h @@ -0,0 +1,35 @@ +/* ISC license. */ + +#ifndef GENALLOC_H +#define GENALLOC_H + +#include <skalibs/stralloc.h> +#include <skalibs/functypes.h> + +typedef stralloc genalloc, *genalloc_ref ; + +#define GENALLOC_ZERO STRALLOC_ZERO +#define genalloc_zero stralloc_zero + +#define genalloc_s(type, g) ((type *)((g)->s)) +#define genalloc_len(type, g) ((g)->len/sizeof(type)) +#define genalloc_setlen(type, g, n) ((g)->len = (n)*sizeof(type)) + +#define genalloc_ready(type, g, n) stralloc_ready((g), (n)*sizeof(type)) +#define genalloc_ready_tuned(type, g, n, base, fracnum, fracden) stralloc_ready_tuned((g), (n)*sizeof(type), base, fracnum, fracden) +#define genalloc_readyplus(type, g, n) stralloc_readyplus((g), (n)*sizeof(type)) +#define genalloc_free(type, g) stralloc_free(g) +#define genalloc_shrink(type, g) stralloc_shrink(g) +#define genalloc_catb(type, g, s, n) stralloc_catb((g), (char const *)(s), (n)*sizeof(type)) +#define genalloc_copyb(type, g, s, n) stralloc_copyb((g), (char const *)(s), (n)*sizeof(type)) +#define genalloc_copy(type, g1, g2) stralloc_copy((g1), g2) +#define genalloc_cat(type, g1, g2) stralloc_cat((g1), g2) +#define genalloc_append(type, g, p) stralloc_catb((g), (char const *)(p), sizeof(type)) +#define genalloc_reverse(type, g) stralloc_reverse_blocks((g), sizeof(type)) +#define genalloc_insertb(type, g, offset, s, n) stralloc_insertb((g), (offset)*sizeof(type), (char const *)(s), (n)*sizeof(type)) +#define genalloc_insert(type, g1, offset, g2) stralloc_insert((g1), (offset)*sizeof(type), (g2)) + +#define genalloc_deepfree(type, g, f) genalloc_deepfree_size(g, (freefunc_t_ref)(f), sizeof(type)) +extern void genalloc_deepfree_size (genalloc *, freefunc_t_ref, unsigned int) ; + +#endif diff --git a/src/include/skalibs/genset.h b/src/include/skalibs/genset.h new file mode 100644 index 0000000..5eb47d7 --- /dev/null +++ b/src/include/skalibs/genset.h @@ -0,0 +1,37 @@ +/* ISC license. */ + +#ifndef GENSET_H +#define GENSET_H + +#include <skalibs/functypes.h> + +typedef struct genset_s genset, *genset_ref ; +struct genset_s +{ + char *storage ; + unsigned int *freelist ; + unsigned int esize ; + unsigned int max ; + unsigned int sp ; +} ; + +#define GENSET_ZERO { .storage = 0, .freelist = 0, .esize = 1, .max = 0, .sp = 0 } +extern void genset_init (genset_ref, void *, unsigned int *, unsigned int, unsigned int) ; +#define GENSET_init(g, type, storage, fl, size) genset_init(g, storage, fl, sizeof(type), size) + +#define genset_p(type, g, i) ((type *)((g)->storage + (i) * (g)->esize)) +extern unsigned int genset_new (genset_ref) ; +extern int genset_delete (genset_ref, unsigned int) ; +#define genset_n(g) ((g)->max - (g)->sp) +extern unsigned int genset_iter (genset_ref, iterfunc_t_ref, void *) ; + + +#define GENSETB_TYPE(type, size) struct { type storage[size] ; unsigned int freelist[size] ; genset info ; } +#define GENSETB_init(type, g, size) GENSET_init(&(g)->info, type, (g)->storage, (g)->freelist, size) +#define gensetb_p(type, g, i) genset_p(type, &(g)->info, i) +#define gensetb_new(g) genset_new(&(g)->info) +#define gensetb_delete(g, i) genset_delete(&(g)->info, i) +#define gensetb_n(g) genset_n(&(g)->info) +#define gensetb_iter(g, f, p) genset_iter(&(g)->info, f, p) + +#endif diff --git a/src/include/skalibs/gensetdyn.h b/src/include/skalibs/gensetdyn.h new file mode 100644 index 0000000..4fe396b --- /dev/null +++ b/src/include/skalibs/gensetdyn.h @@ -0,0 +1,40 @@ +/* ISC license. */ + +#ifndef GENSETDYN_H +#define GENSETDYN_H + +#include <skalibs/stralloc.h> +#include <skalibs/genalloc.h> +#include <skalibs/functypes.h> + +typedef struct gensetdyn_s gensetdyn, *gensetdyn_ref ; +struct gensetdyn_s +{ + stralloc storage ; + genalloc freelist ; /* array of unsigned int */ + unsigned int esize ; + unsigned int base ; + unsigned int fracnum ; + unsigned int fracden ; +} ; + +#define GENSETDYN_ZERO { .storage = STRALLOC_ZERO, .freelist = GENALLOC_ZERO, .esize = 1, .base = 0, .fracnum = 0, .fracden = 1 } +extern gensetdyn const gensetdyn_zero ; + +#define GENSETDYN_INIT(type, b, num, den) { .storage = STRALLOC_ZERO, .freelist = GENALLOC_ZERO, .esize = sizeof(type), .base = (b), .fracnum = (num), .fracden = (den) } +extern void gensetdyn_init (gensetdyn_ref, unsigned int, unsigned int, unsigned int, unsigned int) ; + +#define gensetdyn_n(g) ((g)->storage.len - genalloc_len(unsigned int, &(g)->freelist)) +extern int gensetdyn_ready (gensetdyn_ref, unsigned int) ; +#define gensetdyn_readyplus(x, n) gensetdyn_ready(x, gensetdyn_n(x) + (n)) +extern void gensetdyn_free (gensetdyn_ref) ; + +extern int gensetdyn_new (gensetdyn_ref, unsigned int *) ; +extern int gensetdyn_delete (gensetdyn_ref, unsigned int) ; + +#define gensetdyn_p(g, i) ((g)->storage.s + (i) * (g)->esize) +#define GENSETDYN_P(type, g, i) ((type *)gensetdyn_p(g, i)) + +extern unsigned int gensetdyn_iter (gensetdyn_ref, iterfunc_t_ref, void *) ; + +#endif diff --git a/src/include/skalibs/genwrite.h b/src/include/skalibs/genwrite.h new file mode 100644 index 0000000..9ae0fb9 --- /dev/null +++ b/src/include/skalibs/genwrite.h @@ -0,0 +1,35 @@ +/* ISC license. */ + +#ifndef GENWRITE_H +#define GENWRITE_H + +typedef int genwrite_put_func_t (void *, char const *, unsigned int) ; +typedef genwrite_put_func_t *genwrite_put_func_t_ref ; + +typedef int genwrite_flush_func_t (void *) ; +typedef genwrite_flush_func_t *genwrite_flush_func_t_ref ; + +typedef struct genwrite_s genwrite_t, *genwrite_t_ref ; +struct genwrite_s +{ + genwrite_put_func_t_ref put ; + genwrite_flush_func_t_ref flush ; + void *target ; +} ; +#define GENWRITE_ZERO { .put = 0, .flush = 0, .target = 0 } + +extern genwrite_put_func_t genwrite_put_stralloc ; +extern genwrite_flush_func_t genwrite_flush_stralloc ; +extern genwrite_put_func_t genwrite_put_buffer ; +extern genwrite_flush_func_t genwrite_flush_buffer ; +extern genwrite_put_func_t genwrite_put_bufalloc ; +extern genwrite_flush_func_t genwrite_flush_bufalloc ; + +#define GENWRITE_STRALLOC_INIT(sa) { .put = &genwrite_put_stralloc, .flush = &genwrite_flush_stralloc, .target = (sa) } +#define GENWRITE_BUFFER_INIT(b) { .put = &genwrite_put_buffer, .flush = &genwrite_flush_buffer, .target = (b) } +#define GENWRITE_BUFALLOC_INIT(ba) { .put = &genwrite_put_bufalloc, .flush = &genwrite_flush_bufalloc, .target = (ba) } + +extern genwrite_t genwrite_stdout ; +extern genwrite_t genwrite_stderr ; + +#endif diff --git a/src/include/skalibs/getpeereid.h b/src/include/skalibs/getpeereid.h new file mode 100644 index 0000000..4aa2898 --- /dev/null +++ b/src/include/skalibs/getpeereid.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef GETPEEREID_H +#define GETPEEREID_H + +#include <sys/types.h> + +extern int getpeereid (int, uid_t *, gid_t *) ; + +#endif diff --git a/src/include/skalibs/iobuffer.h b/src/include/skalibs/iobuffer.h new file mode 100644 index 0000000..c1864cf --- /dev/null +++ b/src/include/skalibs/iobuffer.h @@ -0,0 +1,117 @@ +/* ISC license. */ + +#ifndef IOBUFFER_H +#define IOBUFFER_H + +#define IOBUFFER_SIZE 65536U + + /* iobufferu */ + +#include <skalibs/buffer.h> +#include <skalibs/djbunix.h> + +#define IOBUFFERU_SIZE (IOBUFFER_SIZE+1) + +typedef struct iobufferu_s iobufferu, *iobufferu_ref ; +struct iobufferu_s +{ + buffer b[2] ; + char *buf ; +} ; + +extern int iobufferu_init (iobufferu_ref, int, int) ; +extern int iobufferu_fill (iobufferu_ref) ; +extern int iobufferu_flush (iobufferu_ref) ; +extern void iobufferu_finish (iobufferu_ref) ; + +#define iobufferu_len(u) buffer_len(&(u)->b[0]) +#define iobufferu_available(u) buffer_available(&(u)->b[1]) +#define iobufferu_isempty(u) buffer_isempty(&(u)->b[0]) +#define iobufferu_isfull(u) buffer_isfull(&(u)->b[1]) +#define iobufferu_fd(u, h) buffer_fd((u)->b[h]) +#define iobufferu_isreadable(u) iobufferu_available(u) +#define iobufferu_iswritable(u) iobufferu_len(u) +#define iobufferu_nonblock(u, h) ndelay_on(iobufferu_fd(u, h)) +#define iobufferu_block(u, h) ndelay_off(iobufferu_fd(u, h)) +#define iobufferu_nbstate(u, h, j) (j ? iobufferu_nonblock(u, h) : iobufferu_block(u, h)) + + + /* iobufferk */ + +#define IOBUFFERK_SIZE IOBUFFER_SIZE + +typedef struct iobufferk_s iobufferk, *iobufferk_ref ; +struct iobufferk_s +{ + int fd[2] ; + int p[2] ; + unsigned int n ; + unsigned int type : 2 ; + unsigned int nb : 2 ; +} ; + +typedef int iobufferk_io_func_t (iobufferk_ref) ; +typedef iobufferk_io_func_t *iobufferk_io_func_t_ref ; +typedef void iobufferk_finish_func_t (iobufferk_ref) ; +typedef iobufferk_finish_func_t *iobufferk_finish_func_t_ref ; + +extern iobufferk_io_func_t iobufferk_nosys ; +extern iobufferk_io_func_t iobufferk_isworking ; + +extern int iobufferk_init (iobufferk_ref, int, int) ; +extern iobufferk_io_func_t_ref const iobufferk_fill_f[4] ; +extern iobufferk_io_func_t_ref const iobufferk_flush_f[4] ; +extern iobufferk_finish_func_t_ref const iobufferk_finish_f[4] ; + +#define iobufferk_fill(k) (*iobufferk_fill_f[(k)->type])(k) +#define iobufferk_flush(k) (*iobufferk_flush_f[(k)->type])(k) +#define iobufferk_finish(k) (*iobufferk_finish_f[(k)->type])(k) +#define iobufferk_len(k) ((k)->n) +#define iobufferk_available(k) (IOBUFFERK_SIZE - (k)->n) +#define iobufferk_isempty(k) (!iobufferk_len(k)) +#define iobufferk_isfull(k) (!iobufferk_available(k)) +#define iobufferk_fd(k, h) ((k)->fd[h]) +#define iobufferk_isreadable(k) iobufferk_available(k) +#define iobufferk_iswritable(k) iobufferk_len(k) +#define iobufferk_nonblock(k, h) ((k)->nb |= (1 << (h)), 0) +#define iobufferk_block(k, h) ((k)->nb &= (3 - (1 << (h))), 0) +#define iobufferk_nbstate(k, h, j) (j ? iobufferk_nonblock(k, h) : iobufferk_block(k, h)) + + + /* iobuffer */ + +typedef union iobufferku_u iobufferku, *iobufferku_ref ; +union iobufferku_u +{ + iobufferk k ; + iobufferu u ; +} ; + +typedef struct iobuffer_s iobuffer, *iobuffer_ref ; +struct iobuffer_s +{ + iobufferku x ; + unsigned int isk : 1 ; +} ; + +extern int iobuffer_ufromk (iobufferu_ref, iobufferk_ref) ; +extern int iobuffer_kfromu (iobufferk_ref, iobufferu_ref) ; +extern int iobuffer_salvage (iobuffer_ref) ; + +extern int iobuffer_init (iobuffer_ref, int, int) ; +extern int iobuffer_fill (iobuffer_ref) ; +extern int iobuffer_flush (iobuffer_ref) ; + +#define iobuffer_finish(b) ((b)->isk ? iobufferk_finish(&(b)->x.k) : iobufferu_finish(&(b)->x.u)) +#define iobuffer_len(b) ((b)->isk ? iobufferk_len(&(b)->x.k) : iobufferu_len(&(b)->x.u)) +#define iobuffer_available(b) ((b)->isk ? iobufferk_available(&(b)->x.k) : iobufferu_available(&(b)->x.u)) +#define iobuffer_isempty(b) ((b)->isk ? iobufferk_isempty(&(b)->x.k) : iobufferu_isempty(&(b)->x.u)) +#define iobuffer_isfull(b) ((b)->isk ? iobufferk_isfull(&(b)->x.k) : iobufferu_isfull(&(b)->x.u)) +#define iobuffer_fd(b, h) ((b)->isk ? iobufferk_fd(&(b)->x.k, h) : iobufferu_fd(&(b)->x.u, h)) +#define iobuffer_isreadable(b) ((b)->isk ? iobufferk_isreadable(&(b)->x.k) : iobufferu_isreadable(&(b)->x.u)) +#define iobuffer_iswritable(b) ((b)->isk ? iobufferk_iswritable(&(b)->x.k) : iobufferu_iswritable(&(b)->x.u)) +#define iobuffer_nonblock(b, h) ((b)->isk ? iobufferk_nonblock(&(b)->x.k, h) : iobufferu_nonblock(&(b)->x.u, h)) +#define iobuffer_block(b, h) ((b)->isk ? iobufferk_block(&(b)->x.k, h) : iobufferu_block(&(b)->x.u, h)) +#define iobuffer_nbstate(b, h, j) ((b)->isk ? iobufferk_nbstate(&(b)->x.k, h, j) : iobufferu_nbstate(&(b)->x.u, h, j)) + +#endif diff --git a/src/include/skalibs/iopause.h b/src/include/skalibs/iopause.h new file mode 100644 index 0000000..d8f5e1c --- /dev/null +++ b/src/include/skalibs/iopause.h @@ -0,0 +1,32 @@ +/* ISC license. */ + +#ifndef IOPAUSE_H +#define IOPAUSE_H + +#include <sys/types.h> +#include <poll.h> +#include <skalibs/tai.h> + +typedef struct pollfd iopause_fd, *iopause_fd_ref ; + +#define IOPAUSE_READ (POLLIN|POLLHUP) +#define IOPAUSE_WRITE POLLOUT +#define IOPAUSE_EXCEPT (POLLERR|POLLHUP|POLLNVAL) + +typedef int iopause_func_t (iopause_fd *, unsigned int, tain_t const *, tain_t const *) ; +typedef iopause_func_t *iopause_func_t_ref ; + +extern iopause_func_t iopause_select ; +extern iopause_func_t iopause_poll ; +extern iopause_func_t iopause_ppoll ; + +extern iopause_func_t_ref const iopause_ ; +#define iopause (*iopause_) + +extern int iopause_stamp (iopause_fd *, unsigned int, tain_t const *, tain_t *) ; +#define iopause_g(x, n, deadline) iopause_stamp(x, n, (deadline), &STAMP) + +extern void deepsleepuntil (tain_t const *, tain_t *) ; +#define deepsleepuntil_g(deadline) deepsleepuntil((deadline), &STAMP) + +#endif diff --git a/src/include/skalibs/kolbak.h b/src/include/skalibs/kolbak.h new file mode 100644 index 0000000..86a2170 --- /dev/null +++ b/src/include/skalibs/kolbak.h @@ -0,0 +1,32 @@ +/* ISC license. */ + +#ifndef KOLBAK_H +#define KOLBAK_H + +#include <skalibs/unixmessage.h> + +typedef struct kolbak_closure_s kolbak_closure_t, *kolbak_closure_t_ref ; +struct kolbak_closure_s +{ + unixmessage_handler_func_t_ref f ; + void *data ; +} ; +#define KOLBAK_CLOSURE_ZERO { .f = 0, .data = 0 } + +typedef struct kolbak_queue_s kolbak_queue_t, *kolbak_queue_t_ref ; +struct kolbak_queue_s +{ + kolbak_closure_t *x ; + unsigned int n ; + unsigned int head ; + unsigned int tail ; +} ; +#define KOLBAK_QUEUE_ZERO { .x = 0, .n = 0, .head = 0, .tail = 0 } +#define KOLBAK_QUEUE_INIT(s, len) { .x = (s), .n = (len), .head = 0, .tail = 0 } + +extern int kolbak_queue_init (kolbak_queue_t *, kolbak_closure_t *, unsigned int) ; +extern int kolbak_enqueue (kolbak_queue_t *, unixmessage_handler_func_t_ref, void *) ; +extern int kolbak_unenqueue (kolbak_queue_t *) ; +extern int kolbak_call (unixmessage_t const *, kolbak_queue_t *) ; + +#endif diff --git a/src/include/skalibs/lolstdio.h b/src/include/skalibs/lolstdio.h new file mode 100644 index 0000000..ec66da2 --- /dev/null +++ b/src/include/skalibs/lolstdio.h @@ -0,0 +1,30 @@ +/* ISC license. */ + +#ifndef LOLSTDIO_H +#define LOLSTDIO_H + +#include <stdarg.h> +#include <skalibs/buffer.h> +#include <skalibs/bufalloc.h> +#include <skalibs/strerr2.h> + +#ifdef DEBUG +# define LOLDEBUG(...) do \ + { \ + buffer_puts(buffer_2, PROG) ; \ + buffer_puts(buffer_2, ": debug: ") ; \ + bprintf(buffer_2, __VA_ARGS__) ; \ + buffer_putflush(buffer_2, "\n", 1) ; \ + } while (0) +#else +# define LOLDEBUG(...) +#endif + +extern int vbprintf (buffer *, char const *, va_list) ; +extern int bprintf (buffer *, char const *, ...) ; +extern int lolprintf (char const *, ...) ; + +extern int vbaprintf (bufalloc *, char const *, va_list) ; +extern int baprintf (bufalloc *, char const *, ...) ; + +#endif diff --git a/src/include/skalibs/md5.h b/src/include/skalibs/md5.h new file mode 100644 index 0000000..00e43cc --- /dev/null +++ b/src/include/skalibs/md5.h @@ -0,0 +1,21 @@ +/* ISC license. */ + +#ifndef MD5_H +#define MD5_H + +#include <skalibs/uint32.h> + +typedef struct MD5Schedule MD5Schedule, *MD5Schedule_ref ; +struct MD5Schedule +{ + uint32 buf[4] ; + uint32 bits[2] ; + unsigned char in[64] ; +} ; + +#define MD5_INIT() { .buf = { 0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL }, .bits = { 0, 0 }, .in = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" } +extern void md5_init (MD5Schedule_ref) ; +extern void md5_update (MD5Schedule_ref, char const *, unsigned int) ; +extern void md5_final (MD5Schedule_ref, char * /* 16 chars */) ; + +#endif diff --git a/src/include/skalibs/mininetstring.h b/src/include/skalibs/mininetstring.h new file mode 100644 index 0000000..c275fb2 --- /dev/null +++ b/src/include/skalibs/mininetstring.h @@ -0,0 +1,13 @@ +/* ISC license. */ + +#ifndef MININETSTRING_H +#define MININETSTRING_H + +#include <skalibs/uint16.h> +#include <skalibs/uint32.h> +#include <skalibs/stralloc.h> + +extern int mininetstring_read (int, stralloc *, uint32 *) ; +extern int mininetstring_write (int, char const *, uint16, uint32 *) ; + +#endif diff --git a/src/include/skalibs/netstring.h b/src/include/skalibs/netstring.h new file mode 100644 index 0000000..ee5cd85 --- /dev/null +++ b/src/include/skalibs/netstring.h @@ -0,0 +1,24 @@ +/* ISC license. */ + +#ifndef NETSTRING_H +#define NETSTRING_H + +#include <skalibs/buffer.h> +#include <skalibs/siovec.h> +#include <skalibs/stralloc.h> + +extern int netstring_appendb (stralloc *, char const *, unsigned int) ; +#define netstring_appends(sa, s) netstring_appendb((sa), (s), str_len(s)) +#define netstring_append(to, from) netstring_appendb((to), (from)->s, (from)->len) +extern int netstring_appendv (stralloc *, siovec_t const *, unsigned int) ; + +extern int netstring_encode (stralloc *, char const *, unsigned int) ; +extern int netstring_decode (stralloc *, char const *, unsigned int) ; + +extern int netstring_okeof (buffer *, unsigned int) ; +extern int netstring_get (buffer *, stralloc *, unsigned int *) ; +extern int netstring_put (buffer *, char const *, unsigned int, unsigned int *) ; +#define netstring_putba(ba, s, n) netstring_appendb(&(ba)->x, s, n) +#define netstring_putbav(ba, v, n) netstring_appendv(&(ba)->x, v, n) + +#endif diff --git a/src/include/skalibs/nonposix.h b/src/include/skalibs/nonposix.h new file mode 100644 index 0000000..26af211 --- /dev/null +++ b/src/include/skalibs/nonposix.h @@ -0,0 +1,58 @@ +/* ISC license. */ + +#ifndef NONPOSIX_H +#define NONPOSIX_H + + + /* Drop all pretense of standardness: some libc headers are *more* + broken when you define standard feature test macros than when + you don't (I'm looking at you, FreeBSD). */ + +#undef _POSIX_C_SOURCE +#undef _XOPEN_SOURCE + + + /* Solaris: the socket API is not POSIX unless you enable this */ + +#ifndef _XPG4_2 +#define _XPG4_2 +#endif +#ifndef _XPG6 +#define _XPG6 +#endif + + + /* Solaris: for settimeofday() and similar. Notice how you + have to define by hand a macro with double underscores. */ + +#ifndef __EXTENSIONS__ +#define __EXTENSIONS__ +#endif + + + /* GNU: most extensions are unavailable unless you enable this */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#ifndef _BSD_SOURCE +#define _BSD_SOURCE +#endif + + + /* NetBSD: of course they had to have their own macros. */ + +#ifndef _NETBSD_SOURCE +#define _NETBSD_SOURCE +#endif +#ifndef _INCOMPLETE_XOPEN_C063 +#define _INCOMPLETE_XOPEN_C063 +#endif + + + /* old versions of BSD: system headers are not self-contained, + starting with sys/types.h is the usual workaround */ + +#include <sys/types.h> + +#endif diff --git a/src/include/skalibs/nsig.h b/src/include/skalibs/nsig.h new file mode 100644 index 0000000..f6bd240 --- /dev/null +++ b/src/include/skalibs/nsig.h @@ -0,0 +1,13 @@ +/* ISC license. */ + +#ifndef NSIG_H + +#include <signal.h> + +#define SKALIBS_NSIG 64 + +#ifndef NSIG +# define NSIG SKALIBS_NSIG +#endif + +#endif diff --git a/src/include/skalibs/random.h b/src/include/skalibs/random.h new file mode 100644 index 0000000..90aa0c0 --- /dev/null +++ b/src/include/skalibs/random.h @@ -0,0 +1,33 @@ +/* ISC license. */ + +/* MT-unsafe functions only. Use rrandom for MT-safety. */ + +#ifndef RANDOM_H +#define RANDOM_H + +#include <skalibs/gccattributes.h> +#include <skalibs/stralloc.h> + +extern int badrandom_init (void) ; +extern unsigned char badrandom_char (void) ; +extern unsigned int badrandom_int (unsigned int) ; +extern unsigned int badrandom_string (char *, unsigned int) ; +extern void badrandom_finish (void) ; + +extern int goodrandom_init (void) ; +extern unsigned char goodrandom_char (void) ; +extern unsigned int goodrandom_int (unsigned int) ; +extern unsigned int goodrandom_string (char *, unsigned int) ; +extern void goodrandom_finish (void) ; + +#define random_init badrandom_init +#define random_char badrandom_char +#define random_int badrandom_int +#define random_string badrandom_string +#define random_finish badrandom_finish + +extern int random_name (char *, unsigned int) ; +extern int random_sauniquename (stralloc *, unsigned int) ; +extern void random_unsort (char *, unsigned int, unsigned int) ; + +#endif diff --git a/src/include/skalibs/randomegd.h b/src/include/skalibs/randomegd.h new file mode 100644 index 0000000..28d4faa --- /dev/null +++ b/src/include/skalibs/randomegd.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef RANDOMEGD_H +#define RANDOMEGD_H + +extern int randomegd_open (char const *) ; +extern unsigned int randomegd_readb (int, char *, unsigned int) ; +extern unsigned int randomegd_readnb (int, char *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/rc4.h b/src/include/skalibs/rc4.h new file mode 100644 index 0000000..16683ce --- /dev/null +++ b/src/include/skalibs/rc4.h @@ -0,0 +1,19 @@ +/* ISC license. */ + +#ifndef RC4_H +#define RC4_H + +#define RC4_THROWAWAY 128 + +typedef struct RC4Schedule RC4Schedule, *RC4Schedule_ref ; + +struct RC4Schedule +{ + unsigned char tab[256] ; + unsigned char x, y ; +} ; + +extern void rc4_init (RC4Schedule_ref, char const *, unsigned int) ; +extern void rc4 (RC4Schedule_ref, char const *, char *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/rrandom.h b/src/include/skalibs/rrandom.h new file mode 100644 index 0000000..7f6a2aa --- /dev/null +++ b/src/include/skalibs/rrandom.h @@ -0,0 +1,36 @@ +/* ISC license. */ + +#ifndef RRANDOM_H +#define RRANDOM_H + +#include <skalibs/unirandom.h> + +typedef struct rrandominfo rrandominfo, *rrandominfo_ref, **rrandominfo_ref_ref ; +struct rrandominfo +{ + unirandom it ; + unsigned int ok ; +} ; + +#define RRANDOMINFO_ZERO { .it = UNIRANDOM_ZERO, .ok = 3 } + +typedef struct rrandom rrandom, *rrandom_ref, **rrandom_ref_ref ; +struct rrandom +{ + rrandominfo tries[3] ; + unsigned int n ; +} ; + +#define RRANDOM_ZERO { .tries = { RRANDOMINFO_ZERO, RRANDOMINFO_ZERO, RRANDOMINFO_ZERO }, .n = 0 } + +extern int rrandom_add (rrandom_ref, int (*) (unirandom_ref)) ; +extern int rrandom_finish (rrandom_ref) ; + +extern unsigned int rrandom_read (rrandom_ref, char *, unsigned int, unsigned int (*) (unirandom_ref, char *, unsigned int)) ; +extern unsigned int rrandom_readint (rrandom_ref, unsigned int, unsigned int (*) (unirandom_ref, char *, unsigned int)) ; +#define rrandom_readb(z, s, n) rrandom_read((z), (s), (n), &unirandom_readb) +#define rrandom_readnb(z, s, n) rrandom_read((z), (s), (n), &unirandom_readnb) + +extern unsigned int rrandom_name (rrandom_ref, char *, unsigned int, int) ; + +#endif diff --git a/src/include/skalibs/segfault.h b/src/include/skalibs/segfault.h new file mode 100644 index 0000000..732ad4f --- /dev/null +++ b/src/include/skalibs/segfault.h @@ -0,0 +1,11 @@ +/* ISC license. */ + +#ifndef SEGFAULT_H +#define SEGFAULT_H + +extern int sigsegv (void) ; +extern int sigfpe (void) ; + +#define segfault() sigsegv() + +#endif diff --git a/src/include/skalibs/selfpipe.h b/src/include/skalibs/selfpipe.h new file mode 100644 index 0000000..c587f93 --- /dev/null +++ b/src/include/skalibs/selfpipe.h @@ -0,0 +1,17 @@ +/* ISC license. */ + +/* MT-unsafe */ + +#ifndef SELFPIPE_H +#define SELFPIPE_H + +#include <signal.h> + +extern int selfpipe_init (void) ; +extern int selfpipe_trap (int) ; +extern int selfpipe_untrap (int) ; +extern int selfpipe_trapset (sigset_t const *) ; +extern int selfpipe_read (void) ; +extern void selfpipe_finish (void) ; + +#endif diff --git a/src/include/skalibs/sgetopt.h b/src/include/skalibs/sgetopt.h new file mode 100644 index 0000000..367edf3 --- /dev/null +++ b/src/include/skalibs/sgetopt.h @@ -0,0 +1,46 @@ +/* ISC license. */ + +#ifndef SGETOPT_H +#define SGETOPT_H + + + /* reentrant */ + +typedef struct subgetopt_s subgetopt_t, *subgetopt_t_ref ; +struct subgetopt_s +{ + int ind ; + int err ; + int problem ; + char const *arg ; + unsigned int pos ; + char const *prog ; +} ; + +#define SUBGETOPT_ZERO { .ind = 1, .err = 1, .problem = 0, .arg = 0, .pos = 0, .prog = 0 } + +extern int subgetopt_r (int, char const *const *, char const *, subgetopt_t_ref) ; + + + /* non-reentrant */ + +extern int sgetopt_r (int, char const *const *, char const *, subgetopt_t_ref) ; + +extern subgetopt_t subgetopt_here ; + +#define subgetopt(argc, argv, opts) subgetopt_r((argc), (argv), (opts), &subgetopt_here) +#define sgetopt(argc, argv, opts) sgetopt_r((argc), (argv), (opts), &subgetopt_here) +#define sgetopt_prog() (subgetopt_here.prog = PROG) + +/* define SUBGETOPT_SHORT if you don't mind potential name conflicts */ + +#ifdef SUBGETOPT_SHORT +# define getopt sgetopt +# define optarg subgetopt_here.arg +# define optind subgetopt_here.ind +# define opterr subgetopt_here.err +# define optopt subgetopt_here.problem +# define opteof (-1) +#endif + +#endif diff --git a/src/include/skalibs/sha1.h b/src/include/skalibs/sha1.h new file mode 100644 index 0000000..03cdaf0 --- /dev/null +++ b/src/include/skalibs/sha1.h @@ -0,0 +1,22 @@ +/* ISC license. */ + +#ifndef SHA1_H +#define SHA1_H + +#include <skalibs/uint32.h> + +typedef struct SHA1Schedule SHA1Schedule, *SHA1Schedule_ref ; +struct SHA1Schedule +{ + uint32 buf[5] ; + uint32 bits[2] ; + uint32 in[16] ; + unsigned int b ; +} ; + +#define SHA1_INIT() { .buf = { 0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL, 0xc3d2e1f0UL }, .bits = { 0, 0 }, .in = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .b = 0 } +extern void sha1_init (SHA1Schedule_ref) ; +extern void sha1_update (SHA1Schedule_ref, char const *, unsigned int) ; +extern void sha1_final (SHA1Schedule_ref, char * /* 20 chars */) ; + +#endif diff --git a/src/include/skalibs/sha256.h b/src/include/skalibs/sha256.h new file mode 100644 index 0000000..8f28c38 --- /dev/null +++ b/src/include/skalibs/sha256.h @@ -0,0 +1,29 @@ +/* ISC license. */ + +/* SHA256 routines */ +/* Written by David Madore (<URL: http://www.madore.org/~david/ >) */ +/* Adapted by Laurent Bercot. */ +/* This version last modified 2010-01-08 */ + +/* Note: these routines do not depend on endianness. */ + +#ifndef SHA256_H +#define SHA256_H + +#include <skalibs/uint32.h> + +typedef struct SHA256Schedule_s SHA256Schedule, *SHA256Schedule_ref ; +struct SHA256Schedule_s +{ + uint32 buf[8] ; /* The eight chaining variables */ + uint32 bits[2] ; /* Count number of message bits */ + uint32 in[16] ; /* Data being fed in */ + unsigned int b ; /* Our position within the 512 bits (always between 0 and 63) */ +} ; + +#define SHA256_INIT() { .buf = { 0x6a09e667UL, 0xbb67ae85UL, 0x3c6ef372UL, 0xa54ff53aUL, 0x510e527fUL, 0x9b05688cUL, 0x1f83d9abUL, 0x5be0cd19UL }, .bits = { 0, 0 }, .in = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .b = 0 } +extern void sha256_init (SHA256Schedule_ref) ; +extern void sha256_update (SHA256Schedule_ref, char const *, unsigned int) ; +extern void sha256_final (SHA256Schedule_ref, char *digest) ; + +#endif diff --git a/src/include/skalibs/sig.h b/src/include/skalibs/sig.h new file mode 100644 index 0000000..a463b63 --- /dev/null +++ b/src/include/skalibs/sig.h @@ -0,0 +1,61 @@ +/* ISC license. */ + +#ifndef SIG_H +#define SIG_H + +#include <signal.h> +#include <skalibs/gccattributes.h> + +extern int sig_alarm gccattr_deprecated ; +extern int sig_child gccattr_deprecated ; +extern int sig_stop gccattr_deprecated ; +extern int sig_cont gccattr_deprecated ; +extern int sig_hangup gccattr_deprecated ; +extern int sig_int gccattr_deprecated ; +extern int sig_kill gccattr_deprecated ; +extern int sig_pipe gccattr_deprecated ; +extern int sig_term gccattr_deprecated ; +extern int sig_usr1 gccattr_deprecated ; +extern int sig_usr2 gccattr_deprecated ; +extern int sig_quit gccattr_deprecated ; +extern int sig_abort gccattr_deprecated ; + + +typedef void skasighandler_t (int) ; +typedef skasighandler_t *skasighandler_t_ref ; + +struct skasigaction +{ + skasighandler_t_ref handler ; + unsigned int flags : 2 ; +} ; + +#define SKASA_MASKALL 0x01 +#define SKASA_NOCLDSTOP 0x02 + +extern struct skasigaction const SKASIG_DFL ; +extern struct skasigaction const SKASIG_IGN ; +extern int skasigaction (int, struct skasigaction const *, struct skasigaction *) ; + +#define sig_catcha(sig, ac) skasigaction(sig, (ac), 0) +#define sig_restore(sig) skasigaction((sig), &SKASIG_DFL, 0) + +extern void sig_restoreto (sigset_t const *, unsigned int) ; +extern int sig_catch (int, skasighandler_t_ref) ; +#define sig_ignore(sig) sig_catcha((sig), &SKASIG_IGN) +#define sig_uncatch(sig) sig_restore(sig) + +#define SIGSTACKSIZE 16 +extern int sig_pusha (int, struct skasigaction const *) ; +extern int sig_pop (int) ; +extern int sig_push (int, skasighandler_t_ref) ; + +extern void sig_block (int) ; +extern void sig_blockset (sigset_t const *) ; +extern void sig_unblock (int) ; +extern void sig_blocknone (void) ; +extern void sig_pause (void) ; +extern void sig_shield (void) ; +extern void sig_unshield (void) ; + +#endif diff --git a/src/include/skalibs/siovec.h b/src/include/skalibs/siovec.h new file mode 100644 index 0000000..8c8161f --- /dev/null +++ b/src/include/skalibs/siovec.h @@ -0,0 +1,29 @@ +/* ISC license. */ + +#ifndef SIOVEC_H +#define SIOVEC_H + +#include <sys/types.h> +#include <sys/uio.h> +#include <skalibs/gccattributes.h> + +typedef struct siovec_s siovec_t, *siovec_t_ref ; +struct siovec_s +{ + char *s ; + unsigned int len ; +} ; + +extern unsigned int siovec_len (siovec_t const *, unsigned int) gccattr_pure ; +extern unsigned int siovec_gather (siovec_t const *, unsigned int, char *, unsigned int) ; +extern unsigned int siovec_scatter (siovec_t const *, unsigned int, char const *, unsigned int) ; +extern unsigned int siovec_deal (siovec_t const *, unsigned int, siovec_t const *, unsigned int) ; +extern unsigned int siovec_seek (siovec_t *, unsigned int, unsigned int) ; + +extern void siovec_from_iovec (siovec_t *, struct iovec const *, unsigned int) ; +extern void iovec_from_siovec (struct iovec *, siovec_t const *, unsigned int) ; + +extern unsigned int siovec_bytechr (siovec_t const *, unsigned int, char) ; +extern unsigned int siovec_bytein (siovec_t const *, unsigned int, char const *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/skaclient.h b/src/include/skalibs/skaclient.h new file mode 100644 index 0000000..a057d50 --- /dev/null +++ b/src/include/skalibs/skaclient.h @@ -0,0 +1,115 @@ +/* ISC license. */ + +#ifndef SKACLIENT_H +#define SKACLIENT_H + +#include <sys/types.h> +#include <skalibs/kolbak.h> +#include <skalibs/siovec.h> +#include <skalibs/tai.h> +#include <skalibs/uint32.h> +#include <skalibs/unixmessage.h> + + + /* Server part */ + +extern int skaclient_server_ack (unixmessage_t const *, unixmessage_sender_t *, unixmessage_sender_t *, char const *, unsigned int, char const *, unsigned int) ; +extern int skaclient_server_bidi_ack (unixmessage_t const *, unixmessage_sender_t *, unixmessage_sender_t *, unixmessage_receiver_t *, char *, unsigned int, char *, unsigned int, char const *, unsigned int, char const *, unsigned int) ; +extern int skaclient_server_init (unixmessage_receiver_t *, char *, unsigned int, char *, unsigned int, unixmessage_sender_t *, unixmessage_sender_t *, char const *, unsigned int, char const *, unsigned int, tain_t const *, tain_t *) ; +#define skaclient_server_init_g(in, mainbuf, mainlen, auxbuf, auxlen, out, asyncout, before, beforelen, after, afterlen, deadline) skaclient_server_init(in, mainbuf, mainlen, auxbuf, auxlen, out, asyncout, before, beforelen, after, afterlen, (deadline), &STAMP) + + + /* Client part: the rest of this file */ + +#define SKACLIENT_KOLBAK_SIZE 64 +#define SKACLIENT_OPTION_WAITPID 0x00000001U + +#define skaclient_buffer_type(bufsn, auxbufsn, bufan, auxbufan, qlen) struct { char bufs[bufsn] ; char auxbufs[auxbufsn] ; char bufa[bufan] ; char auxbufa[auxbufan] ; kolbak_closure_t q[qlen] ; } +typedef skaclient_buffer_type(UNIXMESSAGE_BUFSIZE, UNIXMESSAGE_AUXBUFSIZE, UNIXMESSAGE_BUFSIZE, UNIXMESSAGE_AUXBUFSIZE, SKACLIENT_KOLBAK_SIZE) skaclient_buffer_t, *skaclient_buffer_t_ref ; + + + /* Callback data for init */ + +typedef struct skaclient_cbdata_s skaclient_cbdata_t, *skaclient_cbdata_t_ref ; +struct skaclient_cbdata_s +{ + unixmessage_receiver_t *asyncin ; + unixmessage_sender_t *asyncout ; + char const *after ; + unsigned int afterlen ; +} ; + + + /* User structure */ + +typedef struct skaclient_s skaclient_t, *skaclient_t_ref ; +struct skaclient_s +{ + unixmessage_receiver_t syncin ; + unixmessage_sender_t syncout ; + kolbak_queue_t kq ; + unixmessage_receiver_t asyncin ; + unixmessage_sender_t asyncout ; + pid_t pid ; + uint32 options ; +} ; +#define SKACLIENT_ZERO { .syncin = UNIXMESSAGE_RECEIVER_ZERO, .syncout = UNIXMESSAGE_SENDER_ZERO, .kq = KOLBAK_QUEUE_ZERO, .asyncin = UNIXMESSAGE_RECEIVER_ZERO, .asyncout = UNIXMESSAGE_SENDER_ZERO, .pid = 0, .options = 0 } +extern skaclient_t const skaclient_zero ; + +extern void skaclient_end (skaclient_t *) ; + +extern int skaclient_start_async (skaclient_t *, char *, unsigned int, char *, unsigned int, char *, unsigned int, char *, unsigned int, kolbak_closure_t *, unsigned int, char const *, char const *, unsigned int, char const *, unsigned int, skaclient_cbdata_t *) ; +#define skaclient_start_async_b(a, sb, path, before, beforelen, after, afterlen, blah) skaclient_start_async(a, (sb)->bufs, sizeof((sb)->bufs), (sb)->auxbufs, sizeof((sb)->auxbufs), (sb)->bufa, sizeof((sb)->bufa), (sb)->auxbufa, sizeof((sb)->auxbufa), (sb)->q, sizeof((sb)->q), path, before, beforelen, after, afterlen, blah) +extern int skaclient_startf_async (skaclient_t *, char *, unsigned int, char *, unsigned int, char *, unsigned int, char *, unsigned int, kolbak_closure_t *, unsigned int, char const *, char const *const *, char const *const *, uint32, char const *, unsigned int, char const *, unsigned int, skaclient_cbdata_t *) ; +#define skaclient_startf_async_b(a, sb, prog, argv, envp, options, before, beforelen, after, afterlen, blah) skaclient_startf_async(a, (sb)->bufs, sizeof((sb)->bufs), (sb)->auxbufs, sizeof((sb)->auxbufs), (sb)->bufa, sizeof((sb)->bufa), (sb)->auxbufa, sizeof((sb)->auxbufa), (sb)->q, sizeof((sb)->q), prog, argv, envp, options, before, beforelen, after, afterlen, blah) + +extern int skaclient_start (skaclient_t *, char *, unsigned int, char *, unsigned int, char *, unsigned int, char *, unsigned int, kolbak_closure_t *, unsigned int, char const *, char const *, unsigned int, char const *, unsigned int, tain_t const *, tain_t *) ; +#define skaclient_start_b(a, sb, path, before, beforelen, after, afterlen, deadline, stamp) skaclient_start((a, (sb)->bufs, sizeof((sb)->bufs), (sb)->auxbufs, sizeof((sb)->auxbufs), (sb)->bufa, sizeof((sb)->bufa), (sb)->auxbufa, sizeof((sb)->auxbufa), (sb)->q, sizeof((sb)->q), path, before, beforelen, after, afterlen, deadline, stamp) +#define skaclient_start_g(a, bufs, bufsn, auxbufs, auxbufsn, bufa, bufan, auxbufa, auxbufan, q, qlen, path, before, beforelen, after, afterlen, deadline) skaclient_start(a, bufs, bufsn, auxbufs, auxbufsn, bufa, bufan, auxbufa, auxbufan, q, qlen, path, before, beforelen, after, afterlen, (deadline), &STAMP) +#define skaclient_start_b_g(a, sb, path, before, beforelen, after, afterlen, deadline) skaclient_start_b(a, sb, path, before, beforelen, after, afterlen, (deadline), &STAMP) + +extern int skaclient_startf (skaclient_t *, char *, unsigned int, char *, unsigned int, char *, unsigned int, char *, unsigned int, kolbak_closure_t *, unsigned int, char const *, char const *const *, char const *const *, uint32, char const *, unsigned int, char const *, unsigned int, tain_t const *, tain_t *) ; +#define skaclient_startf_b(a, sb, prog, argv, envp, before, beforelen, after, afterlen, deadline, stamp) skaclient_start((a, (sb)->bufs, sizeof((sb)->bufs), (sb)->auxbufs, sizeof((sb)->auxbufs), (sb)->bufa, sizeof((sb)->bufa), (sb)->auxbufa, sizeof((sb)->auxbufa), (sb)->q, sizeof((sb)->q), prog, argv, envp, before, beforelen, after, afterlen, deadline, stamp) +#define skaclient_startf_g(a, bufs, bufsn, auxbufs, auxbufsn, bufa, bufan, auxbufa, auxbufan, q, qlen, prog, argv, envp, options, before, beforelen, after, afterlen, deadline) skaclient_startf(a, bufs, bufsn, auxbufs, auxbufsn, bufa, bufan, auxbufa, auxbufan, q, qlen, prog, argv, envp, options, before, beforelen, after, afterlen, (deadline), &STAMP) +#define skaclient_startf_b_g(a, sb, prog, argv, envp, options, before, beforelen, after, afterlen, deadline) skaclient_startf_b(a, sb, prog, argv, envp, options, before, beforelen, after, afterlen, (deadline), &STAMP) + +extern int skaclient_putmsg_and_close (skaclient_t *, unixmessage_t const *, unsigned char const *, unixmessage_handler_func_t *, void *) ; +#define skaclient_putmsg(a, m, cb, result) skaclient_putmsg_and_close(a, m, unixmessage_bits_closenone, cb, result) +extern int skaclient_putmsgv_and_close (skaclient_t *, unixmessage_v_t const *, unsigned char const *, unixmessage_handler_func_t *, void *) ; +#define skaclient_putmsgv(a, m, cb, result) skaclient_putmsgv_and_close(a, m, unixmessage_bits_closenone, cb, result) + +extern int skaclient_put (skaclient_t *, char const *, unsigned int, unixmessage_handler_func_t *, void *) ; +extern int skaclient_putv (skaclient_t *, siovec_t const *, unsigned int, unixmessage_handler_func_t *, void *) ; + +extern int skaclient_sendmsg_and_close (skaclient_t *, unixmessage_t const *, unsigned char const *, unixmessage_handler_func_t *, void *, tain_t const *, tain_t *) ; +#define skaclient_sendmsg_and_close_g(a, m, bits, cb, result, deadline) skaclient_sendmsg_and_close(a, m, bits, cb, result, (deadline), &STAMP) +#define skaclient_sendmsg(a, m, cb, result, deadline, stamp) skaclient_sendmsg_and_close(a, m, unixmessage_bits_closenone, cb, result, deadline, stamp) +#define skaclient_sendmsg_g(a, m, cb, result, deadline) skaclient_sendmsg(a, m, cb, result, (deadline), &STAMP) + +extern int skaclient_sendmsgv_and_close (skaclient_t *, unixmessage_v_t const *, unsigned char const *, unixmessage_handler_func_t *, void *, tain_t const *, tain_t *) ; +#define skaclient_sendmsgv_and_close_g(a, m, bits, cb, result, deadline) skaclient_sendmsgv_and_close(a, m, bits, cb, result, (deadline), &STAMP) +#define skaclient_sendmsgv(a, m, cb, result, deadline, stamp) skaclient_sendmsgv_and_close(a, m, unixmessage_bits_closenone, cb, result, deadline, stamp) +#define skaclient_sendmsgv_g(a, m, cb, result, deadline) skaclient_sendmsgv(a, m, cb, result, (deadline), &STAMP) + +extern int skaclient_send (skaclient_t *, char const *, unsigned int, unixmessage_handler_func_t *, void *, tain_t const *, tain_t *) ; +#define skaclient_send_g(a, s, len, cb, result, deadline) skaclient_send(a, s, len, cb, result, (deadline), &STAMP) +extern int skaclient_sendv (skaclient_t *, siovec_t const *, unsigned int, unixmessage_handler_func_t *, void *, tain_t const *, tain_t *) ; +#define skaclient_sendv_g(a, v, vlen, cb, result, deadline) skaclient_sendv(a, v, vlen, cb, result, (deadline), &STAMP) + +#define skaclient_sfd(a) unixmessage_receiver_fd(&(a)->syncin) +#define skaclient_flush(a) unixmessage_sender_flush(&(a)->syncout) +#define skaclient_timed_flush(a, deadline, stamp) unixmessage_sender_timed_flush(&(a)->syncout, deadline, stamp) +#define skaclient_timed_flush_g(a, deadline) skaclient_timed_flush(a, (deadline), &STAMP) + +#define skaclient_supdate(a) unixmessage_handle(&(a)->syncin, (unixmessage_handler_func_t *)&kolbak_call, &(a)->kq) +#define skaclient_timed_supdate(a, deadline, stamp) unixmessage_timed_handle(&(a)->syncin, (unixmessage_handler_func_t *)&kolbak_call, &(a)->kq, deadline, stamp) +#define skaclient_timed_supdate_g(a, deadline) skaclient_timed_supdate(a, (deadline), &STAMP) + +#define skaclient_fd(a) skaclient_afd(a) +#define skaclient_afd(a) unixmessage_receiver_fd(&(a)->asyncin) +#define skaclient_update(a, f, p) skaclient_aupdate(a, f, p) +#define skaclient_aupdate(a, f, p) unixmessage_handle(&(a)->asyncin, f, p) + +extern unixmessage_handler_func_t skaclient_default_cb ; + +#endif diff --git a/src/include/skalibs/skalibs.h b/src/include/skalibs/skalibs.h new file mode 100644 index 0000000..e889461 --- /dev/null +++ b/src/include/skalibs/skalibs.h @@ -0,0 +1,19 @@ +/* ISC license. */ + +#ifndef SKALIBS_H +#define SKALIBS_H + +/* + This header includes everything in skalibs, + except skalibs/config.h and skalibs/sysdeps.h + It's heavy! +*/ + +#include <skalibs/stddjb.h> +#include <skalibs/stdcrypto.h> +#include <skalibs/random.h> +#include <skalibs/datastruct.h> +#include <skalibs/biguint.h> +#include <skalibs/unixonacid.h> + +#endif diff --git a/src/include/skalibs/skamisc.h b/src/include/skalibs/skamisc.h new file mode 100644 index 0000000..bdc4869 --- /dev/null +++ b/src/include/skalibs/skamisc.h @@ -0,0 +1,29 @@ +/* ISC license. */ + +#ifndef SKAMISC_H +#define SKAMISC_H + +#include <skalibs/buffer.h> +#include <skalibs/stralloc.h> + +extern stralloc satmp ; + +extern int skagetln (buffer *, stralloc *, char) ; +extern int skagetlnsep (buffer *, stralloc *, char const *, unsigned int) ; +extern int getlnmax (buffer *, char *, unsigned int, unsigned int *, char) ; +extern int getlnmaxsep (buffer *, char *, unsigned int, unsigned int *, char const *, unsigned int) ; + +extern int sauniquename (stralloc *) ; + +extern int string_quote (stralloc *, char const *, unsigned int) ; +extern int string_quote_nodelim (stralloc *, char const *, unsigned int) ; +extern int string_quote_nodelim_mustquote (stralloc *, char const *, unsigned int, char const *, unsigned int) ; +extern int string_unquote (char *, unsigned int *, char const *, unsigned int, unsigned int *) ; +extern int string_unquote_nodelim (char *, char const *, unsigned int) ; +extern int string_unquote_withdelim (char *, unsigned int *, char const *, unsigned int, unsigned int *, char const *, unsigned int) ; + +extern int string_format (stralloc *, char const *, char const *, char const *const *) ; + +#define skaoffsetof(n, s, field) do { s sofoftmp ; *(n) = (unsigned char *)&sofoftmp->field - (unsigned char *)&sofoftmp ; } while (0) + +#endif diff --git a/src/include/skalibs/socket.h b/src/include/skalibs/socket.h new file mode 100644 index 0000000..91be2f5 --- /dev/null +++ b/src/include/skalibs/socket.h @@ -0,0 +1,122 @@ +/* ISC license. */ + +#ifndef SOCKET_H +#define SOCKET_H + +#include <skalibs/gccattributes.h> +#include <skalibs/uint16.h> +#include <skalibs/uint32.h> +#include <skalibs/tai.h> +#include <skalibs/djbunix.h> +#include <skalibs/webipc.h> + +typedef int socket_io_func_t (int, char *, unsigned int, char *, uint16 *) ; +typedef socket_io_func_t *socket_io_func_t_ref ; + + + /* INET and INET6 domain socket operations */ + +#define socket_tcp() socket_tcp4() +#define socket_tcp_b() socket_tcp4_b() +#define socket_tcp_nb() socket_tcp4_nb() +#define socket_tcp_coe() socket_tcp4_coe() +#define socket_tcp_nbcoe() socket_tcp4_nbcoe() + +#define socket_tcp4() socket_tcp4_nb() +#define socket_tcp4_b() socket_tcp4_internal(0) +#define socket_tcp4_nb() socket_tcp4_internal(DJBUNIX_FLAG_NB) +#define socket_tcp4_coe() socket_tcp4_internal(DJBUNIX_FLAG_COE) +#define socket_tcp4_nbcoe() socket_tcp4_internal(DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int socket_tcp4_internal (unsigned int) ; + +#define socket_tcp6() socket_tcp6_nb() +#define socket_tcp6_b() socket_tcp6_internal(0) +#define socket_tcp6_nb() socket_tcp6_internal(DJBUNIX_FLAG_NB) +#define socket_tcp6_coe() socket_tcp6_internal(DJBUNIX_FLAG_COE) +#define socket_tcp6_nbcoe() socket_tcp6_internal(DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int socket_tcp6_internal (unsigned int) ; + +#define socket_udp() socket_udp4() +#define socket_udp_b() socket_udp4_b() +#define socket_udp_nb() socket_udp4_nb() +#define socket_udp_coe() socket_udp4_coe() +#define socket_udp_nbcoe() socket_udp4_nbcoe() + +#define socket_udp4() socket_udp4_nb() +#define socket_udp4_b() socket_udp4_internal(0) +#define socket_udp4_nb() socket_udp4_internal(DJBUNIX_FLAG_NB) +#define socket_udp4_coe() socket_udp4_internal(DJBUNIX_FLAG_COE) +#define socket_udp4_nbcoe() socket_udp4_internal(DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int socket_udp4_internal (unsigned int) ; + +#define socket_udp6() socket_udp6_nb() +#define socket_udp6_b() socket_udp6_internal(0) +#define socket_udp6_nb() socket_udp6_internal(DJBUNIX_FLAG_NB) +#define socket_udp6_coe() socket_udp6_internal(DJBUNIX_FLAG_COE) +#define socket_udp6_nbcoe() socket_udp6_internal(DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int socket_udp6_internal (unsigned int) ; + +extern int socket_waitconn (int, tain_t const *, tain_t *) ; +#define socket_waitconn_g(fd, deadline) socket_waitconn(fd, (deadline), &STAMP) +extern int socket_deadlineconnstamp4 (int, char const *, uint16, tain_t const *, tain_t *) ; +#define socket_deadlineconnstamp(s, ip, port, deadline, stamp) socket_deadlineconnstamp4(s, ip, port, deadline, stamp) +#define socket_deadlineconnstamp4_g(fd, ip, port, deadline) socket_deadlineconnstamp4(fd, ip, port, (deadline), &STAMP) +extern int socket_deadlineconnstamp4_u32 (int, uint32, uint16, tain_t const *, tain_t *) ; +#define socket_deadlineconnstamp4_u32_g(fd, ip, port, deadline) socket_deadlineconnstamp4_u32(fd, ip, port, (deadline), &STAMP) + +extern int socket_timeoutconn (int, char const *, uint16, unsigned int) ; +extern int socket_connect4 (int, char const *, uint16) ; +extern int socket_connect4_u32 (int, uint32, uint16) ; +extern int socket_connected (int) gccattr_const ; +extern int socket_bind4 (int, char const *, uint16) ; +extern int socket_bind4_reuse (int, char const *, uint16) ; +#define socket_listen(fd, b) ipc_listen(fd, b) + +extern int socket_connect6 (int, char const *, uint16) ; +extern int socket_deadlineconnstamp6 (int, char const *, uint16, tain_t const *, tain_t *) ; +#define socket_deadlineconnstamp6_g(fd, ip6, port, deadline) socket_deadlineconnstamp6(fd, ip6, port, (deadline), &STAMP) +extern int socket_bind6 (int, char const *, uint16) ; +extern int socket_bind6_reuse (int, char const *, uint16) ; + +#define socket_accept4(s, ip, port) socket_accept4_internal(s, ip, (port), 0) +#define socket_accept4_nb(s, ip, port) socket_accept4_internal(s, ip, (port), DJBUNIX_FLAG_NB) +#define socket_accept4_coe(s, ip, port) socket_accept4_internal(s, ip, (port), DJBUNIX_FLAG_COE) +#define socket_accept4_nbcoe(s, ip, port) socket_accept4_internal(s, ip, (port), DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int socket_accept4_internal (int, char *, uint16 *, unsigned int) ; +extern socket_io_func_t socket_recv4 ; +extern int socket_send4 (int, char const *, unsigned int, char const *, uint16) ; +extern socket_io_func_t socket_ioloop_send4 ; +extern int socket_local4 (int, char *, uint16 *) ; +extern int socket_remote4 (int, char *, uint16 *) ; + +#define socket_accept6(s, ip6, port) socket_accept6_internal(s, ip6, (port), 0) +#define socket_accept6_nb(s, ip6, port) socket_accept6_internal(s, ip6, (port), DJBUNIX_FLAG_NB) +#define socket_accept6_coe(s, ip6, port) socket_accept6_internal(s, ip6, (port), DJBUNIX_FLAG_COE) +#define socket_accept6_nbcoe(s, ip6, port) socket_accept6_internal(s, ip6, (port), DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int socket_accept6_internal (int, char *, uint16 *, unsigned int) ; +extern socket_io_func_t socket_recv6 ; +extern int socket_send6 (int, char const *, unsigned int, char const *, uint16) ; +extern socket_io_func_t socket_ioloop_send6 ; +extern int socket_local6 (int, char *, uint16 *) ; +extern int socket_remote6 (int, char *, uint16 *) ; + +extern int socket_ipoptionskill (int) ; +extern int socket_tcpnodelay (int) ; +extern void socket_tryreservein (int, unsigned int) ; + + + /* Timed send and recv operations (for dgram sockets) */ + +extern int socket_ioloop (int, char *, unsigned int, char *, uint16 *, socket_io_func_t_ref, int, tain_t const *, tain_t *) ; + +#define socket_sendnb4(fd, buf, len, ip4, port, deadline, stamp) socket_ioloop(fd, buf, len, (char *)ip4, &(port), &socket_ioloop_send4, 1, deadline, stamp) +#define socket_sendnb4_g(fd, buf, len, ip4, port, deadline) socket_sendnb4(fd, buf, len, ip4, port, (deadline), &STAMP) +#define socket_recvnb4(fd, buf, len, ip4, port, deadline, stamp) socket_ioloop(fd, buf, len, ip4, port, &socket_recv4, 0, deadline, stamp) +#define socket_recvnb4_g(fd, buf, len, ip4, port, deadline) socket_recvnb4(fd, buf, len, ip4, port, (deadline), &STAMP) + +#define socket_sendnb6(fd, buf, len, ip6, port, deadline, stamp) socket_ioloop(fd, buf, len, (char *)ip6, &(port), &socket_ioloop_send6, 1, deadline, stamp) +#define socket_sendnb6_g(fd, buf, len, ip6, port, deadline) socket_sendnb6(fd, buf, len, ip6, port, (deadline), &STAMP) +#define socket_recvnb6(fd, buf, len, ip6, port, deadline, stamp) socket_ioloop(fd, buf, len, ip6, port, &socket_recv6, 0, deadline, stamp) +#define socket_recvnb6_g(fd, buf, len, ip6, port, deadline) socket_recvnb6(fd, buf, len, ip6, port, (deadline), &STAMP) + +#endif diff --git a/src/include/skalibs/stdcrypto.h b/src/include/skalibs/stdcrypto.h new file mode 100644 index 0000000..bd6751b --- /dev/null +++ b/src/include/skalibs/stdcrypto.h @@ -0,0 +1,11 @@ +/* ISC license. */ + +#ifndef STDCRYPTO_H +#define STDCRYPTO_H + +#include <skalibs/rc4.h> +#include <skalibs/md5.h> +#include <skalibs/sha1.h> +#include <skalibs/sha256.h> + +#endif diff --git a/src/include/skalibs/stddjb.h b/src/include/skalibs/stddjb.h new file mode 100644 index 0000000..e265003 --- /dev/null +++ b/src/include/skalibs/stddjb.h @@ -0,0 +1,60 @@ +/* ISC license. */ + +#ifndef STDDJB_H +#define STDDJB_H + +#include <skalibs/config.h> +#include <skalibs/uint16.h> +#include <skalibs/uint32.h> +#include <skalibs/uint64.h> +#include <skalibs/ushort.h> +#include <skalibs/uint.h> +#include <skalibs/ulong.h> +#include <skalibs/error.h> +#include <skalibs/gidstuff.h> +#include <skalibs/ip46.h> +#include <skalibs/setgroups.h> + +#include <skalibs/alloc.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/bitarray.h> +#include <skalibs/bufalloc.h> +#include <skalibs/buffer.h> +#include <skalibs/bytestr.h> +#include <skalibs/cbuffer.h> +#include <skalibs/cdb.h> +#include <skalibs/cdb_make.h> +#include <skalibs/direntry.h> +#include <skalibs/diuint32.h> +#include <skalibs/diuint.h> +#include <skalibs/djbtime.h> +#include <skalibs/djbunix.h> +#include <skalibs/envalloc.h> +#include <skalibs/env.h> +#include <skalibs/environ.h> +#include <skalibs/fmtscan.h> +#include <skalibs/functypes.h> +#include <skalibs/gccattributes.h> +#include <skalibs/genalloc.h> +#include <skalibs/genwrite.h> +#include <skalibs/getpeereid.h> +#include <skalibs/iobuffer.h> +#include <skalibs/iopause.h> +#include <skalibs/lolstdio.h> +#include <skalibs/mininetstring.h> +#include <skalibs/netstring.h> +#include <skalibs/nsig.h> +#include <skalibs/segfault.h> +#include <skalibs/selfpipe.h> +#include <skalibs/sgetopt.h> +#include <skalibs/sig.h> +#include <skalibs/siovec.h> +#include <skalibs/skamisc.h> +#include <skalibs/socket.h> +#include <skalibs/stralloc.h> +#include <skalibs/strerr.h> +#include <skalibs/strerr2.h> +#include <skalibs/tai.h> +#include <skalibs/webipc.h> + +#endif diff --git a/src/include/skalibs/stralloc.h b/src/include/skalibs/stralloc.h new file mode 100644 index 0000000..2d762d4 --- /dev/null +++ b/src/include/skalibs/stralloc.h @@ -0,0 +1,40 @@ +/* ISC license. */ + +#ifndef STRALLOC_H +#define STRALLOC_H + +#include <skalibs/bytestr.h> +#include <skalibs/siovec.h> + +typedef struct stralloc_s stralloc, *stralloc_ref ; +struct stralloc_s +{ + char *s ; + unsigned int len ; + unsigned int a ; +} ; + +#define STRALLOC_ZERO { .s = 0, .len = 0, .a = 0 } +extern stralloc const stralloc_zero ; + +extern int stralloc_ready_tuned (stralloc *, unsigned int, unsigned int, unsigned int, unsigned int) ; +#define stralloc_ready(sa, n) stralloc_ready_tuned(sa, (n), 8, 1, 8) +#define stralloc_readyplus(sa, n) stralloc_ready(sa, (sa)->len + (n)) +extern void stralloc_free (stralloc *) ; +extern int stralloc_shrink (stralloc *) ; +extern int stralloc_copyb (stralloc *, char const *, unsigned int) ; +extern int stralloc_catb (stralloc *, char const *, unsigned int) ; +extern int stralloc_catv (stralloc *, siovec_t const *, unsigned int) ; +#define stralloc_copys(sa, s) stralloc_copyb(sa, (s), str_len(s)) +#define stralloc_cats(sa, s) stralloc_catb(sa, (s), str_len(s)) +#define stralloc_copy(sa1, sa2) stralloc_copyb(sa1, (sa2)->s, (sa2)->len) +#define stralloc_cat(sa1, sa2) stralloc_catb(sa1, (sa2)->s, (sa2)->len) +extern int stralloc_append (stralloc *, char) ; +extern void stralloc_reverse (stralloc *) ; +extern void stralloc_reverse_blocks (stralloc *, unsigned int) ; +#define stralloc_0(sa) stralloc_catb(sa, "", 1) +extern int stralloc_insertb (stralloc *, unsigned int, char const *, unsigned int) ; +#define stralloc_inserts(sa, offset, s) stralloc_insertb(sa, offset, (s), str_len(s)) +#define stralloc_insert(sa1, offset, sa2) stralloc_insertb(sa1, offset, (sa2)->s, (sa2)->len) + +#endif diff --git a/src/include/skalibs/strerr.h b/src/include/skalibs/strerr.h new file mode 100644 index 0000000..64665b5 --- /dev/null +++ b/src/include/skalibs/strerr.h @@ -0,0 +1,139 @@ +/* ISC license. */ + +#ifndef STRERR_H +#define STRERR_H + +#include <skalibs/gccattributes.h> + +extern void strerr_warn (char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *) ; +extern void strerr_die (int, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *) gccattr_noreturn ; +extern void strerr_warnsys (char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *) ; +extern void strerr_diesys (int, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *, char const *) gccattr_noreturn ; + +#define strerr_warn10(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, se) \ +strerr_warn(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, se) +#define strerr_warn9(x1, x2, x3, x4, x5, x6, x7, x8, x9, se) \ +strerr_warn(x1, x2, x3, x4, x5, x6, x7, x8, x9, 0, se) +#define strerr_warn8(x1, x2, x3, x4, x5, x6, x7, x8, se) \ +strerr_warn(x1, x2, x3, x4, x5, x6, x7, x8, 0, 0, se) +#define strerr_warn7(x1, x2, x3, x4, x5, x6, x7, se) \ +strerr_warn(x1, x2, x3, x4, x5, x6, x7, 0, 0, 0, se) +#define strerr_warn6(x1, x2, x3, x4, x5, x6, se) \ +strerr_warn(x1, x2, x3, x4, x5, x6, 0, 0, 0, 0, se) +#define strerr_warn5(x1, x2, x3, x4, x5, se) \ +strerr_warn(x1, x2, x3, x4, x5, 0, 0, 0, 0, 0, se) +#define strerr_warn4(x1, x2, x3, x4, se) \ +strerr_warn(x1, x2, x3, x4, 0, 0, 0, 0, 0, 0, se) +#define strerr_warn3(x1, x2, x3, se) \ +strerr_warn(x1, x2, x3, 0, 0, 0, 0, 0, 0, 0, se) +#define strerr_warn2(x1, x2, se) \ +strerr_warn(x1, x2, 0, 0, 0, 0, 0, 0, 0, 0, se) +#define strerr_warn1(x1, se) \ +strerr_warn(x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, se) + +#define strerr_warn10sys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) \ +strerr_warnsys(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) +#define strerr_warn9sys(x1, x2, x3, x4, x5, x6, x7, x8, x9) \ +strerr_warnsys(x1, x2, x3, x4, x5, x6, x7, x8, (x9), 0) +#define strerr_warn8sys(x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_warnsys(x1, x2, x3, x4, x5, x6, x7, (x8), 0, 0) +#define strerr_warn7sys(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warnsys(x1, x2, x3, x4, x5, x6, (x7), 0, 0, 0) +#define strerr_warn6sys(x1, x2, x3, x4, x5, x6) \ +strerr_warnsys(x1, x2, x3, x4, x5, (x6), 0, 0, 0, 0) +#define strerr_warn5sys(x1, x2, x3, x4, x5) \ +strerr_warnsys(x1, x2, x3, x4, (x5), 0, 0, 0, 0, 0) +#define strerr_warn4sys(x1, x2, x3, x4) \ +strerr_warnsys(x1, x2, x3, (x4), 0, 0, 0, 0, 0, 0) +#define strerr_warn3sys(x1, x2, x3) \ +strerr_warnsys(x1, x2, (x3), 0, 0, 0, 0, 0, 0, 0) +#define strerr_warn2sys(x1, x2) \ +strerr_warnsys(x1, (x2), 0, 0, 0, 0, 0, 0, 0, 0) +#define strerr_warn1sys(x1) \ +strerr_warnsys((x1), 0, 0, 0, 0, 0, 0, 0, 0, 0) + +#define strerr_warn10x(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) \ +strerr_warn(x1, x2, x3, x4, x5, x6, x7, x8, x9, (x10), 0) +#define strerr_warn9x(x1, x2, x3, x4, x5, x6, x7, x8, x9) \ +strerr_warn(x1, x2, x3, x4, x5, x6, x7, x8, (x9), 0, 0) +#define strerr_warn8x(x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_warn(x1, x2, x3, x4, x5, x6, x7, (x8), 0, 0, 0) +#define strerr_warn7x(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn(x1, x2, x3, x4, x5, x6, (x7), 0, 0, 0, 0) +#define strerr_warn6x(x1, x2, x3, x4, x5, x6) \ +strerr_warn(x1, x2, x3, x4, x5, (x6), 0, 0, 0, 0, 0) +#define strerr_warn5x(x1, x2, x3, x4, x5) \ +strerr_warn(x1, x2, x3, x4, (x5), 0, 0, 0, 0, 0, 0) +#define strerr_warn4x(x1, x2, x3, x4) \ +strerr_warn(x1, x2, x3, (x4), 0, 0, 0, 0, 0, 0, 0) +#define strerr_warn3x(x1, x2, x3) \ +strerr_warn(x1, x2, (x3), 0, 0, 0, 0, 0, 0, 0, 0) +#define strerr_warn2x(x1, x2) \ +strerr_warn(x1, (x2), 0, 0, 0, 0, 0, 0, 0, 0, 0) +#define strerr_warn1x(x1) \ +strerr_warn((x1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + +#define strerr_die10(e, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, se) \ +strerr_die(e, x1, x2, x3, x4, x5 , x6, x7, x8, x9, x10, se) +#define strerr_die9(e, x1, x2, x3, x4, x5, x6, x7, x8, x9, se) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, x7, x8, x9, 0, se) +#define strerr_die8(e, x1, x2, x3, x4, x5, x6, x7, x8, se) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, x7, x8, 0, 0, se) +#define strerr_die7(e, x1, x2, x3, x4, x5, x6, x7, se) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, x7, 0, 0, 0, se) +#define strerr_die6(e, x1, x2, x3, x4, x5, x6, se) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, 0, 0, 0, 0, se) +#define strerr_die5(e, x1, x2, x3, x4, x5, se) \ +strerr_die(e, x1, x2, x3, x4, x5, 0, 0, 0, 0, 0, se) +#define strerr_die4(e, x1, x2, x3, x4, se) \ +strerr_die(e, x1, x2, x3, x4, 0, 0, 0, 0, 0, 0, se) +#define strerr_die3(e, x1, x2, x3, se) \ +strerr_die(e, x1, x2, x3, 0, 0, 0, 0, 0, 0, 0, se) +#define strerr_die2(e, x1, x2, se) \ +strerr_die(e, x1, x2, 0, 0, 0, 0, 0, 0, 0, 0, se) +#define strerr_die1(e, x1, se) \ +strerr_die(e, x1, 0, 0, 0, 0, 0, 0, 0, 0, 0, se) + +#define strerr_die10sys(e, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) \ +strerr_diesys(e, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) +#define strerr_die9sys(e, x1, x2, x3, x4, x5, x6, x7, x8, x9) \ +strerr_diesys(e, x1, x2, x3, x4, x5, x6, x7, x8, (x9), 0) +#define strerr_die8sys(e, x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_diesys(e, x1, x2, x3, x4, x5, x6, x7, (x8), 0, 0) +#define strerr_die7sys(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_diesys(e, x1, x2, x3, x4, x5, x6, (x7), 0, 0, 0) +#define strerr_die6sys(e, x1, x2, x3, x4, x5, x6) \ +strerr_diesys(e, x1, x2, x3, x4, x5, (x6), 0, 0, 0, 0) +#define strerr_die5sys(e, x1, x2, x3, x4, x5) \ +strerr_diesys(e, x1, x2, x3, x4, (x5), 0, 0, 0, 0, 0) +#define strerr_die4sys(e, x1, x2, x3, x4) \ +strerr_diesys(e, x1, x2, x3, (x4), 0, 0, 0, 0, 0, 0) +#define strerr_die3sys(e, x1, x2, x3) \ +strerr_diesys(e, x1, x2, (x3), 0, 0, 0, 0, 0, 0, 0) +#define strerr_die2sys(e, x1, x2) \ +strerr_diesys(e, x1, (x2), 0, 0, 0, 0, 0, 0, 0, 0) +#define strerr_die1sys(e, x1) \ +strerr_diesys(e, (x1), 0, 0, 0, 0, 0, 0, 0, 0, 0) + +#define strerr_die10x(e, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, x7, x8, x9, (x10), 0) +#define strerr_die9x(e, x1, x2, x3, x4, x5, x6, x7, x8, x9) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, x7, x8, (x9), 0, 0) +#define strerr_die8x(e, x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, x7, (x8), 0, 0, 0) +#define strerr_die7x(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_die(e, x1, x2, x3, x4, x5, x6, (x7), 0, 0, 0, 0) +#define strerr_die6x(e, x1, x2, x3, x4, x5, x6) \ +strerr_die(e, x1, x2, x3, x4, x5, (x6), 0, 0, 0, 0, 0) +#define strerr_die5x(e, x1, x2, x3, x4, x5) \ +strerr_die(e, x1, x2, x3, x4, (x5), 0, 0, 0, 0, 0, 0) +#define strerr_die4x(e, x1, x2, x3, x4) \ +strerr_die(e, x1, x2, x3, (x4), 0, 0, 0, 0, 0, 0, 0) +#define strerr_die3x(e, x1, x2, x3) \ +strerr_die(e, x1, x2, (x3), 0, 0, 0, 0, 0, 0, 0, 0) +#define strerr_die2x(e, x1, x2) \ +strerr_die(e, x1, (x2), 0, 0, 0, 0, 0, 0, 0, 0, 0) +#define strerr_die1x(e, x1) \ +strerr_die(e, (x1), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) + +#endif diff --git a/src/include/skalibs/strerr2.h b/src/include/skalibs/strerr2.h new file mode 100644 index 0000000..fa7cc02 --- /dev/null +++ b/src/include/skalibs/strerr2.h @@ -0,0 +1,267 @@ +/* ISC license. */ + +#ifndef STRERR2_H +#define STRERR2_H + +#include <skalibs/strerr.h> + +extern char const *PROG ; + + +#define strerr_warnw1x(x1) \ +strerr_warn3x(PROG, ": warning: ", (x1)) +#define strerr_warnw2x(x1, x2) \ +strerr_warn4x(PROG, ": warning: ", (x1), x2) +#define strerr_warnw3x(x1, x2, x3) \ +strerr_warn5x(PROG, ": warning: ", (x1), x2, x3) +#define strerr_warnw4x(x1, x2, x3, x4) \ +strerr_warn6x(PROG, ": warning: ", (x1), x2, x3, x4) +#define strerr_warnw5x(x1, x2, x3, x4, x5) \ +strerr_warn7x(PROG, ": warning: ", (x1), x2, x3, x4, x5) +#define strerr_warnw6x(x1, x2, x3, x4, x5, x6) \ +strerr_warn8x(PROG, ": warning: ", (x1), x2, x3, x4, x5, x6) +#define strerr_warnw7x(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn9x(PROG, ": warning: ", (x1), x2, x3, x4, x5, x6, x7) +#define strerr_warnw8x(x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_warn10x(PROG, ": warning: ", (x1), x2, x3, x4, x5, x6, x7, x8) + +#define strerr_warnw1sys(x1) \ +strerr_warn4sys(PROG, ": warning: ", (x1), ": ") +#define strerr_warnw2sys(x1, x2) \ +strerr_warn5sys(PROG, ": warning: ", (x1), (x2), ": ") +#define strerr_warnw3sys(x1, x2, x3) \ +strerr_warn6sys(PROG, ": warning: ", (x1), x2, (x3), ": ") +#define strerr_warnw4sys(x1, x2, x3, x4) \ +strerr_warn7sys(PROG, ": warning: ", (x1), x2, x3, (x4), ": ") +#define strerr_warnw5sys(x1, x2, x3, x4, x5) \ +strerr_warn8sys(PROG, ": warning: ", (x1), x2, x3, x4, (x5), ": ") +#define strerr_warnw6sys(x1, x2, x3, x4, x5, x6) \ +strerr_warn9sys(PROG, ": warning: ", (x1), x2, x3, x4, x5, (x6), ": ") +#define strerr_warnw7sys(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn10sys(PROG, ": warning: ", (x1), x2, x3, x4, x5, x6, (x7), ": ") + +#define strerr_diew1x(e, x1) \ +strerr_die3x(e, PROG, ": warning: ", x1) +#define strerr_diew2x(e, x1, x2) \ +strerr_die4x(e, PROG, ": warning: ", x1, x2) +#define strerr_diew3x(e, x1, x2, x3) \ +strerr_die5x(e, PROG, ": warning: ", x1, x2, x3) +#define strerr_diew4x(e, x1, x2, x3, x4) \ +strerr_die6x(e, PROG, ": warning: ", x1, x2, x3, x4) +#define strerr_diew5x(e, x1, x2, x3, x4, x5) \ +strerr_die7x(e, PROG, ": warning: ", x1, x2, x3, x4, x5) +#define strerr_diew6x(e, x1, x2, x3, x4, x5, x6) \ +strerr_die8x(e, PROG, ": warning: ", x1, x2, x3, x4, x5, x6) +#define strerr_diew7x(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_die9x(e, PROG, ": warning: ", x1, x2, x3, x4, x5, x6, x7) +#define strerr_diew8x(e, x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_die10x(e, PROG, ": warning: ", x1, x2, x3, x4, x5, x6, x7, x8) + +#define strerr_diew1sys(e, x1) \ +strerr_die4sys(e, PROG, ": warning: ", (x1), ": ") +#define strerr_diew2sys(e, x1, x2) \ +strerr_die5sys(e, PROG, ": warning: ", x1, (x2), ": ") +#define strerr_diew3sys(e, x1, x2, x3) \ +strerr_die6sys(e, PROG, ": warning: ", x1, x2, (x3), ": ") +#define strerr_diew4sys(e, x1, x2, x3, x4) \ +strerr_die7sys(e, PROG, ": warning: ", x1, x2, x3, (x4), ": ") +#define strerr_diew5sys(e, x1, x2, x3, x4, x5) \ +strerr_die8sys(e, PROG, ": warning: ", x1, x2, x3, x4, (x5), ": ") +#define strerr_diew6sys(e, x1, x2, x3, x4, x5, x6) \ +strerr_die9sys(e, PROG, ": warning: ", x1, x2, x3, x4, x5, (x6), ": ") +#define strerr_diew7sys(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_die10sys(e, PROG, ": warning: ", x1, x2, x3, x4, x5, x6, (x7), ": ") + +#define strerr_dief1x(e, x1) \ +strerr_die3x(e, PROG, ": fatal: ", x1) +#define strerr_dief2x(e, x1, x2) \ +strerr_die4x(e, PROG, ": fatal: ", x1, x2) +#define strerr_dief3x(e, x1, x2, x3) \ +strerr_die5x(e, PROG, ": fatal: ", x1, x2, x3) +#define strerr_dief4x(e, x1, x2, x3, x4) \ +strerr_die6x(e, PROG, ": fatal: ", x1, x2, x3, x4) +#define strerr_dief5x(e, x1, x2, x3, x4, x5) \ +strerr_die7x(e, PROG, ": fatal: ", x1, x2, x3, x4, x5) +#define strerr_dief6x(e, x1, x2, x3, x4, x5, x6) \ +strerr_die8x(e, PROG, ": fatal: ", x1, x2, x3, x4, x5, x6) +#define strerr_dief7x(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_die9x(e, PROG, ": fatal: ", x1, x2, x3, x4, x5, x6, x7) +#define strerr_dief8x(e, x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_die10x(e, PROG, ": fatal: ", x1, x2, x3, x4, x5, x6, x7, x8) + +#define strerr_dief1sys(e, x1) \ +strerr_die4sys(e, PROG, ": fatal: ", (x1), ": ") +#define strerr_dief2sys(e, x1, x2) \ +strerr_die5sys(e, PROG, ": fatal: ", x1, (x2), ": ") +#define strerr_dief3sys(e, x1, x2, x3) \ +strerr_die6sys(e, PROG, ": fatal: ", x1, x2, (x3), ": ") +#define strerr_dief4sys(e, x1, x2, x3, x4) \ +strerr_die7sys(e, PROG, ": fatal: ", x1, x2, x3, (x4), ": ") +#define strerr_dief5sys(e, x1, x2, x3, x4, x5) \ +strerr_die8sys(e, PROG, ": fatal: ", x1, x2, x3, x4, (x5), ": ") +#define strerr_dief6sys(e, x1, x2, x3, x4, x5, x6) \ +strerr_die9sys(e, PROG, ": fatal: ", x1, x2, x3, x4, x5, (x6), ": ") +#define strerr_dief7sys(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_die10sys(e, PROG, ": fatal: ", x1, x2, x3, x4, x5, x6, (x7), ": ") + +#define strerr_diefu1x(e, x1) \ +strerr_die4x(e, PROG, ": fatal: ", "unable to ", x1) +#define strerr_diefu2x(e, x1, x2) \ +strerr_die5x(e, PROG, ": fatal: ", "unable to ", x1, x2) +#define strerr_diefu3x(e, x1, x2, x3) \ +strerr_die6x(e, PROG, ": fatal: ", "unable to ", x1, x2, x3) +#define strerr_diefu4x(e, x1, x2, x3, x4) \ +strerr_die7x(e, PROG, ": fatal: ", "unable to ", x1, x2, x3, x4) +#define strerr_diefu5x(e, x1, x2, x3, x4, x5) \ +strerr_die8x(e, PROG, ": fatal: ", "unable to ", x1, x2, x3, x4, x5) +#define strerr_diefu6x(e, x1, x2, x3, x4, x5, x6) \ +strerr_die9x(e, PROG, ": fatal: ", "unable to ", x1, x2, x3, x4, x5, x6) +#define strerr_diefu7x(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_die10x(e, PROG, ": fatal: ", "unable to ", x1, x2, x3, x4, x5, x6, x7) + +#define strerr_diefu1sys(e, x1) \ +strerr_die5sys(e, PROG, ": fatal: ", "unable to ", (x1), ": ") +#define strerr_diefu2sys(e, x1, x2) \ +strerr_die6sys(e, PROG, ": fatal: ", "unable to ", x1, (x2), ": ") +#define strerr_diefu3sys(e, x1, x2, x3) \ +strerr_die7sys(e, PROG, ": fatal: ", "unable to ", x1, x2, (x3), ": ") +#define strerr_diefu4sys(e, x1, x2, x3, x4) \ +strerr_die8sys(e, PROG, ": fatal: ", "unable to ", x1, x2, x3, (x4), ": ") +#define strerr_diefu5sys(e, x1, x2, x3, x4, x5) \ +strerr_die9sys(e, PROG, ": fatal: ", "unable to ", x1, x2, x3, x4, (x5), ": ") +#define strerr_diefu6sys(e, x1, x2, x3, x4, x5, x6) \ +strerr_die10sys(e, PROG, ": fatal: ", "unable to ", x1, x2, x3, x4, x5, (x6), ": ") + +#define strerr_warnwu1x(x1) \ +strerr_warn4x(PROG, ": warning: ", "unable to ", (x1)) +#define strerr_warnwu2x(x1, x2) \ +strerr_warn5x(PROG, ": warning: ", "unable to ", (x1), x2) +#define strerr_warnwu3x(x1, x2, x3) \ +strerr_warn6x(PROG, ": warning: ", "unable to ", (x1), x2, x3) +#define strerr_warnwu4x(x1, x2, x3, x4) \ +strerr_warn7x(PROG, ": warning: ", "unable to ", (x1), x2, x3, x4) +#define strerr_warnwu5x(x1, x2, x3, x4, x5) \ +strerr_warn8x(PROG, ": warning: ", "unable to ", (x1), x2, x3, x4, x5) +#define strerr_warnwu6x(x1, x2, x3, x4, x5, x6) \ +strerr_warn9x(PROG, ": warning: ", "unable to ", (x1), x2, x3, x4, x5, x6) +#define strerr_warnwu7x(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn10x(PROG, ": warning: ", "unable to ", (x1), x2, x3, x4, x5, x6, x7) + +#define strerr_warnwu1sys(x1) \ +strerr_warn5sys(PROG, ": warning: ", "unable to ", (x1), ": ") +#define strerr_warnwu2sys(x1, x2) \ +strerr_warn6sys(PROG, ": warning: ", "unable to ", (x1), (x2), ": ") +#define strerr_warnwu3sys(x1, x2, x3) \ +strerr_warn7sys(PROG, ": warning: ", "unable to ", (x1), x2, (x3), ": ") +#define strerr_warnwu4sys(x1, x2, x3, x4) \ +strerr_warn8sys(PROG, ": warning: ", "unable to ", (x1), x2, x3, (x4), ": ") +#define strerr_warnwu5sys(x1, x2, x3, x4, x5) \ +strerr_warn9sys(PROG, ": warning: ", "unable to ", (x1), x2, x3, x4, (x5), ": ") +#define strerr_warnwu6sys(x1, x2, x3, x4, x5, x6) \ +strerr_warn10sys(PROG, ": warning: ", "unable to ", (x1), x2, x3, x4, x5, (x6), ": ") + +#define strerr_diewu1x(e, x1) \ +strerr_die4x(e, PROG, ": warning: ", "unable to ", x1) +#define strerr_diewu2x(e, x1, x2) \ +strerr_die5x(e, PROG, ": warning: ", "unable to ", x1, x2) +#define strerr_diewu3x(e, x1, x2, x3) \ +strerr_die6x(e, PROG, ": warning: ", "unable to ", x1, x2, x3) +#define strerr_diewu4x(e, x1, x2, x3, x4) \ +strerr_die7x(e, PROG, ": warning: ", "unable to ", x1, x2, x3, x4) +#define strerr_diewu5x(e, x1, x2, x3, x4, x5) \ +strerr_die8x(e, PROG, ": warning: ", "unable to ", x1, x2, x3, x4, x5) +#define strerr_diewu6x(e, x1, x2, x3, x4, x5, x6) \ +strerr_die9x(e, PROG, ": warning: ", "unable to ", x1, x2, x3, x4, x5, x6) +#define strerr_diewu7x(e, x1, x2, x3, x4, x5, x6, x7) \ +strerr_die10x(e, PROG, ": warning: ", "unable to ", x1, x2, x3, x4, x5, x6, x7) + +#define strerr_diewu1sys(e, x1) \ +strerr_die5sys(e, PROG, ": warning: ", "unable to ", (x1), ": ") +#define strerr_diewu2sys(e, x1, x2) \ +strerr_die6sys(e, PROG, ": warning: ", "unable to ", x1, (x2), ": ") +#define strerr_diewu3sys(e, x1, x2, x3) \ +strerr_die7sys(e, PROG, ": warning: ", "unable to ", x1, x2, (x3), ": ") +#define strerr_diewu4sys(e, x1, x2, x3, x4) \ +strerr_die8sys(e, PROG, ": warning: ", "unable to ", x1, x2, x3, (x4), ": ") +#define strerr_diewu5sys(e, x1, x2, x3, x4, x5) \ +strerr_die9sys(e, PROG, ": warning: ", "unable to ", x1, x2, x3, x4, (x5), ": ") +#define strerr_diewu6sys(e, x1, x2, x3, x4, x5, x6) \ +strerr_die10sys(e, PROG, ": warning: ", "unable to ", x1, x2, x3, x4, x5, (x6), ": ") + +#define strerr_dieusage(e, u) \ +strerr_die3x(e, PROG, ": usage: ", u) + +#define strerr_dienotset(e, x) \ +strerr_dief2x(e, (x), " not set") + +#define strerr_dieinvalid(e, x) \ +strerr_dief2x(e, "invalid $", x) + +#define strerr_dieexec(e, x) \ +strerr_diefu2sys(e, "exec ", x) + +#define strerr_warni1x(x1) \ +strerr_warn3x(PROG, ": info: ", (x1)) +#define strerr_warni2x(x1, x2) \ +strerr_warn4x(PROG, ": info: ", (x1), x2) +#define strerr_warni3x(x1, x2, x3) \ +strerr_warn5x(PROG, ": info: ", (x1), x2, x3) +#define strerr_warni4x(x1, x2, x3, x4) \ +strerr_warn6x(PROG, ": info: ", (x1), x2, x3, x4) +#define strerr_warni5x(x1, x2, x3, x4, x5) \ +strerr_warn7x(PROG, ": info: ", (x1), x2, x3, x4, x5) +#define strerr_warni6x(x1, x2, x3, x4, x5, x6) \ +strerr_warn8x(PROG, ": info: ", (x1), x2, x3, x4, x5, x6) +#define strerr_warni7x(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn9x(PROG, ": info: ", (x1), x2, x3, x4, x5, x6, x7) +#define strerr_warni8x(x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_warn10x(PROG, ": info: ", (x1), x2, x3, x4, x5, x6, x7, x8) + +#define strerr_warni1sys(x1) \ +strerr_warn4sys(PROG, ": info: ", (x1), ": ") +#define strerr_warni2sys(x1, x2) \ +strerr_warn5sys(PROG, ": info: ", (x1), (x2), ": ") +#define strerr_warni3sys(x1, x2, x3) \ +strerr_warn6sys(PROG, ": info: ", (x1), x2, (x3), ": ") +#define strerr_warni4sys(x1, x2, x3, x4) \ +strerr_warn7sys(PROG, ": info: ", (x1), x2, x3, (x4), ": ") +#define strerr_warni5sys(x1, x2, x3, x4, x5) \ +strerr_warn8sys(PROG, ": info: ", (x1), x2, x3, x4, (x5), ": ") +#define strerr_warni6sys(x1, x2, x3, x4, x5, x6) \ +strerr_warn9sys(PROG, ": info: ", (x1), x2, x3, x4, x5, (x6), ": ") +#define strerr_warni7sys(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn10sys(PROG, ": info: ", (x1), x2, x3, x4, x5, x6, (x7), ": ") + +#define strerr_warnt1x(x1) \ +strerr_warn3x(PROG, ": tracing: ", (x1)) +#define strerr_warnt2x(x1, x2) \ +strerr_warn4x(PROG, ": tracing: ", (x1), x2) +#define strerr_warnt3x(x1, x2, x3) \ +strerr_warn5x(PROG, ": tracing: ", (x1), x2, x3) +#define strerr_warnt4x(x1, x2, x3, x4) \ +strerr_warn6x(PROG, ": tracing: ", (x1), x2, x3, x4) +#define strerr_warnt5x(x1, x2, x3, x4, x5) \ +strerr_warn7x(PROG, ": tracing: ", (x1), x2, x3, x4, x5) +#define strerr_warnt6x(x1, x2, x3, x4, x5, x6) \ +strerr_warn8x(PROG, ": tracing: ", (x1), x2, x3, x4, x5, x6) +#define strerr_warnt7x(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn9x(PROG, ": tracing: ", (x1), x2, x3, x4, x5, x6, x7) +#define strerr_warnt8x(x1, x2, x3, x4, x5, x6, x7, x8) \ +strerr_warn10x(PROG, ": tracing: ", (x1), x2, x3, x4, x5, x6, x7, x8) + +#define strerr_warnt1sys(x1) \ +strerr_warn4sys(PROG, ": tracing: ", (x1), ": ") +#define strerr_warnt2sys(x1, x2) \ +strerr_warn5sys(PROG, ": tracing: ", (x1), (x2), ": ") +#define strerr_warnt3sys(x1, x2, x3) \ +strerr_warn6sys(PROG, ": tracing: ", (x1), x2, (x3), ": ") +#define strerr_warnt4sys(x1, x2, x3, x4) \ +strerr_warn7sys(PROG, ": tracing: ", (x1), x2, x3, (x4), ": ") +#define strerr_warnt5sys(x1, x2, x3, x4, x5) \ +strerr_warn8sys(PROG, ": tracing: ", (x1), x2, x3, x4, (x5), ": ") +#define strerr_warnt6sys(x1, x2, x3, x4, x5, x6) \ +strerr_warn9sys(PROG, ": tracing: ", (x1), x2, x3, x4, x5, (x6), ": ") +#define strerr_warnt7sys(x1, x2, x3, x4, x5, x6, x7) \ +strerr_warn10sys(PROG, ": tracing: ", (x1), x2, x3, x4, x5, x6, (x7), ": ") + +#endif diff --git a/src/include/skalibs/surf.h b/src/include/skalibs/surf.h new file mode 100644 index 0000000..6422903 --- /dev/null +++ b/src/include/skalibs/surf.h @@ -0,0 +1,29 @@ +/* ISC license. */ + +#ifndef SKALIBS_SURF_H +#define SKALIBS_SURF_H + +#include <skalibs/uint32.h> + +typedef struct SURFSchedule SURFSchedule, *SURFSchedule_ref, **SURFSchedule_ref_ref ; +struct SURFSchedule +{ + uint32 seed[32] ; + uint32 in[12] ; + char out[32] ; + unsigned int pos ; +} ; + +#define SURFSCHEDULE_ZERO { .seed = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .in = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, .out = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", .pos = 32 } + +extern void surf_sinit (SURFSchedule_ref, char const *) ; /* 160 chars */ +extern void surf (SURFSchedule_ref, char *, unsigned int) ; + + + /* The following functions need libstdcrypto.a */ + +extern void surf_makeseed (char *) ; /* fills 160 chars */ +extern void surf_init (SURFSchedule_ref) ; +extern void surf_autoinit (SURFSchedule_ref, char *, unsigned int) ; + +#endif diff --git a/src/include/skalibs/tai.h b/src/include/skalibs/tai.h new file mode 100644 index 0000000..cce5670 --- /dev/null +++ b/src/include/skalibs/tai.h @@ -0,0 +1,140 @@ +/* ISC license. */ + +#ifndef TAI_H +#define TAI_H + +#include <sys/types.h> +#include <time.h> +#include <sys/time.h> +#include <skalibs/gccattributes.h> +#include <skalibs/uint32.h> +#include <skalibs/uint64.h> + +typedef struct tai_s tai_t, *tai_t_ref ; +struct tai_s +{ + uint64 x ; +} ; + + +#define TAI_ZERO { .x = 0 } +#define TAI_MAGIC ((uint64)4611686018427387904ULL) +#define TAI_EPOCH { .x = TAI_MAGIC + 10UL } +#define TAI_INFINITE_RELATIVE { .x = ((uint64)1 << 60) } +#define TAI_INFINITE { .x = TAI_MAGIC + ((uint64)1 << 61) } + +#define tai_sec(t) ((t)->x) +#define tai_u64(t, u) ((void)((t)->x = (u))) +#define tai_unix(t, u) tai_u64(t, (TAI_MAGIC + (u))) +#define tai_uint(t, u) tai_u64(t, (uint64)(u)) + +extern int tai_relative_from_timeval (tai_t *, struct timeval const *) ; +extern int tai_from_timeval (tai_t *, struct timeval const *) ; +extern int timeval_from_tai_relative (struct timeval *, tai_t const *) ; +extern int timeval_from_tai (struct timeval *, tai_t const *) ; + +extern int tai_relative_from_timespec (tai_t *, struct timespec const *) ; +extern int tai_from_timespec (tai_t *, struct timespec const *) ; +extern int timespec_from_tai_relative (struct timespec *, tai_t const *) ; +extern int timespec_from_tai (struct timespec *, tai_t const *) ; + +extern int tai_now (tai_t *) ; +extern int tai_from_sysclock (tai_t *, uint64) ; +extern int sysclock_from_tai (uint64 *, tai_t const *) ; + +#define tai_approx(t) ((double)(tai_sec(t))) + +extern void tai_add (tai_t *, tai_t const *, tai_t const *) ; +extern void tai_sub (tai_t *, tai_t const *, tai_t const *) ; +#define tai_less(t,u) (tai_sec(t) < tai_sec(u)) + +#define TAI_PACK 8 +extern void tai_pack (char *, tai_t const *) ; +extern void tai_unpack (char const *, tai_t *) ; +extern void tai_pack_little (char *, tai_t const *) ; +extern void tai_unpack_little (char const *, tai_t *) ; + +typedef struct tain_s tain_t, *tain_t_ref ; +struct tain_s +{ + tai_t sec ; + uint32 nano ; /* 0..999999999U */ +} ; + +#define TAIN_ZERO { .sec = TAI_ZERO, .nano = 0 } +#define TAIN_EPOCH { .sec = TAI_EPOCH, .nano = 0 } +#define TAIN_INFINITE { .sec = TAI_INFINITE, .nano = 0 } +#define TAIN_INFINITE_RELATIVE { .sec = TAI_INFINITE_RELATIVE, .nano = 0 } +#define TAIN_NANO500 { .sec = TAI_ZERO, .nano = 500 } + +extern tain_t STAMP ; /* the global process wallclock */ +extern tain_t const tain_infinite_relative ; +extern tain_t const tain_nano500 ; + +#define tain_sec(a) ((a)->sec) +#define tain_secp(a) (&(a)->sec) +#define tain_nano(a) ((a)->nano) + +extern int tain_relative_from_timeval (tain_t *, struct timeval const *) ; +extern int tain_from_timeval (tain_t *, struct timeval const *) ; +extern int timeval_from_tain_relative (struct timeval *, tain_t const *) ; +extern int timeval_from_tain (struct timeval *, tain_t const *) ; + +extern int tain_relative_from_timespec (tain_t *, struct timespec const *) ; +extern int tain_from_timespec (tain_t *, struct timespec const *) ; +extern int timespec_from_tain_relative (struct timespec *, tain_t const *) ; +extern int timespec_from_tain (struct timespec *, tain_t const *) ; + +extern int sysclock_get (tain_t *) ; +extern int tain_sysclock (tain_t *) ; +#define tain_sysclock_g() tain_sysclock(&STAMP) +extern int tain_clockmon_init (tain_t *) ; +extern int tain_clockmon (tain_t *, tain_t const *) ; +#define tain_clockmon_g(offset) tain_clockmon(&STAMP, (offset)) +extern int tain_init (void) ; +extern int tain_now (tain_t *) ; +#define tain_now_g() tain_now(&STAMP) +#define tain_copynow(t) (*(t) = STAMP) + +extern int sysclock_set (tain_t const *) ; +extern int tain_setnow (tain_t const *) ; + +extern double tain_approx (tain_t const *) gccattr_pure ; +extern double tain_frac (tain_t const *) gccattr_pure ; + +extern int tain_from_millisecs (tain_t *, int) ; +extern int tain_to_millisecs (tain_t const *) gccattr_pure ; + +extern void tain_add (tain_t *, tain_t const *, tain_t const *) ; +#define tain_add_g(deadline, tto) tain_add(deadline, &STAMP, tto) +extern void tain_addsec (tain_t *, tain_t const *, int) ; +#define tain_addsec_g(deadline, n) tain_addsec(deadline, &STAMP, n) +extern void tain_sub (tain_t *, tain_t const *, tain_t const *) ; +extern void tain_half (tain_t *, tain_t const *) ; +extern int tain_less (tain_t const *, tain_t const *) gccattr_pure ; +#define tain_future(deadline) tain_less(&STAMP, (deadline)) + +#define TAIN_PACK 12 +extern void tain_pack (char *, tain_t const *) ; +extern void tain_unpack (char const *, tain_t *) ; +extern void tain_pack_little (char *, tain_t const *) ; +extern void tain_unpack_little (char const *, tain_t *) ; + +#define TAIN_FMT 25 +extern unsigned int tain_fmt (char *, tain_t const *) ; +extern unsigned int tain_scan (char const *, tain_t *) ; + +#define TAIN_FMTFRAC 19 +extern unsigned int tain_fmtfrac (char *, tain_t const *) ; + +#define tain_uint(a, u) tain_ulong(a, u) +extern void tain_ulong (tain_t *, unsigned long) ; + +#define TIMESTAMP (1 + (TAIN_PACK << 1)) +extern unsigned int timestamp_fmt (char *, tain_t const *) ; +extern unsigned int timestamp_scan (char const *, tain_t *) ; +extern int timestamp_r (char *, tain_t *) ; +extern int timestamp (char *) ; +#define timestamp_g(s) timestamp_fmt((s), &STAMP) + +#endif diff --git a/src/include/skalibs/unirandom.h b/src/include/skalibs/unirandom.h new file mode 100644 index 0000000..fc87886 --- /dev/null +++ b/src/include/skalibs/unirandom.h @@ -0,0 +1,57 @@ +/* ISC license. */ + +#ifndef UNIRANDOM_H +#define UNIRANDOM_H + +#include <skalibs/buffer.h> +#include <skalibs/surf.h> + +#define RANDOMBUF_BUFSIZE 257 + +struct randombuf_s +{ + char buf[RANDOMBUF_BUFSIZE] ; + buffer b ; + unsigned int nb : 1 ; +} ; +#define RANDOMBUF_ZERO { .buf = "", .b = BUFFER_INIT(0, -1, 0, 0), .nb = 0 } + +struct randomegd_s +{ + int fd ; +} ; + +union unirandominfo +{ + SURFSchedule surf_ctx ; + struct randombuf_s device ; + struct randomegd_s egd ; +} ; + +#define UNIRANDOMINFO_ZERO { .surf_ctx = SURFSCHEDULE_ZERO } + +typedef struct unirandom unirandom, *unirandom_ref, **unirandom_ref_ref ; +struct unirandom +{ + unsigned int (*readb) (union unirandominfo *, char *, unsigned int) ; + unsigned int (*readnb) (union unirandominfo *, char *, unsigned int) ; + int (*init) (union unirandominfo *) ; + int (*finish) (union unirandominfo *) ; + union unirandominfo data ; + unsigned int initted : 1 ; +} ; + +#define UNIRANDOM_ZERO { .readb = 0, .readnb = 0, .init = 0, .finish = 0, .data = UNIRANDOMINFO_ZERO, .initted = 0 } + +extern int unirandom_register_devrandom (unirandom_ref) ; +extern int unirandom_register_devurandom (unirandom_ref) ; +extern int unirandom_register_hasegd (unirandom_ref) ; +extern int unirandom_register_surf (unirandom_ref) ; + +extern int unirandom_init (unirandom_ref) ; +extern unsigned int unirandom_readb (unirandom_ref, char *, unsigned int) ; +extern unsigned int unirandom_readnb (unirandom_ref, char *, unsigned int) ; +extern int unirandom_finish (unirandom_ref) ; + + +#endif diff --git a/src/include/skalibs/unirandomdev.h b/src/include/skalibs/unirandomdev.h new file mode 100644 index 0000000..589a1c1 --- /dev/null +++ b/src/include/skalibs/unirandomdev.h @@ -0,0 +1,19 @@ +/* ISC license. */ + +#ifndef UNIRANDOMDEV_H +#define UNIRANDOMDEV_H + +#include <skalibs/unirandom.h> + +extern int unirandomdev_sinit (union unirandominfo *, char const *) ; +extern unsigned int unirandomdev_readb (union unirandominfo *, char *, unsigned int) ; +extern unsigned int unirandomdev_readnb (union unirandominfo *, char *, unsigned int) ; +extern int unirandomdev_finish (union unirandominfo *) ; + +extern int unidevrandom_init (union unirandominfo *) ; +extern int unidevurandom_init (union unirandominfo *) ; + +#define UNIRANDOM_REGISTER_DEVRANDOM() { .readb = &unirandomdev_readb, .readnb = &unirandomdev_readnb, .init = &unidevrandom_init, .finish = &unirandomdev_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } +#define UNIRANDOM_REGISTER_DEVURANDOM() { .readb = &unirandomdev_readb, .readnb = &unirandomdev_readnb, .init = &unidevurandom_init, .finish = &unirandomdev_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } + +#endif diff --git a/src/include/skalibs/unirandomegd.h b/src/include/skalibs/unirandomegd.h new file mode 100644 index 0000000..11bb464 --- /dev/null +++ b/src/include/skalibs/unirandomegd.h @@ -0,0 +1,17 @@ +/* ISC license. */ + +#ifndef UNIRANDOMEGD_H +#define UNIRANDOMEGD_H + +#include <skalibs/unirandom.h> + +extern int unirandomegd_sinit (union unirandominfo *, char const *) ; +extern unsigned int unirandomegd_readb (union unirandominfo *, char *, unsigned int) ; +extern unsigned int unirandomegd_readnb (union unirandominfo *, char *, unsigned int) ; +extern int unirandomegd_finish (union unirandominfo *) ; + +extern int unihasegd_init (union unirandominfo *) ; + +#define UNIRANDOM_REGISTER_HASEGD() { .readb = &unirandomegd_readb, .readnb = &unirandomegd_readnb, .init = &unihasegd_init, .finish = &unirandomegd_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } + +#endif diff --git a/src/include/skalibs/unisurf.h b/src/include/skalibs/unisurf.h new file mode 100644 index 0000000..5c95c82 --- /dev/null +++ b/src/include/skalibs/unisurf.h @@ -0,0 +1,16 @@ +/* ISC license. */ + +#ifndef UNISURF_H +#define UNISURF_H + +#include <skalibs/unirandom.h> + +extern int unisurf_sinit (union unirandominfo *, char const *) ; +extern unsigned int unisurf_read (union unirandominfo *, char *, unsigned int) ; +extern int unisurf_finish (union unirandominfo *) ; + +extern int unisurf_init (union unirandominfo *) ; + +#define UNIRANDOM_REGISTER_SURF() { .readb = &unisurf_read, .readnb = &unisurf_read, .init = &unisurf_init, .finish = &unisurf_finish, .data = UNIRANDOMINFO_ZERO, .initted = 0 } + +#endif diff --git a/src/include/skalibs/unix-timed.h b/src/include/skalibs/unix-timed.h new file mode 100644 index 0000000..f5a25b6 --- /dev/null +++ b/src/include/skalibs/unix-timed.h @@ -0,0 +1,36 @@ +/* ISC license. */ + +#ifndef UNIX_TIMED_H +#define UNIX_TIMED_H + +#include <skalibs/bufalloc.h> +#include <skalibs/buffer.h> +#include <skalibs/functypes.h> +#include <skalibs/stralloc.h> +#include <skalibs/tai.h> + + /* Timed "blocking" operations (the fd must still be non-blocking) */ + + +extern int timed_flush (void *, initfunc_t_ref, initfunc_t_ref, initfunc_t_ref, tain_t const *, tain_t *) ; +#define timed_flush_g(b, getfd, isnonempty, flush, deadline) timed_flush(b, getfd, isnonempty, flush, (deadline_), &STAMP) +extern int timed_get (void *, initfunc_t_ref, initfunc_t_ref, tain_t const *, tain_t *) ; +#define timed_get_g (b, getfd, get, deadline) timed_get(b, getfd, get, (deadline), &STAMP) + +extern int buffer_timed_fill (buffer *, tain_t const *, tain_t *) ; +#define buffer_timed_fill_g(b, deadline) buffer_timed_fill(b, (deadline), &STAMP) +extern int bufalloc_timed_flush (bufalloc *, tain_t const *, tain_t *) ; +#define bufalloc_timed_flush_g(ba, deadline) bufalloc_timed_flush(ba, (deadline), &STAMP) +extern int buffer_timed_flush (buffer *, tain_t const *, tain_t *) ; +#define buffer_timed_flush_g(b, deadline) buffer_timed_flush(b, (deadline), &STAMP) +extern unsigned int buffer_timed_get (buffer *, char *, unsigned int, tain_t const *, tain_t *) ; +#define buffer_timed_get_g(b, buf, buflen, deadline) buffer_timed_get(b, buf, buflen, (deadline), &STAMP) + +extern int timed_getln (buffer_ref, stralloc *, char, tain_t const *, tain_t *) ; +#define timed_getln_g(b, sa, sep, deadline) timed_getln(b, sa, sep, (deadline), &STAMP) +extern int timed_getlnmax (buffer_ref, char *, unsigned int, unsigned int *, char, tain_t const *, tain_t *) ; +#define timed_getlnmax_g(b, max, maxlen, len, sep, deadline) timed_getlnmax(b, max, maxlen, len, sep, (deadline), &STAMP) +extern int netstring_timed_get (buffer *, stralloc *, tain_t const *, tain_t *) ; +#define netstring_timed_get_g(b, sa, deadline) netstring_timed_get(b, sa, (deadline), &STAMP) + +#endif diff --git a/src/include/skalibs/unix-transactional.h b/src/include/skalibs/unix-transactional.h new file mode 100644 index 0000000..e1545e0 --- /dev/null +++ b/src/include/skalibs/unix-transactional.h @@ -0,0 +1,59 @@ +/* ISC license. */ + +#ifndef UNIX_TRANSACTIONAL_H +#define UNIX_TRANSACTIONAL_H + +#include <skalibs/uint64.h> +#include <skalibs/stralloc.h> + + /* Transactional/reliable filesystem operations */ + +extern int open2_at (int, char const *, int) ; +extern int open3_at (int, char const *, int, unsigned int) ; + +extern int opengetlnclose (char const *, stralloc *, int) ; + +extern int open_readat (int, char const *) ; +extern int open_readatb (int, char const *) ; +extern int open_writeat (int, char const *) ; +extern int open_writeatb (int, char const *) ; +extern int open_truncat (int, char const *) ; +extern int open_truncatb (int, char const *) ; +extern int open_appendat (int, char const *) ; +extern int open_appendatb (int, char const *) ; + +extern unsigned int openreadnclose_at (int, char const *, char *, unsigned int) ; +extern int openslurpclose_at (int, char const *, stralloc *) ; +extern int opengetlnclose_at (int, char const *, stralloc *, int) ; + +extern int openwritenclose (char const *, char const *, unsigned int) ; +extern int openwritenclose_devino (char const *, char const *, unsigned int, uint64 *, uint64 *) ; +extern int openwritenclose_tmp (char const *, char const *, unsigned int, stralloc *) ; +extern int openwritenclose_devino_tmp (char const *, char const *, unsigned int, uint64 *, uint64 *, stralloc *) ; +extern unsigned int openwritenclose_at (int, char const *, char const *, unsigned int) ; + +extern int mkdir_unique (stralloc *, char const *, unsigned int) ; + +typedef struct dirdescriptor_s dirdescriptor_t, *dirdescriptor_t_ref ; +struct dirdescriptor_s +{ + int fd ; + char const *lnkfn ; + stralloc new ; +} ; + +#define DIRDESCRIPTOR_ZERO { .fd = 0, .lnkfn = 0, .new = STRALLOC_ZERO } + +extern int dd_open_read (dirdescriptor_t_ref, char const *) ; +extern int dd_open_write (dirdescriptor_t_ref, char const *, unsigned int) ; +extern int dd_close (dirdescriptor_t_ref) ; /* after dd_open_read */ +extern void dd_cancel (dirdescriptor_t_ref) ; /* after dd_open_write */ +extern int dd_commit (dirdescriptor_t_ref) ; /* after dd_open_write */ +extern int dd_commit_devino (dirdescriptor_t_ref, uint64 *, uint64 *) ; /* after dd_open_write */ + +#define dd_openreadnclose(blah, file, s, len) openreadnclose_at((blah)->fd, file, s, len) +#define dd_openslurpclose(blah, file, sa) openslurpclose_at((blah)->fd, file, sa) +#define dd_opengetlnclose(blah, file, sa, sep) opengetlnclose_at((blah)->fd, file, sa, sep) +#define dd_openwritenclose(blah, file, s, len) openwritenclose_at((blah)->fd, file, s, len) + +#endif diff --git a/src/include/skalibs/unixmessage.h b/src/include/skalibs/unixmessage.h new file mode 100644 index 0000000..69d6717 --- /dev/null +++ b/src/include/skalibs/unixmessage.h @@ -0,0 +1,103 @@ +/* ISC license. */ + +#ifndef UNIXMESSAGE_H +#define UNIXMESSAGE_H + +#include <skalibs/buffer.h> +#include <skalibs/cbuffer.h> +#include <skalibs/gccattributes.h> +#include <skalibs/genalloc.h> +#include <skalibs/siovec.h> +#include <skalibs/stralloc.h> +#include <skalibs/tai.h> + +typedef struct unixmessage_s unixmessage_t, *unixmessage_t_ref ; +struct unixmessage_s +{ + char *s ; + unsigned int len ; + int *fds ; + unsigned int nfds ; +} ; +#define UNIXMESSAGE_ZERO { .s = 0, .len = 0, .fds = 0, .nfds = 0 } +extern unixmessage_t const unixmessage_zero ; + +typedef struct unixmessage_v_s unixmessage_v_t, *unixmessage_v_t_ref ; +struct unixmessage_v_s +{ + siovec_t *v ; + unsigned int vlen ; + int *fds ; + unsigned int nfds ; +} ; +#define UNIXMESSAGE_V_ZERO { .v = 0, .vlen = 0, .fds = 0, .nfds = 0 } +extern unixmessage_v_t const unixmessage_v_zero ; + + +#define UNIXMESSAGE_BUFSIZE 2049 +#define UNIXMESSAGE_AUXBUFSIZE 2049 +#define UNIXMESSAGE_MAXFDS 256 +#define UNIXMESSAGE_MAXREADS 32 + +typedef struct unixmessage_sender_s unixmessage_sender_t, *unixmessage_sender_t_ref ; +struct unixmessage_sender_s +{ + int fd ; + stralloc data ; + genalloc fds ; /* int */ + genalloc offsets ; /* diuint */ + unsigned int head ; +} ; +#define UNIXMESSAGE_SENDER_ZERO UNIXMESSAGE_SENDER_INIT(-1) +#define UNIXMESSAGE_SENDER_INIT(s) { .fd = (s), .data = STRALLOC_ZERO, .fds = GENALLOC_ZERO, .offsets = GENALLOC_ZERO, .head = 0 } + +extern unixmessage_sender_t const unixmessage_sender_zero ; +extern void unixmessage_sender_init (unixmessage_sender_t *, int) ; +extern void unixmessage_sender_free (unixmessage_sender_t *) ; +#define unixmessage_sender_fd(b) ((b)->fd) +extern int unixmessage_sender_getfd (unixmessage_sender_t const *) gccattr_pure ; + +extern int unixmessage_put_and_close (unixmessage_sender_t *, unixmessage_t const *, unsigned char const *) ; +#define unixmessage_put(b, m) unixmessage_put_and_close(b, m, unixmessage_bits_closenone) +extern int unixmessage_putv_and_close (unixmessage_sender_t *, unixmessage_v_t const *, unsigned char const *) ; +#define unixmessage_putv(b, m) unixmessage_putv_and_close(b, m, unixmessage_bits_closenone) + +extern unsigned char const *const unixmessage_bits_closenone ; +extern unsigned char const *const unixmessage_bits_closeall ; + +extern int unixmessage_sender_flush (unixmessage_sender_t *) ; +extern int unixmessage_sender_timed_flush (unixmessage_sender_t *, tain_t const *, tain_t *) ; +#define unixmessage_sender_timed_flush_g(sender, deadline) unixmessage_sender_timed_flush(sender, (deadline), &STAMP) + + +typedef struct unixmessage_receiver_s unixmessage_receiver_t, *unixmessage_receiver_t_ref ; +struct unixmessage_receiver_s +{ + buffer mainb ; + cbuffer_t auxb ; + unsigned int mainlen ; + unsigned int auxlen ; + stralloc data ; + int fds[UNIXMESSAGE_MAXFDS] ; + unsigned int auxw ; +} ; +#define UNIXMESSAGE_RECEIVER_ZERO { .mainb = BUFFER_ZERO, .auxb = CBUFFER_ZERO, .mainlen = 0, .auxlen = 0, .data = STRALLOC_ZERO, .fds = { -1 }, .auxw = 0 } + +extern int unixmessage_receiver_init (unixmessage_receiver_t *, int, char *, unsigned int, char *, unsigned int) ; +extern void unixmessage_receiver_free (unixmessage_receiver_t *) ; +#define unixmessage_receiver_fd(b) buffer_fd(&(b)->mainb) + +extern int unixmessage_receive (unixmessage_receiver_t *, unixmessage_t *) ; +extern int unixmessage_timed_receive (unixmessage_receiver_t *, unixmessage_t *, tain_t const *, tain_t *) ; +#define unixmessage_timed_receive_g(receiver, msg, deadline) unixmessage_timed_receive(receiver, msg, (deadline), &STAMP) + +extern buffer_io_func_t unixmessage_read ; + +typedef int unixmessage_handler_func_t (unixmessage_t const *, void *) ; +typedef unixmessage_handler_func_t *unixmessage_handler_func_t_ref ; + +extern int unixmessage_handle (unixmessage_receiver_t *, unixmessage_handler_func_t *, void *) ; +extern int unixmessage_timed_handle (unixmessage_receiver_t *, unixmessage_handler_func_t *, void *, tain_t const *, tain_t *) ; +#define unixmessage_timed_handle_g(b, f, p, deadline) unixmessage_timed_handle(b, f, p, (deadline), &STAMP) + +#endif diff --git a/src/include/skalibs/unixonacid.h b/src/include/skalibs/unixonacid.h new file mode 100644 index 0000000..a150e39 --- /dev/null +++ b/src/include/skalibs/unixonacid.h @@ -0,0 +1,12 @@ +/* ISC license. */ + +#ifndef UNIXONACID_H +#define UNIXONACID_H + +#include <skalibs/unix-transactional.h> +#include <skalibs/unix-timed.h> +#include <skalibs/unixmessage.h> +#include <skalibs/kolbak.h> +#include <skalibs/skaclient.h> + +#endif diff --git a/src/include/skalibs/webipc.h b/src/include/skalibs/webipc.h new file mode 100644 index 0000000..6b2c1fe --- /dev/null +++ b/src/include/skalibs/webipc.h @@ -0,0 +1,60 @@ +/* ISC license. */ + +#ifndef WEBIPC_H +#define WEBIPC_H + + /* + UNIX domain socket functions. + "web" stands for William E. Baxter, the original author, who kindly + permitted me to modify and release his code as a part of skalibs. + It has nothing to do with the World Wide Web. + */ + +#define IPCPATH_MAX 107 + +#include <skalibs/tai.h> +#include <skalibs/djbunix.h> + +#define ipc_stream() ipc_stream_nb() +#define ipc_stream_b() ipc_stream_internal(0) +#define ipc_stream_nb() ipc_stream_internal(DJBUNIX_FLAG_NB) +#define ipc_stream_coe() ipc_stream_internal(DJBUNIX_FLAG_COE) +#define ipc_stream_nbcoe() ipc_stream_internal(DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int ipc_stream_internal (unsigned int) ; + +#define ipc_datagram() ipc_datagram_nb() +#define ipc_datagram_b() ipc_datagram_internal(0) +#define ipc_datagram_nb() ipc_datagram_internal(DJBUNIX_FLAG_NB) +#define ipc_datagram_coe() ipc_datagram_internal(DJBUNIX_FLAG_COE) +#define ipc_datagram_nbcoe() ipc_datagram_internal(DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int ipc_datagram_internal (unsigned int) ; + +#define ipc_pair(sv) ipc_pair_nb(sv) +#define ipc_pair_b(sv) ipc_pair_internal((sv), 0) +#define ipc_pair_nb(sv) ipc_pair_internal((sv), DJBUNIX_FLAG_NB) +#define ipc_pair_coe(sv) ipc_pair_internal((sv), DJBUNIX_FLAG_COE) +#define ipc_pair_nbcoe(sv) ipc_pair_internal((sv), DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int ipc_pair_internal (int *, unsigned int) ; + +extern int ipc_bind (int, char const *) ; +extern int ipc_bind_reuse (int, char const *) ; +extern int ipc_listen (int, int) ; + +#define ipc_accept(s, path, len, trunc) ipc_accept_internal(s, path, len, (trunc), 0) +#define ipc_accept_nb(s, path, len, trunc) ipc_accept_internal(s, path, len, (trunc), DJBUNIX_FLAG_NB) +#define ipc_accept_coe(s, path, len, trunc) ipc_accept_internal(s, path, len, (trunc), DJBUNIX_FLAG_COE) +#define ipc_accept_nbcoe(s, path, len, trunc) ipc_accept_internal(s, path, len, (trunc), DJBUNIX_FLAG_NB|DJBUNIX_FLAG_COE) +extern int ipc_accept_internal (int, char *, unsigned int, int *, unsigned int) ; + +extern int ipc_eid (int, unsigned int *, unsigned int *) ; +extern int ipc_local (int, char *, unsigned int, int *) ; + +extern int ipc_connect (int, char const *) ; +extern int ipc_connected (int) ; +extern int ipc_timed_connect (int, char const *, tain_t const *, tain_t *) ; +#define ipc_timed_connect_g(fd, path, deadline) ipc_timed_connect(fd, path, (deadline), &STAMP) + +extern int ipc_send (int, char const *, unsigned int, char const *) ; +extern int ipc_recv (int, char *, unsigned int, char *) ; + +#endif |