Coverage Report

Created: 2026-08-31 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/p11-kit/common/lexer.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2005 Stefan Walter
3
 * Copyright (c) 2011 Collabora Ltd.
4
 * Copyright (c) 2013 Red Hat Inc.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 *     * Redistributions of source code must retain the above
11
 *       copyright notice, this list of conditions and the
12
 *       following disclaimer.
13
 *     * Redistributions in binary form must reproduce the
14
 *       above copyright notice, this list of conditions and
15
 *       the following disclaimer in the documentation and/or
16
 *       other materials provided with the distribution.
17
 *     * The names of contributors to this software may not be
18
 *       used to endorse or promote products derived from this
19
 *       software without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32
 * DAMAGE.
33
 *
34
 *
35
 * CONTRIBUTORS
36
 *  Stef Walter <stefw@redhat.com>
37
 */
38
39
#include "config.h"
40
41
#define P11_DEBUG_FLAG P11_DEBUG_CONF
42
#include "debug.h"
43
#include "lexer.h"
44
#include "message.h"
45
46
#include <assert.h>
47
#include <ctype.h>
48
#include <errno.h>
49
#include <stdio.h>
50
#include <stdlib.h>
51
#include <string.h>
52
53
/* Only use on uninitialized lexer or after p11_lexer_done()
54
 */
55
bool
56
p11_lexer_init (p11_lexer *lexer,
57
                const char *filename,
58
                const char *data,
59
                size_t length)
60
2.20k
{
61
2.20k
  return_val_if_fail (lexer != NULL, false);
62
63
2.20k
  memset (lexer, 0, sizeof (p11_lexer));
64
2.20k
  lexer->at = data;
65
2.20k
  lexer->remaining = length;
66
67
2.20k
  return_val_if_fail (filename != NULL, false);
68
2.20k
  lexer->filename = strdup (filename);
69
2.20k
  return_val_if_fail (lexer->filename != NULL, false);
70
2.20k
  return true;
71
2.20k
}
72
73
static void
74
clear_state (p11_lexer *lexer)
75
318k
{
76
318k
  switch (lexer->tok_type) {
77
14.4k
  case TOK_FIELD:
78
14.4k
    free (lexer->tok.field.name);
79
14.4k
    free (lexer->tok.field.value);
80
14.4k
    break;
81
297k
  case TOK_SECTION:
82
297k
    free (lexer->tok.section.name);
83
297k
    break;
84
3.18k
  case TOK_PEM:
85
6.94k
  case TOK_EOF:
86
6.94k
    break;
87
318k
  }
88
89
318k
  memset (&lexer->tok, 0, sizeof (lexer->tok));
90
318k
  lexer->tok_type = TOK_EOF;
91
318k
  lexer->complained = false;
92
318k
}
93
94
bool
95
p11_lexer_next (p11_lexer *lexer,
96
                bool *failed)
97
316k
{
98
316k
  const char *colon;
99
316k
  const char *value;
100
316k
  const char *line;
101
316k
  const char *end;
102
316k
  const char *pos;
103
104
316k
  return_val_if_fail (lexer != NULL, false);
105
106
316k
  clear_state (lexer);
107
316k
  if (failed)
108
316k
    *failed = false;
109
110
  /* Go through lines and process them */
111
347k
  while (lexer->remaining != 0) {
112
346k
    assert (lexer->remaining > 0);
113
114
    /* Is this line the start of a PEM block? */
115
346k
    if (lexer->remaining >= 11 && strncmp (lexer->at, "-----BEGIN ", 11) == 0) {
116
3.20k
      pos = strnstr (lexer->at, "\n-----END ", lexer->remaining);
117
3.20k
      if (pos == NULL) {
118
20
        p11_lexer_msg (lexer, "invalid pem block: no ending line");
119
20
        if (failed)
120
20
          *failed = true;
121
20
        return false;
122
20
      }
123
124
3.18k
      end = memchr (pos + 1, '\n', lexer->remaining - (pos - lexer->at) - 1);
125
3.18k
      end = end ? end + 1 : lexer->at + lexer->remaining;
126
      /* Count newlines in the PEM block */
127
3.18k
      pos = lexer->at;
128
15.0k
      while (pos < end) {
129
12.1k
        pos = memchr (pos, '\n', end - pos);
130
12.1k
        if (!pos)
131
318
          break;
132
11.8k
        lexer->line++;
133
11.8k
        pos++;
134
11.8k
      }
135
3.18k
      lexer->tok_type = TOK_PEM;
136
3.18k
      lexer->tok.pem.begin = lexer->at;
137
3.18k
      lexer->tok.pem.length = end - lexer->at;
138
3.18k
      assert (end - lexer->at <= lexer->remaining);
139
3.18k
      lexer->remaining -= (end - lexer->at);
140
3.18k
      lexer->at = end;
141
3.18k
      return true;
142
3.18k
    }
143
144
342k
    line = lexer->at;
145
342k
    end = memchr (lexer->at, '\n', lexer->remaining);
146
342k
    if (end == NULL) {
147
1.71k
      end = lexer->at + lexer->remaining;
148
1.71k
      lexer->remaining = 0;
149
1.71k
      lexer->at = end;
150
341k
    } else {
151
341k
      assert ((end - lexer->at) + 1 <= lexer->remaining);
152
341k
      lexer->line++;
153
341k
      lexer->remaining -= (end - lexer->at) + 1;
154
341k
      lexer->at = end + 1;
155
341k
    }
156
157
    /* Strip whitespace from line */
158
343k
    while (line != end && isspace (line[0]))
159
412
      ++line;
160
344k
    while (line != end && isspace (*(end - 1)))
161
1.22k
      --end;
162
163
    /* Empty lines / comments at start */
164
342k
    if (line == end || line[0] == '#')
165
30.8k
      continue;
166
167
    /* Is it a section ? */
168
312k
    if (line[0] == '[') {
169
297k
      if (*(end - 1) != ']') {
170
25
        p11_lexer_msg (lexer, "invalid section header: missing braces");
171
25
        if (failed)
172
25
          *failed = true;
173
25
        return false;
174
25
      }
175
176
297k
      lexer->tok.section.name = strndup (line + 1, (end - line) - 2);
177
297k
      return_val_if_fail (lexer->tok.section.name != NULL, false);
178
297k
      lexer->tok_type = TOK_SECTION;
179
297k
      return true;
180
297k
    }
181
182
    /* Look for the break between name: value on the same line */
183
14.5k
    colon = memchr (line, ':', end - line);
184
14.5k
    if (colon == NULL) {
185
147
      p11_lexer_msg (lexer, "invalid field line: no colon");
186
147
      if (failed)
187
147
        *failed = true;
188
147
      return false;
189
147
    }
190
191
    /* Strip whitespace from name and value */
192
14.4k
    value = colon + 1;
193
15.2k
    while (value != end && isspace (value[0]))
194
813
      ++value;
195
14.8k
    while (line != colon && isspace (*(colon - 1)))
196
400
      --colon;
197
198
14.4k
    lexer->tok.field.name = strndup (line, colon - line);
199
14.4k
    return_val_if_fail (lexer->tok.field.name != NULL, false);
200
14.4k
    lexer->tok.field.value = strndup (value, end - value);
201
14.4k
    if (lexer->tok.field.value == NULL) {
202
0
      free (lexer->tok.field.name);
203
0
      return_val_if_reached (false);
204
0
    }
205
14.4k
    lexer->tok_type = TOK_FIELD;
206
14.4k
    return true;
207
14.4k
  }
208
209
1.36k
  return false;
210
316k
}
211
212
void
213
p11_lexer_done (p11_lexer *lexer)
214
2.20k
{
215
2.20k
  return_if_fail (lexer != NULL);
216
2.20k
  clear_state (lexer);
217
2.20k
  free (lexer->filename);
218
2.20k
  memset (lexer, 0, sizeof (p11_lexer));
219
2.20k
}
220
221
void
222
p11_lexer_msg (p11_lexer *lexer,
223
               const char *msg)
224
92.3k
{
225
92.3k
  return_if_fail (lexer != NULL);
226
227
92.3k
  if (lexer->complained)
228
72.6k
    return;
229
230
19.6k
  switch (lexer->tok_type) {
231
316
  case TOK_FIELD:
232
316
    p11_message ("%s:%zu: %s: %s", lexer->filename, lexer->line,
233
316
                 lexer->tok.field.name, msg);
234
316
    break;
235
18.8k
  case TOK_SECTION:
236
18.8k
    p11_message ("%s:%zu: [%s]: %s", lexer->filename, lexer->line,
237
18.8k
                 lexer->tok.section.name, msg);
238
18.8k
    break;
239
326
  case TOK_PEM:
240
326
    p11_message ("%s:%zu: BEGIN ...: %s", lexer->filename,
241
326
           lexer->line, msg);
242
326
    break;
243
192
  default:
244
192
    p11_message ("%s:%zu: %s", lexer->filename, lexer->line, msg);
245
192
    break;
246
19.6k
  }
247
248
19.6k
  lexer->complained = true;
249
19.6k
}