File: kv-scanner.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (345 lines) | stat: -rw-r--r-- 8,344 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * Copyright (c) 2015-2017 Balabit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "kv-scanner.h"
#include "str-repr/decode.h"
#include "str-repr/encode.h"
#include "scratch-buffers.h"
#include <string.h>

static inline gboolean
_is_valid_key_character(gchar c)
{
  return (c >= 'a' && c <= 'z') ||
         (c >= 'A' && c <= 'Z') ||
         (c >= '0' && c <= '9') ||
         (c == '_') ||
         (c == '.') ||
         (c == '-');
}

static inline const gchar *
_locate_separator(KVScanner *self, const gchar *start)
{
  return strchr(start, self->value_separator);
}

static inline void
_locate_start_of_key(KVScanner *self, const gchar *end_of_key, const gchar **start_of_key)
{
  const gchar *input = &self->input[self->input_pos];
  const gchar *cur;

  cur = end_of_key;
  while (cur > input && self->is_valid_key_character(*(cur - 1)))
    cur--;
  *start_of_key = cur;
}

static inline void
_locate_end_of_key(KVScanner *self, const gchar *separator, const gchar **end_of_key)
{
  const gchar *input = &self->input[self->input_pos];
  const gchar *cur;

  /* this function locates the character pointing right next to the end of
   * the key, e.g. with this input
   *   foo   = bar
   *
   * it would start with the '=' sign and skip spaces backwards, to locate
   * the space right next to "foo" */

  cur = separator;
  while (cur > input && (*(cur - 1)) == ' ')
    cur--;
  *end_of_key = cur;
}

static inline gboolean
_extract_key_from_positions(KVScanner *self, const gchar *start_of_key, const gchar *end_of_key)
{
  gint len = end_of_key - start_of_key;

  if (len >= 1)
    {
      g_string_assign_len(self->key, start_of_key, len);
      return TRUE;
    }
  return FALSE;
}

static inline void
_extract_stray_word(KVScanner *self, const gchar *stray_word, gssize len)
{
  if (len < 0)
    len = strlen(stray_word);
  if (self->stray_words && len > 0)
    {
      while (len > 0 && stray_word[len - 1] == ' ')
        len--;
      while (len > 0 && stray_word[0] == ' ')
        {
          stray_word++;
          len--;
        }
      if (len > 0)
        {
          if (self->stray_words->len)
            g_string_append_c(self->stray_words, ',');

          str_repr_encode_append(self->stray_words, stray_word, len, ",");
        }
    }
}

static gboolean
_should_stop(KVScanner *self)
{
  const gchar *input = &self->input[self->input_pos];
  return *input == self->stop_char;
}

static gboolean
_extract_key(KVScanner *self)
{
  const gchar *input = &self->input[self->input_pos];
  const gchar *start_of_key, *end_of_key;
  const gchar *separator;

  separator = _locate_separator(self, input);
  while (separator)
    {
      _locate_end_of_key(self, separator, &end_of_key);
      _locate_start_of_key(self, end_of_key, &start_of_key);

      if (_extract_key_from_positions(self, start_of_key, end_of_key))
        {
          _extract_stray_word(self, input, start_of_key - input);
          self->input_pos = separator - self->input + 1;
          return TRUE;
        }
      separator = _locate_separator(self, separator + 1);
    }
  _extract_stray_word(self, input, -1);
  return FALSE;
}

static gboolean
_is_quoted(const gchar *input)
{
  return *input == '\'' || *input == '\"';
}

static gboolean
_key_follows(KVScanner *self, const gchar *cur)
{
  const gchar *key = cur;

  while (self->is_valid_key_character(*key))
    key++;

  while (*key == ' ')
    key++;
  return (key != cur) && (*key == self->value_separator);
}

static inline void
_skip_spaces(const gchar **input)
{
  const gchar *cur = *input;

  while (*cur == ' ')
    cur++;
  *input = cur;
}

static inline gboolean
_end_of_string(const gchar *cur)
{
  return *cur == 0;
}

static inline gboolean
_pair_separator(KVScanner *self, const gchar *cur, const gchar **new_cur)
{
  if (self->pair_separator && (strncmp(cur, self->pair_separator, self->pair_separator_len) == 0))
    {
      *new_cur = cur + self->pair_separator_len;
      return TRUE;
    }
  return FALSE;
}

static inline gboolean
_pair_separator_starts_with_a_space(KVScanner *self)
{
  return (self->pair_separator && self->pair_separator[0] == ' ');
}

static gboolean
_match_delimiter(const gchar *cur, const gchar **new_cur, gpointer user_data)
{
  KVScanner *self = (gpointer) user_data;
  gboolean result = FALSE;

  if (!self->value_was_quoted &&
      *cur == ' ')
    {
      if (_pair_separator_starts_with_a_space(self) &&
          _pair_separator(self, cur, new_cur))
        {
          result = TRUE;
        }
      else
        {
          _skip_spaces(&cur);

          if (_end_of_string(cur) ||
              _key_follows(self, cur))
            {
              *new_cur = cur;
              result = TRUE;
            }
          else if (_pair_separator(self, cur, new_cur))
            {
              result = TRUE;
            }
        }
    }
  else if (*cur == ' ')
    {
      result = TRUE;
      *new_cur = cur + 1;
    }
  else if (*cur == self->stop_char)
    {
      result = TRUE;
      *new_cur = cur;
    }
  else
    {
      result = _pair_separator(self, cur, new_cur);
    }
  return result;
}

static inline void
_skip_initial_spaces(KVScanner *self)
{
  const gchar *input = &self->input[self->input_pos];
  const gchar *end;

  while (*input == ' ' && !_match_delimiter(input, &end, self))
    input++;
  self->input_pos = input - self->input;
}

static inline void
_decode_value(KVScanner *self)
{
  const gchar *input = &self->input[self->input_pos];
  const gchar *end;
  StrReprDecodeOptions options =
  {
    .match_delimiter = _match_delimiter,
    .match_delimiter_data = self,
    .delimiter_chars = { ' ', self->pair_separator[0], self->stop_char },
  };

  self->value_was_quoted = _is_quoted(input);
  if (str_repr_decode_with_options(self->value, input, &end, &options))
    {
      self->input_pos = end - self->input;
    }
  else
    {
      /* quotation error, set was_quoted to FALSE */
      self->value_was_quoted = FALSE;
    }
}

static void
_extract_optional_annotation(KVScanner *self)
{
  if (self->extract_annotation)
    self->extract_annotation(self);
}

static void
_extract_value(KVScanner *self)
{
  self->value_was_quoted = FALSE;
  _skip_initial_spaces(self);
  _decode_value(self);
}

static inline void
_transform_value(KVScanner *self)
{
  if (self->transform_value)
    {
      g_string_truncate(self->decoded_value, 0);
      if (self->transform_value(self))
        g_string_assign_len(self->value, self->decoded_value->str, self->decoded_value->len);
    }
}

gboolean
kv_scanner_scan_next(KVScanner *s)
{
  KVScanner *self = (KVScanner *)s;

  if (_should_stop(self))
    return FALSE;

  if (!_extract_key(self))
    return FALSE;

  _extract_optional_annotation(self);

  _extract_value(self);
  _transform_value(s);

  return TRUE;
}

void
kv_scanner_deinit(KVScanner *self)
{
}

void
kv_scanner_init(KVScanner *self, gchar value_separator, const gchar *pair_separator,
                gboolean extract_stray_words)
{
  memset(self, 0, sizeof(*self));
  self->key = scratch_buffers_alloc();
  self->value = scratch_buffers_alloc();
  self->decoded_value = scratch_buffers_alloc();
  if (extract_stray_words)
    self->stray_words = scratch_buffers_alloc();
  self->value_separator = value_separator;
  self->pair_separator = pair_separator ? : ", ";
  self->pair_separator_len = strlen(self->pair_separator);
  self->is_valid_key_character = _is_valid_key_character;
  self->stop_char = 0;
}