summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--runtime/lookup.c50
-rw-r--r--runtime/lookup.h9
-rw-r--r--runtime/typedefs.h2
3 files changed, 60 insertions, 1 deletions
diff --git a/runtime/lookup.c b/runtime/lookup.c
index c581da6c..7ee2eba3 100644
--- a/runtime/lookup.c
+++ b/runtime/lookup.c
@@ -98,6 +98,54 @@ lookupInitCnf(lookup_tables_t *lu_tabs)
}
+/* comparison function for qsort() and bsearch() string array compare
+ * this is for the string lookup table type
+ */
+static int
+qs_arrcmp_strtab(const void *s1, const void *s2)
+{
+ return ustrcmp(((lookup_string_tab_etry_t*)s1)->key, ((lookup_string_tab_etry_t*)s2)->key);
+}
+
+rsRetVal
+lookupBuildTable(lookup_t *pThis, struct json_object *jroot)
+{
+ struct json_object *jversion, *jnomatch, *jtype, *jtab;
+ struct json_object *jrow, *jindex, *jvalue;
+ uint32_t i;
+ uint32_t maxStrSize;
+ DEFiRet;
+
+ jversion = json_object_object_get(jroot, "version");
+ jnomatch = json_object_object_get(jroot, "nomatch");
+ jtype = json_object_object_get(jroot, "type");
+ jtab = json_object_object_get(jroot, "table");
+DBGPRINTF("DDDD: version: %d, nomatch %s, type %s, table: '%s'\n",
+ json_object_get_int(jversion),
+ json_object_get_string(jnomatch),
+ json_object_get_string(jtype),
+ json_object_get_string(jtab));
+ pThis->nmemb = json_object_array_length(jtab);
+ CHKmalloc(pThis->d.strtab = malloc(pThis->nmemb * sizeof(lookup_string_tab_etry_t)));
+
+ maxStrSize = 0;
+ for(i = 0 ; i < pThis->nmemb ; ++i) {
+ jrow = json_object_array_get_idx(jtab, i);
+ jindex = json_object_object_get(jrow, "index");
+ jvalue = json_object_object_get(jrow, "value");
+ CHKmalloc(pThis->d.strtab[i].key = (uchar*) strdup(json_object_get_string(jindex)));
+ CHKmalloc(pThis->d.strtab[i].val = (uchar*) strdup(json_object_get_string(jvalue)));
+ maxStrSize += ustrlen(pThis->d.strtab[i].val);
+ }
+
+ qsort(pThis->d.strtab, pThis->nmemb, sizeof(lookup_string_tab_etry_t), qs_arrcmp_strtab);
+dbgprintf("DDDD: table loaded (max size %u):\n", maxStrSize);
+for(i = 0 ; i < pThis->nmemb ; ++i)
+ dbgprintf("key: '%s', val: '%s'\n", pThis->d.strtab[i].key, pThis->d.strtab[i].val);
+finalize_it:
+ RETiRet;
+}
+
/* note: widely-deployed json_c 0.9 does NOT support incremental
* parsing. In order to keep compatible with e.g. Ubuntu 12.04LTS,
* we read the file into one big memory buffer and parse it at once.
@@ -116,7 +164,6 @@ lookupReadFile(lookup_t *pThis)
int fd;
ssize_t nread;
struct stat sb;
- enum json_tokener_error jerr;
DEFiRet;
@@ -158,6 +205,7 @@ dbgprintf("DDDD: read buffer of %lld bytes: '%s'\n", (long long) sb.st_size, iob
}
/* got json object, now populate our own in-memory structure */
+ CHKiRet(lookupBuildTable(pThis, json));
finalize_it:
free(iobuf);
diff --git a/runtime/lookup.h b/runtime/lookup.h
index 8dd7c23e..5dd02283 100644
--- a/runtime/lookup.h
+++ b/runtime/lookup.h
@@ -26,9 +26,18 @@ struct lookup_tables_s {
lookup_t *last; /* points to the last element of the template list */
};
+struct lookup_string_tab_etry_s {
+ uchar *key;
+ uchar *val;
+};
+
struct lookup_s {
uchar *name;
uchar *filename;
+ uint32_t nmemb;
+ union {
+ lookup_string_tab_etry_t *strtab;
+ } d;
lookup_t *next;
};
diff --git a/runtime/typedefs.h b/runtime/typedefs.h
index c962d0a8..a929b30c 100644
--- a/runtime/typedefs.h
+++ b/runtime/typedefs.h
@@ -25,6 +25,7 @@
*/
#ifndef INCLUDED_TYPEDEFS_H
#define INCLUDED_TYPEDEFS_H
+#include <stdint.h>
#if defined(__FreeBSD__)
#include <sys/types.h>
#endif
@@ -100,6 +101,7 @@ typedef struct outchannels_s outchannels_t;
typedef struct modConfData_s modConfData_t;
typedef struct instanceConf_s instanceConf_t;
typedef struct ratelimit_s ratelimit_t;
+typedef struct lookup_string_tab_etry_s lookup_string_tab_etry_t;
typedef struct lookup_tables_s lookup_tables_t;
typedef struct lookup_s lookup_t;
typedef struct action_s action_t;