feat(buffer): add ssh_buffer_dup function to duplicate existing buffers

Signed-off-by: Praneeth Sarode <praneethsarode@gmail.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
Praneeth Sarode
2025-09-22 16:34:34 +05:30
committed by Jakub Jelen
parent a3c5d3b256
commit 2f77727796
2 changed files with 49 additions and 0 deletions

View File

@@ -74,6 +74,8 @@ ssh_string ssh_buffer_get_ssh_string(ssh_buffer buffer);
uint32_t ssh_buffer_pass_bytes_end(ssh_buffer buffer, uint32_t len);
uint32_t ssh_buffer_pass_bytes(ssh_buffer buffer, uint32_t len);
ssh_buffer ssh_buffer_dup(const ssh_buffer buffer);
#ifdef __cplusplus
}
#endif

View File

@@ -617,6 +617,53 @@ uint32_t ssh_buffer_get_len(struct ssh_buffer_struct *buffer){
return buffer->used - buffer->pos;
}
/**
* @internal
*
* @brief Duplicate an existing buffer.
*
* Creates a new ssh_buffer and copies all data from the source buffer.
* The new buffer preserves the secure flag setting of the source.
*
* @param[in] buffer The buffer to duplicate. Can be NULL.
*
* @return A new buffer containing a copy of the data on success,
* NULL on failure or if buffer is NULL.
*
* @see ssh_buffer_free()
*/
ssh_buffer ssh_buffer_dup(const ssh_buffer buffer)
{
ssh_buffer new_buffer = NULL;
int rc;
if (buffer == NULL) {
return NULL;
}
buffer_verify(buffer);
new_buffer = ssh_buffer_new();
if (new_buffer == NULL) {
return NULL;
}
new_buffer->secure = buffer->secure;
if (ssh_buffer_get_len(buffer) > 0) {
rc = ssh_buffer_add_data(new_buffer,
ssh_buffer_get(buffer),
ssh_buffer_get_len(buffer));
if (rc != SSH_OK) {
ssh_buffer_free(new_buffer);
return NULL;
}
}
buffer_verify(new_buffer);
return new_buffer;
}
/**
* @internal
*