Skip to content

Commit

Permalink
C design.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Aug 23, 2019
1 parent 2a81678 commit 7a972e6
Show file tree
Hide file tree
Showing 10 changed files with 1,420 additions and 46 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ test:
test-c:
make -C examples/address_book
examples/address_book/main
make -C tests

release-to-pypi:
python setup.py sdist
Expand Down
3 changes: 3 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
all:
gcc \
files/c_source/int32.c \
files/c_source/int64.c \
files/c_source/sint32.c \
files/c_source/sint64.c \
files/c_source/address_book.c \
main.c
./a.out
81 changes: 36 additions & 45 deletions tests/files/c_source/int32.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ struct decoder_t {
struct int32_heap_t *heap_p;
};

static uint8_t tag(int field_number, int wire_type)
{
return ((field_number << 3) | wire_type);
}

static struct int32_heap_t *heap_new(void *buf_p, size_t size)
{
struct int32_heap_t *heap_p;
Expand Down Expand Up @@ -119,64 +124,50 @@ static void encoder_prepend_byte(struct encoder_t *self_p,
self_p->pos--;
}

static void encoder_prepend_tag(struct encoder_t *self_p,
int field_number,
int wire_type)
static void encoder_prepend_bytes(struct encoder_t *self_p,
uint8_t *buf_p,
int size)
{
encoder_prepend_byte(self_p, (field_number << 3) | wire_type);
int i;

for (i = size - 1; i >= 0; i--) {
encoder_prepend_byte(self_p, buf_p[i]);
}
}

static void encoder_prepend_uint32(struct encoder_t *self_p,
static void encoder_prepend_varint(struct encoder_t *self_p,
int field_number,
uint32_t value)
uint64_t value)
{
uint8_t buf[10];
int pos;

if (value == 0) {
return;
}

if (value < (1 << 7)) {
encoder_prepend_byte(self_p, value);
} else if (value < (1 << 14)) {
encoder_prepend_byte(self_p, value >> 7);
encoder_prepend_byte(self_p, (value & 0x7f) | 0x80);
} else if (value < (1 << 21)) {
encoder_prepend_byte(self_p, value >> 14);
encoder_prepend_byte(self_p, ((value >> 7) & 0x7f) | 0x80);
encoder_prepend_byte(self_p, (value & 0x7f) | 0x80);
} else if (value < (1 << 28)) {
encoder_prepend_byte(self_p, value >> 21);
encoder_prepend_byte(self_p, ((value >> 14) & 0x7f) | 0x80);
encoder_prepend_byte(self_p, ((value >> 7) & 0x7f) | 0x80);
encoder_prepend_byte(self_p, (value & 0x7f) | 0x80);
} else {
encoder_prepend_byte(self_p, value >> 28);
encoder_prepend_byte(self_p, ((value >> 21) & 0x7f) | 0x80);
encoder_prepend_byte(self_p, ((value >> 14) & 0x7f) | 0x80);
encoder_prepend_byte(self_p, ((value >> 7) & 0x7f) | 0x80);
encoder_prepend_byte(self_p, (value & 0x7f) | 0x80);
pos = 0;
buf[pos++] = tag(field_number, 0);

while (value > 0) {
buf[pos++] = (value | 0x80);
value >>= 7;
}

encoder_prepend_tag(self_p, field_number, 0);
buf[pos - 1] &= 0x7f;
encoder_prepend_bytes(self_p, &buf[0], pos);
}

static void encoder_prepend_int32(struct encoder_t *self_p,
int field_number,
int32_t value)
{
if (value < 0) {
encoder_prepend_byte(self_p, 0x01);
encoder_prepend_byte(self_p, 0xff);
encoder_prepend_byte(self_p, 0xff);
encoder_prepend_byte(self_p, 0xff);
encoder_prepend_byte(self_p, 0xff);
encoder_prepend_byte(self_p, (value >> 28) | 0x80);
encoder_prepend_byte(self_p, (value >> 21) | 0x80);
encoder_prepend_byte(self_p, (value >> 14) | 0x80);
encoder_prepend_byte(self_p, (value >> 7) | 0x80);
encoder_prepend_byte(self_p, value | 0x80);
encoder_prepend_tag(self_p, field_number, 0);
encoder_prepend_varint(self_p,
field_number,
value | 0xffffffff00000000ll);
} else {
encoder_prepend_uint32(self_p, field_number, (uint32_t)value);
encoder_prepend_varint(self_p, field_number, (uint64_t)value);
}
}

Expand Down Expand Up @@ -213,14 +204,14 @@ static uint8_t decoder_read_byte(struct decoder_t *self_p)
{
uint8_t value;

if (!decoder_available(self_p)) {
fprintf(stderr, "decoder_read_byte: %d %d\n", self_p->pos, self_p->size);
exit(1);
if (decoder_available(self_p)) {
value = self_p->buf_p[self_p->pos];
self_p->pos++;
} else {
self_p->size = -1;
value = 0;
}

value = self_p->buf_p[self_p->pos];
self_p->pos++;

return (value);
}

Expand Down

0 comments on commit 7a972e6

Please sign in to comment.