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 | /** |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame^] | 638 | * @brief Fill @p hash with hash values. |
| 639 | * |
| 640 | * @param[in] lybctx LYB context. |
| 641 | * @param[in,out] hash Pointer to the array in which the hash values are to be written. |
| 642 | * @param[out] hash_count Number of hashes in @p hash. |
| 643 | * @return LY_ERR value. |
| 644 | */ |
| 645 | static LY_ERR |
| 646 | lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count) |
| 647 | { |
| 648 | uint8_t i = 0, j; |
| 649 | |
| 650 | /* read the first hash */ |
| 651 | lyb_read(&hash[0], sizeof *hash, lybctx); |
| 652 | |
| 653 | if (!hash[0]) { |
| 654 | *hash_count = i + 1; |
| 655 | return LY_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | /* based on the first hash read all the other ones, if any */ |
| 659 | for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) { |
| 660 | if (i > LYB_HASH_BITS) { |
| 661 | LOGINT_RET(lybctx->ctx); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /* move the first hash on its accurate position */ |
| 666 | hash[i] = hash[0]; |
| 667 | |
| 668 | /* read the rest of hashes */ |
| 669 | for (j = i; j; --j) { |
| 670 | lyb_read(&hash[j - 1], sizeof *hash, lybctx); |
| 671 | |
| 672 | /* correct collision ID */ |
| 673 | assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1))); |
| 674 | /* preceded with zeros */ |
| 675 | assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1))))); |
| 676 | } |
| 677 | |
| 678 | *hash_count = i + 1; |
| 679 | |
| 680 | return LY_SUCCESS; |
| 681 | } |
| 682 | |
| 683 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 684 | * @brief Check whether a schema node matches a hash(es). |
| 685 | * |
| 686 | * @param[in] sibling Schema node to check. |
| 687 | * @param[in] hash Hash array to check. |
| 688 | * @param[in] hash_count Number of hashes in @p hash. |
| 689 | * @return non-zero if matches, |
| 690 | * @return 0 if not. |
| 691 | */ |
| 692 | static int |
| 693 | lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count) |
| 694 | { |
| 695 | LYB_HASH sibling_hash; |
| 696 | uint8_t i; |
| 697 | |
| 698 | /* compare all the hashes starting from collision ID 0 */ |
| 699 | for (i = 0; i < hash_count; ++i) { |
Michal Vasko | 11f76c8 | 2021-04-15 14:36:14 +0200 | [diff] [blame] | 700 | sibling_hash = lyb_get_hash(sibling, i); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 701 | if (sibling_hash != hash[i]) { |
| 702 | return 0; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | return 1; |
| 707 | } |
| 708 | |
| 709 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 710 | * @brief Parse schema node hash. |
| 711 | * |
| 712 | * @param[in] lybctx LYB context. |
| 713 | * @param[in] sparent Schema parent, must be set if @p mod is not. |
| 714 | * @param[in] mod Module of the top-level node, must be set if @p sparent is not. |
| 715 | * @param[out] snode Parsed found schema node, may be NULL if opaque. |
| 716 | * @return LY_ERR value. |
| 717 | */ |
| 718 | static LY_ERR |
| 719 | 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] | 720 | const struct lysc_node **snode) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 721 | { |
| 722 | LY_ERR ret; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 723 | const struct lysc_node *sibling; |
| 724 | LYB_HASH hash[LYB_HASH_BITS - 1]; |
Radek Krejci | 1deb5be | 2020-08-26 16:43:36 +0200 | [diff] [blame] | 725 | uint32_t getnext_opts; |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame^] | 726 | uint8_t hash_count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 727 | |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame^] | 728 | ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count); |
| 729 | LY_CHECK_RET(ret); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 730 | |
| 731 | if (!hash[0]) { |
| 732 | /* opaque node */ |
| 733 | return LY_SUCCESS; |
| 734 | } |
| 735 | |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame^] | 736 | *snode = NULL; |
| 737 | getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 738 | |
| 739 | /* find our node with matching hashes */ |
| 740 | sibling = NULL; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 741 | while (1) { |
| 742 | if (!sparent && lybctx->ext) { |
| 743 | sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts); |
| 744 | } else { |
| 745 | sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts); |
| 746 | } |
| 747 | if (!sibling) { |
| 748 | break; |
| 749 | } |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 750 | /* skip schema nodes from models not present during printing */ |
Michal Vasko | 6973015 | 2020-10-09 16:30:07 +0200 | [diff] [blame] | 751 | if (lyb_has_schema_model(sibling, lybctx->lybctx->models) && |
aPiecek | 619350d | 2021-09-09 16:06:59 +0200 | [diff] [blame^] | 752 | lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 753 | /* match found */ |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 758 | if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) { |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 759 | if (lybctx->ext) { |
| 760 | LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.", |
| 761 | lybctx->ext->def->name); |
| 762 | } else if (mod) { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 763 | 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] | 764 | " from \"%s\".", mod->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 765 | } else { |
Radek Krejci | 2efc45b | 2020-12-22 16:25:44 +0100 | [diff] [blame] | 766 | 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] | 767 | " of \"%s\".", sparent->name); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 768 | } |
| 769 | return LY_EVALID; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 770 | } 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] | 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | *snode = sibling; |
| 775 | return LY_SUCCESS; |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * @brief Read until the end of the current subtree. |
| 780 | * |
| 781 | * @param[in] lybctx LYB context. |
| 782 | */ |
| 783 | static void |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 784 | lyb_skip_subtree(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 785 | { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 786 | do { |
| 787 | /* first skip any meta information inside */ |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 788 | 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] | 789 | |
| 790 | /* then read data */ |
| 791 | lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx); |
| 792 | } while (LYB_LAST_SUBTREE(lybctx).written); |
| 793 | } |
| 794 | |
| 795 | /** |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 796 | * @brief Parse the context of anydata/anyxml node. |
| 797 | * |
| 798 | * @param[in] ctx libyang context. |
| 799 | * @param[in] data LYB data to parse. |
| 800 | * @param[out] tree Parsed tree. |
| 801 | * @return LY_ERR value. |
| 802 | */ |
| 803 | static LY_ERR |
| 804 | lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree) |
| 805 | { |
| 806 | LY_ERR ret; |
| 807 | uint32_t prev_lo; |
| 808 | struct ly_in *in; |
| 809 | struct lyd_ctx *lydctx = NULL; |
| 810 | |
| 811 | *tree = NULL; |
| 812 | |
| 813 | LY_CHECK_RET(ly_in_new_memory(data, &in)); |
| 814 | |
| 815 | /* turn logging off */ |
| 816 | prev_lo = ly_log_options(0); |
| 817 | |
Michal Vasko | 56d8860 | 2021-07-15 16:37:59 +0200 | [diff] [blame] | 818 | 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] | 819 | LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx); |
| 820 | |
| 821 | /* turn logging on again */ |
| 822 | ly_log_options(prev_lo); |
| 823 | |
| 824 | ly_in_free(in, 0); |
Michal Vasko | 1391e79 | 2021-08-23 12:15:44 +0200 | [diff] [blame] | 825 | if (lydctx) { |
| 826 | lydctx->free(lydctx); |
| 827 | } |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 828 | if (ret) { |
| 829 | lyd_free_siblings(*tree); |
| 830 | *tree = NULL; |
| 831 | } |
| 832 | return ret; |
| 833 | } |
| 834 | |
| 835 | /** |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 836 | * @brief Insert new node to @p parsed set. |
| 837 | * |
| 838 | * Also if needed, correct @p first_p. |
| 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,out] node Parsed node to insertion. |
| 843 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 844 | * @param[out] parsed Set of all successfully parsed nodes. |
| 845 | * @return LY_ERR value. |
| 846 | */ |
| 847 | static void |
| 848 | lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p, |
| 849 | struct ly_set *parsed) |
| 850 | { |
| 851 | /* insert, keep first pointer correct */ |
| 852 | lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0); |
| 853 | while (!parent && (*first_p)->prev->next) { |
| 854 | *first_p = (*first_p)->prev; |
| 855 | } |
| 856 | |
| 857 | /* rememeber a successfully parsed node */ |
| 858 | if (parsed) { |
| 859 | ly_set_add(parsed, node, 1, NULL); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * @brief Finish parsing the opaq node. |
| 865 | * |
| 866 | * @param[in] lybctx LYB context. |
| 867 | * @param[in] parent Data parent of the subtree, must be set if @p first_p is not. |
| 868 | * @param[in] flags Node flags to set. |
| 869 | * @param[in,out] attr Attributes to be attached. Finally set to NULL. |
| 870 | * @param[in,out] node Parsed opaq node to finish. |
| 871 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 872 | * @param[out] parsed Set of all successfully parsed nodes. |
| 873 | * @return LY_ERR value. |
| 874 | */ |
| 875 | static void |
| 876 | lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr, |
| 877 | struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed) |
| 878 | { |
| 879 | struct lyd_attr *iter; |
| 880 | |
| 881 | /* set flags */ |
| 882 | (*node)->flags = flags; |
| 883 | |
| 884 | /* add attributes */ |
| 885 | assert(!(*node)->schema); |
| 886 | LY_LIST_FOR(*attr, iter) { |
| 887 | iter->parent = (struct lyd_node_opaq *)*node; |
| 888 | } |
| 889 | ((struct lyd_node_opaq *)*node)->attr = *attr; |
| 890 | *attr = NULL; |
| 891 | |
| 892 | lyb_insert_node(lybctx, parent, *node, first_p, parsed); |
| 893 | *node = NULL; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * @brief Finish parsing the node. |
| 898 | * |
| 899 | * @param[in] lybctx LYB context. |
| 900 | * @param[in] parent Data parent of the subtree, must be set if @p first_p is not. |
| 901 | * @param[in] flags Node flags to set. |
| 902 | * @param[in,out] meta Metadata to be attached. Finally set to NULL. |
| 903 | * @param[in,out] node Parsed node to finish. |
| 904 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 905 | * @param[out] parsed Set of all successfully parsed nodes. |
| 906 | * @return LY_ERR value. |
| 907 | */ |
| 908 | static void |
| 909 | lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta, |
| 910 | struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed) |
| 911 | { |
| 912 | struct lyd_meta *m; |
| 913 | |
| 914 | /* set flags */ |
| 915 | (*node)->flags = flags; |
| 916 | |
| 917 | /* add metadata */ |
| 918 | LY_LIST_FOR(*meta, m) { |
| 919 | m->parent = *node; |
| 920 | } |
| 921 | (*node)->meta = *meta; |
| 922 | *meta = NULL; |
| 923 | |
| 924 | lyb_insert_node(lybctx, parent, *node, first_p, parsed); |
| 925 | *node = NULL; |
| 926 | } |
| 927 | |
| 928 | /** |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 929 | * @brief Create term node and fill it with value. |
| 930 | * |
| 931 | * @param[in] lybctx LYB context. |
| 932 | * @param[in] snode Schema of the term node. |
| 933 | * @param[out] node Created term node. |
| 934 | * @return LY_ERR value. |
| 935 | */ |
| 936 | static LY_ERR |
| 937 | lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node) |
| 938 | { |
| 939 | LY_ERR ret; |
| 940 | ly_bool dynamic; |
| 941 | uint8_t *term_value; |
| 942 | uint32_t term_value_len; |
| 943 | |
| 944 | ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx); |
| 945 | LY_CHECK_RET(ret); |
| 946 | |
| 947 | dynamic = 1; |
| 948 | /* create node */ |
| 949 | ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, |
| 950 | term_value, term_value_len, &dynamic, LY_VALUE_LYB, |
| 951 | NULL, LYD_HINT_DATA, node); |
| 952 | if (dynamic) { |
| 953 | free(term_value); |
| 954 | } |
| 955 | if (ret) { |
| 956 | lyd_free_tree(*node); |
| 957 | *node = NULL; |
| 958 | } |
| 959 | |
| 960 | return ret; |
| 961 | } |
| 962 | |
| 963 | /** |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 964 | * @brief Validate inner node, autodelete default values nad create implicit nodes. |
| 965 | * |
| 966 | * @param[in,out] lybctx LYB context. |
| 967 | * @param[in] snode Schema of the inner node. |
| 968 | * @param[in] node Parsed inner node. |
| 969 | * @return LY_ERR value. |
| 970 | */ |
| 971 | static LY_ERR |
| 972 | lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node) |
| 973 | { |
| 974 | LY_ERR ret = LY_SUCCESS; |
| 975 | uint32_t impl_opts; |
| 976 | |
| 977 | if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) { |
| 978 | /* new node validation, autodelete CANNOT occur, all nodes are new */ |
| 979 | ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL); |
| 980 | LY_CHECK_RET(ret); |
| 981 | |
| 982 | /* add any missing default children */ |
| 983 | impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0; |
| 984 | ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, |
| 985 | NULL, &lybctx->node_when, &lybctx->node_exts, |
| 986 | &lybctx->node_types, impl_opts, NULL); |
| 987 | LY_CHECK_RET(ret); |
| 988 | } |
| 989 | |
| 990 | return ret; |
| 991 | } |
| 992 | |
| 993 | /** |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 994 | * @brief Parse opaq node. |
| 995 | * |
| 996 | * @param[in] lybctx LYB context. |
| 997 | * @param[in] parent Data parent of the subtree. |
| 998 | * @param[in,out] first_p First top-level sibling. |
| 999 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1000 | * @return LY_ERR value. |
| 1001 | */ |
| 1002 | static LY_ERR |
| 1003 | lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed) |
| 1004 | { |
| 1005 | LY_ERR ret; |
| 1006 | struct lyd_node *node = NULL; |
| 1007 | struct lyd_attr *attr = NULL; |
| 1008 | char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL; |
| 1009 | ly_bool dynamic = 0; |
| 1010 | LY_VALUE_FORMAT format = 0; |
| 1011 | void *val_prefix_data = NULL; |
| 1012 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
| 1013 | uint32_t flags; |
| 1014 | |
| 1015 | if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) { |
| 1016 | /* unknown data, skip them */ |
| 1017 | lyb_skip_subtree(lybctx->lybctx); |
| 1018 | return LY_SUCCESS; |
| 1019 | } |
| 1020 | |
| 1021 | /* parse opaq node attributes */ |
| 1022 | ret = lyb_parse_attributes(lybctx->lybctx, &attr); |
| 1023 | LY_CHECK_GOTO(ret, cleanup); |
| 1024 | |
| 1025 | /* read flags */ |
| 1026 | lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx); |
| 1027 | |
| 1028 | /* parse prefix */ |
| 1029 | ret = lyb_read_string(&prefix, 1, lybctx->lybctx); |
| 1030 | LY_CHECK_GOTO(ret, cleanup); |
| 1031 | |
| 1032 | /* parse module key */ |
| 1033 | ret = lyb_read_string(&module_key, 1, lybctx->lybctx); |
| 1034 | LY_CHECK_GOTO(ret, cleanup); |
| 1035 | |
| 1036 | /* parse name */ |
| 1037 | ret = lyb_read_string(&name, 1, lybctx->lybctx); |
| 1038 | LY_CHECK_GOTO(ret, cleanup); |
| 1039 | |
| 1040 | /* parse value */ |
| 1041 | ret = lyb_read_string(&value, 1, lybctx->lybctx); |
| 1042 | LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup); |
| 1043 | dynamic = 1; |
| 1044 | |
| 1045 | /* parse format */ |
| 1046 | lyb_read_number(&format, sizeof format, 1, lybctx->lybctx); |
| 1047 | |
| 1048 | /* parse value prefixes */ |
| 1049 | ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data); |
| 1050 | LY_CHECK_GOTO(ret, cleanup); |
| 1051 | |
| 1052 | /* create node */ |
| 1053 | ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key), |
| 1054 | value, strlen(value), &dynamic, format, val_prefix_data, 0, &node); |
| 1055 | LY_CHECK_GOTO(ret, cleanup); |
| 1056 | |
| 1057 | /* process children */ |
| 1058 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
| 1059 | ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL); |
| 1060 | LY_CHECK_GOTO(ret, cleanup); |
| 1061 | } |
| 1062 | |
| 1063 | /* register parsed opaq node */ |
| 1064 | lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed); |
| 1065 | assert(!attr && !node); |
| 1066 | |
| 1067 | cleanup: |
| 1068 | free(prefix); |
| 1069 | free(module_key); |
| 1070 | free(name); |
| 1071 | if (dynamic) { |
| 1072 | free(value); |
| 1073 | } |
| 1074 | lyd_free_attr_siblings(ctx, attr); |
| 1075 | lyd_free_tree(node); |
| 1076 | |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1080 | /** |
| 1081 | * @brief Parse anydata or anyxml node. |
| 1082 | * |
| 1083 | * @param[in] lybctx LYB context. |
| 1084 | * @param[in] parent Data parent of the subtree. |
| 1085 | * @param[in] snode Schema of the node to be parsed. |
| 1086 | * @param[in,out] first_p First top-level sibling. |
| 1087 | * @param[out] parsed Set of all successfully parsed nodes. |
| 1088 | * @return LY_ERR value. |
| 1089 | */ |
| 1090 | static LY_ERR |
| 1091 | lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode, |
| 1092 | struct lyd_node **first_p, struct ly_set *parsed) |
| 1093 | { |
| 1094 | LY_ERR ret; |
| 1095 | struct lyd_node *node = NULL, *tree; |
| 1096 | struct lyd_meta *meta = NULL; |
| 1097 | LYD_ANYDATA_VALUETYPE value_type; |
| 1098 | char *value = NULL; |
| 1099 | const char *val_dict; |
| 1100 | uint32_t flags; |
| 1101 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
| 1102 | |
| 1103 | /* read necessary basic data */ |
| 1104 | ret = lyb_parse_node_header(lybctx, &flags, &meta); |
| 1105 | LY_CHECK_GOTO(ret, error); |
| 1106 | |
| 1107 | /* parse value type */ |
| 1108 | lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx); |
| 1109 | if (value_type == LYD_ANYDATA_DATATREE) { |
| 1110 | /* invalid situation */ |
| 1111 | LOGINT(ctx); |
| 1112 | ret = LY_EINT; |
| 1113 | goto error; |
| 1114 | } |
| 1115 | |
| 1116 | /* read anydata content */ |
| 1117 | ret = lyb_read_string(&value, 0, lybctx->lybctx); |
| 1118 | LY_CHECK_GOTO(ret, error); |
| 1119 | |
| 1120 | if (value_type == LYD_ANYDATA_LYB) { |
| 1121 | /* try to parse LYB into a data tree */ |
| 1122 | if (!lyb_parse_any_content(ctx, value, &tree)) { |
| 1123 | /* successfully parsed */ |
| 1124 | free(value); |
| 1125 | value = (char *)tree; |
| 1126 | value_type = LYD_ANYDATA_DATATREE; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | /* create the node */ |
| 1131 | switch (value_type) { |
| 1132 | case LYD_ANYDATA_LYB: |
| 1133 | case LYD_ANYDATA_DATATREE: |
| 1134 | /* use the value directly */ |
| 1135 | ret = lyd_create_any(snode, value, value_type, 1, &node); |
| 1136 | LY_CHECK_GOTO(ret, error); |
| 1137 | |
| 1138 | break; |
| 1139 | case LYD_ANYDATA_STRING: |
| 1140 | case LYD_ANYDATA_XML: |
| 1141 | case LYD_ANYDATA_JSON: |
| 1142 | /* value is expected to be in the dictionary */ |
| 1143 | ret = lydict_insert_zc(ctx, value, &val_dict); |
| 1144 | LY_CHECK_GOTO(ret, error); |
| 1145 | |
| 1146 | /* use the value in the dictionary */ |
| 1147 | ret = lyd_create_any(snode, val_dict, value_type, 1, &node); |
| 1148 | if (ret) { |
| 1149 | lydict_remove(ctx, val_dict); |
| 1150 | goto error; |
| 1151 | } |
| 1152 | break; |
| 1153 | default: |
| 1154 | LOGINT(ctx); |
| 1155 | ret = LY_EINT; |
| 1156 | goto error; |
| 1157 | } |
| 1158 | |
| 1159 | /* register parsed anydata node */ |
| 1160 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
| 1161 | |
| 1162 | return LY_SUCCESS; |
| 1163 | |
| 1164 | error: |
| 1165 | free(value); |
| 1166 | lyd_free_meta_siblings(meta); |
| 1167 | lyd_free_tree(node); |
| 1168 | return ret; |
| 1169 | } |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1170 | |
| 1171 | /** |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1172 | * @brief Parse header for non-opaq node. |
| 1173 | * |
| 1174 | * @param[in] lybctx LYB context. |
| 1175 | * @param[out] flags Parsed node flags. |
| 1176 | * @param[out] meta Parsed metadata of the node. |
| 1177 | * @return LY_ERR value. |
| 1178 | */ |
| 1179 | static LY_ERR |
| 1180 | lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta) |
| 1181 | { |
| 1182 | LY_ERR ret; |
| 1183 | |
| 1184 | /* create and read metadata */ |
| 1185 | ret = lyb_parse_metadata(lybctx, meta); |
| 1186 | LY_CHECK_RET(ret); |
| 1187 | |
| 1188 | /* read flags */ |
| 1189 | lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx); |
| 1190 | |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * @brief Parse model and hash. |
| 1196 | * |
| 1197 | * @param[in] lybctx LYB context. |
| 1198 | * @param[in] parent Data parent of the subtree. |
| 1199 | * @param[out] snode Schema of the node to be further parsed. Can be NULL for the opaq node. |
| 1200 | * @return LY_ERR value. |
| 1201 | */ |
| 1202 | static LY_ERR |
| 1203 | lyb_print_model_and_hash(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node **snode) |
| 1204 | { |
| 1205 | LY_ERR ret; |
| 1206 | const struct lys_module *mod; |
| 1207 | |
| 1208 | if (!parent || !parent->schema) { |
| 1209 | /* top-level or opaque, read module name */ |
| 1210 | ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod); |
| 1211 | LY_CHECK_RET(ret); |
| 1212 | |
| 1213 | /* read hash, find the schema node starting from mod */ |
| 1214 | ret = lyb_parse_schema_hash(lybctx, NULL, mod, snode); |
| 1215 | LY_CHECK_RET(ret); |
| 1216 | } else { |
| 1217 | /* read hash, find the schema node starting from parent schema */ |
| 1218 | ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, snode); |
| 1219 | LY_CHECK_RET(ret); |
| 1220 | } |
| 1221 | |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | /** |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1226 | * @brief Parse LYB subtree. |
| 1227 | * |
| 1228 | * @param[in] lybctx LYB context. |
| 1229 | * @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] | 1230 | * @param[in,out] first_p First top-level sibling, must be set if @p parent is not. |
| 1231 | * @param[out] parsed Set of all successfully parsed nodes. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1232 | * @return LY_ERR value. |
| 1233 | */ |
| 1234 | static LY_ERR |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1235 | 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] | 1236 | { |
| 1237 | LY_ERR ret = LY_SUCCESS; |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1238 | struct lyd_node *node = NULL; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1239 | const struct lysc_node *snode = NULL; |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1240 | struct lyd_meta *meta = NULL; |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1241 | uint32_t flags; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1242 | const struct ly_ctx *ctx = lybctx->lybctx->ctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1243 | |
| 1244 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1245 | LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1246 | |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1247 | ret = lyb_print_model_and_hash(lybctx, parent, &snode); |
| 1248 | LY_CHECK_RET(ret); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1249 | |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1250 | if (!snode) { |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1251 | ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1252 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 821cf73 | 2021-09-09 15:37:28 +0200 | [diff] [blame] | 1253 | goto stop_subtree; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1254 | } else if (snode->nodetype & LYD_NODE_TERM) { |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1255 | ret = lyb_parse_node_header(lybctx, &flags, &meta); |
| 1256 | LY_CHECK_GOTO(ret, cleanup); |
| 1257 | |
aPiecek | 33fc6b0 | 2021-09-09 15:45:37 +0200 | [diff] [blame] | 1258 | /* read value of term node and create it */ |
| 1259 | ret = lyb_create_term(lybctx, snode, &node); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1260 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1261 | |
| 1262 | /* complete the node processing */ |
| 1263 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1264 | } else if (snode->nodetype & LYD_NODE_INNER) { |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1265 | ret = lyb_parse_node_header(lybctx, &flags, &meta); |
| 1266 | LY_CHECK_GOTO(ret, cleanup); |
| 1267 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1268 | /* create node */ |
| 1269 | ret = lyd_create_inner(snode, &node); |
| 1270 | LY_CHECK_GOTO(ret, cleanup); |
| 1271 | |
| 1272 | /* process children */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1273 | while (LYB_LAST_SUBTREE(lybctx->lybctx).written) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1274 | ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1275 | LY_CHECK_GOTO(ret, cleanup); |
| 1276 | } |
| 1277 | |
aPiecek | 0e2e105 | 2021-09-09 15:48:27 +0200 | [diff] [blame] | 1278 | /* additional procedure for inner node */ |
| 1279 | ret = lyb_validate_node_inner(lybctx, snode, node); |
| 1280 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1281 | |
Michal Vasko | 751cb4d | 2020-07-14 12:25:28 +0200 | [diff] [blame] | 1282 | if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1283 | /* rememeber the RPC/action/notification */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1284 | lybctx->op_node = node; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1285 | } |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1286 | |
| 1287 | /* complete the node processing */ |
| 1288 | lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1289 | } else if (snode->nodetype & LYD_NODE_ANY) { |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1290 | ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed); |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1291 | LY_CHECK_GOTO(ret, cleanup); |
aPiecek | 18457d7 | 2021-09-09 15:52:20 +0200 | [diff] [blame] | 1292 | goto stop_subtree; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1293 | } else { |
aPiecek | 37c493b | 2021-09-09 12:52:30 +0200 | [diff] [blame] | 1294 | LOGINT(ctx); |
| 1295 | ret = LY_EINT; |
| 1296 | goto cleanup; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1297 | } |
| 1298 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1299 | stop_subtree: |
| 1300 | /* end the subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1301 | ret = lyb_read_stop_subtree(lybctx->lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1302 | LY_CHECK_GOTO(ret, cleanup); |
| 1303 | |
| 1304 | cleanup: |
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 | /** |
| 1311 | * @brief Parse used YANG data models. |
| 1312 | * |
| 1313 | * @param[in] lybctx LYB context. |
aPiecek | fc7cf7e | 2021-09-09 11:20:27 +0200 | [diff] [blame] | 1314 | * @param[in] parse_options Flag with options for parsing. |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1315 | * @return LY_ERR value. |
| 1316 | */ |
| 1317 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1318 | lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1319 | { |
| 1320 | LY_ERR ret; |
| 1321 | uint32_t count; |
Michal Vasko | fd69e1d | 2020-07-03 11:57:17 +0200 | [diff] [blame] | 1322 | LY_ARRAY_COUNT_TYPE u; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1323 | |
| 1324 | /* read model count */ |
| 1325 | lyb_read_number(&count, sizeof count, 2, lybctx); |
| 1326 | |
| 1327 | if (count) { |
| 1328 | LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM); |
| 1329 | |
| 1330 | /* read modules */ |
| 1331 | for (u = 0; u < count; ++u) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1332 | ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1333 | LY_CHECK_RET(ret); |
| 1334 | LY_ARRAY_INCREMENT(lybctx->models); |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | return LY_SUCCESS; |
| 1339 | } |
| 1340 | |
| 1341 | /** |
| 1342 | * @brief Parse LYB magic number. |
| 1343 | * |
| 1344 | * @param[in] lybctx LYB context. |
| 1345 | * @return LY_ERR value. |
| 1346 | */ |
| 1347 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1348 | lyb_parse_magic_number(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1349 | { |
| 1350 | char magic_byte = 0; |
| 1351 | |
| 1352 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1353 | if (magic_byte != 'l') { |
| 1354 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte); |
| 1355 | return LY_EINVAL; |
| 1356 | } |
| 1357 | |
| 1358 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1359 | if (magic_byte != 'y') { |
| 1360 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte); |
| 1361 | return LY_EINVAL; |
| 1362 | } |
| 1363 | |
| 1364 | lyb_read((uint8_t *)&magic_byte, 1, lybctx); |
| 1365 | if (magic_byte != 'b') { |
| 1366 | LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte); |
| 1367 | return LY_EINVAL; |
| 1368 | } |
| 1369 | |
| 1370 | return LY_SUCCESS; |
| 1371 | } |
| 1372 | |
| 1373 | /** |
| 1374 | * @brief Parse LYB header. |
| 1375 | * |
| 1376 | * @param[in] lybctx LYB context. |
| 1377 | * @return LY_ERR value. |
| 1378 | */ |
| 1379 | static LY_ERR |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1380 | lyb_parse_header(struct lylyb_ctx *lybctx) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1381 | { |
| 1382 | uint8_t byte = 0; |
| 1383 | |
| 1384 | /* version, future flags */ |
| 1385 | lyb_read((uint8_t *)&byte, sizeof byte, lybctx); |
| 1386 | |
| 1387 | if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) { |
| 1388 | 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] | 1389 | byte & LYB_VERSION_MASK, LYB_VERSION_NUM); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1390 | return LY_EINVAL; |
| 1391 | } |
| 1392 | |
| 1393 | return LY_SUCCESS; |
| 1394 | } |
| 1395 | |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1396 | static LY_ERR |
| 1397 | _lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1398 | 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] | 1399 | struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1400 | { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1401 | LY_ERR rc = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1402 | struct lyd_lyb_ctx *lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1403 | |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1404 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1405 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
Radek Krejci | 7931b19 | 2020-06-25 17:05:03 +0200 | [diff] [blame] | 1406 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1407 | lybctx = calloc(1, sizeof *lybctx); |
| 1408 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM); |
| 1409 | lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx); |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1410 | LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1411 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1412 | lybctx->lybctx->in = in; |
| 1413 | lybctx->lybctx->ctx = ctx; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1414 | lybctx->parse_opts = parse_opts; |
| 1415 | lybctx->val_opts = val_opts; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1416 | lybctx->int_opts = int_opts; |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1417 | lybctx->free = lyd_lyb_ctx_free; |
Radek Krejci | f16e254 | 2021-02-17 15:39:23 +0100 | [diff] [blame] | 1418 | lybctx->ext = ext; |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1419 | |
| 1420 | /* find the operation node if it exists already */ |
| 1421 | LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup); |
| 1422 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1423 | /* read magic number */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1424 | rc = lyb_parse_magic_number(lybctx->lybctx); |
| 1425 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1426 | |
| 1427 | /* read header */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1428 | rc = lyb_parse_header(lybctx->lybctx); |
| 1429 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1430 | |
| 1431 | /* read used models */ |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1432 | rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts); |
| 1433 | LY_CHECK_GOTO(rc, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1434 | |
| 1435 | /* read subtree(s) */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1436 | while (lybctx->lybctx->in->current[0]) { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1437 | rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed); |
| 1438 | LY_CHECK_GOTO(rc, cleanup); |
| 1439 | |
| 1440 | if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) { |
| 1441 | break; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) { |
| 1446 | LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node."); |
| 1447 | rc = LY_EVALID; |
| 1448 | goto cleanup; |
| 1449 | } |
| 1450 | if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) { |
| 1451 | LOGVAL(ctx, LYVE_DATA, "Missing the operation node."); |
| 1452 | rc = LY_EVALID; |
| 1453 | goto cleanup; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1457 | ly_in_skip(lybctx->lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1458 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1459 | cleanup: |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1460 | /* there should be no unres stored if validation should be skipped */ |
| 1461 | assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count && |
| 1462 | !lybctx->node_when.count)); |
| 1463 | |
| 1464 | if (rc) { |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1465 | lyd_lyb_ctx_free((struct lyd_ctx *)lybctx); |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1466 | } else { |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1467 | *lydctx_p = (struct lyd_ctx *)lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1468 | } |
Michal Vasko | e066574 | 2021-02-11 11:08:44 +0100 | [diff] [blame] | 1469 | return rc; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
Michal Vasko | 02ed9d8 | 2021-07-15 14:58:04 +0200 | [diff] [blame] | 1472 | LY_ERR |
| 1473 | lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent, |
| 1474 | struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type, |
| 1475 | struct ly_set *parsed, struct lyd_ctx **lydctx_p) |
| 1476 | { |
| 1477 | uint32_t int_opts; |
| 1478 | |
| 1479 | assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK)); |
| 1480 | assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK)); |
| 1481 | |
| 1482 | switch (data_type) { |
| 1483 | case LYD_TYPE_DATA_YANG: |
| 1484 | int_opts = LYD_INTOPT_WITH_SIBLINGS; |
| 1485 | break; |
| 1486 | case LYD_TYPE_RPC_YANG: |
| 1487 | int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS; |
| 1488 | break; |
| 1489 | case LYD_TYPE_NOTIF_YANG: |
| 1490 | int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS; |
| 1491 | break; |
| 1492 | case LYD_TYPE_REPLY_YANG: |
| 1493 | int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS; |
| 1494 | break; |
| 1495 | default: |
| 1496 | LOGINT(ctx); |
| 1497 | return LY_EINT; |
| 1498 | } |
| 1499 | |
| 1500 | return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p); |
| 1501 | } |
| 1502 | |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1503 | API int |
| 1504 | lyd_lyb_data_length(const char *data) |
| 1505 | { |
| 1506 | LY_ERR ret = LY_SUCCESS; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1507 | struct lylyb_ctx *lybctx; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1508 | int count, i; |
| 1509 | size_t len; |
| 1510 | uint8_t buf[LYB_SIZE_MAX]; |
| 1511 | |
| 1512 | if (!data) { |
| 1513 | return -1; |
| 1514 | } |
| 1515 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1516 | lybctx = calloc(1, sizeof *lybctx); |
| 1517 | LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM); |
| 1518 | ret = ly_in_new_memory(data, &lybctx->in); |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1519 | LY_CHECK_GOTO(ret, cleanup); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1520 | |
| 1521 | /* read magic number */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1522 | ret = lyb_parse_magic_number(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1523 | LY_CHECK_GOTO(ret, cleanup); |
| 1524 | |
| 1525 | /* read header */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1526 | ret = lyb_parse_header(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1527 | LY_CHECK_GOTO(ret, cleanup); |
| 1528 | |
| 1529 | /* read model count */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1530 | lyb_read_number(&count, sizeof count, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1531 | |
| 1532 | /* read all models */ |
| 1533 | for (i = 0; i < count; ++i) { |
| 1534 | /* module name length */ |
| 1535 | len = 0; |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1536 | lyb_read_number(&len, sizeof len, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1537 | |
| 1538 | /* model name */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1539 | lyb_read(buf, len, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1540 | |
| 1541 | /* revision */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1542 | lyb_read(buf, 2, lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1543 | } |
| 1544 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1545 | while (lybctx->in->current[0]) { |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1546 | /* register a new subtree */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1547 | ret = lyb_read_start_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1548 | LY_CHECK_GOTO(ret, cleanup); |
| 1549 | |
| 1550 | /* skip it */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1551 | lyb_skip_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1552 | |
| 1553 | /* subtree finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1554 | ret = lyb_read_stop_subtree(lybctx); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1555 | LY_CHECK_GOTO(ret, cleanup); |
| 1556 | } |
| 1557 | |
| 1558 | /* read the last zero, parsing finished */ |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1559 | ly_in_skip(lybctx->in, 1); |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1560 | |
| 1561 | cleanup: |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1562 | count = lybctx->in->current - lybctx->in->start; |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1563 | |
Radek Krejci | 1798aae | 2020-07-14 13:26:06 +0200 | [diff] [blame] | 1564 | ly_in_free(lybctx->in, 0); |
| 1565 | lylyb_ctx_free(lybctx); |
| 1566 | |
Michal Vasko | 63f3d84 | 2020-07-08 10:10:14 +0200 | [diff] [blame] | 1567 | return ret ? -1 : count; |
Michal Vasko | 60ea635 | 2020-06-29 13:39:39 +0200 | [diff] [blame] | 1568 | } |