Coverage Report

Created: 2024-02-25 06:34

/src/kamailio/src/core/str.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 *
3
 * Copyright (C) 2015 kamailio.org
4
 * Copyright (C) 2014 Victor Seva <vseva@sipwise.com>
5
 *
6
 * This file is part of kamailio, a free SIP server.
7
 *
8
 * Permission to use, copy, modify, and distribute this software for any
9
 * purpose with or without fee is hereby granted, provided that the above
10
 * copyright notice and this permission notice appear in all copies.
11
 *
12
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
 */
20
/*!
21
 * \file
22
 * \brief Kamailio core :: Append to str data
23
 * \ingroup core
24
 * Module: \ref core
25
 */
26
27
#include <string.h>
28
#include "str.h"
29
#include "mem/mem.h"
30
31
int str_append(str *orig, str *suffix, str *dest)
32
0
{
33
0
  if(orig == NULL || suffix == NULL || suffix->len == 0 || dest == NULL) {
34
0
    LM_ERR("wrong parameters\n");
35
0
    return -1;
36
0
  }
37
0
  dest->len = orig->len + suffix->len;
38
0
  dest->s = pkg_malloc(sizeof(char) * dest->len);
39
0
  if(dest->s == NULL) {
40
0
    PKG_MEM_ERROR;
41
0
    return -1;
42
0
  }
43
0
  if(orig->len > 0) {
44
0
    memcpy(dest->s, orig->s, orig->len);
45
0
  }
46
0
  memcpy(dest->s + orig->len, suffix->s, suffix->len);
47
0
  return 0;
48
0
}
49
50
/*
51
* Find the first occurrence of find in s, where the search is limited to the
52
* first slen characters of s.
53
*/
54
char *_strnstr(const char *s, const char *find, size_t slen)
55
0
{
56
0
  char c, sc;
57
0
  size_t len;
58
59
0
  if((c = *find++) != '\0') {
60
0
    len = strlen(find);
61
0
    do {
62
0
      do {
63
0
        if(slen-- < 1 || (sc = *s++) == '\0')
64
0
          return (NULL);
65
0
      } while(sc != c);
66
0
      if(len > slen)
67
0
        return (NULL);
68
0
    } while(strncmp(s, find, len) != 0);
69
0
    s--;
70
0
  }
71
0
  return ((char *)s);
72
0
}
73
74
/*
75
 * Find the first case insensitive occurrence of find in s, where the
76
 * search is limited to the first slen characters of s.
77
 * Based on FreeBSD strnstr.
78
 */
79
char *_strnistr(const char *s, const char *find, size_t slen)
80
0
{
81
0
  char c, sc;
82
0
  size_t len;
83
84
0
  if((c = *find++) != '\0') {
85
0
    len = strlen(find);
86
0
    do {
87
0
      do {
88
0
        if((sc = *s++) == '\0' || slen-- < 1)
89
0
          return (NULL);
90
0
      } while(sc != c);
91
0
      if(len > slen)
92
0
        return (NULL);
93
0
    } while(strncasecmp(s, find, len) != 0);
94
0
    s--;
95
0
  }
96
0
  return ((char *)s);
97
0
}