coding
This commit is contained in:
parent
1f83138df2
commit
3136a23b8f
|
@ -15,12 +15,12 @@
|
||||||
#ifndef _TD_UTIL_CODING_H
|
#ifndef _TD_UTIL_CODING_H
|
||||||
#define _TD_UTIL_CODING_H
|
#define _TD_UTIL_CODING_H
|
||||||
|
|
||||||
|
#include "os.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "os.h"
|
|
||||||
|
|
||||||
#define ENCODE_LIMIT (((uint8_t)1) << 7)
|
#define ENCODE_LIMIT (((uint8_t)1) << 7)
|
||||||
#define ZIGZAGE(T, v) ((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1) // zigzag encode
|
#define ZIGZAGE(T, v) ((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1) // zigzag encode
|
||||||
#define ZIGZAGD(T, v) ((v) >> 1) ^ -((T)((v)&1)) // zigzag decode
|
#define ZIGZAGD(T, v) ((v) >> 1) ^ -((T)((v)&1)) // zigzag decode
|
||||||
|
@ -28,13 +28,13 @@ extern "C" {
|
||||||
/* ------------------------ LEGACY CODES ------------------------ */
|
/* ------------------------ LEGACY CODES ------------------------ */
|
||||||
#if 1
|
#if 1
|
||||||
// ---- Fixed U8
|
// ---- Fixed U8
|
||||||
static FORCE_INLINE int taosEncodeFixedU8(void **buf, uint8_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedU8(void **buf, uint8_t value) {
|
||||||
if (buf != NULL) {
|
if (buf != NULL) {
|
||||||
((uint8_t *)(*buf))[0] = value;
|
((uint8_t *)(*buf))[0] = value;
|
||||||
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int)sizeof(value);
|
return (int32_t)sizeof(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeFixedU8(const void *buf, uint8_t *value) {
|
static FORCE_INLINE void *taosDecodeFixedU8(const void *buf, uint8_t *value) {
|
||||||
|
@ -43,12 +43,12 @@ static FORCE_INLINE void *taosDecodeFixedU8(const void *buf, uint8_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Fixed I8
|
// ---- Fixed I8
|
||||||
static FORCE_INLINE int taosEncodeFixedI8(void **buf, int8_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedI8(void **buf, int8_t value) {
|
||||||
if (buf != NULL) {
|
if (buf != NULL) {
|
||||||
((int8_t *)(*buf))[0] = value;
|
((int8_t *)(*buf))[0] = value;
|
||||||
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
||||||
}
|
}
|
||||||
return (int)sizeof(value);
|
return (int32_t)sizeof(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeFixedI8(const void *buf, int8_t *value) {
|
static FORCE_INLINE void *taosDecodeFixedI8(const void *buf, int8_t *value) {
|
||||||
|
@ -57,7 +57,7 @@ static FORCE_INLINE void *taosDecodeFixedI8(const void *buf, int8_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Fixed U16
|
// ---- Fixed U16
|
||||||
static FORCE_INLINE int taosEncodeFixedU16(void **buf, uint16_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedU16(void **buf, uint16_t value) {
|
||||||
if (buf != NULL) {
|
if (buf != NULL) {
|
||||||
if (IS_LITTLE_ENDIAN()) {
|
if (IS_LITTLE_ENDIAN()) {
|
||||||
memcpy(*buf, &value, sizeof(value));
|
memcpy(*buf, &value, sizeof(value));
|
||||||
|
@ -68,7 +68,7 @@ static FORCE_INLINE int taosEncodeFixedU16(void **buf, uint16_t value) {
|
||||||
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int)sizeof(value);
|
return (int32_t)sizeof(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeFixedU16(const void *buf, uint16_t *value) {
|
static FORCE_INLINE void *taosDecodeFixedU16(const void *buf, uint16_t *value) {
|
||||||
|
@ -83,7 +83,7 @@ static FORCE_INLINE void *taosDecodeFixedU16(const void *buf, uint16_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Fixed I16
|
// ---- Fixed I16
|
||||||
static FORCE_INLINE int taosEncodeFixedI16(void **buf, int16_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedI16(void **buf, int16_t value) {
|
||||||
return taosEncodeFixedU16(buf, ZIGZAGE(int16_t, value));
|
return taosEncodeFixedU16(buf, ZIGZAGE(int16_t, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ static FORCE_INLINE void *taosDecodeFixedI16(const void *buf, int16_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Fixed U32
|
// ---- Fixed U32
|
||||||
static FORCE_INLINE int taosEncodeFixedU32(void **buf, uint32_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedU32(void **buf, uint32_t value) {
|
||||||
if (buf != NULL) {
|
if (buf != NULL) {
|
||||||
if (IS_LITTLE_ENDIAN()) {
|
if (IS_LITTLE_ENDIAN()) {
|
||||||
memcpy(*buf, &value, sizeof(value));
|
memcpy(*buf, &value, sizeof(value));
|
||||||
|
@ -108,7 +108,7 @@ static FORCE_INLINE int taosEncodeFixedU32(void **buf, uint32_t value) {
|
||||||
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int)sizeof(value);
|
return (int32_t)sizeof(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeFixedU32(const void *buf, uint32_t *value) {
|
static FORCE_INLINE void *taosDecodeFixedU32(const void *buf, uint32_t *value) {
|
||||||
|
@ -125,7 +125,7 @@ static FORCE_INLINE void *taosDecodeFixedU32(const void *buf, uint32_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Fixed I32
|
// ---- Fixed I32
|
||||||
static FORCE_INLINE int taosEncodeFixedI32(void **buf, int32_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedI32(void **buf, int32_t value) {
|
||||||
return taosEncodeFixedU32(buf, ZIGZAGE(int32_t, value));
|
return taosEncodeFixedU32(buf, ZIGZAGE(int32_t, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +137,7 @@ static FORCE_INLINE void *taosDecodeFixedI32(const void *buf, int32_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Fixed U64
|
// ---- Fixed U64
|
||||||
static FORCE_INLINE int taosEncodeFixedU64(void **buf, uint64_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedU64(void **buf, uint64_t value) {
|
||||||
if (buf != NULL) {
|
if (buf != NULL) {
|
||||||
if (IS_LITTLE_ENDIAN()) {
|
if (IS_LITTLE_ENDIAN()) {
|
||||||
memcpy(*buf, &value, sizeof(value));
|
memcpy(*buf, &value, sizeof(value));
|
||||||
|
@ -155,7 +155,7 @@ static FORCE_INLINE int taosEncodeFixedU64(void **buf, uint64_t value) {
|
||||||
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
*buf = POINTER_SHIFT(*buf, sizeof(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int)sizeof(value);
|
return (int32_t)sizeof(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeFixedU64(const void *buf, uint64_t *value) {
|
static FORCE_INLINE void *taosDecodeFixedU64(const void *buf, uint64_t *value) {
|
||||||
|
@ -176,7 +176,7 @@ static FORCE_INLINE void *taosDecodeFixedU64(const void *buf, uint64_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Fixed I64
|
// ---- Fixed I64
|
||||||
static FORCE_INLINE int taosEncodeFixedI64(void **buf, int64_t value) {
|
static FORCE_INLINE int32_t taosEncodeFixedI64(void **buf, int64_t value) {
|
||||||
return taosEncodeFixedU64(buf, ZIGZAGE(int64_t, value));
|
return taosEncodeFixedU64(buf, ZIGZAGE(int64_t, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,8 +188,8 @@ static FORCE_INLINE void *taosDecodeFixedI64(const void *buf, int64_t *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Variant U16
|
// ---- Variant U16
|
||||||
static FORCE_INLINE int taosEncodeVariantU16(void **buf, uint16_t value) {
|
static FORCE_INLINE int32_t taosEncodeVariantU16(void **buf, uint16_t value) {
|
||||||
int i = 0;
|
int32_t i = 0;
|
||||||
while (value >= ENCODE_LIMIT) {
|
while (value >= ENCODE_LIMIT) {
|
||||||
if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT);
|
if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT);
|
||||||
value >>= 7;
|
value >>= 7;
|
||||||
|
@ -206,7 +206,7 @@ static FORCE_INLINE int taosEncodeVariantU16(void **buf, uint16_t value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeVariantU16(const void *buf, uint16_t *value) {
|
static FORCE_INLINE void *taosDecodeVariantU16(const void *buf, uint16_t *value) {
|
||||||
int i = 0;
|
int32_t i = 0;
|
||||||
uint16_t tval = 0;
|
uint16_t tval = 0;
|
||||||
*value = 0;
|
*value = 0;
|
||||||
while (i < 3) {
|
while (i < 3) {
|
||||||
|
@ -224,7 +224,7 @@ static FORCE_INLINE void *taosDecodeVariantU16(const void *buf, uint16_t *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Variant I16
|
// ---- Variant I16
|
||||||
static FORCE_INLINE int taosEncodeVariantI16(void **buf, int16_t value) {
|
static FORCE_INLINE int32_t taosEncodeVariantI16(void **buf, int16_t value) {
|
||||||
return taosEncodeVariantU16(buf, ZIGZAGE(int16_t, value));
|
return taosEncodeVariantU16(buf, ZIGZAGE(int16_t, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,8 +236,8 @@ static FORCE_INLINE void *taosDecodeVariantI16(const void *buf, int16_t *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Variant U32
|
// ---- Variant U32
|
||||||
static FORCE_INLINE int taosEncodeVariantU32(void **buf, uint32_t value) {
|
static FORCE_INLINE int32_t taosEncodeVariantU32(void **buf, uint32_t value) {
|
||||||
int i = 0;
|
int32_t i = 0;
|
||||||
while (value >= ENCODE_LIMIT) {
|
while (value >= ENCODE_LIMIT) {
|
||||||
if (buf != NULL) ((uint8_t *)(*buf))[i] = (value | ENCODE_LIMIT);
|
if (buf != NULL) ((uint8_t *)(*buf))[i] = (value | ENCODE_LIMIT);
|
||||||
value >>= 7;
|
value >>= 7;
|
||||||
|
@ -254,7 +254,7 @@ static FORCE_INLINE int taosEncodeVariantU32(void **buf, uint32_t value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeVariantU32(const void *buf, uint32_t *value) {
|
static FORCE_INLINE void *taosDecodeVariantU32(const void *buf, uint32_t *value) {
|
||||||
int i = 0;
|
int32_t i = 0;
|
||||||
uint32_t tval = 0;
|
uint32_t tval = 0;
|
||||||
*value = 0;
|
*value = 0;
|
||||||
while (i < 5) {
|
while (i < 5) {
|
||||||
|
@ -272,7 +272,7 @@ static FORCE_INLINE void *taosDecodeVariantU32(const void *buf, uint32_t *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Variant I32
|
// ---- Variant I32
|
||||||
static FORCE_INLINE int taosEncodeVariantI32(void **buf, int32_t value) {
|
static FORCE_INLINE int32_t taosEncodeVariantI32(void **buf, int32_t value) {
|
||||||
return taosEncodeVariantU32(buf, ZIGZAGE(int32_t, value));
|
return taosEncodeVariantU32(buf, ZIGZAGE(int32_t, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -284,8 +284,8 @@ static FORCE_INLINE void *taosDecodeVariantI32(const void *buf, int32_t *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Variant U64
|
// ---- Variant U64
|
||||||
static FORCE_INLINE int taosEncodeVariantU64(void **buf, uint64_t value) {
|
static FORCE_INLINE int32_t taosEncodeVariantU64(void **buf, uint64_t value) {
|
||||||
int i = 0;
|
int32_t i = 0;
|
||||||
while (value >= ENCODE_LIMIT) {
|
while (value >= ENCODE_LIMIT) {
|
||||||
if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT);
|
if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT);
|
||||||
value >>= 7;
|
value >>= 7;
|
||||||
|
@ -302,7 +302,7 @@ static FORCE_INLINE int taosEncodeVariantU64(void **buf, uint64_t value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static FORCE_INLINE void *taosDecodeVariantU64(const void *buf, uint64_t *value) {
|
static FORCE_INLINE void *taosDecodeVariantU64(const void *buf, uint64_t *value) {
|
||||||
int i = 0;
|
int32_t i = 0;
|
||||||
uint64_t tval = 0;
|
uint64_t tval = 0;
|
||||||
*value = 0;
|
*value = 0;
|
||||||
while (i < 10) {
|
while (i < 10) {
|
||||||
|
@ -320,7 +320,7 @@ static FORCE_INLINE void *taosDecodeVariantU64(const void *buf, uint64_t *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- Variant I64
|
// ---- Variant I64
|
||||||
static FORCE_INLINE int taosEncodeVariantI64(void **buf, int64_t value) {
|
static FORCE_INLINE int32_t taosEncodeVariantI64(void **buf, int64_t value) {
|
||||||
return taosEncodeVariantU64(buf, ZIGZAGE(int64_t, value));
|
return taosEncodeVariantU64(buf, ZIGZAGE(int64_t, value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -332,8 +332,8 @@ static FORCE_INLINE void *taosDecodeVariantI64(const void *buf, int64_t *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- string
|
// ---- string
|
||||||
static FORCE_INLINE int taosEncodeString(void **buf, const char *value) {
|
static FORCE_INLINE int32_t taosEncodeString(void **buf, const char *value) {
|
||||||
int tlen = 0;
|
int32_t tlen = 0;
|
||||||
size_t size = strlen(value);
|
size_t size = strlen(value);
|
||||||
|
|
||||||
tlen += taosEncodeVariantU64(buf, size);
|
tlen += taosEncodeVariantU64(buf, size);
|
||||||
|
@ -341,7 +341,7 @@ static FORCE_INLINE int taosEncodeString(void **buf, const char *value) {
|
||||||
memcpy(*buf, value, size);
|
memcpy(*buf, value, size);
|
||||||
*buf = POINTER_SHIFT(*buf, size);
|
*buf = POINTER_SHIFT(*buf, size);
|
||||||
}
|
}
|
||||||
tlen += (int)size;
|
tlen += (int32_t)size;
|
||||||
|
|
||||||
return tlen;
|
return tlen;
|
||||||
}
|
}
|
||||||
|
@ -372,14 +372,14 @@ static FORCE_INLINE void *taosDecodeStringTo(const void *buf, char *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---- binary
|
// ---- binary
|
||||||
static FORCE_INLINE int taosEncodeBinary(void **buf, const void *value, int32_t valueLen) {
|
static FORCE_INLINE int32_t taosEncodeBinary(void **buf, const void *value, int32_t valueLen) {
|
||||||
int tlen = 0;
|
int32_t tlen = 0;
|
||||||
|
|
||||||
if (buf != NULL) {
|
if (buf != NULL) {
|
||||||
memcpy(*buf, value, valueLen);
|
memcpy(*buf, value, valueLen);
|
||||||
*buf = POINTER_SHIFT(*buf, valueLen);
|
*buf = POINTER_SHIFT(*buf, valueLen);
|
||||||
}
|
}
|
||||||
tlen += (int)valueLen;
|
tlen += (int32_t)valueLen;
|
||||||
|
|
||||||
return tlen;
|
return tlen;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue