blob: 9ea6397e9e3f8dc09d5f9c238500c1509bea31be [file] [log] [blame]
Michal Vasko60ea6352020-06-29 13:39:39 +02001/**
2 * @file parser_lyb.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief LYB data parser for libyang
5 *
Michal Vasko78041d12022-11-29 14:11:07 +01006 * Copyright (c) 2020 - 2022 CESNET, z.s.p.o.
Michal Vasko60ea6352020-06-29 13:39:39 +02007 *
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 Krejciad97c5f2020-06-30 09:19:28 +020018#include <stdint.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "common.h"
24#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "context.h"
26#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "hash_table.h"
28#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
32#include "parser_internal.h"
Michal Vasko9883a3e2022-03-31 12:16:00 +020033#include "plugins_exts.h"
Michal Vaskob4750962022-10-06 15:33:35 +020034#include "plugins_exts/metadata.h"
Radek Krejci77114102021-03-10 15:21:57 +010035#include "set.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020036#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010037#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020038#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010039#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020040#include "tree_schema.h"
41#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010042#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020043
Michal Vasko78041d12022-11-29 14:11:07 +010044static LY_ERR lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
45 struct ly_set *parsed);
aPiecek821cf732021-09-09 15:37:28 +020046
Radek Krejci1798aae2020-07-14 13:26:06 +020047void
48lylyb_ctx_free(struct lylyb_ctx *ctx)
49{
50 LY_ARRAY_COUNT_TYPE u;
51
Michal Vasko82df24a2023-02-21 15:02:11 +010052 if (!ctx) {
53 return;
54 }
55
aPiecek570d7ed2021-09-10 07:15:35 +020056 LY_ARRAY_FREE(ctx->siblings);
Radek Krejci1798aae2020-07-14 13:26:06 +020057 LY_ARRAY_FREE(ctx->models);
58
59 LY_ARRAY_FOR(ctx->sib_hts, u) {
Michal Vasko77b7f90a2023-01-31 15:42:41 +010060 lyht_free(ctx->sib_hts[u].ht, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +020061 }
62 LY_ARRAY_FREE(ctx->sib_hts);
63
64 free(ctx);
65}
66
67void
68lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
69{
70 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
71
Michal Vasko82df24a2023-02-21 15:02:11 +010072 if (!lydctx) {
73 return;
74 }
75
Radek Krejci1798aae2020-07-14 13:26:06 +020076 lyd_ctx_free(lydctx);
77 lylyb_ctx_free(ctx->lybctx);
78 free(ctx);
79}
80
Michal Vasko60ea6352020-06-29 13:39:39 +020081/**
aPiecek6828a312021-09-17 15:53:18 +020082 * @brief Read metadata about siblings.
83 *
84 * @param[out] sib Structure in which the metadata will be stored.
85 * @param[in] lybctx LYB context.
86 */
87static void
88lyb_read_sibling_meta(struct lyd_lyb_sibling *sib, struct lylyb_ctx *lybctx)
89{
90 uint8_t meta_buf[LYB_META_BYTES];
91 uint64_t num = 0;
92
93 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
94
95 memcpy(&num, meta_buf, LYB_SIZE_BYTES);
96 sib->written = le64toh(num);
97 memcpy(&num, meta_buf + LYB_SIZE_BYTES, LYB_INCHUNK_BYTES);
98 sib->inner_chunks = le64toh(num);
99
100 /* remember whether there is a following chunk or not */
101 sib->position = (sib->written == LYB_SIZE_MAX ? 1 : 0);
102}
103
104/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200105 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
106 *
107 * @param[in] buf Destination buffer.
108 * @param[in] count Number of bytes to read.
109 * @param[in] lybctx LYB context.
110 */
111static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200112lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200113{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200114 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200115 struct lyd_lyb_sibling *empty;
Michal Vasko60ea6352020-06-29 13:39:39 +0200116 size_t to_read;
Michal Vasko60ea6352020-06-29 13:39:39 +0200117
118 assert(lybctx);
119
120 while (1) {
121 /* check for fully-read (empty) data chunks */
122 to_read = count;
123 empty = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200124 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200125 /* we want the innermost chunks resolved first, so replace previous empty chunks,
126 * also ignore chunks that are completely finished, there is nothing for us to do */
aPiecek570d7ed2021-09-10 07:15:35 +0200127 if ((lybctx->siblings[u].written <= to_read) && lybctx->siblings[u].position) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200128 /* empty chunk, do not read more */
aPiecek570d7ed2021-09-10 07:15:35 +0200129 to_read = lybctx->siblings[u].written;
130 empty = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200131 }
132 }
133
134 if (!empty && !count) {
135 break;
136 }
137
138 /* we are actually reading some data, not just finishing another chunk */
139 if (to_read) {
140 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200141 ly_in_read(lybctx->in, buf, to_read);
142 } else {
143 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200144 }
145
aPiecek570d7ed2021-09-10 07:15:35 +0200146 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200147 /* decrease all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200148 lybctx->siblings[u].written -= to_read;
149 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200150 }
151 /* decrease count/buf */
152 count -= to_read;
153 if (buf) {
154 buf += to_read;
155 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200156 }
157
158 if (empty) {
159 /* read the next chunk meta information */
aPiecek6828a312021-09-17 15:53:18 +0200160 lyb_read_sibling_meta(empty, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200161 }
162 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200163}
164
165/**
166 * @brief Read a number.
167 *
168 * @param[in] num Destination buffer.
169 * @param[in] num_size Size of @p num.
170 * @param[in] bytes Number of bytes to read.
171 * @param[in] lybctx LYB context.
172 */
173static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200174lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200175{
176 uint64_t buf = 0;
177
178 lyb_read((uint8_t *)&buf, bytes, lybctx);
179
180 /* correct byte order */
181 buf = le64toh(buf);
182
183 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100184 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200185 *((uint8_t *)num) = buf;
186 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100187 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200188 *((uint16_t *)num) = buf;
189 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100190 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200191 *((uint32_t *)num) = buf;
192 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100193 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200194 *((uint64_t *)num) = buf;
195 break;
196 default:
197 LOGINT(lybctx->ctx);
198 }
199}
200
201/**
202 * @brief Read a string.
203 *
204 * @param[in] str Destination buffer, is allocated.
aPieceke99345d2021-09-30 12:49:59 +0200205 * @param[in] len_size Number of bytes on which the length of the string is written.
Michal Vasko60ea6352020-06-29 13:39:39 +0200206 * @param[in] lybctx LYB context.
207 * @return LY_ERR value.
208 */
209static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200210lyb_read_string(char **str, uint8_t len_size, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200211{
aPieceke99345d2021-09-30 12:49:59 +0200212 uint64_t len = 0;
213
214 assert((len_size == 1) || (len_size == 2) || (len_size == 4) || (len_size == 8));
Michal Vasko60ea6352020-06-29 13:39:39 +0200215
216 *str = NULL;
217
aPieceke99345d2021-09-30 12:49:59 +0200218 lyb_read_number(&len, sizeof len, len_size, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200219
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
Michal Vaskocebbae52021-05-31 11:11:36 +0200225 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200226 return LY_SUCCESS;
227}
228
229/**
aPiecekd5796f92021-09-30 13:39:37 +0200230 * @brief Skip string.
231 *
232 * @param[in] len_size Number of bytes on which the length of the string is written.
233 * @param[in] lybctx LYB context.
234 */
235static void
236lyb_skip_string(uint8_t len_size, struct lylyb_ctx *lybctx)
237{
238 size_t len = 0;
239
240 lyb_read_number(&len, sizeof len, len_size, lybctx);
241
242 lyb_read(NULL, len, lybctx);
243}
244
245/**
aPiecek91eec232021-09-09 15:42:37 +0200246 * @brief Read value of term node.
aPiecekea304e32021-08-18 09:13:47 +0200247 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200248 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200249 * @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 */
256static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200257lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint64_t *term_value_len,
aPiecek91eec232021-09-09 15:42:37 +0200258 struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200259{
260 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200261 int32_t lyb_data_len;
262 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200263
aPiecekaa5b70a2021-08-30 08:33:25 +0200264 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200265
aPiecekaa5b70a2021-08-30 08:33:25 +0200266 /* 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 }
aPiecekea304e32021-08-18 09:13:47 +0200283
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/**
aPiecek570d7ed2021-09-10 07:15:35 +0200301 * @brief Stop the current "siblings" - change LYB context state.
Michal Vasko60ea6352020-06-29 13:39:39 +0200302 *
303 * @param[in] lybctx LYB context.
304 * @return LY_ERR value.
305 */
306static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200307lyb_read_stop_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200308{
aPiecek570d7ed2021-09-10 07:15:35 +0200309 if (LYB_LAST_SIBLING(lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200310 LOGINT_RET(lybctx->ctx);
311 }
312
aPiecek570d7ed2021-09-10 07:15:35 +0200313 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200314 return LY_SUCCESS;
315}
316
317/**
aPiecek570d7ed2021-09-10 07:15:35 +0200318 * @brief Start a new "siblings" - change LYB context state but also read the expected metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200319 *
320 * @param[in] lybctx LYB context.
321 * @return LY_ERR value.
322 */
323static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200324lyb_read_start_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200325{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200326 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200327
aPiecek570d7ed2021-09-10 07:15:35 +0200328 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 Vasko60ea6352020-06-29 13:39:39 +0200332 }
333
aPiecek570d7ed2021-09-10 07:15:35 +0200334 LY_ARRAY_INCREMENT(lybctx->siblings);
aPiecek6828a312021-09-17 15:53:18 +0200335 lyb_read_sibling_meta(&LYB_LAST_SIBLING(lybctx), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200336
Michal Vasko60ea6352020-06-29 13:39:39 +0200337 return LY_SUCCESS;
338}
339
340/**
Michal Vasko9883a3e2022-03-31 12:16:00 +0200341 * @brief Read YANG model info.
Michal Vasko60ea6352020-06-29 13:39:39 +0200342 *
343 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +0200344 * @param[out] mod_name Module name, if any.
345 * @param[out] mod_rev Module revision, "" if none.
Michal Vasko78041d12022-11-29 14:11:07 +0100346 * @param[in,out] feat_set Set to add the names of enabled features to. If not set, enabled features are not parsed.
Michal Vasko60ea6352020-06-29 13:39:39 +0200347 * @return LY_ERR value.
348 */
349static LY_ERR
Michal Vasko78041d12022-11-29 14:11:07 +0100350lyb_read_model(struct lylyb_ctx *lybctx, char **mod_name, char mod_rev[], struct ly_set *feat_set)
Michal Vasko60ea6352020-06-29 13:39:39 +0200351{
Michal Vasko78041d12022-11-29 14:11:07 +0100352 uint16_t i, rev, length;
353 char *str;
Michal Vasko60ea6352020-06-29 13:39:39 +0200354
Michal Vasko9883a3e2022-03-31 12:16:00 +0200355 *mod_name = NULL;
356 mod_rev[0] = '\0';
aPiecek339bdc32021-09-10 08:42:36 +0200357
Michal Vasko9883a3e2022-03-31 12:16:00 +0200358 lyb_read_number(&length, 2, 2, lybctx);
359 if (!length) {
360 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +0200361 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200362
Michal Vasko9883a3e2022-03-31 12:16:00 +0200363 /* module name */
Michal Vasko0d156b92022-04-01 12:27:04 +0200364 *mod_name = malloc(length + 1);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200365 LY_CHECK_ERR_RET(!*mod_name, LOGMEM(lybctx->ctx), LY_EMEM);
366 lyb_read(((uint8_t *)*mod_name), length, lybctx);
367 (*mod_name)[length] = '\0';
368
369 /* module revision */
Michal Vasko60ea6352020-06-29 13:39:39 +0200370 lyb_read_number(&rev, sizeof rev, 2, lybctx);
371
Michal Vasko60ea6352020-06-29 13:39:39 +0200372 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100373 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
374 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200375 }
376
Michal Vasko78041d12022-11-29 14:11:07 +0100377 if (!feat_set) {
378 /* enabled features not printed */
379 return LY_SUCCESS;
380 }
381
382 /* enabled feature count */
383 lyb_read_number(&length, sizeof length, sizeof length, lybctx);
384 if (!length) {
385 return LY_SUCCESS;
386 }
387
388 /* enabled features */
389 for (i = 0; i < length; ++i) {
Michal Vasko71541332022-12-01 16:17:20 +0100390 LY_CHECK_RET(lyb_read_string(&str, sizeof length, lybctx));
Michal Vasko78041d12022-11-29 14:11:07 +0100391 ly_set_add(feat_set, str, 1, NULL);
392 }
393
Michal Vasko9883a3e2022-03-31 12:16:00 +0200394 return LY_SUCCESS;
395}
396
397/**
398 * @brief Parse YANG model info.
399 *
400 * @param[in] lybctx LYB context.
401 * @param[in] parse_options Flag with options for parsing.
Michal Vasko78041d12022-11-29 14:11:07 +0100402 * @param[in] with_features Whether the enabled features were also printed and should be read.
Michal Vasko9883a3e2022-03-31 12:16:00 +0200403 * @param[out] mod Parsed module.
404 * @return LY_ERR value.
405 */
406static LY_ERR
Michal Vasko78041d12022-11-29 14:11:07 +0100407lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, ly_bool with_features, const struct lys_module **mod)
Michal Vasko9883a3e2022-03-31 12:16:00 +0200408{
409 LY_ERR ret = LY_SUCCESS;
410 const struct lys_module *m = NULL;
411 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko78041d12022-11-29 14:11:07 +0100412 struct ly_set feat_set = {0};
413 struct lysp_feature *f = NULL;
414 uint32_t i, idx = 0;
415 ly_bool enabled;
Michal Vasko9883a3e2022-03-31 12:16:00 +0200416
417 /* read module info */
Michal Vasko78041d12022-11-29 14:11:07 +0100418 if ((ret = lyb_read_model(lybctx, &mod_name, mod_rev, with_features ? &feat_set : NULL))) {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200419 goto cleanup;
420 }
421
422 /* get the module */
423 if (mod_rev[0]) {
424 m = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
425 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !m) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200426 /* try to use an updated module */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200427 m = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
428 if (m && (!m->revision || (strcmp(m->revision, mod_rev) < 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200429 /* not an implemented module in a newer revision */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200430 m = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200431 }
432 }
433 } else {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200434 m = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200435 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200436
Michal Vasko9883a3e2022-03-31 12:16:00 +0200437 if (!m || !m->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200438 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200439 if (!m) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200440 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko9883a3e2022-03-31 12:16:00 +0200441 mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : "");
442 } else if (!m->implemented) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200443 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko9883a3e2022-03-31 12:16:00 +0200444 mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200445 }
446 ret = LY_EINVAL;
447 goto cleanup;
448 }
449
Michal Vasko78041d12022-11-29 14:11:07 +0100450 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +0200451 }
452
Michal Vasko78041d12022-11-29 14:11:07 +0100453 if (with_features) {
454 /* check features */
455 while ((f = lysp_feature_next(f, m->parsed, &idx))) {
456 enabled = 0;
457 for (i = 0; i < feat_set.count; ++i) {
458 if (!strcmp(feat_set.objs[i], f->name)) {
459 enabled = 1;
460 break;
461 }
462 }
463
464 if (enabled && !(f->flags & LYS_FENABLED)) {
465 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s\" has \"%s\" feature disabled.",
466 mod_name, f->name);
467 ret = LY_EINVAL;
468 goto cleanup;
469 } else if (!enabled && (f->flags & LYS_FENABLED)) {
470 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s\" has \"%s\" feature enabled.",
471 mod_name, f->name);
472 ret = LY_EINVAL;
473 goto cleanup;
474 }
475 }
Michal Vasko85d9edc2021-04-22 09:15:05 +0200476 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200477
Michal Vasko78041d12022-11-29 14:11:07 +0100478 /* fill cached hashes, if not already */
479 lyb_cache_module_hash(m);
480
Michal Vasko60ea6352020-06-29 13:39:39 +0200481cleanup:
Michal Vasko9883a3e2022-03-31 12:16:00 +0200482 *mod = m;
Michal Vasko60ea6352020-06-29 13:39:39 +0200483 free(mod_name);
Michal Vasko78041d12022-11-29 14:11:07 +0100484 ly_set_erase(&feat_set, free);
Michal Vasko60ea6352020-06-29 13:39:39 +0200485 return ret;
486}
487
488/**
489 * @brief Parse YANG node metadata.
490 *
491 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +0100492 * @param[in] sparent Schema parent node of the metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200493 * @param[out] meta Parsed metadata.
494 * @return LY_ERR value.
495 */
496static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100497lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200498{
499 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200500 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200501 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200502 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200503 const struct lys_module *mod;
504
505 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200506 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200507
508 /* read attributes */
509 for (i = 0; i < count; ++i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200510 /* find model */
Michal Vasko78041d12022-11-29 14:11:07 +0100511 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200512 LY_CHECK_GOTO(ret, cleanup);
513
514 if (!mod) {
aPiecekd5796f92021-09-30 13:39:37 +0200515 /* skip meta name */
516 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
517
518 /* skip meta value */
519 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
520 continue;
Michal Vasko60ea6352020-06-29 13:39:39 +0200521 }
522
523 /* meta name */
aPieceke99345d2021-09-30 12:49:59 +0200524 ret = lyb_read_string(&meta_name, sizeof(uint16_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200525 LY_CHECK_GOTO(ret, cleanup);
526
527 /* meta value */
aPieceke99345d2021-09-30 12:49:59 +0200528 ret = lyb_read_string(&meta_value, sizeof(uint64_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200529 LY_CHECK_GOTO(ret, cleanup);
530 dynamic = 1;
531
532 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200533 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100534 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, sparent);
Michal Vasko60ea6352020-06-29 13:39:39 +0200535
536 /* free strings */
537 free(meta_name);
538 meta_name = NULL;
539 if (dynamic) {
540 free(meta_value);
541 dynamic = 0;
542 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200543
Radek Krejci1798aae2020-07-14 13:26:06 +0200544 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200545 }
546
547cleanup:
548 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200549 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200550 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200551 *meta = NULL;
552 }
553 return ret;
554}
555
556/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100557 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200558 *
559 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100560 * @param[in] format Prefix data format.
561 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200562 * @return LY_ERR value.
563 */
564static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200565lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200566{
567 LY_ERR ret = LY_SUCCESS;
568 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100569 struct ly_set *set = NULL;
570 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200571
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100572 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200573 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100574 /* read count */
575 lyb_read(&count, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200576
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100577 /* read all NS elements */
578 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200579
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100580 for (i = 0; i < count; ++i) {
581 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200582
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100583 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200584 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, sizeof(uint16_t), lybctx), cleanup);
Michal Vaskoa3494b42022-02-28 13:58:34 +0100585 if (!strlen(ns->prefix)) {
586 free(ns->prefix);
587 ns->prefix = NULL;
588 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100589
590 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200591 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, sizeof(uint16_t), lybctx), cleanup);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100592
593 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
594 ns = NULL;
595 }
596
597 *prefix_data = set;
598 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200599 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200600 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100601 /* nothing stored */
602 break;
603 default:
604 LOGINT(lybctx->ctx);
605 ret = LY_EINT;
606 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200607 }
608
609cleanup:
610 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100611 ly_free_prefix_data(format, set);
612 if (ns) {
613 free(ns->prefix);
614 free(ns->uri);
615 free(ns);
616 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200617 }
618 return ret;
619}
620
621/**
622 * @brief Parse opaque attributes.
623 *
624 * @param[in] lybctx LYB context.
625 * @param[out] attr Parsed attributes.
626 * @return LY_ERR value.
627 */
628static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200629lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200630{
631 LY_ERR ret = LY_SUCCESS;
632 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200633 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200634 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200635 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200636 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100637 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200638
639 /* read count */
640 lyb_read(&count, 1, lybctx);
641
642 /* read attributes */
643 for (i = 0; i < count; ++i) {
Michal Vasko0fdcd242020-11-11 19:12:30 +0100644 /* prefix, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200645 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200646 LY_CHECK_GOTO(ret, cleanup);
647 if (!prefix[0]) {
648 free(prefix);
649 prefix = NULL;
650 }
651
652 /* namespace, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200653 ret = lyb_read_string(&module_name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200654 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200655 if (!module_name[0]) {
656 free(module_name);
657 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200658 }
659
660 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200661 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200662 LY_CHECK_GOTO(ret, cleanup);
663
Michal Vasko60ea6352020-06-29 13:39:39 +0200664 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200665 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200666
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100667 /* value prefixes */
668 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
669 LY_CHECK_GOTO(ret, cleanup);
670
Michal Vasko60ea6352020-06-29 13:39:39 +0200671 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200672 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100673 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200674 dynamic = 1;
675
676 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100677 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200678 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200679 LY_CHECK_GOTO(ret, cleanup);
680
681 free(prefix);
682 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200683 free(module_name);
684 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200685 free(name);
686 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200687 assert(!dynamic);
688 value = NULL;
689
690 if (!*attr) {
691 *attr = attr2;
692 }
693
Michal Vasko60ea6352020-06-29 13:39:39 +0200694 LY_CHECK_GOTO(ret, cleanup);
695 }
696
697cleanup:
698 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200699 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200700 free(name);
701 if (dynamic) {
702 free(value);
703 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200704 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200705 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200706 *attr = NULL;
707 }
708 return ret;
709}
710
711/**
aPiecek619350d2021-09-09 16:06:59 +0200712 * @brief Fill @p hash with hash values.
713 *
714 * @param[in] lybctx LYB context.
715 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
716 * @param[out] hash_count Number of hashes in @p hash.
717 * @return LY_ERR value.
718 */
719static LY_ERR
720lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
721{
722 uint8_t i = 0, j;
723
724 /* read the first hash */
725 lyb_read(&hash[0], sizeof *hash, lybctx);
726
727 if (!hash[0]) {
728 *hash_count = i + 1;
729 return LY_SUCCESS;
730 }
731
732 /* based on the first hash read all the other ones, if any */
733 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
734 if (i > LYB_HASH_BITS) {
735 LOGINT_RET(lybctx->ctx);
736 }
737 }
738
739 /* move the first hash on its accurate position */
740 hash[i] = hash[0];
741
742 /* read the rest of hashes */
743 for (j = i; j; --j) {
744 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
745
746 /* correct collision ID */
747 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
748 /* preceded with zeros */
749 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
750 }
751
752 *hash_count = i + 1;
753
754 return LY_SUCCESS;
755}
756
757/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200758 * @brief Check whether a schema node matches a hash(es).
759 *
760 * @param[in] sibling Schema node to check.
761 * @param[in] hash Hash array to check.
762 * @param[in] hash_count Number of hashes in @p hash.
763 * @return non-zero if matches,
764 * @return 0 if not.
765 */
766static int
767lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
768{
769 LYB_HASH sibling_hash;
770 uint8_t i;
771
772 /* compare all the hashes starting from collision ID 0 */
773 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200774 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200775 if (sibling_hash != hash[i]) {
776 return 0;
777 }
778 }
779
780 return 1;
781}
782
783/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200784 * @brief Parse schema node hash.
785 *
786 * @param[in] lybctx LYB context.
787 * @param[in] sparent Schema parent, must be set if @p mod is not.
788 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
789 * @param[out] snode Parsed found schema node, may be NULL if opaque.
790 * @return LY_ERR value.
791 */
792static LY_ERR
793lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, const struct lys_module *mod,
Radek Krejci0f969882020-08-21 16:56:47 +0200794 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200795{
796 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200797 const struct lysc_node *sibling;
798 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200799 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200800 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200801
aPiecek570d7ed2021-09-10 07:15:35 +0200802 *snode = NULL;
803
aPiecek619350d2021-09-09 16:06:59 +0200804 ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
805 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200806
807 if (!hash[0]) {
808 /* opaque node */
809 return LY_SUCCESS;
810 }
811
aPiecek619350d2021-09-09 16:06:59 +0200812 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200813
814 /* find our node with matching hashes */
815 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100816 while (1) {
817 if (!sparent && lybctx->ext) {
818 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
819 } else {
820 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
821 }
822 if (!sibling) {
823 break;
824 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200825 /* skip schema nodes from models not present during printing */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200826 if (((sibling->module->ctx != lybctx->lybctx->ctx) || lyb_has_schema_model(sibling, lybctx->lybctx->models)) &&
aPiecek619350d2021-09-09 16:06:59 +0200827 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200828 /* match found */
829 break;
830 }
831 }
832
Michal Vaskoe0665742021-02-11 11:08:44 +0100833 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100834 if (lybctx->ext) {
835 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
836 lybctx->ext->def->name);
837 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100838 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200839 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200840 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200842 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200843 }
844 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200845 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200846 return ret;
847 }
848
849 *snode = sibling;
850 return LY_SUCCESS;
851}
852
853/**
Michal Vasko9883a3e2022-03-31 12:16:00 +0200854 * @brief Parse schema node name of a nested extension data node.
855 *
856 * @param[in] lybctx LYB context.
857 * @param[in] parent Data parent.
858 * @param[in] mod_name Module name of the node.
859 * @param[out] snode Parsed found schema node of a nested extension.
860 * @return LY_ERR value.
861 */
862static LY_ERR
863lyb_parse_schema_nested_ext(struct lyd_lyb_ctx *lybctx, const struct lyd_node *parent, const char *mod_name,
864 const struct lysc_node **snode)
865{
866 LY_ERR rc = LY_SUCCESS, r;
867 char *name = NULL;
868 struct lysc_ext_instance *ext;
869
870 assert(parent);
871
872 /* read schema node name */
873 LY_CHECK_GOTO(rc = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx), cleanup);
874
875 /* check for extension data */
876 r = ly_nested_ext_schema(parent, NULL, mod_name, mod_name ? strlen(mod_name) : 0, LY_VALUE_JSON, NULL, name,
877 strlen(name), snode, &ext);
878 if (r == LY_ENOT) {
879 /* failed to parse */
880 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "Failed to parse node \"%s\" as nested extension instance data.", name);
881 rc = LY_EINVAL;
882 goto cleanup;
883 } else if (r) {
884 /* error */
885 rc = r;
886 goto cleanup;
887 }
888
889 /* fill cached hashes in the module, it may be from a different context */
890 lyb_cache_module_hash((*snode)->module);
891
892cleanup:
893 free(name);
894 return rc;
895}
896
897/**
aPiecek570d7ed2021-09-10 07:15:35 +0200898 * @brief Read until the end of the current siblings.
Michal Vasko60ea6352020-06-29 13:39:39 +0200899 *
900 * @param[in] lybctx LYB context.
901 */
902static void
aPiecek570d7ed2021-09-10 07:15:35 +0200903lyb_skip_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200904{
Michal Vasko60ea6352020-06-29 13:39:39 +0200905 do {
906 /* first skip any meta information inside */
aPiecek570d7ed2021-09-10 07:15:35 +0200907 ly_in_skip(lybctx->in, LYB_LAST_SIBLING(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200908
909 /* then read data */
aPiecek570d7ed2021-09-10 07:15:35 +0200910 lyb_read(NULL, LYB_LAST_SIBLING(lybctx).written, lybctx);
911 } while (LYB_LAST_SIBLING(lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200912}
913
914/**
aPiecek37c493b2021-09-09 12:52:30 +0200915 * @brief Insert new node to @p parsed set.
916 *
917 * Also if needed, correct @p first_p.
918 *
919 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200920 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200921 * @param[in,out] node Parsed node to insertion.
922 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
923 * @param[out] parsed Set of all successfully parsed nodes.
924 * @return LY_ERR value.
925 */
926static void
927lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
928 struct ly_set *parsed)
929{
930 /* insert, keep first pointer correct */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200931 if (parent && (LYD_CTX(parent) != LYD_CTX(node))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200932 lyplg_ext_insert(parent, node);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200933 } else {
934 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
935 }
aPiecek37c493b2021-09-09 12:52:30 +0200936 while (!parent && (*first_p)->prev->next) {
937 *first_p = (*first_p)->prev;
938 }
939
940 /* rememeber a successfully parsed node */
941 if (parsed) {
942 ly_set_add(parsed, node, 1, NULL);
943 }
944}
945
946/**
947 * @brief Finish parsing the opaq node.
948 *
949 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200950 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200951 * @param[in] flags Node flags to set.
952 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
953 * @param[in,out] node Parsed opaq node to finish.
954 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
955 * @param[out] parsed Set of all successfully parsed nodes.
956 * @return LY_ERR value.
957 */
958static void
959lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
960 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
961{
962 struct lyd_attr *iter;
963
964 /* set flags */
965 (*node)->flags = flags;
966
967 /* add attributes */
968 assert(!(*node)->schema);
969 LY_LIST_FOR(*attr, iter) {
970 iter->parent = (struct lyd_node_opaq *)*node;
971 }
972 ((struct lyd_node_opaq *)*node)->attr = *attr;
973 *attr = NULL;
974
975 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
976 *node = NULL;
977}
978
979/**
980 * @brief Finish parsing the node.
981 *
982 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200983 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200984 * @param[in] flags Node flags to set.
985 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
986 * @param[in,out] node Parsed node to finish.
987 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
988 * @param[out] parsed Set of all successfully parsed nodes.
989 * @return LY_ERR value.
990 */
991static void
992lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
993 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
994{
995 struct lyd_meta *m;
996
997 /* set flags */
998 (*node)->flags = flags;
999
1000 /* add metadata */
1001 LY_LIST_FOR(*meta, m) {
1002 m->parent = *node;
1003 }
1004 (*node)->meta = *meta;
1005 *meta = NULL;
1006
Michal Vasko135719f2022-08-25 12:18:17 +02001007 /* insert into parent */
aPiecek37c493b2021-09-09 12:52:30 +02001008 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
Michal Vasko135719f2022-08-25 12:18:17 +02001009
Michal Vaskoeba23112022-08-26 08:35:41 +02001010 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1011 /* store for ext instance node validation, if needed */
1012 (void)lyd_validate_node_ext(*node, &lybctx->ext_node);
1013 }
Michal Vasko135719f2022-08-25 12:18:17 +02001014
aPiecek37c493b2021-09-09 12:52:30 +02001015 *node = NULL;
1016}
1017
1018/**
aPiecek6fdac2f2021-09-10 11:21:10 +02001019 * @brief Parse header for non-opaq node.
1020 *
1021 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +01001022 * @param[in] sparent Schema parent node of the metadata.
aPiecek6fdac2f2021-09-10 11:21:10 +02001023 * @param[out] flags Parsed node flags.
1024 * @param[out] meta Parsed metadata of the node.
1025 * @return LY_ERR value.
1026 */
1027static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001028lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, uint32_t *flags, struct lyd_meta **meta)
aPiecek6fdac2f2021-09-10 11:21:10 +02001029{
1030 LY_ERR ret;
1031
1032 /* create and read metadata */
Michal Vaskoddd76592022-01-17 13:34:48 +01001033 ret = lyb_parse_metadata(lybctx, sparent, meta);
aPiecek6fdac2f2021-09-10 11:21:10 +02001034 LY_CHECK_RET(ret);
1035
1036 /* read flags */
1037 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1038
1039 return ret;
1040}
1041
1042/**
aPiecek33fc6b02021-09-09 15:45:37 +02001043 * @brief Create term node and fill it with value.
1044 *
1045 * @param[in] lybctx LYB context.
1046 * @param[in] snode Schema of the term node.
1047 * @param[out] node Created term node.
1048 * @return LY_ERR value.
1049 */
1050static LY_ERR
1051lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
1052{
1053 LY_ERR ret;
1054 ly_bool dynamic;
1055 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +02001056 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +02001057
1058 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
1059 LY_CHECK_RET(ret);
1060
1061 dynamic = 1;
1062 /* create node */
1063 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
1064 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
1065 NULL, LYD_HINT_DATA, node);
1066 if (dynamic) {
1067 free(term_value);
1068 }
1069 if (ret) {
1070 lyd_free_tree(*node);
1071 *node = NULL;
1072 }
1073
1074 return ret;
1075}
1076
1077/**
aPiecek0e2e1052021-09-09 15:48:27 +02001078 * @brief Validate inner node, autodelete default values nad create implicit nodes.
1079 *
1080 * @param[in,out] lybctx LYB context.
1081 * @param[in] snode Schema of the inner node.
1082 * @param[in] node Parsed inner node.
1083 * @return LY_ERR value.
1084 */
1085static LY_ERR
1086lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
1087{
1088 LY_ERR ret = LY_SUCCESS;
1089 uint32_t impl_opts;
1090
1091 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1092 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +01001093 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, 0, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001094 LY_CHECK_RET(ret);
1095
1096 /* add any missing default children */
1097 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01001098 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001099 &lybctx->ext_node, impl_opts, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001100 LY_CHECK_RET(ret);
1101 }
1102
1103 return ret;
1104}
1105
1106/**
aPiecek821cf732021-09-09 15:37:28 +02001107 * @brief Parse opaq node.
1108 *
1109 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001110 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001111 * @param[in,out] first_p First top-level sibling.
1112 * @param[out] parsed Set of all successfully parsed nodes.
1113 * @return LY_ERR value.
1114 */
1115static LY_ERR
1116lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1117{
1118 LY_ERR ret;
1119 struct lyd_node *node = NULL;
1120 struct lyd_attr *attr = NULL;
1121 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1122 ly_bool dynamic = 0;
1123 LY_VALUE_FORMAT format = 0;
1124 void *val_prefix_data = NULL;
1125 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1126 uint32_t flags;
1127
aPiecek821cf732021-09-09 15:37:28 +02001128 /* parse opaq node attributes */
1129 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1130 LY_CHECK_GOTO(ret, cleanup);
1131
1132 /* read flags */
1133 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1134
1135 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001136 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001137 LY_CHECK_GOTO(ret, cleanup);
1138
1139 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001140 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001141 LY_CHECK_GOTO(ret, cleanup);
1142
1143 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001144 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001145 LY_CHECK_GOTO(ret, cleanup);
1146
1147 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001148 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001149 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1150 dynamic = 1;
1151
1152 /* parse format */
1153 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1154
1155 /* parse value prefixes */
1156 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1157 LY_CHECK_GOTO(ret, cleanup);
1158
aPiecek570d7ed2021-09-10 07:15:35 +02001159 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001160 /* skip children */
1161 ret = lyb_read_start_siblings(lybctx->lybctx);
1162 LY_CHECK_GOTO(ret, cleanup);
1163 lyb_skip_siblings(lybctx->lybctx);
1164 ret = lyb_read_stop_siblings(lybctx->lybctx);
1165 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001166 goto cleanup;
1167 }
1168
aPiecek821cf732021-09-09 15:37:28 +02001169 /* create node */
1170 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1171 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1172 LY_CHECK_GOTO(ret, cleanup);
1173
1174 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001175 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1176 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001177
1178 /* register parsed opaq node */
1179 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1180 assert(!attr && !node);
1181
1182cleanup:
1183 free(prefix);
1184 free(module_key);
1185 free(name);
1186 if (dynamic) {
1187 free(value);
1188 }
1189 lyd_free_attr_siblings(ctx, attr);
1190 lyd_free_tree(node);
1191
1192 return ret;
1193}
1194
aPiecek18457d72021-09-09 15:52:20 +02001195/**
1196 * @brief Parse anydata or anyxml node.
1197 *
1198 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001199 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001200 * @param[in] snode Schema of the node to be parsed.
1201 * @param[in,out] first_p First top-level sibling.
1202 * @param[out] parsed Set of all successfully parsed nodes.
1203 * @return LY_ERR value.
1204 */
1205static LY_ERR
1206lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1207 struct lyd_node **first_p, struct ly_set *parsed)
1208{
1209 LY_ERR ret;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001210 struct lyd_node *node = NULL, *tree = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001211 struct lyd_meta *meta = NULL;
1212 LYD_ANYDATA_VALUETYPE value_type;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001213 struct ly_in *in;
1214 struct lyd_ctx *lydctx = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001215 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001216 uint32_t flags;
1217 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1218
1219 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001220 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001221 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001222
1223 /* parse value type */
1224 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001225 if ((value_type == LYD_ANYDATA_DATATREE) || ((snode->nodetype == LYS_ANYDATA) && (value_type != LYD_ANYDATA_LYB))) {
aPiecek18457d72021-09-09 15:52:20 +02001226 LOGINT(ctx);
1227 ret = LY_EINT;
1228 goto error;
1229 }
1230
1231 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001232 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001233 LY_CHECK_GOTO(ret, error);
1234
1235 if (value_type == LYD_ANYDATA_LYB) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001236 /* parse LYB into a data tree */
1237 LY_CHECK_RET(ly_in_new_memory(value, &in));
1238 ret = lyd_parse_lyb(ctx, NULL, NULL, &tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0,
1239 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, NULL, &lydctx);
1240 ly_in_free(in, 0);
1241 if (lydctx) {
1242 lydctx->free(lydctx);
aPiecek18457d72021-09-09 15:52:20 +02001243 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001244 LY_CHECK_ERR_GOTO(ret, lyd_free_siblings(tree), error);
1245
1246 /* use the parsed tree as the value */
1247 free(value);
1248 value = (char *)tree;
1249 value_type = LYD_ANYDATA_DATATREE;
aPiecek18457d72021-09-09 15:52:20 +02001250 }
1251
1252 /* create the node */
1253 switch (value_type) {
aPiecek18457d72021-09-09 15:52:20 +02001254 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001255 case LYD_ANYDATA_STRING:
1256 case LYD_ANYDATA_XML:
1257 case LYD_ANYDATA_JSON:
Michal Vasko742a5b12022-02-24 16:07:27 +01001258 /* use the value directly */
1259 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001260 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001261 break;
1262 default:
1263 LOGINT(ctx);
1264 ret = LY_EINT;
1265 goto error;
1266 }
1267
1268 /* register parsed anydata node */
1269 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1270
1271 return LY_SUCCESS;
1272
1273error:
1274 free(value);
1275 lyd_free_meta_siblings(meta);
1276 lyd_free_tree(node);
1277 return ret;
1278}
aPiecek821cf732021-09-09 15:37:28 +02001279
1280/**
aPiecek5777f122021-09-10 10:26:23 +02001281 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001282 *
1283 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001284 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1285 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001286 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1287 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001288 * @return LY_ERR value.
1289 */
1290static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001291lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001292 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001293{
1294 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001295 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001296 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001297 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001298
aPiecek307f0772021-09-10 09:09:47 +02001299 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001300 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001301 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001302
aPiecek5777f122021-09-10 10:26:23 +02001303 /* create node */
1304 ret = lyd_create_inner(snode, &node);
1305 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001306
aPiecek5777f122021-09-10 10:26:23 +02001307 /* process children */
1308 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1309 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001310
aPiecek5777f122021-09-10 10:26:23 +02001311 /* additional procedure for inner node */
1312 ret = lyb_validate_node_inner(lybctx, snode, node);
1313 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001314
aPiecek5777f122021-09-10 10:26:23 +02001315 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1316 /* rememeber the RPC/action/notification */
1317 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001318 }
1319
aPiecek307f0772021-09-10 09:09:47 +02001320 /* register parsed node */
1321 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1322
1323 return LY_SUCCESS;
1324
1325error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001326 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001327 lyd_free_tree(node);
1328 return ret;
1329}
1330
1331/**
aPiecek8a555d72021-09-10 10:15:26 +02001332 * @brief Parse leaf node.
1333 *
1334 * @param[in] lybctx LYB context.
1335 * @param[in] parent Data parent of the sibling.
1336 * @param[in] snode Schema of the node to be parsed.
1337 * @param[in,out] first_p First top-level sibling.
1338 * @param[out] parsed Set of all successfully parsed nodes.
1339 * @return LY_ERR value.
1340 */
1341static LY_ERR
1342lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1343 struct lyd_node **first_p, struct ly_set *parsed)
1344{
1345 LY_ERR ret;
1346 struct lyd_node *node = NULL;
1347 struct lyd_meta *meta = NULL;
1348 uint32_t flags;
1349
1350 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001351 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001352 LY_CHECK_GOTO(ret, error);
1353
1354 /* read value of term node and create it */
1355 ret = lyb_create_term(lybctx, snode, &node);
1356 LY_CHECK_GOTO(ret, error);
1357
1358 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1359
1360 return LY_SUCCESS;
1361
1362error:
1363 lyd_free_meta_siblings(meta);
1364 lyd_free_tree(node);
1365 return ret;
1366}
1367
1368/**
aPiecek5777f122021-09-10 10:26:23 +02001369 * @brief Parse all leaflist nodes which belong to same schema.
1370 *
1371 * @param[in] lybctx LYB context.
1372 * @param[in] parent Data parent of the sibling.
1373 * @param[in] snode Schema of the nodes to be parsed.
1374 * @param[in,out] first_p First top-level sibling.
1375 * @param[out] parsed Set of all successfully parsed nodes.
1376 * @return LY_ERR value.
1377 */
1378static LY_ERR
1379lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1380 struct lyd_node **first_p, struct ly_set *parsed)
1381{
1382 LY_ERR ret;
1383
1384 /* register a new sibling */
1385 ret = lyb_read_start_siblings(lybctx->lybctx);
1386 LY_CHECK_RET(ret);
1387
1388 /* process all siblings */
1389 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1390 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1391 LY_CHECK_RET(ret);
1392 }
1393
1394 /* end the sibling */
1395 ret = lyb_read_stop_siblings(lybctx->lybctx);
1396 LY_CHECK_RET(ret);
1397
1398 return ret;
1399}
1400
1401/**
aPiecek77d3e962021-09-10 10:33:59 +02001402 * @brief Parse all list nodes which belong to same schema.
1403 *
1404 * @param[in] lybctx LYB context.
1405 * @param[in] parent Data parent of the sibling.
1406 * @param[in] snode Schema of the nodes to be parsed.
1407 * @param[in,out] first_p First top-level sibling.
1408 * @param[out] parsed Set of all successfully parsed nodes.
1409 * @return LY_ERR value.
1410 */
1411static LY_ERR
1412lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1413 struct lyd_node **first_p, struct ly_set *parsed)
1414{
1415 LY_ERR ret;
1416 struct lyd_node *node = NULL;
1417 struct lyd_meta *meta = NULL;
1418 uint32_t flags;
1419
1420 /* register a new sibling */
1421 ret = lyb_read_start_siblings(lybctx->lybctx);
1422 LY_CHECK_RET(ret);
1423
1424 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1425 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001426 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001427 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001428
1429 /* create list node */
1430 ret = lyd_create_inner(snode, &node);
1431 LY_CHECK_GOTO(ret, error);
1432
1433 /* process children */
1434 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1435 LY_CHECK_GOTO(ret, error);
1436
1437 /* additional procedure for inner node */
1438 ret = lyb_validate_node_inner(lybctx, snode, node);
1439 LY_CHECK_GOTO(ret, error);
1440
1441 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1442 /* rememeber the RPC/action/notification */
1443 lybctx->op_node = node;
1444 }
1445
1446 /* register parsed list node */
1447 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1448 }
1449
1450 /* end the sibling */
1451 ret = lyb_read_stop_siblings(lybctx->lybctx);
1452 LY_CHECK_RET(ret);
1453
1454 return LY_SUCCESS;
1455
1456error:
1457 lyd_free_meta_siblings(meta);
1458 lyd_free_tree(node);
1459 return ret;
1460}
1461
1462/**
Michal Vasko9883a3e2022-03-31 12:16:00 +02001463 * @brief Parse a node.
aPiecek17737b52021-09-21 12:29:52 +02001464 *
aPiecek17737b52021-09-21 12:29:52 +02001465 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +02001466 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1467 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1468 * @param[in,out] parsed Set of all successfully parsed nodes to add to.
aPiecek17737b52021-09-21 12:29:52 +02001469 * @return LY_ERR value.
1470 */
1471static LY_ERR
1472lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1473 struct ly_set *parsed)
1474{
1475 LY_ERR ret;
1476 const struct lysc_node *snode;
1477 const struct lys_module *mod;
Michal Vasko9883a3e2022-03-31 12:16:00 +02001478 enum lylyb_node_type lyb_type;
1479 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek17737b52021-09-21 12:29:52 +02001480
Michal Vasko9883a3e2022-03-31 12:16:00 +02001481 /* read node type */
1482 lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx);
1483
1484 switch (lyb_type) {
1485 case LYB_NODE_TOP:
1486 /* top-level, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001487 LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod), cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001488
1489 /* read hash, find the schema node starting from mod */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001490 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup);
1491 break;
1492 case LYB_NODE_CHILD:
1493 case LYB_NODE_OPAQ:
1494 /* read hash, find the schema node starting from parent schema, if any */
1495 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, parent ? parent->schema : NULL, NULL, &snode), cleanup);
1496 break;
1497 case LYB_NODE_EXT:
1498 /* ext, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001499 LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev, NULL), cleanup);
Michal Vasko9883a3e2022-03-31 12:16:00 +02001500
1501 /* read schema node name, find the nexted ext schema node */
1502 LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup);
1503 break;
aPiecek17737b52021-09-21 12:29:52 +02001504 }
aPiecek17737b52021-09-21 12:29:52 +02001505
1506 if (!snode) {
1507 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1508 } else if (snode->nodetype & LYS_LEAFLIST) {
1509 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1510 } else if (snode->nodetype == LYS_LIST) {
1511 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1512 } else if (snode->nodetype & LYD_NODE_ANY) {
1513 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1514 } else if (snode->nodetype & LYD_NODE_INNER) {
1515 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1516 } else {
1517 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1518 }
Michal Vasko9883a3e2022-03-31 12:16:00 +02001519 LY_CHECK_GOTO(ret, cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001520
Michal Vasko9883a3e2022-03-31 12:16:00 +02001521cleanup:
1522 free(mod_name);
aPiecek17737b52021-09-21 12:29:52 +02001523 return ret;
1524}
1525
1526/**
aPiecek570d7ed2021-09-10 07:15:35 +02001527 * @brief Parse siblings (@ref lyb_print_siblings()).
1528 *
1529 * @param[in] lybctx LYB context.
1530 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1531 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1532 * @param[out] parsed Set of all successfully parsed nodes.
1533 * @return LY_ERR value.
1534 */
1535static LY_ERR
1536lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1537 struct ly_set *parsed)
1538{
aPiecek570d7ed2021-09-10 07:15:35 +02001539 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001540
1541 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1542
1543 /* register a new siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001544 LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001545
1546 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
Michal Vasko9883a3e2022-03-31 12:16:00 +02001547 LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed));
aPiecek570d7ed2021-09-10 07:15:35 +02001548
1549 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1550 break;
1551 }
1552 }
1553
1554 /* end the siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001555 LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001556
Michal Vasko9883a3e2022-03-31 12:16:00 +02001557 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +02001558}
1559
1560/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001561 * @brief Parse used YANG data models.
1562 *
1563 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001564 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001565 * @return LY_ERR value.
1566 */
1567static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001568lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001569{
1570 LY_ERR ret;
1571 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001572 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001573
1574 /* read model count */
1575 lyb_read_number(&count, sizeof count, 2, lybctx);
1576
1577 if (count) {
1578 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1579
1580 /* read modules */
1581 for (u = 0; u < count; ++u) {
Michal Vasko78041d12022-11-29 14:11:07 +01001582 ret = lyb_parse_model(lybctx, parse_options, 1, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001583 LY_CHECK_RET(ret);
1584 LY_ARRAY_INCREMENT(lybctx->models);
1585 }
1586 }
1587
1588 return LY_SUCCESS;
1589}
1590
1591/**
1592 * @brief Parse LYB magic number.
1593 *
1594 * @param[in] lybctx LYB context.
1595 * @return LY_ERR value.
1596 */
1597static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001598lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001599{
1600 char magic_byte = 0;
1601
1602 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1603 if (magic_byte != 'l') {
1604 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1605 return LY_EINVAL;
1606 }
1607
1608 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1609 if (magic_byte != 'y') {
1610 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1611 return LY_EINVAL;
1612 }
1613
1614 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1615 if (magic_byte != 'b') {
1616 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1617 return LY_EINVAL;
1618 }
1619
1620 return LY_SUCCESS;
1621}
1622
1623/**
1624 * @brief Parse LYB header.
1625 *
1626 * @param[in] lybctx LYB context.
1627 * @return LY_ERR value.
1628 */
1629static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001630lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001631{
1632 uint8_t byte = 0;
1633
1634 /* version, future flags */
1635 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1636
1637 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1638 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001639 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001640 return LY_EINVAL;
1641 }
1642
1643 return LY_SUCCESS;
1644}
1645
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001646LY_ERR
1647lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vasko02ed9d82021-07-15 14:58:04 +02001648 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001649 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001650{
Michal Vaskoe0665742021-02-11 11:08:44 +01001651 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001652 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001653
Michal Vaskoe0665742021-02-11 11:08:44 +01001654 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1655 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001656
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001657 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1658
1659 if (subtree_sibling) {
1660 *subtree_sibling = 0;
1661 }
1662
Radek Krejci1798aae2020-07-14 13:26:06 +02001663 lybctx = calloc(1, sizeof *lybctx);
1664 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1665 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001666 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001667
Radek Krejci1798aae2020-07-14 13:26:06 +02001668 lybctx->lybctx->in = in;
1669 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001670 lybctx->parse_opts = parse_opts;
1671 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001672 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001673 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001674 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001675
1676 /* find the operation node if it exists already */
1677 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1678
Michal Vasko60ea6352020-06-29 13:39:39 +02001679 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001680 rc = lyb_parse_magic_number(lybctx->lybctx);
1681 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001682
1683 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001684 rc = lyb_parse_header(lybctx->lybctx);
1685 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001686
1687 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001688 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1689 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001690
aPiecek570d7ed2021-09-10 07:15:35 +02001691 /* read sibling(s) */
1692 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1693 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001694
1695 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1696 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1697 rc = LY_EVALID;
1698 goto cleanup;
1699 }
1700 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1701 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1702 rc = LY_EVALID;
1703 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001704 }
1705
1706 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001707 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001708
Michal Vasko60ea6352020-06-29 13:39:39 +02001709cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001710 /* there should be no unres stored if validation should be skipped */
1711 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1712 !lybctx->node_when.count));
1713
1714 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001715 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001716 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001717 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001718 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001719 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001720}
1721
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001722LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001723lyd_lyb_data_length(const char *data)
1724{
1725 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001726 struct lylyb_ctx *lybctx;
Michal Vasko78041d12022-11-29 14:11:07 +01001727 uint32_t count, feat_count, len = 0, i, j;
Michal Vasko60ea6352020-06-29 13:39:39 +02001728 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001729 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001730
1731 if (!data) {
1732 return -1;
1733 }
1734
Radek Krejci1798aae2020-07-14 13:26:06 +02001735 lybctx = calloc(1, sizeof *lybctx);
1736 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1737 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001738 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001739
1740 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001741 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001742 LY_CHECK_GOTO(ret, cleanup);
1743
1744 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001745 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001746 LY_CHECK_GOTO(ret, cleanup);
1747
1748 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001749 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001750
1751 /* read all models */
1752 for (i = 0; i < count; ++i) {
1753 /* module name length */
Radek Krejci1798aae2020-07-14 13:26:06 +02001754 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001755
1756 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001757 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001758
1759 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001760 lyb_read(buf, 2, lybctx);
Michal Vasko78041d12022-11-29 14:11:07 +01001761
1762 /* enabled feature count */
1763 lyb_read_number(&feat_count, sizeof feat_count, 2, lybctx);
1764
1765 /* enabled features */
1766 for (j = 0; j < feat_count; ++j) {
1767 /* feature name length */
1768 lyb_read_number(&len, sizeof len, 2, lybctx);
1769
1770 /* feature name */
1771 lyb_read(buf, len, lybctx);
1772 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001773 }
1774
aPiecek6828a312021-09-17 15:53:18 +02001775 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001776 /* register a new sibling */
1777 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001778 LY_CHECK_GOTO(ret, cleanup);
1779
1780 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001781 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001782
aPiecek570d7ed2021-09-10 07:15:35 +02001783 /* sibling finished */
1784 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001785 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001786 } else {
aPiecek6828a312021-09-17 15:53:18 +02001787 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001788 }
1789
1790 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001791 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001792
1793cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001794 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001795
Radek Krejci1798aae2020-07-14 13:26:06 +02001796 ly_in_free(lybctx->in, 0);
1797 lylyb_ctx_free(lybctx);
1798
Michal Vasko78041d12022-11-29 14:11:07 +01001799 return ret ? -1 : (int)count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001800}