• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3780

31 Mar 2025 08:30AM UTC coverage: 11.99% (-22.0%) from 34.001%
#3780

push

travis-ci

happyguoxy
test:add case 2

40223 of 491333 branches covered (8.19%)

Branch coverage included in aggregate %.

70904 of 435487 relevant lines covered (16.28%)

1596.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

18.75
/include/util/tencode.h
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#ifndef _TD_UTIL_ENCODE_H_
17
#define _TD_UTIL_ENCODE_H_
18

19
#include "tcoding.h"
20
#include "tlist.h"
21
#include "tutil.h"
22

23
#ifdef __cplusplus
24
extern "C" {
25
#endif
26

27
typedef struct SEncoderNode SEncoderNode;
28
typedef struct SDecoderNode SDecoderNode;
29

30
typedef struct SCoderMem {
31
  struct SCoderMem* next;
32
} SCoderMem;
33

34
typedef struct {
35
  uint8_t*      data;
36
  uint32_t      size;
37
  uint32_t      pos;
38
  SCoderMem*    mList;
39
  SEncoderNode* eStack;
40
} SEncoder;
41

42
typedef struct {
43
  uint8_t*      data;
44
  uint32_t      size;
45
  uint32_t      pos;
46
  SCoderMem*    mList;
47
  SDecoderNode* dStack;
48
} SDecoder;
49

50
#define TD_CODER_CURRENT(CODER)         ((CODER)->data + (CODER)->pos)
51
#define TD_CODER_REMAIN_CAPACITY(CODER) ((CODER)->size - (CODER)->pos)
52

53
#define tEncodeSize(E, S, SIZE, RET) \
54
  do {                               \
55
    SEncoder coder = {0};            \
56
    tEncoderInit(&coder, NULL, 0);   \
57
    if ((E)(&coder, S) >= 0) {       \
58
      SIZE = coder.pos;              \
59
      RET = 0;                       \
60
    } else {                         \
61
      RET = -1;                      \
62
    }                                \
63
    tEncoderClear(&coder);           \
64
  } while (0);
65

66
static void* tEncoderMalloc(SEncoder* pCoder, int32_t size);
67
static void* tDecoderMalloc(SDecoder* pCoder, int32_t size);
68

69
/* ------------------------ ENCODE ------------------------ */
70
void           tEncoderInit(SEncoder* pCoder, uint8_t* data, uint32_t size);
71
void           tEncoderClear(SEncoder* pCoder);
72
int32_t        tStartEncode(SEncoder* pCoder);
73
void           tEndEncode(SEncoder* pCoder);
74
static int32_t tEncodeFixed(SEncoder* pCoder, const void* val, uint32_t size);
75
static int32_t tEncodeU8(SEncoder* pCoder, uint8_t val);
76
static int32_t tEncodeI8(SEncoder* pCoder, int8_t val);
77
static int32_t tEncodeU16(SEncoder* pCoder, uint16_t val);
78
static int32_t tEncodeI16(SEncoder* pCoder, int16_t val);
79
static int32_t tEncodeU32(SEncoder* pCoder, uint32_t val);
80
static int32_t tEncodeI32(SEncoder* pCoder, int32_t val);
81
static int32_t tEncodeU64(SEncoder* pCoder, uint64_t val);
82
static int32_t tEncodeI64(SEncoder* pCoder, int64_t val);
83
static int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val);
84
static int32_t tEncodeI16v(SEncoder* pCoder, int16_t val);
85
static int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val);
86
static int32_t tEncodeI32v(SEncoder* pCoder, int32_t val);
87
static int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val);
88
static int32_t tEncodeI64v(SEncoder* pCoder, int64_t val);
89
static int32_t tEncodeFloat(SEncoder* pCoder, float val);
90
static int32_t tEncodeDouble(SEncoder* pCoder, double val);
91
static int32_t tEncodeBool(SEncoder* pCoder, bool val);
92
static int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len);
93
static int32_t tEncodeBinaryEx(SEncoder* pCoder, const uint8_t* val, uint32_t len);
94
static int32_t tEncodeCStrWithLen(SEncoder* pCoder, const char* val, uint32_t len);
95
static int32_t tEncodeCStr(SEncoder* pCoder, const char* val);
96

97
/* ------------------------ DECODE ------------------------ */
98
void           tDecoderInit(SDecoder* pCoder, uint8_t* data, uint32_t size);
99
void           tDecoderClear(SDecoder* SDecoder);
100
int32_t        tStartDecode(SDecoder* pCoder);
101
void           tEndDecode(SDecoder* pCoder);
102
static bool    tDecodeIsEnd(SDecoder* pCoder);
103
static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size);
104
static int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val);
105
static int32_t tDecodeI8(SDecoder* pCoder, int8_t* val);
106
static int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val);
107
static int32_t tDecodeI16(SDecoder* pCoder, int16_t* val);
108
static int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val);
109
static int32_t tDecodeI32(SDecoder* pCoder, int32_t* val);
110
static int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val);
111
static int32_t tDecodeI64(SDecoder* pCoder, int64_t* val);
112
static int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val);
113
static int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val);
114
static int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val);
115
static int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val);
116
static int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val);
117
static int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val);
118
static int32_t tDecodeFloat(SDecoder* pCoder, float* val);
119
static int32_t tDecodeDouble(SDecoder* pCoder, double* val);
120
static int32_t tDecodeBool(SDecoder* pCoder, bool* val);
121
static int32_t tDecodeBinaryWithSize(SDecoder* pCoder, uint32_t size, uint8_t** val);
122
static int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint32_t* len);
123
static int32_t tDecodeCStrAndLen(SDecoder* pCoder, char** val, uint32_t* len);
124
static int32_t tDecodeCStr(SDecoder* pCoder, char** val);
125
static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val);
126

127
/* ------------------------ IMPL ------------------------ */
128
static FORCE_INLINE int32_t tEncodeFixed(SEncoder* pCoder, const void* val, uint32_t size) {
129
  if (pCoder->data) {
17,420!
130
    if (pCoder->pos + size > pCoder->size) {
14,377!
131
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
132
    }
133
    TAOS_MEMCPY(pCoder->data + pCoder->pos, val, size);
14,377✔
134
  }
135

136
  pCoder->pos += size;
26,754✔
137
  return 0;
26,754✔
138
}
139

140
static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
9,246✔
141
static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
3,956✔
142
static FORCE_INLINE int32_t tEncodeU16(SEncoder* pCoder, uint16_t val) {
143
  return tEncodeFixed(pCoder, &val, sizeof(val));
662✔
144
}
145
static FORCE_INLINE int32_t tEncodeI16(SEncoder* pCoder, int16_t val) {
146
  return tEncodeFixed(pCoder, &val, sizeof(val));
88✔
147
}
148
static FORCE_INLINE int32_t tEncodeU32(SEncoder* pCoder, uint32_t val) {
149
  return tEncodeFixed(pCoder, &val, sizeof(val));
730✔
150
}
151
static FORCE_INLINE int32_t tEncodeI32(SEncoder* pCoder, int32_t val) {
152
  return tEncodeFixed(pCoder, &val, sizeof(val));
8,584✔
153
}
154
static FORCE_INLINE int32_t tEncodeU64(SEncoder* pCoder, uint64_t val) {
155
  return tEncodeFixed(pCoder, &val, sizeof(val));
866✔
156
}
157
static FORCE_INLINE int32_t tEncodeI64(SEncoder* pCoder, int64_t val) {
158
  return tEncodeFixed(pCoder, &val, sizeof(val));
2,606✔
159
}
160
static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) {
161
  while (val >= ENCODE_LIMIT) {
1,388!
162
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
×
163
    val >>= 7;
×
164
  }
165
  return tEncodeU8(pCoder, val);
2,776!
166
}
167
static FORCE_INLINE int32_t tEncodeI16v(SEncoder* pCoder, int16_t val) {
168
  return tEncodeU16v(pCoder, ZIGZAGE(int16_t, val));
2,776✔
169
}
170
static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) {
171
  while (val >= ENCODE_LIMIT) {
7,162!
172
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
1,104!
173
    val >>= 7;
552✔
174
  }
175
  return tEncodeU8(pCoder, val);
13,220!
176
}
177
static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) {
178
  return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val));
3,376✔
179
}
180
static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) {
181
  while (val >= ENCODE_LIMIT) {
612!
182
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
948!
183
    val >>= 7;
474✔
184
  }
185
  return tEncodeU8(pCoder, val);
276!
186
}
187
static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) {
188
  return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val));
204✔
189
}
190

191
static FORCE_INLINE int32_t tEncodeFloat(SEncoder* pCoder, float val) {
192
  union {
193
    uint32_t ui;
194
    float    f;
195
  } v;
196
  v.f = val;
96✔
197

198
  return tEncodeU32(pCoder, v.ui);
192✔
199
}
200

201
static FORCE_INLINE int32_t tEncodeDouble(SEncoder* pCoder, double val) {
202
  union {
203
    uint64_t ui;
204
    double   d;
205
  } v;
206
  v.d = val;
×
207

208
  return tEncodeU64(pCoder, v.ui);
×
209
}
210

211
static int32_t tEncodeBool(SEncoder* pCoder, bool val) { return tEncodeU8(pCoder, val ? 1 : 0); }
152✔
212

213
static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len) {
214
  TAOS_CHECK_RETURN(tEncodeU32v(pCoder, len));
4,922!
215
  if (len) {
4,922!
216
    if (pCoder->data) {
4,906!
217
      if (pCoder->pos + len > pCoder->size) {
2,453!
218
        TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
219
      }
220
      TAOS_MEMCPY(pCoder->data + pCoder->pos, val, len);
2,453✔
221
    }
222

223
    pCoder->pos += len;
4,906✔
224
  }
225
  return 0;
4,922✔
226
}
227

228
static FORCE_INLINE int32_t tEncodeCStrWithLen(SEncoder* pCoder, const char* val, uint32_t len) {
229
  return tEncodeBinary(pCoder, (uint8_t*)val, len + 1);
5,098✔
230
}
231

232
static FORCE_INLINE int32_t tEncodeCStr(SEncoder* pCoder, const char* val) {
233
  return tEncodeCStrWithLen(pCoder, val, (uint32_t)strlen(val));
8,684✔
234
}
235

236
/* ------------------------ FOR DECODER ------------------------ */
237
static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size) {
18,670✔
238
  if (pCoder->pos + size > pCoder->size) {
18,670✔
239
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
236✔
240
  } else if (val) {
18,434✔
241
    TAOS_MEMCPY(val, pCoder->data + pCoder->pos, size);
18,433✔
242
  }
243
  pCoder->pos += size;
18,434✔
244
  return 0;
18,434✔
245
}
246

247
static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) {
248
  return tDecodeFixed(pCoder, val, sizeof(*val));
7,530!
249
}
250

251
static FORCE_INLINE int32_t tDecodeI8(SDecoder* pCoder, int8_t* val) { return tDecodeFixed(pCoder, val, sizeof(*val)); }
3,636✔
252

253
// 16
254
static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) {
255
  return tDecodeFixed(pCoder, val, sizeof(*val));
221✔
256
}
257
static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) {
258
  return tDecodeFixed(pCoder, val, sizeof(*val));
31✔
259
}
260
// 32
261
static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) {
262
  return tDecodeFixed(pCoder, val, sizeof(*val));
934✔
263
}
264
static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) {
265
  return tDecodeFixed(pCoder, val, sizeof(*val));
4,057✔
266
}
267
// 64
268
static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) {
269
  return tDecodeFixed(pCoder, val, sizeof(*val));
492✔
270
}
271
static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) {
272
  return tDecodeFixed(pCoder, val, sizeof(*val));
1,675✔
273
}
274

275
// 16v
276
static FORCE_INLINE int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val) {
277
  uint8_t  byte;
278
  uint16_t tval = 0;
1,970✔
279
  for (int32_t i = 0;; i++) {
1,970✔
280
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
1,970!
281
    if (byte < ENCODE_LIMIT) {
1,970!
282
      tval |= (((uint16_t)byte) << (7 * i));
1,970✔
283
      break;
1,970✔
284
    } else {
285
      tval |= ((((uint16_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
×
286
    }
287
  }
288

289
  if (val) {
1,970!
290
    *val = tval;
1,970✔
291
  }
292

293
  return 0;
1,970✔
294
}
295

296
static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) {
297
  uint16_t tval;
298
  TAOS_CHECK_RETURN(tDecodeU16v(pCoder, &tval));
1,970!
299

300
  if (val) {
1,970!
301
    *val = ZIGZAGD(int16_t, tval);
1,970✔
302
  }
303

304
  return 0;
1,970✔
305
}
306

307
// 32v
308
static FORCE_INLINE int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val) {
309
  uint8_t  byte;
310
  uint32_t tval = 0;
4,884✔
311
  for (int32_t i = 0;; i++) {
2,537✔
312
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
5,088!
313
    if (byte < ENCODE_LIMIT) {
5,088!
314
      tval |= (((uint32_t)byte) << (7 * i));
4,884✔
315
      break;
4,884✔
316
    } else {
317
      tval |= ((((uint32_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
204✔
318
    }
319
  }
×
320

321
  if (val) {
4,884!
322
    *val = tval;
4,884✔
323
  }
324

325
  return 0;
4,884✔
326
}
327

328
static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) {
329
  uint32_t tval;
330
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &tval));
2,333!
331

332
  if (val) {
2,333!
333
    *val = ZIGZAGD(int32_t, tval);
2,322✔
334
  }
335

336
  return 0;
2,333✔
337
}
338

339
// 64v
340
static FORCE_INLINE int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val) {
341
  uint8_t  byte;
342
  uint64_t tval = 0;
275✔
343
  for (int32_t i = 0;; i++) {
135✔
344
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
453!
345
    if (byte < ENCODE_LIMIT) {
453!
346
      tval |= (((uint64_t)byte) << (7 * i));
275✔
347
      break;
275✔
348
    } else {
349
      tval |= ((((uint64_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
178✔
350
    }
351
  }
352

353
  if (val) {
275!
354
    *val = tval;
275✔
355
  }
356

357
  return 0;
275✔
358
}
359

360
static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) {
361
  uint64_t tval;
362
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &tval));
30!
363

364
  if (val) {
30!
365
    *val = ZIGZAGD(int64_t, tval);
30✔
366
  }
367

368
  return 0;
30✔
369
}
370

371
static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) {
372
  union {
373
    uint32_t ui;
374
    float    f;
375
  } v;
376

377
  TAOS_CHECK_RETURN(tDecodeU32(pCoder, &(v.ui)));
30!
378

379
  if (val) {
30!
380
    *val = v.f;
30✔
381
  }
382
  return 0;
30✔
383
}
384

385
static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) {
386
  union {
387
    uint64_t ui;
388
    double   d;
389
  } v;
390

391
  TAOS_CHECK_RETURN(tDecodeU64(pCoder, &(v.ui)));
×
392

393
  if (val) {
×
394
    *val = v.d;
×
395
  }
396
  return 0;
×
397
}
398

399
static int32_t tDecodeBool(SDecoder* pCoder, bool* val) {
95!
400
  uint8_t v;
401
  TAOS_CHECK_RETURN(tDecodeU8(pCoder, &v));
95!
402
  if (val) {
95!
403
    *val = v ? true : false;
95✔
404
  }
405
  return 0;
95✔
406
}
407

408
static FORCE_INLINE int32_t tDecodeBinaryWithSize(SDecoder* pCoder, uint32_t size, uint8_t** val) {
409
  if (pCoder->pos + size > pCoder->size) {
2,569!
410
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
411
  }
412

413
  if (val) {
2,569!
414
    *val = pCoder->data + pCoder->pos;
2,569✔
415
  }
416

417
  pCoder->pos += size;
2,569✔
418
  return 0;
2,569✔
419
}
420

421
static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint32_t* len) {
422
  uint32_t length = 0;
1,735✔
423

424
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
2,551!
425
  if (len) {
2,551!
426
    *len = length;
2,546✔
427
  }
428

429
  TAOS_RETURN(tDecodeBinaryWithSize(pCoder, length, val));
5,102!
430
}
431

432
static FORCE_INLINE int32_t tDecodeCStrAndLen(SDecoder* pCoder, char** val, uint32_t* len) {
433
  TAOS_CHECK_RETURN(tDecodeBinary(pCoder, (uint8_t**)val, len));
2,546!
434
  if (*len > 0) {  // notice!!!  *len maybe 0
2,546!
435
    (*len) -= 1;
2,546✔
436
  }
437
  return 0;
2,546✔
438
}
439

440
static FORCE_INLINE int32_t tDecodeCStr(SDecoder* pCoder, char** val) {
441
  uint32_t len = 0;
434✔
442
  return tDecodeCStrAndLen(pCoder, val, &len);
434✔
443
}
444

445
static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) {
2,112✔
446
  char*    pStr = NULL;
2,112✔
447
  uint32_t len = 0;
2,112✔
448
  TAOS_CHECK_RETURN(tDecodeCStrAndLen(pCoder, &pStr, &len));
2,112!
449

450
  if (len < pCoder->size) {
2,112!
451
    TAOS_MEMCPY(val, pStr, len + 1);
2,112✔
452
  }
453

454
  return 0;
2,112✔
455
}
456

457
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) {
458
  uint64_t length = 0;
135✔
459
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &length));
179!
460
  if (length) {
179!
461
    if (len) *len = length;
177!
462

463
    if (pCoder->pos + length > pCoder->size) {
177!
464
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
465
    }
466

467
    *val = taosMemoryMalloc(length);
177!
468
    if (*val == NULL) {
177!
469
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
470
    }
471

472
    TAOS_MEMCPY(*val, pCoder->data + pCoder->pos, length);
177✔
473

474
    pCoder->pos += length;
177✔
475
  } else {
476
    *val = NULL;
2✔
477
  }
478
  return 0;
179✔
479
}
480

481
static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, uint32_t* len) {
482
  uint32_t length = 0;
×
483
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
×
484
  if (length) {
×
485
    if (len) *len = length;
×
486

487
    if (pCoder->pos + length > pCoder->size) {
×
488
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
489
    }
490
    *val = taosMemoryMalloc(length);
×
491
    if (*val == NULL) {
×
492
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
493
    }
494
    TAOS_MEMCPY(*val, pCoder->data + pCoder->pos, length);
×
495

496
    pCoder->pos += length;
×
497
  } else {
498
    *val = NULL;
×
499
  }
500
  return 0;
×
501
}
502

503
static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SDecoder* pCoder, char** val, uint64_t* len) {
504
  TAOS_CHECK_RETURN(tDecodeBinaryAlloc(pCoder, (void**)val, len));
83!
505
  if (*len > 0) {
83!
506
    (*len) -= 1;
83✔
507
  }
508
  return 0;
83✔
509
}
510

511
static FORCE_INLINE int32_t tDecodeCStrAlloc(SDecoder* pCoder, char** val) {
512
  uint64_t len = 0;
83✔
513
  return tDecodeCStrAndLenAlloc(pCoder, val, &len);
83✔
514
}
515

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

518
static FORCE_INLINE void* tEncoderMalloc(SEncoder* pCoder, int32_t size) {
519
  void*      p = NULL;
1,967✔
520
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
1,967!
521
  if (pMem) {
1,967!
522
    pMem->next = pCoder->mList;
1,967✔
523
    pCoder->mList = pMem;
1,967✔
524
    p = (void*)&pMem[1];
1,967✔
525
  }
526
  return p;
1,967✔
527
}
528

529
static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) {
530
  void*      p = NULL;
1,859✔
531
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
1,859!
532
  if (pMem) {
1,859!
533
    pMem->next = pCoder->mList;
1,859✔
534
    pCoder->mList = pMem;
1,859✔
535
    p = (void*)&pMem[1];
1,859✔
536
  }
537
  return p;
1,859✔
538
}
539

540
// ===========================================
541
#define tPutV(p, v)                    \
542
  do {                                 \
543
    int32_t n = 0;                     \
544
    for (;;) {                         \
545
      if (v <= 0x7f) {                 \
546
        if (p) p[n] = v;               \
547
        n++;                           \
548
        break;                         \
549
      }                                \
550
      if (p) p[n] = (v & 0x7f) | 0x80; \
551
      n++;                             \
552
      v >>= 7;                         \
553
    }                                  \
554
    return n;                          \
555
  } while (0)
556

557
// PUT
558
static FORCE_INLINE int32_t tPutU8(uint8_t* p, uint8_t v) {
559
  if (p) ((uint8_t*)p)[0] = v;
×
560
  return sizeof(uint8_t);
×
561
}
562

563
static FORCE_INLINE int32_t tPutI8(uint8_t* p, int8_t v) {
564
  if (p) ((int8_t*)p)[0] = v;
×
565
  return sizeof(int8_t);
×
566
}
567

568
static FORCE_INLINE int32_t tPutU16(uint8_t* p, uint16_t v) {
569
  if (p) ((uint16_t*)p)[0] = v;
570
  return sizeof(uint16_t);
571
}
572

573
static FORCE_INLINE int32_t tPutI16(uint8_t* p, int16_t v) {
574
  if (p) ((int16_t*)p)[0] = v;
575
  return sizeof(int16_t);
576
}
577

578
static FORCE_INLINE int32_t tPutU32(uint8_t* p, uint32_t v) {
579
  if (p) ((uint32_t*)p)[0] = v;
580
  return sizeof(uint32_t);
581
}
582

583
static FORCE_INLINE int32_t tPutI32(uint8_t* p, int32_t v) {
584
  if (p) ((int32_t*)p)[0] = v;
585
  return sizeof(int32_t);
586
}
587

588
static FORCE_INLINE int32_t tPutU64(uint8_t* p, uint64_t v) {
589
  if (p) ((uint64_t*)p)[0] = v;
590
  return sizeof(uint64_t);
591
}
592

593
static FORCE_INLINE int32_t tPutI64(uint8_t* p, int64_t v) {
594
  if (p) ((int64_t*)p)[0] = v;
595
  return sizeof(int64_t);
596
}
597

598
static FORCE_INLINE int32_t tPutFloat(uint8_t* p, float f) {
599
  union {
600
    uint32_t ui;
601
    float    f;
602
  } v;
603
  v.f = f;
604

605
  return tPutU32(p, v.ui);
606
}
607

608
static FORCE_INLINE int32_t tPutDouble(uint8_t* p, double d) {
609
  union {
610
    uint64_t ui;
611
    double   d;
612
  } v;
613
  v.d = d;
614

615
  return tPutU64(p, v.ui);
616
}
617

618
static FORCE_INLINE int32_t tPutU16v(uint8_t* p, uint16_t v) { tPutV(p, v); }
14!
619

620
static FORCE_INLINE int32_t tPutI16v(uint8_t* p, int16_t v) { return tPutU16v(p, ZIGZAGE(int16_t, v)); }
28✔
621

622
static FORCE_INLINE int32_t tPutU32v(uint8_t* p, uint32_t v) { tPutV(p, v); }
×
623

624
static FORCE_INLINE int32_t tPutI32v(uint8_t* p, int32_t v) { return tPutU32v(p, ZIGZAGE(int32_t, v)); }
×
625

626
static FORCE_INLINE int32_t tPutU64v(uint8_t* p, uint64_t v) { tPutV(p, v); }
×
627

628
static FORCE_INLINE int32_t tPutI64v(uint8_t* p, int64_t v) { return tPutU64v(p, ZIGZAGE(int64_t, v)); }
×
629

630
// GET
631
static FORCE_INLINE int32_t tGetU8(uint8_t* p, uint8_t* v) {
632
  if (v) *v = ((uint8_t*)p)[0];
×
633
  return sizeof(uint8_t);
×
634
}
635

636
static FORCE_INLINE int32_t tGetI8(uint8_t* p, int8_t* v) {
637
  if (v) *v = ((int8_t*)p)[0];
9!
638
  return sizeof(int8_t);
9✔
639
}
640

641
static FORCE_INLINE int32_t tGetU16(uint8_t* p, uint16_t* v) {
642
  if (v) *v = ((uint16_t*)p)[0];
643
  return sizeof(uint16_t);
644
}
645

646
static FORCE_INLINE int32_t tGetI16(uint8_t* p, int16_t* v) {
647
  if (v) *v = ((int16_t*)p)[0];
648
  return sizeof(int16_t);
649
}
650

651
static FORCE_INLINE int32_t tGetU32(uint8_t* p, uint32_t* v) {
652
  if (v) *v = ((uint32_t*)p)[0];
653
  return sizeof(uint32_t);
654
}
655

656
static FORCE_INLINE int32_t tGetI32(uint8_t* p, int32_t* v) {
657
  if (v) *v = ((int32_t*)p)[0];
658
  return sizeof(int32_t);
659
}
660

661
static FORCE_INLINE int32_t tGetU64(uint8_t* p, uint64_t* v) {
662
  if (v) *v = ((uint64_t*)p)[0];
663
  return sizeof(uint64_t);
664
}
665

666
static FORCE_INLINE int32_t tGetI64(uint8_t* p, int64_t* v) {
667
  if (v) *v = ((int64_t*)p)[0];
×
668
  return sizeof(int64_t);
×
669
}
670

671
static FORCE_INLINE int32_t tGetU16v(uint8_t* p, uint16_t* v) {
672
  int32_t n = 0;
9✔
673

674
  if (v) *v = 0;
9!
675
  for (;;) {
676
    if (p[n] <= 0x7f) {
9!
677
      if (v) (*v) |= (((uint16_t)p[n]) << (7 * n));
9!
678
      n++;
9✔
679
      break;
9✔
680
    }
681
    if (v) (*v) |= (((uint16_t)(p[n] & 0x7f)) << (7 * n));
×
682
    n++;
×
683
  }
684

685
  return n;
9✔
686
}
687

688
static FORCE_INLINE int32_t tGetI16v(uint8_t* p, int16_t* v) {
689
  int32_t  n;
690
  uint16_t tv;
691

692
  n = tGetU16v(p, &tv);
9✔
693
  if (v) *v = ZIGZAGD(int16_t, tv);
9!
694

695
  return n;
9✔
696
}
697

698
static FORCE_INLINE int32_t tGetU32v(uint8_t* p, uint32_t* v) {
699
  int32_t n = 0;
20✔
700

701
  if (v) *v = 0;
20!
702
  for (;;) {
703
    if (p[n] <= 0x7f) {
20!
704
      if (v) (*v) |= (((uint32_t)p[n]) << (7 * n));
20!
705
      n++;
20✔
706
      break;
20✔
707
    }
708
    if (v) (*v) |= (((uint32_t)(p[n] & 0x7f)) << (7 * n));
×
709
    n++;
×
710
  }
711

712
  return n;
20✔
713
}
714

715
static FORCE_INLINE int32_t tGetI32v(uint8_t* p, int32_t* v) {
716
  int32_t  n;
717
  uint32_t tv;
718

719
  n = tGetU32v(p, &tv);
×
720
  if (v) *v = ZIGZAGD(int32_t, tv);
×
721

722
  return n;
×
723
}
724

725
static FORCE_INLINE int32_t tGetU64v(uint8_t* p, uint64_t* v) {
726
  int32_t n = 0;
×
727

728
  if (v) *v = 0;
×
729
  for (;;) {
730
    if (p[n] <= 0x7f) {
×
731
      if (v) (*v) |= (((uint64_t)p[n]) << (7 * n));
×
732
      n++;
×
733
      break;
×
734
    }
735
    if (v) (*v) |= (((uint64_t)(p[n] & 0x7f)) << (7 * n));
×
736
    n++;
×
737
  }
738

739
  return n;
×
740
}
741

742
static FORCE_INLINE int32_t tGetI64v(uint8_t* p, int64_t* v) {
743
  int32_t  n;
744
  uint64_t tv;
745

746
  n = tGetU64v(p, &tv);
×
747
  if (v) *v = ZIGZAGD(int64_t, tv);
×
748

749
  return n;
×
750
}
751

752
static FORCE_INLINE int32_t tGetFloat(uint8_t* p, float* f) {
753
  int32_t n = 0;
754

755
  union {
756
    uint32_t ui;
757
    float    f;
758
  } v;
759

760
  n = tGetU32(p, &v.ui);
761

762
  *f = v.f;
763
  return n;
764
}
765

766
static FORCE_INLINE int32_t tGetDouble(uint8_t* p, double* d) {
767
  int32_t n = 0;
768

769
  union {
770
    uint64_t ui;
771
    double   d;
772
  } v;
773

774
  n = tGetU64(p, &v.ui);
775

776
  *d = v.d;
777
  return n;
778
}
779

780
// =====================
781
static FORCE_INLINE int32_t tPutBinary(uint8_t* p, uint8_t* pData, uint32_t nData) {
782
  int n = 0;
×
783

784
  n += tPutU32v(p ? p + n : p, nData);
×
785
  if (p) TAOS_MEMCPY(p + n, pData, nData);
×
786
  n += nData;
×
787

788
  return n;
×
789
}
790

791
static FORCE_INLINE int32_t tGetBinary(uint8_t* p, uint8_t** ppData, uint32_t* nData) {
792
  int32_t  n = 0;
×
793
  uint32_t nt;
794

795
  n += tGetU32v(p, &nt);
×
796
  if (nData) *nData = nt;
×
797
  if (ppData) *ppData = p + n;
×
798
  n += nt;
×
799

800
  return n;
×
801
}
802

803
static FORCE_INLINE int32_t tPutCStr(uint8_t* p, char* pData) {
804
  return tPutBinary(p, (uint8_t*)pData, strlen(pData) + 1);
×
805
}
806
static FORCE_INLINE int32_t tGetCStr(uint8_t* p, char** ppData) { return tGetBinary(p, (uint8_t**)ppData, NULL); }
×
807

808
#ifdef __cplusplus
809
}
810
#endif
811

812
#endif /*_TD_UTIL_ENCODE_H_*/
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc