--- libelwix/src/pack.c 2014/02/10 22:50:44 1.4 +++ libelwix/src/pack.c 2014/02/11 00:08:13 1.4.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: pack.c,v 1.4 2014/02/10 22:50:44 misho Exp $ +* $Id: pack.c,v 1.4.2.1 2014/02/11 00:08:13 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -259,4 +259,37 @@ rpack_uint64(rpack_t * __restrict rp, uint64_t * __res rp->r_next = next + sizeof(uint64_t); return u; +} + +/* + * rpack_data() - Pack/Unpack data + * + * @rp = raw buffer + * @dat = data + * @datlen = data length + * return: NULL error or != NULL get data, must be e_free() after use! + */ +void * +rpack_data(rpack_t * __restrict rp, void * __restrict dat, size_t datlen) +{ + void *buf = NULL; + uint8_t *next; + + if (!datlen || !RPACK_SANITY(rp)) + return NULL; + buf = e_malloc(datlen); + if (!buf) + return NULL; + /* No space left */ + if (!(next = rpack_align_and_reserve(rp, datlen))) { + e_free(buf); + return NULL; + } + + memcpy(buf, next, datlen); + if (dat) + memcpy(next, dat, datlen); + + rp->r_next = next + datlen; + return buf; }