blob: dbdb8b02dad219b153f8174ceac6cd78c6c1da40 [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
aPiecek570d7ed2021-09-10 07:15:35 +020052 LY_ARRAY_FREE(ctx->siblings);
Radek Krejci1798aae2020-07-14 13:26:06 +020053 LY_ARRAY_FREE(ctx->models);
54
55 LY_ARRAY_FOR(ctx->sib_hts, u) {
Michal Vasko77b7f90a2023-01-31 15:42:41 +010056 lyht_free(ctx->sib_hts[u].ht, NULL);
Radek Krejci1798aae2020-07-14 13:26:06 +020057 }
58 LY_ARRAY_FREE(ctx->sib_hts);
59
60 free(ctx);
61}
62
63void
64lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
65{
66 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
67
68 lyd_ctx_free(lydctx);
69 lylyb_ctx_free(ctx->lybctx);
70 free(ctx);
71}
72
Michal Vasko60ea6352020-06-29 13:39:39 +020073/**
aPiecek6828a312021-09-17 15:53:18 +020074 * @brief Read metadata about siblings.
75 *
76 * @param[out] sib Structure in which the metadata will be stored.
77 * @param[in] lybctx LYB context.
78 */
79static void
80lyb_read_sibling_meta(struct lyd_lyb_sibling *sib, struct lylyb_ctx *lybctx)
81{
82 uint8_t meta_buf[LYB_META_BYTES];
83 uint64_t num = 0;
84
85 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
86
87 memcpy(&num, meta_buf, LYB_SIZE_BYTES);
88 sib->written = le64toh(num);
89 memcpy(&num, meta_buf + LYB_SIZE_BYTES, LYB_INCHUNK_BYTES);
90 sib->inner_chunks = le64toh(num);
91
92 /* remember whether there is a following chunk or not */
93 sib->position = (sib->written == LYB_SIZE_MAX ? 1 : 0);
94}
95
96/**
Michal Vasko60ea6352020-06-29 13:39:39 +020097 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
98 *
99 * @param[in] buf Destination buffer.
100 * @param[in] count Number of bytes to read.
101 * @param[in] lybctx LYB context.
102 */
103static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200104lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200105{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200106 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200107 struct lyd_lyb_sibling *empty;
Michal Vasko60ea6352020-06-29 13:39:39 +0200108 size_t to_read;
Michal Vasko60ea6352020-06-29 13:39:39 +0200109
110 assert(lybctx);
111
112 while (1) {
113 /* check for fully-read (empty) data chunks */
114 to_read = count;
115 empty = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200116 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200117 /* we want the innermost chunks resolved first, so replace previous empty chunks,
118 * also ignore chunks that are completely finished, there is nothing for us to do */
aPiecek570d7ed2021-09-10 07:15:35 +0200119 if ((lybctx->siblings[u].written <= to_read) && lybctx->siblings[u].position) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200120 /* empty chunk, do not read more */
aPiecek570d7ed2021-09-10 07:15:35 +0200121 to_read = lybctx->siblings[u].written;
122 empty = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200123 }
124 }
125
126 if (!empty && !count) {
127 break;
128 }
129
130 /* we are actually reading some data, not just finishing another chunk */
131 if (to_read) {
132 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200133 ly_in_read(lybctx->in, buf, to_read);
134 } else {
135 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200136 }
137
aPiecek570d7ed2021-09-10 07:15:35 +0200138 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200139 /* decrease all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200140 lybctx->siblings[u].written -= to_read;
141 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200142 }
143 /* decrease count/buf */
144 count -= to_read;
145 if (buf) {
146 buf += to_read;
147 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200148 }
149
150 if (empty) {
151 /* read the next chunk meta information */
aPiecek6828a312021-09-17 15:53:18 +0200152 lyb_read_sibling_meta(empty, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200153 }
154 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200155}
156
157/**
158 * @brief Read a number.
159 *
160 * @param[in] num Destination buffer.
161 * @param[in] num_size Size of @p num.
162 * @param[in] bytes Number of bytes to read.
163 * @param[in] lybctx LYB context.
164 */
165static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200166lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200167{
168 uint64_t buf = 0;
169
170 lyb_read((uint8_t *)&buf, bytes, lybctx);
171
172 /* correct byte order */
173 buf = le64toh(buf);
174
175 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100176 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200177 *((uint8_t *)num) = buf;
178 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100179 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200180 *((uint16_t *)num) = buf;
181 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100182 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200183 *((uint32_t *)num) = buf;
184 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100185 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200186 *((uint64_t *)num) = buf;
187 break;
188 default:
189 LOGINT(lybctx->ctx);
190 }
191}
192
193/**
194 * @brief Read a string.
195 *
196 * @param[in] str Destination buffer, is allocated.
aPieceke99345d2021-09-30 12:49:59 +0200197 * @param[in] len_size Number of bytes on which the length of the string is written.
Michal Vasko60ea6352020-06-29 13:39:39 +0200198 * @param[in] lybctx LYB context.
199 * @return LY_ERR value.
200 */
201static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200202lyb_read_string(char **str, uint8_t len_size, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200203{
aPieceke99345d2021-09-30 12:49:59 +0200204 uint64_t len = 0;
205
206 assert((len_size == 1) || (len_size == 2) || (len_size == 4) || (len_size == 8));
Michal Vasko60ea6352020-06-29 13:39:39 +0200207
208 *str = NULL;
209
aPieceke99345d2021-09-30 12:49:59 +0200210 lyb_read_number(&len, sizeof len, len_size, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200211
212 *str = malloc((len + 1) * sizeof **str);
213 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
214
215 lyb_read((uint8_t *)*str, len, lybctx);
216
Michal Vaskocebbae52021-05-31 11:11:36 +0200217 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200218 return LY_SUCCESS;
219}
220
221/**
aPiecekd5796f92021-09-30 13:39:37 +0200222 * @brief Skip string.
223 *
224 * @param[in] len_size Number of bytes on which the length of the string is written.
225 * @param[in] lybctx LYB context.
226 */
227static void
228lyb_skip_string(uint8_t len_size, struct lylyb_ctx *lybctx)
229{
230 size_t len = 0;
231
232 lyb_read_number(&len, sizeof len, len_size, lybctx);
233
234 lyb_read(NULL, len, lybctx);
235}
236
237/**
aPiecek91eec232021-09-09 15:42:37 +0200238 * @brief Read value of term node.
aPiecekea304e32021-08-18 09:13:47 +0200239 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200240 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200241 * @param[out] term_value Set to term node value in dynamically
242 * allocated memory. The caller must release it.
243 * @param[out] term_value_len Value length in bytes. The zero byte is
244 * always included and is not counted.
245 * @param[in,out] lybctx LYB context.
246 * @return LY_ERR value.
247 */
248static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200249lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint64_t *term_value_len,
aPiecek91eec232021-09-09 15:42:37 +0200250 struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200251{
252 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200253 int32_t lyb_data_len;
254 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200255
aPiecekaa5b70a2021-08-30 08:33:25 +0200256 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200257
aPiecekaa5b70a2021-08-30 08:33:25 +0200258 /* Find out the size from @ref howtoDataLYB. */
259 if (term->type->basetype == LY_TYPE_LEAFREF) {
260 /* Leafref itself is ignored, the target is loaded directly. */
261 type_lf = (struct lysc_type_leafref *)term->type;
262 lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
263 } else {
264 lyb_data_len = term->type->plugin->lyb_data_len;
265 }
266
267 if (lyb_data_len < 0) {
268 /* Parse value size. */
269 lyb_read_number(term_value_len, sizeof *term_value_len,
270 sizeof *term_value_len, lybctx);
271 } else {
272 /* Data size is fixed. */
273 *term_value_len = lyb_data_len;
274 }
aPiecekea304e32021-08-18 09:13:47 +0200275
276 /* Allocate memory. */
277 allocated_size = *term_value_len + 1;
278 *term_value = malloc(allocated_size * sizeof **term_value);
279 LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM);
280
281 if (*term_value_len > 0) {
282 /* Parse value. */
283 lyb_read(*term_value, *term_value_len, lybctx);
284 }
285
286 /* Add extra zero byte regardless of whether it is string or not. */
287 (*term_value)[allocated_size - 1] = 0;
288
289 return LY_SUCCESS;
290}
291
292/**
aPiecek570d7ed2021-09-10 07:15:35 +0200293 * @brief Stop the current "siblings" - change LYB context state.
Michal Vasko60ea6352020-06-29 13:39:39 +0200294 *
295 * @param[in] lybctx LYB context.
296 * @return LY_ERR value.
297 */
298static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200299lyb_read_stop_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200300{
aPiecek570d7ed2021-09-10 07:15:35 +0200301 if (LYB_LAST_SIBLING(lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200302 LOGINT_RET(lybctx->ctx);
303 }
304
aPiecek570d7ed2021-09-10 07:15:35 +0200305 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200306 return LY_SUCCESS;
307}
308
309/**
aPiecek570d7ed2021-09-10 07:15:35 +0200310 * @brief Start a new "siblings" - change LYB context state but also read the expected metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200311 *
312 * @param[in] lybctx LYB context.
313 * @return LY_ERR value.
314 */
315static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200316lyb_read_start_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200317{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200318 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200319
aPiecek570d7ed2021-09-10 07:15:35 +0200320 u = LY_ARRAY_COUNT(lybctx->siblings);
321 if (u == lybctx->sibling_size) {
322 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
323 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200324 }
325
aPiecek570d7ed2021-09-10 07:15:35 +0200326 LY_ARRAY_INCREMENT(lybctx->siblings);
aPiecek6828a312021-09-17 15:53:18 +0200327 lyb_read_sibling_meta(&LYB_LAST_SIBLING(lybctx), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200328
Michal Vasko60ea6352020-06-29 13:39:39 +0200329 return LY_SUCCESS;
330}
331
332/**
Michal Vasko9883a3e2022-03-31 12:16:00 +0200333 * @brief Read YANG model info.
Michal Vasko60ea6352020-06-29 13:39:39 +0200334 *
335 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +0200336 * @param[out] mod_name Module name, if any.
337 * @param[out] mod_rev Module revision, "" if none.
Michal Vasko78041d12022-11-29 14:11:07 +0100338 * @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 +0200339 * @return LY_ERR value.
340 */
341static LY_ERR
Michal Vasko78041d12022-11-29 14:11:07 +0100342lyb_read_model(struct lylyb_ctx *lybctx, char **mod_name, char mod_rev[], struct ly_set *feat_set)
Michal Vasko60ea6352020-06-29 13:39:39 +0200343{
Michal Vasko78041d12022-11-29 14:11:07 +0100344 uint16_t i, rev, length;
345 char *str;
Michal Vasko60ea6352020-06-29 13:39:39 +0200346
Michal Vasko9883a3e2022-03-31 12:16:00 +0200347 *mod_name = NULL;
348 mod_rev[0] = '\0';
aPiecek339bdc32021-09-10 08:42:36 +0200349
Michal Vasko9883a3e2022-03-31 12:16:00 +0200350 lyb_read_number(&length, 2, 2, lybctx);
351 if (!length) {
352 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +0200353 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200354
Michal Vasko9883a3e2022-03-31 12:16:00 +0200355 /* module name */
Michal Vasko0d156b92022-04-01 12:27:04 +0200356 *mod_name = malloc(length + 1);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200357 LY_CHECK_ERR_RET(!*mod_name, LOGMEM(lybctx->ctx), LY_EMEM);
358 lyb_read(((uint8_t *)*mod_name), length, lybctx);
359 (*mod_name)[length] = '\0';
360
361 /* module revision */
Michal Vasko60ea6352020-06-29 13:39:39 +0200362 lyb_read_number(&rev, sizeof rev, 2, lybctx);
363
Michal Vasko60ea6352020-06-29 13:39:39 +0200364 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
366 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200367 }
368
Michal Vasko78041d12022-11-29 14:11:07 +0100369 if (!feat_set) {
370 /* enabled features not printed */
371 return LY_SUCCESS;
372 }
373
374 /* enabled feature count */
375 lyb_read_number(&length, sizeof length, sizeof length, lybctx);
376 if (!length) {
377 return LY_SUCCESS;
378 }
379
380 /* enabled features */
381 for (i = 0; i < length; ++i) {
Michal Vasko71541332022-12-01 16:17:20 +0100382 LY_CHECK_RET(lyb_read_string(&str, sizeof length, lybctx));
Michal Vasko78041d12022-11-29 14:11:07 +0100383 ly_set_add(feat_set, str, 1, NULL);
384 }
385
Michal Vasko9883a3e2022-03-31 12:16:00 +0200386 return LY_SUCCESS;
387}
388
389/**
390 * @brief Parse YANG model info.
391 *
392 * @param[in] lybctx LYB context.
393 * @param[in] parse_options Flag with options for parsing.
Michal Vasko78041d12022-11-29 14:11:07 +0100394 * @param[in] with_features Whether the enabled features were also printed and should be read.
Michal Vasko9883a3e2022-03-31 12:16:00 +0200395 * @param[out] mod Parsed module.
396 * @return LY_ERR value.
397 */
398static LY_ERR
Michal Vasko78041d12022-11-29 14:11:07 +0100399lyb_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 +0200400{
401 LY_ERR ret = LY_SUCCESS;
402 const struct lys_module *m = NULL;
403 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko78041d12022-11-29 14:11:07 +0100404 struct ly_set feat_set = {0};
405 struct lysp_feature *f = NULL;
406 uint32_t i, idx = 0;
407 ly_bool enabled;
Michal Vasko9883a3e2022-03-31 12:16:00 +0200408
409 /* read module info */
Michal Vasko78041d12022-11-29 14:11:07 +0100410 if ((ret = lyb_read_model(lybctx, &mod_name, mod_rev, with_features ? &feat_set : NULL))) {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200411 goto cleanup;
412 }
413
414 /* get the module */
415 if (mod_rev[0]) {
416 m = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
417 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !m) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 /* try to use an updated module */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200419 m = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
420 if (m && (!m->revision || (strcmp(m->revision, mod_rev) < 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200421 /* not an implemented module in a newer revision */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200422 m = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200423 }
424 }
425 } else {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200426 m = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200427 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200428
Michal Vasko9883a3e2022-03-31 12:16:00 +0200429 if (!m || !m->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200430 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200431 if (!m) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200432 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko9883a3e2022-03-31 12:16:00 +0200433 mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : "");
434 } else if (!m->implemented) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200435 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko9883a3e2022-03-31 12:16:00 +0200436 mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200437 }
438 ret = LY_EINVAL;
439 goto cleanup;
440 }
441
Michal Vasko78041d12022-11-29 14:11:07 +0100442 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +0200443 }
444
Michal Vasko78041d12022-11-29 14:11:07 +0100445 if (with_features) {
446 /* check features */
447 while ((f = lysp_feature_next(f, m->parsed, &idx))) {
448 enabled = 0;
449 for (i = 0; i < feat_set.count; ++i) {
450 if (!strcmp(feat_set.objs[i], f->name)) {
451 enabled = 1;
452 break;
453 }
454 }
455
456 if (enabled && !(f->flags & LYS_FENABLED)) {
457 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s\" has \"%s\" feature disabled.",
458 mod_name, f->name);
459 ret = LY_EINVAL;
460 goto cleanup;
461 } else if (!enabled && (f->flags & LYS_FENABLED)) {
462 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s\" has \"%s\" feature enabled.",
463 mod_name, f->name);
464 ret = LY_EINVAL;
465 goto cleanup;
466 }
467 }
Michal Vasko85d9edc2021-04-22 09:15:05 +0200468 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200469
Michal Vasko78041d12022-11-29 14:11:07 +0100470 /* fill cached hashes, if not already */
471 lyb_cache_module_hash(m);
472
Michal Vasko60ea6352020-06-29 13:39:39 +0200473cleanup:
Michal Vasko9883a3e2022-03-31 12:16:00 +0200474 *mod = m;
Michal Vasko60ea6352020-06-29 13:39:39 +0200475 free(mod_name);
Michal Vasko78041d12022-11-29 14:11:07 +0100476 ly_set_erase(&feat_set, free);
Michal Vasko60ea6352020-06-29 13:39:39 +0200477 return ret;
478}
479
480/**
481 * @brief Parse YANG node metadata.
482 *
483 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +0100484 * @param[in] sparent Schema parent node of the metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200485 * @param[out] meta Parsed metadata.
486 * @return LY_ERR value.
487 */
488static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100489lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200490{
491 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200492 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200493 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200494 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200495 const struct lys_module *mod;
496
497 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200498 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200499
500 /* read attributes */
501 for (i = 0; i < count; ++i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200502 /* find model */
Michal Vasko78041d12022-11-29 14:11:07 +0100503 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200504 LY_CHECK_GOTO(ret, cleanup);
505
506 if (!mod) {
aPiecekd5796f92021-09-30 13:39:37 +0200507 /* skip meta name */
508 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
509
510 /* skip meta value */
511 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
512 continue;
Michal Vasko60ea6352020-06-29 13:39:39 +0200513 }
514
515 /* meta name */
aPieceke99345d2021-09-30 12:49:59 +0200516 ret = lyb_read_string(&meta_name, sizeof(uint16_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200517 LY_CHECK_GOTO(ret, cleanup);
518
519 /* meta value */
aPieceke99345d2021-09-30 12:49:59 +0200520 ret = lyb_read_string(&meta_value, sizeof(uint64_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200521 LY_CHECK_GOTO(ret, cleanup);
522 dynamic = 1;
523
524 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200525 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 +0100526 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, sparent);
Michal Vasko60ea6352020-06-29 13:39:39 +0200527
528 /* free strings */
529 free(meta_name);
530 meta_name = NULL;
531 if (dynamic) {
532 free(meta_value);
533 dynamic = 0;
534 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200535
Radek Krejci1798aae2020-07-14 13:26:06 +0200536 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200537 }
538
539cleanup:
540 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200541 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200542 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200543 *meta = NULL;
544 }
545 return ret;
546}
547
548/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100549 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200550 *
551 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100552 * @param[in] format Prefix data format.
553 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200554 * @return LY_ERR value.
555 */
556static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200557lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200558{
559 LY_ERR ret = LY_SUCCESS;
560 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100561 struct ly_set *set = NULL;
562 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200563
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100564 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200565 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100566 /* read count */
567 lyb_read(&count, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200568
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100569 /* read all NS elements */
570 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200571
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100572 for (i = 0; i < count; ++i) {
573 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200574
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100575 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200576 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, sizeof(uint16_t), lybctx), cleanup);
Michal Vaskoa3494b42022-02-28 13:58:34 +0100577 if (!strlen(ns->prefix)) {
578 free(ns->prefix);
579 ns->prefix = NULL;
580 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100581
582 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200583 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, sizeof(uint16_t), lybctx), cleanup);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100584
585 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
586 ns = NULL;
587 }
588
589 *prefix_data = set;
590 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200591 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200592 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100593 /* nothing stored */
594 break;
595 default:
596 LOGINT(lybctx->ctx);
597 ret = LY_EINT;
598 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200599 }
600
601cleanup:
602 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100603 ly_free_prefix_data(format, set);
604 if (ns) {
605 free(ns->prefix);
606 free(ns->uri);
607 free(ns);
608 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200609 }
610 return ret;
611}
612
613/**
614 * @brief Parse opaque attributes.
615 *
616 * @param[in] lybctx LYB context.
617 * @param[out] attr Parsed attributes.
618 * @return LY_ERR value.
619 */
620static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200621lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200622{
623 LY_ERR ret = LY_SUCCESS;
624 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200625 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200626 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200627 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200628 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100629 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200630
631 /* read count */
632 lyb_read(&count, 1, lybctx);
633
634 /* read attributes */
635 for (i = 0; i < count; ++i) {
Michal Vasko0fdcd242020-11-11 19:12:30 +0100636 /* prefix, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200637 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200638 LY_CHECK_GOTO(ret, cleanup);
639 if (!prefix[0]) {
640 free(prefix);
641 prefix = NULL;
642 }
643
644 /* namespace, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200645 ret = lyb_read_string(&module_name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200646 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200647 if (!module_name[0]) {
648 free(module_name);
649 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200650 }
651
652 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200653 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200654 LY_CHECK_GOTO(ret, cleanup);
655
Michal Vasko60ea6352020-06-29 13:39:39 +0200656 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200657 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200658
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100659 /* value prefixes */
660 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
661 LY_CHECK_GOTO(ret, cleanup);
662
Michal Vasko60ea6352020-06-29 13:39:39 +0200663 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200664 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100665 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200666 dynamic = 1;
667
668 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100669 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200670 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200671 LY_CHECK_GOTO(ret, cleanup);
672
673 free(prefix);
674 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200675 free(module_name);
676 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200677 free(name);
678 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200679 assert(!dynamic);
680 value = NULL;
681
682 if (!*attr) {
683 *attr = attr2;
684 }
685
Michal Vasko60ea6352020-06-29 13:39:39 +0200686 LY_CHECK_GOTO(ret, cleanup);
687 }
688
689cleanup:
690 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200691 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200692 free(name);
693 if (dynamic) {
694 free(value);
695 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200696 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200697 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200698 *attr = NULL;
699 }
700 return ret;
701}
702
703/**
aPiecek619350d2021-09-09 16:06:59 +0200704 * @brief Fill @p hash with hash values.
705 *
706 * @param[in] lybctx LYB context.
707 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
708 * @param[out] hash_count Number of hashes in @p hash.
709 * @return LY_ERR value.
710 */
711static LY_ERR
712lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
713{
714 uint8_t i = 0, j;
715
716 /* read the first hash */
717 lyb_read(&hash[0], sizeof *hash, lybctx);
718
719 if (!hash[0]) {
720 *hash_count = i + 1;
721 return LY_SUCCESS;
722 }
723
724 /* based on the first hash read all the other ones, if any */
725 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
726 if (i > LYB_HASH_BITS) {
727 LOGINT_RET(lybctx->ctx);
728 }
729 }
730
731 /* move the first hash on its accurate position */
732 hash[i] = hash[0];
733
734 /* read the rest of hashes */
735 for (j = i; j; --j) {
736 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
737
738 /* correct collision ID */
739 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
740 /* preceded with zeros */
741 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
742 }
743
744 *hash_count = i + 1;
745
746 return LY_SUCCESS;
747}
748
749/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200750 * @brief Check whether a schema node matches a hash(es).
751 *
752 * @param[in] sibling Schema node to check.
753 * @param[in] hash Hash array to check.
754 * @param[in] hash_count Number of hashes in @p hash.
755 * @return non-zero if matches,
756 * @return 0 if not.
757 */
758static int
759lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
760{
761 LYB_HASH sibling_hash;
762 uint8_t i;
763
764 /* compare all the hashes starting from collision ID 0 */
765 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200766 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200767 if (sibling_hash != hash[i]) {
768 return 0;
769 }
770 }
771
772 return 1;
773}
774
775/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200776 * @brief Parse schema node hash.
777 *
778 * @param[in] lybctx LYB context.
779 * @param[in] sparent Schema parent, must be set if @p mod is not.
780 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
781 * @param[out] snode Parsed found schema node, may be NULL if opaque.
782 * @return LY_ERR value.
783 */
784static LY_ERR
785lyb_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 +0200786 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200787{
788 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200789 const struct lysc_node *sibling;
790 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200791 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200792 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200793
aPiecek570d7ed2021-09-10 07:15:35 +0200794 *snode = NULL;
795
aPiecek619350d2021-09-09 16:06:59 +0200796 ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
797 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200798
799 if (!hash[0]) {
800 /* opaque node */
801 return LY_SUCCESS;
802 }
803
aPiecek619350d2021-09-09 16:06:59 +0200804 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200805
806 /* find our node with matching hashes */
807 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100808 while (1) {
809 if (!sparent && lybctx->ext) {
810 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
811 } else {
812 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
813 }
814 if (!sibling) {
815 break;
816 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200817 /* skip schema nodes from models not present during printing */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200818 if (((sibling->module->ctx != lybctx->lybctx->ctx) || lyb_has_schema_model(sibling, lybctx->lybctx->models)) &&
aPiecek619350d2021-09-09 16:06:59 +0200819 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200820 /* match found */
821 break;
822 }
823 }
824
Michal Vaskoe0665742021-02-11 11:08:44 +0100825 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100826 if (lybctx->ext) {
827 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
828 lybctx->ext->def->name);
829 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100830 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200831 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200832 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100833 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200834 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200835 }
836 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200837 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200838 return ret;
839 }
840
841 *snode = sibling;
842 return LY_SUCCESS;
843}
844
845/**
Michal Vasko9883a3e2022-03-31 12:16:00 +0200846 * @brief Parse schema node name of a nested extension data node.
847 *
848 * @param[in] lybctx LYB context.
849 * @param[in] parent Data parent.
850 * @param[in] mod_name Module name of the node.
851 * @param[out] snode Parsed found schema node of a nested extension.
852 * @return LY_ERR value.
853 */
854static LY_ERR
855lyb_parse_schema_nested_ext(struct lyd_lyb_ctx *lybctx, const struct lyd_node *parent, const char *mod_name,
856 const struct lysc_node **snode)
857{
858 LY_ERR rc = LY_SUCCESS, r;
859 char *name = NULL;
860 struct lysc_ext_instance *ext;
861
862 assert(parent);
863
864 /* read schema node name */
865 LY_CHECK_GOTO(rc = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx), cleanup);
866
867 /* check for extension data */
868 r = ly_nested_ext_schema(parent, NULL, mod_name, mod_name ? strlen(mod_name) : 0, LY_VALUE_JSON, NULL, name,
869 strlen(name), snode, &ext);
870 if (r == LY_ENOT) {
871 /* failed to parse */
872 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "Failed to parse node \"%s\" as nested extension instance data.", name);
873 rc = LY_EINVAL;
874 goto cleanup;
875 } else if (r) {
876 /* error */
877 rc = r;
878 goto cleanup;
879 }
880
881 /* fill cached hashes in the module, it may be from a different context */
882 lyb_cache_module_hash((*snode)->module);
883
884cleanup:
885 free(name);
886 return rc;
887}
888
889/**
aPiecek570d7ed2021-09-10 07:15:35 +0200890 * @brief Read until the end of the current siblings.
Michal Vasko60ea6352020-06-29 13:39:39 +0200891 *
892 * @param[in] lybctx LYB context.
893 */
894static void
aPiecek570d7ed2021-09-10 07:15:35 +0200895lyb_skip_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200896{
Michal Vasko60ea6352020-06-29 13:39:39 +0200897 do {
898 /* first skip any meta information inside */
aPiecek570d7ed2021-09-10 07:15:35 +0200899 ly_in_skip(lybctx->in, LYB_LAST_SIBLING(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200900
901 /* then read data */
aPiecek570d7ed2021-09-10 07:15:35 +0200902 lyb_read(NULL, LYB_LAST_SIBLING(lybctx).written, lybctx);
903 } while (LYB_LAST_SIBLING(lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200904}
905
906/**
aPiecek37c493b2021-09-09 12:52:30 +0200907 * @brief Insert new node to @p parsed set.
908 *
909 * Also if needed, correct @p first_p.
910 *
911 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200912 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200913 * @param[in,out] node Parsed node to insertion.
914 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
915 * @param[out] parsed Set of all successfully parsed nodes.
916 * @return LY_ERR value.
917 */
918static void
919lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
920 struct ly_set *parsed)
921{
922 /* insert, keep first pointer correct */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200923 if (parent && (LYD_CTX(parent) != LYD_CTX(node))) {
Michal Vasko193dacd2022-10-13 08:43:05 +0200924 lyplg_ext_insert(parent, node);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200925 } else {
926 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
927 }
aPiecek37c493b2021-09-09 12:52:30 +0200928 while (!parent && (*first_p)->prev->next) {
929 *first_p = (*first_p)->prev;
930 }
931
932 /* rememeber a successfully parsed node */
933 if (parsed) {
934 ly_set_add(parsed, node, 1, NULL);
935 }
936}
937
938/**
939 * @brief Finish parsing the opaq node.
940 *
941 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200942 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200943 * @param[in] flags Node flags to set.
944 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
945 * @param[in,out] node Parsed opaq node to finish.
946 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
947 * @param[out] parsed Set of all successfully parsed nodes.
948 * @return LY_ERR value.
949 */
950static void
951lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
952 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
953{
954 struct lyd_attr *iter;
955
956 /* set flags */
957 (*node)->flags = flags;
958
959 /* add attributes */
960 assert(!(*node)->schema);
961 LY_LIST_FOR(*attr, iter) {
962 iter->parent = (struct lyd_node_opaq *)*node;
963 }
964 ((struct lyd_node_opaq *)*node)->attr = *attr;
965 *attr = NULL;
966
967 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
968 *node = NULL;
969}
970
971/**
972 * @brief Finish parsing the node.
973 *
974 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200975 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200976 * @param[in] flags Node flags to set.
977 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
978 * @param[in,out] node Parsed node to finish.
979 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
980 * @param[out] parsed Set of all successfully parsed nodes.
981 * @return LY_ERR value.
982 */
983static void
984lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
985 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
986{
987 struct lyd_meta *m;
988
989 /* set flags */
990 (*node)->flags = flags;
991
992 /* add metadata */
993 LY_LIST_FOR(*meta, m) {
994 m->parent = *node;
995 }
996 (*node)->meta = *meta;
997 *meta = NULL;
998
Michal Vasko135719f2022-08-25 12:18:17 +0200999 /* insert into parent */
aPiecek37c493b2021-09-09 12:52:30 +02001000 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
Michal Vasko135719f2022-08-25 12:18:17 +02001001
Michal Vaskoeba23112022-08-26 08:35:41 +02001002 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1003 /* store for ext instance node validation, if needed */
1004 (void)lyd_validate_node_ext(*node, &lybctx->ext_node);
1005 }
Michal Vasko135719f2022-08-25 12:18:17 +02001006
aPiecek37c493b2021-09-09 12:52:30 +02001007 *node = NULL;
1008}
1009
1010/**
aPiecek6fdac2f2021-09-10 11:21:10 +02001011 * @brief Parse header for non-opaq node.
1012 *
1013 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +01001014 * @param[in] sparent Schema parent node of the metadata.
aPiecek6fdac2f2021-09-10 11:21:10 +02001015 * @param[out] flags Parsed node flags.
1016 * @param[out] meta Parsed metadata of the node.
1017 * @return LY_ERR value.
1018 */
1019static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001020lyb_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 +02001021{
1022 LY_ERR ret;
1023
1024 /* create and read metadata */
Michal Vaskoddd76592022-01-17 13:34:48 +01001025 ret = lyb_parse_metadata(lybctx, sparent, meta);
aPiecek6fdac2f2021-09-10 11:21:10 +02001026 LY_CHECK_RET(ret);
1027
1028 /* read flags */
1029 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1030
1031 return ret;
1032}
1033
1034/**
aPiecek33fc6b02021-09-09 15:45:37 +02001035 * @brief Create term node and fill it with value.
1036 *
1037 * @param[in] lybctx LYB context.
1038 * @param[in] snode Schema of the term node.
1039 * @param[out] node Created term node.
1040 * @return LY_ERR value.
1041 */
1042static LY_ERR
1043lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
1044{
1045 LY_ERR ret;
1046 ly_bool dynamic;
1047 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +02001048 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +02001049
1050 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
1051 LY_CHECK_RET(ret);
1052
1053 dynamic = 1;
1054 /* create node */
1055 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
1056 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
1057 NULL, LYD_HINT_DATA, node);
1058 if (dynamic) {
1059 free(term_value);
1060 }
1061 if (ret) {
1062 lyd_free_tree(*node);
1063 *node = NULL;
1064 }
1065
1066 return ret;
1067}
1068
1069/**
aPiecek0e2e1052021-09-09 15:48:27 +02001070 * @brief Validate inner node, autodelete default values nad create implicit nodes.
1071 *
1072 * @param[in,out] lybctx LYB context.
1073 * @param[in] snode Schema of the inner node.
1074 * @param[in] node Parsed inner node.
1075 * @return LY_ERR value.
1076 */
1077static LY_ERR
1078lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
1079{
1080 LY_ERR ret = LY_SUCCESS;
1081 uint32_t impl_opts;
1082
1083 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1084 /* new node validation, autodelete CANNOT occur, all nodes are new */
1085 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
1086 LY_CHECK_RET(ret);
1087
1088 /* add any missing default children */
1089 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01001090 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 +02001091 &lybctx->ext_node, impl_opts, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001092 LY_CHECK_RET(ret);
1093 }
1094
1095 return ret;
1096}
1097
1098/**
aPiecek821cf732021-09-09 15:37:28 +02001099 * @brief Parse opaq node.
1100 *
1101 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001102 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001103 * @param[in,out] first_p First top-level sibling.
1104 * @param[out] parsed Set of all successfully parsed nodes.
1105 * @return LY_ERR value.
1106 */
1107static LY_ERR
1108lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1109{
1110 LY_ERR ret;
1111 struct lyd_node *node = NULL;
1112 struct lyd_attr *attr = NULL;
1113 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1114 ly_bool dynamic = 0;
1115 LY_VALUE_FORMAT format = 0;
1116 void *val_prefix_data = NULL;
1117 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1118 uint32_t flags;
1119
aPiecek821cf732021-09-09 15:37:28 +02001120 /* parse opaq node attributes */
1121 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1122 LY_CHECK_GOTO(ret, cleanup);
1123
1124 /* read flags */
1125 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1126
1127 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001128 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001129 LY_CHECK_GOTO(ret, cleanup);
1130
1131 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001132 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001133 LY_CHECK_GOTO(ret, cleanup);
1134
1135 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001136 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001137 LY_CHECK_GOTO(ret, cleanup);
1138
1139 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001140 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001141 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1142 dynamic = 1;
1143
1144 /* parse format */
1145 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1146
1147 /* parse value prefixes */
1148 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1149 LY_CHECK_GOTO(ret, cleanup);
1150
aPiecek570d7ed2021-09-10 07:15:35 +02001151 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001152 /* skip children */
1153 ret = lyb_read_start_siblings(lybctx->lybctx);
1154 LY_CHECK_GOTO(ret, cleanup);
1155 lyb_skip_siblings(lybctx->lybctx);
1156 ret = lyb_read_stop_siblings(lybctx->lybctx);
1157 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001158 goto cleanup;
1159 }
1160
aPiecek821cf732021-09-09 15:37:28 +02001161 /* create node */
1162 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1163 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1164 LY_CHECK_GOTO(ret, cleanup);
1165
1166 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001167 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1168 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001169
1170 /* register parsed opaq node */
1171 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1172 assert(!attr && !node);
1173
1174cleanup:
1175 free(prefix);
1176 free(module_key);
1177 free(name);
1178 if (dynamic) {
1179 free(value);
1180 }
1181 lyd_free_attr_siblings(ctx, attr);
1182 lyd_free_tree(node);
1183
1184 return ret;
1185}
1186
aPiecek18457d72021-09-09 15:52:20 +02001187/**
1188 * @brief Parse anydata or anyxml node.
1189 *
1190 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001191 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001192 * @param[in] snode Schema of the node to be parsed.
1193 * @param[in,out] first_p First top-level sibling.
1194 * @param[out] parsed Set of all successfully parsed nodes.
1195 * @return LY_ERR value.
1196 */
1197static LY_ERR
1198lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1199 struct lyd_node **first_p, struct ly_set *parsed)
1200{
1201 LY_ERR ret;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001202 struct lyd_node *node = NULL, *tree = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001203 struct lyd_meta *meta = NULL;
1204 LYD_ANYDATA_VALUETYPE value_type;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001205 struct ly_in *in;
1206 struct lyd_ctx *lydctx = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001207 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001208 uint32_t flags;
1209 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1210
1211 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001212 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001213 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001214
1215 /* parse value type */
1216 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001217 if ((value_type == LYD_ANYDATA_DATATREE) || ((snode->nodetype == LYS_ANYDATA) && (value_type != LYD_ANYDATA_LYB))) {
aPiecek18457d72021-09-09 15:52:20 +02001218 LOGINT(ctx);
1219 ret = LY_EINT;
1220 goto error;
1221 }
1222
1223 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001224 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001225 LY_CHECK_GOTO(ret, error);
1226
1227 if (value_type == LYD_ANYDATA_LYB) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001228 /* parse LYB into a data tree */
1229 LY_CHECK_RET(ly_in_new_memory(value, &in));
1230 ret = lyd_parse_lyb(ctx, NULL, NULL, &tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0,
1231 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, NULL, &lydctx);
1232 ly_in_free(in, 0);
1233 if (lydctx) {
1234 lydctx->free(lydctx);
aPiecek18457d72021-09-09 15:52:20 +02001235 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001236 LY_CHECK_ERR_GOTO(ret, lyd_free_siblings(tree), error);
1237
1238 /* use the parsed tree as the value */
1239 free(value);
1240 value = (char *)tree;
1241 value_type = LYD_ANYDATA_DATATREE;
aPiecek18457d72021-09-09 15:52:20 +02001242 }
1243
1244 /* create the node */
1245 switch (value_type) {
aPiecek18457d72021-09-09 15:52:20 +02001246 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001247 case LYD_ANYDATA_STRING:
1248 case LYD_ANYDATA_XML:
1249 case LYD_ANYDATA_JSON:
Michal Vasko742a5b12022-02-24 16:07:27 +01001250 /* use the value directly */
1251 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001252 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001253 break;
1254 default:
1255 LOGINT(ctx);
1256 ret = LY_EINT;
1257 goto error;
1258 }
1259
1260 /* register parsed anydata node */
1261 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1262
1263 return LY_SUCCESS;
1264
1265error:
1266 free(value);
1267 lyd_free_meta_siblings(meta);
1268 lyd_free_tree(node);
1269 return ret;
1270}
aPiecek821cf732021-09-09 15:37:28 +02001271
1272/**
aPiecek5777f122021-09-10 10:26:23 +02001273 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001274 *
1275 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001276 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1277 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001278 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1279 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001280 * @return LY_ERR value.
1281 */
1282static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001283lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001284 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001285{
1286 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001287 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001288 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001289 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001290
aPiecek307f0772021-09-10 09:09:47 +02001291 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001292 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001293 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001294
aPiecek5777f122021-09-10 10:26:23 +02001295 /* create node */
1296 ret = lyd_create_inner(snode, &node);
1297 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001298
aPiecek5777f122021-09-10 10:26:23 +02001299 /* process children */
1300 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1301 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001302
aPiecek5777f122021-09-10 10:26:23 +02001303 /* additional procedure for inner node */
1304 ret = lyb_validate_node_inner(lybctx, snode, node);
1305 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001306
aPiecek5777f122021-09-10 10:26:23 +02001307 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1308 /* rememeber the RPC/action/notification */
1309 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001310 }
1311
aPiecek307f0772021-09-10 09:09:47 +02001312 /* register parsed node */
1313 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1314
1315 return LY_SUCCESS;
1316
1317error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001318 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001319 lyd_free_tree(node);
1320 return ret;
1321}
1322
1323/**
aPiecek8a555d72021-09-10 10:15:26 +02001324 * @brief Parse leaf node.
1325 *
1326 * @param[in] lybctx LYB context.
1327 * @param[in] parent Data parent of the sibling.
1328 * @param[in] snode Schema of the node to be parsed.
1329 * @param[in,out] first_p First top-level sibling.
1330 * @param[out] parsed Set of all successfully parsed nodes.
1331 * @return LY_ERR value.
1332 */
1333static LY_ERR
1334lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1335 struct lyd_node **first_p, struct ly_set *parsed)
1336{
1337 LY_ERR ret;
1338 struct lyd_node *node = NULL;
1339 struct lyd_meta *meta = NULL;
1340 uint32_t flags;
1341
1342 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001343 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001344 LY_CHECK_GOTO(ret, error);
1345
1346 /* read value of term node and create it */
1347 ret = lyb_create_term(lybctx, snode, &node);
1348 LY_CHECK_GOTO(ret, error);
1349
1350 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1351
1352 return LY_SUCCESS;
1353
1354error:
1355 lyd_free_meta_siblings(meta);
1356 lyd_free_tree(node);
1357 return ret;
1358}
1359
1360/**
aPiecek5777f122021-09-10 10:26:23 +02001361 * @brief Parse all leaflist nodes which belong to same schema.
1362 *
1363 * @param[in] lybctx LYB context.
1364 * @param[in] parent Data parent of the sibling.
1365 * @param[in] snode Schema of the nodes to be parsed.
1366 * @param[in,out] first_p First top-level sibling.
1367 * @param[out] parsed Set of all successfully parsed nodes.
1368 * @return LY_ERR value.
1369 */
1370static LY_ERR
1371lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1372 struct lyd_node **first_p, struct ly_set *parsed)
1373{
1374 LY_ERR ret;
1375
1376 /* register a new sibling */
1377 ret = lyb_read_start_siblings(lybctx->lybctx);
1378 LY_CHECK_RET(ret);
1379
1380 /* process all siblings */
1381 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1382 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1383 LY_CHECK_RET(ret);
1384 }
1385
1386 /* end the sibling */
1387 ret = lyb_read_stop_siblings(lybctx->lybctx);
1388 LY_CHECK_RET(ret);
1389
1390 return ret;
1391}
1392
1393/**
aPiecek77d3e962021-09-10 10:33:59 +02001394 * @brief Parse all list nodes which belong to same schema.
1395 *
1396 * @param[in] lybctx LYB context.
1397 * @param[in] parent Data parent of the sibling.
1398 * @param[in] snode Schema of the nodes to be parsed.
1399 * @param[in,out] first_p First top-level sibling.
1400 * @param[out] parsed Set of all successfully parsed nodes.
1401 * @return LY_ERR value.
1402 */
1403static LY_ERR
1404lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1405 struct lyd_node **first_p, struct ly_set *parsed)
1406{
1407 LY_ERR ret;
1408 struct lyd_node *node = NULL;
1409 struct lyd_meta *meta = NULL;
1410 uint32_t flags;
1411
1412 /* register a new sibling */
1413 ret = lyb_read_start_siblings(lybctx->lybctx);
1414 LY_CHECK_RET(ret);
1415
1416 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1417 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001418 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001419 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001420
1421 /* create list node */
1422 ret = lyd_create_inner(snode, &node);
1423 LY_CHECK_GOTO(ret, error);
1424
1425 /* process children */
1426 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1427 LY_CHECK_GOTO(ret, error);
1428
1429 /* additional procedure for inner node */
1430 ret = lyb_validate_node_inner(lybctx, snode, node);
1431 LY_CHECK_GOTO(ret, error);
1432
1433 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1434 /* rememeber the RPC/action/notification */
1435 lybctx->op_node = node;
1436 }
1437
1438 /* register parsed list node */
1439 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1440 }
1441
1442 /* end the sibling */
1443 ret = lyb_read_stop_siblings(lybctx->lybctx);
1444 LY_CHECK_RET(ret);
1445
1446 return LY_SUCCESS;
1447
1448error:
1449 lyd_free_meta_siblings(meta);
1450 lyd_free_tree(node);
1451 return ret;
1452}
1453
1454/**
Michal Vasko9883a3e2022-03-31 12:16:00 +02001455 * @brief Parse a node.
aPiecek17737b52021-09-21 12:29:52 +02001456 *
aPiecek17737b52021-09-21 12:29:52 +02001457 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +02001458 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1459 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1460 * @param[in,out] parsed Set of all successfully parsed nodes to add to.
aPiecek17737b52021-09-21 12:29:52 +02001461 * @return LY_ERR value.
1462 */
1463static LY_ERR
1464lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1465 struct ly_set *parsed)
1466{
1467 LY_ERR ret;
1468 const struct lysc_node *snode;
1469 const struct lys_module *mod;
Michal Vasko9883a3e2022-03-31 12:16:00 +02001470 enum lylyb_node_type lyb_type;
1471 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek17737b52021-09-21 12:29:52 +02001472
Michal Vasko9883a3e2022-03-31 12:16:00 +02001473 /* read node type */
1474 lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx);
1475
1476 switch (lyb_type) {
1477 case LYB_NODE_TOP:
1478 /* top-level, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001479 LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod), cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001480
1481 /* read hash, find the schema node starting from mod */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001482 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup);
1483 break;
1484 case LYB_NODE_CHILD:
1485 case LYB_NODE_OPAQ:
1486 /* read hash, find the schema node starting from parent schema, if any */
1487 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, parent ? parent->schema : NULL, NULL, &snode), cleanup);
1488 break;
1489 case LYB_NODE_EXT:
1490 /* ext, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001491 LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev, NULL), cleanup);
Michal Vasko9883a3e2022-03-31 12:16:00 +02001492
1493 /* read schema node name, find the nexted ext schema node */
1494 LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup);
1495 break;
aPiecek17737b52021-09-21 12:29:52 +02001496 }
aPiecek17737b52021-09-21 12:29:52 +02001497
1498 if (!snode) {
1499 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1500 } else if (snode->nodetype & LYS_LEAFLIST) {
1501 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1502 } else if (snode->nodetype == LYS_LIST) {
1503 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1504 } else if (snode->nodetype & LYD_NODE_ANY) {
1505 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1506 } else if (snode->nodetype & LYD_NODE_INNER) {
1507 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1508 } else {
1509 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1510 }
Michal Vasko9883a3e2022-03-31 12:16:00 +02001511 LY_CHECK_GOTO(ret, cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001512
Michal Vasko9883a3e2022-03-31 12:16:00 +02001513cleanup:
1514 free(mod_name);
aPiecek17737b52021-09-21 12:29:52 +02001515 return ret;
1516}
1517
1518/**
aPiecek570d7ed2021-09-10 07:15:35 +02001519 * @brief Parse siblings (@ref lyb_print_siblings()).
1520 *
1521 * @param[in] lybctx LYB context.
1522 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1523 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1524 * @param[out] parsed Set of all successfully parsed nodes.
1525 * @return LY_ERR value.
1526 */
1527static LY_ERR
1528lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1529 struct ly_set *parsed)
1530{
aPiecek570d7ed2021-09-10 07:15:35 +02001531 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001532
1533 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1534
1535 /* register a new siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001536 LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001537
1538 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
Michal Vasko9883a3e2022-03-31 12:16:00 +02001539 LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed));
aPiecek570d7ed2021-09-10 07:15:35 +02001540
1541 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1542 break;
1543 }
1544 }
1545
1546 /* end the siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001547 LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001548
Michal Vasko9883a3e2022-03-31 12:16:00 +02001549 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +02001550}
1551
1552/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001553 * @brief Parse used YANG data models.
1554 *
1555 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001556 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001557 * @return LY_ERR value.
1558 */
1559static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001560lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001561{
1562 LY_ERR ret;
1563 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001564 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001565
1566 /* read model count */
1567 lyb_read_number(&count, sizeof count, 2, lybctx);
1568
1569 if (count) {
1570 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1571
1572 /* read modules */
1573 for (u = 0; u < count; ++u) {
Michal Vasko78041d12022-11-29 14:11:07 +01001574 ret = lyb_parse_model(lybctx, parse_options, 1, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001575 LY_CHECK_RET(ret);
1576 LY_ARRAY_INCREMENT(lybctx->models);
1577 }
1578 }
1579
1580 return LY_SUCCESS;
1581}
1582
1583/**
1584 * @brief Parse LYB magic number.
1585 *
1586 * @param[in] lybctx LYB context.
1587 * @return LY_ERR value.
1588 */
1589static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001590lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001591{
1592 char magic_byte = 0;
1593
1594 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1595 if (magic_byte != 'l') {
1596 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1597 return LY_EINVAL;
1598 }
1599
1600 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1601 if (magic_byte != 'y') {
1602 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1603 return LY_EINVAL;
1604 }
1605
1606 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1607 if (magic_byte != 'b') {
1608 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1609 return LY_EINVAL;
1610 }
1611
1612 return LY_SUCCESS;
1613}
1614
1615/**
1616 * @brief Parse LYB header.
1617 *
1618 * @param[in] lybctx LYB context.
1619 * @return LY_ERR value.
1620 */
1621static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001622lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001623{
1624 uint8_t byte = 0;
1625
1626 /* version, future flags */
1627 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1628
1629 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1630 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001631 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001632 return LY_EINVAL;
1633 }
1634
1635 return LY_SUCCESS;
1636}
1637
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001638LY_ERR
1639lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vasko02ed9d82021-07-15 14:58:04 +02001640 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 +01001641 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001642{
Michal Vaskoe0665742021-02-11 11:08:44 +01001643 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001644 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001645
Michal Vaskoe0665742021-02-11 11:08:44 +01001646 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1647 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001648
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001649 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1650
1651 if (subtree_sibling) {
1652 *subtree_sibling = 0;
1653 }
1654
Radek Krejci1798aae2020-07-14 13:26:06 +02001655 lybctx = calloc(1, sizeof *lybctx);
1656 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1657 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001658 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001659
Radek Krejci1798aae2020-07-14 13:26:06 +02001660 lybctx->lybctx->in = in;
1661 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001662 lybctx->parse_opts = parse_opts;
1663 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001664 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001665 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001666 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001667
1668 /* find the operation node if it exists already */
1669 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1670
Michal Vasko60ea6352020-06-29 13:39:39 +02001671 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001672 rc = lyb_parse_magic_number(lybctx->lybctx);
1673 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001674
1675 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001676 rc = lyb_parse_header(lybctx->lybctx);
1677 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001678
1679 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001680 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1681 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001682
aPiecek570d7ed2021-09-10 07:15:35 +02001683 /* read sibling(s) */
1684 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1685 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001686
1687 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1688 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1689 rc = LY_EVALID;
1690 goto cleanup;
1691 }
1692 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1693 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1694 rc = LY_EVALID;
1695 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001696 }
1697
1698 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001699 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001700
Michal Vasko60ea6352020-06-29 13:39:39 +02001701cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001702 /* there should be no unres stored if validation should be skipped */
1703 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1704 !lybctx->node_when.count));
1705
1706 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001707 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001708 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001709 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001710 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001711 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001712}
1713
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001714LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001715lyd_lyb_data_length(const char *data)
1716{
1717 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001718 struct lylyb_ctx *lybctx;
Michal Vasko78041d12022-11-29 14:11:07 +01001719 uint32_t count, feat_count, len = 0, i, j;
Michal Vasko60ea6352020-06-29 13:39:39 +02001720 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001721 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001722
1723 if (!data) {
1724 return -1;
1725 }
1726
Radek Krejci1798aae2020-07-14 13:26:06 +02001727 lybctx = calloc(1, sizeof *lybctx);
1728 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1729 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001730 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001731
1732 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001733 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001734 LY_CHECK_GOTO(ret, cleanup);
1735
1736 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001737 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001738 LY_CHECK_GOTO(ret, cleanup);
1739
1740 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001741 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001742
1743 /* read all models */
1744 for (i = 0; i < count; ++i) {
1745 /* module name length */
Radek Krejci1798aae2020-07-14 13:26:06 +02001746 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001747
1748 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001749 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001750
1751 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001752 lyb_read(buf, 2, lybctx);
Michal Vasko78041d12022-11-29 14:11:07 +01001753
1754 /* enabled feature count */
1755 lyb_read_number(&feat_count, sizeof feat_count, 2, lybctx);
1756
1757 /* enabled features */
1758 for (j = 0; j < feat_count; ++j) {
1759 /* feature name length */
1760 lyb_read_number(&len, sizeof len, 2, lybctx);
1761
1762 /* feature name */
1763 lyb_read(buf, len, lybctx);
1764 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001765 }
1766
aPiecek6828a312021-09-17 15:53:18 +02001767 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001768 /* register a new sibling */
1769 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001770 LY_CHECK_GOTO(ret, cleanup);
1771
1772 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001773 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001774
aPiecek570d7ed2021-09-10 07:15:35 +02001775 /* sibling finished */
1776 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001777 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001778 } else {
aPiecek6828a312021-09-17 15:53:18 +02001779 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001780 }
1781
1782 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001783 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001784
1785cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001786 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001787
Radek Krejci1798aae2020-07-14 13:26:06 +02001788 ly_in_free(lybctx->in, 0);
1789 lylyb_ctx_free(lybctx);
1790
Michal Vasko78041d12022-11-29 14:11:07 +01001791 return ret ? -1 : (int)count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001792}