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

📄 util_unistr.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
		if (c == *(COPY_UCS2_CHAR(&cp,p))) {			n--;		}		if (!n) {			return (smb_ucs2_t *)p;		}	} while (p-- != s);	return NULL;}/******************************************************************* Wide strstr().********************************************************************/smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins){	smb_ucs2_t *r;	size_t inslen;	if (!s || !*s || !ins || !*ins) {		return NULL;	}	inslen = strlen_w(ins);	r = (smb_ucs2_t *)s;	while ((r = strchr_w(r, *ins))) {		if (strncmp_w(r, ins, inslen) == 0) {			return r;		}		r++;	}	return NULL;}/******************************************************************* Convert a string to lower case. return True if any char is converted********************************************************************/BOOL strlower_w(smb_ucs2_t *s){	smb_ucs2_t cp;	BOOL ret = False;	while (*(COPY_UCS2_CHAR(&cp,s))) {		smb_ucs2_t v = tolower_w(cp);		if (v != cp) {			COPY_UCS2_CHAR(s,&v);			ret = True;		}		s++;	}	return ret;}/******************************************************************* Convert a string to upper case. return True if any char is converted********************************************************************/BOOL strupper_w(smb_ucs2_t *s){	smb_ucs2_t cp;	BOOL ret = False;	while (*(COPY_UCS2_CHAR(&cp,s))) {		smb_ucs2_t v = toupper_w(cp);		if (v != cp) {			COPY_UCS2_CHAR(s,&v);			ret = True;		}		s++;	}	return ret;}/******************************************************************* Convert a string to "normal" form.********************************************************************/void strnorm_w(smb_ucs2_t *s, int case_default){	if (case_default == CASE_UPPER) {		strupper_w(s);	} else {		strlower_w(s);	}}int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b){	smb_ucs2_t cpa, cpb;	while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {		a++;		b++;	}	return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));	/* warning: if *a != *b and both are not 0 we return a random		greater or lesser than 0 number not realted to which		string is longer */}int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len){	smb_ucs2_t cpa, cpb;	size_t n = 0;	while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {		a++;		b++;		n++;	}	return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;}/******************************************************************* Case insensitive string comparison.********************************************************************/int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b){	smb_ucs2_t cpa, cpb;	while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {		a++;		b++;	}	return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));}/******************************************************************* Case insensitive string comparison, length limited.********************************************************************/int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len){	smb_ucs2_t cpa, cpb;	size_t n = 0;	while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {		a++;		b++;		n++;	}	return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;}/******************************************************************* Compare 2 strings.********************************************************************/BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2){	if (s1 == s2) {		return(True);	}	if (!s1 || !s2) {		return(False);	}  	return(strcasecmp_w(s1,s2)==0);}/******************************************************************* Compare 2 strings up to and including the nth char.******************************************************************/BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n){	if (s1 == s2) {		return(True);	}	if (!s1 || !s2 || !n) {		return(False);	}  	return(strncasecmp_w(s1,s2,n)==0);}/******************************************************************* Duplicate string.********************************************************************/smb_ucs2_t *strdup_w(const smb_ucs2_t *src){	return strndup_w(src, 0);}/* if len == 0 then duplicate the whole string */smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len){	smb_ucs2_t *dest;		if (!len) {		len = strlen_w(src);	}	dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);	if (!dest) {		DEBUG(0,("strdup_w: out of memory!\n"));		return NULL;	}	memcpy(dest, src, len * sizeof(smb_ucs2_t));	dest[len] = 0;	return dest;}/******************************************************************* Copy a string with max len.********************************************************************/smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max){	smb_ucs2_t cp;	size_t len;		if (!dest || !src) {		return NULL;	}		for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {		cp = *COPY_UCS2_CHAR(dest+len,src+len);	}	cp = 0;	for ( /*nothing*/ ; len < max; len++ ) {		cp = *COPY_UCS2_CHAR(dest+len,&cp);	}		return dest;}/******************************************************************* Append a string of len bytes and add a terminator.********************************************************************/smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max){		size_t start;	size_t len;		smb_ucs2_t z = 0;	if (!dest || !src) {		return NULL;	}		start = strlen_w(dest);	len = strnlen_w(src, max);	memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));				z = *COPY_UCS2_CHAR(dest+start+len,&z);	return dest;}smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src){		size_t start;	size_t len;		smb_ucs2_t z = 0;		if (!dest || !src) {		return NULL;	}		start = strlen_w(dest);	len = strlen_w(src);	memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));				z = *COPY_UCS2_CHAR(dest+start+len,&z);		return dest;}/******************************************************************* Replace any occurence of oldc with newc in unicode string.********************************************************************/void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc){	smb_ucs2_t cp;	for(;*(COPY_UCS2_CHAR(&cp,s));s++) {		if(cp==oldc) {			COPY_UCS2_CHAR(s,&newc);		}	}}/******************************************************************* Trim unicode string.********************************************************************/BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,				  const smb_ucs2_t *back){	BOOL ret = False;	size_t len, front_len, back_len;	if (!s) {		return False;	}	len = strlen_w(s);	if (front && *front) {		front_len = strlen_w(front);		while (len && strncmp_w(s, front, front_len) == 0) {			memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));			len -= front_len;			ret = True;		}	}		if (back && *back) {		back_len = strlen_w(back);		while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {			s[len - back_len] = 0;			len -= back_len;			ret = True;		}	}	return ret;}/*  The *_wa() functions take a combination of 7 bit ascii  and wide characters They are used so that you can use string  functions combining C string constants with ucs2 strings  The char* arguments must NOT be multibyte - to be completely sure  of this only pass string constants */int strcmp_wa(const smb_ucs2_t *a, const char *b){	smb_ucs2_t cp = 0;	while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {		a++;		b++;	}	return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));}int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len){	smb_ucs2_t cp = 0;	size_t n = 0;	while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {		a++;		b++;		n++;	}	return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;}smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p){	smb_ucs2_t cp;	while (*(COPY_UCS2_CHAR(&cp,s))) {		int i;		for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++) 			;		if (p[i]) {			return (smb_ucs2_t *)s;		}		s++;	}	return NULL;}smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins){	smb_ucs2_t *r;	size_t inslen;	if (!s || !ins) { 		return NULL;	}	inslen = strlen(ins);	r = (smb_ucs2_t *)s;	while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {		if (strncmp_wa(r, ins, inslen) == 0) 			return r;		r++;	}	return NULL;}BOOL trim_string_wa(smb_ucs2_t *s, const char *front,				  const char *back){	wpstring f, b;	if (front) {		push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);	} else {		*f = 0;	}	if (back) {		push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);	} else {		*b = 0;	}	return trim_string_w(s, f, b);}/******************************************************************* Returns the length in number of wide characters.******************************************************************/int unistrlen(uint16 *s){	int len;	if (!s) {		return -1;	}	for (len=0; SVAL(s,0); s++,len++) {		;	}	return len;}/******************************************************************* Strcpy for unicode strings. Returns length (in num of wide chars). Not odd align safe.********************************************************************/int unistrcpy(uint16 *dst, uint16 *src){	int num_wchars = 0;	while (SVAL(src,0)) {		*dst++ = *src++;		num_wchars++;	}	*dst = 0;	return num_wchars;}/** * Samba ucs2 type to UNISTR2 conversion * * @param ctx Talloc context to create the dst strcture (if null) and the  *            contents of the unicode string. * @param dst UNISTR2 destination. If equals null, then it's allocated. * @param src smb_ucs2_t source. * @param max_len maximum number of unicode characters to copy. If equals *        null, then null-termination of src is taken * * @return copied UNISTR2 destination **/UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src){	size_t len;	if (!src) {		return NULL;	}	len = strlen_w(src);		/* allocate UNISTR2 destination if not given */	if (!dst) {		dst = TALLOC_P(ctx, UNISTR2);		if (!dst)			return NULL;	}	if (!dst->buffer) {		dst->buffer = TALLOC_ARRAY(ctx, uint16, len + 1);		if (!dst->buffer)			return NULL;	}		/* set UNISTR2 parameters */	dst->uni_max_len = len + 1;	dst->offset = 0;	dst->uni_str_len = len;		/* copy the actual unicode string */	strncpy_w(dst->buffer, src, dst->uni_max_len);		return dst;}

⌨️ 快捷键说明

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