add tk_size_of_basic_type and refactor

This commit is contained in:
lixianjing 2024-03-12 16:29:16 +08:00
parent 34dd3ce8b4
commit c41b764899
3 changed files with 88 additions and 172 deletions

View File

@ -2,7 +2,7 @@
2024/03/12 2024/03/12
* 完善函数 tk\_buffer\_set\_value/tk\_buffer\_get\_value * 完善函数 tk\_buffer\_set\_value/tk\_buffer\_get\_value
* 导出tk\_skip\_to\_offset * 增加函数 tk\_size\_of\_basic\_type
2024/03/10 2024/03/10
* 增加函数tk\_basic\_type\_from\_name * 增加函数tk\_basic\_type\_from\_name

View File

@ -2224,144 +2224,103 @@ ret_t tk_bits_data_to_bytes_data(uint8_t* bits, uint32_t bits_size, uint8_t* byt
return RET_OK; return RET_OK;
} }
uint8_t* tk_skip_to_offset(uint8_t* data, uint32_t size, value_type_t type, int16_t index) { uint32_t tk_size_of_basic_type(value_type_t type) {
return_value_if_fail(data != NULL, NULL);
switch (type) { switch (type) {
case VALUE_TYPE_INT8: case VALUE_TYPE_INT8:
case VALUE_TYPE_UINT8: case VALUE_TYPE_UINT8:
case VALUE_TYPE_BOOL: { case VALUE_TYPE_BOOL:
return_value_if_fail((index + 1) <= size, NULL); return 1;
return data + index;
}
case VALUE_TYPE_INT16: case VALUE_TYPE_INT16:
case VALUE_TYPE_UINT16: { case VALUE_TYPE_UINT16:
return_value_if_fail((index + 1) * 2 <= size, NULL); return 2;
return data + index * 2;
}
case VALUE_TYPE_INT32: case VALUE_TYPE_INT32:
case VALUE_TYPE_UINT32: { case VALUE_TYPE_UINT32:
return_value_if_fail((index + 1) * 4 <= size, NULL); case VALUE_TYPE_FLOAT32:
return data + index * 4; return 4;
}
case VALUE_TYPE_INT64: case VALUE_TYPE_INT64:
case VALUE_TYPE_UINT64: { case VALUE_TYPE_UINT64:
return_value_if_fail((index + 1) * 8 <= size, NULL); case VALUE_TYPE_DOUBLE:
return data + index * 8; return 8;
}
case VALUE_TYPE_FLOAT32: {
return_value_if_fail((index + 1) * 4 <= size, NULL);
return data + index * 4;
}
case VALUE_TYPE_DOUBLE: {
return_value_if_fail((index + 1) * 8 <= size, NULL);
return data + index * 8;
}
default: default:
break; break;
} }
return NULL; return 0;
}
uint8_t* tk_skip_to_offset(uint8_t* data, uint32_t size, value_type_t type, int16_t index) {
uint32_t type_size = tk_size_of_basic_type(type);
uint32_t offset = index * type_size;
return_value_if_fail(data != NULL && type_size > 0, NULL);
return_value_if_fail((offset + type_size) <= size, RET_BAD_PARAMS);
return data + offset;
} }
ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int16_t offset, ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int16_t offset,
int16_t bit_offset, const value_t* value) { int16_t bit_offset, const value_t* value) {
uint32_t type_size = tk_size_of_basic_type(type);
uint8_t* data = tk_skip_to_offset(buffer, size, type, offset); uint8_t* data = tk_skip_to_offset(buffer, size, type, offset);
return_value_if_fail(data != NULL, RET_BAD_PARAMS); return_value_if_fail(data != NULL, RET_BAD_PARAMS);
if (bit_offset >= 0) {
return_value_if_fail(bit_offset < type_size * 8, RET_BAD_PARAMS);
return bits_stream_set(data, type_size, bit_offset, value_bool(value));
}
switch (type) { switch (type) {
case VALUE_TYPE_INT8: { case VALUE_TYPE_INT8: {
if (bit_offset < 0) {
*data = value_int8(value); *data = value_int8(value);
} else {
return_value_if_fail(bit_offset < 8, RET_BAD_PARAMS);
bits_stream_set(data, 1, bit_offset, value_int8(value) != 0);
}
break; break;
} }
case VALUE_TYPE_UINT8: { case VALUE_TYPE_UINT8: {
if (bit_offset < 0) {
*data = value_uint8(value); *data = value_uint8(value);
} else {
return_value_if_fail(bit_offset < 8, RET_BAD_PARAMS);
bits_stream_set(data, 1, bit_offset, value_uint8(value) != 0);
}
break; break;
} }
case VALUE_TYPE_INT16: { case VALUE_TYPE_INT16: {
if (bit_offset < 0) {
int16_t v = value_int16(value); int16_t v = value_int16(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_set(data, 2, bit_offset, value_int16(value) != 0);
}
break; break;
} }
case VALUE_TYPE_UINT16: { case VALUE_TYPE_UINT16: {
if (bit_offset < 0) {
uint16_t v = value_uint16(value); uint16_t v = value_uint16(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_set(data, 2, bit_offset, value_uint16(value) != 0);
}
break; break;
} }
case VALUE_TYPE_INT32: { case VALUE_TYPE_INT32: {
if (bit_offset < 0) {
int32_t v = value_int32(value); int32_t v = value_int32(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_set(data, 4, bit_offset, value_int32(value) != 0);
}
break; break;
} }
case VALUE_TYPE_UINT32: { case VALUE_TYPE_UINT32: {
if (bit_offset < 0) {
uint32_t v = value_uint32(value); uint32_t v = value_uint32(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_set(data, 4, bit_offset, value_uint32(value) != 0);
}
break; break;
} }
case VALUE_TYPE_INT64: { case VALUE_TYPE_INT64: {
if (bit_offset < 0) {
int64_t v = value_int64(value); int64_t v = value_int64(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_set(data, 8, bit_offset, value_int64(value) != 0);
}
break; break;
} }
case VALUE_TYPE_UINT64: { case VALUE_TYPE_UINT64: {
if (bit_offset < 0) {
uint64_t v = value_uint64(value); uint64_t v = value_uint64(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_set(data, 8, bit_offset, value_uint64(value) != 0);
}
break; break;
} }
case VALUE_TYPE_FLOAT32: { case VALUE_TYPE_FLOAT32: {
assert(bit_offset < 0);
float v = value_float32(value); float v = value_float32(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
break; break;
} }
case VALUE_TYPE_DOUBLE: { case VALUE_TYPE_DOUBLE: {
assert(bit_offset < 0);
double v = value_double(value); double v = value_double(value);
memcpy(data, &v, sizeof(v)); memcpy(data, &v, sizeof(v));
break; break;
} }
default: default: {
break; log_debug("tk_buffer_set_value: not support %d\n", type);
return RET_NOT_IMPL;
}
} }
return RET_OK; return RET_OK;
@ -2370,119 +2329,80 @@ ret_t tk_buffer_set_value(uint8_t* buffer, uint32_t size, value_type_t type, int
ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int16_t offset, ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int16_t offset,
int16_t bit_offset, value_t* value) { int16_t bit_offset, value_t* value) {
bool_t v = FALSE; bool_t v = FALSE;
uint32_t type_size = tk_size_of_basic_type(type);
uint8_t* data = tk_skip_to_offset(buffer, size, type, offset); uint8_t* data = tk_skip_to_offset(buffer, size, type, offset);
return_value_if_fail(data != NULL, RET_BAD_PARAMS); return_value_if_fail(data != NULL, RET_BAD_PARAMS);
return_value_if_fail(type_size > 0, RET_BAD_PARAMS);
if (bit_offset >= 0) {
return_value_if_fail(bit_offset < type_size * 8, RET_BAD_PARAMS);
bits_stream_get(data, type_size, bit_offset, &v);
value_set_bool(value, v);
return RET_OK;
}
switch (type) { switch (type) {
case VALUE_TYPE_INT8: { case VALUE_TYPE_INT8: {
if (bit_offset < 0) {
value_set_int8(value, *data); value_set_int8(value, *data);
} else {
return_value_if_fail(bit_offset < 8, RET_BAD_PARAMS);
bits_stream_get(data, 1, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_UINT8: { case VALUE_TYPE_UINT8: {
if (bit_offset < 0) {
value_set_uint8(value, *data); value_set_uint8(value, *data);
} else {
return_value_if_fail(bit_offset < 8, RET_BAD_PARAMS);
bits_stream_get(data, 1, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_INT16: { case VALUE_TYPE_INT16: {
if (bit_offset < 0) {
int16_t v = 0; int16_t v = 0;
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_int16(value, v); value_set_int16(value, v);
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_get(data, 2, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_UINT16: { case VALUE_TYPE_UINT16: {
if (bit_offset < 0) {
value_set_uint16(value, *(uint16_t*)data);
uint16_t v = 0; uint16_t v = 0;
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_uint16(value, v); value_set_uint16(value, v);
} else {
return_value_if_fail(bit_offset < 16, RET_BAD_PARAMS);
bits_stream_get(data, 2, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_INT32: { case VALUE_TYPE_INT32: {
if (bit_offset < 0) {
int32_t v = 0; int32_t v = 0;
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_int32(value, v); value_set_int32(value, v);
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_get(data, 4, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_UINT32: { case VALUE_TYPE_UINT32: {
if (bit_offset < 0) {
uint32_t v = 0; uint32_t v = 0;
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_uint32(value, v); value_set_uint32(value, v);
} else {
return_value_if_fail(bit_offset < 32, RET_BAD_PARAMS);
bits_stream_get(data, 4, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_INT64: { case VALUE_TYPE_INT64: {
if (bit_offset < 0) {
int64_t v = 0; int64_t v = 0;
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_int64(value, v); value_set_int64(value, v);
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_get(data, 8, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_UINT64: { case VALUE_TYPE_UINT64: {
if (bit_offset < 0) {
uint64_t v = 0; uint64_t v = 0;
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_uint64(value, v); value_set_uint64(value, v);
} else {
return_value_if_fail(bit_offset < 64, RET_BAD_PARAMS);
bits_stream_get(data, 8, bit_offset, &v);
value_set_bool(value, v);
}
break; break;
} }
case VALUE_TYPE_FLOAT32: { case VALUE_TYPE_FLOAT32: {
float v = 0; float v = 0;
assert(bit_offset < 0);
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_float32(value, v); value_set_float32(value, v);
break; break;
} }
case VALUE_TYPE_DOUBLE: { case VALUE_TYPE_DOUBLE: {
double v = 0; double v = 0;
assert(bit_offset < 0);
memcpy(&v, data, sizeof(v)); memcpy(&v, data, sizeof(v));
value_set_double(value, v); value_set_double(value, v);
break; break;
} }
default: default: {
break; log_debug("tk_buffer_get_value: not support %d\n", type);
return RET_NOT_IMPL;
}
} }
return RET_OK; return RET_OK;

View File

@ -1209,16 +1209,12 @@ ret_t tk_buffer_get_value(uint8_t* buffer, uint32_t size, value_type_t type, int
int16_t bit_offset, value_t* value); int16_t bit_offset, value_t* value);
/** /**
* @method tk_skip_to_offset * @method tk_size_of_basic_type
* * ()
* @param {uint8_t*} data
* @param {uint32_t} size
* @param {value_type_t} type * @param {value_type_t} type
* @param {int16_t} index * @return {uint32_t}
*
* @return {uint8_t*}
*/ */
uint8_t* tk_skip_to_offset(uint8_t* data, uint32_t size, value_type_t type, int16_t index); uint32_t tk_size_of_basic_type(value_type_t type);
/** /**
* @method tk_basic_type_from_name * @method tk_basic_type_from_name