|
|
@ -10,33 +10,31 @@ |
|
|
|
static char *input_str; |
|
|
|
static char *input_str; |
|
|
|
static size_t input_len; |
|
|
|
static size_t input_len; |
|
|
|
static const upb_msgdef *def; |
|
|
|
static const upb_msgdef *def; |
|
|
|
static upb_decoder decoder; |
|
|
|
upb_pipeline pipeline; |
|
|
|
static upb_stringsrc stringsrc; |
|
|
|
static upb_sink *sink; |
|
|
|
static upb_decoderplan *plan; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static upb_sflow_t startsubmsg(void *_m, upb_value fval) { |
|
|
|
static void *startsubmsg(const upb_sinkframe *frame) { |
|
|
|
(void)_m; |
|
|
|
UPB_UNUSED(frame); |
|
|
|
(void)fval; |
|
|
|
return input_str; |
|
|
|
return UPB_CONTINUE_WITH(NULL); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static upb_flow_t value(void *closure, upb_value fval, upb_value val) { |
|
|
|
void onmreg(void *c, upb_handlers *h) { |
|
|
|
(void)closure; |
|
|
|
upb_msg_iter i; |
|
|
|
(void)fval; |
|
|
|
upb_msg_begin(&i, upb_handlers_msgdef(h)); |
|
|
|
(void)val; |
|
|
|
for(; !upb_msg_done(&i); upb_msg_next(&i)) { |
|
|
|
return UPB_CONTINUE; |
|
|
|
const upb_fielddef *f = upb_msg_iter_field(&i); |
|
|
|
} |
|
|
|
if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE) { |
|
|
|
|
|
|
|
upb_handlers_setstartsubmsg(h, f, startsubmsg, NULL, NULL); |
|
|
|
void onfreg(void *c, upb_fhandlers *fh, const upb_fielddef *f) { |
|
|
|
} |
|
|
|
upb_fhandlers_setvalue(fh, &value); |
|
|
|
} |
|
|
|
upb_fhandlers_setstartsubmsg(fh, &startsubmsg); |
|
|
|
UPB_UNUSED(c); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static bool initialize() |
|
|
|
static bool initialize() |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Initialize upb state, decode descriptor.
|
|
|
|
// Initialize upb state, decode descriptor.
|
|
|
|
upb_status status = UPB_STATUS_INIT; |
|
|
|
upb_status status = UPB_STATUS_INIT; |
|
|
|
upb_symtab *s = upb_symtab_new(); |
|
|
|
upb_symtab *s = upb_symtab_new(&s); |
|
|
|
upb_load_descriptor_file_into_symtab(s, MESSAGE_DESCRIPTOR_FILE, &status); |
|
|
|
upb_load_descriptor_file_into_symtab(s, MESSAGE_DESCRIPTOR_FILE, &status); |
|
|
|
if(!upb_ok(&status)) { |
|
|
|
if(!upb_ok(&status)) { |
|
|
|
fprintf(stderr, "Error reading descriptor: %s\n", |
|
|
|
fprintf(stderr, "Error reading descriptor: %s\n", |
|
|
@ -44,12 +42,12 @@ static bool initialize() |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def = upb_dyncast_msgdef_const(upb_symtab_lookup(s, MESSAGE_NAME, &def)); |
|
|
|
def = upb_dyncast_msgdef(upb_symtab_lookup(s, MESSAGE_NAME, &def)); |
|
|
|
if(!def) { |
|
|
|
if(!def) { |
|
|
|
fprintf(stderr, "Error finding symbol '%s'.\n", MESSAGE_NAME); |
|
|
|
fprintf(stderr, "Error finding symbol '%s'.\n", MESSAGE_NAME); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
upb_symtab_unref(s); |
|
|
|
upb_symtab_unref(s, &s); |
|
|
|
|
|
|
|
|
|
|
|
// Read the message data itself.
|
|
|
|
// Read the message data itself.
|
|
|
|
input_str = upb_readfile(MESSAGE_FILE, &input_len); |
|
|
|
input_str = upb_readfile(MESSAGE_FILE, &input_len); |
|
|
@ -58,36 +56,37 @@ static bool initialize() |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
upb_handlers *handlers = upb_handlers_new(); |
|
|
|
|
|
|
|
// Cause all messages to be read, but do nothing when they are.
|
|
|
|
// Cause all messages to be read, but do nothing when they are.
|
|
|
|
upb_handlers_regmsgdef(handlers, def, NULL, &upb_onfreg_hset, NULL); |
|
|
|
const upb_handlers* handlers = |
|
|
|
upb_decoder_init(&decoder); |
|
|
|
upb_handlers_newfrozen(def, NULL, &handlers, &onmreg, NULL); |
|
|
|
plan = upb_decoderplan_new(handlers, JIT); |
|
|
|
const upb_handlers* decoder_handlers = |
|
|
|
upb_decoder_resetplan(&decoder, plan, 0); |
|
|
|
upb_pbdecoder_gethandlers(handlers, JIT, &decoder_handlers); |
|
|
|
upb_handlers_unref(handlers); |
|
|
|
upb_msgdef_unref(def, &def); |
|
|
|
upb_stringsrc_init(&stringsrc); |
|
|
|
|
|
|
|
|
|
|
|
upb_pipeline_init(&pipeline, NULL, 0, upb_realloc, NULL); |
|
|
|
|
|
|
|
upb_sink *s2 = upb_pipeline_newsink(&pipeline, handlers); |
|
|
|
|
|
|
|
sink = upb_pipeline_newsink(&pipeline, decoder_handlers); |
|
|
|
|
|
|
|
upb_pipeline_donateref(&pipeline, decoder_handlers, &decoder_handlers); |
|
|
|
|
|
|
|
upb_pipeline_donateref(&pipeline, handlers, &handlers); |
|
|
|
|
|
|
|
upb_pbdecoder *decoder = upb_sinkframe_userdata(upb_sink_top(sink)); |
|
|
|
|
|
|
|
upb_pbdecoder_resetsink(decoder, s2); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static void cleanup() |
|
|
|
static void cleanup() |
|
|
|
{ |
|
|
|
{ |
|
|
|
free(input_str); |
|
|
|
free(input_str); |
|
|
|
upb_def_unref(UPB_UPCAST(def), &def); |
|
|
|
upb_pipeline_uninit(&pipeline); |
|
|
|
upb_decoder_uninit(&decoder); |
|
|
|
|
|
|
|
upb_decoderplan_unref(plan); |
|
|
|
|
|
|
|
upb_stringsrc_uninit(&stringsrc); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static size_t run(int i) |
|
|
|
static size_t run(int i) |
|
|
|
{ |
|
|
|
{ |
|
|
|
(void)i; |
|
|
|
(void)i; |
|
|
|
upb_status status = UPB_STATUS_INIT; |
|
|
|
upb_pipeline_reset(&pipeline); |
|
|
|
upb_stringsrc_reset(&stringsrc, input_str, input_len); |
|
|
|
if (!upb_bytestream_putstr(sink, input_str, input_len)) { |
|
|
|
upb_decoder_resetinput(&decoder, upb_stringsrc_allbytes(&stringsrc), NULL); |
|
|
|
fprintf(stderr, "Decode error: %s", upb_status_getstr(upb_pipeline_status(&pipeline))); |
|
|
|
if (upb_decoder_decode(&decoder) != UPB_OK) goto err; |
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
return input_len; |
|
|
|
return input_len; |
|
|
|
|
|
|
|
|
|
|
|
err: |
|
|
|
|
|
|
|
fprintf(stderr, "Decode error: %s", upb_status_getstr(&status)); |
|
|
|
|
|
|
|
return 0; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|