tutil.h 4.3 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
util  
Shengliang Guan 已提交
16 17
#ifndef _TD_UTIL_UTIL_H_
#define _TD_UTIL_UTIL_H_
H
hzcheng 已提交
18

S
slguan 已提交
19
#include "os.h"
H
hzcheng 已提交
20
#include "tcrc32c.h"
21
#include "tdef.h"
22
#include "thash.h"
H
Hongze Cheng 已提交
23
#include "tmd5.h"
H
hzcheng 已提交
24

S
util  
Shengliang Guan 已提交
25 26 27 28
#ifdef __cplusplus
extern "C" {
#endif

H
hzcheng 已提交
29
int32_t strdequote(char *src);
S
Shengliang Guan 已提交
30
size_t  strtrim(char *src);
X
Xiaoyu Wang 已提交
31
char   *strnchr(const char *haystack, char needle, int32_t len, bool skipquote);
H
Hongze Cheng 已提交
32
TdUcs4 *wcsnchr(const TdUcs4 *haystack, TdUcs4 needle, size_t len);
H
Haojun Liao 已提交
33

S
Shengliang Guan 已提交
34 35 36 37
char  **strsplit(char *src, const char *delim, int32_t *num);
char   *strtolower(char *dst, const char *src);
char   *strntolower(char *dst, const char *src, int32_t n);
char   *strntolower_s(char *dst, const char *src, int32_t n);
H
hzcheng 已提交
38
int64_t strnatoi(char *num, int32_t len);
H
Haojun Liao 已提交
39

H
Hongze Cheng 已提交
40 41
size_t tstrncspn(const char *str, size_t ssize, const char *reject, size_t rsize);
size_t twcsncspn(const TdUcs4 *wcs, size_t size, const TdUcs4 *reject, size_t rsize);
H
Haojun Liao 已提交
42

H
Hongze Cheng 已提交
43 44
char *strbetween(char *string, char *begin, char *end);
char *paGetToken(char *src, char **token, int32_t *tokenLen);
H
hzcheng 已提交
45 46 47 48

int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]);
int32_t taosHexStrToByteArray(char hexstr[], char bytes[]);

49
int32_t tintToHex(uint64_t val, char hex[]);
50
int32_t titoa(uint64_t val, size_t radix, char str[]);
51

S
Shengliang Guan 已提交
52
char    *taosIpStr(uint32_t ipInt);
S
Shengliang Guan 已提交
53
uint32_t ip2uint(const char *const ip_addr);
S
Shengliang Guan 已提交
54 55
void     taosIp2String(uint32_t ip, char *str);
void     taosIpPort2String(uint32_t ip, uint16_t port, char *str);
S
slguan 已提交
56

L
Liu Jicong 已提交
57 58
void *tmemmem(const char *haystack, int hlen, const char *needle, int nlen);

59
static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, size_t inLen, char *target) {
dengyihao's avatar
dengyihao 已提交
60 61
  T_MD5_CTX context;
  tMD5Init(&context);
S
Shengliang Guan 已提交
62
  tMD5Update(&context, inBuf, (uint32_t)inLen);
dengyihao's avatar
dengyihao 已提交
63
  tMD5Final(&context);
64 65 66 67
  memcpy(target, context.digest, tListLen(context.digest));
}

static FORCE_INLINE void taosEncryptPass_c(uint8_t *inBuf, size_t len, char *target) {
H
Haojun Liao 已提交
68 69
  T_MD5_CTX context;
  tMD5Init(&context);
S
Shengliang Guan 已提交
70
  tMD5Update(&context, inBuf, (uint32_t)len);
H
Haojun Liao 已提交
71
  tMD5Final(&context);
wafwerar's avatar
wafwerar 已提交
72
  char buf[TSDB_PASSWORD_LEN + 1];
73

74
  buf[TSDB_PASSWORD_LEN] = 0;
L
Liu Jicong 已提交
75 76 77 78
  sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0], context.digest[1],
          context.digest[2], context.digest[3], context.digest[4], context.digest[5], context.digest[6],
          context.digest[7], context.digest[8], context.digest[9], context.digest[10], context.digest[11],
          context.digest[12], context.digest[13], context.digest[14], context.digest[15]);
wafwerar's avatar
wafwerar 已提交
79
  memcpy(target, buf, TSDB_PASSWORD_LEN);
80 81
}

S
Shengliang Guan 已提交
82
static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, int32_t method, int32_t prefix,
H
Hongze Cheng 已提交
83
                                             int32_t suffix) {
84 85 86 87 88 89 90 91 92 93
  if (prefix == 0 && suffix == 0) {
    return MurmurHash3_32(tbname, tblen);
  } else {
    if (tblen <= (prefix + suffix)) {
      return MurmurHash3_32(tbname, tblen);
    } else {
      return MurmurHash3_32(tbname + prefix, tblen - prefix - suffix);
    }
  }
}
94

H
Hongze Cheng 已提交
95 96 97 98 99 100 101
#define TSDB_CHECK(condition, CODE, LINO, LABEL, ERRNO) \
  if (!(condition)) {                                   \
    (CODE) = (ERRNO);                                   \
    (LINO) = __LINE__;                                  \
    goto LABEL;                                         \
  }

H
Hongze Cheng 已提交
102
#define TSDB_CHECK_CODE(CODE, LINO, LABEL) \
H
Hongze Cheng 已提交
103 104
  if ((CODE)) {                            \
    (LINO) = __LINE__;                     \
H
Hongze Cheng 已提交
105 106 107
    goto LABEL;                            \
  }

H
Hongze Cheng 已提交
108 109 110 111 112 113 114
#define TSDB_CHECK_NULL(ptr, CODE, LINO, LABEL, ERRNO) \
  if ((ptr) == NULL) {                                 \
    (CODE) = (ERRNO);                                  \
    (LINO) = __LINE__;                                 \
    goto LABEL;                                        \
  }

H
hzcheng 已提交
115 116 117 118
#ifdef __cplusplus
}
#endif

S
util  
Shengliang Guan 已提交
119
#endif /*_TD_UTIL_UTIL_H_*/