diff options
author | Aris Adamantiadis <aris@0xbadc0de.be> | 2018-02-28 10:24:50 -0600 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2018-06-04 16:55:23 +0200 |
commit | 36d52ee205512140d1d7dad6e990c925fe153837 (patch) | |
tree | a8ae51f9b62fd6cbbb5c815e8e5a26372abcdf43 /src | |
parent | 0ad462102a313d22c60b32f4f1c9210839480fb3 (diff) | |
download | libssh-36d52ee205512140d1d7dad6e990c925fe153837.tar.gz libssh-36d52ee205512140d1d7dad6e990c925fe153837.tar.xz libssh-36d52ee205512140d1d7dad6e990c925fe153837.zip |
buffer: Add ssh_buffer_allocate() function
Signed-off-by: Aris Adamantiadis <aris@0xbadc0de.be>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/buffer.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c index 7001eef0..5859e926 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -251,6 +251,44 @@ int ssh_buffer_add_data(struct ssh_buffer_struct *buffer, const void *data, uint /** * @internal * + * @brief Allocate space for data at the tail of a buffer. + * + * @param[in] buffer The buffer to add the data. + * + * @param[in] len The length of the data to add. + * + * @return Pointer on the allocated space + * NULL on error. + */ +void *ssh_buffer_allocate(struct ssh_buffer_struct *buffer, uint32_t len) +{ + void *ptr; + buffer_verify(buffer); + + if (buffer->used + len < len) { + return NULL; + } + + if (buffer->allocated < (buffer->used + len)) { + if (buffer->pos > 0) { + buffer_shift(buffer); + } + + if (realloc_buffer(buffer, buffer->used + len) < 0) { + return NULL; + } + } + + ptr = buffer->data + buffer->used; + buffer->used+=len; + buffer_verify(buffer); + + return ptr; +} + +/** + * @internal + * * @brief Add a SSH string to the tail of a buffer. * * @param[in] buffer The buffer to add the string. |