Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file parser_lyb.c |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief LYB data parser for libyang |
| 5 | * |
| 6 | * Copyright (c) 2020 CESNET, z.s.p.o. |
| 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #include "lyb.h" |
| 16 | |
| 17 | #include <assert.h> |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame^] | 18 | #include <stdint.h> |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "common.h" |
| 24 | #include "compat.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame^] | 25 | #include "context.h" |
| 26 | #include "dict.h" |
| 27 | #include "log.h" |
| 28 | #include "set.h" |
| 29 | #include "tree.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 30 | #include "tree_data_internal.h" |
| 31 | #include "tree_schema.h" |
| 32 | #include "validation.h" |
| 33 | |
| 34 | /** |
| 35 | * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned. |
| 36 | * |
| 37 | * @param[in] buf Destination buffer. |
| 38 | * @param[in] count Number of bytes to read. |
| 39 | * @param[in] lybctx LYB context. |
| 40 | */ |
| 41 | static void |
| 42 | lyb_read(uint8_t *buf, size_t count, struct lyd_lyb_ctx *lybctx) |
| 43 | { |
| 44 | int parsed = 0; |
| 45 | LY_ARRAY_SIZE_TYPE u; |
| 46 | struct lyd_lyb_subtree *empty; |
| 47 | size_t to_read; |
| 48 | uint8_t meta_buf[LYB_META_BYTES]; |
| 49 | |
| 50 | assert(lybctx); |
| 51 | |
| 52 | while (1) { |
| 53 | /* check for fully-read (empty) data chunks */ |
| 54 | to_read = count; |
| 55 | empty = NULL; |
| 56 | LY_ARRAY_FOR(lybctx->subtrees, u) { |
| 57 | /* we want the innermost chunks resolved first, so replace previous empty chunks, |
| 58 | * also ignore chunks that are completely finished, there is nothing for us to do */ |
| 59 | if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) { |
| 60 | /* empty chunk, do not read more */ |
| 61 | to_read = lybctx->subtrees[u].written; |
| 62 | empty = &lybctx->subtrees[u]; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if (!empty && !count) { |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | /* we are actually reading some data, not just finishing another chunk */ |
| 71 | if (to_read) { |
| 72 | if (buf) { |
| 73 | memcpy(buf, lybctx->data + parsed, to_read); |
| 74 | } |
| 75 | |
| 76 | LY_ARRAY_FOR(lybctx->subtrees, u) { |
| 77 | /* decrease all written counters */ |
| 78 | lybctx->subtrees[u].written -= to_read; |
| 79 | assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX); |
| 80 | } |
| 81 | /* decrease count/buf */ |
| 82 | count -= to_read; |
| 83 | if (buf) { |
| 84 | buf += to_read; |
| 85 | } |
| 86 | |
| 87 | parsed += to_read; |
| 88 | } |
| 89 | |
| 90 | if (empty) { |
| 91 | /* read the next chunk meta information */ |
| 92 | memcpy(meta_buf, lybctx->data + parsed, LYB_META_BYTES); |
| 93 | empty->written = meta_buf[0]; |
| 94 | empty->inner_chunks = meta_buf[1]; |
| 95 | |
| 96 | /* remember whether there is a following chunk or not */ |
| 97 | empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0); |
| 98 | |
| 99 | parsed += LYB_META_BYTES; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | lybctx->byte_count += parsed; |
| 104 | lybctx->data += parsed; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @brief Read a number. |
| 109 | * |
| 110 | * @param[in] num Destination buffer. |
| 111 | * @param[in] num_size Size of @p num. |
| 112 | * @param[in] bytes Number of bytes to read. |
| 113 | * @param[in] lybctx LYB context. |
| 114 | */ |
| 115 | static void |
| 116 | lyb_read_number(void *num, size_t num_size, size_t bytes, struct lyd_lyb_ctx *lybctx) |
| 117 | { |
| 118 | uint64_t buf = 0; |
| 119 | |
| 120 | lyb_read((uint8_t *)&buf, bytes, lybctx); |
| 121 | |
| 122 | /* correct byte order */ |
| 123 | buf = le64toh(buf); |
| 124 | |
| 125 | switch (num_size) { |
| 126 | case 1: |
| 127 | *((uint8_t *)num) = buf; |
| 128 | break; |
| 129 | case 2: |
| 130 | *((uint16_t *)num) = buf; |
| 131 | break; |
| 132 | case 4: |
| 133 | *((uint32_t *)num) = buf; |
| 134 | break; |
| 135 | case 8: |
| 136 | *((uint64_t *)num) = buf; |
| 137 | break; |
| 138 | default: |
| 139 | LOGINT(lybctx->ctx); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @brief Read a string. |
| 145 | * |
| 146 | * @param[in] str Destination buffer, is allocated. |
| 147 | * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree. |
| 148 | * @param[in] lybctx LYB context. |
| 149 | * @return LY_ERR value. |
| 150 | */ |
| 151 | static LY_ERR |
| 152 | lyb_read_string(char **str, int with_length, struct lyd_lyb_ctx *lybctx) |
| 153 | { |
| 154 | int next_chunk = 0; |
| 155 | size_t len = 0, cur_len; |
| 156 | |
| 157 | *str = NULL; |
| 158 | |
| 159 | if (with_length) { |
| 160 | lyb_read_number(&len, sizeof len, 2, lybctx); |
| 161 | } else { |
| 162 | /* read until the end of this subtree */ |
| 163 | len = LYB_LAST_SUBTREE(lybctx).written; |
| 164 | if (LYB_LAST_SUBTREE(lybctx).position) { |
| 165 | next_chunk = 1; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | *str = malloc((len + 1) * sizeof **str); |
| 170 | LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM); |
| 171 | |
| 172 | lyb_read((uint8_t *)*str, len, lybctx); |
| 173 | |
| 174 | while (next_chunk) { |
| 175 | cur_len = LYB_LAST_SUBTREE(lybctx).written; |
| 176 | if (LYB_LAST_SUBTREE(lybctx).position) { |
| 177 | next_chunk = 1; |
| 178 | } else { |
| 179 | next_chunk = 0; |
| 180 | } |
| 181 | |
| 182 | *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str); |
| 183 | LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM); |
| 184 | |
| 185 | lyb_read(((uint8_t *)*str) + len, cur_len, lybctx); |
| 186 | |
| 187 | len += cur_len; |
| 188 | } |
| 189 | |
| 190 | ((char *)*str)[len] = '\0'; |
| 191 | return LY_SUCCESS; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @brief Stop the current subtree - change LYB context state. |
| 196 | * |
| 197 | * @param[in] lybctx LYB context. |
| 198 | * @return LY_ERR value. |
| 199 | */ |
| 200 | static LY_ERR |
| 201 | lyb_read_stop_subtree(struct lyd_lyb_ctx *lybctx) |
| 202 | { |
| 203 | if (LYB_LAST_SUBTREE(lybctx).written) { |
| 204 | LOGINT_RET(lybctx->ctx); |
| 205 | } |
| 206 | |
| 207 | LY_ARRAY_DECREMENT(lybctx->subtrees); |
| 208 | return LY_SUCCESS; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * @brief Start a new subtree - change LYB context state but also read the expected metadata. |
| 213 | * |
| 214 | * @param[in] lybctx LYB context. |
| 215 | * @return LY_ERR value. |
| 216 | */ |
| 217 | static LY_ERR |
| 218 | lyb_read_start_subtree(struct lyd_lyb_ctx *lybctx) |
| 219 | { |
| 220 | uint8_t meta_buf[LYB_META_BYTES]; |
| 221 | LY_ARRAY_SIZE_TYPE u; |
| 222 | |
| 223 | if (!lybctx->subtrees) { |
| 224 | u = 0; |
| 225 | } else { |
| 226 | u = LY_ARRAY_SIZE(lybctx->subtrees); |
| 227 | } |
| 228 | if (u == lybctx->subtree_size) { |
| 229 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM); |
| 230 | lybctx->subtree_size = u + LYB_SUBTREE_STEP; |
| 231 | } |
| 232 | |
| 233 | memcpy(meta_buf, lybctx->data, LYB_META_BYTES); |
| 234 | |
| 235 | LY_ARRAY_INCREMENT(lybctx->subtrees); |
| 236 | LYB_LAST_SUBTREE(lybctx).written = meta_buf[0]; |
| 237 | LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES]; |
| 238 | LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0); |
| 239 | |
| 240 | lybctx->byte_count += LYB_META_BYTES; |
| 241 | lybctx->data += LYB_META_BYTES; |
| 242 | return LY_SUCCESS; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * @brief Parse YANG model info. |
| 247 | * |
| 248 | * @param[in] lybctx LYB context. |
| 249 | * @param[out] mod Parsed module. |
| 250 | * @return LY_ERR value. |
| 251 | */ |
| 252 | static LY_ERR |
| 253 | lyb_parse_model(struct lyd_lyb_ctx *lybctx, const struct lys_module **mod) |
| 254 | { |
| 255 | LY_ERR ret = LY_SUCCESS; |
| 256 | char *mod_name = NULL, mod_rev[11]; |
| 257 | uint16_t rev; |
| 258 | |
| 259 | /* model name */ |
| 260 | ret = lyb_read_string(&mod_name, 1, lybctx); |
| 261 | LY_CHECK_GOTO(ret, cleanup); |
| 262 | |
| 263 | /* revision */ |
| 264 | lyb_read_number(&rev, sizeof rev, 2, lybctx); |
| 265 | |
| 266 | if (!mod_name[0]) { |
| 267 | /* opaq node, no module */ |
| 268 | *mod = NULL; |
| 269 | goto cleanup; |
| 270 | } |
| 271 | |
| 272 | if (rev) { |
| 273 | sprintf(mod_rev, "%04u-%02u-%02u", ((rev & 0xFE00) >> 9) + 2000, (rev & 0x01E0) >> 5, rev & 0x001Fu); |
| 274 | *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev); |
| 275 | if ((lybctx->options & LYD_OPT_LYB_MOD_UPDATE) && !(*mod)) { |
| 276 | /* try to use an updated module */ |
| 277 | *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name); |
| 278 | if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) { |
| 279 | /* not an implemented module in a newer revision */ |
| 280 | *mod = NULL; |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name); |
| 285 | } |
| 286 | /* TODO data_clb supported? |
| 287 | if (lybctx->ctx->data_clb) { |
| 288 | if (!*mod) { |
| 289 | *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data); |
| 290 | } else if (!(*mod)->implemented) { |
| 291 | *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data); |
| 292 | } |
| 293 | }*/ |
| 294 | |
| 295 | if (!*mod || !(*mod)->implemented) { |
| 296 | if (lybctx->options & LYD_OPT_STRICT) { |
| 297 | if (!*mod) { |
| 298 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".", |
| 299 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
| 300 | } else if (!(*mod)->implemented) { |
| 301 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.", |
| 302 | mod_name, rev ? "@" : "", rev ? mod_rev : ""); |
| 303 | } |
| 304 | ret = LY_EINVAL; |
| 305 | goto cleanup; |
| 306 | } |
| 307 | |
| 308 | } |
| 309 | |
| 310 | cleanup: |
| 311 | free(mod_name); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * @brief Parse YANG node metadata. |
| 317 | * |
| 318 | * @param[in] lybctx LYB context. |
| 319 | * @param[in] sparent Schema parent node. |
| 320 | * @param[out] meta Parsed metadata. |
| 321 | * @return LY_ERR value. |
| 322 | */ |
| 323 | static LY_ERR |
| 324 | lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta) |
| 325 | { |
| 326 | LY_ERR ret = LY_SUCCESS; |
| 327 | int dynamic = 0; |
| 328 | uint8_t i, count = 0; |
| 329 | char *meta_name = NULL, *meta_value = NULL; |
| 330 | const struct lys_module *mod; |
| 331 | |
| 332 | /* read number of attributes stored */ |
| 333 | lyb_read(&count, 1, lybctx); |
| 334 | |
| 335 | /* read attributes */ |
| 336 | for (i = 0; i < count; ++i) { |
| 337 | ret = lyb_read_start_subtree(lybctx); |
| 338 | LY_CHECK_GOTO(ret, cleanup); |
| 339 | |
| 340 | /* find model */ |
| 341 | ret = lyb_parse_model(lybctx, &mod); |
| 342 | LY_CHECK_GOTO(ret, cleanup); |
| 343 | |
| 344 | if (!mod) { |
| 345 | /* skip it */ |
| 346 | do { |
| 347 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx); |
| 348 | } while (LYB_LAST_SUBTREE(lybctx).written); |
| 349 | goto stop_subtree; |
| 350 | } |
| 351 | |
| 352 | /* meta name */ |
| 353 | ret = lyb_read_string(&meta_name, 1, lybctx); |
| 354 | LY_CHECK_GOTO(ret, cleanup); |
| 355 | |
| 356 | /* meta value */ |
| 357 | ret = lyb_read_string(&meta_value, 0, lybctx); |
| 358 | LY_CHECK_GOTO(ret, cleanup); |
| 359 | dynamic = 1; |
| 360 | |
| 361 | /* create metadata */ |
| 362 | ret = lyd_create_meta(NULL, meta, mod, meta_name, strlen(meta_name), meta_value, strlen(meta_value), &dynamic, |
| 363 | lydjson_resolve_prefix, NULL, LYD_JSON, sparent); |
| 364 | |
| 365 | /* free strings */ |
| 366 | free(meta_name); |
| 367 | meta_name = NULL; |
| 368 | if (dynamic) { |
| 369 | free(meta_value); |
| 370 | dynamic = 0; |
| 371 | } |
| 372 | meta_value = NULL; |
| 373 | |
| 374 | if (ret == LY_EINCOMPLETE) { |
| 375 | ly_set_add(&lybctx->unres_meta_type, *meta, LY_SET_OPT_USEASLIST); |
| 376 | } else if (ret) { |
| 377 | goto cleanup; |
| 378 | } |
| 379 | |
| 380 | stop_subtree: |
| 381 | ret = lyb_read_stop_subtree(lybctx); |
| 382 | LY_CHECK_GOTO(ret, cleanup); |
| 383 | } |
| 384 | |
| 385 | cleanup: |
| 386 | free(meta_name); |
| 387 | if (dynamic) { |
| 388 | free(meta_value); |
| 389 | } |
| 390 | if (ret) { |
| 391 | lyd_free_meta(lybctx->ctx, *meta, 1); |
| 392 | *meta = NULL; |
| 393 | } |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * @brief Parse opaque prefixes structure. |
| 399 | * |
| 400 | * @param[in] lybctx LYB context. |
| 401 | * @param[out] prefs Parsed prefixes. |
| 402 | * @return LY_ERR value. |
| 403 | */ |
| 404 | static LY_ERR |
| 405 | lyb_parse_opaq_prefixes(struct lyd_lyb_ctx *lybctx, struct ly_prefix **prefs) |
| 406 | { |
| 407 | LY_ERR ret = LY_SUCCESS; |
| 408 | uint8_t count, i; |
| 409 | char *str; |
| 410 | |
| 411 | /* read count */ |
| 412 | lyb_read(&count, 1, lybctx); |
| 413 | if (!count) { |
| 414 | return LY_SUCCESS; |
| 415 | } |
| 416 | |
| 417 | LY_ARRAY_CREATE_RET(lybctx->ctx, *prefs, count, LY_EMEM); |
| 418 | for (i = 0; i < count; ++i) { |
| 419 | LY_ARRAY_INCREMENT(*prefs); |
| 420 | |
| 421 | /* prefix */ |
| 422 | ret = lyb_read_string(&str, 1, lybctx); |
| 423 | LY_CHECK_GOTO(ret, cleanup); |
| 424 | (*prefs)[i].pref = lydict_insert_zc(lybctx->ctx, str); |
| 425 | |
| 426 | /* namespace */ |
| 427 | ret = lyb_read_string(&str, 1, lybctx); |
| 428 | LY_CHECK_GOTO(ret, cleanup); |
| 429 | (*prefs)[i].ns = lydict_insert_zc(lybctx->ctx, str); |
| 430 | } |
| 431 | |
| 432 | cleanup: |
| 433 | if (ret) { |
| 434 | ly_free_val_prefs(lybctx->ctx, *prefs); |
| 435 | *prefs = NULL; |
| 436 | } |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * @brief Parse opaque attributes. |
| 442 | * |
| 443 | * @param[in] lybctx LYB context. |
| 444 | * @param[out] attr Parsed attributes. |
| 445 | * @return LY_ERR value. |
| 446 | */ |
| 447 | static LY_ERR |
| 448 | lyb_parse_attributes(struct lyd_lyb_ctx *lybctx, struct ly_attr **attr) |
| 449 | { |
| 450 | LY_ERR ret = LY_SUCCESS; |
| 451 | uint8_t count, i; |
| 452 | struct ly_attr *attr2; |
| 453 | char *prefix = NULL, *ns = NULL, *name = NULL, *value = NULL; |
| 454 | int dynamic = 0; |
| 455 | LYD_FORMAT format = 0; |
| 456 | struct ly_prefix *val_prefs = NULL; |
| 457 | |
| 458 | /* read count */ |
| 459 | lyb_read(&count, 1, lybctx); |
| 460 | |
| 461 | /* read attributes */ |
| 462 | for (i = 0; i < count; ++i) { |
| 463 | ret = lyb_read_start_subtree(lybctx); |
| 464 | LY_CHECK_GOTO(ret, cleanup); |
| 465 | |
| 466 | /* prefix, may be emtpy */ |
| 467 | ret = lyb_read_string(&prefix, 1, lybctx); |
| 468 | LY_CHECK_GOTO(ret, cleanup); |
| 469 | if (!prefix[0]) { |
| 470 | free(prefix); |
| 471 | prefix = NULL; |
| 472 | } |
| 473 | |
| 474 | /* namespace, may be empty */ |
| 475 | ret = lyb_read_string(&ns, 1, lybctx); |
| 476 | LY_CHECK_GOTO(ret, cleanup); |
| 477 | if (!ns[0]) { |
| 478 | free(ns); |
| 479 | ns = NULL; |
| 480 | } |
| 481 | |
| 482 | /* name */ |
| 483 | ret = lyb_read_string(&name, 1, lybctx); |
| 484 | LY_CHECK_GOTO(ret, cleanup); |
| 485 | |
| 486 | /* value prefixes */ |
| 487 | ret = lyb_parse_opaq_prefixes(lybctx, &val_prefs); |
| 488 | LY_CHECK_GOTO(ret, cleanup); |
| 489 | |
| 490 | /* format */ |
| 491 | lyb_read((uint8_t *)&format, 1, lybctx); |
| 492 | |
| 493 | /* value */ |
| 494 | ret = lyb_read_string(&value, 0, lybctx); |
| 495 | LY_CHECK_GOTO(ret, cleanup); |
| 496 | dynamic = 1; |
| 497 | |
| 498 | /* attr2 is always changed to the created attribute */ |
| 499 | ret = ly_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), value, strlen(value), &dynamic, format, |
| 500 | val_prefs, prefix, prefix ? strlen(prefix) : 0, ns); |
| 501 | LY_CHECK_GOTO(ret, cleanup); |
| 502 | |
| 503 | free(prefix); |
| 504 | prefix = NULL; |
| 505 | free(ns); |
| 506 | ns = NULL; |
| 507 | free(name); |
| 508 | name = NULL; |
| 509 | val_prefs = NULL; |
| 510 | assert(!dynamic); |
| 511 | value = NULL; |
| 512 | |
| 513 | if (!*attr) { |
| 514 | *attr = attr2; |
| 515 | } |
| 516 | |
| 517 | ret = lyb_read_stop_subtree(lybctx); |
| 518 | LY_CHECK_GOTO(ret, cleanup); |
| 519 | } |
| 520 | |
| 521 | cleanup: |
| 522 | free(prefix); |
| 523 | free(ns); |
| 524 | free(name); |
| 525 | if (dynamic) { |
| 526 | free(value); |
| 527 | } |
| 528 | ly_free_val_prefs(lybctx->ctx, val_prefs); |
| 529 | if (ret) { |
| 530 | ly_free_attr(lybctx->ctx, *attr, 1); |
| 531 | *attr = NULL; |
| 532 | } |
| 533 | return ret; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * @brief Check whether a schema node matches a hash(es). |
| 538 | * |
| 539 | * @param[in] sibling Schema node to check. |
| 540 | * @param[in] hash Hash array to check. |
| 541 | * @param[in] hash_count Number of hashes in @p hash. |
| 542 | * @return non-zero if matches, |
| 543 | * @return 0 if not. |
| 544 | */ |
| 545 | static int |
| 546 | lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count) |
| 547 | { |
| 548 | LYB_HASH sibling_hash; |
| 549 | uint8_t i; |
| 550 | |
| 551 | /* compare all the hashes starting from collision ID 0 */ |
| 552 | for (i = 0; i < hash_count; ++i) { |
| 553 | sibling_hash = lyb_hash(sibling, i); |
| 554 | if (sibling_hash != hash[i]) { |
| 555 | return 0; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | return 1; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * @brief Check that a schema node is suitable based on options. |
| 564 | * |
| 565 | * @param[in] lybctx LYB context. |
| 566 | * @param[in] snode Schema node to check. |
| 567 | * @return LY_ERR value. |
| 568 | */ |
| 569 | static LY_ERR |
| 570 | lyb_parse_check_schema(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode) |
| 571 | { |
| 572 | LY_ERR ret = LY_SUCCESS; |
| 573 | |
| 574 | if ((lybctx->options & LYD_OPT_NO_STATE) && (snode->flags & LYS_CONFIG_R)) { |
| 575 | LOGVAL(lybctx->ctx, LY_VLOG_LYSC, snode, LY_VCODE_INNODE, "state", snode->name); |
| 576 | return LY_EVALID; |
| 577 | } |
| 578 | |
| 579 | if (snode->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 580 | if (lybctx->int_opts & LYD_INTOPT_RPC) { |
| 581 | if (lybctx->op_ntf) { |
| 582 | LOGVAL(lybctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
| 583 | lys_nodetype2str(snode->nodetype), snode->name, |
| 584 | lys_nodetype2str(lybctx->op_ntf->schema->nodetype), lybctx->op_ntf->schema->name); |
| 585 | return LY_EVALID; |
| 586 | } |
| 587 | } else { |
| 588 | LOGVAL(lybctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\".", |
| 589 | lys_nodetype2str(snode->nodetype), snode->name); |
| 590 | return LY_EVALID; |
| 591 | } |
| 592 | } else if (snode->nodetype == LYS_NOTIF) { |
| 593 | if (lybctx->int_opts & LYD_INTOPT_NOTIF) { |
| 594 | if (lybctx->op_ntf) { |
| 595 | LOGVAL(lybctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\", %s \"%s\" already parsed.", |
| 596 | lys_nodetype2str(snode->nodetype), snode->name, |
| 597 | lys_nodetype2str(lybctx->op_ntf->schema->nodetype), lybctx->op_ntf->schema->name); |
| 598 | return LY_EVALID; |
| 599 | } |
| 600 | } else { |
| 601 | LOGVAL(lybctx->ctx, LY_VLOG_LYSC, snode, LYVE_DATA, "Unexpected %s element \"%s\".", |
| 602 | lys_nodetype2str(snode->nodetype), snode->name); |
| 603 | return LY_EVALID; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * @brief Parse schema node hash. |
| 612 | * |
| 613 | * @param[in] lybctx LYB context. |
| 614 | * @param[in] sparent Schema parent, must be set if @p mod is not. |
| 615 | * @param[in] mod Module of the top-level node, must be set if @p sparent is not. |
| 616 | * @param[out] snode Parsed found schema node, may be NULL if opaque. |
| 617 | * @return LY_ERR value. |
| 618 | */ |
| 619 | static LY_ERR |
| 620 | lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod, |
| 621 | const struct lysc_node **snode) |
| 622 | { |
| 623 | LY_ERR ret; |
| 624 | uint8_t i, j; |
| 625 | const struct lysc_node *sibling; |
| 626 | LYB_HASH hash[LYB_HASH_BITS - 1]; |
| 627 | int getnext_opts; |
| 628 | |
| 629 | *snode = NULL; |
| 630 | /* leave if-feature check for validation */ |
| 631 | getnext_opts = LYS_GETNEXT_NOSTATECHECK | (lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0); |
| 632 | |
| 633 | /* read the first hash */ |
| 634 | lyb_read(&hash[0], sizeof *hash, lybctx); |
| 635 | |
| 636 | if (!hash[0]) { |
| 637 | /* opaque node */ |
| 638 | return LY_SUCCESS; |
| 639 | } |
| 640 | |
| 641 | /* based on the first hash read all the other ones, if any */ |
| 642 | for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) { |
| 643 | if (i > LYB_HASH_BITS) { |
| 644 | LOGINT_RET(lybctx->ctx); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /* move the first hash on its accurate position */ |
| 649 | hash[i] = hash[0]; |
| 650 | |
| 651 | /* read the rest of hashes */ |
| 652 | for (j = i; j; --j) { |
| 653 | lyb_read(&hash[j - 1], sizeof *hash, lybctx); |
| 654 | |
| 655 | /* correct collision ID */ |
| 656 | assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1))); |
| 657 | /* preceded with zeros */ |
| 658 | assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1))))); |
| 659 | } |
| 660 | |
| 661 | /* find our node with matching hashes */ |
| 662 | sibling = NULL; |
| 663 | while ((sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts))) { |
| 664 | /* skip schema nodes from models not present during printing */ |
| 665 | if (lyb_has_schema_model(sibling, lybctx->models) |
| 666 | && lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) { |
| 667 | /* match found */ |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if (!sibling && (lybctx->options & LYD_OPT_STRICT)) { |
| 673 | if (mod) { |
| 674 | LOGVAL(lybctx->ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Failed to find matching hash for a top-level node" |
| 675 | " from \"%s\".", mod->name); |
| 676 | } else { |
| 677 | LOGVAL(lybctx->ctx, LY_VLOG_LYSC, sparent, LYVE_REFERENCE, "Failed to find matching hash for a child node" |
| 678 | " of \"%s\".", sparent->name); |
| 679 | } |
| 680 | return LY_EVALID; |
| 681 | } else if (sibling && (ret = lyb_parse_check_schema(lybctx, sibling))) { |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | *snode = sibling; |
| 686 | return LY_SUCCESS; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * @brief Read until the end of the current subtree. |
| 691 | * |
| 692 | * @param[in] lybctx LYB context. |
| 693 | */ |
| 694 | static void |
| 695 | lyb_skip_subtree(struct lyd_lyb_ctx *lybctx) |
| 696 | { |
| 697 | int parsed; |
| 698 | |
| 699 | do { |
| 700 | /* first skip any meta information inside */ |
| 701 | parsed = LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES; |
| 702 | lybctx->data += parsed; |
| 703 | lybctx->byte_count += parsed; |
| 704 | |
| 705 | /* then read data */ |
| 706 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx); |
| 707 | } while (LYB_LAST_SUBTREE(lybctx).written); |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * @brief Parse LYB subtree. |
| 712 | * |
| 713 | * @param[in] lybctx LYB context. |
| 714 | * @param[in] parent Data parent of the subtree, must be set if @p first is not. |
| 715 | * @param[in,out] first First top-level sibling, must be set if @p parent is not. |
| 716 | * @return LY_ERR value. |
| 717 | */ |
| 718 | static LY_ERR |
| 719 | lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node_inner *parent, struct lyd_node **first) |
| 720 | { |
| 721 | LY_ERR ret = LY_SUCCESS; |
| 722 | struct lyd_node *node = NULL, *tree; |
| 723 | const struct lys_module *mod; |
| 724 | const struct lysc_node *snode = NULL; |
| 725 | struct lyd_meta *meta = NULL, *m; |
| 726 | struct ly_attr *attr = NULL, *a; |
| 727 | struct ly_prefix *val_prefs = NULL; |
| 728 | LYD_ANYDATA_VALUETYPE value_type; |
| 729 | char *value = NULL, *name = NULL, *prefix = NULL, *ns = NULL; |
| 730 | int dynamic = 0; |
| 731 | LYD_FORMAT format = 0; |
| 732 | int prev_lo; |
| 733 | |
| 734 | /* register a new subtree */ |
| 735 | LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx), cleanup); |
| 736 | |
| 737 | if (!parent) { |
| 738 | /* top-level, read module name */ |
| 739 | ret = lyb_parse_model(lybctx, &mod); |
| 740 | LY_CHECK_GOTO(ret, cleanup); |
| 741 | |
| 742 | /* read hash, find the schema node starting from mod */ |
| 743 | ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode); |
| 744 | LY_CHECK_GOTO(ret, cleanup); |
| 745 | } else { |
| 746 | /* read hash, find the schema node starting from parent schema */ |
| 747 | ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode); |
| 748 | LY_CHECK_GOTO(ret, cleanup); |
| 749 | } |
| 750 | |
| 751 | if (!snode && !(lybctx->options & LYD_OPT_OPAQ)) { |
| 752 | /* unknown data, skip them */ |
| 753 | lyb_skip_subtree(lybctx); |
| 754 | goto stop_subtree; |
| 755 | } |
| 756 | |
| 757 | /* create metadata/attributes */ |
| 758 | if (snode) { |
| 759 | ret = lyb_parse_metadata(lybctx, snode, &meta); |
| 760 | LY_CHECK_GOTO(ret, cleanup); |
| 761 | } else { |
| 762 | ret = lyb_parse_attributes(lybctx, &attr); |
| 763 | LY_CHECK_GOTO(ret, cleanup); |
| 764 | } |
| 765 | |
| 766 | if (!snode) { |
| 767 | /* parse prefix */ |
| 768 | ret = lyb_read_string(&prefix, 1, lybctx); |
| 769 | LY_CHECK_GOTO(ret, cleanup); |
| 770 | |
| 771 | /* parse namespace */ |
| 772 | ret = lyb_read_string(&ns, 1, lybctx); |
| 773 | LY_CHECK_GOTO(ret, cleanup); |
| 774 | |
| 775 | /* parse name */ |
| 776 | ret = lyb_read_string(&name, 1, lybctx); |
| 777 | LY_CHECK_GOTO(ret, cleanup); |
| 778 | |
| 779 | /* parse value prefixes */ |
| 780 | ret = lyb_parse_opaq_prefixes(lybctx, &val_prefs); |
| 781 | LY_CHECK_GOTO(ret, cleanup); |
| 782 | |
| 783 | /* parse format */ |
| 784 | lyb_read((uint8_t *)&format, 1, lybctx); |
| 785 | |
| 786 | /* parse value */ |
| 787 | ret = lyb_read_string(&value, 0, lybctx); |
| 788 | LY_CHECK_GOTO(ret, cleanup); |
| 789 | dynamic = 1; |
| 790 | |
| 791 | /* create node */ |
| 792 | ret = lyd_create_opaq(lybctx->ctx, name, strlen(name), value, strlen(value), &dynamic, format, val_prefs, prefix, |
| 793 | strlen(prefix), ns, &node); |
| 794 | LY_CHECK_GOTO(ret, cleanup); |
| 795 | |
| 796 | /* process children */ |
| 797 | while (LYB_LAST_SUBTREE(lybctx).written) { |
| 798 | ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL); |
| 799 | LY_CHECK_GOTO(ret, cleanup); |
| 800 | } |
| 801 | } else if (snode->nodetype & LYD_NODE_TERM) { |
| 802 | /* parse value */ |
| 803 | ret = lyb_read_string(&value, 0, lybctx); |
| 804 | LY_CHECK_GOTO(ret, cleanup); |
| 805 | dynamic = 1; |
| 806 | |
| 807 | /* create node */ |
| 808 | ret = lyd_create_term(snode, value, strlen(value), &dynamic, lydjson_resolve_prefix, NULL, LYD_JSON, &node); |
| 809 | if (dynamic) { |
| 810 | free(value); |
| 811 | dynamic = 0; |
| 812 | } |
| 813 | value = NULL; |
| 814 | if (ret == LY_EINCOMPLETE) { |
| 815 | if (!(lybctx->options & LYD_OPT_PARSE_ONLY)) { |
| 816 | ly_set_add(&lybctx->unres_node_type, node, LY_SET_OPT_USEASLIST); |
| 817 | } |
| 818 | ret = LY_SUCCESS; |
| 819 | } else if (ret) { |
| 820 | goto cleanup; |
| 821 | } |
| 822 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 823 | /* create node */ |
| 824 | ret = lyd_create_inner(snode, &node); |
| 825 | LY_CHECK_GOTO(ret, cleanup); |
| 826 | |
| 827 | /* process children */ |
| 828 | while (LYB_LAST_SUBTREE(lybctx).written) { |
| 829 | ret = lyb_parse_subtree_r(lybctx, (struct lyd_node_inner *)node, NULL); |
| 830 | LY_CHECK_GOTO(ret, cleanup); |
| 831 | } |
| 832 | |
| 833 | if (!(lybctx->options & LYD_OPT_PARSE_ONLY)) { |
| 834 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
| 835 | ret = lyd_validate_new(lyd_node_children_p(node), snode, NULL); |
| 836 | LY_CHECK_GOTO(ret, cleanup); |
| 837 | |
| 838 | /* add any missing default children */ |
| 839 | ret = lyd_validate_defaults_r((struct lyd_node_inner *)node, lyd_node_children_p(node), NULL, NULL, |
| 840 | &lybctx->unres_node_type, &lybctx->when_check, lybctx->options); |
| 841 | LY_CHECK_GOTO(ret, cleanup); |
| 842 | } |
| 843 | |
| 844 | if (snode->nodetype == LYS_LIST) { |
| 845 | /* hash now that all keys should be parsed, rehash for key-less list */ |
| 846 | lyd_hash(node); |
| 847 | } else if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 848 | /* rememeber the RPC/action/notification */ |
| 849 | lybctx->op_ntf = node; |
| 850 | } |
| 851 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 852 | /* parse value type */ |
| 853 | lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx); |
| 854 | if (value_type == LYD_ANYDATA_DATATREE) { |
| 855 | /* invalid situation */ |
| 856 | LOGINT(lybctx->ctx); |
| 857 | goto cleanup; |
| 858 | } |
| 859 | |
| 860 | /* read anydata content */ |
| 861 | ret = lyb_read_string(&value, 0, lybctx); |
| 862 | LY_CHECK_GOTO(ret, cleanup); |
| 863 | dynamic = 1; |
| 864 | |
| 865 | if (value_type == LYD_ANYDATA_LYB) { |
| 866 | /* turn logging off */ |
| 867 | prev_lo = ly_log_options(0); |
| 868 | |
| 869 | /* try to parse LYB into a data tree */ |
| 870 | tree = lyd_parse_mem((struct ly_ctx *)lybctx->ctx, value, LYD_LYB, |
| 871 | LYD_OPT_PARSE_ONLY | LYD_OPT_OPAQ | LYD_OPT_STRICT); |
| 872 | ly_log_options(prev_lo); |
| 873 | if (!ly_errcode(lybctx->ctx)) { |
| 874 | /* successfully parsed */ |
| 875 | free(value); |
| 876 | value = (char *)tree; |
| 877 | value_type = LYD_ANYDATA_DATATREE; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | /* create node */ |
| 882 | ret = lyd_create_any(snode, value, value_type, &node); |
| 883 | LY_CHECK_GOTO(ret, cleanup); |
| 884 | |
| 885 | dynamic = 0; |
| 886 | value = NULL; |
| 887 | } |
| 888 | assert(node); |
| 889 | |
| 890 | /* add/correct flags */ |
| 891 | if (snode) { |
| 892 | lyd_parse_set_data_flags(node, &lybctx->when_check, &meta, lybctx->options); |
| 893 | } |
| 894 | |
| 895 | /* add metadata/attributes */ |
| 896 | if (snode) { |
| 897 | LY_LIST_FOR(meta, m) { |
| 898 | m->parent = node; |
| 899 | } |
| 900 | node->meta = meta; |
| 901 | meta = NULL; |
| 902 | } else { |
| 903 | assert(!node->schema); |
| 904 | LY_LIST_FOR(attr, a) { |
| 905 | a->parent = (struct lyd_node_opaq *)node; |
| 906 | } |
| 907 | ((struct lyd_node_opaq *)node)->attr = attr; |
| 908 | attr = NULL; |
| 909 | } |
| 910 | |
| 911 | /* insert */ |
| 912 | lyd_insert_node((struct lyd_node *)parent, first, node); |
| 913 | node = NULL; |
| 914 | |
| 915 | stop_subtree: |
| 916 | /* end the subtree */ |
| 917 | ret = lyb_read_stop_subtree(lybctx); |
| 918 | LY_CHECK_GOTO(ret, cleanup); |
| 919 | |
| 920 | cleanup: |
| 921 | free(prefix); |
| 922 | free(ns); |
| 923 | free(name); |
| 924 | if (dynamic) { |
| 925 | free(value); |
| 926 | } |
| 927 | ly_free_val_prefs(lybctx->ctx, val_prefs); |
| 928 | |
| 929 | lyd_free_meta(lybctx->ctx, meta, 1); |
| 930 | ly_free_attr(lybctx->ctx, attr, 1); |
| 931 | lyd_free_tree(node); |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * @brief Parse used YANG data models. |
| 937 | * |
| 938 | * @param[in] lybctx LYB context. |
| 939 | * @return LY_ERR value. |
| 940 | */ |
| 941 | static LY_ERR |
| 942 | lyb_parse_data_models(struct lyd_lyb_ctx *lybctx) |
| 943 | { |
| 944 | LY_ERR ret; |
| 945 | uint32_t count; |
| 946 | LY_ARRAY_SIZE_TYPE u; |
| 947 | |
| 948 | /* read model count */ |
| 949 | lyb_read_number(&count, sizeof count, 2, lybctx); |
| 950 | |
| 951 | if (count) { |
| 952 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM); |
| 953 | |
| 954 | /* read modules */ |
| 955 | for (u = 0; u < count; ++u) { |
| 956 | ret = lyb_parse_model(lybctx, &lybctx->models[u]); |
| 957 | LY_CHECK_RET(ret); |
| 958 | LY_ARRAY_INCREMENT(lybctx->models); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | return LY_SUCCESS; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * @brief Parse LYB magic number. |
| 967 | * |
| 968 | * @param[in] lybctx LYB context. |
| 969 | * @return LY_ERR value. |
| 970 | */ |
| 971 | static LY_ERR |
| 972 | lyb_parse_magic_number(struct lyd_lyb_ctx *lybctx) |
| 973 | { |
| 974 | char magic_byte = 0; |
| 975 | |
| 976 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 977 | if (magic_byte != 'l') { |
| 978 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 979 | return LY_EINVAL; |
| 980 | } |
| 981 | |
| 982 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 983 | if (magic_byte != 'y') { |
| 984 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 985 | return LY_EINVAL; |
| 986 | } |
| 987 | |
| 988 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 989 | if (magic_byte != 'b') { |
| 990 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
| 991 | return LY_EINVAL; |
| 992 | } |
| 993 | |
| 994 | return LY_SUCCESS; |
| 995 | } |
| 996 | |
| 997 | /** |
| 998 | * @brief Parse LYB header. |
| 999 | * |
| 1000 | * @param[in] lybctx LYB context. |
| 1001 | * @return LY_ERR value. |
| 1002 | */ |
| 1003 | static LY_ERR |
| 1004 | lyb_parse_header(struct lyd_lyb_ctx *lybctx) |
| 1005 | { |
| 1006 | uint8_t byte = 0; |
| 1007 | |
| 1008 | /* version, future flags */ |
| 1009 | lyb_read((uint8_t *)&byte, sizeof byte, lybctx); |
| 1010 | |
| 1011 | if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) { |
| 1012 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".", |
| 1013 | byte & LYB_VERSION_MASK, LYB_VERSION_NUM); |
| 1014 | return LY_EINVAL; |
| 1015 | } |
| 1016 | |
| 1017 | return LY_SUCCESS; |
| 1018 | } |
| 1019 | |
| 1020 | LY_ERR |
| 1021 | lyd_parse_lyb_data(struct ly_ctx *ctx, const char *data, int options, struct lyd_node **tree, int *parsed_bytes) |
| 1022 | { |
| 1023 | LY_ERR ret = LY_SUCCESS; |
| 1024 | struct lyd_lyb_ctx lybctx = {0}; |
| 1025 | |
| 1026 | *tree = NULL; |
| 1027 | |
| 1028 | lybctx.data = data; |
| 1029 | lybctx.ctx = ctx; |
| 1030 | lybctx.options = options; |
| 1031 | |
| 1032 | /* read magic number */ |
| 1033 | ret = lyb_parse_magic_number(&lybctx); |
| 1034 | LY_CHECK_GOTO(ret, cleanup); |
| 1035 | |
| 1036 | /* read header */ |
| 1037 | ret = lyb_parse_header(&lybctx); |
| 1038 | LY_CHECK_GOTO(ret, cleanup); |
| 1039 | |
| 1040 | /* read used models */ |
| 1041 | ret = lyb_parse_data_models(&lybctx); |
| 1042 | LY_CHECK_GOTO(ret, cleanup); |
| 1043 | |
| 1044 | /* read subtree(s) */ |
| 1045 | while (lybctx.data[0]) { |
| 1046 | ret = lyb_parse_subtree_r(&lybctx, NULL, tree); |
| 1047 | LY_CHECK_GOTO(ret, cleanup); |
| 1048 | } |
| 1049 | |
| 1050 | /* read the last zero, parsing finished */ |
| 1051 | ++lybctx.byte_count; |
| 1052 | ++lybctx.data; |
| 1053 | |
| 1054 | /* TODO validation */ |
| 1055 | |
| 1056 | cleanup: |
| 1057 | LY_ARRAY_FREE(lybctx.subtrees); |
| 1058 | LY_ARRAY_FREE(lybctx.models); |
| 1059 | ly_set_erase(&lybctx.unres_node_type, NULL); |
| 1060 | ly_set_erase(&lybctx.unres_meta_type, NULL); |
| 1061 | ly_set_erase(&lybctx.when_check, NULL); |
| 1062 | |
| 1063 | if (parsed_bytes) { |
| 1064 | *parsed_bytes = lybctx.byte_count; |
| 1065 | } |
| 1066 | if (ret) { |
| 1067 | lyd_free_all(*tree); |
| 1068 | *tree = NULL; |
| 1069 | } |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | LY_ERR |
| 1074 | lyd_parse_lyb_rpc(struct ly_ctx *ctx, const char *data, struct lyd_node **tree, struct lyd_node **op, int *parsed_bytes) |
| 1075 | { |
| 1076 | LY_ERR ret = LY_SUCCESS; |
| 1077 | struct lyd_lyb_ctx lybctx = {0}; |
| 1078 | |
| 1079 | lybctx.data = data; |
| 1080 | lybctx.ctx = ctx; |
| 1081 | lybctx.options = LYD_OPT_PARSE_ONLY | LYD_OPT_STRICT; |
| 1082 | lybctx.int_opts = LYD_INTOPT_RPC; |
| 1083 | |
| 1084 | *tree = NULL; |
| 1085 | if (op) { |
| 1086 | *op = NULL; |
| 1087 | } |
| 1088 | |
| 1089 | /* read magic number */ |
| 1090 | ret = lyb_parse_magic_number(&lybctx); |
| 1091 | LY_CHECK_GOTO(ret, cleanup); |
| 1092 | |
| 1093 | /* read header */ |
| 1094 | ret = lyb_parse_header(&lybctx); |
| 1095 | LY_CHECK_GOTO(ret, cleanup); |
| 1096 | |
| 1097 | /* read used models */ |
| 1098 | ret = lyb_parse_data_models(&lybctx); |
| 1099 | LY_CHECK_GOTO(ret, cleanup); |
| 1100 | |
| 1101 | /* read subtree(s) */ |
| 1102 | while (lybctx.data[0]) { |
| 1103 | ret = lyb_parse_subtree_r(&lybctx, NULL, tree); |
| 1104 | LY_CHECK_GOTO(ret, cleanup); |
| 1105 | } |
| 1106 | |
| 1107 | /* read the last zero, parsing finished */ |
| 1108 | ++lybctx.byte_count; |
| 1109 | ++lybctx.data; |
| 1110 | |
| 1111 | /* make sure we have parsed some operation */ |
| 1112 | if (!lybctx.op_ntf) { |
| 1113 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"rpc\"/\"action\" node."); |
| 1114 | ret = LY_EVALID; |
| 1115 | goto cleanup; |
| 1116 | } |
| 1117 | |
| 1118 | if (op) { |
| 1119 | *op = lybctx.op_ntf; |
| 1120 | } |
| 1121 | assert(*tree); |
| 1122 | |
| 1123 | cleanup: |
| 1124 | LY_ARRAY_FREE(lybctx.subtrees); |
| 1125 | LY_ARRAY_FREE(lybctx.models); |
| 1126 | assert(!lybctx.unres_node_type.count && !lybctx.unres_meta_type.count && !lybctx.when_check.count); |
| 1127 | |
| 1128 | if (parsed_bytes) { |
| 1129 | *parsed_bytes = lybctx.byte_count; |
| 1130 | } |
| 1131 | if (ret) { |
| 1132 | lyd_free_all(*tree); |
| 1133 | *tree = NULL; |
| 1134 | } |
| 1135 | return ret; |
| 1136 | } |
| 1137 | |
| 1138 | LY_ERR |
| 1139 | lyd_parse_lyb_notif(struct ly_ctx *ctx, const char *data, struct lyd_node **tree, struct lyd_node **ntf, int *parsed_bytes) |
| 1140 | { |
| 1141 | LY_ERR ret = LY_SUCCESS; |
| 1142 | struct lyd_lyb_ctx lybctx = {0}; |
| 1143 | |
| 1144 | lybctx.data = data; |
| 1145 | lybctx.ctx = ctx; |
| 1146 | lybctx.options = LYD_OPT_PARSE_ONLY | LYD_OPT_STRICT; |
| 1147 | lybctx.int_opts = LYD_INTOPT_NOTIF; |
| 1148 | |
| 1149 | *tree = NULL; |
| 1150 | if (ntf) { |
| 1151 | *ntf = NULL; |
| 1152 | } |
| 1153 | |
| 1154 | /* read magic number */ |
| 1155 | ret = lyb_parse_magic_number(&lybctx); |
| 1156 | LY_CHECK_GOTO(ret, cleanup); |
| 1157 | |
| 1158 | /* read header */ |
| 1159 | ret = lyb_parse_header(&lybctx); |
| 1160 | LY_CHECK_GOTO(ret, cleanup); |
| 1161 | |
| 1162 | /* read used models */ |
| 1163 | ret = lyb_parse_data_models(&lybctx); |
| 1164 | LY_CHECK_GOTO(ret, cleanup); |
| 1165 | |
| 1166 | /* read subtree(s) */ |
| 1167 | while (lybctx.data[0]) { |
| 1168 | ret = lyb_parse_subtree_r(&lybctx, NULL, tree); |
| 1169 | LY_CHECK_GOTO(ret, cleanup); |
| 1170 | } |
| 1171 | |
| 1172 | /* read the last zero, parsing finished */ |
| 1173 | ++lybctx.byte_count; |
| 1174 | ++lybctx.data; |
| 1175 | |
| 1176 | /* make sure we have parsed some notification */ |
| 1177 | if (!lybctx.op_ntf) { |
| 1178 | LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_DATA, "Missing the \"notification\" node."); |
| 1179 | ret = LY_EVALID; |
| 1180 | goto cleanup; |
| 1181 | } |
| 1182 | |
| 1183 | if (ntf) { |
| 1184 | *ntf = lybctx.op_ntf; |
| 1185 | } |
| 1186 | assert(*tree); |
| 1187 | |
| 1188 | cleanup: |
| 1189 | LY_ARRAY_FREE(lybctx.subtrees); |
| 1190 | LY_ARRAY_FREE(lybctx.models); |
| 1191 | assert(!lybctx.unres_node_type.count && !lybctx.unres_meta_type.count && !lybctx.when_check.count); |
| 1192 | |
| 1193 | if (parsed_bytes) { |
| 1194 | *parsed_bytes = lybctx.byte_count; |
| 1195 | } |
| 1196 | if (ret) { |
| 1197 | lyd_free_all(*tree); |
| 1198 | *tree = NULL; |
| 1199 | } |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
| 1203 | LY_ERR |
| 1204 | lyd_parse_lyb_reply(struct lyd_node *request, const char *data, struct lyd_node **tree, struct lyd_node **op, |
| 1205 | int *parsed_bytes) |
| 1206 | { |
| 1207 | LY_ERR ret = LY_SUCCESS; |
| 1208 | struct lyd_lyb_ctx lybctx = {0}; |
| 1209 | struct lyd_node *iter, *req_op, *rep_op = NULL; |
| 1210 | |
| 1211 | lybctx.data = data; |
| 1212 | lybctx.ctx = LYD_NODE_CTX(request); |
| 1213 | lybctx.options = LYD_OPT_PARSE_ONLY | LYD_OPT_STRICT; |
| 1214 | lybctx.int_opts = LYD_INTOPT_REPLY; |
| 1215 | |
| 1216 | *tree = NULL; |
| 1217 | if (op) { |
| 1218 | *op = NULL; |
| 1219 | } |
| 1220 | |
| 1221 | /* find request OP */ |
| 1222 | LYD_TREE_DFS_BEGIN((struct lyd_node *)request, iter, req_op) { |
| 1223 | if (req_op->schema->nodetype & (LYS_RPC | LYS_ACTION)) { |
| 1224 | break; |
| 1225 | } |
| 1226 | LYD_TREE_DFS_END(request, iter, req_op); |
| 1227 | } |
| 1228 | if (!(req_op->schema->nodetype & (LYS_RPC | LYS_ACTION))) { |
| 1229 | LOGERR(LYD_NODE_CTX(request), LY_EINVAL, "No RPC/action in the request found."); |
| 1230 | ret = LY_EINVAL; |
| 1231 | goto cleanup; |
| 1232 | } |
| 1233 | |
| 1234 | /* duplicate request OP with parents */ |
| 1235 | rep_op = lyd_dup(req_op, NULL, LYD_DUP_WITH_PARENTS); |
| 1236 | LY_CHECK_ERR_GOTO(!rep_op, ret = LY_EMEM, cleanup); |
| 1237 | |
| 1238 | /* read magic number */ |
| 1239 | ret = lyb_parse_magic_number(&lybctx); |
| 1240 | LY_CHECK_GOTO(ret, cleanup); |
| 1241 | |
| 1242 | /* read header */ |
| 1243 | ret = lyb_parse_header(&lybctx); |
| 1244 | LY_CHECK_GOTO(ret, cleanup); |
| 1245 | |
| 1246 | /* read used models */ |
| 1247 | ret = lyb_parse_data_models(&lybctx); |
| 1248 | LY_CHECK_GOTO(ret, cleanup); |
| 1249 | |
| 1250 | /* read subtree(s) */ |
| 1251 | while (lybctx.data[0]) { |
| 1252 | ret = lyb_parse_subtree_r(&lybctx, (struct lyd_node_inner *)rep_op, NULL); |
| 1253 | LY_CHECK_GOTO(ret, cleanup); |
| 1254 | } |
| 1255 | |
| 1256 | /* read the last zero, parsing finished */ |
| 1257 | ++lybctx.byte_count; |
| 1258 | ++lybctx.data; |
| 1259 | |
| 1260 | if (op) { |
| 1261 | *op = rep_op; |
| 1262 | } |
| 1263 | for (iter = rep_op; iter->parent; iter = (struct lyd_node *)iter->parent); |
| 1264 | *tree = iter; |
| 1265 | rep_op = NULL; |
| 1266 | |
| 1267 | cleanup: |
| 1268 | lyd_free_all(rep_op); |
| 1269 | LY_ARRAY_FREE(lybctx.subtrees); |
| 1270 | LY_ARRAY_FREE(lybctx.models); |
| 1271 | assert(!lybctx.unres_node_type.count && !lybctx.unres_meta_type.count && !lybctx.when_check.count); |
| 1272 | |
| 1273 | if (parsed_bytes) { |
| 1274 | *parsed_bytes = lybctx.byte_count; |
| 1275 | } |
| 1276 | if (ret) { |
| 1277 | lyd_free_all(*tree); |
| 1278 | *tree = NULL; |
| 1279 | } |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | API int |
| 1284 | lyd_lyb_data_length(const char *data) |
| 1285 | { |
| 1286 | LY_ERR ret = LY_SUCCESS; |
| 1287 | struct lyd_lyb_ctx lybctx = {0}; |
| 1288 | int count, i; |
| 1289 | size_t len; |
| 1290 | uint8_t buf[LYB_SIZE_MAX]; |
| 1291 | |
| 1292 | if (!data) { |
| 1293 | return -1; |
| 1294 | } |
| 1295 | |
| 1296 | lybctx.data = data; |
| 1297 | |
| 1298 | /* read magic number */ |
| 1299 | ret = lyb_parse_magic_number(&lybctx); |
| 1300 | LY_CHECK_GOTO(ret, cleanup); |
| 1301 | |
| 1302 | /* read header */ |
| 1303 | ret = lyb_parse_header(&lybctx); |
| 1304 | LY_CHECK_GOTO(ret, cleanup); |
| 1305 | |
| 1306 | /* read model count */ |
| 1307 | lyb_read_number(&count, sizeof count, 2, &lybctx); |
| 1308 | |
| 1309 | /* read all models */ |
| 1310 | for (i = 0; i < count; ++i) { |
| 1311 | /* module name length */ |
| 1312 | len = 0; |
| 1313 | lyb_read_number(&len, sizeof len, 2, &lybctx); |
| 1314 | |
| 1315 | /* model name */ |
| 1316 | lyb_read(buf, len, &lybctx); |
| 1317 | |
| 1318 | /* revision */ |
| 1319 | lyb_read(buf, 2, &lybctx); |
| 1320 | } |
| 1321 | |
| 1322 | while (lybctx.data[0]) { |
| 1323 | /* register a new subtree */ |
| 1324 | ret = lyb_read_start_subtree(&lybctx); |
| 1325 | LY_CHECK_GOTO(ret, cleanup); |
| 1326 | |
| 1327 | /* skip it */ |
| 1328 | lyb_skip_subtree(&lybctx); |
| 1329 | |
| 1330 | /* subtree finished */ |
| 1331 | ret = lyb_read_stop_subtree(&lybctx); |
| 1332 | LY_CHECK_GOTO(ret, cleanup); |
| 1333 | } |
| 1334 | |
| 1335 | /* read the last zero, parsing finished */ |
| 1336 | ++lybctx.byte_count; |
| 1337 | ++lybctx.data; |
| 1338 | |
| 1339 | cleanup: |
| 1340 | LY_ARRAY_FREE(lybctx.subtrees); |
| 1341 | return ret ? -1 : (signed)lybctx.byte_count; |
| 1342 | } |