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

taosdata / TDengine / #3824

01 Apr 2025 12:26PM UTC coverage: 34.064% (-0.001%) from 34.065%
#3824

push

travis-ci

happyguoxy
test:alter gcda dir

148483 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222466 of 489445 relevant lines covered (45.45%)

762427.9 hits per line

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

42.25
/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) {
60,650,694!
130
    if (pCoder->pos + size > pCoder->size) {
32,742,534!
131
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
132
    }
133
    TAOS_MEMCPY(pCoder->data + pCoder->pos, val, size);
32,742,534✔
134
  }
135

136
  pCoder->pos += size;
64,849,553✔
137
  return 0;
64,849,553✔
138
}
139

140
static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
4,210,263✔
141
static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
771,565✔
142
static FORCE_INLINE int32_t tEncodeU16(SEncoder* pCoder, uint16_t val) {
143
  return tEncodeFixed(pCoder, &val, sizeof(val));
179,320✔
144
}
145
static FORCE_INLINE int32_t tEncodeI16(SEncoder* pCoder, int16_t val) {
146
  return tEncodeFixed(pCoder, &val, sizeof(val));
10,322✔
147
}
148
static FORCE_INLINE int32_t tEncodeU32(SEncoder* pCoder, uint32_t val) {
149
  return tEncodeFixed(pCoder, &val, sizeof(val));
174,661✔
150
}
151
static FORCE_INLINE int32_t tEncodeI32(SEncoder* pCoder, int32_t val) {
152
  return tEncodeFixed(pCoder, &val, sizeof(val));
1,474,925✔
153
}
154
static FORCE_INLINE int32_t tEncodeU64(SEncoder* pCoder, uint64_t val) {
155
  return tEncodeFixed(pCoder, &val, sizeof(val));
438,009✔
156
}
157
static FORCE_INLINE int32_t tEncodeI64(SEncoder* pCoder, int64_t val) {
158
  return tEncodeFixed(pCoder, &val, sizeof(val));
2,196,200✔
159
}
160
static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) {
161
  while (val >= ENCODE_LIMIT) {
245,671!
162
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
192,218!
163
    val >>= 7;
96,109✔
164
  }
165
  return tEncodeU8(pCoder, val);
299,124!
166
}
167
static FORCE_INLINE int32_t tEncodeI16v(SEncoder* pCoder, int16_t val) {
168
  return tEncodeU16v(pCoder, ZIGZAGE(int16_t, val));
299,124✔
169
}
170
static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) {
171
  while (val >= ENCODE_LIMIT) {
3,081,636!
172
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
1,757,304!
173
    val >>= 7;
878,652✔
174
  }
175
  return tEncodeU8(pCoder, val);
4,405,968!
176
}
177
static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) {
178
  return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val));
2,026,464✔
179
}
180
static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) {
181
  while (val >= ENCODE_LIMIT) {
857,424!
182
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
183,034!
183
    val >>= 7;
91,517✔
184
  }
185
  return tEncodeU8(pCoder, val);
1,531,814✔
186
}
187
static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) {
188
  return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val));
6,072✔
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;
8,318✔
197

198
  return tEncodeU32(pCoder, v.ui);
16,636✔
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;
3,784✔
207

208
  return tEncodeU64(pCoder, v.ui);
7,568✔
209
}
210

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

213
static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len) {
214
  TAOS_CHECK_RETURN(tEncodeU32v(pCoder, len));
1,189,752!
215
  if (len) {
1,189,752!
216
    if (pCoder->data) {
1,189,107!
217
      if (pCoder->pos + len > pCoder->size) {
594,455!
218
        TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
219
      }
220
      TAOS_MEMCPY(pCoder->data + pCoder->pos, val, len);
594,455✔
221
    }
222

223
    pCoder->pos += len;
1,189,107✔
224
  }
225
  return 0;
1,189,752✔
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);
815,234✔
230
}
231

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

236
/* ------------------------ FOR DECODER ------------------------ */
237
static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size) {
19,718,056✔
238
  if (pCoder->pos + size > pCoder->size) {
19,718,056✔
239
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
3,397✔
240
  } else if (val) {
19,714,659!
241
    TAOS_MEMCPY(val, pCoder->data + pCoder->pos, size);
19,727,013✔
242
  }
243
  pCoder->pos += size;
19,714,659✔
244
  return 0;
19,714,659✔
245
}
246

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

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

253
// 16
254
static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) {
255
  return tDecodeFixed(pCoder, val, sizeof(*val));
136,991✔
256
}
257
static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) {
258
  return tDecodeFixed(pCoder, val, sizeof(*val));
9,785✔
259
}
260
// 32
261
static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) {
262
  return tDecodeFixed(pCoder, val, sizeof(*val));
198,217✔
263
}
264
static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) {
265
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,931,401✔
266
}
267
// 64
268
static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) {
269
  return tDecodeFixed(pCoder, val, sizeof(*val));
122,928✔
270
}
271
static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) {
272
  return tDecodeFixed(pCoder, val, sizeof(*val));
5,299,112✔
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;
572,632✔
279
  for (int32_t i = 0;; i++) {
572,632✔
280
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
783,239!
281
    if (byte < ENCODE_LIMIT) {
783,239!
282
      tval |= (((uint16_t)byte) << (7 * i));
572,388✔
283
      break;
572,388✔
284
    } else {
285
      tval |= ((((uint16_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
210,851✔
286
    }
287
  }
288

289
  if (val) {
572,388!
290
    *val = tval;
572,686✔
291
  }
292

293
  return 0;
572,388✔
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));
572,388!
299

300
  if (val) {
572,388!
301
    *val = ZIGZAGD(int16_t, tval);
572,846✔
302
  }
303

304
  return 0;
572,388✔
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,993,140✔
311
  for (int32_t i = 0;; i++) {
4,146,413✔
312
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
6,947,298!
313
    if (byte < ENCODE_LIMIT) {
6,947,296!
314
      tval |= (((uint32_t)byte) << (7 * i));
4,990,822✔
315
      break;
4,990,822✔
316
    } else {
317
      tval |= ((((uint32_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
1,956,474✔
318
    }
319
  }
×
320

321
  if (val) {
4,990,822!
322
    *val = tval;
4,992,041✔
323
  }
324

325
  return 0;
4,990,822✔
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));
4,138,300!
331

332
  if (val) {
4,138,300!
333
    *val = ZIGZAGD(int32_t, tval);
4,136,430✔
334
  }
335

336
  return 0;
4,138,300✔
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;
2,264,014✔
343
  for (int32_t i = 0;; i++) {
2,246,784✔
344
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
2,285,571!
345
    if (byte < ENCODE_LIMIT) {
2,285,571!
346
      tval |= (((uint64_t)byte) << (7 * i));
2,264,796✔
347
      break;
2,264,796✔
348
    } else {
349
      tval |= ((((uint64_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
20,775✔
350
    }
351
  }
352

353
  if (val) {
2,264,796!
354
    *val = tval;
2,265,074✔
355
  }
356

357
  return 0;
2,264,796✔
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));
2,582!
363

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

368
  return 0;
2,582✔
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)));
3,944!
378

379
  if (val) {
3,944!
380
    *val = v.f;
3,944✔
381
  }
382
  return 0;
3,944✔
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)));
1,844!
392

393
  if (val) {
1,844!
394
    *val = v.d;
1,844✔
395
  }
396
  return 0;
1,844✔
397
}
398

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

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

413
  if (val) {
10,206,148!
414
    *val = pCoder->data + pCoder->pos;
10,205,990✔
415
  }
416

417
  pCoder->pos += size;
10,206,148✔
418
  return 0;
10,206,148✔
419
}
420

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

424
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
852,524!
425
  if (len) {
852,522!
426
    *len = length;
830,089✔
427
  }
428

429
  TAOS_RETURN(tDecodeBinaryWithSize(pCoder, length, val));
1,704,962!
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));
789,789!
434
  if (*len > 0) {  // notice!!!  *len maybe 0
789,787!
435
    (*len) -= 1;
789,665✔
436
  }
437
  return 0;
789,787✔
438
}
439

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

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

450
  if (len < pCoder->size) {
716,932✔
451
    TAOS_MEMCPY(val, pStr, len + 1);
715,763✔
452
  }
453

454
  return 0;
716,932✔
455
}
456

457
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) {
458
  uint64_t length = 0;
14,293✔
459
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &length));
19,486!
460
  if (length) {
19,486!
461
    if (len) *len = length;
17,968!
462

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

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

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

474
    pCoder->pos += length;
17,970✔
475
  } else {
476
    *val = NULL;
1,518✔
477
  }
478
  return 0;
19,488✔
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));
6,003!
505
  if (*len > 0) {
6,003!
506
    (*len) -= 1;
6,002✔
507
  }
508
  return 0;
6,003✔
509
}
510

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

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

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

529
static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) {
530
  void*      p = NULL;
2,443,735✔
531
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
2,443,735!
532
  if (pMem) {
2,452,800!
533
    pMem->next = pCoder->mList;
2,451,554✔
534
    pCoder->mList = pMem;
2,451,554✔
535
    p = (void*)&pMem[1];
2,451,554✔
536
  }
537
  return p;
2,452,800✔
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;
145,999✔
565
  return sizeof(int8_t);
145,999✔
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); }
310,830,212!
619

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

622
static FORCE_INLINE int32_t tPutU32v(uint8_t* p, uint32_t v) { tPutV(p, v); }
168,950,165!
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];
841,481!
638
  return sizeof(int8_t);
841,481✔
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;
6,910,111✔
673

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

685
  return n;
6,910,111✔
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);
6,910,111✔
693
  if (v) *v = ZIGZAGD(int16_t, tv);
6,910,111!
694

695
  return n;
6,910,111✔
696
}
697

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

701
  if (v) *v = 0;
4,909,147!
702
  for (;;) {
703
    if (p[n] <= 0x7f) {
7,277,457!
704
      if (v) (*v) |= (((uint32_t)p[n]) << (7 * n));
4,908,176!
705
      n++;
4,908,176✔
706
      break;
4,908,176✔
707
    }
708
    if (v) (*v) |= (((uint32_t)(p[n] & 0x7f)) << (7 * n));
2,369,281!
709
    n++;
2,369,281✔
710
  }
711

712
  return n;
4,908,176✔
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;
44,318✔
783

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

788
  return n;
44,318✔
789
}
790

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

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

800
  return n;
244,980✔
801
}
802

803
static FORCE_INLINE int32_t tPutCStr(uint8_t* p, char* pData) {
804
  return tPutBinary(p, (uint8_t*)pData, strlen(pData) + 1);
124✔
805
}
806
static FORCE_INLINE int32_t tGetCStr(uint8_t* p, char** ppData) { return tGetBinary(p, (uint8_t**)ppData, NULL); }
984✔
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