2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2001
6 Copyright (C) Simo Sorce 2001-2002
7 Copyright (C) Martin Pool 2003
8 Copyright (C) James Peach 2006
9 Copyright (C) Jeremy Allison 1992-2007
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "lib/param/loadparm.h"
27 #include "lib/util/smb_strtox.h"
29 static const char toupper_ascii_fast_table[128] = {
30 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
31 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
32 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
33 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
34 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
35 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
36 0x60, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
37 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f
41 * Compare 2 strings up to and including the nth char.
43 * @note The comparison is case-insensitive.
45 bool strnequal(const char *s1,const char *s2,size_t n)
52 return(strncasecmp_m(s1,s2,n)==0);
56 Convert a string to "normal" form.
59 bool strnorm(char *s, int case_default)
61 if (case_default == CASE_UPPER)
68 Skip past a string in a buffer. Buffer may not be
69 null terminated. end_ptr points to the first byte after
70 then end of the buffer.
73 char *skip_string(const char *base, size_t len, char *buf)
75 const char *end_ptr = base + len;
77 if (end_ptr < base || !base || !buf || buf >= end_ptr) {
94 Count the number of characters in a string. Normally this will
95 be the same as the number of bytes in a string for single byte strings,
96 but will be different for multibyte.
99 size_t str_charnum(const char *s)
101 size_t ret, converted_size;
102 smb_ucs2_t *tmpbuf2 = NULL;
103 if (!push_ucs2_talloc(talloc_tos(), &tmpbuf2, s, &converted_size)) {
106 ret = strlen_w(tmpbuf2);
107 TALLOC_FREE(tmpbuf2);
111 bool trim_char(char *s,char cfront,char cback)
117 /* Ignore null or empty strings. */
118 if (!s || (s[0] == '\0'))
122 while (*fp && *fp == cfront)
125 /* We ate the string. */
133 ep = fp + strlen(fp) - 1;
135 /* Attempt ascii only. Bail for mb strings. */
136 while ((ep >= fp) && (*ep == cback)) {
138 if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) {
139 /* Could be mb... bail back to tim_string. */
147 return trim_string(s, cfront ? fs : NULL, bs);
153 /* We ate the string. */
160 memmove(s, fp, ep-fp+2);
165 Check if a string is part of a list.
168 bool in_list(const char *s, const char *list, bool casesensitive)
178 frame = talloc_stackframe();
179 while (next_token_talloc(frame, &list, &tok,LIST_SEP)) {
181 if (strcmp(tok,s) == 0) {
186 if (strcasecmp_m(tok,s) == 0) {
197 Write an octal as a string.
200 char *octal_string(int i)
204 result = talloc_strdup(talloc_tos(), "-1");
207 result = talloc_asprintf(talloc_tos(), "0%o", i);
209 SMB_ASSERT(result != NULL);
215 Truncate a string at a specified length.
218 char *string_truncate(char *s, unsigned int length)
220 if (s && strlen(s) > length)
226 /***********************************************************************
227 Return the equivalent of doing strrchr 'n' times - always going
229 ***********************************************************************/
231 char *strnrchr_m(const char *s, char c, unsigned int n)
233 smb_ucs2_t *ws = NULL;
237 size_t converted_size;
239 if (!push_ucs2_talloc(talloc_tos(), &ws, s, &converted_size)) {
240 /* Too hard to try and get right. */
243 p = strnrchr_w(ws, UCS2_CHAR(c), n);
249 if (!pull_ucs2_talloc(talloc_tos(), &s2, ws, &converted_size)) {
251 /* Too hard to try and get right. */
254 ret = discard_const_p(char, (s+strlen(s2)));
260 static bool unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
263 smb_ucs2_t *buffer = NULL;
266 if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE, src, srclen,
267 (void **)(void *)&buffer, &size))
271 if (!strlower_w(buffer) && (dest == src)) {
275 ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
280 #if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
283 Convert a string to lower case.
285 _PUBLIC_ void strlower_m(char *s)
288 struct smb_iconv_handle *iconv_handle;
290 iconv_handle = get_iconv_handle();
295 size_t c_size, c_size2;
296 codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
297 c_size2 = push_codepoint_handle(iconv_handle, d, tolower_m(c));
298 if (c_size2 > c_size) {
299 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
300 c, tolower_m(c), (int)c_size, (int)c_size2));
301 smb_panic("codepoint expansion in strlower_m\n");
312 Convert a string to lower case.
315 bool strlower_m(char *s)
321 /* this is quite a common operation, so we want it to be
322 fast. We optimise for the ascii case, knowing that all our
323 supported multi-byte character sets are ascii-compatible
324 (ie. they match for the first 128 chars) */
326 while (*s && !(((unsigned char)s[0]) & 0x80)) {
327 *s = tolower_m((unsigned char)*s);
334 /* I assume that lowercased string takes the same number of bytes
335 * as source string even in UTF-8 encoding. (VIV) */
339 ret = unix_strlower(s,len,s,len);
340 /* Catch mb conversion errors that may not terminate. */
348 static bool unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
354 if (!push_ucs2_talloc(talloc_tos(), &buffer, src, &size)) {
358 if (!strupper_w(buffer) && (dest == src)) {
363 ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
368 #if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
371 Convert a string to UPPER case.
373 _PUBLIC_ void strupper_m(char *s)
376 struct smb_iconv_handle *iconv_handle;
378 iconv_handle = get_iconv_handle();
383 size_t c_size, c_size2;
384 codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
385 c_size2 = push_codepoint_handle(iconv_handle, d, toupper_m(c));
386 if (c_size2 > c_size) {
387 DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
388 c, toupper_m(c), (int)c_size, (int)c_size2));
389 smb_panic("codepoint expansion in strupper_m\n");
400 Convert a string to upper case.
403 bool strupper_m(char *s)
408 /* this is quite a common operation, so we want it to be
409 fast. We optimise for the ascii case, knowing that all our
410 supported multi-byte character sets are ascii-compatible
411 (ie. they match for the first 128 chars) */
413 while (*s && !(((unsigned char)s[0]) & 0x80)) {
414 *s = toupper_ascii_fast_table[(unsigned char)s[0]];
421 /* I assume that uppercased string takes the same number of bytes
422 * as source string even in multibyte encoding. (VIV) */
424 ret = unix_strupper(s,len,s,len);
425 /* Catch mb conversion errors that may not terminate. */
433 Just a typesafety wrapper for snprintf into a fstring.
436 int fstr_sprintf(fstring s, const char *fmt, ...)
442 ret = vsnprintf(s, FSTRING_LEN, fmt, ap);
447 /* read a SMB_BIG_UINT from a string */
448 uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr)
451 uint64_t val = (uint64_t)-1;
452 const char *p = nptr;
461 while (*p && isspace(*p))
464 sscanf(p,"%"SCNu64,&val);
466 while (*p && isdigit(*p))
474 /* Convert a size specification to a count of bytes. We accept the following
476 * bytes if there is no suffix
481 * pP whatever the ISO name for petabytes is
483 * Returns 0 if the string can't be converted.
485 uint64_t conv_str_size(const char * str)
491 if (str == NULL || *str == '\0') {
495 lval = smb_strtoull(str, &end, 10, &error, SMB_STR_STANDARD);
505 if (strwicmp(end, "K") == 0) {
507 } else if (strwicmp(end, "M") == 0) {
508 lval *= (1024ULL * 1024ULL);
509 } else if (strwicmp(end, "G") == 0) {
510 lval *= (1024ULL * 1024ULL *
512 } else if (strwicmp(end, "T") == 0) {
513 lval *= (1024ULL * 1024ULL *
515 } else if (strwicmp(end, "P") == 0) {
516 lval *= (1024ULL * 1024ULL *
526 char *talloc_asprintf_strupper_m(TALLOC_CTX *t, const char *fmt, ...)
532 ret = talloc_vasprintf(t, fmt, ap);
538 if (!strupper_m(ret)) {
545 char *talloc_asprintf_strlower_m(TALLOC_CTX *t, const char *fmt, ...)
551 ret = talloc_vasprintf(t, fmt, ap);
557 if (!strlower_m(ret)) {
565 /********************************************************************
566 Check a string for any occurrences of a specified list of invalid
568 ********************************************************************/
570 bool validate_net_name( const char *name,
571 const char *invalid_chars,
580 for ( i=0; i<max_len && name[i]; i++ ) {
581 /* fail if strchr_m() finds one of the invalid characters */
582 if ( name[i] && strchr_m( invalid_chars, name[i] ) ) {
591 /*******************************************************************
592 Add a shell escape character '\' to any character not in a known list
593 of characters. UNIX charset format.
594 *******************************************************************/
596 #define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_/ \t.,"
597 #define INSIDE_DQUOTE_LIST "$`\n\"\\"
599 char *escape_shell_string(const char *src)
601 size_t srclen = strlen(src);
602 char *ret = SMB_MALLOC_ARRAY(char, (srclen * 2) + 1);
604 bool in_s_quote = false;
605 bool in_d_quote = false;
606 bool next_escaped = false;
614 codepoint_t c = next_codepoint(src, &c_size);
616 if (c == INVALID_CODEPOINT) {
622 memcpy(dest, src, c_size);
625 next_escaped = false;
630 * Deal with backslash escaped state.
631 * This only lasts for one character.
636 next_escaped = false;
641 * Deal with single quote state. The
642 * only thing we care about is exiting
655 * Deal with double quote state. The most
656 * complex state. We must cope with \, meaning
657 * possibly escape next char (depending what it
658 * is), ", meaning exit this state, and possibly
659 * add an \ escape to any unprotected character
660 * (listed in INSIDE_DQUOTE_LIST).
666 * Next character might be escaped.
667 * We have to peek. Inside double
668 * quotes only INSIDE_DQUOTE_LIST
669 * characters are escaped by a \.
674 c = next_codepoint(&src[1], &c_size);
675 if (c == INVALID_CODEPOINT) {
681 * Don't escape the next char.
690 if (nextchar && strchr(INSIDE_DQUOTE_LIST,
699 /* Exit double quote state. */
706 * We know the character isn't \ or ",
707 * so escape it if it's any of the other
708 * possible unprotected characters.
711 if (strchr(INSIDE_DQUOTE_LIST, (int)*src)) {
719 * From here to the end of the loop we're
720 * not in the single or double quote state.
724 /* Next character must be escaped. */
731 /* Go into single quote state. */
738 /* Go into double quote state. */
744 /* Check if we need to escape the character. */
746 if (!strchr(INCLUDE_LIST, (int)*src)) {
756 * This routine improves performance for operations temporarily acting on a
757 * full path. It is equivalent to the much more expensive
759 * talloc_asprintf(talloc_tos(), "%s/%s", dir, name)
761 * This actually does make a difference in metadata-heavy workloads (i.e. the
762 * "standard" client.txt nbench run.
765 ssize_t full_path_tos(const char *dir, const char *name,
766 char *tmpbuf, size_t tmpbuf_len,
767 char **pdst, char **to_free)
769 size_t dirlen, namelen, len;
772 dirlen = strlen(dir);
773 namelen = strlen(name);
774 len = dirlen + namelen + 1;
776 if (len < tmpbuf_len) {
780 dst = talloc_array(talloc_tos(), char, len+1);
787 memcpy(dst, dir, dirlen);
789 memcpy(dst+dirlen+1, name, namelen+1);