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