tencode.h 18.2 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

#ifdef __cplusplus
extern "C" {
#endif

#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

H
mor  
Hongze Cheng 已提交
54 55 56 57 58 59 60 61 62 63 64 65
typedef enum { TD_ENCODER, TD_DECODER } td_coder_t;

#define CODER_NODE_FIELDS \
  uint8_t* data;          \
  int32_t  size;          \
  int32_t  pos;

struct SCoderNode {
  TD_SLIST_NODE(SCoderNode);
  CODER_NODE_FIELDS
};

H
Hongze Cheng 已提交
66 67 68 69
typedef struct SCoderMem {
  struct SCoderMem* next;
} SCoderMem;

H
refact  
Hongze Cheng 已提交
70
typedef struct {
H
mor  
Hongze Cheng 已提交
71
  td_coder_t  type;
H
refact  
Hongze Cheng 已提交
72
  td_endian_t endian;
H
Hongze Cheng 已提交
73
  SCoderMem*  mList;
H
mor  
Hongze Cheng 已提交
74 75 76
  CODER_NODE_FIELDS
  TD_SLIST(SCoderNode) stack;
} SCoder;
H
refact  
Hongze Cheng 已提交
77

S
Shengliang Guan 已提交
78 79 80
#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 已提交
81
#define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE))
wafwerar's avatar
wafwerar 已提交
82 83 84 85 86 87 88 89 90 91 92 93
static FORCE_INLINE void* tCoderMalloc(SCoder* pCoder, int32_t size) {
  void*      ptr = NULL;
  SCoderMem* pMem = (SCoderMem*)taosMemoryMalloc(sizeof(SCoderMem*) + size);
  if (pMem) {
    pMem->next = pCoder->mList;
    pCoder->mList = pMem;
    ptr = (void*)&pMem[1];
  }
  return ptr;
}

#define tEncodeSize(E, S, SIZE, RET)                           \
H
Hongze Cheng 已提交
94
  do {                                                         \
H
Hongze Cheng 已提交
95
    SCoder coder = {0};                                        \
H
Hongze Cheng 已提交
96
    RET = 0;                                                   \
H
Hongze Cheng 已提交
97 98 99 100
    tCoderInit(&coder, TD_LITTLE_ENDIAN, NULL, 0, TD_ENCODER); \
    if ((E)(&coder, S) == 0) {                                 \
      SIZE = coder.pos;                                        \
    } else {                                                   \
wafwerar's avatar
wafwerar 已提交
101
      RET = -1;                                                \
H
Hongze Cheng 已提交
102 103
    }                                                          \
    tCoderClear(&coder);                                       \
H
Hongze Cheng 已提交
104
  } while (0)
wafwerar's avatar
wafwerar 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
// #define tEncodeSize(E, S, SIZE)                                \
//   ({                                                           \
//     SCoder coder = {0};                                        \
//     int    ret = 0;                                            \
//     tCoderInit(&coder, TD_LITTLE_ENDIAN, NULL, 0, TD_ENCODER); \
//     if ((E)(&coder, S) == 0) {                                 \
//       SIZE = coder.pos;                                        \
//     } else {                                                   \
//       ret = -1;                                                \
//     }                                                          \
//     tCoderClear(&coder);                                       \
//     ret;                                                       \
//   })
H
Hongze Cheng 已提交
118

H
mor  
Hongze Cheng 已提交
119 120 121 122
void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type);
void tCoderClear(SCoder* pCoder);

/* ------------------------ ENCODE ------------------------ */
S
encode  
Shengliang Guan 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
int32_t        tStartEncode(SCoder* pEncoder);
void           tEndEncode(SCoder* pEncoder);
static int32_t tEncodeU8(SCoder* pEncoder, uint8_t val);
static int32_t tEncodeI8(SCoder* pEncoder, int8_t val);
static int32_t tEncodeU16(SCoder* pEncoder, uint16_t val);
static int32_t tEncodeI16(SCoder* pEncoder, int16_t val);
static int32_t tEncodeU32(SCoder* pEncoder, uint32_t val);
static int32_t tEncodeI32(SCoder* pEncoder, int32_t val);
static int32_t tEncodeU64(SCoder* pEncoder, uint64_t val);
static int32_t tEncodeI64(SCoder* pEncoder, int64_t val);
static int32_t tEncodeU16v(SCoder* pEncoder, uint16_t val);
static int32_t tEncodeI16v(SCoder* pEncoder, int16_t val);
static int32_t tEncodeU32v(SCoder* pEncoder, uint32_t val);
static int32_t tEncodeI32v(SCoder* pEncoder, int32_t val);
static int32_t tEncodeU64v(SCoder* pEncoder, uint64_t val);
static int32_t tEncodeI64v(SCoder* pEncoder, int64_t val);
static int32_t tEncodeFloat(SCoder* pEncoder, float val);
static int32_t tEncodeDouble(SCoder* pEncoder, double val);
static int32_t tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len);
static int32_t tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len);
static int32_t tEncodeCStr(SCoder* pEncoder, const char* val);
H
mor  
Hongze Cheng 已提交
144 145

/* ------------------------ DECODE ------------------------ */
S
encode  
Shengliang Guan 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
int32_t        tStartDecode(SCoder* pDecoder);
void           tEndDecode(SCoder* pDecoder);
static bool    tDecodeIsEnd(SCoder* pCoder);
static int32_t tDecodeU8(SCoder* pDecoder, uint8_t* val);
static int32_t tDecodeI8(SCoder* pDecoder, int8_t* val);
static int32_t tDecodeU16(SCoder* pDecoder, uint16_t* val);
static int32_t tDecodeI16(SCoder* pDecoder, int16_t* val);
static int32_t tDecodeU32(SCoder* pDecoder, uint32_t* val);
static int32_t tDecodeI32(SCoder* pDecoder, int32_t* val);
static int32_t tDecodeU64(SCoder* pDecoder, uint64_t* val);
static int32_t tDecodeI64(SCoder* pDecoder, int64_t* val);
static int32_t tDecodeU16v(SCoder* pDecoder, uint16_t* val);
static int32_t tDecodeI16v(SCoder* pDecoder, int16_t* val);
static int32_t tDecodeU32v(SCoder* pDecoder, uint32_t* val);
static int32_t tDecodeI32v(SCoder* pDecoder, int32_t* val);
static int32_t tDecodeU64v(SCoder* pDecoder, uint64_t* val);
static int32_t tDecodeI64v(SCoder* pDecoder, int64_t* val);
static int32_t tDecodeFloat(SCoder* pDecoder, float* val);
static int32_t tDecodeDouble(SCoder* pDecoder, double* val);
static int32_t tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len);
static int32_t tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len);
static int32_t tDecodeCStr(SCoder* pDecoder, const char** val);
static int32_t tDecodeCStrTo(SCoder* pDecoder, char* val);
H
mor  
Hongze Cheng 已提交
169 170 171 172 173 174 175 176 177 178 179 180

/* ------------------------ IMPL ------------------------ */
#define TD_ENCODE_MACRO(CODER, VAL, TYPE, BITS)                        \
  if ((CODER)->data) {                                                 \
    if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(VAL))) return -1; \
    if (TD_RT_ENDIAN() == (CODER)->endian) {                           \
      tPut(TYPE, TD_CODER_CURRENT(CODER), (VAL));                      \
    } else {                                                           \
      tRPut##BITS(TD_CODER_CURRENT(CODER), &(VAL));                    \
    }                                                                  \
  }                                                                    \
  TD_CODER_MOVE_POS(CODER, sizeof(VAL));                               \
H
more  
Hongze Cheng 已提交
181 182
  return 0;

H
mor  
Hongze Cheng 已提交
183
#define TD_ENCODE_VARIANT_MACRO(CODER, VAL)                       \
H
more  
Hongze Cheng 已提交
184
  while ((VAL) >= ENCODE_LIMIT) {                                 \
H
mor  
Hongze Cheng 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    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 已提交
199 200
  return 0;

H
mor  
Hongze Cheng 已提交
201 202 203 204 205 206 207 208 209
#define TD_DECODE_MACRO(CODER, PVAL, TYPE, BITS)                         \
  if (TD_CODER_CHECK_CAPACITY_FAILED(CODER, sizeof(*(PVAL)))) return -1; \
  if (TD_RT_ENDIAN() == (CODER)->endian) {                               \
    tGet(TYPE, TD_CODER_CURRENT(CODER), *(PVAL));                        \
  } else {                                                               \
    tRGet##BITS(PVAL, TD_CODER_CURRENT(CODER));                          \
  }                                                                      \
                                                                         \
  TD_CODER_MOVE_POS(CODER, sizeof(*(PVAL)));                             \
H
more  
Hongze Cheng 已提交
210 211
  return 0;

H
mor  
Hongze Cheng 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#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));                          \
      TD_CODER_MOVE_POS(pDecoder, 1);                        \
      break;                                                 \
    } else {                                                 \
      *(PVAL) |= (((tval) & (ENCODE_LIMIT - 1)) << (7 * i)); \
      i++;                                                   \
      TD_CODER_MOVE_POS(pDecoder, 1);                        \
    }                                                        \
  }                                                          \
                                                             \
H
more  
Hongze Cheng 已提交
229 230
  return 0;

H
mor  
Hongze Cheng 已提交
231
// 8
S
encode  
Shengliang Guan 已提交
232
static FORCE_INLINE int32_t tEncodeU8(SCoder* pEncoder, uint8_t val) {
H
more  
Hongze Cheng 已提交
233
  if (pEncoder->data) {
H
mor  
Hongze Cheng 已提交
234 235
    if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1;
    tPut(uint8_t, TD_CODER_CURRENT(pEncoder), val);
H
more  
Hongze Cheng 已提交
236 237 238 239 240
  }
  TD_CODER_MOVE_POS(pEncoder, sizeof(val));
  return 0;
}

S
encode  
Shengliang Guan 已提交
241
static FORCE_INLINE int32_t tEncodeI8(SCoder* pEncoder, int8_t val) {
H
more  
Hongze Cheng 已提交
242
  if (pEncoder->data) {
H
mor  
Hongze Cheng 已提交
243 244
    if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1;
    tPut(int8_t, TD_CODER_CURRENT(pEncoder), val);
H
more  
Hongze Cheng 已提交
245 246 247 248 249
  }
  TD_CODER_MOVE_POS(pEncoder, sizeof(val));
  return 0;
}

H
mor  
Hongze Cheng 已提交
250
// 16
S
encode  
Shengliang Guan 已提交
251 252
static FORCE_INLINE int32_t tEncodeU16(SCoder* pEncoder, uint16_t val) { TD_ENCODE_MACRO(pEncoder, val, uint16_t, 16); }
static FORCE_INLINE int32_t tEncodeI16(SCoder* pEncoder, int16_t val) { TD_ENCODE_MACRO(pEncoder, val, int16_t, 16); }
H
mor  
Hongze Cheng 已提交
253
// 32
S
encode  
Shengliang Guan 已提交
254 255
static FORCE_INLINE int32_t tEncodeU32(SCoder* pEncoder, uint32_t val) { TD_ENCODE_MACRO(pEncoder, val, uint32_t, 32); }
static FORCE_INLINE int32_t tEncodeI32(SCoder* pEncoder, int32_t val) { TD_ENCODE_MACRO(pEncoder, val, int32_t, 32); }
H
more  
Hongze Cheng 已提交
256
// 64
S
encode  
Shengliang Guan 已提交
257 258
static FORCE_INLINE int32_t tEncodeU64(SCoder* pEncoder, uint64_t val) { TD_ENCODE_MACRO(pEncoder, val, uint64_t, 64); }
static FORCE_INLINE int32_t tEncodeI64(SCoder* pEncoder, int64_t val) { TD_ENCODE_MACRO(pEncoder, val, int64_t, 64); }
H
more  
Hongze Cheng 已提交
259
// 16v
S
encode  
Shengliang Guan 已提交
260 261
static FORCE_INLINE int32_t tEncodeU16v(SCoder* pEncoder, uint16_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); }
static FORCE_INLINE int32_t tEncodeI16v(SCoder* pEncoder, int16_t val) {
H
more  
Hongze Cheng 已提交
262 263
  return tEncodeU16v(pEncoder, ZIGZAGE(int16_t, val));
}
H
more  
Hongze Cheng 已提交
264
// 32v
S
encode  
Shengliang Guan 已提交
265 266
static FORCE_INLINE int32_t tEncodeU32v(SCoder* pEncoder, uint32_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); }
static FORCE_INLINE int32_t tEncodeI32v(SCoder* pEncoder, int32_t val) {
H
more  
Hongze Cheng 已提交
267 268
  return tEncodeU32v(pEncoder, ZIGZAGE(int32_t, val));
}
H
more  
Hongze Cheng 已提交
269
// 64v
S
encode  
Shengliang Guan 已提交
270 271
static FORCE_INLINE int32_t tEncodeU64v(SCoder* pEncoder, uint64_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); }
static FORCE_INLINE int32_t tEncodeI64v(SCoder* pEncoder, int64_t val) {
H
more  
Hongze Cheng 已提交
272 273
  return tEncodeU64v(pEncoder, ZIGZAGE(int64_t, val));
}
H
more  
Hongze Cheng 已提交
274

S
encode  
Shengliang Guan 已提交
275
static FORCE_INLINE int32_t tEncodeFloat(SCoder* pEncoder, float val) {
H
more  
Hongze Cheng 已提交
276 277 278
  union {
    uint32_t ui;
    float    f;
wafwerar's avatar
wafwerar 已提交
279 280
  } v;
  v.f = val;
H
more  
Hongze Cheng 已提交
281 282

  return tEncodeU32(pEncoder, v.ui);
H
more  
Hongze Cheng 已提交
283 284
}

S
encode  
Shengliang Guan 已提交
285
static FORCE_INLINE int32_t tEncodeDouble(SCoder* pEncoder, double val) {
H
refact  
Hongze Cheng 已提交
286
  union {
H
more  
Hongze Cheng 已提交
287 288
    uint64_t ui;
    double   d;
wafwerar's avatar
wafwerar 已提交
289 290
  } v;
  v.d = val;
H
more  
Hongze Cheng 已提交
291 292

  return tEncodeU64(pEncoder, v.ui);
H
more  
Hongze Cheng 已提交
293 294
}

S
encode  
Shengliang Guan 已提交
295
static FORCE_INLINE int32_t tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len) {
H
more  
Hongze Cheng 已提交
296 297 298
  if (tEncodeU64v(pEncoder, len) < 0) return -1;
  if (pEncoder->data) {
    if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len)) return -1;
H
more  
Hongze Cheng 已提交
299
    memcpy(TD_CODER_CURRENT(pEncoder), val, len);
H
more  
Hongze Cheng 已提交
300 301 302 303 304 305
  }

  TD_CODER_MOVE_POS(pEncoder, len);
  return 0;
}

S
encode  
Shengliang Guan 已提交
306
static FORCE_INLINE int32_t tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len) {
H
more  
Hongze Cheng 已提交
307
  return tEncodeBinary(pEncoder, (void*)val, len + 1);
H
more  
Hongze Cheng 已提交
308 309
}

S
encode  
Shengliang Guan 已提交
310
static FORCE_INLINE int32_t tEncodeCStr(SCoder* pEncoder, const char* val) {
H
more  
Hongze Cheng 已提交
311
  return tEncodeCStrWithLen(pEncoder, val, (uint64_t)strlen(val));
H
more  
Hongze Cheng 已提交
312 313
}

H
more  
Hongze Cheng 已提交
314 315
/* ------------------------ FOR DECODER ------------------------ */
// 8
S
encode  
Shengliang Guan 已提交
316
static FORCE_INLINE int32_t tDecodeU8(SCoder* pDecoder, uint8_t* val) {
H
mor  
Hongze Cheng 已提交
317
  if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1;
H
more  
Hongze Cheng 已提交
318 319 320 321 322
  tGet(uint8_t, TD_CODER_CURRENT(pDecoder), *val);
  TD_CODER_MOVE_POS(pDecoder, sizeof(*val));
  return 0;
}

S
encode  
Shengliang Guan 已提交
323
static FORCE_INLINE int32_t tDecodeI8(SCoder* pDecoder, int8_t* val) {
H
mor  
Hongze Cheng 已提交
324
  if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1;
H
more  
Hongze Cheng 已提交
325 326 327 328 329 330
  tGet(int8_t, TD_CODER_CURRENT(pDecoder), *val);
  TD_CODER_MOVE_POS(pDecoder, sizeof(*val));
  return 0;
}

// 16
S
encode  
Shengliang Guan 已提交
331 332 333 334
static FORCE_INLINE int32_t tDecodeU16(SCoder* pDecoder, uint16_t* val) {
  TD_DECODE_MACRO(pDecoder, val, uint16_t, 16);
}
static FORCE_INLINE int32_t tDecodeI16(SCoder* pDecoder, int16_t* val) { TD_DECODE_MACRO(pDecoder, val, int16_t, 16); }
H
more  
Hongze Cheng 已提交
335
// 32
S
encode  
Shengliang Guan 已提交
336 337 338 339
static FORCE_INLINE int32_t tDecodeU32(SCoder* pDecoder, uint32_t* val) {
  TD_DECODE_MACRO(pDecoder, val, uint32_t, 32);
}
static FORCE_INLINE int32_t tDecodeI32(SCoder* pDecoder, int32_t* val) { TD_DECODE_MACRO(pDecoder, val, int32_t, 32); }
H
more  
Hongze Cheng 已提交
340
// 64
S
encode  
Shengliang Guan 已提交
341 342 343 344
static FORCE_INLINE int32_t tDecodeU64(SCoder* pDecoder, uint64_t* val) {
  TD_DECODE_MACRO(pDecoder, val, uint64_t, 64);
}
static FORCE_INLINE int32_t tDecodeI64(SCoder* pDecoder, int64_t* val) { TD_DECODE_MACRO(pDecoder, val, int64_t, 64); }
H
more  
Hongze Cheng 已提交
345

H
more  
Hongze Cheng 已提交
346
// 16v
S
encode  
Shengliang Guan 已提交
347
static FORCE_INLINE int32_t tDecodeU16v(SCoder* pDecoder, uint16_t* val) {
H
mor  
Hongze Cheng 已提交
348
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint16_t);
H
more  
Hongze Cheng 已提交
349 350
}

S
encode  
Shengliang Guan 已提交
351
static FORCE_INLINE int32_t tDecodeI16v(SCoder* pDecoder, int16_t* val) {
H
Hongze Cheng 已提交
352 353 354 355 356
  uint16_t tval;
  if (tDecodeU16v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int16_t, tval);
H
more  
Hongze Cheng 已提交
357 358 359 360
  return 0;
}

// 32v
S
encode  
Shengliang Guan 已提交
361
static FORCE_INLINE int32_t tDecodeU32v(SCoder* pDecoder, uint32_t* val) {
H
mor  
Hongze Cheng 已提交
362
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint32_t);
H
more  
Hongze Cheng 已提交
363 364
}

S
encode  
Shengliang Guan 已提交
365
static FORCE_INLINE int32_t tDecodeI32v(SCoder* pDecoder, int32_t* val) {
H
Hongze Cheng 已提交
366 367 368 369 370
  uint32_t tval;
  if (tDecodeU32v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int32_t, tval);
H
more  
Hongze Cheng 已提交
371 372 373 374
  return 0;
}

// 64v
S
encode  
Shengliang Guan 已提交
375
static FORCE_INLINE int32_t tDecodeU64v(SCoder* pDecoder, uint64_t* val) {
H
mor  
Hongze Cheng 已提交
376
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint64_t);
H
more  
Hongze Cheng 已提交
377 378
}

S
encode  
Shengliang Guan 已提交
379
static FORCE_INLINE int32_t tDecodeI64v(SCoder* pDecoder, int64_t* val) {
H
Hongze Cheng 已提交
380 381 382 383 384
  uint64_t tval;
  if (tDecodeU64v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int64_t, tval);
H
more  
Hongze Cheng 已提交
385 386 387
  return 0;
}

S
encode  
Shengliang Guan 已提交
388
static FORCE_INLINE int32_t tDecodeFloat(SCoder* pDecoder, float* val) {
H
more  
Hongze Cheng 已提交
389 390 391 392 393 394 395 396 397 398
  union {
    uint32_t ui;
    float    f;
  } v;

  if (tDecodeU32(pDecoder, &(v.ui)) < 0) {
    return -1;
  }

  *val = v.f;
H
more  
Hongze Cheng 已提交
399 400 401
  return 0;
}

S
encode  
Shengliang Guan 已提交
402
static FORCE_INLINE int32_t tDecodeDouble(SCoder* pDecoder, double* val) {
H
more  
Hongze Cheng 已提交
403 404 405 406 407
  union {
    uint64_t ui;
    double   d;
  } v;

H
more  
Hongze Cheng 已提交
408
  if (tDecodeU64(pDecoder, &(v.ui)) < 0) {
H
more  
Hongze Cheng 已提交
409 410 411 412
    return -1;
  }

  *val = v.d;
H
more  
Hongze Cheng 已提交
413 414 415
  return 0;
}

S
encode  
Shengliang Guan 已提交
416
static FORCE_INLINE int32_t tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len) {
H
more  
Hongze Cheng 已提交
417
  if (tDecodeU64v(pDecoder, len) < 0) return -1;
H
more  
Hongze Cheng 已提交
418 419

  if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len)) return -1;
H
Hongze Cheng 已提交
420 421 422
  if (val) {
    *val = (void*)TD_CODER_CURRENT(pDecoder);
  }
H
more  
Hongze Cheng 已提交
423 424 425 426 427

  TD_CODER_MOVE_POS(pDecoder, *len);
  return 0;
}

S
encode  
Shengliang Guan 已提交
428
static FORCE_INLINE int32_t tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) {
H
more  
Hongze Cheng 已提交
429 430
  if (tDecodeBinary(pDecoder, (const void**)val, len) < 0) return -1;
  (*len) -= 1;
H
more  
Hongze Cheng 已提交
431 432 433
  return 0;
}

S
encode  
Shengliang Guan 已提交
434
static FORCE_INLINE int32_t tDecodeCStr(SCoder* pDecoder, const char** val) {
H
more  
Hongze Cheng 已提交
435 436 437 438
  uint64_t len;
  return tDecodeCStrAndLen(pDecoder, val, &len);
}

S
encode  
Shengliang Guan 已提交
439
static int32_t tDecodeCStrTo(SCoder* pDecoder, char* val) {
H
more  
Hongze Cheng 已提交
440 441 442 443
  const char* pStr;
  uint64_t    len;
  if (tDecodeCStrAndLen(pDecoder, &pStr, &len) < 0) return -1;

H
Hongze Cheng 已提交
444
  memcpy(val, pStr, len + 1);
H
more  
Hongze Cheng 已提交
445 446 447
  return 0;
}

L
Liu Jicong 已提交
448 449 450 451
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SCoder* pDecoder, void** val, uint64_t* len) {
  if (tDecodeU64v(pDecoder, len) < 0) return -1;

  if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len)) return -1;
wafwerar's avatar
wafwerar 已提交
452
  *val = taosMemoryMalloc(*len);
L
Liu Jicong 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
  if (*val == NULL) return -1;
  memcpy(*val, TD_CODER_CURRENT(pDecoder), *len);

  TD_CODER_MOVE_POS(pDecoder, *len);
  return 0;
}

static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SCoder* pDecoder, char** val, uint64_t* len) {
  if (tDecodeBinaryAlloc(pDecoder, (void**)val, len) < 0) return -1;
  (*len) -= 1;
  return 0;
}

static FORCE_INLINE int32_t tDecodeCStrAlloc(SCoder* pDecoder, char** val) {
  uint64_t len;
  return tDecodeCStrAndLenAlloc(pDecoder, val, &len);
}

H
more  
Hongze Cheng 已提交
471 472
static FORCE_INLINE bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); }

H
more  
Hongze Cheng 已提交
473 474 475 476
#ifdef __cplusplus
}
#endif

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