encode.h 15.3 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_

S
tlist  
Shengliang Guan 已提交
19
#include "tfreelist.h"
H
more  
Hongze Cheng 已提交
20
#include "tcoding.h"
H
more  
Hongze Cheng 已提交
21
#include "tmacro.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
refact  
Hongze Cheng 已提交
66
typedef struct {
H
mor  
Hongze Cheng 已提交
67
  td_coder_t  type;
H
refact  
Hongze Cheng 已提交
68
  td_endian_t endian;
H
mor  
Hongze Cheng 已提交
69 70 71 72
  SFreeList   fl;
  CODER_NODE_FIELDS
  TD_SLIST(SCoderNode) stack;
} SCoder;
H
refact  
Hongze Cheng 已提交
73

H
mor  
Hongze Cheng 已提交
74
#define TD_CODER_POS(CODER) ((CODER)->pos)
H
more  
Hongze Cheng 已提交
75 76
#define TD_CODER_CURRENT(CODER) ((CODER)->data + (CODER)->pos)
#define TD_CODER_MOVE_POS(CODER, MOVE) ((CODER)->pos += (MOVE))
H
refact  
Hongze Cheng 已提交
77
#define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE))
H
more  
Hongze Cheng 已提交
78
#define TCODER_MALLOC(SIZE, CODER) TFL_MALLOC(SIZE, &((CODER)->fl))
H
more  
Hongze Cheng 已提交
79

H
mor  
Hongze Cheng 已提交
80 81 82 83
void tCoderInit(SCoder* pCoder, td_endian_t endian, uint8_t* data, int32_t size, td_coder_t type);
void tCoderClear(SCoder* pCoder);

/* ------------------------ ENCODE ------------------------ */
H
more  
Hongze Cheng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
int        tStartEncode(SCoder* pEncoder);
void       tEndEncode(SCoder* pEncoder);
static int tEncodeU8(SCoder* pEncoder, uint8_t val);
static int tEncodeI8(SCoder* pEncoder, int8_t val);
static int tEncodeU16(SCoder* pEncoder, uint16_t val);
static int tEncodeI16(SCoder* pEncoder, int16_t val);
static int tEncodeU32(SCoder* pEncoder, uint32_t val);
static int tEncodeI32(SCoder* pEncoder, int32_t val);
static int tEncodeU64(SCoder* pEncoder, uint64_t val);
static int tEncodeI64(SCoder* pEncoder, int64_t val);
static int tEncodeU16v(SCoder* pEncoder, uint16_t val);
static int tEncodeI16v(SCoder* pEncoder, int16_t val);
static int tEncodeU32v(SCoder* pEncoder, uint32_t val);
static int tEncodeI32v(SCoder* pEncoder, int32_t val);
static int tEncodeU64v(SCoder* pEncoder, uint64_t val);
static int tEncodeI64v(SCoder* pEncoder, int64_t val);
static int tEncodeFloat(SCoder* pEncoder, float val);
static int tEncodeDouble(SCoder* pEncoder, double val);
static int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len);
static int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len);
static int tEncodeCStr(SCoder* pEncoder, const char* val);
H
mor  
Hongze Cheng 已提交
105 106

/* ------------------------ DECODE ------------------------ */
H
more  
Hongze Cheng 已提交
107 108 109
int         tStartDecode(SCoder* pDecoder);
void        tEndDecode(SCoder* pDecoder);
static bool tDecodeIsEnd(SCoder* pCoder);
H
refact  
Hongze Cheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
static int  tDecodeU8(SCoder* pDecoder, uint8_t* val);
static int  tDecodeI8(SCoder* pDecoder, int8_t* val);
static int  tDecodeU16(SCoder* pDecoder, uint16_t* val);
static int  tDecodeI16(SCoder* pDecoder, int16_t* val);
static int  tDecodeU32(SCoder* pDecoder, uint32_t* val);
static int  tDecodeI32(SCoder* pDecoder, int32_t* val);
static int  tDecodeU64(SCoder* pDecoder, uint64_t* val);
static int  tDecodeI64(SCoder* pDecoder, int64_t* val);
static int  tDecodeU16v(SCoder* pDecoder, uint16_t* val);
static int  tDecodeI16v(SCoder* pDecoder, int16_t* val);
static int  tDecodeU32v(SCoder* pDecoder, uint32_t* val);
static int  tDecodeI32v(SCoder* pDecoder, int32_t* val);
static int  tDecodeU64v(SCoder* pDecoder, uint64_t* val);
static int  tDecodeI64v(SCoder* pDecoder, int64_t* val);
static int  tDecodeFloat(SCoder* pDecoder, float* val);
static int  tDecodeDouble(SCoder* pDecoder, double* val);
H
more  
Hongze Cheng 已提交
126
static int  tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len);
H
more  
Hongze Cheng 已提交
127
static int  tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len);
H
more  
Hongze Cheng 已提交
128
static int  tDecodeCStr(SCoder* pDecoder, const char** val);
H
more  
Hongze Cheng 已提交
129
static int  tDecodeCStrTo(SCoder* pDecoder, char* val);
H
mor  
Hongze Cheng 已提交
130 131 132 133 134 135 136 137 138 139 140 141

/* ------------------------ 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 已提交
142 143
  return 0;

H
mor  
Hongze Cheng 已提交
144
#define TD_ENCODE_VARIANT_MACRO(CODER, VAL)                       \
H
more  
Hongze Cheng 已提交
145
  while ((VAL) >= ENCODE_LIMIT) {                                 \
H
mor  
Hongze Cheng 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159
    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 已提交
160 161
  return 0;

H
mor  
Hongze Cheng 已提交
162 163 164 165 166 167 168 169 170
#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 已提交
171 172
  return 0;

H
mor  
Hongze Cheng 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#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 已提交
190 191
  return 0;

H
mor  
Hongze Cheng 已提交
192 193
// 8
static FORCE_INLINE int tEncodeU8(SCoder* pEncoder, uint8_t val) {
H
more  
Hongze Cheng 已提交
194
  if (pEncoder->data) {
H
mor  
Hongze Cheng 已提交
195 196
    if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1;
    tPut(uint8_t, TD_CODER_CURRENT(pEncoder), val);
H
more  
Hongze Cheng 已提交
197 198 199 200 201
  }
  TD_CODER_MOVE_POS(pEncoder, sizeof(val));
  return 0;
}

H
mor  
Hongze Cheng 已提交
202
static FORCE_INLINE int tEncodeI8(SCoder* pEncoder, int8_t val) {
H
more  
Hongze Cheng 已提交
203
  if (pEncoder->data) {
H
mor  
Hongze Cheng 已提交
204 205
    if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, sizeof(val))) return -1;
    tPut(int8_t, TD_CODER_CURRENT(pEncoder), val);
H
more  
Hongze Cheng 已提交
206 207 208 209 210
  }
  TD_CODER_MOVE_POS(pEncoder, sizeof(val));
  return 0;
}

H
mor  
Hongze Cheng 已提交
211 212 213 214 215 216
// 16
static FORCE_INLINE int tEncodeU16(SCoder* pEncoder, uint16_t val) { TD_ENCODE_MACRO(pEncoder, val, uint16_t, 16); }
static FORCE_INLINE int tEncodeI16(SCoder* pEncoder, int16_t val) { TD_ENCODE_MACRO(pEncoder, val, int16_t, 16); }
// 32
static FORCE_INLINE int tEncodeU32(SCoder* pEncoder, uint32_t val) { TD_ENCODE_MACRO(pEncoder, val, uint32_t, 32); }
static FORCE_INLINE int tEncodeI32(SCoder* pEncoder, int32_t val) { TD_ENCODE_MACRO(pEncoder, val, int32_t, 32); }
H
more  
Hongze Cheng 已提交
217
// 64
H
mor  
Hongze Cheng 已提交
218 219
static FORCE_INLINE int tEncodeU64(SCoder* pEncoder, uint64_t val) { TD_ENCODE_MACRO(pEncoder, val, uint64_t, 64); }
static FORCE_INLINE int tEncodeI64(SCoder* pEncoder, int64_t val) { TD_ENCODE_MACRO(pEncoder, val, int64_t, 64); }
H
more  
Hongze Cheng 已提交
220
// 16v
H
mor  
Hongze Cheng 已提交
221 222
static FORCE_INLINE int tEncodeU16v(SCoder* pEncoder, uint16_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); }
static FORCE_INLINE int tEncodeI16v(SCoder* pEncoder, int16_t val) {
H
more  
Hongze Cheng 已提交
223 224
  return tEncodeU16v(pEncoder, ZIGZAGE(int16_t, val));
}
H
more  
Hongze Cheng 已提交
225
// 32v
H
mor  
Hongze Cheng 已提交
226 227
static FORCE_INLINE int tEncodeU32v(SCoder* pEncoder, uint32_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); }
static FORCE_INLINE int tEncodeI32v(SCoder* pEncoder, int32_t val) {
H
more  
Hongze Cheng 已提交
228 229
  return tEncodeU32v(pEncoder, ZIGZAGE(int32_t, val));
}
H
more  
Hongze Cheng 已提交
230
// 64v
H
mor  
Hongze Cheng 已提交
231 232
static FORCE_INLINE int tEncodeU64v(SCoder* pEncoder, uint64_t val) { TD_ENCODE_VARIANT_MACRO(pEncoder, val); }
static FORCE_INLINE int tEncodeI64v(SCoder* pEncoder, int64_t val) {
H
more  
Hongze Cheng 已提交
233 234
  return tEncodeU64v(pEncoder, ZIGZAGE(int64_t, val));
}
H
more  
Hongze Cheng 已提交
235

H
mor  
Hongze Cheng 已提交
236
static FORCE_INLINE int tEncodeFloat(SCoder* pEncoder, float val) {
H
more  
Hongze Cheng 已提交
237 238 239 240 241 242
  union {
    uint32_t ui;
    float    f;
  } v = {.f = val};

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

H
mor  
Hongze Cheng 已提交
245
static FORCE_INLINE int tEncodeDouble(SCoder* pEncoder, double val) {
H
refact  
Hongze Cheng 已提交
246
  union {
H
more  
Hongze Cheng 已提交
247 248 249 250 251
    uint64_t ui;
    double   d;
  } v = {.d = val};

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

H
more  
Hongze Cheng 已提交
254 255 256 257
static FORCE_INLINE int tEncodeBinary(SCoder* pEncoder, const void* val, uint64_t len) {
  if (tEncodeU64v(pEncoder, len) < 0) return -1;
  if (pEncoder->data) {
    if (TD_CODER_CHECK_CAPACITY_FAILED(pEncoder, len)) return -1;
H
more  
Hongze Cheng 已提交
258
    memcpy(TD_CODER_CURRENT(pEncoder), val, len);
H
more  
Hongze Cheng 已提交
259 260 261 262 263 264
  }

  TD_CODER_MOVE_POS(pEncoder, len);
  return 0;
}

H
more  
Hongze Cheng 已提交
265
static FORCE_INLINE int tEncodeCStrWithLen(SCoder* pEncoder, const char* val, uint64_t len) {
H
more  
Hongze Cheng 已提交
266
  return tEncodeBinary(pEncoder, (void*)val, len + 1);
H
more  
Hongze Cheng 已提交
267 268
}

H
more  
Hongze Cheng 已提交
269
static FORCE_INLINE int tEncodeCStr(SCoder* pEncoder, const char* val) {
H
more  
Hongze Cheng 已提交
270
  return tEncodeCStrWithLen(pEncoder, val, (uint64_t)strlen(val));
H
more  
Hongze Cheng 已提交
271 272
}

H
more  
Hongze Cheng 已提交
273 274
/* ------------------------ FOR DECODER ------------------------ */
// 8
H
mor  
Hongze Cheng 已提交
275 276
static FORCE_INLINE int tDecodeU8(SCoder* pDecoder, uint8_t* val) {
  if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1;
H
more  
Hongze Cheng 已提交
277 278 279 280 281
  tGet(uint8_t, TD_CODER_CURRENT(pDecoder), *val);
  TD_CODER_MOVE_POS(pDecoder, sizeof(*val));
  return 0;
}

H
mor  
Hongze Cheng 已提交
282 283
static FORCE_INLINE int tDecodeI8(SCoder* pDecoder, int8_t* val) {
  if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, sizeof(*val))) return -1;
H
more  
Hongze Cheng 已提交
284 285 286 287 288 289
  tGet(int8_t, TD_CODER_CURRENT(pDecoder), *val);
  TD_CODER_MOVE_POS(pDecoder, sizeof(*val));
  return 0;
}

// 16
H
mor  
Hongze Cheng 已提交
290 291
static FORCE_INLINE int tDecodeU16(SCoder* pDecoder, uint16_t* val) { TD_DECODE_MACRO(pDecoder, val, uint16_t, 16); }
static FORCE_INLINE int tDecodeI16(SCoder* pDecoder, int16_t* val) { TD_DECODE_MACRO(pDecoder, val, int16_t, 16); }
H
more  
Hongze Cheng 已提交
292
// 32
H
mor  
Hongze Cheng 已提交
293 294
static FORCE_INLINE int tDecodeU32(SCoder* pDecoder, uint32_t* val) { TD_DECODE_MACRO(pDecoder, val, uint32_t, 32); }
static FORCE_INLINE int tDecodeI32(SCoder* pDecoder, int32_t* val) { TD_DECODE_MACRO(pDecoder, val, int32_t, 32); }
H
more  
Hongze Cheng 已提交
295
// 64
H
mor  
Hongze Cheng 已提交
296 297
static FORCE_INLINE int tDecodeU64(SCoder* pDecoder, uint64_t* val) { TD_DECODE_MACRO(pDecoder, val, uint64_t, 64); }
static FORCE_INLINE int tDecodeI64(SCoder* pDecoder, int64_t* val) { TD_DECODE_MACRO(pDecoder, val, int64_t, 64); }
H
more  
Hongze Cheng 已提交
298

H
more  
Hongze Cheng 已提交
299
// 16v
H
mor  
Hongze Cheng 已提交
300 301
static FORCE_INLINE int tDecodeU16v(SCoder* pDecoder, uint16_t* val) {
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint16_t);
H
more  
Hongze Cheng 已提交
302 303
}

H
mor  
Hongze Cheng 已提交
304
static FORCE_INLINE int tDecodeI16v(SCoder* pDecoder, int16_t* val) {
H
Hongze Cheng 已提交
305 306 307 308 309
  uint16_t tval;
  if (tDecodeU16v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int16_t, tval);
H
more  
Hongze Cheng 已提交
310 311 312 313
  return 0;
}

// 32v
H
mor  
Hongze Cheng 已提交
314 315
static FORCE_INLINE int tDecodeU32v(SCoder* pDecoder, uint32_t* val) {
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint32_t);
H
more  
Hongze Cheng 已提交
316 317
}

H
mor  
Hongze Cheng 已提交
318
static FORCE_INLINE int tDecodeI32v(SCoder* pDecoder, int32_t* val) {
H
Hongze Cheng 已提交
319 320 321 322 323
  uint32_t tval;
  if (tDecodeU32v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int32_t, tval);
H
more  
Hongze Cheng 已提交
324 325 326 327
  return 0;
}

// 64v
H
mor  
Hongze Cheng 已提交
328 329
static FORCE_INLINE int tDecodeU64v(SCoder* pDecoder, uint64_t* val) {
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint64_t);
H
more  
Hongze Cheng 已提交
330 331
}

H
mor  
Hongze Cheng 已提交
332
static FORCE_INLINE int tDecodeI64v(SCoder* pDecoder, int64_t* val) {
H
Hongze Cheng 已提交
333 334 335 336 337
  uint64_t tval;
  if (tDecodeU64v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int64_t, tval);
H
more  
Hongze Cheng 已提交
338 339 340
  return 0;
}

H
mor  
Hongze Cheng 已提交
341
static FORCE_INLINE int tDecodeFloat(SCoder* pDecoder, float* val) {
H
more  
Hongze Cheng 已提交
342 343 344 345 346 347 348 349 350 351
  union {
    uint32_t ui;
    float    f;
  } v;

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

  *val = v.f;
H
more  
Hongze Cheng 已提交
352 353 354
  return 0;
}

H
mor  
Hongze Cheng 已提交
355
static FORCE_INLINE int tDecodeDouble(SCoder* pDecoder, double* val) {
H
more  
Hongze Cheng 已提交
356 357 358 359 360
  union {
    uint64_t ui;
    double   d;
  } v;

H
more  
Hongze Cheng 已提交
361
  if (tDecodeU64(pDecoder, &(v.ui)) < 0) {
H
more  
Hongze Cheng 已提交
362 363 364 365
    return -1;
  }

  *val = v.d;
H
more  
Hongze Cheng 已提交
366 367 368
  return 0;
}

H
more  
Hongze Cheng 已提交
369
static FORCE_INLINE int tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len) {
H
more  
Hongze Cheng 已提交
370
  if (tDecodeU64v(pDecoder, len) < 0) return -1;
H
more  
Hongze Cheng 已提交
371 372 373 374 375 376 377 378

  if (TD_CODER_CHECK_CAPACITY_FAILED(pDecoder, *len)) return -1;
  *val = (void*)TD_CODER_CURRENT(pDecoder);

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

H
more  
Hongze Cheng 已提交
379
static FORCE_INLINE int tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) {
H
more  
Hongze Cheng 已提交
380 381
  if (tDecodeBinary(pDecoder, (const void**)val, len) < 0) return -1;
  (*len) -= 1;
H
more  
Hongze Cheng 已提交
382 383 384
  return 0;
}

H
more  
Hongze Cheng 已提交
385 386 387 388 389
static FORCE_INLINE int tDecodeCStr(SCoder* pDecoder, const char** val) {
  uint64_t len;
  return tDecodeCStrAndLen(pDecoder, val, &len);
}

H
more  
Hongze Cheng 已提交
390 391 392 393 394
static int tDecodeCStrTo(SCoder* pDecoder, char* val) {
  const char* pStr;
  uint64_t    len;
  if (tDecodeCStrAndLen(pDecoder, &pStr, &len) < 0) return -1;

H
Hongze Cheng 已提交
395
  memcpy(val, pStr, len + 1);
H
more  
Hongze Cheng 已提交
396 397 398
  return 0;
}

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

H
more  
Hongze Cheng 已提交
401 402 403 404 405
#ifdef __cplusplus
}
#endif

#endif /*_TD_UTIL_ENCODE_H_*/