2016-05-03 03:29:20 +02:00
|
|
|
#include "utils.h"
|
2017-09-28 05:40:57 +02:00
|
|
|
#include <ccan/list/list.h>
|
2016-05-03 03:29:20 +02:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2017-09-28 05:40:57 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2018-04-25 12:55:34 +02:00
|
|
|
#include <locale.h>
|
2016-05-03 03:29:20 +02:00
|
|
|
|
2016-12-02 08:41:06 +01:00
|
|
|
secp256k1_context *secp256k1_ctx;
|
2018-03-15 05:30:37 +01:00
|
|
|
const tal_t *tmpctx;
|
2019-04-11 17:41:58 +02:00
|
|
|
bool is_elements = false;
|
2016-12-02 08:41:06 +01:00
|
|
|
|
2016-05-03 03:29:20 +02:00
|
|
|
char *tal_hexstr(const tal_t *ctx, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
char *str = tal_arr(ctx, char, hex_str_size(len));
|
|
|
|
hex_encode(data, len, str, hex_str_size(len));
|
|
|
|
return str;
|
|
|
|
}
|
2016-09-07 23:34:36 +02:00
|
|
|
|
2017-01-10 05:49:25 +01:00
|
|
|
char *tal_hex(const tal_t *ctx, const tal_t *data)
|
|
|
|
{
|
2018-07-28 08:00:16 +02:00
|
|
|
return tal_hexstr(ctx, data, tal_bytelen(data));
|
2017-01-10 05:49:25 +01:00
|
|
|
}
|
|
|
|
|
2016-09-07 23:34:36 +02:00
|
|
|
u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len)
|
|
|
|
{
|
|
|
|
u8 *data = tal_arr(ctx, u8, hex_data_size(len));
|
|
|
|
if (!hex_decode(str, len, data, hex_data_size(len)))
|
|
|
|
return NULL;
|
|
|
|
return data;
|
|
|
|
}
|
2017-09-28 05:40:57 +02:00
|
|
|
|
2018-04-25 12:55:34 +02:00
|
|
|
/* Use the POSIX C locale. */
|
|
|
|
void setup_locale(void)
|
|
|
|
{
|
|
|
|
setlocale(LC_ALL, "C");
|
|
|
|
putenv("LC_ALL=C"); /* For exec{l,lp,v,vp}(...) */
|
|
|
|
}
|
|
|
|
|
2018-03-15 05:30:37 +01:00
|
|
|
/* Initial creation of tmpctx. */
|
|
|
|
void setup_tmpctx(void)
|
2017-09-28 05:40:57 +02:00
|
|
|
{
|
2018-07-28 08:00:20 +02:00
|
|
|
tmpctx = tal_arr_label(NULL, char, 0, "tmpctx");
|
2017-09-28 05:40:57 +02:00
|
|
|
}
|
|
|
|
|
2018-03-15 05:30:37 +01:00
|
|
|
/* Free any children of tmpctx. */
|
|
|
|
void clean_tmpctx(void)
|
2017-09-28 05:40:57 +02:00
|
|
|
{
|
2018-08-02 08:49:22 +02:00
|
|
|
const tal_t *p;
|
|
|
|
|
|
|
|
/* Don't actually free tmpctx: we hand pointers to it around. */
|
|
|
|
while ((p = tal_first(tmpctx)) != NULL)
|
|
|
|
tal_free(p);
|
2017-09-28 05:40:57 +02:00
|
|
|
}
|
2018-12-03 14:38:19 +01:00
|
|
|
|
|
|
|
void tal_arr_remove_(void *p, size_t elemsize, size_t n)
|
|
|
|
{
|
|
|
|
// p is a pointer-to-pointer for tal_resize.
|
|
|
|
char *objp = *(char **)p;
|
|
|
|
size_t len = tal_bytelen(objp);
|
|
|
|
assert(len % elemsize == 0);
|
|
|
|
assert((n + 1) * elemsize <= len);
|
|
|
|
memmove(objp + elemsize * n, objp + elemsize * (n+1),
|
|
|
|
len - (elemsize * (n+1)));
|
|
|
|
tal_resize((char **)p, len - elemsize);
|
|
|
|
}
|