diff options
Diffstat (limited to 'upb/pb')
-rw-r--r-- | upb/pb/decoder_x86.dasc | 4 | ||||
-rw-r--r-- | upb/pb/glue.c | 27 | ||||
-rw-r--r-- | upb/pb/glue.h | 23 | ||||
-rw-r--r-- | upb/pb/textprinter.c | 14 | ||||
-rw-r--r-- | upb/pb/textprinter.h | 2 |
5 files changed, 41 insertions, 29 deletions
diff --git a/upb/pb/decoder_x86.dasc b/upb/pb/decoder_x86.dasc index fe15174..0657af6 100644 --- a/upb/pb/decoder_x86.dasc +++ b/upb/pb/decoder_x86.dasc @@ -413,7 +413,7 @@ static void upb_decoder_jit_doappend(upb_decoder *d, uint8_t size, #endif static void upb_decoder_jit_callcb(upb_decoder *d, upb_fhandlers *f) { - upb_fielddef *fd = upb_value_getfielddef(f->fval); + const upb_fielddef *fd = upb_value_getfielddef(f->fval); // Call callbacks. if (upb_issubmsgtype(f->type)) { // Load closure and fval into arg registers. @@ -439,7 +439,7 @@ static void upb_decoder_jit_callcb(upb_decoder *d, upb_fhandlers *f) { } | pushframe f, rdx, esi, false - upb_mhandlers *sub_m = upb_fhandlers_getsubmsg(f); + const upb_mhandlers *sub_m = upb_fhandlers_getsubmsg(f); if (sub_m->jit_parent_field_done_pclabel != UPB_MULTIPLE) { | jmp =>sub_m->jit_startmsg_pclabel; } else { diff --git a/upb/pb/glue.c b/upb/pb/glue.c index 6981aa2..b364a6d 100644 --- a/upb/pb/glue.c +++ b/upb/pb/glue.c @@ -12,7 +12,7 @@ #include "upb/pb/glue.h" #include "upb/pb/textprinter.h" -void upb_strtomsg(const char *str, size_t len, void *msg, upb_msgdef *md, +void upb_strtomsg(const char *str, size_t len, void *msg, const upb_msgdef *md, upb_status *status) { upb_stringsrc strsrc; upb_stringsrc_init(&strsrc); @@ -56,8 +56,8 @@ void upb_msgtotext(upb_string *str, upb_msg *msg, upb_msgdef *md, #endif // TODO: read->load. -upb_def **upb_load_descriptor(const char *str, size_t len, int *n, - upb_status *status) { +upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n, + upb_status *status) { upb_stringsrc strsrc; upb_stringsrc_init(&strsrc); upb_stringsrc_reset(&strsrc, str, len); @@ -98,13 +98,15 @@ upb_def **upb_load_descriptor(const char *str, size_t len, int *n, return defscopy; } -void upb_read_descriptor(upb_symtab *s, const char *str, size_t len, - upb_status *status) { +bool upb_load_descriptor_into_symtab(upb_symtab *s, const char *str, size_t len, + upb_status *status) { int n; - upb_def **defs = upb_load_descriptor(str, len, &n, status); - if (upb_ok(status)) upb_symtab_add(s, defs, n, status); + upb_def **defs = upb_load_defs_from_descriptor(str, len, &n, status); + if (!defs) return false; + bool success = upb_symtab_add(s, defs, n, status); for(int i = 0; i < n; i++) upb_def_unref(defs[i]); free(defs); + return success; } char *upb_readfile(const char *filename, size_t *len) { @@ -125,14 +127,15 @@ error: return NULL; } -void upb_read_descriptorfile(upb_symtab *symtab, const char *fname, - upb_status *status) { +bool upb_load_descriptor_file_into_symtab(upb_symtab *symtab, const char *fname, + upb_status *status) { size_t len; char *data = upb_readfile(fname, &len); if (!data) { - upb_status_seterrf(status, "Couldn't read file: %s", fname); - return; + if (status) upb_status_seterrf(status, "Couldn't read file: %s", fname); + return false; } - upb_read_descriptor(symtab, data, len, status); + bool success = upb_load_descriptor_into_symtab(symtab, data, len, status); free(data); + return success; } diff --git a/upb/pb/glue.h b/upb/pb/glue.h index a2a478c..7aa4a5f 100644 --- a/upb/pb/glue.h +++ b/upb/pb/glue.h @@ -42,20 +42,29 @@ struct _upb_symtab; // Decodes the given string, which must be in protobuf binary format, to the // given upb_msg with msgdef "md", storing the status of the operation in "s". void upb_strtomsg(const char *str, size_t len, void *msg, - struct _upb_msgdef *md, upb_status *s); + const struct _upb_msgdef *md, upb_status *s); //void upb_msgtotext(struct _upb_string *str, void *msg, // struct _upb_msgdef *md, bool single_line); -upb_def **upb_load_descriptor(const char *str, size_t len, int *n, - upb_status *status); -void upb_read_descriptor(struct _upb_symtab *symtab, const char *str, size_t len, - upb_status *status); +// Loads all defs from the given protobuf binary descriptor, setting default +// accessors and a default layout on all messages. The caller owns the +// returned array of defs, which will be of length *n. On error NULL is +// returned and status is set (if non-NULL). +upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n, + upb_status *status); -void upb_read_descriptorfile(struct _upb_symtab *symtab, const char *fname, - upb_status *status); +// Like the previous but also adds the loaded defs to the given symtab. +bool upb_load_descriptor_into_symtab(struct _upb_symtab *symtab, const char *str, + size_t len, upb_status *status); +// Like the previous but also reads the descriptor from the given filename. +bool upb_load_descriptor_file_into_symtab(struct _upb_symtab *symtab, + const char *fname, upb_status *status); + +// Reads the given filename into a character string, returning NULL if there +// was an error. char *upb_readfile(const char *filename, size_t *len); #ifdef __cplusplus diff --git a/upb/pb/textprinter.c b/upb/pb/textprinter.c index 37f5699..434a482 100644 --- a/upb/pb/textprinter.c +++ b/upb/pb/textprinter.c @@ -35,7 +35,7 @@ err: return -1; } -static int upb_textprinter_putescaped(upb_textprinter *p, upb_strref *strref, +static int upb_textprinter_putescaped(upb_textprinter *p, const upb_strref *strref, bool preserve_utf8) { // Based on CEscapeInternal() from Google's protobuf release. // TODO; we could read directly from a bytesrc's buffer instead. @@ -90,7 +90,7 @@ err: static upb_flow_t upb_textprinter_put ## member(void *_p, upb_value fval, \ upb_value val) { \ upb_textprinter *p = _p; \ - upb_fielddef *f = upb_value_getfielddef(fval); \ + const upb_fielddef *f = upb_value_getfielddef(fval); \ uint64_t start_ofs = upb_bytesink_getoffset(p->sink); \ CHECK(upb_textprinter_indent(p)); \ CHECK(upb_bytesink_writestr(p->sink, f->name)); \ @@ -120,7 +120,7 @@ static upb_flow_t upb_textprinter_putenum(void *_p, upb_value fval, upb_textprinter *p = _p; uint64_t start_ofs = upb_bytesink_getoffset(p->sink); - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); upb_enumdef *enum_def = upb_downcast_enumdef(f->def); const char *label = upb_enumdef_iton(enum_def, upb_value_getint32(val)); if (label) { @@ -138,7 +138,7 @@ static upb_flow_t upb_textprinter_putstr(void *_p, upb_value fval, upb_value val) { upb_textprinter *p = _p; uint64_t start_ofs = upb_bytesink_getoffset(p->sink); - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); CHECK(upb_bytesink_putc(p->sink, '"')); CHECK(upb_textprinter_putescaped(p, upb_value_getstrref(val), f->type == UPB_TYPE(STRING))); @@ -152,7 +152,7 @@ err: static upb_sflow_t upb_textprinter_startsubmsg(void *_p, upb_value fval) { upb_textprinter *p = _p; uint64_t start_ofs = upb_bytesink_getoffset(p->sink); - upb_fielddef *f = upb_value_getfielddef(fval); + const upb_fielddef *f = upb_value_getfielddef(fval); CHECK(upb_textprinter_indent(p)); CHECK(upb_bytesink_printf(p->sink, "%s {", f->name)); if (!p->single_line) @@ -192,7 +192,7 @@ void upb_textprinter_reset(upb_textprinter *p, upb_bytesink *sink, p->indent_depth = 0; } -static void upb_textprinter_onfreg(void *c, upb_fhandlers *fh, upb_fielddef *f) { +static void upb_textprinter_onfreg(void *c, upb_fhandlers *fh, const upb_fielddef *f) { (void)c; upb_fhandlers_setstartsubmsg(fh, &upb_textprinter_startsubmsg); upb_fhandlers_setendsubmsg(fh, &upb_textprinter_endsubmsg); @@ -207,7 +207,7 @@ static void upb_textprinter_onfreg(void *c, upb_fhandlers *fh, upb_fielddef *f) upb_fhandlers_setfval(fh, fval); } -upb_mhandlers *upb_textprinter_reghandlers(upb_handlers *h, upb_msgdef *m) { +upb_mhandlers *upb_textprinter_reghandlers(upb_handlers *h, const upb_msgdef *m) { return upb_handlers_regmsgdef( h, m, NULL, &upb_textprinter_onfreg, NULL); } diff --git a/upb/pb/textprinter.h b/upb/pb/textprinter.h index 9455208..25f364e 100644 --- a/upb/pb/textprinter.h +++ b/upb/pb/textprinter.h @@ -22,7 +22,7 @@ upb_textprinter *upb_textprinter_new(); void upb_textprinter_free(upb_textprinter *p); void upb_textprinter_reset(upb_textprinter *p, upb_bytesink *sink, bool single_line); -upb_mhandlers *upb_textprinter_reghandlers(upb_handlers *h, upb_msgdef *m); +upb_mhandlers *upb_textprinter_reghandlers(upb_handlers *h, const upb_msgdef *m); #ifdef __cplusplus } /* extern "C" */ |