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