Coverage Report

Created: 2026-09-01 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/strongswan/src/libstrongswan/utils/lexparser.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2001-2006 Andreas Steffen
3
 *
4
 * Copyright (C) secunet Security Networks AG
5
 *
6
 * This program is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License as published by the
8
 * Free Software Foundation; either version 2 of the License, or (at your
9
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
10
 *
11
 * This program is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14
 * for more details.
15
 */
16
17
#include "lexparser.h"
18
19
/**
20
 * eat whitespace
21
 */
22
bool eat_whitespace(chunk_t *src)
23
0
{
24
0
  while (src->len > 0 && (*src->ptr == ' ' || *src->ptr == '\t'))
25
0
  {
26
0
    src->ptr++;  src->len--;
27
0
  }
28
0
  return  src->len > 0 && *src->ptr != '#';
29
0
}
30
31
/**
32
 * compare string with chunk
33
 */
34
bool match(const char *pattern, const chunk_t *ch)
35
0
{
36
0
  return ch->len == strlen(pattern) && strncmp(pattern, ch->ptr, ch->len) == 0;
37
0
}
38
39
/**
40
 * compare string with chunk ignoring the case of the characters
41
 */
42
bool matchcase(const char *pattern, const chunk_t *ch)
43
0
{
44
0
  return ch->len == strlen(pattern) && strncasecmp(pattern, ch->ptr, ch->len) == 0;
45
0
}
46
47
/**
48
 * extracts a token ending with the first occurrence of a given termination symbol
49
 */
50
bool extract_token(chunk_t *token, const char termination, chunk_t *src)
51
0
{
52
0
  u_char *eot = memchr(src->ptr, termination, src->len);
53
54
0
  if (termination == ' ')
55
0
  {
56
0
    u_char *eot_tab = memchr(src->ptr, '\t', src->len);
57
58
    /* check if a tab instead of a space terminates the token */
59
0
    eot = ( eot_tab == NULL || (eot && eot < eot_tab) ) ? eot : eot_tab;
60
0
  }
61
62
  /* initialize empty token */
63
0
  *token = chunk_empty;
64
65
0
  if (eot == NULL) /* termination symbol not found */
66
0
  {
67
0
    return FALSE;
68
0
  }
69
70
  /* extract token */
71
0
  token->ptr = src->ptr;
72
0
  token->len = (u_int)(eot - src->ptr);
73
74
  /* advance src pointer after termination symbol */
75
0
  src->ptr = eot + 1;
76
0
  src->len -= (token->len + 1);
77
78
0
  return TRUE;
79
0
}
80
81
/**
82
 * extracts a token ending with the first occurrence of a given null-terminated string
83
 */
84
bool extract_token_str(chunk_t *token, const char *termination, chunk_t *src)
85
0
{
86
0
  u_char *eot = memstr(src->ptr, termination, src->len);
87
0
  size_t l = strlen(termination);
88
89
  /* initialize empty token */
90
0
  *token = chunk_empty;
91
92
0
  if (eot == NULL) /* termination string not found */
93
0
  {
94
0
    return FALSE;
95
0
  }
96
97
  /* extract token */
98
0
  token->ptr = src->ptr;
99
0
  token->len = (u_int)(eot - src->ptr);
100
101
  /* advance src pointer after termination string */
102
0
  src->ptr = eot + l;
103
0
  src->len -= (token->len + l);
104
105
0
  return TRUE;
106
0
}
107
108
/**
109
 *  fetches a new line terminated by \n or \r\n
110
 */
111
bool fetchline(chunk_t *src, chunk_t *line)
112
0
{
113
0
  if (src->len == 0) /* end of src reached */
114
0
    return FALSE;
115
116
0
  if (extract_token(line, '\n', src))
117
0
  {
118
0
    if (line->len > 0 && *(line->ptr + line->len -1) == '\r')
119
0
      line->len--;  /* remove optional \r */
120
0
  }
121
0
  else /*last line ends without newline */
122
0
  {
123
0
    *line = *src;
124
0
    src->ptr += src->len;
125
0
    src->len = 0;
126
0
  }
127
0
  return TRUE;
128
0
}
129
130
err_t extract_value(chunk_t *value, chunk_t *line)
131
0
{
132
0
  char delimiter = ' ';
133
134
0
  if (!eat_whitespace(line))
135
0
  {
136
0
    *value = chunk_empty;
137
0
    return NULL;
138
0
  }
139
0
  if (*line->ptr == '\'' || *line->ptr == '"')
140
0
  {
141
0
    delimiter = *line->ptr;
142
0
    line->ptr++;  line->len--;
143
0
  }
144
0
  if (!extract_token(value, delimiter, line))
145
0
  {
146
0
    if (delimiter == ' ')
147
0
    {
148
0
      *value = *line;
149
0
      line->len = 0;
150
0
    }
151
0
    else
152
0
    {
153
0
      return "missing second delimiter";
154
0
    }
155
0
  }
156
0
  return NULL;
157
0
}
158
159
/**
160
 * extracts a parameter: value pair
161
 */
162
err_t extract_parameter_value(chunk_t *name, chunk_t *value, chunk_t *line)
163
0
{
164
  /* extract name */
165
0
  if (!extract_token(name,':', line))
166
0
  {
167
0
    return "missing ':'";
168
0
  }
169
170
  /* extract value */
171
0
  return extract_value(value, line);
172
0
}