TDengine/include/util/tutil.h

144 lines
5 KiB
C
Raw Normal View History

2019-07-11 08:36:16 +00:00
/*
* 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/>.
*/
2022-02-28 06:29:54 +00:00
#ifndef _TD_UTIL_UTIL_H_
#define _TD_UTIL_UTIL_H_
2019-07-11 08:36:16 +00:00
2019-11-07 10:12:26 +00:00
#include "os.h"
2019-07-11 08:36:16 +00:00
#include "tcrc32c.h"
2021-10-04 12:42:53 +00:00
#include "tdef.h"
#include "thash.h"
2022-10-13 03:09:43 +00:00
#include "tmd5.h"
2019-07-11 08:36:16 +00:00
2022-02-28 06:29:54 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2019-07-11 08:36:16 +00:00
int32_t strdequote(char *src);
2020-07-28 06:40:55 +00:00
size_t strtrim(char *src);
2021-12-11 05:51:55 +00:00
char *strnchr(const char *haystack, char needle, int32_t len, bool skipquote);
2023-03-15 02:48:51 +00:00
TdUcs4 *wcsnchr(const TdUcs4 *haystack, TdUcs4 needle, size_t len);
2022-12-29 08:34:14 +00:00
2021-12-08 10:50:52 +00:00
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);
2019-07-11 08:36:16 +00:00
int64_t strnatoi(char *num, int32_t len);
2022-12-29 08:34:14 +00:00
2023-03-15 02:48:51 +00:00
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);
2022-12-29 06:48:57 +00:00
2023-03-15 02:48:51 +00:00
char *strbetween(char *string, char *begin, char *end);
char *paGetToken(char *src, char **token, int32_t *tokenLen);
2019-07-11 08:36:16 +00:00
int32_t taosByteArrayToHexStr(char bytes[], int32_t len, char hexstr[]);
int32_t taosHexStrToByteArray(char hexstr[], char bytes[]);
2023-01-06 16:59:05 +00:00
int32_t tintToHex(uint64_t val, char hex[]);
2023-01-16 15:58:45 +00:00
int32_t titoa(uint64_t val, size_t radix, char str[]);
2023-01-06 16:59:05 +00:00
2021-12-03 06:36:41 +00:00
char *taosIpStr(uint32_t ipInt);
2020-07-28 06:40:55 +00:00
uint32_t ip2uint(const char *const ip_addr);
2021-12-03 06:36:41 +00:00
void taosIp2String(uint32_t ip, char *str);
void taosIpPort2String(uint32_t ip, uint16_t port, char *str);
2019-09-12 14:42:28 +00:00
2022-07-01 06:39:21 +00:00
void *tmemmem(const char *haystack, int hlen, const char *needle, int nlen);
2021-10-04 12:42:53 +00:00
static FORCE_INLINE void taosEncryptPass(uint8_t *inBuf, size_t inLen, char *target) {
2021-12-09 09:11:45 +00:00
T_MD5_CTX context;
tMD5Init(&context);
2022-02-28 06:39:42 +00:00
tMD5Update(&context, inBuf, (uint32_t)inLen);
2021-12-09 09:11:45 +00:00
tMD5Final(&context);
memcpy(target, context.digest, tListLen(context.digest));
}
static FORCE_INLINE void taosEncryptPass_c(uint8_t *inBuf, size_t len, char *target) {
2021-12-10 11:07:24 +00:00
T_MD5_CTX context;
tMD5Init(&context);
2022-02-28 06:39:42 +00:00
tMD5Update(&context, inBuf, (uint32_t)len);
2021-12-10 11:07:24 +00:00
tMD5Final(&context);
2022-06-03 08:13:06 +00:00
char buf[TSDB_PASSWORD_LEN + 1];
2022-09-23 07:42:36 +00:00
buf[TSDB_PASSWORD_LEN] = 0;
2022-07-01 06:39:21 +00:00
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]);
2022-06-03 08:13:06 +00:00
memcpy(target, buf, TSDB_PASSWORD_LEN);
}
2023-08-29 07:51:29 +00:00
static FORCE_INLINE int32_t taosCreateMD5Hash(char *pBuf, int32_t len) {
T_MD5_CTX ctx;
tMD5Init(&ctx);
tMD5Update(&ctx, (uint8_t*)pBuf, len);
tMD5Final(&ctx);
char* p = pBuf;
int32_t resLen = 0;
for (uint8_t i = 0; i < tListLen(ctx.digest); ++i) {
resLen += snprintf(p, 3, "%02x", ctx.digest[i]);
p += 2;
}
return resLen;
}
2022-09-08 08:14:22 +00:00
static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, int32_t method, int32_t prefix,
2022-10-13 03:09:43 +00:00
int32_t suffix) {
if ((prefix == 0 && suffix == 0) || (tblen <= (prefix + suffix)) || (tblen <= -1 * (prefix + suffix)) ||
prefix * suffix < 0) {
return MurmurHash3_32(tbname, tblen);
} else if (prefix > 0 || suffix > 0) {
return MurmurHash3_32(tbname + prefix, tblen - prefix - suffix);
} else {
char tbName[TSDB_TABLE_FNAME_LEN];
int32_t offset = 0;
if (prefix < 0) {
offset = -1 * prefix;
strncpy(tbName, tbname, offset);
}
if (suffix < 0) {
strncpy(tbName + offset, tbname + tblen + suffix, -1 * suffix);
offset += -1 * suffix;
}
return MurmurHash3_32(tbName, offset);
}
}
2022-10-18 08:54:53 +00:00
#define TSDB_CHECK_CODE(CODE, LINO, LABEL) \
2023-05-16 02:07:46 +00:00
do { \
if ((CODE)) { \
LINO = __LINE__; \
goto LABEL; \
} \
} while (0)
2022-10-18 08:54:53 +00:00
2023-03-15 02:48:51 +00:00
#define TSDB_CHECK_NULL(ptr, CODE, LINO, LABEL, ERRNO) \
if ((ptr) == NULL) { \
(CODE) = (ERRNO); \
(LINO) = __LINE__; \
goto LABEL; \
}
2023-03-29 05:27:36 +00:00
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define VND_CHECK_CODE(CODE, LINO, LABEL) TSDB_CHECK_CODE(CODE, LINO, LABEL)
2023-05-16 01:36:32 +00:00
#define TCONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))
2019-07-11 08:36:16 +00:00
#ifdef __cplusplus
}
#endif
2022-02-28 06:29:54 +00:00
#endif /*_TD_UTIL_UTIL_H_*/