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/>.
24 /* these 3 tables define the unicode case handling. They are loaded
25 at startup either via mmap() or read() from the lib directory */
26 static uint8
*valid_table
;
27 static bool initialized
;
30 * Destroy global objects allocated by load_case_tables()
32 void gfree_case_tables(void)
35 unmap_file(valid_table
, 0x10000);
42 * Load the valid character map table from <tt>valid.dat</tt> or
43 * create from the configured codepage.
45 * This function is called whenever the configuration is reloaded.
46 * However, the valid character table is not changed if it's loaded
47 * from a file, because we can't unmap files.
50 static void init_valid_table(void)
56 valid_table
= (uint8
*)map_file(data_path("valid.dat"), 0x10000);
58 smb_panic("Could not load valid.dat file required for mangle method=hash");
63 /*******************************************************************
64 Write a string in (little-endian) unicode format. src is in
65 the current DOS codepage. len is the length in bytes of the
66 string pointed to by dst.
68 if null_terminate is True then null terminate the packet (adds 2 bytes)
70 the return value is the length in bytes consumed by the string, including the
71 null termination if applied
72 ********************************************************************/
74 size_t dos_PutUniCode(char *dst
,const char *src
, size_t len
, bool null_terminate
)
76 int flags
= null_terminate
? STR_UNICODE
|STR_NOALIGN
|STR_TERMINATE
77 : STR_UNICODE
|STR_NOALIGN
;
78 return push_ucs2(NULL
, dst
, src
, len
, flags
);
82 /* Converts a string from internal samba format to unicode
85 int rpcstr_push(void *dest
, const char *src
, size_t dest_len
, int flags
)
87 return push_ucs2(NULL
, dest
, src
, dest_len
, flags
|STR_UNICODE
|STR_NOALIGN
);
90 /* Converts a string from internal samba format to unicode. Always terminates.
91 * Actually just a wrapper round push_ucs2_talloc().
94 int rpcstr_push_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
97 if (push_ucs2_talloc(ctx
, dest
, src
, &size
))
103 /*******************************************************************
104 Determine if a character is valid in a 8.3 name.
105 ********************************************************************/
107 bool isvalid83_w(smb_ucs2_t c
)
110 return valid_table
[SVAL(&c
,0)] != 0;
113 /*******************************************************************
114 Count the number of characters in a smb_ucs2_t string.
115 ********************************************************************/
117 size_t strlen_w(const smb_ucs2_t
*src
)
122 for(len
= 0; *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
129 /*******************************************************************
130 Count up to max number of characters in a smb_ucs2_t string.
131 ********************************************************************/
133 size_t strnlen_w(const smb_ucs2_t
*src
, size_t max
)
138 for(len
= 0; (len
< max
) && *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
145 /*******************************************************************
147 ********************************************************************/
149 smb_ucs2_t
*strchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
152 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
154 return (smb_ucs2_t
*)s
;
159 return (smb_ucs2_t
*)s
;
165 smb_ucs2_t
*strchr_wa(const smb_ucs2_t
*s
, char c
)
167 return strchr_w(s
, UCS2_CHAR(c
));
170 /*******************************************************************
172 ********************************************************************/
174 smb_ucs2_t
*strrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
177 const smb_ucs2_t
*p
= s
;
178 int len
= strlen_w(s
);
185 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
186 return (smb_ucs2_t
*)p
;
192 /*******************************************************************
193 Wide version of strrchr that returns after doing strrchr 'n' times.
194 ********************************************************************/
196 smb_ucs2_t
*strnrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
, unsigned int n
)
199 const smb_ucs2_t
*p
= s
;
200 int len
= strlen_w(s
);
202 if (len
== 0 || !n
) {
207 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
212 return (smb_ucs2_t
*)p
;
218 /*******************************************************************
220 ********************************************************************/
222 smb_ucs2_t
*strstr_w(const smb_ucs2_t
*s
, const smb_ucs2_t
*ins
)
227 if (!s
|| !*s
|| !ins
|| !*ins
) {
231 inslen
= strlen_w(ins
);
234 while ((r
= strchr_w(r
, *ins
))) {
235 if (strncmp_w(r
, ins
, inslen
) == 0) {
244 /*******************************************************************
245 Convert a string to lower case.
246 return True if any char is converted
248 This is unsafe for any string involving a UTF16 character
249 ********************************************************************/
251 bool strlower_w(smb_ucs2_t
*s
)
256 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
257 smb_ucs2_t v
= tolower_m(cp
);
259 COPY_UCS2_CHAR(s
,&v
);
267 /*******************************************************************
268 Convert a string to upper case.
269 return True if any char is converted
271 This is unsafe for any string involving a UTF16 character
272 ********************************************************************/
274 bool strupper_w(smb_ucs2_t
*s
)
278 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
279 smb_ucs2_t v
= toupper_m(cp
);
281 COPY_UCS2_CHAR(s
,&v
);
289 /*******************************************************************
290 Convert a string to "normal" form.
291 ********************************************************************/
293 void strnorm_w(smb_ucs2_t
*s
, int case_default
)
295 if (case_default
== CASE_UPPER
) {
302 int strcmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
306 while ((*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
310 return (*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
)));
311 /* warning: if *a != *b and both are not 0 we return a random
312 greater or lesser than 0 number not realted to which
316 int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
321 while ((n
< len
) && (*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
326 return (len
- n
)?(*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
))):0;
329 /*******************************************************************
330 Case insensitive string comparison.
331 ********************************************************************/
333 int strcasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
337 while ((*COPY_UCS2_CHAR(&cpb
,b
)) && toupper_m(*(COPY_UCS2_CHAR(&cpa
,a
))) == toupper_m(cpb
)) {
341 return (tolower_m(*(COPY_UCS2_CHAR(&cpa
,a
))) - tolower_m(*(COPY_UCS2_CHAR(&cpb
,b
))));
344 /*******************************************************************
345 Case insensitive string comparison, length limited.
346 ********************************************************************/
348 int strncasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
353 while ((n
< len
) && *COPY_UCS2_CHAR(&cpb
,b
) && (toupper_m(*(COPY_UCS2_CHAR(&cpa
,a
))) == toupper_m(cpb
))) {
358 return (len
- n
)?(tolower_m(*(COPY_UCS2_CHAR(&cpa
,a
))) - tolower_m(*(COPY_UCS2_CHAR(&cpb
,b
)))):0;
361 /*******************************************************************
363 ********************************************************************/
365 bool strequal_w(const smb_ucs2_t
*s1
, const smb_ucs2_t
*s2
)
374 return(strcasecmp_w(s1
,s2
)==0);
377 /*******************************************************************
378 Compare 2 strings up to and including the nth char.
379 ******************************************************************/
381 bool strnequal_w(const smb_ucs2_t
*s1
,const smb_ucs2_t
*s2
,size_t n
)
386 if (!s1
|| !s2
|| !n
) {
390 return(strncasecmp_w(s1
,s2
,n
)==0);
393 /*******************************************************************
395 ********************************************************************/
397 smb_ucs2_t
*strdup_w(const smb_ucs2_t
*src
)
399 return strndup_w(src
, 0);
402 /* if len == 0 then duplicate the whole string */
404 smb_ucs2_t
*strndup_w(const smb_ucs2_t
*src
, size_t len
)
411 dest
= SMB_MALLOC_ARRAY(smb_ucs2_t
, len
+ 1);
413 DEBUG(0,("strdup_w: out of memory!\n"));
417 memcpy(dest
, src
, len
* sizeof(smb_ucs2_t
));
422 /*******************************************************************
423 Copy a string with max len.
424 ********************************************************************/
426 smb_ucs2_t
*strncpy_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
, const size_t max
)
435 for (len
= 0; (*COPY_UCS2_CHAR(&cp
,(src
+len
))) && (len
< max
); len
++) {
436 cp
= *COPY_UCS2_CHAR(dest
+len
,src
+len
);
439 for ( /*nothing*/ ; len
< max
; len
++ ) {
440 cp
= *COPY_UCS2_CHAR(dest
+len
,&cp
);
446 /*******************************************************************
447 Append a string of len bytes and add a terminator.
448 ********************************************************************/
450 smb_ucs2_t
*strncat_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
, const size_t max
)
460 start
= strlen_w(dest
);
461 len
= strnlen_w(src
, max
);
463 memcpy(&dest
[start
], src
, len
*sizeof(smb_ucs2_t
));
464 z
= *COPY_UCS2_CHAR(dest
+start
+len
,&z
);
469 smb_ucs2_t
*strcat_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
)
479 start
= strlen_w(dest
);
482 memcpy(&dest
[start
], src
, len
*sizeof(smb_ucs2_t
));
483 z
= *COPY_UCS2_CHAR(dest
+start
+len
,&z
);
489 /*******************************************************************
490 Replace any occurence of oldc with newc in unicode string.
491 ********************************************************************/
493 void string_replace_w(smb_ucs2_t
*s
, smb_ucs2_t oldc
, smb_ucs2_t newc
)
497 for(;*(COPY_UCS2_CHAR(&cp
,s
));s
++) {
499 COPY_UCS2_CHAR(s
,&newc
);
504 /*******************************************************************
506 ********************************************************************/
508 bool trim_string_w(smb_ucs2_t
*s
, const smb_ucs2_t
*front
,
509 const smb_ucs2_t
*back
)
512 size_t len
, front_len
, back_len
;
520 if (front
&& *front
) {
521 front_len
= strlen_w(front
);
522 while (len
&& strncmp_w(s
, front
, front_len
) == 0) {
523 memmove(s
, (s
+ front_len
), (len
- front_len
+ 1) * sizeof(smb_ucs2_t
));
530 back_len
= strlen_w(back
);
531 while (len
&& strncmp_w((s
+ (len
- back_len
)), back
, back_len
) == 0) {
532 s
[len
- back_len
] = 0;
542 The *_wa() functions take a combination of 7 bit ascii
543 and wide characters They are used so that you can use string
544 functions combining C string constants with ucs2 strings
546 The char* arguments must NOT be multibyte - to be completely sure
547 of this only pass string constants */
549 int strcmp_wa(const smb_ucs2_t
*a
, const char *b
)
553 while (*b
&& *(COPY_UCS2_CHAR(&cp
,a
)) == UCS2_CHAR(*b
)) {
557 return (*(COPY_UCS2_CHAR(&cp
,a
)) - UCS2_CHAR(*b
));
560 int strncmp_wa(const smb_ucs2_t
*a
, const char *b
, size_t len
)
565 while ((n
< len
) && *b
&& *(COPY_UCS2_CHAR(&cp
,a
)) == UCS2_CHAR(*b
)) {
570 return (len
- n
)?(*(COPY_UCS2_CHAR(&cp
,a
)) - UCS2_CHAR(*b
)):0;
573 smb_ucs2_t
*strpbrk_wa(const smb_ucs2_t
*s
, const char *p
)
577 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
579 for (i
=0; p
[i
] && cp
!= UCS2_CHAR(p
[i
]); i
++)
582 return (smb_ucs2_t
*)s
;
589 smb_ucs2_t
*strstr_wa(const smb_ucs2_t
*s
, const char *ins
)
598 inslen
= strlen(ins
);
601 while ((r
= strchr_w(r
, UCS2_CHAR(*ins
)))) {
602 if (strncmp_wa(r
, ins
, inslen
) == 0)
610 /*************************************************************
611 ascii only toupper - saves the need for smbd to be in C locale.
612 *************************************************************/
614 int toupper_ascii(int c
)
616 smb_ucs2_t uc
= toupper_m(UCS2_CHAR(c
));
617 return UCS2_TO_CHAR(uc
);
620 /*************************************************************
621 ascii only tolower - saves the need for smbd to be in C locale.
622 *************************************************************/
624 int tolower_ascii(int c
)
626 smb_ucs2_t uc
= tolower_m(UCS2_CHAR(c
));
627 return UCS2_TO_CHAR(uc
);
630 /*************************************************************
631 ascii only isupper - saves the need for smbd to be in C locale.
632 *************************************************************/
634 int isupper_ascii(int c
)
636 return isupper_m(UCS2_CHAR(c
));
639 /*************************************************************
640 ascii only islower - saves the need for smbd to be in C locale.
641 *************************************************************/
643 int islower_ascii(int c
)
645 return islower_m(UCS2_CHAR(c
));