Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions c/tlv_box.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <string.h>
#include "tlv_box.h"

int tlv_box_putobject(tlv_box_t *box, int type, void *value, int length);

static void tlv_box_release_tlv(value_t value)
{
tlv_t *tlv = (tlv_t *)value.value;
Expand All @@ -37,7 +39,7 @@ tlv_box_t *tlv_box_parse(unsigned char *buffer, int buffersize)
unsigned char *cached = (unsigned char*) malloc(buffersize);
memcpy(cached, buffer, buffersize);

int offset = 0, length = 0;
int offset = 0;
while (offset < buffersize) {
int type = (*(int *)(cached + offset));
offset += sizeof(int);
Expand Down Expand Up @@ -103,21 +105,41 @@ int tlv_box_put_char(tlv_box_t *box, int type, char value)
return tlv_box_putobject(box, type, &value, sizeof(char));
}

int tlv_box_put_uchar(tlv_box_t *box, int type, unsigned char value)
{
return tlv_box_putobject(box, type, &value, sizeof(unsigned char));
}

int tlv_box_put_short(tlv_box_t *box, int type, short value)
{
return tlv_box_putobject(box, type, &value, sizeof(short));
}

int tlv_box_put_ushort(tlv_box_t *box, int type, unsigned short value)
{
return tlv_box_putobject(box, type, &value, sizeof(unsigned short));
}

int tlv_box_put_int(tlv_box_t *box, int type, int value)
{
return tlv_box_putobject(box, type, &value, sizeof(int));
}

int tlv_box_put_uint(tlv_box_t *box, int type, unsigned int value)
{
return tlv_box_putobject(box, type, &value, sizeof(unsigned int));
}

int tlv_box_put_long(tlv_box_t *box, int type, long value)
{
return tlv_box_putobject(box, type, &value, sizeof(long));
}

int tlv_box_put_ulong(tlv_box_t *box, int type, unsigned long value)
{
return tlv_box_putobject(box, type, &value, sizeof(unsigned long));
}

int tlv_box_put_longlong(tlv_box_t *box, int type, long long value)
{
return tlv_box_putobject(box, type, &value, sizeof(long long));
Expand Down Expand Up @@ -182,6 +204,17 @@ int tlv_box_get_char(tlv_box_t *box, int type, char *value)
return 0;
}

int tlv_box_get_uchar(tlv_box_t *box, int type, unsigned char *value)
{
value_t object;
if (key_list_get(box->m_list, type, &object) != 0) {
return -1;
}
tlv_t *tlv = (tlv_t *) object.value;
*value = (*(unsigned char *)(tlv->value));
return 0;
}

int tlv_box_get_short(tlv_box_t *box, int type, short *value)
{
value_t object;
Expand All @@ -193,6 +226,17 @@ int tlv_box_get_short(tlv_box_t *box, int type, short *value)
return 0;
}

int tlv_box_get_ushort(tlv_box_t *box, int type, unsigned short *value)
{
value_t object;
if (key_list_get(box->m_list, type, &object) != 0) {
return -1;
}
tlv_t *tlv = (tlv_t *) object.value;
*value = (*(unsigned short *)(tlv->value));
return 0;
}

int tlv_box_get_int(tlv_box_t *box, int type, int *value)
{
value_t object;
Expand All @@ -204,6 +248,17 @@ int tlv_box_get_int(tlv_box_t *box, int type, int *value)
return 0;
}

int tlv_box_get_uint(tlv_box_t *box, int type, unsigned int *value)
{
value_t object;
if (key_list_get(box->m_list, type, &object) != 0) {
return -1;
}
tlv_t *tlv = (tlv_t *) object.value;
*value = (*(unsigned int *)(tlv->value));
return 0;
}

int tlv_box_get_long(tlv_box_t *box, int type, long *value)
{
value_t object;
Expand All @@ -215,6 +270,17 @@ int tlv_box_get_long(tlv_box_t *box, int type, long *value)
return 0;
}

int tlv_box_get_ulong(tlv_box_t *box, int type, unsigned long *value)
{
value_t object;
if (key_list_get(box->m_list, type, &object) != 0) {
return -1;
}
tlv_t *tlv = (tlv_t *) object.value;
*value = (*(unsigned long *)(tlv->value));
return 0;
}

int tlv_box_get_longlong(tlv_box_t *box, int type, long long *value)
{
value_t object;
Expand Down Expand Up @@ -250,7 +316,7 @@ int tlv_box_get_double(tlv_box_t *box, int type, double *value)

int tlv_box_get_string(tlv_box_t *box, int type, char *value, int* length)
{
return tlv_box_get_bytes(box,type,value,length);
return tlv_box_get_bytes(box,type,(unsigned char *)value,length);
}

int tlv_box_get_bytes(tlv_box_t *box, int type, unsigned char *value, int* length)
Expand Down Expand Up @@ -290,4 +356,5 @@ int tlv_box_get_object(tlv_box_t *box, int type, tlv_box_t **object)
tlv_t *tlv = (tlv_t *) value.value;
*object = (tlv_box_t *)tlv_box_parse(tlv->value, tlv->length);
return 0;
}
}

8 changes: 8 additions & 0 deletions c/tlv_box.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ unsigned char *tlv_box_get_buffer(tlv_box_t *box);
int tlv_box_get_size(tlv_box_t *box);

int tlv_box_put_char(tlv_box_t *box,int type,char value);
int tlv_box_put_uchar(tlv_box_t *box,int type,unsigned char value);
int tlv_box_put_short(tlv_box_t *box,int type,short value);
int tlv_box_put_ushort(tlv_box_t *box,int type,unsigned short value);
int tlv_box_put_int(tlv_box_t *box,int type,int value);
int tlv_box_put_uint(tlv_box_t *box,int type,unsigned int value);
int tlv_box_put_long(tlv_box_t *box,int type,long value);
int tlv_box_put_ulong(tlv_box_t *box,int type,unsigned long value);
int tlv_box_put_longlong(tlv_box_t *box,int type,long long value);
int tlv_box_put_float(tlv_box_t *box,int type,float value);
int tlv_box_put_double(tlv_box_t *box,int type,double value);
Expand All @@ -47,9 +51,13 @@ int tlv_box_put_object(tlv_box_t *box,int type,tlv_box_t *object);
int tlv_box_serialize(tlv_box_t *box);

int tlv_box_get_char(tlv_box_t *box,int type,char *value);
int tlv_box_get_uchar(tlv_box_t *box,int type,unsigned char *value);
int tlv_box_get_short(tlv_box_t *box,int type,short *value);
int tlv_box_get_ushort(tlv_box_t *box,int type,unsigned short *value);
int tlv_box_get_int(tlv_box_t *box,int type,int *value);
int tlv_box_get_uint(tlv_box_t *box,int type,unsigned int *value);
int tlv_box_get_long(tlv_box_t *box,int type,long *value);
int tlv_box_get_ulong(tlv_box_t *box,int type,unsigned long *value);
int tlv_box_get_longlong(tlv_box_t *box,int type,long long *value);
int tlv_box_get_float(tlv_box_t *box,int type,float *value);
int tlv_box_get_double(tlv_box_t *box,int type,double *value);
Expand Down