this is fork and i will modify for STM32

Fork of AWS-test by Pierre-Marie Ancèle

Committer:
bcjun@aname.co.kr
Date:
Fri Aug 04 15:34:19 2017 +0900
Revision:
3:1ef624d94403
Parent:
0:cd5404401c2f
add esp82660-driver for NUCLEO_F401RE target.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
peyo 0:cd5404401c2f 1 /*
peyo 0:cd5404401c2f 2 * Copyright (c) 2010 Serge A. Zaitsev
peyo 0:cd5404401c2f 3 *
peyo 0:cd5404401c2f 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
peyo 0:cd5404401c2f 5 * of this software and associated documentation files (the "Software"), to deal
peyo 0:cd5404401c2f 6 * in the Software without restriction, including without limitation the rights
peyo 0:cd5404401c2f 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
peyo 0:cd5404401c2f 8 * copies of the Software, and to permit persons to whom the Software is
peyo 0:cd5404401c2f 9 * furnished to do so, subject to the following conditions:
peyo 0:cd5404401c2f 10 *
peyo 0:cd5404401c2f 11 * The above copyright notice and this permission notice shall be included in
peyo 0:cd5404401c2f 12 * all copies or substantial portions of the Software.
peyo 0:cd5404401c2f 13 *
peyo 0:cd5404401c2f 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
peyo 0:cd5404401c2f 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
peyo 0:cd5404401c2f 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
peyo 0:cd5404401c2f 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
peyo 0:cd5404401c2f 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
peyo 0:cd5404401c2f 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
peyo 0:cd5404401c2f 20 * THE SOFTWARE.
peyo 0:cd5404401c2f 21 */
peyo 0:cd5404401c2f 22
peyo 0:cd5404401c2f 23 /**
peyo 0:cd5404401c2f 24 * @file jsmn.c
peyo 0:cd5404401c2f 25 * @brief Implementation of the JSMN (Jasmine) JSON parser.
peyo 0:cd5404401c2f 26 *
peyo 0:cd5404401c2f 27 * For more information on JSMN:
peyo 0:cd5404401c2f 28 * @see http://zserge.com/jsmn.html
peyo 0:cd5404401c2f 29 */
peyo 0:cd5404401c2f 30
peyo 0:cd5404401c2f 31 #include "jsmn.h"
peyo 0:cd5404401c2f 32
peyo 0:cd5404401c2f 33 /**
peyo 0:cd5404401c2f 34 * Allocates a fresh unused token from the token pull.
peyo 0:cd5404401c2f 35 */
peyo 0:cd5404401c2f 36 static jsmntok_t *jsmn_alloc_token(jsmn_parser *parser,
peyo 0:cd5404401c2f 37 jsmntok_t *tokens, size_t num_tokens) {
peyo 0:cd5404401c2f 38 jsmntok_t *tok;
peyo 0:cd5404401c2f 39 if (parser->toknext >= num_tokens) {
peyo 0:cd5404401c2f 40 return NULL;
peyo 0:cd5404401c2f 41 }
peyo 0:cd5404401c2f 42 tok = &tokens[parser->toknext++];
peyo 0:cd5404401c2f 43 tok->start = tok->end = -1;
peyo 0:cd5404401c2f 44 tok->size = 0;
peyo 0:cd5404401c2f 45 #ifdef JSMN_PARENT_LINKS
peyo 0:cd5404401c2f 46 tok->parent = -1;
peyo 0:cd5404401c2f 47 #endif
peyo 0:cd5404401c2f 48 return tok;
peyo 0:cd5404401c2f 49 }
peyo 0:cd5404401c2f 50
peyo 0:cd5404401c2f 51 /**
peyo 0:cd5404401c2f 52 * Fills token type and boundaries.
peyo 0:cd5404401c2f 53 */
peyo 0:cd5404401c2f 54 static void jsmn_fill_token(jsmntok_t *token, jsmntype_t type,
peyo 0:cd5404401c2f 55 int start, int end) {
peyo 0:cd5404401c2f 56 token->type = type;
peyo 0:cd5404401c2f 57 token->start = start;
peyo 0:cd5404401c2f 58 token->end = end;
peyo 0:cd5404401c2f 59 token->size = 0;
peyo 0:cd5404401c2f 60 }
peyo 0:cd5404401c2f 61
peyo 0:cd5404401c2f 62 /**
peyo 0:cd5404401c2f 63 * Fills next available token with JSON primitive.
peyo 0:cd5404401c2f 64 */
peyo 0:cd5404401c2f 65 static int jsmn_parse_primitive(jsmn_parser *parser, const char *js,
peyo 0:cd5404401c2f 66 size_t len, jsmntok_t *tokens, size_t num_tokens) {
peyo 0:cd5404401c2f 67 jsmntok_t *token;
peyo 0:cd5404401c2f 68 int start;
peyo 0:cd5404401c2f 69
peyo 0:cd5404401c2f 70 start = parser->pos;
peyo 0:cd5404401c2f 71
peyo 0:cd5404401c2f 72 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
peyo 0:cd5404401c2f 73 switch (js[parser->pos]) {
peyo 0:cd5404401c2f 74 #ifndef JSMN_STRICT
peyo 0:cd5404401c2f 75 /* In strict mode primitive must be followed by "," or "}" or "]" */
peyo 0:cd5404401c2f 76 case ':':
peyo 0:cd5404401c2f 77 #endif
peyo 0:cd5404401c2f 78 case '\t' : case '\r' : case '\n' : case ' ' :
peyo 0:cd5404401c2f 79 case ',' : case ']' : case '}' :
peyo 0:cd5404401c2f 80 goto found;
peyo 0:cd5404401c2f 81 }
peyo 0:cd5404401c2f 82 if (js[parser->pos] < 32 || js[parser->pos] >= 127) {
peyo 0:cd5404401c2f 83 parser->pos = start;
peyo 0:cd5404401c2f 84 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 85 }
peyo 0:cd5404401c2f 86 }
peyo 0:cd5404401c2f 87 #ifdef JSMN_STRICT
peyo 0:cd5404401c2f 88 /* In strict mode primitive must be followed by a comma/object/array */
peyo 0:cd5404401c2f 89 parser->pos = start;
peyo 0:cd5404401c2f 90 return JSMN_ERROR_PART;
peyo 0:cd5404401c2f 91 #endif
peyo 0:cd5404401c2f 92
peyo 0:cd5404401c2f 93 found:
peyo 0:cd5404401c2f 94 if (tokens == NULL) {
peyo 0:cd5404401c2f 95 parser->pos--;
peyo 0:cd5404401c2f 96 return 0;
peyo 0:cd5404401c2f 97 }
peyo 0:cd5404401c2f 98 token = jsmn_alloc_token(parser, tokens, num_tokens);
peyo 0:cd5404401c2f 99 if (token == NULL) {
peyo 0:cd5404401c2f 100 parser->pos = start;
peyo 0:cd5404401c2f 101 return JSMN_ERROR_NOMEM;
peyo 0:cd5404401c2f 102 }
peyo 0:cd5404401c2f 103 jsmn_fill_token(token, JSMN_PRIMITIVE, start, parser->pos);
peyo 0:cd5404401c2f 104 #ifdef JSMN_PARENT_LINKS
peyo 0:cd5404401c2f 105 token->parent = parser->toksuper;
peyo 0:cd5404401c2f 106 #endif
peyo 0:cd5404401c2f 107 parser->pos--;
peyo 0:cd5404401c2f 108 return 0;
peyo 0:cd5404401c2f 109 }
peyo 0:cd5404401c2f 110
peyo 0:cd5404401c2f 111 /**
peyo 0:cd5404401c2f 112 * Fills next token with JSON string.
peyo 0:cd5404401c2f 113 */
peyo 0:cd5404401c2f 114 static int jsmn_parse_string(jsmn_parser *parser, const char *js,
peyo 0:cd5404401c2f 115 size_t len, jsmntok_t *tokens, size_t num_tokens) {
peyo 0:cd5404401c2f 116 jsmntok_t *token;
peyo 0:cd5404401c2f 117
peyo 0:cd5404401c2f 118 int start = parser->pos;
peyo 0:cd5404401c2f 119
peyo 0:cd5404401c2f 120 parser->pos++;
peyo 0:cd5404401c2f 121
peyo 0:cd5404401c2f 122 /* Skip starting quote */
peyo 0:cd5404401c2f 123 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
peyo 0:cd5404401c2f 124 char c = js[parser->pos];
peyo 0:cd5404401c2f 125
peyo 0:cd5404401c2f 126 /* Quote: end of string */
peyo 0:cd5404401c2f 127 if (c == '\"') {
peyo 0:cd5404401c2f 128 if (tokens == NULL) {
peyo 0:cd5404401c2f 129 return 0;
peyo 0:cd5404401c2f 130 }
peyo 0:cd5404401c2f 131 token = jsmn_alloc_token(parser, tokens, num_tokens);
peyo 0:cd5404401c2f 132 if (token == NULL) {
peyo 0:cd5404401c2f 133 parser->pos = start;
peyo 0:cd5404401c2f 134 return JSMN_ERROR_NOMEM;
peyo 0:cd5404401c2f 135 }
peyo 0:cd5404401c2f 136 jsmn_fill_token(token, JSMN_STRING, start+1, parser->pos);
peyo 0:cd5404401c2f 137 #ifdef JSMN_PARENT_LINKS
peyo 0:cd5404401c2f 138 token->parent = parser->toksuper;
peyo 0:cd5404401c2f 139 #endif
peyo 0:cd5404401c2f 140 return 0;
peyo 0:cd5404401c2f 141 }
peyo 0:cd5404401c2f 142
peyo 0:cd5404401c2f 143 /* Backslash: Quoted symbol expected */
peyo 0:cd5404401c2f 144 if (c == '\\' && parser->pos + 1 < len) {
peyo 0:cd5404401c2f 145 int i;
peyo 0:cd5404401c2f 146 parser->pos++;
peyo 0:cd5404401c2f 147 switch (js[parser->pos]) {
peyo 0:cd5404401c2f 148 /* Allowed escaped symbols */
peyo 0:cd5404401c2f 149 case '\"': case '/' : case '\\' : case 'b' :
peyo 0:cd5404401c2f 150 case 'f' : case 'r' : case 'n' : case 't' :
peyo 0:cd5404401c2f 151 break;
peyo 0:cd5404401c2f 152 /* Allows escaped symbol \uXXXX */
peyo 0:cd5404401c2f 153 case 'u':
peyo 0:cd5404401c2f 154 parser->pos++;
peyo 0:cd5404401c2f 155 for(i = 0; i < 4 && parser->pos < len && js[parser->pos] != '\0'; i++) {
peyo 0:cd5404401c2f 156 /* If it isn't a hex character we have an error */
peyo 0:cd5404401c2f 157 if(!((js[parser->pos] >= 48 && js[parser->pos] <= 57) || /* 0-9 */
peyo 0:cd5404401c2f 158 (js[parser->pos] >= 65 && js[parser->pos] <= 70) || /* A-F */
peyo 0:cd5404401c2f 159 (js[parser->pos] >= 97 && js[parser->pos] <= 102))) { /* a-f */
peyo 0:cd5404401c2f 160 parser->pos = start;
peyo 0:cd5404401c2f 161 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 162 }
peyo 0:cd5404401c2f 163 parser->pos++;
peyo 0:cd5404401c2f 164 }
peyo 0:cd5404401c2f 165 parser->pos--;
peyo 0:cd5404401c2f 166 break;
peyo 0:cd5404401c2f 167 /* Unexpected symbol */
peyo 0:cd5404401c2f 168 default:
peyo 0:cd5404401c2f 169 parser->pos = start;
peyo 0:cd5404401c2f 170 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 171 }
peyo 0:cd5404401c2f 172 }
peyo 0:cd5404401c2f 173 }
peyo 0:cd5404401c2f 174 parser->pos = start;
peyo 0:cd5404401c2f 175 return JSMN_ERROR_PART;
peyo 0:cd5404401c2f 176 }
peyo 0:cd5404401c2f 177
peyo 0:cd5404401c2f 178 /**
peyo 0:cd5404401c2f 179 * Parse JSON string and fill tokens.
peyo 0:cd5404401c2f 180 */
peyo 0:cd5404401c2f 181 int jsmn_parse(jsmn_parser *parser, const char *js, size_t len,
peyo 0:cd5404401c2f 182 jsmntok_t *tokens, unsigned int num_tokens) {
peyo 0:cd5404401c2f 183 int r;
peyo 0:cd5404401c2f 184 int i;
peyo 0:cd5404401c2f 185 jsmntok_t *token;
peyo 0:cd5404401c2f 186 int count = parser->toknext;
peyo 0:cd5404401c2f 187
peyo 0:cd5404401c2f 188 for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
peyo 0:cd5404401c2f 189 char c;
peyo 0:cd5404401c2f 190 jsmntype_t type;
peyo 0:cd5404401c2f 191
peyo 0:cd5404401c2f 192 c = js[parser->pos];
peyo 0:cd5404401c2f 193 switch (c) {
peyo 0:cd5404401c2f 194 case '{': case '[':
peyo 0:cd5404401c2f 195 count++;
peyo 0:cd5404401c2f 196 if (tokens == NULL) {
peyo 0:cd5404401c2f 197 break;
peyo 0:cd5404401c2f 198 }
peyo 0:cd5404401c2f 199 token = jsmn_alloc_token(parser, tokens, num_tokens);
peyo 0:cd5404401c2f 200 if (token == NULL)
peyo 0:cd5404401c2f 201 return JSMN_ERROR_NOMEM;
peyo 0:cd5404401c2f 202 if (parser->toksuper != -1) {
peyo 0:cd5404401c2f 203 tokens[parser->toksuper].size++;
peyo 0:cd5404401c2f 204 #ifdef JSMN_PARENT_LINKS
peyo 0:cd5404401c2f 205 token->parent = parser->toksuper;
peyo 0:cd5404401c2f 206 #endif
peyo 0:cd5404401c2f 207 }
peyo 0:cd5404401c2f 208 token->type = (c == '{' ? JSMN_OBJECT : JSMN_ARRAY);
peyo 0:cd5404401c2f 209 token->start = parser->pos;
peyo 0:cd5404401c2f 210 parser->toksuper = parser->toknext - 1;
peyo 0:cd5404401c2f 211 break;
peyo 0:cd5404401c2f 212 case '}': case ']':
peyo 0:cd5404401c2f 213 if (tokens == NULL)
peyo 0:cd5404401c2f 214 break;
peyo 0:cd5404401c2f 215 type = (c == '}' ? JSMN_OBJECT : JSMN_ARRAY);
peyo 0:cd5404401c2f 216 #ifdef JSMN_PARENT_LINKS
peyo 0:cd5404401c2f 217 if (parser->toknext < 1) {
peyo 0:cd5404401c2f 218 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 219 }
peyo 0:cd5404401c2f 220 token = &tokens[parser->toknext - 1];
peyo 0:cd5404401c2f 221 for (;;) {
peyo 0:cd5404401c2f 222 if (token->start != -1 && token->end == -1) {
peyo 0:cd5404401c2f 223 if (token->type != type) {
peyo 0:cd5404401c2f 224 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 225 }
peyo 0:cd5404401c2f 226 token->end = parser->pos + 1;
peyo 0:cd5404401c2f 227 parser->toksuper = token->parent;
peyo 0:cd5404401c2f 228 break;
peyo 0:cd5404401c2f 229 }
peyo 0:cd5404401c2f 230 if (token->parent == -1) {
peyo 0:cd5404401c2f 231 break;
peyo 0:cd5404401c2f 232 }
peyo 0:cd5404401c2f 233 token = &tokens[token->parent];
peyo 0:cd5404401c2f 234 }
peyo 0:cd5404401c2f 235 #else
peyo 0:cd5404401c2f 236 for (i = parser->toknext - 1; i >= 0; i--) {
peyo 0:cd5404401c2f 237 token = &tokens[i];
peyo 0:cd5404401c2f 238 if (token->start != -1 && token->end == -1) {
peyo 0:cd5404401c2f 239 if (token->type != type) {
peyo 0:cd5404401c2f 240 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 241 }
peyo 0:cd5404401c2f 242 parser->toksuper = -1;
peyo 0:cd5404401c2f 243 token->end = parser->pos + 1;
peyo 0:cd5404401c2f 244 break;
peyo 0:cd5404401c2f 245 }
peyo 0:cd5404401c2f 246 }
peyo 0:cd5404401c2f 247 /* Error if unmatched closing bracket */
peyo 0:cd5404401c2f 248 if (i == -1) return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 249 for (; i >= 0; i--) {
peyo 0:cd5404401c2f 250 token = &tokens[i];
peyo 0:cd5404401c2f 251 if (token->start != -1 && token->end == -1) {
peyo 0:cd5404401c2f 252 parser->toksuper = i;
peyo 0:cd5404401c2f 253 break;
peyo 0:cd5404401c2f 254 }
peyo 0:cd5404401c2f 255 }
peyo 0:cd5404401c2f 256 #endif
peyo 0:cd5404401c2f 257 break;
peyo 0:cd5404401c2f 258 case '\"':
peyo 0:cd5404401c2f 259 r = jsmn_parse_string(parser, js, len, tokens, num_tokens);
peyo 0:cd5404401c2f 260 if (r < 0) return r;
peyo 0:cd5404401c2f 261 count++;
peyo 0:cd5404401c2f 262 if (parser->toksuper != -1 && tokens != NULL)
peyo 0:cd5404401c2f 263 tokens[parser->toksuper].size++;
peyo 0:cd5404401c2f 264 break;
peyo 0:cd5404401c2f 265 case '\t' : case '\r' : case '\n' : case ' ':
peyo 0:cd5404401c2f 266 break;
peyo 0:cd5404401c2f 267 case ':':
peyo 0:cd5404401c2f 268 parser->toksuper = parser->toknext - 1;
peyo 0:cd5404401c2f 269 break;
peyo 0:cd5404401c2f 270 case ',':
peyo 0:cd5404401c2f 271 if (tokens != NULL && parser->toksuper != -1 &&
peyo 0:cd5404401c2f 272 tokens[parser->toksuper].type != JSMN_ARRAY &&
peyo 0:cd5404401c2f 273 tokens[parser->toksuper].type != JSMN_OBJECT) {
peyo 0:cd5404401c2f 274 #ifdef JSMN_PARENT_LINKS
peyo 0:cd5404401c2f 275 parser->toksuper = tokens[parser->toksuper].parent;
peyo 0:cd5404401c2f 276 #else
peyo 0:cd5404401c2f 277 for (i = parser->toknext - 1; i >= 0; i--) {
peyo 0:cd5404401c2f 278 if (tokens[i].type == JSMN_ARRAY || tokens[i].type == JSMN_OBJECT) {
peyo 0:cd5404401c2f 279 if (tokens[i].start != -1 && tokens[i].end == -1) {
peyo 0:cd5404401c2f 280 parser->toksuper = i;
peyo 0:cd5404401c2f 281 break;
peyo 0:cd5404401c2f 282 }
peyo 0:cd5404401c2f 283 }
peyo 0:cd5404401c2f 284 }
peyo 0:cd5404401c2f 285 #endif
peyo 0:cd5404401c2f 286 }
peyo 0:cd5404401c2f 287 break;
peyo 0:cd5404401c2f 288 #ifdef JSMN_STRICT
peyo 0:cd5404401c2f 289 /* In strict mode primitives are: numbers and booleans */
peyo 0:cd5404401c2f 290 case '-': case '0': case '1' : case '2': case '3' : case '4':
peyo 0:cd5404401c2f 291 case '5': case '6': case '7' : case '8': case '9':
peyo 0:cd5404401c2f 292 case 't': case 'f': case 'n' :
peyo 0:cd5404401c2f 293 /* And they must not be keys of the object */
peyo 0:cd5404401c2f 294 if (tokens != NULL && parser->toksuper != -1) {
peyo 0:cd5404401c2f 295 jsmntok_t *t = &tokens[parser->toksuper];
peyo 0:cd5404401c2f 296 if (t->type == JSMN_OBJECT ||
peyo 0:cd5404401c2f 297 (t->type == JSMN_STRING && t->size != 0)) {
peyo 0:cd5404401c2f 298 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 299 }
peyo 0:cd5404401c2f 300 }
peyo 0:cd5404401c2f 301 #else
peyo 0:cd5404401c2f 302 /* In non-strict mode every unquoted value is a primitive */
peyo 0:cd5404401c2f 303 default:
peyo 0:cd5404401c2f 304 #endif
peyo 0:cd5404401c2f 305 r = jsmn_parse_primitive(parser, js, len, tokens, num_tokens);
peyo 0:cd5404401c2f 306 if (r < 0) return r;
peyo 0:cd5404401c2f 307 count++;
peyo 0:cd5404401c2f 308 if (parser->toksuper != -1 && tokens != NULL)
peyo 0:cd5404401c2f 309 tokens[parser->toksuper].size++;
peyo 0:cd5404401c2f 310 break;
peyo 0:cd5404401c2f 311
peyo 0:cd5404401c2f 312 #ifdef JSMN_STRICT
peyo 0:cd5404401c2f 313 /* Unexpected char in strict mode */
peyo 0:cd5404401c2f 314 default:
peyo 0:cd5404401c2f 315 return JSMN_ERROR_INVAL;
peyo 0:cd5404401c2f 316 #endif
peyo 0:cd5404401c2f 317 }
peyo 0:cd5404401c2f 318 }
peyo 0:cd5404401c2f 319
peyo 0:cd5404401c2f 320 if (tokens != NULL) {
peyo 0:cd5404401c2f 321 for (i = parser->toknext - 1; i >= 0; i--) {
peyo 0:cd5404401c2f 322 /* Unmatched opened object or array */
peyo 0:cd5404401c2f 323 if (tokens[i].start != -1 && tokens[i].end == -1) {
peyo 0:cd5404401c2f 324 return JSMN_ERROR_PART;
peyo 0:cd5404401c2f 325 }
peyo 0:cd5404401c2f 326 }
peyo 0:cd5404401c2f 327 }
peyo 0:cd5404401c2f 328
peyo 0:cd5404401c2f 329 return count;
peyo 0:cd5404401c2f 330 }
peyo 0:cd5404401c2f 331
peyo 0:cd5404401c2f 332 /**
peyo 0:cd5404401c2f 333 * Creates a new parser based over a given buffer with an array of tokens
peyo 0:cd5404401c2f 334 * available.
peyo 0:cd5404401c2f 335 */
peyo 0:cd5404401c2f 336 void jsmn_init(jsmn_parser *parser) {
peyo 0:cd5404401c2f 337 parser->pos = 0;
peyo 0:cd5404401c2f 338 parser->toknext = 0;
peyo 0:cd5404401c2f 339 parser->toksuper = -1;
peyo 0:cd5404401c2f 340 }
peyo 0:cd5404401c2f 341