2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* 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 <string.h>
|
|
|
|
|
|
|
|
#include "../util/bmem.h"
|
2016-01-18 19:59:31 -08:00
|
|
|
#include "../util/base.h"
|
2013-12-25 21:40:33 -08:00
|
|
|
|
|
|
|
#include "calldata.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Uses a data stack. Probably more complex than it should be, but reduces
|
2014-03-01 04:54:55 -08:00
|
|
|
* fetching.
|
2013-12-25 21:40:33 -08:00
|
|
|
*
|
|
|
|
* Stack format is:
|
|
|
|
* [size_t param1_name_size]
|
|
|
|
* [char[] param1_name]
|
|
|
|
* [size_t param1_data_size]
|
|
|
|
* [uint8_t[] param1_data]
|
|
|
|
* [size_t param2_name_size]
|
|
|
|
* [char[] param2_name]
|
|
|
|
* [size_t param2_data_size]
|
|
|
|
* [uint8_t[] param2_data]
|
|
|
|
* [...]
|
2013-12-26 01:02:24 -08:00
|
|
|
* [size_t 0]
|
2013-12-25 21:40:33 -08:00
|
|
|
*
|
|
|
|
* Strings and string sizes always include the null terminator to allow for
|
|
|
|
* direct referencing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline void cd_serialize(uint8_t **pos, void *ptr, size_t size)
|
|
|
|
{
|
|
|
|
memcpy(ptr, *pos, size);
|
|
|
|
*pos += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline size_t cd_serialize_size(uint8_t **pos)
|
|
|
|
{
|
2015-05-30 19:28:53 -07:00
|
|
|
size_t size = 0;
|
|
|
|
memcpy(&size, *pos, sizeof(size_t));
|
2013-12-25 21:40:33 -08:00
|
|
|
*pos += sizeof(size_t);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline const char *cd_serialize_string(uint8_t **pos)
|
|
|
|
{
|
|
|
|
size_t size = cd_serialize_size(pos);
|
2014-02-09 08:53:19 -08:00
|
|
|
const char *str = (const char *)*pos;
|
2013-12-25 21:40:33 -08:00
|
|
|
|
|
|
|
*pos += size;
|
|
|
|
|
|
|
|
return (size != 0) ? str : NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static bool cd_getparam(const calldata_t *data, const char *name, uint8_t **pos)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
|
|
|
size_t name_size;
|
|
|
|
|
|
|
|
if (!data->size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*pos = data->stack;
|
|
|
|
|
|
|
|
name_size = cd_serialize_size(pos);
|
|
|
|
while (name_size != 0) {
|
2014-02-09 08:53:19 -08:00
|
|
|
const char *param_name = (const char *)*pos;
|
2013-12-25 21:40:33 -08:00
|
|
|
size_t param_size;
|
|
|
|
|
|
|
|
*pos += name_size;
|
|
|
|
if (strcmp(param_name, name) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
param_size = cd_serialize_size(pos);
|
|
|
|
*pos += param_size;
|
|
|
|
|
|
|
|
name_size = cd_serialize_size(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
*pos -= sizeof(size_t);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cd_copy_string(uint8_t **pos, const char *str, size_t len)
|
|
|
|
{
|
|
|
|
if (!len)
|
2019-06-22 22:13:45 -07:00
|
|
|
len = strlen(str) + 1;
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2015-05-30 19:28:53 -07:00
|
|
|
memcpy(*pos, &len, sizeof(size_t));
|
2013-12-25 21:40:33 -08:00
|
|
|
*pos += sizeof(size_t);
|
|
|
|
memcpy(*pos, str, len);
|
|
|
|
*pos += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void cd_copy_data(uint8_t **pos, const void *in, size_t size)
|
|
|
|
{
|
2015-05-30 19:28:53 -07:00
|
|
|
memcpy(*pos, &size, sizeof(size_t));
|
2013-12-25 21:40:33 -08:00
|
|
|
*pos += sizeof(size_t);
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
memcpy(*pos, in, size);
|
|
|
|
*pos += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
static inline void cd_set_first_param(calldata_t *data, const char *name,
|
2019-06-22 22:13:45 -07:00
|
|
|
const void *in, size_t size)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
|
|
|
uint8_t *pos;
|
|
|
|
size_t capacity;
|
2019-06-22 22:13:45 -07:00
|
|
|
size_t name_len = strlen(name) + 1;
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
capacity = sizeof(size_t) * 3 + name_len + size;
|
2013-12-25 21:40:33 -08:00
|
|
|
data->size = capacity;
|
|
|
|
|
|
|
|
if (capacity < 128)
|
|
|
|
capacity = 128;
|
|
|
|
|
|
|
|
data->capacity = capacity;
|
2019-06-22 22:13:45 -07:00
|
|
|
data->stack = bmalloc(capacity);
|
2013-12-25 21:40:33 -08:00
|
|
|
|
|
|
|
pos = data->stack;
|
|
|
|
cd_copy_string(&pos, name, name_len);
|
|
|
|
cd_copy_data(&pos, in, size);
|
2015-05-30 19:28:53 -07:00
|
|
|
memset(pos, 0, sizeof(size_t));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
2016-01-18 19:59:31 -08:00
|
|
|
static inline bool cd_ensure_capacity(calldata_t *data, uint8_t **pos,
|
2019-06-22 22:13:45 -07:00
|
|
|
size_t new_size)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
|
|
|
size_t offset;
|
|
|
|
size_t new_capacity;
|
|
|
|
|
|
|
|
if (new_size < data->capacity)
|
2016-01-18 19:59:31 -08:00
|
|
|
return true;
|
|
|
|
if (data->fixed) {
|
|
|
|
blog(LOG_ERROR, "Tried to go above fixed calldata stack size!");
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-25 21:40:33 -08:00
|
|
|
|
|
|
|
offset = *pos - data->stack;
|
|
|
|
|
|
|
|
new_capacity = data->capacity * 2;
|
|
|
|
if (new_capacity < new_size)
|
|
|
|
new_capacity = new_size;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
data->stack = brealloc(data->stack, new_capacity);
|
2013-12-25 21:40:33 -08:00
|
|
|
data->capacity = new_capacity;
|
|
|
|
|
|
|
|
*pos = data->stack + offset;
|
2016-01-18 19:59:31 -08:00
|
|
|
return true;
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
bool calldata_get_data(const calldata_t *data, const char *name, void *out,
|
2019-06-22 22:13:45 -07:00
|
|
|
size_t size)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
|
|
|
uint8_t *pos;
|
|
|
|
size_t data_size;
|
|
|
|
|
2014-02-23 21:39:33 -08:00
|
|
|
if (!data || !name || !*name)
|
2013-12-25 21:40:33 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!cd_getparam(data, name, &pos))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
data_size = cd_serialize_size(&pos);
|
|
|
|
if (data_size != size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
memcpy(out, pos, size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:44:05 -07:00
|
|
|
void calldata_set_data(calldata_t *data, const char *name, const void *in,
|
2019-06-22 22:13:45 -07:00
|
|
|
size_t size)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
2014-07-09 16:11:10 -07:00
|
|
|
uint8_t *pos = NULL;
|
2013-12-25 21:40:33 -08:00
|
|
|
|
2014-02-23 21:39:33 -08:00
|
|
|
if (!data || !name || !*name)
|
2013-12-25 21:40:33 -08:00
|
|
|
return;
|
|
|
|
|
2016-01-18 19:59:31 -08:00
|
|
|
if (!data->fixed && !data->stack) {
|
2013-12-25 21:40:33 -08:00
|
|
|
cd_set_first_param(data, name, in, size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cd_getparam(data, name, &pos)) {
|
2015-05-30 19:28:53 -07:00
|
|
|
size_t cur_size;
|
|
|
|
memcpy(&cur_size, pos, sizeof(size_t));
|
2013-12-25 21:40:33 -08:00
|
|
|
|
|
|
|
if (cur_size < size) {
|
|
|
|
size_t offset = size - cur_size;
|
|
|
|
size_t bytes = data->size;
|
|
|
|
|
2016-01-18 19:59:31 -08:00
|
|
|
if (!cd_ensure_capacity(data, &pos, bytes + offset))
|
|
|
|
return;
|
2019-06-22 22:13:45 -07:00
|
|
|
memmove(pos + offset, pos, bytes - (pos - data->stack));
|
2013-12-25 21:40:33 -08:00
|
|
|
data->size += offset;
|
|
|
|
|
|
|
|
} else if (cur_size > size) {
|
|
|
|
size_t offset = cur_size - size;
|
|
|
|
size_t bytes = data->size - offset;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
memmove(pos, pos + offset, bytes - (pos - data->stack));
|
2013-12-25 21:40:33 -08:00
|
|
|
data->size -= offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
cd_copy_data(&pos, in, size);
|
|
|
|
|
|
|
|
} else {
|
2019-06-22 22:13:45 -07:00
|
|
|
size_t name_len = strlen(name) + 1;
|
|
|
|
size_t offset = name_len + size + sizeof(size_t) * 2;
|
2016-01-18 19:59:31 -08:00
|
|
|
if (!cd_ensure_capacity(data, &pos, data->size + offset))
|
|
|
|
return;
|
2013-12-25 21:40:33 -08:00
|
|
|
data->size += offset;
|
|
|
|
|
|
|
|
cd_copy_string(&pos, name, 0);
|
|
|
|
cd_copy_data(&pos, in, size);
|
2015-05-30 19:28:53 -07:00
|
|
|
memset(pos, 0, sizeof(size_t));
|
2013-12-25 21:40:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 15:25:59 -07:00
|
|
|
bool calldata_get_string(const calldata_t *data, const char *name,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char **str)
|
2013-12-25 21:40:33 -08:00
|
|
|
{
|
|
|
|
uint8_t *pos;
|
2014-02-23 21:39:33 -08:00
|
|
|
if (!data || !name || !*name)
|
2013-12-25 21:40:33 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!cd_getparam(data, name, &pos))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*str = cd_serialize_string(&pos);
|
|
|
|
return true;
|
|
|
|
}
|