2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) Simo Sorce 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /* these 3 tables define the unicode case handling. They are loaded
29 at startup either via mmap() or read() from the lib directory */
30 static smb_ucs2_t *upcase_table;
31 static smb_ucs2_t *lowcase_table;
32 static uint8 *valid_table;
35 /*******************************************************************
36 load the case handling tables
37 ********************************************************************/
38 void load_case_tables(void)
40 static int initialised;
43 if (initialised) return;
46 upcase_table = map_file(lib_path("upcase.dat"), 0x20000);
47 lowcase_table = map_file(lib_path("lowcase.dat"), 0x20000);
49 /* we would like Samba to limp along even if these tables are
52 DEBUG(1,("creating lame upcase table\n"));
53 upcase_table = malloc(0x20000);
54 for (i=0;i<0x10000;i++) {
61 SSVAL(&v, 0, UCS2_CHAR(i));
62 upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
67 DEBUG(1,("creating lame lowcase table\n"));
68 lowcase_table = malloc(0x20000);
69 for (i=0;i<0x10000;i++) {
76 SSVAL(&v, 0, UCS2_CHAR(i));
77 lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
83 see if a ucs2 character can be mapped correctly to a dos character
84 and mapped back to the same character in ucs2
86 static int check_dos_char(smb_ucs2_t c)
91 len1 = convert_string(CH_UCS2, CH_DOS, &c, 2, buf, sizeof(buf));
92 if (len1 == 0) return 0;
93 len2 = convert_string(CH_DOS, CH_UCS2, buf, len1, &c2, 2);
94 if (len2 != 2) return 0;
98 /*******************************************************************
99 load the valid character map table
100 ********************************************************************/
101 void init_valid_table(void)
103 static int initialised;
104 static int mapped_file;
106 const char *allowed = ".!#$%&'()_-@^`~";
108 if (initialised && mapped_file) return;
111 valid_table = map_file(lib_path("valid.dat"), 0x10000);
117 if (valid_table) free(valid_table);
119 DEBUG(2,("creating default valid table\n"));
120 valid_table = malloc(0x10000);
121 for (i=0;i<128;i++) valid_table[i] = isalnum(i) ||
123 for (;i<0x10000;i++) {
126 valid_table[i] = check_dos_char(c);
131 /*******************************************************************
132 Write a string in (little-endian) unicode format. src is in
133 the current DOS codepage. len is the length in bytes of the
134 string pointed to by dst.
136 if null_terminate is True then null terminate the packet (adds 2 bytes)
138 the return value is the length in bytes consumed by the string, including the
139 null termination if applied
140 ********************************************************************/
142 size_t dos_PutUniCode(char *dst,const char *src, ssize_t len, BOOL null_terminate)
144 return push_ucs2(NULL, dst, src, len,
145 STR_UNICODE|STR_NOALIGN | (null_terminate?STR_TERMINATE:0));
149 /*******************************************************************
150 Skip past a unicode string, but not more than len. Always move
151 past a terminating zero if found.
152 ********************************************************************/
154 char *skip_unibuf(char *src, size_t len)
156 char *srcend = src + len;
158 while (src < srcend && SVAL(src,0))
167 /* Copy a string from little-endian or big-endian unicode source (depending
168 * on flags) to internal samba format destination
170 int rpcstr_pull(char* dest, void *src, int dest_len, int src_len, int flags)
172 if(dest_len==-1) dest_len=MAXUNI-3;
173 return pull_ucs2(NULL, dest, src, dest_len, src_len, flags|STR_UNICODE|STR_NOALIGN);
176 /* Copy a string from a unistr2 source to internal samba format
177 destination. Use this instead of direct calls to rpcstr_pull() to avoid
178 having to determine whether the source string is null terminated. */
180 int rpcstr_pull_unistr2_fstring(char *dest, UNISTR2 *src)
182 return pull_ucs2(NULL, dest, src->buffer, sizeof(fstring),
183 src->uni_str_len * 2, 0);
186 /* Converts a string from internal samba format to unicode
188 int rpcstr_push(void* dest, const char *src, int dest_len, int flags)
190 return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
193 /*******************************************************************
194 Return a DOS codepage version of a little-endian unicode string.
195 len is the filename length (ignoring any terminating zero) in uin16
196 units. Always null terminates.
197 Hack alert: uses fixed buffer(s).
198 ********************************************************************/
199 char *dos_unistrn2(const uint16 *src, int len)
201 static char lbufs[8][MAXUNI];
203 char *lbuf = lbufs[nexti];
205 pull_ucs2(NULL, lbuf, src, MAXUNI-3, len*2, STR_NOALIGN);
209 /*******************************************************************
210 Convert a (little-endian) UNISTR2 structure to an ASCII string
211 ********************************************************************/
212 void unistr2_to_ascii(char *dest, const UNISTR2 *str, size_t maxlen)
218 pull_ucs2(NULL, dest, str->buffer, maxlen, str->uni_str_len*2, STR_NOALIGN);
222 /*******************************************************************
223 duplicate a UNISTR2 string into a null terminated char*
224 using a talloc context
225 ********************************************************************/
226 char *unistr2_tdup(TALLOC_CTX *ctx, const UNISTR2 *str)
229 int maxlen = (str->uni_str_len+1)*4;
230 if (!str->buffer) return NULL;
231 s = (char *)talloc(ctx, maxlen); /* convervative */
233 pull_ucs2(NULL, s, str->buffer, maxlen, str->uni_str_len*2,
239 /*******************************************************************
240 Return a number stored in a buffer
241 ********************************************************************/
243 uint32 buffer2_to_uint32(BUFFER2 *str)
245 if (str->buf_len == 4)
246 return IVAL(str->buffer, 0);
251 /*******************************************************************
252 Convert a wchar to upper case.
253 ********************************************************************/
255 smb_ucs2_t toupper_w(smb_ucs2_t val)
257 return upcase_table[SVAL(&val,0)];
260 /*******************************************************************
261 Convert a wchar to lower case.
262 ********************************************************************/
264 smb_ucs2_t tolower_w( smb_ucs2_t val )
266 return lowcase_table[SVAL(&val,0)];
270 /*******************************************************************
271 determine if a character is lowercase
272 ********************************************************************/
273 BOOL islower_w(smb_ucs2_t c)
275 return upcase_table[SVAL(&c,0)] != c;
278 /*******************************************************************
279 determine if a character is uppercase
280 ********************************************************************/
281 BOOL isupper_w(smb_ucs2_t c)
283 return lowcase_table[SVAL(&c,0)] != c;
287 /*******************************************************************
288 determine if a character is valid in a 8.3 name
289 ********************************************************************/
290 BOOL isvalid83_w(smb_ucs2_t c)
292 return valid_table[SVAL(&c,0)] != 0;
295 /*******************************************************************
296 Count the number of characters in a smb_ucs2_t string.
297 ********************************************************************/
298 size_t strlen_w(const smb_ucs2_t *src)
302 for(len = 0; *src++; len++) ;
307 /*******************************************************************
308 Count up to max number of characters in a smb_ucs2_t string.
309 ********************************************************************/
310 size_t strnlen_w(const smb_ucs2_t *src, size_t max)
314 for(len = 0; *src++ && (len < max); len++) ;
319 /*******************************************************************
321 ********************************************************************/
322 smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
325 if (c == *s) return (smb_ucs2_t *)s;
331 smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
333 return strchr_w(s, UCS2_CHAR(c));
336 smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
338 const smb_ucs2_t *p = s;
339 int len = strlen_w(s);
340 if (len == 0) return NULL;
343 if (c == *p) return (smb_ucs2_t *)p;
348 /*******************************************************************
350 ********************************************************************/
351 smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
356 if (!s || !*s || !ins || !*ins) return NULL;
358 inslen = strlen_w(ins);
360 while ((r = strchr_w(r, *ins))) {
361 if (strncmp_w(r, ins, inslen) == 0) return r;
367 /*******************************************************************
368 Convert a string to lower case.
369 return True if any char is converted
370 ********************************************************************/
371 BOOL strlower_w(smb_ucs2_t *s)
375 smb_ucs2_t v = tolower_w(*s);
385 /*******************************************************************
386 Convert a string to upper case.
387 return True if any char is converted
388 ********************************************************************/
389 BOOL strupper_w(smb_ucs2_t *s)
393 smb_ucs2_t v = toupper_w(*s);
403 /*******************************************************************
404 convert a string to "normal" form
405 ********************************************************************/
406 void strnorm_w(smb_ucs2_t *s)
408 extern int case_default;
409 if (case_default == CASE_UPPER)
415 int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
417 while (*b && *a == *b) { a++; b++; }
419 /* warning: if *a != *b and both are not 0 we retrun a random
420 greater or lesser than 0 number not realted to which
424 int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
427 while ((n < len) && *b && *a == *b) { a++; b++; n++;}
428 return (len - n)?(*a - *b):0;
431 /*******************************************************************
432 case insensitive string comparison
433 ********************************************************************/
434 int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
436 while (*b && toupper_w(*a) == toupper_w(*b)) { a++; b++; }
437 return (tolower_w(*a) - tolower_w(*b));
440 /*******************************************************************
441 case insensitive string comparison, lenght limited
442 ********************************************************************/
443 int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
446 while ((n < len) && *b && (toupper_w(*a) == toupper_w(*b))) { a++; b++; n++; }
447 return (len - n)?(tolower_w(*a) - tolower_w(*b)):0;
450 /*******************************************************************
452 ********************************************************************/
453 BOOL strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
455 if (s1 == s2) return(True);
456 if (!s1 || !s2) return(False);
458 return(strcasecmp_w(s1,s2)==0);
461 /*******************************************************************
462 compare 2 strings up to and including the nth char.
463 ******************************************************************/
464 BOOL strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
466 if (s1 == s2) return(True);
467 if (!s1 || !s2 || !n) return(False);
469 return(strncasecmp_w(s1,s2,n)==0);
472 /*******************************************************************
474 ********************************************************************/
475 smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
477 return strndup_w(src, 0);
480 /* if len == 0 then duplicate the whole string */
481 smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
485 if (!len) len = strlen_w(src);
486 dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
488 DEBUG(0,("strdup_w: out of memory!\n"));
492 memcpy(dest, src, len * sizeof(smb_ucs2_t));
498 /*******************************************************************
499 copy a string with max len
500 ********************************************************************/
502 smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
506 if (!dest || !src) return NULL;
508 for (len = 0; (src[len] != 0) && (len < max); len++)
509 dest[len] = src[len];
517 /*******************************************************************
518 append a string of len bytes and add a terminator
519 ********************************************************************/
521 smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
526 if (!dest || !src) return NULL;
528 start = strlen_w(dest);
529 len = strnlen_w(src, max);
531 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
537 smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
542 if (!dest || !src) return NULL;
544 start = strlen_w(dest);
547 memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
554 /*******************************************************************
555 replace any occurence of oldc with newc in unicode string
556 ********************************************************************/
558 void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
561 if(*s==oldc) *s=newc;
565 /*******************************************************************
567 ********************************************************************/
569 BOOL trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
570 const smb_ucs2_t *back)
573 size_t len, front_len, back_len;
575 if (!s || !*s) return False;
579 if (front && *front) {
580 front_len = strlen_w(front);
581 while (len && strncmp_w(s, front, front_len) == 0) {
582 memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
589 back_len = strlen_w(back);
590 while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
591 s[len - back_len] = 0;
601 The *_wa() functions take a combination of 7 bit ascii
602 and wide characters They are used so that you can use string
603 functions combining C string constants with ucs2 strings
605 The char* arguments must NOT be multibyte - to be completely sure
606 of this only pass string constants */
609 void pstrcpy_wa(smb_ucs2_t *dest, const char *src)
612 for (i=0;i<PSTRING_LEN;i++) {
613 dest[i] = UCS2_CHAR(src[i]);
614 if (src[i] == 0) return;
618 int strcmp_wa(const smb_ucs2_t *a, const char *b)
620 while (*b && *a == UCS2_CHAR(*b)) { a++; b++; }
621 return (*a - UCS2_CHAR(*b));
624 int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
627 while ((n < len) && *b && *a == UCS2_CHAR(*b)) { a++; b++; n++;}
628 return (len - n)?(*a - UCS2_CHAR(*b)):0;
631 smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
635 for (i=0; p[i] && *s != UCS2_CHAR(p[i]); i++)
637 if (p[i]) return (smb_ucs2_t *)s;
643 smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
648 if (!s || !*s || !ins || !*ins) return NULL;
650 inslen = strlen(ins);
652 while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
653 if (strncmp_wa(r, ins, inslen) == 0) return r;
659 /*******************************************************************
660 copy a string with max len
661 ********************************************************************/
663 smb_ucs2_t *strncpy_wa(smb_ucs2_t *dest, const char *src, const size_t max)
665 smb_ucs2_t *ucs2_src;
667 if (!dest || !src) return NULL;
668 if (!(ucs2_src = acnv_uxu2(src)))
671 strncpy_w(dest, ucs2_src, max);
676 /*******************************************************************
677 convert and duplicate an ascii string
678 ********************************************************************/
679 smb_ucs2_t *strdup_wa(const char *src)
681 return strndup_wa(src, 0);
684 /* if len == 0 then duplicate the whole string */
685 smb_ucs2_t *strndup_wa(const char *src, size_t len)
687 smb_ucs2_t *dest, *s;
690 if (!len) len = strlen_w(s);
691 dest = (smb_ucs2_t *)malloc((len + 1) * sizeof(smb_ucs2_t));
693 DEBUG(0,("strdup_w: out of memory!\n"));
698 memcpy(dest, src, len * sizeof(smb_ucs2_t));
705 /*******************************************************************
706 append a string of len bytes and add a terminator
707 ********************************************************************/
709 smb_ucs2_t *strncat_wa(smb_ucs2_t *dest, const char *src, const size_t max)
711 smb_ucs2_t *ucs2_src;
713 if (!dest || !src) return NULL;
714 if (!(ucs2_src = acnv_uxu2(src)))
717 strncat_w(dest, ucs2_src, max);
722 smb_ucs2_t *strcat_wa(smb_ucs2_t *dest, const char *src)
724 smb_ucs2_t *ucs2_src;
726 if (!dest || !src) return NULL;
727 if (!(ucs2_src = acnv_uxu2(src)))
730 strcat_w(dest, ucs2_src);
735 BOOL trim_string_wa(smb_ucs2_t *s, const char *front,
740 if (front) push_ucs2(NULL, f, front, sizeof(wpstring) - 1, STR_TERMINATE);
742 if (back) push_ucs2(NULL, b, back, sizeof(wpstring) - 1, STR_TERMINATE);
744 return trim_string_w(s, f, b);
747 /*******************************************************************
748 returns the length in number of wide characters
749 ******************************************************************/
750 int unistrlen(uint16 *s)
757 for (len=0; *s; s++,len++);
762 /*******************************************************************
763 Strcpy for unicode strings. returns length (in num of wide chars)
764 ********************************************************************/
766 int unistrcpy(uint16 *dst, uint16 *src)
780 * Samba ucs2 type to UNISTR2 conversion
782 * @param ctx Talloc context to create the dst strcture (if null) and the
783 * contents of the unicode string.
784 * @param dst UNISTR2 destination. If equals null, then it's allocated.
785 * @param src smb_ucs2_t source.
786 * @param max_len maximum number of unicode characters to copy. If equals
787 * null, then null-termination of src is taken
789 * @return copied UNISTR2 destination
791 UNISTR2* ucs2_to_unistr2(TALLOC_CTX *ctx, UNISTR2* dst, smb_ucs2_t* src)
795 if (!src) return NULL;
798 /* allocate UNISTR2 destination if not given */
800 dst = (UNISTR2*) talloc(ctx, sizeof(UNISTR2));
801 if (!dst) return NULL;
804 dst->buffer = (uint16*) talloc(ctx, sizeof(uint16) * (len + 1));
805 if (!dst->buffer) return NULL;
808 /* set UNISTR2 parameters */
809 dst->uni_max_len = len + 1;
811 dst->uni_str_len = len;
813 /* copy the actual unicode string */
814 strncpy_w(dst->buffer, src, dst->uni_max_len);