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
;
34 * Destroy global objects allocated by load_case_tables()
36 void gfree_case_tables(void)
39 unmap_file(valid_table
, 0x10000);
46 * Load the valid character map table from <tt>valid.dat</tt> or
47 * create from the configured codepage.
49 * This function is called whenever the configuration is reloaded.
50 * However, the valid character table is not changed if it's loaded
51 * from a file, because we can't unmap files.
54 static void init_valid_table(void)
60 valid_table
= (uint8
*)map_file(data_path("valid.dat"), 0x10000);
62 smb_panic("Could not load valid.dat file required for mangle method=hash");
67 /*******************************************************************
68 Write a string in (little-endian) unicode format. src is in
69 the current DOS codepage. len is the length in bytes of the
70 string pointed to by dst.
72 if null_terminate is True then null terminate the packet (adds 2 bytes)
74 the return value is the length in bytes consumed by the string, including the
75 null termination if applied
76 ********************************************************************/
78 size_t dos_PutUniCode(char *dst
,const char *src
, size_t len
, bool null_terminate
)
80 int flags
= null_terminate
? STR_UNICODE
|STR_NOALIGN
|STR_TERMINATE
81 : STR_UNICODE
|STR_NOALIGN
;
82 return push_ucs2(NULL
, dst
, src
, len
, flags
);
86 /*******************************************************************
87 Skip past a unicode string, but not more than len. Always move
88 past a terminating zero if found.
89 ********************************************************************/
91 char *skip_unibuf(char *src
, size_t len
)
93 char *srcend
= src
+ len
;
95 while (src
< srcend
&& SVAL(src
,0)) {
106 /* Converts a string from internal samba format to unicode
109 int rpcstr_push(void *dest
, const char *src
, size_t dest_len
, int flags
)
111 return push_ucs2(NULL
, dest
, src
, dest_len
, flags
|STR_UNICODE
|STR_NOALIGN
);
114 /* Converts a string from internal samba format to unicode. Always terminates.
115 * Actually just a wrapper round push_ucs2_talloc().
118 int rpcstr_push_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
)
121 if (push_ucs2_talloc(ctx
, dest
, src
, &size
))
127 /*******************************************************************
128 Determine if a character is valid in a 8.3 name.
129 ********************************************************************/
131 bool isvalid83_w(smb_ucs2_t c
)
134 return valid_table
[SVAL(&c
,0)] != 0;
137 /*******************************************************************
138 Count the number of characters in a smb_ucs2_t string.
139 ********************************************************************/
141 size_t strlen_w(const smb_ucs2_t
*src
)
146 for(len
= 0; *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
153 /*******************************************************************
154 Count up to max number of characters in a smb_ucs2_t string.
155 ********************************************************************/
157 size_t strnlen_w(const smb_ucs2_t
*src
, size_t max
)
162 for(len
= 0; (len
< max
) && *(COPY_UCS2_CHAR(&c
,src
)); src
++, len
++) {
169 /*******************************************************************
171 ********************************************************************/
173 smb_ucs2_t
*strchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
176 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
178 return (smb_ucs2_t
*)s
;
183 return (smb_ucs2_t
*)s
;
189 smb_ucs2_t
*strchr_wa(const smb_ucs2_t
*s
, char c
)
191 return strchr_w(s
, UCS2_CHAR(c
));
194 /*******************************************************************
196 ********************************************************************/
198 smb_ucs2_t
*strrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
)
201 const smb_ucs2_t
*p
= s
;
202 int len
= strlen_w(s
);
209 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
210 return (smb_ucs2_t
*)p
;
216 /*******************************************************************
217 Wide version of strrchr that returns after doing strrchr 'n' times.
218 ********************************************************************/
220 smb_ucs2_t
*strnrchr_w(const smb_ucs2_t
*s
, smb_ucs2_t c
, unsigned int n
)
223 const smb_ucs2_t
*p
= s
;
224 int len
= strlen_w(s
);
226 if (len
== 0 || !n
) {
231 if (c
== *(COPY_UCS2_CHAR(&cp
,p
))) {
236 return (smb_ucs2_t
*)p
;
242 /*******************************************************************
244 ********************************************************************/
246 smb_ucs2_t
*strstr_w(const smb_ucs2_t
*s
, const smb_ucs2_t
*ins
)
251 if (!s
|| !*s
|| !ins
|| !*ins
) {
255 inslen
= strlen_w(ins
);
258 while ((r
= strchr_w(r
, *ins
))) {
259 if (strncmp_w(r
, ins
, inslen
) == 0) {
268 /*******************************************************************
269 Convert a string to lower case.
270 return True if any char is converted
272 This is unsafe for any string involving a UTF16 character
273 ********************************************************************/
275 bool strlower_w(smb_ucs2_t
*s
)
280 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
281 smb_ucs2_t v
= tolower_m(cp
);
283 COPY_UCS2_CHAR(s
,&v
);
291 /*******************************************************************
292 Convert a string to upper case.
293 return True if any char is converted
295 This is unsafe for any string involving a UTF16 character
296 ********************************************************************/
298 bool strupper_w(smb_ucs2_t
*s
)
302 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
303 smb_ucs2_t v
= toupper_m(cp
);
305 COPY_UCS2_CHAR(s
,&v
);
313 /*******************************************************************
314 Convert a string to "normal" form.
315 ********************************************************************/
317 void strnorm_w(smb_ucs2_t
*s
, int case_default
)
319 if (case_default
== CASE_UPPER
) {
326 int strcmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
330 while ((*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
334 return (*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
)));
335 /* warning: if *a != *b and both are not 0 we return a random
336 greater or lesser than 0 number not realted to which
340 int strncmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
345 while ((n
< len
) && (*(COPY_UCS2_CHAR(&cpb
,b
))) && (*(COPY_UCS2_CHAR(&cpa
,a
)) == cpb
)) {
350 return (len
- n
)?(*(COPY_UCS2_CHAR(&cpa
,a
)) - *(COPY_UCS2_CHAR(&cpb
,b
))):0;
353 /*******************************************************************
354 Case insensitive string comparison.
355 ********************************************************************/
357 int strcasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
)
361 while ((*COPY_UCS2_CHAR(&cpb
,b
)) && toupper_m(*(COPY_UCS2_CHAR(&cpa
,a
))) == toupper_m(cpb
)) {
365 return (tolower_m(*(COPY_UCS2_CHAR(&cpa
,a
))) - tolower_m(*(COPY_UCS2_CHAR(&cpb
,b
))));
368 /*******************************************************************
369 Case insensitive string comparison, length limited.
370 ********************************************************************/
372 int strncasecmp_w(const smb_ucs2_t
*a
, const smb_ucs2_t
*b
, size_t len
)
377 while ((n
< len
) && *COPY_UCS2_CHAR(&cpb
,b
) && (toupper_m(*(COPY_UCS2_CHAR(&cpa
,a
))) == toupper_m(cpb
))) {
382 return (len
- n
)?(tolower_m(*(COPY_UCS2_CHAR(&cpa
,a
))) - tolower_m(*(COPY_UCS2_CHAR(&cpb
,b
)))):0;
385 /*******************************************************************
387 ********************************************************************/
389 bool strequal_w(const smb_ucs2_t
*s1
, const smb_ucs2_t
*s2
)
398 return(strcasecmp_w(s1
,s2
)==0);
401 /*******************************************************************
402 Compare 2 strings up to and including the nth char.
403 ******************************************************************/
405 bool strnequal_w(const smb_ucs2_t
*s1
,const smb_ucs2_t
*s2
,size_t n
)
410 if (!s1
|| !s2
|| !n
) {
414 return(strncasecmp_w(s1
,s2
,n
)==0);
417 /*******************************************************************
419 ********************************************************************/
421 smb_ucs2_t
*strdup_w(const smb_ucs2_t
*src
)
423 return strndup_w(src
, 0);
426 /* if len == 0 then duplicate the whole string */
428 smb_ucs2_t
*strndup_w(const smb_ucs2_t
*src
, size_t len
)
435 dest
= SMB_MALLOC_ARRAY(smb_ucs2_t
, len
+ 1);
437 DEBUG(0,("strdup_w: out of memory!\n"));
441 memcpy(dest
, src
, len
* sizeof(smb_ucs2_t
));
446 /*******************************************************************
447 Copy a string with max len.
448 ********************************************************************/
450 smb_ucs2_t
*strncpy_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
, const size_t max
)
459 for (len
= 0; (*COPY_UCS2_CHAR(&cp
,(src
+len
))) && (len
< max
); len
++) {
460 cp
= *COPY_UCS2_CHAR(dest
+len
,src
+len
);
463 for ( /*nothing*/ ; len
< max
; len
++ ) {
464 cp
= *COPY_UCS2_CHAR(dest
+len
,&cp
);
470 /*******************************************************************
471 Append a string of len bytes and add a terminator.
472 ********************************************************************/
474 smb_ucs2_t
*strncat_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
, const size_t max
)
484 start
= strlen_w(dest
);
485 len
= strnlen_w(src
, max
);
487 memcpy(&dest
[start
], src
, len
*sizeof(smb_ucs2_t
));
488 z
= *COPY_UCS2_CHAR(dest
+start
+len
,&z
);
493 smb_ucs2_t
*strcat_w(smb_ucs2_t
*dest
, const smb_ucs2_t
*src
)
503 start
= strlen_w(dest
);
506 memcpy(&dest
[start
], src
, len
*sizeof(smb_ucs2_t
));
507 z
= *COPY_UCS2_CHAR(dest
+start
+len
,&z
);
513 /*******************************************************************
514 Replace any occurence of oldc with newc in unicode string.
515 ********************************************************************/
517 void string_replace_w(smb_ucs2_t
*s
, smb_ucs2_t oldc
, smb_ucs2_t newc
)
521 for(;*(COPY_UCS2_CHAR(&cp
,s
));s
++) {
523 COPY_UCS2_CHAR(s
,&newc
);
528 /*******************************************************************
530 ********************************************************************/
532 bool trim_string_w(smb_ucs2_t
*s
, const smb_ucs2_t
*front
,
533 const smb_ucs2_t
*back
)
536 size_t len
, front_len
, back_len
;
544 if (front
&& *front
) {
545 front_len
= strlen_w(front
);
546 while (len
&& strncmp_w(s
, front
, front_len
) == 0) {
547 memmove(s
, (s
+ front_len
), (len
- front_len
+ 1) * sizeof(smb_ucs2_t
));
554 back_len
= strlen_w(back
);
555 while (len
&& strncmp_w((s
+ (len
- back_len
)), back
, back_len
) == 0) {
556 s
[len
- back_len
] = 0;
566 The *_wa() functions take a combination of 7 bit ascii
567 and wide characters They are used so that you can use string
568 functions combining C string constants with ucs2 strings
570 The char* arguments must NOT be multibyte - to be completely sure
571 of this only pass string constants */
573 int strcmp_wa(const smb_ucs2_t
*a
, const char *b
)
577 while (*b
&& *(COPY_UCS2_CHAR(&cp
,a
)) == UCS2_CHAR(*b
)) {
581 return (*(COPY_UCS2_CHAR(&cp
,a
)) - UCS2_CHAR(*b
));
584 int strncmp_wa(const smb_ucs2_t
*a
, const char *b
, size_t len
)
589 while ((n
< len
) && *b
&& *(COPY_UCS2_CHAR(&cp
,a
)) == UCS2_CHAR(*b
)) {
594 return (len
- n
)?(*(COPY_UCS2_CHAR(&cp
,a
)) - UCS2_CHAR(*b
)):0;
597 smb_ucs2_t
*strpbrk_wa(const smb_ucs2_t
*s
, const char *p
)
601 while (*(COPY_UCS2_CHAR(&cp
,s
))) {
603 for (i
=0; p
[i
] && cp
!= UCS2_CHAR(p
[i
]); i
++)
606 return (smb_ucs2_t
*)s
;
613 smb_ucs2_t
*strstr_wa(const smb_ucs2_t
*s
, const char *ins
)
622 inslen
= strlen(ins
);
625 while ((r
= strchr_w(r
, UCS2_CHAR(*ins
)))) {
626 if (strncmp_wa(r
, ins
, inslen
) == 0)
634 /*************************************************************
635 ascii only toupper - saves the need for smbd to be in C locale.
636 *************************************************************/
638 int toupper_ascii(int c
)
640 smb_ucs2_t uc
= toupper_m(UCS2_CHAR(c
));
641 return UCS2_TO_CHAR(uc
);
644 /*************************************************************
645 ascii only tolower - saves the need for smbd to be in C locale.
646 *************************************************************/
648 int tolower_ascii(int c
)
650 smb_ucs2_t uc
= tolower_m(UCS2_CHAR(c
));
651 return UCS2_TO_CHAR(uc
);
654 /*************************************************************
655 ascii only isupper - saves the need for smbd to be in C locale.
656 *************************************************************/
658 int isupper_ascii(int c
)
660 return isupper_m(UCS2_CHAR(c
));
663 /*************************************************************
664 ascii only islower - saves the need for smbd to be in C locale.
665 *************************************************************/
667 int islower_ascii(int c
)
669 return islower_m(UCS2_CHAR(c
));