TDengine/source/util/src/tencode.c

156 lines
3.7 KiB
C
Raw Normal View History

2021-12-31 09:41:04 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-02-28 02:05:31 +00:00
#define _DEFAULT_SOURCE
2022-02-28 02:03:53 +00:00
#include "tencode.h"
2021-12-31 09:41:04 +00:00
2022-06-21 12:04:55 +00:00
#if __STDC_VERSION__ >= 201112LL
2022-01-02 09:42:57 +00:00
static_assert(sizeof(float) == sizeof(uint32_t), "sizeof(float) must equal to sizeof(uint32_t)");
static_assert(sizeof(double) == sizeof(uint64_t), "sizeof(double) must equal to sizeof(uint64_t)");
#endif
2022-05-07 10:03:06 +00:00
struct SEncoderNode {
SEncoderNode* pNext;
uint8_t* data;
uint32_t size;
uint32_t pos;
};
struct SDecoderNode {
2022-05-23 07:47:49 +00:00
SDecoderNode* pNext;
uint8_t* data;
uint32_t size;
uint32_t pos;
2022-05-07 10:03:06 +00:00
};
void tEncoderInit(SEncoder* pEncoder, uint8_t* data, uint32_t size) {
if (data == NULL) size = 0;
pEncoder->data = data;
pEncoder->size = size;
pEncoder->pos = 0;
pEncoder->mList = NULL;
pEncoder->eStack = NULL;
2021-12-31 09:41:04 +00:00
}
2022-05-07 10:03:06 +00:00
void tEncoderClear(SEncoder* pCoder) {
for (SCoderMem* pMem = pCoder->mList; pMem; pMem = pCoder->mList) {
2022-04-20 08:50:45 +00:00
pCoder->mList = pMem->next;
taosMemoryFree(pMem);
}
2022-05-07 10:03:06 +00:00
memset(pCoder, 0, sizeof(*pCoder));
}
2022-05-23 07:47:49 +00:00
void tDecoderInit(SDecoder* pDecoder, uint8_t* data, uint32_t size) {
2022-05-07 10:03:06 +00:00
pDecoder->data = data;
pDecoder->size = size;
pDecoder->pos = 0;
pDecoder->mList = NULL;
pDecoder->dStack = NULL;
}
2022-04-20 08:50:45 +00:00
2022-05-07 10:03:06 +00:00
void tDecoderClear(SDecoder* pCoder) {
for (SCoderMem* pMem = pCoder->mList; pMem; pMem = pCoder->mList) {
pCoder->mList = pMem->next;
taosMemoryFree(pMem);
2021-12-31 09:41:04 +00:00
}
2022-05-07 10:03:06 +00:00
memset(pCoder, 0, sizeof(*pCoder));
2021-12-31 09:41:04 +00:00
}
2022-05-07 10:03:06 +00:00
int32_t tStartEncode(SEncoder* pCoder) {
SEncoderNode* pNode;
2022-01-02 06:48:48 +00:00
2021-12-31 09:41:04 +00:00
if (pCoder->data) {
2024-07-15 09:35:00 +00:00
if (pCoder->size - pCoder->pos < sizeof(int32_t)) {
TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
2024-07-15 09:35:00 +00:00
}
2022-01-02 06:48:48 +00:00
2022-05-07 10:03:06 +00:00
pNode = tEncoderMalloc(pCoder, sizeof(*pNode));
2024-07-15 09:35:00 +00:00
if (pNode == NULL) {
2024-09-24 08:29:28 +00:00
TAOS_RETURN(terrno);
2024-07-15 09:35:00 +00:00
}
2021-12-31 09:41:04 +00:00
pNode->data = pCoder->data;
pNode->pos = pCoder->pos;
pNode->size = pCoder->size;
2022-01-03 08:45:31 +00:00
pCoder->data = pNode->data + pNode->pos + sizeof(int32_t);
2021-12-31 09:41:04 +00:00
pCoder->pos = 0;
pCoder->size = pNode->size - pNode->pos - sizeof(int32_t);
2022-05-07 10:03:06 +00:00
pNode->pNext = pCoder->eStack;
pCoder->eStack = pNode;
2021-12-31 09:41:04 +00:00
} else {
pCoder->pos += sizeof(int32_t);
}
2022-05-07 10:03:06 +00:00
2024-07-16 06:19:07 +00:00
return 0;
2021-12-31 09:41:04 +00:00
}
2022-05-07 10:03:06 +00:00
void tEndEncode(SEncoder* pCoder) {
SEncoderNode* pNode;
int32_t len;
2021-12-31 09:41:04 +00:00
2025-04-28 08:47:32 +00:00
if (pCoder->data && pCoder->eStack) {
2022-05-07 10:03:06 +00:00
pNode = pCoder->eStack;
pCoder->eStack = pNode->pNext;
2021-12-31 09:41:04 +00:00
2022-01-03 08:45:31 +00:00
len = pCoder->pos;
2021-12-31 09:41:04 +00:00
pCoder->data = pNode->data;
pCoder->size = pNode->size;
2022-01-03 08:45:31 +00:00
pCoder->pos = pNode->pos;
2024-09-23 08:20:05 +00:00
int32_t ret = tEncodeI32(pCoder, len);
2022-01-03 08:45:31 +00:00
2024-07-16 03:50:19 +00:00
pCoder->pos += len;
2021-12-31 09:41:04 +00:00
}
}
2022-05-07 10:03:06 +00:00
int32_t tStartDecode(SDecoder* pCoder) {
SDecoderNode* pNode;
int32_t len;
2021-12-31 09:41:04 +00:00
2024-07-15 09:35:00 +00:00
TAOS_CHECK_RETURN(tDecodeI32(pCoder, &len));
2022-01-02 06:48:48 +00:00
2022-05-07 10:03:06 +00:00
pNode = tDecoderMalloc(pCoder, sizeof(*pNode));
2024-07-15 09:35:00 +00:00
if (pNode == NULL) {
2024-09-24 08:29:28 +00:00
TAOS_RETURN(terrno);
2024-07-15 09:35:00 +00:00
}
2021-12-31 09:41:04 +00:00
pNode->data = pCoder->data;
pNode->pos = pCoder->pos;
pNode->size = pCoder->size;
2022-01-03 08:45:31 +00:00
pCoder->data = pNode->data + pNode->pos;
pCoder->size = len;
2021-12-31 09:41:04 +00:00
pCoder->pos = 0;
2022-05-07 10:03:06 +00:00
pNode->pNext = pCoder->dStack;
pCoder->dStack = pNode;
2021-12-31 09:41:04 +00:00
2024-07-16 06:19:07 +00:00
return 0;
2021-12-31 09:41:04 +00:00
}
2022-05-07 10:03:06 +00:00
void tEndDecode(SDecoder* pCoder) {
SDecoderNode* pNode;
2022-01-03 10:02:21 +00:00
2022-05-07 10:03:06 +00:00
pNode = pCoder->dStack;
pCoder->dStack = pNode->pNext;
2021-12-31 09:41:04 +00:00
pCoder->data = pNode->data;
2022-01-04 03:46:55 +00:00
pCoder->pos = pCoder->size + pNode->pos;
2021-12-31 09:41:04 +00:00
pCoder->size = pNode->size;
}