Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Update to latest libucl from github
Baptiste Daroussin committed 6 years ago
commit 6aea7863884802b51e3053eb524fa98ad947a0a9
parent 5903a49
7 files changed +166 -21
modified external/libucl/include/ucl.h
@@ -800,6 +800,19 @@ UCL_EXTERN int ucl_object_compare_qsort (const ucl_object_t **o1,
UCL_EXTERN void ucl_object_array_sort (ucl_object_t *ar,
		int (*cmp)(const ucl_object_t **o1, const ucl_object_t **o2));

+
enum ucl_object_keys_sort_flags {
+
	UCL_SORT_KEYS_DEFAULT = 0,
+
	UCL_SORT_KEYS_ICASE = (1u << 0u),
+
	UCL_SORT_KEYS_RECURSIVE = (1u << 1u),
+
};
+
/***
+
 * Sorts keys in object in place
+
 * @param obj
+
 * @param how
+
 */
+
UCL_EXTERN void ucl_object_sort_keys (ucl_object_t *obj,
+
		enum ucl_object_keys_sort_flags how);
+

/**
 * Get the priority for specific UCL object
 * @param obj any ucl object
modified external/libucl/lua/lua_ucl.c
@@ -343,24 +343,30 @@ ucl_object_lua_fromtable (lua_State *L, int idx, ucl_string_flags_t flags)
	}

	if (!found_mt) {
-
		/* Check for array */
+
		/* Check for array (it is all inefficient) */
		lua_pushnil (L);
+

		while (lua_next (L, idx) != 0) {
-
			if (lua_type (L, -2) == LUA_TNUMBER) {
-
				double num = lua_tonumber (L, -2);
+
			lua_pushvalue (L, -2);
+

+
			if (lua_type (L, -1) == LUA_TNUMBER) {
+
				double num = lua_tonumber (L, -1);
				if (num == (int) num) {
					if (num > max) {
						max = num;
					}
-
				} else {
+
				}
+
				else {
					/* Keys are not integer */
					is_array = false;
				}
-
			} else {
+
			}
+
			else {
				/* Keys are not numeric */
				is_array = false;
			}
-
			lua_pop (L, 1);
+

+
			lua_pop (L, 2);
			nelts ++;
		}
	}
@@ -807,8 +813,8 @@ struct _rspamd_lua_text {

/***
 * @method parser:parse_text(input)
-
 * Parse UCL object from file.
-
 * @param {string} input string to parse
+
 * Parse UCL object from text object (Rspamd specific).
+
 * @param {rspamd_text} input text to parse
 * @return {bool[, string]} if res is `true` then file has been parsed successfully, otherwise an error string is also returned
 */
static int
@@ -820,7 +826,7 @@ lua_ucl_parser_parse_text (lua_State *L)
	int ret = 2;

	parser = lua_ucl_parser_get (L, 1);
-
	t = luaL_checkudata (L, 2, "rspamd{text}");
+
	t = lua_touserdata (L, 2);

	if (lua_type (L, 3) == LUA_TSTRING) {
		type = lua_ucl_str_to_parse_type (lua_tostring (L, 3));
@@ -1368,6 +1374,7 @@ 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) {
@@ -1397,10 +1404,22 @@ lua_ucl_to_format (lua_State *L)
				format = UCL_EMIT_MSGPACK;
			}
		}
+

+
		if (lua_isboolean (L, 3)) {
+
			sort = lua_toboolean (L, 3);
+
		}
	}

	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);
+
			}
+
		}
+

		lua_ucl_to_string (L, obj, format);
		ucl_object_unref (obj);
	}
modified external/libucl/src/ucl_chartable.h
@@ -27,7 +27,7 @@
#include "ucl_internal.h"

static const unsigned int ucl_chartable[256] = {
-
UCL_CHARACTER_VALUE_END, UCL_CHARACTER_DENIED, UCL_CHARACTER_DENIED,
+
UCL_CHARACTER_JSON_UNSAFE|UCL_CHARACTER_VALUE_END|UCL_CHARACTER_UCL_UNSAFE, UCL_CHARACTER_DENIED, UCL_CHARACTER_DENIED,
UCL_CHARACTER_DENIED, UCL_CHARACTER_DENIED, UCL_CHARACTER_DENIED,
UCL_CHARACTER_DENIED, UCL_CHARACTER_DENIED,
UCL_CHARACTER_JSON_UNSAFE|UCL_CHARACTER_UCL_UNSAFE,
modified external/libucl/src/ucl_hash.c
@@ -543,3 +543,95 @@ bool ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz)
e0:
	return false;
}
+

+
int
+
ucl_lc_cmp (const char *s, const char *d, size_t l)
+
{
+
	unsigned int fp, i;
+
	unsigned char c1, c2, c3, c4;
+
	union {
+
		unsigned char c[4];
+
		uint32_t n;
+
	} cmp1, cmp2;
+
	size_t leftover = l % 4;
+
	int ret = 0;
+

+
	fp = l - leftover;
+

+
	for (i = 0; i != fp; i += 4) {
+
		c1 = s[i], c2 = s[i + 1], c3 = s[i + 2], c4 = s[i + 3];
+
		cmp1.c[0] = lc_map[c1];
+
		cmp1.c[1] = lc_map[c2];
+
		cmp1.c[2] = lc_map[c3];
+
		cmp1.c[3] = lc_map[c4];
+

+
		c1 = d[i], c2 = d[i + 1], c3 = d[i + 2], c4 = d[i + 3];
+
		cmp2.c[0] = lc_map[c1];
+
		cmp2.c[1] = lc_map[c2];
+
		cmp2.c[2] = lc_map[c3];
+
		cmp2.c[3] = lc_map[c4];
+

+
		if (cmp1.n != cmp2.n) {
+
			return cmp1.n - cmp2.n;
+
		}
+
	}
+

+
	while (leftover > 0) {
+
		if (lc_map[(unsigned char)s[i]] != lc_map[(unsigned char)d[i]]) {
+
			return s[i] - d[i];
+
		}
+

+
		leftover--;
+
		i++;
+
	}
+

+
	return ret;
+
}
+

+
static int
+
ucl_hash_cmp_icase (const void *a, const void *b)
+
{
+
	const ucl_object_t *oa = *(const ucl_object_t **)a,
+
			*ob = *(const ucl_object_t **)b;
+

+
	if (oa->keylen == ob->keylen) {
+
		return ucl_lc_cmp (oa->key, ob->key, oa->keylen);
+
	}
+

+
	return ((int)(oa->keylen)) - ob->keylen;
+
}
+

+
static int
+
ucl_hash_cmp_case_sens (const void *a, const void *b)
+
{
+
	const ucl_object_t *oa = *(const ucl_object_t **)a,
+
			*ob = *(const ucl_object_t **)b;
+

+
	if (oa->keylen == ob->keylen) {
+
		return memcmp (oa->key, ob->key, oa->keylen);
+
	}
+

+
	return ((int)(oa->keylen)) - ob->keylen;
+
}
+

+
void
+
ucl_hash_sort (ucl_hash_t *hashlin, enum ucl_object_keys_sort_flags fl)
+
{
+

+
	if (fl & UCL_SORT_KEYS_ICASE) {
+
		qsort (hashlin->ar.a, hashlin->ar.n, sizeof (ucl_object_t *),
+
				ucl_hash_cmp_icase);
+
	}
+
	else {
+
		qsort (hashlin->ar.a, hashlin->ar.n, sizeof (ucl_object_t *),
+
				ucl_hash_cmp_case_sens);
+
	}
+

+
	if (fl & UCL_SORT_KEYS_RECURSIVE) {
+
		for (size_t i = 0; i < hashlin->ar.n; i ++) {
+
			if (ucl_object_type (hashlin->ar.a[i]) == UCL_OBJECT) {
+
				ucl_hash_sort (hashlin->ar.a[i]->value.ov, fl);
+
			}
+
		}
+
	}
+
}

\ No newline at end of file
modified external/libucl/src/ucl_hash.h
@@ -103,5 +103,7 @@ bool ucl_hash_iter_has_next (ucl_hash_t *hashlin, ucl_hash_iter_t iter);
 * @param hashlin
 */
bool ucl_hash_reserve (ucl_hash_t *hashlin, size_t sz);
+
void ucl_hash_sort (ucl_hash_t *hashlin, enum ucl_object_keys_sort_flags fl);
+


#endif
modified external/libucl/src/ucl_parser.c
@@ -2366,20 +2366,22 @@ ucl_state_machine (struct ucl_parser *parser)

				p = chunk->pos;

-
				if (*p == '[') {
-
					parser->state = UCL_STATE_VALUE;
-
					ucl_chunk_skipc (chunk, p);
-
					seen_obrace = true;
-
				}
-
				else {
-

-
					if (*p == '{') {
+
				if (p < chunk->end) {
+
					if (*p == '[') {
+
						parser->state = UCL_STATE_VALUE;
						ucl_chunk_skipc (chunk, p);
-
						parser->state = UCL_STATE_KEY_OBRACE;
						seen_obrace = true;
					}
					else {
-
						parser->state = UCL_STATE_KEY;
+

+
						if (*p == '{') {
+
							ucl_chunk_skipc (chunk, p);
+
							parser->state = UCL_STATE_KEY_OBRACE;
+
							seen_obrace = true;
+
						}
+
						else {
+
							parser->state = UCL_STATE_KEY;
+
						}
					}
				}

modified external/libucl/src/ucl_util.c
@@ -2244,6 +2244,7 @@ ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags
				if (ucl_test_character (*p, UCL_CHARACTER_JSON_UNSAFE | UCL_CHARACTER_WHITESPACE_UNSAFE)) {
					switch (*p) {
					case '\v':
+
					case '\0':
						escaped_len += 5;
						break;
					case ' ':
@@ -2279,6 +2280,14 @@ ucl_object_fromstring_common (const char *str, size_t len, enum ucl_string_flags
							*d++ = '\\';
							*d = 'f';
							break;
+
						case '\0':
+
							*d++ = '\\';
+
							*d++ = 'u';
+
							*d++ = '0';
+
							*d++ = '0';
+
							*d++ = '0';
+
							*d   = '0';
+
							break;
						case '\v':
							*d++ = '\\';
							*d++ = 'u';
@@ -2749,7 +2758,7 @@ enum ucl_safe_iter_flags {
	UCL_ITERATE_FLAG_EXCEPTION
};

-
const char safe_iter_magic[4] = {'u', 'i', 't', 'e'};
+
static const char safe_iter_magic[4] = {'u', 'i', 't', 'e'};
struct ucl_object_safe_iter {
	char magic[4]; /* safety check */
	uint32_t flags;
@@ -3746,6 +3755,14 @@ ucl_object_array_sort (ucl_object_t *ar,
			(int (*)(const void *, const void *))cmp);
}

+
void ucl_object_sort_keys (ucl_object_t *obj,
+
		enum ucl_object_keys_sort_flags how)
+
{
+
	if (obj != NULL && obj->type == UCL_OBJECT) {
+
		ucl_hash_sort (obj->value.ov, how);
+
	}
+
}
+

#define PRIOBITS 4

unsigned int