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

#ifndef _TD_UTIL_ENCODE_H_
#define _TD_UTIL_ENCODE_H_

H
more  
Hongze Cheng 已提交
19
#include "tcoding.h"
H
Hongze Cheng 已提交
20 21
#include "tlist.h"
// #include "tfreelist.h"
H
more  
Hongze Cheng 已提交
22 23 24 25 26

#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
typedef struct SEncoderNode SEncoderNode;
typedef struct SDecoderNode SDecoderNode;

typedef struct SCoderMem {
  struct SCoderMem* next;
} SCoderMem;

typedef struct {
  uint8_t*      data;
  uint32_t      size;
  uint32_t      pos;
  SCoderMem*    mList;
  SEncoderNode* eStack;
} SEncoder;

typedef struct {
  const uint8_t* data;
  uint32_t       size;
  uint32_t       pos;
  SCoderMem*     mList;
  SDecoderNode*  dStack;
} SDecoder;

H
more  
Hongze Cheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#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

S
Shengliang Guan 已提交
77 78 79
#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))
H
refact  
Hongze Cheng 已提交
80
#define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE))
wafwerar's avatar
wafwerar 已提交
81

H
Hongze Cheng 已提交
82 83 84 85 86 87 88 89 90 91 92
#define tEncodeSize(E, S, SIZE, RET) \
  do {                               \
    SEncoder coder = {0};            \
    tEncoderInit(&coder, NULL, 0);   \
    if ((E)(&coder, S) == 0) {       \
      SIZE = coder.pos;              \
      RET = 0;                       \
    } else {                         \
      RET = -1;                      \
    }                                \
    tEncoderClear(&coder);           \
H
Hongze Cheng 已提交
93
  } while (0)
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95 96
static void* tEncoderMalloc(SEncoder* pCoder, int32_t size);
static void* tDecoderMalloc(SDecoder* pCoder, int32_t size);
H
mor  
Hongze Cheng 已提交
97 98

/* ------------------------ ENCODE ------------------------ */
H
Hongze Cheng 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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 tEncodeU8(SEncoder* pCoder, uint8_t val);
static int32_t tEncodeI8(SEncoder* pCoder, int8_t val);
static int32_t tEncodeU16(SEncoder* pCoder, uint16_t val);
static int32_t tEncodeI16(SEncoder* pCoder, int16_t val);
static int32_t tEncodeU32(SEncoder* pCoder, uint32_t val);
static int32_t tEncodeI32(SEncoder* pCoder, int32_t val);
static int32_t tEncodeU64(SEncoder* pCoder, uint64_t val);
static int32_t tEncodeI64(SEncoder* pCoder, int64_t val);
static int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val);
static int32_t tEncodeI16v(SEncoder* pCoder, int16_t val);
static int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val);
static int32_t tEncodeI32v(SEncoder* pCoder, int32_t val);
static int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val);
static int32_t tEncodeI64v(SEncoder* pCoder, int64_t val);
static int32_t tEncodeFloat(SEncoder* pCoder, float val);
static int32_t tEncodeDouble(SEncoder* pCoder, double val);
static int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len);
static int32_t tEncodeCStrWithLen(SEncoder* pCoder, const char* val, uint32_t len);
static int32_t tEncodeCStr(SEncoder* pCoder, const char* val);
H
mor  
Hongze Cheng 已提交
122 123

/* ------------------------ DECODE ------------------------ */
H
Hongze Cheng 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
void           tDecoderInit(SDecoder* pCoder, const uint8_t* data, uint32_t size);
void           tDecoderClear(SDecoder* SDecoder);
int32_t        tStartDecode(SDecoder* pCoder);
void           tEndDecode(SDecoder* pCoder);
static bool    tDecodeIsEnd(SDecoder* pCoder);
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);
static int32_t tDecodeI16(SDecoder* pCoder, int16_t* val);
static int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val);
static int32_t tDecodeI32(SDecoder* pCoder, int32_t* val);
static int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val);
static int32_t tDecodeI64(SDecoder* pCoder, int64_t* val);
static int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val);
static int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val);
static int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val);
static int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val);
static int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val);
static int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val);
static int32_t tDecodeFloat(SDecoder* pCoder, float* val);
static int32_t tDecodeDouble(SDecoder* pCoder, double* val);
static int32_t tDecodeBinary(SDecoder* pCoder, const uint8_t** val, uint32_t* len);
static int32_t tDecodeCStrAndLen(SDecoder* pCoder, const char** val, uint32_t* len);
static int32_t tDecodeCStr(SDecoder* pCoder, const char** val);
static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val);
H
mor  
Hongze Cheng 已提交
149 150 151 152 153

/* ------------------------ IMPL ------------------------ */
#define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS)                        \
  if ((CODER)->data) {                                                 \
    if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(VAL))) return -1; \
H
Hongze Cheng 已提交
154
    tPut(TYPE, TD_CODER_CURRENT(CODER), (VAL));                        \
H
mor  
Hongze Cheng 已提交
155 156
  }                                                                    \
  TD_CODER_MOVE_POS(CODER, sizeof(VAL));                               \
H
more  
Hongze Cheng 已提交
157 158
  return 0;

H
mor  
Hongze Cheng 已提交
159
#define TD_ENCODE_VARIANT_MACRO(CODER, VAL)                       \
H
more  
Hongze Cheng 已提交
160
  while ((VAL) >= ENCODE_LIMIT) {                                 \
H
mor  
Hongze Cheng 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    if ((CODER)->data) {                                          \
      if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, 1)) return -1;    \
      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 -1;      \
    TD_CODER_CURRENT(CODER)[0] = (uint8_t)(VAL);                  \
  }                                                               \
  TD_CODER_MOVE_POS(CODER, 1);                                    \
H
more  
Hongze Cheng 已提交
175 176
  return 0;

H
mor  
Hongze Cheng 已提交
177 178
#define TD_DECODE_MACRO(CODER, PVAL, TYPE, BITS)                         \
  if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(*(PVAL)))) return -1; \
H
Hongze Cheng 已提交
179
  tGet(TYPE, TD_CODER_CURRENT(CODER), *(PVAL));                          \
H
mor  
Hongze Cheng 已提交
180
  TD_CODER_MOVE_POS(CODER, sizeof(*(PVAL)));                             \
H
more  
Hongze Cheng 已提交
181 182
  return 0;

H
mor  
Hongze Cheng 已提交
183 184 185 186 187 188 189 190
#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; \
    TYPE tval = TD_CODER_CURRENT(CODER)[0];                  \
    if (tval < ENCODE_LIMIT) {                               \
      *(PVAL) |= (tval << (7 * i));                          \
H
Hongze Cheng 已提交
191
      TD_CODER_MOVE_POS(pCoder, 1);                          \
H
mor  
Hongze Cheng 已提交
192 193 194 195
      break;                                                 \
    } else {                                                 \
      *(PVAL) |= (((tval) & (ENCODE_LIMIT - 1)) << (7 * i)); \
      i++;                                                   \
H
Hongze Cheng 已提交
196
      TD_CODER_MOVE_POS(pCoder, 1);                          \
H
mor  
Hongze Cheng 已提交
197 198 199
    }                                                        \
  }                                                          \
                                                             \
H
more  
Hongze Cheng 已提交
200 201
  return 0;

H
mor  
Hongze Cheng 已提交
202
// 8
H
Hongze Cheng 已提交
203 204 205 206
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;
    tPut(uint8_t, TD_CODER_CURRENT(pCoder), val);
H
more  
Hongze Cheng 已提交
207
  }
H
Hongze Cheng 已提交
208
  TD_CODER_MOVE_POS(pCoder, sizeof(val));
H
more  
Hongze Cheng 已提交
209 210 211
  return 0;
}

H
Hongze Cheng 已提交
212 213 214 215
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;
    tPut(int8_t, TD_CODER_CURRENT(pCoder), val);
H
more  
Hongze Cheng 已提交
216
  }
H
Hongze Cheng 已提交
217
  TD_CODER_MOVE_POS(pCoder, sizeof(val));
H
more  
Hongze Cheng 已提交
218 219 220
  return 0;
}

H
mor  
Hongze Cheng 已提交
221
// 16
H
Hongze Cheng 已提交
222 223
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); }
H
mor  
Hongze Cheng 已提交
224
// 32
H
Hongze Cheng 已提交
225 226
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); }
H
more  
Hongze Cheng 已提交
227
// 64
H
Hongze Cheng 已提交
228 229
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); }
H
more  
Hongze Cheng 已提交
230
// 16v
H
Hongze Cheng 已提交
231 232 233
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));
H
more  
Hongze Cheng 已提交
234
}
H
more  
Hongze Cheng 已提交
235
// 32v
H
Hongze Cheng 已提交
236 237 238
static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) { TD_ENCODE_VARIANT_MACRO(pCoder, val); }
static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) {
  return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val));
H
more  
Hongze Cheng 已提交
239
}
H
more  
Hongze Cheng 已提交
240
// 64v
H
Hongze Cheng 已提交
241 242 243
static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) { TD_ENCODE_VARIANT_MACRO(pCoder, val); }
static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) {
  return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val));
H
more  
Hongze Cheng 已提交
244
}
H
more  
Hongze Cheng 已提交
245

H
Hongze Cheng 已提交
246
static FORCE_INLINE int32_t tEncodeFloat(SEncoder* pCoder, float val) {
H
more  
Hongze Cheng 已提交
247 248 249
  union {
    uint32_t ui;
    float    f;
wafwerar's avatar
wafwerar 已提交
250 251
  } v;
  v.f = val;
H
more  
Hongze Cheng 已提交
252

H
Hongze Cheng 已提交
253
  return tEncodeU32(pCoder, v.ui);
H
more  
Hongze Cheng 已提交
254 255
}

H
Hongze Cheng 已提交
256
static FORCE_INLINE int32_t tEncodeDouble(SEncoder* pCoder, double val) {
H
refact  
Hongze Cheng 已提交
257
  union {
H
more  
Hongze Cheng 已提交
258 259
    uint64_t ui;
    double   d;
wafwerar's avatar
wafwerar 已提交
260 261
  } v;
  v.d = val;
H
more  
Hongze Cheng 已提交
262

H
Hongze Cheng 已提交
263
  return tEncodeU64(pCoder, v.ui);
H
more  
Hongze Cheng 已提交
264 265
}

H
Hongze Cheng 已提交
266 267 268 269 270
static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len) {
  if (tEncodeU32v(pCoder, len) < 0) return -1;
  if (pCoder->data) {
    if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, len)) return -1;
    memcpy(TD_CODER_CURRENT(pCoder), val, len);
H
more  
Hongze Cheng 已提交
271 272
  }

H
Hongze Cheng 已提交
273
  TD_CODER_MOVE_POS(pCoder, len);
H
more  
Hongze Cheng 已提交
274 275 276
  return 0;
}

H
Hongze Cheng 已提交
277 278
static FORCE_INLINE int32_t tEncodeCStrWithLen(SEncoder* pCoder, const char* val, uint32_t len) {
  return tEncodeBinary(pCoder, (void*)val, len + 1);
H
more  
Hongze Cheng 已提交
279 280
}

H
Hongze Cheng 已提交
281 282
static FORCE_INLINE int32_t tEncodeCStr(SEncoder* pCoder, const char* val) {
  return tEncodeCStrWithLen(pCoder, val, (uint32_t)strlen(val));
H
more  
Hongze Cheng 已提交
283 284
}

H
more  
Hongze Cheng 已提交
285 286
/* ------------------------ FOR DECODER ------------------------ */
// 8
H
Hongze Cheng 已提交
287 288 289 290
static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) {
  if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) return -1;
  tGet(uint8_t, TD_CODER_CURRENT(pCoder), *val);
  TD_CODER_MOVE_POS(pCoder, sizeof(*val));
H
more  
Hongze Cheng 已提交
291 292 293
  return 0;
}

H
Hongze Cheng 已提交
294 295 296 297
static FORCE_INLINE int32_t tDecodeI8(SDecoder* pCoder, int8_t* val) {
  if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, sizeof(*val))) return -1;
  tGet(int8_t, TD_CODER_CURRENT(pCoder), *val);
  TD_CODER_MOVE_POS(pCoder, sizeof(*val));
H
more  
Hongze Cheng 已提交
298 299 300 301
  return 0;
}

// 16
H
Hongze Cheng 已提交
302 303
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); }
H
more  
Hongze Cheng 已提交
304
// 32
H
Hongze Cheng 已提交
305 306
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); }
H
more  
Hongze Cheng 已提交
307
// 64
H
Hongze Cheng 已提交
308 309
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); }
H
more  
Hongze Cheng 已提交
310

H
more  
Hongze Cheng 已提交
311
// 16v
H
Hongze Cheng 已提交
312 313
static FORCE_INLINE int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val) {
  TD_DECODE_VARIANT_MACRO(pCoder, val, uint16_t);
H
more  
Hongze Cheng 已提交
314 315
}

H
Hongze Cheng 已提交
316
static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) {
H
Hongze Cheng 已提交
317
  uint16_t tval;
H
Hongze Cheng 已提交
318
  if (tDecodeU16v(pCoder, &tval) < 0) {
H
Hongze Cheng 已提交
319 320 321
    return -1;
  }
  *val = ZIGZAGD(int16_t, tval);
H
more  
Hongze Cheng 已提交
322 323 324 325
  return 0;
}

// 32v
H
Hongze Cheng 已提交
326 327
static FORCE_INLINE int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val) {
  TD_DECODE_VARIANT_MACRO(pCoder, val, uint32_t);
H
more  
Hongze Cheng 已提交
328 329
}

H
Hongze Cheng 已提交
330
static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) {
H
Hongze Cheng 已提交
331
  uint32_t tval;
H
Hongze Cheng 已提交
332
  if (tDecodeU32v(pCoder, &tval) < 0) {
H
Hongze Cheng 已提交
333 334 335
    return -1;
  }
  *val = ZIGZAGD(int32_t, tval);
H
more  
Hongze Cheng 已提交
336 337 338 339
  return 0;
}

// 64v
H
Hongze Cheng 已提交
340 341
static FORCE_INLINE int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val) {
  TD_DECODE_VARIANT_MACRO(pCoder, val, uint64_t);
H
more  
Hongze Cheng 已提交
342 343
}

H
Hongze Cheng 已提交
344
static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) {
H
Hongze Cheng 已提交
345
  uint64_t tval;
H
Hongze Cheng 已提交
346
  if (tDecodeU64v(pCoder, &tval) < 0) {
H
Hongze Cheng 已提交
347 348 349
    return -1;
  }
  *val = ZIGZAGD(int64_t, tval);
H
more  
Hongze Cheng 已提交
350 351 352
  return 0;
}

H
Hongze Cheng 已提交
353
static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) {
H
more  
Hongze Cheng 已提交
354 355 356 357 358
  union {
    uint32_t ui;
    float    f;
  } v;

H
Hongze Cheng 已提交
359
  if (tDecodeU32(pCoder, &(v.ui)) < 0) {
H
more  
Hongze Cheng 已提交
360 361 362 363
    return -1;
  }

  *val = v.f;
H
more  
Hongze Cheng 已提交
364 365 366
  return 0;
}

H
Hongze Cheng 已提交
367
static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) {
H
more  
Hongze Cheng 已提交
368 369 370 371 372
  union {
    uint64_t ui;
    double   d;
  } v;

H
Hongze Cheng 已提交
373
  if (tDecodeU64(pCoder, &(v.ui)) < 0) {
H
more  
Hongze Cheng 已提交
374 375 376 377
    return -1;
  }

  *val = v.d;
H
more  
Hongze Cheng 已提交
378 379 380
  return 0;
}

H
Hongze Cheng 已提交
381 382
static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, const uint8_t** val, uint32_t* len) {
  if (tDecodeU32v(pCoder, len) < 0) return -1;
H
more  
Hongze Cheng 已提交
383

H
Hongze Cheng 已提交
384
  if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, *len)) return -1;
H
Hongze Cheng 已提交
385
  if (val) {
H
Hongze Cheng 已提交
386
    *val = (void*)TD_CODER_CURRENT(pCoder);
H
Hongze Cheng 已提交
387
  }
H
more  
Hongze Cheng 已提交
388

H
Hongze Cheng 已提交
389
  TD_CODER_MOVE_POS(pCoder, *len);
H
more  
Hongze Cheng 已提交
390 391 392
  return 0;
}

H
Hongze Cheng 已提交
393 394
static FORCE_INLINE int32_t tDecodeCStrAndLen(SDecoder* pCoder, const char** val, uint32_t* len) {
  if (tDecodeBinary(pCoder, (const uint8_t**)val, len) < 0) return -1;
H
more  
Hongze Cheng 已提交
395
  (*len) -= 1;
H
more  
Hongze Cheng 已提交
396 397 398
  return 0;
}

H
Hongze Cheng 已提交
399
static FORCE_INLINE int32_t tDecodeCStr(SDecoder* pCoder, const char** val) {
H
Hongze Cheng 已提交
400
  uint32_t len;
H
Hongze Cheng 已提交
401
  return tDecodeCStrAndLen(pCoder, val, &len);
H
more  
Hongze Cheng 已提交
402 403
}

H
Hongze Cheng 已提交
404
static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) {
H
more  
Hongze Cheng 已提交
405
  const char* pStr;
H
Hongze Cheng 已提交
406
  uint32_t    len;
H
Hongze Cheng 已提交
407
  if (tDecodeCStrAndLen(pCoder, &pStr, &len) < 0) return -1;
H
more  
Hongze Cheng 已提交
408

H
Hongze Cheng 已提交
409
  memcpy(val, pStr, len + 1);
H
more  
Hongze Cheng 已提交
410 411 412
  return 0;
}

H
Hongze Cheng 已提交
413 414
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) {
  if (tDecodeU64v(pCoder, len) < 0) return -1;
L
Liu Jicong 已提交
415

H
Hongze Cheng 已提交
416
  if (TD_CODER_CHECK_CAPACITY_FAILED(pCoder, *len)) return -1;
wafwerar's avatar
wafwerar 已提交
417
  *val = taosMemoryMalloc(*len);
L
Liu Jicong 已提交
418
  if (*val == NULL) return -1;
H
Hongze Cheng 已提交
419
  memcpy(*val, TD_CODER_CURRENT(pCoder), *len);
L
Liu Jicong 已提交
420

H
Hongze Cheng 已提交
421
  TD_CODER_MOVE_POS(pCoder, *len);
L
Liu Jicong 已提交
422 423 424
  return 0;
}

H
Hongze Cheng 已提交
425 426
static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SDecoder* pCoder, char** val, uint64_t* len) {
  if (tDecodeBinaryAlloc(pCoder, (void**)val, len) < 0) return -1;
L
Liu Jicong 已提交
427 428 429 430
  (*len) -= 1;
  return 0;
}

H
Hongze Cheng 已提交
431
static FORCE_INLINE int32_t tDecodeCStrAlloc(SDecoder* pCoder, char** val) {
L
Liu Jicong 已提交
432
  uint64_t len;
H
Hongze Cheng 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446
  return tDecodeCStrAndLenAlloc(pCoder, val, &len);
}

static FORCE_INLINE bool tDecodeIsEnd(SDecoder* pCoder) { return (pCoder->size == pCoder->pos); }

static FORCE_INLINE void* tEncoderMalloc(SEncoder* pCoder, int32_t size) {
  void*      p = NULL;
  SCoderMem* pMem = (SCoderMem*)taosMemoryMalloc(sizeof(*pMem) + size);
  if (pMem) {
    pMem->next = pCoder->mList;
    pCoder->mList = pMem;
    p = (void*)&pMem[1];
  }
  return p;
L
Liu Jicong 已提交
447 448
}

H
Hongze Cheng 已提交
449 450 451 452 453 454 455 456 457 458
static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) {
  void*      p = NULL;
  SCoderMem* pMem = (SCoderMem*)taosMemoryMalloc(sizeof(*pMem) + size);
  if (pMem) {
    pMem->next = pCoder->mList;
    pCoder->mList = pMem;
    p = (void*)&pMem[1];
  }
  return p;
}
H
more  
Hongze Cheng 已提交
459

H
more  
Hongze Cheng 已提交
460 461 462 463
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
464
#endif /*_TD_UTIL_ENCODE_H_*/