blob: 3ef1c4b8fc35ea6a3a83ebef39d3a7dcbacc9953 [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
Michal Vasko60ea6352020-06-29 13:39:39 +020023#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020024#include "context.h"
25#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010026#include "hash_table.h"
27#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020028#include "in_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020029#include "log.h"
Michal Vasko8f702ee2024-02-20 15:44:24 +010030#include "ly_common.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 {
aPiecek1462ab12024-02-07 09:13:29 +0100934 lyd_insert_node(parent, first_p, node,
935 lybctx->parse_opts & LYD_PARSE_ORDERED ? LYD_INSERT_NODE_LAST : LYD_INSERT_NODE_DEFAULT);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200936 }
aPiecek37c493b2021-09-09 12:52:30 +0200937 while (!parent && (*first_p)->prev->next) {
938 *first_p = (*first_p)->prev;
939 }
940
941 /* rememeber a successfully parsed node */
942 if (parsed) {
943 ly_set_add(parsed, node, 1, NULL);
944 }
945}
946
947/**
948 * @brief Finish parsing the opaq node.
949 *
950 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200951 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200952 * @param[in] flags Node flags to set.
953 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
954 * @param[in,out] node Parsed opaq node to finish.
955 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
956 * @param[out] parsed Set of all successfully parsed nodes.
957 * @return LY_ERR value.
958 */
959static void
960lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
961 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
962{
963 struct lyd_attr *iter;
964
965 /* set flags */
966 (*node)->flags = flags;
967
968 /* add attributes */
969 assert(!(*node)->schema);
970 LY_LIST_FOR(*attr, iter) {
971 iter->parent = (struct lyd_node_opaq *)*node;
972 }
973 ((struct lyd_node_opaq *)*node)->attr = *attr;
974 *attr = NULL;
975
976 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
977 *node = NULL;
978}
979
980/**
981 * @brief Finish parsing the node.
982 *
983 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200984 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200985 * @param[in] flags Node flags to set.
986 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
987 * @param[in,out] node Parsed node to finish.
988 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
989 * @param[out] parsed Set of all successfully parsed nodes.
990 * @return LY_ERR value.
991 */
992static void
993lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
994 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
995{
996 struct lyd_meta *m;
997
998 /* set flags */
999 (*node)->flags = flags;
1000
1001 /* add metadata */
1002 LY_LIST_FOR(*meta, m) {
1003 m->parent = *node;
1004 }
1005 (*node)->meta = *meta;
1006 *meta = NULL;
1007
Michal Vasko135719f2022-08-25 12:18:17 +02001008 /* insert into parent */
aPiecek37c493b2021-09-09 12:52:30 +02001009 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
Michal Vasko135719f2022-08-25 12:18:17 +02001010
Michal Vaskoeba23112022-08-26 08:35:41 +02001011 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1012 /* store for ext instance node validation, if needed */
1013 (void)lyd_validate_node_ext(*node, &lybctx->ext_node);
1014 }
Michal Vasko135719f2022-08-25 12:18:17 +02001015
aPiecek37c493b2021-09-09 12:52:30 +02001016 *node = NULL;
1017}
1018
1019/**
aPiecek6fdac2f2021-09-10 11:21:10 +02001020 * @brief Parse header for non-opaq node.
1021 *
1022 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +01001023 * @param[in] sparent Schema parent node of the metadata.
aPiecek6fdac2f2021-09-10 11:21:10 +02001024 * @param[out] flags Parsed node flags.
1025 * @param[out] meta Parsed metadata of the node.
1026 * @return LY_ERR value.
1027 */
1028static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001029lyb_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 +02001030{
1031 LY_ERR ret;
1032
1033 /* create and read metadata */
Michal Vaskoddd76592022-01-17 13:34:48 +01001034 ret = lyb_parse_metadata(lybctx, sparent, meta);
aPiecek6fdac2f2021-09-10 11:21:10 +02001035 LY_CHECK_RET(ret);
1036
1037 /* read flags */
1038 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1039
1040 return ret;
1041}
1042
1043/**
aPiecek33fc6b02021-09-09 15:45:37 +02001044 * @brief Create term node and fill it with value.
1045 *
1046 * @param[in] lybctx LYB context.
1047 * @param[in] snode Schema of the term node.
1048 * @param[out] node Created term node.
1049 * @return LY_ERR value.
1050 */
1051static LY_ERR
1052lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
1053{
1054 LY_ERR ret;
1055 ly_bool dynamic;
1056 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +02001057 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +02001058
1059 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
1060 LY_CHECK_RET(ret);
1061
1062 dynamic = 1;
1063 /* create node */
1064 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
1065 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
1066 NULL, LYD_HINT_DATA, node);
1067 if (dynamic) {
1068 free(term_value);
1069 }
1070 if (ret) {
1071 lyd_free_tree(*node);
1072 *node = NULL;
1073 }
1074
1075 return ret;
1076}
1077
1078/**
aPiecek0e2e1052021-09-09 15:48:27 +02001079 * @brief Validate inner node, autodelete default values nad create implicit nodes.
1080 *
1081 * @param[in,out] lybctx LYB context.
1082 * @param[in] snode Schema of the inner node.
1083 * @param[in] node Parsed inner node.
1084 * @return LY_ERR value.
1085 */
1086static LY_ERR
1087lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
1088{
1089 LY_ERR ret = LY_SUCCESS;
1090 uint32_t impl_opts;
1091
1092 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1093 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskod027f382023-02-10 09:13:25 +01001094 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, 0, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001095 LY_CHECK_RET(ret);
1096
1097 /* add any missing default children */
1098 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01001099 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 +02001100 &lybctx->ext_node, impl_opts, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001101 LY_CHECK_RET(ret);
1102 }
1103
1104 return ret;
1105}
1106
1107/**
aPiecek821cf732021-09-09 15:37:28 +02001108 * @brief Parse opaq node.
1109 *
1110 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001111 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001112 * @param[in,out] first_p First top-level sibling.
1113 * @param[out] parsed Set of all successfully parsed nodes.
1114 * @return LY_ERR value.
1115 */
1116static LY_ERR
1117lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1118{
1119 LY_ERR ret;
1120 struct lyd_node *node = NULL;
1121 struct lyd_attr *attr = NULL;
1122 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1123 ly_bool dynamic = 0;
1124 LY_VALUE_FORMAT format = 0;
1125 void *val_prefix_data = NULL;
1126 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1127 uint32_t flags;
1128
aPiecek821cf732021-09-09 15:37:28 +02001129 /* parse opaq node attributes */
1130 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1131 LY_CHECK_GOTO(ret, cleanup);
1132
1133 /* read flags */
1134 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1135
1136 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001137 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001138 LY_CHECK_GOTO(ret, cleanup);
1139
1140 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001141 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001142 LY_CHECK_GOTO(ret, cleanup);
1143
1144 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001145 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001146 LY_CHECK_GOTO(ret, cleanup);
1147
1148 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001149 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001150 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1151 dynamic = 1;
1152
1153 /* parse format */
1154 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1155
1156 /* parse value prefixes */
1157 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1158 LY_CHECK_GOTO(ret, cleanup);
1159
aPiecek570d7ed2021-09-10 07:15:35 +02001160 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001161 /* skip children */
1162 ret = lyb_read_start_siblings(lybctx->lybctx);
1163 LY_CHECK_GOTO(ret, cleanup);
1164 lyb_skip_siblings(lybctx->lybctx);
1165 ret = lyb_read_stop_siblings(lybctx->lybctx);
1166 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001167 goto cleanup;
1168 }
1169
aPiecek821cf732021-09-09 15:37:28 +02001170 /* create node */
1171 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 +02001172 value, strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA, &node);
aPiecek821cf732021-09-09 15:37:28 +02001173 LY_CHECK_GOTO(ret, cleanup);
1174
Michal Vaskoa878a892023-08-18 12:22:07 +02001175 assert(node);
Michal Vasko7a266772024-01-23 11:02:38 +01001176 LOG_LOCSET(NULL, node);
Michal Vaskoa878a892023-08-18 12:22:07 +02001177
aPiecek821cf732021-09-09 15:37:28 +02001178 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001179 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1180 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001181
1182 /* register parsed opaq node */
1183 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1184 assert(!attr && !node);
Michal Vasko7a266772024-01-23 11:02:38 +01001185 LOG_LOCBACK(0, 1);
aPiecek821cf732021-09-09 15:37:28 +02001186
1187cleanup:
Michal Vaskoa878a892023-08-18 12:22:07 +02001188 if (node) {
Michal Vasko7a266772024-01-23 11:02:38 +01001189 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +02001190 }
aPiecek821cf732021-09-09 15:37:28 +02001191 free(prefix);
1192 free(module_key);
1193 free(name);
1194 if (dynamic) {
1195 free(value);
1196 }
1197 lyd_free_attr_siblings(ctx, attr);
1198 lyd_free_tree(node);
1199
1200 return ret;
1201}
1202
aPiecek18457d72021-09-09 15:52:20 +02001203/**
1204 * @brief Parse anydata or anyxml node.
1205 *
1206 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001207 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001208 * @param[in] snode Schema of the node to be parsed.
1209 * @param[in,out] first_p First top-level sibling.
1210 * @param[out] parsed Set of all successfully parsed nodes.
1211 * @return LY_ERR value.
1212 */
1213static LY_ERR
1214lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1215 struct lyd_node **first_p, struct ly_set *parsed)
1216{
1217 LY_ERR ret;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001218 struct lyd_node *node = NULL, *tree = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001219 struct lyd_meta *meta = NULL;
1220 LYD_ANYDATA_VALUETYPE value_type;
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001221 struct ly_in *in;
1222 struct lyd_ctx *lydctx = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001223 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001224 uint32_t flags;
1225 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1226
1227 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001228 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001229 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001230
1231 /* parse value type */
1232 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001233 if ((value_type == LYD_ANYDATA_DATATREE) || ((snode->nodetype == LYS_ANYDATA) && (value_type != LYD_ANYDATA_LYB))) {
aPiecek18457d72021-09-09 15:52:20 +02001234 LOGINT(ctx);
1235 ret = LY_EINT;
1236 goto error;
1237 }
1238
1239 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001240 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001241 LY_CHECK_GOTO(ret, error);
1242
1243 if (value_type == LYD_ANYDATA_LYB) {
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001244 /* parse LYB into a data tree */
1245 LY_CHECK_RET(ly_in_new_memory(value, &in));
1246 ret = lyd_parse_lyb(ctx, NULL, NULL, &tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0,
1247 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, NULL, &lydctx);
1248 ly_in_free(in, 0);
1249 if (lydctx) {
1250 lydctx->free(lydctx);
aPiecek18457d72021-09-09 15:52:20 +02001251 }
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001252 LY_CHECK_ERR_GOTO(ret, lyd_free_siblings(tree), error);
1253
1254 /* use the parsed tree as the value */
1255 free(value);
1256 value = (char *)tree;
1257 value_type = LYD_ANYDATA_DATATREE;
aPiecek18457d72021-09-09 15:52:20 +02001258 }
1259
1260 /* create the node */
1261 switch (value_type) {
aPiecek18457d72021-09-09 15:52:20 +02001262 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001263 case LYD_ANYDATA_STRING:
1264 case LYD_ANYDATA_XML:
1265 case LYD_ANYDATA_JSON:
Michal Vasko742a5b12022-02-24 16:07:27 +01001266 /* use the value directly */
1267 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001268 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001269 break;
1270 default:
1271 LOGINT(ctx);
1272 ret = LY_EINT;
1273 goto error;
1274 }
1275
Michal Vaskoa878a892023-08-18 12:22:07 +02001276 assert(node);
Michal Vasko7a266772024-01-23 11:02:38 +01001277 LOG_LOCSET(NULL, node);
Michal Vaskoa878a892023-08-18 12:22:07 +02001278
aPiecek18457d72021-09-09 15:52:20 +02001279 /* register parsed anydata node */
1280 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1281
Michal Vasko7a266772024-01-23 11:02:38 +01001282 LOG_LOCBACK(0, 1);
aPiecek18457d72021-09-09 15:52:20 +02001283 return LY_SUCCESS;
1284
1285error:
1286 free(value);
1287 lyd_free_meta_siblings(meta);
1288 lyd_free_tree(node);
1289 return ret;
1290}
aPiecek821cf732021-09-09 15:37:28 +02001291
1292/**
aPiecek5777f122021-09-10 10:26:23 +02001293 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001294 *
1295 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001296 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1297 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001298 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1299 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001300 * @return LY_ERR value.
1301 */
1302static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001303lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001304 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001305{
1306 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001307 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001308 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001309 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001310
aPiecek307f0772021-09-10 09:09:47 +02001311 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001312 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001313 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001314
aPiecek5777f122021-09-10 10:26:23 +02001315 /* create node */
1316 ret = lyd_create_inner(snode, &node);
1317 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001318
Michal Vaskoa878a892023-08-18 12:22:07 +02001319 assert(node);
Michal Vasko7a266772024-01-23 11:02:38 +01001320 LOG_LOCSET(NULL, node);
Michal Vaskoa878a892023-08-18 12:22:07 +02001321
aPiecek5777f122021-09-10 10:26:23 +02001322 /* process children */
1323 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1324 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001325
aPiecek5777f122021-09-10 10:26:23 +02001326 /* additional procedure for inner node */
1327 ret = lyb_validate_node_inner(lybctx, snode, node);
1328 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001329
aPiecek5777f122021-09-10 10:26:23 +02001330 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1331 /* rememeber the RPC/action/notification */
1332 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001333 }
1334
aPiecek307f0772021-09-10 09:09:47 +02001335 /* register parsed node */
1336 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1337
Michal Vasko7a266772024-01-23 11:02:38 +01001338 LOG_LOCBACK(0, 1);
aPiecek307f0772021-09-10 09:09:47 +02001339 return LY_SUCCESS;
1340
1341error:
Michal Vaskoa878a892023-08-18 12:22:07 +02001342 if (node) {
Michal Vasko7a266772024-01-23 11:02:38 +01001343 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +02001344 }
Michal Vasko3a41dff2020-07-15 14:30:28 +02001345 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001346 lyd_free_tree(node);
1347 return ret;
1348}
1349
1350/**
aPiecek8a555d72021-09-10 10:15:26 +02001351 * @brief Parse leaf node.
1352 *
1353 * @param[in] lybctx LYB context.
1354 * @param[in] parent Data parent of the sibling.
1355 * @param[in] snode Schema of the node to be parsed.
1356 * @param[in,out] first_p First top-level sibling.
1357 * @param[out] parsed Set of all successfully parsed nodes.
1358 * @return LY_ERR value.
1359 */
1360static LY_ERR
1361lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1362 struct lyd_node **first_p, struct ly_set *parsed)
1363{
1364 LY_ERR ret;
1365 struct lyd_node *node = NULL;
1366 struct lyd_meta *meta = NULL;
1367 uint32_t flags;
1368
1369 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001370 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001371 LY_CHECK_GOTO(ret, error);
1372
1373 /* read value of term node and create it */
1374 ret = lyb_create_term(lybctx, snode, &node);
1375 LY_CHECK_GOTO(ret, error);
1376
Michal Vaskoa878a892023-08-18 12:22:07 +02001377 assert(node);
Michal Vasko7a266772024-01-23 11:02:38 +01001378 LOG_LOCSET(NULL, node);
Michal Vaskoa878a892023-08-18 12:22:07 +02001379
aPiecek8a555d72021-09-10 10:15:26 +02001380 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1381
Michal Vasko7a266772024-01-23 11:02:38 +01001382 LOG_LOCBACK(0, 1);
aPiecek8a555d72021-09-10 10:15:26 +02001383 return LY_SUCCESS;
1384
1385error:
1386 lyd_free_meta_siblings(meta);
1387 lyd_free_tree(node);
1388 return ret;
1389}
1390
1391/**
aPiecek5777f122021-09-10 10:26:23 +02001392 * @brief Parse all leaflist nodes which belong to same schema.
1393 *
1394 * @param[in] lybctx LYB context.
1395 * @param[in] parent Data parent of the sibling.
1396 * @param[in] snode Schema of the nodes to be parsed.
1397 * @param[in,out] first_p First top-level sibling.
1398 * @param[out] parsed Set of all successfully parsed nodes.
1399 * @return LY_ERR value.
1400 */
1401static LY_ERR
1402lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1403 struct lyd_node **first_p, struct ly_set *parsed)
1404{
1405 LY_ERR ret;
1406
1407 /* register a new sibling */
1408 ret = lyb_read_start_siblings(lybctx->lybctx);
1409 LY_CHECK_RET(ret);
1410
1411 /* process all siblings */
1412 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1413 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1414 LY_CHECK_RET(ret);
1415 }
1416
1417 /* end the sibling */
1418 ret = lyb_read_stop_siblings(lybctx->lybctx);
1419 LY_CHECK_RET(ret);
1420
1421 return ret;
1422}
1423
1424/**
aPiecek77d3e962021-09-10 10:33:59 +02001425 * @brief Parse all list nodes which belong to same schema.
1426 *
1427 * @param[in] lybctx LYB context.
1428 * @param[in] parent Data parent of the sibling.
1429 * @param[in] snode Schema of the nodes to be parsed.
1430 * @param[in,out] first_p First top-level sibling.
1431 * @param[out] parsed Set of all successfully parsed nodes.
1432 * @return LY_ERR value.
1433 */
1434static LY_ERR
1435lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1436 struct lyd_node **first_p, struct ly_set *parsed)
1437{
1438 LY_ERR ret;
1439 struct lyd_node *node = NULL;
1440 struct lyd_meta *meta = NULL;
1441 uint32_t flags;
Michal Vaskoa878a892023-08-18 12:22:07 +02001442 ly_bool log_node = 0;
aPiecek77d3e962021-09-10 10:33:59 +02001443
1444 /* register a new sibling */
1445 ret = lyb_read_start_siblings(lybctx->lybctx);
1446 LY_CHECK_RET(ret);
1447
1448 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1449 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001450 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001451 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001452
1453 /* create list node */
1454 ret = lyd_create_inner(snode, &node);
1455 LY_CHECK_GOTO(ret, error);
1456
Michal Vaskoa878a892023-08-18 12:22:07 +02001457 assert(node);
Michal Vasko7a266772024-01-23 11:02:38 +01001458 LOG_LOCSET(NULL, node);
Michal Vaskoa878a892023-08-18 12:22:07 +02001459 log_node = 1;
1460
aPiecek77d3e962021-09-10 10:33:59 +02001461 /* process children */
1462 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1463 LY_CHECK_GOTO(ret, error);
1464
1465 /* additional procedure for inner node */
1466 ret = lyb_validate_node_inner(lybctx, snode, node);
1467 LY_CHECK_GOTO(ret, error);
1468
1469 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1470 /* rememeber the RPC/action/notification */
1471 lybctx->op_node = node;
1472 }
1473
1474 /* register parsed list node */
1475 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vaskoa878a892023-08-18 12:22:07 +02001476
Michal Vasko7a266772024-01-23 11:02:38 +01001477 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +02001478 log_node = 0;
aPiecek77d3e962021-09-10 10:33:59 +02001479 }
1480
1481 /* end the sibling */
1482 ret = lyb_read_stop_siblings(lybctx->lybctx);
1483 LY_CHECK_RET(ret);
1484
1485 return LY_SUCCESS;
1486
1487error:
Michal Vaskoa878a892023-08-18 12:22:07 +02001488 if (log_node) {
Michal Vasko7a266772024-01-23 11:02:38 +01001489 LOG_LOCBACK(0, 1);
Michal Vaskoa878a892023-08-18 12:22:07 +02001490 }
aPiecek77d3e962021-09-10 10:33:59 +02001491 lyd_free_meta_siblings(meta);
1492 lyd_free_tree(node);
1493 return ret;
1494}
1495
1496/**
Michal Vasko9883a3e2022-03-31 12:16:00 +02001497 * @brief Parse a node.
aPiecek17737b52021-09-21 12:29:52 +02001498 *
aPiecek17737b52021-09-21 12:29:52 +02001499 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +02001500 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1501 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1502 * @param[in,out] parsed Set of all successfully parsed nodes to add to.
aPiecek17737b52021-09-21 12:29:52 +02001503 * @return LY_ERR value.
1504 */
1505static LY_ERR
1506lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1507 struct ly_set *parsed)
1508{
1509 LY_ERR ret;
1510 const struct lysc_node *snode;
1511 const struct lys_module *mod;
Michal Vasko9883a3e2022-03-31 12:16:00 +02001512 enum lylyb_node_type lyb_type;
1513 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek17737b52021-09-21 12:29:52 +02001514
Michal Vasko9883a3e2022-03-31 12:16:00 +02001515 /* read node type */
1516 lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx);
1517
1518 switch (lyb_type) {
1519 case LYB_NODE_TOP:
1520 /* top-level, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001521 LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, 0, &mod), cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001522
1523 /* read hash, find the schema node starting from mod */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001524 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup);
1525 break;
1526 case LYB_NODE_CHILD:
1527 case LYB_NODE_OPAQ:
1528 /* read hash, find the schema node starting from parent schema, if any */
Michal Vaskoa878a892023-08-18 12:22:07 +02001529 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, lyd_parser_node_schema(parent), NULL, &snode), cleanup);
Michal Vasko9883a3e2022-03-31 12:16:00 +02001530 break;
1531 case LYB_NODE_EXT:
1532 /* ext, read module name */
Michal Vasko78041d12022-11-29 14:11:07 +01001533 LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev, NULL), cleanup);
Michal Vasko9883a3e2022-03-31 12:16:00 +02001534
1535 /* read schema node name, find the nexted ext schema node */
1536 LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup);
1537 break;
aPiecek17737b52021-09-21 12:29:52 +02001538 }
aPiecek17737b52021-09-21 12:29:52 +02001539
1540 if (!snode) {
1541 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1542 } else if (snode->nodetype & LYS_LEAFLIST) {
1543 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1544 } else if (snode->nodetype == LYS_LIST) {
1545 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1546 } else if (snode->nodetype & LYD_NODE_ANY) {
1547 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1548 } else if (snode->nodetype & LYD_NODE_INNER) {
1549 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1550 } else {
1551 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1552 }
Michal Vasko9883a3e2022-03-31 12:16:00 +02001553 LY_CHECK_GOTO(ret, cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001554
Michal Vasko9883a3e2022-03-31 12:16:00 +02001555cleanup:
1556 free(mod_name);
aPiecek17737b52021-09-21 12:29:52 +02001557 return ret;
1558}
1559
1560/**
aPiecek570d7ed2021-09-10 07:15:35 +02001561 * @brief Parse siblings (@ref lyb_print_siblings()).
1562 *
1563 * @param[in] lybctx LYB context.
1564 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1565 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1566 * @param[out] parsed Set of all successfully parsed nodes.
1567 * @return LY_ERR value.
1568 */
1569static LY_ERR
1570lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1571 struct ly_set *parsed)
1572{
aPiecek570d7ed2021-09-10 07:15:35 +02001573 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001574
1575 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1576
1577 /* register a new siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001578 LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001579
1580 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
Michal Vasko9883a3e2022-03-31 12:16:00 +02001581 LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed));
aPiecek570d7ed2021-09-10 07:15:35 +02001582
1583 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1584 break;
1585 }
1586 }
1587
1588 /* end the siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001589 LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001590
Michal Vasko9883a3e2022-03-31 12:16:00 +02001591 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +02001592}
1593
1594/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001595 * @brief Parse used YANG data models.
1596 *
1597 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001598 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001599 * @return LY_ERR value.
1600 */
1601static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001602lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001603{
1604 LY_ERR ret;
1605 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001606 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001607
1608 /* read model count */
1609 lyb_read_number(&count, sizeof count, 2, lybctx);
1610
1611 if (count) {
1612 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1613
1614 /* read modules */
1615 for (u = 0; u < count; ++u) {
Michal Vasko78041d12022-11-29 14:11:07 +01001616 ret = lyb_parse_model(lybctx, parse_options, 1, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001617 LY_CHECK_RET(ret);
1618 LY_ARRAY_INCREMENT(lybctx->models);
1619 }
1620 }
1621
1622 return LY_SUCCESS;
1623}
1624
1625/**
1626 * @brief Parse LYB magic number.
1627 *
1628 * @param[in] lybctx LYB context.
1629 * @return LY_ERR value.
1630 */
1631static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001632lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001633{
1634 char magic_byte = 0;
1635
1636 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1637 if (magic_byte != 'l') {
1638 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1639 return LY_EINVAL;
1640 }
1641
1642 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1643 if (magic_byte != 'y') {
1644 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1645 return LY_EINVAL;
1646 }
1647
1648 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1649 if (magic_byte != 'b') {
1650 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1651 return LY_EINVAL;
1652 }
1653
1654 return LY_SUCCESS;
1655}
1656
1657/**
1658 * @brief Parse LYB header.
1659 *
1660 * @param[in] lybctx LYB context.
1661 * @return LY_ERR value.
1662 */
1663static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001664lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001665{
1666 uint8_t byte = 0;
1667
1668 /* version, future flags */
1669 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1670
1671 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1672 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001673 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001674 return LY_EINVAL;
1675 }
1676
1677 return LY_SUCCESS;
1678}
1679
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001680LY_ERR
1681lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
Michal Vasko02ed9d82021-07-15 14:58:04 +02001682 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 +01001683 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001684{
Michal Vaskoe0665742021-02-11 11:08:44 +01001685 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001686 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001687
Michal Vaskoe0665742021-02-11 11:08:44 +01001688 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1689 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001690
Michal Vaskoe3ed7dc2022-11-30 11:39:44 +01001691 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1692
1693 if (subtree_sibling) {
1694 *subtree_sibling = 0;
1695 }
1696
Radek Krejci1798aae2020-07-14 13:26:06 +02001697 lybctx = calloc(1, sizeof *lybctx);
1698 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1699 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001700 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001701
Radek Krejci1798aae2020-07-14 13:26:06 +02001702 lybctx->lybctx->in = in;
1703 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001704 lybctx->parse_opts = parse_opts;
1705 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001706 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001707 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001708 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001709
1710 /* find the operation node if it exists already */
1711 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1712
Michal Vasko60ea6352020-06-29 13:39:39 +02001713 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001714 rc = lyb_parse_magic_number(lybctx->lybctx);
1715 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001716
1717 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001718 rc = lyb_parse_header(lybctx->lybctx);
1719 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001720
1721 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001722 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1723 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001724
aPiecek570d7ed2021-09-10 07:15:35 +02001725 /* read sibling(s) */
1726 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1727 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001728
1729 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1730 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1731 rc = LY_EVALID;
1732 goto cleanup;
1733 }
1734 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1735 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1736 rc = LY_EVALID;
1737 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001738 }
1739
1740 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001741 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001742
Michal Vasko60ea6352020-06-29 13:39:39 +02001743cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001744 /* there should be no unres stored if validation should be skipped */
1745 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1746 !lybctx->node_when.count));
1747
1748 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001749 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001750 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001751 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001752 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001753 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001754}
1755
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001756LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001757lyd_lyb_data_length(const char *data)
1758{
1759 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001760 struct lylyb_ctx *lybctx;
Michal Vasko78041d12022-11-29 14:11:07 +01001761 uint32_t count, feat_count, len = 0, i, j;
Michal Vasko60ea6352020-06-29 13:39:39 +02001762 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001763 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001764
1765 if (!data) {
1766 return -1;
1767 }
1768
Radek Krejci1798aae2020-07-14 13:26:06 +02001769 lybctx = calloc(1, sizeof *lybctx);
1770 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1771 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001772 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001773
1774 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001775 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001776 LY_CHECK_GOTO(ret, cleanup);
1777
1778 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001779 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001780 LY_CHECK_GOTO(ret, cleanup);
1781
1782 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001783 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001784
1785 /* read all models */
1786 for (i = 0; i < count; ++i) {
1787 /* module name length */
Radek Krejci1798aae2020-07-14 13:26:06 +02001788 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001789
1790 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001791 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001792
1793 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001794 lyb_read(buf, 2, lybctx);
Michal Vasko78041d12022-11-29 14:11:07 +01001795
1796 /* enabled feature count */
1797 lyb_read_number(&feat_count, sizeof feat_count, 2, lybctx);
1798
1799 /* enabled features */
1800 for (j = 0; j < feat_count; ++j) {
1801 /* feature name length */
1802 lyb_read_number(&len, sizeof len, 2, lybctx);
1803
1804 /* feature name */
1805 lyb_read(buf, len, lybctx);
1806 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001807 }
1808
aPiecek6828a312021-09-17 15:53:18 +02001809 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001810 /* register a new sibling */
1811 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001812 LY_CHECK_GOTO(ret, cleanup);
1813
1814 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001815 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001816
aPiecek570d7ed2021-09-10 07:15:35 +02001817 /* sibling finished */
1818 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001819 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001820 } else {
aPiecek6828a312021-09-17 15:53:18 +02001821 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001822 }
1823
1824 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001825 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001826
1827cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001828 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001829
Radek Krejci1798aae2020-07-14 13:26:06 +02001830 ly_in_free(lybctx->in, 0);
1831 lylyb_ctx_free(lybctx);
1832
Michal Vasko78041d12022-11-29 14:11:07 +01001833 return ret ? -1 : (int)count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001834}