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

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 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) {
182,450,118!
130
    if (pCoder->pos + size > pCoder->size) {
98,697,324!
131
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
132
    }
133
    TAOS_MEMCPY(pCoder->data + pCoder->pos, val, size);
98,697,324✔
134
  }
135

136
  pCoder->pos += size;
195,429,153✔
137
  return 0;
195,429,153✔
138
}
139

140
static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
13,011,756✔
141
static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
2,360,685✔
142
static FORCE_INLINE int32_t tEncodeU16(SEncoder* pCoder, uint16_t val) {
143
  return tEncodeFixed(pCoder, &val, sizeof(val));
595,692✔
144
}
145
static FORCE_INLINE int32_t tEncodeI16(SEncoder* pCoder, int16_t val) {
146
  return tEncodeFixed(pCoder, &val, sizeof(val));
33,168✔
147
}
148
static FORCE_INLINE int32_t tEncodeU32(SEncoder* pCoder, uint32_t val) {
149
  return tEncodeFixed(pCoder, &val, sizeof(val));
552,366✔
150
}
151
static FORCE_INLINE int32_t tEncodeI32(SEncoder* pCoder, int32_t val) {
152
  return tEncodeFixed(pCoder, &val, sizeof(val));
4,553,616✔
153
}
154
static FORCE_INLINE int32_t tEncodeU64(SEncoder* pCoder, uint64_t val) {
155
  return tEncodeFixed(pCoder, &val, sizeof(val));
1,329,354✔
156
}
157
static FORCE_INLINE int32_t tEncodeI64(SEncoder* pCoder, int64_t val) {
158
  return tEncodeFixed(pCoder, &val, sizeof(val));
6,744,693✔
159
}
160
static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) {
161
  while (val >= ENCODE_LIMIT) {
740,889!
162
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
581,556!
163
    val >>= 7;
290,778✔
164
  }
165
  return tEncodeU8(pCoder, val);
900,222!
166
}
167
static FORCE_INLINE int32_t tEncodeI16v(SEncoder* pCoder, int16_t val) {
168
  return tEncodeU16v(pCoder, ZIGZAGE(int16_t, val));
900,222✔
169
}
170
static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) {
171
  while (val >= ENCODE_LIMIT) {
9,515,529!
172
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
5,443,962!
173
    val >>= 7;
2,721,981✔
174
  }
175
  return tEncodeU8(pCoder, val);
13,587,096!
176
}
177
static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) {
178
  return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val));
6,290,832✔
179
}
180
static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) {
181
  while (val >= ENCODE_LIMIT) {
2,678,745!
182
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
548,400!
183
    val >>= 7;
274,200✔
184
  }
185
  return tEncodeU8(pCoder, val);
4,809,090✔
186
}
187
static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) {
188
  return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val));
17,292✔
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;
26,586✔
197

198
  return tEncodeU32(pCoder, v.ui);
53,172✔
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;
11,208✔
207

208
  return tEncodeU64(pCoder, v.ui);
22,416✔
209
}
210

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

213
static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len) {
214
  TAOS_CHECK_RETURN(tEncodeU32v(pCoder, len));
3,648,132!
215
  if (len) {
3,648,132!
216
    if (pCoder->data) {
3,645,495!
217
      if (pCoder->pos + len > pCoder->size) {
1,822,293!
218
        TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
219
      }
220
      TAOS_MEMCPY(pCoder->data + pCoder->pos, val, len);
1,822,293✔
221
    }
222

223
    pCoder->pos += len;
3,645,495✔
224
  }
225
  return 0;
3,648,132✔
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,578,629✔
230
}
231

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

236
/* ------------------------ FOR DECODER ------------------------ */
237
static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size) {
60,942,894✔
238
  if (pCoder->pos + size > pCoder->size) {
60,942,894✔
239
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
10,605✔
240
  } else if (val) {
60,932,289!
241
    TAOS_MEMCPY(val, pCoder->data + pCoder->pos, size);
60,966,363✔
242
  }
243
  pCoder->pos += size;
60,932,289✔
244
  return 0;
60,932,289✔
245
}
246

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

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

253
// 16
254
static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) {
255
  return tDecodeFixed(pCoder, val, sizeof(*val));
442,212✔
256
}
257
static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) {
258
  return tDecodeFixed(pCoder, val, sizeof(*val));
30,489✔
259
}
260
// 32
261
static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) {
262
  return tDecodeFixed(pCoder, val, sizeof(*val));
600,783✔
263
}
264
static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) {
265
  return tDecodeFixed(pCoder, val, sizeof(*val));
9,116,709✔
266
}
267
// 64
268
static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) {
269
  return tDecodeFixed(pCoder, val, sizeof(*val));
374,211✔
270
}
271
static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) {
272
  return tDecodeFixed(pCoder, val, sizeof(*val));
16,346,160✔
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,706,922✔
279
  for (int32_t i = 0;; i++) {
1,706,922✔
280
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
2,352,222!
281
    if (byte < ENCODE_LIMIT) {
2,352,222!
282
      tval |= (((uint16_t)byte) << (7 * i));
1,705,272✔
283
      break;
1,705,272✔
284
    } else {
285
      tval |= ((((uint16_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
646,950✔
286
    }
287
  }
288

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

293
  return 0;
1,705,272✔
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,705,272!
299

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

304
  return 0;
1,705,272✔
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;
15,342,684✔
311
  for (int32_t i = 0;; i++) {
12,746,259✔
312
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
21,390,333!
313
    if (byte < ENCODE_LIMIT) {
21,390,327!
314
      tval |= (((uint32_t)byte) << (7 * i));
15,332,613✔
315
      break;
15,332,613✔
316
    } else {
317
      tval |= ((((uint32_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
6,057,714✔
318
    }
319
  }
×
320

321
  if (val) {
15,332,613!
322
    *val = tval;
15,336,570✔
323
  }
324

325
  return 0;
15,332,613✔
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));
12,718,356!
331

332
  if (val) {
12,718,356!
333
    *val = ZIGZAGD(int32_t, tval);
12,711,279✔
334
  }
335

336
  return 0;
12,718,356✔
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;
7,054,404✔
343
  for (int32_t i = 0;; i++) {
7,003,152✔
344
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
7,118,661!
345
    if (byte < ENCODE_LIMIT) {
7,118,661!
346
      tval |= (((uint64_t)byte) << (7 * i));
7,059,270✔
347
      break;
7,059,270✔
348
    } else {
349
      tval |= ((((uint64_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
59,391✔
350
    }
351
  }
352

353
  if (val) {
7,059,270!
354
    *val = tval;
7,060,044✔
355
  }
356

357
  return 0;
7,059,270✔
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));
7,164!
363

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

368
  return 0;
7,164✔
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)));
12,639!
378

379
  if (val) {
12,639!
380
    *val = v.f;
12,639✔
381
  }
382
  return 0;
12,639✔
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)));
5,352!
392

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

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

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

413
  if (val) {
31,452,003!
414
    *val = pCoder->data + pCoder->pos;
31,451,940✔
415
  }
416

417
  pCoder->pos += size;
31,452,003✔
418
  return 0;
31,452,003✔
419
}
420

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

424
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
2,614,263!
425
  if (len) {
2,614,257!
426
    *len = length;
2,536,308✔
427
  }
428

429
  TAOS_RETURN(tDecodeBinaryWithSize(pCoder, length, val));
5,227,116!
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,411,604!
434
  if (*len > 0) {  // notice!!!  *len maybe 0
2,411,598!
435
    (*len) -= 1;
2,411,610✔
436
  }
437
  return 0;
2,411,598✔
438
}
439

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

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

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

454
  return 0;
2,185,494✔
455
}
456

457
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) {
458
  uint64_t length = 0;
41,859✔
459
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &length));
56,967!
460
  if (length) {
56,967!
461
    if (len) *len = length;
52,434!
462

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

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

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

474
    pCoder->pos += length;
52,437✔
475
  } else {
476
    *val = NULL;
4,533✔
477
  }
478
  return 0;
56,970✔
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));
17,544!
505
  if (*len > 0) {
17,544!
506
    (*len) -= 1;
17,544✔
507
  }
508
  return 0;
17,544✔
509
}
510

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

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

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

529
static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) {
530
  void*      p = NULL;
7,602,288✔
531
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
7,602,288!
532
  if (pMem) {
7,631,712!
533
    pMem->next = pCoder->mList;
7,628,100✔
534
    pCoder->mList = pMem;
7,628,100✔
535
    p = (void*)&pMem[1];
7,628,100✔
536
  }
537
  return p;
7,631,712✔
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;
464,688✔
565
  return sizeof(int8_t);
464,688✔
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); }
936,164,685!
619

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

622
static FORCE_INLINE int32_t tPutU32v(uint8_t* p, uint32_t v) { tPutV(p, v); }
507,291,336!
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];
2,634,570!
638
  return sizeof(int8_t);
2,634,570✔
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;
19,755,648✔
673

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

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

695
  return n;
19,755,648✔
696
}
697

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

701
  if (v) *v = 0;
14,211,987!
702
  for (;;) {
703
    if (p[n] <= 0x7f) {
21,200,856!
704
      if (v) (*v) |= (((uint32_t)p[n]) << (7 * n));
14,205,222!
705
      n++;
14,205,222✔
706
      break;
14,205,222✔
707
    }
708
    if (v) (*v) |= (((uint32_t)(p[n] & 0x7f)) << (7 * n));
6,995,634!
709
    n++;
6,995,634✔
710
  }
711

712
  return n;
14,205,222✔
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;
146,529✔
783

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

788
  return n;
146,529✔
789
}
790

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

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

800
  return n;
755,859✔
801
}
802

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