diff options
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/aho-corasick/acism.c | 191 | ||||
-rw-r--r-- | contrib/cdb/cdb_init.c | 54 | ||||
-rw-r--r-- | contrib/lc-btrie/btrie.c | 1251 | ||||
-rw-r--r-- | contrib/lc-btrie/btrie.h | 17 | ||||
-rw-r--r-- | contrib/librdns/resolver.c | 780 | ||||
-rw-r--r-- | contrib/libucl/lua_ucl.c | 907 | ||||
-rw-r--r-- | contrib/libucl/ucl_hash.c | 236 |
7 files changed, 1888 insertions, 1548 deletions
diff --git a/contrib/aho-corasick/acism.c b/contrib/aho-corasick/acism.c index e2b48a590..b0cee0d66 100644 --- a/contrib/aho-corasick/acism.c +++ b/contrib/aho-corasick/acism.c @@ -1,4 +1,20 @@ /* + * Copyright 2024 Vsevolod Stakhov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* ** Copyright (C) 2009-2014 Mischa Sandberg <mischasan@gmail.com> ** ** This program is free software; you can redistribute it and/or modify @@ -22,103 +38,104 @@ #include "_acism.h" #include "unix-std.h" -#define BACK ((SYMBOL)0) +#define BACK ((SYMBOL) 0) #define ROOT ((STATE) 0) -extern const guchar lc_map[256]; +extern const unsigned char lc_map[256]; -int -acism_lookup(ac_trie_t const *psp, const char *text, size_t len, - ACISM_ACTION *cb, void *context, int *statep, bool caseless) +int acism_lookup(ac_trie_t const *psp, const char *text, size_t len, + ACISM_ACTION *cb, void *context, int *statep, bool caseless) { - char const *cp = text, *endp = cp + len; - uint8_t s; - STATE state = *statep; - int ret = 0; - - while (cp < endp) { - s = caseless ? lc_map[(guint8)*cp++] : *cp++; - _SYMBOL sym = psp->symv[s]; - if (!sym) { - // Input byte is not in any pattern string. - state = ROOT; - continue; - } - - // Search for a valid transition from this (state, sym), - // following the backref chain. - - TRAN next; - while (!t_valid(psp, next = p_tran(psp, state, sym)) && state != ROOT) { - TRAN back = p_tran(psp, state, BACK); - state = t_valid(psp, back) ? t_next(psp, back) : ROOT; - } - - if (!t_valid(psp, next)) - continue; - - if (!(next & (IS_MATCH | IS_SUFFIX))) { - // No complete match yet; keep going. - state = t_next(psp, next); - continue; - } - - // At this point, one or more patterns have matched. - // Find all matches by following the backref chain. - // A valid node for (sym) with no SUFFIX flag marks the - // end of the suffix chain. - // In the same backref traversal, find a new (state), - // if the original transition is to a leaf. - - STATE s = state; - - // Initially state is ROOT. The chain search saves the - // first state from which the next char has a transition. - state = t_isleaf(psp, next) ? 0 : t_next(psp, next); - - while (1) { - - if (t_valid(psp, next)) { - - if (next & IS_MATCH) { - unsigned strno, ss = s + sym, i; - if (t_isleaf(psp, psp->tranv[ss])) { - strno = t_strno(psp, psp->tranv[ss]); - } else { - for (i = p_hash(psp, ss); psp->hashv[i].state != ss; ++i); - strno = psp->hashv[i].strno; - } - - if ((ret = cb(strno, cp - text, context))) - goto EXIT; - } - - if (!state && !t_isleaf(psp, next)) - state = t_next(psp, next); - if ( state && !(next & IS_SUFFIX)) - break; - } - - if (s == ROOT) - break; - - TRAN b = p_tran(psp, s, BACK); - s = t_valid(psp, b) ? t_next(psp, b) : ROOT; - next = p_tran(psp, s, sym); - } - } + char const *cp = text, *endp = cp + len; + uint8_t s; + STATE state = *statep; + int ret = 0; + + while (cp < endp) { + s = caseless ? lc_map[(uint8_t) *cp++] : *cp++; + _SYMBOL sym = psp->symv[s]; + if (!sym) { + // Input byte is not in any pattern string. + state = ROOT; + continue; + } + + // Search for a valid transition from this (state, sym), + // following the backref chain. + + TRAN next; + while (!t_valid(psp, next = p_tran(psp, state, sym)) && state != ROOT) { + TRAN back = p_tran(psp, state, BACK); + state = t_valid(psp, back) ? t_next(psp, back) : ROOT; + } + + if (!t_valid(psp, next)) + continue; + + if (!(next & (IS_MATCH | IS_SUFFIX))) { + // No complete match yet; keep going. + state = t_next(psp, next); + continue; + } + + // At this point, one or more patterns have matched. + // Find all matches by following the backref chain. + // A valid node for (sym) with no SUFFIX flag marks the + // end of the suffix chain. + // In the same backref traversal, find a new (state), + // if the original transition is to a leaf. + + STATE s = state; + + // Initially state is ROOT. The chain search saves the + // first state from which the next char has a transition. + state = t_isleaf(psp, next) ? 0 : t_next(psp, next); + + while (1) { + + if (t_valid(psp, next)) { + + if (next & IS_MATCH) { + unsigned strno, ss = s + sym, i; + if (t_isleaf(psp, psp->tranv[ss])) { + strno = t_strno(psp, psp->tranv[ss]); + } + else { + for (i = p_hash(psp, ss); psp->hashv[i].state != ss; ++i) + ; + strno = psp->hashv[i].strno; + } + + if ((ret = cb(strno, cp - text, context))) + goto EXIT; + } + + if (!state && !t_isleaf(psp, next)) + state = t_next(psp, next); + if (state && !(next & IS_SUFFIX)) + break; + } + + if (s == ROOT) + break; + + TRAN b = p_tran(psp, s, BACK); + s = t_valid(psp, b) ? t_next(psp, b) : ROOT; + next = p_tran(psp, s, sym); + } + } EXIT: *statep = state; - return ret; + return ret; } -void -acism_destroy(ac_trie_t *psp) +void acism_destroy(ac_trie_t *psp) { if (!psp) return; if (psp->flags & IS_MMAP) - munmap((char*)psp->tranv - sizeof(ac_trie_t), - sizeof(ac_trie_t) + p_size(psp)); - else g_free(psp->tranv); + munmap((char *) psp->tranv - sizeof(ac_trie_t), + sizeof(ac_trie_t) + p_size(psp)); + else + g_free(psp->tranv); g_free(psp); } //EOF diff --git a/contrib/cdb/cdb_init.c b/contrib/cdb/cdb_init.c index bfc6dd0c2..278506d21 100644 --- a/contrib/cdb/cdb_init.c +++ b/contrib/cdb/cdb_init.c @@ -18,8 +18,7 @@ cdb_hash(const void *buf, unsigned len) return hash; } -int -cdb_init(struct cdb *cdbp, int fd) +int cdb_init(struct cdb *cdbp, int fd) { struct stat st; unsigned char *mem; @@ -29,7 +28,7 @@ cdb_init(struct cdb *cdbp, int fd) #endif /* get file size */ - if (fstat (fd, &st) < 0) + if (fstat(fd, &st) < 0) return -1; /* trivial sanity check: at least toc should be here */ if (st.st_size < 2048) @@ -39,16 +38,16 @@ cdb_init(struct cdb *cdbp, int fd) #ifdef _WIN32 hFile = (HANDLE) _get_osfhandle(fd); if (hFile == (HANDLE) -1) - return -1; + return -1; hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (!hMapping) - return -1; - mem = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); + return -1; + mem = (unsigned char *) MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0); CloseHandle(hMapping); if (!mem) - return -1; + return -1; #else - mem = (unsigned char*) mmap (NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); + mem = (unsigned char *) mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0); if (mem == MAP_FAILED) return -1; #endif /* _WIN32 */ @@ -60,7 +59,7 @@ cdb_init(struct cdb *cdbp, int fd) cdbp->cdb_vpos = cdbp->cdb_vlen = 0; cdbp->cdb_kpos = cdbp->cdb_klen = 0; - dend = cdb_unpack (mem); + dend = cdb_unpack(mem); if (dend < 2048) dend = 2048; else if (dend >= fsize) @@ -70,21 +69,20 @@ cdb_init(struct cdb *cdbp, int fd) return 0; } -void -cdb_free(struct cdb *cdbp) +void cdb_free(struct cdb *cdbp) { if (cdbp->cdb_mem) { #ifdef _WIN32 - UnmapViewOfFile((void*) cdbp->cdb_mem); + UnmapViewOfFile((void *) cdbp->cdb_mem); #else - munmap ((void*) cdbp->cdb_mem, cdbp->cdb_fsize); + munmap((void *) cdbp->cdb_mem, cdbp->cdb_fsize); #endif /* _WIN32 */ cdbp->cdb_mem = NULL; } cdbp->cdb_fsize = 0; if (cdbp->loop) { - ev_stat_stop (cdbp->loop, &cdbp->stat_ev); + ev_stat_stop(cdbp->loop, &cdbp->stat_ev); } } @@ -98,43 +96,41 @@ cdb_get(const struct cdb *cdbp, unsigned len, unsigned pos) return cdbp->cdb_mem + pos; } -int -cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos) +int cdb_read(const struct cdb *cdbp, void *buf, unsigned len, unsigned pos) { - const void *data = cdb_get (cdbp, len, pos); + const void *data = cdb_get(cdbp, len, pos); if (!data) return -1; - memcpy (buf, data, len); + memcpy(buf, data, len); return 0; } static void -cdb_timer_callback (EV_P_ ev_stat *w, int revents) +cdb_timer_callback(EV_P_ ev_stat *w, int revents) { struct cdb *cdbp = w->data; - gint nfd; + int nfd; /* Check cdb file for modifications */ - if ((nfd = open (cdbp->filename, O_RDONLY)) != -1) { + if ((nfd = open(cdbp->filename, O_RDONLY)) != -1) { if (cdbp->cdb_mem) { #ifdef _WIN32 - UnmapViewOfFile((void*) cdbp->cdb_mem); + UnmapViewOfFile((void *) cdbp->cdb_mem); #else - munmap ((void*) cdbp->cdb_mem, cdbp->cdb_fsize); + munmap((void *) cdbp->cdb_mem, cdbp->cdb_fsize); #endif /* _WIN32 */ cdbp->cdb_mem = NULL; } - (void)close (cdbp->cdb_fd); + (void) close(cdbp->cdb_fd); cdbp->cdb_fsize = 0; - (void)cdb_init (cdbp, nfd); + (void) cdb_init(cdbp, nfd); } } -void -cdb_add_timer (struct cdb *cdbp, EV_P_ ev_tstamp seconds) +void cdb_add_timer(struct cdb *cdbp, EV_P_ ev_tstamp seconds) { cdbp->loop = loop; - ev_stat_init (&cdbp->stat_ev, cdb_timer_callback, cdbp->filename, seconds); + ev_stat_init(&cdbp->stat_ev, cdb_timer_callback, cdbp->filename, seconds); cdbp->stat_ev.data = cdbp; - ev_stat_start (EV_A_ &cdbp->stat_ev); + ev_stat_start(EV_A_ & cdbp->stat_ev); } diff --git a/contrib/lc-btrie/btrie.c b/contrib/lc-btrie/btrie.c index 81b69b2b9..c7272ec20 100644 --- a/contrib/lc-btrie/btrie.c +++ b/contrib/lc-btrie/btrie.c @@ -302,8 +302,8 @@ #include <string.h> #include <setjmp.h> #if defined(TEST) && defined(NDEBUG) -# warning undefining NDEBUG for TEST build -# undef NDEBUG +#warning undefining NDEBUG for TEST build +#undef NDEBUG #endif #include <assert.h> @@ -314,54 +314,54 @@ #define SIZEOF_VOID_P __SIZEOF_POINTER__ #else #if defined(__ILP32__) || defined(__ILP32) || defined(_ILP32) -# define SIZEOF_VOID_P 4 +#define SIZEOF_VOID_P 4 #elif defined(__ILP64__) || defined(__ILP64) || defined(_ILP64) -# define SIZEOF_VOID_P 8 +#define SIZEOF_VOID_P 8 #elif defined(__LLP64__) || defined(__LLP64) || defined(_LLP64) || defined(_WIN64) -# define SIZEOF_VOID_P 8 +#define SIZEOF_VOID_P 8 #elif defined(__LP64__) || defined(__LP64) || defined(_LP64) -# define SIZEOF_VOID_P 8 +#define SIZEOF_VOID_P 8 #elif defined(UINTPTR_MAX) && defined(UINT64_MAX) && (UINTPTR_MAX == UINT64_MAX) -# define SIZEOF_VOID_P 8 +#define SIZEOF_VOID_P 8 #else -# define SIZEOF_VOID_P 4 +#define SIZEOF_VOID_P 4 #endif #endif #if SIZEOF_VOID_P == 4 -# define TBM_STRIDE 4 +#define TBM_STRIDE 4 #elif SIZEOF_VOID_P == 8 -# define TBM_STRIDE 5 +#define TBM_STRIDE 5 #else -# error "Unsupported word size" +#error "Unsupported word size" #endif #ifndef NO_STDINT_H -# if TBM_STRIDE == 4 +#if TBM_STRIDE == 4 typedef uint16_t tbm_bitmap_t; -# else +#else typedef uint32_t tbm_bitmap_t; -# endif +#endif #else /* NO_STDINT_H */ -# if TBM_STRIDE == 4 -# if SIZEOF_SHORT == 2 +#if TBM_STRIDE == 4 +#if SIZEOF_SHORT == 2 typedef short unsigned tbm_bitmap_t; -# else -# error "can not determine type for 16 bit unsigned int" -# endif -# else /* TBM_STRIDE == 5 */ -# if SIZEOF_INT == 4 +#else +#error "can not determine type for 16 bit unsigned int" +#endif +#else /* TBM_STRIDE == 5 */ +#if SIZEOF_INT == 4 typedef unsigned tbm_bitmap_t; -# elif SIZEOF_LONG == 4 +#elif SIZEOF_LONG == 4 typedef long unsigned tbm_bitmap_t; -# else -# error "can not determine type for 32 bit unsigned int" -# endif -# endif +#else +#error "can not determine type for 32 bit unsigned int" +#endif +#endif #endif -#define TBM_FANOUT (1U << TBM_STRIDE) -#define LC_BYTES_PER_NODE (SIZEOF_VOID_P - 1) +#define TBM_FANOUT (1U << TBM_STRIDE) +#define LC_BYTES_PER_NODE (SIZEOF_VOID_P - 1) typedef union node_u node_t; @@ -373,8 +373,7 @@ typedef union node_u node_t; * lc_node.) */ -struct tbm_node -{ +struct tbm_node { #ifdef WORDS_BIGENDIAN tbm_bitmap_t int_bm; /* the internal bitmap */ tbm_bitmap_t ext_bm; /* extending path ("external") bitmap */ @@ -382,21 +381,19 @@ struct tbm_node tbm_bitmap_t ext_bm; /* extending path ("external") bitmap */ tbm_bitmap_t int_bm; /* the internal bitmap */ #endif - union - { - node_t *children; /* pointer to array of children */ + union { + node_t *children; /* pointer to array of children */ const void **data_end; /* one past end of internal prefix data array */ } ptr; }; -struct lc_node -{ +struct lc_node { /* lc_flags contains the LC prefix length and a couple of bit flags * (apparently char-sized bit fields are a gcc extension) */ -# define LC_FLAGS_IS_LC 0x80 -# define LC_FLAGS_IS_TERMINAL 0x40 -# define LC_FLAGS_LEN_MASK 0x3f +#define LC_FLAGS_IS_LC 0x80 +#define LC_FLAGS_IS_TERMINAL 0x40 +#define LC_FLAGS_LEN_MASK 0x3f #ifdef WORDS_BIGENDIAN btrie_oct_t lc_flags; btrie_oct_t prefix[LC_BYTES_PER_NODE]; @@ -404,28 +401,24 @@ struct lc_node btrie_oct_t prefix[LC_BYTES_PER_NODE]; btrie_oct_t lc_flags; #endif - union - { - node_t *child; /* pointer to child (if !is_terminal) */ + union { + node_t *child; /* pointer to child (if !is_terminal) */ const void *data; /* the prefix data (if is_terminal) */ } ptr; }; -union node_u -{ +union node_u { struct tbm_node tbm_node; struct lc_node lc_node; }; -struct free_hunk -{ +struct free_hunk { struct free_hunk *next; }; #define MAX_CHILD_ARRAY_LEN (TBM_FANOUT + TBM_FANOUT / 2) -struct btrie -{ +struct btrie { node_t root; rspamd_mempool_t *mp; @@ -433,16 +426,16 @@ struct btrie jmp_buf exception; /* mem mgmt stats */ size_t alloc_total; /* total bytes allocated from mempool */ - size_t alloc_data; /* bytes allocated for TBM node int. prefix data */ + size_t alloc_data; /* bytes allocated for TBM node int. prefix data */ size_t alloc_waste; /* bytes wasted by rounding of data array size */ #ifdef BTRIE_DEBUG_ALLOC size_t alloc_hist[MAX_CHILD_ARRAY_LEN * 2]; /* histogram of alloc sizes */ #endif /* trie stats */ - size_t n_entries; /* number of entries */ + size_t n_entries; /* number of entries */ size_t n_tbm_nodes; /* total number of TBM nodes in tree */ - size_t n_lc_nodes; /* total number of LC nodes in tree */ + size_t n_lc_nodes; /* total number of LC nodes in tree */ }; /**************************************************************** @@ -487,7 +480,7 @@ alloc_nodes(struct btrie *btrie, unsigned nchildren, unsigned ndata) assert(n_nodes > 0 && n_nodes <= MAX_CHILD_ARRAY_LEN); - hunk = _get_hunk (btrie, n_nodes); + hunk = _get_hunk(btrie, n_nodes); if (hunk == NULL) { /* Do not have free hunk of exactly the requested size, look for a * larger hunk. (The funny order in which we scan the buckets is @@ -496,27 +489,28 @@ alloc_nodes(struct btrie *btrie, unsigned nchildren, unsigned ndata) */ size_t n, skip = n_nodes > 4 ? 4 : n_nodes; for (n = n_nodes + skip; n <= MAX_CHILD_ARRAY_LEN; n++) { - if ((hunk = _get_hunk (btrie, n)) != NULL) { - _free_hunk (btrie, hunk + n_nodes, n - n_nodes); + if ((hunk = _get_hunk(btrie, n)) != NULL) { + _free_hunk(btrie, hunk + n_nodes, n - n_nodes); goto DONE; } } for (n = n_nodes + 1; n < n_nodes + skip && n <= MAX_CHILD_ARRAY_LEN; - n++) { - if ((hunk = _get_hunk (btrie, n)) != NULL) { - _free_hunk (btrie, hunk + n_nodes, n - n_nodes); + n++) { + if ((hunk = _get_hunk(btrie, n)) != NULL) { + _free_hunk(btrie, hunk + n_nodes, n - n_nodes); goto DONE; } } /* failed to find free hunk, allocate a fresh one */ - hunk = rspamd_mempool_alloc0 (btrie->mp, n_nodes * sizeof(node_t)); + hunk = rspamd_mempool_alloc0(btrie->mp, n_nodes * sizeof(node_t)); if (hunk == NULL) - longjmp (btrie->exception, BTRIE_ALLOC_FAILED); + longjmp(btrie->exception, BTRIE_ALLOC_FAILED); btrie->alloc_total += n_nodes * sizeof(node_t); } - DONE: btrie->alloc_data += ndata * sizeof(void *); +DONE: + btrie->alloc_data += ndata * sizeof(void *); btrie->alloc_waste += (ndata % 2) * sizeof(void *); #ifdef BTRIE_DEBUG_ALLOC btrie->alloc_hist[2 * nchildren + ndata]++; @@ -528,13 +522,13 @@ alloc_nodes(struct btrie *btrie, unsigned nchildren, unsigned ndata) /* Free memory allocated by alloc_nodes */ static void free_nodes(struct btrie *btrie, node_t *buf, unsigned nchildren, - unsigned ndata) + unsigned ndata) { size_t n_nodes = nchildren + (ndata + 1) / 2; assert(n_nodes > 0 && n_nodes <= MAX_CHILD_ARRAY_LEN); - _free_hunk (btrie, buf - (ndata + 1) / 2, n_nodes); + _free_hunk(btrie, buf - (ndata + 1) / 2, n_nodes); btrie->alloc_data -= ndata * sizeof(void *); btrie->alloc_waste -= (ndata % 2) * sizeof(void *); @@ -567,12 +561,12 @@ dump_alloc_hist(const struct btrie *btrie) if (bin % 2 == 0) { const struct free_hunk *hunk; for (hunk = btrie->free_list[bin / 2 - 1]; hunk; hunk = hunk->next) - n_free++; + n_free++; } free_bytes = n_free * bin * sizeof(void *); printf("%3zu: %6zu %6zu %8zu %8zu %8zu\n", bin * sizeof(void *), - n_alloc, n_free, bytes, waste_bytes, free_bytes); + n_alloc, n_free, bytes, waste_bytes, free_bytes); total_alloc += n_alloc; total_free += n_free; @@ -582,7 +576,7 @@ dump_alloc_hist(const struct btrie *btrie) } puts("---- ------ ------ -------- -------- --------"); printf("SUM: %6zu %6zu %8zu %8zu %8zu\n", - total_alloc, total_free, total_bytes, total_waste, total_free_bytes); + total_alloc, total_free, total_bytes, total_waste, total_free_bytes); } #endif @@ -622,17 +616,17 @@ static inline unsigned count_bits(tbm_bitmap_t v) static inline unsigned count_bits_before(tbm_bitmap_t bm, int b) { - return b ? count_bits (bm >> ((1 << TBM_STRIDE) - b)) : 0; + return b ? count_bits(bm >> ((1 << TBM_STRIDE) - b)) : 0; } static inline unsigned count_bits_from(tbm_bitmap_t bm, int b) { - return count_bits (bm << b); + return count_bits(bm << b); } /* extracts a few bits from bitstring, returning them as an integer */ static inline btrie_oct_t RSPAMD_NO_SANITIZE extract_bits(const btrie_oct_t *prefix, unsigned pos, - unsigned nbits) + unsigned nbits) { if (nbits == 0) return 0; @@ -650,38 +644,283 @@ static inline unsigned extract_bit(const btrie_oct_t *prefix, int pos) /* get mask for high n bits of a byte */ static inline btrie_oct_t high_bits(unsigned n) { - return (btrie_oct_t) -(1U << (8 - n)); + return (btrie_oct_t) - (1U << (8 - n)); } /* determine whether two prefixes are equal */ static inline int prefixes_equal(const btrie_oct_t *pfx1, - const btrie_oct_t *pfx2, unsigned len) + const btrie_oct_t *pfx2, unsigned len) { - return (memcmp (pfx1, pfx2, len / 8) == 0 - && (len % 8 == 0 || - ((pfx1[len / 8] ^ pfx2[len / 8]) & high_bits (len % 8)) == 0)); + return (memcmp(pfx1, pfx2, len / 8) == 0 && (len % 8 == 0 || + ((pfx1[len / 8] ^ pfx2[len / 8]) & high_bits(len % 8)) == 0)); } /* determine length of longest common subprefix */ static inline unsigned common_prefix(const btrie_oct_t *pfx1, - const btrie_oct_t *pfx2, unsigned len) + const btrie_oct_t *pfx2, unsigned len) { /* algorithm adapted from * http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogLookup */ static btrie_oct_t leading_zeros[] = - { 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, }; + { + 8, + 7, + 6, + 6, + 5, + 5, + 5, + 5, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 4, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 3, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 2, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + }; unsigned nb; for (nb = 0; nb < len / 8; nb++) { @@ -712,13 +951,13 @@ static inline int is_lc_node(const node_t *node) static inline int is_tbm_node(const node_t *node) { - return !is_lc_node (node); + return !is_lc_node(node); } /* is node a TBM node with internal data? */ static inline int has_data(const node_t *node) { - return is_tbm_node (node) && node->tbm_node.int_bm != 0; + return is_tbm_node(node) && node->tbm_node.int_bm != 0; } static inline unsigned base_index(unsigned pfx, unsigned plen) @@ -739,108 +978,136 @@ static inline void init_empty_node(struct btrie *btrie, node_t *node) static inline const void ** tbm_data_p(const struct tbm_node *node, unsigned pfx, unsigned plen) { - unsigned bi = base_index (pfx, plen); + unsigned bi = base_index(pfx, plen); - if ((node->int_bm & bit (bi)) == 0) + if ((node->int_bm & bit(bi)) == 0) return NULL; /* no data */ else { - return &node->ptr.data_end[-(int) count_bits_from (node->int_bm, bi)]; + return &node->ptr.data_end[-(int) count_bits_from(node->int_bm, bi)]; } } /* add an element to the internal data array */ static void tbm_insert_data(struct btrie *btrie, struct tbm_node *node, - unsigned pfx, unsigned plen, const void *data) + unsigned pfx, unsigned plen, const void *data) { /* XXX: don't realloc if already big enough? */ - unsigned bi = base_index (pfx, plen); - unsigned nchildren = count_bits (node->ext_bm); - int ndata = count_bits (node->int_bm); - unsigned di = count_bits_before (node->int_bm, bi); + unsigned bi = base_index(pfx, plen); + unsigned nchildren = count_bits(node->ext_bm); + int ndata = count_bits(node->int_bm); + unsigned di = count_bits_before(node->int_bm, bi); node_t *old_children = node->ptr.children; const void **old_data_beg = node->ptr.data_end - ndata; const void **data_beg; - assert((node->int_bm & bit (bi)) == 0); + assert((node->int_bm & bit(bi)) == 0); - node->ptr.children = alloc_nodes (btrie, nchildren, ndata + 1); + node->ptr.children = alloc_nodes(btrie, nchildren, ndata + 1); data_beg = node->ptr.data_end - (ndata + 1); data_beg[di] = data; - node->int_bm |= bit (bi); + node->int_bm |= bit(bi); if (nchildren != 0 || ndata != 0) { memcpy(data_beg, old_data_beg, di * sizeof(data_beg[0])); memcpy(&data_beg[di + 1], &old_data_beg[di], - (ndata - di) * sizeof(data_beg[0]) - + nchildren * sizeof(node_t)); - free_nodes (btrie, old_children, nchildren, ndata); + (ndata - di) * sizeof(data_beg[0]) + nchildren * sizeof(node_t)); + free_nodes(btrie, old_children, nchildren, ndata); } } /* determine whether TBM has internal prefix data for pfx/plen or ancestors */ static inline int has_internal_data(const struct tbm_node *node, unsigned pfx, - unsigned plen) + unsigned plen) { -# define BIT(n) (1U << ((1 << TBM_STRIDE) - 1 - (n))) -# define B0() BIT(1) /* the bit for 0/0 */ -# define B1(n) (BIT((n) + 2) | B0()) /* the bits for n/1 and its ancestors */ -# define B2(n) (BIT((n) + 4) | B1(n >> 1)) /* the bits for n/2 and ancestors */ -# define B3(n) (BIT((n) + 8) | B2(n >> 1)) /* the bits for n/3 and ancestors */ -# define B4(n) (BIT((n) + 16) | B3(n >> 1)) /* the bits for n/4 and ancestors */ +#define BIT(n) (1U << ((1 << TBM_STRIDE) - 1 - (n))) +#define B0() BIT(1) /* the bit for 0/0 */ +#define B1(n) (BIT((n) + 2) | B0()) /* the bits for n/1 and its ancestors */ +#define B2(n) (BIT((n) + 4) | B1(n >> 1)) /* the bits for n/2 and ancestors */ +#define B3(n) (BIT((n) + 8) | B2(n >> 1)) /* the bits for n/3 and ancestors */ +#define B4(n) (BIT((n) + 16) | B3(n >> 1)) /* the bits for n/4 and ancestors */ static tbm_bitmap_t ancestors[] = - { 0, B0(), B1(0), B1(1), B2(0), B2(1), B2(2), B2(3), B3(0), B3(1), B3(2), - B3(3), B3(4), B3(5), B3(6), B3(7), -# if TBM_STRIDE == 5 - B4(0), B4(1), B4(2), B4(3), B4(4), B4(5), B4(6), B4(7), B4(8), B4( - 9), B4(10), B4(11), B4(12), B4(13), B4(14), B4(15), -# elif TBM_STRIDE != 4 -# error "unsupported TBM_STRIDE" -# endif - }; -# undef B4 -# undef B3 -# undef B2 -# undef B1 -# undef B0 -# undef BIT - - return (node->int_bm & ancestors[base_index (pfx, plen)]) != 0; + { 0, + B0(), + B1(0), + B1(1), + B2(0), + B2(1), + B2(2), + B2(3), + B3(0), + B3(1), + B3(2), + B3(3), + B3(4), + B3(5), + B3(6), + B3(7), +#if TBM_STRIDE == 5 + B4(0), + B4(1), + B4(2), + B4(3), + B4(4), + B4(5), + B4(6), + B4(7), + B4(8), + B4( + 9), + B4(10), + B4(11), + B4(12), + B4(13), + B4(14), + B4(15), +#elif TBM_STRIDE != 4 +#error "unsupported TBM_STRIDE" +#endif + }; +#undef B4 +#undef B3 +#undef B2 +#undef B1 +#undef B0 +#undef BIT + + return (node->int_bm & ancestors[base_index(pfx, plen)]) != 0; } /* get pointer to TBM extending path */ static inline node_t * tbm_ext_path(const struct tbm_node *node, unsigned pfx) { - if ((node->ext_bm & bit (pfx)) == 0) + if ((node->ext_bm & bit(pfx)) == 0) return NULL; else - return &node->ptr.children[count_bits_before (node->ext_bm, pfx)]; + return &node->ptr.children[count_bits_before(node->ext_bm, pfx)]; } /* resize TBM node child array to make space for new child node */ static node_t * tbm_insert_ext_path(struct btrie *btrie, struct tbm_node *node, unsigned pfx) { - unsigned nchildren = count_bits (node->ext_bm); - unsigned ci = count_bits_before (node->ext_bm, pfx); - int ndata = count_bits (node->int_bm); + unsigned nchildren = count_bits(node->ext_bm); + unsigned ci = count_bits_before(node->ext_bm, pfx); + int ndata = count_bits(node->int_bm); node_t *old_children = node->ptr.children; const void **old_data_beg = node->ptr.data_end - ndata; - assert((node->ext_bm & bit (pfx)) == 0); + assert((node->ext_bm & bit(pfx)) == 0); - node->ptr.children = alloc_nodes (btrie, nchildren + 1, ndata); - init_empty_node (btrie, &node->ptr.children[ci]); - node->ext_bm |= bit (pfx); + node->ptr.children = alloc_nodes(btrie, nchildren + 1, ndata); + init_empty_node(btrie, &node->ptr.children[ci]); + node->ext_bm |= bit(pfx); if (nchildren != 0 || ndata != 0) { const void **data_beg = node->ptr.data_end - ndata; memcpy(data_beg, old_data_beg, - ndata * sizeof(data_beg[0]) + ci * sizeof(node_t)); + ndata * sizeof(data_beg[0]) + ci * sizeof(node_t)); memcpy(&node->ptr.children[ci + 1], &old_children[ci], - (nchildren - ci) * sizeof(old_children[0])); - free_nodes (btrie, old_children, nchildren, ndata); + (nchildren - ci) * sizeof(old_children[0])); + free_nodes(btrie, old_children, nchildren, ndata); } return &node->ptr.children[ci]; @@ -857,7 +1124,7 @@ static inline unsigned lc_len(const struct lc_node *node) } static inline void lc_init_flags(struct lc_node *node, int is_terminal, - unsigned len) + unsigned len) { assert((len & ~LC_FLAGS_LEN_MASK) == 0); node->lc_flags = LC_FLAGS_IS_LC | len; @@ -867,7 +1134,7 @@ static inline void lc_init_flags(struct lc_node *node, int is_terminal, static inline void lc_add_to_len(struct lc_node *node, int increment) { - unsigned new_len = lc_len (node) + increment; + unsigned new_len = lc_len(node) + increment; assert((new_len & ~LC_FLAGS_LEN_MASK) == 0); node->lc_flags = (node->lc_flags & ~LC_FLAGS_LEN_MASK) | new_len; } @@ -879,23 +1146,23 @@ static inline unsigned lc_shift(unsigned pos) static inline unsigned lc_base(unsigned pos) { - return 8 * lc_shift (pos); + return 8 * lc_shift(pos); } static inline unsigned lc_bits(const struct lc_node *node, unsigned pos) { - return pos % 8 + lc_len (node); + return pos % 8 + lc_len(node); } static inline unsigned lc_bytes(const struct lc_node *node, unsigned pos) { - return (lc_bits (node, pos) + 7) / 8; + return (lc_bits(node, pos) + 7) / 8; } static inline unsigned lc_leading_bits(const struct lc_node *node, unsigned pos, - unsigned nbits) + unsigned nbits) { - return extract_bits (node->prefix, pos % 8, nbits); + return extract_bits(node->prefix, pos % 8, nbits); } /* Initialize a new terminal LC node @@ -904,22 +1171,22 @@ static inline unsigned lc_leading_bits(const struct lc_node *node, unsigned pos, * of LC nodes will be created. */ static void init_terminal_node(struct btrie *btrie, node_t *dst, unsigned pos, - const btrie_oct_t *prefix, unsigned len, const void *data) + const btrie_oct_t *prefix, unsigned len, const void *data) { struct lc_node *node = &dst->lc_node; unsigned nbytes = (len + 7) / 8; - while (nbytes - lc_shift (pos) > LC_BYTES_PER_NODE) { - memcpy(node->prefix, prefix + lc_shift (pos), LC_BYTES_PER_NODE); - lc_init_flags (node, 0, 8 * LC_BYTES_PER_NODE - pos % 8); - node->ptr.child = alloc_nodes (btrie, 1, 0); - pos += lc_len (node); + while (nbytes - lc_shift(pos) > LC_BYTES_PER_NODE) { + memcpy(node->prefix, prefix + lc_shift(pos), LC_BYTES_PER_NODE); + lc_init_flags(node, 0, 8 * LC_BYTES_PER_NODE - pos % 8); + node->ptr.child = alloc_nodes(btrie, 1, 0); + pos += lc_len(node); node = &node->ptr.child->lc_node; btrie->n_lc_nodes++; } - memcpy(node->prefix, prefix + lc_shift (pos), nbytes - lc_shift (pos)); - lc_init_flags (node, 1, len - pos); + memcpy(node->prefix, prefix + lc_shift(pos), nbytes - lc_shift(pos)); + lc_init_flags(node, 1, len - pos); node->ptr.data = data; btrie->n_lc_nodes++; } @@ -929,75 +1196,74 @@ static void init_terminal_node(struct btrie *btrie, node_t *dst, unsigned pos, * also ensure that the leading nodes in the LC chain have maximum length. */ static void coalesce_lc_node(struct btrie *btrie, struct lc_node *node, - unsigned pos) + unsigned pos) { - while (!lc_is_terminal (node) && lc_bits (node, pos) < 8 * LC_BYTES_PER_NODE - && is_lc_node (node->ptr.child)) { + while (!lc_is_terminal(node) && lc_bits(node, pos) < 8 * LC_BYTES_PER_NODE && is_lc_node(node->ptr.child)) { struct lc_node *child = &node->ptr.child->lc_node; - unsigned spare_bits = 8 * LC_BYTES_PER_NODE - lc_bits (node, pos); - unsigned end = pos + lc_len (node); - unsigned shift = lc_shift (end) - lc_shift (pos); - if (lc_len (child) <= spare_bits) { + unsigned spare_bits = 8 * LC_BYTES_PER_NODE - lc_bits(node, pos); + unsigned end = pos + lc_len(node); + unsigned shift = lc_shift(end) - lc_shift(pos); + if (lc_len(child) <= spare_bits) { /* node plus child will fit in single node - merge */ - memcpy(node->prefix + shift, child->prefix, lc_bytes (child, end)); - lc_init_flags (node, lc_is_terminal (child), - lc_len (node) + lc_len (child)); + memcpy(node->prefix + shift, child->prefix, lc_bytes(child, end)); + lc_init_flags(node, lc_is_terminal(child), + lc_len(node) + lc_len(child)); node->ptr = child->ptr; - free_nodes (btrie, (node_t *) child, 1, 0); + free_nodes(btrie, (node_t *) child, 1, 0); btrie->n_lc_nodes--; } else { /* can't merge, but can take some of children bits */ - unsigned cshift = lc_shift (end + spare_bits) - lc_shift (end); + unsigned cshift = lc_shift(end + spare_bits) - lc_shift(end); memcpy(node->prefix + shift, child->prefix, - LC_BYTES_PER_NODE - shift); - lc_add_to_len (node, spare_bits); + LC_BYTES_PER_NODE - shift); + lc_add_to_len(node, spare_bits); if (cshift) memmove(child->prefix, child->prefix + cshift, - lc_bytes (child, end) - cshift); - assert(lc_len (child) > spare_bits); - lc_add_to_len (child, -spare_bits); + lc_bytes(child, end) - cshift); + assert(lc_len(child) > spare_bits); + lc_add_to_len(child, -spare_bits); - pos += lc_len (node); + pos += lc_len(node); node = child; } } } static void init_tbm_node(struct btrie *btrie, node_t *node, unsigned pos, - const btrie_oct_t pbyte, const void **root_data_p, node_t *left, - node_t *right); + const btrie_oct_t pbyte, const void **root_data_p, node_t *left, + node_t *right); /* given an LC node at orig_pos, create a new (shorter) node at pos */ static void shorten_lc_node(struct btrie *btrie, node_t *dst, unsigned pos, - struct lc_node *src, unsigned orig_pos) + struct lc_node *src, unsigned orig_pos) { assert(orig_pos < pos); - assert(lc_len (src) >= pos - orig_pos); - assert(dst != (node_t * )src); + assert(lc_len(src) >= pos - orig_pos); + assert(dst != (node_t *) src); - if (lc_len (src) == pos - orig_pos && !lc_is_terminal (src)) { + if (lc_len(src) == pos - orig_pos && !lc_is_terminal(src)) { /* just steal the child */ node_t *child = src->ptr.child; *dst = *child; - free_nodes (btrie, child, 1, 0); + free_nodes(btrie, child, 1, 0); btrie->n_lc_nodes--; } else { struct lc_node *node = &dst->lc_node; - unsigned shift = lc_shift (pos) - lc_shift (orig_pos); + unsigned shift = lc_shift(pos) - lc_shift(orig_pos); if (shift) { memmove(node->prefix, src->prefix + shift, - lc_bytes (src, orig_pos) - shift); + lc_bytes(src, orig_pos) - shift); node->lc_flags = src->lc_flags; node->ptr = src->ptr; } else { *node = *src; } - lc_add_to_len (node, -(pos - orig_pos)); - coalesce_lc_node (btrie, node, pos); + lc_add_to_len(node, -(pos - orig_pos)); + coalesce_lc_node(btrie, node, pos); } } @@ -1006,101 +1272,101 @@ static void shorten_lc_node(struct btrie *btrie, node_t *dst, unsigned pos, * on entry, node must have length at least len */ static void split_lc_node(struct btrie *btrie, struct lc_node *node, - unsigned pos, unsigned len) + unsigned pos, unsigned len) { - node_t *child = alloc_nodes (btrie, 1, 0); + node_t *child = alloc_nodes(btrie, 1, 0); - assert(lc_len (node) >= len); - shorten_lc_node (btrie, child, pos + len, node, pos); + assert(lc_len(node) >= len); + shorten_lc_node(btrie, child, pos + len, node, pos); - lc_init_flags (node, 0, len); + lc_init_flags(node, 0, len); node->ptr.child = child; btrie->n_lc_nodes++; } /* convert non-terminal LC node of length one to a TBM node *in place* */ static void convert_lc_node_1(struct btrie *btrie, struct lc_node *node, - unsigned pos) + unsigned pos) { btrie_oct_t pbyte = node->prefix[0]; node_t *child = node->ptr.child; node_t *left, *right; - assert(lc_len (node) == 1); - assert(!lc_is_terminal (node)); + assert(lc_len(node) == 1); + assert(!lc_is_terminal(node)); - if (extract_bit (node->prefix, pos % 8)) + if (extract_bit(node->prefix, pos % 8)) left = NULL, right = child; else left = child, right = NULL; - init_tbm_node (btrie, (node_t *) node, pos, pbyte, NULL, left, right); - free_nodes (btrie, child, 1, 0); + init_tbm_node(btrie, (node_t *) node, pos, pbyte, NULL, left, right); + free_nodes(btrie, child, 1, 0); btrie->n_lc_nodes--; } /* convert an LC node to TBM node *in place* */ static void convert_lc_node(struct btrie *btrie, struct lc_node *node, - unsigned pos) + unsigned pos) { - unsigned len = lc_len (node); + unsigned len = lc_len(node); if (len >= TBM_STRIDE) { - unsigned pfx = lc_leading_bits (node, pos, TBM_STRIDE); + unsigned pfx = lc_leading_bits(node, pos, TBM_STRIDE); struct tbm_node *result = (struct tbm_node *) node; /* split to LC of len TBM_STRIDE followed by child (extending path) */ - split_lc_node (btrie, node, pos, TBM_STRIDE); + split_lc_node(btrie, node, pos, TBM_STRIDE); /* then convert leading LC node to TBM node */ result->int_bm = 0; - result->ext_bm = bit (pfx); + result->ext_bm = bit(pfx); btrie->n_lc_nodes--; btrie->n_tbm_nodes++; } - else if (lc_is_terminal (node)) { + else if (lc_is_terminal(node)) { /* convert short terminal LC to TBM (with internal data) */ - unsigned pfx = lc_leading_bits (node, pos, len); + unsigned pfx = lc_leading_bits(node, pos, len); const void *data = node->ptr.data; node_t *result = (node_t *) node; - init_empty_node (btrie, result); - tbm_insert_data (btrie, &result->tbm_node, pfx, len, data); + init_empty_node(btrie, result); + tbm_insert_data(btrie, &result->tbm_node, pfx, len, data); btrie->n_lc_nodes--; } else { assert(len > 0); for (; len > 1; len--) { - split_lc_node (btrie, node, pos, len - 1); - convert_lc_node_1 (btrie, &node->ptr.child->lc_node, pos + len - 1); + split_lc_node(btrie, node, pos, len - 1); + convert_lc_node_1(btrie, &node->ptr.child->lc_node, pos + len - 1); } - convert_lc_node_1 (btrie, node, pos); + convert_lc_node_1(btrie, node, pos); } } static void insert_lc_node(struct btrie *btrie, node_t *dst, unsigned pos, - btrie_oct_t pbyte, unsigned last_bit, node_t *tail) + btrie_oct_t pbyte, unsigned last_bit, node_t *tail) { struct lc_node *node = &dst->lc_node; btrie_oct_t mask = 1 << (7 - (pos % 8)); btrie_oct_t bit = last_bit ? mask : 0; - if (mask != 0x01 && is_lc_node (tail)) { + if (mask != 0x01 && is_lc_node(tail)) { /* optimization: LC tail has room for the extra bit (without shifting) */ assert((tail->lc_node.prefix[0] & mask) == bit); *node = tail->lc_node; - lc_add_to_len (node, 1); + lc_add_to_len(node, 1); return; } /* add new leading LC node of len 1 */ node->prefix[0] = pbyte | bit; - lc_init_flags (node, 0, 1); - node->ptr.child = alloc_nodes (btrie, 1, 0); + lc_init_flags(node, 0, 1); + node->ptr.child = alloc_nodes(btrie, 1, 0); node->ptr.child[0] = *tail; btrie->n_lc_nodes++; - if (is_lc_node (tail)) - coalesce_lc_node (btrie, node, pos); + if (is_lc_node(tail)) + coalesce_lc_node(btrie, node, pos); } /* given: @@ -1110,14 +1376,14 @@ static void insert_lc_node(struct btrie *btrie, node_t *dst, unsigned pos, * the bits in the prefix between lc_base(pos + plen) and pos + plen */ static inline btrie_oct_t next_pbyte(btrie_oct_t pbyte, unsigned pos, - unsigned pfx) + unsigned pfx) { unsigned end = pos + TBM_STRIDE; if (end % 8 != 0) { btrie_oct_t nbyte = (btrie_oct_t) pfx << (8 - end % 8); if (end % 8 > TBM_STRIDE) - nbyte |= pbyte & high_bits (pos % 8); + nbyte |= pbyte & high_bits(pos % 8); return nbyte; } return 0; @@ -1127,8 +1393,8 @@ static inline btrie_oct_t next_pbyte(btrie_oct_t pbyte, unsigned pos, * root prefix of the new node. */ static void init_tbm_node(struct btrie *btrie, node_t *dst, unsigned pos, - const btrie_oct_t pbyte, const void **root_data_p, node_t *left, - node_t *right) + const btrie_oct_t pbyte, const void **root_data_p, node_t *left, + node_t *right) { struct tbm_node *node = &dst->tbm_node; unsigned nchildren = 0; @@ -1139,33 +1405,33 @@ static void init_tbm_node(struct btrie *btrie, node_t *dst, unsigned pos, tbm_bitmap_t int_bm = 0; unsigned i, d, pfx_base; - if (left && is_lc_node (left) && lc_len (&left->lc_node) < TBM_STRIDE) - convert_lc_node (btrie, &left->lc_node, pos + 1); - if (right && is_lc_node (right) && lc_len (&right->lc_node) < TBM_STRIDE) - convert_lc_node (btrie, &right->lc_node, pos + 1); + if (left && is_lc_node(left) && lc_len(&left->lc_node) < TBM_STRIDE) + convert_lc_node(btrie, &left->lc_node, pos + 1); + if (right && is_lc_node(right) && lc_len(&right->lc_node) < TBM_STRIDE) + convert_lc_node(btrie, &right->lc_node, pos + 1); /* set internal data for root prefix */ if (root_data_p) { data[ndata++] = *root_data_p; - int_bm |= bit (base_index (0, 0)); + int_bm |= bit(base_index(0, 0)); } /* copy internal data from children */ for (d = 0; d < TBM_STRIDE - 1; d++) { - if (left && has_data (left)) { + if (left && has_data(left)) { for (i = 0; i < 1U << d; i++) { - const void **data_p = tbm_data_p (&left->tbm_node, i, d); + const void **data_p = tbm_data_p(&left->tbm_node, i, d); if (data_p) { data[ndata++] = *data_p; - int_bm |= bit (base_index (i, d + 1)); + int_bm |= bit(base_index(i, d + 1)); } } } - if (right && has_data (right)) { + if (right && has_data(right)) { for (i = 0; i < 1U << d; i++) { - const void **data_p = tbm_data_p (&right->tbm_node, i, d); + const void **data_p = tbm_data_p(&right->tbm_node, i, d); if (data_p) { data[ndata++] = *data_p; - int_bm |= bit (base_index (i + (1 << d), d + 1)); + int_bm |= bit(base_index(i + (1 << d), d + 1)); } } } @@ -1177,32 +1443,32 @@ static void init_tbm_node(struct btrie *btrie, node_t *dst, unsigned pos, if (child == NULL) { continue; } - else if (is_lc_node (child)) { - unsigned pfx = pfx_base + lc_leading_bits (&child->lc_node, pos + 1, - TBM_STRIDE - 1); + else if (is_lc_node(child)) { + unsigned pfx = pfx_base + lc_leading_bits(&child->lc_node, pos + 1, + TBM_STRIDE - 1); /* child is LC node, just shorten it by TBM_STRIDE - 1 */ - shorten_lc_node (btrie, &children[nchildren++], pos + TBM_STRIDE, - &child->lc_node, pos + 1); - ext_bm |= bit (pfx); + shorten_lc_node(btrie, &children[nchildren++], pos + TBM_STRIDE, + &child->lc_node, pos + 1); + ext_bm |= bit(pfx); } - else if (!is_empty_node (child)) { + else if (!is_empty_node(child)) { /* convert deepest internal prefixes of child to extending paths * of the new node */ for (i = 0; i < TBM_FANOUT / 2; i++) { - const void **data_p = tbm_data_p (&child->tbm_node, i, - TBM_STRIDE - 1); - node_t *left_ext = tbm_ext_path (&child->tbm_node, 2 * i); - node_t *right_ext = tbm_ext_path (&child->tbm_node, 2 * i + 1); + const void **data_p = tbm_data_p(&child->tbm_node, i, + TBM_STRIDE - 1); + node_t *left_ext = tbm_ext_path(&child->tbm_node, 2 * i); + node_t *right_ext = tbm_ext_path(&child->tbm_node, 2 * i + 1); if (data_p || left_ext || right_ext) { node_t *ext_path = &children[nchildren++]; unsigned pfx = pfx_base + i; - btrie_oct_t npbyte = next_pbyte (pbyte, pos, pfx); + btrie_oct_t npbyte = next_pbyte(pbyte, pos, pfx); - ext_bm |= bit (pfx); + ext_bm |= bit(pfx); if (left_ext == NULL && right_ext == NULL) { /* only have data - set ext_path to zero-length terminal LC node */ - lc_init_flags (&ext_path->lc_node, 1, 0); + lc_init_flags(&ext_path->lc_node, 1, 0); ext_path->lc_node.prefix[0] = npbyte; ext_path->lc_node.ptr.data = *data_p; btrie->n_lc_nodes++; @@ -1210,33 +1476,33 @@ static void init_tbm_node(struct btrie *btrie, node_t *dst, unsigned pos, else if (data_p || (left_ext && right_ext)) { /* have at least two of data, left_ext, right_ext * ext_path must be a full TBM node */ - init_tbm_node (btrie, ext_path, pos + TBM_STRIDE, - npbyte, data_p, left_ext, right_ext); + init_tbm_node(btrie, ext_path, pos + TBM_STRIDE, + npbyte, data_p, left_ext, right_ext); } else if (left_ext) { /* have only left_ext, insert length-one LC node */ - insert_lc_node (btrie, ext_path, pos + TBM_STRIDE, - npbyte, 0, left_ext); + insert_lc_node(btrie, ext_path, pos + TBM_STRIDE, + npbyte, 0, left_ext); } else { /* have only right_ext, insert length-one LC node */ - insert_lc_node (btrie, ext_path, pos + TBM_STRIDE, - npbyte, 1, right_ext); + insert_lc_node(btrie, ext_path, pos + TBM_STRIDE, + npbyte, 1, right_ext); } } } btrie->n_tbm_nodes--; - free_nodes (btrie, child->tbm_node.ptr.children, - count_bits (child->tbm_node.ext_bm), - count_bits (child->tbm_node.int_bm)); + free_nodes(btrie, child->tbm_node.ptr.children, + count_bits(child->tbm_node.ext_bm), + count_bits(child->tbm_node.int_bm)); } } - assert(count_bits (int_bm) == ndata); - assert(count_bits (ext_bm) == nchildren); + assert(count_bits(int_bm) == ndata); + assert(count_bits(ext_bm) == nchildren); - node->ptr.children = alloc_nodes (btrie, nchildren, ndata); - memcpy(node->ptr.data_end - (int )ndata, data, ndata * sizeof(data[0])); + node->ptr.children = alloc_nodes(btrie, nchildren, ndata); + memcpy(node->ptr.data_end - (int) ndata, data, ndata * sizeof(data[0])); memcpy(node->ptr.children, children, nchildren * sizeof(children[0])); node->ext_bm = ext_bm; node->int_bm = int_bm; @@ -1244,41 +1510,41 @@ static void init_tbm_node(struct btrie *btrie, node_t *dst, unsigned pos, } static enum btrie_result add_to_trie(struct btrie *btrie, node_t *node, - unsigned pos, const btrie_oct_t *prefix, unsigned len, const void *data) + unsigned pos, const btrie_oct_t *prefix, unsigned len, const void *data) { for (;;) { - if (is_lc_node (node)) { + if (is_lc_node(node)) { struct lc_node *lc_node = &node->lc_node; - unsigned end = pos + lc_len (lc_node); - unsigned cbits = common_prefix (prefix + lc_shift (pos), - lc_node->prefix, (len < end ? len : end) - lc_base (pos)); - unsigned clen = lc_base (pos) + cbits; /* position of first mismatch */ + unsigned end = pos + lc_len(lc_node); + unsigned cbits = common_prefix(prefix + lc_shift(pos), + lc_node->prefix, (len < end ? len : end) - lc_base(pos)); + unsigned clen = lc_base(pos) + cbits; /* position of first mismatch */ - if (clen == end && !lc_is_terminal (lc_node)) { + if (clen == end && !lc_is_terminal(lc_node)) { /* matched entire prefix of LC node, proceed to child */ - assert(lc_len (lc_node) > 0); + assert(lc_len(lc_node) > 0); node = lc_node->ptr.child; pos = end; } - else if (clen == end && len == end && lc_is_terminal (lc_node)) { + else if (clen == end && len == end && lc_is_terminal(lc_node)) { /* exact match for terminal node - already have data for prefix */ return BTRIE_DUPLICATE_PREFIX; } else { - assert(clen < end || (lc_is_terminal (lc_node) && len > end)); + assert(clen < end || (lc_is_terminal(lc_node) && len > end)); /* Need to insert new TBM node at clen */ if (clen > pos) { - split_lc_node (btrie, lc_node, pos, clen - pos); + split_lc_node(btrie, lc_node, pos, clen - pos); node = lc_node->ptr.child; - assert(is_lc_node (node)); + assert(is_lc_node(node)); pos = clen; } - convert_lc_node (btrie, &node->lc_node, pos); + convert_lc_node(btrie, &node->lc_node, pos); } } - else if (is_empty_node (node)) { + else if (is_empty_node(node)) { /* at empty TBM node - just replace with terminal LC node */ - init_terminal_node (btrie, node, pos, prefix, len, data); + init_terminal_node(btrie, node, pos, prefix, len, data); btrie->n_entries++; btrie->n_tbm_nodes--; return BTRIE_OKAY; @@ -1289,23 +1555,23 @@ static enum btrie_result add_to_trie(struct btrie *btrie, node_t *node, if (len < end) { unsigned plen = len - pos; - unsigned pfx = extract_bits (prefix, pos, plen); + unsigned pfx = extract_bits(prefix, pos, plen); - if (tbm_data_p (tbm_node, pfx, plen) != NULL) + if (tbm_data_p(tbm_node, pfx, plen) != NULL) return BTRIE_DUPLICATE_PREFIX; /* prefix already has data */ else { - tbm_insert_data (btrie, tbm_node, pfx, plen, data); + tbm_insert_data(btrie, tbm_node, pfx, plen, data); btrie->n_entries++; return BTRIE_OKAY; } } else { - unsigned pfx = extract_bits (prefix, pos, TBM_STRIDE); + unsigned pfx = extract_bits(prefix, pos, TBM_STRIDE); /* follow extending path */ - node = tbm_ext_path (tbm_node, pfx); + node = tbm_ext_path(tbm_node, pfx); if (node == NULL) - node = tbm_insert_ext_path (btrie, tbm_node, pfx); + node = tbm_insert_ext_path(btrie, tbm_node, pfx); pos = end; } } @@ -1314,23 +1580,23 @@ static enum btrie_result add_to_trie(struct btrie *btrie, node_t *node, static const void * search_trie(const node_t *node, unsigned pos, const btrie_oct_t *prefix, - unsigned len) + unsigned len) { /* remember last TBM node seen with internal data */ const struct tbm_node *int_node = 0; unsigned int_pfx = 0, int_plen = 0; while (node) { - if (is_lc_node (node)) { + if (is_lc_node(node)) { const struct lc_node *lc_node = &node->lc_node; - unsigned end = pos + lc_len (lc_node); + unsigned end = pos + lc_len(lc_node); if (len < end) break; - if (!prefixes_equal (prefix + lc_shift (pos), lc_node->prefix, - end - lc_base (pos))) + if (!prefixes_equal(prefix + lc_shift(pos), lc_node->prefix, + end - lc_base(pos))) break; - if (lc_is_terminal (lc_node)) + if (lc_is_terminal(lc_node)) return lc_node->ptr.data; /* found terminal node */ pos = end; @@ -1341,8 +1607,8 @@ search_trie(const node_t *node, unsigned pos, const btrie_oct_t *prefix, unsigned end = pos + TBM_STRIDE; if (len < end) { unsigned plen = len - pos; - unsigned pfx = extract_bits (prefix, pos, plen); - if (has_internal_data (tbm_node, pfx, plen)) { + unsigned pfx = extract_bits(prefix, pos, plen); + if (has_internal_data(tbm_node, pfx, plen)) { int_node = tbm_node; int_pfx = pfx; int_plen = plen; @@ -1350,25 +1616,25 @@ search_trie(const node_t *node, unsigned pos, const btrie_oct_t *prefix, break; } else { - unsigned pfx = extract_bits (prefix, pos, TBM_STRIDE); - if (has_internal_data (tbm_node, pfx >> 1, TBM_STRIDE - 1)) { + unsigned pfx = extract_bits(prefix, pos, TBM_STRIDE); + if (has_internal_data(tbm_node, pfx >> 1, TBM_STRIDE - 1)) { int_node = tbm_node; int_pfx = pfx >> 1; int_plen = TBM_STRIDE - 1; } pos = end; - node = tbm_ext_path (tbm_node, pfx); + node = tbm_ext_path(tbm_node, pfx); } } } if (int_node) { - const void **data_p = tbm_data_p (int_node, int_pfx, int_plen); + const void **data_p = tbm_data_p(int_node, int_pfx, int_plen); while (data_p == NULL) { assert(int_plen > 0); int_pfx >>= 1; int_plen--; - data_p = tbm_data_p (int_node, int_pfx, int_plen); + data_p = tbm_data_p(int_node, int_pfx, int_plen); } return *data_p; } @@ -1381,7 +1647,7 @@ btrie_init(rspamd_mempool_t *mp) { struct btrie *btrie; - if (!(btrie = rspamd_mempool_alloc0 (mp, sizeof(*btrie)))) { + if (!(btrie = rspamd_mempool_alloc0(mp, sizeof(*btrie)))) { return NULL; } @@ -1395,19 +1661,19 @@ btrie_init(rspamd_mempool_t *mp) } enum btrie_result btrie_add_prefix(struct btrie *btrie, - const btrie_oct_t *prefix, unsigned len, const void *data) + const btrie_oct_t *prefix, unsigned len, const void *data) { enum btrie_result rv; - if ((rv = setjmp (btrie->exception)) != 0) + if ((rv = setjmp(btrie->exception)) != 0) return rv; /* out of memory */ - return add_to_trie (btrie, &btrie->root, 0, prefix, len, data); + return add_to_trie(btrie, &btrie->root, 0, prefix, len, data); } const void * btrie_lookup(const struct btrie *btrie, const btrie_oct_t *prefix, unsigned len) { - return search_trie (&btrie->root, 0, prefix, len); + return search_trie(&btrie->root, 0, prefix, len); } /**************************************************************** @@ -1438,7 +1704,7 @@ static void node_stats(const node_t *node, size_t depth, struct stats *stats) { if (depth > stats->max_depth) - stats->max_depth = depth; + stats->max_depth = depth; stats->total_depth += depth; if (is_lc_node(node)) { @@ -1446,10 +1712,10 @@ node_stats(const node_t *node, size_t depth, struct stats *stats) stats->n_lc_nodes++; #endif if (!lc_is_terminal(&node->lc_node)) - node_stats(node->lc_node.ptr.child, depth + 1, stats); + node_stats(node->lc_node.ptr.child, depth + 1, stats); #ifndef NDEBUG else - stats->n_entries++; + stats->n_entries++; #endif } else { @@ -1464,7 +1730,7 @@ node_stats(const node_t *node, size_t depth, struct stats *stats) stats->alloc_waste += (ndata % 2) * sizeof(void *); #endif for (i = 0; i < nchildren; i++) - node_stats(&node->tbm_node.ptr.children[i], depth + 1, stats); + node_stats(&node->tbm_node.ptr.children[i], depth + 1, stats); } } #endif /* BTRIE_EXTENDED_STATS */ @@ -1486,20 +1752,19 @@ static size_t count_free(const struct btrie *btrie) #endif /* not NDEBUG */ const char * -btrie_stats(const struct btrie *btrie, guint duplicates) +btrie_stats(const struct btrie *btrie, unsigned int duplicates) { static char buf[128]; size_t n_nodes = btrie->n_lc_nodes + btrie->n_tbm_nodes; size_t alloc_free = (btrie->alloc_total + sizeof(node_t) /* do not double-count the root node */ - - n_nodes * sizeof(node_t) - btrie->alloc_data - btrie->alloc_waste - - sizeof(*btrie)); + - n_nodes * sizeof(node_t) - btrie->alloc_data - btrie->alloc_waste - sizeof(*btrie)); #ifdef BTRIE_EXTENDED_STATS struct stats stats; double average_depth; memset(&stats, 0, sizeof(stats)); node_stats(&btrie->root, 0, &stats); - average_depth = (double)stats.total_depth / n_nodes; + average_depth = (double) stats.total_depth / n_nodes; #ifndef NDEBUG /* check the node counts */ @@ -1513,7 +1778,7 @@ btrie_stats(const struct btrie *btrie, guint duplicates) #ifndef NDEBUG /* check that we haven't lost any memory */ - assert(alloc_free == count_free (btrie)); + assert(alloc_free == count_free(btrie)); #endif #ifdef BTRIE_DEBUG_ALLOC @@ -1523,21 +1788,19 @@ btrie_stats(const struct btrie *btrie, guint duplicates) #ifdef BTRIE_EXTENDED_STATS snprintf(buf, sizeof(buf), - "ents=%lu tbm=%lu lc=%lu mem=%.0fk free=%lu waste=%lu" - " depth=%.1f/%lu" - ,(long unsigned)btrie->n_entries, (long unsigned)btrie->n_tbm_nodes, - (long unsigned)btrie->n_lc_nodes, (double)btrie->alloc_total / 1024, - (long unsigned)alloc_free, (long unsigned)btrie->alloc_waste - , average_depth, (long unsigned)stats.max_depth); + "ents=%lu tbm=%lu lc=%lu mem=%.0fk free=%lu waste=%lu" + " depth=%.1f/%lu", + (long unsigned) btrie->n_entries, (long unsigned) btrie->n_tbm_nodes, + (long unsigned) btrie->n_lc_nodes, (double) btrie->alloc_total / 1024, + (long unsigned) alloc_free, (long unsigned) btrie->alloc_waste, average_depth, (long unsigned) stats.max_depth); #else snprintf(buf, sizeof(buf), - "ents=%lu dup=%u tbm=%lu lc=%lu mem=%.0fk free=%lu waste=%lu", - (long unsigned)btrie->n_entries, - duplicates, - (long unsigned)btrie->n_tbm_nodes, - (long unsigned)btrie->n_lc_nodes, (double)btrie->alloc_total / 1024, - (long unsigned)alloc_free, (long unsigned)btrie->alloc_waste - ); + "ents=%lu dup=%u tbm=%lu lc=%lu mem=%.0fk free=%lu waste=%lu", + (long unsigned) btrie->n_entries, + duplicates, + (long unsigned) btrie->n_tbm_nodes, + (long unsigned) btrie->n_lc_nodes, (double) btrie->alloc_total / 1024, + (long unsigned) alloc_free, (long unsigned) btrie->alloc_waste); #endif buf[sizeof(buf) - 1] = '\0'; return buf; @@ -1547,8 +1810,7 @@ btrie_stats(const struct btrie *btrie, guint duplicates) #ifndef NO_MASTER_DUMP -struct walk_context -{ +struct walk_context { btrie_walk_cb_t *callback; void *user_data; @@ -1559,12 +1821,12 @@ static void walk_node(const node_t *node, unsigned pos, struct walk_context *ctx); static void walk_tbm_node(const struct tbm_node *node, unsigned pos, - unsigned pfx, unsigned plen, struct walk_context *ctx) + unsigned pfx, unsigned plen, struct walk_context *ctx) { btrie_oct_t *prefix = ctx->prefix; int pbyte = pos / 8; btrie_oct_t pbit = 0x80 >> (pos % 8); - const void **data_p = tbm_data_p (node, pfx, plen); + const void **data_p = tbm_data_p(node, pfx, plen); if (pos >= BTRIE_MAX_PREFIX) { /* This can/should not happen, but don't overwrite buffers if it does. */ @@ -1572,38 +1834,38 @@ static void walk_tbm_node(const struct tbm_node *node, unsigned pos, } if (data_p) - ctx->callback (prefix, pos, *data_p, 0, ctx->user_data); + ctx->callback(prefix, pos, *data_p, 0, ctx->user_data); /* walk children */ if (plen < TBM_STRIDE - 1) { /* children are internal prefixes in same node */ - walk_tbm_node (node, pos + 1, pfx << 1, plen + 1, ctx); + walk_tbm_node(node, pos + 1, pfx << 1, plen + 1, ctx); prefix[pbyte] |= pbit; - walk_tbm_node (node, pos + 1, (pfx << 1) + 1, plen + 1, ctx); + walk_tbm_node(node, pos + 1, (pfx << 1) + 1, plen + 1, ctx); prefix[pbyte] &= ~pbit; } else { /* children are extending paths */ const node_t *ext_path; - if ((ext_path = tbm_ext_path (node, pfx << 1)) != NULL) - walk_node (ext_path, pos + 1, ctx); - if ((ext_path = tbm_ext_path (node, (pfx << 1) + 1)) != NULL) { + if ((ext_path = tbm_ext_path(node, pfx << 1)) != NULL) + walk_node(ext_path, pos + 1, ctx); + if ((ext_path = tbm_ext_path(node, (pfx << 1) + 1)) != NULL) { prefix[pbyte] |= pbit; - walk_node (ext_path, pos + 1, ctx); + walk_node(ext_path, pos + 1, ctx); prefix[pbyte] &= ~pbit; } } if (data_p) - ctx->callback (prefix, pos, *data_p, 1, ctx->user_data); + ctx->callback(prefix, pos, *data_p, 1, ctx->user_data); } static void walk_lc_node(const struct lc_node *node, unsigned pos, - struct walk_context *ctx) + struct walk_context *ctx) { btrie_oct_t *prefix = ctx->prefix; - unsigned end = pos + lc_len (node); - btrie_oct_t save_prefix = prefix[lc_shift (pos)]; + unsigned end = pos + lc_len(node); + btrie_oct_t save_prefix = prefix[lc_shift(pos)]; if (end > BTRIE_MAX_PREFIX) { /* This can/should not happen, but don't overwrite buffers if it does. */ @@ -1611,29 +1873,29 @@ static void walk_lc_node(const struct lc_node *node, unsigned pos, } /* construct full prefix to node */ - memcpy(&prefix[lc_shift (pos)], node->prefix, lc_bytes (node, pos)); + memcpy(&prefix[lc_shift(pos)], node->prefix, lc_bytes(node, pos)); if (end % 8) - prefix[end / 8] &= high_bits (end % 8); + prefix[end / 8] &= high_bits(end % 8); - if (lc_is_terminal (node)) { - ctx->callback (prefix, end, node->ptr.data, 0, ctx->user_data); - ctx->callback (prefix, end, node->ptr.data, 1, ctx->user_data); + if (lc_is_terminal(node)) { + ctx->callback(prefix, end, node->ptr.data, 0, ctx->user_data); + ctx->callback(prefix, end, node->ptr.data, 1, ctx->user_data); } else - walk_node (node->ptr.child, end, ctx); + walk_node(node->ptr.child, end, ctx); - prefix[lc_shift (pos)] = save_prefix; /* restore parents prefix */ - if (lc_bytes (node, pos) > 1) - memset(&prefix[lc_shift (pos) + 1], 0, lc_bytes (node, pos) - 1); + prefix[lc_shift(pos)] = save_prefix; /* restore parents prefix */ + if (lc_bytes(node, pos) > 1) + memset(&prefix[lc_shift(pos) + 1], 0, lc_bytes(node, pos) - 1); } static void walk_node(const node_t *node, unsigned pos, - struct walk_context *ctx) + struct walk_context *ctx) { - if (is_lc_node (node)) - walk_lc_node (&node->lc_node, pos, ctx); + if (is_lc_node(node)) + walk_lc_node(&node->lc_node, pos, ctx); else - walk_tbm_node (&node->tbm_node, pos, 0, 0, ctx); + walk_tbm_node(&node->tbm_node, pos, 0, 0, ctx); } /* walk trie in lexicographical order @@ -1641,7 +1903,7 @@ static void walk_node(const node_t *node, unsigned pos, * calls callback twice (once preorder, once postorder) at each prefix */ void btrie_walk(const struct btrie *btrie, btrie_walk_cb_t *callback, - void *user_data) + void *user_data) { struct walk_context ctx; @@ -1649,7 +1911,7 @@ void btrie_walk(const struct btrie *btrie, btrie_walk_cb_t *callback, ctx.callback = callback; ctx.user_data = user_data; - walk_node (&btrie->root, 0, &ctx); + walk_node(&btrie->root, 0, &ctx); } #endif /* not NO_MASTER_DUMP */ @@ -1664,7 +1926,7 @@ void btrie_walk(const struct btrie *btrie, btrie_walk_cb_t *callback, #include <stdio.h> #ifndef UNUSED -# define UNUSED __attribute__((unused)) +#define UNUSED __attribute__((unused)) #endif /* bogus replacements mp_alloc for running self-tests */ @@ -1675,12 +1937,14 @@ mp_alloc(UNUSED struct mempool *mp, unsigned sz, UNUSED int align) } #if 0 -# define PASS(name) puts("OK " name) +#define PASS(name) puts("OK " name) #else -# define PASS(name) fputs(".", stdout); fflush(stdout) +#define PASS(name) \ + fputs(".", stdout); \ + fflush(stdout) #endif -const char * pgm_name = "???"; +const char *pgm_name = "???"; static void test_struct_node_packing() @@ -1705,7 +1969,7 @@ test_struct_node_packing() static void test_bit() { - tbm_bitmap_t ones = ~(tbm_bitmap_t)0; + tbm_bitmap_t ones = ~(tbm_bitmap_t) 0; tbm_bitmap_t high_bit = ones ^ (ones >> 1); assert(bit(0) == high_bit); @@ -1718,7 +1982,7 @@ static void test_count_bits() { unsigned max_bits = sizeof(tbm_bitmap_t) * 8; - tbm_bitmap_t ones = ~(tbm_bitmap_t)0; + tbm_bitmap_t ones = ~(tbm_bitmap_t) 0; assert(count_bits(0) == 0); assert(count_bits(1) == 1); @@ -1743,7 +2007,7 @@ static void test_count_bits_before() { unsigned max_bits = sizeof(tbm_bitmap_t) * 8; - tbm_bitmap_t ones = ~(tbm_bitmap_t)0; + tbm_bitmap_t ones = ~(tbm_bitmap_t) 0; unsigned i; for (i = 0; i < max_bits; i++) { @@ -1758,7 +2022,7 @@ static void test_count_bits_from() { unsigned max_bits = sizeof(tbm_bitmap_t) * 8; - tbm_bitmap_t ones = ~(tbm_bitmap_t)0; + tbm_bitmap_t ones = ~(tbm_bitmap_t) 0; unsigned i; for (i = 0; i < max_bits; i++) { @@ -1776,16 +2040,16 @@ test_extract_bits() unsigned i; for (i = 0; i < 32; i++) - assert(extract_bits(prefix, i, 0) == 0); + assert(extract_bits(prefix, i, 0) == 0); for (i = 0; i < 8; i++) - assert(extract_bits(prefix, i, 1) == 1); + assert(extract_bits(prefix, i, 1) == 1); for (i = 8; i < 16; i++) - assert(extract_bits(prefix, i, 1) == i % 2); + assert(extract_bits(prefix, i, 1) == i % 2); for (i = 16; i < 24; i++) - assert(extract_bits(prefix, i, 1) == (i + 1) % 2); + assert(extract_bits(prefix, i, 1) == (i + 1) % 2); for (i = 24; i < 32; i++) - assert(extract_bits(prefix, i, 1) == 0); + assert(extract_bits(prefix, i, 1) == 0); assert(extract_bits(prefix, 2, 6) == 0x3f); assert(extract_bits(prefix, 3, 6) == 0x3e); @@ -1828,7 +2092,7 @@ test_prefixes_equal() assert(!prefixes_equal(prefix1, prefix2, 8 * LC_BYTES_PER_NODE)); assert(prefixes_equal(prefix1, prefix2, i)); if (i + 1 < 8 * LC_BYTES_PER_NODE) - assert(!prefixes_equal(prefix1, prefix2, i + 1)); + assert(!prefixes_equal(prefix1, prefix2, i + 1)); prefix1[i / 8] ^= 1 << (7 - i % 8); } PASS("test_prefixes_equal"); @@ -1848,7 +2112,7 @@ test_common_prefix() prefix1[i / 8] ^= 1 << (7 - i % 8); assert(common_prefix(prefix1, prefix2, 8 * LC_BYTES_PER_NODE) == i); if (i + 1 < 8 * LC_BYTES_PER_NODE) - assert(common_prefix(prefix1, prefix2, i+1) == i); + assert(common_prefix(prefix1, prefix2, i + 1) == i); prefix1[i / 8] ^= 1 << (7 - i % 8); } PASS("test_common_prefix"); @@ -1857,13 +2121,13 @@ test_common_prefix() static void test_base_index() { - assert(base_index(0,0) == 1); - assert(base_index(0,1) == 2); - assert(base_index(1,1) == 3); - assert(base_index(0,2) == 4); - assert(base_index(1,2) == 5); - assert(base_index(2,2) == 6); - assert(base_index(3,2) == 7); + assert(base_index(0, 0) == 1); + assert(base_index(0, 1) == 2); + assert(base_index(1, 1) == 3); + assert(base_index(0, 2) == 4); + assert(base_index(1, 2) == 5); + assert(base_index(2, 2) == 6); + assert(base_index(3, 2) == 7); PASS("test_base_index"); } @@ -1889,20 +2153,76 @@ test_has_internal_data() /****************************************************************/ static const btrie_oct_t numbered_bytes[] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, - 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, - 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, - 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x00, + 0x01, + 0x02, + 0x03, + 0x04, + 0x05, + 0x06, + 0x07, + 0x08, + 0x09, + 0x0a, + 0x0b, + 0x0c, + 0x0d, + 0x0e, + 0x0f, + 0x10, + 0x11, + 0x12, + 0x13, + 0x14, + 0x15, + 0x16, + 0x17, + 0x18, + 0x19, + 0x1a, + 0x1b, + 0x1c, + 0x1d, + 0x1e, + 0x1f, + 0x20, + 0x21, + 0x22, + 0x23, + 0x24, + 0x25, + 0x26, + 0x27, + 0x28, + 0x29, + 0x2a, + 0x2b, + 0x2c, + 0x2d, + 0x2e, + 0x2f, + 0x30, + 0x31, + 0x32, + 0x33, + 0x34, + 0x35, + 0x36, + 0x37, + 0x38, + 0x39, + 0x3a, + 0x3b, + 0x3c, + 0x3d, + 0x3e, + 0x3f, }; static void check_non_terminal_lc_node(struct lc_node *node, unsigned len) { - assert(is_lc_node((node_t *)node)); + assert(is_lc_node((node_t *) node)); assert(!lc_is_terminal(node)); assert(lc_len(node) == len); } @@ -1910,7 +2230,7 @@ check_non_terminal_lc_node(struct lc_node *node, unsigned len) static void check_terminal_lc_node(struct lc_node *node, unsigned len, const void *data) { - assert(is_lc_node((node_t *)node)); + assert(is_lc_node((node_t *) node)); assert(lc_is_terminal(node)); assert(lc_len(node) == len); assert(node->ptr.data == data); @@ -1920,33 +2240,33 @@ static void test_init_terminal_node() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; node_t node; struct lc_node *head = &node.lc_node; init_terminal_node(btrie, &node, 0, - numbered_bytes, 8 * LC_BYTES_PER_NODE, data); + numbered_bytes, 8 * LC_BYTES_PER_NODE, data); check_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE, data); assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE) == 0); init_terminal_node(btrie, &node, 7, - numbered_bytes, 8 * LC_BYTES_PER_NODE, data); + numbered_bytes, 8 * LC_BYTES_PER_NODE, data); check_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE - 7, data); assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE) == 0); init_terminal_node(btrie, &node, 0, - numbered_bytes, 2 * 8 * LC_BYTES_PER_NODE, data); + numbered_bytes, 2 * 8 * LC_BYTES_PER_NODE, data); check_non_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE); assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE) == 0); { struct lc_node *child = &head->ptr.child->lc_node; check_terminal_lc_node(child, 8 * LC_BYTES_PER_NODE, data); assert(memcmp(child->prefix, &numbered_bytes[LC_BYTES_PER_NODE], - LC_BYTES_PER_NODE) == 0); + LC_BYTES_PER_NODE) == 0); } init_terminal_node(btrie, &node, 15, - numbered_bytes, 8 * LC_BYTES_PER_NODE + 15, data); + numbered_bytes, 8 * LC_BYTES_PER_NODE + 15, data); check_non_terminal_lc_node(head, 8 * LC_BYTES_PER_NODE - 7); assert(memcmp(head->prefix, &numbered_bytes[1], LC_BYTES_PER_NODE) == 0); { @@ -1962,35 +2282,33 @@ static void test_coalesce_lc_node() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; node_t node; struct lc_node *head = &node.lc_node; /* test merging */ init_terminal_node(btrie, &node, 0, - numbered_bytes, 8 * (LC_BYTES_PER_NODE + 1), data); + numbered_bytes, 8 * (LC_BYTES_PER_NODE + 1), data); check_non_terminal_lc_node(head, LC_BYTES_PER_NODE * 8); lc_add_to_len(head, -8); coalesce_lc_node(btrie, head, 8); check_terminal_lc_node(head, LC_BYTES_PER_NODE * 8, data); - assert(head->prefix[LC_BYTES_PER_NODE - 1] - == numbered_bytes[LC_BYTES_PER_NODE]); + assert(head->prefix[LC_BYTES_PER_NODE - 1] == numbered_bytes[LC_BYTES_PER_NODE]); /* test bit stealing */ init_terminal_node(btrie, &node, 0, - numbered_bytes, 8 * (2 * LC_BYTES_PER_NODE), data); + numbered_bytes, 8 * (2 * LC_BYTES_PER_NODE), data); check_non_terminal_lc_node(head, LC_BYTES_PER_NODE * 8); lc_add_to_len(head, -15); coalesce_lc_node(btrie, head, 15); check_non_terminal_lc_node(head, LC_BYTES_PER_NODE * 8 - 7); assert(memcmp(head->prefix, numbered_bytes, LC_BYTES_PER_NODE - 1) == 0); - assert(head->prefix[LC_BYTES_PER_NODE - 1] - == numbered_bytes[LC_BYTES_PER_NODE]); + assert(head->prefix[LC_BYTES_PER_NODE - 1] == numbered_bytes[LC_BYTES_PER_NODE]); { struct lc_node *child = &head->ptr.child->lc_node; check_terminal_lc_node(child, 8 * (LC_BYTES_PER_NODE - 1), data); assert(memcmp(child->prefix, &numbered_bytes[LC_BYTES_PER_NODE + 1], - LC_BYTES_PER_NODE - 1) == 0); + LC_BYTES_PER_NODE - 1) == 0); } PASS("test_coalesce_lc_node"); @@ -2000,26 +2318,25 @@ static void test_shorten_lc_node() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; node_t node, shorter; /* test shorten without shift */ init_terminal_node(btrie, &node, 0, - numbered_bytes, 8 * LC_BYTES_PER_NODE, data); + numbered_bytes, 8 * LC_BYTES_PER_NODE, data); memset(shorter.lc_node.prefix, 0xff, LC_BYTES_PER_NODE); shorten_lc_node(btrie, &shorter, 7, &node.lc_node, 0); check_terminal_lc_node(&shorter.lc_node, LC_BYTES_PER_NODE * 8 - 7, data); - assert(memcmp(shorter.lc_node.prefix, numbered_bytes, LC_BYTES_PER_NODE) - == 0); + assert(memcmp(shorter.lc_node.prefix, numbered_bytes, LC_BYTES_PER_NODE) == 0); /* test shorten with shift */ init_terminal_node(btrie, &node, 7, - numbered_bytes, 8 * LC_BYTES_PER_NODE, data); + numbered_bytes, 8 * LC_BYTES_PER_NODE, data); memset(shorter.lc_node.prefix, 0xff, LC_BYTES_PER_NODE); shorten_lc_node(btrie, &shorter, 9, &node.lc_node, 7); check_terminal_lc_node(&shorter.lc_node, LC_BYTES_PER_NODE * 8 - 9, data); assert(memcmp(shorter.lc_node.prefix, &numbered_bytes[1], - LC_BYTES_PER_NODE - 1) == 0); + LC_BYTES_PER_NODE - 1) == 0); { /* test child stealing */ @@ -2041,16 +2358,16 @@ static void test_split_lc_node() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; struct lc_node node; - init_terminal_node(btrie, (node_t *)&node, 1, numbered_bytes, 25, data); + init_terminal_node(btrie, (node_t *) &node, 1, numbered_bytes, 25, data); split_lc_node(btrie, &node, 1, 8); check_non_terminal_lc_node(&node, 8); check_terminal_lc_node(&node.ptr.child->lc_node, 16, data); /* test conversion of terminal to non-terminal */ - init_terminal_node(btrie, (node_t *)&node, 7, numbered_bytes, 10, data); + init_terminal_node(btrie, (node_t *) &node, 7, numbered_bytes, 10, data); split_lc_node(btrie, &node, 7, 3); check_non_terminal_lc_node(&node, 3); check_terminal_lc_node(&node.ptr.child->lc_node, 0, data); @@ -2062,7 +2379,7 @@ static void test_convert_lc_node_1() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; struct lc_node head; /* test tail is left */ @@ -2072,7 +2389,7 @@ test_convert_lc_node_1() init_terminal_node(btrie, head.ptr.child, 1, numbered_bytes, 1, data); convert_lc_node_1(btrie, &head, 0); { - node_t *result = (node_t *)&head; + node_t *result = (node_t *) &head; assert(is_tbm_node(result)); assert(result->tbm_node.ext_bm == 0); assert(result->tbm_node.int_bm == bit(base_index(0, 1))); @@ -2086,7 +2403,7 @@ test_convert_lc_node_1() init_terminal_node(btrie, head.ptr.child, 8, numbered_bytes, 10, data); convert_lc_node_1(btrie, &head, 7); { - node_t *result = (node_t *)&head; + node_t *result = (node_t *) &head; assert(is_tbm_node(result)); assert(result->tbm_node.ext_bm == 0); assert(result->tbm_node.int_bm == bit(base_index(4, 3))); @@ -2100,7 +2417,7 @@ static void test_convert_lc_node() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; node_t node; /* if (len >= TBM_STRIDE) */ @@ -2139,7 +2456,7 @@ static void test_insert_lc_node() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; node_t node, tail; /* test optimized case, last_bit == 0 */ @@ -2187,7 +2504,7 @@ static void test_init_tbm_node() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; unsigned lr; node_t node; @@ -2212,35 +2529,35 @@ test_init_tbm_node() assert(node.tbm_node.ext_bm == bit(base)); assert(node.tbm_node.int_bm == 0); check_terminal_lc_node(&tbm_ext_path(&node.tbm_node, base)->lc_node, - 1, data); + 1, data); /* test with short LC node children */ init_terminal_node(btrie, &child, 1, numbered_bytes, TBM_STRIDE - 1, data); init_tbm_node(btrie, &node, 0, 0, NULL, left, right); assert(is_tbm_node(&node)); assert(node.tbm_node.ext_bm == 0); - assert(node.tbm_node.int_bm == bit(base_index(base >> 1, TBM_STRIDE-1))); - assert(*tbm_data_p(&node.tbm_node, base >> 1, TBM_STRIDE-1) == data); + assert(node.tbm_node.int_bm == bit(base_index(base >> 1, TBM_STRIDE - 1))); + assert(*tbm_data_p(&node.tbm_node, base >> 1, TBM_STRIDE - 1) == data); /* construct TBM node with all eight combinations of having data, * left_ext and/or right_ext in its extending paths */ init_empty_node(btrie, &child); for (pfx = 0; pfx < 8; pfx++) { if (pfx & 1) - tbm_insert_data(btrie, &child.tbm_node, pfx, TBM_STRIDE - 1, data); + tbm_insert_data(btrie, &child.tbm_node, pfx, TBM_STRIDE - 1, data); if (pfx & 2) { btrie_oct_t prefix0 = 0; init_terminal_node(btrie, - tbm_insert_ext_path(btrie, &child.tbm_node, 2*pfx), - TBM_STRIDE + 1, - &prefix0, TBM_STRIDE + 2, data); + tbm_insert_ext_path(btrie, &child.tbm_node, 2 * pfx), + TBM_STRIDE + 1, + &prefix0, TBM_STRIDE + 2, data); } if (pfx & 4) { btrie_oct_t prefix0 = 0x80 >> TBM_STRIDE; init_terminal_node(btrie, - tbm_insert_ext_path(btrie, &child.tbm_node, 2*pfx+1), - TBM_STRIDE + 1, - &prefix0, TBM_STRIDE + 3, data); + tbm_insert_ext_path(btrie, &child.tbm_node, 2 * pfx + 1), + TBM_STRIDE + 1, + &prefix0, TBM_STRIDE + 3, data); } } init_tbm_node(btrie, &node, 0, 0, NULL, left, right); @@ -2248,9 +2565,9 @@ test_init_tbm_node() unsigned base = lr ? (1U << (TBM_STRIDE - 1)) : 0; node_t *ext_path = tbm_ext_path(&node.tbm_node, base + pfx); if (pfx == 0) - assert(ext_path == NULL); + assert(ext_path == NULL); else if (pfx == 1) - check_terminal_lc_node(&ext_path->lc_node, 0, data); + check_terminal_lc_node(&ext_path->lc_node, 0, data); else if (pfx == 2) { check_terminal_lc_node(&ext_path->lc_node, 2, data); assert(ext_path->lc_node.prefix[0] == 0); @@ -2286,7 +2603,7 @@ static void test_add_to_trie() { struct btrie *btrie = btrie_init(NULL); - const void *data = (void *)0xdeadbeef; + const void *data = (void *) 0xdeadbeef; enum btrie_result result; unsigned pfx, plen; node_t root; @@ -2294,20 +2611,20 @@ test_add_to_trie() /* test initial insertion */ init_empty_node(btrie, &root); result = add_to_trie(btrie, &root, 0, - numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data); + numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data); assert(result == BTRIE_OKAY); check_non_terminal_lc_node(&root.lc_node, 8 * LC_BYTES_PER_NODE); check_terminal_lc_node(&root.lc_node.ptr.child->lc_node, - 8 * LC_BYTES_PER_NODE, data); + 8 * LC_BYTES_PER_NODE, data); /* test can follow LC node to tail, and then detect duplicate prefix */ result = add_to_trie(btrie, &root, 0, - numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data); + numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data); assert(result == BTRIE_DUPLICATE_PREFIX); /* test can insert new TBM node within existing LC node */ result = add_to_trie(btrie, &root, 0, - &numbered_bytes[1], 16, data); + &numbered_bytes[1], 16, data); assert(result == BTRIE_OKAY); check_non_terminal_lc_node(&root.lc_node, 7); assert(is_tbm_node(root.lc_node.ptr.child)); @@ -2325,8 +2642,8 @@ test_add_to_trie() btrie_oct_t prefix0 = plen ? pfx << (8 - plen) : 0; init_empty_node(btrie, &root); init_terminal_node(btrie, tbm_insert_ext_path(btrie, &root.tbm_node, 0), - TBM_STRIDE, - numbered_bytes, 8, data); + TBM_STRIDE, + numbered_bytes, 8, data); result = add_to_trie(btrie, &root, 0, &prefix0, plen, data); assert(result == BTRIE_OKAY); assert(is_tbm_node(&root)); @@ -2350,7 +2667,7 @@ test_add_to_trie() assert(root.tbm_node.ext_bm == bit(pfx)); assert(root.tbm_node.int_bm == bit(base_index(0, 0))); check_terminal_lc_node(&tbm_ext_path(&root.tbm_node, pfx)->lc_node, - 8 - TBM_STRIDE, data); + 8 - TBM_STRIDE, data); result = add_to_trie(btrie, &root, 0, &prefix0, 8, data); assert(result == BTRIE_DUPLICATE_PREFIX); @@ -2359,14 +2676,14 @@ test_add_to_trie() /* test can follow extending path */ init_empty_node(btrie, &root); init_terminal_node(btrie, - tbm_insert_ext_path(btrie, &root.tbm_node, 0), TBM_STRIDE, - numbered_bytes, 8, data); + tbm_insert_ext_path(btrie, &root.tbm_node, 0), TBM_STRIDE, + numbered_bytes, 8, data); result = add_to_trie(btrie, &root, 0, numbered_bytes, 7, data); assert(result == BTRIE_OKAY); assert(root.tbm_node.ext_bm == bit(0)); assert(root.tbm_node.int_bm == 0); check_non_terminal_lc_node(&root.tbm_node.ptr.children[0].lc_node, - 7 - TBM_STRIDE); + 7 - TBM_STRIDE); PASS("test_add_to_trie"); } @@ -2375,25 +2692,21 @@ static void test_search_trie() { struct btrie *btrie = btrie_init(NULL); - const void *data01 = (void *)0xdead0001; - const void *data11 = (void *)0xdead0101; - const void *data = (void *)0xdeadbeef; + const void *data01 = (void *) 0xdead0001; + const void *data11 = (void *) 0xdead0101; + const void *data = (void *) 0xdeadbeef; unsigned plen, pfx; node_t root; /* test can follow chain of LC nodes to an exact match */ init_empty_node(btrie, &root); add_to_trie(btrie, &root, 0, - numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data); + numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE, data); - assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE) - == data); - assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE + 1) - == data); - assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE - 1) - == NULL); - assert(search_trie(&root, 0, &numbered_bytes[1], 8 * 2 * LC_BYTES_PER_NODE) - == NULL); + assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE) == data); + assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE + 1) == data); + assert(search_trie(&root, 0, numbered_bytes, 8 * 2 * LC_BYTES_PER_NODE - 1) == NULL); + assert(search_trie(&root, 0, &numbered_bytes[1], 8 * 2 * LC_BYTES_PER_NODE) == NULL); /* test can follow extending path to an exact match */ for (pfx = 0; pfx < (1U << TBM_STRIDE); pfx++) { @@ -2405,14 +2718,14 @@ test_search_trie() assert(search_trie(&root, 0, &prefix0, 8) == data); /* test that last matching TBM internal prefix gets picked up */ if (prefix0 & 0x80) - assert(search_trie(&root, 0, &prefix0, 7) == data11); + assert(search_trie(&root, 0, &prefix0, 7) == data11); else - assert(search_trie(&root, 0, &prefix0, 7) == data01); + assert(search_trie(&root, 0, &prefix0, 7) == data01); prefix0 ^= 1 << (8 - TBM_STRIDE); if (prefix0 & 0x80) - assert(search_trie(&root, 0, &prefix0, 8) == data11); + assert(search_trie(&root, 0, &prefix0, 8) == data11); else - assert(search_trie(&root, 0, &prefix0, 8) == data01); + assert(search_trie(&root, 0, &prefix0, 8) == data01); } /* test finding of TBM internal prefixes */ @@ -2425,9 +2738,9 @@ test_search_trie() for (pfx = 0; pfx < (1U << TBM_STRIDE); pfx++) { btrie_oct_t prefix0 = pfx << (8 - plen); if (prefix0 & 0x80) - assert(search_trie(&root, 0, &prefix0, plen) == data11); + assert(search_trie(&root, 0, &prefix0, plen) == data11); else - assert(search_trie(&root, 0, &prefix0, plen) == data01); + assert(search_trie(&root, 0, &prefix0, plen) == data01); } } @@ -2473,7 +2786,7 @@ unit_tests() #define INDENT_FILL "....:....|....:....|....:....|....:....|" static void dump_node(const node_t *node, unsigned pos, btrie_oct_t *prefix, - int indent); + int indent); static void dump_prefix(btrie_oct_t *prefix, unsigned len, int indent, const char *tail) @@ -2482,9 +2795,9 @@ dump_prefix(btrie_oct_t *prefix, unsigned len, int indent, const char *tail) printf("%*.*s0x", indent, indent, INDENT_FILL); for (i = 0; i < len / 8; i++) - printf("%02x", prefix[i]); + printf("%02x", prefix[i]); if (len % 8) - printf("%02x", prefix[len / 8] & high_bits(len % 8)); + printf("%02x", prefix[len / 8] & high_bits(len % 8)); printf("/%u%s", len, tail); } @@ -2498,13 +2811,13 @@ insert_bits(btrie_oct_t *prefix, unsigned pos, btrie_oct_t pfx, unsigned nbits) unsigned shift = 16 - (pos % 8) - nbits; v = (v & ~(mask << shift)) | (pfx << shift); prefix[pos / 8] = v >> 8; - prefix[pos / 8 + 1] = (btrie_oct_t)v; + prefix[pos / 8 + 1] = (btrie_oct_t) v; } } static void dump_tbm_node(const struct tbm_node *node, unsigned pos, - btrie_oct_t *prefix, int indent) + btrie_oct_t *prefix, int indent) { unsigned pfx = 0, plen = 0; @@ -2516,7 +2829,7 @@ dump_tbm_node(const struct tbm_node *node, unsigned pos, if (data_p) { insert_bits(prefix, pos, pfx, plen); dump_prefix(prefix, pos + plen, indent, ""); - printf(" [%u/%u] (%s)\n", pfx, plen, (const char *)*data_p); + printf(" [%u/%u] (%s)\n", pfx, plen, (const char *) *data_p); } plen++; pfx <<= 1; @@ -2529,7 +2842,7 @@ dump_tbm_node(const struct tbm_node *node, unsigned pos, } while (pfx & 1) { if (--plen == 0) - return; + return; pfx >>= 1; } pfx++; @@ -2539,7 +2852,7 @@ dump_tbm_node(const struct tbm_node *node, unsigned pos, static void dump_lc_node(const struct lc_node *node, unsigned pos, - btrie_oct_t *prefix, int indent) + btrie_oct_t *prefix, int indent) { unsigned end = pos + lc_len(node); btrie_oct_t save_prefix = prefix[lc_shift(pos)]; @@ -2548,7 +2861,7 @@ dump_lc_node(const struct lc_node *node, unsigned pos, if (lc_is_terminal(node)) { dump_prefix(prefix, end, indent, ""); - printf(" (%s)\n", (const char *)node->ptr.data); + printf(" (%s)\n", (const char *) node->ptr.data); } else { dump_prefix(prefix, end, indent, "\n"); @@ -2557,16 +2870,16 @@ dump_lc_node(const struct lc_node *node, unsigned pos, prefix[lc_shift(pos)] = save_prefix; if (lc_bytes(node, pos) > 1) - memset(&prefix[lc_shift(pos) + 1], 0, lc_bytes(node, pos) - 1); + memset(&prefix[lc_shift(pos) + 1], 0, lc_bytes(node, pos) - 1); } static void dump_node(const node_t *node, unsigned pos, btrie_oct_t *prefix, int indent) { if (is_lc_node(node)) - dump_lc_node(&node->lc_node, pos, prefix, indent); + dump_lc_node(&node->lc_node, pos, prefix, indent); else - dump_tbm_node(&node->tbm_node, pos, prefix, indent); + dump_tbm_node(&node->tbm_node, pos, prefix, indent); } static void @@ -2591,8 +2904,7 @@ static int parse_prefix(const char *arg, btrie_oct_t prefix[16], unsigned *len) { char addrbuf[128]; - return sscanf(arg, "%127[0-9a-fA-F:]/%u", addrbuf, len) == 2 - && inet_pton(AF_INET6, addrbuf, prefix) == 1; + return sscanf(arg, "%127[0-9a-fA-F:]/%u", addrbuf, len) == 2 && inet_pton(AF_INET6, addrbuf, prefix) == 1; } static int @@ -2603,7 +2915,7 @@ test_btrie(int argc, char *argv[]) btrie_oct_t prefix[16]; unsigned len; - for (i = 1; i < argc-1; i++) { + for (i = 1; i < argc - 1; i++) { if (!parse_prefix(argv[i], prefix, &len)) { fprintf(stderr, "Can not parse arg '%s'\n", argv[i]); return 1; @@ -2616,29 +2928,28 @@ test_btrie(int argc, char *argv[]) if (argc > 1) { const void *data; - if (!parse_prefix(argv[argc-1], prefix, &len)) { - fprintf(stderr, "Can not parse arg '%s'\n", argv[argc-1]); + if (!parse_prefix(argv[argc - 1], prefix, &len)) { + fprintf(stderr, "Can not parse arg '%s'\n", argv[argc - 1]); return 1; } data = btrie_lookup(btrie, prefix, 128); - printf("lookup(%s) => %s\n", argv[argc-1], (const char *)data); + printf("lookup(%s) => %s\n", argv[argc - 1], (const char *) data); } return 0; } -int -main(int argc, char *argv[]) +int main(int argc, char *argv[]) { if ((pgm_name = strrchr(argv[0], '/')) != NULL) - pgm_name++; + pgm_name++; else - pgm_name = argv[0]; + pgm_name = argv[0]; if (argc > 1) - return test_btrie(argc, argv); + return test_btrie(argc, argv); else - return unit_tests(); + return unit_tests(); } #endif /* TEST */ diff --git a/contrib/lc-btrie/btrie.h b/contrib/lc-btrie/btrie.h index 370a03e51..4f147aa4a 100644 --- a/contrib/lc-btrie/btrie.h +++ b/contrib/lc-btrie/btrie.h @@ -50,34 +50,33 @@ typedef uint8_t btrie_oct_t; * in btrie_walk() --- btrie_add_prefix() and btrie_lookup() impose no * limit on the length of bitstrings */ -#define BTRIE_MAX_PREFIX 128 +#define BTRIE_MAX_PREFIX 128 struct btrie; struct memory_pool_s; -struct btrie * btrie_init(struct memory_pool_s *mp); +struct btrie *btrie_init(struct memory_pool_s *mp); -enum btrie_result -{ +enum btrie_result { BTRIE_OKAY = 0, BTRIE_ALLOC_FAILED = -1, BTRIE_DUPLICATE_PREFIX = 1 }; enum btrie_result btrie_add_prefix(struct btrie *btrie, - const btrie_oct_t *prefix, unsigned len, const void *data); + const btrie_oct_t *prefix, unsigned len, const void *data); const void *btrie_lookup(const struct btrie *btrie, const btrie_oct_t *pfx, - unsigned len); + unsigned len); -const char *btrie_stats(const struct btrie *btrie, guint duplicates); +const char *btrie_stats(const struct btrie *btrie, unsigned int duplicates); #ifndef NO_MASTER_DUMP typedef void btrie_walk_cb_t(const btrie_oct_t *prefix, unsigned len, - const void *data, int post, void *user_data); + const void *data, int post, void *user_data); void btrie_walk(const struct btrie *btrie, btrie_walk_cb_t *callback, - void *user_data); + void *user_data); #endif /* not NO_MASTER_DUMP */ #endif /* _BTRIE_H_INCLUDED */ diff --git a/contrib/librdns/resolver.c b/contrib/librdns/resolver.c index bfcfd0ae2..29624ef2a 100644 --- a/contrib/librdns/resolver.c +++ b/contrib/librdns/resolver.c @@ -1,4 +1,20 @@ /* + * Copyright 2024 Vsevolod Stakhov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* * Copyright (c) 2014, Vsevolod Stakhov * * All rights reserved. @@ -43,10 +59,10 @@ #include "compression.h" __KHASH_IMPL(rdns_requests_hash, kh_inline, int, struct rdns_request *, true, - kh_int_hash_func, kh_int_hash_equal); + kh_int_hash_func, kh_int_hash_equal); static int -rdns_send_request (struct rdns_request *req, int fd, bool new_req) +rdns_send_request(struct rdns_request *req, int fd, bool new_req) { ssize_t r; struct rdns_server *serv = req->io->srv; @@ -78,26 +94,26 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req) if (resolver->curve_plugin == NULL) { if (!IS_CHANNEL_CONNECTED(req->io)) { - r = sendto (fd, req->packet, req->pos, 0, - req->io->saddr, - req->io->slen); + r = sendto(fd, req->packet, req->pos, 0, + req->io->saddr, + req->io->slen); } else { - r = send (fd, req->packet, req->pos, 0); + r = send(fd, req->packet, req->pos, 0); } } else { if (!IS_CHANNEL_CONNECTED(req->io)) { - r = resolver->curve_plugin->cb.curve_plugin.send_cb (req, - resolver->curve_plugin->data, - req->io->saddr, - req->io->slen); + r = resolver->curve_plugin->cb.curve_plugin.send_cb(req, + resolver->curve_plugin->data, + req->io->saddr, + req->io->slen); } else { - r = resolver->curve_plugin->cb.curve_plugin.send_cb (req, - resolver->curve_plugin->data, - NULL, - 0); + r = resolver->curve_plugin->cb.curve_plugin.send_cb(req, + resolver->curve_plugin->data, + NULL, + 0); } } if (r == -1) { @@ -108,8 +124,8 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req) k = kh_put(rdns_requests_hash, req->io->requests, req->id, &pr); kh_value(req->io->requests, k) = req; - req->async_event = resolver->async->add_write (resolver->async->data, - fd, req); + req->async_event = resolver->async->add_write(resolver->async->data, + fd, req); req->state = RDNS_REQUEST_WAIT_SEND; } /* @@ -119,17 +135,17 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req) return 0; } else { - rdns_debug ("send failed: %s for server %s", strerror (errno), serv->name); + rdns_debug("send failed: %s for server %s", strerror(errno), serv->name); return -1; } } else if (!IS_CHANNEL_CONNECTED(req->io)) { /* Connect socket */ - r = connect (fd, req->io->saddr, req->io->slen); + r = connect(fd, req->io->saddr, req->io->slen); if (r == -1) { - rdns_err ("cannot connect after sending request: %s for server %s", - strerror (errno), serv->name); + rdns_err("cannot connect after sending request: %s for server %s", + strerror(errno), serv->name); } else { req->io->flags |= RDNS_CHANNEL_CONNECTED; @@ -142,8 +158,8 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req) k = kh_put(rdns_requests_hash, req->io->requests, req->id, &pr); kh_value(req->io->requests, k) = req; /* Fill timeout */ - req->async_event = resolver->async->add_timer (resolver->async->data, - req->timeout, req); + req->async_event = resolver->async->add_timer(resolver->async->data, + req->timeout, req); req->state = RDNS_REQUEST_WAIT_REPLY; } @@ -152,19 +168,19 @@ rdns_send_request (struct rdns_request *req, int fd, bool new_req) static struct rdns_request * -rdns_find_dns_request (uint8_t *in, struct rdns_io_channel *ioc) +rdns_find_dns_request(uint8_t *in, struct rdns_io_channel *ioc) { struct dns_header header; int id; struct rdns_resolver *resolver = ioc->resolver; - memcpy (&header, in, sizeof(header)); + memcpy(&header, in, sizeof(header)); id = header.qid; khiter_t k = kh_get(rdns_requests_hash, ioc->requests, id); if (k == kh_end(ioc->requests)) { /* No such requests found */ - rdns_debug ("DNS request with id %d has not been found for IO channel", id); + rdns_debug("DNS request with id %d has not been found for IO channel", id); return NULL; } @@ -173,10 +189,10 @@ rdns_find_dns_request (uint8_t *in, struct rdns_io_channel *ioc) } static bool -rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req, - struct rdns_reply **_rep) +rdns_parse_reply(uint8_t *in, int r, struct rdns_request *req, + struct rdns_reply **_rep) { - struct dns_header *header = (struct dns_header *)in; + struct dns_header *header = (struct dns_header *) in; struct rdns_reply *rep; struct rdns_reply_entry *elt; uint8_t *pos, *npos; @@ -189,14 +205,14 @@ rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req, /* First check header fields */ if (header->qr == 0) { - rdns_info ("got request while waiting for reply"); + rdns_info("got request while waiting for reply"); return false; } - qdcount = ntohs (header->qdcount); + qdcount = ntohs(header->qdcount); if (qdcount != req->qcount) { - rdns_info ("request has %d queries, reply has %d queries", (int)req->qcount, (int)header->qdcount); + rdns_info("request has %d queries, reply has %d queries", (int) req->qcount, (int) header->qdcount); return false; } @@ -204,12 +220,12 @@ rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req, * Now we have request and query data is now at the end of header, so compare * request QR section and reply QR section */ - req->pos = sizeof (struct dns_header); - pos = in + sizeof (struct dns_header); - t = r - sizeof (struct dns_header); - for (i = 0; i < (int)qdcount; i ++) { - if ((npos = rdns_request_reply_cmp (req, pos,t)) == NULL) { - rdns_info ("DNS request with id %d is for different query, ignoring", (int)req->id); + req->pos = sizeof(struct dns_header); + pos = in + sizeof(struct dns_header); + t = r - sizeof(struct dns_header); + for (i = 0; i < (int) qdcount; i++) { + if ((npos = rdns_request_reply_cmp(req, pos, t)) == NULL) { + rdns_info("DNS request with id %d is for different query, ignoring", (int) req->id); return false; } t -= npos - pos; @@ -218,7 +234,7 @@ rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req, /* * Now pos is in answer section, so we should extract data and form reply */ - rep = rdns_make_reply (req, header->rcode); + rep = rdns_make_reply(req, header->rcode); if (header->ad) { rep->flags |= RDNS_AUTH; @@ -229,7 +245,7 @@ rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req, } if (rep == NULL) { - rdns_warn ("Cannot allocate memory for reply"); + rdns_warn("Cannot allocate memory for reply"); return false; } @@ -238,24 +254,24 @@ rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req, if (rep->code == RDNS_RC_NOERROR) { r -= pos - in; /* Extract RR records */ - for (i = 0; i < ntohs (header->ancount); i ++) { - elt = malloc (sizeof (struct rdns_reply_entry)); - t = rdns_parse_rr (resolver, in, elt, &pos, rep, &r); + for (i = 0; i < ntohs(header->ancount); i++) { + elt = malloc(sizeof(struct rdns_reply_entry)); + t = rdns_parse_rr(resolver, in, elt, &pos, rep, &r); if (t == -1) { - free (elt); - rdns_debug ("incomplete reply"); + free(elt); + rdns_debug("incomplete reply"); break; } else if (t == 1) { - DL_APPEND (rep->entries, elt); + DL_APPEND(rep->entries, elt); if (elt->type == type) { found = true; } } else { - rdns_debug ("no matching reply for %s", - req->requested_names[0].name); - free (elt); + rdns_debug("no matching reply for %s", + req->requested_names[0].name); + free(elt); } } } @@ -272,7 +288,7 @@ rdns_parse_reply (uint8_t *in, int r, struct rdns_request *req, } static bool -rdns_tcp_maybe_realloc_read_buf (struct rdns_io_channel *ioc) +rdns_tcp_maybe_realloc_read_buf(struct rdns_io_channel *ioc) { if (ioc->tcp->read_buf_allocated == 0 && ioc->tcp->next_read_size > 0) { ioc->tcp->cur_read_buf = malloc(ioc->tcp->next_read_size); @@ -294,7 +310,7 @@ rdns_tcp_maybe_realloc_read_buf (struct rdns_io_channel *ioc) void *next_buf = realloc(ioc->tcp->cur_read_buf, next_shift); if (next_buf == NULL) { - free (ioc->tcp->cur_read_buf); + free(ioc->tcp->cur_read_buf); ioc->tcp->cur_read_buf = NULL; return false; } @@ -306,7 +322,7 @@ rdns_tcp_maybe_realloc_read_buf (struct rdns_io_channel *ioc) } static void -rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) +rdns_process_tcp_read(int fd, struct rdns_io_channel *ioc) { ssize_t r; struct rdns_resolver *resolver = ioc->resolver; @@ -327,7 +343,7 @@ rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) /* We have read the size, so we can try read one more time */ if (!rdns_tcp_maybe_realloc_read_buf(ioc)) { rdns_err("failed to allocate %d bytes: %s", - (int)ioc->tcp->next_read_size, strerror(errno)); + (int) ioc->tcp->next_read_size, strerror(errno)); r = -1; goto err; } @@ -338,7 +354,7 @@ rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) } } else if (ioc->tcp->cur_read == 1) { - r = read(fd, ((unsigned char *)&ioc->tcp->next_read_size) + 1, 1); + r = read(fd, ((unsigned char *) &ioc->tcp->next_read_size) + 1, 1); if (r == -1 || r == 0) { goto err; @@ -350,7 +366,7 @@ rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) /* We have read the size, so we can try read one more time */ if (!rdns_tcp_maybe_realloc_read_buf(ioc)) { rdns_err("failed to allocate %d bytes: %s", - (int)ioc->tcp->next_read_size, strerror(errno)); + (int) ioc->tcp->next_read_size, strerror(errno)); r = -1; goto err; } @@ -380,22 +396,22 @@ rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) if ((ioc->tcp->cur_read - 2) == ioc->tcp->next_read_size) { /* We have a full packet ready, process it */ - struct rdns_request *req = rdns_find_dns_request (ioc->tcp->cur_read_buf, ioc); + struct rdns_request *req = rdns_find_dns_request(ioc->tcp->cur_read_buf, ioc); if (req != NULL) { struct rdns_reply *rep; - if (rdns_parse_reply (ioc->tcp->cur_read_buf, - ioc->tcp->next_read_size, req, &rep)) { - UPSTREAM_OK (req->io->srv); + if (rdns_parse_reply(ioc->tcp->cur_read_buf, + ioc->tcp->next_read_size, req, &rep)) { + UPSTREAM_OK(req->io->srv); if (req->resolver->ups && req->io->srv->ups_elt) { - req->resolver->ups->ok (req->io->srv->ups_elt, - req->resolver->ups->data); + req->resolver->ups->ok(req->io->srv->ups_elt, + req->resolver->ups->data); } - req->func (rep, req->arg); - REF_RELEASE (req); + req->func(rep, req->arg); + REF_RELEASE(req); } } else { @@ -406,7 +422,7 @@ rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) ioc->tcp->cur_read = 0; /* Retry read the next packet to avoid unnecessary polling */ - rdns_process_tcp_read (fd, ioc); + rdns_process_tcp_read(fd, ioc); } return; @@ -414,37 +430,37 @@ rdns_process_tcp_read (int fd, struct rdns_io_channel *ioc) err: if (r == 0) { /* Got EOF, just close the socket */ - rdns_debug ("closing TCP channel due to EOF"); - rdns_ioc_tcp_reset (ioc); + rdns_debug("closing TCP channel due to EOF"); + rdns_ioc_tcp_reset(ioc); } else if (errno == EINTR || errno == EAGAIN) { /* We just retry later as there is no real error */ return; } else { - rdns_debug ("closing TCP channel due to IO error: %s", strerror(errno)); - rdns_ioc_tcp_reset (ioc); + rdns_debug("closing TCP channel due to IO error: %s", strerror(errno)); + rdns_ioc_tcp_reset(ioc); } } static void -rdns_process_tcp_connect (int fd, struct rdns_io_channel *ioc) +rdns_process_tcp_connect(int fd, struct rdns_io_channel *ioc) { - ioc->flags |= RDNS_CHANNEL_CONNECTED|RDNS_CHANNEL_ACTIVE; + ioc->flags |= RDNS_CHANNEL_CONNECTED | RDNS_CHANNEL_ACTIVE; ioc->flags &= ~RDNS_CHANNEL_TCP_CONNECTING; if (ioc->tcp->async_read == NULL) { ioc->tcp->async_read = ioc->resolver->async->add_read(ioc->resolver->async->data, - ioc->sock, ioc); + ioc->sock, ioc); } } static bool -rdns_reschedule_req_over_tcp (struct rdns_request *req, struct rdns_server *serv) +rdns_reschedule_req_over_tcp(struct rdns_request *req, struct rdns_server *serv) { struct rdns_resolver *resolver; struct rdns_io_channel *old_ioc = req->io, - *ioc = serv->tcp_io_channels[ottery_rand_uint32 () % serv->tcp_io_cnt]; + *ioc = serv->tcp_io_channels[ottery_rand_uint32() % serv->tcp_io_cnt]; resolver = req->resolver; @@ -461,25 +477,25 @@ rdns_reschedule_req_over_tcp (struct rdns_request *req, struct rdns_server *serv if (oc == NULL) { rdns_err("failed to allocate output buffer for TCP ioc: %s", - strerror(errno)); + strerror(errno)); return false; } - oc->write_buf = ((unsigned char *)oc) + sizeof(*oc); + oc->write_buf = ((unsigned char *) oc) + sizeof(*oc); memcpy(oc->write_buf, req->packet, req->packet_len); oc->next_write_size = htons(req->packet_len); DL_APPEND(ioc->tcp->output_chain, oc); if (ioc->tcp->async_write == NULL) { - ioc->tcp->async_write = resolver->async->add_write ( - resolver->async->data, - ioc->sock, ioc); + ioc->tcp->async_write = resolver->async->add_write( + resolver->async->data, + ioc->sock, ioc); } req->state = RDNS_REQUEST_TCP; /* Switch IO channel from UDP to TCP */ - rdns_request_remove_from_hash (req); + rdns_request_remove_from_hash(req); req->io = ioc; khiter_t k; @@ -489,7 +505,7 @@ rdns_reschedule_req_over_tcp (struct rdns_request *req, struct rdns_server *serv if (pr == 0) { /* We have already a request with this id, so we have to regenerate ID */ - req->id = rdns_permutor_generate_id (); + req->id = rdns_permutor_generate_id(); /* Update packet as well */ uint16_t raw_id = req->id; memcpy(req->packet, &raw_id, sizeof(raw_id)); @@ -499,8 +515,8 @@ rdns_reschedule_req_over_tcp (struct rdns_request *req, struct rdns_server *serv } } - req->async_event = resolver->async->add_timer (resolver->async->data, - req->timeout, req); + req->async_event = resolver->async->add_timer(resolver->async->data, + req->timeout, req); kh_value(req->io->requests, k) = req; REF_RELEASE(old_ioc); @@ -513,7 +529,7 @@ rdns_reschedule_req_over_tcp (struct rdns_request *req, struct rdns_server *serv } static void -rdns_process_udp_read (int fd, struct rdns_io_channel *ioc) +rdns_process_udp_read(int fd, struct rdns_io_channel *ioc) { struct rdns_resolver *resolver; struct rdns_request *req = NULL; @@ -525,47 +541,47 @@ rdns_process_udp_read (int fd, struct rdns_io_channel *ioc) /* First read packet from socket */ if (resolver->curve_plugin == NULL) { - r = recv (fd, in, sizeof (in), 0); - if (r > (int)(sizeof (struct dns_header) + sizeof (struct dns_query))) { - req = rdns_find_dns_request (in, ioc); + r = recv(fd, in, sizeof(in), 0); + if (r > (int) (sizeof(struct dns_header) + sizeof(struct dns_query))) { + req = rdns_find_dns_request(in, ioc); } } else { - r = resolver->curve_plugin->cb.curve_plugin.recv_cb (ioc, in, - sizeof (in), resolver->curve_plugin->data, &req, - ioc->saddr, ioc->slen); + r = resolver->curve_plugin->cb.curve_plugin.recv_cb(ioc, in, + sizeof(in), resolver->curve_plugin->data, &req, + ioc->saddr, ioc->slen); if (req == NULL && - r > (int)(sizeof (struct dns_header) + sizeof (struct dns_query))) { - req = rdns_find_dns_request (in, ioc); + r > (int) (sizeof(struct dns_header) + sizeof(struct dns_query))) { + req = rdns_find_dns_request(in, ioc); } } if (req != NULL) { - if (rdns_parse_reply (in, r, req, &rep)) { - UPSTREAM_OK (req->io->srv); + if (rdns_parse_reply(in, r, req, &rep)) { + UPSTREAM_OK(req->io->srv); if (req->resolver->ups && req->io->srv->ups_elt) { - req->resolver->ups->ok (req->io->srv->ups_elt, - req->resolver->ups->data); + req->resolver->ups->ok(req->io->srv->ups_elt, + req->resolver->ups->data); } - rdns_request_unschedule (req, true); + rdns_request_unschedule(req, true); if (!(rep->flags & RDNS_TRUNCATED)) { req->state = RDNS_REQUEST_REPLIED; req->func(rep, req->arg); /* This will free reply as well */ - REF_RELEASE (req); + REF_RELEASE(req); } else { if (req->io->srv->tcp_io_cnt > 0) { rdns_debug("truncated UDP reply for %s; schedule over TCP", req->requested_names[0].name); /* Reschedule via TCP */ - if (!rdns_reschedule_req_over_tcp (req, req->io->srv)) { + if (!rdns_reschedule_req_over_tcp(req, req->io->srv)) { /* Use truncated reply as we have no other options */ req->state = RDNS_REQUEST_REPLIED; req->func(rep, req->arg); - REF_RELEASE (req); + REF_RELEASE(req); } else { /* Remove and free the truncated reply, as we have rescheduled the reply */ @@ -578,42 +594,40 @@ rdns_process_udp_read (int fd, struct rdns_io_channel *ioc) req->state = RDNS_REQUEST_REPLIED; req->func(rep, req->arg); /* This will free reply as well */ - REF_RELEASE (req); + REF_RELEASE(req); } } } } else { /* Still want to increase uses */ - ioc->uses ++; + ioc->uses++; } } -void -rdns_process_read (int fd, void *arg) +void rdns_process_read(int fd, void *arg) { - struct rdns_io_channel *ioc = (struct rdns_io_channel *)arg; + struct rdns_io_channel *ioc = (struct rdns_io_channel *) arg; struct rdns_resolver *resolver; resolver = ioc->resolver; if (IS_CHANNEL_TCP(ioc)) { if (IS_CHANNEL_CONNECTED(ioc)) { - rdns_process_tcp_read (fd, ioc); + rdns_process_tcp_read(fd, ioc); } else { - rdns_err ("read readiness on non connected TCP channel!"); + rdns_err("read readiness on non connected TCP channel!"); } } else { - rdns_process_udp_read (fd, ioc); + rdns_process_udp_read(fd, ioc); } } -void -rdns_process_timer (void *arg) +void rdns_process_timer(void *arg) { - struct rdns_request *req = (struct rdns_request *)arg; + struct rdns_request *req = (struct rdns_request *) arg; struct rdns_reply *rep; int r; bool renew = false; @@ -621,34 +635,34 @@ rdns_process_timer (void *arg) struct rdns_server *serv = NULL; unsigned cnt; - req->retransmits --; + req->retransmits--; resolver = req->resolver; if (req->resolver->ups && req->io->srv->ups_elt) { - req->resolver->ups->fail (req->io->srv->ups_elt, - req->resolver->ups->data, "timeout waiting reply"); + req->resolver->ups->fail(req->io->srv->ups_elt, + req->resolver->ups->data, "timeout waiting reply"); } else { - UPSTREAM_FAIL (req->io->srv, time (NULL)); + UPSTREAM_FAIL(req->io->srv, time(NULL)); } if (req->state == RDNS_REQUEST_TCP) { - rep = rdns_make_reply (req, RDNS_RC_TIMEOUT); - rdns_request_unschedule (req, true); + rep = rdns_make_reply(req, RDNS_RC_TIMEOUT); + rdns_request_unschedule(req, true); req->state = RDNS_REQUEST_REPLIED; - req->func (rep, req->arg); - REF_RELEASE (req); + req->func(rep, req->arg); + REF_RELEASE(req); return; } if (req->retransmits == 0) { - rep = rdns_make_reply (req, RDNS_RC_TIMEOUT); - rdns_request_unschedule (req, true); + rep = rdns_make_reply(req, RDNS_RC_TIMEOUT); + rdns_request_unschedule(req, true); req->state = RDNS_REQUEST_REPLIED; - req->func (rep, req->arg); - REF_RELEASE (req); + req->func(rep, req->arg); + REF_RELEASE(req); return; } @@ -656,56 +670,57 @@ rdns_process_timer (void *arg) if (!IS_CHANNEL_ACTIVE(req->io) || req->retransmits == 1) { if (resolver->ups) { - cnt = resolver->ups->count (resolver->ups->data); + cnt = resolver->ups->count(resolver->ups->data); } else { cnt = 0; - UPSTREAM_FOREACH (resolver->servers, serv) { - cnt ++; + UPSTREAM_FOREACH(resolver->servers, serv) + { + cnt++; } } if (!IS_CHANNEL_ACTIVE(req->io) || cnt > 1) { /* Do not reschedule IO requests on inactive sockets */ - rdns_debug ("reschedule request with id: %d", (int)req->id); - rdns_request_unschedule (req, true); - REF_RELEASE (req->io); + rdns_debug("reschedule request with id: %d", (int) req->id); + rdns_request_unschedule(req, true); + REF_RELEASE(req->io); if (resolver->ups) { struct rdns_upstream_elt *elt; - elt = resolver->ups->select_retransmit ( - req->requested_names[0].name, - req->requested_names[0].len, - req->io->srv->ups_elt, - resolver->ups->data); + elt = resolver->ups->select_retransmit( + req->requested_names[0].name, + req->requested_names[0].len, + req->io->srv->ups_elt, + resolver->ups->data); if (elt) { serv = elt->server; serv->ups_elt = elt; } else { - UPSTREAM_SELECT_ROUND_ROBIN (resolver->servers, serv); + UPSTREAM_SELECT_ROUND_ROBIN(resolver->servers, serv); } } else { - UPSTREAM_SELECT_ROUND_ROBIN (resolver->servers, serv); + UPSTREAM_SELECT_ROUND_ROBIN(resolver->servers, serv); } if (serv == NULL) { - rdns_warn ("cannot find suitable server for request"); - rep = rdns_make_reply (req, RDNS_RC_SERVFAIL); + rdns_warn("cannot find suitable server for request"); + rep = rdns_make_reply(req, RDNS_RC_SERVFAIL); req->state = RDNS_REQUEST_REPLIED; - req->func (rep, req->arg); - REF_RELEASE (req); + req->func(rep, req->arg); + REF_RELEASE(req); return; } /* Select random IO channel */ - req->io = serv->io_channels[ottery_rand_uint32 () % serv->io_cnt]; - req->io->uses ++; - REF_RETAIN (req->io); + req->io = serv->io_channels[ottery_rand_uint32() % serv->io_cnt]; + req->io->uses++; + REF_RETAIN(req->io); renew = true; } } @@ -714,61 +729,62 @@ rdns_process_timer (void *arg) * Note: when `renew` is true, then send_request deals with the * timers and events itself */ - r = rdns_send_request (req, req->io->sock, renew); + r = rdns_send_request(req, req->io->sock, renew); if (r == 0) { /* Retransmit one more time */ if (!renew) { - req->async->del_timer (req->async->data, - req->async_event); - req->async_event = req->async->add_write (req->async->data, - req->io->sock, req); + req->async->del_timer(req->async->data, + req->async_event); + req->async_event = req->async->add_write(req->async->data, + req->io->sock, req); } req->state = RDNS_REQUEST_WAIT_SEND; } else if (r == -1) { if (req->resolver->ups && req->io->srv->ups_elt) { - req->resolver->ups->fail (req->io->srv->ups_elt, - req->resolver->ups->data, "cannot send retransmit after timeout"); + req->resolver->ups->fail(req->io->srv->ups_elt, + req->resolver->ups->data, "cannot send retransmit after timeout"); } else { - UPSTREAM_FAIL (req->io->srv, time (NULL)); + UPSTREAM_FAIL(req->io->srv, time(NULL)); } if (!renew) { - req->async->del_timer (req->async->data, - req->async_event); + req->async->del_timer(req->async->data, + req->async_event); req->async_event = NULL; rdns_request_remove_from_hash(req); } /* We have not scheduled timeout actually due to send error */ - rep = rdns_make_reply (req, RDNS_RC_NETERR); + rep = rdns_make_reply(req, RDNS_RC_NETERR); req->state = RDNS_REQUEST_REPLIED; - req->func (rep, req->arg); - REF_RELEASE (req); + req->func(rep, req->arg); + REF_RELEASE(req); } else { - req->async->repeat_timer (req->async->data, req->async_event); + req->async->repeat_timer(req->async->data, req->async_event); req->state = RDNS_REQUEST_WAIT_REPLY; } } static void -rdns_process_periodic (void *arg) +rdns_process_periodic(void *arg) { - struct rdns_resolver *resolver = (struct rdns_resolver*)arg; + struct rdns_resolver *resolver = (struct rdns_resolver *) arg; struct rdns_server *serv; - UPSTREAM_RESCAN (resolver->servers, time (NULL)); + UPSTREAM_RESCAN(resolver->servers, time(NULL)); - UPSTREAM_FOREACH (resolver->servers, serv) { - for (int i = 0; i < serv->tcp_io_cnt; i ++) { + UPSTREAM_FOREACH(resolver->servers, serv) + { + for (int i = 0; i < serv->tcp_io_cnt; i++) { if (IS_CHANNEL_CONNECTED(serv->tcp_io_channels[i])) { /* Disconnect channels with no requests in flight */ if (kh_size(serv->tcp_io_channels[i]->requests) == 0) { - rdns_debug ("reset inactive TCP connection to %s", serv->name); - rdns_ioc_tcp_reset (serv->tcp_io_channels[i]); + rdns_debug("reset inactive TCP connection to %s", serv->name); + rdns_ioc_tcp_reset(serv->tcp_io_channels[i]); } } } @@ -776,31 +792,33 @@ rdns_process_periodic (void *arg) } static void -rdns_process_ioc_refresh (void *arg) +rdns_process_ioc_refresh(void *arg) { - struct rdns_resolver *resolver = (struct rdns_resolver*)arg; + struct rdns_resolver *resolver = (struct rdns_resolver *) arg; struct rdns_server *serv; struct rdns_io_channel *ioc, *nioc; unsigned int i; if (resolver->max_ioc_uses > 0) { - UPSTREAM_FOREACH (resolver->servers, serv) { - for (i = 0; i < serv->io_cnt; i ++) { + UPSTREAM_FOREACH(resolver->servers, serv) + { + for (i = 0; i < serv->io_cnt; i++) { ioc = serv->io_channels[i]; if (ioc->uses > resolver->max_ioc_uses) { /* Schedule IOC removing */ - nioc = rdns_ioc_new (serv, resolver, false); + nioc = rdns_ioc_new(serv, resolver, false); if (nioc == NULL) { - rdns_err ("calloc fails to allocate rdns_io_channel"); + rdns_err("calloc fails to allocate rdns_io_channel"); continue; } serv->io_channels[i] = nioc; - rdns_debug ("scheduled io channel for server %s to be refreshed after " - "%lu usages", serv->name, (unsigned long)ioc->uses); + rdns_debug("scheduled io channel for server %s to be refreshed after " + "%lu usages", + serv->name, (unsigned long) ioc->uses); ioc->flags &= ~RDNS_CHANNEL_ACTIVE; - REF_RELEASE (ioc); + REF_RELEASE(ioc); } } } @@ -808,7 +826,7 @@ rdns_process_ioc_refresh (void *arg) } static void -rdns_process_udp_retransmit (int fd, struct rdns_request *req) +rdns_process_udp_retransmit(int fd, struct rdns_request *req) { struct rdns_resolver *resolver; struct rdns_reply *rep; @@ -816,67 +834,67 @@ rdns_process_udp_retransmit (int fd, struct rdns_request *req) resolver = req->resolver; - resolver->async->del_write (resolver->async->data, - req->async_event); + resolver->async->del_write(resolver->async->data, + req->async_event); req->async_event = NULL; if (req->state == RDNS_REQUEST_FAKE) { /* Reply is ready */ - req->func (req->reply, req->arg); - REF_RELEASE (req); + req->func(req->reply, req->arg); + REF_RELEASE(req); return; } - r = rdns_send_request (req, fd, false); + r = rdns_send_request(req, fd, false); if (r == 0) { /* Retransmit one more time */ - req->async_event = req->async->add_write (req->async->data, - fd, req); + req->async_event = req->async->add_write(req->async->data, + fd, req); req->state = RDNS_REQUEST_WAIT_SEND; } else if (r == -1) { if (req->resolver->ups && req->io->srv->ups_elt) { - req->resolver->ups->fail (req->io->srv->ups_elt, - req->resolver->ups->data, "retransmit send failed"); + req->resolver->ups->fail(req->io->srv->ups_elt, + req->resolver->ups->data, "retransmit send failed"); } else { - UPSTREAM_FAIL (req->io->srv, time (NULL)); + UPSTREAM_FAIL(req->io->srv, time(NULL)); } - rep = rdns_make_reply (req, RDNS_RC_NETERR); + rep = rdns_make_reply(req, RDNS_RC_NETERR); req->state = RDNS_REQUEST_REPLIED; - req->func (rep, req->arg); - REF_RELEASE (req); + req->func(rep, req->arg); + REF_RELEASE(req); } else { - req->async_event = req->async->add_timer (req->async->data, - req->timeout, req); + req->async_event = req->async->add_timer(req->async->data, + req->timeout, req); req->state = RDNS_REQUEST_WAIT_REPLY; } } static ssize_t -rdns_write_output_chain (struct rdns_io_channel *ioc, struct rdns_tcp_output_chain *oc) +rdns_write_output_chain(struct rdns_io_channel *ioc, struct rdns_tcp_output_chain *oc) { ssize_t r; struct iovec iov[2]; int niov, already_written; - int packet_len = ntohs (oc->next_write_size); + int packet_len = ntohs(oc->next_write_size); switch (oc->cur_write) { case 0: /* Size + DNS request in full */ iov[0].iov_base = &oc->next_write_size; - iov[0].iov_len = sizeof (oc->next_write_size); + iov[0].iov_len = sizeof(oc->next_write_size); iov[1].iov_base = oc->write_buf; iov[1].iov_len = packet_len; niov = 2; break; case 1: /* Partial Size + DNS request in full */ - iov[0].iov_base = ((unsigned char *)&oc->next_write_size) + 1; + iov[0].iov_base = ((unsigned char *) &oc->next_write_size) + 1; iov[0].iov_len = 1; iov[1].iov_base = oc->write_buf; iov[1].iov_len = packet_len; @@ -905,15 +923,16 @@ rdns_write_output_chain (struct rdns_io_channel *ioc, struct rdns_tcp_output_cha } static void -rdns_process_tcp_write (int fd, struct rdns_io_channel *ioc) +rdns_process_tcp_write(int fd, struct rdns_io_channel *ioc) { struct rdns_resolver *resolver = ioc->resolver; /* Try to write as much as we can */ struct rdns_tcp_output_chain *oc, *tmp; - DL_FOREACH_SAFE(ioc->tcp->output_chain, oc, tmp) { - ssize_t r = rdns_write_output_chain (ioc, oc); + DL_FOREACH_SAFE(ioc->tcp->output_chain, oc, tmp) + { + ssize_t r = rdns_write_output_chain(ioc, oc); if (r == -1) { if (errno == EAGAIN || errno == EINTR) { @@ -921,17 +940,17 @@ rdns_process_tcp_write (int fd, struct rdns_io_channel *ioc) return; } else { - rdns_err ("error when trying to write request to %s: %s", - ioc->srv->name, strerror (errno)); - rdns_ioc_tcp_reset (ioc); + rdns_err("error when trying to write request to %s: %s", + ioc->srv->name, strerror(errno)); + rdns_ioc_tcp_reset(ioc); return; } } else if (ntohs(oc->next_write_size) < oc->cur_write) { /* Packet has been fully written, remove it */ DL_DELETE(ioc->tcp->output_chain, oc); - free (oc); /* It also frees write buf */ - ioc->tcp->cur_output_chains --; + free(oc); /* It also frees write buf */ + ioc->tcp->cur_output_chains--; } else { /* Buffer is not yet processed, stop unless we can continue */ @@ -941,14 +960,13 @@ rdns_process_tcp_write (int fd, struct rdns_io_channel *ioc) if (ioc->tcp->cur_output_chains == 0) { /* Unregister write event */ - ioc->resolver->async->del_write (ioc->resolver->async->data, - ioc->tcp->async_write); + ioc->resolver->async->del_write(ioc->resolver->async->data, + ioc->tcp->async_write); ioc->tcp->async_write = NULL; } } -void -rdns_process_write (int fd, void *arg) +void rdns_process_write(int fd, void *arg) { /* * We first need to dispatch *arg to understand what has caused the write @@ -962,7 +980,7 @@ rdns_process_write (int fd, void *arg) */ uint64_t tag; - memcpy (&tag, arg, sizeof(tag)); + memcpy(&tag, arg, sizeof(tag)); if (tag == RDNS_IO_CHANNEL_TAG) { struct rdns_io_channel *ioc = (struct rdns_io_channel *) arg; @@ -982,10 +1000,10 @@ rdns_process_write (int fd, void *arg) } struct rdns_server * -rdns_select_request_upstream (struct rdns_resolver *resolver, - struct rdns_request *req, - bool is_retransmit, - struct rdns_server *prev_serv) +rdns_select_request_upstream(struct rdns_resolver *resolver, + struct rdns_request *req, + bool is_retransmit, + struct rdns_server *prev_serv) { struct rdns_server *serv = NULL; @@ -993,14 +1011,14 @@ rdns_select_request_upstream (struct rdns_resolver *resolver, struct rdns_upstream_elt *elt; if (is_retransmit && prev_serv) { - elt = resolver->ups->select_retransmit (req->requested_names[0].name, - req->requested_names[0].len, - prev_serv->ups_elt, - resolver->ups->data); + elt = resolver->ups->select_retransmit(req->requested_names[0].name, + req->requested_names[0].len, + prev_serv->ups_elt, + resolver->ups->data); } else { - elt = resolver->ups->select (req->requested_names[0].name, - req->requested_names[0].len, resolver->ups->data); + elt = resolver->ups->select(req->requested_names[0].name, + req->requested_names[0].len, resolver->ups->data); } if (elt) { @@ -1008,29 +1026,28 @@ rdns_select_request_upstream (struct rdns_resolver *resolver, serv->ups_elt = elt; } else { - UPSTREAM_SELECT_ROUND_ROBIN (resolver->servers, serv); + UPSTREAM_SELECT_ROUND_ROBIN(resolver->servers, serv); } } else { - UPSTREAM_SELECT_ROUND_ROBIN (resolver->servers, serv); + UPSTREAM_SELECT_ROUND_ROBIN(resolver->servers, serv); } return serv; } -#define align_ptr(p, a) \ - (guint8 *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) - -struct rdns_request* -rdns_make_request_full ( - struct rdns_resolver *resolver, - dns_callback_type cb, - void *cbdata, - double timeout, - unsigned int repeats, - unsigned int queries, - ... - ) +#define align_ptr(p, a) \ + (uint8_t *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1)) + +struct rdns_request * +rdns_make_request_full( + struct rdns_resolver *resolver, + dns_callback_type cb, + void *cbdata, + double timeout, + unsigned int repeats, + unsigned int queries, + ...) { va_list args; struct rdns_request *req; @@ -1041,7 +1058,7 @@ rdns_make_request_full ( const char *cur_name, *last_name = NULL; khash_t(rdns_compression_hash) *comp = NULL; struct rdns_fake_reply *fake_rep = NULL; - char fake_buf[MAX_FAKE_NAME + sizeof (struct rdns_fake_reply_idx) + 16]; + char fake_buf[MAX_FAKE_NAME + sizeof(struct rdns_fake_reply_idx) + 16]; struct rdns_fake_reply_idx *idx; if (resolver == NULL || !resolver->initialized) { @@ -1049,15 +1066,15 @@ rdns_make_request_full ( return NULL; } - rdns_err ("resolver is uninitialized"); + rdns_err("resolver is uninitialized"); return NULL; } - req = malloc (sizeof (struct rdns_request)); + req = malloc(sizeof(struct rdns_request)); if (req == NULL) { - rdns_err ("failed to allocate memory for request: %s", - strerror (errno)); + rdns_err("failed to allocate memory for request: %s", + strerror(errno)); return NULL; } @@ -1069,13 +1086,13 @@ rdns_make_request_full ( req->io = NULL; req->state = RDNS_REQUEST_NEW; req->packet = NULL; - req->requested_names = calloc (queries, sizeof (struct rdns_request_name)); + req->requested_names = calloc(queries, sizeof(struct rdns_request_name)); req->async_event = NULL; if (req->requested_names == NULL) { - free (req); - rdns_err ("failed to allocate memory for request data: %s", - strerror (errno)); + free(req); + rdns_err("failed to allocate memory for request data: %s", + strerror(errno)); return NULL; } @@ -1084,34 +1101,34 @@ rdns_make_request_full ( #ifdef TWEETNACL req->curve_plugin_data = NULL; #endif - REF_INIT_RETAIN (req, rdns_request_free); + REF_INIT_RETAIN(req, rdns_request_free); /* Calculate packet's total length based on records count */ - va_start (args, queries); + va_start(args, queries); for (i = 0; i < queries * 2; i += 2) { cur = i / 2; - cur_name = va_arg (args, const char *); - type = va_arg (args, int); + cur_name = va_arg(args, const char *); + type = va_arg(args, int); if (cur_name != NULL) { - clen = strlen (cur_name); + clen = strlen(cur_name); if (clen == 0) { - rdns_warn ("got empty name to resolve"); - rdns_request_free (req); + rdns_warn("got empty name to resolve"); + rdns_request_free(req); return NULL; } if (cur_name[0] == '.') { /* Skip dots at the begin */ - unsigned int ndots = strspn (cur_name, "."); + unsigned int ndots = strspn(cur_name, "."); cur_name += ndots; clen -= ndots; if (clen == 0) { - rdns_warn ("got empty name to resolve"); - rdns_request_free (req); + rdns_warn("got empty name to resolve"); + rdns_request_free(req); return NULL; } } @@ -1119,28 +1136,28 @@ rdns_make_request_full ( if (cur_name[clen - 1] == '.') { /* Skip trailing dots */ while (clen >= 1 && cur_name[clen - 1] == '.') { - clen --; + clen--; } if (clen == 0) { - rdns_warn ("got empty name to resolve"); - rdns_request_free (req); + rdns_warn("got empty name to resolve"); + rdns_request_free(req); return NULL; } } if (last_name == NULL && queries == 1 && clen < MAX_FAKE_NAME) { /* We allocate structure in the static space */ - idx = (struct rdns_fake_reply_idx *)align_ptr (fake_buf, 16); + idx = (struct rdns_fake_reply_idx *) align_ptr(fake_buf, 16); idx->type = type; idx->len = clen; - memcpy (idx->request, cur_name, clen); - HASH_FIND (hh, resolver->fake_elts, idx, sizeof (*idx) + clen, - fake_rep); + memcpy(idx->request, cur_name, clen); + HASH_FIND(hh, resolver->fake_elts, idx, sizeof(*idx) + clen, + fake_rep); if (fake_rep) { /* We actually treat it as a short-circuit */ - req->reply = rdns_make_reply (req, fake_rep->rcode); + req->reply = rdns_make_reply(req, fake_rep->rcode); req->reply->entries = fake_rep->result; req->state = RDNS_REQUEST_FAKE; } @@ -1150,16 +1167,16 @@ rdns_make_request_full ( tlen += clen; } else if (last_name == NULL) { - rdns_err ("got NULL as the first name to resolve"); - rdns_request_free (req); + rdns_err("got NULL as the first name to resolve"); + rdns_request_free(req); return NULL; } if (req->state != RDNS_REQUEST_FAKE) { - if (!rdns_format_dns_name (resolver, last_name, clen, - &req->requested_names[cur].name, &olen)) { - rdns_err ("cannot format %s", last_name); - rdns_request_free (req); + if (!rdns_format_dns_name(resolver, last_name, clen, + &req->requested_names[cur].name, &olen)) { + rdns_err("cannot format %s", last_name); + rdns_request_free(req); return NULL; } @@ -1172,27 +1189,28 @@ rdns_make_request_full ( req->requested_names[cur].type = type; } - va_end (args); + va_end(args); if (req->state != RDNS_REQUEST_FAKE) { - rdns_allocate_packet (req, tlen); - rdns_make_dns_header (req, queries); + rdns_allocate_packet(req, tlen); + rdns_make_dns_header(req, queries); for (i = 0; i < queries; i++) { cur_name = req->requested_names[i].name; clen = req->requested_names[i].len; type = req->requested_names[i].type; if (queries > 1) { - if (!rdns_add_rr (req, cur_name, clen, type, &comp)) { - rdns_err ("cannot add rr"); - REF_RELEASE (req); + if (!rdns_add_rr(req, cur_name, clen, type, &comp)) { + rdns_err("cannot add rr"); + REF_RELEASE(req); rdns_compression_free(comp); return NULL; } - } else { - if (!rdns_add_rr (req, cur_name, clen, type, NULL)) { - rdns_err ("cannot add rr"); - REF_RELEASE (req); + } + else { + if (!rdns_add_rr(req, cur_name, clen, type, NULL)) { + rdns_err("cannot add rr"); + REF_RELEASE(req); rdns_compression_free(comp); return NULL; } @@ -1202,7 +1220,7 @@ rdns_make_request_full ( rdns_compression_free(comp); /* Add EDNS RR */ - rdns_add_edns0 (req); + rdns_add_edns0(req); req->retransmits = repeats ? repeats : 1; req->timeout = timeout; @@ -1211,59 +1229,59 @@ rdns_make_request_full ( req->async = resolver->async; - serv = rdns_select_request_upstream (resolver, req, false, NULL); + serv = rdns_select_request_upstream(resolver, req, false, NULL); if (serv == NULL) { - rdns_warn ("cannot find suitable server for request"); - REF_RELEASE (req); + rdns_warn("cannot find suitable server for request"); + REF_RELEASE(req); return NULL; } /* Select random IO channel */ - req->io = serv->io_channels[ottery_rand_uint32 () % serv->io_cnt]; + req->io = serv->io_channels[ottery_rand_uint32() % serv->io_cnt]; if (req->state == RDNS_REQUEST_FAKE) { - req->async_event = resolver->async->add_write (resolver->async->data, - req->io->sock, req); + req->async_event = resolver->async->add_write(resolver->async->data, + req->io->sock, req); } else { /* Now send request to server */ do { - r = rdns_send_request (req, req->io->sock, true); + r = rdns_send_request(req, req->io->sock, true); if (r == -1) { - req->retransmits --; /* It must be > 0 */ + req->retransmits--; /* It must be > 0 */ if (req->retransmits > 0) { if (resolver->ups && serv->ups_elt) { - resolver->ups->fail (serv->ups_elt, resolver->ups->data, - "send IO error"); + resolver->ups->fail(serv->ups_elt, resolver->ups->data, + "send IO error"); } else { - UPSTREAM_FAIL (serv, time (NULL)); + UPSTREAM_FAIL(serv, time(NULL)); } - serv = rdns_select_request_upstream (resolver, req, - true, serv); + serv = rdns_select_request_upstream(resolver, req, + true, serv); if (serv == NULL) { - rdns_warn ("cannot find suitable server for request"); - REF_RELEASE (req); + rdns_warn("cannot find suitable server for request"); + REF_RELEASE(req); return NULL; } - req->io = serv->io_channels[ottery_rand_uint32 () % serv->io_cnt]; + req->io = serv->io_channels[ottery_rand_uint32() % serv->io_cnt]; } else { - rdns_info ("cannot send DNS request: %s", strerror (errno)); - REF_RELEASE (req); + rdns_info("cannot send DNS request: %s", strerror(errno)); + REF_RELEASE(req); if (resolver->ups && serv->ups_elt) { - resolver->ups->fail (serv->ups_elt, resolver->ups->data, - "send IO error"); + resolver->ups->fail(serv->ups_elt, resolver->ups->data, + "send IO error"); } else { - UPSTREAM_FAIL (serv, time (NULL)); + UPSTREAM_FAIL(serv, time(NULL)); } return NULL; @@ -1277,43 +1295,43 @@ rdns_make_request_full ( } while (req->retransmits > 0); } - REF_RETAIN (req->io); - REF_RETAIN (req->resolver); + REF_RETAIN(req->io); + REF_RETAIN(req->resolver); return req; } -bool -rdns_resolver_init (struct rdns_resolver *resolver) +bool rdns_resolver_init(struct rdns_resolver *resolver) { unsigned int i; struct rdns_server *serv; struct rdns_io_channel *ioc; if (!resolver->async_binded) { - rdns_err ("no async backend specified"); + rdns_err("no async backend specified"); return false; } if (resolver->servers == NULL) { - rdns_err ("no DNS servers defined"); + rdns_err("no DNS servers defined"); return false; } /* Now init io channels to all servers */ - UPSTREAM_FOREACH (resolver->servers, serv) { - serv->io_channels = calloc (serv->io_cnt, sizeof (struct rdns_io_channel *)); + UPSTREAM_FOREACH(resolver->servers, serv) + { + serv->io_channels = calloc(serv->io_cnt, sizeof(struct rdns_io_channel *)); if (serv->io_channels == NULL) { - rdns_err ("cannot allocate memory for the resolver IO channels"); + rdns_err("cannot allocate memory for the resolver IO channels"); return false; } - for (i = 0; i < serv->io_cnt; i ++) { + for (i = 0; i < serv->io_cnt; i++) { ioc = rdns_ioc_new(serv, resolver, false); if (ioc == NULL) { - rdns_err ("cannot allocate memory or init the IO channel"); + rdns_err("cannot allocate memory or init the IO channel"); return false; } @@ -1326,16 +1344,16 @@ rdns_resolver_init (struct rdns_resolver *resolver) * We are more forgiving for TCP IO channels: we can have zero of them * if DNS is misconfigured and still be able to resolve stuff */ - serv->tcp_io_channels = calloc (serv->tcp_io_cnt, sizeof (struct rdns_io_channel *)); + serv->tcp_io_channels = calloc(serv->tcp_io_cnt, sizeof(struct rdns_io_channel *)); if (serv->tcp_io_channels == NULL) { - rdns_err ("cannot allocate memory for the resolver TCP IO channels"); + rdns_err("cannot allocate memory for the resolver TCP IO channels"); return false; } - for (i = 0; i < serv->tcp_io_cnt; i ++) { - ioc = rdns_ioc_new (serv, resolver, true); + for (i = 0; i < serv->tcp_io_cnt; i++) { + ioc = rdns_ioc_new(serv, resolver, true); if (ioc == NULL) { - rdns_err ("cannot allocate memory or init the TCP IO channel"); + rdns_err("cannot allocate memory or init the TCP IO channel"); continue; } @@ -1346,8 +1364,8 @@ rdns_resolver_init (struct rdns_resolver *resolver) } if (resolver->async->add_periodic) { - resolver->periodic = resolver->async->add_periodic (resolver->async->data, - UPSTREAM_REVIVE_TIME, rdns_process_periodic, resolver); + resolver->periodic = resolver->async->add_periodic(resolver->async->data, + UPSTREAM_REVIVE_TIME, rdns_process_periodic, resolver); } resolver->initialized = true; @@ -1355,9 +1373,8 @@ rdns_resolver_init (struct rdns_resolver *resolver) return true; } -void -rdns_resolver_register_plugin (struct rdns_resolver *resolver, - struct rdns_plugin *plugin) +void rdns_resolver_register_plugin(struct rdns_resolver *resolver, + struct rdns_plugin *plugin) { if (resolver != NULL && plugin != NULL) { /* XXX: support only network plugin now, and only a single one */ @@ -1368,9 +1385,9 @@ rdns_resolver_register_plugin (struct rdns_resolver *resolver, } void * -rdns_resolver_add_server (struct rdns_resolver *resolver, - const char *name, unsigned int port, - int priority, unsigned int io_cnt) +rdns_resolver_add_server(struct rdns_resolver *resolver, + const char *name, unsigned int port, + int priority, unsigned int io_cnt) { struct rdns_server *serv; union { @@ -1378,8 +1395,8 @@ rdns_resolver_add_server (struct rdns_resolver *resolver, struct in6_addr v6; } addr; - if (inet_pton (AF_INET, name, &addr) == 0 && - inet_pton (AF_INET6, name, &addr) == 0) { + if (inet_pton(AF_INET, name, &addr) == 0 && + inet_pton(AF_INET6, name, &addr) == 0) { /* Invalid IP */ return NULL; } @@ -1391,13 +1408,13 @@ rdns_resolver_add_server (struct rdns_resolver *resolver, return NULL; } - serv = calloc (1, sizeof (struct rdns_server)); + serv = calloc(1, sizeof(struct rdns_server)); if (serv == NULL) { return NULL; } - serv->name = strdup (name); + serv->name = strdup(name); if (serv->name == NULL) { - free (serv); + free(serv); return NULL; } @@ -1406,56 +1423,52 @@ rdns_resolver_add_server (struct rdns_resolver *resolver, serv->tcp_io_cnt = default_tcp_io_cnt; serv->port = port; - UPSTREAM_ADD (resolver->servers, serv, priority); + UPSTREAM_ADD(resolver->servers, serv, priority); return serv; } -void -rdns_resolver_set_logger (struct rdns_resolver *resolver, - rdns_log_function logger, void *log_data) +void rdns_resolver_set_logger(struct rdns_resolver *resolver, + rdns_log_function logger, void *log_data) { resolver->logger = logger; resolver->log_data = log_data; } -void -rdns_resolver_set_log_level (struct rdns_resolver *resolver, - enum rdns_log_level level) +void rdns_resolver_set_log_level(struct rdns_resolver *resolver, + enum rdns_log_level level) { resolver->log_level = level; } -void -rdns_resolver_set_upstream_lib (struct rdns_resolver *resolver, - struct rdns_upstream_context *ups_ctx, - void *ups_data) +void rdns_resolver_set_upstream_lib(struct rdns_resolver *resolver, + struct rdns_upstream_context *ups_ctx, + void *ups_data) { resolver->ups = ups_ctx; resolver->ups->data = ups_data; } -void -rdns_resolver_set_max_io_uses (struct rdns_resolver *resolver, - uint64_t max_ioc_uses, double check_time) +void rdns_resolver_set_max_io_uses(struct rdns_resolver *resolver, + uint64_t max_ioc_uses, double check_time) { if (resolver->refresh_ioc_periodic != NULL) { - resolver->async->del_periodic (resolver->async->data, - resolver->refresh_ioc_periodic); + resolver->async->del_periodic(resolver->async->data, + resolver->refresh_ioc_periodic); resolver->refresh_ioc_periodic = NULL; } resolver->max_ioc_uses = max_ioc_uses; if (check_time > 0.0 && resolver->async->add_periodic) { resolver->refresh_ioc_periodic = - resolver->async->add_periodic (resolver->async->data, - check_time, rdns_process_ioc_refresh, resolver); + resolver->async->add_periodic(resolver->async->data, + check_time, rdns_process_ioc_refresh, resolver); } } static void -rdns_resolver_free (struct rdns_resolver *resolver) +rdns_resolver_free(struct rdns_resolver *resolver) { struct rdns_server *serv, *stmp; struct rdns_io_channel *ioc; @@ -1463,45 +1476,46 @@ rdns_resolver_free (struct rdns_resolver *resolver) if (resolver->initialized) { if (resolver->periodic != NULL) { - resolver->async->del_periodic (resolver->async->data, resolver->periodic); + resolver->async->del_periodic(resolver->async->data, resolver->periodic); } if (resolver->refresh_ioc_periodic != NULL) { - resolver->async->del_periodic (resolver->async->data, - resolver->refresh_ioc_periodic); + resolver->async->del_periodic(resolver->async->data, + resolver->refresh_ioc_periodic); } if (resolver->curve_plugin != NULL && resolver->curve_plugin->dtor != NULL) { - resolver->curve_plugin->dtor (resolver, resolver->curve_plugin->data); + resolver->curve_plugin->dtor(resolver, resolver->curve_plugin->data); } /* Stop IO watch on all IO channels */ - UPSTREAM_FOREACH_SAFE (resolver->servers, serv, stmp) { - for (i = 0; i < serv->io_cnt; i ++) { + UPSTREAM_FOREACH_SAFE(resolver->servers, serv, stmp) + { + for (i = 0; i < serv->io_cnt; i++) { ioc = serv->io_channels[i]; - REF_RELEASE (ioc); + REF_RELEASE(ioc); } - for (i = 0; i < serv->tcp_io_cnt; i ++) { + for (i = 0; i < serv->tcp_io_cnt; i++) { ioc = serv->tcp_io_channels[i]; - REF_RELEASE (ioc); + REF_RELEASE(ioc); } - UPSTREAM_DEL (resolver->servers, serv); - free (serv->io_channels); - free (serv->tcp_io_channels); - free (serv->name); - free (serv); + UPSTREAM_DEL(resolver->servers, serv); + free(serv->io_channels); + free(serv->tcp_io_channels); + free(serv->name); + free(serv); } } - free (resolver->async); - free (resolver); + free(resolver->async); + free(resolver); } struct rdns_resolver * -rdns_resolver_new (int flags) +rdns_resolver_new(int flags) { - struct rdns_resolver *new_resolver; + struct rdns_resolver *new_resolver; - new_resolver = calloc (1, sizeof (struct rdns_resolver)); + new_resolver = calloc(1, sizeof(struct rdns_resolver)); - REF_INIT_RETAIN (new_resolver, rdns_resolver_free); + REF_INIT_RETAIN(new_resolver, rdns_resolver_free); new_resolver->logger = rdns_logger_internal; new_resolver->log_data = new_resolver; @@ -1510,9 +1524,8 @@ rdns_resolver_new (int flags) return new_resolver; } -void -rdns_resolver_async_bind (struct rdns_resolver *resolver, - struct rdns_async_context *ctx) +void rdns_resolver_async_bind(struct rdns_resolver *resolver, + struct rdns_async_context *ctx) { if (resolver != NULL && ctx != NULL) { resolver->async = ctx; @@ -1520,8 +1533,7 @@ rdns_resolver_async_bind (struct rdns_resolver *resolver, } } -void -rdns_resolver_set_dnssec (struct rdns_resolver *resolver, bool enabled) +void rdns_resolver_set_dnssec(struct rdns_resolver *resolver, bool enabled) { if (resolver) { resolver->enable_dnssec = enabled; @@ -1529,49 +1541,49 @@ rdns_resolver_set_dnssec (struct rdns_resolver *resolver, bool enabled) } -void rdns_resolver_set_fake_reply (struct rdns_resolver *resolver, - const char *name, - enum rdns_request_type type, - enum dns_rcode rcode, - struct rdns_reply_entry *reply) +void rdns_resolver_set_fake_reply(struct rdns_resolver *resolver, + const char *name, + enum rdns_request_type type, + enum dns_rcode rcode, + struct rdns_reply_entry *reply) { struct rdns_fake_reply *fake_rep; struct rdns_fake_reply_idx *srch; - unsigned len = strlen (name); + unsigned len = strlen(name); - assert (len < MAX_FAKE_NAME); - srch = malloc (sizeof (*srch) + len); + assert(len < MAX_FAKE_NAME); + srch = malloc(sizeof(*srch) + len); srch->len = len; srch->type = type; - memcpy (srch->request, name, len); + memcpy(srch->request, name, len); - HASH_FIND (hh, resolver->fake_elts, srch, len + sizeof (*srch), fake_rep); + HASH_FIND(hh, resolver->fake_elts, srch, len + sizeof(*srch), fake_rep); if (fake_rep) { /* Append reply to the existing list */ fake_rep->rcode = rcode; if (reply) { - DL_CONCAT (fake_rep->result, reply); + DL_CONCAT(fake_rep->result, reply); } } else { - fake_rep = calloc (1, sizeof (*fake_rep) + len); + fake_rep = calloc(1, sizeof(*fake_rep) + len); if (fake_rep == NULL) { - abort (); + abort(); } fake_rep->rcode = rcode; - memcpy (&fake_rep->key, srch, sizeof (*srch) + len); + memcpy(&fake_rep->key, srch, sizeof(*srch) + len); if (reply) { - DL_CONCAT (fake_rep->result, reply); + DL_CONCAT(fake_rep->result, reply); } - HASH_ADD (hh, resolver->fake_elts, key, sizeof (*srch) + len, fake_rep); + HASH_ADD(hh, resolver->fake_elts, key, sizeof(*srch) + len, fake_rep); } - free (srch); + free(srch); } diff --git a/contrib/libucl/lua_ucl.c b/contrib/libucl/lua_ucl.c index c2e39c462..a9edb3e4d 100644 --- a/contrib/libucl/lua_ucl.c +++ b/contrib/libucl/lua_ucl.c @@ -74,11 +74,11 @@ func = "huh"; #define UCL_ARRAY_TYPE_META "ucl.type.array" #define UCL_IMPL_ARRAY_TYPE_META "ucl.type.impl_array" -static int ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj, int flags); -static int ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj, int flags); -static int ucl_object_push_lua_common (lua_State *L, const ucl_object_t *obj, int flags); -static ucl_object_t* ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags); -static ucl_object_t* ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags); +static int ucl_object_lua_push_array(lua_State *L, const ucl_object_t *obj, int flags); +static int ucl_object_lua_push_scalar(lua_State *L, const ucl_object_t *obj, int flags); +static int ucl_object_push_lua_common(lua_State *L, const ucl_object_t *obj, int flags); +static ucl_object_t *ucl_object_lua_fromtable(lua_State *L, int idx, ucl_string_flags_t flags); +static ucl_object_t *ucl_object_lua_fromelt(lua_State *L, int idx, ucl_string_flags_t flags); static void *ucl_null; @@ -101,46 +101,46 @@ enum lua_ucl_push_flags { * @param obj */ static void -ucl_object_lua_push_element (lua_State *L, const char *key, - const ucl_object_t *obj, int flags) +ucl_object_lua_push_element(lua_State *L, const char *key, + const ucl_object_t *obj, int flags) { - lua_pushstring (L, key); - ucl_object_push_lua_common (L, obj, flags|LUA_UCL_ALLOW_ARRAY); - lua_settable (L, -3); + lua_pushstring(L, key); + ucl_object_push_lua_common(L, obj, flags | LUA_UCL_ALLOW_ARRAY); + lua_settable(L, -3); } static void -lua_ucl_userdata_dtor (void *ud) +lua_ucl_userdata_dtor(void *ud) { - struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud; + struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *) ud; - luaL_unref (fd->L, LUA_REGISTRYINDEX, fd->idx); + luaL_unref(fd->L, LUA_REGISTRYINDEX, fd->idx); if (fd->ret != NULL) { - free (fd->ret); + free(fd->ret); } - free (fd); + free(fd); } static const char * -lua_ucl_userdata_emitter (void *ud) +lua_ucl_userdata_emitter(void *ud) { - struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *)ud; + struct ucl_lua_funcdata *fd = (struct ucl_lua_funcdata *) ud; const char *out = ""; - lua_rawgeti (fd->L, LUA_REGISTRYINDEX, fd->idx); + lua_rawgeti(fd->L, LUA_REGISTRYINDEX, fd->idx); - lua_pcall (fd->L, 0, 1, 0); - out = lua_tostring (fd->L, -1); + lua_pcall(fd->L, 0, 1, 0); + out = lua_tostring(fd->L, -1); if (out != NULL) { /* We need to store temporary string in a more appropriate place */ if (fd->ret) { - free (fd->ret); + free(fd->ret); } - fd->ret = strdup (out); + fd->ret = strdup(out); } - lua_settop (fd->L, 0); + lua_settop(fd->L, 0); return fd->ret; } @@ -152,26 +152,26 @@ lua_ucl_userdata_emitter (void *ud) * @return */ static int -ucl_object_lua_push_object (lua_State *L, const ucl_object_t *obj, - int flags) +ucl_object_lua_push_object(lua_State *L, const ucl_object_t *obj, + int flags) { const ucl_object_t *cur; ucl_object_iter_t it = NULL; if ((flags & LUA_UCL_ALLOW_ARRAY) && obj->next != NULL) { /* Actually we need to push this as an array */ - return ucl_object_lua_push_array (L, obj, flags); + return ucl_object_lua_push_array(L, obj, flags); } - lua_createtable (L, 0, obj->len); + lua_createtable(L, 0, obj->len); it = NULL; - while ((cur = ucl_object_iterate (obj, &it, true)) != NULL) { - ucl_object_lua_push_element (L, ucl_object_key (cur), cur, flags); + while ((cur = ucl_object_iterate(obj, &it, true)) != NULL) { + ucl_object_lua_push_element(L, ucl_object_key(cur), cur, flags); } - luaL_getmetatable (L, UCL_OBJECT_TYPE_META); - lua_setmetatable (L, -2); + luaL_getmetatable(L, UCL_OBJECT_TYPE_META); + lua_setmetatable(L, -2); return 1; } @@ -183,7 +183,7 @@ ucl_object_lua_push_object (lua_State *L, const ucl_object_t *obj, * @return */ static int -ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj, int flags) +ucl_object_lua_push_array(lua_State *L, const ucl_object_t *obj, int flags) { const ucl_object_t *cur; ucl_object_iter_t it; @@ -191,36 +191,38 @@ ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj, int flags) if (obj->type == UCL_ARRAY) { nelt = obj->len; - it = ucl_object_iterate_new (obj); - lua_createtable (L, nelt, 0); + it = ucl_object_iterate_new(obj); + lua_createtable(L, nelt, 0); - while ((cur = ucl_object_iterate_safe (it, true))) { - ucl_object_push_lua (L, cur, (flags & ~LUA_UCL_ALLOW_ARRAY)); - lua_rawseti (L, -2, i); - i ++; + while ((cur = ucl_object_iterate_safe(it, true))) { + ucl_object_push_lua(L, cur, (flags & ~LUA_UCL_ALLOW_ARRAY)); + lua_rawseti(L, -2, i); + i++; } - luaL_getmetatable (L, UCL_ARRAY_TYPE_META); - lua_setmetatable (L, -2); + luaL_getmetatable(L, UCL_ARRAY_TYPE_META); + lua_setmetatable(L, -2); - ucl_object_iterate_free (it); + ucl_object_iterate_free(it); } else { /* Optimize allocation by preallocation of table */ - LL_FOREACH (obj, cur) { - nelt ++; + LL_FOREACH(obj, cur) + { + nelt++; } - lua_createtable (L, nelt, 0); + lua_createtable(L, nelt, 0); - LL_FOREACH (obj, cur) { - ucl_object_push_lua (L, cur, (flags & ~LUA_UCL_ALLOW_ARRAY)); - lua_rawseti (L, -2, i); - i ++; + LL_FOREACH(obj, cur) + { + ucl_object_push_lua(L, cur, (flags & ~LUA_UCL_ALLOW_ARRAY)); + lua_rawseti(L, -2, i); + i++; } - luaL_getmetatable (L, UCL_IMPL_ARRAY_TYPE_META); - lua_setmetatable (L, -2); + luaL_getmetatable(L, UCL_IMPL_ARRAY_TYPE_META); + lua_setmetatable(L, -2); } return 1; @@ -230,48 +232,48 @@ ucl_object_lua_push_array (lua_State *L, const ucl_object_t *obj, int flags) * Push a simple object to lua depending on its actual type */ static int -ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj, - int flags) +ucl_object_lua_push_scalar(lua_State *L, const ucl_object_t *obj, + int flags) { struct ucl_lua_funcdata *fd; if ((flags & LUA_UCL_ALLOW_ARRAY) && obj->next != NULL) { /* Actually we need to push this as an array */ - return ucl_object_lua_push_array (L, obj, flags); + return ucl_object_lua_push_array(L, obj, flags); } switch (obj->type) { case UCL_BOOLEAN: - lua_pushboolean (L, ucl_obj_toboolean (obj)); + lua_pushboolean(L, ucl_obj_toboolean(obj)); break; case UCL_STRING: - lua_pushlstring (L, ucl_obj_tostring (obj), obj->len); + lua_pushlstring(L, ucl_obj_tostring(obj), obj->len); break; case UCL_INT: #if LUA_VERSION_NUM >= 501 - lua_pushinteger (L, ucl_obj_toint (obj)); + lua_pushinteger(L, ucl_obj_toint(obj)); #else - lua_pushnumber (L, ucl_obj_toint (obj)); + lua_pushnumber(L, ucl_obj_toint(obj)); #endif break; case UCL_FLOAT: case UCL_TIME: - lua_pushnumber (L, ucl_obj_todouble (obj)); + lua_pushnumber(L, ucl_obj_todouble(obj)); break; case UCL_NULL: if (flags & LUA_UCL_CONVERT_NIL) { - lua_pushboolean (L, false); + lua_pushboolean(L, false); } else { - lua_getfield (L, LUA_REGISTRYINDEX, "ucl.null"); + lua_getfield(L, LUA_REGISTRYINDEX, "ucl.null"); } break; case UCL_USERDATA: - fd = (struct ucl_lua_funcdata *)obj->value.ud; - lua_rawgeti (L, LUA_REGISTRYINDEX, fd->idx); + fd = (struct ucl_lua_funcdata *) obj->value.ud; + lua_rawgeti(L, LUA_REGISTRYINDEX, fd->idx); break; default: - lua_pushnil (L); + lua_pushnil(L); break; } @@ -279,15 +281,15 @@ ucl_object_lua_push_scalar (lua_State *L, const ucl_object_t *obj, } static int -ucl_object_push_lua_common (lua_State *L, const ucl_object_t *obj, int flags) +ucl_object_push_lua_common(lua_State *L, const ucl_object_t *obj, int flags) { switch (obj->type) { case UCL_OBJECT: - return ucl_object_lua_push_object (L, obj, flags); + return ucl_object_lua_push_object(L, obj, flags); case UCL_ARRAY: - return ucl_object_lua_push_array (L, obj, flags); + return ucl_object_lua_push_array(L, obj, flags); default: - return ucl_object_lua_push_scalar (L, obj, flags); + return ucl_object_lua_push_scalar(L, obj, flags); } } @@ -306,19 +308,16 @@ ucl_object_push_lua_common (lua_State *L, const ucl_object_t *obj, int flags) * @param {bool} allow_array expand implicit arrays (should be true for all but partial arrays) * @return {int} `1` if an object is pushed to lua */ -int -ucl_object_push_lua (lua_State *L, const ucl_object_t *obj, bool allow_array) +int ucl_object_push_lua(lua_State *L, const ucl_object_t *obj, bool allow_array) { - return ucl_object_push_lua_common (L, obj, - allow_array ? LUA_UCL_ALLOW_ARRAY : LUA_UCL_DEFAULT_FLAGS); + return ucl_object_push_lua_common(L, obj, + allow_array ? LUA_UCL_ALLOW_ARRAY : LUA_UCL_DEFAULT_FLAGS); } -int -ucl_object_push_lua_filter_nil (lua_State *L, const ucl_object_t *obj, bool allow_array) +int ucl_object_push_lua_filter_nil(lua_State *L, const ucl_object_t *obj, bool allow_array) { - return ucl_object_push_lua_common (L, obj, - allow_array ? (LUA_UCL_ALLOW_ARRAY|LUA_UCL_CONVERT_NIL) : - (LUA_UCL_DEFAULT_FLAGS|LUA_UCL_CONVERT_NIL)); + return ucl_object_push_lua_common(L, obj, + allow_array ? (LUA_UCL_ALLOW_ARRAY | LUA_UCL_CONVERT_NIL) : (LUA_UCL_DEFAULT_FLAGS | LUA_UCL_CONVERT_NIL)); } /** @@ -328,7 +327,7 @@ ucl_object_push_lua_filter_nil (lua_State *L, const ucl_object_t *obj, bool allo * @param idx */ static ucl_object_t * -ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags) +ucl_object_lua_fromtable(lua_State *L, int idx, ucl_string_flags_t flags) { ucl_object_t *obj, *top = NULL, *cur; size_t keylen; @@ -338,52 +337,54 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags) if (idx < 0) { /* For negative indicies we want to invert them */ - idx = lua_gettop (L) + idx + 1; + idx = lua_gettop(L) + idx + 1; } /* First, we check from metatable */ - if (luaL_getmetafield (L, idx, "class") != 0) { + if (luaL_getmetafield(L, idx, "class") != 0) { - if (lua_type (L, -1) == LUA_TSTRING) { - const char *classname = lua_tostring (L, -1); + if (lua_type(L, -1) == LUA_TSTRING) { + const char *classname = lua_tostring(L, -1); - if (strcmp (classname, UCL_OBJECT_TYPE_META) == 0) { + if (strcmp(classname, UCL_OBJECT_TYPE_META) == 0) { is_array = false; found_mt = true; - } else if (strcmp (classname, UCL_ARRAY_TYPE_META) == 0) { + } + else if (strcmp(classname, UCL_ARRAY_TYPE_META) == 0) { is_array = true; found_mt = true; #if LUA_VERSION_NUM >= 502 - max = lua_rawlen (L, idx); + max = lua_rawlen(L, idx); #else - max = lua_objlen (L, idx); + max = lua_objlen(L, idx); #endif nelts = max; - } else if (strcmp (classname, UCL_IMPL_ARRAY_TYPE_META) == 0) { + } + else if (strcmp(classname, UCL_IMPL_ARRAY_TYPE_META) == 0) { is_array = true; is_implicit = true; found_mt = true; #if LUA_VERSION_NUM >= 502 - max = lua_rawlen (L, idx); + max = lua_rawlen(L, idx); #else - max = lua_objlen (L, idx); + max = lua_objlen(L, idx); #endif nelts = max; } } - lua_pop (L, 1); + lua_pop(L, 1); } if (!found_mt) { /* Check for array (it is all inefficient) */ - lua_pushnil (L); + lua_pushnil(L); - while (lua_next (L, idx) != 0) { - lua_pushvalue (L, -2); + while (lua_next(L, idx) != 0) { + lua_pushvalue(L, -2); - if (lua_type (L, -1) == LUA_TNUMBER) { - double num = lua_tonumber (L, -1); + if (lua_type(L, -1) == LUA_TNUMBER) { + double num = lua_tonumber(L, -1); if (num == (int) num) { if (num > max) { max = num; @@ -399,8 +400,8 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags) is_array = false; } - lua_pop (L, 2); - nelts ++; + lua_pop(L, 2); + nelts++; } } @@ -409,52 +410,53 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags) int i; if (!is_implicit) { - top = ucl_object_typed_new (UCL_ARRAY); - ucl_object_reserve (top, nelts); + top = ucl_object_typed_new(UCL_ARRAY); + ucl_object_reserve(top, nelts); } else { top = NULL; } - for (i = 1; i <= max; i ++) { - lua_pushinteger (L, i); - lua_gettable (L, idx); + for (i = 1; i <= max; i++) { + lua_pushinteger(L, i); + lua_gettable(L, idx); - obj = ucl_object_lua_fromelt (L, lua_gettop (L), flags); + obj = ucl_object_lua_fromelt(L, lua_gettop(L), flags); if (obj != NULL) { if (is_implicit) { - DL_APPEND (top, obj); + DL_APPEND(top, obj); } else { - ucl_array_append (top, obj); + ucl_array_append(top, obj); } } - lua_pop (L, 1); + lua_pop(L, 1); } } else { - lua_pushnil (L); - top = ucl_object_typed_new (UCL_OBJECT); - ucl_object_reserve (top, nelts); + lua_pushnil(L); + top = ucl_object_typed_new(UCL_OBJECT); + ucl_object_reserve(top, nelts); - while (lua_next (L, idx) != 0) { + while (lua_next(L, idx) != 0) { /* copy key to avoid modifications */ - lua_pushvalue (L, -2); - k = lua_tolstring (L, -1, &keylen); - obj = ucl_object_lua_fromelt (L, lua_gettop (L) - 1, flags); + lua_pushvalue(L, -2); + k = lua_tolstring(L, -1, &keylen); + obj = ucl_object_lua_fromelt(L, lua_gettop(L) - 1, flags); if (obj != NULL) { - ucl_object_insert_key (top, obj, k, keylen, true); + ucl_object_insert_key(top, obj, k, keylen, true); - DL_FOREACH (obj, cur) { + DL_FOREACH(obj, cur) + { if (cur->keylen == 0) { cur->keylen = obj->keylen; cur->key = obj->key; } } } - lua_pop (L, 2); + lua_pop(L, 2); } } @@ -468,7 +470,7 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags) * @param idx */ static ucl_object_t * -ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags) +ucl_object_lua_fromelt(lua_State *L, int idx, ucl_string_flags_t flags) { int type; double num; @@ -477,11 +479,11 @@ ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags) const char *str; size_t sz; - type = lua_type (L, idx); + type = lua_type(L, idx); switch (type) { case LUA_TSTRING: - str = lua_tolstring (L, idx, &sz); + str = lua_tolstring(L, idx, &sz); if (str) { /* @@ -496,31 +498,31 @@ ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags) } } else { - obj = ucl_object_typed_new (UCL_NULL); + obj = ucl_object_typed_new(UCL_NULL); } break; case LUA_TNUMBER: - num = lua_tonumber (L, idx); - if (num == (int64_t)num) { - obj = ucl_object_fromint (num); + num = lua_tonumber(L, idx); + if (num == (int64_t) num) { + obj = ucl_object_fromint(num); } else { - obj = ucl_object_fromdouble (num); + obj = ucl_object_fromdouble(num); } break; case LUA_TBOOLEAN: - obj = ucl_object_frombool (lua_toboolean (L, idx)); + obj = ucl_object_frombool(lua_toboolean(L, idx)); break; case LUA_TUSERDATA: - if (lua_topointer (L, idx) == ucl_null) { - obj = ucl_object_typed_new (UCL_NULL); + if (lua_topointer(L, idx) == ucl_null) { + obj = ucl_object_typed_new(UCL_NULL); } else { /* Assume it is a text like object */ - struct _rspamd_lua_text *t = lua_touserdata (L, idx); + struct _rspamd_lua_text *t = lua_touserdata(L, idx); if (t) { - if (t->len >0) { + if (t->len > 0) { obj = ucl_object_fromstring_common(t->start, t->len, 0); } else { @@ -537,30 +539,30 @@ ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags) case LUA_TTABLE: case LUA_TFUNCTION: case LUA_TTHREAD: - if (luaL_getmetafield (L, idx, "__gen_ucl")) { - if (lua_isfunction (L, -1)) { - lua_settop (L, 3); /* gen, obj, func */ - lua_insert (L, 1); /* func, gen, obj */ - lua_insert (L, 2); /* func, obj, gen */ + if (luaL_getmetafield(L, idx, "__gen_ucl")) { + if (lua_isfunction(L, -1)) { + lua_settop(L, 3); /* gen, obj, func */ + lua_insert(L, 1); /* func, gen, obj */ + lua_insert(L, 2); /* func, obj, gen */ lua_call(L, 2, 1); - obj = ucl_object_lua_fromelt (L, 1, flags); + obj = ucl_object_lua_fromelt(L, 1, flags); } - lua_pop (L, 2); + lua_pop(L, 2); } else { if (type == LUA_TTABLE) { - obj = ucl_object_lua_fromtable (L, idx, flags); + obj = ucl_object_lua_fromtable(L, idx, flags); } else if (type == LUA_TFUNCTION) { - fd = malloc (sizeof (*fd)); + fd = malloc(sizeof(*fd)); if (fd != NULL) { - lua_pushvalue (L, idx); + lua_pushvalue(L, idx); fd->L = L; fd->ret = NULL; - fd->idx = luaL_ref (L, LUA_REGISTRYINDEX); + fd->idx = luaL_ref(L, LUA_REGISTRYINDEX); - obj = ucl_object_new_userdata (lua_ucl_userdata_dtor, - lua_ucl_userdata_emitter, (void *)fd); + obj = ucl_object_new_userdata(lua_ucl_userdata_dtor, + lua_ucl_userdata_emitter, (void *) fd); } } } @@ -580,18 +582,18 @@ ucl_object_lua_fromelt (lua_State *L, int idx, ucl_string_flags_t flags) * this object thus needs to be unref'ed after usage. */ ucl_object_t * -ucl_object_lua_import (lua_State *L, int idx) +ucl_object_lua_import(lua_State *L, int idx) { ucl_object_t *obj; int t; - t = lua_type (L, idx); + t = lua_type(L, idx); switch (t) { case LUA_TTABLE: - obj = ucl_object_lua_fromtable (L, idx, UCL_STRING_RAW); + obj = ucl_object_lua_fromtable(L, idx, UCL_STRING_RAW); break; default: - obj = ucl_object_lua_fromelt (L, idx, UCL_STRING_RAW); + obj = ucl_object_lua_fromelt(L, idx, UCL_STRING_RAW); break; } @@ -608,18 +610,18 @@ ucl_object_lua_import (lua_State *L, int idx) * this object thus needs to be unref'ed after usage. */ ucl_object_t * -ucl_object_lua_import_escape (lua_State *L, int idx) +ucl_object_lua_import_escape(lua_State *L, int idx) { ucl_object_t *obj; int t; - t = lua_type (L, idx); + t = lua_type(L, idx); switch (t) { case LUA_TTABLE: - obj = ucl_object_lua_fromtable (L, idx, UCL_STRING_ESCAPE); + obj = ucl_object_lua_fromtable(L, idx, UCL_STRING_ESCAPE); break; default: - obj = ucl_object_lua_fromelt (L, idx, UCL_STRING_ESCAPE); + obj = ucl_object_lua_fromelt(L, idx, UCL_STRING_ESCAPE); break; } @@ -627,84 +629,84 @@ ucl_object_lua_import_escape (lua_State *L, int idx) } static int -lua_ucl_to_string (lua_State *L, const ucl_object_t *obj, enum ucl_emitter type) +lua_ucl_to_string(lua_State *L, const ucl_object_t *obj, enum ucl_emitter type) { unsigned char *result; size_t outlen; - result = ucl_object_emit_len (obj, type, &outlen); + result = ucl_object_emit_len(obj, type, &outlen); if (result != NULL) { - lua_pushlstring (L, (const char *)result, outlen); - free (result); + lua_pushlstring(L, (const char *) result, outlen); + free(result); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_ucl_parser_init (lua_State *L) +lua_ucl_parser_init(lua_State *L) { struct ucl_parser *parser, **pparser; int flags = UCL_PARSER_NO_FILEVARS; - if (lua_gettop (L) >= 1) { - flags = lua_tonumber (L, 1); + if (lua_gettop(L) >= 1) { + flags = lua_tonumber(L, 1); } - parser = ucl_parser_new (flags); + parser = ucl_parser_new(flags); if (parser == NULL) { - lua_pushnil (L); + lua_pushnil(L); } - pparser = lua_newuserdata (L, sizeof (parser)); + pparser = lua_newuserdata(L, sizeof(parser)); *pparser = parser; - luaL_getmetatable (L, PARSER_META); - lua_setmetatable (L, -2); + luaL_getmetatable(L, PARSER_META); + lua_setmetatable(L, -2); return 1; } static struct ucl_parser * -lua_ucl_parser_get (lua_State *L, int index) +lua_ucl_parser_get(lua_State *L, int index) { return *((struct ucl_parser **) luaL_checkudata(L, index, PARSER_META)); } static ucl_object_t * -lua_ucl_object_get (lua_State *L, int index) +lua_ucl_object_get(lua_State *L, int index) { return *((ucl_object_t **) luaL_checkudata(L, index, OBJECT_META)); } static void -lua_ucl_push_opaque (lua_State *L, ucl_object_t *obj) +lua_ucl_push_opaque(lua_State *L, ucl_object_t *obj) { ucl_object_t **pobj; - pobj = lua_newuserdata (L, sizeof (*pobj)); + pobj = lua_newuserdata(L, sizeof(*pobj)); *pobj = obj; - luaL_getmetatable (L, OBJECT_META); - lua_setmetatable (L, -2); + luaL_getmetatable(L, OBJECT_META); + lua_setmetatable(L, -2); } static inline enum ucl_parse_type -lua_ucl_str_to_parse_type (const char *str) +lua_ucl_str_to_parse_type(const char *str) { enum ucl_parse_type type = UCL_PARSE_UCL; if (str != NULL) { - if (strcasecmp (str, "msgpack") == 0) { + if (strcasecmp(str, "msgpack") == 0) { type = UCL_PARSE_MSGPACK; } - else if (strcasecmp (str, "sexp") == 0 || - strcasecmp (str, "csexp") == 0) { + else if (strcasecmp(str, "sexp") == 0 || + strcasecmp(str, "csexp") == 0) { type = UCL_PARSE_CSEXP; } - else if (strcasecmp (str, "auto") == 0) { + else if (strcasecmp(str, "auto") == 0) { type = UCL_PARSE_AUTO; } } @@ -728,28 +730,28 @@ else end */ static int -lua_ucl_parser_parse_file (lua_State *L) +lua_ucl_parser_parse_file(lua_State *L) { struct ucl_parser *parser; const char *file; int ret = 2; - parser = lua_ucl_parser_get (L, 1); - file = luaL_checkstring (L, 2); + parser = lua_ucl_parser_get(L, 1); + file = luaL_checkstring(L, 2); if (parser != NULL && file != NULL) { - if (ucl_parser_add_file (parser, file)) { - lua_pushboolean (L, true); + if (ucl_parser_add_file(parser, file)) { + lua_pushboolean(L, true); ret = 1; } else { - lua_pushboolean (L, false); - lua_pushstring (L, ucl_parser_get_error (parser)); + lua_pushboolean(L, false); + lua_pushstring(L, ucl_parser_get_error(parser)); } } else { - lua_pushboolean (L, false); - lua_pushstring (L, "invalid arguments"); + lua_pushboolean(L, false); + lua_pushstring(L, "invalid arguments"); } return ret; @@ -766,23 +768,23 @@ local parser = ucl.parser() local res = parser:register_variable('CONFDIR', '/etc/foo') */ static int -lua_ucl_parser_register_variable (lua_State *L) +lua_ucl_parser_register_variable(lua_State *L) { struct ucl_parser *parser; const char *name, *value; int ret = 2; - parser = lua_ucl_parser_get (L, 1); - name = luaL_checkstring (L, 2); - value = luaL_checkstring (L, 3); + parser = lua_ucl_parser_get(L, 1); + name = luaL_checkstring(L, 2); + value = luaL_checkstring(L, 3); if (parser != NULL && name != NULL && value != NULL) { - ucl_parser_register_variable (parser, name, value); - lua_pushboolean (L, true); + ucl_parser_register_variable(parser, name, value); + lua_pushboolean(L, true); ret = 1; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return ret; @@ -798,28 +800,28 @@ local parser = ucl.parser() local res = parser:register_variables({CONFDIR = '/etc/foo', VARDIR = '/var'}) */ static int -lua_ucl_parser_register_variables (lua_State *L) +lua_ucl_parser_register_variables(lua_State *L) { struct ucl_parser *parser; const char *name, *value; int ret = 2; - parser = lua_ucl_parser_get (L, 1); + parser = lua_ucl_parser_get(L, 1); - if (parser != NULL && lua_type (L, 2) == LUA_TTABLE) { - for (lua_pushnil (L); lua_next (L, 2); lua_pop (L, 1)) { - lua_pushvalue (L, -2); - name = luaL_checkstring (L, -1); - value = luaL_checkstring (L, -2); - ucl_parser_register_variable (parser, name, value); - lua_pop (L, 1); + if (parser != NULL && lua_type(L, 2) == LUA_TTABLE) { + for (lua_pushnil(L); lua_next(L, 2); lua_pop(L, 1)) { + lua_pushvalue(L, -2); + name = luaL_checkstring(L, -1); + value = luaL_checkstring(L, -2); + ucl_parser_register_variable(parser, name, value); + lua_pop(L, 1); } - lua_pushboolean (L, true); + lua_pushboolean(L, true); ret = 1; } else { - return luaL_error (L, "invalid arguments"); + return luaL_error(L, "invalid arguments"); } return ret; @@ -832,7 +834,7 @@ lua_ucl_parser_register_variables (lua_State *L) * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned */ static int -lua_ucl_parser_parse_string (lua_State *L) +lua_ucl_parser_parse_string(lua_State *L) { struct ucl_parser *parser; const char *string; @@ -840,27 +842,27 @@ lua_ucl_parser_parse_string (lua_State *L) enum ucl_parse_type type = UCL_PARSE_UCL; int ret = 2; - parser = lua_ucl_parser_get (L, 1); - string = luaL_checklstring (L, 2, &llen); + parser = lua_ucl_parser_get(L, 1); + string = luaL_checklstring(L, 2, &llen); - if (lua_type (L, 3) == LUA_TSTRING) { - type = lua_ucl_str_to_parse_type (lua_tostring (L, 3)); + if (lua_type(L, 3) == LUA_TSTRING) { + type = lua_ucl_str_to_parse_type(lua_tostring(L, 3)); } if (parser != NULL && string != NULL) { - if (ucl_parser_add_chunk_full (parser, (const unsigned char *)string, - llen, 0, UCL_DUPLICATE_APPEND, type)) { - lua_pushboolean (L, true); + if (ucl_parser_add_chunk_full(parser, (const unsigned char *) string, + llen, 0, UCL_DUPLICATE_APPEND, type)) { + lua_pushboolean(L, true); ret = 1; } else { - lua_pushboolean (L, false); - lua_pushstring (L, ucl_parser_get_error (parser)); + lua_pushboolean(L, false); + lua_pushstring(L, ucl_parser_get_error(parser)); } } else { - lua_pushboolean (L, false); - lua_pushstring (L, "invalid arguments"); + lua_pushboolean(L, false); + lua_pushstring(L, "invalid arguments"); } return ret; @@ -873,24 +875,24 @@ lua_ucl_parser_parse_string (lua_State *L) * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned */ static int -lua_ucl_parser_parse_text (lua_State *L) +lua_ucl_parser_parse_text(lua_State *L) { struct ucl_parser *parser; struct _rspamd_lua_text *t; enum ucl_parse_type type = UCL_PARSE_UCL; int ret = 2; - parser = lua_ucl_parser_get (L, 1); + parser = lua_ucl_parser_get(L, 1); - if (lua_type (L, 2) == LUA_TUSERDATA) { - t = lua_touserdata (L, 2); + if (lua_type(L, 2) == LUA_TUSERDATA) { + t = lua_touserdata(L, 2); } - else if (lua_type (L, 2) == LUA_TSTRING) { - const gchar *s; + else if (lua_type(L, 2) == LUA_TSTRING) { + const char *s; gsize len; static struct _rspamd_lua_text st_t; - s = lua_tolstring (L, 2, &len); + s = lua_tolstring(L, 2, &len); st_t.start = s; st_t.len = len; @@ -900,24 +902,24 @@ lua_ucl_parser_parse_text (lua_State *L) return luaL_error(L, "invalid argument as input, expected userdata or a string"); } - if (lua_type (L, 3) == LUA_TSTRING) { - type = lua_ucl_str_to_parse_type (lua_tostring (L, 3)); + if (lua_type(L, 3) == LUA_TSTRING) { + type = lua_ucl_str_to_parse_type(lua_tostring(L, 3)); } if (parser != NULL && t != NULL) { - if (ucl_parser_add_chunk_full (parser, (const unsigned char *)t->start, - t->len, 0, UCL_DUPLICATE_APPEND, type)) { - lua_pushboolean (L, true); + if (ucl_parser_add_chunk_full(parser, (const unsigned char *) t->start, + t->len, 0, UCL_DUPLICATE_APPEND, type)) { + lua_pushboolean(L, true); ret = 1; } else { - lua_pushboolean (L, false); - lua_pushstring (L, ucl_parser_get_error (parser)); + lua_pushboolean(L, false); + lua_pushstring(L, ucl_parser_get_error(parser)); } } else { - lua_pushboolean (L, false); - lua_pushstring (L, "invalid arguments"); + lua_pushboolean(L, false); + lua_pushstring(L, "invalid arguments"); } return ret; @@ -929,22 +931,22 @@ lua_ucl_parser_parse_text (lua_State *L) * @return {variant or nil} ucl object as lua native variable */ static int -lua_ucl_parser_get_object (lua_State *L) +lua_ucl_parser_get_object(lua_State *L) { struct ucl_parser *parser; ucl_object_t *obj; int ret = 1; - parser = lua_ucl_parser_get (L, 1); - obj = ucl_parser_get_object (parser); + parser = lua_ucl_parser_get(L, 1); + obj = ucl_parser_get_object(parser); if (obj != NULL) { - ret = ucl_object_push_lua (L, obj, false); + ret = ucl_object_push_lua(L, obj, false); /* no need to keep reference */ - ucl_object_unref (obj); + ucl_object_unref(obj); } else { - lua_pushnil (L); + lua_pushnil(L); } return ret; @@ -957,20 +959,20 @@ lua_ucl_parser_get_object (lua_State *L) * @return {ucl.object or nil} ucl object wrapped variable */ static int -lua_ucl_parser_get_object_wrapped (lua_State *L) +lua_ucl_parser_get_object_wrapped(lua_State *L) { struct ucl_parser *parser; ucl_object_t *obj; int ret = 1; - parser = lua_ucl_parser_get (L, 1); - obj = ucl_parser_get_object (parser); + parser = lua_ucl_parser_get(L, 1); + obj = ucl_parser_get_object(parser); if (obj != NULL) { - lua_ucl_push_opaque (L, obj); + lua_ucl_push_opaque(L, obj); } else { - lua_pushnil (L); + lua_pushnil(L); } return ret; @@ -986,76 +988,78 @@ lua_ucl_parser_get_object_wrapped (lua_State *L) * */ static int -lua_ucl_parser_validate (lua_State *L) +lua_ucl_parser_validate(lua_State *L) { struct ucl_parser *parser, *schema_parser; ucl_object_t *schema; const char *schema_file; struct ucl_schema_error err; - parser = lua_ucl_parser_get (L, 1); + parser = lua_ucl_parser_get(L, 1); if (parser && parser->top_obj) { - if (lua_type (L, 2) == LUA_TTABLE) { - schema = ucl_object_lua_import (L, 2); + if (lua_type(L, 2) == LUA_TTABLE) { + schema = ucl_object_lua_import(L, 2); if (schema == NULL) { - lua_pushboolean (L, false); - lua_pushstring (L, "cannot load schema from lua table"); + lua_pushboolean(L, false); + lua_pushstring(L, "cannot load schema from lua table"); return 2; } } - else if (lua_type (L, 2) == LUA_TSTRING) { - schema_parser = ucl_parser_new (0); - schema_file = luaL_checkstring (L, 2); + else if (lua_type(L, 2) == LUA_TSTRING) { + schema_parser = ucl_parser_new(0); + schema_file = luaL_checkstring(L, 2); - if (!ucl_parser_add_file (schema_parser, schema_file)) { - lua_pushboolean (L, false); - lua_pushfstring (L, "cannot parse schema file \"%s\": " - "%s", schema_file, ucl_parser_get_error (parser)); - ucl_parser_free (schema_parser); + if (!ucl_parser_add_file(schema_parser, schema_file)) { + lua_pushboolean(L, false); + lua_pushfstring(L, "cannot parse schema file \"%s\": " + "%s", + schema_file, ucl_parser_get_error(parser)); + ucl_parser_free(schema_parser); return 2; } - schema = ucl_parser_get_object (schema_parser); - ucl_parser_free (schema_parser); + schema = ucl_parser_get_object(schema_parser); + ucl_parser_free(schema_parser); } else { - lua_pushboolean (L, false); - lua_pushstring (L, "invalid schema argument"); + lua_pushboolean(L, false); + lua_pushstring(L, "invalid schema argument"); return 2; } - if (!ucl_object_validate (schema, parser->top_obj, &err)) { - lua_pushboolean (L, false); - lua_pushfstring (L, "validation error: " - "%s", err.msg); + if (!ucl_object_validate(schema, parser->top_obj, &err)) { + lua_pushboolean(L, false); + lua_pushfstring(L, "validation error: " + "%s", + err.msg); } else { - lua_pushboolean (L, true); - lua_pushnil (L); + lua_pushboolean(L, true); + lua_pushnil(L); } - ucl_object_unref (schema); + ucl_object_unref(schema); } else { - lua_pushboolean (L, false); - lua_pushstring (L, "invalid parser or empty top object"); + lua_pushboolean(L, false); + lua_pushstring(L, "invalid parser or empty top object"); } return 2; } static int -lua_ucl_parser_gc (lua_State *L) +lua_ucl_parser_gc(lua_State *L) { struct ucl_parser *parser; - parser = lua_ucl_parser_get (L, 1); - ucl_parser_free (parser); + parser = lua_ucl_parser_get(L, 1); + ucl_parser_free(parser); return 0; } @@ -1066,38 +1070,38 @@ lua_ucl_parser_gc (lua_State *L) * @return {variant} any lua object */ static int -lua_ucl_object_unwrap (lua_State *L) +lua_ucl_object_unwrap(lua_State *L) { ucl_object_t *obj; - obj = lua_ucl_object_get (L, 1); + obj = lua_ucl_object_get(L, 1); if (obj) { - ucl_object_push_lua (L, obj, true); + ucl_object_push_lua(L, obj, true); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static inline enum ucl_emitter -lua_ucl_str_to_emit_type (const char *strtype) +lua_ucl_str_to_emit_type(const char *strtype) { enum ucl_emitter format = UCL_EMIT_JSON_COMPACT; - if (strcasecmp (strtype, "json") == 0) { + if (strcasecmp(strtype, "json") == 0) { format = UCL_EMIT_JSON; } - else if (strcasecmp (strtype, "json-compact") == 0) { + else if (strcasecmp(strtype, "json-compact") == 0) { format = UCL_EMIT_JSON_COMPACT; } - else if (strcasecmp (strtype, "yaml") == 0) { + else if (strcasecmp(strtype, "yaml") == 0) { format = UCL_EMIT_YAML; } - else if (strcasecmp (strtype, "config") == 0 || - strcasecmp (strtype, "ucl") == 0) { + else if (strcasecmp(strtype, "config") == 0 || + strcasecmp(strtype, "ucl") == 0) { format = UCL_EMIT_CONFIG; } @@ -1118,26 +1122,26 @@ lua_ucl_str_to_emit_type (const char *strtype) * @return {string} string representation of the opaque ucl object */ static int -lua_ucl_object_tostring (lua_State *L) +lua_ucl_object_tostring(lua_State *L) { ucl_object_t *obj; enum ucl_emitter format = UCL_EMIT_JSON_COMPACT; - obj = lua_ucl_object_get (L, 1); + obj = lua_ucl_object_get(L, 1); if (obj) { - if (lua_gettop (L) > 1) { - if (lua_type (L, 2) == LUA_TSTRING) { - const char *strtype = lua_tostring (L, 2); + if (lua_gettop(L) > 1) { + if (lua_type(L, 2) == LUA_TSTRING) { + const char *strtype = lua_tostring(L, 2); - format = lua_ucl_str_to_emit_type (strtype); + format = lua_ucl_str_to_emit_type(strtype); } } - return lua_ucl_to_string (L, obj, format); + return lua_ucl_to_string(L, obj, format); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -1156,7 +1160,7 @@ lua_ucl_object_tostring (lua_State *L) * ucl object as {result,err,ext_refs} */ static int -lua_ucl_object_validate (lua_State *L) +lua_ucl_object_validate(lua_State *L) { ucl_object_t *obj, *schema, *ext_refs = NULL; const ucl_object_t *schema_elt; @@ -1164,34 +1168,34 @@ lua_ucl_object_validate (lua_State *L) struct ucl_schema_error err; const char *path = NULL; - obj = lua_ucl_object_get (L, 1); - schema = lua_ucl_object_get (L, 2); + obj = lua_ucl_object_get(L, 1); + schema = lua_ucl_object_get(L, 2); - if (schema && obj && ucl_object_type (schema) == UCL_OBJECT) { - if (lua_gettop (L) > 2) { - if (lua_type (L, 3) == LUA_TSTRING) { - path = lua_tostring (L, 3); + if (schema && obj && ucl_object_type(schema) == UCL_OBJECT) { + if (lua_gettop(L) > 2) { + if (lua_type(L, 3) == LUA_TSTRING) { + path = lua_tostring(L, 3); if (path[0] == '#') { path++; } } - else if (lua_type (L, 3) == LUA_TUSERDATA || lua_type (L, 3) == - LUA_TTABLE) { + else if (lua_type(L, 3) == LUA_TUSERDATA || lua_type(L, 3) == + LUA_TTABLE) { /* External refs */ - ext_refs = lua_ucl_object_get (L, 3); + ext_refs = lua_ucl_object_get(L, 3); } - if (lua_gettop (L) > 3) { - if (lua_type (L, 4) == LUA_TUSERDATA || lua_type (L, 4) == - LUA_TTABLE) { + if (lua_gettop(L) > 3) { + if (lua_type(L, 4) == LUA_TUSERDATA || lua_type(L, 4) == + LUA_TTABLE) { /* External refs */ - ext_refs = lua_ucl_object_get (L, 4); + ext_refs = lua_ucl_object_get(L, 4); } } } if (path) { - schema_elt = ucl_object_lookup_path_char (schema, path, '/'); + schema_elt = ucl_object_lookup_path_char(schema, path, '/'); } else { /* Use the top object */ @@ -1199,39 +1203,39 @@ lua_ucl_object_validate (lua_State *L) } if (schema_elt) { - res = ucl_object_validate_root_ext (schema_elt, obj, schema, - ext_refs, &err); + res = ucl_object_validate_root_ext(schema_elt, obj, schema, + ext_refs, &err); if (res) { - lua_pushboolean (L, res); - lua_pushnil (L); + lua_pushboolean(L, res); + lua_pushnil(L); if (ext_refs) { - lua_ucl_push_opaque (L, ext_refs); + lua_ucl_push_opaque(L, ext_refs); } } else { - lua_pushboolean (L, res); - lua_pushfstring (L, "validation error: %s", err.msg); + lua_pushboolean(L, res); + lua_pushfstring(L, "validation error: %s", err.msg); if (ext_refs) { - lua_ucl_push_opaque (L, ext_refs); + lua_ucl_push_opaque(L, ext_refs); } } } else { - lua_pushboolean (L, res); + lua_pushboolean(L, res); - lua_pushfstring (L, "cannot find the requested path: %s", path); + lua_pushfstring(L, "cannot find the requested path: %s", path); if (ext_refs) { - lua_ucl_push_opaque (L, ext_refs); + lua_ucl_push_opaque(L, ext_refs); } } } else { - lua_pushboolean (L, res); - lua_pushstring (L, "invalid object or schema"); + lua_pushboolean(L, res); + lua_pushstring(L, "invalid object or schema"); } if (ext_refs) { @@ -1242,166 +1246,166 @@ lua_ucl_object_validate (lua_State *L) } static int -lua_ucl_object_gc (lua_State *L) +lua_ucl_object_gc(lua_State *L) { ucl_object_t *obj; - obj = lua_ucl_object_get (L, 1); + obj = lua_ucl_object_get(L, 1); - ucl_object_unref (obj); + ucl_object_unref(obj); return 0; } static void -lua_ucl_parser_mt (lua_State *L) +lua_ucl_parser_mt(lua_State *L) { - luaL_newmetatable (L, PARSER_META); + luaL_newmetatable(L, PARSER_META); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - lua_pushcfunction (L, lua_ucl_parser_gc); - lua_setfield (L, -2, "__gc"); + lua_pushcfunction(L, lua_ucl_parser_gc); + lua_setfield(L, -2, "__gc"); - lua_pushcfunction (L, lua_ucl_parser_parse_file); - lua_setfield (L, -2, "parse_file"); + lua_pushcfunction(L, lua_ucl_parser_parse_file); + lua_setfield(L, -2, "parse_file"); - lua_pushcfunction (L, lua_ucl_parser_parse_string); - lua_setfield (L, -2, "parse_string"); + lua_pushcfunction(L, lua_ucl_parser_parse_string); + lua_setfield(L, -2, "parse_string"); - lua_pushcfunction (L, lua_ucl_parser_parse_text); - lua_setfield (L, -2, "parse_text"); + lua_pushcfunction(L, lua_ucl_parser_parse_text); + lua_setfield(L, -2, "parse_text"); - lua_pushcfunction (L, lua_ucl_parser_register_variable); - lua_setfield (L, -2, "register_variable"); + lua_pushcfunction(L, lua_ucl_parser_register_variable); + lua_setfield(L, -2, "register_variable"); - lua_pushcfunction (L, lua_ucl_parser_register_variables); - lua_setfield (L, -2, "register_variables"); + lua_pushcfunction(L, lua_ucl_parser_register_variables); + lua_setfield(L, -2, "register_variables"); - lua_pushcfunction (L, lua_ucl_parser_get_object); - lua_setfield (L, -2, "get_object"); + lua_pushcfunction(L, lua_ucl_parser_get_object); + lua_setfield(L, -2, "get_object"); - lua_pushcfunction (L, lua_ucl_parser_get_object_wrapped); - lua_setfield (L, -2, "get_object_wrapped"); + lua_pushcfunction(L, lua_ucl_parser_get_object_wrapped); + lua_setfield(L, -2, "get_object_wrapped"); - lua_pushcfunction (L, lua_ucl_parser_validate); - lua_setfield (L, -2, "validate"); + lua_pushcfunction(L, lua_ucl_parser_validate); + lua_setfield(L, -2, "validate"); - lua_pop (L, 1); + lua_pop(L, 1); } static void -lua_ucl_object_mt (lua_State *L) +lua_ucl_object_mt(lua_State *L) { - luaL_newmetatable (L, OBJECT_META); + luaL_newmetatable(L, OBJECT_META); lua_pushvalue(L, -1); lua_setfield(L, -2, "__index"); - lua_pushcfunction (L, lua_ucl_object_gc); - lua_setfield (L, -2, "__gc"); + lua_pushcfunction(L, lua_ucl_object_gc); + lua_setfield(L, -2, "__gc"); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "__tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "__tostring"); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "tostring"); - lua_pushcfunction (L, lua_ucl_object_unwrap); - lua_setfield (L, -2, "unwrap"); + lua_pushcfunction(L, lua_ucl_object_unwrap); + lua_setfield(L, -2, "unwrap"); - lua_pushcfunction (L, lua_ucl_object_unwrap); - lua_setfield (L, -2, "tolua"); + lua_pushcfunction(L, lua_ucl_object_unwrap); + lua_setfield(L, -2, "tolua"); - lua_pushcfunction (L, lua_ucl_object_validate); - lua_setfield (L, -2, "validate"); + lua_pushcfunction(L, lua_ucl_object_validate); + lua_setfield(L, -2, "validate"); - lua_pushstring (L, OBJECT_META); - lua_setfield (L, -2, "class"); + lua_pushstring(L, OBJECT_META); + lua_setfield(L, -2, "class"); - lua_pop (L, 1); + lua_pop(L, 1); } static void -lua_ucl_types_mt (lua_State *L) +lua_ucl_types_mt(lua_State *L) { - luaL_newmetatable (L, UCL_OBJECT_TYPE_META); + luaL_newmetatable(L, UCL_OBJECT_TYPE_META); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "__tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "__tostring"); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "tostring"); - lua_pushstring (L, UCL_OBJECT_TYPE_META); - lua_setfield (L, -2, "class"); + lua_pushstring(L, UCL_OBJECT_TYPE_META); + lua_setfield(L, -2, "class"); - lua_pop (L, 1); + lua_pop(L, 1); - luaL_newmetatable (L, UCL_ARRAY_TYPE_META); + luaL_newmetatable(L, UCL_ARRAY_TYPE_META); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "__tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "__tostring"); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "tostring"); - lua_pushstring (L, UCL_ARRAY_TYPE_META); - lua_setfield (L, -2, "class"); + lua_pushstring(L, UCL_ARRAY_TYPE_META); + lua_setfield(L, -2, "class"); - lua_pop (L, 1); + lua_pop(L, 1); - luaL_newmetatable (L, UCL_IMPL_ARRAY_TYPE_META); + luaL_newmetatable(L, UCL_IMPL_ARRAY_TYPE_META); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "__tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "__tostring"); - lua_pushcfunction (L, lua_ucl_object_tostring); - lua_setfield (L, -2, "tostring"); + lua_pushcfunction(L, lua_ucl_object_tostring); + lua_setfield(L, -2, "tostring"); - lua_pushstring (L, UCL_IMPL_ARRAY_TYPE_META); - lua_setfield (L, -2, "class"); + lua_pushstring(L, UCL_IMPL_ARRAY_TYPE_META); + lua_setfield(L, -2, "class"); - lua_pop (L, 1); + lua_pop(L, 1); } static int -lua_ucl_to_json (lua_State *L) +lua_ucl_to_json(lua_State *L) { ucl_object_t *obj; int format = UCL_EMIT_JSON; - if (lua_gettop (L) > 1) { - if (lua_toboolean (L, 2)) { + if (lua_gettop(L) > 1) { + if (lua_toboolean(L, 2)) { format = UCL_EMIT_JSON_COMPACT; } } - obj = ucl_object_lua_import (L, 1); + obj = ucl_object_lua_import(L, 1); if (obj != NULL) { - lua_ucl_to_string (L, obj, format); - ucl_object_unref (obj); + lua_ucl_to_string(L, obj, format); + ucl_object_unref(obj); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_ucl_to_config (lua_State *L) +lua_ucl_to_config(lua_State *L) { ucl_object_t *obj; - obj = ucl_object_lua_import (L, 1); + obj = ucl_object_lua_import(L, 1); if (obj != NULL) { - lua_ucl_to_string (L, obj, UCL_EMIT_CONFIG); - ucl_object_unref (obj); + lua_ucl_to_string(L, obj, UCL_EMIT_CONFIG); + ucl_object_unref(obj); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; @@ -1442,133 +1446,132 @@ func = "huh"; --]] */ static int -lua_ucl_to_format (lua_State *L) +lua_ucl_to_format(lua_State *L) { ucl_object_t *obj; int format = UCL_EMIT_JSON; bool sort = false; - if (lua_gettop (L) > 1) { - if (lua_type (L, 2) == LUA_TNUMBER) { - format = lua_tonumber (L, 2); + if (lua_gettop(L) > 1) { + if (lua_type(L, 2) == LUA_TNUMBER) { + format = lua_tonumber(L, 2); if (format < 0 || format >= UCL_EMIT_YAML) { - lua_pushnil (L); + lua_pushnil(L); return 1; } } - else if (lua_type (L, 2) == LUA_TSTRING) { - const char *strtype = lua_tostring (L, 2); + else if (lua_type(L, 2) == LUA_TSTRING) { + const char *strtype = lua_tostring(L, 2); - if (strcasecmp (strtype, "json") == 0) { + if (strcasecmp(strtype, "json") == 0) { format = UCL_EMIT_JSON; } - else if (strcasecmp (strtype, "json-compact") == 0) { + else if (strcasecmp(strtype, "json-compact") == 0) { format = UCL_EMIT_JSON_COMPACT; } - else if (strcasecmp (strtype, "yaml") == 0) { + else if (strcasecmp(strtype, "yaml") == 0) { format = UCL_EMIT_YAML; } - else if (strcasecmp (strtype, "config") == 0 || - strcasecmp (strtype, "ucl") == 0) { + else if (strcasecmp(strtype, "config") == 0 || + strcasecmp(strtype, "ucl") == 0) { format = UCL_EMIT_CONFIG; } - else if (strcasecmp (strtype, "msgpack") == 0 || - strcasecmp (strtype, "messagepack") == 0) { + else if (strcasecmp(strtype, "msgpack") == 0 || + strcasecmp(strtype, "messagepack") == 0) { format = UCL_EMIT_MSGPACK; } } - if (lua_isboolean (L, 3)) { - sort = lua_toboolean (L, 3); + if (lua_isboolean(L, 3)) { + sort = lua_toboolean(L, 3); } } - obj = ucl_object_lua_import (L, 1); + obj = ucl_object_lua_import(L, 1); if (obj != NULL) { if (sort) { - if (ucl_object_type (obj) == UCL_OBJECT) { - ucl_object_sort_keys (obj, UCL_SORT_KEYS_RECURSIVE); + if (ucl_object_type(obj) == UCL_OBJECT) { + ucl_object_sort_keys(obj, UCL_SORT_KEYS_RECURSIVE); } } - lua_ucl_to_string (L, obj, format); - ucl_object_unref (obj); + lua_ucl_to_string(L, obj, format); + ucl_object_unref(obj); } else { - lua_pushnil (L); + lua_pushnil(L); } return 1; } static int -lua_ucl_null_tostring (lua_State* L) +lua_ucl_null_tostring(lua_State *L) { - lua_pushstring (L, "null"); + lua_pushstring(L, "null"); return 1; } static void -lua_ucl_null_mt (lua_State *L) +lua_ucl_null_mt(lua_State *L) { - luaL_newmetatable (L, NULL_META); + luaL_newmetatable(L, NULL_META); - lua_pushcfunction (L, lua_ucl_null_tostring); - lua_setfield (L, -2, "__tostring"); + lua_pushcfunction(L, lua_ucl_null_tostring); + lua_setfield(L, -2, "__tostring"); - lua_pop (L, 1); + lua_pop(L, 1); } -int -luaopen_ucl (lua_State *L) +int luaopen_ucl(lua_State *L) { - lua_ucl_parser_mt (L); - lua_ucl_null_mt (L); - lua_ucl_object_mt (L); - lua_ucl_types_mt (L); + lua_ucl_parser_mt(L); + lua_ucl_null_mt(L); + lua_ucl_object_mt(L); + lua_ucl_types_mt(L); /* Create the refs weak table: */ - lua_createtable (L, 0, 2); - lua_pushliteral (L, "v"); /* tbl, "v" */ - lua_setfield (L, -2, "__mode"); - lua_pushvalue (L, -1); /* tbl, tbl */ - lua_setmetatable (L, -2); /* tbl */ - lua_setfield (L, LUA_REGISTRYINDEX, "ucl.refs"); + lua_createtable(L, 0, 2); + lua_pushliteral(L, "v"); /* tbl, "v" */ + lua_setfield(L, -2, "__mode"); + lua_pushvalue(L, -1); /* tbl, tbl */ + lua_setmetatable(L, -2); /* tbl */ + lua_setfield(L, LUA_REGISTRYINDEX, "ucl.refs"); - lua_newtable (L); + lua_newtable(L); - lua_pushcfunction (L, lua_ucl_parser_init); - lua_setfield (L, -2, "parser"); + lua_pushcfunction(L, lua_ucl_parser_init); + lua_setfield(L, -2, "parser"); - lua_pushcfunction (L, lua_ucl_to_json); - lua_setfield (L, -2, "to_json"); + lua_pushcfunction(L, lua_ucl_to_json); + lua_setfield(L, -2, "to_json"); - lua_pushcfunction (L, lua_ucl_to_config); - lua_setfield (L, -2, "to_config"); + lua_pushcfunction(L, lua_ucl_to_config); + lua_setfield(L, -2, "to_config"); - lua_pushcfunction (L, lua_ucl_to_format); - lua_setfield (L, -2, "to_format"); + lua_pushcfunction(L, lua_ucl_to_format); + lua_setfield(L, -2, "to_format"); - ucl_null = lua_newuserdata (L, 0); - luaL_getmetatable (L, NULL_META); - lua_setmetatable (L, -2); + ucl_null = lua_newuserdata(L, 0); + luaL_getmetatable(L, NULL_META); + lua_setmetatable(L, -2); - lua_pushvalue (L, -1); - lua_setfield (L, LUA_REGISTRYINDEX, "ucl.null"); + lua_pushvalue(L, -1); + lua_setfield(L, LUA_REGISTRYINDEX, "ucl.null"); - lua_setfield (L, -2, "null"); + lua_setfield(L, -2, "null"); return 1; } -struct ucl_lua_funcdata* -ucl_object_toclosure (const ucl_object_t *obj) +struct ucl_lua_funcdata * +ucl_object_toclosure(const ucl_object_t *obj) { if (obj == NULL || obj->type != UCL_USERDATA) { return NULL; } - return (struct ucl_lua_funcdata*)obj->value.ud; + return (struct ucl_lua_funcdata *) obj->value.ud; } diff --git a/contrib/libucl/ucl_hash.c b/contrib/libucl/ucl_hash.c index a26c26f9f..eb2053ba1 100644 --- a/contrib/libucl/ucl_hash.c +++ b/contrib/libucl/ucl_hash.c @@ -45,7 +45,7 @@ struct ucl_hash_struct { }; static uint64_t -ucl_hash_seed (void) +ucl_hash_seed(void) { static uint64_t seed; if (seed == 0) { @@ -53,41 +53,41 @@ ucl_hash_seed (void) seed = UCL_RANDOM_FUNCTION; #else /* Not very random but can be useful for our purposes */ - seed = time (NULL); + seed = time(NULL); #endif } return seed; } -extern const guchar lc_map[256]; +extern const unsigned char lc_map[256]; static inline uint32_t -ucl_hash_func (const ucl_object_t *o) +ucl_hash_func(const ucl_object_t *o) { - return (uint32_t)rspamd_cryptobox_fast_hash (o->key, o->keylen, 0xb9a1ef83c4561c95ULL); + return (uint32_t) rspamd_cryptobox_fast_hash(o->key, o->keylen, 0xb9a1ef83c4561c95ULL); } static inline int -ucl_hash_equal (const ucl_object_t *k1, const ucl_object_t *k2) +ucl_hash_equal(const ucl_object_t *k1, const ucl_object_t *k2) { if (k1->keylen == k2->keylen) { - return memcmp (k1->key, k2->key, k1->keylen) == 0; + return memcmp(k1->key, k2->key, k1->keylen) == 0; } return 0; } -KHASH_INIT (ucl_hash_node, const ucl_object_t *, struct ucl_hash_elt *, 1, - ucl_hash_func, ucl_hash_equal) +KHASH_INIT(ucl_hash_node, const ucl_object_t *, struct ucl_hash_elt *, 1, + ucl_hash_func, ucl_hash_equal) static inline uint32_t -ucl_hash_caseless_func (const ucl_object_t *o) +ucl_hash_caseless_func(const ucl_object_t *o) { unsigned len = o->keylen; unsigned leftover = o->keylen % 4; unsigned fp, i; - const uint8_t* s = (const uint8_t*)o->key; + const uint8_t *s = (const uint8_t *) o->key; union { struct { unsigned char c1, c2, c3, c4; @@ -98,7 +98,7 @@ ucl_hash_caseless_func (const ucl_object_t *o) rspamd_cryptobox_fast_hash_state_t hst; fp = len - leftover; - rspamd_cryptobox_fast_hash_init (&hst, h); + rspamd_cryptobox_fast_hash_init(&hst, h); for (i = 0; i != fp; i += 4) { u.c.c1 = s[i], u.c.c2 = s[i + 1], u.c.c3 = s[i + 2], u.c.c4 = s[i + 3]; @@ -106,58 +106,58 @@ ucl_hash_caseless_func (const ucl_object_t *o) u.c.c2 = lc_map[u.c.c2]; u.c.c3 = lc_map[u.c.c3]; u.c.c4 = lc_map[u.c.c4]; - rspamd_cryptobox_fast_hash_update (&hst, &u, sizeof (u)); + rspamd_cryptobox_fast_hash_update(&hst, &u, sizeof(u)); } u.pp = 0; switch (leftover) { case 3: - u.c.c3 = lc_map[(unsigned char)s[i++]]; + u.c.c3 = lc_map[(unsigned char) s[i++]]; case 2: /* fallthrough */ - u.c.c2 = lc_map[(unsigned char)s[i++]]; + u.c.c2 = lc_map[(unsigned char) s[i++]]; case 1: /* fallthrough */ - u.c.c1 = lc_map[(unsigned char)s[i]]; - rspamd_cryptobox_fast_hash_update (&hst, &u, sizeof (u)); + u.c.c1 = lc_map[(unsigned char) s[i]]; + rspamd_cryptobox_fast_hash_update(&hst, &u, sizeof(u)); break; } - return (uint32_t)rspamd_cryptobox_fast_hash_final (&hst); + return (uint32_t) rspamd_cryptobox_fast_hash_final(&hst); } static inline bool -ucl_hash_caseless_equal (const ucl_object_t *k1, const ucl_object_t *k2) +ucl_hash_caseless_equal(const ucl_object_t *k1, const ucl_object_t *k2) { if (k1->keylen == k2->keylen) { - return rspamd_lc_cmp (k1->key, k2->key, k1->keylen) == 0; + return rspamd_lc_cmp(k1->key, k2->key, k1->keylen) == 0; } return false; } -KHASH_INIT (ucl_hash_caseless_node, const ucl_object_t *, struct ucl_hash_elt *, 1, - ucl_hash_caseless_func, ucl_hash_caseless_equal) +KHASH_INIT(ucl_hash_caseless_node, const ucl_object_t *, struct ucl_hash_elt *, 1, + ucl_hash_caseless_func, ucl_hash_caseless_equal) -ucl_hash_t* -ucl_hash_create (bool ignore_case) +ucl_hash_t * +ucl_hash_create(bool ignore_case) { ucl_hash_t *new; - new = UCL_ALLOC (sizeof (ucl_hash_t)); + new = UCL_ALLOC(sizeof(ucl_hash_t)); if (new != NULL) { void *h; new->head = NULL; new->caseless = ignore_case; if (ignore_case) { - h = (void *)kh_init (ucl_hash_caseless_node); + h = (void *) kh_init(ucl_hash_caseless_node); } else { - h = (void *)kh_init (ucl_hash_node); + h = (void *) kh_init(ucl_hash_node); } if (h == NULL) { - UCL_FREE (sizeof (ucl_hash_t), new); + UCL_FREE(sizeof(ucl_hash_t), new); return NULL; } new->hash = h; @@ -165,7 +165,7 @@ ucl_hash_create (bool ignore_case) return new; } -void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func func) +void ucl_hash_destroy(ucl_hash_t *hashlin, ucl_hash_free_func func) { if (hashlin == NULL) { @@ -175,16 +175,16 @@ void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func func) if (func != NULL) { /* Iterate over the hash first */ khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) - hashlin->hash; + hashlin->hash; khiter_t k; const ucl_object_t *cur, *tmp; - for (k = kh_begin (h); k != kh_end (h); ++k) { - if (kh_exist (h, k)) { - cur = (kh_value (h, k))->obj; + for (k = kh_begin(h); k != kh_end(h); ++k) { + if (kh_exist(h, k)) { + cur = (kh_value(h, k))->obj; while (cur != NULL) { tmp = cur->next; - func (__DECONST (ucl_object_t *, cur)); + func(__DECONST(ucl_object_t *, cur)); cur = tmp; } } @@ -193,27 +193,27 @@ void ucl_hash_destroy (ucl_hash_t* hashlin, ucl_hash_free_func func) if (hashlin->caseless) { khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) - hashlin->hash; - kh_destroy (ucl_hash_caseless_node, h); + hashlin->hash; + kh_destroy(ucl_hash_caseless_node, h); } else { khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) - hashlin->hash; - kh_destroy (ucl_hash_node, h); + hashlin->hash; + kh_destroy(ucl_hash_node, h); } struct ucl_hash_elt *cur, *tmp; - DL_FOREACH_SAFE(hashlin->head, cur, tmp) { + DL_FOREACH_SAFE(hashlin->head, cur, tmp) + { UCL_FREE(sizeof(*cur), cur); } - UCL_FREE (sizeof (*hashlin), hashlin); + UCL_FREE(sizeof(*hashlin), hashlin); } -bool -ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, - const char *key, unsigned keylen) +bool ucl_hash_insert(ucl_hash_t *hashlin, const ucl_object_t *obj, + const char *key, unsigned keylen) { khiter_t k; int ret; @@ -225,11 +225,11 @@ ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, if (hashlin->caseless) { khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) - hashlin->hash; - k = kh_put (ucl_hash_caseless_node, h, obj, &ret); + hashlin->hash; + k = kh_put(ucl_hash_caseless_node, h, obj, &ret); if (ret > 0) { elt = UCL_ALLOC(sizeof(*elt)); - pelt = &kh_value (h, k); + pelt = &kh_value(h, k); *pelt = elt; DL_APPEND(hashlin->head, elt); elt->obj = obj; @@ -240,25 +240,26 @@ ucl_hash_insert (ucl_hash_t* hashlin, const ucl_object_t *obj, } else { khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) - hashlin->hash; - k = kh_put (ucl_hash_node, h, obj, &ret); + hashlin->hash; + k = kh_put(ucl_hash_node, h, obj, &ret); if (ret > 0) { elt = UCL_ALLOC(sizeof(*elt)); - pelt = &kh_value (h, k); + pelt = &kh_value(h, k); *pelt = elt; DL_APPEND(hashlin->head, elt); elt->obj = obj; - } else if (ret < 0) { + } + else if (ret < 0) { goto e0; } } return true; - e0: +e0: return false; } -void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old, - const ucl_object_t *new) +void ucl_hash_replace(ucl_hash_t *hashlin, const ucl_object_t *old, + const ucl_object_t *new) { khiter_t k; int ret; @@ -270,12 +271,12 @@ void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old, if (hashlin->caseless) { khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) - hashlin->hash; - k = kh_put (ucl_hash_caseless_node, h, old, &ret); + hashlin->hash; + k = kh_put(ucl_hash_caseless_node, h, old, &ret); if (ret == 0) { elt = kh_value(h, k); - kh_del (ucl_hash_caseless_node, h, k); - k = kh_put (ucl_hash_caseless_node, h, new, &ret); + kh_del(ucl_hash_caseless_node, h, k); + k = kh_put(ucl_hash_caseless_node, h, new, &ret); nelt = UCL_ALLOC(sizeof(*nelt)); nelt->obj = new; kh_value(h, k) = nelt; @@ -285,12 +286,12 @@ void ucl_hash_replace (ucl_hash_t* hashlin, const ucl_object_t *old, } else { khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) - hashlin->hash; - k = kh_put (ucl_hash_node, h, old, &ret); + hashlin->hash; + k = kh_put(ucl_hash_node, h, old, &ret); if (ret == 0) { - elt = kh_value (h, k); - kh_del (ucl_hash_node, h, k); - k = kh_put (ucl_hash_node, h, new, &ret); + elt = kh_value(h, k); + kh_del(ucl_hash_node, h, k); + k = kh_put(ucl_hash_node, h, new, &ret); nelt = UCL_ALLOC(sizeof(*nelt)); nelt->obj = new; kh_value(h, k) = nelt; @@ -304,12 +305,15 @@ struct ucl_hash_real_iter { const struct ucl_hash_elt *cur; }; -#define UHI_SETERR(ep, ern) {if (ep != NULL) *ep = (ern);} +#define UHI_SETERR(ep, ern) \ + { \ + if (ep != NULL) *ep = (ern); \ + } -const void* -ucl_hash_iterate2 (ucl_hash_t *hashlin, ucl_hash_iter_t *iter, int *ep) +const void * +ucl_hash_iterate2(ucl_hash_t *hashlin, ucl_hash_iter_t *iter, int *ep) { - struct ucl_hash_real_iter *it = (struct ucl_hash_real_iter *)(*iter); + struct ucl_hash_real_iter *it = (struct ucl_hash_real_iter *) (*iter); const ucl_object_t *ret = NULL; if (hashlin == NULL) { @@ -318,7 +322,7 @@ ucl_hash_iterate2 (ucl_hash_t *hashlin, ucl_hash_iter_t *iter, int *ep) } if (it == NULL) { - it = UCL_ALLOC (sizeof (*it)); + it = UCL_ALLOC(sizeof(*it)); if (it == NULL) { UHI_SETERR(ep, ENOMEM); @@ -334,7 +338,7 @@ ucl_hash_iterate2 (ucl_hash_t *hashlin, ucl_hash_iter_t *iter, int *ep) it->cur = it->cur->next; } else { - UCL_FREE (sizeof (*it), it); + UCL_FREE(sizeof(*it), it); *iter = NULL; return NULL; } @@ -344,17 +348,16 @@ ucl_hash_iterate2 (ucl_hash_t *hashlin, ucl_hash_iter_t *iter, int *ep) return ret; } -bool -ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter) +bool ucl_hash_iter_has_next(ucl_hash_t *hashlin, ucl_hash_iter_t iter) { - struct ucl_hash_real_iter *it = (struct ucl_hash_real_iter *)(iter); + struct ucl_hash_real_iter *it = (struct ucl_hash_real_iter *) (iter); return it->cur != NULL; } -const ucl_object_t* -ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen) +const ucl_object_t * +ucl_hash_search(ucl_hash_t *hashlin, const char *key, unsigned keylen) { khiter_t k; const ucl_object_t *ret = NULL; @@ -370,20 +373,20 @@ ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen) if (hashlin->caseless) { khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) - hashlin->hash; + hashlin->hash; - k = kh_get (ucl_hash_caseless_node, h, &search); - if (k != kh_end (h)) { - elt = kh_value (h, k); + k = kh_get(ucl_hash_caseless_node, h, &search); + if (k != kh_end(h)) { + elt = kh_value(h, k); ret = elt->obj; } } else { khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) - hashlin->hash; - k = kh_get (ucl_hash_node, h, &search); - if (k != kh_end (h)) { - elt = kh_value (h, k); + hashlin->hash; + k = kh_get(ucl_hash_node, h, &search); + if (k != kh_end(h)) { + elt = kh_value(h, k); ret = elt->obj; } } @@ -391,8 +394,7 @@ ucl_hash_search (ucl_hash_t* hashlin, const char *key, unsigned keylen) return ret; } -void -ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj) +void ucl_hash_delete(ucl_hash_t *hashlin, const ucl_object_t *obj) { khiter_t k; struct ucl_hash_elt *elt; @@ -403,46 +405,46 @@ ucl_hash_delete (ucl_hash_t* hashlin, const ucl_object_t *obj) if (hashlin->caseless) { khash_t(ucl_hash_caseless_node) *h = (khash_t(ucl_hash_caseless_node) *) - hashlin->hash; + hashlin->hash; - k = kh_get (ucl_hash_caseless_node, h, obj); - if (k != kh_end (h)) { - elt = kh_value (h, k); + k = kh_get(ucl_hash_caseless_node, h, obj); + if (k != kh_end(h)) { + elt = kh_value(h, k); DL_DELETE(hashlin->head, elt); - kh_del (ucl_hash_caseless_node, h, k); + kh_del(ucl_hash_caseless_node, h, k); UCL_FREE(sizeof(*elt), elt); } } else { khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) - hashlin->hash; - k = kh_get (ucl_hash_node, h, obj); - if (k != kh_end (h)) { - elt = kh_value (h, k); + hashlin->hash; + k = kh_get(ucl_hash_node, h, obj); + if (k != kh_end(h)) { + elt = kh_value(h, k); DL_DELETE(hashlin->head, elt); - kh_del (ucl_hash_node, h, k); + kh_del(ucl_hash_node, h, k); UCL_FREE(sizeof(*elt), elt); } } } -bool -ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz) +bool ucl_hash_reserve(ucl_hash_t *hashlin, size_t sz) { if (hashlin == NULL) { return false; } - if (sz > kh_size((khash_t(ucl_hash_node) *)hashlin->hash)) { + if (sz > kh_size((khash_t(ucl_hash_node) *) hashlin->hash)) { if (hashlin->caseless) { khash_t(ucl_hash_caseless_node) *h = (khash_t( - ucl_hash_caseless_node) *) - hashlin->hash; - kh_resize (ucl_hash_caseless_node, h, sz * 2); - } else { + ucl_hash_caseless_node) *) + hashlin->hash; + kh_resize(ucl_hash_caseless_node, h, sz * 2); + } + else { khash_t(ucl_hash_node) *h = (khash_t(ucl_hash_node) *) - hashlin->hash; - kh_resize (ucl_hash_node, h, sz * 2); + hashlin->hash; + kh_resize(ucl_hash_node, h, sz * 2); } } @@ -450,33 +452,32 @@ ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz) } static int -ucl_hash_cmp_icase (const void *a, const void *b) +ucl_hash_cmp_icase(const void *a, const void *b) { - const struct ucl_hash_elt *oa = (const struct ucl_hash_elt *)a, - *ob = (const struct ucl_hash_elt *)b; + const struct ucl_hash_elt *oa = (const struct ucl_hash_elt *) a, + *ob = (const struct ucl_hash_elt *) b; if (oa->obj->keylen == ob->obj->keylen) { - return rspamd_lc_cmp (oa->obj->key, ob->obj->key, oa->obj->keylen); + return rspamd_lc_cmp(oa->obj->key, ob->obj->key, oa->obj->keylen); } - return ((int)(oa->obj->keylen)) - ob->obj->keylen; + return ((int) (oa->obj->keylen)) - ob->obj->keylen; } static int -ucl_hash_cmp_case_sens (const void *a, const void *b) +ucl_hash_cmp_case_sens(const void *a, const void *b) { - const struct ucl_hash_elt *oa = (const struct ucl_hash_elt *)a, - *ob = (const struct ucl_hash_elt *)b; + const struct ucl_hash_elt *oa = (const struct ucl_hash_elt *) a, + *ob = (const struct ucl_hash_elt *) b; if (oa->obj->keylen == ob->obj->keylen) { - return memcmp (oa->obj->key, ob->obj->key, oa->obj->keylen); + return memcmp(oa->obj->key, ob->obj->key, oa->obj->keylen); } - return ((int)(oa->obj->keylen)) - ob->obj->keylen; + return ((int) (oa->obj->keylen)) - ob->obj->keylen; } -void -ucl_hash_sort (ucl_hash_t *hashlin, enum ucl_object_keys_sort_flags fl) +void ucl_hash_sort(ucl_hash_t *hashlin, enum ucl_object_keys_sort_flags fl) { if (fl & UCL_SORT_KEYS_ICASE) { @@ -489,9 +490,10 @@ ucl_hash_sort (ucl_hash_t *hashlin, enum ucl_object_keys_sort_flags fl) if (fl & UCL_SORT_KEYS_RECURSIVE) { struct ucl_hash_elt *elt; - DL_FOREACH(hashlin->head, elt) { - if (ucl_object_type (elt->obj) == UCL_OBJECT) { - ucl_hash_sort (elt->obj->value.ov, fl); + DL_FOREACH(hashlin->head, elt) + { + if (ucl_object_type(elt->obj) == UCL_OBJECT) { + ucl_hash_sort(elt->obj->value.ov, fl); } } } |