tencode.c 3.5 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
mor  
Hongze Cheng 已提交
24 25 26 27 28 29
void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type) {
  if (type == TD_ENCODER) {
    if (data == NULL) size = 0;
  } else {
    ASSERT(data && size > 0);
  }
H
mroe  
Hongze Cheng 已提交
30

H
mor  
Hongze Cheng 已提交
31
  pCoder->type = type;
H
mroe  
Hongze Cheng 已提交
32 33 34 35
  pCoder->endian = endian;
  pCoder->data = data;
  pCoder->size = size;
  pCoder->pos = 0;
H
Hongze Cheng 已提交
36
  pCoder->mList = NULL;
H
mroe  
Hongze Cheng 已提交
37 38 39 40
  TD_SLIST_INIT(&(pCoder->stack));
}

void tCoderClear(SCoder* pCoder) {
H
Hongze Cheng 已提交
41 42 43 44 45 46 47 48
  SCoderMem* pMem;

  // clear memory
  for (pMem = pCoder->mList; pMem; pMem = pCoder->mList) {
    pCoder->mList = pMem->next;
    taosMemoryFree(pMem);
  }

H
mroe  
Hongze Cheng 已提交
49 50 51 52 53
  struct SCoderNode* pNode;
  for (;;) {
    pNode = TD_SLIST_HEAD(&(pCoder->stack));
    if (pNode == NULL) break;
    TD_SLIST_POP(&(pCoder->stack));
wafwerar's avatar
wafwerar 已提交
54
    taosMemoryFree(pNode);
H
mroe  
Hongze Cheng 已提交
55 56 57
  }
}

S
encode  
Shengliang Guan 已提交
58
int32_t tStartEncode(SCoder* pCoder) {
H
refact  
Hongze Cheng 已提交
59 60
  struct SCoderNode* pNode;

H
Hongze Cheng 已提交
61
  ASSERT(pCoder->type == TD_ENCODER);
H
mroe  
Hongze Cheng 已提交
62
  if (pCoder->data) {
H
refact  
Hongze Cheng 已提交
63 64
    if (pCoder->size - pCoder->pos < sizeof(int32_t)) return -1;

wafwerar's avatar
wafwerar 已提交
65
    pNode = taosMemoryMalloc(sizeof(*pNode));
H
mroe  
Hongze Cheng 已提交
66 67 68 69 70 71
    if (pNode == NULL) return -1;

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

H
more  
Hongze Cheng 已提交
72
    pCoder->data = pNode->data + pNode->pos + sizeof(int32_t);
H
mroe  
Hongze Cheng 已提交
73 74 75 76 77 78 79 80 81 82 83 84
    pCoder->pos = 0;
    pCoder->size = pNode->size - pNode->pos - sizeof(int32_t);

    TD_SLIST_PUSH(&(pCoder->stack), pNode);
  } else {
    pCoder->pos += sizeof(int32_t);
  }
  return 0;
}

void tEndEncode(SCoder* pCoder) {
  struct SCoderNode* pNode;
H
more  
Hongze Cheng 已提交
85
  int32_t            len;
H
mroe  
Hongze Cheng 已提交
86

H
Hongze Cheng 已提交
87
  ASSERT(pCoder->type == TD_ENCODER);
H
mroe  
Hongze Cheng 已提交
88 89 90 91 92
  if (pCoder->data) {
    pNode = TD_SLIST_HEAD(&(pCoder->stack));
    ASSERT(pNode);
    TD_SLIST_POP(&(pCoder->stack));

H
more  
Hongze Cheng 已提交
93
    len = pCoder->pos;
H
mroe  
Hongze Cheng 已提交
94 95 96

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

H
Hongze Cheng 已提交
99
    tEncodeI32(pCoder, len);
H
more  
Hongze Cheng 已提交
100

H
Hongze Cheng 已提交
101
    TD_CODER_MOVE_POS(pCoder, len);
H
mroe  
Hongze Cheng 已提交
102

wafwerar's avatar
wafwerar 已提交
103
    taosMemoryFree(pNode);
H
mroe  
Hongze Cheng 已提交
104 105 106
  }
}

S
encode  
Shengliang Guan 已提交
107
int32_t tStartDecode(SCoder* pCoder) {
H
more  
Hongze Cheng 已提交
108
  int32_t            len;
H
mroe  
Hongze Cheng 已提交
109 110
  struct SCoderNode* pNode;

H
Hongze Cheng 已提交
111
  ASSERT(pCoder->type == TD_DECODER);
H
more  
Hongze Cheng 已提交
112
  if (tDecodeI32(pCoder, &len) < 0) return -1;
H
refact  
Hongze Cheng 已提交
113

wafwerar's avatar
wafwerar 已提交
114
  pNode = taosMemoryMalloc(sizeof(*pNode));
H
mroe  
Hongze Cheng 已提交
115 116 117 118 119 120
  if (pNode == NULL) return -1;

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

H
more  
Hongze Cheng 已提交
121 122
  pCoder->data = pNode->data + pNode->pos;
  pCoder->size = len;
H
mroe  
Hongze Cheng 已提交
123 124 125 126 127 128 129 130 131 132
  pCoder->pos = 0;

  TD_SLIST_PUSH(&(pCoder->stack), pNode);

  return 0;
}

void tEndDecode(SCoder* pCoder) {
  struct SCoderNode* pNode;

H
Hongze Cheng 已提交
133 134
  ASSERT(pCoder->type == TD_DECODER);

H
mroe  
Hongze Cheng 已提交
135 136 137 138 139
  pNode = TD_SLIST_HEAD(&(pCoder->stack));
  ASSERT(pNode);
  TD_SLIST_POP(&(pCoder->stack));

  pCoder->data = pNode->data;
H
Hongze Cheng 已提交
140
  pCoder->pos = pCoder->size + pNode->pos;
H
mroe  
Hongze Cheng 已提交
141 142
  pCoder->size = pNode->size;

wafwerar's avatar
wafwerar 已提交
143
  taosMemoryFree(pNode);
H
mroe  
Hongze Cheng 已提交
144
}