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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

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

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

83.23
/include/util/tcoding.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_CODING_H_
17
#define _TD_UTIL_CODING_H_
18

19
#include "os.h"
20
#include "tlog.h"
21

22
#ifdef __cplusplus
23
extern "C" {
24
#endif
25

26
#define ENCODE_LIMIT  (((uint8_t)1) << 7)
27
#define ZIGZAGE(T, v) (((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1))  // zigzag encode
28
#define ZIGZAGD(T, v) (((v) >> 1) ^ -((T)((v)&1)))                                 // zigzag decode
29

30
/* ------------------------ LEGACY CODES ------------------------ */
31
#if 1
32
// ---- Fixed U8
33
static FORCE_INLINE int32_t taosEncodeFixedU8(void **buf, uint8_t value) {
34
  if (buf != NULL) {
35
    ((uint8_t *)(*buf))[0] = value;
36
    *buf = POINTER_SHIFT(*buf, sizeof(value));
37
  }
38

39
  return (int32_t)sizeof(value);
40
}
41

42
static FORCE_INLINE void *taosDecodeFixedU8(const void *buf, uint8_t *value) {
43
  *value = ((uint8_t *)buf)[0];
44
  return POINTER_SHIFT(buf, sizeof(*value));
45
}
46

47
// ---- Fixed I8
48
static FORCE_INLINE int32_t taosEncodeFixedI8(void **buf, int8_t value) {
49
  if (buf != NULL) {
61,302,339✔
50
    ((int8_t *)(*buf))[0] = value;
30,656,435✔
51
    *buf = POINTER_SHIFT(*buf, sizeof(value));
30,656,733✔
52
  }
53
  return (int32_t)sizeof(value);
61,301,743✔
54
}
55

56
static FORCE_INLINE void *taosDecodeFixedI8(const void *buf, int8_t *value) {
57
  *value = ((int8_t *)buf)[0];
50,273,013✔
58
  return POINTER_SHIFT(buf, sizeof(*value));
50,273,013✔
59
}
60

61
static FORCE_INLINE void *taosSkipFixedLen(const void *buf, size_t len) { return POINTER_SHIFT(buf, len); }
62

63
// --- Bool
64

65
static FORCE_INLINE int32_t taosEncodeFixedBool(void **buf, bool value) {
66
  if (buf != NULL) {
5,922,485✔
67
    ((int8_t *)(*buf))[0] = (value ? 1 : 0);
2,962,476✔
68
    *buf = POINTER_SHIFT(*buf, sizeof(int8_t));
2,962,476✔
69
  }
70
  return (int32_t)sizeof(int8_t);
5,922,485✔
71
}
72

73
static FORCE_INLINE void *taosDecodeFixedBool(const void *buf, bool *value) {
74
  *value = ((((int8_t *)buf)[0] == 0) ? false : true);
2,764,255✔
75
  return POINTER_SHIFT(buf, sizeof(int8_t));
2,764,255✔
76
}
77

78
// ---- Fixed U16
79
static FORCE_INLINE int32_t taosEncodeFixedU16(void **buf, uint16_t value) {
80
  if (buf != NULL) {
51,049,431✔
81
    if (IS_LITTLE_ENDIAN()) {
25,528,633✔
82
      TAOS_MEMCPY(*buf, &value, sizeof(value));
25,528,801✔
83
    } else {
84
      ((uint8_t *)(*buf))[0] = value & 0xff;
×
85
      ((uint8_t *)(*buf))[1] = (value >> 8) & 0xff;
×
86
    }
87
    *buf = POINTER_SHIFT(*buf, sizeof(value));
25,528,633✔
88
  }
89

90
  return (int32_t)sizeof(value);
51,050,059✔
91
}
92

93
static FORCE_INLINE void *taosDecodeFixedU16(const void *buf, uint16_t *value) {
94
  if (IS_LITTLE_ENDIAN()) {
34,178,680✔
95
    TAOS_MEMCPY(value, buf, sizeof(*value));
34,178,904✔
96
  } else {
97
    ((uint8_t *)value)[1] = ((uint8_t *)buf)[0];
×
98
    ((uint8_t *)value)[0] = ((uint8_t *)buf)[1];
×
99
  }
100

101
  return POINTER_SHIFT(buf, sizeof(*value));
34,178,680✔
102
}
103

104
// ---- Fixed I16
105
static FORCE_INLINE int32_t taosEncodeFixedI16(void **buf, int16_t value) {
106
  return taosEncodeFixedU16(buf, ZIGZAGE(int16_t, value));
40,143,118✔
107
}
108

109
static FORCE_INLINE void *taosDecodeFixedI16(const void *buf, int16_t *value) {
110
  uint16_t tvalue = 0;
19,725,525✔
111
  void    *ret = taosDecodeFixedU16(buf, &tvalue);
19,725,525✔
112
  *value = ZIGZAGD(int16_t, tvalue);
19,725,525✔
113
  return ret;
19,725,525✔
114
}
115

116
// ---- Fixed U32
117
static FORCE_INLINE int32_t taosEncodeFixedU32(void **buf, uint32_t value) {
118
  if (buf != NULL) {
80,461,641✔
119
    if (IS_LITTLE_ENDIAN()) {
40,690,303✔
120
      TAOS_MEMCPY(*buf, &value, sizeof(value));
40,690,415✔
121
    } else {
UNCOV
122
      ((uint8_t *)(*buf))[0] = value & 0xff;
×
UNCOV
123
      ((uint8_t *)(*buf))[1] = (value >> 8) & 0xff;
×
UNCOV
124
      ((uint8_t *)(*buf))[2] = (value >> 16) & 0xff;
×
UNCOV
125
      ((uint8_t *)(*buf))[3] = (value >> 24) & 0xff;
×
126
    }
127
    *buf = POINTER_SHIFT(*buf, sizeof(value));
40,689,707✔
128
  }
129

130
  return (int32_t)sizeof(value);
80,461,641✔
131
}
132

133
static FORCE_INLINE void *taosDecodeFixedU32(const void *buf, uint32_t *value) {
134
  if (IS_LITTLE_ENDIAN()) {
56,800,108✔
135
    TAOS_MEMCPY(value, buf, sizeof(*value));
56,800,332✔
136
  } else {
UNCOV
137
    ((uint8_t *)value)[3] = ((uint8_t *)buf)[0];
×
UNCOV
138
    ((uint8_t *)value)[2] = ((uint8_t *)buf)[1];
×
UNCOV
139
    ((uint8_t *)value)[1] = ((uint8_t *)buf)[2];
×
UNCOV
140
    ((uint8_t *)value)[0] = ((uint8_t *)buf)[3];
×
141
  }
142

143
  return POINTER_SHIFT(buf, sizeof(*value));
56,800,108✔
144
}
145

146
// ---- Fixed I32
147
static FORCE_INLINE int32_t taosEncodeFixedI32(void **buf, int32_t value) {
148
  return taosEncodeFixedU32(buf, ZIGZAGE(int32_t, value));
159,099,174✔
149
}
150

151
static FORCE_INLINE void *taosDecodeFixedI32(const void *buf, int32_t *value) {
152
  uint32_t tvalue = 0;
35,638,404✔
153
  void    *ret = taosDecodeFixedU32(buf, &tvalue);
35,638,404✔
154
  *value = ZIGZAGD(int32_t, tvalue);
35,638,404✔
155
  return ret;
35,638,404✔
156
}
157

158
// ---- Fixed U64
159
static FORCE_INLINE int32_t taosEncodeFixedU64(void **buf, uint64_t value) {
160
  if (buf != NULL) {
32,556,165✔
161
    if (IS_LITTLE_ENDIAN()) {
22,108,407✔
162
      TAOS_MEMCPY(*buf, &value, sizeof(value));
22,107,717✔
163
    } else {
164
      ((uint8_t *)(*buf))[0] = value & 0xff;
690✔
165
      ((uint8_t *)(*buf))[1] = (value >> 8) & 0xff;
392✔
166
      ((uint8_t *)(*buf))[2] = (value >> 16) & 0xff;
392✔
167
      ((uint8_t *)(*buf))[3] = (value >> 24) & 0xff;
392✔
168
      ((uint8_t *)(*buf))[4] = (value >> 32) & 0xff;
392✔
169
      ((uint8_t *)(*buf))[5] = (value >> 40) & 0xff;
392✔
170
      ((uint8_t *)(*buf))[6] = (value >> 48) & 0xff;
392✔
171
      ((uint8_t *)(*buf))[7] = (value >> 56) & 0xff;
392✔
172
    }
173

174
    *buf = POINTER_SHIFT(*buf, sizeof(value));
22,108,109✔
175
  }
176

177
  return (int32_t)sizeof(value);
44,069,453✔
178
}
179

180
static FORCE_INLINE void *taosDecodeFixedU64(const void *buf, uint64_t *value) {
181
  if (IS_LITTLE_ENDIAN()) {
20,197,715✔
182
    TAOS_MEMCPY(value, buf, sizeof(*value));
21,818,722✔
183
  } else {
UNCOV
184
    ((uint8_t *)value)[7] = ((uint8_t *)buf)[0];
×
UNCOV
185
    ((uint8_t *)value)[6] = ((uint8_t *)buf)[1];
×
UNCOV
186
    ((uint8_t *)value)[5] = ((uint8_t *)buf)[2];
×
UNCOV
187
    ((uint8_t *)value)[4] = ((uint8_t *)buf)[3];
×
UNCOV
188
    ((uint8_t *)value)[3] = ((uint8_t *)buf)[4];
×
UNCOV
189
    ((uint8_t *)value)[2] = ((uint8_t *)buf)[5];
×
UNCOV
190
    ((uint8_t *)value)[1] = ((uint8_t *)buf)[6];
×
UNCOV
191
    ((uint8_t *)value)[0] = ((uint8_t *)buf)[7];
×
192
  }
193

194
  return POINTER_SHIFT(buf, sizeof(*value));
21,818,271✔
195
}
196

197
// ---- Fixed I64
198
static FORCE_INLINE int32_t taosEncodeFixedI64(void **buf, int64_t value) {
199
  return taosEncodeFixedU64(buf, ZIGZAGE(int64_t, value));
86,371,364✔
200
}
201

202
static FORCE_INLINE void *taosDecodeFixedI64(const void *buf, int64_t *value) {
203
  uint64_t tvalue = 0;
18,986,493✔
204
  void    *ret = taosDecodeFixedU64(buf, &tvalue);
18,986,493✔
205
  *value = ZIGZAGD(int64_t, tvalue);
18,986,493✔
206
  return ret;
18,986,493✔
207
}
208

209
// ---- Variant U16
210
static FORCE_INLINE int32_t taosEncodeVariantU16(void **buf, uint16_t value) {
211
  int32_t i = 0;
212
  while (value >= ENCODE_LIMIT) {
213
    if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT);
214
    value >>= 7;
215
    i++;
216
  }
217

218
  if (buf != NULL) {
219
    ((uint8_t *)(*buf))[i] = (uint8_t)value;
220
    *buf = POINTER_SHIFT(*buf, i + 1);
221
  }
222

223
  return i + 1;
224
}
225

226
static FORCE_INLINE void *taosDecodeVariantU16(const void *buf, uint16_t *value) {
227
  int32_t  i = 0;
228
  uint16_t tval = 0;
229
  *value = 0;
230
  while (i < 3) {
231
    tval = (uint16_t)(((uint8_t *)buf)[i]);
232
    if (tval < ENCODE_LIMIT) {
233
      (*value) |= (tval << (7 * i));
234
      return POINTER_SHIFT(buf, i + 1);
235
    } else {
236
      (*value) |= ((tval & (ENCODE_LIMIT - 1)) << (7 * i));
237
      i++;
238
    }
239
  }
240

241
  return NULL;  // error happened
242
}
243

244
// ---- Variant I16
245
static FORCE_INLINE int32_t taosEncodeVariantI16(void **buf, int16_t value) {
246
  return taosEncodeVariantU16(buf, ZIGZAGE(int16_t, value));
247
}
248

249
static FORCE_INLINE void *taosDecodeVariantI16(const void *buf, int16_t *value) {
250
  uint16_t tvalue = 0;
251
  void    *ret = taosDecodeVariantU16(buf, &tvalue);
252
  *value = ZIGZAGD(int16_t, tvalue);
253
  return ret;
254
}
255

256
// ---- Variant U32
257
static FORCE_INLINE int32_t taosEncodeVariantU32(void **buf, uint32_t value) {
258
  int32_t i = 0;
21,727,131✔
259
  while (value >= ENCODE_LIMIT) {
21,727,131✔
260
    if (buf != NULL) ((uint8_t *)(*buf))[i] = (value | ENCODE_LIMIT);
×
261
    value >>= 7;
×
262
    i++;
×
263
  }
264

265
  if (buf != NULL) {
21,727,131✔
266
    ((uint8_t *)(*buf))[i] = value;
21,302,043✔
267
    *buf = POINTER_SHIFT(*buf, i + 1);
21,302,043✔
268
  }
269

270
  return i + 1;
21,727,131✔
271
}
272

273
static FORCE_INLINE void *taosDecodeVariantU32(const void *buf, uint32_t *value) {
274
  int32_t  i = 0;
2,147,483,647✔
275
  uint32_t tval = 0;
2,147,483,647✔
276
  *value = 0;
2,147,483,647✔
277
  while (i < 5) {
2,147,483,647✔
278
    tval = (uint32_t)(((uint8_t *)buf)[i]);
2,147,483,647✔
279
    if (tval < ENCODE_LIMIT) {
2,147,483,647✔
280
      (*value) |= (tval << (7 * i));
2,147,483,647✔
281
      return POINTER_SHIFT(buf, i + 1);
2,147,483,647✔
282
    } else {
283
      (*value) |= ((tval & (ENCODE_LIMIT - 1)) << (7 * i));
×
284
      i++;
×
285
    }
286
  }
287

288
  return NULL;  // error happened
×
289
}
290

291
// ---- Variant I32
292
static FORCE_INLINE int32_t taosEncodeVariantI32(void **buf, int32_t value) {
293
  return taosEncodeVariantU32(buf, ZIGZAGE(int32_t, value));
43,454,262✔
294
}
295

296
static FORCE_INLINE void *taosDecodeVariantI32(const void *buf, int32_t *value) {
297
  uint32_t tvalue = 0;
2,147,483,647✔
298
  void    *ret = taosDecodeVariantU32(buf, &tvalue);
2,147,483,647✔
299
  *value = ZIGZAGD(int32_t, tvalue);
2,147,483,647✔
300
  return ret;
2,147,483,647✔
301
}
302

303
// ---- Variant U64
304
static FORCE_INLINE int32_t taosEncodeVariantU64(void **buf, uint64_t value) {
305
  int32_t i = 0;
70,742,829✔
306
  while (value >= ENCODE_LIMIT) {
108,050,853✔
307
    if (buf != NULL) ((uint8_t *)(*buf))[i] = (uint8_t)(value | ENCODE_LIMIT);
37,308,024✔
308
    value >>= 7;
37,308,024✔
309
    i++;
37,308,024✔
310
  }
311

312
  if (buf != NULL) {
70,742,829✔
313
    ((uint8_t *)(*buf))[i] = (uint8_t)value;
45,840,962✔
314
    *buf = POINTER_SHIFT(*buf, i + 1);
45,840,664✔
315
  }
316

317
  return i + 1;
70,742,201✔
318
}
319

320
static FORCE_INLINE void *taosDecodeVariantU64(const void *buf, uint64_t *value) {
321
  int32_t  i = 0;
2,147,483,647✔
322
  uint64_t tval = 0;
2,147,483,647✔
323
  *value = 0;
130,493,213✔
324
  while (i < 10) {
2,147,483,647✔
325
    tval = (uint64_t)(((uint8_t *)buf)[i]);
2,147,483,647✔
326
    if (tval < ENCODE_LIMIT) {
2,147,483,647✔
327
      (*value) |= (tval << (7 * i));
2,147,483,647✔
328
      return POINTER_SHIFT(buf, i + 1);
2,147,483,647✔
329
    } else {
330
      (*value) |= ((tval & (ENCODE_LIMIT - 1)) << (7 * i));
2,147,483,647✔
331
      i++;
2,147,483,647✔
332
    }
333
  }
334

335
  return NULL;  // error happened
×
336
}
337

338
// ---- Variant I64
339
static FORCE_INLINE int32_t taosEncodeVariantI64(void **buf, int64_t value) {
340
  return taosEncodeVariantU64(buf, ZIGZAGE(int64_t, value));
41,830,694✔
341
}
342

343
static FORCE_INLINE void *taosDecodeVariantI64(const void *buf, int64_t *value) {
344
  uint64_t tvalue = 0;
2,147,483,647✔
345
  void    *ret = taosDecodeVariantU64(buf, &tvalue);
2,147,483,647✔
346
  *value = ZIGZAGD(int64_t, tvalue);
2,147,483,647✔
347
  return ret;
2,147,483,647✔
348
}
349

350
// ---- string
351
static FORCE_INLINE int32_t taosEncodeString(void **buf, const char *value) {
352
  int32_t tlen = 0;
49,803,122✔
353
  size_t  size = strlen(value);
49,803,122✔
354

355
  tlen += taosEncodeVariantU64(buf, size);
49,802,494✔
356
  if (buf != NULL) {
49,802,494✔
357
    TAOS_MEMCPY(*buf, value, size);
24,900,047✔
358
    *buf = POINTER_SHIFT(*buf, size);
24,900,345✔
359
  }
360
  tlen += (int32_t)size;
49,803,718✔
361

362
  return tlen;
49,803,718✔
363
}
364

365
static FORCE_INLINE void *taosDecodeString(const void *buf, char **value) {
366
  uint64_t size = 0;
843,036✔
367

368
  buf = taosDecodeVariantU64(buf, &size);
843,036✔
369
  *value = (char *)taosMemoryMalloc((size_t)size + 1);
843,036✔
370

371
  if (*value == NULL) return NULL;
843,036✔
372
  TAOS_MEMCPY(*value, buf, (size_t)size);
843,036✔
373

374
  (*value)[size] = '\0';
843,036✔
375

376
  return POINTER_SHIFT(buf, size);
843,036✔
377
}
378

379
static FORCE_INLINE void *taosDecodeStringTo(const void *buf, char *value) {
380
  uint64_t size = 0;
22,202,057✔
381

382
  buf = taosDecodeVariantU64(buf, &size);
22,202,057✔
383
  TAOS_MEMCPY(value, buf, (size_t)size);
22,202,057✔
384

385
  value[size] = '\0';
22,202,057✔
386

387
  return POINTER_SHIFT(buf, size);
22,202,057✔
388
}
389

390
// ---- binary
391
static FORCE_INLINE int32_t taosEncodeBinary(void **buf, const void *value, int32_t valueLen) {
392
  int32_t tlen = 0;
38,985,710✔
393

394
  if (buf != NULL) {
38,985,710✔
395
    TAOS_MEMCPY(*buf, value, valueLen);
29,933,238✔
396
    *buf = POINTER_SHIFT(*buf, valueLen);
29,933,238✔
397
  }
398
  tlen += (int32_t)valueLen;
38,985,710✔
399

400
  return tlen;
38,985,710✔
401
}
402

403
static FORCE_INLINE void *taosDecodeBinary(const void *buf, void **value, int32_t valueLen) {
404
  *value = taosMemoryMalloc((size_t)valueLen);
7,929,662✔
405
  if (*value == NULL) return NULL;
7,930,110✔
406
  TAOS_MEMCPY(*value, buf, (size_t)valueLen);
7,930,110✔
407

408
  return POINTER_SHIFT(buf, valueLen);
7,930,110✔
409
}
410

411
static FORCE_INLINE void *taosDecodeBinaryTo(const void *buf, void *value, int32_t valueLen) {
412
  TAOS_MEMCPY(value, buf, (size_t)valueLen);
203,727✔
413
  return POINTER_SHIFT(buf, valueLen);
203,727✔
414
}
415

416
#endif
417

418
#ifdef __cplusplus
419
}
420
#endif
421

422
#endif /*_TD_UTIL_CODING_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