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 { |
| 934 | lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0); |
| 935 | } |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 936 | while (!parent && (*first_p)->prev->next) { |
| 937 | *first_p = (*first_p)->prev; |
| 938 | } |
| 939 | |
| 940 | /* rememeber a successfully parsed node */ |
| 941 | if (parsed) { |
| 942 | ly_set_add(parsed, node, 1, NULL); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /** |
| 947 | * @brief Finish parsing the opaq node. |
| 948 | * |
| 949 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 950 | * @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] | 951 | * @param[in] flags Node flags to set. |
| 952 | * @param[in,out] attr Attributes to be attached. Finally set to NULL. |
| 953 | * @param[in,out] node Parsed opaq node to finish. |
| 954 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 955 | * @param[out] parsed Set of all successfully parsed nodes. |
| 956 | * @return LY_ERR value. |
| 957 | */ |
| 958 | static void |
| 959 | lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr, |
| 960 | struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed) |
| 961 | { |
| 962 | struct lyd_attr *iter; |
| 963 | |
| 964 | /* set flags */ |
| 965 | (*node)->flags = flags; |
| 966 | |
| 967 | /* add attributes */ |
| 968 | assert(!(*node)->schema); |
| 969 | LY_LIST_FOR(*attr, iter) { |
| 970 | iter->parent = (struct lyd_node_opaq *)*node; |
| 971 | } |
| 972 | ((struct lyd_node_opaq *)*node)->attr = *attr; |
| 973 | *attr = NULL; |
| 974 | |
| 975 | lyb_insert_node(lybctx, parent, *node, first_p, parsed); |
| 976 | *node = NULL; |
| 977 | } |
| 978 | |
| 979 | /** |
| 980 | * @brief Finish parsing the node. |
| 981 | * |
| 982 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 983 | * @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] | 984 | * @param[in] flags Node flags to set. |
| 985 | * @param[in,out] meta Metadata to be attached. Finally set to NULL. |
| 986 | * @param[in,out] node Parsed node to finish. |
| 987 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 988 | * @param[out] parsed Set of all successfully parsed nodes. |
| 989 | * @return LY_ERR value. |
| 990 | */ |
| 991 | static void |
| 992 | lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta, |
| 993 | struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed) |
| 994 | { |
| 995 | struct lyd_meta *m; |
| 996 | |
| 997 | /* set flags */ |
| 998 | (*node)->flags = flags; |
| 999 | |
| 1000 | /* add metadata */ |
| 1001 | LY_LIST_FOR(*meta, m) { |
| 1002 | m->parent = *node; |
| 1003 | } |
| 1004 | (*node)->meta = *meta; |
| 1005 | *meta = NULL; |
| 1006 | |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1007 | /* insert into parent */ |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1008 | lyb_insert_node(lybctx, parent, *node, first_p, parsed); |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1009 | |
Michal Vasko | eba2311 | 2022-08-26 08:35:41 +0200 | [diff] [blame] | 1010 | if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) { |
| 1011 | /* store for ext instance node validation, if needed */ |
| 1012 | (void)lyd_validate_node_ext(*node, &lybctx->ext_node); |
| 1013 | } |
Michal Vasko | 135719f | 2022-08-25 12:18:17 +0200 | [diff] [blame] | 1014 | |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1015 | *node = NULL; |
| 1016 | } |
| 1017 | |
| 1018 | /** |
aPiecek | 6fdac2f | 2021-09-10 11:21:10 +0200 | [diff] [blame] | 1019 | * @brief Parse header for non-opaq node. |
| 1020 | * |
| 1021 | * @param[in] lybctx LYB context. |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1022 | * @param[in] sparent Schema parent node of the metadata. |
aPiecek | 6fdac2f | 2021-09-10 11:21:10 +0200 | [diff] [blame] | 1023 | * @param[out] flags Parsed node flags. |
| 1024 | * @param[out] meta Parsed metadata of the node. |
| 1025 | * @return LY_ERR value. |
| 1026 | */ |
| 1027 | static LY_ERR |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1028 | 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] | 1029 | { |
| 1030 | LY_ERR ret; |
| 1031 | |
| 1032 | /* create and read metadata */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1033 | ret = lyb_parse_metadata(lybctx, sparent, meta); |
aPiecek | 6fdac2f | 2021-09-10 11:21:10 +0200 | [diff] [blame] | 1034 | LY_CHECK_RET(ret); |
| 1035 | |
| 1036 | /* read flags */ |
| 1037 | lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx); |
| 1038 | |
| 1039 | return ret; |
| 1040 | } |
| 1041 | |
| 1042 | /** |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1043 | * @brief Create term node and fill it with value. |
| 1044 | * |
| 1045 | * @param[in] lybctx LYB context. |
| 1046 | * @param[in] snode Schema of the term node. |
| 1047 | * @param[out] node Created term node. |
| 1048 | * @return LY_ERR value. |
| 1049 | */ |
| 1050 | static LY_ERR |
| 1051 | lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node) |
| 1052 | { |
| 1053 | LY_ERR ret; |
| 1054 | ly_bool dynamic; |
| 1055 | uint8_t *term_value; |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1056 | uint64_t term_value_len; |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1057 | |
| 1058 | ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx); |
| 1059 | LY_CHECK_RET(ret); |
| 1060 | |
| 1061 | dynamic = 1; |
| 1062 | /* create node */ |
| 1063 | ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, |
| 1064 | term_value, term_value_len, &dynamic, LY_VALUE_LYB, |
| 1065 | NULL, LYD_HINT_DATA, node); |
| 1066 | if (dynamic) { |
| 1067 | free(term_value); |
| 1068 | } |
| 1069 | if (ret) { |
| 1070 | lyd_free_tree(*node); |
| 1071 | *node = NULL; |
| 1072 | } |
| 1073 | |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
| 1077 | /** |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 1078 | * @brief Validate inner node, autodelete default values nad create implicit nodes. |
| 1079 | * |
| 1080 | * @param[in,out] lybctx LYB context. |
| 1081 | * @param[in] snode Schema of the inner node. |
| 1082 | * @param[in] node Parsed inner node. |
| 1083 | * @return LY_ERR value. |
| 1084 | */ |
| 1085 | static LY_ERR |
| 1086 | lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node) |
| 1087 | { |
| 1088 | LY_ERR ret = LY_SUCCESS; |
| 1089 | uint32_t impl_opts; |
| 1090 | |
| 1091 | if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) { |
| 1092 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
Michal Vasko | d027f38 | 2023-02-10 09:13:25 +0100 | [diff] [blame] | 1093 | ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, 0, NULL); |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 1094 | LY_CHECK_RET(ret); |
| 1095 | |
| 1096 | /* add any missing default children */ |
| 1097 | 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] | 1098 | 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] | 1099 | &lybctx->ext_node, impl_opts, NULL); |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 1100 | LY_CHECK_RET(ret); |
| 1101 | } |
| 1102 | |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1107 | * @brief Parse opaq node. |
| 1108 | * |
| 1109 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1110 | * @param[in] parent Data parent of the sibling. |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1111 | * @param[in,out] first_p First top-level sibling. |
| 1112 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1113 | * @return LY_ERR value. |
| 1114 | */ |
| 1115 | static LY_ERR |
| 1116 | lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
| 1117 | { |
| 1118 | LY_ERR ret; |
| 1119 | struct lyd_node *node = NULL; |
| 1120 | struct lyd_attr *attr = NULL; |
| 1121 | char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL; |
| 1122 | ly_bool dynamic = 0; |
| 1123 | LY_VALUE_FORMAT format = 0; |
| 1124 | void *val_prefix_data = NULL; |
| 1125 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
| 1126 | uint32_t flags; |
| 1127 | |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1128 | /* parse opaq node attributes */ |
| 1129 | ret = lyb_parse_attributes(lybctx->lybctx, &attr); |
| 1130 | LY_CHECK_GOTO(ret, cleanup); |
| 1131 | |
| 1132 | /* read flags */ |
| 1133 | lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx); |
| 1134 | |
| 1135 | /* parse prefix */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1136 | ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1137 | LY_CHECK_GOTO(ret, cleanup); |
| 1138 | |
| 1139 | /* parse module key */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1140 | ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1141 | LY_CHECK_GOTO(ret, cleanup); |
| 1142 | |
| 1143 | /* parse name */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1144 | ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1145 | LY_CHECK_GOTO(ret, cleanup); |
| 1146 | |
| 1147 | /* parse value */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1148 | ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1149 | LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup); |
| 1150 | dynamic = 1; |
| 1151 | |
| 1152 | /* parse format */ |
| 1153 | lyb_read_number(&format, sizeof format, 1, lybctx->lybctx); |
| 1154 | |
| 1155 | /* parse value prefixes */ |
| 1156 | ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data); |
| 1157 | LY_CHECK_GOTO(ret, cleanup); |
| 1158 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1159 | if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) { |
Michal Vasko | efa96e6 | 2021-11-02 12:19:14 +0100 | [diff] [blame] | 1160 | /* skip children */ |
| 1161 | ret = lyb_read_start_siblings(lybctx->lybctx); |
| 1162 | LY_CHECK_GOTO(ret, cleanup); |
| 1163 | lyb_skip_siblings(lybctx->lybctx); |
| 1164 | ret = lyb_read_stop_siblings(lybctx->lybctx); |
| 1165 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1166 | goto cleanup; |
| 1167 | } |
| 1168 | |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1169 | /* create node */ |
| 1170 | 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] | 1171 | value, strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA, &node); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1172 | LY_CHECK_GOTO(ret, cleanup); |
| 1173 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1174 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1175 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1176 | |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1177 | /* process children */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1178 | ret = lyb_parse_siblings(lybctx, node, NULL, NULL); |
| 1179 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1180 | |
| 1181 | /* register parsed opaq node */ |
| 1182 | lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed); |
| 1183 | assert(!attr && !node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1184 | LOG_LOCBACK(0, 1); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1185 | |
| 1186 | cleanup: |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1187 | if (node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1188 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1189 | } |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1190 | free(prefix); |
| 1191 | free(module_key); |
| 1192 | free(name); |
| 1193 | if (dynamic) { |
| 1194 | free(value); |
| 1195 | } |
| 1196 | lyd_free_attr_siblings(ctx, attr); |
| 1197 | lyd_free_tree(node); |
| 1198 | |
| 1199 | return ret; |
| 1200 | } |
| 1201 | |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1202 | /** |
| 1203 | * @brief Parse anydata or anyxml node. |
| 1204 | * |
| 1205 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1206 | * @param[in] parent Data parent of the sibling. |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1207 | * @param[in] snode Schema of the node to be parsed. |
| 1208 | * @param[in,out] first_p First top-level sibling. |
| 1209 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1210 | * @return LY_ERR value. |
| 1211 | */ |
| 1212 | static LY_ERR |
| 1213 | lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1214 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1215 | { |
| 1216 | LY_ERR ret; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1217 | struct lyd_node *node = NULL, *tree = NULL; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1218 | struct lyd_meta *meta = NULL; |
| 1219 | LYD_ANYDATA_VALUETYPE value_type; |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1220 | struct ly_in *in; |
| 1221 | struct lyd_ctx *lydctx = NULL; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1222 | char *value = NULL; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1223 | uint32_t flags; |
| 1224 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
| 1225 | |
| 1226 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1227 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
Michal Vasko | edd4f25 | 2021-09-23 08:37:40 +0200 | [diff] [blame] | 1228 | LY_CHECK_GOTO(ret, error); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1229 | |
| 1230 | /* parse value type */ |
| 1231 | 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] | 1232 | 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] | 1233 | LOGINT(ctx); |
| 1234 | ret = LY_EINT; |
| 1235 | goto error; |
| 1236 | } |
| 1237 | |
| 1238 | /* read anydata content */ |
aPiecek | e99345d | 2021-09-30 12:49:59 +0200 | [diff] [blame] | 1239 | ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1240 | LY_CHECK_GOTO(ret, error); |
| 1241 | |
| 1242 | if (value_type == LYD_ANYDATA_LYB) { |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1243 | /* parse LYB into a data tree */ |
| 1244 | LY_CHECK_RET(ly_in_new_memory(value, &in)); |
| 1245 | ret = lyd_parse_lyb(ctx, NULL, NULL, &tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0, |
| 1246 | LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, NULL, &lydctx); |
| 1247 | ly_in_free(in, 0); |
| 1248 | if (lydctx) { |
| 1249 | lydctx->free(lydctx); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1250 | } |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1251 | LY_CHECK_ERR_GOTO(ret, lyd_free_siblings(tree), error); |
| 1252 | |
| 1253 | /* use the parsed tree as the value */ |
| 1254 | free(value); |
| 1255 | value = (char *)tree; |
| 1256 | value_type = LYD_ANYDATA_DATATREE; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | /* create the node */ |
| 1260 | switch (value_type) { |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1261 | case LYD_ANYDATA_DATATREE: |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1262 | case LYD_ANYDATA_STRING: |
| 1263 | case LYD_ANYDATA_XML: |
| 1264 | case LYD_ANYDATA_JSON: |
Michal Vasko | 742a5b1 | 2022-02-24 16:07:27 +0100 | [diff] [blame] | 1265 | /* use the value directly */ |
| 1266 | ret = lyd_create_any(snode, value, value_type, 1, &node); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1267 | LY_CHECK_GOTO(ret, error); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1268 | break; |
| 1269 | default: |
| 1270 | LOGINT(ctx); |
| 1271 | ret = LY_EINT; |
| 1272 | goto error; |
| 1273 | } |
| 1274 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1275 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1276 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1277 | |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1278 | /* register parsed anydata node */ |
| 1279 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
| 1280 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1281 | LOG_LOCBACK(0, 1); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1282 | return LY_SUCCESS; |
| 1283 | |
| 1284 | error: |
| 1285 | free(value); |
| 1286 | lyd_free_meta_siblings(meta); |
| 1287 | lyd_free_tree(node); |
| 1288 | return ret; |
| 1289 | } |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1290 | |
| 1291 | /** |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1292 | * @brief Parse inner node. |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1293 | * |
| 1294 | * @param[in] lybctx LYB context. |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1295 | * @param[in] parent Data parent of the sibling, must be set if @p first is not. |
| 1296 | * @param[in] snode Schema of the node to be parsed. |
aPiecek | fc7cf7e | 2021-09-09 11:20:27 +0200 | [diff] [blame] | 1297 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 1298 | * @param[out] parsed Set of all successfully parsed nodes. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1299 | * @return LY_ERR value. |
| 1300 | */ |
| 1301 | static LY_ERR |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1302 | 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] | 1303 | struct lyd_node **first_p, struct ly_set *parsed) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1304 | { |
| 1305 | LY_ERR ret = LY_SUCCESS; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1306 | struct lyd_node *node = NULL; |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1307 | struct lyd_meta *meta = NULL; |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1308 | uint32_t flags; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1309 | |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1310 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1311 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1312 | LY_CHECK_GOTO(ret, error); |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1313 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1314 | /* create node */ |
| 1315 | ret = lyd_create_inner(snode, &node); |
| 1316 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1317 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1318 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1319 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1320 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1321 | /* process children */ |
| 1322 | ret = lyb_parse_siblings(lybctx, node, NULL, NULL); |
| 1323 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1324 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1325 | /* additional procedure for inner node */ |
| 1326 | ret = lyb_validate_node_inner(lybctx, snode, node); |
| 1327 | LY_CHECK_GOTO(ret, error); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1328 | |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1329 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 1330 | /* rememeber the RPC/action/notification */ |
| 1331 | lybctx->op_node = node; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1332 | } |
| 1333 | |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1334 | /* register parsed node */ |
| 1335 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
| 1336 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1337 | LOG_LOCBACK(0, 1); |
aPiecek | 307f077 | 2021-09-10 09:09:47 +0200 | [diff] [blame] | 1338 | return LY_SUCCESS; |
| 1339 | |
| 1340 | error: |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1341 | if (node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1342 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1343 | } |
Michal Vasko | 3a41dff | 2020-07-15 14:30:28 +0200 | [diff] [blame] | 1344 | lyd_free_meta_siblings(meta); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1345 | lyd_free_tree(node); |
| 1346 | return ret; |
| 1347 | } |
| 1348 | |
| 1349 | /** |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1350 | * @brief Parse leaf node. |
| 1351 | * |
| 1352 | * @param[in] lybctx LYB context. |
| 1353 | * @param[in] parent Data parent of the sibling. |
| 1354 | * @param[in] snode Schema of the node to be parsed. |
| 1355 | * @param[in,out] first_p First top-level sibling. |
| 1356 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1357 | * @return LY_ERR value. |
| 1358 | */ |
| 1359 | static LY_ERR |
| 1360 | lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1361 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1362 | { |
| 1363 | LY_ERR ret; |
| 1364 | struct lyd_node *node = NULL; |
| 1365 | struct lyd_meta *meta = NULL; |
| 1366 | uint32_t flags; |
| 1367 | |
| 1368 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1369 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1370 | LY_CHECK_GOTO(ret, error); |
| 1371 | |
| 1372 | /* read value of term node and create it */ |
| 1373 | ret = lyb_create_term(lybctx, snode, &node); |
| 1374 | LY_CHECK_GOTO(ret, error); |
| 1375 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1376 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1377 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1378 | |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1379 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
| 1380 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1381 | LOG_LOCBACK(0, 1); |
aPiecek | 8a555d7 | 2021-09-10 10:15:26 +0200 | [diff] [blame] | 1382 | return LY_SUCCESS; |
| 1383 | |
| 1384 | error: |
| 1385 | lyd_free_meta_siblings(meta); |
| 1386 | lyd_free_tree(node); |
| 1387 | return ret; |
| 1388 | } |
| 1389 | |
| 1390 | /** |
aPiecek | 5777f12 | 2021-09-10 10:26:23 +0200 | [diff] [blame] | 1391 | * @brief Parse all leaflist nodes which belong to same schema. |
| 1392 | * |
| 1393 | * @param[in] lybctx LYB context. |
| 1394 | * @param[in] parent Data parent of the sibling. |
| 1395 | * @param[in] snode Schema of the nodes to be parsed. |
| 1396 | * @param[in,out] first_p First top-level sibling. |
| 1397 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1398 | * @return LY_ERR value. |
| 1399 | */ |
| 1400 | static LY_ERR |
| 1401 | lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1402 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1403 | { |
| 1404 | LY_ERR ret; |
| 1405 | |
| 1406 | /* register a new sibling */ |
| 1407 | ret = lyb_read_start_siblings(lybctx->lybctx); |
| 1408 | LY_CHECK_RET(ret); |
| 1409 | |
| 1410 | /* process all siblings */ |
| 1411 | while (LYB_LAST_SIBLING(lybctx->lybctx).written) { |
| 1412 | ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed); |
| 1413 | LY_CHECK_RET(ret); |
| 1414 | } |
| 1415 | |
| 1416 | /* end the sibling */ |
| 1417 | ret = lyb_read_stop_siblings(lybctx->lybctx); |
| 1418 | LY_CHECK_RET(ret); |
| 1419 | |
| 1420 | return ret; |
| 1421 | } |
| 1422 | |
| 1423 | /** |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1424 | * @brief Parse all list nodes which belong to same schema. |
| 1425 | * |
| 1426 | * @param[in] lybctx LYB context. |
| 1427 | * @param[in] parent Data parent of the sibling. |
| 1428 | * @param[in] snode Schema of the nodes to be parsed. |
| 1429 | * @param[in,out] first_p First top-level sibling. |
| 1430 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1431 | * @return LY_ERR value. |
| 1432 | */ |
| 1433 | static LY_ERR |
| 1434 | lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1435 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1436 | { |
| 1437 | LY_ERR ret; |
| 1438 | struct lyd_node *node = NULL; |
| 1439 | struct lyd_meta *meta = NULL; |
| 1440 | uint32_t flags; |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1441 | ly_bool log_node = 0; |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1442 | |
| 1443 | /* register a new sibling */ |
| 1444 | ret = lyb_read_start_siblings(lybctx->lybctx); |
| 1445 | LY_CHECK_RET(ret); |
| 1446 | |
| 1447 | while (LYB_LAST_SIBLING(lybctx->lybctx).written) { |
| 1448 | /* read necessary basic data */ |
Michal Vasko | ddd7659 | 2022-01-17 13:34:48 +0100 | [diff] [blame] | 1449 | ret = lyb_parse_node_header(lybctx, snode, &flags, &meta); |
Michal Vasko | edd4f25 | 2021-09-23 08:37:40 +0200 | [diff] [blame] | 1450 | LY_CHECK_GOTO(ret, error); |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1451 | |
| 1452 | /* create list node */ |
| 1453 | ret = lyd_create_inner(snode, &node); |
| 1454 | LY_CHECK_GOTO(ret, error); |
| 1455 | |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1456 | assert(node); |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1457 | LOG_LOCSET(NULL, node); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1458 | log_node = 1; |
| 1459 | |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1460 | /* process children */ |
| 1461 | ret = lyb_parse_siblings(lybctx, node, NULL, NULL); |
| 1462 | LY_CHECK_GOTO(ret, error); |
| 1463 | |
| 1464 | /* additional procedure for inner node */ |
| 1465 | ret = lyb_validate_node_inner(lybctx, snode, node); |
| 1466 | LY_CHECK_GOTO(ret, error); |
| 1467 | |
| 1468 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
| 1469 | /* rememeber the RPC/action/notification */ |
| 1470 | lybctx->op_node = node; |
| 1471 | } |
| 1472 | |
| 1473 | /* register parsed list node */ |
| 1474 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1475 | |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1476 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1477 | log_node = 0; |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | /* end the sibling */ |
| 1481 | ret = lyb_read_stop_siblings(lybctx->lybctx); |
| 1482 | LY_CHECK_RET(ret); |
| 1483 | |
| 1484 | return LY_SUCCESS; |
| 1485 | |
| 1486 | error: |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1487 | if (log_node) { |
Michal Vasko | 7a26677 | 2024-01-23 11:02:38 +0100 | [diff] [blame] | 1488 | LOG_LOCBACK(0, 1); |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1489 | } |
aPiecek | 77d3e96 | 2021-09-10 10:33:59 +0200 | [diff] [blame] | 1490 | lyd_free_meta_siblings(meta); |
| 1491 | lyd_free_tree(node); |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
| 1495 | /** |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1496 | * @brief Parse a node. |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1497 | * |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1498 | * @param[in] lybctx LYB context. |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1499 | * @param[in] parent Data parent of the sibling, must be set if @p first_p is not. |
| 1500 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 1501 | * @param[in,out] parsed Set of all successfully parsed nodes to add to. |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1502 | * @return LY_ERR value. |
| 1503 | */ |
| 1504 | static LY_ERR |
| 1505 | lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 1506 | struct ly_set *parsed) |
| 1507 | { |
| 1508 | LY_ERR ret; |
| 1509 | const struct lysc_node *snode; |
| 1510 | const struct lys_module *mod; |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1511 | enum lylyb_node_type lyb_type; |
| 1512 | char *mod_name = NULL, mod_rev[LY_REV_SIZE]; |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1513 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1514 | /* read node type */ |
| 1515 | lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx); |
| 1516 | |
| 1517 | switch (lyb_type) { |
| 1518 | case LYB_NODE_TOP: |
| 1519 | /* top-level, read module name */ |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1520 | 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] | 1521 | |
| 1522 | /* read hash, find the schema node starting from mod */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1523 | LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup); |
| 1524 | break; |
| 1525 | case LYB_NODE_CHILD: |
| 1526 | case LYB_NODE_OPAQ: |
| 1527 | /* read hash, find the schema node starting from parent schema, if any */ |
Michal Vasko | a878a89 | 2023-08-18 12:22:07 +0200 | [diff] [blame] | 1528 | 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] | 1529 | break; |
| 1530 | case LYB_NODE_EXT: |
| 1531 | /* ext, read module name */ |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1532 | 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] | 1533 | |
| 1534 | /* read schema node name, find the nexted ext schema node */ |
| 1535 | LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup); |
| 1536 | break; |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1537 | } |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1538 | |
| 1539 | if (!snode) { |
| 1540 | ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed); |
| 1541 | } else if (snode->nodetype & LYS_LEAFLIST) { |
| 1542 | ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed); |
| 1543 | } else if (snode->nodetype == LYS_LIST) { |
| 1544 | ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed); |
| 1545 | } else if (snode->nodetype & LYD_NODE_ANY) { |
| 1546 | ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed); |
| 1547 | } else if (snode->nodetype & LYD_NODE_INNER) { |
| 1548 | ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed); |
| 1549 | } else { |
| 1550 | ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed); |
| 1551 | } |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1552 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1553 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1554 | cleanup: |
| 1555 | free(mod_name); |
aPiecek | 17737b5 | 2021-09-21 12:29:52 +0200 | [diff] [blame] | 1556 | return ret; |
| 1557 | } |
| 1558 | |
| 1559 | /** |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1560 | * @brief Parse siblings (@ref lyb_print_siblings()). |
| 1561 | * |
| 1562 | * @param[in] lybctx LYB context. |
| 1563 | * @param[in] parent Data parent of the sibling, must be set if @p first_p is not. |
| 1564 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 1565 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1566 | * @return LY_ERR value. |
| 1567 | */ |
| 1568 | static LY_ERR |
| 1569 | lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, |
| 1570 | struct ly_set *parsed) |
| 1571 | { |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1572 | ly_bool top_level; |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1573 | |
| 1574 | top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings); |
| 1575 | |
| 1576 | /* register a new siblings */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1577 | LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx)); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1578 | |
| 1579 | while (LYB_LAST_SIBLING(lybctx->lybctx).written) { |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1580 | LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed)); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1581 | |
| 1582 | if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1583 | break; |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | /* end the siblings */ |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1588 | LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx)); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1589 | |
Michal Vasko | 9883a3e | 2022-03-31 12:16:00 +0200 | [diff] [blame] | 1590 | return LY_SUCCESS; |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1594 | * @brief Parse used YANG data models. |
| 1595 | * |
| 1596 | * @param[in] lybctx LYB context. |
aPiecek | fc7cf7e | 2021-09-09 11:20:27 +0200 | [diff] [blame] | 1597 | * @param[in] parse_options Flag with options for parsing. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1598 | * @return LY_ERR value. |
| 1599 | */ |
| 1600 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1601 | lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1602 | { |
| 1603 | LY_ERR ret; |
| 1604 | uint32_t count; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1605 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1606 | |
| 1607 | /* read model count */ |
| 1608 | lyb_read_number(&count, sizeof count, 2, lybctx); |
| 1609 | |
| 1610 | if (count) { |
| 1611 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM); |
| 1612 | |
| 1613 | /* read modules */ |
| 1614 | for (u = 0; u < count; ++u) { |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1615 | ret = lyb_parse_model(lybctx, parse_options, 1, &lybctx->models[u]); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1616 | LY_CHECK_RET(ret); |
| 1617 | LY_ARRAY_INCREMENT(lybctx->models); |
| 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | return LY_SUCCESS; |
| 1622 | } |
| 1623 | |
| 1624 | /** |
| 1625 | * @brief Parse LYB magic number. |
| 1626 | * |
| 1627 | * @param[in] lybctx LYB context. |
| 1628 | * @return LY_ERR value. |
| 1629 | */ |
| 1630 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1631 | lyb_parse_magic_number(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1632 | { |
| 1633 | char magic_byte = 0; |
| 1634 | |
| 1635 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1636 | if (magic_byte != 'l') { |
| 1637 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 1638 | return LY_EINVAL; |
| 1639 | } |
| 1640 | |
| 1641 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1642 | if (magic_byte != 'y') { |
| 1643 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 1644 | return LY_EINVAL; |
| 1645 | } |
| 1646 | |
| 1647 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1648 | if (magic_byte != 'b') { |
| 1649 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
| 1650 | return LY_EINVAL; |
| 1651 | } |
| 1652 | |
| 1653 | return LY_SUCCESS; |
| 1654 | } |
| 1655 | |
| 1656 | /** |
| 1657 | * @brief Parse LYB header. |
| 1658 | * |
| 1659 | * @param[in] lybctx LYB context. |
| 1660 | * @return LY_ERR value. |
| 1661 | */ |
| 1662 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1663 | lyb_parse_header(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1664 | { |
| 1665 | uint8_t byte = 0; |
| 1666 | |
| 1667 | /* version, future flags */ |
| 1668 | lyb_read((uint8_t *)&byte, sizeof byte, lybctx); |
| 1669 | |
| 1670 | if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) { |
| 1671 | 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] | 1672 | byte & LYB_VERSION_MASK, LYB_VERSION_NUM); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1673 | return LY_EINVAL; |
| 1674 | } |
| 1675 | |
| 1676 | return LY_SUCCESS; |
| 1677 | } |
| 1678 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1679 | LY_ERR |
| 1680 | 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] | 1681 | 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] | 1682 | 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] | 1683 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1684 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1685 | struct lyd_lyb_ctx *lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1686 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1687 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1688 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1689 | |
Michal Vasko | e3ed7dc | 2022-11-30 11:39:44 +0100 | [diff] [blame] | 1690 | LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL); |
| 1691 | |
| 1692 | if (subtree_sibling) { |
| 1693 | *subtree_sibling = 0; |
| 1694 | } |
| 1695 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1696 | lybctx = calloc(1, sizeof *lybctx); |
| 1697 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM); |
| 1698 | lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1699 | LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1700 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1701 | lybctx->lybctx->in = in; |
| 1702 | lybctx->lybctx->ctx = ctx; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1703 | lybctx->parse_opts = parse_opts; |
| 1704 | lybctx->val_opts = val_opts; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1705 | lybctx->int_opts = int_opts; |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1706 | lybctx->free = lyd_lyb_ctx_free; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1707 | lybctx->ext = ext; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1708 | |
| 1709 | /* find the operation node if it exists already */ |
| 1710 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup); |
| 1711 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1712 | /* read magic number */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1713 | rc = lyb_parse_magic_number(lybctx->lybctx); |
| 1714 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1715 | |
| 1716 | /* read header */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1717 | rc = lyb_parse_header(lybctx->lybctx); |
| 1718 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1719 | |
| 1720 | /* read used models */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1721 | rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts); |
| 1722 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1723 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1724 | /* read sibling(s) */ |
| 1725 | rc = lyb_parse_siblings(lybctx, parent, first_p, parsed); |
| 1726 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1727 | |
| 1728 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) { |
| 1729 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1730 | rc = LY_EVALID; |
| 1731 | goto cleanup; |
| 1732 | } |
| 1733 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) { |
| 1734 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1735 | rc = LY_EVALID; |
| 1736 | goto cleanup; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1740 | ly_in_skip(lybctx->lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1741 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1742 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1743 | /* there should be no unres stored if validation should be skipped */ |
| 1744 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count && |
| 1745 | !lybctx->node_when.count)); |
| 1746 | |
| 1747 | if (rc) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1748 | lyd_lyb_ctx_free((struct lyd_ctx *)lybctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1749 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1750 | *lydctx_p = (struct lyd_ctx *)lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1751 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1752 | return rc; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1753 | } |
| 1754 | |
Jan Kundrát | c53a7ec | 2021-12-09 16:01:19 +0100 | [diff] [blame] | 1755 | LIBYANG_API_DEF int |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1756 | lyd_lyb_data_length(const char *data) |
| 1757 | { |
| 1758 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1759 | struct lylyb_ctx *lybctx; |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1760 | uint32_t count, feat_count, len = 0, i, j; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1761 | uint8_t buf[LYB_SIZE_MAX]; |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 1762 | uint8_t zero[LYB_SIZE_BYTES] = {0}; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1763 | |
| 1764 | if (!data) { |
| 1765 | return -1; |
| 1766 | } |
| 1767 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1768 | lybctx = calloc(1, sizeof *lybctx); |
| 1769 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM); |
| 1770 | ret = ly_in_new_memory(data, &lybctx->in); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1771 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1772 | |
| 1773 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1774 | ret = lyb_parse_magic_number(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1775 | LY_CHECK_GOTO(ret, cleanup); |
| 1776 | |
| 1777 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1778 | ret = lyb_parse_header(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1779 | LY_CHECK_GOTO(ret, cleanup); |
| 1780 | |
| 1781 | /* read model count */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1782 | lyb_read_number(&count, sizeof count, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1783 | |
| 1784 | /* read all models */ |
| 1785 | for (i = 0; i < count; ++i) { |
| 1786 | /* module name length */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1787 | lyb_read_number(&len, sizeof len, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1788 | |
| 1789 | /* model name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1790 | lyb_read(buf, len, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1791 | |
| 1792 | /* revision */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1793 | lyb_read(buf, 2, lybctx); |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* enabled feature count */ |
| 1796 | lyb_read_number(&feat_count, sizeof feat_count, 2, lybctx); |
| 1797 | |
| 1798 | /* enabled features */ |
| 1799 | for (j = 0; j < feat_count; ++j) { |
| 1800 | /* feature name length */ |
| 1801 | lyb_read_number(&len, sizeof len, 2, lybctx); |
| 1802 | |
| 1803 | /* feature name */ |
| 1804 | lyb_read(buf, len, lybctx); |
| 1805 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1806 | } |
| 1807 | |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 1808 | if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) { |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1809 | /* register a new sibling */ |
| 1810 | ret = lyb_read_start_siblings(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1811 | LY_CHECK_GOTO(ret, cleanup); |
| 1812 | |
| 1813 | /* skip it */ |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1814 | lyb_skip_siblings(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1815 | |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1816 | /* sibling finished */ |
| 1817 | ret = lyb_read_stop_siblings(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1818 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 570d7ed | 2021-09-10 07:15:35 +0200 | [diff] [blame] | 1819 | } else { |
aPiecek | 6828a31 | 2021-09-17 15:53:18 +0200 | [diff] [blame] | 1820 | lyb_read(NULL, LYB_SIZE_BYTES, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1824 | ly_in_skip(lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1825 | |
| 1826 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1827 | count = lybctx->in->current - lybctx->in->start; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1828 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1829 | ly_in_free(lybctx->in, 0); |
| 1830 | lylyb_ctx_free(lybctx); |
| 1831 | |
Michal Vasko | 78041d1 | 2022-11-29 14:11:07 +0100 | [diff] [blame] | 1832 | return ret ? -1 : (int)count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1833 | } |