Coverage Report

Created: 2026-08-14 07:18

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
470
{
61
470
  return_val_if_fail (lexer != NULL, false);
62
63
470
  memset (lexer, 0, sizeof (p11_lexer));
64
470
  lexer->at = data;
65
470
  lexer->remaining = length;
66
67
470
  return_val_if_fail (filename != NULL, false);
68
470
  lexer->filename = strdup (filename);
69
470
  return_val_if_fail (lexer->filename != NULL, false);
70
470
  return true;
71
470
}
72
73
static void
74
clear_state (p11_lexer *lexer)
75
3.76k
{
76
3.76k
  switch (lexer->tok_type) {
77
1.51k
  case TOK_FIELD:
78
1.51k
    free (lexer->tok.field.name);
79
1.51k
    free (lexer->tok.field.value);
80
1.51k
    break;
81
307
  case TOK_SECTION:
82
307
    free (lexer->tok.section.name);
83
307
    break;
84
998
  case TOK_PEM:
85
1.93k
  case TOK_EOF:
86
1.93k
    break;
87
3.76k
  }
88
89
3.76k
  memset (&lexer->tok, 0, sizeof (lexer->tok));
90
3.76k
  lexer->tok_type = TOK_EOF;
91
3.76k
  lexer->complained = false;
92
3.76k
}
93
94
bool
95
p11_lexer_next (p11_lexer *lexer,
96
                bool *failed)
97
3.29k
{
98
3.29k
  const char *colon;
99
3.29k
  const char *value;
100
3.29k
  const char *line;
101
3.29k
  const char *end;
102
3.29k
  const char *pos;
103
104
3.29k
  return_val_if_fail (lexer != NULL, false);
105
106
3.29k
  clear_state (lexer);
107
3.29k
  if (failed)
108
3.29k
    *failed = false;
109
110
  /* Go through lines and process them */
111
6.36k
  while (lexer->remaining != 0) {
112
6.22k
    assert (lexer->remaining > 0);
113
114
    /* Is this line the start of a PEM block? */
115
6.22k
    if (lexer->remaining >= 11 && strncmp (lexer->at, "-----BEGIN ", 11) == 0) {
116
1.13k
      pos = strnstr (lexer->at, "\n-----END ", lexer->remaining);
117
1.13k
      if (pos == NULL) {
118
137
        p11_lexer_msg (lexer, "invalid pem block: no ending line");
119
137
        if (failed)
120
137
          *failed = true;
121
137
        return false;
122
137
      }
123
124
998
      end = memchr (pos + 1, '\n', lexer->remaining - (pos - lexer->at) - 1);
125
998
      end = end ? end + 1 : lexer->at + lexer->remaining;
126
      /* Count newlines in the PEM block */
127
998
      pos = lexer->at;
128
11.1k
      while (pos < end) {
129
10.1k
        pos = memchr (pos, '\n', end - pos);
130
10.1k
        if (!pos)
131
20
          break;
132
10.1k
        lexer->line++;
133
10.1k
        pos++;
134
10.1k
      }
135
998
      lexer->tok_type = TOK_PEM;
136
998
      lexer->tok.pem.begin = lexer->at;
137
998
      lexer->tok.pem.length = end - lexer->at;
138
998
      assert (end - lexer->at <= lexer->remaining);
139
998
      lexer->remaining -= (end - lexer->at);
140
998
      lexer->at = end;
141
998
      return true;
142
998
    }
143
144
5.09k
    line = lexer->at;
145
5.09k
    end = memchr (lexer->at, '\n', lexer->remaining);
146
5.09k
    if (end == NULL) {
147
233
      end = lexer->at + lexer->remaining;
148
233
      lexer->remaining = 0;
149
233
      lexer->at = end;
150
4.85k
    } else {
151
4.85k
      assert ((end - lexer->at) + 1 <= lexer->remaining);
152
4.85k
      lexer->line++;
153
4.85k
      lexer->remaining -= (end - lexer->at) + 1;
154
4.85k
      lexer->at = end + 1;
155
4.85k
    }
156
157
    /* Strip whitespace from line */
158
6.73k
    while (line != end && isspace (line[0]))
159
1.64k
      ++line;
160
7.03k
    while (line != end && isspace (*(end - 1)))
161
1.93k
      --end;
162
163
    /* Empty lines / comments at start */
164
5.09k
    if (line == end || line[0] == '#')
165
3.07k
      continue;
166
167
    /* Is it a section ? */
168
2.02k
    if (line[0] == '[') {
169
321
      if (*(end - 1) != ']') {
170
14
        p11_lexer_msg (lexer, "invalid section header: missing braces");
171
14
        if (failed)
172
14
          *failed = true;
173
14
        return false;
174
14
      }
175
176
307
      lexer->tok.section.name = strndup (line + 1, (end - line) - 2);
177
307
      return_val_if_fail (lexer->tok.section.name != NULL, false);
178
307
      lexer->tok_type = TOK_SECTION;
179
307
      return true;
180
307
    }
181
182
    /* Look for the break between name: value on the same line */
183
1.70k
    colon = memchr (line, ':', end - line);
184
1.70k
    if (colon == NULL) {
185
186
      p11_lexer_msg (lexer, "invalid field line: no colon");
186
186
      if (failed)
187
186
        *failed = true;
188
186
      return false;
189
186
    }
190
191
    /* Strip whitespace from name and value */
192
1.51k
    value = colon + 1;
193
2.84k
    while (value != end && isspace (value[0]))
194
1.33k
      ++value;
195
2.48k
    while (line != colon && isspace (*(colon - 1)))
196
969
      --colon;
197
198
1.51k
    lexer->tok.field.name = strndup (line, colon - line);
199
1.51k
    return_val_if_fail (lexer->tok.field.name != NULL, false);
200
1.51k
    lexer->tok.field.value = strndup (value, end - value);
201
1.51k
    if (lexer->tok.field.value == NULL) {
202
0
      free (lexer->tok.field.name);
203
0
      return_val_if_reached (false);
204
0
    }
205
1.51k
    lexer->tok_type = TOK_FIELD;
206
1.51k
    return true;
207
1.51k
  }
208
209
133
  return false;
210
3.29k
}
211
212
void
213
p11_lexer_done (p11_lexer *lexer)
214
470
{
215
470
  return_if_fail (lexer != NULL);
216
470
  clear_state (lexer);
217
470
  free (lexer->filename);
218
470
  memset (lexer, 0, sizeof (p11_lexer));
219
470
}
220
221
void
222
p11_lexer_msg (p11_lexer *lexer,
223
               const char *msg)
224
337
{
225
337
  return_if_fail (lexer != NULL);
226
227
337
  if (lexer->complained)
228
0
    return;
229
230
337
  switch (lexer->tok_type) {
231
0
  case TOK_FIELD:
232
0
    p11_message ("%s:%zu: %s: %s", lexer->filename, lexer->line,
233
0
                 lexer->tok.field.name, msg);
234
0
    break;
235
0
  case TOK_SECTION:
236
0
    p11_message ("%s:%zu: [%s]: %s", lexer->filename, lexer->line,
237
0
                 lexer->tok.section.name, msg);
238
0
    break;
239
0
  case TOK_PEM:
240
0
    p11_message ("%s:%zu: BEGIN ...: %s", lexer->filename,
241
0
           lexer->line, msg);
242
0
    break;
243
337
  default:
244
337
    p11_message ("%s:%zu: %s", lexer->filename, lexer->line, msg);
245
337
    break;
246
337
  }
247
248
337
  lexer->complained = true;
249
337
}