/*
- * Copyright (C) 2012 Tobias Brunner
+ * Copyright (C) 2012-2013 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* Copyright (C) 2010 Martin Willi
};
/**
- * 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;
}
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;
}
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;
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)
{
- if (this->used + 8 > this->buf.len)
- {
- increase(this);
- }
+ 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;
}
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);
}
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);
}
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);
}
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);
}
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;
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;
{
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);
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;
{
chunk_t skipped;
- while (this->used + len > this->buf.len)
- {
- increase(this);
- }
+ increase(this, len);
skipped = chunk_create(this->buf.ptr + this->used, len);
this->used += len;
return skipped;