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
|
/* Copyright (c) 2013, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "rcl.h"
#include "rcl_internal.h"
/**
* @file rcl_util.c
* Utilities for rcl parsing
*/
static void
rspamd_cl_obj_free_internal (rspamd_cl_object_t *obj, gboolean allow_rec)
{
rspamd_cl_object_t *sub, *tmp;
while (obj != NULL) {
if (obj->key != NULL) {
g_free (obj->key);
}
if (obj->type == RSPAMD_CL_STRING) {
g_free (obj->value.sv);
}
else if (obj->type == RSPAMD_CL_ARRAY) {
sub = obj->value.ov;
while (sub != NULL) {
tmp = sub->next;
rspamd_cl_obj_free_internal (sub, FALSE);
sub = tmp;
}
}
else if (obj->type == RSPAMD_CL_OBJECT) {
HASH_ITER (hh, obj->value.ov, sub, tmp) {
HASH_DELETE (hh, obj->value.ov, sub);
rspamd_cl_obj_free_internal (sub, TRUE);
}
}
tmp = obj->next;
g_slice_free1 (sizeof (rspamd_cl_object_t), obj);
obj = tmp;
if (!allow_rec) {
break;
}
}
}
void
rspamd_cl_obj_free (rspamd_cl_object_t *obj)
{
rspamd_cl_obj_free_internal (obj, TRUE);
}
void
rspamd_cl_unescape_json_string (gchar *str)
{
gchar *t = str, *h = str;
gint i, uval;
/* t is target (tortoise), h is source (hare) */
while (*h != '\0') {
if (*h == '\\') {
h ++;
switch (*h) {
case 'n':
*t++ = '\n';
break;
case 'r':
*t++ = '\r';
break;
case 'b':
*t++ = '\b';
break;
case 't':
*t++ = '\t';
break;
case 'f':
*t++ = '\f';
break;
case '\\':
*t++ = '\\';
break;
case '"':
*t++ = '"';
break;
case 'u':
/* Unicode escape */
uval = 0;
for (i = 0; i < 4; i++) {
uval <<= 4;
if (g_ascii_isdigit (h[i])) {
uval += h[i] - '0';
}
else if (h[i] >= 'a' && h[i] <= 'f') {
uval += h[i] - 'a' + 10;
}
else if (h[i] >= 'A' && h[i] <= 'F') {
uval += h[i] - 'A' + 10;
}
}
h += 3;
/* Encode */
if(uval < 0x80) {
t[0] = (char)uval;
t ++;
}
else if(uval < 0x800) {
t[0] = 0xC0 + ((uval & 0x7C0) >> 6);
t[1] = 0x80 + ((uval & 0x03F));
t += 2;
}
else if(uval < 0x10000) {
t[0] = 0xE0 + ((uval & 0xF000) >> 12);
t[1] = 0x80 + ((uval & 0x0FC0) >> 6);
t[2] = 0x80 + ((uval & 0x003F));
t += 3;
}
else if(uval <= 0x10FFFF) {
t[0] = 0xF0 + ((uval & 0x1C0000) >> 18);
t[1] = 0x80 + ((uval & 0x03F000) >> 12);
t[2] = 0x80 + ((uval & 0x000FC0) >> 6);
t[3] = 0x80 + ((uval & 0x00003F));
t += 4;
}
else {
*t++ = '?';
}
break;
default:
*t++ = '?';
break;
}
h ++;
}
else {
*t++ = *h++;
}
}
}
rspamd_cl_object_t*
rspamd_cl_parser_get_object (struct rspamd_cl_parser *parser, GError **err)
{
if (parser->state != RSPAMD_RCL_STATE_INIT && parser->state != RSPAMD_RCL_STATE_ERROR) {
return parser->top_obj;
}
return NULL;
}
void
rspamd_cl_parser_free (struct rspamd_cl_parser *parser)
{
struct rspamd_cl_stack *stack, *stmp;
struct rspamd_cl_macro *macro, *mtmp;
struct rspamd_cl_chunk *chunk, *ctmp;
if (parser->top_obj != NULL) {
rspamd_cl_obj_free (parser->top_obj);
}
LL_FOREACH_SAFE (parser->stack, stack, stmp) {
g_slice_free1 (sizeof (struct rspamd_cl_stack), stack);
}
HASH_ITER (hh, parser->macroes, macro, mtmp) {
g_slice_free1 (sizeof (struct rspamd_cl_macro), macro);
}
LL_FOREACH_SAFE (parser->chunks, chunk, ctmp) {
g_slice_free1 (sizeof (struct rspamd_cl_chunk), chunk);
}
g_slice_free1 (sizeof (struct rspamd_cl_parser), parser);
}
|