1 #include "utils.h"
2 #include <bitcoin/chainparams.h>
3 #include <ccan/list/list.h>
4 #include <ccan/str/hex/hex.h>
5 #include <ccan/utf8/utf8.h>
6 #include <errno.h>
7 #include <locale.h>
8
9 const tal_t *wally_tal_ctx;
10 secp256k1_context *secp256k1_ctx;
11 const tal_t *tmpctx;
12
13 const struct chainparams *chainparams;
14
is_elements(const struct chainparams * chainparams)15 bool is_elements(const struct chainparams *chainparams)
16 {
17 return chainparams->is_elements;
18 }
19
tal_wally_start(void)20 void tal_wally_start(void)
21 {
22 if (wally_tal_ctx) {
23 /* This makes valgrind show us backtraces! */
24 *(u8 *)wally_tal_ctx = '\0';
25 abort();
26 }
27
28 wally_tal_ctx = tal_arr(NULL, char, 0);
29 }
30
tal_wally_end(const tal_t * parent)31 void tal_wally_end(const tal_t *parent)
32 {
33 tal_t *p;
34 while ((p = tal_first(wally_tal_ctx)) != NULL) {
35 if (p != parent)
36 tal_steal(parent, p);
37 }
38 wally_tal_ctx = tal_free(wally_tal_ctx);
39 }
40
41 #if DEVELOPER
42 /* If you've got a softref, we assume no reallocs. */
dont_move_softref(tal_t * ctx,enum tal_notify_type ntype,void * info)43 static void dont_move_softref(tal_t *ctx, enum tal_notify_type ntype, void *info)
44 {
45 abort();
46 }
47 #endif
48
softref_nullify(tal_t * obj,void ** ptr)49 static void softref_nullify(tal_t *obj, void **ptr)
50 {
51 *ptr = NULL;
52 #if DEVELOPER
53 tal_del_notifier(obj, dont_move_softref);
54 #endif
55 }
56
softref_cleanup(const tal_t * outer,void ** ptr)57 static void softref_cleanup(const tal_t *outer, void **ptr)
58 {
59 if (*ptr) {
60 tal_del_destructor2(*ptr, softref_nullify, ptr);
61 }
62 #if DEVELOPER
63 tal_del_notifier(outer, dont_move_softref);
64 #endif
65 }
66
set_softref_(const tal_t * outer,size_t outersize,void ** ptr,tal_t * obj)67 void set_softref_(const tal_t *outer, size_t outersize, void **ptr, tal_t *obj)
68 {
69 /* pointer is inside outer, right? */
70 assert((char *)ptr >= (char *)outer);
71 assert((char *)ptr < (char *)outer + outersize);
72
73 /* This is harmless if there was no prior, otherwise constrains the
74 * leak: we don't have enough information in softref_nullify to
75 * clear softref_cleanup */
76 tal_del_destructor2(outer, softref_cleanup, ptr);
77
78 if (obj) {
79 tal_add_destructor2(outer, softref_cleanup, ptr);
80 tal_add_destructor2(obj, softref_nullify, ptr);
81 #if DEVELOPER
82 tal_add_notifier(obj, TAL_NOTIFY_MOVE, dont_move_softref);
83 #endif
84 }
85
86 #if DEVELOPER
87 tal_add_notifier(outer, TAL_NOTIFY_MOVE, dont_move_softref);
88 #endif
89
90 *ptr = obj;
91 }
92
clear_softref_(const tal_t * outer,size_t outersize,void ** ptr)93 void clear_softref_(const tal_t *outer, size_t outersize, void **ptr)
94 {
95 assert((char *)ptr >= (char *)outer);
96 assert((char *)ptr < (char *)outer + outersize);
97
98 if (*ptr) {
99 tal_del_destructor2(outer, softref_cleanup, ptr);
100 tal_del_destructor2(*ptr, softref_nullify, ptr);
101 #if DEVELOPER
102 tal_del_notifier(*ptr, dont_move_softref);
103 #endif
104 }
105
106 #if DEVELOPER
107 tal_del_notifier(outer, dont_move_softref);
108 #endif
109
110 *ptr = NULL;
111 }
112
tal_hexstr(const tal_t * ctx,const void * data,size_t len)113 char *tal_hexstr(const tal_t *ctx, const void *data, size_t len)
114 {
115 char *str = tal_arr(ctx, char, hex_str_size(len));
116 hex_encode(data, len, str, hex_str_size(len));
117 return str;
118 }
119
tal_hex(const tal_t * ctx,const tal_t * data)120 char *tal_hex(const tal_t *ctx, const tal_t *data)
121 {
122 return tal_hexstr(ctx, data, tal_bytelen(data));
123 }
124
tal_hexdata(const tal_t * ctx,const void * str,size_t len)125 u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len)
126 {
127 u8 *data = tal_arr(ctx, u8, hex_data_size(len));
128 if (!hex_decode(str, len, data, hex_data_size(len)))
129 return NULL;
130 return data;
131 }
132
133 /* Use the POSIX C locale. */
setup_locale(void)134 void setup_locale(void)
135 {
136 setlocale(LC_ALL, "C");
137 putenv("LC_ALL=C"); /* For exec{l,lp,v,vp}(...) */
138 }
139
140 /* Initial creation of tmpctx. */
setup_tmpctx(void)141 void setup_tmpctx(void)
142 {
143 tmpctx = tal_arr_label(NULL, char, 0, "tmpctx");
144 }
145
146 /* Free any children of tmpctx. */
clean_tmpctx(void)147 void clean_tmpctx(void)
148 {
149 const tal_t *p;
150
151 /* Don't actually free tmpctx: we hand pointers to it around. */
152 while ((p = tal_first(tmpctx)) != NULL)
153 tal_free(p);
154 }
155
tal_arr_remove_(void * p,size_t elemsize,size_t n)156 void tal_arr_remove_(void *p, size_t elemsize, size_t n)
157 {
158 // p is a pointer-to-pointer for tal_resize.
159 char *objp = *(char **)p;
160 size_t len = tal_bytelen(objp);
161 assert(len % elemsize == 0);
162 assert((n + 1) * elemsize <= len);
163 memmove(objp + elemsize * n, objp + elemsize * (n+1),
164 len - (elemsize * (n+1)));
165 tal_resize((char **)p, len - elemsize);
166 }
167
tal_dup_talarr_(const tal_t * ctx,const tal_t * src TAKES,const char * label)168 void *tal_dup_talarr_(const tal_t *ctx, const tal_t *src TAKES, const char *label)
169 {
170 if (!src) {
171 /* Correctly handle TAKES on a NULL `src`. */
172 (void) taken(src);
173 return NULL;
174 }
175 return tal_dup_(ctx, src, 1, tal_bytelen(src), 0, label);
176 }
177
178 /* Check for valid UTF-8 */
utf8_check(const void * vbuf,size_t buflen)179 bool utf8_check(const void *vbuf, size_t buflen)
180 {
181 const u8 *buf = vbuf;
182 struct utf8_state utf8_state = UTF8_STATE_INIT;
183 bool need_more = false;
184
185 for (size_t i = 0; i < buflen; i++) {
186 if (!utf8_decode(&utf8_state, buf[i])) {
187 need_more = true;
188 continue;
189 }
190 need_more = false;
191 if (errno != 0)
192 return false;
193 }
194 return !need_more;
195 }
196
utf8_str(const tal_t * ctx,const u8 * buf TAKES,size_t buflen)197 char *utf8_str(const tal_t *ctx, const u8 *buf TAKES, size_t buflen)
198 {
199 char *ret;
200
201 if (!utf8_check(buf, buflen)) {
202 if (taken(buf))
203 tal_free(buf);
204 return NULL;
205 }
206
207 /* Add one for nul term */
208 ret = tal_dup_arr(ctx, char, (const char *)buf, buflen, 1);
209 ret[buflen] = '\0';
210 return ret;
211 }
212