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
|
/*
* Copyright (c) 2014 Balabit
* Copyright (c) 2014 Balazs Scheidler <bazsi@balabit.hu>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; 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 "dot-notation.h"
#include <stdlib.h>
typedef struct _JSONDotNotationElem
{
gboolean used;
enum
{
JS_MEMBER_REF,
JS_ARRAY_REF
} type;
union
{
struct
{
gchar *name;
} member_ref;
struct
{
gint index;
} array_ref;
};
} JSONDotNotationElem;
typedef struct JSONDotNotation
{
JSONDotNotationElem *compiled_elems;
} JSONDotNotation;
static void _free_compiled_dot_notation(JSONDotNotationElem *compiled);
static gboolean
_compile_dot_notation_array_ref(const gchar *level, JSONDotNotationElem *elem)
{
const gchar *p = level;
gint index_ = 0;
g_assert(*p == '[');
p++;
index_ = strtol(p, (gchar **) &p, 10);
if (*p != ']')
return FALSE;
if (index_ < 0)
return FALSE;
p++;
if (*p != 0)
return FALSE;
elem->type = JS_ARRAY_REF;
elem->array_ref.index = index_;
return TRUE;
}
static gboolean
_compile_dot_notation_member_ref(const gchar *level, JSONDotNotationElem *elem)
{
const gchar *p = level;
if (!g_ascii_isprint(*p) || strchr(".[]", *p) != NULL)
return FALSE;
while (g_ascii_isprint(*p) && strchr(".[]", *p) == NULL)
p++;
if (*p != 0)
return FALSE;
elem->type = JS_MEMBER_REF;
elem->member_ref.name = g_strdup(level);
return TRUE;
}
static gboolean
_compile_dot_notation_elem(const gchar *level, GArray *compiled_array)
{
JSONDotNotationElem elem;
memset(&elem, 0, sizeof(elem));
if (level[0] == '[')
{
if (!_compile_dot_notation_array_ref(level, &elem))
return FALSE;
}
else
{
if (!_compile_dot_notation_member_ref(level, &elem))
return FALSE;
}
elem.used = TRUE;
g_array_append_val(compiled_array, elem);
return TRUE;
}
static gchar **
_split_dot_notation(const gchar *dot_notation)
{
GPtrArray *array;
const gchar *p, *last;
array = g_ptr_array_new();
p = last = dot_notation;
while (*p)
{
if (*p == '.')
{
g_ptr_array_add(array, g_strndup(last, p - last));
p++;
last = p;
}
else if (*p == '[')
{
g_ptr_array_add(array, g_strndup(last, p - last));
last = p;
p++;
}
else
{
p++;
}
}
g_ptr_array_add(array, g_strndup(last, p - last));
g_ptr_array_add(array, NULL);
return (gchar **) g_ptr_array_free(array, FALSE);
}
static JSONDotNotationElem *
_compile_dot_notation(const gchar *dot_notation)
{
GArray *compiled;
gchar **levels;
gint i;
levels = _split_dot_notation(dot_notation);
compiled = g_array_new(TRUE, TRUE, sizeof(JSONDotNotationElem));
for (i = 0; levels[i]; i++)
{
if (i == 0 && strlen(levels[i]) == 0)
continue;
if (!_compile_dot_notation_elem(levels[i], compiled))
goto error;
}
g_strfreev(levels);
return (JSONDotNotationElem *) g_array_free(compiled, FALSE);
error:
g_strfreev(levels);
_free_compiled_dot_notation((JSONDotNotationElem *) g_array_free(compiled, FALSE));
return NULL;
}
static void
_free_compiled_dot_notation(JSONDotNotationElem *compiled)
{
gint i;
i = -1;
for (i = 0; compiled && compiled[i].used; i++)
{
if (compiled[i].type == JS_MEMBER_REF)
g_free(compiled[i].member_ref.name);
}
g_free(compiled);
}
static gboolean
json_dot_notation_compile(JSONDotNotation *self, const gchar *dot_notation)
{
if (dot_notation[0] == 0)
{
self->compiled_elems = NULL;
return TRUE;
}
self->compiled_elems = _compile_dot_notation(dot_notation);
return self->compiled_elems != NULL;
}
#ifdef JSON_C_VERSION
struct json_object *
_json_object_object_get(struct json_object *obj, const char *key)
{
struct json_object *value;
json_object_object_get_ex(obj, key, &value);
return value;
}
#else
#define _json_object_object_get json_object_object_get
#endif
struct json_object *
json_dot_notation_eval(JSONDotNotation *self, struct json_object *jso)
{
JSONDotNotationElem *compiled;
gint i;
if (!jso)
goto error;
compiled = self->compiled_elems;
for (i = 0; compiled && compiled[i].used; i++)
{
if (1)
{
const gchar *name;
if (compiled[i].type == JS_MEMBER_REF)
{
name = compiled[i].member_ref.name;
if (!json_object_is_type(jso, json_type_object))
{
jso = NULL;
goto error;
}
jso = _json_object_object_get(jso, name);
}
else if (compiled[i].type == JS_ARRAY_REF)
{
if (!json_object_is_type(jso, json_type_array) ||
compiled[i].array_ref.index >= json_object_array_length(jso))
{
jso = NULL;
goto error;
}
jso = json_object_array_get_idx(jso, compiled[i].array_ref.index);
}
}
}
error:
return jso;
}
JSONDotNotation *
json_dot_notation_new(void)
{
JSONDotNotation *self = g_new0(JSONDotNotation, 1);
return self;
}
void
json_dot_notation_free(JSONDotNotation *self)
{
_free_compiled_dot_notation(self->compiled_elems);
g_free(self);
}
struct json_object *
json_extract(struct json_object *jso, const gchar *dot_notation)
{
JSONDotNotation *self = json_dot_notation_new();
if (!json_dot_notation_compile(self, dot_notation))
{
jso = NULL;
goto error;
}
jso = json_dot_notation_eval(self, jso);
error:
json_dot_notation_free(self);
return jso;
}
|