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

taosdata / TDengine / #4912

04 Jan 2026 09:05AM UTC coverage: 64.888% (-0.1%) from 65.028%
#4912

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

5351 existing lines in 123 files now uncovered.

194856 of 300296 relevant lines covered (64.89%)

118198896.2 hits per line

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

86.79
/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) {
2,147,483,647✔
130
    if (pCoder->pos + size > pCoder->size) {
2,147,483,647✔
131
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
132
    }
133
    TAOS_MEMCPY(pCoder->data + pCoder->pos, val, size);
2,147,483,647✔
134
  }
135

136
  pCoder->pos += size;
2,147,483,647✔
137
  return 0;
2,147,483,647✔
138
}
139

140
static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
2,147,483,647✔
141
static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
2,147,483,647✔
142
static FORCE_INLINE int32_t tEncodeU16(SEncoder* pCoder, uint16_t val) {
143
  return tEncodeFixed(pCoder, &val, sizeof(val));
2,147,483,647✔
144
}
145
static FORCE_INLINE int32_t tEncodeI16(SEncoder* pCoder, int16_t val) {
146
  return tEncodeFixed(pCoder, &val, sizeof(val));
396,789,002✔
147
}
148
static FORCE_INLINE int32_t tEncodeU32(SEncoder* pCoder, uint32_t val) {
149
  return tEncodeFixed(pCoder, &val, sizeof(val));
2,147,483,647✔
150
}
151
static FORCE_INLINE int32_t tEncodeI32(SEncoder* pCoder, int32_t val) {
152
  return tEncodeFixed(pCoder, &val, sizeof(val));
2,147,483,647✔
153
}
154
static FORCE_INLINE int32_t tEncodeU64(SEncoder* pCoder, uint64_t val) {
155
  return tEncodeFixed(pCoder, &val, sizeof(val));
2,147,483,647✔
156
}
157
static FORCE_INLINE int32_t tEncodeI64(SEncoder* pCoder, int64_t val) {
158
  return tEncodeFixed(pCoder, &val, sizeof(val));
2,147,483,647✔
159
}
160
static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) {
161
  while (val >= ENCODE_LIMIT) {
2,147,483,647✔
162
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
2,147,483,647✔
163
    val >>= 7;
2,147,483,647✔
164
  }
165
  return tEncodeU8(pCoder, val);
2,147,483,647✔
166
}
167
static FORCE_INLINE int32_t tEncodeI16v(SEncoder* pCoder, int16_t val) {
168
  return tEncodeU16v(pCoder, ZIGZAGE(int16_t, val));
2,147,483,647✔
169
}
170
static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) {
171
  while (val >= ENCODE_LIMIT) {
2,147,483,647✔
172
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
2,147,483,647✔
173
    val >>= 7;
2,147,483,647✔
174
  }
175
  return tEncodeU8(pCoder, val);
2,147,483,647✔
176
}
177
static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) {
178
  return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val));
2,147,483,647✔
179
}
180
static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) {
181
  while (val >= ENCODE_LIMIT) {
2,147,483,647✔
182
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
2,147,483,647✔
183
    val >>= 7;
1,842,586,270✔
184
  }
185
  return tEncodeU8(pCoder, val);
2,147,483,647✔
186
}
187
static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) {
188
  return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val));
355,263,903✔
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;
90,793,768✔
197

198
  return tEncodeU32(pCoder, v.ui);
181,587,536✔
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;
38,816,086✔
207

208
  return tEncodeU64(pCoder, v.ui);
77,634,889✔
209
}
210

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

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

223
    pCoder->pos += len;
2,147,483,647✔
224
  }
225
  return 0;
2,147,483,647✔
226
}
227

228
static FORCE_INLINE int32_t tEncodeCStrWithLen(SEncoder* pCoder, const char* val, uint32_t len) {
229
  if (val == NULL) {
2,147,483,647✔
NEW
230
    return tEncodeU32v(pCoder, 0);
×
231
  }
232
  return tEncodeBinary(pCoder, (uint8_t*)val, len + 1);
2,147,483,647✔
233
}
234

235
static FORCE_INLINE int32_t tEncodeCStr(SEncoder* pCoder, const char* val) {
236
  if (val == NULL) {
2,147,483,647✔
NEW
237
    return tEncodeU32v(pCoder, 0);
×
238
  }
239
  return tEncodeCStrWithLen(pCoder, val, (uint32_t)strlen(val));
2,147,483,647✔
240
}
241

242
/* ------------------------ FOR DECODER ------------------------ */
243
static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size) {
2,147,483,647✔
244
  if (pCoder->pos + size > pCoder->size) {
2,147,483,647✔
245
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
3,762,755✔
246
  } else if (val) {
2,147,483,647✔
247
    TAOS_MEMCPY(val, pCoder->data + pCoder->pos, size);
2,147,483,647✔
248
  }
249
  pCoder->pos += size;
2,147,483,647✔
250
  return 0;
2,147,483,647✔
251
}
252

253
static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) {
254
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
255
}
256

257
static FORCE_INLINE int32_t tDecodeI8(SDecoder* pCoder, int8_t* val) { return tDecodeFixed(pCoder, val, sizeof(*val)); }
2,147,483,647✔
258

259
// 16
260
static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) {
261
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
262
}
263
static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) {
264
  return tDecodeFixed(pCoder, val, sizeof(*val));
198,726,215✔
265
}
266
// 32
267
static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) {
268
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
269
}
270
static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) {
271
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
272
}
273
// 64
274
static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) {
275
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
276
}
277
static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) {
278
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
279
}
280

281
// 16v
282
static FORCE_INLINE int32_t tDecodeU16v(SDecoder* pCoder, uint16_t* val) {
283
  uint8_t  byte;
2,147,483,647✔
284
  uint16_t tval = 0;
2,147,483,647✔
285
  for (int32_t i = 0;; i++) {
2,147,483,647✔
286
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
2,147,483,647✔
287
    if (byte < ENCODE_LIMIT) {
2,147,483,647✔
288
      tval |= (((uint16_t)byte) << (7 * i));
2,147,483,647✔
289
      break;
2,147,483,647✔
290
    } else {
291
      tval |= ((((uint16_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
2,147,483,647✔
292
    }
293
  }
294

295
  if (val) {
2,147,483,647✔
296
    *val = tval;
2,147,483,647✔
297
  }
298

299
  return 0;
2,147,483,647✔
300
}
301

302
static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) {
303
  uint16_t tval;
2,147,483,647✔
304
  TAOS_CHECK_RETURN(tDecodeU16v(pCoder, &tval));
2,147,483,647✔
305

306
  if (val) {
2,147,483,647✔
307
    *val = ZIGZAGD(int16_t, tval);
2,147,483,647✔
308
  }
309

310
  return 0;
2,147,483,647✔
311
}
312

313
// 32v
314
static FORCE_INLINE int32_t tDecodeU32v(SDecoder* pCoder, uint32_t* val) {
315
  uint8_t  byte;
2,147,483,647✔
316
  uint32_t tval = 0;
2,147,483,647✔
317
  for (int32_t i = 0;; i++) {
2,147,483,647✔
318
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
2,147,483,647✔
319
    if (byte < ENCODE_LIMIT) {
2,147,483,647✔
320
      tval |= (((uint32_t)byte) << (7 * i));
2,147,483,647✔
321
      break;
2,147,483,647✔
322
    } else {
323
      tval |= ((((uint32_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
2,147,483,647✔
324
    }
325
  }
326

327
  if (val) {
2,147,483,647✔
328
    *val = tval;
2,147,483,647✔
329
  }
330

331
  return 0;
2,147,483,647✔
332
}
333

334
static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) {
335
  uint32_t tval;
2,147,483,647✔
336
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &tval));
2,147,483,647✔
337

338
  if (val) {
2,147,483,647✔
339
    *val = ZIGZAGD(int32_t, tval);
2,147,483,647✔
340
  }
341

342
  return 0;
2,147,483,647✔
343
}
344

345
// 64v
346
static FORCE_INLINE int32_t tDecodeU64v(SDecoder* pCoder, uint64_t* val) {
347
  uint8_t  byte;
2,147,483,647✔
348
  uint64_t tval = 0;
2,147,483,647✔
349
  for (int32_t i = 0;; i++) {
2,147,483,647✔
350
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
2,147,483,647✔
351
    if (byte < ENCODE_LIMIT) {
2,147,483,647✔
352
      tval |= (((uint64_t)byte) << (7 * i));
2,147,483,647✔
353
      break;
2,147,483,647✔
354
    } else {
355
      tval |= ((((uint64_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
1,927,320,552✔
356
    }
357
  }
358

359
  if (val) {
2,147,483,647✔
360
    *val = tval;
2,147,483,647✔
361
  }
362

363
  return 0;
2,147,483,647✔
364
}
365

366
static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) {
367
  uint64_t tval;
1,092,469,199✔
368
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &tval));
1,091,609,232✔
369

370
  if (val) {
1,091,609,232✔
371
    *val = ZIGZAGD(int64_t, tval);
1,091,452,245✔
372
  }
373

374
  return 0;
1,091,435,469✔
375
}
376

377
static FORCE_INLINE int32_t tDecodeFloat(SDecoder* pCoder, float* val) {
378
  union {
379
    uint32_t ui;
380
    float    f;
381
  } v;
44,360,808✔
382

383
  TAOS_CHECK_RETURN(tDecodeU32(pCoder, &(v.ui)));
44,364,752✔
384

385
  if (val) {
44,364,752✔
386
    *val = v.f;
44,364,752✔
387
  }
388
  return 0;
44,364,752✔
389
}
390

391
static FORCE_INLINE int32_t tDecodeDouble(SDecoder* pCoder, double* val) {
392
  union {
393
    uint64_t ui;
394
    double   d;
395
  } v;
16,042,398✔
396

397
  TAOS_CHECK_RETURN(tDecodeU64(pCoder, &(v.ui)));
16,042,398✔
398

399
  if (val) {
16,042,398✔
400
    *val = v.d;
16,042,398✔
401
  }
402
  return 0;
16,042,398✔
403
}
404

405
static int32_t tDecodeBool(SDecoder* pCoder, bool* val) {
1,208,980,059✔
406
  uint8_t v;
1,208,947,761✔
407
  TAOS_CHECK_RETURN(tDecodeU8(pCoder, &v));
1,209,337,796✔
408
  if (val) {
1,209,337,796✔
409
    *val = v ? true : false;
1,209,130,235✔
410
  }
411
  return 0;
1,209,491,740✔
412
}
413

414
static FORCE_INLINE int32_t tDecodeBinaryWithSize(SDecoder* pCoder, uint32_t size, uint8_t** val) {
415
  if (pCoder->pos + size > pCoder->size) {
2,147,483,647✔
416
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
54✔
417
  }
418

419
  if (val) {
2,147,483,647✔
420
    *val = pCoder->data + pCoder->pos;
2,147,483,647✔
421
  }
422

423
  pCoder->pos += size;
2,147,483,647✔
424
  return 0;
2,147,483,647✔
425
}
426

427
static FORCE_INLINE int32_t tDecodeBinary(SDecoder* pCoder, uint8_t** val, uint32_t* len) {
428
  uint32_t length = 0;
2,147,483,647✔
429

430
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
2,147,483,647✔
431
  if (len) {
2,147,483,647✔
432
    *len = length;
2,147,483,647✔
433
  }
434

435
  TAOS_RETURN(tDecodeBinaryWithSize(pCoder, length, val));
2,147,483,647✔
436
}
437

438
static FORCE_INLINE int32_t tDecodeBinaryTo(SDecoder* pCoder, uint8_t* val, uint32_t size) {
439
  uint32_t len = 0;
2,362,444✔
440

441
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &len));
2,362,440✔
442

443
  if (len != size) {
2,362,440✔
UNCOV
444
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
445
  }
446

447
  if (pCoder->pos + len > pCoder->size) {
2,362,440✔
UNCOV
448
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
449
  }
450

451
  TAOS_MEMCPY(val, pCoder->data + pCoder->pos, len);
2,362,400✔
452
  pCoder->pos += len;
2,362,400✔
453
  return 0;
2,362,063✔
454
}
455

456
static FORCE_INLINE int32_t tDecodeCStrAndLen(SDecoder* pCoder, char** val, uint32_t* len) {
457
  TAOS_CHECK_RETURN(tDecodeBinary(pCoder, (uint8_t**)val, len));
2,147,483,647✔
458
  if (*len > 0) {  // notice!!!  *len maybe 0
2,147,483,647✔
459
    (*len) -= 1;
2,147,483,647✔
460
  }
461
  return 0;
2,147,483,647✔
462
}
463

464
static FORCE_INLINE int32_t tDecodeCStr(SDecoder* pCoder, char** val) {
465
  uint32_t len = 0;
1,998,970,676✔
466
  return tDecodeCStrAndLen(pCoder, val, &len);
1,999,562,472✔
467
}
468

469
static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) {
2,147,483,647✔
470
  char*    pStr = NULL;
2,147,483,647✔
471
  uint32_t len = 0;
2,147,483,647✔
472
  TAOS_CHECK_RETURN(tDecodeCStrAndLen(pCoder, &pStr, &len));
2,147,483,647✔
473

474
  if (len < pCoder->size) {
2,147,483,647✔
475
    TAOS_MEMCPY(val, pStr, len + 1);
2,147,483,647✔
476
  }
477

478
  return 0;
2,147,483,647✔
479
}
480

481
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) {
482
  uint64_t length = 0;
1,793,829,911✔
483
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &length));
1,794,339,305✔
484
  if (length) {
1,794,339,305✔
485
    if (len) *len = length;
1,732,951,418✔
486

487
    if (pCoder->pos + length > pCoder->size) {
1,733,014,119✔
UNCOV
488
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
489
    }
490

491
    *val = taosMemoryMalloc(length);
1,732,917,213✔
492
    if (*val == NULL) {
1,732,115,628✔
UNCOV
493
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
494
    }
495

496
    TAOS_MEMCPY(*val, pCoder->data + pCoder->pos, length);
1,732,228,916✔
497

498
    pCoder->pos += length;
1,732,549,401✔
499
  } else {
500
    *val = NULL;
61,387,887✔
501
  }
502
  return 0;
1,794,275,131✔
503
}
504

505
static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, uint32_t* len) {
506
  uint32_t length = 0;
2,757✔
507
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
2,757✔
508
  if (length) {
2,757✔
509
    if (len) *len = length;
2,757✔
510

511
    if (pCoder->pos + length > pCoder->size) {
2,757✔
UNCOV
512
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
513
    }
514
    *val = taosMemoryMalloc(length);
2,757✔
515
    if (*val == NULL) {
2,757✔
UNCOV
516
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
517
    }
518
    TAOS_MEMCPY(*val, pCoder->data + pCoder->pos, length);
2,757✔
519

520
    pCoder->pos += length;
2,757✔
521
  } else {
522
    *val = NULL;
×
523
  }
524
  return 0;
2,757✔
525
}
526

527
static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SDecoder* pCoder, char** val, uint64_t* len) {
528
  TAOS_CHECK_RETURN(tDecodeBinaryAlloc(pCoder, (void**)val, len));
800,156,825✔
529
  if (*len > 0) {
800,156,825✔
530
    (*len) -= 1;
800,100,537✔
531
  }
532
  return 0;
800,219,154✔
533
}
534

535
static FORCE_INLINE int32_t tDecodeCStrAlloc(SDecoder* pCoder, char** val) {
536
  uint64_t len = 0;
800,202,236✔
537
  return tDecodeCStrAndLenAlloc(pCoder, val, &len);
800,219,154✔
538
}
539

540
static FORCE_INLINE bool tDecodeIsEnd(SDecoder* pCoder) { return (pCoder->size == pCoder->pos); }
2,147,483,647✔
541

542
static FORCE_INLINE void* tEncoderMalloc(SEncoder* pCoder, int32_t size) {
543
  void*      p = NULL;
2,147,483,647✔
544
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
2,147,483,647✔
545
  if (pMem) {
2,147,483,647✔
546
    pMem->next = pCoder->mList;
2,147,483,647✔
547
    pCoder->mList = pMem;
2,147,483,647✔
548
    p = (void*)&pMem[1];
2,147,483,647✔
549
  }
550
  return p;
2,147,483,647✔
551
}
552

553
static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) {
554
  void*      p = NULL;
2,147,483,647✔
555
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
2,147,483,647✔
556
  if (pMem) {
2,147,483,647✔
557
    pMem->next = pCoder->mList;
2,147,483,647✔
558
    pCoder->mList = pMem;
2,147,483,647✔
559
    p = (void*)&pMem[1];
2,147,483,647✔
560
  }
561
  return p;
2,147,483,647✔
562
}
563

564
// ===========================================
565
#define tPutV(p, v)                    \
566
  do {                                 \
567
    int32_t n = 0;                     \
568
    for (;;) {                         \
569
      if (v <= 0x7f) {                 \
570
        if (p) p[n] = v;               \
571
        n++;                           \
572
        break;                         \
573
      }                                \
574
      if (p) p[n] = (v & 0x7f) | 0x80; \
575
      n++;                             \
576
      v >>= 7;                         \
577
    }                                  \
578
    return n;                          \
579
  } while (0)
580

581
// PUT
582
static FORCE_INLINE int32_t tPutU8(uint8_t* p, uint8_t v) {
UNCOV
583
  if (p) ((uint8_t*)p)[0] = v;
×
UNCOV
584
  return sizeof(uint8_t);
×
585
}
586

587
static FORCE_INLINE int32_t tPutI8(uint8_t* p, int8_t v) {
588
  if (p) ((int8_t*)p)[0] = v;
500,839,856✔
589
  return sizeof(int8_t);
500,839,787✔
590
}
591

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

597
static FORCE_INLINE int32_t tPutI16(uint8_t* p, int16_t v) {
598
  if (p) ((int16_t*)p)[0] = v;
599
  return sizeof(int16_t);
600
}
601

602
static FORCE_INLINE int32_t tPutU32(uint8_t* p, uint32_t v) {
603
  if (p) ((uint32_t*)p)[0] = v;
604
  return sizeof(uint32_t);
605
}
606

607
static FORCE_INLINE int32_t tPutI32(uint8_t* p, int32_t v) {
608
  if (p) ((int32_t*)p)[0] = v;
609
  return sizeof(int32_t);
610
}
611

612
static FORCE_INLINE int32_t tPutU64(uint8_t* p, uint64_t v) {
613
  if (p) ((uint64_t*)p)[0] = v;
41,413,634✔
614
  return sizeof(uint64_t);
41,413,634✔
615
}
616

617
static FORCE_INLINE int32_t tPutI64(uint8_t* p, int64_t v) {
618
  if (p) ((int64_t*)p)[0] = v;
619
  return sizeof(int64_t);
620
}
621

622
static FORCE_INLINE int32_t tPutFloat(uint8_t* p, float f) {
623
  union {
624
    uint32_t ui;
625
    float    f;
626
  } v;
627
  v.f = f;
628

629
  return tPutU32(p, v.ui);
630
}
631

632
static FORCE_INLINE int32_t tPutDouble(uint8_t* p, double d) {
633
  union {
634
    uint64_t ui;
635
    double   d;
636
  } v;
637
  v.d = d;
638

639
  return tPutU64(p, v.ui);
640
}
641

642
static FORCE_INLINE int32_t tPutU16v(uint8_t* p, uint16_t v) { tPutV(p, v); }
2,147,483,647✔
643

644
static FORCE_INLINE int32_t tPutI16v(uint8_t* p, int16_t v) { return tPutU16v(p, ZIGZAGE(int16_t, v)); }
2,147,483,647✔
645

646
static FORCE_INLINE int32_t tPutU32v(uint8_t* p, uint32_t v) { tPutV(p, v); }
2,147,483,647✔
647

UNCOV
648
static FORCE_INLINE int32_t tPutI32v(uint8_t* p, int32_t v) { return tPutU32v(p, ZIGZAGE(int32_t, v)); }
×
649

UNCOV
650
static FORCE_INLINE int32_t tPutU64v(uint8_t* p, uint64_t v) { tPutV(p, v); }
×
651

UNCOV
652
static FORCE_INLINE int32_t tPutI64v(uint8_t* p, int64_t v) { return tPutU64v(p, ZIGZAGE(int64_t, v)); }
×
653

654
// GET
655
static FORCE_INLINE int32_t tGetU8(uint8_t* p, uint8_t* v) {
656
  if (v) *v = ((uint8_t*)p)[0];
×
UNCOV
657
  return sizeof(uint8_t);
×
658
}
659

660
static FORCE_INLINE int32_t tGetI8(uint8_t* p, int8_t* v) {
661
  if (v) *v = ((int8_t*)p)[0];
2,147,483,647✔
662
  return sizeof(int8_t);
2,147,483,647✔
663
}
664

665
static FORCE_INLINE int32_t tGetU16(uint8_t* p, uint16_t* v) {
666
  if (v) *v = ((uint16_t*)p)[0];
667
  return sizeof(uint16_t);
668
}
669

670
static FORCE_INLINE int32_t tGetI16(uint8_t* p, int16_t* v) {
671
  if (v) *v = ((int16_t*)p)[0];
672
  return sizeof(int16_t);
673
}
674

675
static FORCE_INLINE int32_t tGetU32(uint8_t* p, uint32_t* v) {
676
  if (v) *v = ((uint32_t*)p)[0];
677
  return sizeof(uint32_t);
678
}
679

680
static FORCE_INLINE int32_t tGetI32(uint8_t* p, int32_t* v) {
681
  if (v) *v = ((int32_t*)p)[0];
682
  return sizeof(int32_t);
683
}
684

685
static FORCE_INLINE int32_t tGetU64(uint8_t* p, uint64_t* v) {
686
  if (v) *v = ((uint64_t*)p)[0];
30,686,008✔
687
  return sizeof(uint64_t);
30,686,008✔
688
}
689

690
static FORCE_INLINE int32_t tGetI64(uint8_t* p, int64_t* v) {
UNCOV
691
  if (v) *v = ((int64_t*)p)[0];
×
UNCOV
692
  return sizeof(int64_t);
×
693
}
694

695
static FORCE_INLINE int32_t tGetU16v(uint8_t* p, uint16_t* v) {
696
  int32_t n = 0;
2,147,483,647✔
697

698
  if (v) *v = 0;
2,147,483,647✔
699
  for (;;) {
700
    if (p[n] <= 0x7f) {
2,147,483,647✔
701
      if (v) (*v) |= (((uint16_t)p[n]) << (7 * n));
2,147,483,647✔
702
      n++;
2,147,483,647✔
703
      break;
2,147,483,647✔
704
    }
705
    if (v) (*v) |= (((uint16_t)(p[n] & 0x7f)) << (7 * n));
2,147,483,647✔
706
    n++;
2,147,483,647✔
707
  }
708

709
  return n;
2,147,483,647✔
710
}
711

712
static FORCE_INLINE int32_t tGetI16v(uint8_t* p, int16_t* v) {
713
  int32_t  n;
714
  uint16_t tv;
2,147,483,647✔
715

716
  n = tGetU16v(p, &tv);
2,147,483,647✔
717
  if (v) *v = ZIGZAGD(int16_t, tv);
2,147,483,647✔
718

719
  return n;
2,147,483,647✔
720
}
721

722
static FORCE_INLINE int32_t tGetU32v(uint8_t* p, uint32_t* v) {
723
  int32_t n = 0;
2,147,483,647✔
724

725
  if (v) *v = 0;
2,147,483,647✔
726
  for (;;) {
727
    if (p[n] <= 0x7f) {
2,147,483,647✔
728
      if (v) (*v) |= (((uint32_t)p[n]) << (7 * n));
2,147,483,647✔
729
      n++;
2,147,483,647✔
730
      break;
2,147,483,647✔
731
    }
732
    if (v) (*v) |= (((uint32_t)(p[n] & 0x7f)) << (7 * n));
2,064,853,384✔
733
    n++;
2,064,909,850✔
734
  }
735

736
  return n;
2,147,483,647✔
737
}
738

739
static FORCE_INLINE int32_t tGetI32v(uint8_t* p, int32_t* v) {
740
  int32_t  n;
UNCOV
741
  uint32_t tv;
×
742

UNCOV
743
  n = tGetU32v(p, &tv);
×
UNCOV
744
  if (v) *v = ZIGZAGD(int32_t, tv);
×
745

UNCOV
746
  return n;
×
747
}
748

749
static FORCE_INLINE int32_t tGetU64v(uint8_t* p, uint64_t* v) {
750
  int32_t n = 0;
×
751

752
  if (v) *v = 0;
×
753
  for (;;) {
UNCOV
754
    if (p[n] <= 0x7f) {
×
UNCOV
755
      if (v) (*v) |= (((uint64_t)p[n]) << (7 * n));
×
756
      n++;
×
UNCOV
757
      break;
×
758
    }
UNCOV
759
    if (v) (*v) |= (((uint64_t)(p[n] & 0x7f)) << (7 * n));
×
760
    n++;
×
761
  }
762

763
  return n;
×
764
}
765

766
static FORCE_INLINE int32_t tGetI64v(uint8_t* p, int64_t* v) {
767
  int32_t  n;
UNCOV
768
  uint64_t tv;
×
769

UNCOV
770
  n = tGetU64v(p, &tv);
×
UNCOV
771
  if (v) *v = ZIGZAGD(int64_t, tv);
×
772

UNCOV
773
  return n;
×
774
}
775

776
static FORCE_INLINE int32_t tGetFloat(uint8_t* p, float* f) {
777
  int32_t n = 0;
778

779
  union {
780
    uint32_t ui;
781
    float    f;
782
  } v;
783

784
  n = tGetU32(p, &v.ui);
785

786
  *f = v.f;
787
  return n;
788
}
789

790
static FORCE_INLINE int32_t tGetDouble(uint8_t* p, double* d) {
791
  int32_t n = 0;
792

793
  union {
794
    uint64_t ui;
795
    double   d;
796
  } v;
797

798
  n = tGetU64(p, &v.ui);
799

800
  *d = v.d;
801
  return n;
802
}
803

804
// =====================
805
static FORCE_INLINE int32_t tPutBinary(uint8_t* p, uint8_t* pData, uint32_t nData) {
806
  int n = 0;
136,203,220✔
807

808
  n += tPutU32v(p ? p + n : p, nData);
136,203,220✔
809
  if (p) TAOS_MEMCPY(p + n, pData, nData);
136,206,516✔
810
  n += nData;
136,204,866✔
811

812
  return n;
136,204,866✔
813
}
814

815
static FORCE_INLINE int32_t tGetBinary(uint8_t* p, uint8_t** ppData, uint32_t* nData) {
816
  int32_t  n = 0;
876,459,871✔
817
  uint32_t nt;
876,456,053✔
818

819
  n += tGetU32v(p, &nt);
876,481,104✔
820
  if (nData) *nData = nt;
876,481,104✔
821
  if (ppData) *ppData = p + n;
876,508,467✔
822
  n += nt;
876,488,919✔
823

824
  return n;
876,488,919✔
825
}
826

827
static FORCE_INLINE int32_t tPutCStr(uint8_t* p, char* pData) {
828
  return tPutBinary(p, (uint8_t*)pData, strlen(pData) + 1);
2,338,528✔
829
}
830
static FORCE_INLINE int32_t tGetCStr(uint8_t* p, char** ppData) { return tGetBinary(p, (uint8_t**)ppData, NULL); }
4,372,718✔
831

832
#ifdef __cplusplus
833
}
834
#endif
835

836
#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