Commit | Line | Data |
---|---|---|
2fe58dfd SE |
1 | #ifndef util_h |
2 | #define util_h | |
3 | ||
2fe58dfd | 4 | #include "secnet.h" |
2fe58dfd SE |
5 | #include <gmp.h> |
6 | ||
3b83c932 SE |
7 | #include "hackypar.h" |
8 | ||
2fe58dfd SE |
9 | #define BUF_ASSERT_FREE(buf) do { buffer_assert_free((buf), \ |
10 | __FILE__,__LINE__); } \ | |
11 | while(0) | |
12 | #define BUF_ASSERT_USED(buf) do { buffer_assert_used((buf), \ | |
13 | __FILE__,__LINE__); } \ | |
14 | while(0) | |
15 | #define BUF_ALLOC(buf,own) do { buffer_assert_free((buf),__FILE__,__LINE__); \ | |
16 | (buf)->free=False; (buf)->owner=(own); (buf)->start=(buf)->base; \ | |
17 | (buf)->size=0; } while(0) | |
18 | #define BUF_FREE(buf) do { (buf)->free=True; } while(0) | |
19 | ||
fe5e9cc4 | 20 | extern void buffer_assert_free(struct buffer_if *buffer, cstring_t file, |
1caa23ff | 21 | int line); |
fe5e9cc4 | 22 | extern void buffer_assert_used(struct buffer_if *buffer, cstring_t file, |
1caa23ff IJ |
23 | int line); |
24 | extern void buffer_new(struct buffer_if *buffer, int32_t len); | |
25 | extern void buffer_init(struct buffer_if *buffer, int32_t max_start_pad); | |
05f39b4d | 26 | extern void buffer_copy(struct buffer_if *dst, const struct buffer_if *src); |
1caa23ff IJ |
27 | extern void *buf_append(struct buffer_if *buf, int32_t amount); |
28 | extern void *buf_prepend(struct buffer_if *buf, int32_t amount); | |
29 | extern void *buf_unappend(struct buffer_if *buf, int32_t amount); | |
30 | extern void *buf_unprepend(struct buffer_if *buf, int32_t amount); | |
2fe58dfd | 31 | |
92795040 IJ |
32 | static inline int32_t buf_remaining_space(const struct buffer_if *buf) |
33 | { | |
10068344 | 34 | return (buf->base + buf->alloclen) - buf->start; |
92795040 IJ |
35 | } |
36 | ||
28db900b IJ |
37 | extern void buffer_readonly_view(struct buffer_if *n, const void*, int32_t len); |
38 | extern void buffer_readonly_clone(struct buffer_if *n, const struct buffer_if*); | |
39 | /* Caller must only use unappend, unprepend et al. on n. | |
40 | * New buffer state (in n) before this can be undefined. After use, | |
41 | * it must NOT be freed. */ | |
42 | ||
fe5e9cc4 | 43 | extern void buf_append_string(struct buffer_if *buf, cstring_t s); |
2fe58dfd | 44 | |
2fe58dfd SE |
45 | extern void read_mpbin(MP_INT *a, uint8_t *bin, int binsize); |
46 | ||
47 | extern char *write_mpstring(MP_INT *a); | |
48 | ||
1caa23ff | 49 | extern int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen); |
2fe58dfd SE |
50 | |
51 | extern struct log_if *init_log(list_t *loglist); | |
52 | ||
8534d602 IJ |
53 | extern void send_nak(const struct comm_addr *dest, uint32_t our_index, |
54 | uint32_t their_index, uint32_t msgtype, | |
55 | struct buffer_if *buf, const char *logwhy); | |
56 | ||
5ad34db2 IJ |
57 | extern int consttime_memeq(const void *s1, const void *s2, size_t n); |
58 | ||
ceb05e5d IJ |
59 | #define MINMAX(ae,be,op) ({ \ |
60 | typeof((ae)) a=(ae); \ | |
61 | typeof((be)) b=(be); \ | |
62 | a op b ? a : b; \ | |
63 | }) | |
64 | #define MAX(a,b) MINMAX((a),(b),>) | |
65 | #define MIN(a,b) MINMAX((a),(b),<) | |
66 | ||
2fe58dfd | 67 | #endif /* util_h */ |