Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3410 lines
94 KiB
3410 lines
94 KiB
10 years ago
|
|
||
|
#line 1 "upb/json/parser.rl"
|
||
|
/*
|
||
10 years ago
|
** upb::json::Parser (upb_json_parser)
|
||
|
**
|
||
|
** A parser that uses the Ragel State Machine Compiler to generate
|
||
|
** the finite automata.
|
||
|
**
|
||
|
** Ragel only natively handles regular languages, but we can manually
|
||
|
** program it a bit to handle context-free languages like JSON, by using
|
||
|
** the "fcall" and "fret" constructs.
|
||
|
**
|
||
|
** This parser can handle the basics, but needs several things to be fleshed
|
||
|
** out:
|
||
|
**
|
||
|
** - handling of unicode escape sequences (including high surrogate pairs).
|
||
|
** - properly check and report errors for unknown fields, stack overflow,
|
||
|
** improper array nesting (or lack of nesting).
|
||
|
** - handling of base64 sequences with padding characters.
|
||
|
** - handling of push-back (non-success returns from sink functions).
|
||
|
** - handling of keys/escape-sequences/etc that span input buffers.
|
||
|
*/
|
||
10 years ago
|
|
||
6 years ago
|
#include <ctype.h>
|
||
10 years ago
|
#include <errno.h>
|
||
8 years ago
|
#include <float.h>
|
||
|
#include <math.h>
|
||
9 years ago
|
#include <stdint.h>
|
||
6 years ago
|
#include <stdio.h>
|
||
9 years ago
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
10 years ago
|
|
||
6 years ago
|
#include <time.h>
|
||
|
|
||
10 years ago
|
#include "upb/json/parser.h"
|
||
6 years ago
|
#include "upb/pb/encoder.h"
|
||
10 years ago
|
|
||
6 years ago
|
#include "upb/port_def.inc"
|
||
|
|
||
10 years ago
|
#define UPB_JSON_MAX_DEPTH 64
|
||
|
|
||
6 years ago
|
/* Type of value message */
|
||
|
enum {
|
||
|
VALUE_NULLVALUE = 0,
|
||
|
VALUE_NUMBERVALUE = 1,
|
||
|
VALUE_STRINGVALUE = 2,
|
||
|
VALUE_BOOLVALUE = 3,
|
||
|
VALUE_STRUCTVALUE = 4,
|
||
|
VALUE_LISTVALUE = 5
|
||
|
};
|
||
|
|
||
6 years ago
|
/* Forward declare */
|
||
|
static bool is_top_level(upb_json_parser *p);
|
||
6 years ago
|
static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type);
|
||
|
static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type);
|
||
6 years ago
|
|
||
|
static bool is_number_wrapper_object(upb_json_parser *p);
|
||
|
static bool does_number_wrapper_start(upb_json_parser *p);
|
||
|
static bool does_number_wrapper_end(upb_json_parser *p);
|
||
|
|
||
|
static bool is_string_wrapper_object(upb_json_parser *p);
|
||
|
static bool does_string_wrapper_start(upb_json_parser *p);
|
||
|
static bool does_string_wrapper_end(upb_json_parser *p);
|
||
|
|
||
6 years ago
|
static bool does_fieldmask_start(upb_json_parser *p);
|
||
|
static bool does_fieldmask_end(upb_json_parser *p);
|
||
|
static void start_fieldmask_object(upb_json_parser *p);
|
||
|
static void end_fieldmask_object(upb_json_parser *p);
|
||
|
|
||
6 years ago
|
static void start_wrapper_object(upb_json_parser *p);
|
||
|
static void end_wrapper_object(upb_json_parser *p);
|
||
|
|
||
6 years ago
|
static void start_value_object(upb_json_parser *p, int value_type);
|
||
|
static void end_value_object(upb_json_parser *p);
|
||
|
|
||
|
static void start_listvalue_object(upb_json_parser *p);
|
||
|
static void end_listvalue_object(upb_json_parser *p);
|
||
|
|
||
|
static void start_structvalue_object(upb_json_parser *p);
|
||
|
static void end_structvalue_object(upb_json_parser *p);
|
||
|
|
||
6 years ago
|
static void start_object(upb_json_parser *p);
|
||
|
static void end_object(upb_json_parser *p);
|
||
|
|
||
6 years ago
|
static void start_any_object(upb_json_parser *p, const char *ptr);
|
||
|
static bool end_any_object(upb_json_parser *p, const char *ptr);
|
||
|
|
||
6 years ago
|
static bool start_subobject(upb_json_parser *p);
|
||
|
static void end_subobject(upb_json_parser *p);
|
||
|
|
||
6 years ago
|
static void start_member(upb_json_parser *p);
|
||
|
static void end_member(upb_json_parser *p);
|
||
|
static bool end_membername(upb_json_parser *p);
|
||
|
|
||
6 years ago
|
static void start_any_member(upb_json_parser *p, const char *ptr);
|
||
|
static void end_any_member(upb_json_parser *p, const char *ptr);
|
||
|
static bool end_any_membername(upb_json_parser *p);
|
||
|
|
||
|
size_t parse(void *closure, const void *hd, const char *buf, size_t size,
|
||
|
const upb_bufhandle *handle);
|
||
|
static bool end(void *closure, const void *hd);
|
||
|
|
||
6 years ago
|
static const char eof_ch = 'e';
|
||
|
|
||
6 years ago
|
/* stringsink */
|
||
|
typedef struct {
|
||
|
upb_byteshandler handler;
|
||
|
upb_bytessink sink;
|
||
|
char *ptr;
|
||
|
size_t len, size;
|
||
|
} upb_stringsink;
|
||
|
|
||
|
|
||
|
static void *stringsink_start(void *_sink, const void *hd, size_t size_hint) {
|
||
|
upb_stringsink *sink = _sink;
|
||
|
sink->len = 0;
|
||
|
UPB_UNUSED(hd);
|
||
|
UPB_UNUSED(size_hint);
|
||
|
return sink;
|
||
|
}
|
||
|
|
||
|
static size_t stringsink_string(void *_sink, const void *hd, const char *ptr,
|
||
|
size_t len, const upb_bufhandle *handle) {
|
||
|
upb_stringsink *sink = _sink;
|
||
|
size_t new_size = sink->size;
|
||
|
|
||
|
UPB_UNUSED(hd);
|
||
|
UPB_UNUSED(handle);
|
||
|
|
||
|
while (sink->len + len > new_size) {
|
||
|
new_size *= 2;
|
||
|
}
|
||
|
|
||
|
if (new_size != sink->size) {
|
||
|
sink->ptr = realloc(sink->ptr, new_size);
|
||
|
sink->size = new_size;
|
||
|
}
|
||
|
|
||
|
memcpy(sink->ptr + sink->len, ptr, len);
|
||
|
sink->len += len;
|
||
|
|
||
|
return len;
|
||
|
}
|
||
|
|
||
|
void upb_stringsink_init(upb_stringsink *sink) {
|
||
|
upb_byteshandler_init(&sink->handler);
|
||
|
upb_byteshandler_setstartstr(&sink->handler, stringsink_start, NULL);
|
||
|
upb_byteshandler_setstring(&sink->handler, stringsink_string, NULL);
|
||
|
|
||
|
upb_bytessink_reset(&sink->sink, &sink->handler, sink);
|
||
|
|
||
|
sink->size = 32;
|
||
|
sink->ptr = malloc(sink->size);
|
||
|
sink->len = 0;
|
||
|
}
|
||
|
|
||
|
void upb_stringsink_uninit(upb_stringsink *sink) { free(sink->ptr); }
|
||
|
|
||
|
typedef struct {
|
||
|
/* For encoding Any value field in binary format. */
|
||
6 years ago
|
upb_handlercache *encoder_handlercache;
|
||
6 years ago
|
upb_stringsink stringsink;
|
||
|
|
||
|
/* For decoding Any value field in json format. */
|
||
6 years ago
|
upb_json_codecache *parser_codecache;
|
||
6 years ago
|
upb_sink sink;
|
||
6 years ago
|
upb_json_parser *parser;
|
||
6 years ago
|
|
||
|
/* Mark the range of uninterpreted values in json input before type url. */
|
||
|
const char *before_type_url_start;
|
||
|
const char *before_type_url_end;
|
||
|
|
||
|
/* Mark the range of uninterpreted values in json input after type url. */
|
||
|
const char *after_type_url_start;
|
||
|
} upb_jsonparser_any_frame;
|
||
|
|
||
10 years ago
|
typedef struct {
|
||
|
upb_sink sink;
|
||
|
|
||
10 years ago
|
/* The current message in which we're parsing, and the field whose value we're
|
||
|
* expecting next. */
|
||
10 years ago
|
const upb_msgdef *m;
|
||
|
const upb_fielddef *f;
|
||
|
|
||
9 years ago
|
/* The table mapping json name to fielddef for this message. */
|
||
6 years ago
|
const upb_strtable *name_table;
|
||
9 years ago
|
|
||
6 years ago
|
/* We are in a repeated-field context. We need this flag to decide whether to
|
||
|
* handle the array as a normal repeated field or a
|
||
|
* google.protobuf.ListValue/google.protobuf.Value. */
|
||
|
bool is_repeated;
|
||
|
|
||
10 years ago
|
/* We are in a repeated-field context, ready to emit mapentries as
|
||
|
* submessages. This flag alters the start-of-object (open-brace) behavior to
|
||
|
* begin a sequence of mapentry messages rather than a single submessage. */
|
||
10 years ago
|
bool is_map;
|
||
|
|
||
10 years ago
|
/* We are in a map-entry message context. This flag is set when parsing the
|
||
|
* value field of a single map entry and indicates to all value-field parsers
|
||
|
* (subobjects, strings, numbers, and bools) that the map-entry submessage
|
||
|
* should end as soon as the value is parsed. */
|
||
10 years ago
|
bool is_mapentry;
|
||
|
|
||
10 years ago
|
/* If |is_map| or |is_mapentry| is true, |mapfield| refers to the parent
|
||
|
* message's map field that we're currently parsing. This differs from |f|
|
||
|
* because |f| is the field in the *current* message (i.e., the map-entry
|
||
|
* message itself), not the parent's field that leads to this map. */
|
||
10 years ago
|
const upb_fielddef *mapfield;
|
||
6 years ago
|
|
||
6 years ago
|
/* We are in an Any message context. This flag is set when parsing the Any
|
||
|
* message and indicates to all field parsers (subobjects, strings, numbers,
|
||
|
* and bools) that the parsed field should be serialized as binary data or
|
||
|
* cached (type url not found yet). */
|
||
|
bool is_any;
|
||
|
|
||
|
/* The type of packed message in Any. */
|
||
|
upb_jsonparser_any_frame *any_frame;
|
||
|
|
||
6 years ago
|
/* True if the field to be parsed is unknown. */
|
||
|
bool is_unknown_field;
|
||
10 years ago
|
} upb_jsonparser_frame;
|
||
|
|
||
6 years ago
|
static void init_frame(upb_jsonparser_frame* frame) {
|
||
|
frame->m = NULL;
|
||
|
frame->f = NULL;
|
||
|
frame->name_table = NULL;
|
||
|
frame->is_repeated = false;
|
||
|
frame->is_map = false;
|
||
|
frame->is_mapentry = false;
|
||
|
frame->mapfield = NULL;
|
||
|
frame->is_any = false;
|
||
|
frame->any_frame = NULL;
|
||
|
frame->is_unknown_field = false;
|
||
|
}
|
||
|
|
||
10 years ago
|
struct upb_json_parser {
|
||
6 years ago
|
upb_arena *arena;
|
||
9 years ago
|
const upb_json_parsermethod *method;
|
||
10 years ago
|
upb_bytessink input_;
|
||
|
|
||
10 years ago
|
/* Stack to track the JSON scopes we are in. */
|
||
10 years ago
|
upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH];
|
||
|
upb_jsonparser_frame *top;
|
||
|
upb_jsonparser_frame *limit;
|
||
|
|
||
6 years ago
|
upb_status *status;
|
||
10 years ago
|
|
||
10 years ago
|
/* Ragel's internal parsing stack for the parsing state machine. */
|
||
10 years ago
|
int current_state;
|
||
|
int parser_stack[UPB_JSON_MAX_DEPTH];
|
||
|
int parser_top;
|
||
|
|
||
10 years ago
|
/* The handle for the current buffer. */
|
||
10 years ago
|
const upb_bufhandle *handle;
|
||
|
|
||
10 years ago
|
/* Accumulate buffer. See details in parser.rl. */
|
||
10 years ago
|
const char *accumulated;
|
||
|
size_t accumulated_len;
|
||
|
char *accumulate_buf;
|
||
|
size_t accumulate_buf_size;
|
||
|
|
||
10 years ago
|
/* Multi-part text data. See details in parser.rl. */
|
||
10 years ago
|
int multipart_state;
|
||
|
upb_selector_t string_selector;
|
||
|
|
||
10 years ago
|
/* Input capture. See details in parser.rl. */
|
||
10 years ago
|
const char *capture;
|
||
|
|
||
10 years ago
|
/* Intermediate result of parsing a unicode escape sequence. */
|
||
10 years ago
|
uint32_t digit;
|
||
7 years ago
|
|
||
6 years ago
|
/* For resolve type url in Any. */
|
||
|
const upb_symtab *symtab;
|
||
|
|
||
7 years ago
|
/* Whether to proceed if unknown field is met. */
|
||
|
bool ignore_json_unknown;
|
||
6 years ago
|
|
||
|
/* Cache for parsing timestamp due to base and zone are handled in different
|
||
|
* handlers. */
|
||
|
struct tm tm;
|
||
10 years ago
|
};
|
||
|
|
||
6 years ago
|
static upb_jsonparser_frame* start_jsonparser_frame(upb_json_parser *p) {
|
||
|
upb_jsonparser_frame *inner;
|
||
|
inner = p->top + 1;
|
||
|
init_frame(inner);
|
||
|
return inner;
|
||
|
}
|
||
|
|
||
6 years ago
|
struct upb_json_codecache {
|
||
6 years ago
|
upb_arena *arena;
|
||
6 years ago
|
upb_inttable methods; /* upb_msgdef* -> upb_json_parsermethod* */
|
||
|
};
|
||
9 years ago
|
|
||
6 years ago
|
struct upb_json_parsermethod {
|
||
|
const upb_json_codecache *cache;
|
||
9 years ago
|
upb_byteshandler input_handler_;
|
||
|
|
||
6 years ago
|
/* Maps json_name -> fielddef */
|
||
|
upb_strtable name_table;
|
||
9 years ago
|
};
|
||
|
|
||
10 years ago
|
#define PARSER_CHECK_RETURN(x) if (!(x)) return false
|
||
10 years ago
|
|
||
6 years ago
|
static upb_jsonparser_any_frame *json_parser_any_frame_new(
|
||
|
upb_json_parser *p) {
|
||
|
upb_jsonparser_any_frame *frame;
|
||
|
|
||
6 years ago
|
frame = upb_arena_malloc(p->arena, sizeof(upb_jsonparser_any_frame));
|
||
6 years ago
|
|
||
|
frame->encoder_handlercache = upb_pb_encoder_newcache();
|
||
|
frame->parser_codecache = upb_json_codecache_new();
|
||
6 years ago
|
frame->parser = NULL;
|
||
|
frame->before_type_url_start = NULL;
|
||
|
frame->before_type_url_end = NULL;
|
||
|
frame->after_type_url_start = NULL;
|
||
6 years ago
|
|
||
|
upb_stringsink_init(&frame->stringsink);
|
||
|
|
||
|
return frame;
|
||
6 years ago
|
}
|
||
|
|
||
|
static void json_parser_any_frame_set_payload_type(
|
||
|
upb_json_parser *p,
|
||
|
upb_jsonparser_any_frame *frame,
|
||
|
const upb_msgdef *payload_type) {
|
||
6 years ago
|
const upb_handlers *h;
|
||
|
const upb_json_parsermethod *parser_method;
|
||
|
upb_pb_encoder *encoder;
|
||
|
|
||
6 years ago
|
/* Initialize encoder. */
|
||
6 years ago
|
h = upb_handlercache_get(frame->encoder_handlercache, payload_type);
|
||
6 years ago
|
encoder = upb_pb_encoder_create(p->arena, h, frame->stringsink.sink);
|
||
6 years ago
|
|
||
|
/* Initialize parser. */
|
||
6 years ago
|
parser_method = upb_json_codecache_get(frame->parser_codecache, payload_type);
|
||
|
upb_sink_reset(&frame->sink, h, encoder);
|
||
6 years ago
|
frame->parser =
|
||
|
upb_json_parser_create(p->arena, parser_method, p->symtab, frame->sink,
|
||
|
p->status, p->ignore_json_unknown);
|
||
6 years ago
|
}
|
||
|
|
||
|
static void json_parser_any_frame_free(upb_jsonparser_any_frame *frame) {
|
||
6 years ago
|
upb_handlercache_free(frame->encoder_handlercache);
|
||
|
upb_json_codecache_free(frame->parser_codecache);
|
||
6 years ago
|
upb_stringsink_uninit(&frame->stringsink);
|
||
|
}
|
||
|
|
||
|
static bool json_parser_any_frame_has_type_url(
|
||
|
upb_jsonparser_any_frame *frame) {
|
||
6 years ago
|
return frame->parser != NULL;
|
||
6 years ago
|
}
|
||
|
|
||
|
static bool json_parser_any_frame_has_value_before_type_url(
|
||
|
upb_jsonparser_any_frame *frame) {
|
||
|
return frame->before_type_url_start != frame->before_type_url_end;
|
||
|
}
|
||
|
|
||
|
static bool json_parser_any_frame_has_value_after_type_url(
|
||
|
upb_jsonparser_any_frame *frame) {
|
||
|
return frame->after_type_url_start != NULL;
|
||
|
}
|
||
|
|
||
|
static bool json_parser_any_frame_has_value(
|
||
|
upb_jsonparser_any_frame *frame) {
|
||
|
return json_parser_any_frame_has_value_before_type_url(frame) ||
|
||
|
json_parser_any_frame_has_value_after_type_url(frame);
|
||
|
}
|
||
|
|
||
|
static void json_parser_any_frame_set_before_type_url_end(
|
||
|
upb_jsonparser_any_frame *frame,
|
||
|
const char *ptr) {
|
||
6 years ago
|
if (frame->parser == NULL) {
|
||
6 years ago
|
frame->before_type_url_end = ptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static void json_parser_any_frame_set_after_type_url_start_once(
|
||
|
upb_jsonparser_any_frame *frame,
|
||
|
const char *ptr) {
|
||
|
if (json_parser_any_frame_has_type_url(frame) &&
|
||
|
frame->after_type_url_start == NULL) {
|
||
|
frame->after_type_url_start = ptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Used to signal that a capture has been suspended. */
|
||
10 years ago
|
static char suspend_capture;
|
||
|
|
||
10 years ago
|
static upb_selector_t getsel_for_handlertype(upb_json_parser *p,
|
||
|
upb_handlertype_t type) {
|
||
|
upb_selector_t sel;
|
||
|
bool ok = upb_handlers_getselector(p->top->f, type, &sel);
|
||
9 years ago
|
UPB_ASSERT(ok);
|
||
10 years ago
|
return sel;
|
||
|
}
|
||
|
|
||
10 years ago
|
static upb_selector_t parser_getsel(upb_json_parser *p) {
|
||
10 years ago
|
return getsel_for_handlertype(
|
||
|
p, upb_handlers_getprimitivehandlertype(p->top->f));
|
||
|
}
|
||
|
|
||
|
static bool check_stack(upb_json_parser *p) {
|
||
|
if ((p->top + 1) == p->limit) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "Nesting too deep");
|
||
10 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
9 years ago
|
static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) {
|
||
|
upb_value v;
|
||
6 years ago
|
const upb_json_codecache *cache = p->method->cache;
|
||
6 years ago
|
bool ok;
|
||
|
const upb_json_parsermethod *method;
|
||
|
|
||
|
ok = upb_inttable_lookupptr(&cache->methods, frame->m, &v);
|
||
9 years ago
|
UPB_ASSERT(ok);
|
||
6 years ago
|
method = upb_value_getconstptr(v);
|
||
6 years ago
|
|
||
|
frame->name_table = &method->name_table;
|
||
9 years ago
|
}
|
||
|
|
||
10 years ago
|
/* There are GCC/Clang built-ins for overflow checking which we could start
|
||
|
* using if there was any performance benefit to it. */
|
||
10 years ago
|
|
||
10 years ago
|
static bool checked_add(size_t a, size_t b, size_t *c) {
|
||
|
if (SIZE_MAX - a < b) return false;
|
||
|
*c = a + b;
|
||
10 years ago
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
static size_t saturating_multiply(size_t a, size_t b) {
|
||
10 years ago
|
/* size_t is unsigned, so this is defined behavior even on overflow. */
|
||
10 years ago
|
size_t ret = a * b;
|
||
|
if (b != 0 && ret / b != a) {
|
||
|
ret = SIZE_MAX;
|
||
10 years ago
|
}
|
||
10 years ago
|
return ret;
|
||
10 years ago
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/* Base64 decoding ************************************************************/
|
||
10 years ago
|
|
||
10 years ago
|
/* TODO(haberman): make this streaming. */
|
||
10 years ago
|
|
||
|
static const signed char b64table[] = {
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */,
|
||
|
52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
|
||
|
60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1,
|
||
|
-1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
|
||
|
07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
|
||
|
15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
|
||
|
23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1,
|
||
|
-1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
|
||
|
33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
|
||
|
41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
|
||
|
49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1,
|
||
|
-1, -1, -1, -1, -1, -1, -1, -1
|
||
|
};
|
||
|
|
||
10 years ago
|
/* Returns the table value sign-extended to 32 bits. Knowing that the upper
|
||
|
* bits will be 1 for unrecognized characters makes it easier to check for
|
||
|
* this error condition later (see below). */
|
||
10 years ago
|
int32_t b64lookup(unsigned char ch) { return b64table[ch]; }
|
||
|
|
||
10 years ago
|
/* Returns true if the given character is not a valid base64 character or
|
||
|
* padding. */
|
||
10 years ago
|
bool nonbase64(unsigned char ch) { return b64lookup(ch) == -1 && ch != '='; }
|
||
|
|
||
|
static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr,
|
||
|
size_t len) {
|
||
|
const char *limit = ptr + len;
|
||
|
for (; ptr < limit; ptr += 4) {
|
||
10 years ago
|
uint32_t val;
|
||
|
char output[3];
|
||
|
|
||
10 years ago
|
if (limit - ptr < 4) {
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
10 years ago
|
"Base64 input for bytes field not a multiple of 4: %s",
|
||
|
upb_fielddef_name(p->top->f));
|
||
|
return false;
|
||
|
}
|
||
|
|
||
10 years ago
|
val = b64lookup(ptr[0]) << 18 |
|
||
|
b64lookup(ptr[1]) << 12 |
|
||
|
b64lookup(ptr[2]) << 6 |
|
||
|
b64lookup(ptr[3]);
|
||
10 years ago
|
|
||
10 years ago
|
/* Test the upper bit; returns true if any of the characters returned -1. */
|
||
10 years ago
|
if (val & 0x80000000) {
|
||
|
goto otherchar;
|
||
|
}
|
||
|
|
||
|
output[0] = val >> 16;
|
||
|
output[1] = (val >> 8) & 0xff;
|
||
|
output[2] = val & 0xff;
|
||
6 years ago
|
upb_sink_putstring(p->top->sink, sel, output, 3, NULL);
|
||
10 years ago
|
}
|
||
|
return true;
|
||
|
|
||
|
otherchar:
|
||
|
if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) ||
|
||
|
nonbase64(ptr[3]) ) {
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
10 years ago
|
"Non-base64 characters in bytes field: %s",
|
||
|
upb_fielddef_name(p->top->f));
|
||
|
return false;
|
||
|
} if (ptr[2] == '=') {
|
||
10 years ago
|
uint32_t val;
|
||
|
char output;
|
||
|
|
||
|
/* Last group contains only two input bytes, one output byte. */
|
||
10 years ago
|
if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') {
|
||
|
goto badpadding;
|
||
|
}
|
||
|
|
||
10 years ago
|
val = b64lookup(ptr[0]) << 18 |
|
||
|
b64lookup(ptr[1]) << 12;
|
||
10 years ago
|
|
||
9 years ago
|
UPB_ASSERT(!(val & 0x80000000));
|
||
10 years ago
|
output = val >> 16;
|
||
6 years ago
|
upb_sink_putstring(p->top->sink, sel, &output, 1, NULL);
|
||
10 years ago
|
return true;
|
||
|
} else {
|
||
10 years ago
|
uint32_t val;
|
||
|
char output[2];
|
||
|
|
||
|
/* Last group contains only three input bytes, two output bytes. */
|
||
10 years ago
|
if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') {
|
||
|
goto badpadding;
|
||
|
}
|
||
|
|
||
10 years ago
|
val = b64lookup(ptr[0]) << 18 |
|
||
|
b64lookup(ptr[1]) << 12 |
|
||
|
b64lookup(ptr[2]) << 6;
|
||
10 years ago
|
|
||
|
output[0] = val >> 16;
|
||
|
output[1] = (val >> 8) & 0xff;
|
||
6 years ago
|
upb_sink_putstring(p->top->sink, sel, output, 2, NULL);
|
||
10 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
badpadding:
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
10 years ago
|
"Incorrect base64 padding for field: %s (%.*s)",
|
||
|
upb_fielddef_name(p->top->f),
|
||
|
4, ptr);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
|
||
10 years ago
|
/* Accumulate buffer **********************************************************/
|
||
|
|
||
10 years ago
|
/* Functionality for accumulating a buffer.
|
||
|
*
|
||
|
* Some parts of the parser need an entire value as a contiguous string. For
|
||
|
* example, to look up a member name in a hash table, or to turn a string into
|
||
|
* a number, the relevant library routines need the input string to be in
|
||
|
* contiguous memory, even if the value spanned two or more buffers in the
|
||
|
* input. These routines handle that.
|
||
|
*
|
||
|
* In the common case we can just point to the input buffer to get this
|
||
|
* contiguous string and avoid any actual copy. So we optimistically begin
|
||
|
* this way. But there are a few cases where we must instead copy into a
|
||
|
* separate buffer:
|
||
|
*
|
||
|
* 1. The string was not contiguous in the input (it spanned buffers).
|
||
|
*
|
||
|
* 2. The string included escape sequences that need to be interpreted to get
|
||
|
* the true value in a contiguous buffer. */
|
||
10 years ago
|
|
||
|
static void assert_accumulate_empty(upb_json_parser *p) {
|
||
9 years ago
|
UPB_ASSERT(p->accumulated == NULL);
|
||
|
UPB_ASSERT(p->accumulated_len == 0);
|
||
10 years ago
|
}
|
||
|
|
||
|
static void accumulate_clear(upb_json_parser *p) {
|
||
|
p->accumulated = NULL;
|
||
|
p->accumulated_len = 0;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Used internally by accumulate_append(). */
|
||
10 years ago
|
static bool accumulate_realloc(upb_json_parser *p, size_t need) {
|
||
10 years ago
|
void *mem;
|
||
10 years ago
|
size_t old_size = p->accumulate_buf_size;
|
||
|
size_t new_size = UPB_MAX(old_size, 128);
|
||
10 years ago
|
while (new_size < need) {
|
||
|
new_size = saturating_multiply(new_size, 2);
|
||
|
}
|
||
|
|
||
6 years ago
|
mem = upb_arena_realloc(p->arena, p->accumulate_buf, old_size, new_size);
|
||
10 years ago
|
if (!mem) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "Out of memory allocating buffer.");
|
||
10 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
p->accumulate_buf = mem;
|
||
|
p->accumulate_buf_size = new_size;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Logically appends the given data to the append buffer.
|
||
|
* If "can_alias" is true, we will try to avoid actually copying, but the buffer
|
||
|
* must be valid until the next accumulate_append() call (if any). */
|
||
10 years ago
|
static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len,
|
||
|
bool can_alias) {
|
||
10 years ago
|
size_t need;
|
||
|
|
||
10 years ago
|
if (!p->accumulated && can_alias) {
|
||
|
p->accumulated = buf;
|
||
|
p->accumulated_len = len;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (!checked_add(p->accumulated_len, len, &need)) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "Integer overflow.");
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (need > p->accumulate_buf_size && !accumulate_realloc(p, need)) {
|
||
|
return false;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if (p->accumulated != p->accumulate_buf) {
|
||
|
memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len);
|
||
|
p->accumulated = p->accumulate_buf;
|
||
|
}
|
||
|
|
||
|
memcpy(p->accumulate_buf + p->accumulated_len, buf, len);
|
||
|
p->accumulated_len += len;
|
||
10 years ago
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Returns a pointer to the data accumulated since the last accumulate_clear()
|
||
|
* call, and writes the length to *len. This with point either to the input
|
||
|
* buffer or a temporary accumulate buffer. */
|
||
10 years ago
|
static const char *accumulate_getptr(upb_json_parser *p, size_t *len) {
|
||
9 years ago
|
UPB_ASSERT(p->accumulated);
|
||
10 years ago
|
*len = p->accumulated_len;
|
||
|
return p->accumulated;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
|
||
10 years ago
|
/* Mult-part text data ********************************************************/
|
||
|
|
||
10 years ago
|
/* When we have text data in the input, it can often come in multiple segments.
|
||
|
* For example, there may be some raw string data followed by an escape
|
||
|
* sequence. The two segments are processed with different logic. Also buffer
|
||
|
* seams in the input can cause multiple segments.
|
||
|
*
|
||
|
* As we see segments, there are two main cases for how we want to process them:
|
||
|
*
|
||
|
* 1. we want to push the captured input directly to string handlers.
|
||
|
*
|
||
|
* 2. we need to accumulate all the parts into a contiguous buffer for further
|
||
|
* processing (field name lookup, string->number conversion, etc). */
|
||
|
|
||
|
/* This is the set of states for p->multipart_state. */
|
||
10 years ago
|
enum {
|
||
10 years ago
|
/* We are not currently processing multipart data. */
|
||
10 years ago
|
MULTIPART_INACTIVE = 0,
|
||
|
|
||
10 years ago
|
/* We are processing multipart data by accumulating it into a contiguous
|
||
|
* buffer. */
|
||
10 years ago
|
MULTIPART_ACCUMULATE = 1,
|
||
|
|
||
10 years ago
|
/* We are processing multipart data by pushing each part directly to the
|
||
|
* current string handlers. */
|
||
10 years ago
|
MULTIPART_PUSHEAGERLY = 2
|
||
|
};
|
||
10 years ago
|
|
||
10 years ago
|
/* Start a multi-part text value where we accumulate the data for processing at
|
||
|
* the end. */
|
||
10 years ago
|
static void multipart_startaccum(upb_json_parser *p) {
|
||
|
assert_accumulate_empty(p);
|
||
9 years ago
|
UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE);
|
||
10 years ago
|
p->multipart_state = MULTIPART_ACCUMULATE;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Start a multi-part text value where we immediately push text data to a string
|
||
|
* value with the given selector. */
|
||
10 years ago
|
static void multipart_start(upb_json_parser *p, upb_selector_t sel) {
|
||
|
assert_accumulate_empty(p);
|
||
9 years ago
|
UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE);
|
||
10 years ago
|
p->multipart_state = MULTIPART_PUSHEAGERLY;
|
||
|
p->string_selector = sel;
|
||
|
}
|
||
|
|
||
|
static bool multipart_text(upb_json_parser *p, const char *buf, size_t len,
|
||
|
bool can_alias) {
|
||
|
switch (p->multipart_state) {
|
||
|
case MULTIPART_INACTIVE:
|
||
|
upb_status_seterrmsg(
|
||
6 years ago
|
p->status, "Internal error: unexpected state MULTIPART_INACTIVE");
|
||
10 years ago
|
return false;
|
||
|
|
||
|
case MULTIPART_ACCUMULATE:
|
||
|
if (!accumulate_append(p, buf, len, can_alias)) {
|
||
|
return false;
|
||
|
}
|
||
|
break;
|
||
|
|
||
|
case MULTIPART_PUSHEAGERLY: {
|
||
|
const upb_bufhandle *handle = can_alias ? p->handle : NULL;
|
||
6 years ago
|
upb_sink_putstring(p->top->sink, p->string_selector, buf, len, handle);
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Note: this invalidates the accumulate buffer! Call only after reading its
|
||
|
* contents. */
|
||
10 years ago
|
static void multipart_end(upb_json_parser *p) {
|
||
9 years ago
|
UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE);
|
||
10 years ago
|
p->multipart_state = MULTIPART_INACTIVE;
|
||
|
accumulate_clear(p);
|
||
|
}
|
||
|
|
||
|
|
||
|
/* Input capture **************************************************************/
|
||
|
|
||
10 years ago
|
/* Functionality for capturing a region of the input as text. Gracefully
|
||
|
* handles the case where a buffer seam occurs in the middle of the captured
|
||
|
* region. */
|
||
10 years ago
|
|
||
|
static void capture_begin(upb_json_parser *p, const char *ptr) {
|
||
9 years ago
|
UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE);
|
||
|
UPB_ASSERT(p->capture == NULL);
|
||
10 years ago
|
p->capture = ptr;
|
||
|
}
|
||
|
|
||
|
static bool capture_end(upb_json_parser *p, const char *ptr) {
|
||
9 years ago
|
UPB_ASSERT(p->capture);
|
||
10 years ago
|
if (multipart_text(p, p->capture, ptr - p->capture, true)) {
|
||
|
p->capture = NULL;
|
||
10 years ago
|
return true;
|
||
|
} else {
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/* This is called at the end of each input buffer (ie. when we have hit a
|
||
|
* buffer seam). If we are in the middle of capturing the input, this
|
||
|
* processes the unprocessed capture region. */
|
||
10 years ago
|
static void capture_suspend(upb_json_parser *p, const char **ptr) {
|
||
|
if (!p->capture) return;
|
||
|
|
||
|
if (multipart_text(p, p->capture, *ptr - p->capture, false)) {
|
||
10 years ago
|
/* We use this as a signal that we were in the middle of capturing, and
|
||
|
* that capturing should resume at the beginning of the next buffer.
|
||
|
*
|
||
|
* We can't use *ptr here, because we have no guarantee that this pointer
|
||
|
* will be valid when we resume (if the underlying memory is freed, then
|
||
|
* using the pointer at all, even to compare to NULL, is likely undefined
|
||
|
* behavior). */
|
||
10 years ago
|
p->capture = &suspend_capture;
|
||
|
} else {
|
||
10 years ago
|
/* Need to back up the pointer to the beginning of the capture, since
|
||
|
* we were not able to actually preserve it. */
|
||
10 years ago
|
*ptr = p->capture;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static void capture_resume(upb_json_parser *p, const char *ptr) {
|
||
|
if (p->capture) {
|
||
9 years ago
|
UPB_ASSERT(p->capture == &suspend_capture);
|
||
10 years ago
|
p->capture = ptr;
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
|
||
|
/* Callbacks from the parser **************************************************/
|
||
|
|
||
10 years ago
|
/* These are the functions called directly from the parser itself.
|
||
|
* We define these in the same order as their declarations in the parser. */
|
||
10 years ago
|
|
||
|
static char escape_char(char in) {
|
||
|
switch (in) {
|
||
|
case 'r': return '\r';
|
||
|
case 't': return '\t';
|
||
|
case 'n': return '\n';
|
||
|
case 'f': return '\f';
|
||
|
case 'b': return '\b';
|
||
|
case '/': return '/';
|
||
|
case '"': return '"';
|
||
|
case '\\': return '\\';
|
||
|
default:
|
||
9 years ago
|
UPB_ASSERT(0);
|
||
10 years ago
|
return 'x';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static bool escape(upb_json_parser *p, const char *ptr) {
|
||
|
char ch = escape_char(*ptr);
|
||
|
return multipart_text(p, &ch, 1, false);
|
||
|
}
|
||
|
|
||
|
static void start_hex(upb_json_parser *p) {
|
||
|
p->digit = 0;
|
||
|
}
|
||
|
|
||
|
static void hexdigit(upb_json_parser *p, const char *ptr) {
|
||
|
char ch = *ptr;
|
||
|
|
||
|
p->digit <<= 4;
|
||
|
|
||
|
if (ch >= '0' && ch <= '9') {
|
||
|
p->digit += (ch - '0');
|
||
|
} else if (ch >= 'a' && ch <= 'f') {
|
||
|
p->digit += ((ch - 'a') + 10);
|
||
|
} else {
|
||
9 years ago
|
UPB_ASSERT(ch >= 'A' && ch <= 'F');
|
||
10 years ago
|
p->digit += ((ch - 'A') + 10);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
static bool end_hex(upb_json_parser *p) {
|
||
|
uint32_t codepoint = p->digit;
|
||
|
|
||
10 years ago
|
/* emit the codepoint as UTF-8. */
|
||
|
char utf8[3]; /* support \u0000 -- \uFFFF -- need only three bytes. */
|
||
10 years ago
|
int length = 0;
|
||
|
if (codepoint <= 0x7F) {
|
||
|
utf8[0] = codepoint;
|
||
|
length = 1;
|
||
|
} else if (codepoint <= 0x07FF) {
|
||
|
utf8[1] = (codepoint & 0x3F) | 0x80;
|
||
|
codepoint >>= 6;
|
||
|
utf8[0] = (codepoint & 0x1F) | 0xC0;
|
||
|
length = 2;
|
||
|
} else /* codepoint <= 0xFFFF */ {
|
||
|
utf8[2] = (codepoint & 0x3F) | 0x80;
|
||
|
codepoint >>= 6;
|
||
|
utf8[1] = (codepoint & 0x3F) | 0x80;
|
||
|
codepoint >>= 6;
|
||
|
utf8[0] = (codepoint & 0x0F) | 0xE0;
|
||
|
length = 3;
|
||
|
}
|
||
10 years ago
|
/* TODO(haberman): Handle high surrogates: if codepoint is a high surrogate
|
||
|
* we have to wait for the next escape to get the full code point). */
|
||
10 years ago
|
|
||
|
return multipart_text(p, utf8, length, false);
|
||
|
}
|
||
|
|
||
|
static void start_text(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
|
static bool end_text(upb_json_parser *p, const char *ptr) {
|
||
|
return capture_end(p, ptr);
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool start_number(upb_json_parser *p, const char *ptr) {
|
||
|
if (is_top_level(p)) {
|
||
6 years ago
|
if (is_number_wrapper_object(p)) {
|
||
|
start_wrapper_object(p);
|
||
6 years ago
|
} else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
start_value_object(p, VALUE_NUMBERVALUE);
|
||
|
} else {
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
} else if (does_number_wrapper_start(p)) {
|
||
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_wrapper_object(p);
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_value_object(p, VALUE_NUMBERVALUE);
|
||
6 years ago
|
}
|
||
|
|
||
10 years ago
|
multipart_startaccum(p);
|
||
|
capture_begin(p, ptr);
|
||
6 years ago
|
return true;
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
static bool parse_number(upb_json_parser *p, bool is_quoted);
|
||
10 years ago
|
|
||
6 years ago
|
static bool end_number_nontop(upb_json_parser *p, const char *ptr) {
|
||
10 years ago
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
7 years ago
|
if (p->top->f == NULL) {
|
||
|
multipart_end(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
8 years ago
|
return parse_number(p, false);
|
||
10 years ago
|
}
|
||
|
|
||
6 years ago
|
static bool end_number(upb_json_parser *p, const char *ptr) {
|
||
|
if (!end_number_nontop(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (does_number_wrapper_end(p)) {
|
||
|
end_wrapper_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
6 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
end_value_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
return true;
|
||
6 years ago
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
8 years ago
|
/* |buf| is NULL-terminated. |buf| itself will never include quotes;
|
||
|
* |is_quoted| tells us whether this text originally appeared inside quotes. */
|
||
8 years ago
|
static bool parse_number_from_buffer(upb_json_parser *p, const char *buf,
|
||
8 years ago
|
bool is_quoted) {
|
||
|
size_t len = strlen(buf);
|
||
|
const char *bufend = buf + len;
|
||
10 years ago
|
char *end;
|
||
8 years ago
|
upb_fieldtype_t type = upb_fielddef_type(p->top->f);
|
||
|
double val;
|
||
|
double dummy;
|
||
8 years ago
|
double inf = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */
|
||
10 years ago
|
|
||
8 years ago
|
errno = 0;
|
||
|
|
||
|
if (len == 0 || buf[0] == ' ') {
|
||
10 years ago
|
return false;
|
||
|
}
|
||
|
|
||
8 years ago
|
/* For integer types, first try parsing with integer-specific routines.
|
||
|
* If these succeed, they will be more accurate for int64/uint64 than
|
||
|
* strtod().
|
||
|
*/
|
||
|
switch (type) {
|
||
10 years ago
|
case UPB_TYPE_ENUM:
|
||
|
case UPB_TYPE_INT32: {
|
||
8 years ago
|
long val = strtol(buf, &end, 0);
|
||
|
if (errno == ERANGE || end != bufend) {
|
||
|
break;
|
||
|
} else if (val > INT32_MAX || val < INT32_MIN) {
|
||
|
return false;
|
||
|
} else {
|
||
6 years ago
|
upb_sink_putint32(p->top->sink, parser_getsel(p), val);
|
||
8 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
case UPB_TYPE_UINT32: {
|
||
8 years ago
|
unsigned long val = strtoul(buf, &end, 0);
|
||
|
if (end != bufend) {
|
||
|
break;
|
||
|
} else if (val > UINT32_MAX || errno == ERANGE) {
|
||
|
return false;
|
||
|
} else {
|
||
6 years ago
|
upb_sink_putuint32(p->top->sink, parser_getsel(p), val);
|
||
8 years ago
|
return true;
|
||
|
}
|
||
|
}
|
||
|
/* XXX: We can't handle [u]int64 properly on 32-bit machines because
|
||
|
* strto[u]ll isn't in C89. */
|
||
|
case UPB_TYPE_INT64: {
|
||
|
long val = strtol(buf, &end, 0);
|
||
|
if (errno == ERANGE || end != bufend) {
|
||
|
break;
|
||
|
} else {
|
||
6 years ago
|
upb_sink_putint64(p->top->sink, parser_getsel(p), val);
|
||
8 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
case UPB_TYPE_UINT64: {
|
||
8 years ago
|
unsigned long val = strtoul(p->accumulated, &end, 0);
|
||
|
if (end != bufend) {
|
||
|
break;
|
||
|
} else if (errno == ERANGE) {
|
||
|
return false;
|
||
|
} else {
|
||
6 years ago
|
upb_sink_putuint64(p->top->sink, parser_getsel(p), val);
|
||
8 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
}
|
||
8 years ago
|
default:
|
||
10 years ago
|
break;
|
||
8 years ago
|
}
|
||
|
|
||
|
if (type != UPB_TYPE_DOUBLE && type != UPB_TYPE_FLOAT && is_quoted) {
|
||
8 years ago
|
/* Quoted numbers for integer types are not allowed to be in double form. */
|
||
8 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
if (len == strlen("Infinity") && strcmp(buf, "Infinity") == 0) {
|
||
|
/* C89 does not have an INFINITY macro. */
|
||
8 years ago
|
val = inf;
|
||
8 years ago
|
} else if (len == strlen("-Infinity") && strcmp(buf, "-Infinity") == 0) {
|
||
8 years ago
|
val = -inf;
|
||
8 years ago
|
} else {
|
||
|
val = strtod(buf, &end);
|
||
|
if (errno == ERANGE || end != bufend) {
|
||
|
return false;
|
||
10 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
switch (type) {
|
||
8 years ago
|
#define CASE(capitaltype, smalltype, ctype, min, max) \
|
||
8 years ago
|
case UPB_TYPE_ ## capitaltype: { \
|
||
|
if (modf(val, &dummy) != 0 || val > max || val < min) { \
|
||
|
return false; \
|
||
|
} else { \
|
||
6 years ago
|
upb_sink_put ## smalltype(p->top->sink, parser_getsel(p), \
|
||
8 years ago
|
(ctype)val); \
|
||
8 years ago
|
return true; \
|
||
|
} \
|
||
|
break; \
|
||
10 years ago
|
}
|
||
8 years ago
|
case UPB_TYPE_ENUM:
|
||
8 years ago
|
CASE(INT32, int32, int32_t, INT32_MIN, INT32_MAX);
|
||
|
CASE(INT64, int64, int64_t, INT64_MIN, INT64_MAX);
|
||
|
CASE(UINT32, uint32, uint32_t, 0, UINT32_MAX);
|
||
|
CASE(UINT64, uint64, uint64_t, 0, UINT64_MAX);
|
||
8 years ago
|
#undef CASE
|
||
|
|
||
|
case UPB_TYPE_DOUBLE:
|
||
6 years ago
|
upb_sink_putdouble(p->top->sink, parser_getsel(p), val);
|
||
8 years ago
|
return true;
|
||
|
case UPB_TYPE_FLOAT:
|
||
8 years ago
|
if ((val > FLT_MAX || val < -FLT_MAX) && val != inf && val != -inf) {
|
||
8 years ago
|
return false;
|
||
|
} else {
|
||
6 years ago
|
upb_sink_putfloat(p->top->sink, parser_getsel(p), val);
|
||
8 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
default:
|
||
8 years ago
|
return false;
|
||
10 years ago
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
static bool parse_number(upb_json_parser *p, bool is_quoted) {
|
||
|
size_t len;
|
||
|
const char *buf;
|
||
10 years ago
|
|
||
8 years ago
|
/* strtol() and friends unfortunately do not support specifying the length of
|
||
|
* the input string, so we need to force a copy into a NULL-terminated buffer. */
|
||
|
if (!multipart_text(p, "\0", 1, false)) {
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
buf = accumulate_getptr(p, &len);
|
||
|
|
||
8 years ago
|
if (parse_number_from_buffer(p, buf, is_quoted)) {
|
||
8 years ago
|
multipart_end(p);
|
||
|
return true;
|
||
|
} else {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing number: %s", buf);
|
||
8 years ago
|
multipart_end(p);
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static bool parser_putbool(upb_json_parser *p, bool val) {
|
||
10 years ago
|
bool ok;
|
||
|
|
||
7 years ago
|
if (p->top->f == NULL) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) {
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
10 years ago
|
"Boolean value specified for non-bool field: %s",
|
||
|
upb_fielddef_name(p->top->f));
|
||
|
return false;
|
||
|
}
|
||
|
|
||
6 years ago
|
ok = upb_sink_putbool(p->top->sink, parser_getsel(p), val);
|
||
9 years ago
|
UPB_ASSERT(ok);
|
||
10 years ago
|
|
||
10 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool end_bool(upb_json_parser *p, bool val) {
|
||
|
if (is_top_level(p)) {
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) {
|
||
6 years ago
|
start_wrapper_object(p);
|
||
6 years ago
|
} else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
start_value_object(p, VALUE_BOOLVALUE);
|
||
|
} else {
|
||
6 years ago
|
return false;
|
||
|
}
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_BOOLVALUE)) {
|
||
6 years ago
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_wrapper_object(p);
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_value_object(p, VALUE_BOOLVALUE);
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
if (p->top->is_unknown_field) {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (!parser_putbool(p, val)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_BOOLVALUE)) {
|
||
6 years ago
|
end_wrapper_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
6 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
end_value_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static bool end_null(upb_json_parser *p) {
|
||
|
const char *zero_ptr = "0";
|
||
|
|
||
|
if (is_top_level(p)) {
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
start_value_object(p, VALUE_NULLVALUE);
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_value_object(p, VALUE_NULLVALUE);
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/* Fill null_value field. */
|
||
|
multipart_startaccum(p);
|
||
|
capture_begin(p, zero_ptr);
|
||
|
capture_end(p, zero_ptr + 1);
|
||
|
parse_number(p, false);
|
||
|
|
||
|
end_value_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
6 years ago
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool start_any_stringval(upb_json_parser *p) {
|
||
|
multipart_startaccum(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
static bool start_stringval(upb_json_parser *p) {
|
||
6 years ago
|
if (is_top_level(p)) {
|
||
6 years ago
|
if (is_string_wrapper_object(p)) {
|
||
|
start_wrapper_object(p);
|
||
6 years ago
|
} else if (is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) {
|
||
|
start_fieldmask_object(p);
|
||
|
return true;
|
||
6 years ago
|
} else if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) ||
|
||
|
is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) {
|
||
6 years ago
|
start_object(p);
|
||
6 years ago
|
} else if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
start_value_object(p, VALUE_STRINGVALUE);
|
||
6 years ago
|
} else {
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
} else if (does_string_wrapper_start(p)) {
|
||
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_wrapper_object(p);
|
||
6 years ago
|
} else if (does_fieldmask_start(p)) {
|
||
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_fieldmask_object(p);
|
||
|
return true;
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_TIMESTAMP) ||
|
||
|
is_wellknown_field(p, UPB_WELLKNOWN_DURATION)) {
|
||
6 years ago
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_object(p);
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
if (!start_subobject(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
start_value_object(p, VALUE_STRINGVALUE);
|
||
6 years ago
|
}
|
||
|
|
||
7 years ago
|
if (p->top->f == NULL) {
|
||
|
multipart_startaccum(p);
|
||
|
return true;
|
||
|
}
|
||
10 years ago
|
|
||
6 years ago
|
if (p->top->is_any) {
|
||
|
return start_any_stringval(p);
|
||
|
}
|
||
|
|
||
10 years ago
|
if (upb_fielddef_isstring(p->top->f)) {
|
||
10 years ago
|
upb_jsonparser_frame *inner;
|
||
|
upb_selector_t sel;
|
||
|
|
||
10 years ago
|
if (!check_stack(p)) return false;
|
||
|
|
||
10 years ago
|
/* Start a new parser frame: parser frames correspond one-to-one with
|
||
|
* handler frames, and string events occur in a sub-frame. */
|
||
6 years ago
|
inner = start_jsonparser_frame(p);
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
|
||
6 years ago
|
upb_sink_startstr(p->top->sink, sel, 0, &inner->sink);
|
||
10 years ago
|
inner->m = p->top->m;
|
||
|
inner->f = p->top->f;
|
||
|
p->top = inner;
|
||
|
|
||
|
if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) {
|
||
10 years ago
|
/* For STRING fields we push data directly to the handlers as it is
|
||
|
* parsed. We don't do this yet for BYTES fields, because our base64
|
||
|
* decoder is not streaming.
|
||
|
*
|
||
|
* TODO(haberman): make base64 decoding streaming also. */
|
||
10 years ago
|
multipart_start(p, getsel_for_handlertype(p, UPB_HANDLER_STRING));
|
||
|
return true;
|
||
|
} else {
|
||
|
multipart_startaccum(p);
|
||
|
return true;
|
||
|
}
|
||
8 years ago
|
} else if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL &&
|
||
|
upb_fielddef_type(p->top->f) != UPB_TYPE_MESSAGE) {
|
||
|
/* No need to push a frame -- numeric values in quotes remain in the
|
||
|
* current parser frame. These values must accmulate so we can convert
|
||
|
* them all at once at the end. */
|
||
10 years ago
|
multipart_startaccum(p);
|
||
|
return true;
|
||
|
} else {
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
8 years ago
|
"String specified for bool or submessage field: %s",
|
||
10 years ago
|
upb_fielddef_name(p->top->f));
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool end_any_stringval(upb_json_parser *p) {
|
||
|
size_t len;
|
||
|
const char *buf = accumulate_getptr(p, &len);
|
||
|
|
||
|
/* Set type_url */
|
||
|
upb_selector_t sel;
|
||
|
upb_jsonparser_frame *inner;
|
||
|
if (!check_stack(p)) return false;
|
||
|
inner = p->top + 1;
|
||
|
|
||
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
|
||
6 years ago
|
upb_sink_startstr(p->top->sink, sel, 0, &inner->sink);
|
||
6 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
|
||
6 years ago
|
upb_sink_putstring(inner->sink, sel, buf, len, NULL);
|
||
6 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
|
||
6 years ago
|
upb_sink_endstr(inner->sink, sel);
|
||
6 years ago
|
|
||
|
multipart_end(p);
|
||
|
|
||
|
/* Resolve type url */
|
||
|
if (strncmp(buf, "type.googleapis.com/", 20) == 0 && len > 20) {
|
||
|
const upb_msgdef *payload_type = NULL;
|
||
|
buf += 20;
|
||
|
len -= 20;
|
||
|
|
||
|
payload_type = upb_symtab_lookupmsg2(p->symtab, buf, len);
|
||
|
if (payload_type == NULL) {
|
||
|
upb_status_seterrf(
|
||
6 years ago
|
p->status, "Cannot find packed type: %.*s\n", (int)len, buf);
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
json_parser_any_frame_set_payload_type(p, p->top->any_frame, payload_type);
|
||
6 years ago
|
|
||
6 years ago
|
return true;
|
||
|
} else {
|
||
|
upb_status_seterrf(
|
||
6 years ago
|
p->status, "Invalid type url: %.*s\n", (int)len, buf);
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool end_stringval_nontop(upb_json_parser *p) {
|
||
10 years ago
|
bool ok = true;
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) ||
|
||
|
is_wellknown_msg(p, UPB_WELLKNOWN_DURATION)) {
|
||
6 years ago
|
multipart_end(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
7 years ago
|
if (p->top->f == NULL) {
|
||
|
multipart_end(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (p->top->is_any) {
|
||
|
return end_any_stringval(p);
|
||
|
}
|
||
|
|
||
10 years ago
|
switch (upb_fielddef_type(p->top->f)) {
|
||
|
case UPB_TYPE_BYTES:
|
||
|
if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING),
|
||
|
p->accumulated, p->accumulated_len)) {
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
/* Fall through. */
|
||
10 years ago
|
|
||
|
case UPB_TYPE_STRING: {
|
||
|
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
|
||
6 years ago
|
upb_sink_endstr(p->top->sink, sel);
|
||
6 years ago
|
p->top--;
|
||
10 years ago
|
break;
|
||
|
}
|
||
|
|
||
|
case UPB_TYPE_ENUM: {
|
||
10 years ago
|
/* Resolve enum symbolic name to integer value. */
|
||
6 years ago
|
const upb_enumdef *enumdef = upb_fielddef_enumsubdef(p->top->f);
|
||
10 years ago
|
|
||
|
size_t len;
|
||
|
const char *buf = accumulate_getptr(p, &len);
|
||
|
|
||
|
int32_t int_val = 0;
|
||
|
ok = upb_enumdef_ntoi(enumdef, buf, len, &int_val);
|
||
|
|
||
|
if (ok) {
|
||
|
upb_selector_t sel = parser_getsel(p);
|
||
6 years ago
|
upb_sink_putint32(p->top->sink, sel, int_val);
|
||
10 years ago
|
} else {
|
||
6 years ago
|
upb_status_seterrf(p->status, "Enum value unknown: '%.*s'", len, buf);
|
||
10 years ago
|
}
|
||
|
|
||
|
break;
|
||
|
}
|
||
|
|
||
8 years ago
|
case UPB_TYPE_INT32:
|
||
|
case UPB_TYPE_INT64:
|
||
|
case UPB_TYPE_UINT32:
|
||
|
case UPB_TYPE_UINT64:
|
||
|
case UPB_TYPE_DOUBLE:
|
||
|
case UPB_TYPE_FLOAT:
|
||
|
ok = parse_number(p, true);
|
||
|
break;
|
||
|
|
||
10 years ago
|
default:
|
||
9 years ago
|
UPB_ASSERT(false);
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "Internal error in JSON decoder");
|
||
10 years ago
|
ok = false;
|
||
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
multipart_end(p);
|
||
10 years ago
|
|
||
10 years ago
|
return ok;
|
||
10 years ago
|
}
|
||
|
|
||
6 years ago
|
static bool end_stringval(upb_json_parser *p) {
|
||
6 years ago
|
/* FieldMask's stringvals have been ended when handling them. Only need to
|
||
|
* close FieldMask here.*/
|
||
|
if (does_fieldmask_end(p)) {
|
||
|
end_fieldmask_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (!end_stringval_nontop(p)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (does_string_wrapper_end(p)) {
|
||
|
end_wrapper_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
6 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
end_value_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
return true;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_TIMESTAMP) ||
|
||
6 years ago
|
is_wellknown_msg(p, UPB_WELLKNOWN_DURATION) ||
|
||
|
is_wellknown_msg(p, UPB_WELLKNOWN_FIELDMASK)) {
|
||
6 years ago
|
end_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
6 years ago
|
return true;
|
||
6 years ago
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
static void start_duration_base(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
|
static bool end_duration_base(upb_json_parser *p, const char *ptr) {
|
||
|
size_t len;
|
||
|
const char *buf;
|
||
|
char seconds_buf[14];
|
||
|
char nanos_buf[12];
|
||
|
char *end;
|
||
|
int64_t seconds = 0;
|
||
|
int32_t nanos = 0;
|
||
|
double val = 0.0;
|
||
|
const char *seconds_membername = "seconds";
|
||
|
const char *nanos_membername = "nanos";
|
||
|
size_t fraction_start;
|
||
|
|
||
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
buf = accumulate_getptr(p, &len);
|
||
|
|
||
|
memset(seconds_buf, 0, 14);
|
||
|
memset(nanos_buf, 0, 12);
|
||
|
|
||
|
/* Find out base end. The maximus duration is 315576000000, which cannot be
|
||
|
* represented by double without losing precision. Thus, we need to handle
|
||
|
* fraction and base separately. */
|
||
|
for (fraction_start = 0; fraction_start < len && buf[fraction_start] != '.';
|
||
|
fraction_start++);
|
||
|
|
||
|
/* Parse base */
|
||
|
memcpy(seconds_buf, buf, fraction_start);
|
||
|
seconds = strtol(seconds_buf, &end, 10);
|
||
|
if (errno == ERANGE || end != seconds_buf + fraction_start) {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing duration: %s",
|
||
6 years ago
|
seconds_buf);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (seconds > 315576000000) {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing duration: "
|
||
6 years ago
|
"maximum acceptable value is "
|
||
|
"315576000000");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if (seconds < -315576000000) {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing duration: "
|
||
6 years ago
|
"minimum acceptable value is "
|
||
|
"-315576000000");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/* Parse fraction */
|
||
|
nanos_buf[0] = '0';
|
||
|
memcpy(nanos_buf + 1, buf + fraction_start, len - fraction_start);
|
||
|
val = strtod(nanos_buf, &end);
|
||
|
if (errno == ERANGE || end != nanos_buf + len - fraction_start + 1) {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing duration: %s",
|
||
6 years ago
|
nanos_buf);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
nanos = val * 1000000000;
|
||
|
if (seconds < 0) nanos = -nanos;
|
||
|
|
||
|
/* Clean up buffer */
|
||
|
multipart_end(p);
|
||
|
|
||
|
/* Set seconds */
|
||
|
start_member(p);
|
||
|
capture_begin(p, seconds_membername);
|
||
|
capture_end(p, seconds_membername + 7);
|
||
|
end_membername(p);
|
||
6 years ago
|
upb_sink_putint64(p->top->sink, parser_getsel(p), seconds);
|
||
6 years ago
|
end_member(p);
|
||
|
|
||
|
/* Set nanos */
|
||
|
start_member(p);
|
||
|
capture_begin(p, nanos_membername);
|
||
|
capture_end(p, nanos_membername + 5);
|
||
|
end_membername(p);
|
||
6 years ago
|
upb_sink_putint32(p->top->sink, parser_getsel(p), nanos);
|
||
6 years ago
|
end_member(p);
|
||
|
|
||
6 years ago
|
/* Continue previous arena */
|
||
6 years ago
|
multipart_startaccum(p);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
static int parse_timestamp_number(upb_json_parser *p) {
|
||
|
size_t len;
|
||
|
const char *buf;
|
||
|
int val;
|
||
|
|
||
|
/* atoi() and friends unfortunately do not support specifying the length of
|
||
|
* the input string, so we need to force a copy into a NULL-terminated buffer. */
|
||
|
multipart_text(p, "\0", 1, false);
|
||
|
|
||
|
buf = accumulate_getptr(p, &len);
|
||
|
val = atoi(buf);
|
||
|
multipart_end(p);
|
||
|
multipart_startaccum(p);
|
||
|
|
||
|
return val;
|
||
|
}
|
||
|
|
||
|
static void start_year(upb_json_parser *p, const char *ptr) {
|
||
6 years ago
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool end_year(upb_json_parser *p, const char *ptr) {
|
||
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
p->tm.tm_year = parse_timestamp_number(p) - 1900;
|
||
|
return true;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
static void start_month(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
static bool end_month(upb_json_parser *p, const char *ptr) {
|
||
6 years ago
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
6 years ago
|
p->tm.tm_mon = parse_timestamp_number(p) - 1;
|
||
|
return true;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
static void start_day(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
static bool end_day(upb_json_parser *p, const char *ptr) {
|
||
|
if (!capture_end(p, ptr)) {
|
||
6 years ago
|
return false;
|
||
|
}
|
||
6 years ago
|
p->tm.tm_mday = parse_timestamp_number(p);
|
||
|
return true;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
static void start_hour(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
static bool end_hour(upb_json_parser *p, const char *ptr) {
|
||
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
p->tm.tm_hour = parse_timestamp_number(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static void start_minute(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
|
static bool end_minute(upb_json_parser *p, const char *ptr) {
|
||
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
p->tm.tm_min = parse_timestamp_number(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static void start_second(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
|
static bool end_second(upb_json_parser *p, const char *ptr) {
|
||
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
p->tm.tm_sec = parse_timestamp_number(p);
|
||
6 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
static void start_timestamp_base(upb_json_parser *p) {
|
||
|
memset(&p->tm, 0, sizeof(struct tm));
|
||
|
}
|
||
|
|
||
6 years ago
|
static void start_timestamp_fraction(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
|
static bool end_timestamp_fraction(upb_json_parser *p, const char *ptr) {
|
||
|
size_t len;
|
||
|
const char *buf;
|
||
|
char nanos_buf[12];
|
||
|
char *end;
|
||
|
double val = 0.0;
|
||
|
int32_t nanos;
|
||
|
const char *nanos_membername = "nanos";
|
||
|
|
||
|
memset(nanos_buf, 0, 12);
|
||
|
|
||
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
buf = accumulate_getptr(p, &len);
|
||
|
|
||
|
if (len > 10) {
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
6 years ago
|
"error parsing timestamp: at most 9-digit fraction.");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/* Parse nanos */
|
||
|
nanos_buf[0] = '0';
|
||
|
memcpy(nanos_buf + 1, buf, len);
|
||
|
val = strtod(nanos_buf, &end);
|
||
|
|
||
|
if (errno == ERANGE || end != nanos_buf + len + 1) {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing timestamp nanos: %s",
|
||
6 years ago
|
nanos_buf);
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
nanos = val * 1000000000;
|
||
|
|
||
|
/* Clean up previous environment */
|
||
|
multipart_end(p);
|
||
|
|
||
|
/* Set nanos */
|
||
|
start_member(p);
|
||
|
capture_begin(p, nanos_membername);
|
||
|
capture_end(p, nanos_membername + 5);
|
||
|
end_membername(p);
|
||
6 years ago
|
upb_sink_putint32(p->top->sink, parser_getsel(p), nanos);
|
||
6 years ago
|
end_member(p);
|
||
|
|
||
|
/* Continue previous environment */
|
||
|
multipart_startaccum(p);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static void start_timestamp_zone(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
|
static bool end_timestamp_zone(upb_json_parser *p, const char *ptr) {
|
||
|
size_t len;
|
||
|
const char *buf;
|
||
|
int hours;
|
||
|
int64_t seconds;
|
||
|
const char *seconds_membername = "seconds";
|
||
|
|
||
|
if (!capture_end(p, ptr)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
buf = accumulate_getptr(p, &len);
|
||
|
|
||
|
if (buf[0] != 'Z') {
|
||
|
if (sscanf(buf + 1, "%2d:00", &hours) != 1) {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing timestamp offset");
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
if (buf[0] == '+') {
|
||
|
hours = -hours;
|
||
|
}
|
||
|
|
||
|
p->tm.tm_hour += hours;
|
||
|
}
|
||
|
|
||
|
/* Normalize tm */
|
||
|
seconds = mktime(&p->tm);
|
||
|
|
||
|
/* Check timestamp boundary */
|
||
|
if (seconds < -62135596800) {
|
||
6 years ago
|
upb_status_seterrf(p->status, "error parsing timestamp: "
|
||
6 years ago
|
"minimum acceptable value is "
|
||
|
"0001-01-01T00:00:00Z");
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/* Clean up previous environment */
|
||
|
multipart_end(p);
|
||
|
|
||
|
/* Set seconds */
|
||
|
start_member(p);
|
||
|
capture_begin(p, seconds_membername);
|
||
|
capture_end(p, seconds_membername + 7);
|
||
|
end_membername(p);
|
||
6 years ago
|
upb_sink_putint64(p->top->sink, parser_getsel(p), seconds);
|
||
6 years ago
|
end_member(p);
|
||
|
|
||
|
/* Continue previous environment */
|
||
|
multipart_startaccum(p);
|
||
|
|
||
6 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
static void start_fieldmask_path_text(upb_json_parser *p, const char *ptr) {
|
||
|
capture_begin(p, ptr);
|
||
|
}
|
||
|
|
||
|
static bool end_fieldmask_path_text(upb_json_parser *p, const char *ptr) {
|
||
6 years ago
|
return capture_end(p, ptr);
|
||
6 years ago
|
}
|
||
|
|
||
|
static bool start_fieldmask_path(upb_json_parser *p) {
|
||
|
upb_jsonparser_frame *inner;
|
||
|
upb_selector_t sel;
|
||
|
|
||
|
if (!check_stack(p)) return false;
|
||
|
|
||
|
/* Start a new parser frame: parser frames correspond one-to-one with
|
||
|
* handler frames, and string events occur in a sub-frame. */
|
||
6 years ago
|
inner = start_jsonparser_frame(p);
|
||
6 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
|
||
6 years ago
|
upb_sink_startstr(p->top->sink, sel, 0, &inner->sink);
|
||
6 years ago
|
inner->m = p->top->m;
|
||
|
inner->f = p->top->f;
|
||
|
p->top = inner;
|
||
|
|
||
|
multipart_startaccum(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static bool lower_camel_push(
|
||
|
upb_json_parser *p, upb_selector_t sel, const char *ptr, size_t len) {
|
||
|
const char *limit = ptr + len;
|
||
|
bool first = true;
|
||
|
for (;ptr < limit; ptr++) {
|
||
|
if (*ptr >= 'A' && *ptr <= 'Z' && !first) {
|
||
|
char lower = tolower(*ptr);
|
||
6 years ago
|
upb_sink_putstring(p->top->sink, sel, "_", 1, NULL);
|
||
|
upb_sink_putstring(p->top->sink, sel, &lower, 1, NULL);
|
||
6 years ago
|
} else {
|
||
6 years ago
|
upb_sink_putstring(p->top->sink, sel, ptr, 1, NULL);
|
||
6 years ago
|
}
|
||
|
first = false;
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
static bool end_fieldmask_path(upb_json_parser *p) {
|
||
|
upb_selector_t sel;
|
||
|
|
||
|
if (!lower_camel_push(
|
||
|
p, getsel_for_handlertype(p, UPB_HANDLER_STRING),
|
||
|
p->accumulated, p->accumulated_len)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
|
||
6 years ago
|
upb_sink_endstr(p->top->sink, sel);
|
||
6 years ago
|
p->top--;
|
||
|
|
||
|
multipart_end(p);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
static void start_member(upb_json_parser *p) {
|
||
9 years ago
|
UPB_ASSERT(!p->top->f);
|
||
10 years ago
|
multipart_startaccum(p);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
/* Helper: invoked during parse_mapentry() to emit the mapentry message's key
|
||
|
* field based on the current contents of the accumulate buffer. */
|
||
10 years ago
|
static bool parse_mapentry_key(upb_json_parser *p) {
|
||
|
|
||
10 years ago
|
size_t len;
|
||
|
const char *buf = accumulate_getptr(p, &len);
|
||
|
|
||
10 years ago
|
/* Emit the key field. We do a bit of ad-hoc parsing here because the
|
||
|
* parser state machine has already decided that this is a string field
|
||
|
* name, and we are reinterpreting it as some arbitrary key type. In
|
||
|
* particular, integer and bool keys are quoted, so we need to parse the
|
||
|
* quoted string contents here. */
|
||
10 years ago
|
|
||
10 years ago
|
p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY);
|
||
|
if (p->top->f == NULL) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "mapentry message has no key");
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
switch (upb_fielddef_type(p->top->f)) {
|
||
|
case UPB_TYPE_INT32:
|
||
|
case UPB_TYPE_INT64:
|
||
|
case UPB_TYPE_UINT32:
|
||
|
case UPB_TYPE_UINT64:
|
||
10 years ago
|
/* Invoke end_number. The accum buffer has the number's text already. */
|
||
8 years ago
|
if (!parse_number(p, true)) {
|
||
10 years ago
|
return false;
|
||
|
}
|
||
|
break;
|
||
|
case UPB_TYPE_BOOL:
|
||
|
if (len == 4 && !strncmp(buf, "true", 4)) {
|
||
|
if (!parser_putbool(p, true)) {
|
||
|
return false;
|
||
|
}
|
||
|
} else if (len == 5 && !strncmp(buf, "false", 5)) {
|
||
|
if (!parser_putbool(p, false)) {
|
||
|
return false;
|
||
|
}
|
||
|
} else {
|
||
6 years ago
|
upb_status_seterrmsg(p->status,
|
||
10 years ago
|
"Map bool key not 'true' or 'false'");
|
||
|
return false;
|
||
|
}
|
||
|
multipart_end(p);
|
||
|
break;
|
||
|
case UPB_TYPE_STRING:
|
||
|
case UPB_TYPE_BYTES: {
|
||
|
upb_sink subsink;
|
||
|
upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
|
||
6 years ago
|
upb_sink_startstr(p->top->sink, sel, len, &subsink);
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
|
||
6 years ago
|
upb_sink_putstring(subsink, sel, buf, len, NULL);
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
|
||
6 years ago
|
upb_sink_endstr(subsink, sel);
|
||
10 years ago
|
multipart_end(p);
|
||
|
break;
|
||
|
}
|
||
|
default:
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "Invalid field type for map key");
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Helper: emit one map entry (as a submessage in the map field sequence). This
|
||
|
* is invoked from end_membername(), at the end of the map entry's key string,
|
||
|
* with the map key in the accumulate buffer. It parses the key from that
|
||
|
* buffer, emits the handler calls to start the mapentry submessage (setting up
|
||
|
* its subframe in the process), and sets up state in the subframe so that the
|
||
|
* value parser (invoked next) will emit the mapentry's value field and then
|
||
|
* end the mapentry message. */
|
||
10 years ago
|
|
||
|
static bool handle_mapentry(upb_json_parser *p) {
|
||
10 years ago
|
const upb_fielddef *mapfield;
|
||
|
const upb_msgdef *mapentrymsg;
|
||
|
upb_jsonparser_frame *inner;
|
||
|
upb_selector_t sel;
|
||
|
|
||
|
/* Map entry: p->top->sink is the seq frame, so we need to start a frame
|
||
|
* for the mapentry itself, and then set |f| in that frame so that the map
|
||
|
* value field is parsed, and also set a flag to end the frame after the
|
||
|
* map-entry value is parsed. */
|
||
10 years ago
|
if (!check_stack(p)) return false;
|
||
|
|
||
10 years ago
|
mapfield = p->top->mapfield;
|
||
|
mapentrymsg = upb_fielddef_msgsubdef(mapfield);
|
||
10 years ago
|
|
||
6 years ago
|
inner = start_jsonparser_frame(p);
|
||
10 years ago
|
p->top->f = mapfield;
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
|
||
6 years ago
|
upb_sink_startsubmsg(p->top->sink, sel, &inner->sink);
|
||
10 years ago
|
inner->m = mapentrymsg;
|
||
|
inner->mapfield = mapfield;
|
||
|
|
||
10 years ago
|
/* Don't set this to true *yet* -- we reuse parsing handlers below to push
|
||
|
* the key field value to the sink, and these handlers will pop the frame
|
||
|
* if they see is_mapentry (when invoked by the parser state machine, they
|
||
|
* would have just seen the map-entry value, not key). */
|
||
10 years ago
|
inner->is_mapentry = false;
|
||
|
p->top = inner;
|
||
|
|
||
10 years ago
|
/* send STARTMSG in submsg frame. */
|
||
6 years ago
|
upb_sink_startmsg(p->top->sink);
|
||
10 years ago
|
|
||
|
parse_mapentry_key(p);
|
||
|
|
||
10 years ago
|
/* Set up the value field to receive the map-entry value. */
|
||
10 years ago
|
p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_VALUE);
|
||
10 years ago
|
p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */
|
||
10 years ago
|
p->top->mapfield = mapfield;
|
||
|
if (p->top->f == NULL) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "mapentry message has no value");
|
||
10 years ago
|
return false;
|
||
|
}
|
||
10 years ago
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
10 years ago
|
static bool end_membername(upb_json_parser *p) {
|
||
9 years ago
|
UPB_ASSERT(!p->top->f);
|
||
10 years ago
|
|
||
7 years ago
|
if (!p->top->m) {
|
||
6 years ago
|
p->top->is_unknown_field = true;
|
||
|
multipart_end(p);
|
||
7 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
if (p->top->is_any) {
|
||
|
return end_any_membername(p);
|
||
|
} else if (p->top->is_map) {
|
||
10 years ago
|
return handle_mapentry(p);
|
||
|
} else {
|
||
|
size_t len;
|
||
|
const char *buf = accumulate_getptr(p, &len);
|
||
9 years ago
|
upb_value v;
|
||
10 years ago
|
|
||
9 years ago
|
if (upb_strtable_lookup2(p->top->name_table, buf, len, &v)) {
|
||
|
p->top->f = upb_value_getconstptr(v);
|
||
|
multipart_end(p);
|
||
|
|
||
7 years ago
|
return true;
|
||
|
} else if (p->ignore_json_unknown) {
|
||
6 years ago
|
p->top->is_unknown_field = true;
|
||
7 years ago
|
multipart_end(p);
|
||
9 years ago
|
return true;
|
||
|
} else {
|
||
6 years ago
|
upb_status_seterrf(p->status, "No such field: %.*s\n", (int)len, buf);
|
||
10 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool end_any_membername(upb_json_parser *p) {
|
||
|
size_t len;
|
||
|
const char *buf = accumulate_getptr(p, &len);
|
||
|
upb_value v;
|
||
|
|
||
|
if (len == 5 && strncmp(buf, "@type", len) == 0) {
|
||
|
upb_strtable_lookup2(p->top->name_table, "type_url", 8, &v);
|
||
|
p->top->f = upb_value_getconstptr(v);
|
||
|
multipart_end(p);
|
||
|
return true;
|
||
|
} else {
|
||
|
p->top->is_unknown_field = true;
|
||
|
multipart_end(p);
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
static void end_member(upb_json_parser *p) {
|
||
10 years ago
|
/* If we just parsed a map-entry value, end that frame too. */
|
||
10 years ago
|
if (p->top->is_mapentry) {
|
||
10 years ago
|
upb_selector_t sel;
|
||
|
bool ok;
|
||
|
const upb_fielddef *mapfield;
|
||
|
|
||
9 years ago
|
UPB_ASSERT(p->top > p->stack);
|
||
10 years ago
|
/* send ENDMSG on submsg. */
|
||
6 years ago
|
upb_sink_endmsg(p->top->sink, p->status);
|
||
10 years ago
|
mapfield = p->top->mapfield;
|
||
10 years ago
|
|
||
10 years ago
|
/* send ENDSUBMSG in repeated-field-of-mapentries frame. */
|
||
10 years ago
|
p->top--;
|
||
10 years ago
|
ok = upb_handlers_getselector(mapfield, UPB_HANDLER_ENDSUBMSG, &sel);
|
||
9 years ago
|
UPB_ASSERT(ok);
|
||
6 years ago
|
upb_sink_endsubmsg(p->top->sink, sel);
|
||
10 years ago
|
}
|
||
|
|
||
|
p->top->f = NULL;
|
||
6 years ago
|
p->top->is_unknown_field = false;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
6 years ago
|
static void start_any_member(upb_json_parser *p, const char *ptr) {
|
||
|
start_member(p);
|
||
|
json_parser_any_frame_set_after_type_url_start_once(p->top->any_frame, ptr);
|
||
|
}
|
||
|
|
||
|
static void end_any_member(upb_json_parser *p, const char *ptr) {
|
||
|
json_parser_any_frame_set_before_type_url_end(p->top->any_frame, ptr);
|
||
|
end_member(p);
|
||
|
}
|
||
|
|
||
10 years ago
|
static bool start_subobject(upb_json_parser *p) {
|
||
6 years ago
|
if (p->top->is_unknown_field) {
|
||
7 years ago
|
if (!check_stack(p)) return false;
|
||
|
|
||
6 years ago
|
p->top = start_jsonparser_frame(p);
|
||
7 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (upb_fielddef_ismap(p->top->f)) {
|
||
10 years ago
|
upb_jsonparser_frame *inner;
|
||
|
upb_selector_t sel;
|
||
|
|
||
|
/* Beginning of a map. Start a new parser frame in a repeated-field
|
||
|
* context. */
|
||
10 years ago
|
if (!check_stack(p)) return false;
|
||
|
|
||
6 years ago
|
inner = start_jsonparser_frame(p);
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
|
||
6 years ago
|
upb_sink_startseq(p->top->sink, sel, &inner->sink);
|
||
10 years ago
|
inner->m = upb_fielddef_msgsubdef(p->top->f);
|
||
|
inner->mapfield = p->top->f;
|
||
|
inner->is_map = true;
|
||
|
p->top = inner;
|
||
|
|
||
|
return true;
|
||
|
} else if (upb_fielddef_issubmsg(p->top->f)) {
|
||
10 years ago
|
upb_jsonparser_frame *inner;
|
||
|
upb_selector_t sel;
|
||
|
|
||
|
/* Beginning of a subobject. Start a new parser frame in the submsg
|
||
|
* context. */
|
||
10 years ago
|
if (!check_stack(p)) return false;
|
||
|
|
||
6 years ago
|
inner = start_jsonparser_frame(p);
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
|
||
6 years ago
|
upb_sink_startsubmsg(p->top->sink, sel, &inner->sink);
|
||
10 years ago
|
inner->m = upb_fielddef_msgsubdef(p->top->f);
|
||
9 years ago
|
set_name_table(p, inner);
|
||
10 years ago
|
p->top = inner;
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
|
||
|
p->top->is_any = true;
|
||
6 years ago
|
p->top->any_frame = json_parser_any_frame_new(p);
|
||
6 years ago
|
} else {
|
||
|
p->top->is_any = false;
|
||
|
p->top->any_frame = NULL;
|
||
|
}
|
||
|
|
||
10 years ago
|
return true;
|
||
|
} else {
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
10 years ago
|
"Object specified for non-message/group field: %s",
|
||
|
upb_fielddef_name(p->top->f));
|
||
|
return false;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool start_subobject_full(upb_json_parser *p) {
|
||
|
if (is_top_level(p)) {
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
start_value_object(p, VALUE_STRUCTVALUE);
|
||
|
if (!start_subobject(p)) return false;
|
||
|
start_structvalue_object(p);
|
||
6 years ago
|
} else if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) {
|
||
6 years ago
|
start_structvalue_object(p);
|
||
|
} else {
|
||
|
return true;
|
||
|
}
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_STRUCT)) {
|
||
6 years ago
|
if (!start_subobject(p)) return false;
|
||
|
start_structvalue_object(p);
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
if (!start_subobject(p)) return false;
|
||
|
start_value_object(p, VALUE_STRUCTVALUE);
|
||
|
if (!start_subobject(p)) return false;
|
||
|
start_structvalue_object(p);
|
||
|
}
|
||
|
|
||
|
return start_subobject(p);
|
||
|
}
|
||
|
|
||
10 years ago
|
static void end_subobject(upb_json_parser *p) {
|
||
6 years ago
|
if (is_top_level(p)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (p->top->is_map) {
|
||
10 years ago
|
upb_selector_t sel;
|
||
10 years ago
|
p->top--;
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
|
||
6 years ago
|
upb_sink_endseq(p->top->sink, sel);
|
||
10 years ago
|
} else {
|
||
10 years ago
|
upb_selector_t sel;
|
||
7 years ago
|
bool is_unknown = p->top->m == NULL;
|
||
10 years ago
|
p->top--;
|
||
7 years ago
|
if (!is_unknown) {
|
||
|
sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG);
|
||
6 years ago
|
upb_sink_endsubmsg(p->top->sink, sel);
|
||
7 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
6 years ago
|
static void end_subobject_full(upb_json_parser *p) {
|
||
|
end_subobject(p);
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_STRUCT)) {
|
||
6 years ago
|
end_structvalue_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
end_value_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
static bool start_array(upb_json_parser *p) {
|
||
10 years ago
|
upb_jsonparser_frame *inner;
|
||
|
upb_selector_t sel;
|
||
|
|
||
6 years ago
|
if (is_top_level(p)) {
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
start_value_object(p, VALUE_LISTVALUE);
|
||
|
if (!start_subobject(p)) return false;
|
||
|
start_listvalue_object(p);
|
||
6 years ago
|
} else if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) {
|
||
6 years ago
|
start_listvalue_object(p);
|
||
|
} else {
|
||
|
return false;
|
||
|
}
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_LISTVALUE) &&
|
||
|
(!upb_fielddef_isseq(p->top->f) ||
|
||
|
p->top->is_repeated)) {
|
||
6 years ago
|
if (!start_subobject(p)) return false;
|
||
|
start_listvalue_object(p);
|
||
6 years ago
|
} else if (is_wellknown_field(p, UPB_WELLKNOWN_VALUE) &&
|
||
|
(!upb_fielddef_isseq(p->top->f) ||
|
||
|
p->top->is_repeated)) {
|
||
6 years ago
|
if (!start_subobject(p)) return false;
|
||
|
start_value_object(p, VALUE_LISTVALUE);
|
||
|
if (!start_subobject(p)) return false;
|
||
|
start_listvalue_object(p);
|
||
|
}
|
||
|
|
||
6 years ago
|
if (p->top->is_unknown_field) {
|
||
6 years ago
|
inner = start_jsonparser_frame(p);
|
||
6 years ago
|
inner->is_unknown_field = true;
|
||
|
p->top = inner;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
10 years ago
|
|
||
|
if (!upb_fielddef_isseq(p->top->f)) {
|
||
6 years ago
|
upb_status_seterrf(p->status,
|
||
10 years ago
|
"Array specified for non-repeated field: %s",
|
||
|
upb_fielddef_name(p->top->f));
|
||
|
return false;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
if (!check_stack(p)) return false;
|
||
|
|
||
6 years ago
|
inner = start_jsonparser_frame(p);
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
|
||
6 years ago
|
upb_sink_startseq(p->top->sink, sel, &inner->sink);
|
||
10 years ago
|
inner->m = p->top->m;
|
||
|
inner->f = p->top->f;
|
||
6 years ago
|
inner->is_repeated = true;
|
||
10 years ago
|
p->top = inner;
|
||
|
|
||
|
return true;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static void end_array(upb_json_parser *p) {
|
||
10 years ago
|
upb_selector_t sel;
|
||
|
|
||
9 years ago
|
UPB_ASSERT(p->top > p->stack);
|
||
10 years ago
|
|
||
|
p->top--;
|
||
6 years ago
|
|
||
|
if (p->top->is_unknown_field) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
|
||
6 years ago
|
upb_sink_endseq(p->top->sink, sel);
|
||
6 years ago
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_LISTVALUE)) {
|
||
6 years ago
|
end_listvalue_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_VALUE)) {
|
||
6 years ago
|
end_value_object(p);
|
||
|
if (!is_top_level(p)) {
|
||
|
end_subobject(p);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
static void start_object(upb_json_parser *p) {
|
||
6 years ago
|
if (!p->top->is_map && p->top->m != NULL) {
|
||
6 years ago
|
upb_sink_startmsg(p->top->sink);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
static void end_object(upb_json_parser *p) {
|
||
6 years ago
|
if (!p->top->is_map && p->top->m != NULL) {
|
||
6 years ago
|
upb_sink_endmsg(p->top->sink, p->status);
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
|
|
||
6 years ago
|
static void start_any_object(upb_json_parser *p, const char *ptr) {
|
||
|
start_object(p);
|
||
|
p->top->any_frame->before_type_url_start = ptr;
|
||
|
p->top->any_frame->before_type_url_end = ptr;
|
||
|
}
|
||
|
|
||
|
static bool end_any_object(upb_json_parser *p, const char *ptr) {
|
||
|
const char *value_membername = "value";
|
||
|
bool is_well_known_packed = false;
|
||
|
const char *packed_end = ptr + 1;
|
||
|
upb_selector_t sel;
|
||
|
upb_jsonparser_frame *inner;
|
||
|
|
||
|
if (json_parser_any_frame_has_value(p->top->any_frame) &&
|
||
|
!json_parser_any_frame_has_type_url(p->top->any_frame)) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "No valid type url");
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
/* Well known types data is represented as value field. */
|
||
|
if (upb_msgdef_wellknowntype(p->top->any_frame->parser->top->m) !=
|
||
|
UPB_WELLKNOWN_UNSPECIFIED) {
|
||
|
is_well_known_packed = true;
|
||
|
|
||
|
if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) {
|
||
|
p->top->any_frame->before_type_url_start =
|
||
|
memchr(p->top->any_frame->before_type_url_start, ':',
|
||
|
p->top->any_frame->before_type_url_end -
|
||
|
p->top->any_frame->before_type_url_start);
|
||
|
if (p->top->any_frame->before_type_url_start == NULL) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "invalid data for well known type.");
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
p->top->any_frame->before_type_url_start++;
|
||
|
}
|
||
|
|
||
|
if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) {
|
||
|
p->top->any_frame->after_type_url_start =
|
||
|
memchr(p->top->any_frame->after_type_url_start, ':',
|
||
|
(ptr + 1) -
|
||
|
p->top->any_frame->after_type_url_start);
|
||
|
if (p->top->any_frame->after_type_url_start == NULL) {
|
||
6 years ago
|
upb_status_seterrmsg(p->status, "Invalid data for well known type.");
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
p->top->any_frame->after_type_url_start++;
|
||
|
packed_end = ptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame)) {
|
||
|
if (!parse(p->top->any_frame->parser, NULL,
|
||
|
p->top->any_frame->before_type_url_start,
|
||
|
p->top->any_frame->before_type_url_end -
|
||
|
p->top->any_frame->before_type_url_start, NULL)) {
|
||
|
return false;
|
||
|
}
|
||
|
} else {
|
||
|
if (!is_well_known_packed) {
|
||
|
if (!parse(p->top->any_frame->parser, NULL, "{", 1, NULL)) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (json_parser_any_frame_has_value_before_type_url(p->top->any_frame) &&
|
||
|
json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) {
|
||
|
if (!parse(p->top->any_frame->parser, NULL, ",", 1, NULL)) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (json_parser_any_frame_has_value_after_type_url(p->top->any_frame)) {
|
||
|
if (!parse(p->top->any_frame->parser, NULL,
|
||
|
p->top->any_frame->after_type_url_start,
|
||
|
packed_end - p->top->any_frame->after_type_url_start, NULL)) {
|
||
|
return false;
|
||
|
}
|
||
|
} else {
|
||
|
if (!is_well_known_packed) {
|
||
|
if (!parse(p->top->any_frame->parser, NULL, "}", 1, NULL)) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!end(p->top->any_frame->parser, NULL)) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
p->top->is_any = false;
|
||
|
|
||
|
/* Set value */
|
||
|
start_member(p);
|
||
|
capture_begin(p, value_membername);
|
||
|
capture_end(p, value_membername + 5);
|
||
|
end_membername(p);
|
||
|
|
||
|
if (!check_stack(p)) return false;
|
||
|
inner = p->top + 1;
|
||
|
|
||
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
|
||
6 years ago
|
upb_sink_startstr(p->top->sink, sel, 0, &inner->sink);
|
||
6 years ago
|
sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
|
||
6 years ago
|
upb_sink_putstring(inner->sink, sel, p->top->any_frame->stringsink.ptr,
|
||
6 years ago
|
p->top->any_frame->stringsink.len, NULL);
|
||
|
sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
|
||
6 years ago
|
upb_sink_endstr(inner->sink, sel);
|
||
6 years ago
|
|
||
|
end_member(p);
|
||
|
|
||
|
end_object(p);
|
||
|
|
||
|
/* Deallocate any parse frame. */
|
||
|
json_parser_any_frame_free(p->top->any_frame);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool is_string_wrapper(const upb_msgdef *m) {
|
||
6 years ago
|
upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
|
||
|
return type == UPB_WELLKNOWN_STRINGVALUE ||
|
||
|
type == UPB_WELLKNOWN_BYTESVALUE;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
static bool is_fieldmask(const upb_msgdef *m) {
|
||
|
upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
|
||
|
return type == UPB_WELLKNOWN_FIELDMASK;
|
||
|
}
|
||
|
|
||
|
static void start_fieldmask_object(upb_json_parser *p) {
|
||
|
const char *membername = "paths";
|
||
|
|
||
|
start_object(p);
|
||
|
|
||
|
/* Set up context for parsing value */
|
||
|
start_member(p);
|
||
|
capture_begin(p, membername);
|
||
|
capture_end(p, membername + 5);
|
||
|
end_membername(p);
|
||
|
|
||
|
start_array(p);
|
||
|
}
|
||
|
|
||
|
static void end_fieldmask_object(upb_json_parser *p) {
|
||
|
end_array(p);
|
||
|
end_member(p);
|
||
|
end_object(p);
|
||
|
}
|
||
|
|
||
6 years ago
|
static void start_wrapper_object(upb_json_parser *p) {
|
||
|
const char *membername = "value";
|
||
|
|
||
|
start_object(p);
|
||
|
|
||
|
/* Set up context for parsing value */
|
||
|
start_member(p);
|
||
|
capture_begin(p, membername);
|
||
|
capture_end(p, membername + 5);
|
||
|
end_membername(p);
|
||
|
}
|
||
|
|
||
|
static void end_wrapper_object(upb_json_parser *p) {
|
||
|
end_member(p);
|
||
|
end_object(p);
|
||
|
}
|
||
|
|
||
6 years ago
|
static void start_value_object(upb_json_parser *p, int value_type) {
|
||
|
const char *nullmember = "null_value";
|
||
|
const char *numbermember = "number_value";
|
||
|
const char *stringmember = "string_value";
|
||
|
const char *boolmember = "bool_value";
|
||
|
const char *structmember = "struct_value";
|
||
|
const char *listmember = "list_value";
|
||
6 years ago
|
const char *membername = "";
|
||
6 years ago
|
|
||
|
switch (value_type) {
|
||
|
case VALUE_NULLVALUE:
|
||
|
membername = nullmember;
|
||
|
break;
|
||
|
case VALUE_NUMBERVALUE:
|
||
|
membername = numbermember;
|
||
|
break;
|
||
|
case VALUE_STRINGVALUE:
|
||
|
membername = stringmember;
|
||
|
break;
|
||
|
case VALUE_BOOLVALUE:
|
||
|
membername = boolmember;
|
||
|
break;
|
||
|
case VALUE_STRUCTVALUE:
|
||
|
membername = structmember;
|
||
|
break;
|
||
|
case VALUE_LISTVALUE:
|
||
|
membername = listmember;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
start_object(p);
|
||
|
|
||
|
/* Set up context for parsing value */
|
||
|
start_member(p);
|
||
|
capture_begin(p, membername);
|
||
|
capture_end(p, membername + strlen(membername));
|
||
|
end_membername(p);
|
||
|
}
|
||
|
|
||
|
static void end_value_object(upb_json_parser *p) {
|
||
|
end_member(p);
|
||
|
end_object(p);
|
||
|
}
|
||
|
|
||
|
static void start_listvalue_object(upb_json_parser *p) {
|
||
|
const char *membername = "values";
|
||
|
|
||
|
start_object(p);
|
||
|
|
||
|
/* Set up context for parsing value */
|
||
|
start_member(p);
|
||
|
capture_begin(p, membername);
|
||
|
capture_end(p, membername + strlen(membername));
|
||
|
end_membername(p);
|
||
|
}
|
||
|
|
||
|
static void end_listvalue_object(upb_json_parser *p) {
|
||
|
end_member(p);
|
||
|
end_object(p);
|
||
|
}
|
||
|
|
||
|
static void start_structvalue_object(upb_json_parser *p) {
|
||
|
const char *membername = "fields";
|
||
|
|
||
|
start_object(p);
|
||
|
|
||
|
/* Set up context for parsing value */
|
||
|
start_member(p);
|
||
|
capture_begin(p, membername);
|
||
|
capture_end(p, membername + strlen(membername));
|
||
|
end_membername(p);
|
||
|
}
|
||
|
|
||
|
static void end_structvalue_object(upb_json_parser *p) {
|
||
|
end_member(p);
|
||
|
end_object(p);
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool is_top_level(upb_json_parser *p) {
|
||
6 years ago
|
return p->top == p->stack && p->top->f == NULL && !p->top->is_unknown_field;
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
static bool is_wellknown_msg(upb_json_parser *p, upb_wellknowntype_t type) {
|
||
|
return p->top->m != NULL && upb_msgdef_wellknowntype(p->top->m) == type;
|
||
|
}
|
||
|
|
||
|
static bool is_wellknown_field(upb_json_parser *p, upb_wellknowntype_t type) {
|
||
|
return p->top->f != NULL &&
|
||
|
upb_fielddef_issubmsg(p->top->f) &&
|
||
|
(upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(p->top->f))
|
||
|
== type);
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool does_number_wrapper_start(upb_json_parser *p) {
|
||
|
return p->top->f != NULL &&
|
||
|
upb_fielddef_issubmsg(p->top->f) &&
|
||
6 years ago
|
upb_msgdef_isnumberwrapper(upb_fielddef_msgsubdef(p->top->f));
|
||
6 years ago
|
}
|
||
|
|
||
|
static bool does_number_wrapper_end(upb_json_parser *p) {
|
||
6 years ago
|
return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m);
|
||
6 years ago
|
}
|
||
|
|
||
|
static bool is_number_wrapper_object(upb_json_parser *p) {
|
||
6 years ago
|
return p->top->m != NULL && upb_msgdef_isnumberwrapper(p->top->m);
|
||
6 years ago
|
}
|
||
|
|
||
|
static bool does_string_wrapper_start(upb_json_parser *p) {
|
||
|
return p->top->f != NULL &&
|
||
|
upb_fielddef_issubmsg(p->top->f) &&
|
||
|
is_string_wrapper(upb_fielddef_msgsubdef(p->top->f));
|
||
|
}
|
||
|
|
||
|
static bool does_string_wrapper_end(upb_json_parser *p) {
|
||
|
return p->top->m != NULL && is_string_wrapper(p->top->m);
|
||
|
}
|
||
|
|
||
|
static bool is_string_wrapper_object(upb_json_parser *p) {
|
||
|
return p->top->m != NULL && is_string_wrapper(p->top->m);
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool does_fieldmask_start(upb_json_parser *p) {
|
||
|
return p->top->f != NULL &&
|
||
|
upb_fielddef_issubmsg(p->top->f) &&
|
||
|
is_fieldmask(upb_fielddef_msgsubdef(p->top->f));
|
||
|
}
|
||
|
|
||
|
static bool does_fieldmask_end(upb_json_parser *p) {
|
||
|
return p->top->m != NULL && is_fieldmask(p->top->m);
|
||
|
}
|
||
|
|
||
10 years ago
|
#define CHECK_RETURN_TOP(x) if (!(x)) goto error
|
||
|
|
||
10 years ago
|
|
||
|
/* The actual parser **********************************************************/
|
||
|
|
||
10 years ago
|
/* What follows is the Ragel parser itself. The language is specified in Ragel
|
||
|
* and the actions call our C functions above.
|
||
|
*
|
||
|
* Ragel has an extensive set of functionality, and we use only a small part of
|
||
|
* it. There are many action types but we only use a few:
|
||
|
*
|
||
|
* ">" -- transition into a machine
|
||
|
* "%" -- transition out of a machine
|
||
|
* "@" -- transition into a final state of a machine.
|
||
|
*
|
||
|
* "@" transitions are tricky because a machine can transition into a final
|
||
|
* state repeatedly. But in some cases we know this can't happen, for example
|
||
|
* a string which is delimited by a final '"' can only transition into its
|
||
|
* final state once, when the closing '"' is seen. */
|
||
10 years ago
|
|
||
10 years ago
|
|
||
6 years ago
|
#line 2749 "upb/json/parser.rl"
|
||
10 years ago
|
|
||
|
|
||
|
|
||
6 years ago
|
#line 2552 "upb/json/parser.c"
|
||
10 years ago
|
static const char _json_actions[] = {
|
||
6 years ago
|
0, 1, 0, 1, 1, 1, 3, 1,
|
||
|
4, 1, 6, 1, 7, 1, 8, 1,
|
||
6 years ago
|
9, 1, 11, 1, 12, 1, 13, 1,
|
||
|
14, 1, 15, 1, 16, 1, 17, 1,
|
||
|
18, 1, 19, 1, 20, 1, 22, 1,
|
||
|
23, 1, 24, 1, 35, 1, 37, 1,
|
||
|
39, 1, 40, 1, 42, 1, 43, 1,
|
||
|
44, 1, 46, 1, 48, 1, 49, 1,
|
||
|
50, 1, 51, 1, 53, 1, 54, 2,
|
||
|
4, 9, 2, 5, 6, 2, 7, 3,
|
||
|
2, 7, 9, 2, 21, 26, 2, 25,
|
||
|
10, 2, 27, 28, 2, 29, 30, 2,
|
||
|
32, 34, 2, 33, 31, 2, 38, 36,
|
||
|
2, 40, 42, 2, 45, 2, 2, 46,
|
||
|
54, 2, 47, 36, 2, 49, 54, 2,
|
||
|
50, 54, 2, 51, 54, 2, 52, 41,
|
||
|
2, 53, 54, 3, 32, 34, 35, 4,
|
||
|
21, 26, 27, 28
|
||
10 years ago
|
};
|
||
|
|
||
6 years ago
|
static const short _json_key_offsets[] = {
|
||
6 years ago
|
0, 0, 12, 13, 18, 23, 28, 29,
|
||
|
30, 31, 32, 33, 34, 35, 36, 37,
|
||
6 years ago
|
38, 43, 44, 48, 53, 58, 63, 67,
|
||
|
71, 74, 77, 79, 83, 87, 89, 91,
|
||
|
96, 98, 100, 109, 115, 121, 127, 133,
|
||
|
135, 139, 142, 144, 146, 149, 150, 154,
|
||
|
156, 158, 160, 162, 163, 165, 167, 168,
|
||
|
170, 172, 173, 175, 177, 178, 180, 182,
|
||
|
183, 185, 187, 191, 193, 195, 196, 197,
|
||
6 years ago
|
198, 199, 201, 206, 208, 210, 212, 221,
|
||
|
222, 222, 222, 227, 232, 237, 238, 239,
|
||
|
240, 241, 241, 242, 243, 244, 244, 245,
|
||
|
246, 247, 247, 252, 253, 257, 262, 267,
|
||
|
272, 276, 276, 279, 282, 285, 288, 291,
|
||
|
294, 294, 294, 294, 294, 294
|
||
10 years ago
|
};
|
||
|
|
||
|
static const char _json_trans_keys[] = {
|
||
6 years ago
|
32, 34, 45, 91, 102, 110, 116, 123,
|
||
|
9, 13, 48, 57, 34, 32, 93, 125,
|
||
|
9, 13, 32, 44, 93, 9, 13, 32,
|
||
|
93, 125, 9, 13, 97, 108, 115, 101,
|
||
|
117, 108, 108, 114, 117, 101, 32, 34,
|
||
6 years ago
|
125, 9, 13, 34, 32, 58, 9, 13,
|
||
|
32, 93, 125, 9, 13, 32, 44, 125,
|
||
6 years ago
|
9, 13, 32, 44, 125, 9, 13, 32,
|
||
6 years ago
|
34, 9, 13, 45, 48, 49, 57, 48,
|
||
|
49, 57, 46, 69, 101, 48, 57, 69,
|
||
|
101, 48, 57, 43, 45, 48, 57, 48,
|
||
|
57, 48, 57, 46, 69, 101, 48, 57,
|
||
|
34, 92, 34, 92, 34, 47, 92, 98,
|
||
|
102, 110, 114, 116, 117, 48, 57, 65,
|
||
|
70, 97, 102, 48, 57, 65, 70, 97,
|
||
|
102, 48, 57, 65, 70, 97, 102, 48,
|
||
|
57, 65, 70, 97, 102, 34, 92, 45,
|
||
|
48, 49, 57, 48, 49, 57, 46, 115,
|
||
|
48, 57, 115, 48, 57, 34, 46, 115,
|
||
|
48, 57, 48, 57, 48, 57, 48, 57,
|
||
|
48, 57, 45, 48, 57, 48, 57, 45,
|
||
|
48, 57, 48, 57, 84, 48, 57, 48,
|
||
|
57, 58, 48, 57, 48, 57, 58, 48,
|
||
|
57, 48, 57, 43, 45, 46, 90, 48,
|
||
|
57, 48, 57, 58, 48, 48, 34, 48,
|
||
6 years ago
|
57, 43, 45, 90, 48, 57, 34, 44,
|
||
|
34, 44, 34, 44, 34, 45, 91, 102,
|
||
|
110, 116, 123, 48, 57, 34, 32, 93,
|
||
|
125, 9, 13, 32, 44, 93, 9, 13,
|
||
|
32, 93, 125, 9, 13, 97, 108, 115,
|
||
|
101, 117, 108, 108, 114, 117, 101, 32,
|
||
|
34, 125, 9, 13, 34, 32, 58, 9,
|
||
|
13, 32, 93, 125, 9, 13, 32, 44,
|
||
|
125, 9, 13, 32, 44, 125, 9, 13,
|
||
|
32, 34, 9, 13, 32, 9, 13, 32,
|
||
6 years ago
|
9, 13, 32, 9, 13, 32, 9, 13,
|
||
6 years ago
|
32, 9, 13, 32, 9, 13, 0
|
||
10 years ago
|
};
|
||
|
|
||
|
static const char _json_single_lengths[] = {
|
||
6 years ago
|
0, 8, 1, 3, 3, 3, 1, 1,
|
||
|
1, 1, 1, 1, 1, 1, 1, 1,
|
||
6 years ago
|
3, 1, 2, 3, 3, 3, 2, 2,
|
||
|
1, 3, 0, 2, 2, 0, 0, 3,
|
||
|
2, 2, 9, 0, 0, 0, 0, 2,
|
||
|
2, 1, 2, 0, 1, 1, 2, 0,
|
||
|
0, 0, 0, 1, 0, 0, 1, 0,
|
||
|
0, 1, 0, 0, 1, 0, 0, 1,
|
||
|
0, 0, 4, 0, 0, 1, 1, 1,
|
||
6 years ago
|
1, 0, 3, 2, 2, 2, 7, 1,
|
||
|
0, 0, 3, 3, 3, 1, 1, 1,
|
||
|
1, 0, 1, 1, 1, 0, 1, 1,
|
||
|
1, 0, 3, 1, 2, 3, 3, 3,
|
||
|
2, 0, 1, 1, 1, 1, 1, 1,
|
||
|
0, 0, 0, 0, 0, 0
|
||
10 years ago
|
};
|
||
|
|
||
|
static const char _json_range_lengths[] = {
|
||
6 years ago
|
0, 2, 0, 1, 1, 1, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
6 years ago
|
1, 0, 1, 1, 1, 1, 1, 1,
|
||
|
1, 0, 1, 1, 1, 1, 1, 1,
|
||
|
0, 0, 0, 3, 3, 3, 3, 0,
|
||
6 years ago
|
1, 1, 0, 1, 1, 0, 1, 1,
|
||
6 years ago
|
1, 1, 1, 0, 1, 1, 0, 1,
|
||
|
1, 0, 1, 1, 0, 1, 1, 0,
|
||
|
1, 1, 0, 1, 1, 0, 0, 0,
|
||
6 years ago
|
0, 1, 1, 0, 0, 0, 1, 0,
|
||
|
0, 0, 1, 1, 1, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 1, 0, 1, 1, 1, 1,
|
||
|
1, 0, 1, 1, 1, 1, 1, 1,
|
||
|
0, 0, 0, 0, 0, 0
|
||
10 years ago
|
};
|
||
|
|
||
|
static const short _json_index_offsets[] = {
|
||
6 years ago
|
0, 0, 11, 13, 18, 23, 28, 30,
|
||
|
32, 34, 36, 38, 40, 42, 44, 46,
|
||
6 years ago
|
48, 53, 55, 59, 64, 69, 74, 78,
|
||
|
82, 85, 89, 91, 95, 99, 101, 103,
|
||
|
108, 111, 114, 124, 128, 132, 136, 140,
|
||
|
143, 147, 150, 153, 155, 158, 160, 164,
|
||
|
166, 168, 170, 172, 174, 176, 178, 180,
|
||
|
182, 184, 186, 188, 190, 192, 194, 196,
|
||
|
198, 200, 202, 207, 209, 211, 213, 215,
|
||
6 years ago
|
217, 219, 221, 226, 229, 232, 235, 244,
|
||
|
246, 247, 248, 253, 258, 263, 265, 267,
|
||
|
269, 271, 272, 274, 276, 278, 279, 281,
|
||
|
283, 285, 286, 291, 293, 297, 302, 307,
|
||
|
312, 316, 317, 320, 323, 326, 329, 332,
|
||
|
335, 336, 337, 338, 339, 340
|
||
10 years ago
|
};
|
||
|
|
||
6 years ago
|
static const unsigned char _json_indicies[] = {
|
||
6 years ago
|
0, 2, 3, 4, 5, 6, 7, 8,
|
||
|
0, 3, 1, 9, 1, 11, 12, 1,
|
||
|
11, 10, 13, 14, 12, 13, 1, 14,
|
||
|
1, 1, 14, 10, 15, 1, 16, 1,
|
||
|
17, 1, 18, 1, 19, 1, 20, 1,
|
||
|
21, 1, 22, 1, 23, 1, 24, 1,
|
||
6 years ago
|
25, 26, 27, 25, 1, 28, 1, 29,
|
||
|
30, 29, 1, 30, 1, 1, 30, 31,
|
||
|
32, 33, 34, 32, 1, 35, 36, 27,
|
||
|
35, 1, 36, 26, 36, 1, 37, 38,
|
||
|
39, 1, 38, 39, 1, 41, 42, 42,
|
||
|
40, 43, 1, 42, 42, 43, 40, 44,
|
||
|
44, 45, 1, 45, 1, 45, 40, 41,
|
||
|
42, 42, 39, 40, 47, 48, 46, 50,
|
||
|
51, 49, 52, 52, 52, 52, 52, 52,
|
||
|
52, 52, 53, 1, 54, 54, 54, 1,
|
||
|
55, 55, 55, 1, 56, 56, 56, 1,
|
||
|
57, 57, 57, 1, 59, 60, 58, 61,
|
||
|
62, 63, 1, 64, 65, 1, 66, 67,
|
||
|
1, 68, 1, 67, 68, 1, 69, 1,
|
||
|
66, 67, 65, 1, 70, 1, 71, 1,
|
||
|
72, 1, 73, 1, 74, 1, 75, 1,
|
||
|
76, 1, 77, 1, 78, 1, 79, 1,
|
||
|
80, 1, 81, 1, 82, 1, 83, 1,
|
||
|
84, 1, 85, 1, 86, 1, 87, 1,
|
||
|
88, 1, 89, 89, 90, 91, 1, 92,
|
||
|
1, 93, 1, 94, 1, 95, 1, 96,
|
||
|
1, 97, 1, 98, 1, 99, 99, 100,
|
||
6 years ago
|
98, 1, 102, 1, 101, 104, 105, 103,
|
||
|
1, 1, 101, 106, 107, 108, 109, 110,
|
||
|
111, 112, 107, 1, 113, 1, 114, 115,
|
||
|
117, 118, 1, 117, 116, 119, 120, 118,
|
||
|
119, 1, 120, 1, 1, 120, 116, 121,
|
||
|
1, 122, 1, 123, 1, 124, 1, 125,
|
||
|
126, 1, 127, 1, 128, 1, 129, 130,
|
||
|
1, 131, 1, 132, 1, 133, 134, 135,
|
||
|
136, 134, 1, 137, 1, 138, 139, 138,
|
||
|
1, 139, 1, 1, 139, 140, 141, 142,
|
||
|
143, 141, 1, 144, 145, 136, 144, 1,
|
||
|
145, 135, 145, 1, 146, 147, 147, 1,
|
||
|
148, 148, 1, 149, 149, 1, 150, 150,
|
||
|
1, 151, 151, 1, 152, 152, 1, 1,
|
||
|
1, 1, 1, 1, 1, 0
|
||
10 years ago
|
};
|
||
|
|
||
|
static const char _json_trans_targs[] = {
|
||
6 years ago
|
1, 0, 2, 107, 3, 6, 10, 13,
|
||
|
16, 106, 4, 3, 106, 4, 5, 7,
|
||
|
8, 9, 108, 11, 12, 109, 14, 15,
|
||
|
110, 16, 17, 111, 18, 18, 19, 20,
|
||
|
21, 22, 111, 21, 22, 24, 25, 31,
|
||
|
112, 26, 28, 27, 29, 30, 33, 113,
|
||
|
34, 33, 113, 34, 32, 35, 36, 37,
|
||
|
38, 39, 33, 113, 34, 41, 42, 46,
|
||
|
42, 46, 43, 45, 44, 114, 48, 49,
|
||
6 years ago
|
50, 51, 52, 53, 54, 55, 56, 57,
|
||
|
58, 59, 60, 61, 62, 63, 64, 65,
|
||
|
66, 67, 73, 72, 68, 69, 70, 71,
|
||
6 years ago
|
72, 115, 74, 67, 72, 76, 116, 76,
|
||
|
116, 77, 79, 81, 82, 85, 90, 94,
|
||
|
98, 80, 117, 117, 83, 82, 80, 83,
|
||
|
84, 86, 87, 88, 89, 117, 91, 92,
|
||
|
93, 117, 95, 96, 97, 117, 98, 99,
|
||
|
105, 100, 100, 101, 102, 103, 104, 105,
|
||
|
103, 104, 117, 106, 106, 106, 106, 106,
|
||
|
106
|
||
10 years ago
|
};
|
||
|
|
||
6 years ago
|
static const unsigned char _json_trans_actions[] = {
|
||
|
0, 0, 113, 107, 53, 0, 0, 0,
|
||
|
125, 59, 45, 0, 55, 0, 0, 0,
|
||
6 years ago
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
6 years ago
|
0, 0, 101, 51, 47, 0, 0, 45,
|
||
|
49, 49, 104, 0, 0, 0, 0, 0,
|
||
6 years ago
|
3, 0, 0, 0, 0, 0, 5, 15,
|
||
6 years ago
|
0, 0, 71, 7, 13, 0, 74, 9,
|
||
|
9, 9, 77, 80, 11, 37, 37, 37,
|
||
|
0, 0, 0, 39, 0, 41, 86, 0,
|
||
|
0, 0, 17, 19, 0, 21, 23, 0,
|
||
|
25, 27, 0, 29, 31, 0, 33, 35,
|
||
|
0, 135, 83, 135, 0, 0, 0, 0,
|
||
|
0, 92, 0, 89, 89, 98, 43, 0,
|
||
|
131, 95, 113, 107, 53, 0, 0, 0,
|
||
|
125, 59, 69, 110, 45, 0, 55, 0,
|
||
|
0, 0, 0, 0, 0, 119, 0, 0,
|
||
|
0, 122, 0, 0, 0, 116, 0, 101,
|
||
|
51, 47, 0, 0, 45, 49, 49, 104,
|
||
|
0, 0, 128, 0, 57, 63, 65, 61,
|
||
|
67
|
||
6 years ago
|
};
|
||
|
|
||
6 years ago
|
static const unsigned char _json_eof_actions[] = {
|
||
6 years ago
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
6 years ago
|
0, 1, 0, 1, 0, 0, 1, 1,
|
||
6 years ago
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
6 years ago
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
6 years ago
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
6 years ago
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||
6 years ago
|
0, 0, 0, 57, 63, 65, 61, 67,
|
||
6 years ago
|
0, 0, 0, 0, 0, 0
|
||
10 years ago
|
};
|
||
|
|
||
|
static const int json_start = 1;
|
||
|
|
||
6 years ago
|
static const int json_en_number_machine = 23;
|
||
|
static const int json_en_string_machine = 32;
|
||
|
static const int json_en_duration_machine = 40;
|
||
|
static const int json_en_timestamp_machine = 47;
|
||
6 years ago
|
static const int json_en_fieldmask_machine = 75;
|
||
|
static const int json_en_value_machine = 78;
|
||
10 years ago
|
static const int json_en_main = 1;
|
||
|
|
||
|
|
||
6 years ago
|
#line 2752 "upb/json/parser.rl"
|
||
10 years ago
|
|
||
|
size_t parse(void *closure, const void *hd, const char *buf, size_t size,
|
||
|
const upb_bufhandle *handle) {
|
||
|
upb_json_parser *parser = closure;
|
||
|
|
||
10 years ago
|
/* Variables used by Ragel's generated code. */
|
||
10 years ago
|
int cs = parser->current_state;
|
||
|
int *stack = parser->parser_stack;
|
||
|
int top = parser->parser_top;
|
||
|
|
||
|
const char *p = buf;
|
||
|
const char *pe = buf + size;
|
||
6 years ago
|
const char *eof = &eof_ch;
|
||
10 years ago
|
|
||
10 years ago
|
parser->handle = handle;
|
||
|
|
||
|
UPB_UNUSED(hd);
|
||
|
UPB_UNUSED(handle);
|
||
|
|
||
10 years ago
|
capture_resume(parser, buf);
|
||
|
|
||
10 years ago
|
|
||
6 years ago
|
#line 2830 "upb/json/parser.c"
|
||
10 years ago
|
{
|
||
|
int _klen;
|
||
|
unsigned int _trans;
|
||
|
const char *_acts;
|
||
|
unsigned int _nacts;
|
||
|
const char *_keys;
|
||
|
|
||
|
if ( p == pe )
|
||
|
goto _test_eof;
|
||
|
if ( cs == 0 )
|
||
|
goto _out;
|
||
|
_resume:
|
||
|
_keys = _json_trans_keys + _json_key_offsets[cs];
|
||
|
_trans = _json_index_offsets[cs];
|
||
|
|
||
|
_klen = _json_single_lengths[cs];
|
||
|
if ( _klen > 0 ) {
|
||
|
const char *_lower = _keys;
|
||
|
const char *_mid;
|
||
|
const char *_upper = _keys + _klen - 1;
|
||
|
while (1) {
|
||
|
if ( _upper < _lower )
|
||
|
break;
|
||
|
|
||
|
_mid = _lower + ((_upper-_lower) >> 1);
|
||
|
if ( (*p) < *_mid )
|
||
|
_upper = _mid - 1;
|
||
|
else if ( (*p) > *_mid )
|
||
|
_lower = _mid + 1;
|
||
|
else {
|
||
|
_trans += (unsigned int)(_mid - _keys);
|
||
|
goto _match;
|
||
|
}
|
||
|
}
|
||
|
_keys += _klen;
|
||
|
_trans += _klen;
|
||
|
}
|
||
|
|
||
|
_klen = _json_range_lengths[cs];
|
||
|
if ( _klen > 0 ) {
|
||
|
const char *_lower = _keys;
|
||
|
const char *_mid;
|
||
|
const char *_upper = _keys + (_klen<<1) - 2;
|
||
|
while (1) {
|
||
|
if ( _upper < _lower )
|
||
|
break;
|
||
|
|
||
|
_mid = _lower + (((_upper-_lower) >> 1) & ~1);
|
||
|
if ( (*p) < _mid[0] )
|
||
|
_upper = _mid - 2;
|
||
|
else if ( (*p) > _mid[1] )
|
||
|
_lower = _mid + 2;
|
||
|
else {
|
||
|
_trans += (unsigned int)((_mid - _keys)>>1);
|
||
|
goto _match;
|
||
|
}
|
||
|
}
|
||
|
_trans += _klen;
|
||
|
}
|
||
|
|
||
|
_match:
|
||
|
_trans = _json_indicies[_trans];
|
||
|
cs = _json_trans_targs[_trans];
|
||
|
|
||
|
if ( _json_trans_actions[_trans] == 0 )
|
||
|
goto _again;
|
||
|
|
||
|
_acts = _json_actions + _json_trans_actions[_trans];
|
||
|
_nacts = (unsigned int) *_acts++;
|
||
|
while ( _nacts-- > 0 )
|
||
|
{
|
||
|
switch ( *_acts++ )
|
||
|
{
|
||
|
case 1:
|
||
6 years ago
|
#line 2557 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {cs = stack[--top]; goto _again;} }
|
||
10 years ago
|
break;
|
||
|
case 2:
|
||
6 years ago
|
#line 2559 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {stack[top++] = cs; cs = 23;goto _again;} }
|
||
10 years ago
|
break;
|
||
|
case 3:
|
||
6 years ago
|
#line 2563 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_text(parser, p); }
|
||
10 years ago
|
break;
|
||
|
case 4:
|
||
6 years ago
|
#line 2564 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_text(parser, p)); }
|
||
10 years ago
|
break;
|
||
|
case 5:
|
||
6 years ago
|
#line 2570 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_hex(parser); }
|
||
10 years ago
|
break;
|
||
|
case 6:
|
||
6 years ago
|
#line 2571 "upb/json/parser.rl"
|
||
6 years ago
|
{ hexdigit(parser, p); }
|
||
10 years ago
|
break;
|
||
|
case 7:
|
||
6 years ago
|
#line 2572 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_hex(parser)); }
|
||
10 years ago
|
break;
|
||
|
case 8:
|
||
6 years ago
|
#line 2578 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(escape(parser, p)); }
|
||
10 years ago
|
break;
|
||
|
case 9:
|
||
6 years ago
|
#line 2584 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {cs = stack[--top]; goto _again;} }
|
||
10 years ago
|
break;
|
||
|
case 10:
|
||
6 years ago
|
#line 2589 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_year(parser, p); }
|
||
10 years ago
|
break;
|
||
|
case 11:
|
||
6 years ago
|
#line 2590 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_year(parser, p)); }
|
||
10 years ago
|
break;
|
||
|
case 12:
|
||
6 years ago
|
#line 2594 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_month(parser, p); }
|
||
10 years ago
|
break;
|
||
|
case 13:
|
||
6 years ago
|
#line 2595 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_month(parser, p)); }
|
||
10 years ago
|
break;
|
||
|
case 14:
|
||
6 years ago
|
#line 2599 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_day(parser, p); }
|
||
10 years ago
|
break;
|
||
|
case 15:
|
||
6 years ago
|
#line 2600 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_day(parser, p)); }
|
||
10 years ago
|
break;
|
||
|
case 16:
|
||
6 years ago
|
#line 2604 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_hour(parser, p); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 17:
|
||
6 years ago
|
#line 2605 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_hour(parser, p)); }
|
||
6 years ago
|
break;
|
||
|
case 18:
|
||
6 years ago
|
#line 2609 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_minute(parser, p); }
|
||
6 years ago
|
break;
|
||
|
case 19:
|
||
6 years ago
|
#line 2610 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_minute(parser, p)); }
|
||
6 years ago
|
break;
|
||
|
case 20:
|
||
6 years ago
|
#line 2614 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_second(parser, p); }
|
||
6 years ago
|
break;
|
||
|
case 21:
|
||
6 years ago
|
#line 2615 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_second(parser, p)); }
|
||
6 years ago
|
break;
|
||
|
case 22:
|
||
6 years ago
|
#line 2620 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_duration_base(parser, p); }
|
||
6 years ago
|
break;
|
||
|
case 23:
|
||
6 years ago
|
#line 2621 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_duration_base(parser, p)); }
|
||
6 years ago
|
break;
|
||
|
case 24:
|
||
6 years ago
|
#line 2623 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {cs = stack[--top]; goto _again;} }
|
||
|
break;
|
||
|
case 25:
|
||
6 years ago
|
#line 2628 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_timestamp_base(parser); }
|
||
|
break;
|
||
|
case 26:
|
||
6 years ago
|
#line 2630 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_timestamp_fraction(parser, p); }
|
||
|
break;
|
||
|
case 27:
|
||
6 years ago
|
#line 2631 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); }
|
||
|
break;
|
||
|
case 28:
|
||
6 years ago
|
#line 2633 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_timestamp_zone(parser, p); }
|
||
|
break;
|
||
|
case 29:
|
||
6 years ago
|
#line 2634 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); }
|
||
|
break;
|
||
|
case 30:
|
||
6 years ago
|
#line 2636 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {cs = stack[--top]; goto _again;} }
|
||
|
break;
|
||
|
case 31:
|
||
6 years ago
|
#line 2641 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_fieldmask_path_text(parser, p); }
|
||
|
break;
|
||
|
case 32:
|
||
6 years ago
|
#line 2642 "upb/json/parser.rl"
|
||
6 years ago
|
{ end_fieldmask_path_text(parser, p); }
|
||
|
break;
|
||
|
case 33:
|
||
6 years ago
|
#line 2647 "upb/json/parser.rl"
|
||
6 years ago
|
{ start_fieldmask_path(parser); }
|
||
|
break;
|
||
|
case 34:
|
||
6 years ago
|
#line 2648 "upb/json/parser.rl"
|
||
6 years ago
|
{ end_fieldmask_path(parser); }
|
||
|
break;
|
||
|
case 35:
|
||
6 years ago
|
#line 2654 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {cs = stack[--top]; goto _again;} }
|
||
|
break;
|
||
|
case 36:
|
||
6 years ago
|
#line 2659 "upb/json/parser.rl"
|
||
6 years ago
|
{
|
||
6 years ago
|
if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) {
|
||
6 years ago
|
{stack[top++] = cs; cs = 47;goto _again;}
|
||
6 years ago
|
} else if (is_wellknown_msg(parser, UPB_WELLKNOWN_DURATION)) {
|
||
6 years ago
|
{stack[top++] = cs; cs = 40;goto _again;}
|
||
6 years ago
|
} else if (is_wellknown_msg(parser, UPB_WELLKNOWN_FIELDMASK)) {
|
||
|
{stack[top++] = cs; cs = 75;goto _again;}
|
||
6 years ago
|
} else {
|
||
6 years ago
|
{stack[top++] = cs; cs = 32;goto _again;}
|
||
6 years ago
|
}
|
||
|
}
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 37:
|
||
6 years ago
|
#line 2672 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {stack[top++] = cs; cs = 78;goto _again;} }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 38:
|
||
6 years ago
|
#line 2677 "upb/json/parser.rl"
|
||
6 years ago
|
{
|
||
|
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
|
||
|
start_any_member(parser, p);
|
||
|
} else {
|
||
|
start_member(parser);
|
||
|
}
|
||
|
}
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 39:
|
||
6 years ago
|
#line 2684 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_membername(parser)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 40:
|
||
6 years ago
|
#line 2687 "upb/json/parser.rl"
|
||
6 years ago
|
{
|
||
|
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
|
||
|
end_any_member(parser, p);
|
||
|
} else {
|
||
|
end_member(parser);
|
||
|
}
|
||
|
}
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 41:
|
||
6 years ago
|
#line 2698 "upb/json/parser.rl"
|
||
6 years ago
|
{
|
||
|
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
|
||
|
start_any_object(parser, p);
|
||
|
} else {
|
||
|
start_object(parser);
|
||
|
}
|
||
|
}
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 42:
|
||
6 years ago
|
#line 2707 "upb/json/parser.rl"
|
||
6 years ago
|
{
|
||
|
if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) {
|
||
|
CHECK_RETURN_TOP(end_any_object(parser, p));
|
||
|
} else {
|
||
|
end_object(parser);
|
||
|
}
|
||
|
}
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 43:
|
||
6 years ago
|
#line 2719 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(start_array(parser)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 44:
|
||
6 years ago
|
#line 2723 "upb/json/parser.rl"
|
||
6 years ago
|
{ end_array(parser); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 45:
|
||
6 years ago
|
#line 2728 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(start_number(parser, p)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 46:
|
||
6 years ago
|
#line 2729 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_number(parser, p)); }
|
||
10 years ago
|
break;
|
||
6 years ago
|
case 47:
|
||
6 years ago
|
#line 2731 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(start_stringval(parser)); }
|
||
|
break;
|
||
6 years ago
|
case 48:
|
||
6 years ago
|
#line 2732 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_stringval(parser)); }
|
||
|
break;
|
||
6 years ago
|
case 49:
|
||
6 years ago
|
#line 2734 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_bool(parser, true)); }
|
||
|
break;
|
||
6 years ago
|
case 50:
|
||
6 years ago
|
#line 2736 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_bool(parser, false)); }
|
||
|
break;
|
||
6 years ago
|
case 51:
|
||
6 years ago
|
#line 2738 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_null(parser)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 52:
|
||
6 years ago
|
#line 2740 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(start_subobject_full(parser)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 53:
|
||
6 years ago
|
#line 2741 "upb/json/parser.rl"
|
||
6 years ago
|
{ end_subobject_full(parser); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 54:
|
||
6 years ago
|
#line 2746 "upb/json/parser.rl"
|
||
10 years ago
|
{ p--; {cs = stack[--top]; goto _again;} }
|
||
|
break;
|
||
6 years ago
|
#line 3154 "upb/json/parser.c"
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
_again:
|
||
|
if ( cs == 0 )
|
||
|
goto _out;
|
||
|
if ( ++p != pe )
|
||
|
goto _resume;
|
||
|
_test_eof: {}
|
||
6 years ago
|
if ( p == eof )
|
||
|
{
|
||
|
const char *__acts = _json_actions + _json_eof_actions[cs];
|
||
|
unsigned int __nacts = (unsigned int) *__acts++;
|
||
|
while ( __nacts-- > 0 ) {
|
||
|
switch ( *__acts++ ) {
|
||
6 years ago
|
case 0:
|
||
6 years ago
|
#line 2555 "upb/json/parser.rl"
|
||
6 years ago
|
{ p--; {cs = stack[--top]; if ( p == pe )
|
||
|
goto _test_eof;
|
||
|
goto _again;} }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 46:
|
||
6 years ago
|
#line 2729 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_number(parser, p)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 49:
|
||
6 years ago
|
#line 2734 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_bool(parser, true)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 50:
|
||
6 years ago
|
#line 2736 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_bool(parser, false)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 51:
|
||
6 years ago
|
#line 2738 "upb/json/parser.rl"
|
||
6 years ago
|
{ CHECK_RETURN_TOP(end_null(parser)); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
case 53:
|
||
6 years ago
|
#line 2741 "upb/json/parser.rl"
|
||
6 years ago
|
{ end_subobject_full(parser); }
|
||
6 years ago
|
break;
|
||
6 years ago
|
#line 3196 "upb/json/parser.c"
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
_out: {}
|
||
|
}
|
||
|
|
||
6 years ago
|
#line 2774 "upb/json/parser.rl"
|
||
10 years ago
|
|
||
|
if (p != pe) {
|
||
6 years ago
|
upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p);
|
||
10 years ago
|
} else {
|
||
|
capture_suspend(parser, &p);
|
||
10 years ago
|
}
|
||
|
|
||
|
error:
|
||
10 years ago
|
/* Save parsing state back to parser. */
|
||
10 years ago
|
parser->current_state = cs;
|
||
|
parser->parser_top = top;
|
||
|
|
||
|
return p - buf;
|
||
|
}
|
||
|
|
||
6 years ago
|
static bool end(void *closure, const void *hd) {
|
||
6 years ago
|
upb_json_parser *parser = closure;
|
||
10 years ago
|
|
||
10 years ago
|
/* Prevent compile warning on unused static constants. */
|
||
10 years ago
|
UPB_UNUSED(json_start);
|
||
6 years ago
|
UPB_UNUSED(json_en_duration_machine);
|
||
6 years ago
|
UPB_UNUSED(json_en_fieldmask_machine);
|
||
6 years ago
|
UPB_UNUSED(json_en_number_machine);
|
||
|
UPB_UNUSED(json_en_string_machine);
|
||
6 years ago
|
UPB_UNUSED(json_en_timestamp_machine);
|
||
10 years ago
|
UPB_UNUSED(json_en_value_machine);
|
||
|
UPB_UNUSED(json_en_main);
|
||
6 years ago
|
|
||
6 years ago
|
parse(parser, hd, &eof_ch, 0, NULL);
|
||
6 years ago
|
|
||
6 years ago
|
return parser->current_state >= 106;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
static void json_parser_reset(upb_json_parser *p) {
|
||
10 years ago
|
int cs;
|
||
|
int top;
|
||
|
|
||
10 years ago
|
p->top = p->stack;
|
||
6 years ago
|
init_frame(p->top);
|
||
10 years ago
|
|
||
10 years ago
|
/* Emit Ragel initialization of the parser. */
|
||
10 years ago
|
|
||
6 years ago
|
#line 3247 "upb/json/parser.c"
|
||
10 years ago
|
{
|
||
|
cs = json_start;
|
||
|
top = 0;
|
||
|
}
|
||
|
|
||
6 years ago
|
#line 2816 "upb/json/parser.rl"
|
||
10 years ago
|
p->current_state = cs;
|
||
|
p->parser_top = top;
|
||
10 years ago
|
accumulate_clear(p);
|
||
|
p->multipart_state = MULTIPART_INACTIVE;
|
||
|
p->capture = NULL;
|
||
10 years ago
|
p->accumulated = NULL;
|
||
10 years ago
|
}
|
||
|
|
||
6 years ago
|
static upb_json_parsermethod *parsermethod_new(upb_json_codecache *c,
|
||
|
const upb_msgdef *md) {
|
||
|
upb_msg_field_iter i;
|
||
6 years ago
|
upb_alloc *alloc = upb_arena_alloc(c->arena);
|
||
9 years ago
|
|
||
6 years ago
|
upb_json_parsermethod *m = upb_malloc(alloc, sizeof(*m));
|
||
9 years ago
|
|
||
6 years ago
|
m->cache = c;
|
||
9 years ago
|
|
||
6 years ago
|
upb_byteshandler_init(&m->input_handler_);
|
||
|
upb_byteshandler_setstring(&m->input_handler_, parse, m);
|
||
|
upb_byteshandler_setendstr(&m->input_handler_, end, m);
|
||
9 years ago
|
|
||
6 years ago
|
upb_strtable_init2(&m->name_table, UPB_CTYPE_CONSTPTR, alloc);
|
||
9 years ago
|
|
||
6 years ago
|
/* Build name_table */
|
||
9 years ago
|
|
||
|
for(upb_msg_field_begin(&i, md);
|
||
|
!upb_msg_field_done(&i);
|
||
|
upb_msg_field_next(&i)) {
|
||
|
const upb_fielddef *f = upb_msg_iter_field(&i);
|
||
6 years ago
|
upb_value v = upb_value_constptr(f);
|
||
|
char *buf;
|
||
9 years ago
|
|
||
9 years ago
|
/* Add an entry for the JSON name. */
|
||
6 years ago
|
size_t len = upb_fielddef_getjsonname(f, NULL, 0);
|
||
|
buf = upb_malloc(alloc, len);
|
||
|
upb_fielddef_getjsonname(f, buf, len);
|
||
6 years ago
|
upb_strtable_insert3(&m->name_table, buf, strlen(buf), v, alloc);
|
||
9 years ago
|
|
||
9 years ago
|
if (strcmp(buf, upb_fielddef_name(f)) != 0) {
|
||
|
/* Since the JSON name is different from the regular field name, add an
|
||
|
* entry for the raw name (compliant proto3 JSON parsers must accept
|
||
|
* both). */
|
||
6 years ago
|
const char *name = upb_fielddef_name(f);
|
||
|
upb_strtable_insert3(&m->name_table, name, strlen(name), v, alloc);
|
||
9 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
6 years ago
|
return m;
|
||
9 years ago
|
}
|
||
10 years ago
|
|
||
|
/* Public API *****************************************************************/
|
||
|
|
||
6 years ago
|
upb_json_parser *upb_json_parser_create(upb_arena *arena,
|
||
9 years ago
|
const upb_json_parsermethod *method,
|
||
6 years ago
|
const upb_symtab* symtab,
|
||
6 years ago
|
upb_sink output,
|
||
6 years ago
|
upb_status *status,
|
||
7 years ago
|
bool ignore_json_unknown) {
|
||
10 years ago
|
#ifndef NDEBUG
|
||
6 years ago
|
const size_t size_before = upb_arena_bytesallocated(arena);
|
||
10 years ago
|
#endif
|
||
6 years ago
|
upb_json_parser *p = upb_arena_malloc(arena, sizeof(upb_json_parser));
|
||
10 years ago
|
if (!p) return false;
|
||
|
|
||
6 years ago
|
p->arena = arena;
|
||
9 years ago
|
p->method = method;
|
||
6 years ago
|
p->status = status;
|
||
10 years ago
|
p->limit = p->stack + UPB_JSON_MAX_DEPTH;
|
||
|
p->accumulate_buf = NULL;
|
||
|
p->accumulate_buf_size = 0;
|
||
9 years ago
|
upb_bytessink_reset(&p->input_, &method->input_handler_, p);
|
||
10 years ago
|
|
||
|
json_parser_reset(p);
|
||
6 years ago
|
p->top->sink = output;
|
||
|
p->top->m = upb_handlers_msgdef(output.handlers);
|
||
6 years ago
|
if (is_wellknown_msg(p, UPB_WELLKNOWN_ANY)) {
|
||
|
p->top->is_any = true;
|
||
6 years ago
|
p->top->any_frame = json_parser_any_frame_new(p);
|
||
6 years ago
|
} else {
|
||
|
p->top->is_any = false;
|
||
|
p->top->any_frame = NULL;
|
||
|
}
|
||
9 years ago
|
set_name_table(p, p->top);
|
||
6 years ago
|
p->symtab = symtab;
|
||
10 years ago
|
|
||
7 years ago
|
p->ignore_json_unknown = ignore_json_unknown;
|
||
|
|
||
10 years ago
|
/* If this fails, uncomment and increase the value in parser.h. */
|
||
6 years ago
|
/* fprintf(stderr, "%zd\n", upb_arena_bytesallocated(arena) - size_before); */
|
||
|
UPB_ASSERT_DEBUGVAR(upb_arena_bytesallocated(arena) - size_before <=
|
||
9 years ago
|
UPB_JSON_PARSER_SIZE);
|
||
10 years ago
|
return p;
|
||
10 years ago
|
}
|
||
|
|
||
6 years ago
|
upb_bytessink upb_json_parser_input(upb_json_parser *p) {
|
||
|
return p->input_;
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
6 years ago
|
const upb_byteshandler *upb_json_parsermethod_inputhandler(
|
||
|
const upb_json_parsermethod *m) {
|
||
|
return &m->input_handler_;
|
||
|
}
|
||
|
|
||
|
upb_json_codecache *upb_json_codecache_new() {
|
||
|
upb_alloc *alloc;
|
||
|
upb_json_codecache *c;
|
||
9 years ago
|
|
||
6 years ago
|
c = upb_gmalloc(sizeof(*c));
|
||
9 years ago
|
|
||
6 years ago
|
c->arena = upb_arena_new();
|
||
|
alloc = upb_arena_alloc(c->arena);
|
||
9 years ago
|
|
||
6 years ago
|
upb_inttable_init2(&c->methods, UPB_CTYPE_CONSTPTR, alloc);
|
||
9 years ago
|
|
||
6 years ago
|
return c;
|
||
9 years ago
|
}
|
||
|
|
||
6 years ago
|
void upb_json_codecache_free(upb_json_codecache *c) {
|
||
6 years ago
|
upb_arena_free(c->arena);
|
||
6 years ago
|
upb_gfree(c);
|
||
|
}
|
||
|
|
||
6 years ago
|
const upb_json_parsermethod *upb_json_codecache_get(upb_json_codecache *c,
|
||
|
const upb_msgdef *md) {
|
||
6 years ago
|
upb_json_parsermethod *m;
|
||
|
upb_value v;
|
||
|
upb_msg_field_iter i;
|
||
6 years ago
|
upb_alloc *alloc = upb_arena_alloc(c->arena);
|
||
6 years ago
|
|
||
|
if (upb_inttable_lookupptr(&c->methods, md, &v)) {
|
||
6 years ago
|
return upb_value_getconstptr(v);
|
||
6 years ago
|
}
|
||
|
|
||
|
m = parsermethod_new(c, md);
|
||
6 years ago
|
v = upb_value_constptr(m);
|
||
6 years ago
|
|
||
|
if (!m) return NULL;
|
||
6 years ago
|
if (!upb_inttable_insertptr2(&c->methods, md, v, alloc)) return NULL;
|
||
6 years ago
|
|
||
|
/* Populate parser methods for all submessages, so the name tables will
|
||
|
* be available during parsing. */
|
||
|
for(upb_msg_field_begin(&i, md);
|
||
|
!upb_msg_field_done(&i);
|
||
|
upb_msg_field_next(&i)) {
|
||
|
upb_fielddef *f = upb_msg_iter_field(&i);
|
||
|
|
||
|
if (upb_fielddef_issubmsg(f)) {
|
||
|
const upb_msgdef *subdef = upb_fielddef_msgsubdef(f);
|
||
|
const upb_json_parsermethod *sub_method =
|
||
|
upb_json_codecache_get(c, subdef);
|
||
|
|
||
|
if (!sub_method) return NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return m;
|
||
9 years ago
|
}
|