Bugfixes, test_decoder successfully stream-decodes a stream!

pull/13171/head
Joshua Haberman 15 years ago
parent b77db14646
commit 0fcfeab521
  1. 5
      core/upb_stream.c
  2. 39
      stream/upb_decoder.c
  3. 9
      stream/upb_textprinter.c

@ -37,14 +37,19 @@ void upb_streamdata(upb_src *src, upb_sink *sink, upb_status *status) {
CHECKSRC(upb_src_eof(src));
if (depth == 0) break;
--depth;
upb_src_endmsg(src);
upb_sink_endmsg(sink);
}
upb_string_unref(str);
return;
src_err:
upb_string_unref(str);
upb_copyerr(status, upb_src_status(src));
return;
sink_err:
upb_string_unref(str);
upb_copyerr(status, upb_sink_status(sink));
return;
}

@ -76,7 +76,7 @@ struct upb_decoder {
static upb_strlen_t upb_decoder_offset(upb_decoder *d)
{
return d->buf_stream_offset - d->buf_offset;
return d->buf_stream_offset + d->buf_offset;
}
static bool upb_decoder_nextbuf(upb_decoder *d)
@ -101,34 +101,30 @@ static bool upb_decoder_nextbuf(upb_decoder *d)
d->buf_bytesleft += upb_string_len(d->buf);
return true;
} else {
// Error or EOF.
if(!upb_bytesrc_eof(d->bytesrc)) {
// Error from bytesrc.
upb_copyerr(&d->src.status, upb_bytesrc_status(d->bytesrc));
return false;
} else if(d->buf_bytesleft == 0) {
// EOF from bytesrc and we don't have any residual bytes left.
d->src.eof = true;
return false;
} else {
// No more data left from the bytesrc, but we still have residual bytes.
return true;
}
return false;
}
}
static const uint8_t *upb_decoder_getbuf_full(upb_decoder *d, uint32_t *bytes)
{
if(d->buf_bytesleft < UPB_MAX_ENCODED_SIZE && !upb_bytesrc_eof(d->bytesrc))
upb_decoder_nextbuf(d);
if(d->buf_bytesleft < UPB_MAX_ENCODED_SIZE) {
// GCC is currently complaining about use of an uninitialized value if we
// don't set this now. I think this is incorrect, but leaving this in
// to suppress the warning for now.
*bytes = 0;
if(!upb_decoder_nextbuf(d)) return NULL;
if(upb_bytesrc_eof(d->bytesrc) && d->buf_bytesleft > 0) {
// We're working through the last few bytes of the buffer.
} else if(upb_bytesrc_eof(d->bytesrc)) {
// End of stream, no more bytes left.
assert(d->buf_bytesleft == 0);
d->src.eof = true;
return NULL;
} else {
// We are short of bytes even though the bytesrc isn't EOF; must be error.
upb_copyerr(&d->src.status, upb_bytesrc_status(d->bytesrc));
return NULL;
}
}
assert(d->buf_bytesleft >= UPB_MAX_ENCODED_SIZE);
if(d->buf_offset >= 0) {
// Common case: the main buffer contains at least UPB_MAX_ENCODED_SIZE
// contiguous bytes, so we can read directly out of it.
@ -467,6 +463,7 @@ bool upb_decoder_startmsg(upb_decoder *d) {
} else {
frame->end_offset = upb_decoder_offset(d) + d->delimited_len;
}
d->field = NULL;
return true;
}

@ -23,9 +23,9 @@ struct _upb_textprinter {
static void upb_textprinter_endfield(upb_textprinter *p)
{
if(p->single_line)
upb_bytesink_put(p->bytesink, UPB_STRLIT(' '));
upb_bytesink_put(p->bytesink, UPB_STRLIT(" "));
else
upb_bytesink_put(p->bytesink, UPB_STRLIT('\n'));
upb_bytesink_put(p->bytesink, UPB_STRLIT("\n"));
}
static bool upb_textprinter_putval(upb_textprinter *p, upb_value val) {
@ -86,10 +86,9 @@ static bool upb_textprinter_putdef(upb_textprinter *p, upb_fielddef *f)
static bool upb_textprinter_startmsg(upb_textprinter *p)
{
upb_textprinter_indent(p);
upb_bytesink_put(p->bytesink, p->f->def->fqname);
upb_bytesink_put(p->bytesink, UPB_STRLIT(" {"));
if(!p->single_line) upb_bytesink_put(p->bytesink, UPB_STRLIT('\n'));
if(!p->single_line) upb_bytesink_put(p->bytesink, UPB_STRLIT("\n"));
p->indent_depth++;
return upb_ok(upb_bytesink_status(p->bytesink));
}
@ -114,10 +113,12 @@ upb_sink_vtable upb_textprinter_vtbl = {
upb_textprinter *upb_textprinter_new() {
upb_textprinter *p = malloc(sizeof(*p));
upb_sink_init(&p->sink, &upb_textprinter_vtbl);
p->str = NULL;
return p;
}
void upb_textprinter_free(upb_textprinter *p) {
upb_string_unref(p->str);
free(p);
}

Loading…
Cancel
Save