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 * This table says which Unicode characters are valid dos
38 * Each value is just a single bit.
40 static uint8 doschar_table
[8192]; /* 65536 characters / 8 bits/byte */
44 * Load or generate the case handling tables.
46 * The case tables are defined in UCS2 and don't depend on any
47 * configured parameters, so they never need to be reloaded.
49 void load_case_tables(void)
51 static int initialised
;
54 if (initialised
) return;
57 upcase_table
= map_file(lib_path("upcase.dat"), 0x20000);
58 lowcase_table
= map_file(lib_path("lowcase.dat"), 0x20000);
60 /* we would like Samba to limp along even if these tables are
63 DEBUG(1,("creating lame upcase table\n"));
64 upcase_table
= SMB_MALLOC(0x20000);
65 for (i
=0;i
<0x10000;i
++) {
72 SSVAL(&v
, 0, UCS2_CHAR(i
));
73 upcase_table
[v
] = UCS2_CHAR(islower(i
)?toupper(i
):i
);
78 DEBUG(1,("creating lame lowcase table\n"));
79 lowcase_table
= SMB_MALLOC(0x20000);
80 for (i
=0;i
<0x10000;i
++) {
87 SSVAL(&v
, 0, UCS2_CHAR(i
));
88 lowcase_table
[v
] = UCS2_CHAR(isupper(i
)?tolower(i
):i
);
94 see if a ucs2 character can be mapped correctly to a dos character
95 and mapped back to the same character in ucs2
97 int check_dos_char(smb_ucs2_t c
)
99 lazy_initialize_conv();
101 /* Find the right byte, and right bit within the byte; return
103 return (doschar_table
[(c
& 0xffff) / 8] & (1 << (c
& 7))) != 0;
107 static int check_dos_char_slowly(smb_ucs2_t c
)
112 len1
= convert_string(CH_UCS2
, CH_DOS
, &c
, 2, buf
, sizeof(buf
),False
);
113 if (len1
== 0) return 0;
114 len2
= convert_string(CH_DOS
, CH_UCS2
, buf
, len1
, &c2
, 2,False
);
115 if (len2
!= 2) return 0;
121 * Fill out doschar table the hard way, by examining each character
123 void init_doschar_table(void)
127 /* For each byte of packed table */
129 for (i
= 0; i
<= 0xffff; i
+= 8) {
131 for (j
= 0; j
<= 7; j
++) {
136 if (check_dos_char_slowly(c
))
139 doschar_table
[i
/8] = byteval
;
145 * Load the valid character map table from <tt>valid.dat</tt> or
146 * create from the configured codepage.
148 * This function is called whenever the configuration is reloaded.
149 * However, the valid character table is not changed if it's loaded
150 * from a file, because we can't unmap files.
152 void init_valid_table(void)
154 static int mapped_file
;
156 const char *allowed
= ".!#$%&'()_-@^`~";
160 /* Can't unmap files, so stick with what we have */
164 valid_file
= map_file(lib_path("valid.dat"), 0x10000);
166 valid_table
= valid_file
;
171 /* Otherwise, we're using a dynamically created valid_table.
172 * It might need to be regenerated if the code page changed.
173 * We know that we're not using a mapped file, so we can
174 * free() the old one. */
175 if (valid_table
) free(valid_table
);
177 DEBUG(2,("creating default valid table\n"));
178 valid_table
= SMB_MALLOC(0x10000);
180 valid_table
[i
] = isalnum(i
) || strchr(allowed
,i
);
182 for (;i
<0x10000;i
++) {
185 valid_table
[i
] = check_dos_char(c
);
191 /*******************************************************************
192 Write a string in (little-endian) unicode format. src is in
193 the current DOS codepage. len is the length in bytes of the
194 string pointed to by dst.
196 if null_terminate is True then null terminate the packet (adds 2 bytes)
198 the return value is the length in bytes consumed by the string, including the
199 null termination if applied
200 ********************************************************************/
202 size_t dos_PutUniCode(char *dst
,const char *src
, ssize_t len
, BOOL null_terminate
)
204 return push_ucs2(NULL
, dst
, src
, len
,
205 STR_UNICODE
|STR_NOALIGN
| (null_terminate
?STR_TERMINATE
:0));
209 /*******************************************************************
210 Skip past a unicode string, but not more than len. Always move
211 past a terminating zero if found.
212 ********************************************************************/
214 char *skip_unibuf(char *src
, size_t len
)
216 char *srcend
= src
+ len
;
218 while (src
< srcend
&& SVAL(src
,0))
227 /* Copy a string from little-endian or big-endian unicode source (depending
228 * on flags) to internal samba format destination
230 int rpcstr_pull(char* dest
, void *src
, int dest_len
, int src_len
, int flags
)
236 if(dest_len
==-1) dest_len
=MAXUNI
-3;
237 return pull_ucs2(NULL
, dest
, src
, dest_len
, src_len
, flags
|STR_UNICODE
|STR_NOALIGN
);
240 /* Copy a string from a unistr2 source to internal samba format
241 destination. Use this instead of direct calls to rpcstr_pull() to avoid
242 having to determine whether the source string is null terminated. */
244 int rpcstr_pull_unistr2_fstring(char *dest
, UNISTR2
*src
)
246 return pull_ucs2(NULL
, dest
, src
->buffer
, sizeof(fstring
),
247 src
->uni_str_len
* 2, 0);
250 /* Converts a string from internal samba format to unicode
252 int rpcstr_push(void* dest
, const char *src
, int dest_len
, int flags
)
254 return push_ucs2(NULL
, dest
, src
, dest_len
, flags
|STR_UNICODE
|STR_NOALIGN
);
257 /*******************************************************************
258 Return a DOS codepage version of a little-endian unicode string.
259 len is the filename length (ignoring any terminating zero) in uin16
260 units. Always null terminates.
261 Hack alert: uses fixed buffer(s).
262 ********************************************************************/
263 char *dos_unistrn2(const uint16
*src
, int len
)
265 static char lbufs
[8][MAXUNI
];
267 char *lbuf
= lbufs
[nexti
];
269 pull_ucs2(NULL
, lbuf
, src
, MAXUNI
-3, len
*2, STR_NOALIGN
);
273 /*******************************************************************
274 Convert a (little-endian) UNISTR2 structure to an ASCII string
275 ********************************************************************/
276 void unistr2_to_ascii(char *dest
, const UNISTR2
*str
, size_t maxlen
)
282 pull_ucs2(NULL
, dest
, str
->buffer
, maxlen
, str
->uni_str_len
*2, STR_NOALIGN
);
285 /*******************************************************************
286 give a static string for displaying a UNISTR2
287 ********************************************************************/
288 const char *unistr2_static(const UNISTR2
*str
)
291 unistr2_to_ascii(ret
, str
, sizeof(ret
));
296 /*******************************************************************
297 duplicate a UNISTR2 string into a null terminated char*
298 using a talloc context
299 ********************************************************************/
300 char *unistr2_tdup(TALLOC_CTX
*ctx
, const UNISTR2
*str
)
303 int maxlen
= (str
->uni_str_len
+1)*4;
304 if (!str
->buffer
) return NULL
;
305 s
= (char *)TALLOC(ctx
, maxlen
); /* convervative */
307 pull_ucs2(NULL
, s
, str
->buffer
, maxlen
, str
->uni_str_len
*2,
313 /*******************************************************************
314 Return a number stored in a buffer
315 ********************************************************************/
317 uint32
buffer2_to_uint32(BUFFER2
*str
)
319 if (str
->buf_len
== 4)
320 return IVAL(str
->buffer
, 0);
325 /*******************************************************************
326 Convert a wchar to upper case.
327 ********************************************************************/
329 smb_ucs2_t
toupper_w(smb_ucs2_t val
)
331 return upcase_table
[SVAL(&val
,0)];
334 /*******************************************************************
335 Convert a wchar to lower case.
336 ********************************************************************/
338 smb_ucs2_t
tolower_w( smb_ucs2_t val
)
340 return lowcase_table
[SVAL(&val
,0)];
344 /*******************************************************************
345 determine if a character is lowercase
346 ********************************************************************/
347 BOOL
islower_w(smb_ucs2_t c
)
349 return upcase_table
[SVAL(&c
,0)] != c
;
352 /*******************************************************************
353 determine if a character is uppercase
354 ********************************************************************/
355 BOOL
isupper_w(smb_ucs2_t c
)
357 return lowcase_table
[SVAL(&c
,0)] != c
;
361 /*******************************************************************
362 determine if a character is valid in a 8.3 name
363 ********************************************************************/
364 BOOL
isvalid83_w(smb_ucs2_t c
)
366 return valid_table
[SVAL(&c
,0)] != 0;
369 /*******************************************************************
370 Count the number of characters in a smb_ucs2_t string.
371 ********************************************************************/
372 size_t strlen_w(const smb_ucs2_t
*src
)
376 for(len
= 0; *src
++; len
++) ;
381 /*******************************************************************
382 Count up to max number of characters in a smb_ucs2_t string.
383 ********************************************************************/
384 size_t strnlen_w(const smb_ucs2_t
*src
, size_t max
)
388 for(len
= 0; *src
++ && (len
< max
); len
++) ;
393 /*******************************************************************
395 ********************************************************************/
397 smb_ucs2_t
*strchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
400 if (c
== *s
) return (smb_ucs2_t
*)s
;
403 if (c
== *s
) return (smb_ucs2_t
*)s
;
408 smb_ucs2_t
*strchr_wa(const smb_ucs2_t
*s
, char c
)
410 return strchr_w(s
, UCS2_CHAR(c
));
413 /*******************************************************************
415 ********************************************************************/
417 smb_ucs2_t
*strrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
419 const smb_ucs2_t
*p
= s
;
420 int len
= strlen_w(s
);
421 if (len
== 0) return NULL
;
424 if (c
== *p
) return (smb_ucs2_t
*)p
;
429 /*******************************************************************
430 Wide version of strrchr that returns after doing strrchr 'n' times.
431 ********************************************************************/
433 smb_ucs2_t
*strnrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
, unsigned int n
)
435 const smb_ucs2_t
*p
= s
;
436 int len
= strlen_w(s
);
445 return (smb_ucs2_t
*)p
;
450 /*******************************************************************
452 ********************************************************************/
454 smb_ucs2_t
*strstr_w(const smb_ucs2_t
*s
, const smb_ucs2_t
*ins
)
459 if (!s
|| !*s
|| !ins
|| !*ins
)
462 inslen
= strlen_w(ins
);
465 while ((r
= strchr_w(r
, *ins
))) {
466 if (strncmp_w(r
, ins
, inslen
) == 0)
474 /*******************************************************************
475 Convert a string to lower case.
476 return True if any char is converted
477 ********************************************************************/
478 BOOL
strlower_w(smb_ucs2_t
*s
)
482 smb_ucs2_t v
= tolower_w(*s
);
492 /*******************************************************************
493 Convert a string to upper case.
494 return True if any char is converted
495 ********************************************************************/
496 BOOL
strupper_w(smb_ucs2_t
*s
)
500 smb_ucs2_t v
= toupper_w(*s
);
510 /*******************************************************************
511 convert a string to "normal" form
512 ********************************************************************/
514 void strnorm_w(smb_ucs2_t
*s
, int case_default
)
516 if (case_default
== CASE_UPPER
)
522 int strcmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
524 while (*b
&& *a
== *b
) { a
++; b
++; }
526 /* warning: if *a != *b and both are not 0 we retrun a random
527 greater or lesser than 0 number not realted to which
531 int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
534 while ((n
< len
) && *b
&& *a
== *b
) { a
++; b
++; n
++;}
535 return (len
- n
)?(*a
- *b
):0;
538 /*******************************************************************
539 case insensitive string comparison
540 ********************************************************************/
541 int strcasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
543 while (*b
&& toupper_w(*a
) == toupper_w(*b
)) { a
++; b
++; }
544 return (tolower_w(*a
) - tolower_w(*b
));
547 /*******************************************************************
548 case insensitive string comparison, lenght limited
549 ********************************************************************/
550 int strncasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
553 while ((n
< len
) && *b
&& (toupper_w(*a
) == toupper_w(*b
))) { a
++; b
++; n
++; }
554 return (len
- n
)?(tolower_w(*a
) - tolower_w(*b
)):0;
557 /*******************************************************************
559 ********************************************************************/
560 BOOL
strequal_w(const smb_ucs2_t
*s1
, const smb_ucs2_t
*s2
)
562 if (s1
== s2
) return(True
);
563 if (!s1
|| !s2
) return(False
);
565 return(strcasecmp_w(s1
,s2
)==0);
568 /*******************************************************************
569 compare 2 strings up to and including the nth char.
570 ******************************************************************/
571 BOOL
strnequal_w(const smb_ucs2_t
*s1
,const smb_ucs2_t
*s2
,size_t n
)
573 if (s1
== s2
) return(True
);
574 if (!s1
|| !s2
|| !n
) return(False
);
576 return(strncasecmp_w(s1
,s2
,n
)==0);
579 /*******************************************************************
581 ********************************************************************/
582 smb_ucs2_t
*strdup_w(const smb_ucs2_t
*src
)
584 return strndup_w(src
, 0);
587 /* if len == 0 then duplicate the whole string */
588 smb_ucs2_t
*strndup_w(const smb_ucs2_t
*src
, size_t len
)
592 if (!len
) len
= strlen_w(src
);
593 dest
= SMB_MALLOC_ARRAY(smb_ucs2_t
, len
+ 1);
595 DEBUG(0,("strdup_w: out of memory!\n"));
599 memcpy(dest
, src
, len
* sizeof(smb_ucs2_t
));
605 /*******************************************************************
606 copy a string with max len
607 ********************************************************************/
609 smb_ucs2_t
*strncpy_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
, const size_t max
)
613 if (!dest
|| !src
) return NULL
;
615 for (len
= 0; (src
[len
] != 0) && (len
< max
); len
++)
616 dest
[len
] = src
[len
];
624 /*******************************************************************
625 append a string of len bytes and add a terminator
626 ********************************************************************/
628 smb_ucs2_t
*strncat_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
, const size_t max
)
633 if (!dest
|| !src
) return NULL
;
635 start
= strlen_w(dest
);
636 len
= strnlen_w(src
, max
);
638 memcpy(&dest
[start
], src
, len
*sizeof(smb_ucs2_t
));
644 smb_ucs2_t
*strcat_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
)
649 if (!dest
|| !src
) return NULL
;
651 start
= strlen_w(dest
);
654 memcpy(&dest
[start
], src
, len
*sizeof(smb_ucs2_t
));
661 /*******************************************************************
662 replace any occurence of oldc with newc in unicode string
663 ********************************************************************/
665 void string_replace_w(smb_ucs2_t
*s
, smb_ucs2_t oldc
, smb_ucs2_t newc
)
668 if(*s
==oldc
) *s
=newc
;
672 /*******************************************************************
674 ********************************************************************/
676 BOOL
trim_string_w(smb_ucs2_t
*s
, const smb_ucs2_t
*front
,
677 const smb_ucs2_t
*back
)
680 size_t len
, front_len
, back_len
;
682 if (!s
|| !*s
) return False
;
686 if (front
&& *front
) {
687 front_len
= strlen_w(front
);
688 while (len
&& strncmp_w(s
, front
, front_len
) == 0) {
689 memmove(s
, (s
+ front_len
), (len
- front_len
+ 1) * sizeof(smb_ucs2_t
));
696 back_len
= strlen_w(back
);
697 while (len
&& strncmp_w((s
+ (len
- back_len
)), back
, back_len
) == 0) {
698 s
[len
- back_len
] = 0;
708 The *_wa() functions take a combination of 7 bit ascii
709 and wide characters They are used so that you can use string
710 functions combining C string constants with ucs2 strings
712 The char* arguments must NOT be multibyte - to be completely sure
713 of this only pass string constants */
715 int strcmp_wa(const smb_ucs2_t
*a
, const char *b
)
717 while (*b
&& *a
== UCS2_CHAR(*b
)) { a
++; b
++; }
718 return (*a
- UCS2_CHAR(*b
));
721 int strncmp_wa(const smb_ucs2_t
*a
, const char *b
, size_t len
)
724 while ((n
< len
) && *b
&& *a
== UCS2_CHAR(*b
)) { a
++; b
++; n
++;}
725 return (len
- n
)?(*a
- UCS2_CHAR(*b
)):0;
728 smb_ucs2_t
*strpbrk_wa(const smb_ucs2_t
*s
, const char *p
)
732 for (i
=0; p
[i
] && *s
!= UCS2_CHAR(p
[i
]); i
++)
734 if (p
[i
]) return (smb_ucs2_t
*)s
;
740 smb_ucs2_t
*strstr_wa(const smb_ucs2_t
*s
, const char *ins
)
745 if (!s
|| !*s
|| !ins
|| !*ins
)
748 inslen
= strlen(ins
);
751 while ((r
= strchr_w(r
, UCS2_CHAR(*ins
)))) {
752 if (strncmp_wa(r
, ins
, inslen
) == 0)
760 BOOL
trim_string_wa(smb_ucs2_t
*s
, const char *front
,
765 if (front
) push_ucs2(NULL
, f
, front
, sizeof(wpstring
) - 1, STR_TERMINATE
);
767 if (back
) push_ucs2(NULL
, b
, back
, sizeof(wpstring
) - 1, STR_TERMINATE
);
769 return trim_string_w(s
, f
, b
);
772 /*******************************************************************
773 returns the length in number of wide characters
774 ******************************************************************/
775 int unistrlen(uint16
*s
)
782 for (len
=0; *s
; s
++,len
++);
787 /*******************************************************************
788 Strcpy for unicode strings. returns length (in num of wide chars)
789 ********************************************************************/
791 int unistrcpy(uint16
*dst
, uint16
*src
)
805 * Samba ucs2 type to UNISTR2 conversion
807 * @param ctx Talloc context to create the dst strcture (if null) and the
808 * contents of the unicode string.
809 * @param dst UNISTR2 destination. If equals null, then it's allocated.
810 * @param src smb_ucs2_t source.
811 * @param max_len maximum number of unicode characters to copy. If equals
812 * null, then null-termination of src is taken
814 * @return copied UNISTR2 destination
816 UNISTR2
* ucs2_to_unistr2(TALLOC_CTX
*ctx
, UNISTR2
* dst
, smb_ucs2_t
* src
)
824 /* allocate UNISTR2 destination if not given */
826 dst
= TALLOC_P(ctx
, UNISTR2
);
831 dst
->buffer
= TALLOC_ARRAY(ctx
, uint16
, len
+ 1);
836 /* set UNISTR2 parameters */
837 dst
->uni_max_len
= len
+ 1;
839 dst
->uni_str_len
= len
;
841 /* copy the actual unicode string */
842 strncpy_w(dst
->buffer
, src
, dst
->uni_max_len
);