Skip to content

Commit

Permalink
Misc. bug fixes and improvements + Unsigned Integers implemented!
Browse files Browse the repository at this point in the history
  • Loading branch information
st1vms committed Feb 1, 2023
1 parent 07334ed commit e1cffab
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 93 deletions.
30 changes: 14 additions & 16 deletions include/dbits.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ extern "C"
#endif

/**
* @brief Serialize `packet` into a byte buffer,
* storing into `out_size` the total size of the buffer
* @param packet Packet pointer allocated with ``NewPacket(id)`,
* also having one or more serializables assigned,
* and also registered using `RegisterPacket(id, format)`.
* @param out_size Output reference to store the buffer size
* @return unsigned char * e.g. Serialized byte buffer,
* call free() on this reference after using it.
* @brief Serialize a Packet structure into a byte buffer,
* using no more than `buffer_size` bytes,
* storing the output size into `out_size`
*
* @param buffer Output byte buffer
* @param buffer_size Output byte buffer size
* @param packet Packet structure to serialize
* @param out_size Output size pointer, filled with serialized buffer size
* @return 1 on success, 0 in case of errors.
*/
extern char SerializePacket(unsigned char *buffer, size_t buffer_size, dpacket_t packet, size_t *out_size);

/**
* @brief Deserialize buffer,
* storing the packet structure into an output reference,
* reading no more than `buffer_size` bytes,
* up to `n_bytes`.
* @brief Deserialize a byte buffer into a packet structure reference,
* reading no more than `buffer_size` bytes.
*
* @param buffer A constant unsigned char pointer, e.g the byte buffer
* @param buffer_size Constant size of the buffer
* @param n_bytes Number of bytes to read
* @param packet_out dpacket_t destination pointer reference, NULL initialized
* @param buffer Byte buffer to deserialize from
* @param buffer_size Byte buffer size
* @param packet_out Output packet structure pointer
* @return 1 on success, 0 in case of errors.
*/
extern char DeserializeBuffer(unsigned char *buffer, const size_t buffer_size, dpacket_t packet_out);
Expand Down
50 changes: 47 additions & 3 deletions include/dpacket.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef __DPACKET_H

// Max number of packets
#define PACKET_TABLE_SIZE 1
#define MAX_PACKET_FIELDS 1000

// Max number of fields x packet
#define MAX_PACKET_FIELDS 10

#include "dtypes.h"

Expand All @@ -27,19 +30,60 @@ extern "C"
typedef struct dpacket_struct_t
{
packet_id_t packet_id;
serializable_list_t data_deque;
serializable_list_t data_list;
} dpacket_struct_t;

typedef dpacket_struct_t *dpacket_t;

/**
* @brief Initialize a packet reference,
* the output reference must be freed using FreePacket() when it's no more needed.
* @param packet_p Output packet structure pointer
* @param packet_id Packet ID
* @return 1 in case a reference was stored, 0 on errors.
*/
char NewPacket(dpacket_t packet_p, packet_id_t packet_id);

/**
* @brief Register a packet along with fields and ID,
* this function is needed for DeserializeBuffer()
* in order to look for the corresponding packet id when deserializing a packet.
* @param packet_id Packet ID, unique for this packet, must not be greater than PACKET_TABLE_SIZE.
* @param packet_format Format int array, sequentially storing the packet's fields types.
* @param format_size Format array size, must not be greater than MAX_PACKET_FIELDS.
* @return 1 on success, 0 in case of errors.
*/
char RegisterPacket(packet_id_t packet_id, int *packet_format, size_t format_size);

/**
* @brief Get the packet format array from a packet_id,
* storing the array size in `out_size`
*
* @param packet_id The packet id to look for in the packet table.
* @param out_size Output format array size.
* @return int array pointer on success.
* In case of errors, NULL is returned and `out_size` will get the value of 0
*/
int *GetPacketFormat(packet_id_t packet_id, size_t *out_size);

void FreePacket(dpacket_t deque_p);
/**
* @brief This function will attempt to free the serializable list stored in
* this packet reference.
* After calling this function, the new packet will contain 0 serializables,
* but it can still be adoperated.
*
* @param list_p Packet structure pointer
*/
void FreePacket(dpacket_t list_p);

/**
* @brief Add a Serializable to the end of the packet serializable list
*
* @param dpacket_p Packet structure pointer
* @param stype Serializable type enum
* @param datav Data variable union
* @return 1 on success, 0 in case of errors
*/
char AddSerializable(dpacket_t dpacket_p, serializable_type_t stype, data_union_t datav);

#ifdef __cplusplus
Expand Down
19 changes: 18 additions & 1 deletion include/dserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extern "C"
{
#endif

char SerializeNumericalHeader(UInt8 header_value,
data_header_size_t header_size,
unsigned char *buffer_p,
Expand All @@ -27,6 +27,23 @@ extern "C"
unsigned char is_header,
UInt8 *out);

unsigned char *DeserializeUInt16(unsigned char *buffer,
size_t *m_bytes,
size_t *bit_count,
unsigned_int_size_t uint_size,
UInt16 *out);

unsigned char *DeserializeUInt32(unsigned char *buffer,
size_t *m_bytes,
size_t *bit_count,
unsigned_int_size_t uint_size,
UInt32 *out);

unsigned char *DeserializeUInt64(unsigned char *buffer,
size_t *m_bytes,
size_t *bit_count,
unsigned_int_size_t uint_size,
UInt64 *out);
#ifdef __cplusplus
}
#endif
Expand Down
19 changes: 10 additions & 9 deletions include/dtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,19 @@ extern "C"
HEADER64_SIZE = 0x6
} data_header_size_t;

typedef signed char Int8;
typedef signed short int Int16;
typedef signed int Int32;
typedef signed long long int Int64;
typedef uint8_t Boolean;

typedef unsigned char UInt8;
typedef unsigned short int UInt16;
typedef unsigned int UInt32;
typedef unsigned long long int UInt64;
typedef int8_t Int8;
typedef int16_t Int16;
typedef int32_t Int32;
typedef int64_t Int64;

typedef uint8_t UInt8;
typedef uint16_t UInt16;
typedef uint32_t UInt32;
typedef uint64_t UInt64;

typedef double Double;
typedef UInt8 Boolean;

typedef struct utf8_string_t
{
Expand Down
111 changes: 98 additions & 13 deletions src/dbits.c
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
#include <math.h>
#include <stdlib.h>
#include "dserial.h"
#include "dpacket.h"
#include "dbits.h"

static unsigned_int_size_t GetUIntBitsize(UInt8 v, data_header_size_t header_size)
static unsigned char GetUIntBitsize(UInt64 v)
{
unsigned int bitsize = (unsigned int)floor(log2(v)) + 1;
if (bitsize > (unsigned int)exp2(header_size))
{
return 0;
if(v == 0){return 1;}

unsigned char bitsize = 0;
while(v != 0){
bitsize++;
v >>= 1;
}
return bitsize;
}

char SerializePacket(unsigned char *buffer, size_t buffer_size, dpacket_t packet, size_t *out_size)
{
if (buffer == NULL || out_size == NULL || packet == NULL || packet->data_deque.size == 0)
if (buffer == NULL || out_size == NULL || packet == NULL || packet->data_list.size == 0)
{
return 0;
}

*out_size = 1;
*out_size = 0;

unsigned char bit_off = 0;
unsigned int header_bitsize = 0;
UInt8 header_bitsize = 0;

// Serialize packet_id header
if (!(header_bitsize = GetUIntBitsize(packet->packet_id, HEADER8_SIZE)))
if (!(header_bitsize = GetUIntBitsize(packet->packet_id)))
{
*out_size = 0;
return 0;
Expand All @@ -40,14 +43,14 @@ char SerializePacket(unsigned char *buffer, size_t buffer_size, dpacket_t packet
return 0;
}

serializable_list_node_t *node = packet->data_deque.first_node;
for (size_t i = 0; node != NULL && i < packet->data_deque.size; node = node->next_node, i++)
serializable_list_node_t *node = packet->data_list.first_node;
for (size_t i = 0; node != NULL && i < packet->data_list.size; node = node->next_node, i++)
{
switch (node->stype)
{
case UINT8_STYPE:
// Serialize UINT8 header
if (!(header_bitsize = GetUIntBitsize(node->data.numerical_v.u8_v, HEADER8_SIZE)))
if (!(header_bitsize = GetUIntBitsize(node->data.numerical_v.u8_v)))
{
*out_size = 0;
return 0;
Expand All @@ -60,12 +63,58 @@ char SerializePacket(unsigned char *buffer, size_t buffer_size, dpacket_t packet
return 0;
}
break;
case UINT16_STYPE:
// Serialize UINT8 header
if (!(header_bitsize = GetUIntBitsize(node->data.numerical_v.u16_v)))
{
*out_size = 0;
return 0;
}
// Serialize UINT8
if (!SerializeNumericalHeader(header_bitsize, HEADER16_SIZE, buffer, buffer_size, out_size, &bit_off) ||
!SerializeUInt(node->data.numerical_v.u16_v, buffer, buffer_size, out_size, &bit_off))
{
*out_size = 0;
return 0;
}
break;
case UINT32_STYPE:
// Serialize UINT8 header
if (!(header_bitsize = GetUIntBitsize(node->data.numerical_v.u32_v)))
{
*out_size = 0;
return 0;
}
// Serialize UINT8
if (!SerializeNumericalHeader(header_bitsize, HEADER32_SIZE, buffer, buffer_size, out_size, &bit_off) ||
!SerializeUInt(node->data.numerical_v.u32_v, buffer, buffer_size, out_size, &bit_off))
{
*out_size = 0;
return 0;
}
break;
case UINT64_STYPE:
// Serialize UINT8 header
if (!(header_bitsize = GetUIntBitsize(node->data.numerical_v.u64_v)))
{
*out_size = 0;
return 0;
}
// Serialize UINT8
if (!SerializeNumericalHeader(header_bitsize, HEADER64_SIZE, buffer, buffer_size, out_size, &bit_off) ||
!SerializeUInt(node->data.numerical_v.u64_v, buffer, buffer_size, out_size, &bit_off))
{
*out_size = 0;
return 0;
}
break;
default:
*out_size = 0;
return 0;
}
}

*out_size += 1;
return 1;
}

Expand Down Expand Up @@ -105,7 +154,7 @@ char DeserializeBuffer(unsigned char *buffer, const size_t buffer_size, dpacket_

data_union_t data;

for (; packet_format != NULL && format_size > 0; format_size--)
for (; packet_format != NULL && format_size > 0; format_size--, packet_format++)
{

if (*packet_format == 0)
Expand All @@ -127,6 +176,42 @@ char DeserializeBuffer(unsigned char *buffer, const size_t buffer_size, dpacket_
return 0;
}

break;
case UINT16_STYPE:

data.numerical_v.u16_v = 0;
if (NULL == (buffer = DeserializeUInt8(buffer, &n, &bit_count, HEADER16_SIZE, 1, &header_size)) ||
NULL == (buffer = DeserializeUInt16(buffer, &n, &bit_count, header_size, &(data.numerical_v.u16_v))) ||
!AddSerializable(packet_out, UINT16_STYPE, data))
{
FreePacket(packet_out);
return 0;
}

break;
case UINT32_STYPE:

data.numerical_v.u32_v = 0;
if (NULL == (buffer = DeserializeUInt8(buffer, &n, &bit_count, HEADER32_SIZE, 1, &header_size)) ||
NULL == (buffer = DeserializeUInt32(buffer, &n, &bit_count, header_size, &(data.numerical_v.u32_v))) ||
!AddSerializable(packet_out, UINT32_STYPE, data))
{
FreePacket(packet_out);
return 0;
}

break;
case UINT64_STYPE:

data.numerical_v.u64_v = 0;
if (NULL == (buffer = DeserializeUInt8(buffer, &n, &bit_count, HEADER64_SIZE, 1, &header_size)) ||
NULL == (buffer = DeserializeUInt64(buffer, &n, &bit_count, header_size, &(data.numerical_v.u64_v))) ||
!AddSerializable(packet_out, UINT64_STYPE, data))
{
FreePacket(packet_out);
return 0;
}

break;
default:
FreePacket(packet_out);
Expand Down
Loading

0 comments on commit e1cffab

Please sign in to comment.