From 513bbc50b603ab283a5fbe801901ba54b0b36d8d Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 15 Jul 2024 17:35:00 +0800 Subject: [PATCH 1/4] enh: refact encode and decode function --- include/util/tencode.h | 157 +++++++++++++++++++++----------------- source/util/src/tencode.c | 14 +++- 2 files changed, 95 insertions(+), 76 deletions(-) diff --git a/include/util/tencode.h b/include/util/tencode.h index b8da040689..a50a9ea8e4 100644 --- a/include/util/tencode.h +++ b/include/util/tencode.h @@ -18,6 +18,7 @@ #include "tcoding.h" #include "tlist.h" +#include "tutil.h" #ifdef __cplusplus extern "C" { @@ -49,30 +50,6 @@ typedef struct { #define tPut(TYPE, BUF, VAL) ((TYPE*)(BUF))[0] = (VAL) #define tGet(TYPE, BUF, VAL) (VAL) = ((TYPE*)(BUF))[0] -#define tRPut16(PDEST, PSRC) \ - ((uint8_t*)(PDEST))[0] = ((uint8_t*)(PSRC))[1]; \ - ((uint8_t*)(PDEST))[1] = ((uint8_t*)(PSRC))[0]; - -#define tRPut32(PDEST, PSRC) \ - ((uint8_t*)(PDEST))[0] = ((uint8_t*)(PSRC))[3]; \ - ((uint8_t*)(PDEST))[1] = ((uint8_t*)(PSRC))[2]; \ - ((uint8_t*)(PDEST))[2] = ((uint8_t*)(PSRC))[1]; \ - ((uint8_t*)(PDEST))[3] = ((uint8_t*)(PSRC))[0]; - -#define tRPut64(PDEST, PSRC) \ - ((uint8_t*)(PDEST))[0] = ((uint8_t*)(PSRC))[7]; \ - ((uint8_t*)(PDEST))[1] = ((uint8_t*)(PSRC))[6]; \ - ((uint8_t*)(PDEST))[2] = ((uint8_t*)(PSRC))[5]; \ - ((uint8_t*)(PDEST))[3] = ((uint8_t*)(PSRC))[4]; \ - ((uint8_t*)(PDEST))[4] = ((uint8_t*)(PSRC))[3]; \ - ((uint8_t*)(PDEST))[5] = ((uint8_t*)(PSRC))[2]; \ - ((uint8_t*)(PDEST))[6] = ((uint8_t*)(PSRC))[1]; \ - ((uint8_t*)(PDEST))[7] = ((uint8_t*)(PSRC))[0]; - -#define tRGet16 tRPut16 -#define tRGet32 tRPut32 -#define tRGet64 tRPut64 - #define TD_CODER_POS(CODER) ((CODER)->pos) #define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) #define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) @@ -149,18 +126,22 @@ static int32_t tDecodeCStr(SDecoder* pCoder, char** val); static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val); /* ------------------------ IMPL ------------------------ */ -#define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ - if ((CODER)->data) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(VAL))) return -1; \ - tPut(TYPE, TD_CODER_CURRENT(CODER), (VAL)); \ - } \ - TD_CODER_MOVE_POS(CODER, sizeof(VAL)); \ +#define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ + if ((CODER)->data) { \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(VAL))) { \ + return TSDB_CODE_OUT_OF_RANGE; \ + } \ + tPut(TYPE, TD_CODER_CURRENT(CODER), (VAL)); \ + } \ + TD_CODER_MOVE_POS(CODER, sizeof(VAL)); \ return 0; #define TD_ENCODE_VARIANT_MACRO(CODER, VAL) \ while ((VAL) >= ENCODE_LIMIT) { \ if ((CODER)->data) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) { \ + return TSDB_CODE_OUT_OF_RANGE; \ + } \ TD_CODER_CURRENT(CODER)[0] = ((VAL) | ENCODE_LIMIT) & 0xff; \ } \ \ @@ -169,23 +150,29 @@ static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val); } \ \ if ((CODER)->data) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) { \ + return TSDB_CODE_OUT_OF_RANGE; \ + } \ TD_CODER_CURRENT(CODER)[0] = (uint8_t)(VAL); \ } \ TD_CODER_MOVE_POS(CODER, 1); \ return 0; -#define TD_DECODE_MACRO(CODER, PVAL, TYPE, BITS) \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(*(PVAL)))) return -1; \ - tGet(TYPE, TD_CODER_CURRENT(CODER), *(PVAL)); \ - TD_CODER_MOVE_POS(CODER, sizeof(*(PVAL))); \ +#define TD_DECODE_MACRO(CODER, PVAL, TYPE, BITS) \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(*(PVAL)))) { \ + return TSDB_CODE_OUT_OF_RANGE; \ + } \ + tGet(TYPE, TD_CODER_CURRENT(CODER), *(PVAL)); \ + TD_CODER_MOVE_POS(CODER, sizeof(*(PVAL))); \ return 0; #define TD_DECODE_VARIANT_MACRO(CODER, PVAL, TYPE) \ int32_t i = 0; \ *(PVAL) = 0; \ for (;;) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1; \ + if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) { \ + return TSDB_CODE_OUT_OF_RANGE; \ + } \ TYPE tval = TD_CODER_CURRENT(CODER)[0]; \ if (tval < ENCODE_LIMIT) { \ *(PVAL) |= (tval << (7 * i)); \ @@ -203,7 +190,9 @@ static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val); // 8 static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { if (pCoder->data) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(val))) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(val))) { + return TSDB_CODE_OUT_OF_RANGE; + } tPut(uint8_t, TD_CODER_CURRENT(pCoder), val); } TD_CODER_MOVE_POS(pCoder, sizeof(val)); @@ -212,7 +201,9 @@ static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { if (pCoder->data) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(val))) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(val))) { + return TSDB_CODE_OUT_OF_RANGE; + } tPut(int8_t, TD_CODER_CURRENT(pCoder), val); } TD_CODER_MOVE_POS(pCoder, sizeof(val)); @@ -265,10 +256,12 @@ static FORCE_INLINE int32_t tEncodeDouble(SEncoder* pCoder, double val) { } static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len) { - if (tEncodeU32v(pCoder, len) < 0) return -1; + TAOS_CHECK_RETURN(tEncodeU32v(pCoder, len)); if (len) { if (pCoder->data) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, len)) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, len)) { + return TSDB_CODE_OUT_OF_RANGE; + } memcpy(TD_CODER_CURRENT(pCoder), val, len); } @@ -288,14 +281,18 @@ static FORCE_INLINE int32_t tEncodeCStr(SEncoder* pCoder, const char* val) { /* ------------------------ FOR DECODER ------------------------ */ // 8 static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) { + return TSDB_CODE_OUT_OF_RANGE; + } tGet(uint8_t, TD_CODER_CURRENT(pCoder), *val); TD_CODER_MOVE_POS(pCoder, sizeof(*val)); return 0; } static FORCE_INLINE int32_t tDecodeI8(SDecoder* pCoder, int8_t* val) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) { + return TSDB_CODE_OUT_OF_RANGE; + } tGet(int8_t, TD_CODER_CURRENT(pCoder), *val); TD_CODER_MOVE_POS(pCoder, sizeof(*val)); return 0; @@ -318,10 +315,10 @@ static FORCE_INLINE int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val) { static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) { uint16_t tval; - if (tDecodeU16v(pCoder, &tval) < 0) { - return -1; + TAOS_CHECK_RETURN(tDecodeU16v(pCoder, &tval)); + if (val) { + *val = ZIGZAGD(int16_t, tval); } - if (val) *val = ZIGZAGD(int16_t, tval); return 0; } @@ -332,10 +329,10 @@ static FORCE_INLINE int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val) { static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) { uint32_t tval; - if (tDecodeU32v(pCoder, &tval) < 0) { - return -1; + TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &tval)); + if (val) { + *val = ZIGZAGD(int32_t, tval); } - if (val) *val = ZIGZAGD(int32_t, tval); return 0; } @@ -346,10 +343,10 @@ static FORCE_INLINE int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val) { static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) { uint64_t tval; - if (tDecodeU64v(pCoder, &tval) < 0) { - return -1; + TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &tval)); + if (val) { + *val = ZIGZAGD(int64_t, tval); } - if (val) *val = ZIGZAGD(int64_t, tval); return 0; } @@ -359,11 +356,11 @@ static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) { float f; } v; - if (tDecodeU32(pCoder, &(v.ui)) < 0) { - return -1; - } + TAOS_CHECK_RETURN(tDecodeU32(pCoder, &(v.ui))); - *val = v.f; + if (val) { + *val = v.f; + } return 0; } @@ -373,20 +370,26 @@ static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) { double d; } v; - if (tDecodeU64(pCoder, &(v.ui)) < 0) { - return -1; - } + TAOS_CHECK_RETURN(tDecodeU64(pCoder, &(v.ui))); - *val = v.d; + if (val) { + *val = v.d; + } return 0; } static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint32_t* len) { uint32_t length = 0; - if (tDecodeU32v(pCoder, &length) < 0) return -1; - if (len) *len = length; - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) return -1; + TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length)); + if (len) { + *len = length; + } + + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { + return TSDB_CODE_OUT_OF_RANGE; + } + if (val) { *val = (uint8_t*)TD_CODER_CURRENT(pCoder); } @@ -396,7 +399,7 @@ static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint3 } static FORCE_INLINE int32_t tDecodeCStrAndLen(SDecoder* pCoder, char** val, uint32_t* len) { - if (tDecodeBinary(pCoder, (uint8_t**)val, len) < 0) return -1; + TAOS_CHECK_RETURN(tDecodeBinary(pCoder, (uint8_t**)val, len)); (*len) -= 1; return 0; } @@ -409,7 +412,7 @@ static FORCE_INLINE int32_t tDecodeCStr(SDecoder* pCoder, char** val) { static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) { char* pStr; uint32_t len; - if (tDecodeCStrAndLen(pCoder, &pStr, &len) < 0) return -1; + TAOS_CHECK_RETURN(tDecodeCStrAndLen(pCoder, &pStr, &len)); memcpy(val, pStr, len + 1); return 0; @@ -417,13 +420,19 @@ static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) { static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) { uint64_t length = 0; - if (tDecodeU64v(pCoder, &length) < 0) return -1; + TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &length)); if (length) { if (len) *len = length; - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { + return TSDB_CODE_OUT_OF_RANGE; + } + *val = taosMemoryMalloc(length); - if (*val == NULL) return -1; + if (*val == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + memcpy(*val, TD_CODER_CURRENT(pCoder), length); TD_CODER_MOVE_POS(pCoder, length); @@ -435,13 +444,17 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uin static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, uint32_t* len) { uint32_t length = 0; - if (tDecodeU32v(pCoder, &length) < 0) return -1; + TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length)); if (length) { if (len) *len = length; - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) return -1; + if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { + return TSDB_CODE_OUT_OF_RANGE; + } *val = taosMemoryMalloc(length); - if (*val == NULL) return -1; + if (*val == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } memcpy(*val, TD_CODER_CURRENT(pCoder), length); TD_CODER_MOVE_POS(pCoder, length); @@ -452,7 +465,7 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, u } static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SDecoder* pCoder, char** val, uint64_t* len) { - if (tDecodeBinaryAlloc(pCoder, (void**)val, len) < 0) return -1; + TAOS_CHECK_RETURN(tDecodeBinaryAlloc(pCoder, (void**)val, len)); (*len) -= 1; return 0; } diff --git a/source/util/src/tencode.c b/source/util/src/tencode.c index 7f8a0edfdd..d99732fedf 100644 --- a/source/util/src/tencode.c +++ b/source/util/src/tencode.c @@ -72,10 +72,14 @@ int32_t tStartEncode(SEncoder* pCoder) { SEncoderNode* pNode; if (pCoder->data) { - if (pCoder->size - pCoder->pos < sizeof(int32_t)) return -1; + if (pCoder->size - pCoder->pos < sizeof(int32_t)) { + return TSDB_CODE_OUT_OF_RANGE; + } pNode = tEncoderMalloc(pCoder, sizeof(*pNode)); - if (pNode == NULL) return -1; + if (pNode == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } pNode->data = pCoder->data; pNode->pos = pCoder->pos; @@ -119,10 +123,12 @@ int32_t tStartDecode(SDecoder* pCoder) { SDecoderNode* pNode; int32_t len; - if (tDecodeI32(pCoder, &len) < 0) return -1; + TAOS_CHECK_RETURN(tDecodeI32(pCoder, &len)); pNode = tDecoderMalloc(pCoder, sizeof(*pNode)); - if (pNode == NULL) return -1; + if (pNode == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } pNode->data = pCoder->data; pNode->pos = pCoder->pos; From d2b05bfbbd54e6566493cd45e1fc2ce713a91c85 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 15 Jul 2024 18:06:38 +0800 Subject: [PATCH 2/4] enh: refact more encode and decode function --- include/util/tencode.h | 290 ++++++++++++++++++++------------------ source/util/src/tencode.c | 10 +- 2 files changed, 158 insertions(+), 142 deletions(-) diff --git a/include/util/tencode.h b/include/util/tencode.h index a50a9ea8e4..419be59a92 100644 --- a/include/util/tencode.h +++ b/include/util/tencode.h @@ -47,9 +47,6 @@ typedef struct { SDecoderNode* dStack; } SDecoder; -#define tPut(TYPE, BUF, VAL) ((TYPE*)(BUF))[0] = (VAL) -#define tGet(TYPE, BUF, VAL) (VAL) = ((TYPE*)(BUF))[0] - #define TD_CODER_POS(CODER) ((CODER)->pos) #define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) #define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) @@ -77,6 +74,7 @@ void tEncoderInit(SEncoder* pCoder, uint8_t* data, uint32_t size); void tEncoderClear(SEncoder* pCoder); int32_t tStartEncode(SEncoder* pCoder); void tEndEncode(SEncoder* pCoder); +static int32_t tEncodeFixed(SEncoder* pCoder, const void* val, uint32_t size); static int32_t tEncodeU8(SEncoder* pCoder, uint8_t val); static int32_t tEncodeI8(SEncoder* pCoder, int8_t val); static int32_t tEncodeU16(SEncoder* pCoder, uint16_t val); @@ -104,6 +102,7 @@ void tDecoderClear(SDecoder* SDecoder); int32_t tStartDecode(SDecoder* pCoder); void tEndDecode(SDecoder* pCoder); static bool tDecodeIsEnd(SDecoder* pCoder); +static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size); static int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val); static int32_t tDecodeI8(SDecoder* pCoder, int8_t* val); static int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val); @@ -126,111 +125,65 @@ static int32_t tDecodeCStr(SDecoder* pCoder, char** val); static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val); /* ------------------------ IMPL ------------------------ */ -#define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS) \ - if ((CODER)->data) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(VAL))) { \ - return TSDB_CODE_OUT_OF_RANGE; \ - } \ - tPut(TYPE, TD_CODER_CURRENT(CODER), (VAL)); \ - } \ - TD_CODER_MOVE_POS(CODER, sizeof(VAL)); \ - return 0; - -#define TD_ENCODE_VARIANT_MACRO(CODER, VAL) \ - while ((VAL) >= ENCODE_LIMIT) { \ - if ((CODER)->data) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) { \ - return TSDB_CODE_OUT_OF_RANGE; \ - } \ - TD_CODER_CURRENT(CODER)[0] = ((VAL) | ENCODE_LIMIT) & 0xff; \ - } \ - \ - (VAL) >>= 7; \ - TD_CODER_MOVE_POS(CODER, 1); \ - } \ - \ - if ((CODER)->data) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) { \ - return TSDB_CODE_OUT_OF_RANGE; \ - } \ - TD_CODER_CURRENT(CODER)[0] = (uint8_t)(VAL); \ - } \ - TD_CODER_MOVE_POS(CODER, 1); \ - return 0; - -#define TD_DECODE_MACRO(CODER, PVAL, TYPE, BITS) \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(*(PVAL)))) { \ - return TSDB_CODE_OUT_OF_RANGE; \ - } \ - tGet(TYPE, TD_CODER_CURRENT(CODER), *(PVAL)); \ - TD_CODER_MOVE_POS(CODER, sizeof(*(PVAL))); \ - return 0; - -#define TD_DECODE_VARIANT_MACRO(CODER, PVAL, TYPE) \ - int32_t i = 0; \ - *(PVAL) = 0; \ - for (;;) { \ - if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) { \ - return TSDB_CODE_OUT_OF_RANGE; \ - } \ - TYPE tval = TD_CODER_CURRENT(CODER)[0]; \ - if (tval < ENCODE_LIMIT) { \ - *(PVAL) |= (tval << (7 * i)); \ - TD_CODER_MOVE_POS(pCoder, 1); \ - break; \ - } else { \ - *(PVAL) |= (((tval) & (ENCODE_LIMIT - 1)) << (7 * i)); \ - i++; \ - TD_CODER_MOVE_POS(pCoder, 1); \ - } \ - } \ - \ - return 0; - -// 8 -static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { +static FORCE_INLINE int32_t tEncodeFixed(SEncoder* pCoder, const void* val, uint32_t size) { if (pCoder->data) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(val))) { - return TSDB_CODE_OUT_OF_RANGE; + if (pCoder->pos + size > pCoder->size) { + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } - tPut(uint8_t, TD_CODER_CURRENT(pCoder), val); + memcpy(pCoder->data + pCoder->pos, val, size); } - TD_CODER_MOVE_POS(pCoder, sizeof(val)); - return 0; + + pCoder->pos += size; + TAOS_RETURN(0); } -static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { - if (pCoder->data) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(val))) { - return TSDB_CODE_OUT_OF_RANGE; - } - tPut(int8_t, TD_CODER_CURRENT(pCoder), val); - } - TD_CODER_MOVE_POS(pCoder, sizeof(val)); - return 0; +static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); } +static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); } +static FORCE_INLINE int32_t tEncodeU16(SEncoder* pCoder, uint16_t val) { + return tEncodeFixed(pCoder, &val, sizeof(val)); +} +static FORCE_INLINE int32_t tEncodeI16(SEncoder* pCoder, int16_t val) { + return tEncodeFixed(pCoder, &val, sizeof(val)); +} +static FORCE_INLINE int32_t tEncodeU32(SEncoder* pCoder, uint32_t val) { + return tEncodeFixed(pCoder, &val, sizeof(val)); +} +static FORCE_INLINE int32_t tEncodeI32(SEncoder* pCoder, int32_t val) { + return tEncodeFixed(pCoder, &val, sizeof(val)); +} +static FORCE_INLINE int32_t tEncodeU64(SEncoder* pCoder, uint64_t val) { + return tEncodeFixed(pCoder, &val, sizeof(val)); +} +static FORCE_INLINE int32_t tEncodeI64(SEncoder* pCoder, int64_t val) { + return tEncodeFixed(pCoder, &val, sizeof(val)); +} +static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) { + while (val >= ENCODE_LIMIT) { + TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff)); + val >>= 7; + } + TAOS_RETURN(tEncodeU8(pCoder, val)); } - -// 16 -static FORCE_INLINE int32_t tEncodeU16(SEncoder* pCoder, uint16_t val) { TD_ENCODE_MACRO(pCoder, val, uint16_t, 16); } -static FORCE_INLINE int32_t tEncodeI16(SEncoder* pCoder, int16_t val) { TD_ENCODE_MACRO(pCoder, val, int16_t, 16); } -// 32 -static FORCE_INLINE int32_t tEncodeU32(SEncoder* pCoder, uint32_t val) { TD_ENCODE_MACRO(pCoder, val, uint32_t, 32); } -static FORCE_INLINE int32_t tEncodeI32(SEncoder* pCoder, int32_t val) { TD_ENCODE_MACRO(pCoder, val, int32_t, 32); } -// 64 -static FORCE_INLINE int32_t tEncodeU64(SEncoder* pCoder, uint64_t val) { TD_ENCODE_MACRO(pCoder, val, uint64_t, 64); } -static FORCE_INLINE int32_t tEncodeI64(SEncoder* pCoder, int64_t val) { TD_ENCODE_MACRO(pCoder, val, int64_t, 64); } -// 16v -static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) { TD_ENCODE_VARIANT_MACRO(pCoder, val); } static FORCE_INLINE int32_t tEncodeI16v(SEncoder* pCoder, int16_t val) { return tEncodeU16v(pCoder, ZIGZAGE(int16_t, val)); } -// 32v -static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) { TD_ENCODE_VARIANT_MACRO(pCoder, val); } +static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) { + while (val >= ENCODE_LIMIT) { + TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff)); + val >>= 7; + } + TAOS_RETURN(tEncodeU8(pCoder, val)); +} static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) { return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val)); } -// 64v -static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) { TD_ENCODE_VARIANT_MACRO(pCoder, val); } +static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) { + while (val >= ENCODE_LIMIT) { + TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff)); + val >>= 7; + } + TAOS_RETURN(tEncodeU8(pCoder, val)); +} static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) { return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val)); } @@ -260,14 +213,14 @@ static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, if (len) { if (pCoder->data) { if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, len)) { - return TSDB_CODE_OUT_OF_RANGE; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } memcpy(TD_CODER_CURRENT(pCoder), val, len); } TD_CODER_MOVE_POS(pCoder, len); } - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tEncodeCStrWithLen(SEncoder* pCoder, const char* val, uint32_t len) { @@ -279,75 +232,138 @@ static FORCE_INLINE int32_t tEncodeCStr(SEncoder* pCoder, const char* val) { } /* ------------------------ FOR DECODER ------------------------ */ -// 8 -static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) { - return TSDB_CODE_OUT_OF_RANGE; +static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size) { + if (pCoder->pos + size > pCoder->size) { + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); + } else if (val) { + memcpy(val, pCoder->data + pCoder->pos, size); } - tGet(uint8_t, TD_CODER_CURRENT(pCoder), *val); - TD_CODER_MOVE_POS(pCoder, sizeof(*val)); - return 0; + pCoder->pos += size; + TAOS_RETURN(0); } -static FORCE_INLINE int32_t tDecodeI8(SDecoder* pCoder, int8_t* val) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) { - return TSDB_CODE_OUT_OF_RANGE; - } - tGet(int8_t, TD_CODER_CURRENT(pCoder), *val); - TD_CODER_MOVE_POS(pCoder, sizeof(*val)); - return 0; +static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) { + return tDecodeFixed(pCoder, val, sizeof(*val)); } +static FORCE_INLINE int32_t tDecodeI8(SDecoder* pCoder, int8_t* val) { return tDecodeFixed(pCoder, val, sizeof(*val)); } + // 16 -static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) { TD_DECODE_MACRO(pCoder, val, uint16_t, 16); } -static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) { TD_DECODE_MACRO(pCoder, val, int16_t, 16); } +static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) { + return tDecodeFixed(pCoder, val, sizeof(*val)); +} +static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) { + return tDecodeFixed(pCoder, val, sizeof(*val)); +} // 32 -static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) { TD_DECODE_MACRO(pCoder, val, uint32_t, 32); } -static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) { TD_DECODE_MACRO(pCoder, val, int32_t, 32); } +static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) { + return tDecodeFixed(pCoder, val, sizeof(*val)); +} +static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) { + return tDecodeFixed(pCoder, val, sizeof(*val)); +} // 64 -static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) { TD_DECODE_MACRO(pCoder, val, uint64_t, 64); } -static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) { TD_DECODE_MACRO(pCoder, val, int64_t, 64); } +static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) { + return tDecodeFixed(pCoder, val, sizeof(*val)); +} +static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) { + return tDecodeFixed(pCoder, val, sizeof(*val)); +} // 16v static FORCE_INLINE int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val) { - TD_DECODE_VARIANT_MACRO(pCoder, val, uint16_t); + uint8_t byte; + uint16_t tval = 0; + for (int32_t i = 0;; i++) { + TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte)); + if (byte < ENCODE_LIMIT) { + tval |= (byte << (7 * i)); + break; + } else { + tval |= ((byte & (ENCODE_LIMIT - 1)) << (7 * i)); + } + } + + if (val) { + *val = tval; + } + + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) { uint16_t tval; TAOS_CHECK_RETURN(tDecodeU16v(pCoder, &tval)); + if (val) { *val = ZIGZAGD(int16_t, tval); } - return 0; + + TAOS_RETURN(0); } // 32v static FORCE_INLINE int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val) { - TD_DECODE_VARIANT_MACRO(pCoder, val, uint32_t); + uint8_t byte; + uint32_t tval = 0; + for (int32_t i = 0;; i++) { + TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte)); + if (byte < ENCODE_LIMIT) { + tval |= (byte << (7 * i)); + break; + } else { + tval |= ((byte & (ENCODE_LIMIT - 1)) << (7 * i)); + } + } + + if (val) { + *val = tval; + } + + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) { uint32_t tval; TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &tval)); + if (val) { *val = ZIGZAGD(int32_t, tval); } - return 0; + + TAOS_RETURN(0); } // 64v static FORCE_INLINE int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val) { - TD_DECODE_VARIANT_MACRO(pCoder, val, uint64_t); + uint8_t byte; + uint64_t tval = 0; + for (int32_t i = 0;; i++) { + TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte)); + if (byte < ENCODE_LIMIT) { + tval |= (byte << (7 * i)); + break; + } else { + tval |= ((byte & (ENCODE_LIMIT - 1)) << (7 * i)); + } + } + + if (val) { + *val = tval; + } + + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) { uint64_t tval; TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &tval)); + if (val) { *val = ZIGZAGD(int64_t, tval); } - return 0; + + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) { @@ -361,7 +377,7 @@ static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) { if (val) { *val = v.f; } - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) { @@ -375,7 +391,7 @@ static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) { if (val) { *val = v.d; } - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint32_t* len) { @@ -387,7 +403,7 @@ static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint3 } if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { - return TSDB_CODE_OUT_OF_RANGE; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } if (val) { @@ -395,13 +411,13 @@ static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint3 } TD_CODER_MOVE_POS(pCoder, length); - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeCStrAndLen(SDecoder* pCoder, char** val, uint32_t* len) { TAOS_CHECK_RETURN(tDecodeBinary(pCoder, (uint8_t**)val, len)); (*len) -= 1; - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeCStr(SDecoder* pCoder, char** val) { @@ -415,7 +431,7 @@ static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) { TAOS_CHECK_RETURN(tDecodeCStrAndLen(pCoder, &pStr, &len)); memcpy(val, pStr, len + 1); - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) { @@ -425,12 +441,12 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uin if (len) *len = length; if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { - return TSDB_CODE_OUT_OF_RANGE; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } *val = taosMemoryMalloc(length); if (*val == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } memcpy(*val, TD_CODER_CURRENT(pCoder), length); @@ -439,7 +455,7 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uin } else { *val = NULL; } - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, uint32_t* len) { @@ -449,11 +465,11 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, u if (len) *len = length; if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { - return TSDB_CODE_OUT_OF_RANGE; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } *val = taosMemoryMalloc(length); if (*val == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } memcpy(*val, TD_CODER_CURRENT(pCoder), length); @@ -461,13 +477,13 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, u } else { *val = NULL; } - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SDecoder* pCoder, char** val, uint64_t* len) { TAOS_CHECK_RETURN(tDecodeBinaryAlloc(pCoder, (void**)val, len)); (*len) -= 1; - return 0; + TAOS_RETURN(0); } static FORCE_INLINE int32_t tDecodeCStrAlloc(SDecoder* pCoder, char** val) { diff --git a/source/util/src/tencode.c b/source/util/src/tencode.c index d99732fedf..0c7275d481 100644 --- a/source/util/src/tencode.c +++ b/source/util/src/tencode.c @@ -73,12 +73,12 @@ int32_t tStartEncode(SEncoder* pCoder) { if (pCoder->data) { if (pCoder->size - pCoder->pos < sizeof(int32_t)) { - return TSDB_CODE_OUT_OF_RANGE; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } pNode = tEncoderMalloc(pCoder, sizeof(*pNode)); if (pNode == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pNode->data = pCoder->data; @@ -95,7 +95,7 @@ int32_t tStartEncode(SEncoder* pCoder) { pCoder->pos += sizeof(int32_t); } - return 0; + TAOS_RETURN(0); } void tEndEncode(SEncoder* pCoder) { @@ -127,7 +127,7 @@ int32_t tStartDecode(SDecoder* pCoder) { pNode = tDecoderMalloc(pCoder, sizeof(*pNode)); if (pNode == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pNode->data = pCoder->data; @@ -141,7 +141,7 @@ int32_t tStartDecode(SDecoder* pCoder) { pNode->pNext = pCoder->dStack; pCoder->dStack = pNode; - return 0; + TAOS_RETURN(0); } void tEndDecode(SDecoder* pCoder) { From 88281f7f34f5deb4a642c65e1a910ecef0c977fd Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 16 Jul 2024 11:50:19 +0800 Subject: [PATCH 3/4] more fix --- include/util/tencode.h | 83 +++++++++++++++++++-------------------- source/util/src/tencode.c | 2 +- 2 files changed, 41 insertions(+), 44 deletions(-) diff --git a/include/util/tencode.h b/include/util/tencode.h index 419be59a92..9b9f8af1b6 100644 --- a/include/util/tencode.h +++ b/include/util/tencode.h @@ -47,11 +47,8 @@ typedef struct { SDecoderNode* dStack; } SDecoder; -#define TD_CODER_POS(CODER) ((CODER)->pos) -#define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) -#define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE)) -#define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE)) -#define TD_CODER_REMAIN_CAPACITY(CODER) ((CODER)->size - (CODER)->pos) +#define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos) +#define TD_CODER_REMAIN_CAPACITY(CODER) ((CODER)->size - (CODER)->pos) #define tEncodeSize(E, S, SIZE, RET) \ do { \ @@ -134,7 +131,7 @@ static FORCE_INLINE int32_t tEncodeFixed(SEncoder* pCoder, const void* val, uint } pCoder->pos += size; - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); } @@ -162,7 +159,7 @@ static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) { TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff)); val >>= 7; } - TAOS_RETURN(tEncodeU8(pCoder, val)); + return tEncodeU8(pCoder, val); } static FORCE_INLINE int32_t tEncodeI16v(SEncoder* pCoder, int16_t val) { return tEncodeU16v(pCoder, ZIGZAGE(int16_t, val)); @@ -172,7 +169,7 @@ static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) { TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff)); val >>= 7; } - TAOS_RETURN(tEncodeU8(pCoder, val)); + return tEncodeU8(pCoder, val); } static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) { return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val)); @@ -182,7 +179,7 @@ static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) { TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff)); val >>= 7; } - TAOS_RETURN(tEncodeU8(pCoder, val)); + return tEncodeU8(pCoder, val); } static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) { return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val)); @@ -212,15 +209,15 @@ static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, TAOS_CHECK_RETURN(tEncodeU32v(pCoder, len)); if (len) { if (pCoder->data) { - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, len)) { + if (pCoder->pos + len > pCoder->size) { TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } - memcpy(TD_CODER_CURRENT(pCoder), val, len); + memcpy(pCoder->data + pCoder->pos, val, len); } - TD_CODER_MOVE_POS(pCoder, len); + pCoder->pos += len; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tEncodeCStrWithLen(SEncoder* pCoder, const char* val, uint32_t len) { @@ -239,7 +236,7 @@ static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size) { memcpy(val, pCoder->data + pCoder->pos, size); } pCoder->pos += size; - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) { @@ -277,10 +274,10 @@ static FORCE_INLINE int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val) { for (int32_t i = 0;; i++) { TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte)); if (byte < ENCODE_LIMIT) { - tval |= (byte << (7 * i)); + tval |= (((uint16_t)byte) << (7 * i)); break; } else { - tval |= ((byte & (ENCODE_LIMIT - 1)) << (7 * i)); + tval |= ((((uint16_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i)); } } @@ -288,7 +285,7 @@ static FORCE_INLINE int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val) { *val = tval; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) { @@ -299,7 +296,7 @@ static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) { *val = ZIGZAGD(int16_t, tval); } - TAOS_RETURN(0); + return 0; } // 32v @@ -309,10 +306,10 @@ static FORCE_INLINE int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val) { for (int32_t i = 0;; i++) { TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte)); if (byte < ENCODE_LIMIT) { - tval |= (byte << (7 * i)); + tval |= (((uint32_t)byte) << (7 * i)); break; } else { - tval |= ((byte & (ENCODE_LIMIT - 1)) << (7 * i)); + tval |= ((((uint32_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i)); } } @@ -320,7 +317,7 @@ static FORCE_INLINE int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val) { *val = tval; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) { @@ -331,7 +328,7 @@ static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) { *val = ZIGZAGD(int32_t, tval); } - TAOS_RETURN(0); + return 0; } // 64v @@ -341,10 +338,10 @@ static FORCE_INLINE int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val) { for (int32_t i = 0;; i++) { TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte)); if (byte < ENCODE_LIMIT) { - tval |= (byte << (7 * i)); + tval |= (((uint64_t)byte) << (7 * i)); break; } else { - tval |= ((byte & (ENCODE_LIMIT - 1)) << (7 * i)); + tval |= ((((uint64_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i)); } } @@ -352,7 +349,7 @@ static FORCE_INLINE int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val) { *val = tval; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) { @@ -363,7 +360,7 @@ static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) { *val = ZIGZAGD(int64_t, tval); } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) { @@ -377,7 +374,7 @@ static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) { if (val) { *val = v.f; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) { @@ -391,7 +388,7 @@ static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) { if (val) { *val = v.d; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint32_t* len) { @@ -402,22 +399,22 @@ static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint3 *len = length; } - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { + if (pCoder->pos + length > pCoder->size) { TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } if (val) { - *val = (uint8_t*)TD_CODER_CURRENT(pCoder); + *val = pCoder->data + pCoder->pos; } - TD_CODER_MOVE_POS(pCoder, length); - TAOS_RETURN(0); + pCoder->pos += length; + return 0; } static FORCE_INLINE int32_t tDecodeCStrAndLen(SDecoder* pCoder, char** val, uint32_t* len) { TAOS_CHECK_RETURN(tDecodeBinary(pCoder, (uint8_t**)val, len)); (*len) -= 1; - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeCStr(SDecoder* pCoder, char** val) { @@ -431,7 +428,7 @@ static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) { TAOS_CHECK_RETURN(tDecodeCStrAndLen(pCoder, &pStr, &len)); memcpy(val, pStr, len + 1); - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) { @@ -440,7 +437,7 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uin if (length) { if (len) *len = length; - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { + if (pCoder->pos + length > pCoder->size) { TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } @@ -449,13 +446,13 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uin TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } - memcpy(*val, TD_CODER_CURRENT(pCoder), length); + memcpy(*val, pCoder->data + pCoder->pos, length); - TD_CODER_MOVE_POS(pCoder, length); + pCoder->pos += length; } else { *val = NULL; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, uint32_t* len) { @@ -464,26 +461,26 @@ static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, u if (length) { if (len) *len = length; - if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, length)) { + if (pCoder->pos + length > pCoder->size) { TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } *val = taosMemoryMalloc(length); if (*val == NULL) { TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } - memcpy(*val, TD_CODER_CURRENT(pCoder), length); + memcpy(*val, pCoder->data + pCoder->pos, length); - TD_CODER_MOVE_POS(pCoder, length); + pCoder->pos += length; } else { *val = NULL; } - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SDecoder* pCoder, char** val, uint64_t* len) { TAOS_CHECK_RETURN(tDecodeBinaryAlloc(pCoder, (void**)val, len)); (*len) -= 1; - TAOS_RETURN(0); + return 0; } static FORCE_INLINE int32_t tDecodeCStrAlloc(SDecoder* pCoder, char** val) { diff --git a/source/util/src/tencode.c b/source/util/src/tencode.c index 0c7275d481..13b3fa4f5a 100644 --- a/source/util/src/tencode.c +++ b/source/util/src/tencode.c @@ -115,7 +115,7 @@ void tEndEncode(SEncoder* pCoder) { (void)tEncodeI32(pCoder, len); - TD_CODER_MOVE_POS(pCoder, len); + pCoder->pos += len; } } From 459a452db21c933c81c38fc0dc92b409b654d2b3 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 16 Jul 2024 14:19:07 +0800 Subject: [PATCH 4/4] fix more --- source/util/src/tencode.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/util/src/tencode.c b/source/util/src/tencode.c index 13b3fa4f5a..aa9f157f3a 100644 --- a/source/util/src/tencode.c +++ b/source/util/src/tencode.c @@ -95,7 +95,7 @@ int32_t tStartEncode(SEncoder* pCoder) { pCoder->pos += sizeof(int32_t); } - TAOS_RETURN(0); + return 0; } void tEndEncode(SEncoder* pCoder) { @@ -141,7 +141,7 @@ int32_t tStartDecode(SDecoder* pCoder) { pNode->pNext = pCoder->dStack; pCoder->dStack = pNode; - TAOS_RETURN(0); + return 0; } void tEndDecode(SDecoder* pCoder) {