blob: 788be949945c92d7cbc1b2c13da67183844af238 [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),
Michal Vaskoee81f812023-05-25 13:32:51 +02001171 value, strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA, &node);
aPiecek821cf732021-09-09 15:37:28 +02001172 LY_CHECK_GOTO(ret, cleanup);
1173
Michal Vaskoa878a892023-08-18 12:22:07 +02001174 assert(node);
1175 LOG_LOCSET(NULL, node, NULL, NULL);
1176
aPiecek821cf732021-09-09 15:37:28 +02001177 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001178 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1179 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001180
1181 /* register parsed opaq node */
1182 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1183 assert(!attr && !node);
Michal Vaskoa878a892023-08-18 12:22:07 +02001184 LOG_LOCBACK(0, 1, 0, 0);
aPiecek821cf732021-09-09 15:37:28 +02001185
1186cleanup:
Michal Vaskoa878a892023-08-18 12:22:07 +02001187 if (node) {
1188 LOG_LOCBACK(0, 1, 0, 0);
1189 }
aPiecek821cf732021-09-09 15:37:28 +02001190 free(prefix);
1191 free(module_key);
1192 free(name);
1193 if (dynamic) {
1194 free(value);
1195 }
1196 lyd_free_attr_siblings(ctx, attr);
1197 lyd_free_tree(node);
1198
1199 return ret;
1200}
1201
aPiecek18457d72021-09-09 15:52:20 +02001202/**
1203 * @brief Parse anydata or anyxml node.
1204 *
1205 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001206 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001207 * @param[in] snode Schema of the node to be parsed.
1208 * @param[in,out] first_p First top-level sibling.
1209 * @param[out] parsed Set of all successfully parsed nodes.
1210 * @return LY_ERR value.
1211 */
1212static LY_ERR
1213lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1214 struct lyd_node **first_p, struct ly_set *parsed)
1215{
1216 LY_ERR ret;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001217 struct lyd_node *node = NULL, *tree = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001218 struct lyd_meta *meta = NULL;
1219 LYD_ANYDATA_VALUETYPE value_type;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001220 struct ly_in *in;
1221 struct lyd_ctx *lydctx = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001222 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001223 uint32_t flags;
1224 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1225
1226 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001227 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001228 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001229
1230 /* parse value type */
1231 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001232 if ((value_type == LYD_ANYDATA_DATATREE) || ((snode->nodetype == LYS_ANYDATA) && (value_type != LYD_ANYDATA_LYB))) {
aPiecek18457d72021-09-09 15:52:20 +02001233 LOGINT(ctx);
1234 ret = LY_EINT;
1235 goto error;
1236 }
1237
1238 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001239 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001240 LY_CHECK_GOTO(ret, error);
1241
1242 if (value_type == LYD_ANYDATA_LYB) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001243 /* parse LYB into a data tree */
1244 LY_CHECK_RET(ly_in_new_memory(value, &in));
1245 ret = lyd_parse_lyb(ctx, NULL, NULL, &tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0,
1246 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, NULL, &lydctx);
1247 ly_in_free(in, 0);
1248 if (lydctx) {
1249 lydctx->free(lydctx);
aPiecek18457d72021-09-09 15:52:20 +02001250 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001251 LY_CHECK_ERR_GOTO(ret, lyd_free_siblings(tree), error);
1252
1253 /* use the parsed tree as the value */
1254 free(value);
1255 value = (char *)tree;
1256 value_type = LYD_ANYDATA_DATATREE;
aPiecek18457d72021-09-09 15:52:20 +02001257 }
1258
1259 /* create the node */
1260 switch (value_type) {
aPiecek18457d72021-09-09 15:52:20 +02001261 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001262 case LYD_ANYDATA_STRING:
1263 case LYD_ANYDATA_XML:
1264 case LYD_ANYDATA_JSON:
Michal Vasko742a5b12022-02-24 16:07:27 +01001265 /* use the value directly */
1266 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001267 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001268 break;
1269 default:
1270 LOGINT(ctx);
1271 ret = LY_EINT;
1272 goto error;
1273 }
1274
Michal Vaskoa878a892023-08-18 12:22:07 +02001275 assert(node);
1276 LOG_LOCSET(NULL, node, NULL, NULL);
1277
aPiecek18457d72021-09-09 15:52:20 +02001278 /* register parsed anydata node */
1279 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1280
Michal Vaskoa878a892023-08-18 12:22:07 +02001281 LOG_LOCBACK(0, 1, 0, 0);
aPiecek18457d72021-09-09 15:52:20 +02001282 return LY_SUCCESS;
1283
1284error:
1285 free(value);
1286 lyd_free_meta_siblings(meta);
1287 lyd_free_tree(node);
1288 return ret;
1289}
aPiecek821cf732021-09-09 15:37:28 +02001290
1291/**
aPiecek5777f122021-09-10 10:26:23 +02001292 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001293 *
1294 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001295 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1296 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001297 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1298 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001299 * @return LY_ERR value.
1300 */
1301static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001302lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001303 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001304{
1305 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001306 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001307 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001308 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001309
aPiecek307f0772021-09-10 09:09:47 +02001310 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001311 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001312 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001313
aPiecek5777f122021-09-10 10:26:23 +02001314 /* create node */
1315 ret = lyd_create_inner(snode, &node);
1316 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001317
Michal Vaskoa878a892023-08-18 12:22:07 +02001318 assert(node);
1319 LOG_LOCSET(NULL, node, NULL, NULL);
1320
aPiecek5777f122021-09-10 10:26:23 +02001321 /* process children */
1322 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1323 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001324
aPiecek5777f122021-09-10 10:26:23 +02001325 /* additional procedure for inner node */
1326 ret = lyb_validate_node_inner(lybctx, snode, node);
1327 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001328
aPiecek5777f122021-09-10 10:26:23 +02001329 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1330 /* rememeber the RPC/action/notification */
1331 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001332 }
1333
aPiecek307f0772021-09-10 09:09:47 +02001334 /* register parsed node */
1335 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1336
Michal Vaskoa878a892023-08-18 12:22:07 +02001337 LOG_LOCBACK(0, 1, 0, 0);
aPiecek307f0772021-09-10 09:09:47 +02001338 return LY_SUCCESS;
1339
1340error:
Michal Vaskoa878a892023-08-18 12:22:07 +02001341 if (node) {
1342 LOG_LOCBACK(0, 1, 0, 0);
1343 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001344 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001345 lyd_free_tree(node);
1346 return ret;
1347}
1348
1349/**
aPiecek8a555d72021-09-10 10:15:26 +02001350 * @brief Parse leaf node.
1351 *
1352 * @param[in] lybctx LYB context.
1353 * @param[in] parent Data parent of the sibling.
1354 * @param[in] snode Schema of the node to be parsed.
1355 * @param[in,out] first_p First top-level sibling.
1356 * @param[out] parsed Set of all successfully parsed nodes.
1357 * @return LY_ERR value.
1358 */
1359static LY_ERR
1360lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1361 struct lyd_node **first_p, struct ly_set *parsed)
1362{
1363 LY_ERR ret;
1364 struct lyd_node *node = NULL;
1365 struct lyd_meta *meta = NULL;
1366 uint32_t flags;
1367
1368 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001369 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001370 LY_CHECK_GOTO(ret, error);
1371
1372 /* read value of term node and create it */
1373 ret = lyb_create_term(lybctx, snode, &node);
1374 LY_CHECK_GOTO(ret, error);
1375
Michal Vaskoa878a892023-08-18 12:22:07 +02001376 assert(node);
1377 LOG_LOCSET(NULL, node, NULL, NULL);
1378
aPiecek8a555d72021-09-10 10:15:26 +02001379 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1380
Michal Vaskoa878a892023-08-18 12:22:07 +02001381 LOG_LOCBACK(0, 1, 0, 0);
aPiecek8a555d72021-09-10 10:15:26 +02001382 return LY_SUCCESS;
1383
1384error:
1385 lyd_free_meta_siblings(meta);
1386 lyd_free_tree(node);
1387 return ret;
1388}
1389
1390/**
aPiecek5777f122021-09-10 10:26:23 +02001391 * @brief Parse all leaflist nodes which belong to same schema.
1392 *
1393 * @param[in] lybctx LYB context.
1394 * @param[in] parent Data parent of the sibling.
1395 * @param[in] snode Schema of the nodes to be parsed.
1396 * @param[in,out] first_p First top-level sibling.
1397 * @param[out] parsed Set of all successfully parsed nodes.
1398 * @return LY_ERR value.
1399 */
1400static LY_ERR
1401lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1402 struct lyd_node **first_p, struct ly_set *parsed)
1403{
1404 LY_ERR ret;
1405
1406 /* register a new sibling */
1407 ret = lyb_read_start_siblings(lybctx->lybctx);
1408 LY_CHECK_RET(ret);
1409
1410 /* process all siblings */
1411 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1412 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1413 LY_CHECK_RET(ret);
1414 }
1415
1416 /* end the sibling */
1417 ret = lyb_read_stop_siblings(lybctx->lybctx);
1418 LY_CHECK_RET(ret);
1419
1420 return ret;
1421}
1422
1423/**
aPiecek77d3e962021-09-10 10:33:59 +02001424 * @brief Parse all list nodes which belong to same schema.
1425 *
1426 * @param[in] lybctx LYB context.
1427 * @param[in] parent Data parent of the sibling.
1428 * @param[in] snode Schema of the nodes to be parsed.
1429 * @param[in,out] first_p First top-level sibling.
1430 * @param[out] parsed Set of all successfully parsed nodes.
1431 * @return LY_ERR value.
1432 */
1433static LY_ERR
1434lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1435 struct lyd_node **first_p, struct ly_set *parsed)
1436{
1437 LY_ERR ret;
1438 struct lyd_node *node = NULL;
1439 struct lyd_meta *meta = NULL;
1440 uint32_t flags;
Michal Vaskoa878a892023-08-18 12:22:07 +02001441 ly_bool log_node = 0;
aPiecek77d3e962021-09-10 10:33:59 +02001442
1443 /* register a new sibling */
1444 ret = lyb_read_start_siblings(lybctx->lybctx);
1445 LY_CHECK_RET(ret);
1446
1447 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1448 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001449 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001450 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001451
1452 /* create list node */
1453 ret = lyd_create_inner(snode, &node);
1454 LY_CHECK_GOTO(ret, error);
1455
Michal Vaskoa878a892023-08-18 12:22:07 +02001456 assert(node);
1457 LOG_LOCSET(NULL, node, NULL, NULL);
1458 log_node = 1;
1459
aPiecek77d3e962021-09-10 10:33:59 +02001460 /* process children */
1461 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1462 LY_CHECK_GOTO(ret, error);
1463
1464 /* additional procedure for inner node */
1465 ret = lyb_validate_node_inner(lybctx, snode, node);
1466 LY_CHECK_GOTO(ret, error);
1467
1468 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1469 /* rememeber the RPC/action/notification */
1470 lybctx->op_node = node;
1471 }
1472
1473 /* register parsed list node */
1474 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vaskoa878a892023-08-18 12:22:07 +02001475
1476 LOG_LOCBACK(0, 1, 0, 0);
1477 log_node = 0;
aPiecek77d3e962021-09-10 10:33:59 +02001478 }
1479
1480 /* end the sibling */
1481 ret = lyb_read_stop_siblings(lybctx->lybctx);
1482 LY_CHECK_RET(ret);
1483
1484 return LY_SUCCESS;
1485
1486error:
Michal Vaskoa878a892023-08-18 12:22:07 +02001487 if (log_node) {
1488 LOG_LOCBACK(0, 1, 0, 0);
1489 }
aPiecek77d3e962021-09-10 10:33:59 +02001490 lyd_free_meta_siblings(meta);
1491 lyd_free_tree(node);
1492 return ret;
1493}
1494
1495/**
Michal Vasko9883a3e2022-03-31 12:16:00 +02001496 * @brief Parse a node.
aPiecek17737b52021-09-21 12:29:52 +02001497 *
aPiecek17737b52021-09-21 12:29:52 +02001498 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +02001499 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1500 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1501 * @param[in,out] parsed Set of all successfully parsed nodes to add to.
aPiecek17737b52021-09-21 12:29:52 +02001502 * @return LY_ERR value.
1503 */
1504static LY_ERR
1505lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1506 struct ly_set *parsed)
1507{
1508 LY_ERR ret;
1509 const struct lysc_node *snode;
1510 const struct lys_module *mod;
Michal Vasko9883a3e2022-03-31 12:16:00 +02001511 enum lylyb_node_type lyb_type;
1512 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek17737b52021-09-21 12:29:52 +02001513
Michal Vasko9883a3e2022-03-31 12:16:00 +02001514 /* read node type */
1515 lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx);
1516
1517 switch (lyb_type) {
1518 case LYB_NODE_TOP:
1519 /* top-level, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001520 LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod), cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001521
1522 /* read hash, find the schema node starting from mod */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001523 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup);
1524 break;
1525 case LYB_NODE_CHILD:
1526 case LYB_NODE_OPAQ:
1527 /* read hash, find the schema node starting from parent schema, if any */
Michal Vaskoa878a892023-08-18 12:22:07 +02001528 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, lyd_parser_node_schema(parent), NULL, &snode), cleanup);
Michal Vasko9883a3e2022-03-31 12:16:00 +02001529 break;
1530 case LYB_NODE_EXT:
1531 /* ext, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001532 LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev, NULL), cleanup);
Michal Vasko9883a3e2022-03-31 12:16:00 +02001533
1534 /* read schema node name, find the nexted ext schema node */
1535 LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup);
1536 break;
aPiecek17737b52021-09-21 12:29:52 +02001537 }
aPiecek17737b52021-09-21 12:29:52 +02001538
1539 if (!snode) {
1540 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1541 } else if (snode->nodetype & LYS_LEAFLIST) {
1542 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1543 } else if (snode->nodetype == LYS_LIST) {
1544 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1545 } else if (snode->nodetype & LYD_NODE_ANY) {
1546 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1547 } else if (snode->nodetype & LYD_NODE_INNER) {
1548 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1549 } else {
1550 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1551 }
Michal Vasko9883a3e2022-03-31 12:16:00 +02001552 LY_CHECK_GOTO(ret, cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001553
Michal Vasko9883a3e2022-03-31 12:16:00 +02001554cleanup:
1555 free(mod_name);
aPiecek17737b52021-09-21 12:29:52 +02001556 return ret;
1557}
1558
1559/**
aPiecek570d7ed2021-09-10 07:15:35 +02001560 * @brief Parse siblings (@ref lyb_print_siblings()).
1561 *
1562 * @param[in] lybctx LYB context.
1563 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1564 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1565 * @param[out] parsed Set of all successfully parsed nodes.
1566 * @return LY_ERR value.
1567 */
1568static LY_ERR
1569lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1570 struct ly_set *parsed)
1571{
aPiecek570d7ed2021-09-10 07:15:35 +02001572 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001573
1574 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1575
1576 /* register a new siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001577 LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001578
1579 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
Michal Vasko9883a3e2022-03-31 12:16:00 +02001580 LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed));
aPiecek570d7ed2021-09-10 07:15:35 +02001581
1582 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1583 break;
1584 }
1585 }
1586
1587 /* end the siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001588 LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001589
Michal Vasko9883a3e2022-03-31 12:16:00 +02001590 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +02001591}
1592
1593/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001594 * @brief Parse used YANG data models.
1595 *
1596 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001597 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001598 * @return LY_ERR value.
1599 */
1600static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001601lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001602{
1603 LY_ERR ret;
1604 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001605 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001606
1607 /* read model count */
1608 lyb_read_number(&count, sizeof count, 2, lybctx);
1609
1610 if (count) {
1611 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1612
1613 /* read modules */
1614 for (u = 0; u < count; ++u) {
Michal Vasko78041d12022-11-29 14:11:07 +01001615 ret = lyb_parse_model(lybctx, parse_options, 1, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001616 LY_CHECK_RET(ret);
1617 LY_ARRAY_INCREMENT(lybctx->models);
1618 }
1619 }
1620
1621 return LY_SUCCESS;
1622}
1623
1624/**
1625 * @brief Parse LYB magic number.
1626 *
1627 * @param[in] lybctx LYB context.
1628 * @return LY_ERR value.
1629 */
1630static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001631lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001632{
1633 char magic_byte = 0;
1634
1635 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1636 if (magic_byte != 'l') {
1637 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1638 return LY_EINVAL;
1639 }
1640
1641 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1642 if (magic_byte != 'y') {
1643 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1644 return LY_EINVAL;
1645 }
1646
1647 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1648 if (magic_byte != 'b') {
1649 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1650 return LY_EINVAL;
1651 }
1652
1653 return LY_SUCCESS;
1654}
1655
1656/**
1657 * @brief Parse LYB header.
1658 *
1659 * @param[in] lybctx LYB context.
1660 * @return LY_ERR value.
1661 */
1662static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001663lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001664{
1665 uint8_t byte = 0;
1666
1667 /* version, future flags */
1668 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1669
1670 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1671 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001672 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001673 return LY_EINVAL;
1674 }
1675
1676 return LY_SUCCESS;
1677}
1678
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001679LY_ERR
1680lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vasko02ed9d82021-07-15 14:58:04 +02001681 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 +01001682 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001683{
Michal Vaskoe0665742021-02-11 11:08:44 +01001684 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001685 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001686
Michal Vaskoe0665742021-02-11 11:08:44 +01001687 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1688 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001689
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001690 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1691
1692 if (subtree_sibling) {
1693 *subtree_sibling = 0;
1694 }
1695
Radek Krejci1798aae2020-07-14 13:26:06 +02001696 lybctx = calloc(1, sizeof *lybctx);
1697 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1698 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001699 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001700
Radek Krejci1798aae2020-07-14 13:26:06 +02001701 lybctx->lybctx->in = in;
1702 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001703 lybctx->parse_opts = parse_opts;
1704 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001705 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001706 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001707 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001708
1709 /* find the operation node if it exists already */
1710 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1711
Michal Vasko60ea6352020-06-29 13:39:39 +02001712 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001713 rc = lyb_parse_magic_number(lybctx->lybctx);
1714 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001715
1716 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001717 rc = lyb_parse_header(lybctx->lybctx);
1718 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001719
1720 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001721 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1722 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001723
aPiecek570d7ed2021-09-10 07:15:35 +02001724 /* read sibling(s) */
1725 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1726 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001727
1728 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1729 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1730 rc = LY_EVALID;
1731 goto cleanup;
1732 }
1733 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1734 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1735 rc = LY_EVALID;
1736 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001737 }
1738
1739 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001740 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001741
Michal Vasko60ea6352020-06-29 13:39:39 +02001742cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001743 /* there should be no unres stored if validation should be skipped */
1744 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1745 !lybctx->node_when.count));
1746
1747 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001748 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001749 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001750 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001751 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001752 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001753}
1754
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001755LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001756lyd_lyb_data_length(const char *data)
1757{
1758 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001759 struct lylyb_ctx *lybctx;
Michal Vasko78041d12022-11-29 14:11:07 +01001760 uint32_t count, feat_count, len = 0, i, j;
Michal Vasko60ea6352020-06-29 13:39:39 +02001761 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001762 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001763
1764 if (!data) {
1765 return -1;
1766 }
1767
Radek Krejci1798aae2020-07-14 13:26:06 +02001768 lybctx = calloc(1, sizeof *lybctx);
1769 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1770 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001771 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001772
1773 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001774 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001775 LY_CHECK_GOTO(ret, cleanup);
1776
1777 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001778 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001779 LY_CHECK_GOTO(ret, cleanup);
1780
1781 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001782 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001783
1784 /* read all models */
1785 for (i = 0; i < count; ++i) {
1786 /* module name length */
Radek Krejci1798aae2020-07-14 13:26:06 +02001787 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001788
1789 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001790 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001791
1792 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001793 lyb_read(buf, 2, lybctx);
Michal Vasko78041d12022-11-29 14:11:07 +01001794
1795 /* enabled feature count */
1796 lyb_read_number(&feat_count, sizeof feat_count, 2, lybctx);
1797
1798 /* enabled features */
1799 for (j = 0; j < feat_count; ++j) {
1800 /* feature name length */
1801 lyb_read_number(&len, sizeof len, 2, lybctx);
1802
1803 /* feature name */
1804 lyb_read(buf, len, lybctx);
1805 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001806 }
1807
aPiecek6828a312021-09-17 15:53:18 +02001808 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001809 /* register a new sibling */
1810 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001811 LY_CHECK_GOTO(ret, cleanup);
1812
1813 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001814 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001815
aPiecek570d7ed2021-09-10 07:15:35 +02001816 /* sibling finished */
1817 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001818 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001819 } else {
aPiecek6828a312021-09-17 15:53:18 +02001820 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001821 }
1822
1823 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001824 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001825
1826cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001827 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001828
Radek Krejci1798aae2020-07-14 13:26:06 +02001829 ly_in_free(lybctx->in, 0);
1830 lylyb_ctx_free(lybctx);
1831
Michal Vasko78041d12022-11-29 14:11:07 +01001832 return ret ? -1 : (int)count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001833}