File: decode.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 (281 lines) | stat: -rw-r--r-- 7,080 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
/*
 * Copyright (c) 2015-2016 Balabit
 * Copyright (c) 2015-2016 Balázs Scheidler
 *
 * 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 "str-repr/decode.h"

#include <string.h>

enum
{
  KV_QUOTE_INITIAL = 0,
  KV_QUOTE_STRING,
  KV_QUOTE_BACKSLASH,
  KV_QUOTE_FINISH,
  KV_EXPECT_DELIMITER,
  KV_QUOTE_ERROR,
  KV_UNQUOTED_CHARACTERS,
  KV_FINISH_SUCCESS,
  KV_FINISH_FAILURE,
};

static void
_decode_backslash_escape(GString *value, gchar quote_char, gchar ch)
{
  gchar control;
  switch (ch)
    {
    case 'b':
      control = '\b';
      break;
    case 'f':
      control = '\f';
      break;
    case 'n':
      control = '\n';
      break;
    case 'r':
      control = '\r';
      break;
    case 't':
      control = '\t';
      break;
    case '\\':
      control = '\\';
      break;
    default:
      if (quote_char != ch)
        g_string_append_c(value, '\\');
      control = ch;
      break;
    }
  g_string_append_c(value, control);
}

typedef struct _StrReprDecodeState
{
  GString *value;
  const gchar *cur;
  gchar quote_char;
  const StrReprDecodeOptions *options;
} StrReprDecodeState;

static gboolean
_invoke_match_delimiter(StrReprDecodeState *state, const gchar **new_cur)
{
  const StrReprDecodeOptions *options = state->options;

  if (options->delimiter_chars[0])
    {
      if (*state->cur == options->delimiter_chars[0] || *state->cur == options->delimiter_chars[1]
          || *state->cur == options->delimiter_chars[2])
        {
          if (options->match_delimiter)
            return options->match_delimiter(state->cur, new_cur, options->match_delimiter_data);
          else
            {
              *new_cur = state->cur + 1;
              return TRUE;
            }
        }
    }
  else
    {
      if (options->match_delimiter)
        return options->match_delimiter(state->cur, new_cur, options->match_delimiter_data);
    }
  return FALSE;
}

static gboolean
_match_and_skip_delimiter(StrReprDecodeState *state)
{
  const gchar *new_cur;

  if (_invoke_match_delimiter(state, &new_cur))
    {
      state->cur = new_cur;
      return TRUE;
    }
  return FALSE;
}


static gint
_process_initial_character(StrReprDecodeState *state)
{
  if (_match_and_skip_delimiter(state))
    {
      return KV_FINISH_SUCCESS;
    }
  else if (*state->cur == '\"' || *state->cur == '\'')
    {
      state->quote_char = *state->cur;
      return KV_QUOTE_STRING;
    }
  else
    {
      g_string_append_c(state->value, *state->cur);
      return KV_UNQUOTED_CHARACTERS;
    }
}

static gint
_process_quoted_string_characters(StrReprDecodeState *state)
{
  if (*state->cur == state->quote_char)
    return KV_EXPECT_DELIMITER;
  else if (*state->cur == '\\')
    return KV_QUOTE_BACKSLASH;

  g_string_append_c(state->value, *state->cur);
  return KV_QUOTE_STRING;
}

static gint
_process_backslash_escaped_character_in_strings(StrReprDecodeState *state)
{
  _decode_backslash_escape(state->value, state->quote_char, *state->cur);
  return KV_QUOTE_STRING;
}

static gint
_process_delimiter_characters_after_a_quoted_string(StrReprDecodeState *state)
{
  if (_match_and_skip_delimiter(state))
    return KV_FINISH_SUCCESS;
  return KV_QUOTE_ERROR;
}

static gint
_process_junk_after_closing_quote(StrReprDecodeState *state)
{
  if (_match_and_skip_delimiter(state))
    return KV_FINISH_FAILURE;
  return KV_QUOTE_ERROR;
}

static gint
_process_unquoted_characters(StrReprDecodeState *state)
{
  if (_match_and_skip_delimiter(state))
    return KV_FINISH_SUCCESS;
  g_string_append_c(state->value, *state->cur);
  return KV_UNQUOTED_CHARACTERS;
}

static gboolean
_is_an_ending_string_acceptable_in_this_state(gint quote_state)
{
  return quote_state == KV_QUOTE_INITIAL ||
         quote_state == KV_EXPECT_DELIMITER ||
         quote_state == KV_UNQUOTED_CHARACTERS ||
         quote_state == KV_FINISH_SUCCESS;
}

static gboolean
_decode(StrReprDecodeState *state)
{
  gint quote_state = KV_QUOTE_INITIAL;

  for (; *state->cur; state->cur++)
    {
      switch (quote_state)
        {
        case KV_QUOTE_INITIAL:
          quote_state = _process_initial_character(state);
          break;
        case KV_QUOTE_STRING:
          quote_state = _process_quoted_string_characters(state);
          break;
        case KV_QUOTE_BACKSLASH:
          quote_state = _process_backslash_escaped_character_in_strings(state);
          break;
        case KV_EXPECT_DELIMITER:
          quote_state = _process_delimiter_characters_after_a_quoted_string(state);
          break;
        case KV_QUOTE_ERROR:
          quote_state = _process_junk_after_closing_quote(state);
          break;
        case KV_UNQUOTED_CHARACTERS:
          quote_state = _process_unquoted_characters(state);
          break;
        default:
          break;
        }
      if (quote_state == KV_FINISH_SUCCESS || quote_state == KV_FINISH_FAILURE)
        break;
    }
  if (_is_an_ending_string_acceptable_in_this_state(quote_state))
    return TRUE;
  return FALSE;
}

gboolean
str_repr_decode_append_with_options(GString *value, const gchar *input, const gchar **end,
                                    const StrReprDecodeOptions *options)
{
  StrReprDecodeState state =
  {
    .value = value,
    .cur = input,
    .quote_char = 0,
    .options = options,
  };
  gsize initial_len = value->len;

  gboolean success = _decode(&state);
  *end = state.cur;

  if (!success)
    {
      g_string_truncate(value, initial_len);
      g_string_append_len(value, input, state.cur - input);
      return FALSE;
    }
  return TRUE;
}

gboolean
str_repr_decode_append(GString *value, const gchar *input, const gchar **end)
{
  StrReprDecodeOptions options =
  {
    0,
    .delimiter_chars = " ",
  };
  return str_repr_decode_append_with_options(value, input, end, &options);
}

gboolean
str_repr_decode(GString *value, const gchar *input, const gchar **end)
{
  g_string_truncate(value, 0);
  return str_repr_decode_append(value, input, end);
}

gboolean
str_repr_decode_with_options(GString *value, const gchar *input, const gchar **end, const StrReprDecodeOptions *options)
{
  g_string_truncate(value, 0);
  return str_repr_decode_append_with_options(value, input, end, options);
}