tencode.c 3.6 KB
Newer Older
H
mroe  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
encode  
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "tencode.h"
H
mroe  
Hongze Cheng 已提交
18

H
more  
Hongze Cheng 已提交
19 20 21 22 23
#if __STDC_VERSION__ >= 201112L
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

H
Hongze Cheng 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
struct SEncoderNode {
  SEncoderNode* pNext;
  uint8_t*      data;
  uint32_t      size;
  uint32_t      pos;
};

struct SDecoderNode {
  SDecoderNode*  pNext;
  const uint8_t* data;
  uint32_t       size;
  uint32_t       pos;
};

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;
H
mroe  
Hongze Cheng 已提交
45 46
}

H
Hongze Cheng 已提交
47 48
void tEncoderClear(SEncoder* pCoder) {
  for (SCoderMem* pMem = pCoder->mList; pMem; pMem = pCoder->mList) {
H
Hongze Cheng 已提交
49 50 51
    pCoder->mList = pMem->next;
    taosMemoryFree(pMem);
  }
H
Hongze Cheng 已提交
52 53 54 55 56 57 58 59 60 61
  memset(pCoder, 0, sizeof(*pCoder));
}

void tDecoderInit(SDecoder* pDecoder, const uint8_t* data, uint32_t size) {
  pDecoder->data = data;
  pDecoder->size = size;
  pDecoder->pos = 0;
  pDecoder->mList = NULL;
  pDecoder->dStack = NULL;
}
H
Hongze Cheng 已提交
62

H
Hongze Cheng 已提交
63 64 65 66
void tDecoderClear(SDecoder* pCoder) {
  for (SCoderMem* pMem = pCoder->mList; pMem; pMem = pCoder->mList) {
    pCoder->mList = pMem->next;
    taosMemoryFree(pMem);
H
mroe  
Hongze Cheng 已提交
67
  }
H
Hongze Cheng 已提交
68
  memset(pCoder, 0, sizeof(*pCoder));
H
mroe  
Hongze Cheng 已提交
69 70
}

H
Hongze Cheng 已提交
71 72
int32_t tStartEncode(SEncoder* pCoder) {
  SEncoderNode* pNode;
H
refact  
Hongze Cheng 已提交
73

H
mroe  
Hongze Cheng 已提交
74
  if (pCoder->data) {
H
refact  
Hongze Cheng 已提交
75 76
    if (pCoder->size - pCoder->pos < sizeof(int32_t)) return -1;

H
Hongze Cheng 已提交
77
    pNode = tEncoderMalloc(pCoder, sizeof(*pNode));
H
mroe  
Hongze Cheng 已提交
78 79 80 81 82 83
    if (pNode == NULL) return -1;

    pNode->data = pCoder->data;
    pNode->pos = pCoder->pos;
    pNode->size = pCoder->size;

H
more  
Hongze Cheng 已提交
84
    pCoder->data = pNode->data + pNode->pos + sizeof(int32_t);
H
mroe  
Hongze Cheng 已提交
85 86 87
    pCoder->pos = 0;
    pCoder->size = pNode->size - pNode->pos - sizeof(int32_t);

H
Hongze Cheng 已提交
88 89
    pNode->pNext = pCoder->eStack;
    pCoder->eStack = pNode;
H
mroe  
Hongze Cheng 已提交
90 91 92
  } else {
    pCoder->pos += sizeof(int32_t);
  }
H
Hongze Cheng 已提交
93

H
mroe  
Hongze Cheng 已提交
94 95 96
  return 0;
}

H
Hongze Cheng 已提交
97 98 99
void tEndEncode(SEncoder* pCoder) {
  SEncoderNode* pNode;
  int32_t       len;
H
mroe  
Hongze Cheng 已提交
100 101

  if (pCoder->data) {
H
Hongze Cheng 已提交
102
    pNode = pCoder->eStack;
H
mroe  
Hongze Cheng 已提交
103
    ASSERT(pNode);
H
Hongze Cheng 已提交
104
    pCoder->eStack = pNode->pNext;
H
mroe  
Hongze Cheng 已提交
105

H
more  
Hongze Cheng 已提交
106
    len = pCoder->pos;
H
mroe  
Hongze Cheng 已提交
107 108 109

    pCoder->data = pNode->data;
    pCoder->size = pNode->size;
H
more  
Hongze Cheng 已提交
110 111
    pCoder->pos = pNode->pos;

H
Hongze Cheng 已提交
112
    tEncodeI32(pCoder, len);
H
more  
Hongze Cheng 已提交
113

H
Hongze Cheng 已提交
114
    TD_CODER_MOVE_POS(pCoder, len);
H
mroe  
Hongze Cheng 已提交
115 116 117
  }
}

H
Hongze Cheng 已提交
118 119 120
int32_t tStartDecode(SDecoder* pCoder) {
  SDecoderNode* pNode;
  int32_t       len;
H
mroe  
Hongze Cheng 已提交
121

H
more  
Hongze Cheng 已提交
122
  if (tDecodeI32(pCoder, &len) < 0) return -1;
H
refact  
Hongze Cheng 已提交
123

H
Hongze Cheng 已提交
124
  pNode = tDecoderMalloc(pCoder, sizeof(*pNode));
H
mroe  
Hongze Cheng 已提交
125 126 127 128 129 130
  if (pNode == NULL) return -1;

  pNode->data = pCoder->data;
  pNode->pos = pCoder->pos;
  pNode->size = pCoder->size;

H
more  
Hongze Cheng 已提交
131 132
  pCoder->data = pNode->data + pNode->pos;
  pCoder->size = len;
H
mroe  
Hongze Cheng 已提交
133 134
  pCoder->pos = 0;

H
Hongze Cheng 已提交
135 136
  pNode->pNext = pCoder->dStack;
  pCoder->dStack = pNode;
H
mroe  
Hongze Cheng 已提交
137 138 139 140

  return 0;
}

H
Hongze Cheng 已提交
141 142
void tEndDecode(SDecoder* pCoder) {
  SDecoderNode* pNode;
H
Hongze Cheng 已提交
143

H
Hongze Cheng 已提交
144
  pNode = pCoder->dStack;
H
mroe  
Hongze Cheng 已提交
145
  ASSERT(pNode);
H
Hongze Cheng 已提交
146
  pCoder->dStack = pNode->pNext;
H
mroe  
Hongze Cheng 已提交
147 148

  pCoder->data = pNode->data;
H
Hongze Cheng 已提交
149
  pCoder->pos = pCoder->size + pNode->pos;
H
mroe  
Hongze Cheng 已提交
150 151
  pCoder->size = pNode->size;
}