]> git.ipfire.org Git - people/ms/strongswan.git/blobdiff - src/libstrongswan/bio/bio_writer.c
Merge branch 'unit-tests'
[people/ms/strongswan.git] / src / libstrongswan / bio / bio_writer.c
index a5df5ba0c52dee9198f6a499ad840ee38f765352..152d9ce22e8459faeb7dc359513e48a75d6ebb14 100644 (file)
@@ -1,4 +1,7 @@
 /*
+ * Copyright (C) 2012-2013 Tobias Brunner
+ * Hochschule fuer Technik Rapperswil
+ *
  * Copyright (C) 2010 Martin Willi
  * Copyright (C) 2010 revosec AG
  *
@@ -44,21 +47,27 @@ struct private_bio_writer_t {
 };
 
 /**
- * Increase buffer size
+ * Increase buffer size, if required
  */
-static void increase(private_bio_writer_t *this)
+static inline void increase(private_bio_writer_t *this, size_t required)
 {
-       this->buf.len += this->increase;
-       this->buf.ptr = realloc(this->buf.ptr, this->buf.len);
+       bool inc = FALSE;
+
+       while (this->used + required > this->buf.len)
+       {
+               this->buf.len += this->increase;
+               inc = TRUE;
+       }
+       if (inc)
+       {
+               this->buf.ptr = realloc(this->buf.ptr, this->buf.len);
+       }
 }
 
 METHOD(bio_writer_t, write_uint8, void,
        private_bio_writer_t *this, u_int8_t value)
 {
-       if (this->used + 1 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 1);
        this->buf.ptr[this->used] = value;
        this->used += 1;
 }
@@ -66,10 +75,7 @@ METHOD(bio_writer_t, write_uint8, void,
 METHOD(bio_writer_t, write_uint16, void,
        private_bio_writer_t *this, u_int16_t value)
 {
-       if (this->used + 2 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 2);
        htoun16(this->buf.ptr + this->used, value);
        this->used += 2;
 }
@@ -77,10 +83,7 @@ METHOD(bio_writer_t, write_uint16, void,
 METHOD(bio_writer_t, write_uint24, void,
        private_bio_writer_t *this, u_int32_t value)
 {
-       if (this->used + 3 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 3);
        value = htonl(value);
        memcpy(this->buf.ptr + this->used, ((char*)&value) + 1, 3);
        this->used += 3;
@@ -89,21 +92,23 @@ METHOD(bio_writer_t, write_uint24, void,
 METHOD(bio_writer_t, write_uint32, void,
        private_bio_writer_t *this, u_int32_t value)
 {
-       if (this->used + 4 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 4);
        htoun32(this->buf.ptr + this->used, value);
        this->used += 4;
 }
 
+METHOD(bio_writer_t, write_uint64, void,
+       private_bio_writer_t *this, u_int64_t value)
+{
+       increase(this, 8);
+       htoun64(this->buf.ptr + this->used, value);
+       this->used += 8;
+}
+
 METHOD(bio_writer_t, write_data, void,
        private_bio_writer_t *this, chunk_t value)
 {
-       while (this->used + value.len > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, value.len);
        memcpy(this->buf.ptr + this->used, value.ptr, value.len);
        this->used += value.len;
 }
@@ -111,6 +116,7 @@ METHOD(bio_writer_t, write_data, void,
 METHOD(bio_writer_t, write_data8, void,
        private_bio_writer_t *this, chunk_t value)
 {
+       increase(this, 1 + value.len);
        write_uint8(this, value.len);
        write_data(this, value);
 }
@@ -118,6 +124,7 @@ METHOD(bio_writer_t, write_data8, void,
 METHOD(bio_writer_t, write_data16, void,
        private_bio_writer_t *this, chunk_t value)
 {
+       increase(this, 2 + value.len);
        write_uint16(this, value.len);
        write_data(this, value);
 }
@@ -125,6 +132,7 @@ METHOD(bio_writer_t, write_data16, void,
 METHOD(bio_writer_t, write_data24, void,
        private_bio_writer_t *this, chunk_t value)
 {
+       increase(this, 3 + value.len);
        write_uint24(this, value.len);
        write_data(this, value);
 }
@@ -132,6 +140,7 @@ METHOD(bio_writer_t, write_data24, void,
 METHOD(bio_writer_t, write_data32, void,
        private_bio_writer_t *this, chunk_t value)
 {
+       increase(this, 4 + value.len);
        write_uint32(this, value.len);
        write_data(this, value);
 }
@@ -139,10 +148,7 @@ METHOD(bio_writer_t, write_data32, void,
 METHOD(bio_writer_t, wrap8, void,
        private_bio_writer_t *this)
 {
-       if (this->used + 1 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 1);
        memmove(this->buf.ptr + 1, this->buf.ptr, this->used);
        this->buf.ptr[0] = this->used;
        this->used += 1;
@@ -151,10 +157,7 @@ METHOD(bio_writer_t, wrap8, void,
 METHOD(bio_writer_t, wrap16, void,
        private_bio_writer_t *this)
 {
-       if (this->used + 2 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 2);
        memmove(this->buf.ptr + 2, this->buf.ptr, this->used);
        htoun16(this->buf.ptr, this->used);
        this->used += 2;
@@ -165,10 +168,7 @@ METHOD(bio_writer_t, wrap24, void,
 {
        u_int32_t len;
 
-       if (this->used + 3 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 3);
        memmove(this->buf.ptr + 3, this->buf.ptr, this->used);
 
        len = htonl(this->used);
@@ -179,21 +179,38 @@ METHOD(bio_writer_t, wrap24, void,
 METHOD(bio_writer_t, wrap32, void,
        private_bio_writer_t *this)
 {
-       if (this->used + 4 > this->buf.len)
-       {
-               increase(this);
-       }
+       increase(this, 4);
        memmove(this->buf.ptr + 4, this->buf.ptr, this->used);
        htoun32(this->buf.ptr, this->used);
        this->used += 4;
 }
 
+METHOD(bio_writer_t, skip, chunk_t,
+       private_bio_writer_t *this, size_t len)
+{
+       chunk_t skipped;
+
+       increase(this, len);
+       skipped = chunk_create(this->buf.ptr + this->used, len);
+       this->used += len;
+       return skipped;
+}
+
 METHOD(bio_writer_t, get_buf, chunk_t,
        private_bio_writer_t *this)
 {
        return chunk_create(this->buf.ptr, this->used);
 }
 
+METHOD(bio_writer_t, extract_buf, chunk_t,
+       private_bio_writer_t *this)
+{
+       chunk_t buf = get_buf(this);
+       this->buf = chunk_empty;
+       this->used = 0;
+       return buf;
+}
+
 METHOD(bio_writer_t, destroy, void,
        private_bio_writer_t *this)
 {
@@ -214,6 +231,7 @@ bio_writer_t *bio_writer_create(u_int32_t bufsize)
                        .write_uint16 = _write_uint16,
                        .write_uint24 = _write_uint24,
                        .write_uint32 = _write_uint32,
+                       .write_uint64 = _write_uint64,
                        .write_data = _write_data,
                        .write_data8 = _write_data8,
                        .write_data16 = _write_data16,
@@ -223,7 +241,9 @@ bio_writer_t *bio_writer_create(u_int32_t bufsize)
                        .wrap16 = _wrap16,
                        .wrap24 = _wrap24,
                        .wrap32 = _wrap32,
+                       .skip = _skip,
                        .get_buf = _get_buf,
+                       .extract_buf = _extract_buf,
                        .destroy = _destroy,
                },
                .increase = bufsize ? max(bufsize, 4) : 32,