Coverage Report

Created: 2025-12-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/source3/lib/util_str.c
Line
Count
Source
1
/*
2
   Unix SMB/CIFS implementation.
3
   Samba utility functions
4
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
10
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.
15
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.
20
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/>.
23
*/
24
25
#include "includes.h"
26
#include "lib/param/loadparm.h"
27
#include "lib/util/smb_strtox.h"
28
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
38
};
39
40
/**
41
 * Compare 2 strings up to and including the nth char.
42
 *
43
 * @note The comparison is case-insensitive.
44
 **/
45
bool strnequal(const char *s1,const char *s2,size_t n)
46
0
{
47
0
  if (s1 == s2)
48
0
    return(true);
49
0
  if (!s1 || !s2 || !n)
50
0
    return(false);
51
52
0
  return(strncasecmp_m(s1,s2,n)==0);
53
0
}
54
55
/**
56
 Skip past a string in a buffer. Buffer may not be
57
 null terminated. end_ptr points to the first byte after
58
 then end of the buffer.
59
**/
60
61
char *skip_string(const char *base, size_t len, char *buf)
62
0
{
63
0
  const char *end_ptr = base + len;
64
65
0
  if (end_ptr < base || !base || !buf || buf >= end_ptr) {
66
0
    return NULL;
67
0
  }
68
69
  /* Skip the string */
70
0
  while (*buf) {
71
0
    buf++;
72
0
    if (buf >= end_ptr) {
73
0
      return NULL;
74
0
    }
75
0
  }
76
  /* Skip the '\0' */
77
0
  buf++;
78
0
  return buf;
79
0
}
80
81
/**
82
 Count the number of characters in a string. Normally this will
83
 be the same as the number of bytes in a string for single byte strings,
84
 but will be different for multibyte.
85
**/
86
87
size_t str_charnum(const char *s)
88
0
{
89
0
  size_t ret, converted_size;
90
0
  smb_ucs2_t *tmpbuf2 = NULL;
91
0
  if (!push_ucs2_talloc(talloc_tos(), &tmpbuf2, s, &converted_size)) {
92
0
    return 0;
93
0
  }
94
0
  ret = strlen_w(tmpbuf2);
95
0
  TALLOC_FREE(tmpbuf2);
96
0
  return ret;
97
0
}
98
99
bool trim_char(char *s,char cfront,char cback)
100
51
{
101
51
  bool ret = false;
102
51
  char *ep;
103
51
  char *fp = s;
104
105
  /* Ignore null or empty strings. */
106
51
  if (!s || (s[0] == '\0'))
107
1
    return false;
108
109
50
  if (cfront) {
110
0
    while (*fp && *fp == cfront)
111
0
      fp++;
112
0
    if (!*fp) {
113
      /* We ate the string. */
114
0
      s[0] = '\0';
115
0
      return true;
116
0
    }
117
0
    if (fp != s)
118
0
      ret = true;
119
0
  }
120
121
50
  ep = fp + strlen(fp) - 1;
122
50
  if (cback) {
123
    /* Attempt ascii only. Bail for mb strings. */
124
210
    while ((ep >= fp) && (*ep == cback)) {
125
175
      ret = true;
126
175
      if ((ep > fp) && (((unsigned char)ep[-1]) & 0x80)) {
127
        /* Could be mb... bail back to trim_string. */
128
15
        char fs[2], bs[2];
129
15
        if (cfront) {
130
0
          fs[0] = cfront;
131
0
          fs[1] = '\0';
132
0
        }
133
15
        bs[0] = cback;
134
15
        bs[1] = '\0';
135
15
        return trim_string(s, cfront ? fs : NULL, bs);
136
160
      } else {
137
160
        ep--;
138
160
      }
139
175
    }
140
35
    if (ep < fp) {
141
      /* We ate the string. */
142
5
      s[0] = '\0';
143
5
      return true;
144
5
    }
145
35
  }
146
147
30
  ep[1] = '\0';
148
30
  memmove(s, fp, ep-fp+2);
149
30
  return ret;
150
50
}
151
152
/**
153
 Check if a string is part of a list.
154
**/
155
156
bool in_list(const char *s, const char *list, bool casesensitive)
157
0
{
158
0
  char *tok = NULL;
159
0
  bool ret = false;
160
0
  TALLOC_CTX *frame;
161
162
0
  if (!list) {
163
0
    return false;
164
0
  }
165
166
0
  frame = talloc_stackframe();
167
0
  while (next_token_talloc(frame, &list, &tok,LIST_SEP)) {
168
0
    if (casesensitive) {
169
0
      if (strcmp(tok,s) == 0) {
170
0
        ret = true;
171
0
        break;
172
0
      }
173
0
    } else {
174
0
      if (strcasecmp_m(tok,s) == 0) {
175
0
        ret = true;
176
0
        break;
177
0
      }
178
0
    }
179
0
  }
180
0
  TALLOC_FREE(frame);
181
0
  return ret;
182
0
}
183
184
/**
185
 Truncate a string at a specified length.
186
**/
187
188
char *string_truncate(char *s, unsigned int length)
189
0
{
190
0
  if (s && strlen(s) > length)
191
0
    s[length] = 0;
192
0
  return s;
193
0
}
194
195
static bool unix_strlower(const char *src, size_t srclen, char *dest, size_t destlen)
196
1.11k
{
197
1.11k
  size_t size;
198
1.11k
  smb_ucs2_t *buffer = NULL;
199
1.11k
  bool ret;
200
201
1.11k
  if (!convert_string_talloc(talloc_tos(), CH_UNIX, CH_UTF16LE, src, srclen,
202
1.11k
           (void **)(void *)&buffer, &size))
203
596
  {
204
596
    return false;
205
596
  }
206
518
  if (!strlower_w(buffer) && (dest == src)) {
207
159
    TALLOC_FREE(buffer);
208
159
    return true;
209
159
  }
210
359
  ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
211
359
  TALLOC_FREE(buffer);
212
359
  return ret;
213
518
}
214
215
#if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
216
217
/**
218
 Convert a string to lower case.
219
**/
220
_PUBLIC_ void strlower_m(char *s)
221
{
222
  char *d;
223
  struct smb_iconv_handle *iconv_handle;
224
225
  iconv_handle = get_iconv_handle();
226
227
  d = s;
228
229
  while (*s) {
230
    size_t c_size, c_size2;
231
    codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
232
    c_size2 = push_codepoint_handle(iconv_handle, d, tolower_m(c));
233
    if (c_size2 > c_size) {
234
      DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strlower_m\n",
235
         c, tolower_m(c), (int)c_size, (int)c_size2));
236
      smb_panic("codepoint expansion in strlower_m\n");
237
    }
238
    s += c_size;
239
    d += c_size2;
240
  }
241
  *d = 0;
242
}
243
244
#endif
245
246
/**
247
 Convert a string to lower case.
248
**/
249
250
bool strlower_m(char *s)
251
1.47k
{
252
1.47k
  size_t len;
253
1.47k
  int errno_save;
254
1.47k
  bool ret = false;
255
256
  /* this is quite a common operation, so we want it to be
257
     fast. We optimise for the ascii case, knowing that all our
258
     supported multi-byte character sets are ascii-compatible
259
     (ie. they match for the first 128 chars) */
260
261
1.10M
  while (*s && !(((unsigned char)s[0]) & 0x80)) {
262
1.10M
    *s = tolower_m((unsigned char)*s);
263
1.10M
    s++;
264
1.10M
  }
265
266
1.47k
  if (!*s)
267
357
    return true;
268
269
  /* I assume that lowercased string takes the same number of bytes
270
   * as source string even in UTF-8 encoding. (VIV) */
271
1.11k
  len = strlen(s) + 1;
272
1.11k
  errno_save = errno;
273
1.11k
  errno = 0;
274
1.11k
  ret = unix_strlower(s,len,s,len);
275
  /* Catch mb conversion errors that may not terminate. */
276
1.11k
  if (errno) {
277
596
    s[len-1] = '\0';
278
596
  }
279
1.11k
  errno = errno_save;
280
1.11k
  return ret;
281
1.47k
}
282
283
static bool unix_strupper(const char *src, size_t srclen, char *dest, size_t destlen)
284
0
{
285
0
  size_t size;
286
0
  smb_ucs2_t *buffer;
287
0
  bool ret;
288
289
0
  if (!push_ucs2_talloc(talloc_tos(), &buffer, src, &size)) {
290
0
    return false;
291
0
  }
292
293
0
  if (!strupper_w(buffer) && (dest == src)) {
294
0
    TALLOC_FREE(buffer);
295
0
    return true;
296
0
  }
297
298
0
  ret = convert_string(CH_UTF16LE, CH_UNIX, buffer, size, dest, destlen, &size);
299
0
  TALLOC_FREE(buffer);
300
0
  return ret;
301
0
}
302
303
#if 0 /* Alternate function that avoid talloc calls for ASCII and non ASCII */
304
305
/**
306
 Convert a string to UPPER case.
307
**/
308
_PUBLIC_ void strupper_m(char *s)
309
{
310
  char *d;
311
  struct smb_iconv_handle *iconv_handle;
312
313
  iconv_handle = get_iconv_handle();
314
315
  d = s;
316
317
  while (*s) {
318
    size_t c_size, c_size2;
319
    codepoint_t c = next_codepoint_handle(iconv_handle, s, &c_size);
320
    c_size2 = push_codepoint_handle(iconv_handle, d, toupper_m(c));
321
    if (c_size2 > c_size) {
322
      DEBUG(0,("FATAL: codepoint 0x%x (0x%x) expanded from %d to %d bytes in strupper_m\n",
323
         c, toupper_m(c), (int)c_size, (int)c_size2));
324
      smb_panic("codepoint expansion in strupper_m\n");
325
    }
326
    s += c_size;
327
    d += c_size2;
328
  }
329
  *d = 0;
330
}
331
332
#endif
333
334
/**
335
 Convert a string to upper case.
336
**/
337
338
bool strupper_m(char *s)
339
0
{
340
0
  size_t len;
341
0
  bool ret = false;
342
343
  /* this is quite a common operation, so we want it to be
344
     fast. We optimise for the ascii case, knowing that all our
345
     supported multi-byte character sets are ascii-compatible
346
     (ie. they match for the first 128 chars) */
347
348
0
  while (*s && !(((unsigned char)s[0]) & 0x80)) {
349
0
    *s = toupper_ascii_fast_table[(unsigned char)s[0]];
350
0
    s++;
351
0
  }
352
353
0
  if (!*s)
354
0
    return true;
355
356
  /* I assume that uppercased string takes the same number of bytes
357
   * as source string even in multibyte encoding. (VIV) */
358
0
  len = strlen(s) + 1;
359
0
  ret = unix_strupper(s,len,s,len);
360
  /* Catch mb conversion errors that may not terminate. */
361
0
  if (!ret) {
362
0
    s[len-1] = '\0';
363
0
  }
364
0
  return ret;
365
0
}
366
367
/**
368
 Just a typesafety wrapper for snprintf into a fstring.
369
**/
370
371
int fstr_sprintf(fstring s, const char *fmt, ...)
372
0
{
373
0
  va_list ap;
374
0
  int ret;
375
376
0
  va_start(ap, fmt);
377
0
  ret = vsnprintf(s, FSTRING_LEN, fmt, ap);
378
0
  va_end(ap);
379
0
  return ret;
380
0
}
381
382
/* read a SMB_BIG_UINT from a string */
383
uint64_t STR_TO_SMB_BIG_UINT(const char *nptr, const char **entptr)
384
0
{
385
386
0
  uint64_t val = (uint64_t)-1;
387
0
  const char *p = nptr;
388
389
0
  if (!p) {
390
0
    if (entptr) {
391
0
      *entptr = p;
392
0
    }
393
0
    return val;
394
0
  }
395
396
0
  while (*p && isspace(*p))
397
0
    p++;
398
399
0
  sscanf(p,"%"SCNu64,&val);
400
0
  if (entptr) {
401
0
    while (*p && isdigit(*p))
402
0
      p++;
403
0
    *entptr = p;
404
0
  }
405
406
0
  return val;
407
0
}
408
409
/* Convert a size specification to a count of bytes. We accept the following
410
 * suffixes:
411
 *      bytes if there is no suffix
412
 *  kK  kibibytes
413
 *  mM  mebibytes
414
 *  gG  gibibytes
415
 *  tT  tibibytes
416
 *  pP  whatever the ISO name for petabytes is
417
 *
418
 *  Returns 0 if the string can't be converted.
419
 */
420
uint64_t conv_str_size(const char * str)
421
0
{
422
0
        uint64_t lval;
423
0
        char *end;
424
0
  int error = 0;
425
426
0
        if (str == NULL || *str == '\0') {
427
0
                return 0;
428
0
        }
429
430
0
  lval = smb_strtoull(str, &end, 10, &error, SMB_STR_STANDARD);
431
432
0
        if (error != 0) {
433
0
                return 0;
434
0
        }
435
436
0
  if (*end == '\0') {
437
0
    return lval;
438
0
  }
439
440
0
  if (strwicmp(end, "K") == 0) {
441
0
    lval *= 1024ULL;
442
0
  } else if (strwicmp(end, "M") == 0) {
443
0
    lval *= (1024ULL * 1024ULL);
444
0
  } else if (strwicmp(end, "G") == 0) {
445
0
    lval *= (1024ULL * 1024ULL *
446
0
       1024ULL);
447
0
  } else if (strwicmp(end, "T") == 0) {
448
0
    lval *= (1024ULL * 1024ULL *
449
0
       1024ULL * 1024ULL);
450
0
  } else if (strwicmp(end, "P") == 0) {
451
0
    lval *= (1024ULL * 1024ULL *
452
0
       1024ULL * 1024ULL *
453
0
       1024ULL);
454
0
  } else {
455
0
    return 0;
456
0
  }
457
458
0
  return lval;
459
0
}
460
461
char *talloc_asprintf_strupper_m(TALLOC_CTX *t, const char *fmt, ...)
462
0
{
463
0
  va_list ap;
464
0
  char *ret;
465
466
0
  va_start(ap, fmt);
467
0
  ret = talloc_vasprintf(t, fmt, ap);
468
0
  va_end(ap);
469
470
0
  if (ret == NULL) {
471
0
    return NULL;
472
0
  }
473
0
  if (!strupper_m(ret)) {
474
0
    TALLOC_FREE(ret);
475
0
    return NULL;
476
0
  }
477
0
  return ret;
478
0
}
479
480
char *talloc_asprintf_strlower_m(TALLOC_CTX *t, const char *fmt, ...)
481
0
{
482
0
  va_list ap;
483
0
  char *ret;
484
485
0
  va_start(ap, fmt);
486
0
  ret = talloc_vasprintf(t, fmt, ap);
487
0
  va_end(ap);
488
489
0
  if (ret == NULL) {
490
0
    return NULL;
491
0
  }
492
0
  if (!strlower_m(ret)) {
493
0
    TALLOC_FREE(ret);
494
0
    return NULL;
495
0
  }
496
0
  return ret;
497
0
}
498
499
500
/********************************************************************
501
 Check a string for any occurrences of a specified list of invalid
502
 characters.
503
********************************************************************/
504
505
bool validate_net_name( const char *name,
506
    const char *invalid_chars,
507
    int max_len)
508
0
{
509
0
  int i;
510
511
0
  if (!name) {
512
0
    return false;
513
0
  }
514
515
0
  for ( i=0; i<max_len && name[i]; i++ ) {
516
    /* fail if strchr_m() finds one of the invalid characters */
517
0
    if ( name[i] && strchr_m( invalid_chars, name[i] ) ) {
518
0
      return false;
519
0
    }
520
0
  }
521
522
0
  return true;
523
0
}
524
525
526
/*******************************************************************
527
 Add a shell escape character '\' to any character not in a known list
528
 of characters. UNIX charset format.
529
*******************************************************************/
530
531
0
#define INCLUDE_LIST "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_/ \t.,"
532
0
#define INSIDE_DQUOTE_LIST "$`\n\"\\"
533
534
char *escape_shell_string(const char *src)
535
0
{
536
0
  size_t srclen = strlen(src);
537
0
  char *ret = SMB_MALLOC_ARRAY(char, (srclen * 2) + 1);
538
0
  char *dest = ret;
539
0
  bool in_s_quote = false;
540
0
  bool in_d_quote = false;
541
0
  bool next_escaped = false;
542
543
0
  if (!ret) {
544
0
    return NULL;
545
0
  }
546
547
0
  while (*src) {
548
0
    size_t c_size;
549
0
    codepoint_t c = next_codepoint(src, &c_size);
550
551
0
    if (c == INVALID_CODEPOINT) {
552
0
      SAFE_FREE(ret);
553
0
      return NULL;
554
0
    }
555
556
0
    if (c_size > 1) {
557
0
      memcpy(dest, src, c_size);
558
0
      src += c_size;
559
0
      dest += c_size;
560
0
      next_escaped = false;
561
0
      continue;
562
0
    }
563
564
    /*
565
     * Deal with backslash escaped state.
566
     * This only lasts for one character.
567
     */
568
569
0
    if (next_escaped) {
570
0
      *dest++ = *src++;
571
0
      next_escaped = false;
572
0
      continue;
573
0
    }
574
575
    /*
576
     * Deal with single quote state. The
577
     * only thing we care about is exiting
578
     * this state.
579
     */
580
581
0
    if (in_s_quote) {
582
0
      if (*src == '\'') {
583
0
        in_s_quote = false;
584
0
      }
585
0
      *dest++ = *src++;
586
0
      continue;
587
0
    }
588
589
    /*
590
     * Deal with double quote state. The most
591
     * complex state. We must cope with \, meaning
592
     * possibly escape next char (depending what it
593
     * is), ", meaning exit this state, and possibly
594
     * add an \ escape to any unprotected character
595
     * (listed in INSIDE_DQUOTE_LIST).
596
     */
597
598
0
    if (in_d_quote) {
599
0
      if (*src == '\\') {
600
        /*
601
         * Next character might be escaped.
602
         * We have to peek. Inside double
603
         * quotes only INSIDE_DQUOTE_LIST
604
         * characters are escaped by a \.
605
         */
606
607
0
        char nextchar;
608
609
0
        c = next_codepoint(&src[1], &c_size);
610
0
        if (c == INVALID_CODEPOINT) {
611
0
          SAFE_FREE(ret);
612
0
          return NULL;
613
0
        }
614
0
        if (c_size > 1) {
615
          /*
616
           * Don't escape the next char.
617
           * Just copy the \.
618
           */
619
0
          *dest++ = *src++;
620
0
          continue;
621
0
        }
622
623
0
        nextchar = src[1];
624
625
0
        if (nextchar && strchr(INSIDE_DQUOTE_LIST,
626
0
              (int)nextchar)) {
627
0
          next_escaped = true;
628
0
        }
629
0
        *dest++ = *src++;
630
0
        continue;
631
0
      }
632
633
0
      if (*src == '\"') {
634
        /* Exit double quote state. */
635
0
        in_d_quote = false;
636
0
        *dest++ = *src++;
637
0
        continue;
638
0
      }
639
640
      /*
641
       * We know the character isn't \ or ",
642
       * so escape it if it's any of the other
643
       * possible unprotected characters.
644
       */
645
646
0
            if (strchr(INSIDE_DQUOTE_LIST, (int)*src)) {
647
0
        *dest++ = '\\';
648
0
      }
649
0
      *dest++ = *src++;
650
0
      continue;
651
0
    }
652
653
    /*
654
     * From here to the end of the loop we're
655
     * not in the single or double quote state.
656
     */
657
658
0
    if (*src == '\\') {
659
      /* Next character must be escaped. */
660
0
      next_escaped = true;
661
0
      *dest++ = *src++;
662
0
      continue;
663
0
    }
664
665
0
    if (*src == '\'') {
666
      /* Go into single quote state. */
667
0
      in_s_quote = true;
668
0
      *dest++ = *src++;
669
0
      continue;
670
0
    }
671
672
0
    if (*src == '\"') {
673
      /* Go into double quote state. */
674
0
      in_d_quote = true;
675
0
      *dest++ = *src++;
676
0
      continue;
677
0
    }
678
679
    /* Check if we need to escape the character. */
680
681
0
          if (!strchr(INCLUDE_LIST, (int)*src)) {
682
0
      *dest++ = '\\';
683
0
    }
684
0
    *dest++ = *src++;
685
0
  }
686
0
  *dest++ = '\0';
687
0
  return ret;
688
0
}
689
690
/*
691
 * This routine improves performance for operations temporarily acting on a
692
 * full path. It is equivalent to the much more expensive
693
 *
694
 * talloc_asprintf(talloc_tos(), "%s/%s", dir, name)
695
 *
696
 * This actually does make a difference in metadata-heavy workloads (i.e. the
697
 * "standard" client.txt nbench run.
698
 */
699
700
ssize_t full_path_tos(const char *dir, const char *name,
701
          char *tmpbuf, size_t tmpbuf_len,
702
          char **pdst, char **to_free)
703
0
{
704
0
  size_t dirlen, namelen, len;
705
0
  char *dst;
706
707
0
  dirlen = strlen(dir);
708
0
  namelen = strlen(name);
709
0
  len = dirlen + namelen + 1;
710
711
0
  if (len < tmpbuf_len) {
712
0
    dst = tmpbuf;
713
0
    *to_free = NULL;
714
0
  } else {
715
0
    dst = talloc_array(talloc_tos(), char, len+1);
716
0
    if (dst == NULL) {
717
0
      return -1;
718
0
    }
719
0
    *to_free = dst;
720
0
  }
721
722
0
  memcpy(dst, dir, dirlen);
723
0
  dst[dirlen] = '/';
724
0
  memcpy(dst+dirlen+1, name, namelen+1);
725
0
  *pdst = dst;
726
0
  return len;
727
0
}