tcoding.h 9.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef _TD_CODING_H_
#define _TD_CODING_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <string.h>

#include "tutil.h"

H
hzcheng 已提交
27 28 29
// TODO: move this to a platform file
#define ENCODE_LIMIT (((uint8_t)1) << 7)
static const int32_t TNUMBER = 1;
H
hzcheng 已提交
30
#define IS_LITTLE_ENDIAN() (*(uint8_t *)(&TNUMBER) != 0)
H
hzcheng 已提交
31

H
Hongze Cheng 已提交
32 33 34 35
#define ZIGZAGE(T, v) ((u##T)((v) >> (sizeof(T) * 8 - 1))) ^ (((u##T)(v)) << 1)  // zigzag encode
#define ZIGZAGD(T, v) ((v) >> 1) ^ -((T)((v)&1))                                 // zigzag decode

// ---- Fixed U8
H
TD-353  
Hongze Cheng 已提交
36 37 38 39 40 41 42
static FORCE_INLINE int taosEncodeFixedU8(void **buf, uint8_t value) {
  if (buf != NULL) {
    ((uint8_t *)(*buf))[0] = value;
    *buf = POINTER_SHIFT(*buf, sizeof(value));
  }

  return (int)sizeof(value);
H
TD-185  
Hongze Cheng 已提交
43 44
}

H
Hongze Cheng 已提交
45 46 47 48 49 50
static FORCE_INLINE void *taosDecodeFixedU8(void *buf, uint8_t *value) {
  *value = ((uint8_t *)buf)[0];
  return POINTER_SHIFT(buf, sizeof(*value));
}

// ---- Fixed I8
H
TD-353  
Hongze Cheng 已提交
51 52 53 54 55 56
static FORCE_INLINE int taosEncodeFixedI8(void **buf, int8_t value) {
  if (buf != NULL) {
    ((int8_t *)(*buf))[0] = value;
    *buf = POINTER_SHIFT(*buf, sizeof(value));
  }
  return (int)sizeof(value);
H
Hongze Cheng 已提交
57 58 59 60 61 62 63 64
}

static FORCE_INLINE void *taosDecodeFixedI8(void *buf, int8_t *value) {
  *value = ((int8_t *)buf)[0];
  return POINTER_SHIFT(buf, sizeof(*value));
}

// ---- Fixed U16
H
TD-353  
Hongze Cheng 已提交
65 66 67 68 69 70 71 72 73
static FORCE_INLINE int taosEncodeFixedU16(void **buf, uint16_t value) {
  if (buf != NULL) {
    if (IS_LITTLE_ENDIAN()) {
      memcpy(*buf, &value, sizeof(value));
    } else {
      ((uint8_t *)(*buf))[0] = value & 0xff;
      ((uint8_t *)(*buf))[1] = (value >> 8) & 0xff;
    }
    *buf = POINTER_SHIFT(*buf, sizeof(value));
H
hzcheng 已提交
74 75
  }

H
TD-353  
Hongze Cheng 已提交
76
  return (int)sizeof(value);
H
hzcheng 已提交
77 78
}

H
Hongze Cheng 已提交
79
static FORCE_INLINE void *taosDecodeFixedU16(void *buf, uint16_t *value) {
H
hzcheng 已提交
80
  if (IS_LITTLE_ENDIAN()) {
H
Hongze Cheng 已提交
81
    memcpy(value, buf, sizeof(*value));
H
hzcheng 已提交
82
  } else {
H
Hongze Cheng 已提交
83 84
    ((uint8_t *)value)[1] = ((uint8_t *)buf)[0];
    ((uint8_t *)value)[0] = ((uint8_t *)buf)[1];
H
hzcheng 已提交
85 86
  }

H
Hongze Cheng 已提交
87 88 89 90
  return POINTER_SHIFT(buf, sizeof(*value));
}

// ---- Fixed I16
H
TD-353  
Hongze Cheng 已提交
91
static FORCE_INLINE int taosEncodeFixedI16(void **buf, int16_t value) {
H
Hongze Cheng 已提交
92
  return taosEncodeFixedU16(buf, ZIGZAGE(int16_t, value));
H
hzcheng 已提交
93 94
}

H
Hongze Cheng 已提交
95 96 97 98 99 100 101 102
static FORCE_INLINE void *taosDecodeFixedI16(void *buf, int16_t *value) {
  uint16_t tvalue = 0;
  void *   ret = taosDecodeFixedU16(buf, &tvalue);
  *value = ZIGZAGD(int16_t, tvalue);
  return ret;
}

// ---- Fixed U32
H
TD-353  
Hongze Cheng 已提交
103 104 105 106 107 108 109 110 111 112 113
static FORCE_INLINE int taosEncodeFixedU32(void **buf, uint32_t value) {
  if (buf != NULL) {
    if (IS_LITTLE_ENDIAN()) {
      memcpy(*buf, &value, sizeof(value));
    } else {
      ((uint8_t *)(*buf))[0] = value & 0xff;
      ((uint8_t *)(*buf))[1] = (value >> 8) & 0xff;
      ((uint8_t *)(*buf))[2] = (value >> 16) & 0xff;
      ((uint8_t *)(*buf))[3] = (value >> 24) & 0xff;
    }
    *buf = POINTER_SHIFT(*buf, sizeof(value));
H
hzcheng 已提交
114 115
  }

H
TD-353  
Hongze Cheng 已提交
116
  return (int)sizeof(value);
H
hzcheng 已提交
117 118
}

H
Hongze Cheng 已提交
119
static FORCE_INLINE void *taosDecodeFixedU32(void *buf, uint32_t *value) {
H
hzcheng 已提交
120 121 122
  if (IS_LITTLE_ENDIAN()) {
    memcpy(value, buf, sizeof(*value));
  } else {
H
Hongze Cheng 已提交
123 124 125 126
    ((uint8_t *)value)[3] = ((uint8_t *)buf)[0];
    ((uint8_t *)value)[2] = ((uint8_t *)buf)[1];
    ((uint8_t *)value)[1] = ((uint8_t *)buf)[2];
    ((uint8_t *)value)[0] = ((uint8_t *)buf)[3];
H
hzcheng 已提交
127 128
  }

H
hzcheng 已提交
129
  return POINTER_SHIFT(buf, sizeof(*value));
H
hzcheng 已提交
130 131
}

H
Hongze Cheng 已提交
132
// ---- Fixed I32
H
TD-353  
Hongze Cheng 已提交
133
static FORCE_INLINE int taosEncodeFixedI32(void **buf, int32_t value) {
H
Hongze Cheng 已提交
134 135 136 137 138 139 140 141 142 143 144
  return taosEncodeFixedU32(buf, ZIGZAGE(int32_t, value));
}

static FORCE_INLINE void *taosDecodeFixedI32(void *buf, int32_t *value) {
  uint32_t tvalue = 0;
  void *   ret = taosDecodeFixedU32(buf, &tvalue);
  *value = ZIGZAGD(int32_t, tvalue);
  return ret;
}

// ---- Fixed U64
H
TD-353  
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
static FORCE_INLINE int taosEncodeFixedU64(void **buf, uint64_t value) {
  if (buf != NULL) {
    if (IS_LITTLE_ENDIAN()) {
      memcpy(*buf, &value, sizeof(value));
    } else {
      ((uint8_t *)(*buf))[0] = value & 0xff;
      ((uint8_t *)(*buf))[1] = (value >> 8) & 0xff;
      ((uint8_t *)(*buf))[2] = (value >> 16) & 0xff;
      ((uint8_t *)(*buf))[3] = (value >> 24) & 0xff;
      ((uint8_t *)(*buf))[4] = (value >> 32) & 0xff;
      ((uint8_t *)(*buf))[5] = (value >> 40) & 0xff;
      ((uint8_t *)(*buf))[6] = (value >> 48) & 0xff;
      ((uint8_t *)(*buf))[7] = (value >> 56) & 0xff;
    }

    *buf = POINTER_SHIFT(*buf, sizeof(value));
H
hzcheng 已提交
161 162
  }

H
TD-353  
Hongze Cheng 已提交
163
  return (int)sizeof(value);
H
hzcheng 已提交
164 165
}

H
Hongze Cheng 已提交
166
static FORCE_INLINE void *taosDecodeFixedU64(void *buf, uint64_t *value) {
H
hzcheng 已提交
167 168 169
  if (IS_LITTLE_ENDIAN()) {
    memcpy(value, buf, sizeof(*value));
  } else {
H
hzcheng 已提交
170 171 172 173 174 175 176 177
    ((uint8_t *)value)[7] = ((uint8_t *)buf)[0];
    ((uint8_t *)value)[6] = ((uint8_t *)buf)[1];
    ((uint8_t *)value)[5] = ((uint8_t *)buf)[2];
    ((uint8_t *)value)[4] = ((uint8_t *)buf)[3];
    ((uint8_t *)value)[3] = ((uint8_t *)buf)[4];
    ((uint8_t *)value)[2] = ((uint8_t *)buf)[5];
    ((uint8_t *)value)[1] = ((uint8_t *)buf)[6];
    ((uint8_t *)value)[0] = ((uint8_t *)buf)[7];
H
hzcheng 已提交
178 179
  }

H
hzcheng 已提交
180 181 182
  return POINTER_SHIFT(buf, sizeof(*value));
}

H
Hongze Cheng 已提交
183
// ---- Fixed I64
H
TD-353  
Hongze Cheng 已提交
184
static FORCE_INLINE int taosEncodeFixedI64(void **buf, int64_t value) {
H
Hongze Cheng 已提交
185
  return taosEncodeFixedU64(buf, ZIGZAGE(int64_t, value));
H
hzcheng 已提交
186 187
}

H
Hongze Cheng 已提交
188 189 190 191 192
static FORCE_INLINE void *taosDecodeFixedI64(void *buf, int64_t *value) {
  uint64_t tvalue = 0;
  void *   ret = taosDecodeFixedU64(buf, &tvalue);
  *value = ZIGZAGD(int64_t, tvalue);
  return ret;
H
hzcheng 已提交
193 194
}

H
Hongze Cheng 已提交
195
// ---- Variant U16
H
TD-353  
Hongze Cheng 已提交
196
static FORCE_INLINE int taosEncodeVariantU16(void **buf, uint16_t value) {
H
hzcheng 已提交
197 198
  int i = 0;
  while (value >= ENCODE_LIMIT) {
H
TD-353  
Hongze Cheng 已提交
199
    if (buf != NULL) ((uint8_t *)(*buf))[i] = (value | ENCODE_LIMIT);
H
hzcheng 已提交
200 201
    value >>= 7;
    i++;
H
Hongze Cheng 已提交
202
    ASSERT(i < 3);
H
hzcheng 已提交
203 204
  }

H
TD-353  
Hongze Cheng 已提交
205 206 207 208
  if (buf != NULL) {
    ((uint8_t *)(*buf))[i] = value;
    *buf = POINTER_SHIFT(*buf, i + 1);
  }
H
hzcheng 已提交
209

H
TD-353  
Hongze Cheng 已提交
210
  return i + 1;
H
hzcheng 已提交
211 212
}

H
Hongze Cheng 已提交
213 214
static FORCE_INLINE void *taosDecodeVariantU16(void *buf, uint16_t *value) {
  int      i = 0;
H
hzcheng 已提交
215
  uint16_t tval = 0;
H
hzcheng 已提交
216
  *value = 0;
H
hzcheng 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230
  while (i < 3) {
    tval = (uint16_t)(((uint8_t *)buf)[i]);
    if (tval < ENCODE_LIMIT) {
      (*value) |= (tval << (7 * i));
      return POINTER_SHIFT(buf, i + 1);
    } else {
      (*value) |= ((tval & (ENCODE_LIMIT - 1)) << (7 * i));
      i++;
    }
  }

  return NULL;  // error happened
}

H
Hongze Cheng 已提交
231
// ---- Variant I16
H
TD-353  
Hongze Cheng 已提交
232
static FORCE_INLINE int taosEncodeVariantI16(void **buf, int16_t value) {
H
Hongze Cheng 已提交
233 234 235 236 237 238 239 240 241 242 243
  return taosEncodeVariantU16(buf, ZIGZAGE(int16_t, value));
}

static FORCE_INLINE void *taosDecodeVariantI16(void *buf, int16_t *value) {
  uint16_t tvalue = 0;
  void *   ret = taosDecodeVariantU16(buf, &tvalue);
  *value = ZIGZAGD(int16_t, tvalue);
  return ret;
}

// ---- Variant U32
H
TD-353  
Hongze Cheng 已提交
244
static FORCE_INLINE int taosEncodeVariantU32(void **buf, uint32_t value) {
H
hzcheng 已提交
245
  int i = 0;
H
Hongze Cheng 已提交
246
  while (value >= ENCODE_LIMIT) {
H
TD-353  
Hongze Cheng 已提交
247
    if (buf != NULL) ((uint8_t *)(*buf))[i] = (value | ENCODE_LIMIT);
H
Hongze Cheng 已提交
248 249 250 251 252
    value >>= 7;
    i++;
    ASSERT(i < 5);
  }

H
TD-353  
Hongze Cheng 已提交
253 254 255 256
  if (buf != NULL) {
    ((uint8_t *)(*buf))[i] = value;
    *buf = POINTER_SHIFT(*buf, i + 1);
  }
H
Hongze Cheng 已提交
257

H
TD-353  
Hongze Cheng 已提交
258
  return i + 1;
H
Hongze Cheng 已提交
259 260 261 262
}

static FORCE_INLINE void *taosDecodeVariantU32(void *buf, uint32_t *value) {
  int      i = 0;
H
hzcheng 已提交
263 264 265 266 267 268 269 270 271 272 273
  uint32_t tval = 0;
  *value = 0;
  while (i < 5) {
    tval = (uint32_t)(((uint8_t *)buf)[i]);
    if (tval < ENCODE_LIMIT) {
      (*value) |= (tval << (7 * i));
      return POINTER_SHIFT(buf, i + 1);
    } else {
      (*value) |= ((tval & (ENCODE_LIMIT - 1)) << (7 * i));
      i++;
    }
H
hzcheng 已提交
274 275
  }

H
hzcheng 已提交
276
  return NULL;  // error happened
H
hzcheng 已提交
277 278
}

H
Hongze Cheng 已提交
279
// ---- Variant I32
H
TD-353  
Hongze Cheng 已提交
280
static FORCE_INLINE int taosEncodeVariantI32(void **buf, int32_t value) {
H
Hongze Cheng 已提交
281 282 283 284 285 286 287 288 289 290 291
  return taosEncodeVariantU32(buf, ZIGZAGE(int32_t, value));
}

static FORCE_INLINE void *taosDecodeVariantI32(void *buf, int32_t *value) {
  uint32_t tvalue = 0;
  void *   ret = taosDecodeVariantU32(buf, &tvalue);
  *value = ZIGZAGD(int32_t, tvalue);
  return ret;
}

// ---- Variant U64
H
TD-353  
Hongze Cheng 已提交
292
static FORCE_INLINE int taosEncodeVariantU64(void **buf, uint64_t value) {
H
hzcheng 已提交
293
  int i = 0;
H
Hongze Cheng 已提交
294
  while (value >= ENCODE_LIMIT) {
H
TD-353  
Hongze Cheng 已提交
295
    if (buf != NULL) ((uint8_t *)(*buf))[i] = (value | ENCODE_LIMIT);
H
Hongze Cheng 已提交
296 297 298 299 300
    value >>= 7;
    i++;
    ASSERT(i < 10);
  }

H
TD-353  
Hongze Cheng 已提交
301 302 303 304
  if (buf != NULL) {
    ((uint8_t *)(*buf))[i] = value;
    *buf = POINTER_SHIFT(*buf, i + 1);
  }
H
Hongze Cheng 已提交
305

H
TD-353  
Hongze Cheng 已提交
306
  return i + 1;
H
Hongze Cheng 已提交
307 308 309 310
}

static FORCE_INLINE void *taosDecodeVariantU64(void *buf, uint64_t *value) {
  int      i = 0;
H
hzcheng 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  uint64_t tval = 0;
  *value = 0;
  while (i < 10) {
    tval = (uint64_t)(((uint8_t *)buf)[i]);
    if (tval < ENCODE_LIMIT) {
      (*value) |= (tval << (7 * i));
      return POINTER_SHIFT(buf, i + 1);
    } else {
      (*value) |= ((tval & (ENCODE_LIMIT - 1)) << (7 * i));
      i++;
    }
  }

  return NULL;  // error happened
}
H
hzcheng 已提交
326

H
Hongze Cheng 已提交
327
// ---- Variant I64
H
TD-353  
Hongze Cheng 已提交
328
static FORCE_INLINE int taosEncodeVariantI64(void **buf, int64_t value) {
H
Hongze Cheng 已提交
329 330 331 332 333 334 335 336 337 338 339
  return taosEncodeVariantU64(buf, ZIGZAGE(int64_t, value));
}

static FORCE_INLINE void *taosDecodeVariantI64(void *buf, int64_t *value) {
  uint64_t tvalue = 0;
  void *   ret = taosDecodeVariantU64(buf, &tvalue);
  *value = ZIGZAGD(int64_t, tvalue);
  return ret;
}

// ---- string
H
TD-353  
Hongze Cheng 已提交
340 341
static FORCE_INLINE int taosEncodeString(void **buf, char *value) {
  int    tlen = 0;
H
TD-354  
Hongze Cheng 已提交
342 343
  size_t size = strlen(value);

H
TD-353  
Hongze Cheng 已提交
344 345 346 347 348 349
  tlen += taosEncodeVariantU64(buf, size);
  if (buf != NULL) {
    memcpy(*buf, value, size);
    *buf = POINTER_SHIFT(*buf, size);
  }
  tlen += size;
H
TD-354  
Hongze Cheng 已提交
350

H
TD-353  
Hongze Cheng 已提交
351
  return tlen;
H
TD-354  
Hongze Cheng 已提交
352 353 354 355 356
}

static FORCE_INLINE void *taosDecodeString(void *buf, char **value) {
  uint64_t size = 0;

H
Hongze Cheng 已提交
357
  buf = taosDecodeVariantU64(buf, &size);
H
TD-354  
Hongze Cheng 已提交
358 359 360 361 362 363 364 365 366
  *value = (char *)malloc(size + 1);
  if (*value == NULL) return NULL;
  memcpy(*value, buf, size);

  (*value)[size] = '\0';

  return POINTER_SHIFT(buf, size);
}

H
hzcheng 已提交
367 368 369 370 371
#ifdef __cplusplus
}
#endif

#endif