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