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

📄 util_unistr.c

📁 samba-3.0.22.tar.gz 编译smb服务器的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    Unix SMB/CIFS implementation.   Samba utility functions   Copyright (C) Andrew Tridgell 1992-2001   Copyright (C) Simo Sorce 2001   Copyright (C) Jeremy Allison 2005      This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2 of the License, or   (at your option) any later version.      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.  See the   GNU General Public License for more details.      You should have received a copy of the GNU General Public License   along with this program; if not, write to the Free Software   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "includes.h"#ifndef MAXUNI#define MAXUNI 1024#endif/* these 3 tables define the unicode case handling.  They are loaded   at startup either via mmap() or read() from the lib directory */static smb_ucs2_t *upcase_table;static smb_ucs2_t *lowcase_table;static uint8 *valid_table;/** * This table says which Unicode characters are valid dos * characters. * * Each value is just a single bit. **/static uint8 doschar_table[8192]; /* 65536 characters / 8 bits/byte *//** * Load or generate the case handling tables. * * The case tables are defined in UCS2 and don't depend on any * configured parameters, so they never need to be reloaded. **/void load_case_tables(void){	static int initialised;	int i;	if (initialised) {		return;	}	initialised = 1;	upcase_table = map_file(lib_path("upcase.dat"), 0x20000);	lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000);	/* we would like Samba to limp along even if these tables are	   not available */	if (!upcase_table) {		DEBUG(1,("creating lame upcase table\n"));		upcase_table = SMB_MALLOC(0x20000);		for (i=0;i<0x10000;i++) {			smb_ucs2_t v;			SSVAL(&v, 0, i);			upcase_table[v] = i;		}		for (i=0;i<256;i++) {			smb_ucs2_t v;			SSVAL(&v, 0, UCS2_CHAR(i));			upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);		}	}	if (!lowcase_table) {		DEBUG(1,("creating lame lowcase table\n"));		lowcase_table = SMB_MALLOC(0x20000);		for (i=0;i<0x10000;i++) {			smb_ucs2_t v;			SSVAL(&v, 0, i);			lowcase_table[v] = i;		}		for (i=0;i<256;i++) {			smb_ucs2_t v;			SSVAL(&v, 0, UCS2_CHAR(i));			lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);		}	}}/*  see if a ucs2 character can be mapped correctly to a dos character  and mapped back to the same character in ucs2*/int check_dos_char(smb_ucs2_t c){	lazy_initialize_conv();		/* Find the right byte, and right bit within the byte; return	 * 1 or 0 */	return (doschar_table[(c & 0xffff) / 8] & (1 << (c & 7))) != 0;}static int check_dos_char_slowly(smb_ucs2_t c){	char buf[10];	smb_ucs2_t c2 = 0;	int len1, len2;	len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf),False);	if (len1 == 0) {		return 0;	}	len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2,False);	if (len2 != 2) {		return 0;	}	return (c == c2);}/** * Fill out doschar table the hard way, by examining each character **/void init_doschar_table(void){	int i, j, byteval;	/* For each byte of packed table */		for (i = 0; i <= 0xffff; i += 8) {		byteval = 0;		for (j = 0; j <= 7; j++) {			smb_ucs2_t c;			c = i + j;						if (check_dos_char_slowly(c)) {				byteval |= 1 << j;			}		}		doschar_table[i/8] = byteval;	}}/** * Load the valid character map table from <tt>valid.dat</tt> or * create from the configured codepage. * * This function is called whenever the configuration is reloaded. * However, the valid character table is not changed if it's loaded * from a file, because we can't unmap files. **/void init_valid_table(void){	static int mapped_file;	int i;	const char *allowed = ".!#$%&'()_-@^`~";	uint8 *valid_file;	if (mapped_file) {		/* Can't unmap files, so stick with what we have */		return;	}	valid_file = map_file(lib_path("valid.dat"), 0x10000);	if (valid_file) {		valid_table = valid_file;		mapped_file = 1;		return;	}	/* Otherwise, we're using a dynamically created valid_table.	 * It might need to be regenerated if the code page changed.	 * We know that we're not using a mapped file, so we can	 * free() the old one. */	if (valid_table) free(valid_table);	DEBUG(2,("creating default valid table\n"));	valid_table = SMB_MALLOC(0x10000);	for (i=0;i<128;i++) {		valid_table[i] = isalnum(i) || strchr(allowed,i);	}		for (;i<0x10000;i++) {		smb_ucs2_t c;		SSVAL(&c, 0, i);		valid_table[i] = check_dos_char(c);	}}/******************************************************************* Write a string in (little-endian) unicode format. src is in the current DOS codepage. len is the length in bytes of the string pointed to by dst. if null_terminate is True then null terminate the packet (adds 2 bytes) the return value is the length in bytes consumed by the string, including the null termination if applied********************************************************************/size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate){	int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE				   : STR_UNICODE|STR_NOALIGN;	return push_ucs2(NULL, dst, src, len, flags);}/******************************************************************* Skip past a unicode string, but not more than len. Always move past a terminating zero if found.********************************************************************/char *skip_unibuf(char *src, size_t len){	char *srcend = src + len;	while (src < srcend && SVAL(src,0)) {		src += 2;	}	if(!SVAL(src,0)) {		src += 2;	}	return src;}/* Copy a string from little-endian or big-endian unicode source (depending * on flags) to internal samba format destination */ int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags){	if (!src) {		dest[0] = 0;		return 0;	}	if(dest_len==-1) {		dest_len=MAXUNI-3;	}	return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);}/* Copy a string from a unistr2 source to internal samba format   destination.  Use this instead of direct calls to rpcstr_pull() to avoid   having to determine whether the source string is null terminated. */int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src){        return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),                         src->uni_str_len * 2, 0);}/* Helper function to return a talloc'ed string. I have implemented it with a * copy because I don't really know how pull_ucs2 and friends calculate the * target size. If this turns out to be a major bottleneck someone with deeper * multi-byte knowledge needs to revisit this. * My (VL) use is dsr_getdcname, which returns 6 strings, the alternative would * have been to manually talloc_strdup them in rpc_client/cli_netlogon.c. */size_t rpcstr_pull_unistr2_talloc(TALLOC_CTX *mem_ctx, char **dest,				  UNISTR2 *src){	pstring tmp;	size_t result;	result = pull_ucs2(NULL, tmp, src->buffer, sizeof(tmp),			   src->uni_str_len * 2, 0);	if (result < 0) {		return result;	}	*dest = talloc_strdup(mem_ctx, tmp);	if (*dest == NULL) {		return -1;	}	return result;}/* Converts a string from internal samba format to unicode */ int rpcstr_push(void* dest, const char *src, int dest_len, int flags){	return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);}/******************************************************************* Convert a (little-endian) UNISTR2 structure to an ASCII string.********************************************************************/void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen){	if (str == NULL) {		*dest='\0';		return;	}	pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);}/******************************************************************* Convert a (little-endian) UNISTR3 structure to an ASCII string.********************************************************************/void unistr3_to_ascii(char *dest, const UNISTR3 *str, size_t maxlen){	if (str == NULL) {		*dest='\0';		return;	}	pull_ucs2(NULL, dest, str->str.buffer, maxlen, str->uni_str_len*2,	          STR_NOALIGN);}	/******************************************************************* Give a static string for displaying a UNISTR2.********************************************************************/const char *unistr2_static(const UNISTR2 *str){	static pstring ret;	unistr2_to_ascii(ret, str, sizeof(ret));	return ret;}/******************************************************************* Duplicate a UNISTR2 string into a null terminated char* using a talloc context.********************************************************************/char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str){	char *s;	int maxlen = (str->uni_str_len+1)*4;	if (!str->buffer) {		return NULL;	}	s = (char *)TALLOC(ctx, maxlen); /* convervative */	if (!s) {		return NULL;	}	pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);	return s;}/******************************************************************* Convert a wchar to upper case.********************************************************************/smb_ucs2_t toupper_w(smb_ucs2_t val){	return upcase_table[SVAL(&val,0)];}/******************************************************************* Convert a wchar to lower case.********************************************************************/smb_ucs2_t tolower_w( smb_ucs2_t val ){	return lowcase_table[SVAL(&val,0)];}/******************************************************************* Determine if a character is lowercase.********************************************************************/BOOL islower_w(smb_ucs2_t c){	return upcase_table[SVAL(&c,0)] != c;}/******************************************************************* Determine if a character is uppercase.********************************************************************/BOOL isupper_w(smb_ucs2_t c){	return lowcase_table[SVAL(&c,0)] != c;}/******************************************************************* Determine if a character is valid in a 8.3 name.********************************************************************/BOOL isvalid83_w(smb_ucs2_t c){	return valid_table[SVAL(&c,0)] != 0;}/******************************************************************* Count the number of characters in a smb_ucs2_t string.********************************************************************/size_t strlen_w(const smb_ucs2_t *src){	size_t len;	smb_ucs2_t c;	for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {		;	}	return len;}/******************************************************************* Count up to max number of characters in a smb_ucs2_t string.********************************************************************/size_t strnlen_w(const smb_ucs2_t *src, size_t max){	size_t len;	smb_ucs2_t c;	for(len = 0; *(COPY_UCS2_CHAR(&c,src)) && (len < max); src++, len++) {		;	}	return len;}/******************************************************************* Wide strchr().********************************************************************/smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c){	smb_ucs2_t cp;	while (*(COPY_UCS2_CHAR(&cp,s))) {		if (c == cp) {			return (smb_ucs2_t *)s;		}		s++;	}	if (c == cp) {		return (smb_ucs2_t *)s;	}	return NULL;}smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c){	return strchr_w(s, UCS2_CHAR(c));}/******************************************************************* Wide strrchr().********************************************************************/smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c){	smb_ucs2_t cp;	const smb_ucs2_t *p = s;	int len = strlen_w(s);	if (len == 0) {		return NULL;	}	p += (len - 1);	do {		if (c == *(COPY_UCS2_CHAR(&cp,p))) {			return (smb_ucs2_t *)p;		}	} while (p-- != s);	return NULL;}/******************************************************************* Wide version of strrchr that returns after doing strrchr 'n' times.********************************************************************/smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n){	smb_ucs2_t cp;	const smb_ucs2_t *p = s;	int len = strlen_w(s);	if (len == 0 || !n) {		return NULL;	}	p += (len - 1);	do {

⌨️ 快捷键说明

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