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

#include "encode.h"

H
more  
Hongze Cheng 已提交
18 19 20 21 22
#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 已提交
23 24 25 26 27 28
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 已提交
29

H
mor  
Hongze Cheng 已提交
30
  pCoder->type = type;
H
mroe  
Hongze Cheng 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  pCoder->endian = endian;
  pCoder->data = data;
  pCoder->size = size;
  pCoder->pos = 0;
  tFreeListInit(&(pCoder->fl));
  TD_SLIST_INIT(&(pCoder->stack));
}

void tCoderClear(SCoder* pCoder) {
  tFreeListClear(&(pCoder->fl));
  struct SCoderNode* pNode;
  for (;;) {
    pNode = TD_SLIST_HEAD(&(pCoder->stack));
    if (pNode == NULL) break;
    TD_SLIST_POP(&(pCoder->stack));
    free(pNode);
  }
}

H
mor  
Hongze Cheng 已提交
50 51
bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); }

H
mroe  
Hongze Cheng 已提交
52
int tStartEncode(SCoder* pCoder) {
H
refact  
Hongze Cheng 已提交
53 54
  struct SCoderNode* pNode;

H
mroe  
Hongze Cheng 已提交
55
  if (pCoder->data) {
H
refact  
Hongze Cheng 已提交
56 57 58
    if (pCoder->size - pCoder->pos < sizeof(int32_t)) return -1;

    pNode = malloc(sizeof(*pNode));
H
mroe  
Hongze Cheng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    if (pNode == NULL) return -1;

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

    pCoder->data = pNode->data + sizeof(int32_t);
    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;

  if (pCoder->data) {
    pNode = TD_SLIST_HEAD(&(pCoder->stack));
    ASSERT(pNode);
    TD_SLIST_POP(&(pCoder->stack));

    // TODO: tEncodeI32(pNode, pCoder->pos);

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

    free(pNode);
  }
}

int tStartDecode(SCoder* pCoder) {
  int32_t            size;
  struct SCoderNode* pNode;

H
refact  
Hongze Cheng 已提交
98 99
  // TODO: if (tDecodeI32(pCoder, &size) < 0) return -1;

H
mroe  
Hongze Cheng 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  pNode = malloc(sizeof(*pNode));
  if (pNode == NULL) return -1;

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

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

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

  return 0;
}

void tEndDecode(SCoder* pCoder) {
  ASSERT(tDecodeIsEnd(pCoder));

  struct SCoderNode* pNode;

  pNode = TD_SLIST_HEAD(&(pCoder->stack));
  ASSERT(pNode);
  TD_SLIST_POP(&(pCoder->stack));

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

  free(pNode);
}