You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libsxmp/include/sxt/rdb.h

162 lines
5.2 KiB
C

/*
* Secure eXtended Message Passing framework
* Secure eXtended Transport layer implementation: (libsxt)
* - very similar to SSH2/TLS
* - using already proven and tested crypto algos
* - better than TLS for message passing
*
* raw data buffer implementation used within library
*
* (c) Alexander Vdolainen 2016 <avdolainen@zoho.com>
*
* libsxmp is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* libsxmp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*
*/
#ifndef __SXT_RDB_H__
#define __SXT_RDB_H__
#include <stdarg.h>
#define SXTRDB_TERMINAT 0xdeadbeef
#define SXTRDB_BURN (1 << 1)
typedef struct __sxtrawdatabuffer_type {
char *raw;
uint32_t rlength; /* raw buffer allocated length */
uint32_t ulength; /* length of used allocated space */
uint32_t curr; /* cursor for read/write operations */
int flags;
} sxtrdb_t;
/* variadic macro workaround */
#define VA_APPLY_VARIADIC_MACRO(macro, tuple) macro tuple
#define __VA_NARG__(...) \
(__VA_NARG_(_0, ## __VA_ARGS__, __RSEQ_N()) - 1)
#define __VA_NARG_(...) \
VA_APPLY_VARIADIC_MACRO(__VA_ARG_N, (__VA_ARGS__))
#define __VA_ARG_N( \
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
_61,_62,_63,N,...) N
#define __RSEQ_N() \
63, 62, 61, 60, \
59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
9, 8, 7, 6, 5, 4, 3, 2, 1, 0
/* allocation, freeing */
/* allocate raw buffer with defaults preallocation */
sxtrdb_t *sxtrdb_new(void);
/* will do the same as sxtrdb_new but will allocate given length */
sxtrdb_t *sxtrdb_new_palloc(uint32_t);
/* free all allocated space and buffer itself */
void sxtrdb_free(sxtrdb_t *);
/* read/write */
/* read different types, should return the size of the
* ridden data, otherwise error occurs */
uint32_t sxtrdb_read_u8(sxtrdb_t *, uint8_t *);
uint32_t sxtrdb_read_u16(sxtrdb_t *, uint16_t *);
uint32_t sxtrdb_read_u32(sxtrdb_t *, uint32_t *);
uint32_t sxtrdb_read_u64(sxtrdb_t *, uint64_t *);
/* get raw data, pointer must be allocated with at least required length,
* will return size of ridden data
*/
uint32_t sxtrdb_read_raw(sxtrdb_t *, void *, uint32_t);
/* write different types, should return the size of the
* written data, otherwise error occurs */
uint32_t sxtrdb_write_u8(sxtrdb_t *, uint8_t);
uint32_t sxtrdb_write_u16(sxtrdb_t *, uint16_t);
uint32_t sxtrdb_write_u32(sxtrdb_t *, uint32_t);
uint32_t sxtrdb_write_u64(sxtrdb_t *, uint64_t);
/* write raw data with the given length */
uint32_t sxtrdb_write_raw(sxtrdb_t *, void *, uint32_t);
/* write raw data *before* existing data */
uint32_t sxtrdb_write_raw_head(sxtrdb_t *, void *, uint32_t);
/* parse */
int sxtrdb_escan_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap);
int sxtrdb_escan_wot(sxtrdb_t *b, const char *fmt, int argc, ...);
#define sxtrdb_escan(b, fmt, ...) \
sxtrdb_escan_wot((b), (fmt), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SXTRDB_TERMINAT)
/* print */
uint32_t sxtrdb_print_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap);
uint32_t sxtrdb_print_wot(sxtrdb_t *b, const char *fmt, int argc, ...);
#define sxtrdb_print(b, fmt, ...) \
sxtrdb_print_wot((b), (fmt), __VA_NARG__(__VA_ARGS__), __VA_ARGS__, SXTRDB_TERMINAT)
/* misc */
/* returns length of used space in the buffer */
uint32_t sxtrdb_length(sxtrdb_t *);
/* set used length */
int sxtrdb_setlength(sxtrdb_t *, uint32_t);
/* returns length of allocated space in the buffer */
uint32_t sxtrdb_alength(sxtrdb_t *);
/* returns length of the left data to read */
uint32_t sxtrdb_leftlength(sxtrdb_t *);
/* reaturn a pointer to the raw data */
void *sxtrdb_rdata(sxtrdb_t *);
/* return pointer to the data currently being read/write */
void *sxtrdb_rdatacur(sxtrdb_t *);
/* set flags to the raw buffer */
void sxtrdb_setflags(sxtrdb_t *, int);
/* exchange flags for raw buff */
void sxtrdb_exflags(sxtrdb_t *, int);
/* reset current pointer */
void sxtrdb_resetcur(sxtrdb_t *);
/* reset all the flags */
#define sxtrdb_flagsreset(a) sxtrdb_exflags((a), 0)
/* compare buffers, returns 0 if equal */
int sxtrdb_cmp(sxtrdb_t *, sxtrdb_t *);
/* let the buffer to use actually used bytes, not all allocated space
* will return SXT_SUCCESS on success (or in case if it doesn't required),
* SXT_ENOMEM or other error if fails
*/
int sxtrdb_memopt(sxtrdb_t *);
#endif /* __SXT_RDB_H__ */