Coverage Report

Created: 2025-07-11 06:28

/src/opensips/script_var.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2006 voice-system.ro
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
19
 *
20
 */
21
22
/*!
23
 * \file
24
 * \brief Script variables
25
 */
26
27
#include <assert.h>
28
#include <ctype.h>
29
#include <string.h>
30
#include <stdlib.h>
31
32
#include "dprint.h"
33
#include "mem/mem.h"
34
35
#include "script_var.h"
36
37
static script_var_t *script_vars = 0;
38
39
script_var_t* add_var(const str *name)
40
0
{
41
0
  script_var_t *it;
42
43
0
  if(name==0 || name->s==0 || name->len<=0)
44
0
    return 0;
45
46
0
  for(it=script_vars; it; it=it->next)
47
0
  {
48
0
    if(it->name.len==name->len
49
0
        && strncmp(name->s, it->name.s, name->len)==0)
50
0
      return it;
51
0
  }
52
0
  it = (script_var_t*)pkg_malloc(sizeof(script_var_t));
53
0
  if(it==0)
54
0
  {
55
0
    LM_ERR("out of pkg mem\n");
56
0
    return 0;
57
0
  }
58
0
  memset(it, 0, sizeof(script_var_t));
59
0
  it->name.s = (char*)pkg_malloc((name->len+1)*sizeof(char));
60
0
  it->v.flags = VAR_VAL_NULL;
61
62
0
  if(it->name.s==0)
63
0
  {
64
0
    LM_ERR("out of pkg mem!\n");
65
0
    return 0;
66
0
  }
67
0
  it->name.len = name->len;
68
0
  strncpy(it->name.s, name->s, name->len);
69
0
  it->name.s[it->name.len] = '\0';
70
71
0
  it->next = script_vars;
72
73
0
  script_vars = it;
74
75
0
  return it;
76
0
}
77
78
script_var_t* set_var_value(script_var_t* var, int_str *value, int flags)
79
0
{
80
0
  if(var==0)
81
0
    return 0;
82
83
0
  if(value==NULL || flags&VAR_VAL_NULL)
84
0
  {
85
0
    if(var->v.flags&VAR_VAL_STR)
86
0
      pkg_free(var->v.value.s.s);
87
0
    memset(&var->v.value, 0, sizeof(int_str));
88
0
    var->v.flags = VAR_VAL_NULL;
89
0
    return var;
90
0
  }
91
92
0
  if(flags&VAR_VAL_STR)
93
0
  {
94
0
    if(var->v.flags&VAR_VAL_STR)
95
0
    { /* old and new values are str */
96
0
      if(value->s.len>var->v.value.s.len)
97
0
      { /* not enough space to copy */
98
0
        pkg_free(var->v.value.s.s);
99
0
        memset(&var->v.value, 0, sizeof(int_str));
100
0
        var->v.value.s.s =
101
0
          (char*)pkg_malloc((value->s.len+1)*sizeof(char));
102
0
        if(var->v.value.s.s==0)
103
0
        {
104
0
          LM_ERR("out of pkg mem\n");
105
0
          goto error;
106
0
        }
107
0
      }
108
0
    } else {
109
      /* old value was INT or NULL */
110
0
      memset(&var->v.value, 0, sizeof(int_str));
111
0
      var->v.value.s.s =
112
0
          (char*)pkg_malloc((value->s.len+1)*sizeof(char));
113
0
      if(var->v.value.s.s==0)
114
0
      {
115
0
        LM_ERR("out of pkg mem!\n");
116
0
        goto error;
117
0
      }
118
0
      var->v.flags = VAR_VAL_STR;
119
0
    }
120
0
    memcpy(var->v.value.s.s, value->s.s, value->s.len);
121
0
    var->v.value.s.len = value->s.len;
122
0
    var->v.value.s.s[value->s.len] = '\0';
123
0
  } else {
124
    /* new value is INT */
125
0
    if(var->v.flags&VAR_VAL_STR)
126
0
    {
127
0
      pkg_free(var->v.value.s.s);
128
0
      memset(&var->v.value, 0, sizeof(int_str));
129
0
    }
130
0
    var->v.flags = 0; /* no STR, no NULL */
131
0
    var->v.value.n = value->n;
132
0
  }
133
134
0
  return var;
135
0
error:
136
  /* set the var to init value */
137
0
  memset(&var->v.value, 0, sizeof(int_str));
138
0
  var->v.flags = VAR_VAL_NULL;
139
0
  return NULL;
140
0
}
141
142
script_var_t* get_var_by_name(str *name)
143
0
{
144
0
  script_var_t *it;
145
146
0
  if(name==0 || name->s==0 || name->len<=0)
147
0
    return 0;
148
149
0
  for(it=script_vars; it; it=it->next)
150
0
  {
151
0
    if(it->name.len==name->len
152
0
        && strncmp(name->s, it->name.s, name->len)==0)
153
0
      return it;
154
0
  }
155
0
  return 0;
156
0
}
157
158
void reset_vars(void)
159
0
{
160
0
  script_var_t *it;
161
0
  for(it=script_vars; it; it=it->next)
162
0
  {
163
0
    if(it->v.flags&VAR_VAL_STR)
164
0
      pkg_free(it->v.value.s.s);
165
0
    it->v.flags = VAR_VAL_NULL ;
166
0
    memset(&it->v.value, 0, sizeof(int_str));
167
0
  }
168
0
}
169
170
void destroy_vars_list(script_var_t *svl)
171
0
{
172
0
  script_var_t *it;
173
0
  script_var_t *it0;
174
175
0
  it = svl;
176
0
  while(it)
177
0
  {
178
0
    it0 = it;
179
0
    it = it->next;
180
0
    pkg_free(it0->name.s);
181
0
    if(it0->v.flags&VAR_VAL_STR)
182
0
      pkg_free(it0->v.value.s.s);
183
0
    pkg_free(it0);
184
0
  }
185
186
0
  svl = 0;
187
0
}
188
189
void destroy_vars(void)
190
0
{
191
0
  destroy_vars_list(script_vars);
192
0
}