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