2013-05-02 03:51:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Joris Vink <joris@coders.se>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
|
|
|
|
struct kore_buf *
|
|
|
|
kore_buf_create(u_int32_t initial)
|
|
|
|
{
|
|
|
|
struct kore_buf *buf;
|
|
|
|
|
2013-07-13 21:08:55 +02:00
|
|
|
buf = kore_malloc(sizeof(*buf));
|
|
|
|
buf->data = kore_malloc(initial);
|
2013-05-02 03:51:04 +02:00
|
|
|
buf->length = initial;
|
|
|
|
buf->offset = 0;
|
|
|
|
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-22 10:06:39 +02:00
|
|
|
kore_buf_append(struct kore_buf *buf, void *d, u_int32_t len)
|
2013-05-02 03:51:04 +02:00
|
|
|
{
|
|
|
|
if ((buf->offset + len) >= buf->length) {
|
|
|
|
buf->length += len + KORE_BUF_INCREMENT;
|
2013-07-13 21:08:55 +02:00
|
|
|
buf->data = kore_realloc(buf->data, buf->length);
|
2013-05-02 03:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy((buf->data + buf->offset), d, len);
|
|
|
|
buf->offset += len;
|
|
|
|
}
|
|
|
|
|
2013-06-05 11:27:03 +02:00
|
|
|
void
|
|
|
|
kore_buf_appendb(struct kore_buf *buf, struct kore_buf *src)
|
|
|
|
{
|
|
|
|
u_int8_t *d;
|
|
|
|
u_int32_t len;
|
|
|
|
|
|
|
|
d = kore_buf_release(src, &len);
|
|
|
|
kore_buf_append(buf, d, len);
|
2013-07-22 23:42:40 +02:00
|
|
|
kore_mem_free(d);
|
2013-06-05 11:27:03 +02:00
|
|
|
}
|
|
|
|
|
2013-05-04 19:09:07 +02:00
|
|
|
void
|
2014-04-22 12:16:21 +02:00
|
|
|
kore_buf_appendv(struct kore_buf *buf, const char *fmt, va_list args)
|
2013-05-04 19:09:07 +02:00
|
|
|
{
|
2014-04-22 12:16:21 +02:00
|
|
|
int l;
|
2014-08-04 18:59:59 +02:00
|
|
|
char *b;
|
2013-05-04 19:09:07 +02:00
|
|
|
|
2014-08-04 18:59:59 +02:00
|
|
|
l = vasprintf(&b, fmt, args);
|
|
|
|
if (l == -1)
|
2014-04-22 12:16:21 +02:00
|
|
|
fatal("kore_buf_appendv(): error or truncation");
|
|
|
|
|
|
|
|
kore_buf_append(buf, (u_int8_t *)b, l);
|
2014-08-04 18:59:59 +02:00
|
|
|
free(b);
|
2013-05-04 19:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_buf_appendf(struct kore_buf *buf, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2014-04-22 12:16:21 +02:00
|
|
|
kore_buf_appendv(buf, fmt, args);
|
2013-05-04 19:09:07 +02:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
u_int8_t *
|
|
|
|
kore_buf_release(struct kore_buf *buf, u_int32_t *len)
|
|
|
|
{
|
|
|
|
u_int8_t *p;
|
|
|
|
|
|
|
|
p = buf->data;
|
|
|
|
*len = buf->offset;
|
2013-06-27 08:43:07 +02:00
|
|
|
kore_mem_free(buf);
|
2013-05-02 03:51:04 +02:00
|
|
|
|
|
|
|
return (p);
|
|
|
|
}
|
2013-07-10 15:00:53 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
kore_buf_free(struct kore_buf *buf)
|
|
|
|
{
|
2013-07-12 10:49:37 +02:00
|
|
|
kore_mem_free(buf->data);
|
|
|
|
kore_mem_free(buf);
|
2013-07-10 15:00:53 +02:00
|
|
|
}
|
2013-08-07 14:56:14 +02:00
|
|
|
|
|
|
|
void
|
2013-09-26 16:49:44 +02:00
|
|
|
kore_buf_replace_string(struct kore_buf *b, char *src, void *dst, size_t len)
|
2013-08-07 14:56:14 +02:00
|
|
|
{
|
|
|
|
u_int32_t blen, off, off2;
|
|
|
|
size_t nlen, klen;
|
|
|
|
char *key, *end, *tmp, *p;
|
|
|
|
|
|
|
|
off = 0;
|
|
|
|
klen = strlen(src);
|
|
|
|
for (;;) {
|
|
|
|
blen = b->offset;
|
|
|
|
nlen = blen + len;
|
|
|
|
p = (char *)b->data;
|
|
|
|
|
2013-09-26 16:49:44 +02:00
|
|
|
key = kore_mem_find(p + off, b->offset - off, src, klen);
|
|
|
|
if (key == NULL)
|
2013-08-07 14:56:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
end = key + klen;
|
|
|
|
off = key - p;
|
|
|
|
off2 = ((char *)(b->data + b->offset) - end);
|
|
|
|
|
|
|
|
tmp = kore_malloc(nlen);
|
|
|
|
memcpy(tmp, p, off);
|
2013-08-13 14:18:47 +02:00
|
|
|
if (dst != NULL)
|
|
|
|
memcpy((tmp + off), dst, len);
|
2013-08-07 14:56:14 +02:00
|
|
|
memcpy((tmp + off + len), end, off2);
|
|
|
|
|
|
|
|
kore_mem_free(b->data);
|
|
|
|
b->data = (u_int8_t *)tmp;
|
|
|
|
b->offset = off + len + off2;
|
|
|
|
b->length = nlen;
|
|
|
|
|
|
|
|
off = off + len;
|
|
|
|
}
|
|
|
|
}
|