Protocol Buffers implementation in C

Build Status Coverage Status

Overview

This is protobuf-c, a C implementation of the Google Protocol Buffers data serialization format. It includes libprotobuf-c, a pure C library that implements protobuf encoding and decoding, and protoc-c, a code generator that converts Protocol Buffer .proto files to C descriptor code, based on the original protoc. protobuf-c formerly included an RPC implementation; that code has been split out into the protobuf-c-rpc project.

protobuf-c was originally written by Dave Benson and maintained by him through version 0.15 but is now being maintained by a new team. Thanks, Dave!

Mailing list

protobuf-c's mailing list is hosted on a Google Groups forum. Subscribe by sending an email to [email protected].

Building

protobuf-c requires a C compiler, a C++ compiler, protobuf, and pkg-config to be installed.

./configure && make && make install

If building from a git checkout, the autotools (autoconf, automake, libtool) must also be installed, and the build system must be generated by running the autogen.sh script.

./autogen.sh && ./configure && make && make install

Documentation

See the online Doxygen documentation here or the Wiki for a detailed reference. The Doxygen documentation can be built from the source tree by running:

make html

Synopsis

Use the protoc command to generate .pb-c.c and .pb-c.h output files from your .proto input file. The --c_out options instructs protoc to use the protobuf-c plugin.

protoc --c_out=. example.proto

Include the .pb-c.h file from your C source code.

#include "example.pb-c.h"

Compile your C source code together with the .pb-c.c file. Add the output of the following command to your compile flags.

pkg-config --cflags 'libprotobuf-c >= 1.0.0'

Link against the libprotobuf-c support library. Add the output of the following command to your link flags.

pkg-config --libs 'libprotobuf-c >= 1.0.0'

If using autotools, the PKG_CHECK_MODULES macro can be used to detect the presence of libprotobuf-c. Add the following line to your configure.ac file:

PKG_CHECK_MODULES([PROTOBUF_C], [libprotobuf-c >= 1.0.0])

This will place compiler flags in the PROTOBUF_C_CFLAGS variable and linker flags in the PROTOBUF_C_LDFLAGS variable. Read more information here about the PKG_CHECK_MODULES macro.

Versioning

protobuf-c follows the Semantic Versioning Specification as of version 1.0.0.

Note that as of version of 1.0.0, the header files generated by the protoc-c compiler contain version guards to prevent incompatibilities due to version skew between the .pb-c.h files generated by protoc-c and the public protobuf-c.h include file supplied by the libprotobuf-c support library. While we will try not to make changes to protobuf-c that will require triggering the version guard often, such as releasing a new major version of protobuf-c, this cannot be guaranteed. Thus, it's a good idea to recompile your .pb-c.c and .pb-c.h files from their source .proto files with protoc-c as part of your build system, with proper source file dependency tracking, rather than shipping potentially stale .pb-c.c and .pb-c.h files that may not be compatible with the libprotobuf-c headers installed on the system in project artifacts like repositories and release tarballs. (Note that the output of the protoc-c code generator is not standalone, as the output of some other tools that generate C code is, such as flex and bison.)

Major API/ABI changes may occur between major version releases, by definition. It is not recommended to export the symbols in the code generated by protoc-c in a stable library interface, as this will embed the protobuf-c ABI into your library's ABI. Nor is it recommended to install generated .pb-c.h files into a public header file include path as part of a library API, as this will tie clients of your library's API to particular versions of libprotobuf-c.

Contributing

Please send patches to the protobuf-c mailing list or by opening a GitHub pull request.

The most recently released protobuf-c version is kept on the master branch, while the next branch is used for commits targeted at the next release. Please base patches and pull requests against the next branch, not the master branch.

Copyright to all contributions are retained by the original author, but must be licensed under the terms of the BSD-2-Clause license. Please add a Signed-off-by header to your commit message (git commit -s) to indicate that you are licensing your contribution under these terms.

Comments
  • proto3 support

    proto3 support

    This is an experimental first cut at adding proto3 support and I have not tested it further than porting to proto3 the testcase. Before pushing this further I would like to have some feedback on whether I am on the right track.

    As far as I understand protobuf-c already has pretty much everything needed once it is built using a new version of protibuf itself. The only missing thing is that in proto3 all fields are optional and having to manually set has_foo is inconvenient.

    This patch special cases the proto3 syntax files so that structs for the bytes, enum and primitive fields do not emit the has_ field.

    It also adds PROTOBUF_C_LABEL_NONE to the label enum that is used for proto3 fields. When a fields has this label, the quantifier is not consulted and instead the field is packed/unpacked depending on whether it has a value different from NULL/0.

    The patch changes the generated-code test to proto3 as a quick test, clearly this should not be part of the final patch.

    The patch also slightly refactors packing/unpacking optional fields so that the internal functions take a boolean istead of a pointer to a boolean: casting in the caller seems cleaner to me... this part can be dropped or split out in its own preliminary patch.

  • proto3 and nested submessages

    proto3 and nested submessages

    I think I found a bug using the current code with proto3 syntax. I'm using a protobuf with two layers of nested repeated submessages, in other words I have a subsubmessage. I can populate the fields just fine, but packing the buffer turns all the subsubmessage values to 0. The other fields in the submessage are sent fine. Switching back to proto2 fixes the issue.

  • unable to get ProtobufCFieldDescriptor for oneof message

    unable to get ProtobufCFieldDescriptor for oneof message

    if I have a message with one of like

    message Wrapper { oneof msg { MsgA msg_a = 1; MsgB msg_b = 2; } }

    How can I get the offset at which I can write my message A or B?

    In other words, if I do

    desc = protobuf_c_message_descriptor_get_field_by_name(descriptor, "msg_a"); I can get desc->offset

    But this requires knowing that the field is called "msg_a". Is there any way to obtain the same offset only knowing that the oneof is called "msg"?

    Basically the use case I have is that I have many wrapper messages that contain "oneof msg", but the actual fields inside the oneof change. I would like to be able to write a generic function that gives me the "unwrapped" message

  • fix compilation errors on Visual Studio

    fix compilation errors on Visual Studio

    I have separated the Visual Studio build errors from cmake (https://github.com/protobuf-c/protobuf-c/pull/155).

    Here is the patch that allows compilation on Windows. It fixes 3 different (but simple) errors with following reasons:

    • use _inline for non-c99 msvc compiler
    • use char* instead of void* in pointer shifting
    • on non-gnuc family compilers DEPRECATED macros was undefined

    (see also https://github.com/protobuf-c/protobuf-c/issues/168)

  • unpack to stack

    unpack to stack

    It'd be nice to have a way to unpack to a struct on the stack, rather than require an allocator. This might not be practical for protos with strings or similar, but should be fine for number-only data.

  • Invalid namespace

    Invalid namespace

    google::protobuf::message::Reflaction is not exist namespace.

    I try to ubuntu build. But namespace is invalid, so can't build.

    I changed correct namespace

  • Install failed

    Install failed

    There were new changes in https://github.com/google/protobuf.git. Because of that protobuf-c install is getting failed. Error faced as below:

    checking google/protobuf/compiler/command_line_interface.h usability... no checking google/protobuf/compiler/command_line_interface.h presence... no checking for google/protobuf/compiler/command_line_interface.h... no ERROR: Installing protobuf-c library. Please check env_protobuf.log

  • proto3: make strings default to

    proto3: make strings default to "" instead of NULL

    The spec talks about "empty string" and other languages like C# return a zero length string and not null. This is useful because when moving from proto2's "required string" to a proto3's plain string we will be guaranteed that we never get a null pointer. The tradeoff is adding a special case to the library but avoiding a lot of null checks in the calling code. The current code is already special casing "" in pack_string.

  • compile using casues std:bad_Alloc error

    compile using casues std:bad_Alloc error

    step1:

    ./configure CFLAGS=-m64 CXXFLAGS=-m64 LDFLAGS=-m64^C

    step 2: make

    Get error during make stage: GEN t/test.pb-c.c terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc /bin/sh: line 1: 4337 Aborted (core dumped) ./protoc-c/protoc-c -I. --c_out=. ./t/test.proto

    GEN t/test.pb-c.c terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc /bin/sh: line 1: 4337 Aborted (core dumped) ./protoc-c/protoc-c -I. --c_out=. ./t/test.proto

    GEN t/test.pb-c.c terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc /bin/sh: line 1: 4337 Aborted (core dumped) ./protoc-c/protoc-c -I. --c_out=. ./t/test.proto

  • text format is support...

    text format is support...

    I like to use protobufs as a config file format so the ability to parse text format protobufs would be nice. It's also nice to be able to dump them to text.

    I've been using the ProtobufCMessageDescriptor and ProtobufCFieldDescriptor types and they seem like they'll be useful in that and I have the basics of a text format generator now. A parser should be relatively easy.

    Would there be any interest in this code for libprotobuf-c? It would really add a lot of bulk to the library as the linker will just discard it if it's not used. Or I could make a different target for it - say libprotobuf-c-text.

    Thoughts?

  • make error on Ubuntu 14.0LTS

    make error on Ubuntu 14.0LTS

    I cloned protobuf-c from git master and trying to make it run but i Have following errors: image

    After configuring the output looks like this :

    image

    I am wondering whats wrong there.

  • Add CIFuzz GitHub action

    Add CIFuzz GitHub action

    Add CIFuzz workflow action to have fuzzers build and run on each PR.

    This is a service offered by OSS-Fuzz where protobuf-c already runs (https://github.com/google/oss-fuzz/tree/master/projects/protobuf-c). CIFuzz can help detect regressions and catch fuzzing build issues early, and has a variety of features (see the URL above). In the current PR the fuzzers gets build on a pull request and will run for 120 seconds.

  • Q: which C (not C++) grpc library do you use with protobuf-c?

    Q: which C (not C++) grpc library do you use with protobuf-c?

    Hi,

    Maybe a silly question: but which C (not C++) grpc library do you use with protobuf-c?

    (I searched a bit, but didn't find any good / active maintained C (not C++) grpc library on the web).

    Or may I use a Rust (or some other language's) grpc library with protobuf-c? has anyone tried that? any tutorials?

    Thanks.

  • GitHub actions fail on Windows due to missing unzip command

    GitHub actions fail on Windows due to missing unzip command

    Windows builds fail on GitHub due to missing unzip:

    https://github.com/protobuf-c/protobuf-c/actions/runs/3120315250/jobs/5060804075

    7zip is installed on the GitHub runners and can be used instead.

  • Fix codesmell

    Fix codesmell "or with 0" in generated flags

    New versions of cppcheck are complaining loudly (and rightfully) about the use of patterns like:

    0 | PROTOBUF_C_FIELD_FLAG_ONEOF
    

    This PR specifically removes those cases after the concatenation of the various strings which seemed like the minimal intrusive solution (rather than trying to avoid creating them in the first place).

    Signed-off-by: Daniel Egger [email protected]

  • uppack PB file with repeated define more than 30 will give NULL error

    uppack PB file with repeated define more than 30 will give NULL error

    message DownloadParametersRequest { int32 RequestId = 1; repeated int32 ParameterIds = 2; repeated int32 ParameterValues = 3; } for the PB file define with repeated, but when use it to download the parameter more than 30, when unpack the messages, with will give NULL error: ;Communication__DownloadParametersRequest *unpack_DownloadParametersRequest; // Unpack the message using protobuf-c. unpack_DownloadParametersRequest = communication__download_parameters_request__unpack(NULL, pbmsg_len, ((const uint8_t *)pbmsg_data)); if (unpack_DownloadParametersRequest == NULL) { }

    So what will be the reason??

Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Nov 28, 2022
Google Protocol Buffers tools (C code generator).

About Google Protocol Buffers tools in Python 3.6+. C source code generator. Rust source code generator ( ?? ?? ?? under construction ?? ?? ?? ). prot

Nov 29, 2022
Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Dec 3, 2022
MessagePack implementation for C and C++ / msgpack.org[C/C++]

msgpack for C/C++ It's like JSON but smaller and faster. Overview MessagePack is an efficient binary serialization format, which lets you exchange dat

Nov 24, 2022
a small protobuf implementation in C

μpb - a small protobuf implementation in C Platform Build Status macOS ubuntu μpb (often written 'upb') is a small protobuf implementation written in

Nov 24, 2022
An implementation of the MessagePack serialization format in C / msgpack.org[C]

CMP CMP is a C implementation of the MessagePack serialization format. It currently implements version 5 of the MessagePack Spec. CMP's goal is to be

Nov 17, 2022
Base64 Encoding implementation in C Programming Language

cb64 Base64 Encoding implementation in C Programming Language Spec: https://datatracker.ietf.org/doc/html/rfc4648#page-5 Header only So just copy cb64

Dec 14, 2021
static_vector implementation in terms of std::vector. No need to implement each function again and again ;)

static_vector static_vector implementation in terms of std::vector. No need to implement each function again and again ;) The usage is basically the s

Oct 29, 2022
Protocol Buffers with small code size

Nanopb - Protocol Buffers for Embedded Systems Nanopb is a small code-size Protocol Buffers implementation in ansi C. It is especially suitable for us

Nov 29, 2022
Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Nov 28, 2022
Google Protocol Buffers tools (C code generator).

About Google Protocol Buffers tools in Python 3.6+. C source code generator. Rust source code generator ( ?? ?? ?? under construction ?? ?? ?? ). prot

Nov 29, 2022
A protocol buffers library for C

PBC PBC is a google protocol buffers library for C without code generation. Quick Example package tutorial; message Person { required string name =

Nov 20, 2022
Protocol Buffers - Google's data interchange format

Protocol Buffers - Google's data interchange format Copyright 2008 Google Inc. https://developers.google.com/protocol-buffers/ Overview Protocol Buffe

Dec 3, 2022
Simple and fast C library implementing a thread-safe API to manage hash-tables, linked lists, lock-free ring buffers and queues

libhl C library implementing a set of APIs to efficiently manage some basic data structures such as : hashtables, linked lists, queues, trees, ringbuf

Nov 19, 2022
CUDA Custom Buffers and example blocks
CUDA Custom Buffers and example blocks

gr-cuda CUDA Support for GNU Radio using the custom buffer changes introduced in GR 3.10. Custom buffers for CUDA-enabled hardware are provided that c

Aug 17, 2022
An implementation of the Mobile Adapter GB protocol, to connect to custom game servers.

libmobile Library that implements the Mobile Adapter GB protocol, in a way that should be easy to integrate into a plethora of different emulators/har

Nov 5, 2022
The implementation of the Domino Network protocol based program.

spacex The implementation of the Domino distributed storage network protocol for the Smart Ecology. With a fully featured and well documented that pro

Sep 26, 2021
Simple HTTP protocol implementation in C

C-SimpleHTTP A simple HTTP protocol implementation in C How do i run it? First of all, this project is made in c, then you will need GCC Compiler. The

May 25, 2022
xeus-wren is a Jupyter kernel for wren based on the native implementation of the Jupyter protocol xeus.

xeus-wren is a Jupyter kernel for wren based on the native implementation of the Jupyter protocol xeus. Installation xeus-wren has not been packaged f

Mar 9, 2022