tencode.h 16.4 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"
S
Shengliang Guan 已提交
20
#include "tfreelist.h"
H
more  
Hongze Cheng 已提交
21 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

#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 已提交
53 54 55 56 57 58 59 60 61 62 63 64
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 已提交
65
typedef struct {
H
mor  
Hongze Cheng 已提交
66
  td_coder_t  type;
H
refact  
Hongze Cheng 已提交
67
  td_endian_t endian;
H
mor  
Hongze Cheng 已提交
68 69 70 71
  SFreeList   fl;
  CODER_NODE_FIELDS
  TD_SLIST(SCoderNode) stack;
} SCoder;
H
refact  
Hongze Cheng 已提交
72

S
Shengliang Guan 已提交
73 74 75
#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 已提交
76
#define TD_CODER_CHECK_CAPACITY_FAILED(CODER, EXPSIZE) (((CODER)->size - (CODER)->pos) < (EXPSIZE))
77
#define TCODER_MALLOC(PTR, TYPE, SIZE, CODER)          TFL_MALLOC(PTR, TYPE, SIZE, &((CODER)->fl))
H
more  
Hongze Cheng 已提交
78

H
mor  
Hongze Cheng 已提交
79 80 81 82
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 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
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 已提交
104 105

/* ------------------------ DECODE ------------------------ */
S
encode  
Shengliang Guan 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
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 已提交
129 130 131 132 133 134 135 136 137 138 139 140

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

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

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

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

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

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

H
mor  
Hongze Cheng 已提交
210
// 16
S
encode  
Shengliang Guan 已提交
211 212
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 已提交
213
// 32
S
encode  
Shengliang Guan 已提交
214 215
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 已提交
216
// 64
S
encode  
Shengliang Guan 已提交
217 218
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 已提交
219
// 16v
S
encode  
Shengliang Guan 已提交
220 221
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 已提交
222 223
  return tEncodeU16v(pEncoder, ZIGZAGE(int16_t, val));
}
H
more  
Hongze Cheng 已提交
224
// 32v
S
encode  
Shengliang Guan 已提交
225 226
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 已提交
227 228
  return tEncodeU32v(pEncoder, ZIGZAGE(int32_t, val));
}
H
more  
Hongze Cheng 已提交
229
// 64v
S
encode  
Shengliang Guan 已提交
230 231
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 已提交
232 233
  return tEncodeU64v(pEncoder, ZIGZAGE(int64_t, val));
}
H
more  
Hongze Cheng 已提交
234

S
encode  
Shengliang Guan 已提交
235
static FORCE_INLINE int32_t tEncodeFloat(SCoder* pEncoder, float val) {
H
more  
Hongze Cheng 已提交
236 237 238 239 240 241
  union {
    uint32_t ui;
    float    f;
  } v = {.f = val};

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

S
encode  
Shengliang Guan 已提交
244
static FORCE_INLINE int32_t tEncodeDouble(SCoder* pEncoder, double val) {
H
refact  
Hongze Cheng 已提交
245
  union {
H
more  
Hongze Cheng 已提交
246 247 248 249 250
    uint64_t ui;
    double   d;
  } v = {.d = val};

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

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

  TD_CODER_MOVE_POS(pEncoder, len);
  return 0;
}

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

S
encode  
Shengliang Guan 已提交
268
static FORCE_INLINE int32_t tEncodeCStr(SCoder* pEncoder, const char* val) {
H
more  
Hongze Cheng 已提交
269
  return tEncodeCStrWithLen(pEncoder, val, (uint64_t)strlen(val));
H
more  
Hongze Cheng 已提交
270 271
}

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

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

// 16
S
encode  
Shengliang Guan 已提交
289 290 291 292
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 已提交
293
// 32
S
encode  
Shengliang Guan 已提交
294 295 296 297
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 已提交
298
// 64
S
encode  
Shengliang Guan 已提交
299 300 301 302
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 已提交
303

H
more  
Hongze Cheng 已提交
304
// 16v
S
encode  
Shengliang Guan 已提交
305
static FORCE_INLINE int32_t tDecodeU16v(SCoder* pDecoder, uint16_t* val) {
H
mor  
Hongze Cheng 已提交
306
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint16_t);
H
more  
Hongze Cheng 已提交
307 308
}

S
encode  
Shengliang Guan 已提交
309
static FORCE_INLINE int32_t tDecodeI16v(SCoder* pDecoder, int16_t* val) {
H
Hongze Cheng 已提交
310 311 312 313 314
  uint16_t tval;
  if (tDecodeU16v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int16_t, tval);
H
more  
Hongze Cheng 已提交
315 316 317 318
  return 0;
}

// 32v
S
encode  
Shengliang Guan 已提交
319
static FORCE_INLINE int32_t tDecodeU32v(SCoder* pDecoder, uint32_t* val) {
H
mor  
Hongze Cheng 已提交
320
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint32_t);
H
more  
Hongze Cheng 已提交
321 322
}

S
encode  
Shengliang Guan 已提交
323
static FORCE_INLINE int32_t tDecodeI32v(SCoder* pDecoder, int32_t* val) {
H
Hongze Cheng 已提交
324 325 326 327 328
  uint32_t tval;
  if (tDecodeU32v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int32_t, tval);
H
more  
Hongze Cheng 已提交
329 330 331 332
  return 0;
}

// 64v
S
encode  
Shengliang Guan 已提交
333
static FORCE_INLINE int32_t tDecodeU64v(SCoder* pDecoder, uint64_t* val) {
H
mor  
Hongze Cheng 已提交
334
  TD_DECODE_VARIANT_MACRO(pDecoder, val, uint64_t);
H
more  
Hongze Cheng 已提交
335 336
}

S
encode  
Shengliang Guan 已提交
337
static FORCE_INLINE int32_t tDecodeI64v(SCoder* pDecoder, int64_t* val) {
H
Hongze Cheng 已提交
338 339 340 341 342
  uint64_t tval;
  if (tDecodeU64v(pDecoder, &tval) < 0) {
    return -1;
  }
  *val = ZIGZAGD(int64_t, tval);
H
more  
Hongze Cheng 已提交
343 344 345
  return 0;
}

S
encode  
Shengliang Guan 已提交
346
static FORCE_INLINE int32_t tDecodeFloat(SCoder* pDecoder, float* val) {
H
more  
Hongze Cheng 已提交
347 348 349 350 351 352 353 354 355 356
  union {
    uint32_t ui;
    float    f;
  } v;

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

  *val = v.f;
H
more  
Hongze Cheng 已提交
357 358 359
  return 0;
}

S
encode  
Shengliang Guan 已提交
360
static FORCE_INLINE int32_t tDecodeDouble(SCoder* pDecoder, double* val) {
H
more  
Hongze Cheng 已提交
361 362 363 364 365
  union {
    uint64_t ui;
    double   d;
  } v;

H
more  
Hongze Cheng 已提交
366
  if (tDecodeU64(pDecoder, &(v.ui)) < 0) {
H
more  
Hongze Cheng 已提交
367 368 369 370
    return -1;
  }

  *val = v.d;
H
more  
Hongze Cheng 已提交
371 372 373
  return 0;
}

S
encode  
Shengliang Guan 已提交
374
static FORCE_INLINE int32_t tDecodeBinary(SCoder* pDecoder, const void** val, uint64_t* len) {
H
more  
Hongze Cheng 已提交
375
  if (tDecodeU64v(pDecoder, len) < 0) return -1;
H
more  
Hongze Cheng 已提交
376 377 378 379 380 381 382 383

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

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

S
encode  
Shengliang Guan 已提交
384
static FORCE_INLINE int32_t tDecodeCStrAndLen(SCoder* pDecoder, const char** val, uint64_t* len) {
H
more  
Hongze Cheng 已提交
385 386
  if (tDecodeBinary(pDecoder, (const void**)val, len) < 0) return -1;
  (*len) -= 1;
H
more  
Hongze Cheng 已提交
387 388 389
  return 0;
}

S
encode  
Shengliang Guan 已提交
390
static FORCE_INLINE int32_t tDecodeCStr(SCoder* pDecoder, const char** val) {
H
more  
Hongze Cheng 已提交
391 392 393 394
  uint64_t len;
  return tDecodeCStrAndLen(pDecoder, val, &len);
}

S
encode  
Shengliang Guan 已提交
395
static int32_t tDecodeCStrTo(SCoder* pDecoder, char* val) {
H
more  
Hongze Cheng 已提交
396 397 398 399
  const char* pStr;
  uint64_t    len;
  if (tDecodeCStrAndLen(pDecoder, &pStr, &len) < 0) return -1;

H
Hongze Cheng 已提交
400
  memcpy(val, pStr, len + 1);
H
more  
Hongze Cheng 已提交
401 402 403
  return 0;
}

L
Liu Jicong 已提交
404 405 406 407
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 已提交
408
  *val = taosMemoryMalloc(*len);
L
Liu Jicong 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
  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 已提交
427 428
static FORCE_INLINE bool tDecodeIsEnd(SCoder* pCoder) { return (pCoder->size == pCoder->pos); }

H
more  
Hongze Cheng 已提交
429 430 431 432
#ifdef __cplusplus
}
#endif

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