LCOV - code coverage report
Current view: top level - lib/util - util_str_common.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 51 61 83.6 %
Date: 2024-02-29 22:57:05 Functions: 4 4 100.0 %

          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 "system/locale.h"
      26             : #include "lib/util/samba_util.h"
      27             : 
      28             : /**
      29             : Do a case-insensitive, whitespace-ignoring ASCII string compare.
      30             : **/
      31  8150968207 : _PUBLIC_ int strwicmp(const char *psz1, const char *psz2)
      32             : {
      33             :         /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
      34             :         /* appropriate value. */
      35  8150968207 :         if (psz1 == psz2)
      36        3136 :                 return (0);
      37  8150803025 :         else if (psz1 == NULL)
      38           0 :                 return (-1);
      39  8150803025 :         else if (psz2 == NULL)
      40           0 :                 return (1);
      41             : 
      42             :         /* sync the strings on first non-whitespace */
      43    11595455 :         while (1) {
      44  9570893688 :                 while (isspace((int)*psz1))
      45    79525184 :                         psz1++;
      46  9568081600 :                 while (isspace((int)*psz2))
      47    76713096 :                         psz2++;
      48             : 
      49             :                 /*
      50             :                  * This does not do a genuine multi-byte comparison,
      51             :                  * instead it just uses the fast-path for ASCII in
      52             :                  * these common routines
      53             :                  */
      54  9491368504 :                 if (toupper_m((unsigned char)*psz1) != toupper_m((unsigned char)*psz2)
      55  1368693346 :                     || *psz1 == '\0'
      56  1340565479 :                     || *psz2 == '\0')
      57             :                         break;
      58  1340565479 :                 psz1++;
      59  1340565479 :                 psz2++;
      60             :         }
      61  8150803025 :         return (*psz1 - *psz2);
      62             : }
      63             : 
      64             : /**
      65             :  String replace.
      66             :  NOTE: oldc and newc must be 7 bit characters
      67             : **/
      68      658750 : void string_replace( char *s, char oldc, char newc )
      69             : {
      70    14524502 :         while (*s != '\0') {
      71      111880 :                 size_t c_size;
      72    13865752 :                 next_codepoint(s, &c_size);
      73             : 
      74    13865752 :                 if (c_size == 1) {
      75    13865752 :                         if (*s == oldc) {
      76     1211399 :                                 *s = newc;
      77             :                         }
      78             :                 }
      79    13865752 :                 s += c_size;
      80             :         }
      81      658750 : }
      82             : 
      83             : 
      84             : /**
      85             :  Paranoid strcpy into a buffer of given length (includes terminating
      86             :  zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
      87             :  and replaces with '_'. Deliberately does *NOT* check for multibyte
      88             :  characters. Treats src as an array of bytes, not as a multibyte
      89             :  string. Any byte >0x7f is automatically converted to '_'.
      90             :  other_safe_chars must also contain an ascii string (bytes<0x7f).
      91             : **/
      92             : 
      93      112394 : char *alpha_strcpy(char *dest,
      94             :                    const char *src,
      95             :                    const char *other_safe_chars,
      96             :                    size_t maxlength)
      97             : {
      98        1755 :         size_t len, i;
      99             : 
     100      112394 :         if (!dest) {
     101           0 :                 smb_panic("ERROR: NULL dest in alpha_strcpy");
     102             :         }
     103             : 
     104      112394 :         if (!src) {
     105           0 :                 *dest = 0;
     106           0 :                 return dest;
     107             :         }
     108             : 
     109      112394 :         len = strlen(src);
     110      112394 :         if (len >= maxlength)
     111           0 :                 len = maxlength - 1;
     112             : 
     113      112394 :         if (!other_safe_chars)
     114          12 :                 other_safe_chars = "";
     115             : 
     116     1021183 :         for(i = 0; i < len; i++) {
     117      908789 :                 int val = (src[i] & 0xff);
     118      908789 :                 if (val > 0x7f) {
     119           0 :                         dest[i] = '_';
     120           0 :                         continue;
     121             :                 }
     122      908789 :                 if (isupper(val) || islower(val) ||
     123      351149 :                                 isdigit(val) || strchr(other_safe_chars, val))
     124      906849 :                         dest[i] = src[i];
     125             :                 else
     126        1940 :                         dest[i] = '_';
     127             :         }
     128             : 
     129      112394 :         dest[i] = '\0';
     130             : 
     131      112394 :         return dest;
     132             : }
     133             : 
     134       59629 : char *talloc_alpha_strcpy(TALLOC_CTX *mem_ctx,
     135             :                           const char *src,
     136             :                           const char *other_safe_chars)
     137             : {
     138       59629 :         char *dest = NULL;
     139         913 :         size_t slen;
     140             : 
     141       59629 :         if (src == NULL) {
     142           0 :                 return NULL;
     143             :         }
     144             : 
     145       59629 :         slen = strlen(src);
     146             : 
     147       59629 :         dest = talloc_zero_size(mem_ctx, slen + 1);
     148       59629 :         if (dest == NULL) {
     149           0 :                 return NULL;
     150             :         }
     151             : 
     152       59629 :         alpha_strcpy(dest, src, other_safe_chars, slen + 1);
     153       59629 :         return dest;
     154             : }

Generated by: LCOV version 1.14