2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Tridgell 1992-2001
5 Copyright (C) Simo Sorce 2001
6 Copyright (C) Jeremy Allison 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
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 uint8
*valid_table
;
31 static bool initialized
;
33 /* Copy into a smb_ucs2_t from a possibly unaligned buffer. Return the copied smb_ucs2_t */
34 #define COPY_UCS2_CHAR(dest,src) (((unsigned char *)(dest))[0] = ((unsigned char *)(src))[0],\
35 ((unsigned char *)(dest))[1] = ((unsigned char *)(src))[1], (dest))
38 /* return an ascii version of a ucs2 character */
39 #define UCS2_TO_CHAR(c) (((c) >> UCS2_SHIFT) & 0xff)
43 * Destroy global objects allocated by load_case_tables()
45 void gfree_case_tables(void)
48 unmap_file(valid_table
, 0x10000);
55 * Load the valid character map table from <tt>valid.dat</tt> or
56 * create from the configured codepage.
58 * This function is called whenever the configuration is reloaded.
59 * However, the valid character table is not changed if it's loaded
60 * from a file, because we can't unmap files.
63 static void init_valid_table(void)
69 valid_table
= (uint8
*)map_file(data_path("valid.dat"), 0x10000);
71 smb_panic("Could not load valid.dat file required for mangle method=hash");
76 /*******************************************************************
77 Write a string in (little-endian) unicode format. src is in
78 the current DOS codepage. len is the length in bytes of the
79 string pointed to by dst.
81 if null_terminate is True then null terminate the packet (adds 2 bytes)
83 the return value is the length in bytes consumed by the string, including the
84 null termination if applied
85 ********************************************************************/
87 size_t dos_PutUniCode(char *dst
,const char *src
, size_t len
, bool null_terminate
)
89 int flags
= null_terminate
? STR_UNICODE
|STR_NOALIGN
|STR_TERMINATE
90 : STR_UNICODE
|STR_NOALIGN
;
91 return push_ucs2(NULL
, dst
, src
, len
, flags
);
95 /*******************************************************************
96 Skip past a unicode string, but not more than len. Always move
97 past a terminating zero if found.
98 ********************************************************************/
100 char *skip_unibuf(char *src
, size_t len
)
102 char *srcend
= src
+ len
;
104 while (src
< srcend
&& SVAL(src
,0)) {
115 /* Converts a string from internal samba format to unicode
118 int rpcstr_push(void *dest
, const char *src
, size_t dest_len
, int flags
)
120 return push_ucs2(NULL
, dest
, src
, dest_len
, flags
|STR_UNICODE
|STR_NOALIGN
);
123 /* Converts a string from internal samba format to unicode. Always terminates.
124 * Actually just a wrapper round push_ucs2_talloc().
127 int rpcstr_push_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
130 if (push_ucs2_talloc(ctx
, dest
, src
, &size
))
136 /*******************************************************************
137 Determine if a character is valid in a 8.3 name.
138 ********************************************************************/
140 bool isvalid83_w(smb_ucs2_t c
)
143 return valid_table
[SVAL(&c
,0)] != 0;
146 /*******************************************************************
147 Count the number of two-byte pairs in a UTF16 string.
148 ********************************************************************/
150 size_t strlen_w(const smb_ucs2_t
*src
)
155 for(len
= 0; *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
162 /*******************************************************************
163 Count up to max number of characters in a smb_ucs2_t string.
164 ********************************************************************/
166 size_t strnlen_w(const smb_ucs2_t
*src
, size_t max
)
171 for(len
= 0; (len
< max
) && *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
178 /*******************************************************************
180 ********************************************************************/
182 smb_ucs2_t
*strchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
185 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
187 return (smb_ucs2_t
*)s
;
192 return (smb_ucs2_t
*)s
;
198 smb_ucs2_t
*strchr_wa(const smb_ucs2_t
*s
, char c
)
200 return strchr_w(s
, UCS2_CHAR(c
));
203 /*******************************************************************
205 ********************************************************************/
207 smb_ucs2_t
*strrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
210 const smb_ucs2_t
*p
= s
;
211 int len
= strlen_w(s
);
218 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
219 return (smb_ucs2_t
*)p
;
225 /*******************************************************************
226 Wide version of strrchr that returns after doing strrchr 'n' times.
227 ********************************************************************/
229 smb_ucs2_t
*strnrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
, unsigned int n
)
232 const smb_ucs2_t
*p
= s
;
233 int len
= strlen_w(s
);
235 if (len
== 0 || !n
) {
240 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
245 return (smb_ucs2_t
*)p
;
251 /*******************************************************************
253 ********************************************************************/
255 smb_ucs2_t
*strstr_w(const smb_ucs2_t
*s
, const smb_ucs2_t
*ins
)
260 if (!s
|| !*s
|| !ins
|| !*ins
) {
264 inslen
= strlen_w(ins
);
267 while ((r
= strchr_w(r
, *ins
))) {
268 if (strncmp_w(r
, ins
, inslen
) == 0) {
277 /*******************************************************************
278 Convert a string to lower case.
279 return True if any char is converted
281 This is unsafe for any string involving a UTF16 character
282 ********************************************************************/
284 bool strlower_w(smb_ucs2_t
*s
)
289 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
290 smb_ucs2_t v
= tolower_m(cp
);
292 COPY_UCS2_CHAR(s
,&v
);
300 /*******************************************************************
301 Convert a string to upper case.
302 return True if any char is converted
304 This is unsafe for any string involving a UTF16 character
305 ********************************************************************/
307 bool strupper_w(smb_ucs2_t
*s
)
311 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
312 smb_ucs2_t v
= toupper_m(cp
);
314 COPY_UCS2_CHAR(s
,&v
);
322 /*******************************************************************
323 Convert a string to "normal" form.
324 ********************************************************************/
326 void strnorm_w(smb_ucs2_t
*s
, int case_default
)
328 if (case_default
== CASE_UPPER
) {
335 int strcmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
339 while ((*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
343 return (*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
)));
344 /* warning: if *a != *b and both are not 0 we return a random
345 greater or lesser than 0 number not realted to which
349 int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
354 while ((n
< len
) && (*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
359 return (len
- n
)?(*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
))):0;
362 /*******************************************************************
363 Case insensitive string comparison.
364 ********************************************************************/
366 int strcasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
370 while ((*COPY_UCS2_CHAR(&cpb
,b
)) && toupper_m(*(COPY_UCS2_CHAR(&cpa
,a
))) == toupper_m(cpb
)) {
374 return (tolower_m(*(COPY_UCS2_CHAR(&cpa
,a
))) - tolower_m(*(COPY_UCS2_CHAR(&cpb
,b
))));
377 /*******************************************************************
378 Case insensitive string comparison, length limited.
379 ********************************************************************/
381 int strncasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
386 while ((n
< len
) && *COPY_UCS2_CHAR(&cpb
,b
) && (toupper_m(*(COPY_UCS2_CHAR(&cpa
,a
))) == toupper_m(cpb
))) {
391 return (len
- n
)?(tolower_m(*(COPY_UCS2_CHAR(&cpa
,a
))) - tolower_m(*(COPY_UCS2_CHAR(&cpb
,b
)))):0;
394 /*******************************************************************
396 ********************************************************************/
398 smb_ucs2_t
*strdup_w(const smb_ucs2_t
*src
)
401 size_t len
= strlen_w(src
);
402 dest
= SMB_MALLOC_ARRAY(smb_ucs2_t
, len
+ 1);
404 DEBUG(0,("strdup_w: out of memory!\n"));
408 memcpy(dest
, src
, len
* sizeof(smb_ucs2_t
));
413 The *_wa() functions take a combination of 7 bit ascii
414 and wide characters They are used so that you can use string
415 functions combining C string constants with ucs2 strings
417 The char* arguments must NOT be multibyte - to be completely sure
418 of this only pass string constants */
420 int strcmp_wa(const smb_ucs2_t
*a
, const char *b
)
424 while (*b
&& *(COPY_UCS2_CHAR(&cp
,a
)) == UCS2_CHAR(*b
)) {
428 return (*(COPY_UCS2_CHAR(&cp
,a
)) - UCS2_CHAR(*b
));
431 /*************************************************************
432 ascii only toupper - saves the need for smbd to be in C locale.
433 *************************************************************/
435 int toupper_ascii(int c
)
437 smb_ucs2_t uc
= toupper_m(UCS2_CHAR(c
));
438 return UCS2_TO_CHAR(uc
);
441 /*************************************************************
442 ascii only tolower - saves the need for smbd to be in C locale.
443 *************************************************************/
445 int tolower_ascii(int c
)
447 smb_ucs2_t uc
= tolower_m(UCS2_CHAR(c
));
448 return UCS2_TO_CHAR(uc
);
451 /*************************************************************
452 ascii only isupper - saves the need for smbd to be in C locale.
453 *************************************************************/
455 int isupper_ascii(int c
)
457 return isupper_m(UCS2_CHAR(c
));
460 /*************************************************************
461 ascii only islower - saves the need for smbd to be in C locale.
462 *************************************************************/
464 int islower_ascii(int c
)
466 return islower_m(UCS2_CHAR(c
));