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 7a972e6 commit d2ce1e0
Show file tree
Hide file tree
Showing 14 changed files with 2,840 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ all:
files/c_source/int64.c \
files/c_source/sint32.c \
files/c_source/sint64.c \
files/c_source/uint32.c \
files/c_source/uint64.c \
files/c_source/fixed32.c \
files/c_source/fixed64.c \
files/c_source/sfixed32.c \
files/c_source/sfixed64.c \
files/c_source/address_book.c \
main.c
./a.out
288 changes: 288 additions & 0 deletions tests/files/c_source/fixed32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* This file was generated by pbtools version 0.1.0 Thu Jan 24 08:14:06 2019.
*/

#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>

#include "fixed32.h"

struct encoder_t {
uint8_t *buf_p;
int size;
int pos;
};

struct decoder_t {
const uint8_t *buf_p;
int size;
int pos;
struct fixed32_heap_t *heap_p;
};

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

static struct fixed32_heap_t *heap_new(void *buf_p, size_t size)
{
struct fixed32_heap_t *heap_p;

if (size >= sizeof(*heap_p)) {
heap_p = (struct fixed32_heap_t *)buf_p;
heap_p->buf_p = buf_p;
heap_p->size = size;
heap_p->pos = sizeof(*heap_p);
} else {
heap_p = NULL;
}

return (heap_p);
}

static void *heap_alloc(struct fixed32_heap_t *self_p, size_t size)
{
void *buf_p;
int left;

left = (self_p->size - self_p->pos);

if (size <= left) {
buf_p = &self_p->buf_p[self_p->pos];
self_p->pos += size;
} else {
buf_p = NULL;
}

return (buf_p);
}

static void encoder_init(struct encoder_t *self_p,
uint8_t *buf_p,
size_t size)
{
self_p->buf_p = buf_p;
self_p->size = size;
self_p->pos = (size - 1);
}

static size_t encoder_pos(struct encoder_t *self_p)
{
return (self_p->pos);
}

static int encoder_get_result(struct encoder_t *self_p)
{
int length;

length = (self_p->size - self_p->pos - 1);
memmove(self_p->buf_p,
&self_p->buf_p[self_p->pos + 1],
length);

return (length);
}

static void encoder_prepend_byte(struct encoder_t *self_p,
uint8_t value)
{
if (self_p->pos < 0) {
fprintf(stderr, "encoder_prepend_byte: %d\n", self_p->pos);
exit(1);
}

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

static void encoder_prepend_bytes(struct encoder_t *self_p,
uint8_t *buf_p,
int size)
{
int i;

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

static void encoder_prepend_fixed32(struct encoder_t *self_p,
int field_number,
uint32_t value)
{
uint8_t buf[5];

if (value > 0) {
buf[0] = tag(field_number, 5);
buf[1] = (value & 0xff);
buf[2] = ((value >> 8) & 0xff);
buf[3] = ((value >> 16) & 0xff);
buf[4] = ((value >> 24) & 0xff);
encoder_prepend_bytes(self_p, &buf[0], 5);
}
}

static void decoder_init(struct decoder_t *self_p,
const uint8_t *buf_p,
size_t size,
struct fixed32_heap_t *heap_p)
{
self_p->buf_p = buf_p;
self_p->size = size;
self_p->pos = 0;
self_p->heap_p = heap_p;
}

static int decoder_get_result(struct decoder_t *self_p)
{
int res;

if (self_p->pos == self_p->size) {
res = self_p->pos;
} else {
res = -1;
}

return (res);
}

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)
{
uint8_t value;

if (decoder_available(self_p)) {
value = self_p->buf_p[self_p->pos];
self_p->pos++;
} else {
self_p->size = -1;
value = 0;
}

return (value);
}

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

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

return (value >> 3);
}

static uint32_t decoder_read_fixed32(struct decoder_t *self_p,
int wire_type)
{
return (0);
}

struct fixed32_message_t *fixed32_message_new(
void *workspace_p,
size_t size)
{
struct fixed32_message_t *message_p;
struct fixed32_heap_t *heap_p;

heap_p = heap_new(workspace_p, size);

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

message_p = heap_alloc(heap_p, sizeof(*message_p));

if (message_p != NULL) {
message_p->heap_p = heap_p;
message_p->value = 0;
}

return (message_p);
}

void fixed32_message_encode_inner(
struct encoder_t *encoder_p,
struct fixed32_message_t *message_p)
{
encoder_prepend_fixed32(encoder_p, 1, message_p->value);
}

void fixed32_message_decode_inner(
struct decoder_t *decoder_p,
struct fixed32_message_t *message_p)
{
int wire_type;

while (decoder_available(decoder_p)) {
switch (decoder_read_tag(decoder_p, &wire_type)) {

case 1:
message_p->value = decoder_read_fixed32(decoder_p, wire_type);
break;

default:
break;
}
}
}

int fixed32_message_encode(
struct fixed32_message_t *message_p,
uint8_t *encoded_p,
size_t size)
{
struct encoder_t encoder;

encoder_init(&encoder, encoded_p, size);
fixed32_message_encode_inner(&encoder, message_p);

return (encoder_get_result(&encoder));
}

int fixed32_message_decode(
struct fixed32_message_t *message_p,
const uint8_t *encoded_p,
size_t size)
{
struct decoder_t decoder;

decoder_init(&decoder, encoded_p, size, message_p->heap_p);
fixed32_message_decode_inner(&decoder, message_p);

return (decoder_get_result(&decoder));
}
91 changes: 91 additions & 0 deletions tests/files/c_source/fixed32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2019 Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/**
* This file was generated by pbtools version 0.1.0 Thu Jan 24 08:14:06 2019.
*/

#ifndef FIXED32_H
#define FIXED32_H

#include <stdint.h>
#include <stdbool.h>

struct fixed32_heap_t {
char *buf_p;
int size;
int pos;
};

/**
* Message Message in package fixed32.
*/
struct fixed32_message_t {
struct fixed32_heap_t *heap_p;
uint32_t value;
};

/**
* Create a new message Message in given workspace.
*
* @param[in] src_p Data to decode.
* @param[in] size Size of src_p.
*
* @return Initialized address book, or NULL on failure.
*/
struct fixed32_message_t *fixed32_message_new(
void *workspace_p,
size_t size);

/**
* Encode message Message defined in package fixed32.
*
* @param[out] dst_p Buffer to encode into.
* @param[in] size Size of dst_p.
* @param[in] src_p Data to encode.
*
* @return Encoded data length or negative error code.
*/
int fixed32_message_encode(
struct fixed32_message_t *message_p,
uint8_t *encoded_p,
size_t size);

/**
* Decode message Message defined in package fixed32.
*
* @param[out] dst_p Decoded data.
* @param[in] src_p Data to decode.
* @param[in] size Size of src_p.
*
* @return Number of bytes decoded or negative error code.
*/
int fixed32_message_decode(
struct fixed32_message_t *message_p,
const uint8_t *encoded_p,
size_t size);

#endif

0 comments on commit d2ce1e0

Please sign in to comment.