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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "system/locale.h"
24 struct smb_iconv_convenience
*global_iconv_convenience
= NULL
;
26 static inline struct smb_iconv_convenience
*get_iconv_convenience(void)
28 if (global_iconv_convenience
== NULL
)
29 global_iconv_convenience
= smb_iconv_convenience_reinit(talloc_autofree_context(),
30 "ASCII", "UTF-8", true, NULL
);
31 return global_iconv_convenience
;
35 Case insensitive string compararison
37 _PUBLIC_
int strcasecmp_m(const char *s1
, const char *s2
)
39 codepoint_t c1
=0, c2
=0;
41 struct smb_iconv_convenience
*iconv_convenience
= get_iconv_convenience();
43 /* handle null ptr comparisons to simplify the use in qsort */
44 if (s1
== s2
) return 0;
45 if (s1
== NULL
) return -1;
46 if (s2
== NULL
) return 1;
49 c1
= next_codepoint_convenience(iconv_convenience
, s1
, &size1
);
50 c2
= next_codepoint_convenience(iconv_convenience
, s2
, &size2
);
59 if (c1
== INVALID_CODEPOINT
||
60 c2
== INVALID_CODEPOINT
) {
61 /* what else can we do?? */
62 return strcasecmp(s1
, s2
);
65 if (toupper_m(c1
) != toupper_m(c2
)) {
74 * Get the next token from a string, return False if none found.
75 * Handles double-quotes.
77 * Based on a routine by GJC@VILLAGE.COM.
78 * Extensively modified by Andrew.Tridgell@anu.edu.au
80 _PUBLIC_
bool next_token(const char **ptr
,char *buff
, const char *sep
, size_t bufsize
)
91 /* default to simple separators */
95 /* find the first non sep char */
96 while (*s
&& strchr_m(sep
,*s
))
103 /* copy over the token */
104 for (quoted
= false; len
< bufsize
&& *s
&& (quoted
|| !strchr_m(sep
,*s
)); s
++) {
113 *ptr
= (*s
) ? s
+1 : s
;
120 Case insensitive string compararison, length limited
122 _PUBLIC_
int strncasecmp_m(const char *s1
, const char *s2
, size_t n
)
124 codepoint_t c1
=0, c2
=0;
126 struct smb_iconv_convenience
*iconv_convenience
= get_iconv_convenience();
128 /* handle null ptr comparisons to simplify the use in qsort */
129 if (s1
== s2
) return 0;
130 if (s1
== NULL
) return -1;
131 if (s2
== NULL
) return 1;
133 while (*s1
&& *s2
&& n
) {
136 c1
= next_codepoint_convenience(iconv_convenience
, s1
, &size1
);
137 c2
= next_codepoint_convenience(iconv_convenience
, s2
, &size2
);
146 if (c1
== INVALID_CODEPOINT
||
147 c2
== INVALID_CODEPOINT
) {
148 /* what else can we do?? */
149 return strcasecmp(s1
, s2
);
152 if (toupper_m(c1
) != toupper_m(c2
)) {
167 * @note The comparison is case-insensitive.
169 _PUBLIC_
bool strequal_m(const char *s1
, const char *s2
)
171 return strcasecmp_m(s1
,s2
) == 0;
175 Compare 2 strings (case sensitive).
177 _PUBLIC_
bool strcsequal_m(const char *s1
,const char *s2
)
184 return strcmp(s1
,s2
) == 0;
190 NOTE: oldc and newc must be 7 bit characters
192 _PUBLIC_
void string_replace_m(char *s
, char oldc
, char newc
)
194 struct smb_iconv_convenience
*ic
= get_iconv_convenience();
197 codepoint_t c
= next_codepoint_convenience(ic
, s
, &size
);
206 Paranoid strcpy into a buffer of given length (includes terminating
207 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
208 and replaces with '_'. Deliberately does *NOT* check for multibyte
209 characters. Don't change it !
212 _PUBLIC_
char *alpha_strcpy(char *dest
, const char *src
, const char *other_safe_chars
, size_t maxlength
)
216 if (maxlength
== 0) {
217 /* can't fit any bytes at all! */
222 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
232 if (len
>= maxlength
)
235 if (!other_safe_chars
)
236 other_safe_chars
= "";
238 for(i
= 0; i
< len
; i
++) {
239 int val
= (src
[i
] & 0xff);
240 if (isupper(val
) || islower(val
) || isdigit(val
) || strchr_m(other_safe_chars
, val
))
252 * Calculate the number of units (8 or 16-bit, depending on the
253 * destination charset), that would be needed to convert the input
254 * string which is expected to be in in src_charset encoding to the
255 * destination charset (which should be a unicode charset).
257 _PUBLIC_
size_t strlen_m_ext(const char *s
, charset_t src_charset
, charset_t dst_charset
)
260 struct smb_iconv_convenience
*ic
= get_iconv_convenience();
266 while (*s
&& !(((uint8_t)*s
) & 0x80)) {
277 codepoint_t c
= next_codepoint_convenience_ext(ic
, s
, src_charset
, &c_size
);
280 switch (dst_charset
) {
292 * this only checks ranges, and does not
293 * check for invalid codepoints
297 } else if (c
< 0x800) {
299 } else if (c
< 0x1000) {
307 * non-unicode encoding:
308 * assume that each codepoint fits into
309 * one unit in the destination encoding.
318 _PUBLIC_
size_t strlen_m_ext_term(const char *s
, const charset_t src_charset
,
319 const charset_t dst_charset
)
324 return strlen_m_ext(s
, src_charset
, dst_charset
) + 1;
329 Count the number of UCS2 characters in a string. Normally this will
330 be the same as the number of bytes in a string for single byte strings,
331 but will be different for multibyte.
333 _PUBLIC_
size_t strlen_m(const char *s
)
335 return strlen_m_ext(s
, CH_UNIX
, CH_UTF16LE
);
339 Work out the number of multibyte chars in a string, including the NULL
342 _PUBLIC_
size_t strlen_m_term(const char *s
)
348 return strlen_m(s
) + 1;
352 * Weird helper routine for the winreg pipe: If nothing is around, return 0,
353 * if a string is there, include the terminator.
356 _PUBLIC_
size_t strlen_m_term_null(const char *s
)
371 Strchr and strrchr_m are a bit complex on general multi-byte strings.
373 _PUBLIC_
char *strchr_m(const char *s
, char c
)
375 struct smb_iconv_convenience
*ic
= get_iconv_convenience();
379 /* characters below 0x3F are guaranteed to not appear in
380 non-initial position in multi-byte charsets */
381 if ((c
& 0xC0) == 0) {
387 codepoint_t c2
= next_codepoint_convenience(ic
, s
, &size
);
389 return discard_const_p(char, s
);
398 * Multibyte-character version of strrchr
400 _PUBLIC_
char *strrchr_m(const char *s
, char c
)
402 struct smb_iconv_convenience
*ic
= get_iconv_convenience();
409 /* characters below 0x3F are guaranteed to not appear in
410 non-initial position in multi-byte charsets */
411 if ((c
& 0xC0) == 0) {
412 return strrchr(s
, c
);
417 codepoint_t c2
= next_codepoint_convenience(ic
, s
, &size
);
419 ret
= discard_const_p(char, s
);
428 return True if any (multi-byte) character is lower case
430 _PUBLIC_
bool strhaslower(const char *string
)
432 struct smb_iconv_convenience
*ic
= get_iconv_convenience();
438 s
= next_codepoint_convenience(ic
, string
, &c_size
);
444 return true; /* that means it has lower case chars */
452 return True if any (multi-byte) character is upper case
454 _PUBLIC_
bool strhasupper(const char *string
)
456 struct smb_iconv_convenience
*ic
= get_iconv_convenience();
462 s
= next_codepoint_convenience(ic
, string
, &c_size
);
468 return true; /* that means it has upper case chars */
476 Convert a string to lower case, allocated with talloc
478 _PUBLIC_
char *strlower_talloc(TALLOC_CTX
*ctx
, const char *src
)
482 struct smb_iconv_convenience
*iconv_convenience
= get_iconv_convenience();
488 /* this takes advantage of the fact that upper/lower can't
489 change the length of a character by more than 1 byte */
490 dest
= talloc_array(ctx
, char, 2*(strlen(src
))+1);
497 codepoint_t c
= next_codepoint_convenience(iconv_convenience
, src
, &c_size
);
502 c_size
= push_codepoint_convenience(iconv_convenience
, dest
+size
, c
);
512 /* trim it so talloc_append_string() works */
513 dest
= talloc_realloc(ctx
, dest
, char, size
+1);
515 talloc_set_name_const(dest
, dest
);
521 Convert a string to UPPER case, allocated with talloc
522 source length limited to n bytes
524 _PUBLIC_
char *strupper_talloc_n(TALLOC_CTX
*ctx
, const char *src
, size_t n
)
528 struct smb_iconv_convenience
*iconv_convenience
= get_iconv_convenience();
534 /* this takes advantage of the fact that upper/lower can't
535 change the length of a character by more than 1 byte */
536 dest
= talloc_array(ctx
, char, 2*(n
+1));
541 while (n
-- && *src
) {
543 codepoint_t c
= next_codepoint_convenience(iconv_convenience
, src
, &c_size
);
548 c_size
= push_codepoint_convenience(iconv_convenience
, dest
+size
, c
);
558 /* trim it so talloc_append_string() works */
559 dest
= talloc_realloc(ctx
, dest
, char, size
+1);
561 talloc_set_name_const(dest
, dest
);
567 Convert a string to UPPER case, allocated with talloc
569 _PUBLIC_
char *strupper_talloc(TALLOC_CTX
*ctx
, const char *src
)
571 return strupper_talloc_n(ctx
, src
, src
?strlen(src
):0);
575 talloc_strdup() a unix string to upper case.
577 _PUBLIC_
char *talloc_strdup_upper(TALLOC_CTX
*ctx
, const char *src
)
579 return strupper_talloc(ctx
, src
);
583 Convert a string to lower case.
585 _PUBLIC_
void strlower_m(char *s
)
588 struct smb_iconv_convenience
*iconv_convenience
;
590 /* this is quite a common operation, so we want it to be
591 fast. We optimise for the ascii case, knowing that all our
592 supported multi-byte character sets are ascii-compatible
593 (ie. they match for the first 128 chars) */
594 while (*s
&& !(((uint8_t)*s
) & 0x80)) {
595 *s
= tolower((uint8_t)*s
);
602 iconv_convenience
= get_iconv_convenience();
607 size_t c_size
, c_size2
;
608 codepoint_t c
= next_codepoint_convenience(iconv_convenience
, s
, &c_size
);
609 c_size2
= push_codepoint_convenience(iconv_convenience
, d
, tolower_m(c
));
610 if (c_size2
> c_size
) {
611 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
612 c
, tolower_m(c
), (int)c_size
, (int)c_size2
));
613 smb_panic("codepoint expansion in strlower_m\n");
622 Convert a string to UPPER case.
624 _PUBLIC_
void strupper_m(char *s
)
627 struct smb_iconv_convenience
*iconv_convenience
;
629 /* this is quite a common operation, so we want it to be
630 fast. We optimise for the ascii case, knowing that all our
631 supported multi-byte character sets are ascii-compatible
632 (ie. they match for the first 128 chars) */
633 while (*s
&& !(((uint8_t)*s
) & 0x80)) {
634 *s
= toupper((uint8_t)*s
);
641 iconv_convenience
= get_iconv_convenience();
646 size_t c_size
, c_size2
;
647 codepoint_t c
= next_codepoint_convenience(iconv_convenience
, s
, &c_size
);
648 c_size2
= push_codepoint_convenience(iconv_convenience
, d
, toupper_m(c
));
649 if (c_size2
> c_size
) {
650 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
651 c
, toupper_m(c
), (int)c_size
, (int)c_size2
));
652 smb_panic("codepoint expansion in strupper_m\n");
662 Find the number of 'c' chars in a string
664 _PUBLIC_
size_t count_chars_m(const char *s
, char c
)
666 struct smb_iconv_convenience
*ic
= get_iconv_convenience();
671 codepoint_t c2
= next_codepoint_convenience(ic
, s
, &size
);
672 if (c2
== c
) count
++;
681 * Copy a string from a char* unix src to a dos codepage string destination.
683 * @return the number of bytes occupied by the string in the destination.
685 * @param flags can include
687 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
688 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
691 * @param dest_len the maximum length in bytes allowed in the
692 * destination. If @p dest_len is -1 then no maximum is used.
694 static ssize_t
push_ascii(void *dest
, const char *src
, size_t dest_len
, int flags
)
699 if (flags
& STR_UPPER
) {
700 char *tmpbuf
= strupper_talloc(NULL
, src
);
701 if (tmpbuf
== NULL
) {
704 ret
= push_ascii(dest
, tmpbuf
, dest_len
, flags
& ~STR_UPPER
);
709 src_len
= strlen(src
);
711 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
))
714 return convert_string(CH_UNIX
, CH_DOS
, src
, src_len
, dest
, dest_len
, false);
718 * Copy a string from a unix char* src to an ASCII destination,
719 * allocating a buffer using talloc().
721 * @param dest always set at least to NULL
723 * @returns The number of bytes occupied by the string in the destination
724 * or -1 in case of error.
726 _PUBLIC_
bool push_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
, size_t *converted_size
)
728 size_t src_len
= strlen(src
)+1;
730 return convert_string_talloc(ctx
, CH_UNIX
, CH_DOS
, src
, src_len
, (void **)dest
, converted_size
, false);
735 * Copy a string from a dos codepage source to a unix char* destination.
737 * The resulting string in "dest" is always null terminated.
739 * @param flags can have:
741 * <dt>STR_TERMINATE</dt>
742 * <dd>STR_TERMINATE means the string in @p src
743 * is null terminated, and src_len is ignored.</dd>
746 * @param src_len is the length of the source area in bytes.
747 * @returns the number of bytes occupied by the string in @p src.
749 static ssize_t
pull_ascii(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
753 if (flags
& (STR_TERMINATE
| STR_TERMINATE_ASCII
)) {
754 if (src_len
== (size_t)-1) {
755 src_len
= strlen((const char *)src
) + 1;
757 size_t len
= strnlen((const char *)src
, src_len
);
764 ret
= convert_string(CH_DOS
, CH_UNIX
, src
, src_len
, dest
, dest_len
, false);
767 dest
[MIN(ret
, dest_len
-1)] = 0;
773 * Copy a string from a char* src to a unicode destination.
775 * @returns the number of bytes occupied by the string in the destination.
777 * @param flags can have:
780 * <dt>STR_TERMINATE <dd>means include the null termination.
781 * <dt>STR_UPPER <dd>means uppercase in the destination.
782 * <dt>STR_NOALIGN <dd>means don't do alignment.
785 * @param dest_len is the maximum length allowed in the
786 * destination. If dest_len is -1 then no maxiumum is used.
788 static ssize_t
push_ucs2(void *dest
, const char *src
, size_t dest_len
, int flags
)
791 size_t src_len
= strlen(src
);
794 if (flags
& STR_UPPER
) {
795 char *tmpbuf
= strupper_talloc(NULL
, src
);
796 if (tmpbuf
== NULL
) {
799 ret
= push_ucs2(dest
, tmpbuf
, dest_len
, flags
& ~STR_UPPER
);
804 if (flags
& STR_TERMINATE
)
807 if (ucs2_align(NULL
, dest
, flags
)) {
809 dest
= (void *)((char *)dest
+ 1);
810 if (dest_len
) dest_len
--;
814 /* ucs2 is always a multiple of 2 bytes */
817 ret
= convert_string(CH_UNIX
, CH_UTF16
, src
, src_len
, dest
, dest_len
, false);
818 if (ret
== (size_t)-1) {
829 * Copy a string from a unix char* src to a UCS2 destination,
830 * allocating a buffer using talloc().
832 * @param dest always set at least to NULL
834 * @returns The number of bytes occupied by the string in the destination
835 * or -1 in case of error.
837 _PUBLIC_
bool push_ucs2_talloc(TALLOC_CTX
*ctx
, smb_ucs2_t
**dest
, const char *src
, size_t *converted_size
)
839 size_t src_len
= strlen(src
)+1;
841 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF16
, src
, src_len
, (void **)dest
, converted_size
, false);
846 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
848 * @param dest always set at least to NULL
850 * @returns The number of bytes occupied by the string in the destination
853 _PUBLIC_
bool push_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
, size_t *converted_size
)
855 size_t src_len
= strlen(src
)+1;
857 return convert_string_talloc(ctx
, CH_UNIX
, CH_UTF8
, src
, src_len
, (void **)dest
, converted_size
, false);
861 Copy a string from a ucs2 source to a unix char* destination.
863 STR_TERMINATE means the string in src is null terminated.
864 STR_NOALIGN means don't try to align.
865 if STR_TERMINATE is set then src_len is ignored if it is -1.
866 src_len is the length of the source area in bytes
867 Return the number of bytes occupied by the string in src.
868 The resulting string in "dest" is always null terminated.
871 static size_t pull_ucs2(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
875 if (ucs2_align(NULL
, src
, flags
)) {
876 src
= (const void *)((const char *)src
+ 1);
881 if (flags
& STR_TERMINATE
) {
882 if (src_len
== (size_t)-1) {
883 src_len
= utf16_len(src
);
885 src_len
= utf16_len_n(src
, src_len
);
889 /* ucs2 is always a multiple of 2 bytes */
890 if (src_len
!= (size_t)-1)
893 ret
= convert_string(CH_UTF16
, CH_UNIX
, src
, src_len
, dest
, dest_len
, false);
895 dest
[MIN(ret
, dest_len
-1)] = 0;
901 * Copy a string from a ASCII src to a unix char * destination, allocating a buffer using talloc
903 * @param dest always set at least to NULL
905 * @returns The number of bytes occupied by the string in the destination
908 _PUBLIC_
bool pull_ascii_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
, size_t *converted_size
)
910 size_t src_len
= strlen(src
)+1;
912 return convert_string_talloc(ctx
, CH_DOS
, CH_UNIX
, src
, src_len
, (void **)dest
, converted_size
, false);
916 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
918 * @param dest always set at least to NULL
920 * @returns The number of bytes occupied by the string in the destination
923 _PUBLIC_
bool pull_ucs2_talloc(TALLOC_CTX
*ctx
, char **dest
, const smb_ucs2_t
*src
, size_t *converted_size
)
925 size_t src_len
= utf16_len(src
);
927 return convert_string_talloc(ctx
, CH_UTF16
, CH_UNIX
, src
, src_len
, (void **)dest
, converted_size
, false);
931 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
933 * @param dest always set at least to NULL
935 * @returns The number of bytes occupied by the string in the destination
938 _PUBLIC_
bool pull_utf8_talloc(TALLOC_CTX
*ctx
, char **dest
, const char *src
, size_t *converted_size
)
940 size_t src_len
= strlen(src
)+1;
942 return convert_string_talloc(ctx
, CH_UTF8
, CH_UNIX
, src
, src_len
, (void **)dest
, converted_size
, false);
946 Copy a string from a char* src to a unicode or ascii
947 dos codepage destination choosing unicode or ascii based on the
948 flags in the SMB buffer starting at base_ptr.
949 Return the number of bytes occupied by the string in the destination.
951 STR_TERMINATE means include the null termination.
952 STR_UPPER means uppercase in the destination.
953 STR_ASCII use ascii even with unicode packet.
954 STR_NOALIGN means don't do alignment.
955 dest_len is the maximum length allowed in the destination. If dest_len
956 is -1 then no maxiumum is used.
959 _PUBLIC_ ssize_t
push_string(void *dest
, const char *src
, size_t dest_len
, int flags
)
961 if (flags
& STR_ASCII
) {
962 return push_ascii(dest
, src
, dest_len
, flags
);
963 } else if (flags
& STR_UNICODE
) {
964 return push_ucs2(dest
, src
, dest_len
, flags
);
966 smb_panic("push_string requires either STR_ASCII or STR_UNICODE flag to be set");
973 Copy a string from a unicode or ascii source (depending on
974 the packet flags) to a char* destination.
976 STR_TERMINATE means the string in src is null terminated.
977 STR_UNICODE means to force as unicode.
978 STR_ASCII use ascii even with unicode packet.
979 STR_NOALIGN means don't do alignment.
980 if STR_TERMINATE is set then src_len is ignored is it is -1
981 src_len is the length of the source area in bytes.
982 Return the number of bytes occupied by the string in src.
983 The resulting string in "dest" is always null terminated.
986 _PUBLIC_ ssize_t
pull_string(char *dest
, const void *src
, size_t dest_len
, size_t src_len
, int flags
)
988 if (flags
& STR_ASCII
) {
989 return pull_ascii(dest
, src
, dest_len
, src_len
, flags
);
990 } else if (flags
& STR_UNICODE
) {
991 return pull_ucs2(dest
, src
, dest_len
, src_len
, flags
);
993 smb_panic("pull_string requires either STR_ASCII or STR_UNICODE flag to be set");
1000 * Convert string from one encoding to another, making error checking etc
1002 * @param src pointer to source string (multibyte or singlebyte)
1003 * @param srclen length of the source string in bytes
1004 * @param dest pointer to destination string (multibyte or singlebyte)
1005 * @param destlen maximal length allowed for string
1006 * @returns the number of bytes occupied in the destination
1008 _PUBLIC_
size_t convert_string(charset_t from
, charset_t to
,
1009 void const *src
, size_t srclen
,
1010 void *dest
, size_t destlen
,
1011 bool allow_badcharcnv
)
1014 if (!convert_string_convenience(get_iconv_convenience(), from
, to
,
1016 dest
, destlen
, &ret
,
1023 * Convert between character sets, allocating a new buffer using talloc for the result.
1025 * @param srclen length of source buffer.
1026 * @param dest always set at least to NULL
1027 * @param converted_size Size in bytes of the converted string
1028 * @note -1 is not accepted for srclen.
1030 * @returns boolean indication whether the conversion succeeded
1033 _PUBLIC_
bool convert_string_talloc(TALLOC_CTX
*ctx
,
1034 charset_t from
, charset_t to
,
1035 void const *src
, size_t srclen
,
1036 void *dest
, size_t *converted_size
,
1037 bool allow_badcharcnv
)
1039 return convert_string_talloc_convenience(ctx
, get_iconv_convenience(),
1040 from
, to
, src
, srclen
, dest
,
1045 _PUBLIC_ codepoint_t
next_codepoint_ext(const char *str
, charset_t src_charset
,
1048 return next_codepoint_convenience_ext(get_iconv_convenience(), str
,
1052 _PUBLIC_ codepoint_t
next_codepoint(const char *str
, size_t *size
)
1054 return next_codepoint_convenience(get_iconv_convenience(), str
, size
);
1057 _PUBLIC_ ssize_t
push_codepoint(char *str
, codepoint_t c
)
1059 return push_codepoint_convenience(get_iconv_convenience(), str
, c
);