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 | * |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 6 | * Copyright (c) 2020 - 2022 CESNET, z.s.p.o. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 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 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 23 | #include "compat.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 24 | #include "context.h" |
| 25 | #include "dict.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 26 | #include "hash_table.h" |
| 27 | #include "in.h" |
Michal Vasko | afac782 | 2020-10-20 14:22:26 +0200 | [diff] [blame] | 28 | #include "in_internal.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 29 | #include "log.h" |
Michal Vasko | 8f702ee | 2024-02-20 15:44:24 +0100 | [diff] [blame] | 30 | #include "ly_common.h" |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 31 | #include "parser_data.h" |
| 32 | #include "parser_internal.h" |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 33 | #include "plugins_exts.h" |
Michal Vasko | b475096 | 2022-10-06 15:33:35 +0200 | [diff] [blame] | 34 | #include "plugins_exts/metadata.h" |
Radek Krejci | 7711410 | 2021-03-10 15:21:57 +0100 | [diff] [blame] | 35 | #include "set.h" |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 36 | #include "tree.h" |
Radek Krejci | 47fab89 | 2020-11-05 17:02:41 +0100 | [diff] [blame] | 37 | #include "tree_data.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 38 | #include "tree_data_internal.h" |
Radek Krejci | 859a15a | 2021-03-05 20:56:59 +0100 | [diff] [blame] | 39 | #include "tree_edit.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 40 | #include "tree_schema.h" |
| 41 | #include "validation.h" |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 42 | #include "xml.h" |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 43 | |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 44 | static LY_ERR lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 45 | struct ly_set *parsed); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 46 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 47 | void |
| 48 | lylyb_ctx_free(struct lylyb_ctx *ctx) |
| 49 | { |
| 50 | LY_ARRAY_COUNT_TYPE u; |
| 51 | |
Michal Vasko | 82df24a | 2023-02-21 15:02:11 +0100 | [diff] [blame] | 52 | if (!ctx) { |
| 53 | return; |
| 54 | } |
| 55 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 56 | LY_ARRAY_FREE(ctx->siblings); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 57 | LY_ARRAY_FREE(ctx->models); |
| 58 | |
| 59 | LY_ARRAY_FOR(ctx->sib_hts, u) { |
Michal Vasko | 77b7f90a | 2023-01-31 15:42:41 +0100 | [diff] [blame] | 60 | lyht_free(ctx->sib_hts[u].ht, NULL); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 61 | } |
| 62 | LY_ARRAY_FREE(ctx->sib_hts); |
| 63 | |
| 64 | free(ctx); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | lyd_lyb_ctx_free(struct lyd_ctx *lydctx) |
| 69 | { |
| 70 | struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx; |
| 71 | |
Michal Vasko | 82df24a | 2023-02-21 15:02:11 +0100 | [diff] [blame] | 72 | if (!lydctx) { |
| 73 | return; |
| 74 | } |
| 75 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 76 | lyd_ctx_free(lydctx); |
| 77 | lylyb_ctx_free(ctx->lybctx); |
| 78 | free(ctx); |
| 79 | } |
| 80 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 81 | /** |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 82 | * @brief Read metadata about siblings. |
| 83 | * |
| 84 | * @param[out] sib Structure in which the metadata will be stored. |
| 85 | * @param[in] lybctx LYB context. |
| 86 | */ |
| 87 | static void |
| 88 | lyb_read_sibling_meta(struct lyd_lyb_sibling *sib, struct lylyb_ctx *lybctx) |
| 89 | { |
| 90 | uint8_t meta_buf[LYB_META_BYTES]; |
| 91 | uint64_t num = 0; |
| 92 | |
| 93 | ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES); |
| 94 | |
| 95 | memcpy(&num, meta_buf, LYB_SIZE_BYTES); |
| 96 | sib->written = le64toh(num); |
| 97 | memcpy(&num, meta_buf + LYB_SIZE_BYTES, LYB_INCHUNK_BYTES); |
| 98 | sib->inner_chunks = le64toh(num); |
| 99 | |
| 100 | /* remember whether there is a following chunk or not */ |
| 101 | sib->position = (sib->written == LYB_SIZE_MAX ? 1 : 0); |
| 102 | } |
| 103 | |
| 104 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 105 | * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned. |
| 106 | * |
| 107 | * @param[in] buf Destination buffer. |
| 108 | * @param[in] count Number of bytes to read. |
| 109 | * @param[in] lybctx LYB context. |
| 110 | */ |
| 111 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 112 | lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 113 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 114 | LY_ARRAY_COUNT_TYPE u; |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 115 | struct lyd_lyb_sibling *empty; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 116 | size_t to_read; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 117 | |
| 118 | assert(lybctx); |
| 119 | |
| 120 | while (1) { |
| 121 | /* check for fully-read (empty) data chunks */ |
| 122 | to_read = count; |
| 123 | empty = NULL; |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 124 | LY_ARRAY_FOR(lybctx->siblings, u) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 125 | /* we want the innermost chunks resolved first, so replace previous empty chunks, |
| 126 | * also ignore chunks that are completely finished, there is nothing for us to do */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 127 | if ((lybctx->siblings[u].written <= to_read) && lybctx->siblings[u].position) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 128 | /* empty chunk, do not read more */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 129 | to_read = lybctx->siblings[u].written; |
| 130 | empty = &lybctx->siblings[u]; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | if (!empty && !count) { |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | /* we are actually reading some data, not just finishing another chunk */ |
| 139 | if (to_read) { |
| 140 | if (buf) { |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 141 | ly_in_read(lybctx->in, buf, to_read); |
| 142 | } else { |
| 143 | ly_in_skip(lybctx->in, to_read); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 144 | } |
| 145 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 146 | LY_ARRAY_FOR(lybctx->siblings, u) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 147 | /* decrease all written counters */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 148 | lybctx->siblings[u].written -= to_read; |
| 149 | assert(lybctx->siblings[u].written <= LYB_SIZE_MAX); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 150 | } |
| 151 | /* decrease count/buf */ |
| 152 | count -= to_read; |
| 153 | if (buf) { |
| 154 | buf += to_read; |
| 155 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | if (empty) { |
| 159 | /* read the next chunk meta information */ |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 160 | lyb_read_sibling_meta(empty, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 161 | } |
| 162 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * @brief Read a number. |
| 167 | * |
| 168 | * @param[in] num Destination buffer. |
| 169 | * @param[in] num_size Size of @p num. |
| 170 | * @param[in] bytes Number of bytes to read. |
| 171 | * @param[in] lybctx LYB context. |
| 172 | */ |
| 173 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 174 | lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 175 | { |
| 176 | uint64_t buf = 0; |
| 177 | |
| 178 | lyb_read((uint8_t *)&buf, bytes, lybctx); |
| 179 | |
| 180 | /* correct byte order */ |
| 181 | buf = le64toh(buf); |
| 182 | |
| 183 | switch (num_size) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 184 | case sizeof(uint8_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 185 | *((uint8_t *)num) = buf; |
| 186 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 187 | case sizeof(uint16_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 188 | *((uint16_t *)num) = buf; |
| 189 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 190 | case sizeof(uint32_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 191 | *((uint32_t *)num) = buf; |
| 192 | break; |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 193 | case sizeof(uint64_t): |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 194 | *((uint64_t *)num) = buf; |
| 195 | break; |
| 196 | default: |
| 197 | LOGINT(lybctx->ctx); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * @brief Read a string. |
| 203 | * |
| 204 | * @param[in] str Destination buffer, is allocated. |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 205 | * @param[in] len_size Number of bytes on which the length of the string is written. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 206 | * @param[in] lybctx LYB context. |
| 207 | * @return LY_ERR value. |
| 208 | */ |
| 209 | static LY_ERR |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 210 | lyb_read_string(char **str, uint8_t len_size, struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 211 | { |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 212 | uint64_t len = 0; |
| 213 | |
| 214 | assert((len_size == 1) || (len_size == 2) || (len_size == 4) || (len_size == 8)); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 215 | |
| 216 | *str = NULL; |
| 217 | |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 218 | lyb_read_number(&len, sizeof len, len_size, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 219 | |
| 220 | *str = malloc((len + 1) * sizeof **str); |
| 221 | LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM); |
| 222 | |
| 223 | lyb_read((uint8_t *)*str, len, lybctx); |
| 224 | |
Michal Vasko | cebbae5 | 2021-05-31 11:11:36 +0200 | [diff] [blame] | 225 | (*str)[len] = '\0'; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 226 | return LY_SUCCESS; |
| 227 | } |
| 228 | |
| 229 | /** |
aPiecek | d5796f9 | 2021-09-30 13:39:37 +0200 | [diff] [blame] | 230 | * @brief Skip string. |
| 231 | * |
| 232 | * @param[in] len_size Number of bytes on which the length of the string is written. |
| 233 | * @param[in] lybctx LYB context. |
| 234 | */ |
| 235 | static void |
| 236 | lyb_skip_string(uint8_t len_size, struct lylyb_ctx *lybctx) |
| 237 | { |
| 238 | size_t len = 0; |
| 239 | |
| 240 | lyb_read_number(&len, sizeof len, len_size, lybctx); |
| 241 | |
| 242 | lyb_read(NULL, len, lybctx); |
| 243 | } |
| 244 | |
| 245 | /** |
aPiecek | 91eec23 | 2021-09-09 15:42:37 +0200 | [diff] [blame] | 246 | * @brief Read value of term node. |
aPiecek | ea304e3 | 2021-08-18 09:13:47 +0200 | [diff] [blame] | 247 | * |
aPiecek | aa5b70a | 2021-08-30 08:33:25 +0200 | [diff] [blame] | 248 | * @param[in] term Compiled term node. |
aPiecek | ea304e3 | 2021-08-18 09:13:47 +0200 | [diff] [blame] | 249 | * @param[out] term_value Set to term node value in dynamically |
| 250 | * allocated memory. The caller must release it. |
| 251 | * @param[out] term_value_len Value length in bytes. The zero byte is |
| 252 | * always included and is not counted. |
| 253 | * @param[in,out] lybctx LYB context. |
| 254 | * @return LY_ERR value. |
| 255 | */ |
| 256 | static LY_ERR |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 257 | lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint64_t *term_value_len, |
aPiecek | 91eec23 | 2021-09-09 15:42:37 +0200 | [diff] [blame] | 258 | struct lylyb_ctx *lybctx) |
aPiecek | ea304e3 | 2021-08-18 09:13:47 +0200 | [diff] [blame] | 259 | { |
| 260 | uint32_t allocated_size; |
aPiecek | aa5b70a | 2021-08-30 08:33:25 +0200 | [diff] [blame] | 261 | int32_t lyb_data_len; |
| 262 | struct lysc_type_leafref *type_lf; |
aPiecek | ea304e3 | 2021-08-18 09:13:47 +0200 | [diff] [blame] | 263 | |
aPiecek | aa5b70a | 2021-08-30 08:33:25 +0200 | [diff] [blame] | 264 | assert(term && term_value && term_value_len && lybctx); |
aPiecek | ea304e3 | 2021-08-18 09:13:47 +0200 | [diff] [blame] | 265 | |
aPiecek | aa5b70a | 2021-08-30 08:33:25 +0200 | [diff] [blame] | 266 | /* Find out the size from @ref howtoDataLYB. */ |
| 267 | if (term->type->basetype == LY_TYPE_LEAFREF) { |
| 268 | /* Leafref itself is ignored, the target is loaded directly. */ |
| 269 | type_lf = (struct lysc_type_leafref *)term->type; |
| 270 | lyb_data_len = type_lf->realtype->plugin->lyb_data_len; |
| 271 | } else { |
| 272 | lyb_data_len = term->type->plugin->lyb_data_len; |
| 273 | } |
| 274 | |
| 275 | if (lyb_data_len < 0) { |
| 276 | /* Parse value size. */ |
| 277 | lyb_read_number(term_value_len, sizeof *term_value_len, |
| 278 | sizeof *term_value_len, lybctx); |
| 279 | } else { |
| 280 | /* Data size is fixed. */ |
| 281 | *term_value_len = lyb_data_len; |
| 282 | } |
aPiecek | ea304e3 | 2021-08-18 09:13:47 +0200 | [diff] [blame] | 283 | |
| 284 | /* Allocate memory. */ |
| 285 | allocated_size = *term_value_len + 1; |
| 286 | *term_value = malloc(allocated_size * sizeof **term_value); |
| 287 | LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM); |
| 288 | |
| 289 | if (*term_value_len > 0) { |
| 290 | /* Parse value. */ |
| 291 | lyb_read(*term_value, *term_value_len, lybctx); |
| 292 | } |
| 293 | |
| 294 | /* Add extra zero byte regardless of whether it is string or not. */ |
| 295 | (*term_value)[allocated_size - 1] = 0; |
| 296 | |
| 297 | return LY_SUCCESS; |
| 298 | } |
| 299 | |
| 300 | /** |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 301 | * @brief Stop the current "siblings" - change LYB context state. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 302 | * |
| 303 | * @param[in] lybctx LYB context. |
| 304 | * @return LY_ERR value. |
| 305 | */ |
| 306 | static LY_ERR |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 307 | lyb_read_stop_siblings(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 308 | { |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 309 | if (LYB_LAST_SIBLING(lybctx).written) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 310 | LOGINT_RET(lybctx->ctx); |
| 311 | } |
| 312 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 313 | LY_ARRAY_DECREMENT(lybctx->siblings); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 314 | return LY_SUCCESS; |
| 315 | } |
| 316 | |
| 317 | /** |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 318 | * @brief Start a new "siblings" - change LYB context state but also read the expected metadata. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 319 | * |
| 320 | * @param[in] lybctx LYB context. |
| 321 | * @return LY_ERR value. |
| 322 | */ |
| 323 | static LY_ERR |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 324 | lyb_read_start_siblings(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 325 | { |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 326 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 327 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 328 | u = LY_ARRAY_COUNT(lybctx->siblings); |
| 329 | if (u == lybctx->sibling_size) { |
| 330 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM); |
| 331 | lybctx->sibling_size = u + LYB_SIBLING_STEP; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 332 | } |
| 333 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 334 | LY_ARRAY_INCREMENT(lybctx->siblings); |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 335 | lyb_read_sibling_meta(&LYB_LAST_SIBLING(lybctx), lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 336 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 337 | return LY_SUCCESS; |
| 338 | } |
| 339 | |
| 340 | /** |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 341 | * @brief Read YANG model info. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 342 | * |
| 343 | * @param[in] lybctx LYB context. |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 344 | * @param[out] mod_name Module name, if any. |
| 345 | * @param[out] mod_rev Module revision, "" if none. |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 346 | * @param[in,out] feat_set Set to add the names of enabled features to. If not set, enabled features are not parsed. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 347 | * @return LY_ERR value. |
| 348 | */ |
| 349 | static LY_ERR |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 350 | lyb_read_model(struct lylyb_ctx *lybctx, char **mod_name, char mod_rev[], struct ly_set *feat_set) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 351 | { |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 352 | uint16_t i, rev, length; |
| 353 | char *str; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 354 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 355 | *mod_name = NULL; |
| 356 | mod_rev[0] = '\0'; |
aPiecek | 339bdc3 | 2021-09-10 08:42:36 +0200 | [diff] [blame] | 357 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 358 | lyb_read_number(&length, 2, 2, lybctx); |
| 359 | if (!length) { |
| 360 | return LY_SUCCESS; |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 361 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 362 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 363 | /* module name */ |
Michal Vasko | 0d156b9 | 2022-04-01 12:27:04 +0200 | [diff] [blame] | 364 | *mod_name = malloc(length + 1); |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 365 | LY_CHECK_ERR_RET(!*mod_name, LOGMEM(lybctx->ctx), LY_EMEM); |
| 366 | lyb_read(((uint8_t *)*mod_name), length, lybctx); |
| 367 | (*mod_name)[length] = '\0'; |
| 368 | |
| 369 | /* module revision */ |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 370 | lyb_read_number(&rev, sizeof rev, 2, lybctx); |
| 371 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 372 | if (rev) { |
Radek Krejci | f13b87b | 2020-12-01 22:02:17 +0100 | [diff] [blame] | 373 | sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET, |
| 374 | (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK); |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 377 | if (!feat_set) { |
| 378 | /* enabled features not printed */ |
| 379 | return LY_SUCCESS; |
| 380 | } |
| 381 | |
| 382 | /* enabled feature count */ |
| 383 | lyb_read_number(&length, sizeof length, sizeof length, lybctx); |
| 384 | if (!length) { |
| 385 | return LY_SUCCESS; |
| 386 | } |
| 387 | |
| 388 | /* enabled features */ |
| 389 | for (i = 0; i < length; ++i) { |
Michal Vasko | 7154133 | 2022-12-01 16:17:20 +0100 | [diff] [blame] | 390 | LY_CHECK_RET(lyb_read_string(&str, sizeof length, lybctx)); |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 391 | ly_set_add(feat_set, str, 1, NULL); |
| 392 | } |
| 393 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 394 | return LY_SUCCESS; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * @brief Parse YANG model info. |
| 399 | * |
| 400 | * @param[in] lybctx LYB context. |
| 401 | * @param[in] parse_options Flag with options for parsing. |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 402 | * @param[in] with_features Whether the enabled features were also printed and should be read. |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 403 | * @param[out] mod Parsed module. |
| 404 | * @return LY_ERR value. |
| 405 | */ |
| 406 | static LY_ERR |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 407 | lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, ly_bool with_features, const struct lys_module **mod) |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 408 | { |
| 409 | LY_ERR ret = LY_SUCCESS; |
| 410 | const struct lys_module *m = NULL; |
| 411 | char *mod_name = NULL, mod_rev[LY_REV_SIZE]; |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 412 | struct ly_set feat_set = {0}; |
| 413 | struct lysp_feature *f = NULL; |
| 414 | uint32_t i, idx = 0; |
| 415 | ly_bool enabled; |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 416 | |
| 417 | /* read module info */ |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 418 | if ((ret = lyb_read_model(lybctx, &mod_name, mod_rev, with_features ? &feat_set : NULL))) { |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 419 | goto cleanup; |
| 420 | } |
| 421 | |
| 422 | /* get the module */ |
| 423 | if (mod_rev[0]) { |
| 424 | m = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev); |
| 425 | if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !m) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 426 | /* try to use an updated module */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 427 | m = ly_ctx_get_module_implemented(lybctx->ctx, mod_name); |
| 428 | if (m && (!m->revision || (strcmp(m->revision, mod_rev) < 0))) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 429 | /* not an implemented module in a newer revision */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 430 | m = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | } else { |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 434 | m = ly_ctx_get_module_latest(lybctx->ctx, mod_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 435 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 436 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 437 | if (!m || !m->implemented) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 438 | if (parse_options & LYD_PARSE_STRICT) { |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 439 | if (!m) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 440 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".", |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 441 | mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : ""); |
| 442 | } else if (!m->implemented) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 443 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.", |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 444 | mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : ""); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 445 | } |
| 446 | ret = LY_EINVAL; |
| 447 | goto cleanup; |
| 448 | } |
| 449 | |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 450 | goto cleanup; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 453 | if (with_features) { |
| 454 | /* check features */ |
| 455 | while ((f = lysp_feature_next(f, m->parsed, &idx))) { |
| 456 | enabled = 0; |
| 457 | for (i = 0; i < feat_set.count; ++i) { |
| 458 | if (!strcmp(feat_set.objs[i], f->name)) { |
| 459 | enabled = 1; |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | if (enabled && !(f->flags & LYS_FENABLED)) { |
| 465 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s\" has \"%s\" feature disabled.", |
| 466 | mod_name, f->name); |
| 467 | ret = LY_EINVAL; |
| 468 | goto cleanup; |
| 469 | } else if (!enabled && (f->flags & LYS_FENABLED)) { |
| 470 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s\" has \"%s\" feature enabled.", |
| 471 | mod_name, f->name); |
| 472 | ret = LY_EINVAL; |
| 473 | goto cleanup; |
| 474 | } |
| 475 | } |
Michal Vasko | 85d9edc | 2021-04-22 09:15:05 +0200 | [diff] [blame] | 476 | } |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 477 | |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 478 | /* fill cached hashes, if not already */ |
| 479 | lyb_cache_module_hash(m); |
| 480 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 481 | cleanup: |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 482 | *mod = m; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 483 | free(mod_name); |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 484 | ly_set_erase(&feat_set, free); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * @brief Parse YANG node metadata. |
| 490 | * |
| 491 | * @param[in] lybctx LYB context. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 492 | * @param[in] sparent Schema parent node of the metadata. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 493 | * @param[out] meta Parsed metadata. |
| 494 | * @return LY_ERR value. |
| 495 | */ |
| 496 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 497 | lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 498 | { |
| 499 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 500 | ly_bool dynamic; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 501 | uint8_t i, count = 0; |
Michal Vasko | 1e5d561 | 2020-07-03 13:29:26 +0200 | [diff] [blame] | 502 | char *meta_name = NULL, *meta_value; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 503 | const struct lys_module *mod; |
| 504 | |
| 505 | /* read number of attributes stored */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 506 | lyb_read(&count, 1, lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 507 | |
| 508 | /* read attributes */ |
| 509 | for (i = 0; i < count; ++i) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 510 | /* find model */ |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 511 | ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 512 | LY_CHECK_GOTO(ret, cleanup); |
| 513 | |
| 514 | if (!mod) { |
aPiecek | d5796f9 | 2021-09-30 13:39:37 +0200 | [diff] [blame] | 515 | /* skip meta name */ |
| 516 | lyb_skip_string(sizeof(uint16_t), lybctx->lybctx); |
| 517 | |
| 518 | /* skip meta value */ |
| 519 | lyb_skip_string(sizeof(uint16_t), lybctx->lybctx); |
| 520 | continue; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* meta name */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 524 | ret = lyb_read_string(&meta_name, sizeof(uint16_t), lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 525 | LY_CHECK_GOTO(ret, cleanup); |
| 526 | |
| 527 | /* meta value */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 528 | ret = lyb_read_string(&meta_value, sizeof(uint64_t), lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 529 | LY_CHECK_GOTO(ret, cleanup); |
| 530 | dynamic = 1; |
| 531 | |
| 532 | /* create metadata */ |
Michal Vasko | c8a230d | 2020-08-14 12:17:10 +0200 | [diff] [blame] | 533 | ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value, |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 534 | ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, sparent); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 535 | |
| 536 | /* free strings */ |
| 537 | free(meta_name); |
| 538 | meta_name = NULL; |
| 539 | if (dynamic) { |
| 540 | free(meta_value); |
| 541 | dynamic = 0; |
| 542 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 543 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 544 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | cleanup: |
| 548 | free(meta_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 549 | if (ret) { |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 550 | lyd_free_meta_siblings(*meta); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 551 | *meta = NULL; |
| 552 | } |
| 553 | return ret; |
| 554 | } |
| 555 | |
| 556 | /** |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 557 | * @brief Parse format-specific prefix data. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 558 | * |
| 559 | * @param[in] lybctx LYB context. |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 560 | * @param[in] format Prefix data format. |
| 561 | * @param[out] prefix_data Parsed prefix data. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 562 | * @return LY_ERR value. |
| 563 | */ |
| 564 | static LY_ERR |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 565 | lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 566 | { |
| 567 | LY_ERR ret = LY_SUCCESS; |
| 568 | uint8_t count, i; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 569 | struct ly_set *set = NULL; |
| 570 | struct lyxml_ns *ns = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 571 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 572 | switch (format) { |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 573 | case LY_VALUE_XML: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 574 | /* read count */ |
| 575 | lyb_read(&count, 1, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 576 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 577 | /* read all NS elements */ |
| 578 | LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 579 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 580 | for (i = 0; i < count; ++i) { |
| 581 | ns = calloc(1, sizeof *ns); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 582 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 583 | /* prefix */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 584 | LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, sizeof(uint16_t), lybctx), cleanup); |
Michal Vasko | a3494b4 | 2022-02-28 13:58:34 +0100 | [diff] [blame] | 585 | if (!strlen(ns->prefix)) { |
| 586 | free(ns->prefix); |
| 587 | ns->prefix = NULL; |
| 588 | } |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 589 | |
| 590 | /* namespace */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 591 | LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, sizeof(uint16_t), lybctx), cleanup); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 592 | |
| 593 | LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup); |
| 594 | ns = NULL; |
| 595 | } |
| 596 | |
| 597 | *prefix_data = set; |
| 598 | break; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 599 | case LY_VALUE_JSON: |
Radek Krejci | f994364 | 2021-04-26 10:18:21 +0200 | [diff] [blame] | 600 | case LY_VALUE_LYB: |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 601 | /* nothing stored */ |
| 602 | break; |
| 603 | default: |
| 604 | LOGINT(lybctx->ctx); |
| 605 | ret = LY_EINT; |
| 606 | break; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | cleanup: |
| 610 | if (ret) { |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 611 | ly_free_prefix_data(format, set); |
| 612 | if (ns) { |
| 613 | free(ns->prefix); |
| 614 | free(ns->uri); |
| 615 | free(ns); |
| 616 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 617 | } |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * @brief Parse opaque attributes. |
| 623 | * |
| 624 | * @param[in] lybctx LYB context. |
| 625 | * @param[out] attr Parsed attributes. |
| 626 | * @return LY_ERR value. |
| 627 | */ |
| 628 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 629 | lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 630 | { |
| 631 | LY_ERR ret = LY_SUCCESS; |
| 632 | uint8_t count, i; |
Michal Vasko | 486e4f9 | 2021-07-01 13:12:32 +0200 | [diff] [blame] | 633 | struct lyd_attr *attr2 = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 634 | char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL; |
Radek Krejci | 857189e | 2020-09-01 13:26:36 +0200 | [diff] [blame] | 635 | ly_bool dynamic = 0; |
Radek Krejci | 8df109d | 2021-04-23 12:19:08 +0200 | [diff] [blame] | 636 | LY_VALUE_FORMAT format = 0; |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 637 | void *val_prefix_data = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 638 | |
| 639 | /* read count */ |
| 640 | lyb_read(&count, 1, lybctx); |
| 641 | |
| 642 | /* read attributes */ |
| 643 | for (i = 0; i < count; ++i) { |
Michal Vasko | 0fdcd24 | 2020-11-11 19:12:30 +0100 | [diff] [blame] | 644 | /* prefix, may be empty */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 645 | ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 646 | LY_CHECK_GOTO(ret, cleanup); |
| 647 | if (!prefix[0]) { |
| 648 | free(prefix); |
| 649 | prefix = NULL; |
| 650 | } |
| 651 | |
| 652 | /* namespace, may be empty */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 653 | ret = lyb_read_string(&module_name, sizeof(uint16_t), lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 654 | LY_CHECK_GOTO(ret, cleanup); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 655 | if (!module_name[0]) { |
| 656 | free(module_name); |
| 657 | module_name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | /* name */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 661 | ret = lyb_read_string(&name, sizeof(uint16_t), lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 662 | LY_CHECK_GOTO(ret, cleanup); |
| 663 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 664 | /* format */ |
Michal Vasko | 403beac | 2021-08-24 08:27:52 +0200 | [diff] [blame] | 665 | lyb_read_number(&format, sizeof format, 1, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 666 | |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 667 | /* value prefixes */ |
| 668 | ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data); |
| 669 | LY_CHECK_GOTO(ret, cleanup); |
| 670 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 671 | /* value */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 672 | ret = lyb_read_string(&value, sizeof(uint64_t), lybctx); |
Michal Vasko | 6b5cb2a | 2020-11-11 19:11:21 +0100 | [diff] [blame] | 673 | LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 674 | dynamic = 1; |
| 675 | |
| 676 | /* attr2 is always changed to the created attribute */ |
Michal Vasko | 501af03 | 2020-11-11 20:27:44 +0100 | [diff] [blame] | 677 | ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name, |
Michal Vasko | bb51279 | 2021-07-01 13:12:49 +0200 | [diff] [blame] | 678 | ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 679 | LY_CHECK_GOTO(ret, cleanup); |
| 680 | |
| 681 | free(prefix); |
| 682 | prefix = NULL; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 683 | free(module_name); |
| 684 | module_name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 685 | free(name); |
| 686 | name = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 687 | assert(!dynamic); |
| 688 | value = NULL; |
| 689 | |
| 690 | if (!*attr) { |
| 691 | *attr = attr2; |
| 692 | } |
| 693 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 694 | LY_CHECK_GOTO(ret, cleanup); |
| 695 | } |
| 696 | |
| 697 | cleanup: |
| 698 | free(prefix); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 699 | free(module_name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 700 | free(name); |
| 701 | if (dynamic) { |
| 702 | free(value); |
| 703 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 704 | if (ret) { |
Radek Krejci | 011e4aa | 2020-09-04 15:22:31 +0200 | [diff] [blame] | 705 | lyd_free_attr_siblings(lybctx->ctx, *attr); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 706 | *attr = NULL; |
| 707 | } |
| 708 | return ret; |
| 709 | } |
| 710 | |
| 711 | /** |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame] | 712 | * @brief Fill @p hash with hash values. |
| 713 | * |
| 714 | * @param[in] lybctx LYB context. |
| 715 | * @param[in,out] hash Pointer to the array in which the hash values are to be written. |
| 716 | * @param[out] hash_count Number of hashes in @p hash. |
| 717 | * @return LY_ERR value. |
| 718 | */ |
| 719 | static LY_ERR |
| 720 | lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count) |
| 721 | { |
| 722 | uint8_t i = 0, j; |
| 723 | |
| 724 | /* read the first hash */ |
| 725 | lyb_read(&hash[0], sizeof *hash, lybctx); |
| 726 | |
| 727 | if (!hash[0]) { |
| 728 | *hash_count = i + 1; |
| 729 | return LY_SUCCESS; |
| 730 | } |
| 731 | |
| 732 | /* based on the first hash read all the other ones, if any */ |
| 733 | for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) { |
| 734 | if (i > LYB_HASH_BITS) { |
| 735 | LOGINT_RET(lybctx->ctx); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | /* move the first hash on its accurate position */ |
| 740 | hash[i] = hash[0]; |
| 741 | |
| 742 | /* read the rest of hashes */ |
| 743 | for (j = i; j; --j) { |
| 744 | lyb_read(&hash[j - 1], sizeof *hash, lybctx); |
| 745 | |
| 746 | /* correct collision ID */ |
| 747 | assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1))); |
| 748 | /* preceded with zeros */ |
| 749 | assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1))))); |
| 750 | } |
| 751 | |
| 752 | *hash_count = i + 1; |
| 753 | |
| 754 | return LY_SUCCESS; |
| 755 | } |
| 756 | |
| 757 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 758 | * @brief Check whether a schema node matches a hash(es). |
| 759 | * |
| 760 | * @param[in] sibling Schema node to check. |
| 761 | * @param[in] hash Hash array to check. |
| 762 | * @param[in] hash_count Number of hashes in @p hash. |
| 763 | * @return non-zero if matches, |
| 764 | * @return 0 if not. |
| 765 | */ |
| 766 | static int |
| 767 | lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count) |
| 768 | { |
| 769 | LYB_HASH sibling_hash; |
| 770 | uint8_t i; |
| 771 | |
| 772 | /* compare all the hashes starting from collision ID 0 */ |
| 773 | for (i = 0; i < hash_count; ++i) { |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 774 | sibling_hash = lyb_get_hash(sibling, i); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 775 | if (sibling_hash != hash[i]) { |
| 776 | return 0; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | return 1; |
| 781 | } |
| 782 | |
| 783 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 784 | * @brief Parse schema node hash. |
| 785 | * |
| 786 | * @param[in] lybctx LYB context. |
| 787 | * @param[in] sparent Schema parent, must be set if @p mod is not. |
| 788 | * @param[in] mod Module of the top-level node, must be set if @p sparent is not. |
| 789 | * @param[out] snode Parsed found schema node, may be NULL if opaque. |
| 790 | * @return LY_ERR value. |
| 791 | */ |
| 792 | static LY_ERR |
| 793 | lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod, |
Radek Krejci | 0f96988 | 2020-08-21 16:56:47 +0200 | [diff] [blame] | 794 | const struct lysc_node **snode) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 795 | { |
| 796 | LY_ERR ret; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 797 | const struct lysc_node *sibling; |
| 798 | LYB_HASH hash[LYB_HASH_BITS - 1]; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 799 | uint32_t getnext_opts; |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame] | 800 | uint8_t hash_count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 801 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 802 | *snode = NULL; |
| 803 | |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame] | 804 | ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count); |
| 805 | LY_CHECK_RET(ret); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 806 | |
| 807 | if (!hash[0]) { |
| 808 | /* opaque node */ |
| 809 | return LY_SUCCESS; |
| 810 | } |
| 811 | |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame] | 812 | getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 813 | |
| 814 | /* find our node with matching hashes */ |
| 815 | sibling = NULL; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 816 | while (1) { |
| 817 | if (!sparent && lybctx->ext) { |
| 818 | sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts); |
| 819 | } else { |
| 820 | sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts); |
| 821 | } |
| 822 | if (!sibling) { |
| 823 | break; |
| 824 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 825 | /* skip schema nodes from models not present during printing */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 826 | if (((sibling->module->ctx != lybctx->lybctx->ctx) || lyb_has_schema_model(sibling, lybctx->lybctx->models)) && |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame] | 827 | lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 828 | /* match found */ |
| 829 | break; |
| 830 | } |
| 831 | } |
| 832 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 833 | if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 834 | if (lybctx->ext) { |
| 835 | LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.", |
| 836 | lybctx->ext->def->name); |
| 837 | } else if (mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 838 | LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 839 | " from \"%s\".", mod->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 840 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 841 | LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node" |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 842 | " of \"%s\".", sparent->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 843 | } |
| 844 | return LY_EVALID; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 845 | } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | *snode = sibling; |
| 850 | return LY_SUCCESS; |
| 851 | } |
| 852 | |
| 853 | /** |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 854 | * @brief Parse schema node name of a nested extension data node. |
| 855 | * |
| 856 | * @param[in] lybctx LYB context. |
| 857 | * @param[in] parent Data parent. |
| 858 | * @param[in] mod_name Module name of the node. |
| 859 | * @param[out] snode Parsed found schema node of a nested extension. |
| 860 | * @return LY_ERR value. |
| 861 | */ |
| 862 | static LY_ERR |
| 863 | lyb_parse_schema_nested_ext(struct lyd_lyb_ctx *lybctx, const struct lyd_node *parent, const char *mod_name, |
| 864 | const struct lysc_node **snode) |
| 865 | { |
| 866 | LY_ERR rc = LY_SUCCESS, r; |
| 867 | char *name = NULL; |
| 868 | struct lysc_ext_instance *ext; |
| 869 | |
| 870 | assert(parent); |
| 871 | |
| 872 | /* read schema node name */ |
| 873 | LY_CHECK_GOTO(rc = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx), cleanup); |
| 874 | |
| 875 | /* check for extension data */ |
| 876 | r = ly_nested_ext_schema(parent, NULL, mod_name, mod_name ? strlen(mod_name) : 0, LY_VALUE_JSON, NULL, name, |
| 877 | strlen(name), snode, &ext); |
| 878 | if (r == LY_ENOT) { |
| 879 | /* failed to parse */ |
| 880 | LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "Failed to parse node \"%s\" as nested extension instance data.", name); |
| 881 | rc = LY_EINVAL; |
| 882 | goto cleanup; |
| 883 | } else if (r) { |
| 884 | /* error */ |
| 885 | rc = r; |
| 886 | goto cleanup; |
| 887 | } |
| 888 | |
| 889 | /* fill cached hashes in the module, it may be from a different context */ |
| 890 | lyb_cache_module_hash((*snode)->module); |
| 891 | |
| 892 | cleanup: |
| 893 | free(name); |
| 894 | return rc; |
| 895 | } |
| 896 | |
| 897 | /** |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 898 | * @brief Read until the end of the current siblings. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 899 | * |
| 900 | * @param[in] lybctx LYB context. |
| 901 | */ |
| 902 | static void |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 903 | lyb_skip_siblings(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 904 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 905 | do { |
| 906 | /* first skip any meta information inside */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 907 | ly_in_skip(lybctx->in, LYB_LAST_SIBLING(lybctx).inner_chunks * LYB_META_BYTES); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 908 | |
| 909 | /* then read data */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 910 | lyb_read(NULL, LYB_LAST_SIBLING(lybctx).written, lybctx); |
| 911 | } while (LYB_LAST_SIBLING(lybctx).written); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | /** |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 915 | * @brief Insert new node to @p parsed set. |
| 916 | * |
| 917 | * Also if needed, correct @p first_p. |
| 918 | * |
| 919 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 920 | * @param[in] parent Data parent of the sibling, must be set if @p first_p is not. |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 921 | * @param[in,out] node Parsed node to insertion. |
| 922 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 923 | * @param[out] parsed Set of all successfully parsed nodes. |
| 924 | * @return LY_ERR value. |
| 925 | */ |
| 926 | static void |
| 927 | lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p, |
| 928 | struct ly_set *parsed) |
| 929 | { |
| 930 | /* insert, keep first pointer correct */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 931 | if (parent && (LYD_CTX(parent) != LYD_CTX(node))) { |
Michal Vasko | 193dacd | 2022-10-13 08:43:05 +0200 | [diff] [blame] | 932 | lyplg_ext_insert(parent, node); |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 933 | } else { |
aPiecek | 1462ab1 | 2024-02-07 09:13:29 +0100 | [diff] [blame] | 934 | lyd_insert_node(parent, first_p, node, |
| 935 | lybctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT); |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 936 | } |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 937 | while (!parent && (*first_p)->prev->next) { |
| 938 | *first_p = (*first_p)->prev; |
| 939 | } |
| 940 | |
| 941 | /* rememeber a successfully parsed node */ |
| 942 | if (parsed) { |
| 943 | ly_set_add(parsed, node, 1, NULL); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * @brief Finish parsing the opaq node. |
| 949 | * |
| 950 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 951 | * @param[in] parent Data parent of the sibling, must be set if @p first_p is not. |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 952 | * @param[in] flags Node flags to set. |
| 953 | * @param[in,out] attr Attributes to be attached. Finally set to NULL. |
| 954 | * @param[in,out] node Parsed opaq node to finish. |
| 955 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 956 | * @param[out] parsed Set of all successfully parsed nodes. |
| 957 | * @return LY_ERR value. |
| 958 | */ |
| 959 | static void |
| 960 | lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr, |
| 961 | struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed) |
| 962 | { |
| 963 | struct lyd_attr *iter; |
| 964 | |
| 965 | /* set flags */ |
| 966 | (*node)->flags = flags; |
| 967 | |
| 968 | /* add attributes */ |
| 969 | assert(!(*node)->schema); |
| 970 | LY_LIST_FOR(*attr, iter) { |
| 971 | iter->parent = (struct lyd_node_opaq *)*node; |
| 972 | } |
| 973 | ((struct lyd_node_opaq *)*node)->attr = *attr; |
| 974 | *attr = NULL; |
| 975 | |
| 976 | lyb_insert_node(lybctx, parent, *node, first_p, parsed); |
| 977 | *node = NULL; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * @brief Finish parsing the node. |
| 982 | * |
| 983 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 984 | * @param[in] parent Data parent of the sibling, must be set if @p first_p is not. |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 985 | * @param[in] flags Node flags to set. |
| 986 | * @param[in,out] meta Metadata to be attached. Finally set to NULL. |
| 987 | * @param[in,out] node Parsed node to finish. |
| 988 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 989 | * @param[out] parsed Set of all successfully parsed nodes. |
| 990 | * @return LY_ERR value. |
| 991 | */ |
| 992 | static void |
| 993 | lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta, |
| 994 | struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed) |
| 995 | { |
| 996 | struct lyd_meta *m; |
| 997 | |
| 998 | /* set flags */ |
| 999 | (*node)->flags = flags; |
| 1000 | |
| 1001 | /* add metadata */ |
| 1002 | LY_LIST_FOR(*meta, m) { |
| 1003 | m->parent = *node; |
| 1004 | } |
| 1005 | (*node)->meta = *meta; |
| 1006 | *meta = NULL; |
| 1007 | |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1008 | /* insert into parent */ |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1009 | lyb_insert_node(lybctx, parent, *node, first_p, parsed); |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1010 | |
Michal Vasko | eba2311 | 2022-08-26 08:35:41 +0200 | [diff] [blame] | 1011 | if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) { |
| 1012 | /* store for ext instance node validation, if needed */ |
| 1013 | (void)lyd_validate_node_ext(*node, &lybctx->ext_node); |
| 1014 | } |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1015 | |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1016 | *node = NULL; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
aPiecek | 6fdac2f | 2021-09-10 11:21:10 +0200 | [diff] [blame] | 1020 | * @brief Parse header for non-opaq node. |
| 1021 | * |
| 1022 | * @param[in] lybctx LYB context. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1023 | * @param[in] sparent Schema parent node of the metadata. |
aPiecek | 6fdac2f | 2021-09-10 11:21:10 +0200 | [diff] [blame] | 1024 | * @param[out] flags Parsed node flags. |
| 1025 | * @param[out] meta Parsed metadata of the node. |
| 1026 | * @return LY_ERR value. |
| 1027 | */ |
| 1028 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1029 | lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, uint32_t *flags, struct lyd_meta **meta) |
aPiecek | 6fdac2f | 2021-09-10 11:21:10 +0200 | [diff] [blame] | 1030 | { |
| 1031 | LY_ERR ret; |
| 1032 | |
| 1033 | /* create and read metadata */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1034 | ret = lyb_parse_metadata(lybctx, sparent, meta); |
aPiecek | 6fdac2f | 2021-09-10 11:21:10 +0200 | [diff] [blame] | 1035 | LY_CHECK_RET(ret); |
| 1036 | |
| 1037 | /* read flags */ |
| 1038 | lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx); |
| 1039 | |
| 1040 | return ret; |
| 1041 | } |
| 1042 | |
| 1043 | /** |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1044 | * @brief Create term node and fill it with value. |
| 1045 | * |
| 1046 | * @param[in] lybctx LYB context. |
| 1047 | * @param[in] snode Schema of the term node. |
| 1048 | * @param[out] node Created term node. |
| 1049 | * @return LY_ERR value. |
| 1050 | */ |
| 1051 | static LY_ERR |
| 1052 | lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node) |
| 1053 | { |
| 1054 | LY_ERR ret; |
| 1055 | ly_bool dynamic; |
| 1056 | uint8_t *term_value; |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1057 | uint64_t term_value_len; |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1058 | |
| 1059 | ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx); |
| 1060 | LY_CHECK_RET(ret); |
| 1061 | |
| 1062 | dynamic = 1; |
| 1063 | /* create node */ |
| 1064 | ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, |
| 1065 | term_value, term_value_len, &dynamic, LY_VALUE_LYB, |
| 1066 | NULL, LYD_HINT_DATA, node); |
| 1067 | if (dynamic) { |
| 1068 | free(term_value); |
| 1069 | } |
| 1070 | if (ret) { |
| 1071 | lyd_free_tree(*node); |
| 1072 | *node = NULL; |
| 1073 | } |
| 1074 | |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | /** |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 1079 | * @brief Validate inner node, autodelete default values nad create implicit nodes. |
| 1080 | * |
| 1081 | * @param[in,out] lybctx LYB context. |
| 1082 | * @param[in] snode Schema of the inner node. |
| 1083 | * @param[in] node Parsed inner node. |
| 1084 | * @return LY_ERR value. |
| 1085 | */ |
| 1086 | static LY_ERR |
| 1087 | lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node) |
| 1088 | { |
| 1089 | LY_ERR ret = LY_SUCCESS; |
| 1090 | uint32_t impl_opts; |
| 1091 | |
| 1092 | if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) { |
| 1093 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1094 | ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, 0, NULL); |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 1095 | LY_CHECK_RET(ret); |
| 1096 | |
| 1097 | /* add any missing default children */ |
| 1098 | impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0; |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1099 | ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_types, |
Michal Vasko | fcbd78f | 2022-08-26 08:34:15 +0200 | [diff] [blame] | 1100 | &lybctx->ext_node, impl_opts, NULL); |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 1101 | LY_CHECK_RET(ret); |
| 1102 | } |
| 1103 | |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | /** |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1108 | * @brief Parse opaq node. |
| 1109 | * |
| 1110 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1111 | * @param[in] parent Data parent of the sibling. |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1112 | * @param[in,out] first_p First top-level sibling. |
| 1113 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1114 | * @return LY_ERR value. |
| 1115 | */ |
| 1116 | static LY_ERR |
| 1117 | lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
| 1118 | { |
| 1119 | LY_ERR ret; |
| 1120 | struct lyd_node *node = NULL; |
| 1121 | struct lyd_attr *attr = NULL; |
| 1122 | char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL; |
| 1123 | ly_bool dynamic = 0; |
| 1124 | LY_VALUE_FORMAT format = 0; |
| 1125 | void *val_prefix_data = NULL; |
| 1126 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
| 1127 | uint32_t flags; |
| 1128 | |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1129 | /* parse opaq node attributes */ |
| 1130 | ret = lyb_parse_attributes(lybctx->lybctx, &attr); |
| 1131 | LY_CHECK_GOTO(ret, cleanup); |
| 1132 | |
| 1133 | /* read flags */ |
| 1134 | lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx); |
| 1135 | |
| 1136 | /* parse prefix */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1137 | ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1138 | LY_CHECK_GOTO(ret, cleanup); |
| 1139 | |
| 1140 | /* parse module key */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1141 | ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1142 | LY_CHECK_GOTO(ret, cleanup); |
| 1143 | |
| 1144 | /* parse name */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1145 | ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1146 | LY_CHECK_GOTO(ret, cleanup); |
| 1147 | |
| 1148 | /* parse value */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1149 | ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1150 | LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup); |
| 1151 | dynamic = 1; |
| 1152 | |
| 1153 | /* parse format */ |
| 1154 | lyb_read_number(&format, sizeof format, 1, lybctx->lybctx); |
| 1155 | |
| 1156 | /* parse value prefixes */ |
| 1157 | ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data); |
| 1158 | LY_CHECK_GOTO(ret, cleanup); |
| 1159 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1160 | if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) { |
Michal Vasko | efa96e6 | 2021-11-02 12:19:14 +0100 | [diff] [blame] | 1161 | /* skip children */ |
| 1162 | ret = lyb_read_start_siblings(lybctx->lybctx); |
| 1163 | LY_CHECK_GOTO(ret, cleanup); |
| 1164 | lyb_skip_siblings(lybctx->lybctx); |
| 1165 | ret = lyb_read_stop_siblings(lybctx->lybctx); |
| 1166 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1167 | goto cleanup; |
| 1168 | } |
| 1169 | |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1170 | /* create node */ |
| 1171 | ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key), |
Michal Vasko | ee81f81 | 2023-05-25 13:32:51 +0200 | [diff] [blame] | 1172 | value, strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA, &node); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1173 | LY_CHECK_GOTO(ret, cleanup); |
| 1174 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1175 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1176 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1177 | |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1178 | /* process children */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1179 | ret = lyb_parse_siblings(lybctx, node, NULL, NULL); |
| 1180 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1181 | |
| 1182 | /* register parsed opaq node */ |
| 1183 | lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed); |
| 1184 | assert(!attr && !node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1185 | LOG_LOCBACK(0, 1); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1186 | |
| 1187 | cleanup: |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1188 | if (node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1189 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1190 | } |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1191 | free(prefix); |
| 1192 | free(module_key); |
| 1193 | free(name); |
| 1194 | if (dynamic) { |
| 1195 | free(value); |
| 1196 | } |
| 1197 | lyd_free_attr_siblings(ctx, attr); |
| 1198 | lyd_free_tree(node); |
| 1199 | |
| 1200 | return ret; |
| 1201 | } |
| 1202 | |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1203 | /** |
| 1204 | * @brief Parse anydata or anyxml node. |
| 1205 | * |
| 1206 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1207 | * @param[in] parent Data parent of the sibling. |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1208 | * @param[in] snode Schema of the node to be parsed. |
| 1209 | * @param[in,out] first_p First top-level sibling. |
| 1210 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1211 | * @return LY_ERR value. |
| 1212 | */ |
| 1213 | static LY_ERR |
| 1214 | lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1215 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1216 | { |
| 1217 | LY_ERR ret; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1218 | struct lyd_node *node = NULL, *tree = NULL; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1219 | struct lyd_meta *meta = NULL; |
| 1220 | LYD_ANYDATA_VALUETYPE value_type; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1221 | struct ly_in *in; |
| 1222 | struct lyd_ctx *lydctx = NULL; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1223 | char *value = NULL; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1224 | uint32_t flags; |
| 1225 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
| 1226 | |
| 1227 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1228 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
Michal Vasko | edd4f25 | 2021-09-23 08:37:40 +0200 | [diff] [blame] | 1229 | LY_CHECK_GOTO(ret, error); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1230 | |
| 1231 | /* parse value type */ |
| 1232 | lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx); |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1233 | if ((value_type == LYD_ANYDATA_DATATREE) || ((snode->nodetype == LYS_ANYDATA) && (value_type != LYD_ANYDATA_LYB))) { |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1234 | LOGINT(ctx); |
| 1235 | ret = LY_EINT; |
| 1236 | goto error; |
| 1237 | } |
| 1238 | |
| 1239 | /* read anydata content */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1240 | ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1241 | LY_CHECK_GOTO(ret, error); |
| 1242 | |
| 1243 | if (value_type == LYD_ANYDATA_LYB) { |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1244 | /* parse LYB into a data tree */ |
| 1245 | LY_CHECK_RET(ly_in_new_memory(value, &in)); |
| 1246 | ret = lyd_parse_lyb(ctx, NULL, NULL, &tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0, |
| 1247 | LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, NULL, &lydctx); |
| 1248 | ly_in_free(in, 0); |
| 1249 | if (lydctx) { |
| 1250 | lydctx->free(lydctx); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1251 | } |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1252 | LY_CHECK_ERR_GOTO(ret, lyd_free_siblings(tree), error); |
| 1253 | |
| 1254 | /* use the parsed tree as the value */ |
| 1255 | free(value); |
| 1256 | value = (char *)tree; |
| 1257 | value_type = LYD_ANYDATA_DATATREE; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | /* create the node */ |
| 1261 | switch (value_type) { |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1262 | case LYD_ANYDATA_DATATREE: |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1263 | case LYD_ANYDATA_STRING: |
| 1264 | case LYD_ANYDATA_XML: |
| 1265 | case LYD_ANYDATA_JSON: |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 1266 | /* use the value directly */ |
| 1267 | ret = lyd_create_any(snode, value, value_type, 1, &node); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1268 | LY_CHECK_GOTO(ret, error); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1269 | break; |
| 1270 | default: |
| 1271 | LOGINT(ctx); |
| 1272 | ret = LY_EINT; |
| 1273 | goto error; |
| 1274 | } |
| 1275 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1276 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1277 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1278 | |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1279 | /* register parsed anydata node */ |
| 1280 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
| 1281 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1282 | LOG_LOCBACK(0, 1); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1283 | return LY_SUCCESS; |
| 1284 | |
| 1285 | error: |
| 1286 | free(value); |
| 1287 | lyd_free_meta_siblings(meta); |
| 1288 | lyd_free_tree(node); |
| 1289 | return ret; |
| 1290 | } |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1291 | |
| 1292 | /** |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1293 | * @brief Parse inner node. |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1294 | * |
| 1295 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1296 | * @param[in] parent Data parent of the sibling, must be set if @p first is not. |
| 1297 | * @param[in] snode Schema of the node to be parsed. |
aPiecek | fc7cf7e | 2021-09-09 11:20:27 +0200 | [diff] [blame] | 1298 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 1299 | * @param[out] parsed Set of all successfully parsed nodes. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1300 | * @return LY_ERR value. |
| 1301 | */ |
| 1302 | static LY_ERR |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1303 | lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1304 | struct lyd_node **first_p, struct ly_set *parsed) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1305 | { |
| 1306 | LY_ERR ret = LY_SUCCESS; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1307 | struct lyd_node *node = NULL; |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1308 | struct lyd_meta *meta = NULL; |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1309 | uint32_t flags; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1310 | |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1311 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1312 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1313 | LY_CHECK_GOTO(ret, error); |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1314 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1315 | /* create node */ |
| 1316 | ret = lyd_create_inner(snode, &node); |
| 1317 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1318 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1319 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1320 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1321 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1322 | /* process children */ |
| 1323 | ret = lyb_parse_siblings(lybctx, node, NULL, NULL); |
| 1324 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1325 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1326 | /* additional procedure for inner node */ |
| 1327 | ret = lyb_validate_node_inner(lybctx, snode, node); |
| 1328 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1329 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1330 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 1331 | /* rememeber the RPC/action/notification */ |
| 1332 | lybctx->op_node = node; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1335 | /* register parsed node */ |
| 1336 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
| 1337 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1338 | LOG_LOCBACK(0, 1); |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1339 | return LY_SUCCESS; |
| 1340 | |
| 1341 | error: |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1342 | if (node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1343 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1344 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1345 | lyd_free_meta_siblings(meta); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1346 | lyd_free_tree(node); |
| 1347 | return ret; |
| 1348 | } |
| 1349 | |
| 1350 | /** |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1351 | * @brief Parse leaf node. |
| 1352 | * |
| 1353 | * @param[in] lybctx LYB context. |
| 1354 | * @param[in] parent Data parent of the sibling. |
| 1355 | * @param[in] snode Schema of the node to be parsed. |
| 1356 | * @param[in,out] first_p First top-level sibling. |
| 1357 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1358 | * @return LY_ERR value. |
| 1359 | */ |
| 1360 | static LY_ERR |
| 1361 | lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1362 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1363 | { |
| 1364 | LY_ERR ret; |
| 1365 | struct lyd_node *node = NULL; |
| 1366 | struct lyd_meta *meta = NULL; |
| 1367 | uint32_t flags; |
| 1368 | |
| 1369 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1370 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1371 | LY_CHECK_GOTO(ret, error); |
| 1372 | |
| 1373 | /* read value of term node and create it */ |
| 1374 | ret = lyb_create_term(lybctx, snode, &node); |
| 1375 | LY_CHECK_GOTO(ret, error); |
| 1376 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1377 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1378 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1379 | |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1380 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
| 1381 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1382 | LOG_LOCBACK(0, 1); |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1383 | return LY_SUCCESS; |
| 1384 | |
| 1385 | error: |
| 1386 | lyd_free_meta_siblings(meta); |
| 1387 | lyd_free_tree(node); |
| 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1392 | * @brief Parse all leaflist nodes which belong to same schema. |
| 1393 | * |
| 1394 | * @param[in] lybctx LYB context. |
| 1395 | * @param[in] parent Data parent of the sibling. |
| 1396 | * @param[in] snode Schema of the nodes to be parsed. |
| 1397 | * @param[in,out] first_p First top-level sibling. |
| 1398 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1399 | * @return LY_ERR value. |
| 1400 | */ |
| 1401 | static LY_ERR |
| 1402 | lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1403 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1404 | { |
| 1405 | LY_ERR ret; |
| 1406 | |
| 1407 | /* register a new sibling */ |
| 1408 | ret = lyb_read_start_siblings(lybctx->lybctx); |
| 1409 | LY_CHECK_RET(ret); |
| 1410 | |
| 1411 | /* process all siblings */ |
| 1412 | while (LYB_LAST_SIBLING(lybctx->lybctx).written) { |
| 1413 | ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed); |
| 1414 | LY_CHECK_RET(ret); |
| 1415 | } |
| 1416 | |
| 1417 | /* end the sibling */ |
| 1418 | ret = lyb_read_stop_siblings(lybctx->lybctx); |
| 1419 | LY_CHECK_RET(ret); |
| 1420 | |
| 1421 | return ret; |
| 1422 | } |
| 1423 | |
| 1424 | /** |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1425 | * @brief Parse all list nodes which belong to same schema. |
| 1426 | * |
| 1427 | * @param[in] lybctx LYB context. |
| 1428 | * @param[in] parent Data parent of the sibling. |
| 1429 | * @param[in] snode Schema of the nodes to be parsed. |
| 1430 | * @param[in,out] first_p First top-level sibling. |
| 1431 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1432 | * @return LY_ERR value. |
| 1433 | */ |
| 1434 | static LY_ERR |
| 1435 | lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1436 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1437 | { |
| 1438 | LY_ERR ret; |
| 1439 | struct lyd_node *node = NULL; |
| 1440 | struct lyd_meta *meta = NULL; |
| 1441 | uint32_t flags; |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1442 | ly_bool log_node = 0; |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1443 | |
| 1444 | /* register a new sibling */ |
| 1445 | ret = lyb_read_start_siblings(lybctx->lybctx); |
| 1446 | LY_CHECK_RET(ret); |
| 1447 | |
| 1448 | while (LYB_LAST_SIBLING(lybctx->lybctx).written) { |
| 1449 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1450 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
Michal Vasko | edd4f25 | 2021-09-23 08:37:40 +0200 | [diff] [blame] | 1451 | LY_CHECK_GOTO(ret, error); |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1452 | |
| 1453 | /* create list node */ |
| 1454 | ret = lyd_create_inner(snode, &node); |
| 1455 | LY_CHECK_GOTO(ret, error); |
| 1456 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1457 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1458 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1459 | log_node = 1; |
| 1460 | |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1461 | /* process children */ |
| 1462 | ret = lyb_parse_siblings(lybctx, node, NULL, NULL); |
| 1463 | LY_CHECK_GOTO(ret, error); |
| 1464 | |
| 1465 | /* additional procedure for inner node */ |
| 1466 | ret = lyb_validate_node_inner(lybctx, snode, node); |
| 1467 | LY_CHECK_GOTO(ret, error); |
| 1468 | |
| 1469 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 1470 | /* rememeber the RPC/action/notification */ |
| 1471 | lybctx->op_node = node; |
| 1472 | } |
| 1473 | |
| 1474 | /* register parsed list node */ |
| 1475 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1476 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1477 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1478 | log_node = 0; |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* end the sibling */ |
| 1482 | ret = lyb_read_stop_siblings(lybctx->lybctx); |
| 1483 | LY_CHECK_RET(ret); |
| 1484 | |
| 1485 | return LY_SUCCESS; |
| 1486 | |
| 1487 | error: |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1488 | if (log_node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1489 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1490 | } |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1491 | lyd_free_meta_siblings(meta); |
| 1492 | lyd_free_tree(node); |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
| 1496 | /** |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1497 | * @brief Parse a node. |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1498 | * |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1499 | * @param[in] lybctx LYB context. |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1500 | * @param[in] parent Data parent of the sibling, must be set if @p first_p is not. |
| 1501 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 1502 | * @param[in,out] parsed Set of all successfully parsed nodes to add to. |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1503 | * @return LY_ERR value. |
| 1504 | */ |
| 1505 | static LY_ERR |
| 1506 | lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 1507 | struct ly_set *parsed) |
| 1508 | { |
| 1509 | LY_ERR ret; |
| 1510 | const struct lysc_node *snode; |
| 1511 | const struct lys_module *mod; |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1512 | enum lylyb_node_type lyb_type; |
| 1513 | char *mod_name = NULL, mod_rev[LY_REV_SIZE]; |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1514 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1515 | /* read node type */ |
| 1516 | lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx); |
| 1517 | |
| 1518 | switch (lyb_type) { |
| 1519 | case LYB_NODE_TOP: |
| 1520 | /* top-level, read module name */ |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1521 | LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod), cleanup); |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1522 | |
| 1523 | /* read hash, find the schema node starting from mod */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1524 | LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup); |
| 1525 | break; |
| 1526 | case LYB_NODE_CHILD: |
| 1527 | case LYB_NODE_OPAQ: |
| 1528 | /* read hash, find the schema node starting from parent schema, if any */ |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1529 | LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, lyd_parser_node_schema(parent), NULL, &snode), cleanup); |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1530 | break; |
| 1531 | case LYB_NODE_EXT: |
| 1532 | /* ext, read module name */ |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1533 | LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev, NULL), cleanup); |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1534 | |
| 1535 | /* read schema node name, find the nexted ext schema node */ |
| 1536 | LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup); |
| 1537 | break; |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1538 | } |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1539 | |
| 1540 | if (!snode) { |
| 1541 | ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed); |
| 1542 | } else if (snode->nodetype & LYS_LEAFLIST) { |
| 1543 | ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed); |
| 1544 | } else if (snode->nodetype == LYS_LIST) { |
| 1545 | ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed); |
| 1546 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 1547 | ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed); |
| 1548 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 1549 | ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed); |
| 1550 | } else { |
| 1551 | ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed); |
| 1552 | } |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1553 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1554 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1555 | cleanup: |
| 1556 | free(mod_name); |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | /** |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1561 | * @brief Parse siblings (@ref lyb_print_siblings()). |
| 1562 | * |
| 1563 | * @param[in] lybctx LYB context. |
| 1564 | * @param[in] parent Data parent of the sibling, must be set if @p first_p is not. |
| 1565 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 1566 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1567 | * @return LY_ERR value. |
| 1568 | */ |
| 1569 | static LY_ERR |
| 1570 | lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 1571 | struct ly_set *parsed) |
| 1572 | { |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1573 | ly_bool top_level; |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1574 | |
| 1575 | top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings); |
| 1576 | |
| 1577 | /* register a new siblings */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1578 | LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx)); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1579 | |
| 1580 | while (LYB_LAST_SIBLING(lybctx->lybctx).written) { |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1581 | LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed)); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1582 | |
| 1583 | if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1584 | break; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | /* end the siblings */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1589 | LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx)); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1590 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1591 | return LY_SUCCESS; |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1595 | * @brief Parse used YANG data models. |
| 1596 | * |
| 1597 | * @param[in] lybctx LYB context. |
aPiecek | fc7cf7e | 2021-09-09 11:20:27 +0200 | [diff] [blame] | 1598 | * @param[in] parse_options Flag with options for parsing. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1599 | * @return LY_ERR value. |
| 1600 | */ |
| 1601 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1602 | lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1603 | { |
| 1604 | LY_ERR ret; |
| 1605 | uint32_t count; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1606 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1607 | |
| 1608 | /* read model count */ |
| 1609 | lyb_read_number(&count, sizeof count, 2, lybctx); |
| 1610 | |
| 1611 | if (count) { |
| 1612 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM); |
| 1613 | |
| 1614 | /* read modules */ |
| 1615 | for (u = 0; u < count; ++u) { |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1616 | ret = lyb_parse_model(lybctx, parse_options, 1, &lybctx->models[u]); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1617 | LY_CHECK_RET(ret); |
| 1618 | LY_ARRAY_INCREMENT(lybctx->models); |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | return LY_SUCCESS; |
| 1623 | } |
| 1624 | |
| 1625 | /** |
| 1626 | * @brief Parse LYB magic number. |
| 1627 | * |
| 1628 | * @param[in] lybctx LYB context. |
| 1629 | * @return LY_ERR value. |
| 1630 | */ |
| 1631 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1632 | lyb_parse_magic_number(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1633 | { |
| 1634 | char magic_byte = 0; |
| 1635 | |
| 1636 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1637 | if (magic_byte != 'l') { |
| 1638 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 1639 | return LY_EINVAL; |
| 1640 | } |
| 1641 | |
| 1642 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1643 | if (magic_byte != 'y') { |
| 1644 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 1645 | return LY_EINVAL; |
| 1646 | } |
| 1647 | |
| 1648 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1649 | if (magic_byte != 'b') { |
| 1650 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
| 1651 | return LY_EINVAL; |
| 1652 | } |
| 1653 | |
| 1654 | return LY_SUCCESS; |
| 1655 | } |
| 1656 | |
| 1657 | /** |
| 1658 | * @brief Parse LYB header. |
| 1659 | * |
| 1660 | * @param[in] lybctx LYB context. |
| 1661 | * @return LY_ERR value. |
| 1662 | */ |
| 1663 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1664 | lyb_parse_header(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1665 | { |
| 1666 | uint8_t byte = 0; |
| 1667 | |
| 1668 | /* version, future flags */ |
| 1669 | lyb_read((uint8_t *)&byte, sizeof byte, lybctx); |
| 1670 | |
| 1671 | if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) { |
| 1672 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".", |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 1673 | byte & LYB_VERSION_MASK, LYB_VERSION_NUM); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1674 | return LY_EINVAL; |
| 1675 | } |
| 1676 | |
| 1677 | return LY_SUCCESS; |
| 1678 | } |
| 1679 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1680 | LY_ERR |
| 1681 | lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1682 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts, |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1683 | struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1684 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1685 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1686 | struct lyd_lyb_ctx *lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1687 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1688 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1689 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1690 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1691 | LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL); |
| 1692 | |
| 1693 | if (subtree_sibling) { |
| 1694 | *subtree_sibling = 0; |
| 1695 | } |
| 1696 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1697 | lybctx = calloc(1, sizeof *lybctx); |
| 1698 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM); |
| 1699 | lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1700 | LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1701 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1702 | lybctx->lybctx->in = in; |
| 1703 | lybctx->lybctx->ctx = ctx; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1704 | lybctx->parse_opts = parse_opts; |
| 1705 | lybctx->val_opts = val_opts; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1706 | lybctx->int_opts = int_opts; |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1707 | lybctx->free = lyd_lyb_ctx_free; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1708 | lybctx->ext = ext; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1709 | |
| 1710 | /* find the operation node if it exists already */ |
| 1711 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup); |
| 1712 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1713 | /* read magic number */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1714 | rc = lyb_parse_magic_number(lybctx->lybctx); |
| 1715 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1716 | |
| 1717 | /* read header */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1718 | rc = lyb_parse_header(lybctx->lybctx); |
| 1719 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1720 | |
| 1721 | /* read used models */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1722 | rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts); |
| 1723 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1724 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1725 | /* read sibling(s) */ |
| 1726 | rc = lyb_parse_siblings(lybctx, parent, first_p, parsed); |
| 1727 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1728 | |
| 1729 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) { |
| 1730 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1731 | rc = LY_EVALID; |
| 1732 | goto cleanup; |
| 1733 | } |
| 1734 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) { |
| 1735 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1736 | rc = LY_EVALID; |
| 1737 | goto cleanup; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1741 | ly_in_skip(lybctx->lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1742 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1743 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1744 | /* there should be no unres stored if validation should be skipped */ |
| 1745 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count && |
| 1746 | !lybctx->node_when.count)); |
| 1747 | |
| 1748 | if (rc) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1749 | lyd_lyb_ctx_free((struct lyd_ctx *)lybctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1750 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1751 | *lydctx_p = (struct lyd_ctx *)lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1752 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1753 | return rc; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1754 | } |
| 1755 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1756 | LIBYANG_API_DEF int |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1757 | lyd_lyb_data_length(const char *data) |
| 1758 | { |
| 1759 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1760 | struct lylyb_ctx *lybctx; |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1761 | uint32_t count, feat_count, len = 0, i, j; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1762 | uint8_t buf[LYB_SIZE_MAX]; |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 1763 | uint8_t zero[LYB_SIZE_BYTES] = {0}; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1764 | |
| 1765 | if (!data) { |
| 1766 | return -1; |
| 1767 | } |
| 1768 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1769 | lybctx = calloc(1, sizeof *lybctx); |
| 1770 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM); |
| 1771 | ret = ly_in_new_memory(data, &lybctx->in); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1772 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1773 | |
| 1774 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1775 | ret = lyb_parse_magic_number(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1776 | LY_CHECK_GOTO(ret, cleanup); |
| 1777 | |
| 1778 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1779 | ret = lyb_parse_header(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1780 | LY_CHECK_GOTO(ret, cleanup); |
| 1781 | |
| 1782 | /* read model count */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1783 | lyb_read_number(&count, sizeof count, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1784 | |
| 1785 | /* read all models */ |
| 1786 | for (i = 0; i < count; ++i) { |
| 1787 | /* module name length */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1788 | lyb_read_number(&len, sizeof len, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1789 | |
| 1790 | /* model name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1791 | lyb_read(buf, len, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1792 | |
| 1793 | /* revision */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1794 | lyb_read(buf, 2, lybctx); |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1795 | |
| 1796 | /* enabled feature count */ |
| 1797 | lyb_read_number(&feat_count, sizeof feat_count, 2, lybctx); |
| 1798 | |
| 1799 | /* enabled features */ |
| 1800 | for (j = 0; j < feat_count; ++j) { |
| 1801 | /* feature name length */ |
| 1802 | lyb_read_number(&len, sizeof len, 2, lybctx); |
| 1803 | |
| 1804 | /* feature name */ |
| 1805 | lyb_read(buf, len, lybctx); |
| 1806 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1807 | } |
| 1808 | |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 1809 | if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) { |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1810 | /* register a new sibling */ |
| 1811 | ret = lyb_read_start_siblings(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1812 | LY_CHECK_GOTO(ret, cleanup); |
| 1813 | |
| 1814 | /* skip it */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1815 | lyb_skip_siblings(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1816 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1817 | /* sibling finished */ |
| 1818 | ret = lyb_read_stop_siblings(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1819 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1820 | } else { |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 1821 | lyb_read(NULL, LYB_SIZE_BYTES, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1825 | ly_in_skip(lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1826 | |
| 1827 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1828 | count = lybctx->in->current - lybctx->in->start; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1829 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1830 | ly_in_free(lybctx->in, 0); |
| 1831 | lylyb_ctx_free(lybctx); |
| 1832 | |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1833 | return ret ? -1 : (int)count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1834 | } |