Code cleanup

This commit is contained in:
Pierre Pronchery 2014-03-27 00:20:00 +01:00
parent ce0ae5d065
commit d4c9b21700

View File

@ -50,6 +50,15 @@ struct _Variable
}; };
/* constants */
#define VT_LAST VT_STRING
#define VT_COUNT (VT_LAST + 1)
static const size_t _variable_sizes[VT_COUNT] = { 0, 1,
sizeof(int8_t), sizeof(uint8_t), sizeof(int16_t), sizeof(uint16_t),
sizeof(int32_t), sizeof(uint32_t), sizeof(int64_t), sizeof(uint64_t),
0, 0, sizeof(uint32_t), 0 };
/* public */ /* public */
/* variable_new */ /* variable_new */
Variable * variable_new(VariableType type, void * value) Variable * variable_new(VariableType type, void * value)
@ -214,12 +223,13 @@ Variable * variable_new_deserialize(size_t * size, char const * data)
uint8_t u8; uint8_t u8;
size_t s; size_t s;
/* obtain the type from the data */ /* check the arguments */
if(*size < sizeof(u8)) if(size == NULL || *size < sizeof(u8) || data == NULL)
{ {
*size = 1; error_set_code(-EINVAL, "%s", strerror(EINVAL));
return NULL; return NULL;
} }
/* obtain the type from the data */
u8 = data[0]; u8 = data[0];
s = *size - sizeof(u8); s = *size - sizeof(u8);
/* deserialize according to the type */ /* deserialize according to the type */
@ -258,36 +268,31 @@ Variable * variable_new_deserialize_type(VariableType type, size_t * size,
(void *)data); (void *)data);
#endif #endif
/* estimate the size required */ /* estimate the size required */
s = (type < sizeof(_variable_sizes) / sizeof(*_variable_sizes))
? _variable_sizes[type] : 0;
switch(type) switch(type)
{ {
case VT_NULL: case VT_NULL:
s = 0;
break; break;
case VT_BOOL: case VT_BOOL:
s = sizeof(uint8_t);
p = (char *)&u8; p = (char *)&u8;
break; break;
case VT_INT8: case VT_INT8:
case VT_UINT8: case VT_UINT8:
s = sizeof(int8_t);
break; break;
case VT_INT16: case VT_INT16:
case VT_UINT16: case VT_UINT16:
s = sizeof(int16_t);
p = (char *)&i16; p = (char *)&i16;
break; break;
case VT_INT32: case VT_INT32:
case VT_UINT32: case VT_UINT32:
s = sizeof(int32_t);
p = (char *)&i32; p = (char *)&i32;
break; break;
case VT_INT64: case VT_INT64:
case VT_UINT64: case VT_UINT64:
s = sizeof(int64_t);
p = (char *)&i64; p = (char *)&i64;
break; break;
case VT_BUFFER: case VT_BUFFER:
s = sizeof(uint32_t);
if(*size < s) if(*size < s)
break; break;
memcpy(&u32, data, s); memcpy(&u32, data, s);
@ -302,7 +307,7 @@ Variable * variable_new_deserialize_type(VariableType type, size_t * size,
continue; continue;
else if((p = malloc(++s)) == NULL) else if((p = malloc(++s)) == NULL)
{ {
error_set_code(1, "%s", error_set_code(-errno, "%s",
strerror(errno)); strerror(errno));
return NULL; return NULL;
} }