static bool write_repeated_svarint(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { return pb_encode_tag_for_field(stream, field) && pb_encode_svarint(stream, 0) && pb_encode_tag_for_field(stream, field) && pb_encode_svarint(stream, 0) && pb_encode_tag_for_field(stream, field) && pb_encode_svarint(stream, 0) && pb_encode_tag_for_field(stream, field) && pb_encode_svarint(stream, 0) && pb_encode_tag_for_field(stream, field) && pb_encode_svarint(stream, (long)*arg); }
bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src) { int64_t value = 0; switch (field->data_size) { case 4: value = *(const int32_t*)src; break; case 8: value = *(const int64_t*)src; break; default: return false; } return pb_encode_svarint(stream, value); }
static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src) { int64_t value = 0; switch (field->data_size) { case 4: value = *(const int32_t*)src; break; case 8: value = *(const int64_t*)src; break; default: PB_RETURN_ERROR(stream, "invalid data_size"); } return pb_encode_svarint(stream, value); }
static bool checkreturn pb_enc_svarint(pb_ostream_t *stream, const pb_field_t *field, const void *src) { int64_t value = 0; if (field->data_size == sizeof(int_least8_t)) value = *(const int_least8_t*)src; else if (field->data_size == sizeof(int_least16_t)) value = *(const int_least16_t*)src; else if (field->data_size == sizeof(int32_t)) value = *(const int32_t*)src; else if (field->data_size == sizeof(int64_t)) value = *(const int64_t*)src; else PB_RETURN_ERROR(stream, "invalid data_size"); return pb_encode_svarint(stream, value); }
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 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; }