static bool write_repeated_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
    return pb_encode_tag_for_field(stream, field) &&
           pb_encode_string(stream, 0, 0) &&
           pb_encode_tag_for_field(stream, field) &&
           pb_encode_string(stream, 0, 0) &&
           pb_encode_tag_for_field(stream, field) &&
           pb_encode_string(stream, 0, 0) &&
           pb_encode_tag_for_field(stream, field) &&
           pb_encode_string(stream, 0, 0) &&
           pb_encode_tag_for_field(stream, field) &&
           pb_encode_string(stream, *arg, strlen(*arg));
}
Esempio n. 2
0
static bool write_string_from_arg(pb_ostream_t *stream, const pb_field_t *field,
                                  void *const *arg) {
  const char *str = *arg;
  if (!pb_encode_tag_for_field(stream, field)) return false;

  return pb_encode_string(stream, (uint8_t *)str, strlen(str));
}
Esempio n. 3
0
static bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
    size_t size = 0;
    size_t max_size = field->data_size;
    const char *p = (const char*)src;
    
    if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
        max_size = (size_t)-1;

    if (src == NULL)
    {
        size = 0; /* Threat null pointer as an empty string */
    }
    else
    {
        /* strnlen() is not always available, so just use a loop */
        while (size < max_size && *p != '\0')
        {
            size++;
            p++;
        }
    }

    return pb_encode_string(stream, (const uint8_t*)src, size);
}
Esempio n. 4
0
bool npb_encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
{
    char *str = (char *)*arg;
    if (!pb_encode_tag_for_field(stream, field))
        return false;
    return pb_encode_string(stream, (uint8_t*)str, strlen(str));
}
Esempio n. 5
0
static bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
    const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
    
    if (src == NULL)
    {
        /* Threat null pointer as an empty bytes field */
        return pb_encode_string(stream, NULL, 0);
    }
    
    if (PB_ATYPE(field->type) == PB_ATYPE_STATIC &&
        PB_BYTES_ARRAY_T_ALLOCSIZE(bytes->size) > field->data_size)
    {
        PB_RETURN_ERROR(stream, "bytes size exceeded");
    }
    
    return pb_encode_string(stream, bytes->bytes, bytes->size);
}
bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
    const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;

    if (bytes->size + offsetof(pb_bytes_array_t, bytes) > field->data_size)
        PB_RETURN_ERROR(stream, "bytes size exceeded");
    
    return pb_encode_string(stream, bytes->bytes, bytes->size);
}
Esempio n. 7
0
// TODO: I'm not entirely sure why this can't be in the header...
bool micropb_callback_string_from_tensor(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) {
  at::Tensor* t = static_cast<at::Tensor*>(*arg);
  JIT_ASSERT(t->is_contiguous());
  // Packed array format!
  pb_encode_tag_for_field(stream, field);
  pb_encode_string(stream, (pb_byte_t*)(t->data_ptr()),  t->type().elementSizeInBytes()*t->numel());

  return true;
}
bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
    /* strnlen() is not always available, so just use a for-loop */
    size_t size = 0;
    const char *p = (const char*)src;
    while (size < field->data_size && *p != '\0')
    {
        size++;
        p++;
    }

    return pb_encode_string(stream, (const uint8_t*)src, size);
}
Esempio n. 9
0
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;
}
Esempio n. 10
0
bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
    UNUSED(field);
    return pb_encode_string(stream, (const uint8_t*)src, strlen((const char*)src));
}
Esempio n. 11
0
bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
    const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
    UNUSED(field);
    return pb_encode_string(stream, bytes->bytes, bytes->size);
}
Esempio n. 12
0
bool micropb_encode<std::string, nullptr>(pb_ostream_t *stream, std::string* arg) {
  return pb_encode_string(stream, reinterpret_cast<const pb_byte_t *>(arg->c_str()), arg->size());
}
Esempio n. 13
0
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;
}