[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 03/20] buffer: ssh_buffer_allocate function


From: Aris Adamantiadis <aris@xxxxxxxxxxxx>

---
 include/libssh/buffer.h |  1 +
 src/buffer.c            | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/include/libssh/buffer.h b/include/libssh/buffer.h
index 81d9452b..14ce5e67 100644
--- a/include/libssh/buffer.h
+++ b/include/libssh/buffer.h
@@ -50,6 +50,7 @@ int ssh_buffer_add_u64(ssh_buffer buffer, uint64_t data);
 
 int ssh_buffer_validate_length(struct ssh_buffer_struct *buffer, size_t len);
 
+void *ssh_buffer_allocate(struct ssh_buffer_struct *buffer, uint32_t len);
 int ssh_buffer_pack_va(struct ssh_buffer_struct *buffer,
                        const char *format,
                        int argc,
diff --git a/src/buffer.c b/src/buffer.c
index 28a345c6..5d0e9fdb 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -242,6 +242,37 @@ 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
  *
-- 
2.14.1


References:
[PATCH 00/20] Add chacha20-poly1305 supportAlberto Aguirre <albaguirre@xxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org