For each enum, we generate a C enum. For each message, we generate a C structure which can be cast to a {\ttfamily \hyperlink{struct_protobuf_c_message}{Protobuf\+C\+Message}}.

For each enum and message, we generate a descriptor object that allows us to implement a kind of reflection on the structures.

First, some naming conventions\+:


\begin{DoxyItemize}
\item The name of the type for enums and messages and services is camel case (meaning Words\+Are\+Crammed\+Together) except that double underscores are used to delimit scopes. For example, the following {\ttfamily .proto} file\+:
\end{DoxyItemize}


\begin{DoxyCode}
package foo.bar;
message BazBah \{
    optional int32 val = 1;
\}
\end{DoxyCode}


would generate a C type {\ttfamily Foo\+\_\+\+\_\+\+Bar\+\_\+\+\_\+\+Baz\+Bah}.


\begin{DoxyItemize}
\item Identifiers for functions and globals are all lowercase, with camel case words separated by single underscores. For example, one of the function prototypes generated by {\ttfamily protoc-\/c} for the above example\+:
\end{DoxyItemize}


\begin{DoxyCode}
Foo\_\_Bar\_\_BazBah *
       foo\_\_bar\_\_baz\_bah\_\_unpack
                     (\hyperlink{struct_protobuf_c_allocator}{ProtobufCAllocator}  *allocator,
                      \textcolor{keywordtype}{size\_t}               len,
                      \textcolor{keyword}{const} \hyperlink{uthash_8h_aba7bc1797add20fe3efdf37ced1182c5}{uint8\_t}       *data);
\end{DoxyCode}



\begin{DoxyItemize}
\item Identifiers for enum values contain an uppercase prefix which embeds the package name and the enum type name.
\item A double underscore is used to separate further components of identifier names.
\end{DoxyItemize}

For example, in the name of the unpack function above, the package name {\ttfamily foo.\+bar} has become {\ttfamily foo\+\_\+\+\_\+bar}, the message name Baz\+Bah has become {\ttfamily baz\+\_\+bah}, and the method name is {\ttfamily unpack}. These are all joined with double underscores to form the C identifier {\ttfamily foo\+\_\+\+\_\+bar\+\_\+\+\_\+baz\+\_\+bah\+\_\+\+\_\+unpack}.

We also generate descriptor objects for messages and enums. These are declared in the {\ttfamily .pb-\/c.\+h} files\+:


\begin{DoxyCode}
\textcolor{keyword}{extern} \textcolor{keyword}{const} \hyperlink{struct_protobuf_c_message_descriptor}{ProtobufCMessageDescriptor} foo\_\_bar\_\_baz\_bah\_\_descriptor;
\end{DoxyCode}


The message structures all begin with {\ttfamily \hyperlink{struct_protobuf_c_message_descriptor}{Protobuf\+C\+Message\+Descriptor} $\ast$} which is sufficient to allow them to be cast to {\ttfamily \hyperlink{struct_protobuf_c_message}{Protobuf\+C\+Message}}.

For each message defined in a {\ttfamily .proto} file, we generate a number of functions and macros. Each function name contains a prefix based on the package name and message name in order to make it a unique C identifier.


\begin{DoxyItemize}
\item {\ttfamily I\+N\+IT}. Statically initializes a message object, initializing its descriptor and setting its fields to default values. Uninitialized messages cannot be processed by the protobuf-\/c library.
\end{DoxyItemize}


\begin{DoxyCode}
\textcolor{preprocessor}{#define FOO\_\_BAR\_\_BAZ\_BAH\_\_INIT \(\backslash\)}
\textcolor{preprocessor}{ \{ PROTOBUF\_C\_MESSAGE\_INIT (&foo\_\_bar\_\_baz\_bah\_\_descriptor), 0 \}}
\end{DoxyCode}

\begin{DoxyItemize}
\item {\ttfamily init()}. Initializes a message object, initializing its descriptor and setting its fields to default values. Uninitialized messages cannot be processed by the protobuf-\/c library.
\end{DoxyItemize}


\begin{DoxyCode}
\textcolor{keywordtype}{void} foo\_\_bar\_\_baz\_bah\_\_init
                     (Foo\_\_Bar\_\_BazBah *message);
\end{DoxyCode}

\begin{DoxyItemize}
\item {\ttfamily unpack()}. Unpacks data for a particular message format. Note that the {\ttfamily allocator} parameter is usually {\ttfamily N\+U\+LL} to indicate that the system\textquotesingle{}s {\ttfamily malloc()} and {\ttfamily free()} functions should be used for dynamically allocating memory.
\end{DoxyItemize}


\begin{DoxyCode}
Foo\_\_Bar\_\_BazBah *
       foo\_\_bar\_\_baz\_bah\_\_unpack
                     (\hyperlink{struct_protobuf_c_allocator}{ProtobufCAllocator}  *allocator,
                      \textcolor{keywordtype}{size\_t}               len,
                      \textcolor{keyword}{const} \hyperlink{uthash_8h_aba7bc1797add20fe3efdf37ced1182c5}{uint8\_t}       *data);
\end{DoxyCode}



\begin{DoxyItemize}
\item {\ttfamily free\+\_\+unpacked()}. Frees a message object obtained with the {\ttfamily unpack()} method.
\end{DoxyItemize}


\begin{DoxyCode}
\textcolor{keywordtype}{void}   foo\_\_bar\_\_baz\_bah\_\_free\_unpacked
                     (Foo\_\_Bar\_\_BazBah *message,
                      \hyperlink{struct_protobuf_c_allocator}{ProtobufCAllocator} *allocator);
\end{DoxyCode}



\begin{DoxyItemize}
\item {\ttfamily get\+\_\+packed\+\_\+size()}. Calculates the length in bytes of the serialized representation of the message object.
\end{DoxyItemize}


\begin{DoxyCode}
\textcolor{keywordtype}{size\_t} foo\_\_bar\_\_baz\_bah\_\_get\_packed\_size
                     (\textcolor{keyword}{const} Foo\_\_Bar\_\_BazBah   *message);
\end{DoxyCode}



\begin{DoxyItemize}
\item {\ttfamily pack()}. Pack a message object into a preallocated buffer. Assumes that the buffer is large enough. (Use {\ttfamily get\+\_\+packed\+\_\+size()} first.)
\end{DoxyItemize}


\begin{DoxyCode}
\textcolor{keywordtype}{size\_t} foo\_\_bar\_\_baz\_bah\_\_pack
                     (\textcolor{keyword}{const} Foo\_\_Bar\_\_BazBah   *message,
                      \hyperlink{uthash_8h_aba7bc1797add20fe3efdf37ced1182c5}{uint8\_t}             *out);
\end{DoxyCode}



\begin{DoxyItemize}
\item {\ttfamily pack\+\_\+to\+\_\+buffer()}. Packs a message into a \char`\"{}virtual buffer\char`\"{}. This is an object which defines an \char`\"{}append bytes\char`\"{} callback to consume data as it is serialized.
\end{DoxyItemize}


\begin{DoxyCode}
\textcolor{keywordtype}{size\_t} foo\_\_bar\_\_baz\_bah\_\_pack\_to\_buffer
                     (\textcolor{keyword}{const} Foo\_\_Bar\_\_BazBah   *message,
                      \hyperlink{struct_protobuf_c_buffer}{ProtobufCBuffer}     *buffer);
\end{DoxyCode}
 