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