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