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

taosdata / TDengine / #3814

01 Apr 2025 05:36AM UTC coverage: 30.169% (-3.6%) from 33.798%
#3814

push

travis-ci

happyguoxy
test:add build cmake

129680 of 592945 branches covered (21.87%)

Branch coverage included in aggregate %.

196213 of 487270 relevant lines covered (40.27%)

555639.42 hits per line

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

38.12
/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) {
49,659,199!
130
    if (pCoder->pos + size > pCoder->size) {
25,743,187!
131
      TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
132
    }
133
    TAOS_MEMCPY(pCoder->data + pCoder->pos, val, size);
25,743,187✔
134
  }
135

136
  pCoder->pos += size;
51,108,047✔
137
  return 0;
51,108,047✔
138
}
139

140
static FORCE_INLINE int32_t tEncodeU8(SEncoder* pCoder, uint8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
1,461,781✔
141
static FORCE_INLINE int32_t tEncodeI8(SEncoder* pCoder, int8_t val) { return tEncodeFixed(pCoder, &val, sizeof(val)); }
297,170✔
142
static FORCE_INLINE int32_t tEncodeU16(SEncoder* pCoder, uint16_t val) {
143
  return tEncodeFixed(pCoder, &val, sizeof(val));
23,029✔
144
}
145
static FORCE_INLINE int32_t tEncodeI16(SEncoder* pCoder, int16_t val) {
146
  return tEncodeFixed(pCoder, &val, sizeof(val));
1,882✔
147
}
148
static FORCE_INLINE int32_t tEncodeU32(SEncoder* pCoder, uint32_t val) {
149
  return tEncodeFixed(pCoder, &val, sizeof(val));
51,215✔
150
}
151
static FORCE_INLINE int32_t tEncodeI32(SEncoder* pCoder, int32_t val) {
152
  return tEncodeFixed(pCoder, &val, sizeof(val));
657,072✔
153
}
154
static FORCE_INLINE int32_t tEncodeU64(SEncoder* pCoder, uint64_t val) {
155
  return tEncodeFixed(pCoder, &val, sizeof(val));
261,065✔
156
}
157
static FORCE_INLINE int32_t tEncodeI64(SEncoder* pCoder, int64_t val) {
158
  return tEncodeFixed(pCoder, &val, sizeof(val));
1,385,408✔
159
}
160
static FORCE_INLINE int32_t tEncodeU16v(SEncoder* pCoder, uint16_t val) {
161
  while (val >= ENCODE_LIMIT) {
14,905!
162
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
×
163
    val >>= 7;
×
164
  }
165
  return tEncodeU8(pCoder, val);
29,810!
166
}
167
static FORCE_INLINE int32_t tEncodeI16v(SEncoder* pCoder, int16_t val) {
168
  return tEncodeU16v(pCoder, ZIGZAGE(int16_t, val));
29,810✔
169
}
170
static FORCE_INLINE int32_t tEncodeU32v(SEncoder* pCoder, uint32_t val) {
171
  while (val >= ENCODE_LIMIT) {
975,381!
172
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
501,750!
173
    val >>= 7;
250,875✔
174
  }
175
  return tEncodeU8(pCoder, val);
1,449,012!
176
}
177
static FORCE_INLINE int32_t tEncodeI32v(SEncoder* pCoder, int32_t val) {
178
  return tEncodeU32v(pCoder, ZIGZAGE(int32_t, val));
966,432✔
179
}
180
static FORCE_INLINE int32_t tEncodeU64v(SEncoder* pCoder, uint64_t val) {
181
  while (val >= ENCODE_LIMIT) {
454,154!
182
    TAOS_CHECK_RETURN(tEncodeU8(pCoder, (val | ENCODE_LIMIT) & 0xff));
168,770!
183
    val >>= 7;
84,385✔
184
  }
185
  return tEncodeU8(pCoder, val);
739,538✔
186
}
187
static FORCE_INLINE int32_t tEncodeI64v(SEncoder* pCoder, int64_t val) {
188
  return tEncodeU64v(pCoder, ZIGZAGE(int64_t, val));
3,100✔
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;
1,476✔
197

198
  return tEncodeU32(pCoder, v.ui);
2,952✔
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;
2,476✔
207

208
  return tEncodeU64(pCoder, v.ui);
4,952✔
209
}
210

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

213
static FORCE_INLINE int32_t tEncodeBinary(SEncoder* pCoder, const uint8_t* val, uint32_t len) {
214
  TAOS_CHECK_RETURN(tEncodeU32v(pCoder, len));
241,290!
215
  if (len) {
241,290!
216
    if (pCoder->data) {
240,974!
217
      if (pCoder->pos + len > pCoder->size) {
120,536!
218
        TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
×
219
      }
220
      TAOS_MEMCPY(pCoder->data + pCoder->pos, val, len);
120,536✔
221
    }
222

223
    pCoder->pos += len;
240,974✔
224
  }
225
  return 0;
241,290✔
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);
228,266✔
230
}
231

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

236
/* ------------------------ FOR DECODER ------------------------ */
237
static int32_t tDecodeFixed(SDecoder* pCoder, void* val, uint32_t size) {
7,719,841✔
238
  if (pCoder->pos + size > pCoder->size) {
7,719,841✔
239
    TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
401✔
240
  } else if (val) {
7,719,440!
241
    TAOS_MEMCPY(val, pCoder->data + pCoder->pos, size);
7,724,484✔
242
  }
243
  pCoder->pos += size;
7,719,440✔
244
  return 0;
7,719,440✔
245
}
246

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

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

253
// 16
254
static FORCE_INLINE int32_t tDecodeU16(SDecoder* pCoder, uint16_t* val) {
255
  return tDecodeFixed(pCoder, val, sizeof(*val));
56,467✔
256
}
257
static FORCE_INLINE int32_t tDecodeI16(SDecoder* pCoder, int16_t* val) {
258
  return tDecodeFixed(pCoder, val, sizeof(*val));
4,942✔
259
}
260
// 32
261
static FORCE_INLINE int32_t tDecodeU32(SDecoder* pCoder, uint32_t* val) {
262
  return tDecodeFixed(pCoder, val, sizeof(*val));
71,136✔
263
}
264
static FORCE_INLINE int32_t tDecodeI32(SDecoder* pCoder, int32_t* val) {
265
  return tDecodeFixed(pCoder, val, sizeof(*val));
2,327,743✔
266
}
267
// 64
268
static FORCE_INLINE int32_t tDecodeU64(SDecoder* pCoder, uint64_t* val) {
269
  return tDecodeFixed(pCoder, val, sizeof(*val));
37,845✔
270
}
271
static FORCE_INLINE int32_t tDecodeI64(SDecoder* pCoder, int64_t* val) {
272
  return tDecodeFixed(pCoder, val, sizeof(*val));
1,671,994✔
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;
144,542✔
279
  for (int32_t i = 0;; i++) {
144,542✔
280
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
144,551!
281
    if (byte < ENCODE_LIMIT) {
144,551!
282
      tval |= (((uint16_t)byte) << (7 * i));
144,557✔
283
      break;
144,557✔
284
    } else {
285
      tval |= ((((uint16_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
×
286
    }
287
  }
288

289
  if (val) {
144,557!
290
    *val = tval;
144,554✔
291
  }
292

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

300
  if (val) {
144,557!
301
    *val = ZIGZAGD(int16_t, tval);
144,556✔
302
  }
303

304
  return 0;
144,557✔
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;
1,705,364✔
311
  for (int32_t i = 0;; i++) {
1,434,388✔
312
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
2,422,027!
313
    if (byte < ENCODE_LIMIT) {
2,422,025!
314
      tval |= (((uint32_t)byte) << (7 * i));
1,702,750✔
315
      break;
1,702,750✔
316
    } else {
317
      tval |= ((((uint32_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
719,275✔
318
    }
319
  }
×
320

321
  if (val) {
1,702,750!
322
    *val = tval;
1,702,991✔
323
  }
324

325
  return 0;
1,702,750✔
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));
1,430,552!
331

332
  if (val) {
1,430,552!
333
    *val = ZIGZAGD(int32_t, tval);
1,429,119✔
334
  }
335

336
  return 0;
1,430,552✔
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;
715,993✔
343
  for (int32_t i = 0;; i++) {
709,036✔
344
    TAOS_CHECK_RETURN(tDecodeU8(pCoder, &byte));
726,036!
345
    if (byte < ENCODE_LIMIT) {
726,036!
346
      tval |= (((uint64_t)byte) << (7 * i));
717,299✔
347
      break;
717,299✔
348
    } else {
349
      tval |= ((((uint64_t)byte) & (ENCODE_LIMIT - 1)) << (7 * i));
9,068✔
350
    }
351
  }
352

353
  if (val) {
717,299!
354
    *val = tval;
717,389✔
355
  }
356

357
  return 0;
717,299✔
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));
1,211!
363

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

368
  return 0;
1,211✔
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)));
657!
378

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

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

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

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

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

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

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

424
  TAOS_CHECK_RETURN(tDecodeU32v(pCoder, &length));
272,200!
425
  if (len) {
272,198!
426
    *len = length;
271,998✔
427
  }
428

429
  TAOS_RETURN(tDecodeBinaryWithSize(pCoder, length, val));
544,388!
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));
256,088!
434
  if (*len > 0) {  // notice!!!  *len maybe 0
256,086!
435
    (*len) -= 1;
256,082✔
436
  }
437
  return 0;
256,086✔
438
}
439

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

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

450
  if (len < pCoder->size) {
227,010✔
451
    TAOS_MEMCPY(val, pStr, len + 1);
227,006✔
452
  }
453

454
  return 0;
227,010✔
455
}
456

457
static FORCE_INLINE int32_t tDecodeBinaryAlloc(SDecoder* pCoder, void** val, uint64_t* len) {
458
  uint64_t length = 0;
7,225✔
459
  TAOS_CHECK_RETURN(tDecodeU64v(pCoder, &length));
8,595!
460
  if (length) {
8,595!
461
    if (len) *len = length;
7,388!
462

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

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

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

474
    pCoder->pos += length;
7,388✔
475
  } else {
476
    *val = NULL;
1,207✔
477
  }
478
  return 0;
8,595✔
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));
1,696!
505
  if (*len > 0) {
1,696!
506
    (*len) -= 1;
1,696✔
507
  }
508
  return 0;
1,696✔
509
}
510

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

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

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

529
static FORCE_INLINE void* tDecoderMalloc(SDecoder* pCoder, int32_t size) {
530
  void*      p = NULL;
2,093,642✔
531
  SCoderMem* pMem = (SCoderMem*)taosMemoryCalloc(1, sizeof(*pMem) + size);
2,093,642!
532
  if (pMem) {
2,103,415!
533
    pMem->next = pCoder->mList;
2,102,195✔
534
    pCoder->mList = pMem;
2,102,195✔
535
    p = (void*)&pMem[1];
2,102,195✔
536
  }
537
  return p;
2,103,415✔
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;
115,406✔
565
  return sizeof(int8_t);
115,406✔
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); }
257,305,375!
619

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

622
static FORCE_INLINE int32_t tPutU32v(uint8_t* p, uint32_t v) { tPutV(p, v); }
157,056,979!
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];
36,120!
638
  return sizeof(int8_t);
36,120✔
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;
25,544✔
673

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

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

695
  return n;
25,544✔
696
}
697

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

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

712
  return n;
252,278✔
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;
40,330✔
783

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

788
  return n;
40,330✔
789
}
790

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

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

800
  return n;
7,860✔
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