Browse Source

json: routine to parse a uint64_t.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
06a25887da
  1. 23
      daemon/json.c
  2. 9
      daemon/json.h

23
daemon/json.c

@ -1,6 +1,7 @@
/* JSON core and helpers */ /* JSON core and helpers */
#include "json.h" #include "json.h"
#include <assert.h> #include <assert.h>
#include <ccan/build_assert/build_assert.h>
#include <ccan/str/hex/hex.h> #include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h> #include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
@ -39,8 +40,8 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str)
return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0; return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0;
} }
bool json_tok_number(const char *buffer, const jsmntok_t *tok, bool json_tok_u64(const char *buffer, const jsmntok_t *tok,
unsigned int *num) uint64_t *num)
{ {
char *end; char *end;
unsigned long l; unsigned long l;
@ -49,6 +50,7 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok,
if (end != buffer + tok->end) if (end != buffer + tok->end)
return false; return false;
BUILD_ASSERT(sizeof(l) >= sizeof(*num));
*num = l; *num = l;
/* Check for overflow */ /* Check for overflow */
@ -59,7 +61,22 @@ bool json_tok_number(const char *buffer, const jsmntok_t *tok,
return false; return false;
return true; return true;
} }
bool json_tok_number(const char *buffer, const jsmntok_t *tok,
unsigned int *num)
{
uint64_t u64;
if (!json_tok_u64(buffer, tok, &u64))
return false;
*num = u64;
/* Just in case it doesn't fit. */
if (*num != u64)
return false;
return true;
}
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok) bool json_tok_is_null(const char *buffer, const jsmntok_t *tok)
{ {

9
daemon/json.h

@ -1,9 +1,10 @@
#ifndef LIGHTNING_DAEMON_JSON_H #ifndef LIGHTNING_DAEMON_JSON_H
#define LIGHTNING_DAEMON_JSON_H #define LIGHTNING_DAEMON_JSON_H
#include "config.h" #include "config.h"
#include "stdbool.h"
#include "stdlib.h"
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#define JSMN_STRICT 1 #define JSMN_STRICT 1
# include "jsmn/jsmn.h" # include "jsmn/jsmn.h"
@ -23,6 +24,10 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str);
bool json_tok_number(const char *buffer, const jsmntok_t *tok, bool json_tok_number(const char *buffer, const jsmntok_t *tok,
unsigned int *num); unsigned int *num);
/* Extract number from this (may be a string, or a number literal) */
bool json_tok_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num);
/* Is this the null primitive? */ /* Is this the null primitive? */
bool json_tok_is_null(const char *buffer, const jsmntok_t *tok); bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);

Loading…
Cancel
Save