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