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