From 721adc03d4e75029e6152a8a8cdbfdf2853b5a03 Mon Sep 17 00:00:00 2001 From: Alexander Vdolainen Date: Tue, 7 Jun 2016 23:42:35 +0300 Subject: [PATCH] sxt: sxrdb_t API design; --- include/sxt/rdb.h | 116 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 include/sxt/rdb.h diff --git a/include/sxt/rdb.h b/include/sxt/rdb.h new file mode 100644 index 0000000..69be720 --- /dev/null +++ b/include/sxt/rdb.h @@ -0,0 +1,116 @@ +/* + * 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 + * + * 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 3 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 ."; + * + */ + +#ifndef __SXT_RDB_H__ +#define __SXT_RDB_H__ + +#include + +#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; + +/* 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); + +/* print */ +uint32_t sxtrdb_print_va(sxtrdb_t *b, const char *fmt, int argc, va_list ap); + +/* misc */ +/* returns length of used space in the buffer */ +uint32_t sxtrdb_length(sxtrdb_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 all the flags */ +#define sxtrdb_flagsreset(a) sxtrdb_exflags((a), 0) + +/* 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(sxtbuf_t *); + + +#endif /* __SXT_RDB_H__ */