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