diff options
author | Vipul Rahane <vipul@runtime.io> | 2016-04-25 17:33:21 -0700 |
---|---|---|
committer | Vipul Rahane <vipul@runtime.io> | 2016-05-16 14:16:53 -0700 |
commit | ed16b3f2e21809b68f45b10777cfb1331be0f697 (patch) | |
tree | b947fa10e3eb8efb28db3b2b4c669a4f1abc251e | |
parent | 57e945dc1b08d5d0f1f886e9e328abc0b25511d4 (diff) |
Fix json encoding
- Changing atoi to strtoull and strtoll as atoi return only 32 bit
values. strtoll and strtoull return 64bit values which we encode our
inetegers as.
- Changing a few other places where json encoding should use 64 bit
values instead of 32 bit.
-rw-r--r-- | libs/json/include/json/json.h | 8 | ||||
-rw-r--r-- | libs/json/src/json_decode.c | 20 |
2 files changed, 15 insertions, 13 deletions
diff --git a/libs/json/include/json/json.h b/libs/json/include/json/json.h index 1b97d1b1..84601260 100644 --- a/libs/json/include/json/json.h +++ b/libs/json/include/json/json.h @@ -128,8 +128,8 @@ typedef enum { } json_type; struct json_enum_t { - char *name; - int value; + char *name; + long long int value; }; struct json_array_t { @@ -146,10 +146,10 @@ struct json_array_t { int storelen; } strings; struct { - int *store; + long long int *store; } integers; struct { - unsigned int *store; + long long unsigned int *store; } uintegers; struct { double *store; diff --git a/libs/json/src/json_decode.c b/libs/json/src/json_decode.c index 2dcde6f0..0d81c0d3 100644 --- a/libs/json/src/json_decode.c +++ b/libs/json/src/json_decode.c @@ -152,10 +152,10 @@ json_internal_read_object(struct json_buffer *jb, const struct json_attr_t *attr if (lptr != NULL) { switch (cursor->type) { case t_integer: - memcpy(lptr, &cursor->dflt.integer, sizeof(int)); + memcpy(lptr, &cursor->dflt.integer, sizeof(long long int)); break; case t_uinteger: - memcpy(lptr, &cursor->dflt.uinteger, sizeof(unsigned int)); + memcpy(lptr, &cursor->dflt.uinteger, sizeof(long long unsigned int)); break; case t_real: memcpy(lptr, &cursor->dflt.real, sizeof(double)); @@ -396,19 +396,21 @@ json_internal_read_object(struct json_buffer *jb, const struct json_attr_t *attr } return JSON_ERR_BADENUM; foundit: - (void)snprintf(valbuf, sizeof(valbuf), "%d", mp->value); + (void)snprintf(valbuf, sizeof(valbuf), "%lld", mp->value); } lptr = json_target_address(cursor, parent, offset); if (lptr != NULL) { switch (cursor->type) { case t_integer: { - int tmp = atoi(valbuf); - memcpy(lptr, &tmp, sizeof(int)); + long long int tmp = + (long long int)strtoll(valbuf, NULL, 10); + memcpy(lptr, &tmp, sizeof(long long int)); } break; case t_uinteger: { - unsigned int tmp = (unsigned int)atoi(valbuf); - memcpy(lptr, &tmp, sizeof(unsigned int)); + long long unsigned int tmp = + (long long unsigned int)strtoull(valbuf, NULL, 10); + memcpy(lptr, &tmp, sizeof(long long unsigned int)); } break; case t_real: { @@ -548,7 +550,7 @@ json_read_array(struct json_buffer *jb, const struct json_array_t *arr) n = jb->jb_readn(jb, valbuf, sizeof(valbuf)-1); valbuf[n] = '\0'; - arr->arr.integers.store[offset] = (int)strtol(valbuf, &ep, 0); + arr->arr.integers.store[offset] = (long long int)strtoll(valbuf, &ep, 0); if (ep == valbuf) { return JSON_ERR_BADNUM; } else { @@ -562,7 +564,7 @@ json_read_array(struct json_buffer *jb, const struct json_array_t *arr) n = jb->jb_readn(jb, valbuf, sizeof(valbuf)-1); valbuf[n] = '\0'; - arr->arr.uintegers.store[offset] = (unsigned int)strtoul(valbuf, &ep, 0); + arr->arr.uintegers.store[offset] = (long long unsigned int)strtoull(valbuf, &ep, 0); if (ep == valbuf) { return JSON_ERR_BADNUM; } else { |