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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <platform.h>
#include <alloc.h>
#include <logging.h>
#include <misc_lib.h>
#include <json-yaml.h>
#include <json-priv.h>
JsonParseError JsonParseYamlFile(const char *path, size_t size_max, JsonElement **json_out)
{
return JsonParseAnyFile(path, size_max, json_out, true);
}
#ifdef HAVE_LIBYAML
// macros and some of the parse guesses follow https://php-yaml.googlecode.com/svn-history/trunk/parse.c
#define JSON_YAML_SCALAR_TAG_IS(tag, name) \
!strcmp((const char *)tag, name)
#define JSON_YAML_IS_NOT_IMPLICIT_AND_TAG_IS(event, tag, name) \
(!event->data.scalar.quoted_implicit && !event->data.scalar.plain_implicit && JSON_YAML_SCALAR_TAG_IS(tag, name))
#define JSON_YAML_IS_NOT_QUOTED_OR_TAG_IS(event, tag, name) \
(!event->data.scalar.quoted_implicit && (event->data.scalar.plain_implicit || JSON_YAML_SCALAR_TAG_IS(tag, name)))
static JsonElement* JsonParseYamlScalarValue(yaml_event_t *event)
{
assert(event != NULL);
assert(event->type == YAML_SCALAR_EVENT);
const char *tag = (const char *) event->data.scalar.tag;
const char *value = (const char *) event->data.scalar.value;
size_t length = event->data.scalar.length;
if (tag == NULL)
{
tag = YAML_DEFAULT_SCALAR_TAG;
}
if (JSON_YAML_SCALAR_TAG_IS(tag, YAML_NULL_TAG) ||
event->data.scalar.plain_implicit)
{
if ((length == 1 && *value == '~') || length == 0 ||
!strcmp("NULL", value) || !strcmp("Null", value) ||
!strcmp("null", value))
{
return JsonNullCreate();
}
}
if (JSON_YAML_IS_NOT_QUOTED_OR_TAG_IS(event, tag, YAML_BOOL_TAG))
{
// see http://yaml.org/type/bool.html
if ((length == 1 && (*value == 'Y' || *value == 'y')) ||
!strcmp("YES", value) || !strcmp("Yes", value) ||
!strcmp("yes", value) || !strcmp("TRUE", value) ||
!strcmp("True", value) || !strcmp("true", value) ||
!strcmp("ON", value) || !strcmp("On", value) || !strcmp("on", value))
{
return JsonBoolCreate(true);
}
if ((length == 1 && (*value == 'N' || *value == 'n')) ||
!strcmp("NO", value) || !strcmp("No", value) || !strcmp("no", value) ||
!strcmp("FALSE", value) || !strcmp("False", value) ||
!strcmp("false", value) || !strcmp("OFF", value) ||
!strcmp("Off", value) || !strcmp("off", value))
{
return JsonBoolCreate(false);
}
}
else if (JSON_YAML_IS_NOT_IMPLICIT_AND_TAG_IS(event, tag, YAML_BOOL_TAG))
{
if (length == 0 || (length == 1 && *value == '0'))
{
return JsonBoolCreate(false);
}
else
{
return JsonBoolCreate(true);
}
}
/* check for numeric (int or float) */
if (!event->data.scalar.quoted_implicit &&
(event->data.scalar.plain_implicit ||
JSON_YAML_SCALAR_TAG_IS(tag, YAML_INT_TAG) ||
JSON_YAML_SCALAR_TAG_IS(tag, YAML_FLOAT_TAG)))
{
JsonElement *tobuild;
const char *end_of_num_part = value;
if (JSON_PARSE_OK == JsonParseAsNumber(&end_of_num_part, &tobuild))
{
return tobuild;
}
}
if (strcmp(tag, YAML_TIMESTAMP_TAG) == 0)
{
// what else could we do, return a epoch time?
Log(LOG_LEVEL_VERBOSE, "YAML parse: treating timestamp value '%s' as a string", value);
return JsonStringCreate(value);
}
if (JSON_YAML_SCALAR_TAG_IS(tag, YAML_STR_TAG))
{
return JsonStringCreate(value);
}
Log(LOG_LEVEL_VERBOSE, "YAML parse: unhandled scalar tag %s, returning as string", tag);
return JsonStringCreate(value);
}
static void JsonParseYamlData(yaml_parser_t *parser, JsonElement *element, const int depth)
{
yaml_event_t event;
char* key = NULL;
Log(LOG_LEVEL_DEBUG, "YAML parse: entering JsonParseYamlStore");
while (1)
{
yaml_parser_parse(parser, &event);
Log(LOG_LEVEL_DEBUG, "YAML parse: event of type %d arrived with depth %d, key %s",
event.type,
depth,
key == NULL ? "[NULL]" : key);
// Parse value either as a new leaf in the mapping
// or as a leaf value (one of them, in case it's a sequence)
if (event.type == YAML_SCALAR_EVENT)
{
Log(LOG_LEVEL_DEBUG, "YAML parse: scalar event, value '%s'", event.data.scalar.value);
if (JsonGetElementType(element) == JSON_ELEMENT_TYPE_CONTAINER)
{
if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_OBJECT)
{
if (key == NULL)
{
// save key
key = xstrdup(event.data.scalar.value);
}
else
{
JsonObjectAppendElement(element, key, JsonParseYamlScalarValue(&event));
// clear key
free(key);
key = NULL;
}
}
else if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_ARRAY)
{
JsonArrayAppendElement(element, JsonParseYamlScalarValue(&event));
// clear key
free(key);
key = NULL;
}
else
{
ProgrammingError("YAML Parse: scalar event received inside an unknown JSON container type");
}
}
else
{
ProgrammingError("YAML Parse: scalar event received inside a non-container JSON element");
}
}
else if (event.type == YAML_SEQUENCE_START_EVENT)
{
Log(LOG_LEVEL_DEBUG, "YAML parse: starting sequence");
JsonElement *arr = JsonArrayCreate(DEFAULT_CONTAINER_CAPACITY);
if (JsonGetElementType(element) == JSON_ELEMENT_TYPE_CONTAINER)
{
if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_OBJECT)
{
if (key)
{
JsonObjectAppendElement(element, key, arr);
JsonParseYamlData(parser, arr, depth+1);
// clear key
free(key);
key = NULL;
}
else
{
ProgrammingError("YAML Parse: Unexpected sequence start event inside a container without a key");
}
}
else if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_ARRAY)
{
JsonArrayAppendArray(element, arr);
JsonParseYamlData(parser, arr, depth+1);
// clear key
free(key);
key = NULL;
}
else
{
ProgrammingError("YAML Parse: Unexpected sequence start event inside a non-container");
}
}
}
else if (event.type == YAML_SEQUENCE_END_EVENT)
{
Log(LOG_LEVEL_DEBUG, "YAML parse: ending sequence");
if (JsonGetElementType(element) == JSON_ELEMENT_TYPE_CONTAINER)
{
if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_ARRAY)
{
// finished with this array, return up
break;
}
else
{
ProgrammingError("YAML Parse: Unexpected sequence end event inside a non-array container");
}
}
else
{
ProgrammingError("YAML Parse: Unexpected sequence end event inside a non-container");
}
}
else if (event.type == YAML_MAPPING_START_EVENT)
{
Log(LOG_LEVEL_DEBUG, "YAML parse: starting mapping");
JsonElement *obj = JsonObjectCreate(DEFAULT_CONTAINER_CAPACITY);
if (JsonGetElementType(element) == JSON_ELEMENT_TYPE_CONTAINER)
{
if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_OBJECT)
{
if (key)
{
JsonObjectAppendElement(element, key, obj);
JsonParseYamlData(parser, obj, depth+1);
// clear key
free(key);
key = NULL;
}
else
{
ProgrammingError("YAML Parse: Unexpected mapping start event inside a container without a key");
}
}
else if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_ARRAY)
{
JsonArrayAppendObject(element, obj);
JsonParseYamlData(parser, obj, depth+1);
// clear key
free(key);
key = NULL;
}
else
{
ProgrammingError("YAML Parse: Unexpected mapping start event inside a non-container");
}
}
}
else if (event.type == YAML_MAPPING_END_EVENT)
{
Log(LOG_LEVEL_DEBUG, "YAML parse: ending mapping");
if (JsonGetElementType(element) == JSON_ELEMENT_TYPE_CONTAINER)
{
if (JsonGetContainerType(element) == JSON_CONTAINER_TYPE_OBJECT)
{
// finished with this object, return up
break;
}
else
{
ProgrammingError("YAML Parse: Unexpected mapping end event inside a non-object container");
}
}
else
{
ProgrammingError("YAML Parse: Unexpected mapping end event inside a non-container");
}
}
else if (event.type == YAML_STREAM_END_EVENT)
{
Log(LOG_LEVEL_DEBUG, "YAML parse: ending stream");
break;
}
else if (event.type == YAML_NO_EVENT)
{
Log(LOG_LEVEL_DEBUG, "YAML parse: NO_EVENT");
break; // NO_EVENT doesn't need to be deleted
}
else
{
// ignore other events
}
yaml_event_delete(&event);
Log(LOG_LEVEL_DEBUG, "YAML parse: running inner loop");
}
if (key)
{
free(key);
}
Log(LOG_LEVEL_DEBUG, "YAML parse: exiting JsonParseYamlData");
}
JsonParseError JsonParseYamlString(const char **data, JsonElement **json_out)
{
assert(data && *data);
if (data == NULL || *data == NULL)
{
return JSON_PARSE_ERROR_NO_DATA;
}
yaml_parser_t parser;
if(!yaml_parser_initialize(&parser))
{
return JSON_PARSE_ERROR_LIBYAML_FAILURE;
}
yaml_parser_set_input_string(&parser, *data, strlen(*data));
JsonElement *holder = JsonArrayCreate(1);
JsonParseYamlData(&parser, holder, 0);
*json_out = JsonCopy(JsonAt(holder, 0));
JsonDestroy(holder);
yaml_parser_delete(&parser);
return JSON_PARSE_OK;
}
#else // !HAVE_LIBYAML
JsonParseError JsonParseYamlString(ARG_UNUSED const char **data,
ARG_UNUSED JsonElement **json_out)
{
return JSON_PARSE_ERROR_NO_LIBYAML;
}
#endif
|