1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-29 01:03:57 +03:00

buffer: Add ssh_buffer_allocate() function

Signed-off-by: Aris Adamantiadis <aris@0xbadc0de.be>
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
This commit is contained in:
Aris Adamantiadis
2018-02-28 10:24:50 -06:00
committed by Andreas Schneider
parent 0ad462102a
commit 36d52ee205
2 changed files with 39 additions and 0 deletions

View File

@@ -248,6 +248,44 @@ int ssh_buffer_add_data(struct ssh_buffer_struct *buffer, const void *data, uint
return 0;
}
/**
* @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
*