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"
23 #include "param/param.h"
25 static inline struct smb_iconv_convenience *get_iconv_convenience(void)
27 static struct smb_iconv_convenience *ic = NULL;
29 ic = lp_iconv_convenience(global_loadparm);
34 Case insensitive string compararison
36 _PUBLIC_ int strcasecmp_m(const char *s1, const char *s2)
38 codepoint_t c1=0, c2=0;
40 struct smb_iconv_convenience *iconv_convenience = get_iconv_convenience();
42 /* handle null ptr comparisons to simplify the use in qsort */
43 if (s1 == s2) return 0;
44 if (s1 == NULL) return -1;
45 if (s2 == NULL) return 1;
48 c1 = next_codepoint(iconv_convenience, s1, &size1);
49 c2 = next_codepoint(iconv_convenience, s2, &size2);
58 if (c1 == INVALID_CODEPOINT ||
59 c2 == INVALID_CODEPOINT) {
60 /* what else can we do?? */
61 return strcasecmp(s1, s2);
64 if (toupper_m(c1) != toupper_m(c2)) {
73 * Get the next token from a string, return False if none found.
74 * Handles double-quotes.
76 * Based on a routine by GJC@VILLAGE.COM.
77 * Extensively modified by Andrew.Tridgell@anu.edu.au
79 _PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
90 /* default to simple separators */
94 /* find the first non sep char */
95 while (*s && strchr_m(sep,*s))
102 /* copy over the token */
103 for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
112 *ptr = (*s) ? s+1 : s;
119 Case insensitive string compararison, length limited
121 _PUBLIC_ int strncasecmp_m(const char *s1, const char *s2, size_t n)
123 codepoint_t c1=0, c2=0;
125 struct smb_iconv_convenience *iconv_convenience = get_iconv_convenience();
127 /* handle null ptr comparisons to simplify the use in qsort */
128 if (s1 == s2) return 0;
129 if (s1 == NULL) return -1;
130 if (s2 == NULL) return 1;
132 while (*s1 && *s2 && n) {
135 c1 = next_codepoint(iconv_convenience, s1, &size1);
136 c2 = next_codepoint(iconv_convenience, s2, &size2);
145 if (c1 == INVALID_CODEPOINT ||
146 c2 == INVALID_CODEPOINT) {
147 /* what else can we do?? */
148 return strcasecmp(s1, s2);
151 if (toupper_m(c1) != toupper_m(c2)) {
166 * @note The comparison is case-insensitive.
168 _PUBLIC_ bool strequal_m(const char *s1, const char *s2)
170 return strcasecmp_m(s1,s2) == 0;
174 Compare 2 strings (case sensitive).
176 _PUBLIC_ bool strcsequal_m(const char *s1,const char *s2)
183 return strcmp(s1,s2) == 0;
189 NOTE: oldc and newc must be 7 bit characters
191 _PUBLIC_ void string_replace_m(char *s, char oldc, char newc)
195 codepoint_t c = next_codepoint(get_iconv_convenience(), s, &size);
204 Paranoid strcpy into a buffer of given length (includes terminating
205 zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
206 and replaces with '_'. Deliberately does *NOT* check for multibyte
207 characters. Don't change it !
210 _PUBLIC_ char *alpha_strcpy(char *dest, const char *src, const char *other_safe_chars, size_t maxlength)
214 if (maxlength == 0) {
215 /* can't fit any bytes at all! */
220 DEBUG(0,("ERROR: NULL dest in alpha_strcpy\n"));
230 if (len >= maxlength)
233 if (!other_safe_chars)
234 other_safe_chars = "";
236 for(i = 0; i < len; i++) {
237 int val = (src[i] & 0xff);
238 if (isupper(val) || islower(val) || isdigit(val) || strchr_m(other_safe_chars, val))
250 Count the number of UCS2 characters in a string. Normally this will
251 be the same as the number of bytes in a string for single byte strings,
252 but will be different for multibyte.
254 _PUBLIC_ size_t strlen_m(const char *s)
262 while (*s && !(((uint8_t)*s) & 0x80)) {
273 codepoint_t c = next_codepoint(get_iconv_convenience(), s, &c_size);
286 Work out the number of multibyte chars in a string, including the NULL
289 _PUBLIC_ size_t strlen_m_term(const char *s)
295 return strlen_m(s) + 1;
299 Strchr and strrchr_m are a bit complex on general multi-byte strings.
301 _PUBLIC_ char *strchr_m(const char *s, char c)
306 /* characters below 0x3F are guaranteed to not appear in
307 non-initial position in multi-byte charsets */
308 if ((c & 0xC0) == 0) {
314 codepoint_t c2 = next_codepoint(get_iconv_convenience(), s, &size);
316 return discard_const_p(char, s);
325 * Multibyte-character version of strrchr
327 _PUBLIC_ char *strrchr_m(const char *s, char c)
335 /* characters below 0x3F are guaranteed to not appear in
336 non-initial position in multi-byte charsets */
337 if ((c & 0xC0) == 0) {
338 return strrchr(s, c);
343 codepoint_t c2 = next_codepoint(get_iconv_convenience(), s, &size);
345 ret = discard_const_p(char, s);
354 return True if any (multi-byte) character is lower case
356 _PUBLIC_ bool strhaslower(const char *string)
363 s = next_codepoint(get_iconv_convenience(), string, &c_size);
369 return true; /* that means it has lower case chars */
377 return True if any (multi-byte) character is upper case
379 _PUBLIC_ bool strhasupper(const char *string)
386 s = next_codepoint(get_iconv_convenience(), string, &c_size);
392 return true; /* that means it has upper case chars */
400 Convert a string to lower case, allocated with talloc
402 _PUBLIC_ char *strlower_talloc(TALLOC_CTX *ctx, const char *src)
406 struct smb_iconv_convenience *iconv_convenience = get_iconv_convenience();
408 /* this takes advantage of the fact that upper/lower can't
409 change the length of a character by more than 1 byte */
410 dest = talloc_array(ctx, char, 2*(strlen(src))+1);
417 codepoint_t c = next_codepoint(iconv_convenience, src, &c_size);
422 c_size = push_codepoint(iconv_convenience, dest+size, c);
432 /* trim it so talloc_append_string() works */
433 dest = talloc_realloc(ctx, dest, char, size+1);
435 talloc_set_name_const(dest, dest);
441 Convert a string to UPPER case, allocated with talloc
442 source length limited to n bytes
444 _PUBLIC_ char *strupper_talloc_n(TALLOC_CTX *ctx, const char *src, size_t n)
448 struct smb_iconv_convenience *iconv_convenience = get_iconv_convenience();
454 /* this takes advantage of the fact that upper/lower can't
455 change the length of a character by more than 1 byte */
456 dest = talloc_array(ctx, char, 2*(n+1));
461 while (*src && n--) {
463 codepoint_t c = next_codepoint(iconv_convenience, src, &c_size);
468 c_size = push_codepoint(iconv_convenience, dest+size, c);
478 /* trim it so talloc_append_string() works */
479 dest = talloc_realloc(ctx, dest, char, size+1);
481 talloc_set_name_const(dest, dest);
487 Convert a string to UPPER case, allocated with talloc
489 _PUBLIC_ char *strupper_talloc(TALLOC_CTX *ctx, const char *src)
491 return strupper_talloc_n(ctx, src, src?strlen(src):0);
495 talloc_strdup() a unix string to upper case.
497 _PUBLIC_ char *talloc_strdup_upper(TALLOC_CTX *ctx, const char *src)
499 return strupper_talloc(ctx, src);
503 Convert a string to lower case.
505 _PUBLIC_ void strlower_m(char *s)
508 struct smb_iconv_convenience *iconv_convenience;
510 /* this is quite a common operation, so we want it to be
511 fast. We optimise for the ascii case, knowing that all our
512 supported multi-byte character sets are ascii-compatible
513 (ie. they match for the first 128 chars) */
514 while (*s && !(((uint8_t)*s) & 0x80)) {
515 *s = tolower((uint8_t)*s);
522 iconv_convenience = get_iconv_convenience();
527 size_t c_size, c_size2;
528 codepoint_t c = next_codepoint(iconv_convenience, s, &c_size);
529 c_size2 = push_codepoint(iconv_convenience, d, tolower_m(c));
530 if (c_size2 > c_size) {
531 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
532 c, tolower_m(c), (int)c_size, (int)c_size2));
533 smb_panic("codepoint expansion in strlower_m\n");
542 Convert a string to UPPER case.
544 _PUBLIC_ void strupper_m(char *s)
547 struct smb_iconv_convenience *iconv_convenience;
549 /* this is quite a common operation, so we want it to be
550 fast. We optimise for the ascii case, knowing that all our
551 supported multi-byte character sets are ascii-compatible
552 (ie. they match for the first 128 chars) */
553 while (*s && !(((uint8_t)*s) & 0x80)) {
554 *s = toupper((uint8_t)*s);
561 iconv_convenience = get_iconv_convenience();
566 size_t c_size, c_size2;
567 codepoint_t c = next_codepoint(iconv_convenience, s, &c_size);
568 c_size2 = push_codepoint(iconv_convenience, d, toupper_m(c));
569 if (c_size2 > c_size) {
570 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
571 c, toupper_m(c), (int)c_size, (int)c_size2));
572 smb_panic("codepoint expansion in strupper_m\n");
582 Find the number of 'c' chars in a string
584 _PUBLIC_ size_t count_chars_m(const char *s, char c)
590 codepoint_t c2 = next_codepoint(get_iconv_convenience(), s, &size);
591 if (c2 == c) count++;
600 * Copy a string from a char* unix src to a dos codepage string destination.
602 * @return the number of bytes occupied by the string in the destination.
604 * @param flags can include
606 * <dt>STR_TERMINATE</dt> <dd>means include the null termination</dd>
607 * <dt>STR_UPPER</dt> <dd>means uppercase in the destination</dd>
610 * @param dest_len the maximum length in bytes allowed in the
611 * destination. If @p dest_len is -1 then no maximum is used.
613 static ssize_t push_ascii(void *dest, const char *src, size_t dest_len, int flags)
617 struct smb_iconv_convenience *ic = get_iconv_convenience();
619 if (flags & STR_UPPER) {
620 char *tmpbuf = strupper_talloc(NULL, src);
621 if (tmpbuf == NULL) {
624 ret = push_ascii(dest, tmpbuf, dest_len, flags & ~STR_UPPER);
629 src_len = strlen(src);
631 if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII))
634 return convert_string(ic, CH_UNIX, CH_DOS, src, src_len, dest, dest_len);
638 * Copy a string from a unix char* src to an ASCII destination,
639 * allocating a buffer using talloc().
641 * @param dest always set at least to NULL
643 * @returns The number of bytes occupied by the string in the destination
644 * or -1 in case of error.
646 _PUBLIC_ ssize_t push_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
648 struct smb_iconv_convenience *ic = get_iconv_convenience();
649 size_t src_len = strlen(src)+1;
651 return convert_string_talloc(ctx, ic, CH_UNIX, CH_DOS, src, src_len, (void **)dest);
656 * Copy a string from a dos codepage source to a unix char* destination.
658 * The resulting string in "dest" is always null terminated.
660 * @param flags can have:
662 * <dt>STR_TERMINATE</dt>
663 * <dd>STR_TERMINATE means the string in @p src
664 * is null terminated, and src_len is ignored.</dd>
667 * @param src_len is the length of the source area in bytes.
668 * @returns the number of bytes occupied by the string in @p src.
670 static ssize_t pull_ascii(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
672 struct smb_iconv_convenience *ic = get_iconv_convenience();
675 if (flags & (STR_TERMINATE | STR_TERMINATE_ASCII)) {
676 if (src_len == (size_t)-1) {
677 src_len = strlen((const char *)src) + 1;
679 size_t len = strnlen((const char *)src, src_len);
686 ret = convert_string(ic, CH_DOS, CH_UNIX, src, src_len, dest, dest_len);
689 dest[MIN(ret, dest_len-1)] = 0;
695 * Copy a string from a char* src to a unicode destination.
697 * @returns the number of bytes occupied by the string in the destination.
699 * @param flags can have:
702 * <dt>STR_TERMINATE <dd>means include the null termination.
703 * <dt>STR_UPPER <dd>means uppercase in the destination.
704 * <dt>STR_NOALIGN <dd>means don't do alignment.
707 * @param dest_len is the maximum length allowed in the
708 * destination. If dest_len is -1 then no maxiumum is used.
710 static ssize_t push_ucs2(void *dest, const char *src, size_t dest_len, int flags)
712 struct smb_iconv_convenience *ic = get_iconv_convenience();
714 size_t src_len = strlen(src);
717 if (flags & STR_UPPER) {
718 char *tmpbuf = strupper_talloc(NULL, src);
719 if (tmpbuf == NULL) {
722 ret = push_ucs2(dest, tmpbuf, dest_len, flags & ~STR_UPPER);
727 if (flags & STR_TERMINATE)
730 if (ucs2_align(NULL, dest, flags)) {
732 dest = (void *)((char *)dest + 1);
733 if (dest_len) dest_len--;
737 /* ucs2 is always a multiple of 2 bytes */
740 ret = convert_string(ic, CH_UNIX, CH_UTF16, src, src_len, dest, dest_len);
741 if (ret == (size_t)-1) {
752 * Copy a string from a unix char* src to a UCS2 destination,
753 * allocating a buffer using talloc().
755 * @param dest always set at least to NULL
757 * @returns The number of bytes occupied by the string in the destination
758 * or -1 in case of error.
760 _PUBLIC_ ssize_t push_ucs2_talloc(TALLOC_CTX *ctx, void **dest, const char *src)
762 struct smb_iconv_convenience *ic = get_iconv_convenience();
763 size_t src_len = strlen(src)+1;
765 return convert_string_talloc(ctx, ic, CH_UNIX, CH_UTF16, src, src_len, dest);
770 * Copy a string from a unix char* src to a UTF-8 destination, allocating a buffer using talloc
772 * @param dest always set at least to NULL
774 * @returns The number of bytes occupied by the string in the destination
777 _PUBLIC_ ssize_t push_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
779 struct smb_iconv_convenience *ic = get_iconv_convenience();
780 size_t src_len = strlen(src)+1;
782 return convert_string_talloc(ctx, ic, CH_UNIX, CH_UTF8, src, src_len, (void **)dest);
786 Copy a string from a ucs2 source to a unix char* destination.
788 STR_TERMINATE means the string in src is null terminated.
789 STR_NOALIGN means don't try to align.
790 if STR_TERMINATE is set then src_len is ignored if it is -1.
791 src_len is the length of the source area in bytes
792 Return the number of bytes occupied by the string in src.
793 The resulting string in "dest" is always null terminated.
796 static size_t pull_ucs2(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
798 struct smb_iconv_convenience *ic = get_iconv_convenience();
801 if (ucs2_align(NULL, src, flags)) {
802 src = (const void *)((const char *)src + 1);
807 if (flags & STR_TERMINATE) {
808 if (src_len == (size_t)-1) {
809 src_len = utf16_len(src);
811 src_len = utf16_len_n(src, src_len);
815 /* ucs2 is always a multiple of 2 bytes */
816 if (src_len != (size_t)-1)
819 ret = convert_string(ic, CH_UTF16, CH_UNIX, src, src_len, dest, dest_len);
821 dest[MIN(ret, dest_len-1)] = 0;
827 * Copy a string from a ASCII src to a unix char * destination, allocating a buffer using talloc
829 * @param dest always set at least to NULL
831 * @returns The number of bytes occupied by the string in the destination
834 _PUBLIC_ ssize_t pull_ascii_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
836 struct smb_iconv_convenience *ic = get_iconv_convenience();
837 size_t src_len = strlen(src)+1;
839 return convert_string_talloc(ctx, ic, CH_DOS, CH_UNIX, src, src_len, (void **)dest);
843 * Copy a string from a UCS2 src to a unix char * destination, allocating a buffer using talloc
845 * @param dest always set at least to NULL
847 * @returns The number of bytes occupied by the string in the destination
850 _PUBLIC_ ssize_t pull_ucs2_talloc(TALLOC_CTX *ctx, char **dest, const void *src)
852 struct smb_iconv_convenience *ic = get_iconv_convenience();
853 size_t src_len = utf16_len(src);
855 return convert_string_talloc(ctx, ic, CH_UTF16, CH_UNIX, src, src_len, (void **)dest);
859 * Copy a string from a UTF-8 src to a unix char * destination, allocating a buffer using talloc
861 * @param dest always set at least to NULL
863 * @returns The number of bytes occupied by the string in the destination
866 _PUBLIC_ ssize_t pull_utf8_talloc(TALLOC_CTX *ctx, char **dest, const char *src)
868 struct smb_iconv_convenience *ic = get_iconv_convenience();
869 size_t src_len = strlen(src)+1;
871 return convert_string_talloc(ctx, ic, CH_UTF8, CH_UNIX, src, src_len, (void **)dest);
875 Copy a string from a char* src to a unicode or ascii
876 dos codepage destination choosing unicode or ascii based on the
877 flags in the SMB buffer starting at base_ptr.
878 Return the number of bytes occupied by the string in the destination.
880 STR_TERMINATE means include the null termination.
881 STR_UPPER means uppercase in the destination.
882 STR_ASCII use ascii even with unicode packet.
883 STR_NOALIGN means don't do alignment.
884 dest_len is the maximum length allowed in the destination. If dest_len
885 is -1 then no maxiumum is used.
888 _PUBLIC_ ssize_t push_string(void *dest, const char *src, size_t dest_len, int flags)
890 if (flags & STR_ASCII) {
891 return push_ascii(dest, src, dest_len, flags);
892 } else if (flags & STR_UNICODE) {
893 return push_ucs2(dest, src, dest_len, flags);
895 smb_panic("push_string requires either STR_ASCII or STR_UNICODE flag to be set");
902 Copy a string from a unicode or ascii source (depending on
903 the packet flags) to a char* destination.
905 STR_TERMINATE means the string in src is null terminated.
906 STR_UNICODE means to force as unicode.
907 STR_ASCII use ascii even with unicode packet.
908 STR_NOALIGN means don't do alignment.
909 if STR_TERMINATE is set then src_len is ignored is it is -1
910 src_len is the length of the source area in bytes.
911 Return the number of bytes occupied by the string in src.
912 The resulting string in "dest" is always null terminated.
915 _PUBLIC_ ssize_t pull_string(char *dest, const void *src, size_t dest_len, size_t src_len, int flags)
917 if (flags & STR_ASCII) {
918 return pull_ascii(dest, src, dest_len, src_len, flags);
919 } else if (flags & STR_UNICODE) {
920 return pull_ucs2(dest, src, dest_len, src_len, flags);
922 smb_panic("pull_string requires either STR_ASCII or STR_UNICODE flag to be set");