2013-05-02 03:51:04 +02:00
|
|
|
/*
|
2016-01-04 12:58:51 +01:00
|
|
|
* Copyright (c) 2013-2016 Joris Vink <joris@coders.se>
|
2013-05-02 03:51:04 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-01-22 12:08:13 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
#include "kore.h"
|
|
|
|
|
|
|
|
struct kore_buf *
|
2016-07-14 12:33:13 +02:00
|
|
|
kore_buf_alloc(size_t initial)
|
2013-05-02 03:51:04 +02:00
|
|
|
{
|
|
|
|
struct kore_buf *buf;
|
|
|
|
|
2013-07-13 21:08:55 +02:00
|
|
|
buf = kore_malloc(sizeof(*buf));
|
2016-07-05 16:48:58 +02:00
|
|
|
kore_buf_init(buf, initial);
|
|
|
|
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
kore_buf_init(struct kore_buf *buf, size_t initial)
|
|
|
|
{
|
2016-07-14 12:33:13 +02:00
|
|
|
if (initial > 0)
|
|
|
|
buf->data = kore_malloc(initial);
|
|
|
|
else
|
|
|
|
buf->data = NULL;
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
buf->length = initial;
|
|
|
|
buf->offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-07-08 10:02:18 +02:00
|
|
|
kore_buf_append(struct kore_buf *buf, const void *d, size_t len)
|
2013-05-02 03:51:04 +02:00
|
|
|
{
|
2016-01-09 13:51:50 +01:00
|
|
|
if ((buf->offset + len) < len)
|
|
|
|
fatal("overflow in kore_buf_append");
|
|
|
|
|
2016-01-09 01:57:59 +01:00
|
|
|
if ((buf->offset + len) > buf->length) {
|
2016-07-14 12:33:13 +02:00
|
|
|
buf->length += len;
|
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;
|
2016-07-08 10:02:18 +02:00
|
|
|
size_t len;
|
2013-06-05 11:27:03 +02:00
|
|
|
|
|
|
|
d = kore_buf_release(src, &len);
|
|
|
|
kore_buf_append(buf, d, len);
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_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-22 14:40:19 +02:00
|
|
|
char *b, sb[BUFSIZ];
|
2013-05-04 19:09:07 +02:00
|
|
|
|
2014-08-22 14:40:19 +02:00
|
|
|
l = vsnprintf(sb, sizeof(sb), fmt, args);
|
2014-08-04 18:59:59 +02:00
|
|
|
if (l == -1)
|
2014-08-22 14:40:19 +02:00
|
|
|
fatal("kore_buf_appendv(): vsnprintf error");
|
|
|
|
|
|
|
|
if ((size_t)l >= sizeof(sb)) {
|
|
|
|
l = vasprintf(&b, fmt, args);
|
|
|
|
if (l == -1)
|
|
|
|
fatal("kore_buf_appendv(): error or truncation");
|
|
|
|
} else {
|
|
|
|
b = sb;
|
|
|
|
}
|
2014-04-22 12:16:21 +02:00
|
|
|
|
2016-05-16 09:45:27 +02:00
|
|
|
kore_buf_append(buf, b, l);
|
2014-08-22 14:40:19 +02:00
|
|
|
if (b != sb)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-18 11:26:55 +01:00
|
|
|
char *
|
2016-06-02 07:08:19 +02:00
|
|
|
kore_buf_stringify(struct kore_buf *buf, size_t *len)
|
2016-01-18 11:26:55 +01:00
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
2016-06-02 07:08:19 +02:00
|
|
|
if (len != NULL)
|
|
|
|
*len = buf->offset;
|
|
|
|
|
2016-01-18 11:26:55 +01:00
|
|
|
c = '\0';
|
|
|
|
kore_buf_append(buf, &c, sizeof(c));
|
2016-06-02 07:08:19 +02:00
|
|
|
|
2016-01-18 11:26:55 +01:00
|
|
|
return ((char *)buf->data);
|
|
|
|
}
|
|
|
|
|
2013-05-02 03:51:04 +02:00
|
|
|
u_int8_t *
|
2016-07-08 10:02:18 +02:00
|
|
|
kore_buf_release(struct kore_buf *buf, size_t *len)
|
2013-05-02 03:51:04 +02:00
|
|
|
{
|
|
|
|
u_int8_t *p;
|
|
|
|
|
|
|
|
p = buf->data;
|
|
|
|
*len = buf->offset;
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_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)
|
|
|
|
{
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(buf->data);
|
|
|
|
kore_free(buf);
|
2013-07-10 15:00:53 +02:00
|
|
|
}
|
2013-08-07 14:56:14 +02:00
|
|
|
|
2016-07-05 16:48:58 +02:00
|
|
|
void
|
2016-07-14 12:33:13 +02:00
|
|
|
kore_buf_cleanup(struct kore_buf *buf)
|
2016-07-05 16:48:58 +02:00
|
|
|
{
|
|
|
|
if (buf->data)
|
2016-07-14 12:29:49 +02:00
|
|
|
kore_free(buf->data);
|
2016-07-05 16:48:58 +02:00
|
|
|
buf->data = NULL;
|
|
|
|
buf->offset = 0;
|
|
|
|
buf->length = 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
char *key, *end, *tmp, *p;
|
2016-07-08 10:02:18 +02:00
|
|
|
size_t blen, off, off2, nlen, klen;
|
2013-08-07 14:56:14 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(b->data);
|
2013-08-07 14:56:14 +02:00
|
|
|
b->data = (u_int8_t *)tmp;
|
|
|
|
b->offset = off + len + off2;
|
|
|
|
b->length = nlen;
|
|
|
|
|
|
|
|
off = off + len;
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 22:31:59 +01:00
|
|
|
|
|
|
|
void
|
2016-01-04 13:09:45 +01:00
|
|
|
kore_buf_reset(struct kore_buf *buf)
|
2015-12-11 22:31:59 +01:00
|
|
|
{
|
2015-12-11 23:31:14 +01:00
|
|
|
buf->offset = 0;
|
2015-12-11 22:31:59 +01:00
|
|
|
}
|