static bool write_repeated_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { uint32_t dummy = 0; /* Make it a packed field */ return pb_encode_tag(stream, PB_WT_STRING, field->tag) && pb_encode_varint(stream, 5 * 4) && /* Number of bytes */ pb_encode_fixed32(stream, &dummy) && pb_encode_fixed32(stream, &dummy) && pb_encode_fixed32(stream, &dummy) && pb_encode_fixed32(stream, &dummy) && pb_encode_fixed32(stream, *arg); }
static int pb_encode_proto(pb_ostream_t *stream, const pc_JSON *gprotos, const pc_JSON *protos, const pc_JSON *proto, pc_JSON *value) { pc_JSON *_messages; pc_JSON *_type = pc_JSON_GetObjectItem(proto, "type"); pc_JSON *sub_msg; const char *type = _type->valuestring; const char *str; int int_val; float float_val; double double_val; int length; _messages = pc_JSON_GetObjectItem(protos, "__messages"); switch (pb_get_type(type)) { case PB_uInt32: int_val = value->valueint; if (!pb_encode_varint(stream, int_val)) { return 0; } break; case PB_int32: case PB_sInt32: int_val = value->valueint; if (!pb_encode_svarint(stream, int_val)) { return 0; } break; case PB_float: float_val = (float)value->valuedouble; if (!pb_encode_fixed32(stream, &float_val)) { return 0; } break; case PB_double: double_val = value->valuedouble; if (!pb_encode_fixed64(stream, &double_val)) { return 0; } break; case PB_string: str = value->valuestring; length = strlen(str); if (!pb_encode_string(stream, (const uint8_t *)str, length)) { return 0; } break; default: if (_messages) { sub_msg = pc_JSON_GetObjectItem(_messages, type); if (!sub_msg) { /* check root msg in gprotos */ const char *head = "message "; size_t len = strlen(head) + strlen(type) + 1; char *head_text = (char *)malloc(len); memset(head_text, 0, len); strcpy(head_text, head); strcat(head_text, type); sub_msg = pc_JSON_GetObjectItem(gprotos, head_text); free(head_text); } if (sub_msg) { if (!pb_encode_submessage(stream, gprotos, sub_msg, value)) { return 0; } } else { return 0; } } } return 1; }
static bool write_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { return pb_encode_tag_for_field(stream, field) && pb_encode_fixed32(stream, *arg); }
static bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, const void *src) { PB_UNUSED(field); return pb_encode_fixed32(stream, src); }
bool micropb_encode<float, nullptr>(pb_ostream_t *stream, float* arg) { return pb_encode_fixed32(stream, static_cast<void*>(arg)); }
static int checkreturn pb_encode_proto(pb_ostream_t *stream, const json_t *gprotos, const json_t *protos, const json_t *proto, json_t *value) { json_t *_messages; json_t *_type = json_object_get(proto, "type"); json_t *sub_msg; const char *type = json_string_value(_type); const char *str; json_int_t int_val; float float_val; double double_val; int length; _messages = json_object_get(protos, "__messages"); switch (pb__get_type(type)) { case PB_uInt32: int_val = json_number_value(value); if (!pb_encode_varint(stream, int_val)) { return 0; } break; case PB_int32: case PB_sInt32: int_val = json_number_value(value); if (!pb_encode_svarint(stream, int_val)) { return 0; } break; case PB_float: float_val = json_number_value(value); if (!pb_encode_fixed32(stream, &float_val)) { return 0; } break; case PB_double: double_val = json_number_value(value); if (!pb_encode_fixed64(stream, &double_val)) { return 0; } break; case PB_string: str = json_string_value(value); length = strlen(str); if (!pb_encode_string(stream, (const uint8_t *)str, length)) { return 0; } break; default: if (_messages) { sub_msg = json_object_get(_messages, type); if (!sub_msg) { // check root msg in gprotos const char *head = "message "; char *head_text = (char *)pc_jsonp_malloc(strlen(head) + strlen(type) + 1); memset(head_text, 0, sizeof(head_text)); strcpy(head_text, head); strcat(head_text, type); sub_msg = json_object_get(gprotos, head_text); pc_jsonp_free(head_text); } if (sub_msg) { if (!pb_encode_submessage(stream, gprotos, sub_msg, value)) { return 0; } } else { return 0; } } } return 1; }