diff options
Diffstat (limited to 'src/libstrongswan/chunk.h')
-rw-r--r-- | src/libstrongswan/chunk.h | 70 |
1 files changed, 60 insertions, 10 deletions
diff --git a/src/libstrongswan/chunk.h b/src/libstrongswan/chunk.h index 93c4b512f..d3477eba8 100644 --- a/src/libstrongswan/chunk.h +++ b/src/libstrongswan/chunk.h @@ -47,21 +47,36 @@ struct chunk_t { extern chunk_t chunk_empty; /** - * Initialize a chunk to point to a static(!) buffer + * Create a new chunk pointing to "ptr" with length "len" */ -#define chunk_from_buf(str) { str, sizeof(str) } +chunk_t chunk_create(u_char *ptr, size_t len); + +/** + * Create a clone of a chunk pointing to "ptr" + */ +chunk_t chunk_create_clone(u_char *ptr, chunk_t chunk); /** - * Clone chunk contents in a newly allocated chunk + * Calculate length of multiple chunks */ -chunk_t chunk_clone(chunk_t chunk); +size_t chunk_length(const char *mode, ...); /** - * Allocate a chunk from concatenation of other chunks. - * mode is a string 'm' and 'c, 'm' means move chunk, - * 'c' means copy chunk. + * Concatenate chunks into a chunk pointing to "ptr", + * "mode" is a string of "c" (copy) and "m" (move), which says + * how to handle to chunks in "..." */ -chunk_t chunk_cat(const char* mode, ...); +chunk_t chunk_create_cat(u_char *ptr, const char* mode, ...); + +/** + * Split up a chunk into parts, "mode" is a string of "a" (alloc), + * "c" (copy) and "m" (move). Each letter say for the corresponding chunk if + * it should get allocated on heap, copied into existing chunk, or the chunk + * should point into "chunk". The length of each part is an argument before + * each target chunk. E.g.: + * chunk_split(chunk, "mcac", 3, &a, 7, &b, 5, &c, d.len, &d); + */ +void chunk_split(chunk_t chunk, const char *mode, ...); /** * Free contents of a chunk @@ -69,9 +84,44 @@ chunk_t chunk_cat(const char* mode, ...); void chunk_free(chunk_t *chunk); /** - * Allocate a chunk + * Initialize a chunk to point to buffer inspectable by sizeof() + */ +#define chunk_from_buf(str) { str, sizeof(str) } + +/** + * Allocate a chunk on the heap + */ +#define chunk_alloc(bytes) chunk_create(malloc(bytes), bytes) + +/** + * Allocate a chunk on the stack + */ +#define chunk_alloca(bytes) chunk_create(alloca(bytes), bytes) + +/** + * Clone a chunk on heap + */ +#define chunk_clone(chunk) chunk_create_clone(malloc(chunk.len), chunk) + +/** + * Clone a chunk on stack + */ +#define chunk_clonea(chunk) chunk_create_clone(alloca(chunk.len), chunk) + +/** + * Concatenate chunks into a chunk on heap + */ +#define chunk_cat(mode, ...) chunk_create_cat(malloc(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__) + +/** + * Concatenate chunks into a chunk on stack + */ +#define chunk_cata(mode, ...) chunk_create_cat(alloca(chunk_length(mode, __VA_ARGS__)), mode, __VA_ARGS__) + +/** + * Skip n bytes in chunk (forward pointer, shorten length) */ -chunk_t chunk_alloc(size_t bytes); +chunk_t chunk_skip(chunk_t chunk, size_t bytes); /** * Compare two chunks for equality, |