Merged silc_1_0_branch to trunk.
[silc.git] / lib / silcutil / silcbuffer.h
index 6aa197fda40df74534ab77b76235adef9420b709..4dc1472ae6822fc5bff68575c5db519c7e0c3604 100644 (file)
@@ -209,14 +209,44 @@ static inline
 void silc_buffer_free(SilcBuffer sb)
 {
   if (sb) {
-#ifdef SILC_DEBUG
-    memset(sb->head, 'F', sb->truelen);
+#if defined(SILC_DEBUG)
+    if (sb->head)
+      memset(sb->head, 'F', sb->truelen);
 #endif
     silc_free(sb->head);
     silc_free(sb);
   }
 }
 
+/****f* silcutil/SilcBufferAPI/silc_buffer_steal
+ *
+ * SYNOPSIS
+ *
+ *    static inline
+ *    unsigned char *silc_buffer_steal(SilcBuffer sb, SilcUInt32 *data_len);
+ *
+ * DESCRIPTION
+ *
+ *    Steals the data from the buffer `sb'.  This returns pointer to the
+ *    start of the buffer and the true length of that buffer.  The `sb'
+ *    cannot be used anymore after calling this function because the
+ *    data buffer was stolen.  The `sb' must be freed with silc_buffer_free.
+ *    The caller is responsible of freeing the stolen data buffer with
+ *    silc_free.
+ *
+ ***/
+
+static inline
+unsigned char *silc_buffer_steal(SilcBuffer sb, SilcUInt32 *data_len)
+{
+  unsigned char *buf = sb->head;
+  if (data_len)
+    *data_len = sb->truelen;
+  sb->head = sb->data = sb->tail = sb->end = NULL;
+  sb->len = sb->truelen = 0;
+  return buf;
+}
+
 /****f* silcutil/SilcBufferAPI/silc_buffer_set
  *
  * SYNOPSIS
@@ -275,7 +305,7 @@ unsigned char *silc_buffer_pull(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_data = sb->data;
 
-#ifdef SILC_DEBUG
+#if defined(SILC_DEBUG)
   assert(len <= (SilcUInt32)(sb->tail - sb->data));
 #endif
 
@@ -318,7 +348,7 @@ unsigned char *silc_buffer_push(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_data = sb->data;
 
-#ifdef SILC_DEBUG
+#if defined(SILC_DEBUG)
   assert((sb->data - len) >= sb->head);
 #endif
 
@@ -361,7 +391,7 @@ unsigned char *silc_buffer_pull_tail(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_tail = sb->tail;
 
-#ifdef SILC_DEBUG
+#if defined(SILC_DEBUG)
   assert((SilcUInt32)(sb->end - sb->tail) >= len);
 #endif
 
@@ -404,7 +434,7 @@ unsigned char *silc_buffer_push_tail(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_tail = sb->tail;
 
-#ifdef SILC_DEBUG
+#if defined(SILC_DEBUG)
   assert((sb->tail - len) >= sb->data);
 #endif
 
@@ -443,7 +473,7 @@ unsigned char *silc_buffer_put_head(SilcBuffer sb,
                                    const unsigned char *data,
                                    SilcUInt32 len)
 {
-#ifdef SILC_DEBUG
+#if defined(SILC_DEBUG)
   assert((SilcUInt32)(sb->data - sb->head) >= len);
 #endif
   return (unsigned char *)memcpy(sb->head, data, len);
@@ -478,7 +508,7 @@ unsigned char *silc_buffer_put(SilcBuffer sb,
                               const unsigned char *data,
                               SilcUInt32 len)
 {
-#ifdef SILC_DEBUG
+#if defined(SILC_DEBUG)
   assert((SilcUInt32)(sb->tail - sb->data) >= len);
 #endif
   return (unsigned char *)memcpy(sb->data, data, len);
@@ -513,7 +543,7 @@ unsigned char *silc_buffer_put_tail(SilcBuffer sb,
                                    const unsigned char *data,
                                    SilcUInt32 len)
 {
-#ifdef SILC_DEBUG
+#if defined(SILC_DEBUG)
   assert((SilcUInt32)(sb->end - sb->tail) >= len);
 #endif
   return (unsigned char *)memcpy(sb->tail, data, len);
@@ -561,6 +591,8 @@ SilcBuffer silc_buffer_alloc_size(SilcUInt32 len)
 static inline
 void silc_buffer_clear(SilcBuffer sb)
 {
+  if (!sb)
+    return;
   memset(sb->head, 0, sb->truelen);
   sb->data = sb->head;
   sb->tail = sb->head;