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