Skip to content

Commit

Permalink
Merge pull request #3 from st1vms/v0.1.3
Browse files Browse the repository at this point in the history
UTF8 Strings implemented!
  • Loading branch information
st1vms authored Feb 3, 2023
2 parents 96c56a2 + 81d22da commit 301a8f6
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 21 deletions.
11 changes: 11 additions & 0 deletions include/dserial.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ extern "C"
size_t *size_off,
unsigned char *bit_off);

char SerializeUTF8String(utf8_string_t dval,
unsigned char *buffer_p,
size_t buffer_size,
size_t *size_off,
unsigned char *bit_off);

char SerializeBoolean(Boolean boolv,
unsigned char *buffer_p,
size_t buffer_size,
Expand Down Expand Up @@ -103,6 +109,11 @@ extern "C"
size_t *bit_count,
Double *out);

unsigned char *DeserializeUTF8String(unsigned char *buffer,
size_t *m_bytes,
size_t *bit_count,
utf8_string_t *out);

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 3 additions & 1 deletion include/dtypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <ctype.h>
#include <stdint.h>

#define MAX_STRING_LENGTH (0xff)

#ifdef __cplusplus
extern "C"
{
Expand Down Expand Up @@ -53,7 +55,7 @@ extern "C"
typedef struct utf8_string_t
{
size_t length;
char *utf8_string;
UInt8 utf8_string[MAX_STRING_LENGTH];
} utf8_string_t;

typedef union decimal_union_t
Expand Down
23 changes: 23 additions & 0 deletions src/dbits.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "dserial.h"
#include "dpacket.h"
Expand Down Expand Up @@ -162,6 +163,14 @@ char SerializePacket(unsigned char *buffer, size_t buffer_size, dpacket_t packet
return 0;
}
break;
case UTF8_STRING_STYPE:

if (!SerializeUTF8String(node->data.utf8_str_v, buffer, buffer_size, out_size, &bit_off))
{
*out_size = 0;
return 0;
}
break;
default:
*out_size = 0;
return 0;
Expand Down Expand Up @@ -335,6 +344,20 @@ char DeserializeBuffer(unsigned char *buffer, const size_t buffer_size, dpacket_
return 0;
}

break;
case UTF8_STRING_STYPE:

data.utf8_str_v = (utf8_string_t){
.length = 0,
.utf8_string = {0}};
memset(data.utf8_str_v.utf8_string, 0, MAX_STRING_LENGTH);

if (NULL == (buffer = DeserializeUTF8String(buffer, &n, &bit_count, &(data.utf8_str_v))) ||
!AddSerializable(packet_out, UTF8_STRING_STYPE, data))
{
FreePacket(packet_out);
return 0;
}
break;
default:
FreePacket(packet_out);
Expand Down
16 changes: 0 additions & 16 deletions src/dpacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@

static int _PACKET_TABLE[PACKET_TABLE_SIZE][MAX_PACKET_FIELDS];

static void TryDeallocateNodeString(serializable_list_node_t *node_p)
{
if (node_p != NULL)
{
if (node_p->data.utf8_str_v.utf8_string != NULL)
{
free(node_p->data.utf8_str_v.utf8_string);
}
node_p->data.utf8_str_v.length = 0;
}
}

static unsigned char IsValidList(serializable_list_t list_p)
{
return ((list_p.size > 0 && NULL == list_p.first_node) ||
Expand All @@ -33,10 +21,6 @@ void FreePacket(dpacket_t packet)
while (tmp != NULL)
{
packet->data_list.first_node = tmp->next_node;
if (tmp->stype == UTF8_STRING_STYPE)
{
TryDeallocateNodeString(tmp);
}
free(tmp);
tmp = packet->data_list.first_node;
}
Expand Down
145 changes: 145 additions & 0 deletions src/dserial.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,78 @@ char SerializeDouble(Double dval,
return 1;
}

char SerializeUTF8String(utf8_string_t dval,
unsigned char *buffer_p,
size_t buffer_size,
size_t *size_off,
unsigned char *bit_off)
{
if (NULL == buffer_p || buffer_size == 0 ||
NULL == size_off || NULL == bit_off || *bit_off > 8 ||
dval.length >= MAX_STRING_LENGTH || dval.utf8_string == NULL)
{
return 0;
}

SizeIncrementCheck(size_off, buffer_size, bit_off);

UInt8 header_size = 0;
// Serialize appropriate string length header
if (MAX_STRING_LENGTH <= UINT8_MAX)
{
header_size = GetUIntBitsize(dval.length);
if (header_size == 0 ||
!SerializeNumericalHeader(header_size, HEADER8_SIZE, buffer_p, buffer_size, size_off, bit_off))
{
return 0;
}
}
else if (MAX_STRING_LENGTH <= UINT16_MAX)
{
header_size = GetUIntBitsize(dval.length);
if (header_size == 0 ||
!SerializeNumericalHeader(header_size, HEADER16_SIZE, buffer_p, buffer_size, size_off, bit_off))
{
return 0;
}
}
else
{
return 0;
}

// Serialize string length
if (!SerializeUInt(dval.length, buffer_p, buffer_size, size_off, bit_off))
{
return 0;
}

// Serialize string bytes
UInt8 *v = dval.utf8_string;
for (size_t i = 0; v != NULL && i < dval.length; v++, i++)
{

for (size_t b = 0; b < UINT8_SIZE; b++)
{

SizeIncrementCheck(size_off, buffer_size, bit_off);

if (*v != 0)
{
if (*v & 1)
{
SetBufferBit_Serialize(&buffer_p, size_off, bit_off);
}
*v >>= 1;
}

*bit_off += 1;
}
}

return 1;
}

char SerializeBoolean(Boolean boolv,
unsigned char *buffer_p,
size_t buffer_size,
Expand Down Expand Up @@ -608,3 +680,76 @@ unsigned char *DeserializeDouble(unsigned char *buffer,

return buffer;
}

unsigned char *DeserializeUTF8String(unsigned char *buffer,
size_t *m_bytes,
size_t *bit_count,
utf8_string_t *out)
{
if (out == NULL || !DeserializeArgCheck(buffer, m_bytes, bit_count, 1))
{
return NULL;
}

ByteIncrementCheck(&buffer, bit_count, m_bytes);

// Deserialize appropriate string length
UInt16 header_value_16 = 0;
UInt8 header_value_8 = 0;

if (MAX_STRING_LENGTH <= UINT8_MAX)
{
if (!(buffer = DeserializeUInt8(buffer, m_bytes, bit_count, HEADER8_SIZE, 1, &header_value_8)) ||
!(buffer = DeserializeUInt8(buffer, m_bytes, bit_count, header_value_8, 0, &header_value_8)))
{
return NULL;
}
out->length = header_value_8;
}
else if (MAX_STRING_LENGTH <= UINT16_MAX)
{
if (!(buffer = DeserializeUInt8(buffer, m_bytes, bit_count, HEADER16_SIZE, 1, &header_value_8)) ||
!(buffer = DeserializeUInt16(buffer, m_bytes, bit_count, header_value_8, &header_value_16)))
{
return NULL;
}
out->length = header_value_8;
}
else
{
return NULL;
}

if (out->length >= MAX_STRING_LENGTH)
{
return NULL;
}

// Deserialize string bytes
UInt8 v = 0;
for (size_t i = 0; i < out->length; i++)
{

for (size_t b = 0; b < UINT8_SIZE; b++)
{
if (NULL == buffer || bit_count == NULL || out == NULL)
{
return NULL;
}

ByteIncrementCheck(&buffer, bit_count, m_bytes);

if (*buffer & __BIT_MASKS[*bit_count])
{
v += (1U << b);
}

*bit_count += 1;
}

out->utf8_string[i] = v;
v = 0;
}

return buffer;
}
15 changes: 11 additions & 4 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <float.h>
#include "dbits.h"

#define SIZE_TEST 19
#define SIZE_TEST 20

static int packet_format[SIZE_TEST] = {
BOOLEAN_STYPE,
Expand All @@ -25,7 +25,7 @@ static int packet_format[SIZE_TEST] = {
INT32_STYPE,
INT64_STYPE,
INT64_STYPE,
};
UTF8_STRING_STYPE};

int main()
{
Expand Down Expand Up @@ -116,7 +116,7 @@ int main()
FreePacket(&packet);
return -1;
}
if (!AddSerializable(&packet, INT32_STYPE, (data_union_t){.decimal_v.i32_v = INT32_MAX}))
if (!AddSerializable(&packet, INT32_STYPE, (data_union_t){.decimal_v.i32_v = INT32_MAX}))
{
FreePacket(&packet);
return -1;
Expand All @@ -136,7 +136,11 @@ int main()
FreePacket(&packet);
return -1;
}

if (!AddSerializable(&packet, UTF8_STRING_STYPE, (data_union_t){.utf8_str_v.utf8_string = "I do desire we may be better strangers", .utf8_str_v.length = strlen("I do desire we may be better strangers")}))
{
FreePacket(&packet);
return -1;
}

size_t buffer_size = 0;
unsigned char buffer[SIZE_TEST * 100] = {0};
Expand Down Expand Up @@ -199,6 +203,9 @@ int main()
case DOUBLE_STYPE:
printf("\nD = %lf\n", tmp->data.double_v);
break;
case UTF8_STRING_STYPE:
printf("\nUTF8 (length = %lu) = %s\n", tmp->data.utf8_str_v.length, tmp->data.utf8_str_v.utf8_string);
break;
default:
break;
}
Expand Down

0 comments on commit 301a8f6

Please sign in to comment.