+/* In proto3, all fields are optional and are only encoded if their value is "non-zero".
+ * This function implements the check for the zero value. */
+static bool pb_check_proto3_default_value(const pb_field_t *field, const void *pData)
+{
+ if(PB_LTYPE(field->type) == PB_LTYPE_BYTES)
+ {
+ const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)pData;
+ return bytes->size == 0;
+ }
+ else if (PB_LTYPE(field->type) == PB_LTYPE_STRING)
+ {
+ return *(const char*)pData == '\0';
+ }
+ else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED_LENGTH_BYTES)
+ {
+ /* Fixed length bytes is only empty if its length is fixed
+ * as 0. Which would be pretty strange, but we can check
+ * it anyway. */
+ return field->data_size == 0;
+ }
+ else
+ {
+ /* PB_LTYPE_VARINT, UVARINT, SVARINT, FIXED32, FIXED64,
+ * SUBMESSAGE, EXTENSION: These all have integer or pointer
+ * value which can be compared with 0. This does the check
+ * byte-by-byte to avoid the switch-cast logic used in
+ * pb_enc_varint(). (Casting to char* is safe with regards
+ * to C strict aliasing rules.)
+ */
+ uint_fast8_t i;
+ const char *p = (const char*)pData;
+ for (i = 0; i < field->data_size; i++)
+ {
+ if (p[i] != 0)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+}
+