LCOV - code coverage report
Current view: top level - lib/util - util_str.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 95 124 76.6 %
Date: 2024-02-29 22:57:05 Functions: 8 9 88.9 %

          Line data    Source code
       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     2005
       9             : 
      10             :    This program is free software; you can redistribute it and/or modify
      11             :    it under the terms of the GNU General Public License as published by
      12             :    the Free Software Foundation; either version 3 of the License, or
      13             :    (at your option) any later version.
      14             : 
      15             :    This program is distributed in the hope that it will be useful,
      16             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      17             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      18             :    GNU General Public License for more details.
      19             : 
      20             :    You should have received a copy of the GNU General Public License
      21             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      22             : */
      23             : 
      24             : #include "replace.h"
      25             : #include "lib/util/samba_util.h"
      26             : #include "system/locale.h"
      27             : #include "smb_strtox.h"
      28             : #undef strncasecmp
      29             : #undef strcasemp
      30             : 
      31             : /**
      32             :  * @file
      33             :  * @brief String utilities.
      34             :  **/
      35             : 
      36             : /**
      37             :  * Parse a string containing a boolean value.
      38             :  *
      39             :  * val will be set to the read value.
      40             :  *
      41             :  * @retval true if a boolean value was parsed, false otherwise.
      42             :  */
      43           0 : _PUBLIC_ bool conv_str_bool(const char * str, bool * val)
      44             : {
      45           0 :         char *  end = NULL;
      46           0 :         long    lval;
      47             : 
      48           0 :         if (str == NULL || *str == '\0') {
      49           0 :                 return false;
      50             :         }
      51             : 
      52           0 :         lval = strtol(str, &end, 10 /* base */);
      53           0 :         if (end == NULL || *end != '\0' || end == str) {
      54           0 :                 return set_boolean(str, val);
      55             :         }
      56             : 
      57           0 :         *val = (lval) ? true : false;
      58           0 :         return true;
      59             : }
      60             : 
      61             : /**
      62             :  * Convert a size specification like 16K into an integral number of bytes.
      63             :  **/
      64      195823 : _PUBLIC_ bool conv_str_size_error(const char * str, uint64_t * val)
      65             : {
      66      195823 :         char *              end = NULL;
      67        4421 :         unsigned long long  lval;
      68      195823 :         int error = 0;
      69             : 
      70      195823 :         if (str == NULL || *str == '\0') {
      71           0 :                 return false;
      72             :         }
      73             : 
      74      195823 :         lval = smb_strtoull(str, &end, 10, &error, SMB_STR_STANDARD);
      75      195823 :         if (error != 0) {
      76           0 :                 return false;
      77             :         }
      78             : 
      79      195823 :         if (*end) {
      80       43911 :                 if (strwicmp(end, "K") == 0) {
      81       43911 :                         lval *= 1024ULL;
      82           0 :                 } else if (strwicmp(end, "M") == 0) {
      83           0 :                         lval *= (1024ULL * 1024ULL);
      84           0 :                 } else if (strwicmp(end, "G") == 0) {
      85           0 :                         lval *= (1024ULL * 1024ULL * 1024ULL);
      86           0 :                 } else if (strwicmp(end, "T") == 0) {
      87           0 :                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
      88           0 :                 } else if (strwicmp(end, "P") == 0) {
      89           0 :                         lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
      90             :                 } else {
      91           0 :                         return false;
      92             :                 }
      93             :         }
      94             : 
      95      195823 :         *val = (uint64_t)lval;
      96      195823 :         return true;
      97             : }
      98             : 
      99             : /**
     100             :  * Parse a uint64_t value from a string
     101             :  *
     102             :  * val will be set to the value read.
     103             :  *
     104             :  * @retval true if parsing was successful, false otherwise
     105             :  */
     106          22 : _PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
     107             : {
     108           6 :         unsigned long long  lval;
     109          22 :         int error = 0;
     110             : 
     111          22 :         if (str == NULL || *str == '\0') {
     112           0 :                 return false;
     113             :         }
     114             : 
     115          22 :         lval = smb_strtoull(str, NULL, 10, &error, SMB_STR_FULL_STR_CONV);
     116          22 :         if (error != 0) {
     117           0 :                 return false;
     118             :         }
     119             : 
     120          20 :         *val = (uint64_t)lval;
     121          20 :         return true;
     122             : }
     123             : 
     124             : /**
     125             :  * Compare 2 strings.
     126             :  *
     127             :  * @note The comparison is case-insensitive.
     128             :  **/
     129   265887126 : _PUBLIC_ bool strequal(const char *s1, const char *s2)
     130             : {
     131   265887126 :         if (s1 == s2)
     132      623447 :                 return true;
     133   265252805 :         if (!s1 || !s2)
     134     6119987 :                 return false;
     135             : 
     136   259115169 :         return strcasecmp_m(s1,s2) == 0;
     137             : }
     138             : 
     139             : /**
     140             :  * @file
     141             :  * @brief String utilities.
     142             :  **/
     143             : 
     144    14281091 : static bool next_token_internal_talloc(TALLOC_CTX *ctx,
     145             :                                 const char **ptr,
     146             :                                 char **pp_buff,
     147             :                                 const char *sep,
     148             :                                 bool ltrim)
     149             : {
     150       84063 :         const char *s;
     151       84063 :         const char *saved_s;
     152       84063 :         char *pbuf;
     153       84063 :         bool quoted;
     154    14281091 :         size_t len=1;
     155             : 
     156    14281091 :         *pp_buff = NULL;
     157    14281091 :         if (!ptr) {
     158           0 :                 return(false);
     159             :         }
     160             : 
     161    14281091 :         s = *ptr;
     162             : 
     163             :         /* default to simple separators */
     164    14281091 :         if (!sep) {
     165       60506 :                 sep = " \t\n\r";
     166             :         }
     167             : 
     168             :         /* find the first non sep char, if left-trimming is requested */
     169    14281091 :         if (ltrim) {
     170    14342135 :                 while (*s && strchr_m(sep,*s)) {
     171       64034 :                         s++;
     172             :                 }
     173             :         }
     174             : 
     175             :         /* nothing left? */
     176    14281091 :         if (!*s) {
     177     3779487 :                 return false;
     178             :         }
     179             : 
     180             :         /* When restarting we need to go from here. */
     181    10417541 :         saved_s = s;
     182             : 
     183             :         /* Work out the length needed. */
     184   141713724 :         for (quoted = false; *s &&
     185   268611772 :                         (quoted || !strchr_m(sep,*s)); s++) {
     186   131233003 :                 if (*s == '\"') {
     187       79746 :                         quoted = !quoted;
     188             :                 } else {
     189   131153257 :                         len++;
     190             :                 }
     191             :         }
     192             : 
     193             :         /* We started with len = 1 so we have space for the nul. */
     194    10480721 :         *pp_buff = talloc_array(ctx, char, len);
     195    10480721 :         if (!*pp_buff) {
     196           0 :                 return false;
     197             :         }
     198             : 
     199             :         /* copy over the token */
     200    10417541 :         pbuf = *pp_buff;
     201    10417541 :         s = saved_s;
     202   141713724 :         for (quoted = false; *s &&
     203   268611772 :                         (quoted || !strchr_m(sep,*s)); s++) {
     204   131233003 :                 if ( *s == '\"' ) {
     205       79746 :                         quoted = !quoted;
     206             :                 } else {
     207   131153257 :                         *pbuf++ = *s;
     208             :                 }
     209             :         }
     210             : 
     211    10480721 :         *ptr = (*s) ? s+1 : s;
     212    10480721 :         *pbuf = 0;
     213             : 
     214    10480721 :         return true;
     215             : }
     216             : 
     217    14278101 : bool next_token_talloc(TALLOC_CTX *ctx,
     218             :                         const char **ptr,
     219             :                         char **pp_buff,
     220             :                         const char *sep)
     221             : {
     222    14278101 :         return next_token_internal_talloc(ctx, ptr, pp_buff, sep, true);
     223             : }
     224             : 
     225             : /*
     226             :  * Get the next token from a string, return false if none found.  Handles
     227             :  * double-quotes.  This version does not trim leading separator characters
     228             :  * before looking for a token.
     229             :  */
     230             : 
     231        2990 : bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
     232             :                         const char **ptr,
     233             :                         char **pp_buff,
     234             :                         const char *sep)
     235             : {
     236        2990 :         return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
     237             : }
     238             : 
     239             : /**
     240             :  * Get the next token from a string, return False if none found.
     241             :  * Handles double-quotes.
     242             :  *
     243             :  * Based on a routine by GJC@VILLAGE.COM.
     244             :  * Extensively modified by Andrew.Tridgell@anu.edu.au
     245             :  **/
     246        2184 : _PUBLIC_ bool next_token(const char **ptr,char *buff, const char *sep, size_t bufsize)
     247             : {
     248          16 :         const char *s;
     249          16 :         bool quoted;
     250        2184 :         size_t len=1;
     251             : 
     252        2184 :         if (!ptr)
     253           0 :                 return false;
     254             : 
     255        2183 :         s = *ptr;
     256             : 
     257             :         /* default to simple separators */
     258        2183 :         if (!sep)
     259           4 :                 sep = " \t\n\r";
     260             : 
     261             :         /* find the first non sep char */
     262        2187 :         while (*s && strchr_m(sep,*s))
     263           4 :                 s++;
     264             : 
     265             :         /* nothing left? */
     266        2183 :         if (!*s)
     267         104 :                 return false;
     268             : 
     269             :         /* copy over the token */
     270       65590 :         for (quoted = false; len < bufsize && *s && (quoted || !strchr_m(sep,*s)); s++) {
     271       63516 :                 if (*s == '\"') {
     272           3 :                         quoted = !quoted;
     273             :                 } else {
     274       63513 :                         len++;
     275       63513 :                         *buff++ = *s;
     276             :                 }
     277             :         }
     278             : 
     279        2074 :         *ptr = (*s) ? s+1 : s;
     280        2074 :         *buff = 0;
     281             : 
     282        2074 :         return true;
     283             : }
     284             : 
     285             : /**
     286             :  Set a boolean variable from the text value stored in the passed string.
     287             :  Returns true in success, false if the passed string does not correctly
     288             :  represent a boolean.
     289             : **/
     290             : 
     291     5634996 : _PUBLIC_ bool set_boolean(const char *boolean_string, bool *boolean)
     292             : {
     293     8084221 :         if (strwicmp(boolean_string, "yes") == 0 ||
     294     3952577 :             strwicmp(boolean_string, "true") == 0 ||
     295     3006704 :             strwicmp(boolean_string, "on") == 0 ||
     296     1503352 :             strwicmp(boolean_string, "1") == 0) {
     297     4131650 :                 *boolean = true;
     298     4131650 :                 return true;
     299     1679858 :         } else if (strwicmp(boolean_string, "no") == 0 ||
     300      176512 :                    strwicmp(boolean_string, "false") == 0 ||
     301           0 :                    strwicmp(boolean_string, "off") == 0 ||
     302           0 :                    strwicmp(boolean_string, "0") == 0) {
     303     1503346 :                 *boolean = false;
     304     1503346 :                 return true;
     305             :         }
     306           0 :         return false;
     307             : }

Generated by: LCOV version 1.14