Skip to content

Commit

Permalink
C decoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Aug 24, 2019
1 parent b2746f8 commit bd90867
Show file tree
Hide file tree
Showing 16 changed files with 443 additions and 152 deletions.
8 changes: 6 additions & 2 deletions tests/files/c_source/bool.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,13 @@ static int decoder_read_tag(struct decoder_t *self_p,
}

static bool decoder_read_bool(struct decoder_t *self_p,
int wire_type)
int wire_type)
{
return (0);
if (wire_type != 0) {
return (false);
}

return (decoder_read_byte(self_p) == 1);
}

struct bool_message_t *bool_message_new(
Expand Down
64 changes: 55 additions & 9 deletions tests/files/c_source/bytes.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ static bool decoder_available(struct decoder_t *self_p)
return (self_p->pos < self_p->size);
}

static uint8_t decoder_read_byte(struct decoder_t *self_p)
static uint8_t decoder_get(struct decoder_t *self_p)
{
uint8_t value;

Expand All @@ -214,22 +214,68 @@ static uint8_t decoder_read_byte(struct decoder_t *self_p)
return (value);
}

static void decoder_read(struct decoder_t *self_p,
uint8_t *buf_p,
int size)
{
int i;

for (i = 0; i < size; i++) {
buf_p[i] = decoder_get(self_p);
}
}

static int decoder_read_tag(struct decoder_t *self_p,
int *wire_type_p)
{
uint8_t value;

value = decoder_read_byte(self_p);
value = decoder_get(self_p);
*wire_type_p = (value & 0x7);

return (value >> 3);
}

static uint8_t *decoder_read_bytes2(struct decoder_t *self_p,
int wire_type,
size_t *size_p)
static uint64_t decoder_read_varint(struct decoder_t *self_p)
{
uint64_t value;
uint8_t byte;
int offset;

value = 0;
offset = 0;

do {
byte = decoder_get(self_p);
value |= (((uint64_t)byte & 0x7f) << offset);
offset += 7;
} while (byte & 0x80);

return (value);
}

static uint8_t *decoder_read_bytes(struct decoder_t *self_p,
int wire_type,
size_t *size_p)
{
return (0);
uint64_t size;
uint8_t *value_p;

if (wire_type != 2) {
return (NULL);
}

size = decoder_read_varint(self_p);
value_p = heap_alloc(self_p->heap_p, size);

if (value_p == NULL) {
return (NULL);
}

decoder_read(self_p, value_p, size);
*size_p = size;

return (value_p);
}

struct bytes_message_t *bytes_message_new(
Expand Down Expand Up @@ -275,9 +321,9 @@ void bytes_message_decode_inner(
switch (decoder_read_tag(decoder_p, &wire_type)) {

case 1:
message_p->value.buf_p = decoder_read_bytes2(decoder_p,
wire_type,
&message_p->value.size);
message_p->value.buf_p = decoder_read_bytes(decoder_p,
wire_type,
&message_p->value.size);
break;

default:
Expand Down
19 changes: 18 additions & 1 deletion tests/files/c_source/double.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,24 @@ static int decoder_read_tag(struct decoder_t *self_p,
static double decoder_read_double(struct decoder_t *self_p,
int wire_type)
{
return (0);
uint64_t data;
double value;

if (wire_type != 1) {
return (0.0);
}

data = decoder_read_byte(self_p);
data |= ((uint64_t)decoder_read_byte(self_p) << 8);
data |= ((uint64_t)decoder_read_byte(self_p) << 16);
data |= ((uint64_t)decoder_read_byte(self_p) << 24);
data |= ((uint64_t)decoder_read_byte(self_p) << 32);
data |= ((uint64_t)decoder_read_byte(self_p) << 40);
data |= ((uint64_t)decoder_read_byte(self_p) << 48);
data |= ((uint64_t)decoder_read_byte(self_p) << 56);
memcpy(&value, &data, sizeof(value));

return (value);
}

struct double_message_t *double_message_new(
Expand Down
9 changes: 8 additions & 1 deletion tests/files/c_source/fixed32.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,14 @@ static int decoder_read_tag(struct decoder_t *self_p,
static uint32_t decoder_read_fixed32(struct decoder_t *self_p,
int wire_type)
{
return (0);
uint32_t value;

value = decoder_read_byte(self_p);
value |= (decoder_read_byte(self_p) << 8);
value |= (decoder_read_byte(self_p) << 16);
value |= (decoder_read_byte(self_p) << 24);

return (value);
}

struct fixed32_message_t *fixed32_message_new(
Expand Down
13 changes: 12 additions & 1 deletion tests/files/c_source/fixed64.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,18 @@ static int decoder_read_tag(struct decoder_t *self_p,
static uint64_t decoder_read_fixed64(struct decoder_t *self_p,
int wire_type)
{
return (0);
uint64_t value;

value = decoder_read_byte(self_p);
value |= ((uint64_t)decoder_read_byte(self_p) << 8);
value |= ((uint64_t)decoder_read_byte(self_p) << 16);
value |= ((uint64_t)decoder_read_byte(self_p) << 24);
value |= ((uint64_t)decoder_read_byte(self_p) << 32);
value |= ((uint64_t)decoder_read_byte(self_p) << 40);
value |= ((uint64_t)decoder_read_byte(self_p) << 48);
value |= ((uint64_t)decoder_read_byte(self_p) << 56);

return (value);
}

struct fixed64_message_t *fixed64_message_new(
Expand Down
15 changes: 14 additions & 1 deletion tests/files/c_source/float.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,20 @@ static int decoder_read_tag(struct decoder_t *self_p,
static float decoder_read_float(struct decoder_t *self_p,
int wire_type)
{
return (0);
uint32_t data;
float value;

if (wire_type != 5) {
return (0.0);
}

data = decoder_read_byte(self_p);
data |= (decoder_read_byte(self_p) << 8);
data |= (decoder_read_byte(self_p) << 16);
data |= (decoder_read_byte(self_p) << 24);
memcpy(&value, &data, sizeof(value));

return (value);
}

struct float_message_t *float_message_new(
Expand Down
28 changes: 27 additions & 1 deletion tests/files/c_source/int32.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ static uint8_t decoder_read_byte(struct decoder_t *self_p)
value = 0;
}

fprintf(stderr, "rv: 0x%02x\n", value);

return (value);
}

Expand All @@ -222,14 +224,37 @@ static int decoder_read_tag(struct decoder_t *self_p,

value = decoder_read_byte(self_p);
*wire_type_p = (value & 0x7);
fprintf(stderr, "b: 0x%02x, %d\n", value, value >> 3);

return (value >> 3);
}

static uint64_t decoder_read_varint(struct decoder_t *self_p)
{
uint64_t value;
uint8_t byte;
int offset;

value = 0;
offset = 0;

do {
byte = decoder_read_byte(self_p);
value |= (((uint64_t)byte & 0x7f) << offset);
offset += 7;
} while (byte & 0x80);

return (value);
}

static int32_t decoder_read_int32(struct decoder_t *self_p,
int wire_type)
{
return (0);
if (wire_type != 0) {
return (0);
}

return (decoder_read_varint(self_p));
}

struct int32_message_t *int32_message_new(
Expand Down Expand Up @@ -276,6 +301,7 @@ void int32_message_decode_inner(
break;

default:
fprintf(stderr, "bad tag\n");
break;
}
}
Expand Down
24 changes: 23 additions & 1 deletion tests/files/c_source/int64.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,32 @@ static int decoder_read_tag(struct decoder_t *self_p,
return (value >> 3);
}

static uint64_t decoder_read_varint(struct decoder_t *self_p)
{
uint64_t value;
uint8_t byte;
int offset;

value = 0;
offset = 0;

do {
byte = decoder_read_byte(self_p);
value |= (((uint64_t)byte & 0x7f) << offset);
offset += 7;
} while (byte & 0x80);

return (value);
}

static int64_t decoder_read_int64(struct decoder_t *self_p,
int wire_type)
{
return (0);
if (wire_type != 0) {
return (0);
}

return (decoder_read_varint(self_p));
}

struct int64_message_t *int64_message_new(
Expand Down
9 changes: 8 additions & 1 deletion tests/files/c_source/sfixed32.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,14 @@ static int decoder_read_tag(struct decoder_t *self_p,
static int32_t decoder_read_sfixed32(struct decoder_t *self_p,
int wire_type)
{
return (0);
uint32_t value;

value = decoder_read_byte(self_p);
value |= (decoder_read_byte(self_p) << 8);
value |= (decoder_read_byte(self_p) << 16);
value |= (decoder_read_byte(self_p) << 24);

return (value);
}

struct sfixed32_message_t *sfixed32_message_new(
Expand Down
13 changes: 12 additions & 1 deletion tests/files/c_source/sfixed64.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,18 @@ static int decoder_read_tag(struct decoder_t *self_p,
static int64_t decoder_read_sfixed64(struct decoder_t *self_p,
int wire_type)
{
return (0);
uint64_t value;

value = decoder_read_byte(self_p);
value |= ((uint64_t)decoder_read_byte(self_p) << 8);
value |= ((uint64_t)decoder_read_byte(self_p) << 16);
value |= ((uint64_t)decoder_read_byte(self_p) << 24);
value |= ((uint64_t)decoder_read_byte(self_p) << 32);
value |= ((uint64_t)decoder_read_byte(self_p) << 40);
value |= ((uint64_t)decoder_read_byte(self_p) << 48);
value |= ((uint64_t)decoder_read_byte(self_p) << 56);

return (value);
}

struct sfixed64_message_t *sfixed64_message_new(
Expand Down
37 changes: 35 additions & 2 deletions tests/files/c_source/sint32.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,43 @@ static int decoder_read_tag(struct decoder_t *self_p,
return (value >> 3);
}

static uint64_t decoder_read_varint(struct decoder_t *self_p)
{
uint64_t value;
uint8_t byte;
int offset;

value = 0;
offset = 0;

do {
byte = decoder_read_byte(self_p);
value |= (((uint64_t)byte & 0x7f) << offset);
offset += 7;
} while (byte & 0x80);

return (value);
}

static int32_t decoder_read_sint32(struct decoder_t *self_p,
int wire_type)
int wire_type)
{
return (0);
uint64_t value;

if (wire_type != 0) {
return (0);
}

value = decoder_read_varint(self_p);

if (value & 0x1) {
value >>= 1;
value = ~value;
} else {
value >>= 1;
}

return (value);
}

struct sint32_message_t *sint32_message_new(
Expand Down

0 comments on commit bd90867

Please sign in to comment.