⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util_str.c

📁 samba最新软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/** Unescape a URL encoded string, in place.**/_PUBLIC_ void rfc1738_unescape(char *buf){	char *p=buf;	while ((p=strchr(p,'+')))		*p = ' ';	p = buf;	while (p && *p && (p=strchr(p,'%'))) {		int c1 = p[1];		int c2 = p[2];		if (c1 >= '0' && c1 <= '9')			c1 = c1 - '0';		else if (c1 >= 'A' && c1 <= 'F')			c1 = 10 + c1 - 'A';		else if (c1 >= 'a' && c1 <= 'f')			c1 = 10 + c1 - 'a';		else {p++; continue;}		if (c2 >= '0' && c2 <= '9')			c2 = c2 - '0';		else if (c2 >= 'A' && c2 <= 'F')			c2 = 10 + c2 - 'A';		else if (c2 >= 'a' && c2 <= 'f')			c2 = 10 + c2 - 'a';		else {p++; continue;}					*p = (c1<<4) | c2;		memmove(p+1, p+3, strlen(p+3)+1);		p++;	}}#ifdef VALGRINDsize_t valgrind_strlen(const char *s){	size_t count;	for(count = 0; *s++; count++)		;	return count;}#endif/**  format a string into length-prefixed dotted domain format, as used in NBT  and in some ADS structures**/_PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s){	char *ret;	int i;	if (!s || !*s) {		return talloc_strdup(mem_ctx, "");	}	ret = talloc_array(mem_ctx, char, strlen(s)+2);	if (!ret) {		return ret;	}		memcpy(ret+1, s, strlen(s)+1);	ret[0] = '.';	for (i=0;ret[i];i++) {		if (ret[i] == '.') {			char *p = strchr(ret+i+1, '.');			if (p) {				ret[i] = p-(ret+i+1);			} else {				ret[i] = strlen(ret+i+1);			}		}	}	return ret;}/** * Add a string to an array of strings. * * num should be a pointer to an integer that holds the current  * number of elements in strings. It will be updated by this function. */_PUBLIC_ bool add_string_to_array(TALLOC_CTX *mem_ctx,			 const char *str, const char ***strings, int *num){	char *dup_str = talloc_strdup(mem_ctx, str);	*strings = talloc_realloc(mem_ctx,				    *strings,				    const char *, ((*num)+1));	if ((*strings == NULL) || (dup_str == NULL))		return false;	(*strings)[*num] = dup_str;	*num += 1;	return true;}/**  varient of strcmp() that handles NULL ptrs**/_PUBLIC_ int strcmp_safe(const char *s1, const char *s2){	if (s1 == s2) {		return 0;	}	if (s1 == NULL || s2 == NULL) {		return s1?-1:1;	}	return strcmp(s1, s2);}/**return the number of bytes occupied by a buffer in ASCII formatthe result includes the null terminationlimited by 'n' bytes**/_PUBLIC_ size_t ascii_len_n(const char *src, size_t n){	size_t len;	len = strnlen(src, n);	if (len+1 <= n) {		len += 1;	}	return len;}/** Return a string representing a CIFS attribute for a file.**/_PUBLIC_ char *attrib_string(TALLOC_CTX *mem_ctx, uint32_t attrib){	int i, len;	const struct {		char c;		uint16_t attr;	} attr_strs[] = {		{'V', FILE_ATTRIBUTE_VOLUME},		{'D', FILE_ATTRIBUTE_DIRECTORY},		{'A', FILE_ATTRIBUTE_ARCHIVE},		{'H', FILE_ATTRIBUTE_HIDDEN},		{'S', FILE_ATTRIBUTE_SYSTEM},		{'N', FILE_ATTRIBUTE_NORMAL},		{'R', FILE_ATTRIBUTE_READONLY},		{'d', FILE_ATTRIBUTE_DEVICE},		{'t', FILE_ATTRIBUTE_TEMPORARY},		{'s', FILE_ATTRIBUTE_SPARSE},		{'r', FILE_ATTRIBUTE_REPARSE_POINT},		{'c', FILE_ATTRIBUTE_COMPRESSED},		{'o', FILE_ATTRIBUTE_OFFLINE},		{'n', FILE_ATTRIBUTE_NONINDEXED},		{'e', FILE_ATTRIBUTE_ENCRYPTED}	};	char *ret;	ret = talloc_array(mem_ctx, char, ARRAY_SIZE(attr_strs)+1);	if (!ret) {		return NULL;	}	for (len=i=0; i<ARRAY_SIZE(attr_strs); i++) {		if (attrib & attr_strs[i].attr) {			ret[len++] = attr_strs[i].c;		}	}	ret[len] = 0;	return ret;}/** Set a boolean variable from the text value stored in the passed string. Returns true in success, false if the passed string does not correctly  represent a boolean.**/_PUBLIC_ bool set_boolean(const char *boolean_string, bool *boolean){	if (strwicmp(boolean_string, "yes") == 0 ||	    strwicmp(boolean_string, "true") == 0 ||	    strwicmp(boolean_string, "on") == 0 ||	    strwicmp(boolean_string, "1") == 0) {		*boolean = true;		return true;	} else if (strwicmp(boolean_string, "no") == 0 ||		   strwicmp(boolean_string, "false") == 0 ||		   strwicmp(boolean_string, "off") == 0 ||		   strwicmp(boolean_string, "0") == 0) {		*boolean = false;		return true;	}	return false;}/** * Parse a string containing a boolean value. * * val will be set to the read value. * * @retval true if a boolean value was parsed, false otherwise. */_PUBLIC_ bool conv_str_bool(const char * str, bool * val){	char *	end = NULL;	long	lval;	if (str == NULL || *str == '\0') {		return false;	}	lval = strtol(str, &end, 10 /* base */);	if (end == NULL || *end != '\0' || end == str) {		return set_boolean(str, val);	}	*val = (lval) ? true : false;	return true;}/** * Convert a size specification like 16K into an integral number of bytes.  **/_PUBLIC_ bool conv_str_size(const char * str, uint64_t * val){	char *		    end = NULL;	unsigned long long  lval;	if (str == NULL || *str == '\0') {		return false;	}	lval = strtoull(str, &end, 10 /* base */);	if (end == NULL || end == str) {		return false;	}	if (*end) {		if (strwicmp(end, "K") == 0) {			lval *= 1024ULL;		} else if (strwicmp(end, "M") == 0) {			lval *= (1024ULL * 1024ULL);		} else if (strwicmp(end, "G") == 0) {			lval *= (1024ULL * 1024ULL * 1024ULL);		} else if (strwicmp(end, "T") == 0) {			lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);		} else if (strwicmp(end, "P") == 0) {			lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);		} else {			return false;		}	}	*val = (uint64_t)lval;	return true;}/** * Parse a uint64_t value from a string * * val will be set to the value read. * * @retval true if parsing was successful, false otherwise */_PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val){	char *		    end = NULL;	unsigned long long  lval;	if (str == NULL || *str == '\0') {		return false;	}	lval = strtoull(str, &end, 10 /* base */);	if (end == NULL || *end != '\0' || end == str) {		return false;	}	*val = (uint64_t)lval;	return true;}/**return the number of bytes occupied by a buffer in CH_UTF16 formatthe result includes the null termination**/_PUBLIC_ size_t utf16_len(const void *buf){	size_t len;	for (len = 0; SVAL(buf,len); len += 2) ;	return len + 2;}/**return the number of bytes occupied by a buffer in CH_UTF16 formatthe result includes the null terminationlimited by 'n' bytes**/_PUBLIC_ size_t utf16_len_n(const void *src, size_t n){	size_t len;	for (len = 0; (len+2 < n) && SVAL(src, len); len += 2) ;	if (len+2 <= n) {		len += 2;	}	return len;}_PUBLIC_ size_t ucs2_align(const void *base_ptr, const void *p, int flags){	if (flags & (STR_NOALIGN|STR_ASCII))		return 0;	return PTR_DIFF(p, base_ptr) & 1;}/**Do a case-insensitive, whitespace-ignoring string compare.**/_PUBLIC_ int strwicmp(const char *psz1, const char *psz2){	/* if BOTH strings are NULL, return TRUE, if ONE is NULL return */	/* appropriate value. */	if (psz1 == psz2)		return (0);	else if (psz1 == NULL)		return (-1);	else if (psz2 == NULL)		return (1);	/* sync the strings on first non-whitespace */	while (1) {		while (isspace((int)*psz1))			psz1++;		while (isspace((int)*psz2))			psz2++;		if (toupper((unsigned char)*psz1) != toupper((unsigned char)*psz2) 		    || *psz1 == '\0'		    || *psz2 == '\0')			break;		psz1++;		psz2++;	}	return (*psz1 - *psz2);}/** String replace.**/_PUBLIC_ void string_replace(char *s, char oldc, char newc){	while (*s) {		if (*s == oldc) *s = newc;		s++;	}}/** * Compare 2 strings. * * @note The comparison is case-insensitive. **/_PUBLIC_ bool strequal(const char *s1, const char *s2){	if (s1 == s2)		return true;	if (!s1 || !s2)		return false;  	return strcasecmp(s1,s2) == 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -