+Field callbacks
+===============
+When a field has dynamic length, nanopb cannot statically allocate storage for it. Instead, it allows you to handle the field in whatever way you want, using a callback function.
+
+The `pb_callback_t`_ structure contains a function pointer and a *void* pointer you can use for passing data to the callback. The actual behavior of the callback function is different in encoding and decoding modes.
+
+.. _`pb_callback_t`: reference.html#pb-callback-t
+
+Encoding callbacks
+------------------
+::
+
+ bool (*encode)(pb_ostream_t *stream, const pb_field_t *field, const void *arg);
+
+When encoding, the callback should write out complete fields, including the wire type and field number tag. It can write as many or as few fields as it likes. For example, if you want to write out an array as *repeated* field, you should do it all in a single call.
+
+The callback may be called multiple times during a single call to `pb_encode`_. It must produce the same amount of data every time.
+
+.. _`pb_encode`: reference.html#pb-encode
+
+This callback writes out a dynamically sized string::
+
+ bool write_string(pb_ostream_t *stream, const pb_field_t *field, const void *arg)
+ {
+ char *str = get_string_from_somewhere();
+ if (!pb_encode_tag_for_field(stream, field))
+ return false;
+
+ return pb_encode_string(stream, (uint8_t*)str, strlen(str));
+ }
+
+Decoding callbacks
+------------------
+::
+
+ bool (*decode)(pb_istream_t *stream, const pb_field_t *field, void *arg);
+
+When decoding, the callback receives a length-limited substring that reads the contents of a single field. The field tag has already been read.
+
+The callback will be called multiple times for repeated fields. For packed fields, you can either read multiple values until the stream ends, or leave it to `pb_decode`_ to call your function over and over until all values have been read.
+
+.. _`pb_decode`: reference.html#pb-decode
+
+This callback reads multiple integers and prints them::
+
+ bool read_ints(pb_istream_t *stream, const pb_field_t *field, void *arg)
+ {
+ while (stream.bytes_left)
+ {
+ uint64_t value;
+ if (!pb_decode_varint(stream, &value))
+ return false;
+ printf("%lld\n", value);
+ }
+ return true;
+ }