aboutsummaryrefslogtreecommitdiffstats
path: root/src/json
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rambler-co.ru>2013-01-09 16:35:03 +0400
committerVsevolod Stakhov <vsevolod@rambler-co.ru>2013-01-09 16:35:03 +0400
commit823c263b9d417a9d28344c469b253e0dfe76e640 (patch)
tree9b87cc08829c7a66c5b4181848da8baf53fba415 /src/json
parent86d52c83c8cb219e503704e4ee762a7633431e36 (diff)
downloadrspamd-823c263b9d417a9d28344c469b253e0dfe76e640.tar.gz
rspamd-823c263b9d417a9d28344c469b253e0dfe76e640.zip
Add saving of actions, symbols and maps.
Diffstat (limited to 'src/json')
-rw-r--r--src/json/jansson.h1
-rw-r--r--src/json/load.c58
2 files changed, 59 insertions, 0 deletions
diff --git a/src/json/jansson.h b/src/json/jansson.h
index 0d859f384..a66fc363f 100644
--- a/src/json/jansson.h
+++ b/src/json/jansson.h
@@ -120,6 +120,7 @@ typedef struct {
json_t *json_loads(const char *input, json_error_t *error);
json_t *json_loadf(FILE *input, json_error_t *error);
json_t *json_load_file(const char *path, json_error_t *error);
+json_t *json_load_evbuffer(struct evbuffer *evb, json_error_t *error);
#define JSON_INDENT(n) (n & 0xFF)
diff --git a/src/json/load.c b/src/json/load.c
index 07d0b9f8b..0f0968e9e 100644
--- a/src/json/load.c
+++ b/src/json/load.c
@@ -856,3 +856,61 @@ json_load_file (const char *path, json_error_t * error)
fclose (fp);
return result;
}
+
+
+typedef struct evbuffer_data_s {
+ const char *data;
+ gsize len;
+ guint pos;
+} evbuffer_data_t;
+
+
+static int
+evbuffer_get (void *data)
+{
+ evbuffer_data_t *stream = (evbuffer_data_t *) data;
+
+ if (stream->pos >= stream->len) {
+ return EOF;
+ }
+
+ return *(stream->data + stream->pos++);
+}
+
+static int
+evbuffer_eof (void *data)
+{
+ evbuffer_data_t *stream = (evbuffer_data_t *) data;
+
+ return stream->pos >= stream->len;
+}
+
+json_t *
+json_load_evbuffer (struct evbuffer *evb, json_error_t *error)
+{
+ evbuffer_data_t stream_data;
+ lex_t lex;
+ json_t *result;
+
+ stream_data.data = EVBUFFER_DATA (evb);
+ stream_data.pos = 0;
+ stream_data.len = EVBUFFER_LENGTH (evb);
+
+ if (lex_init (&lex, evbuffer_get, evbuffer_eof, (void *)&stream_data))
+ return NULL;
+
+ result = parse_json (&lex, error);
+ if (!result)
+ goto out;
+
+ lex_scan (&lex, error);
+ if (lex.token != TOKEN_EOF) {
+ error_set (error, &lex, "end of file expected");
+ json_decref (result);
+ result = NULL;
+ }
+
+ out:
+ lex_close (&lex);
+ return result;
+}