tcoding.h 4.6 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
const int32_t TNUMBER = 1;
const uint8_t ENCODE_LIMIT = (1 << 7);
#define IS_LITTLE_ENDIAN() (*(uint8_t *)(&TNUMBER) != 0)
H
hzcheng 已提交
30 31 32 33 34

static FORCE_INLINE void *taosEncodeFixed16(void *buf, uint16_t value) {
  if (IS_LITTLE_ENDIAN()) {
    memcpy(buf, &value, sizeof(value));
  } else {
H
hzcheng 已提交
35 36
    ((uint8_t *)buf)[0] = value & 0xff;
    ((uint8_t *)buf)[1] = (value >> 8) & 0xff;
H
hzcheng 已提交
37 38
  }

H
hzcheng 已提交
39
  return POINTER_SHIFT(buf, sizeof(value));
H
hzcheng 已提交
40 41 42 43 44 45
}

static FORCE_INLINE void *taosEncodeFixed32(void *buf, uint32_t value) {
  if (IS_LITTLE_ENDIAN()) {
    memcpy(buf, &value, sizeof(value));
  } else {
H
hzcheng 已提交
46 47 48 49
    ((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;
H
hzcheng 已提交
50 51
  }

H
hzcheng 已提交
52
  return POINTER_SHIFT(buf, sizeof(value));
H
hzcheng 已提交
53 54 55 56 57 58
}

static FORCE_INLINE void *taosEncodeFixed64(void *buf, uint64_t value) {
  if (IS_LITTLE_ENDIAN()) {
    memcpy(buf, &value, sizeof(value));
  } else {
H
hzcheng 已提交
59 60 61 62 63 64 65 66
    ((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;
H
hzcheng 已提交
67 68
  }

H
hzcheng 已提交
69
  return POINTER_SHIFT(buf, sizeof(value));
H
hzcheng 已提交
70 71 72 73 74 75
}

static FORCE_INLINE void *taosDecodeFixed16(void *buf, uint16_t *value) {
  if (IS_LITTLE_ENDIAN()) {
    memcpy(value, buf, sizeof(*value));
  } else {
H
hzcheng 已提交
76 77
    ((uint8_t *)value)[1] = ((uint8_t *)buf)[0];
    ((uint8_t *)value)[0] = ((uint8_t *)buf)[1];
H
hzcheng 已提交
78 79
  }

H
hzcheng 已提交
80
  return POINTER_SHIFT(buf, sizeof(*value));
H
hzcheng 已提交
81 82 83 84 85 86
}

static FORCE_INLINE void *taosDecodeFixed32(void *buf, uint32_t *value) {
  if (IS_LITTLE_ENDIAN()) {
    memcpy(value, buf, sizeof(*value));
  } else {
H
hzcheng 已提交
87 88 89 90
    ((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 已提交
91 92
  }

H
hzcheng 已提交
93
  return POINTER_SHIFT(buf, sizeof(*value));
H
hzcheng 已提交
94 95 96 97 98 99
}

static FORCE_INLINE void *taosDecodeFixed64(void *buf, uint64_t *value) {
  if (IS_LITTLE_ENDIAN()) {
    memcpy(value, buf, sizeof(*value));
  } else {
H
hzcheng 已提交
100 101 102 103 104 105 106 107
    ((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 已提交
108 109
  }

H
hzcheng 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  return POINTER_SHIFT(buf, sizeof(*value));
}

static FORCE_INLINE void *taosEncodeVariant16(void *buf, uint16_t value) {
  int i = 0;
  while (value >= ENCODE_LIMIT) {
    ((uint8_t *)buf)[i] = (value | ENCODE_LIMIT);
    value >>= 7;
    i++;
    ASSERT(i < 3);
  }

  ((uint8_t *)buf)[i] = value;

  return POINTER_SHIFT(buf, i+1);
}

static FORCE_INLINE void *taosEncodeVariant32(void *buf, uint32_t value) {
  int i = 0;
  while (value >= ENCODE_LIMIT) {
    ((uint8_t *)buf)[i] = (value | ENCODE_LIMIT);
    value >>= 7;
    i++;
    ASSERT(i < 5);
  }

  ((uint8_t *)buf)[i] = value;

  return POINTER_SHIFT(buf, i + 1);
}

static FORCE_INLINE void *taosEncodeVariant64(void *buf, uint64_t value) {
  int i = 0;
  while (value >= ENCODE_LIMIT) {
    ((uint8_t *)buf)[i] = (value | ENCODE_LIMIT);
    value >>= 7;
    i++;
    ASSERT(i < 10);
  }

  ((uint8_t *)buf)[i] = value;

  return POINTER_SHIFT(buf, i + 1);
}

static FORCE_INLINE void *taosDecodeVariant16(void *buf, uint16_t *value) {
  int i = 0;
  *value = 0;
  while (i < 3 && ) {
    (*value) |= (((uint8_t *)buf)[i] << (7 * i));
    i++;
  }

  return NULL; // error happened
H
hzcheng 已提交
164 165 166 167 168 169 170 171 172 173
}

static FORCE_INLINE void *taosDecodeVariant32(void *buf, uint32_t *value) {}
static FORCE_INLINE void *taosDecodeVariant64(void *buf, uint64_t *value) {}

#ifdef __cplusplus
}
#endif

#endif