+/*********************************************************************
+ *
+ * Function : buf_extend
+ *
+ * Description : Ensure that a given binbuffer can hold a given amount
+ * of bytes, by reallocating its buffer if necessary.
+ * Allocate new mem in chunks of 1000 bytes, so we don't
+ * have to realloc() too often.
+ *
+ * Parameters :
+ * 1 : buf = Pointer to the binbuffer
+ * 2 : length = Desired minimum size
+ *
+ *
+ * Returns : 0 on success, 1 on failiure.
+ *
+ *********************************************************************/
+int buf_extend(struct binbuffer *buf, int length)
+{
+ char *newbuf;
+
+ if (buf->offset + length > buf->size)
+ {
+ buf->size = buf->size + length + 1000 - (buf->size + length) % 1000;
+ newbuf = (char *)realloc(buf->buffer, buf->size);
+
+ if (newbuf == NULL)
+ {
+ freez(buf->buffer);
+ return 1;
+ }
+ else
+ {
+ buf->buffer = newbuf;
+ return 0;
+ }
+ }
+ return 0;
+
+}
+
+