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
|
|
|
|
|
|
|
|
#include "tcrc32c.h"
|
2021-10-04 12:42:53 +00:00
|
|
|
#include "tdef.h"
|
2022-09-02 09:12:08 +00:00
|
|
|
#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);
|
|
|
|
|
|
2024-07-15 08:06:47 +00:00
|
|
|
int32_t parseCfgReal(const char *str, double *out);
|
2024-04-10 08:48:02 +00:00
|
|
|
|
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);
|
2024-07-25 00:51:19 +00:00
|
|
|
(void)memcpy(target, context.digest, tListLen(context.digest));
|
2021-12-10 09:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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];
|
2021-12-10 09:28:09 +00:00
|
|
|
|
2022-09-23 07:42:36 +00:00
|
|
|
buf[TSDB_PASSWORD_LEN] = 0;
|
2024-07-25 09:34:41 +00:00
|
|
|
(void)sprintf(buf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", context.digest[0], context.digest[1],
|
2022-07-01 06:39:21 +00:00
|
|
|
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]);
|
2024-07-25 00:51:19 +00:00
|
|
|
(void)memcpy(target, buf, TSDB_PASSWORD_LEN);
|
2019-07-16 02:31:43 +00:00
|
|
|
}
|
|
|
|
|
|
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);
|
2024-07-15 08:06:47 +00:00
|
|
|
tMD5Update(&ctx, (uint8_t *)pBuf, len);
|
2023-08-29 07:51:29 +00:00
|
|
|
tMD5Final(&ctx);
|
2024-07-15 08:06:47 +00:00
|
|
|
char *p = pBuf;
|
2023-08-29 07:51:29 +00:00
|
|
|
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) {
|
2023-05-15 06:26:40 +00:00
|
|
|
if ((prefix == 0 && suffix == 0) || (tblen <= (prefix + suffix)) || (tblen <= -1 * (prefix + suffix)) ||
|
|
|
|
|
prefix * suffix < 0) {
|
2022-09-08 05:54:54 +00:00
|
|
|
return MurmurHash3_32(tbname, tblen);
|
2023-04-21 06:17:07 +00:00
|
|
|
} else if (prefix > 0 || suffix > 0) {
|
|
|
|
|
return MurmurHash3_32(tbname + prefix, tblen - prefix - suffix);
|
2022-09-08 05:54:54 +00:00
|
|
|
} else {
|
2023-05-15 06:26:40 +00:00
|
|
|
char tbName[TSDB_TABLE_FNAME_LEN];
|
2023-04-21 06:17:07 +00:00
|
|
|
int32_t offset = 0;
|
|
|
|
|
if (prefix < 0) {
|
|
|
|
|
offset = -1 * prefix;
|
2024-07-25 09:34:41 +00:00
|
|
|
(void)strncpy(tbName, tbname, offset);
|
2022-09-08 05:54:54 +00:00
|
|
|
}
|
2023-04-21 06:17:07 +00:00
|
|
|
if (suffix < 0) {
|
2024-07-25 09:34:41 +00:00
|
|
|
(void)strncpy(tbName + offset, tbname + tblen + suffix, -1 * suffix);
|
2023-05-15 06:26:40 +00:00
|
|
|
offset += -1 * suffix;
|
2023-04-21 06:17:07 +00:00
|
|
|
}
|
|
|
|
|
return MurmurHash3_32(tbName, offset);
|
2022-09-08 05:54:54 +00:00
|
|
|
}
|
|
|
|
|
}
|
2022-09-02 09:12:08 +00:00
|
|
|
|
2024-07-03 10:51:50 +00:00
|
|
|
#define TAOS_CHECK_ERRNO(CODE) \
|
|
|
|
|
do { \
|
|
|
|
|
terrno = (CODE); \
|
|
|
|
|
if (terrno != TSDB_CODE_SUCCESS) { \
|
|
|
|
|
terrln = __LINE__; \
|
|
|
|
|
goto _exit; \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2022-10-18 08:54:53 +00:00
|
|
|
#define TSDB_CHECK_CODE(CODE, LINO, LABEL) \
|
2023-05-16 02:07:46 +00:00
|
|
|
do { \
|
2024-05-30 08:11:53 +00:00
|
|
|
if (TSDB_CODE_SUCCESS != (CODE)) { \
|
2023-05-16 02:07:46 +00:00
|
|
|
LINO = __LINE__; \
|
|
|
|
|
goto LABEL; \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
2022-10-18 08:54:53 +00:00
|
|
|
|
2024-07-19 08:15:37 +00:00
|
|
|
#define QUERY_CHECK_CODE TSDB_CHECK_CODE
|
|
|
|
|
|
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; \
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 08:15:37 +00:00
|
|
|
#define QUERY_CHECK_NULL TSDB_CHECK_NULL
|
|
|
|
|
|
2023-03-29 05:27:36 +00:00
|
|
|
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
|
|
2023-04-20 08:22:16 +00:00
|
|
|
#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)))
|
|
|
|
|
|
2024-07-24 09:56:29 +00:00
|
|
|
#define TAOS_GET_TERRNO(code) \
|
2024-07-25 05:31:18 +00:00
|
|
|
(terrno == 0 ? code : terrno)
|
2024-07-24 09:56:29 +00:00
|
|
|
|
2024-07-23 07:06:14 +00:00
|
|
|
#define TAOS_RETURN(CODE) \
|
2024-07-15 08:08:20 +00:00
|
|
|
do { \
|
2024-07-23 07:06:14 +00:00
|
|
|
return (terrno = (CODE)); \
|
2024-07-15 08:08:20 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
2024-07-28 10:03:13 +00:00
|
|
|
#define TAOS_CHECK_RETURN(CMD) \
|
|
|
|
|
do { \
|
|
|
|
|
int32_t __c = (CMD); \
|
|
|
|
|
if (__c != TSDB_CODE_SUCCESS) { \
|
|
|
|
|
TAOS_RETURN(__c); \
|
|
|
|
|
} \
|
2024-07-15 08:06:47 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
2024-07-15 11:29:27 +00:00
|
|
|
#define TAOS_CHECK_GOTO(CMD, LINO, LABEL) \
|
|
|
|
|
do { \
|
|
|
|
|
code = (CMD); \
|
|
|
|
|
if (code != TSDB_CODE_SUCCESS) { \
|
|
|
|
|
if (LINO) { \
|
|
|
|
|
*((int32_t *)(LINO)) = __LINE__; \
|
|
|
|
|
} \
|
|
|
|
|
goto LABEL; \
|
|
|
|
|
} \
|
2024-07-15 08:06:47 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
2024-07-22 16:43:33 +00:00
|
|
|
#define TAOS_CHECK_EXIT(CMD) \
|
|
|
|
|
do { \
|
|
|
|
|
code = (CMD); \
|
|
|
|
|
if (code < TSDB_CODE_SUCCESS) { \
|
|
|
|
|
lino = __LINE__; \
|
|
|
|
|
goto _exit; \
|
|
|
|
|
} \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
2024-07-17 08:15:19 +00:00
|
|
|
#define TAOS_UNUSED(expr) (void)(expr)
|
|
|
|
|
|
2019-07-11 08:36:16 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-02-28 06:29:54 +00:00
|
|
|
#endif /*_TD_UTIL_UTIL_H_*/
|