Increases code size 9 bytes per each field. Compiler error will tell if you need this.
PB_NO_ERRMSG Disables the support for error messages; only error information is the true/false return value.
Decreases the code size by a few hundred bytes.
+PB_BUFFER_ONLY Disables the support for custom streams. Only supports encoding to memory buffers.
+ Speeds up execution and decreases code size slightly.
============================ ================================================================================================
The PB_MAX_REQUIRED_FIELDS, PB_FIELD_16BIT and PB_FIELD_32BIT settings allow raising some datatype limits to suit larger messages.
bool checkreturn pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
{
+#ifndef PB_BUFFER_ONLY
if (buf == NULL && stream->callback != buf_read)
{
/* Skip input bytes */
return pb_read(stream, tmp, count);
}
+#endif
if (stream->bytes_left < count)
PB_RETURN_ERROR(stream, "end-of-stream");
+#ifndef PB_BUFFER_ONLY
if (!stream->callback(stream, buf, count))
PB_RETURN_ERROR(stream, "io error");
+#else
+ if (!buf_read(stream, buf, count))
+ return false;
+#endif
stream->bytes_left -= count;
return true;
pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
{
pb_istream_t stream;
+#ifdef PB_BUFFER_ONLY
+ stream.callback = NULL;
+#else
stream.callback = &buf_read;
+#endif
stream.state = buf;
stream.bytes_left = bufsize;
#ifndef PB_NO_ERRMSG
*/
struct _pb_istream_t
{
+#ifdef PB_BUFFER_ONLY
+ /* Callback pointer is not used in buffer-only configuration.
+ * Having an int pointer here allows binary compatibility but
+ * gives an error if someone tries to assign callback function.
+ */
+ int *callback;
+#else
bool (*callback)(pb_istream_t *stream, uint8_t *buf, size_t count);
+#endif
+
void *state; /* Free field for use by callback implementation */
size_t bytes_left;
pb_ostream_t pb_ostream_from_buffer(uint8_t *buf, size_t bufsize)
{
pb_ostream_t stream;
+#ifdef PB_BUFFER_ONLY
+ stream.callback = (void*)1; /* Just some marker value */
+#else
stream.callback = &buf_write;
+#endif
stream.state = buf;
stream.max_size = bufsize;
stream.bytes_written = 0;
{
if (stream->bytes_written + count > stream->max_size)
return false;
-
+
+#ifdef PB_BUFFER_ONLY
+ if (!buf_write(stream, buf, count))
+ return false;
+#else
if (!stream->callback(stream, buf, count))
return false;
+#endif
}
stream->bytes_written += count;
*/
struct _pb_ostream_t
{
+#ifdef PB_BUFFER_ONLY
+ /* Callback pointer is not used in buffer-only configuration.
+ * Having an int pointer here allows binary compatibility but
+ * gives an error if someone tries to assign callback function.
+ * Also, NULL pointer marks a 'sizing stream' that does not
+ * write anything.
+ */
+ int *callback;
+#else
bool (*callback)(pb_ostream_t *stream, const uint8_t *buf, size_t count);
+#endif
void *state; /* Free field for use by callback implementation */
size_t max_size; /* Limit number of output bytes written (or use SIZE_MAX). */
size_t bytes_written;
callbacks2.pb.h callbacks.pb.h unittests.h unittestproto.pb.h \
alltypes.pb.h missing_fields.pb.h
TESTS= decode_unittests encode_unittests \
- test_decode1 test_decode2 test_decode3 \
- test_encode1 test_encode2 test_encode3 \
+ test_decode1 test_decode2 test_decode3 test_decode3_buf \
+ test_encode1 test_encode2 test_encode3 test_encode3_buf \
test_decode_callbacks test_encode_callbacks \
test_missing_fields test_no_messages \
test_multiple_files test_cxxcompile test_options \
pb_decode.o: ../pb_decode.c $(DEPS)
$(CC) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
+# Test for compilability with c++ compiler
+
pb_encode.cxx.o: ../pb_encode.c $(DEPS)
$(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
pb_decode.cxx.o: ../pb_decode.c $(DEPS)
$(CXX) $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
+# Test for PB_BUF_ONLY compilation option
+
+pb_encode.buf.o: ../pb_encode.c $(DEPS)
+ $(CC) -DPB_BUFFER_ONLY $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
+pb_decode.buf.o: ../pb_decode.c $(DEPS)
+ $(CC) -DPB_BUFFER_ONLY $(CFLAGS) $(CFLAGS_CORE) -c -o $@ $<
+%.buf.o: %.c $(DEPS)
+ $(CC) -DPB_BUFFER_ONLY $(CFLAGS) -c -o $@ $<
+test_encode3_buf: test_encode3.buf.o pb_encode.buf.o alltypes.pb.o
+ $(CC) $(LDFLAGS) $^ -o $@
+test_decode3_buf: test_decode3.buf.o pb_decode.buf.o alltypes.pb.o
+ $(CC) $(LDFLAGS) $^ -o $@
+
test_cxxcompile: pb_encode.cxx.o pb_decode.cxx.o
test_decode1: test_decode1.o pb_decode.o person.pb.o
test_decode2: test_decode2.o pb_decode.o person.pb.o
./test_encode3 | ./test_decode3
./test_encode3 1 | ./test_decode3 1
./test_encode3 1 | protoc --decode=AllTypes -I. -I../generator -I/usr/include alltypes.proto >/dev/null
+ ./test_encode3_buf 1 | ./test_decode3_buf 1
./bc_encode | ./bc_decode
./test_missing_fields