Commit f87e0849 authored by lahiker42's avatar lahiker42

use uint8_t and size_t where appropriate.

implement and test unknown fields


git-svn-id: https://protobuf-c.googlecode.com/svn/trunk@44 00440858-1255-0410-a3e6-75ea37f81c3a
parent 348d3006
- check over documentation again
- handle unknown fields when packing
- test code
- make services derive from ProtobufCService;
be usable with a cast.
- make services derive from ProtobufCService; be usable with a cast.
- provide example rpc
- use size_t wherever appropriate in generated code,
and in uses of that code (eg pack and unpack)
- To document:
- __INIT macro
......
This diff is collapsed.
......@@ -5,6 +5,15 @@
#include <stddef.h>
#include <assert.h>
#ifdef __cplusplus
# define PROTOBUF_C_BEGIN_DECLS extern "C" {
# define PROTOBUF_C_END_DECLS }
#else
# define PROTOBUF_C_BEGIN_DECLS
# define PROTOBUF_C_END_DECLS
#endif
PROTOBUF_C_BEGIN_DECLS
typedef enum
{
......@@ -45,7 +54,7 @@ typedef struct _ProtobufCBinaryData ProtobufCBinaryData;
struct _ProtobufCBinaryData
{
size_t len;
unsigned char *data;
uint8_t *data;
};
typedef struct _ProtobufCIntRange ProtobufCIntRange; /* private */
......@@ -71,7 +80,7 @@ struct _ProtobufCBuffer
{
void (*append)(ProtobufCBuffer *buffer,
size_t len,
const unsigned char *data);
const uint8_t *data);
};
/* --- enums --- */
typedef struct _ProtobufCEnumValue ProtobufCEnumValue;
......@@ -147,7 +156,7 @@ struct _ProtobufCMessage
size_t protobuf_c_message_get_packed_size(const ProtobufCMessage *message);
size_t protobuf_c_message_pack (const ProtobufCMessage *message,
unsigned char *out);
uint8_t *out);
size_t protobuf_c_message_pack_to_buffer (const ProtobufCMessage *message,
ProtobufCBuffer *buffer);
......@@ -155,7 +164,7 @@ ProtobufCMessage *
protobuf_c_message_unpack (const ProtobufCMessageDescriptor *,
ProtobufCAllocator *allocator,
size_t len,
const unsigned char *data);
const uint8_t *data);
void protobuf_c_message_free_unpacked (ProtobufCMessage *message,
ProtobufCAllocator *allocator);
......@@ -219,7 +228,7 @@ struct _ProtobufCMessageUnknownField
uint32_t tag;
ProtobufCWireType wire_type;
size_t len;
unsigned char *data;
uint8_t *data;
};
/* --- extra (superfluous) api: trivial buffer --- */
......@@ -229,7 +238,7 @@ struct _ProtobufCBufferSimple
ProtobufCBuffer base;
size_t alloced;
size_t len;
unsigned char *data;
uint8_t *data;
protobuf_c_boolean must_free_data;
};
#define PROTOBUF_C_BUFFER_SIMPLE_INIT(array_of_bytes) \
......@@ -241,4 +250,7 @@ struct _ProtobufCBufferSimple
/* ====== private ====== */
#include "protobuf-c-private.h"
PROTOBUF_C_END_DECLS
#endif /* __PROTOBUF_C_RUNTIME_H_ */
......@@ -61,7 +61,7 @@ void BytesFieldGenerator::GenerateStructMembers(io::Printer* printer) const
printer->Print(variables_, "ProtobufCBinaryData $name$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
printer->Print(variables_, "unsigned n_$name$;\n");
printer->Print(variables_, "size_t n_$name$;\n");
printer->Print(variables_, "ProtobufCBinaryData *$name$;\n");
break;
}
......
......@@ -66,7 +66,7 @@ void EnumFieldGenerator::GenerateStructMembers(io::Printer* printer) const
printer->Print(variables_, "$type$ $name$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
printer->Print(variables_, "unsigned n_$name$;\n");
printer->Print(variables_, "size_t n_$name$;\n");
printer->Print(variables_, "$type$ *$name$;\n");
break;
}
......
......@@ -84,6 +84,8 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
"#define PROTOBUF_C_$filename_identifier$__INCLUDED\n"
"\n"
"#include <google/protobuf-c/protobuf-c.h>\n"
"\n"
"PROTOBUF_C_BEGIN_DECLS\n"
"\n",
"filename_identifier", filename_identifier);
......@@ -169,6 +171,8 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
}
printer->Print(
"\n"
"PROTOBUF_C_END_DECLS\n"
"\n\n#endif // PROTOBUF_$filename_identifier$__INCLUDED\n",
"filename_identifier", filename_identifier);
}
......
......@@ -145,7 +145,7 @@ GenerateHelperFunctionDeclarations(io::Printer* printer)
" (const $classname$ *message);\n"
"size_t $lcclassname$__pack\n"
" (const $classname$ *message,\n"
" unsigned char *out);\n"
" uint8_t *out);\n"
"size_t $lcclassname$__pack_to_buffer\n"
" (const $classname$ *message,\n"
" ProtobufCBuffer *buffer);\n"
......@@ -153,7 +153,7 @@ GenerateHelperFunctionDeclarations(io::Printer* printer)
" $lcclassname$__unpack\n"
" (ProtobufCAllocator *allocator,\n"
" size_t len,\n"
" const unsigned char *data);\n"
" const uint8_t *data);\n"
"void $lcclassname$__free_unpacked\n"
" ($classname$ *message,\n"
" ProtobufCAllocator *allocator);\n"
......@@ -211,7 +211,7 @@ GenerateHelperFunctionDefinitions(io::Printer* printer)
"}\n"
"size_t $lcclassname$__pack\n"
" (const $classname$ *message,\n"
" unsigned char *out)\n"
" uint8_t *out)\n"
"{\n"
" PROTOBUF_C_ASSERT (message->base.descriptor == &$lcclassname$__descriptor);\n"
" return protobuf_c_message_pack ((ProtobufCMessage*)message, out);\n"
......@@ -227,7 +227,7 @@ GenerateHelperFunctionDefinitions(io::Printer* printer)
" $lcclassname$__unpack\n"
" (ProtobufCAllocator *allocator,\n"
" size_t len,\n"
" const unsigned char *data)\n"
" const uint8_t *data)\n"
"{\n"
" return ($classname$ *)\n"
" protobuf_c_message_unpack (&$lcclassname$__descriptor,\n"
......
......@@ -52,7 +52,7 @@ void MessageFieldGenerator::GenerateStructMembers(io::Printer* printer) const
printer->Print(vars, "$type$ *$name$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
printer->Print(vars, "unsigned n_$name$;\n");
printer->Print(vars, "size_t n_$name$;\n");
printer->Print(vars, "$type$ **$name$;\n");
break;
}
......
......@@ -76,7 +76,7 @@ void PrimitiveFieldGenerator::GenerateStructMembers(io::Printer* printer) const
printer->Print(vars, "$c_type$ $name$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
printer->Print(vars, "unsigned n_$name$;\n");
printer->Print(vars, "size_t n_$name$;\n");
printer->Print(vars, "$c_type$ *$name$;\n");
break;
}
......
......@@ -58,7 +58,7 @@ void StringFieldGenerator::GenerateStructMembers(io::Printer* printer) const
printer->Print(variables_, "char *$name$;\n");
break;
case FieldDescriptor::LABEL_REPEATED:
printer->Print(variables_, "unsigned n_$name$;\n");
printer->Print(variables_, "size_t n_$name$;\n");
printer->Print(variables_, "char **$name$;\n");
break;
}
......
......@@ -752,6 +752,26 @@ static void dump_test_repeated_SubMess (void)
dump_message_bytes(&mess, "test_repeated_submess_1");
}
static void dump_test_unknown_fields (void)
{
EmptyMess mess;
google::protobuf::Message::Reflection *reflection = mess.GetReflection();
google::protobuf::UnknownFieldSet *fs = reflection->MutableUnknownFields();
google::protobuf::UnknownField *f;
f = fs->AddField(5454);
f->add_varint(255);
f = fs->AddField(5555);
f->add_fixed32(260);
dump_message_bytes (&mess, "test_unknown_fields_0");
fs->Clear();
f = fs->AddField(6666);
f->add_length_delimited("xxxxxxxx");
f = fs->AddField(7777);
f->add_fixed64(0x10101);
dump_message_bytes (&mess, "test_unknown_fields_1");
}
int main()
{
dump_test_enum_small ();
......@@ -811,5 +831,6 @@ int main()
dump_test_repeated_string ();
dump_test_repeated_bytes ();
dump_test_repeated_SubMess ();
dump_test_unknown_fields ();
return 0;
}
......@@ -142,4 +142,6 @@ message TestMessRequiredBytes {
message TestMessRequiredMessage {
required SubMess test = 1;
}
message EmptyMess {
}
......@@ -975,6 +975,62 @@ static void test_repeated_SubMess (void)
#undef DO_TEST
}
static void test_unknown_fields (void)
{
static Foo__EmptyMess mess = FOO__EMPTY_MESS__INIT;
static Foo__EmptyMess *mess2;
ProtobufCMessageUnknownField fields[2];
size_t len; uint8_t *data;
mess.base.n_unknown_fields = 2;
mess.base.unknown_fields = fields;
fields[0].tag = 5454;
fields[0].wire_type = PROTOBUF_C_WIRE_TYPE_VARINT;
fields[0].len = 2;
fields[0].data = (uint8_t*)"\377\1";
fields[1].tag = 5555;
fields[1].wire_type = PROTOBUF_C_WIRE_TYPE_32BIT;
fields[1].len = 4;
fields[1].data = (uint8_t*)"\4\1\0\0";
mess2 = test_compare_pack_methods (&mess.base, &len, &data);
assert (mess2->base.n_unknown_fields == 2);
assert (mess2->base.unknown_fields[0].tag == 5454);
assert (mess2->base.unknown_fields[0].wire_type == PROTOBUF_C_WIRE_TYPE_VARINT);
assert (mess2->base.unknown_fields[0].len == 2);
assert (memcmp (mess2->base.unknown_fields[0].data, fields[0].data, 2) == 0);
assert (mess2->base.unknown_fields[1].tag == 5555);
assert (mess2->base.unknown_fields[1].wire_type == PROTOBUF_C_WIRE_TYPE_32BIT);
assert (mess2->base.unknown_fields[1].len == 4);
assert (memcmp (mess2->base.unknown_fields[1].data, fields[1].data, 4) == 0);
TEST_VERSUS_STATIC_ARRAY (len, data, test_unknown_fields_0);
free (data);
foo__empty_mess__free_unpacked (mess2, NULL);
fields[0].tag = 6666;
fields[0].wire_type = PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED;
fields[0].len = 9;
fields[0].data = (uint8_t*)"\10xxxxxxxx";
fields[1].tag = 7777;
fields[1].wire_type = PROTOBUF_C_WIRE_TYPE_64BIT;
fields[1].len = 8;
fields[1].data = (uint8_t*)"\1\1\1\0\0\0\0\0";
mess2 = test_compare_pack_methods (&mess.base, &len, &data);
assert (mess2->base.n_unknown_fields == 2);
assert (mess2->base.unknown_fields[0].tag == 6666);
assert (mess2->base.unknown_fields[0].wire_type == PROTOBUF_C_WIRE_TYPE_LENGTH_PREFIXED);
assert (mess2->base.unknown_fields[0].len == 9);
assert (memcmp (mess2->base.unknown_fields[0].data, fields[0].data, 9) == 0);
assert (mess2->base.unknown_fields[1].tag == 7777);
assert (mess2->base.unknown_fields[1].wire_type == PROTOBUF_C_WIRE_TYPE_64BIT);
assert (mess2->base.unknown_fields[1].len == 8);
assert (memcmp (mess2->base.unknown_fields[1].data, fields[1].data, 8) == 0);
TEST_VERSUS_STATIC_ARRAY (len, data, test_unknown_fields_1);
free (data);
foo__empty_mess__free_unpacked (mess2, NULL);
}
/* === simple testing framework === */
typedef void (*TestFunc) (void);
......@@ -1048,6 +1104,8 @@ static Test tests[] =
{ "test repeated string", test_repeated_string },
{ "test repeated bytes", test_repeated_bytes },
{ "test repeated SubMess", test_repeated_SubMess },
{ "test unknown fields", test_unknown_fields }
};
#define n_tests (sizeof(tests)/sizeof(Test))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment