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

taosdata / TDengine / #4909

30 Dec 2025 10:52AM UTC coverage: 65.542% (+0.2%) from 65.386%
#4909

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

857 existing lines in 113 files now uncovered.

193924 of 295877 relevant lines covered (65.54%)

120594206.86 hits per line

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

86.96
/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));
191,743,737✔
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,718,156,950✔
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));
352,165,779✔
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;
113,501,154✔
197

198
  return tEncodeU32(pCoder, v.ui);
227,002,308✔
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;
37,546,797✔
207

208
  return tEncodeU64(pCoder, v.ui);
75,096,435✔
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
  return tEncodeBinary(pCoder, (uint8_t*)val, len + 1);
2,147,483,647✔
230
}
231

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

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

247
static FORCE_INLINE int32_t tDecodeU8(SDecoder* pCoder, uint8_t* val) {
248
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
249
}
250

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

253
// 16
254
static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) {
255
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
256
}
257
static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) {
258
  return tDecodeFixed(pCoder, val, sizeof(*val));
96,856,920✔
259
}
260
// 32
261
static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) {
262
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
263
}
264
static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) {
265
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
266
}
267
// 64
268
static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) {
269
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
270
}
271
static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) {
272
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,147,483,647✔
273
}
274

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

289
  if (val) {
2,147,483,647✔
290
    *val = tval;
2,147,483,647✔
291
  }
292

293
  return 0;
2,147,483,647✔
294
}
295

296
static FORCE_INLINE int32_t tDecodeI16v(SDecoder* pCoder, int16_t* val) {
297
  uint16_t tval;
2,147,483,647✔
298
  TAOS_CHECK_RETURN(tDecodeU16v(pCoder, &tval));
2,147,483,647✔
299

300
  if (val) {
2,147,483,647✔
301
    *val = ZIGZAGD(int16_t, tval);
2,147,483,647✔
302
  }
303

304
  return 0;
2,147,483,647✔
305
}
306

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

321
  if (val) {
2,147,483,647✔
322
    *val = tval;
2,147,483,647✔
323
  }
324

325
  return 0;
2,147,483,647✔
326
}
327

328
static FORCE_INLINE int32_t tDecodeI32v(SDecoder* pCoder, int32_t* val) {
329
  uint32_t tval;
2,147,483,647✔
330
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &tval));
2,147,483,647✔
331

332
  if (val) {
2,147,483,647✔
333
    *val = ZIGZAGD(int32_t, tval);
2,147,483,647✔
334
  }
335

336
  return 0;
2,147,483,647✔
337
}
338

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

353
  if (val) {
2,147,483,647✔
354
    *val = tval;
2,147,483,647✔
355
  }
356

357
  return 0;
2,147,483,647✔
358
}
359

360
static FORCE_INLINE int32_t tDecodeI64v(SDecoder* pCoder, int64_t* val) {
361
  uint64_t tval;
1,064,807,544✔
362
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &tval));
1,064,503,462✔
363

364
  if (val) {
1,064,503,462✔
365
    *val = ZIGZAGD(int64_t, tval);
1,064,555,637✔
366
  }
367

368
  return 0;
1,064,505,456✔
369
}
370

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

377
  TAOS_CHECK_RETURN(tDecodeU32(pCoder, &(v.ui)));
55,621,789✔
378

379
  if (val) {
55,621,789✔
380
    *val = v.f;
55,621,789✔
381
  }
382
  return 0;
55,621,789✔
383
}
384

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

391
  TAOS_CHECK_RETURN(tDecodeU64(pCoder, &(v.ui)));
15,509,318✔
392

393
  if (val) {
15,509,318✔
394
    *val = v.d;
15,509,318✔
395
  }
396
  return 0;
15,509,318✔
397
}
398

399
static int32_t tDecodeBool(SDecoder* pCoder, bool* val) {
1,121,812,621✔
400
  uint8_t v;
1,121,784,246✔
401
  TAOS_CHECK_RETURN(tDecodeU8(pCoder, &v));
1,121,946,019✔
402
  if (val) {
1,121,946,019✔
403
    *val = v ? true : false;
1,121,893,722✔
404
  }
405
  return 0;
1,121,886,011✔
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,147,483,647✔
UNCOV
410
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
411
  }
412

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

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

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

424
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
2,147,483,647✔
425
  if (len) {
2,147,483,647✔
426
    *len = length;
2,147,483,647✔
427
  }
428

429
  TAOS_RETURN(tDecodeBinaryWithSize(pCoder, length, val));
2,147,483,647✔
430
}
431

432
static FORCE_INLINE int32_t tDecodeBinaryTo(SDecoder* pCoder, uint8_t* val, uint32_t size) {
433
  uint32_t len = 0;
1,933,733✔
434

435
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &len));
1,933,714✔
436

437
  if (len != size) {
1,933,714✔
438
    TAOS_RETURN(TSDB_CODE_INVALID_MSG);
×
439
  }
440

441
  if (pCoder->pos + len > pCoder->size) {
1,933,714✔
442
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
443
  }
444

445
  TAOS_MEMCPY(val, pCoder->data + pCoder->pos, len);
1,933,714✔
446
  pCoder->pos += len;
1,933,714✔
447
  return 0;
1,933,714✔
448
}
449

450
static FORCE_INLINE int32_t tDecodeCStrAndLen(SDecoder* pCoder, char** val, uint32_t* len) {
451
  TAOS_CHECK_RETURN(tDecodeBinary(pCoder, (uint8_t**)val, len));
2,147,483,647✔
452
  if (*len > 0) {  // notice!!!  *len maybe 0
2,147,483,647✔
453
    (*len) -= 1;
2,147,483,647✔
454
  }
455
  return 0;
2,147,483,647✔
456
}
457

458
static FORCE_INLINE int32_t tDecodeCStr(SDecoder* pCoder, char** val) {
459
  uint32_t len = 0;
2,005,595,987✔
460
  return tDecodeCStrAndLen(pCoder, val, &len);
2,005,568,904✔
461
}
462

463
static int32_t tDecodeCStrTo(SDecoder* pCoder, char* val) {
2,147,483,647✔
464
  char*    pStr = NULL;
2,147,483,647✔
465
  uint32_t len = 0;
2,147,483,647✔
466
  TAOS_CHECK_RETURN(tDecodeCStrAndLen(pCoder, &pStr, &len));
2,147,483,647✔
467

468
  if (len < pCoder->size) {
2,147,483,647✔
469
    TAOS_MEMCPY(val, pStr, len + 1);
2,147,483,647✔
470
  }
471

472
  return 0;
2,147,483,647✔
473
}
474

475
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) {
476
  uint64_t length = 0;
1,618,811,868✔
477
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &length));
1,618,933,764✔
478
  if (length) {
1,618,933,764✔
479
    if (len) *len = length;
1,563,282,911✔
480

481
    if (pCoder->pos + length > pCoder->size) {
1,563,343,094✔
482
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
483
    }
484

485
    *val = taosMemoryMalloc(length);
1,563,275,118✔
486
    if (*val == NULL) {
1,563,062,233✔
487
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
488
    }
489

490
    TAOS_MEMCPY(*val, pCoder->data + pCoder->pos, length);
1,563,064,516✔
491

492
    pCoder->pos += length;
1,563,233,878✔
493
  } else {
494
    *val = NULL;
55,650,853✔
495
  }
496
  return 0;
1,618,898,502✔
497
}
498

499
static FORCE_INLINE int32_t tDecodeBinaryAlloc32(SDecoder* pCoder, void** val, uint32_t* len) {
500
  uint32_t length = 0;
2,541✔
501
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
2,541✔
502
  if (length) {
2,541✔
503
    if (len) *len = length;
2,541✔
504

505
    if (pCoder->pos + length > pCoder->size) {
2,541✔
506
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
507
    }
508
    *val = taosMemoryMalloc(length);
2,541✔
509
    if (*val == NULL) {
2,541✔
510
      TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
×
511
    }
512
    TAOS_MEMCPY(*val, pCoder->data + pCoder->pos, length);
2,541✔
513

514
    pCoder->pos += length;
2,541✔
515
  } else {
516
    *val = NULL;
×
517
  }
518
  return 0;
2,541✔
519
}
520

521
static FORCE_INLINE int32_t tDecodeCStrAndLenAlloc(SDecoder* pCoder, char** val, uint64_t* len) {
522
  TAOS_CHECK_RETURN(tDecodeBinaryAlloc(pCoder, (void**)val, len));
720,358,598✔
523
  if (*len > 0) {
720,358,598✔
524
    (*len) -= 1;
720,326,914✔
525
  }
526
  return 0;
720,334,004✔
527
}
528

529
static FORCE_INLINE int32_t tDecodeCStrAlloc(SDecoder* pCoder, char** val) {
530
  uint64_t len = 0;
720,330,755✔
531
  return tDecodeCStrAndLenAlloc(pCoder, val, &len);
720,334,004✔
532
}
533

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

536
static FORCE_INLINE void* tEncoderMalloc(SEncoder* pCoder, int32_t size) {
537
  void*      p = NULL;
2,147,483,647✔
538
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
2,147,483,647✔
539
  if (pMem) {
2,147,483,647✔
540
    pMem->next = pCoder->mList;
2,147,483,647✔
541
    pCoder->mList = pMem;
2,147,483,647✔
542
    p = (void*)&pMem[1];
2,147,483,647✔
543
  }
544
  return p;
2,147,483,647✔
545
}
546

547
static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) {
548
  void*      p = NULL;
2,147,483,647✔
549
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
2,147,483,647✔
550
  if (pMem) {
2,147,483,647✔
551
    pMem->next = pCoder->mList;
2,147,483,647✔
552
    pCoder->mList = pMem;
2,147,483,647✔
553
    p = (void*)&pMem[1];
2,147,483,647✔
554
  }
555
  return p;
2,147,483,647✔
556
}
557

558
// ===========================================
559
#define tPutV(p, v)                    \
560
  do {                                 \
561
    int32_t n = 0;                     \
562
    for (;;) {                         \
563
      if (v <= 0x7f) {                 \
564
        if (p) p[n] = v;               \
565
        n++;                           \
566
        break;                         \
567
      }                                \
568
      if (p) p[n] = (v & 0x7f) | 0x80; \
569
      n++;                             \
570
      v >>= 7;                         \
571
    }                                  \
572
    return n;                          \
573
  } while (0)
574

575
// PUT
576
static FORCE_INLINE int32_t tPutU8(uint8_t* p, uint8_t v) {
577
  if (p) ((uint8_t*)p)[0] = v;
×
578
  return sizeof(uint8_t);
×
579
}
580

581
static FORCE_INLINE int32_t tPutI8(uint8_t* p, int8_t v) {
582
  if (p) ((int8_t*)p)[0] = v;
477,954,395✔
583
  return sizeof(int8_t);
477,954,719✔
584
}
585

586
static FORCE_INLINE int32_t tPutU16(uint8_t* p, uint16_t v) {
587
  if (p) ((uint16_t*)p)[0] = v;
588
  return sizeof(uint16_t);
589
}
590

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

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

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

606
static FORCE_INLINE int32_t tPutU64(uint8_t* p, uint64_t v) {
607
  if (p) ((uint64_t*)p)[0] = v;
41,186,763✔
608
  return sizeof(uint64_t);
41,186,763✔
609
}
610

611
static FORCE_INLINE int32_t tPutI64(uint8_t* p, int64_t v) {
612
  if (p) ((int64_t*)p)[0] = v;
613
  return sizeof(int64_t);
614
}
615

616
static FORCE_INLINE int32_t tPutFloat(uint8_t* p, float f) {
617
  union {
618
    uint32_t ui;
619
    float    f;
620
  } v;
621
  v.f = f;
622

623
  return tPutU32(p, v.ui);
624
}
625

626
static FORCE_INLINE int32_t tPutDouble(uint8_t* p, double d) {
627
  union {
628
    uint64_t ui;
629
    double   d;
630
  } v;
631
  v.d = d;
632

633
  return tPutU64(p, v.ui);
634
}
635

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

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

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

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

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

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

648
// GET
649
static FORCE_INLINE int32_t tGetU8(uint8_t* p, uint8_t* v) {
650
  if (v) *v = ((uint8_t*)p)[0];
×
651
  return sizeof(uint8_t);
×
652
}
653

654
static FORCE_INLINE int32_t tGetI8(uint8_t* p, int8_t* v) {
655
  if (v) *v = ((int8_t*)p)[0];
2,147,483,647✔
656
  return sizeof(int8_t);
2,147,483,647✔
657
}
658

659
static FORCE_INLINE int32_t tGetU16(uint8_t* p, uint16_t* v) {
660
  if (v) *v = ((uint16_t*)p)[0];
661
  return sizeof(uint16_t);
662
}
663

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

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

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

679
static FORCE_INLINE int32_t tGetU64(uint8_t* p, uint64_t* v) {
680
  if (v) *v = ((uint64_t*)p)[0];
30,856,084✔
681
  return sizeof(uint64_t);
30,856,084✔
682
}
683

684
static FORCE_INLINE int32_t tGetI64(uint8_t* p, int64_t* v) {
685
  if (v) *v = ((int64_t*)p)[0];
×
686
  return sizeof(int64_t);
×
687
}
688

689
static FORCE_INLINE int32_t tGetU16v(uint8_t* p, uint16_t* v) {
690
  int32_t n = 0;
2,147,483,647✔
691

692
  if (v) *v = 0;
2,147,483,647✔
693
  for (;;) {
694
    if (p[n] <= 0x7f) {
2,147,483,647✔
695
      if (v) (*v) |= (((uint16_t)p[n]) << (7 * n));
2,147,483,647✔
696
      n++;
2,147,483,647✔
697
      break;
2,147,483,647✔
698
    }
699
    if (v) (*v) |= (((uint16_t)(p[n] & 0x7f)) << (7 * n));
2,147,483,647✔
700
    n++;
2,147,483,647✔
701
  }
702

703
  return n;
2,147,483,647✔
704
}
705

706
static FORCE_INLINE int32_t tGetI16v(uint8_t* p, int16_t* v) {
707
  int32_t  n;
708
  uint16_t tv;
2,147,483,647✔
709

710
  n = tGetU16v(p, &tv);
2,147,483,647✔
711
  if (v) *v = ZIGZAGD(int16_t, tv);
2,147,483,647✔
712

713
  return n;
2,147,483,647✔
714
}
715

716
static FORCE_INLINE int32_t tGetU32v(uint8_t* p, uint32_t* v) {
717
  int32_t n = 0;
2,147,483,647✔
718

719
  if (v) *v = 0;
2,147,483,647✔
720
  for (;;) {
721
    if (p[n] <= 0x7f) {
2,147,483,647✔
722
      if (v) (*v) |= (((uint32_t)p[n]) << (7 * n));
2,147,483,647✔
723
      n++;
2,147,483,647✔
724
      break;
2,147,483,647✔
725
    }
726
    if (v) (*v) |= (((uint32_t)(p[n] & 0x7f)) << (7 * n));
1,709,613,087✔
727
    n++;
1,709,614,187✔
728
  }
729

730
  return n;
2,147,483,647✔
731
}
732

733
static FORCE_INLINE int32_t tGetI32v(uint8_t* p, int32_t* v) {
734
  int32_t  n;
735
  uint32_t tv;
×
736

737
  n = tGetU32v(p, &tv);
×
738
  if (v) *v = ZIGZAGD(int32_t, tv);
×
739

740
  return n;
×
741
}
742

743
static FORCE_INLINE int32_t tGetU64v(uint8_t* p, uint64_t* v) {
744
  int32_t n = 0;
×
745

746
  if (v) *v = 0;
×
747
  for (;;) {
748
    if (p[n] <= 0x7f) {
×
749
      if (v) (*v) |= (((uint64_t)p[n]) << (7 * n));
×
750
      n++;
×
751
      break;
×
752
    }
753
    if (v) (*v) |= (((uint64_t)(p[n] & 0x7f)) << (7 * n));
×
754
    n++;
×
755
  }
756

757
  return n;
×
758
}
759

760
static FORCE_INLINE int32_t tGetI64v(uint8_t* p, int64_t* v) {
761
  int32_t  n;
762
  uint64_t tv;
×
763

764
  n = tGetU64v(p, &tv);
×
765
  if (v) *v = ZIGZAGD(int64_t, tv);
×
766

767
  return n;
×
768
}
769

770
static FORCE_INLINE int32_t tGetFloat(uint8_t* p, float* f) {
771
  int32_t n = 0;
772

773
  union {
774
    uint32_t ui;
775
    float    f;
776
  } v;
777

778
  n = tGetU32(p, &v.ui);
779

780
  *f = v.f;
781
  return n;
782
}
783

784
static FORCE_INLINE int32_t tGetDouble(uint8_t* p, double* d) {
785
  int32_t n = 0;
786

787
  union {
788
    uint64_t ui;
789
    double   d;
790
  } v;
791

792
  n = tGetU64(p, &v.ui);
793

794
  *d = v.d;
795
  return n;
796
}
797

798
// =====================
799
static FORCE_INLINE int32_t tPutBinary(uint8_t* p, uint8_t* pData, uint32_t nData) {
800
  int n = 0;
129,372,569✔
801

802
  n += tPutU32v(p ? p + n : p, nData);
129,372,569✔
803
  if (p) TAOS_MEMCPY(p + n, pData, nData);
129,368,629✔
804
  n += nData;
129,367,109✔
805

806
  return n;
129,367,109✔
807
}
808

809
static FORCE_INLINE int32_t tGetBinary(uint8_t* p, uint8_t** ppData, uint32_t* nData) {
810
  int32_t  n = 0;
1,465,379,890✔
811
  uint32_t nt;
1,465,376,626✔
812

813
  n += tGetU32v(p, &nt);
1,465,448,335✔
814
  if (nData) *nData = nt;
1,465,448,335✔
815
  if (ppData) *ppData = p + n;
1,465,535,468✔
816
  n += nt;
1,465,524,582✔
817

818
  return n;
1,465,524,582✔
819
}
820

821
static FORCE_INLINE int32_t tPutCStr(uint8_t* p, char* pData) {
822
  return tPutBinary(p, (uint8_t*)pData, strlen(pData) + 1);
2,239,960✔
823
}
824
static FORCE_INLINE int32_t tGetCStr(uint8_t* p, char** ppData) { return tGetBinary(p, (uint8_t**)ppData, NULL); }
4,190,010✔
825

826
#ifdef __cplusplus
827
}
828
#endif
829

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