LCOV - code coverage report
Current view: top level - util - string_utils.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 53 92.5 %
Date: 2016-06-29 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /*
       2             :     SSSD
       3             : 
       4             :     Authors:
       5             :         Lukas Slebodnik <slebodnikl@redhat.com>
       6             : 
       7             :     Copyright (C) 2014 Red Hat
       8             : 
       9             :     This program is free software; you can redistribute it and/or modify
      10             :     it under the terms of the GNU General Public License as published by
      11             :     the Free Software Foundation; either version 3 of the License, or
      12             :     (at your option) any later version.
      13             : 
      14             :     This program is distributed in the hope that it will be useful,
      15             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :     GNU General Public License for more details.
      18             : 
      19             :     You should have received a copy of the GNU General Public License
      20             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : #include "util/util.h"
      24             : 
      25          29 : static char *replace_char(TALLOC_CTX *mem_ctx,
      26             :                           const char *in,
      27             :                           const char match,
      28             :                           const char sub)
      29             : {
      30             :     char *p;
      31             :     char *out;
      32             : 
      33          29 :     out = talloc_strdup(mem_ctx, in);
      34          29 :     if (out == NULL) {
      35           0 :         return NULL;
      36             :     }
      37             : 
      38         200 :     for (p = out; *p != '\0'; ++p) {
      39         171 :         if (*p == match) {
      40          49 :             *p = sub;
      41             :         }
      42             :     }
      43             : 
      44          29 :     return out;
      45             : }
      46             : 
      47          53 : char * sss_replace_space(TALLOC_CTX *mem_ctx,
      48             :                          const char *orig_name,
      49             :                          const char subst)
      50             : {
      51          53 :     if (subst == '\0' || subst == ' ') {
      52          39 :         return talloc_strdup(mem_ctx, orig_name);
      53             :     }
      54             : 
      55          14 :     if (strchr(orig_name, subst) != NULL) {
      56           1 :         DEBUG(SSSDBG_CRIT_FAILURE,
      57             :               "Input [%s] already contains replacement character [%c].\n",
      58             :               orig_name, subst);
      59           1 :         sss_log(SSS_LOG_CRIT,
      60             :                 "Name [%s] already contains replacement character [%c]. " \
      61             :                 "No replacement will be done.\n",
      62             :                 orig_name, subst);
      63           1 :         return talloc_strdup(mem_ctx, orig_name);
      64             :     }
      65             : 
      66          13 :     return replace_char(mem_ctx, orig_name, ' ', subst);
      67             : }
      68             : 
      69         155 : char * sss_reverse_replace_space(TALLOC_CTX *mem_ctx,
      70             :                                  const char *orig_name,
      71             :                                  const char subst)
      72             : {
      73         155 :     if (subst == '\0' || subst == ' ') {
      74         138 :         return talloc_strdup(mem_ctx, orig_name);
      75             :     }
      76             : 
      77          17 :     if (strchr(orig_name, subst) != NULL && strchr(orig_name, ' ') != NULL) {
      78           1 :         DEBUG(SSSDBG_TRACE_FUNC,
      79             :               "Input [%s] contains replacement character [%c] and space.\n",
      80             :               orig_name, subst);
      81           1 :         return talloc_strdup(mem_ctx, orig_name);
      82             :     }
      83             : 
      84          16 :     return replace_char(mem_ctx, orig_name, subst, ' ');
      85             : }
      86             : 
      87           6 : errno_t guid_blob_to_string_buf(const uint8_t *blob, char *str_buf,
      88             :                                 size_t buf_size)
      89             : {
      90             :     int ret;
      91             : 
      92           6 :     if (blob == NULL || str_buf == NULL || buf_size < GUID_STR_BUF_SIZE) {
      93           3 :         DEBUG(SSSDBG_CRIT_FAILURE, "Buffer too small.\n");
      94           3 :         return EINVAL;
      95             :     }
      96             : 
      97          48 :     ret = snprintf(str_buf, buf_size,
      98             :          "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
      99          12 :          blob[3], blob[2], blob[1], blob[0],
     100           6 :          blob[5], blob[4],
     101           6 :          blob[7], blob[6],
     102           6 :          blob[8], blob[9],
     103          18 :          blob[10], blob[11],blob[12], blob[13],blob[14], blob[15]);;
     104           3 :     if (ret != (GUID_STR_BUF_SIZE -1)) {
     105           0 :         DEBUG(SSSDBG_CRIT_FAILURE, "snprintf failed.\n");
     106           0 :         return EIO;
     107             :     }
     108             : 
     109           3 :     return EOK;
     110             : }
     111             : 
     112          16 : const char *get_last_x_chars(const char *str, size_t x)
     113             : {
     114             :     size_t len;
     115             : 
     116          16 :     if (str == NULL) {
     117           1 :         return NULL;
     118             :     }
     119             : 
     120          15 :     len = strlen(str);
     121             : 
     122          15 :     if (len < x) {
     123           1 :         return str;
     124             :     }
     125             : 
     126          14 :     return (str + len - x);
     127             : }
     128             : 
     129           2 : char **concatenate_string_array(TALLOC_CTX *mem_ctx,
     130             :                                 char **arr1, size_t len1,
     131             :                                 char **arr2, size_t len2)
     132             : {
     133             :     size_t i, j;
     134           2 :     size_t new_size = len1 + len2;
     135           2 :     char ** string_array = talloc_realloc(mem_ctx, arr1, char *, new_size + 1);
     136           2 :     if (string_array == NULL) {
     137           0 :         return NULL;
     138             :     }
     139             : 
     140           5 :     for (i=len1, j=0; i < new_size; ++i,++j) {
     141           3 :         string_array[i] = talloc_steal(string_array,
     142             :                                        arr2[j]);
     143             :     }
     144             : 
     145           2 :     string_array[i] = NULL;
     146             : 
     147           2 :     return string_array;
     148             : }

Generated by: LCOV version 1.10