From 2262bc18fe5872dd4454eb5acfe83b306e7c4761 Mon Sep 17 00:00:00 2001 From: Gao feng Date: Thu, 17 Sep 2015 14:39:10 +0800 Subject: [PATCH] accept read raw data from write message It's difficult to encode the json with special characters such as '"', '>'... encoding and unicoding these characters will change the length of json message, it's a pain to get bytes contains these characters from json. This patch allows to read the bytes contains special character outside the json. for example: {"container":"c5f67934326d2ecef59fee62148bc9237e2481cd803cc0760a6406da15f4ee60","file":"/tmp/aaa"}sssssss Signed-off-by: Gao feng --- src/init.c | 2 +- src/jsmn.c | 15 +++++++++++---- src/parse.c | 26 +++++++++++--------------- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/init.c b/src/init.c index f4b3d5c..82c72b7 100644 --- a/src/init.c +++ b/src/init.c @@ -780,7 +780,7 @@ static int hyper_cmd_write_file(char *json, int length) } sprintf(path, "/tmp/hyper/%s/root/%s/", c->id, c->rootfs); - fprintf(stdout, "root directory for container is %s\n",path); + fprintf(stdout, "write file %s, data len %d\n", writter.file, writter.len); /* TODO: wait for container finishing setup root */ if (chroot(path) < 0) { diff --git a/src/jsmn.c b/src/jsmn.c index b1c3ebb..027b8aa 100644 --- a/src/jsmn.c +++ b/src/jsmn.c @@ -164,14 +164,18 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, int i; jsmntok_t *token; int count = 0; + int num = 0; + int out = 0; - for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) { + for (; parser->pos < len && js[parser->pos] != '\0' && !out; parser->pos++) { char c; jsmntype_t type; c = js[parser->pos]; switch (c) { - case '{': case '[': + case '{': + num++; + case '[': count++; if (tokens == NULL) break; @@ -189,7 +193,10 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, token->start = parser->pos; parser->toksuper = parser->toknext - 1; break; - case '}': case ']': + case '}': + if (--num <= 0) + out = 1; + case ']': if (tokens == NULL) break; type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY); @@ -221,7 +228,7 @@ jsmnerr_t jsmn_parse(jsmn_parser *parser, const char *js, size_t len, parser->toksuper = -1; token->end = parser->pos + 1; - break; + break; } } /* Error if unmatched closing bracket */ diff --git a/src/parse.c b/src/parse.c index 55fcf02..3e046bf 100644 --- a/src/parse.c +++ b/src/parse.c @@ -645,12 +645,22 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length jsmn_init(&p); n = jsmn_parse(&p, json, length, toks, toks_num); - if (n < 0) { + /* Must be json first */ + if (n <= 0) { fprintf(stdout, "jsmn parse failed, n is %d\n", n); ret = -1; goto out; } + writter->len = length - toks[0].end; + writter->data = malloc(writter->len); + + if (writter->data == NULL) + goto fail; + + memcpy(writter->data, json + toks[0].end, writter->len); + fprintf(stdout, "writefile get data len %d %s\n", writter->len, writter->data); + for (i = 0; i < n; i++) { jsmntok_t *t = &toks[i]; @@ -669,24 +679,10 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length writter->file = strdup(json_token_str(json, &toks[i])); fprintf(stdout, "writefile get file %s\n", writter->file); - } else if (json_token_streq(json, t, "data")) { - if (i++ == n) - goto fail; - - writter->len = toks[i].end - toks[i].start + 1; - writter->data = malloc(writter->len); - - if (writter->data == NULL) - goto fail; - - memcpy(writter->data, json + toks[i].start, writter->len); - writter->data[writter->len - 1] = '\0'; - fprintf(stdout, "writefile get data len %d %s\n", writter->len, writter->data); } else { fprintf(stdout, "in writefile incorrect %s\n", json_token_str(json, &toks[i])); } } - out: free(toks); return ret;