blob: 43eafc17c278a6a2b31e4cd26ff4e04b16cd9a3d [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 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "lyb.h"
16
17#include <assert.h>
Radek 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"
Radek Krejci77114102021-03-10 15:21:57 +010033#include "set.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020034#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020036#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010037#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020038#include "tree_schema.h"
39#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010040#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020041
Michal Vasko02ed9d82021-07-15 14:58:04 +020042static LY_ERR _lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
43 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
44 struct ly_set *parsed, struct lyd_ctx **lydctx_p);
45
aPiecek570d7ed2021-09-10 07:15:35 +020046static LY_ERR lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed);
aPiecek821cf732021-09-09 15:37:28 +020047
Radek Krejci1798aae2020-07-14 13:26:06 +020048void
49lylyb_ctx_free(struct lylyb_ctx *ctx)
50{
51 LY_ARRAY_COUNT_TYPE u;
52
aPiecek570d7ed2021-09-10 07:15:35 +020053 LY_ARRAY_FREE(ctx->siblings);
Radek Krejci1798aae2020-07-14 13:26:06 +020054 LY_ARRAY_FREE(ctx->models);
55
56 LY_ARRAY_FOR(ctx->sib_hts, u) {
57 lyht_free(ctx->sib_hts[u].ht);
58 }
59 LY_ARRAY_FREE(ctx->sib_hts);
60
61 free(ctx);
62}
63
64void
65lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
66{
67 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
68
69 lyd_ctx_free(lydctx);
70 lylyb_ctx_free(ctx->lybctx);
71 free(ctx);
72}
73
Michal Vasko60ea6352020-06-29 13:39:39 +020074/**
aPiecek6828a312021-09-17 15:53:18 +020075 * @brief Read metadata about siblings.
76 *
77 * @param[out] sib Structure in which the metadata will be stored.
78 * @param[in] lybctx LYB context.
79 */
80static void
81lyb_read_sibling_meta(struct lyd_lyb_sibling *sib, struct lylyb_ctx *lybctx)
82{
83 uint8_t meta_buf[LYB_META_BYTES];
84 uint64_t num = 0;
85
86 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
87
88 memcpy(&num, meta_buf, LYB_SIZE_BYTES);
89 sib->written = le64toh(num);
90 memcpy(&num, meta_buf + LYB_SIZE_BYTES, LYB_INCHUNK_BYTES);
91 sib->inner_chunks = le64toh(num);
92
93 /* remember whether there is a following chunk or not */
94 sib->position = (sib->written == LYB_SIZE_MAX ? 1 : 0);
95}
96
97/**
Michal Vasko60ea6352020-06-29 13:39:39 +020098 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
99 *
100 * @param[in] buf Destination buffer.
101 * @param[in] count Number of bytes to read.
102 * @param[in] lybctx LYB context.
103 */
104static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200105lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200106{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200107 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200108 struct lyd_lyb_sibling *empty;
Michal Vasko60ea6352020-06-29 13:39:39 +0200109 size_t to_read;
Michal Vasko60ea6352020-06-29 13:39:39 +0200110
111 assert(lybctx);
112
113 while (1) {
114 /* check for fully-read (empty) data chunks */
115 to_read = count;
116 empty = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200117 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200118 /* we want the innermost chunks resolved first, so replace previous empty chunks,
119 * also ignore chunks that are completely finished, there is nothing for us to do */
aPiecek570d7ed2021-09-10 07:15:35 +0200120 if ((lybctx->siblings[u].written <= to_read) && lybctx->siblings[u].position) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200121 /* empty chunk, do not read more */
aPiecek570d7ed2021-09-10 07:15:35 +0200122 to_read = lybctx->siblings[u].written;
123 empty = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200124 }
125 }
126
127 if (!empty && !count) {
128 break;
129 }
130
131 /* we are actually reading some data, not just finishing another chunk */
132 if (to_read) {
133 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200134 ly_in_read(lybctx->in, buf, to_read);
135 } else {
136 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200137 }
138
aPiecek570d7ed2021-09-10 07:15:35 +0200139 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200140 /* decrease all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200141 lybctx->siblings[u].written -= to_read;
142 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200143 }
144 /* decrease count/buf */
145 count -= to_read;
146 if (buf) {
147 buf += to_read;
148 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200149 }
150
151 if (empty) {
152 /* read the next chunk meta information */
aPiecek6828a312021-09-17 15:53:18 +0200153 lyb_read_sibling_meta(empty, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200154 }
155 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200156}
157
158/**
159 * @brief Read a number.
160 *
161 * @param[in] num Destination buffer.
162 * @param[in] num_size Size of @p num.
163 * @param[in] bytes Number of bytes to read.
164 * @param[in] lybctx LYB context.
165 */
166static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200167lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200168{
169 uint64_t buf = 0;
170
171 lyb_read((uint8_t *)&buf, bytes, lybctx);
172
173 /* correct byte order */
174 buf = le64toh(buf);
175
176 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100177 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200178 *((uint8_t *)num) = buf;
179 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100180 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200181 *((uint16_t *)num) = buf;
182 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100183 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200184 *((uint32_t *)num) = buf;
185 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100186 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200187 *((uint64_t *)num) = buf;
188 break;
189 default:
190 LOGINT(lybctx->ctx);
191 }
192}
193
194/**
195 * @brief Read a string.
196 *
197 * @param[in] str Destination buffer, is allocated.
aPieceke99345d2021-09-30 12:49:59 +0200198 * @param[in] len_size Number of bytes on which the length of the string is written.
Michal Vasko60ea6352020-06-29 13:39:39 +0200199 * @param[in] lybctx LYB context.
200 * @return LY_ERR value.
201 */
202static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200203lyb_read_string(char **str, uint8_t len_size, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200204{
aPieceke99345d2021-09-30 12:49:59 +0200205 uint64_t len = 0;
206
207 assert((len_size == 1) || (len_size == 2) || (len_size == 4) || (len_size == 8));
Michal Vasko60ea6352020-06-29 13:39:39 +0200208
209 *str = NULL;
210
aPieceke99345d2021-09-30 12:49:59 +0200211 lyb_read_number(&len, sizeof len, len_size, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200212
213 *str = malloc((len + 1) * sizeof **str);
214 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
215
216 lyb_read((uint8_t *)*str, len, lybctx);
217
Michal Vaskocebbae52021-05-31 11:11:36 +0200218 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200219 return LY_SUCCESS;
220}
221
222/**
aPiecekd5796f92021-09-30 13:39:37 +0200223 * @brief Skip string.
224 *
225 * @param[in] len_size Number of bytes on which the length of the string is written.
226 * @param[in] lybctx LYB context.
227 */
228static void
229lyb_skip_string(uint8_t len_size, struct lylyb_ctx *lybctx)
230{
231 size_t len = 0;
232
233 lyb_read_number(&len, sizeof len, len_size, lybctx);
234
235 lyb_read(NULL, len, lybctx);
236}
237
238/**
aPiecek91eec232021-09-09 15:42:37 +0200239 * @brief Read value of term node.
aPiecekea304e32021-08-18 09:13:47 +0200240 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200241 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200242 * @param[out] term_value Set to term node value in dynamically
243 * allocated memory. The caller must release it.
244 * @param[out] term_value_len Value length in bytes. The zero byte is
245 * always included and is not counted.
246 * @param[in,out] lybctx LYB context.
247 * @return LY_ERR value.
248 */
249static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200250lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint64_t *term_value_len,
aPiecek91eec232021-09-09 15:42:37 +0200251 struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200252{
253 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200254 int32_t lyb_data_len;
255 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200256
aPiecekaa5b70a2021-08-30 08:33:25 +0200257 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200258
aPiecekaa5b70a2021-08-30 08:33:25 +0200259 /* Find out the size from @ref howtoDataLYB. */
260 if (term->type->basetype == LY_TYPE_LEAFREF) {
261 /* Leafref itself is ignored, the target is loaded directly. */
262 type_lf = (struct lysc_type_leafref *)term->type;
263 lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
264 } else {
265 lyb_data_len = term->type->plugin->lyb_data_len;
266 }
267
268 if (lyb_data_len < 0) {
269 /* Parse value size. */
270 lyb_read_number(term_value_len, sizeof *term_value_len,
271 sizeof *term_value_len, lybctx);
272 } else {
273 /* Data size is fixed. */
274 *term_value_len = lyb_data_len;
275 }
aPiecekea304e32021-08-18 09:13:47 +0200276
277 /* Allocate memory. */
278 allocated_size = *term_value_len + 1;
279 *term_value = malloc(allocated_size * sizeof **term_value);
280 LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM);
281
282 if (*term_value_len > 0) {
283 /* Parse value. */
284 lyb_read(*term_value, *term_value_len, lybctx);
285 }
286
287 /* Add extra zero byte regardless of whether it is string or not. */
288 (*term_value)[allocated_size - 1] = 0;
289
290 return LY_SUCCESS;
291}
292
293/**
aPiecek570d7ed2021-09-10 07:15:35 +0200294 * @brief Stop the current "siblings" - change LYB context state.
Michal Vasko60ea6352020-06-29 13:39:39 +0200295 *
296 * @param[in] lybctx LYB context.
297 * @return LY_ERR value.
298 */
299static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200300lyb_read_stop_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200301{
aPiecek570d7ed2021-09-10 07:15:35 +0200302 if (LYB_LAST_SIBLING(lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200303 LOGINT_RET(lybctx->ctx);
304 }
305
aPiecek570d7ed2021-09-10 07:15:35 +0200306 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200307 return LY_SUCCESS;
308}
309
310/**
aPiecek570d7ed2021-09-10 07:15:35 +0200311 * @brief Start a new "siblings" - change LYB context state but also read the expected metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200312 *
313 * @param[in] lybctx LYB context.
314 * @return LY_ERR value.
315 */
316static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200317lyb_read_start_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200318{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200319 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200320
aPiecek570d7ed2021-09-10 07:15:35 +0200321 u = LY_ARRAY_COUNT(lybctx->siblings);
322 if (u == lybctx->sibling_size) {
323 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
324 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200325 }
326
aPiecek570d7ed2021-09-10 07:15:35 +0200327 LY_ARRAY_INCREMENT(lybctx->siblings);
aPiecek6828a312021-09-17 15:53:18 +0200328 lyb_read_sibling_meta(&LYB_LAST_SIBLING(lybctx), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200329
Michal Vasko60ea6352020-06-29 13:39:39 +0200330 return LY_SUCCESS;
331}
332
333/**
334 * @brief Parse YANG model info.
335 *
336 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +0200337 * @param[in] parse_options Flag with options for parsing.
aPiecek339bdc32021-09-10 08:42:36 +0200338 * @param[out] model Parsed module.
Michal Vasko60ea6352020-06-29 13:39:39 +0200339 * @return LY_ERR value.
340 */
341static LY_ERR
aPiecek339bdc32021-09-10 08:42:36 +0200342lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **model)
Michal Vasko60ea6352020-06-29 13:39:39 +0200343{
344 LY_ERR ret = LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +0200345 const struct lys_module *mod = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100346 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek570d7ed2021-09-10 07:15:35 +0200347 uint16_t rev, length;
Michal Vasko60ea6352020-06-29 13:39:39 +0200348
aPiecek570d7ed2021-09-10 07:15:35 +0200349 lyb_read_number(&length, 2, 2, lybctx);
aPiecek339bdc32021-09-10 08:42:36 +0200350
aPiecek570d7ed2021-09-10 07:15:35 +0200351 if (length) {
352 mod_name = malloc((length + 1) * sizeof *mod_name);
353 LY_CHECK_ERR_RET(!mod_name, LOGMEM(lybctx->ctx), LY_EMEM);
354 lyb_read(((uint8_t *)mod_name), length, lybctx);
355 mod_name[length] = '\0';
356 } else {
357 goto cleanup;
358 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200359
360 /* revision */
361 lyb_read_number(&rev, sizeof rev, 2, lybctx);
362
Michal Vasko60ea6352020-06-29 13:39:39 +0200363 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100364 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
365 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
aPiecek339bdc32021-09-10 08:42:36 +0200366 mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
367 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !mod) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200368 /* try to use an updated module */
aPiecek339bdc32021-09-10 08:42:36 +0200369 mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
370 if (mod && (!mod->revision || (strcmp(mod->revision, mod_rev) < 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200371 /* not an implemented module in a newer revision */
aPiecek339bdc32021-09-10 08:42:36 +0200372 mod = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200373 }
374 }
375 } else {
aPiecek339bdc32021-09-10 08:42:36 +0200376 mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200377 }
378 /* TODO data_clb supported?
379 if (lybctx->ctx->data_clb) {
380 if (!*mod) {
381 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
382 } else if (!(*mod)->implemented) {
383 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
384 }
385 }*/
386
aPiecek339bdc32021-09-10 08:42:36 +0200387 if (!mod || !mod->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200388 if (parse_options & LYD_PARSE_STRICT) {
aPiecek339bdc32021-09-10 08:42:36 +0200389 if (!mod) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200390 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200391 mod_name, rev ? "@" : "", rev ? mod_rev : "");
aPiecek339bdc32021-09-10 08:42:36 +0200392 } else if (!mod->implemented) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200393 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200394 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200395 }
396 ret = LY_EINVAL;
397 goto cleanup;
398 }
399
400 }
401
aPiecek339bdc32021-09-10 08:42:36 +0200402 if (mod) {
Michal Vasko85d9edc2021-04-22 09:15:05 +0200403 /* fill cached hashes, if not already */
aPiecek339bdc32021-09-10 08:42:36 +0200404 lyb_cache_module_hash(mod);
Michal Vasko85d9edc2021-04-22 09:15:05 +0200405 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200406
Michal Vasko60ea6352020-06-29 13:39:39 +0200407cleanup:
aPiecek570d7ed2021-09-10 07:15:35 +0200408 *model = mod;
Michal Vasko60ea6352020-06-29 13:39:39 +0200409 free(mod_name);
410 return ret;
411}
412
413/**
414 * @brief Parse YANG node metadata.
415 *
416 * @param[in] lybctx LYB context.
Michal Vaskoaac267d2022-01-17 13:34:48 +0100417 * @param[in] sparent Schema parent node of the metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 * @param[out] meta Parsed metadata.
419 * @return LY_ERR value.
420 */
421static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +0100422lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200423{
424 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200425 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200426 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200427 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200428 const struct lys_module *mod;
429
430 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200431 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200432
433 /* read attributes */
434 for (i = 0; i < count; ++i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200435 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100436 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200437 LY_CHECK_GOTO(ret, cleanup);
438
439 if (!mod) {
aPiecekd5796f92021-09-30 13:39:37 +0200440 /* skip meta name */
441 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
442
443 /* skip meta value */
444 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
445 continue;
Michal Vasko60ea6352020-06-29 13:39:39 +0200446 }
447
448 /* meta name */
aPieceke99345d2021-09-30 12:49:59 +0200449 ret = lyb_read_string(&meta_name, sizeof(uint16_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200450 LY_CHECK_GOTO(ret, cleanup);
451
452 /* meta value */
aPieceke99345d2021-09-30 12:49:59 +0200453 ret = lyb_read_string(&meta_value, sizeof(uint64_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200454 LY_CHECK_GOTO(ret, cleanup);
455 dynamic = 1;
456
457 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200458 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Michal Vaskoaac267d2022-01-17 13:34:48 +0100459 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, sparent);
Michal Vasko60ea6352020-06-29 13:39:39 +0200460
461 /* free strings */
462 free(meta_name);
463 meta_name = NULL;
464 if (dynamic) {
465 free(meta_value);
466 dynamic = 0;
467 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200468
Radek Krejci1798aae2020-07-14 13:26:06 +0200469 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200470 }
471
472cleanup:
473 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200474 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200475 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200476 *meta = NULL;
477 }
478 return ret;
479}
480
481/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100482 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200483 *
484 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100485 * @param[in] format Prefix data format.
486 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200487 * @return LY_ERR value.
488 */
489static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200490lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200491{
492 LY_ERR ret = LY_SUCCESS;
493 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100494 struct ly_set *set = NULL;
495 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200496
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100497 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200498 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100499 /* read count */
500 lyb_read(&count, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200501
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100502 /* read all NS elements */
503 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200504
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100505 for (i = 0; i < count; ++i) {
506 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200507
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100508 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200509 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, sizeof(uint16_t), lybctx), cleanup);
Michal Vaskod87382e2022-02-28 13:58:34 +0100510 if (!strlen(ns->prefix)) {
511 free(ns->prefix);
512 ns->prefix = NULL;
513 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100514
515 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200516 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, sizeof(uint16_t), lybctx), cleanup);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100517
518 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
519 ns = NULL;
520 }
521
522 *prefix_data = set;
523 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200524 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200525 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100526 /* nothing stored */
527 break;
528 default:
529 LOGINT(lybctx->ctx);
530 ret = LY_EINT;
531 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200532 }
533
534cleanup:
535 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100536 ly_free_prefix_data(format, set);
537 if (ns) {
538 free(ns->prefix);
539 free(ns->uri);
540 free(ns);
541 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200542 }
543 return ret;
544}
545
546/**
547 * @brief Parse opaque attributes.
548 *
549 * @param[in] lybctx LYB context.
550 * @param[out] attr Parsed attributes.
551 * @return LY_ERR value.
552 */
553static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200554lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200555{
556 LY_ERR ret = LY_SUCCESS;
557 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200558 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200559 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200560 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200561 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100562 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200563
564 /* read count */
565 lyb_read(&count, 1, lybctx);
566
567 /* read attributes */
568 for (i = 0; i < count; ++i) {
Michal Vasko0fdcd242020-11-11 19:12:30 +0100569 /* prefix, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200570 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200571 LY_CHECK_GOTO(ret, cleanup);
572 if (!prefix[0]) {
573 free(prefix);
574 prefix = NULL;
575 }
576
577 /* namespace, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200578 ret = lyb_read_string(&module_name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200579 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200580 if (!module_name[0]) {
581 free(module_name);
582 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200583 }
584
585 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200586 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200587 LY_CHECK_GOTO(ret, cleanup);
588
Michal Vasko60ea6352020-06-29 13:39:39 +0200589 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200590 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200591
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100592 /* value prefixes */
593 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
594 LY_CHECK_GOTO(ret, cleanup);
595
Michal Vasko60ea6352020-06-29 13:39:39 +0200596 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200597 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100598 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200599 dynamic = 1;
600
601 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100602 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200603 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200604 LY_CHECK_GOTO(ret, cleanup);
605
606 free(prefix);
607 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200608 free(module_name);
609 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200610 free(name);
611 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200612 assert(!dynamic);
613 value = NULL;
614
615 if (!*attr) {
616 *attr = attr2;
617 }
618
Michal Vasko60ea6352020-06-29 13:39:39 +0200619 LY_CHECK_GOTO(ret, cleanup);
620 }
621
622cleanup:
623 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200624 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200625 free(name);
626 if (dynamic) {
627 free(value);
628 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200629 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200630 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200631 *attr = NULL;
632 }
633 return ret;
634}
635
636/**
aPiecek619350d2021-09-09 16:06:59 +0200637 * @brief Fill @p hash with hash values.
638 *
639 * @param[in] lybctx LYB context.
640 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
641 * @param[out] hash_count Number of hashes in @p hash.
642 * @return LY_ERR value.
643 */
644static LY_ERR
645lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
646{
647 uint8_t i = 0, j;
648
649 /* read the first hash */
650 lyb_read(&hash[0], sizeof *hash, lybctx);
651
652 if (!hash[0]) {
653 *hash_count = i + 1;
654 return LY_SUCCESS;
655 }
656
657 /* based on the first hash read all the other ones, if any */
658 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
659 if (i > LYB_HASH_BITS) {
660 LOGINT_RET(lybctx->ctx);
661 }
662 }
663
664 /* move the first hash on its accurate position */
665 hash[i] = hash[0];
666
667 /* read the rest of hashes */
668 for (j = i; j; --j) {
669 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
670
671 /* correct collision ID */
672 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
673 /* preceded with zeros */
674 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
675 }
676
677 *hash_count = i + 1;
678
679 return LY_SUCCESS;
680}
681
682/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200683 * @brief Check whether a schema node matches a hash(es).
684 *
685 * @param[in] sibling Schema node to check.
686 * @param[in] hash Hash array to check.
687 * @param[in] hash_count Number of hashes in @p hash.
688 * @return non-zero if matches,
689 * @return 0 if not.
690 */
691static int
692lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
693{
694 LYB_HASH sibling_hash;
695 uint8_t i;
696
697 /* compare all the hashes starting from collision ID 0 */
698 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200699 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200700 if (sibling_hash != hash[i]) {
701 return 0;
702 }
703 }
704
705 return 1;
706}
707
708/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200709 * @brief Parse schema node hash.
710 *
711 * @param[in] lybctx LYB context.
712 * @param[in] sparent Schema parent, must be set if @p mod is not.
713 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
714 * @param[out] snode Parsed found schema node, may be NULL if opaque.
715 * @return LY_ERR value.
716 */
717static LY_ERR
718lyb_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 +0200719 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200720{
721 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200722 const struct lysc_node *sibling;
723 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200724 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200725 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200726
aPiecek570d7ed2021-09-10 07:15:35 +0200727 *snode = NULL;
728
aPiecek619350d2021-09-09 16:06:59 +0200729 ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
730 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200731
732 if (!hash[0]) {
733 /* opaque node */
734 return LY_SUCCESS;
735 }
736
aPiecek619350d2021-09-09 16:06:59 +0200737 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200738
739 /* find our node with matching hashes */
740 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100741 while (1) {
742 if (!sparent && lybctx->ext) {
743 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
744 } else {
745 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
746 }
747 if (!sibling) {
748 break;
749 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200750 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200751 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
aPiecek619350d2021-09-09 16:06:59 +0200752 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200753 /* match found */
754 break;
755 }
756 }
757
Michal Vaskoe0665742021-02-11 11:08:44 +0100758 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100759 if (lybctx->ext) {
760 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
761 lybctx->ext->def->name);
762 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100763 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200764 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200765 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100766 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200767 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200768 }
769 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200770 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200771 return ret;
772 }
773
774 *snode = sibling;
775 return LY_SUCCESS;
776}
777
778/**
aPiecek570d7ed2021-09-10 07:15:35 +0200779 * @brief Read until the end of the current siblings.
Michal Vasko60ea6352020-06-29 13:39:39 +0200780 *
781 * @param[in] lybctx LYB context.
782 */
783static void
aPiecek570d7ed2021-09-10 07:15:35 +0200784lyb_skip_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200785{
Michal Vasko60ea6352020-06-29 13:39:39 +0200786 do {
787 /* first skip any meta information inside */
aPiecek570d7ed2021-09-10 07:15:35 +0200788 ly_in_skip(lybctx->in, LYB_LAST_SIBLING(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200789
790 /* then read data */
aPiecek570d7ed2021-09-10 07:15:35 +0200791 lyb_read(NULL, LYB_LAST_SIBLING(lybctx).written, lybctx);
792 } while (LYB_LAST_SIBLING(lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200793}
794
795/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200796 * @brief Parse the context of anydata/anyxml node.
797 *
798 * @param[in] ctx libyang context.
799 * @param[in] data LYB data to parse.
800 * @param[out] tree Parsed tree.
801 * @return LY_ERR value.
802 */
803static LY_ERR
804lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
805{
806 LY_ERR ret;
807 uint32_t prev_lo;
808 struct ly_in *in;
809 struct lyd_ctx *lydctx = NULL;
810
811 *tree = NULL;
812
813 LY_CHECK_RET(ly_in_new_memory(data, &in));
814
815 /* turn logging off */
816 prev_lo = ly_log_options(0);
817
Michal Vasko56d88602021-07-15 16:37:59 +0200818 ret = _lyd_parse_lyb(ctx, NULL, NULL, tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0,
Michal Vasko02ed9d82021-07-15 14:58:04 +0200819 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
820
821 /* turn logging on again */
822 ly_log_options(prev_lo);
823
824 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200825 if (lydctx) {
826 lydctx->free(lydctx);
827 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200828 if (ret) {
829 lyd_free_siblings(*tree);
830 *tree = NULL;
831 }
832 return ret;
833}
834
835/**
aPiecek37c493b2021-09-09 12:52:30 +0200836 * @brief Insert new node to @p parsed set.
837 *
838 * Also if needed, correct @p first_p.
839 *
840 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200841 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200842 * @param[in,out] node Parsed node to insertion.
843 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
844 * @param[out] parsed Set of all successfully parsed nodes.
845 * @return LY_ERR value.
846 */
847static void
848lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
849 struct ly_set *parsed)
850{
851 /* insert, keep first pointer correct */
852 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
853 while (!parent && (*first_p)->prev->next) {
854 *first_p = (*first_p)->prev;
855 }
856
857 /* rememeber a successfully parsed node */
858 if (parsed) {
859 ly_set_add(parsed, node, 1, NULL);
860 }
861}
862
863/**
864 * @brief Finish parsing the opaq node.
865 *
866 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200867 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200868 * @param[in] flags Node flags to set.
869 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
870 * @param[in,out] node Parsed opaq node to finish.
871 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
872 * @param[out] parsed Set of all successfully parsed nodes.
873 * @return LY_ERR value.
874 */
875static void
876lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
877 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
878{
879 struct lyd_attr *iter;
880
881 /* set flags */
882 (*node)->flags = flags;
883
884 /* add attributes */
885 assert(!(*node)->schema);
886 LY_LIST_FOR(*attr, iter) {
887 iter->parent = (struct lyd_node_opaq *)*node;
888 }
889 ((struct lyd_node_opaq *)*node)->attr = *attr;
890 *attr = NULL;
891
892 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
893 *node = NULL;
894}
895
896/**
897 * @brief Finish parsing the node.
898 *
899 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200900 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200901 * @param[in] flags Node flags to set.
902 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
903 * @param[in,out] node Parsed node to finish.
904 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
905 * @param[out] parsed Set of all successfully parsed nodes.
906 * @return LY_ERR value.
907 */
908static void
909lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
910 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
911{
912 struct lyd_meta *m;
913
914 /* set flags */
915 (*node)->flags = flags;
916
917 /* add metadata */
918 LY_LIST_FOR(*meta, m) {
919 m->parent = *node;
920 }
921 (*node)->meta = *meta;
922 *meta = NULL;
923
924 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
925 *node = NULL;
926}
927
928/**
aPiecek6fdac2f2021-09-10 11:21:10 +0200929 * @brief Parse header for non-opaq node.
930 *
931 * @param[in] lybctx LYB context.
Michal Vaskoaac267d2022-01-17 13:34:48 +0100932 * @param[in] sparent Schema parent node of the metadata.
aPiecek6fdac2f2021-09-10 11:21:10 +0200933 * @param[out] flags Parsed node flags.
934 * @param[out] meta Parsed metadata of the node.
935 * @return LY_ERR value.
936 */
937static LY_ERR
Michal Vaskoaac267d2022-01-17 13:34:48 +0100938lyb_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 +0200939{
940 LY_ERR ret;
941
942 /* create and read metadata */
Michal Vaskoaac267d2022-01-17 13:34:48 +0100943 ret = lyb_parse_metadata(lybctx, sparent, meta);
aPiecek6fdac2f2021-09-10 11:21:10 +0200944 LY_CHECK_RET(ret);
945
946 /* read flags */
947 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
948
949 return ret;
950}
951
952/**
aPiecek33fc6b02021-09-09 15:45:37 +0200953 * @brief Create term node and fill it with value.
954 *
955 * @param[in] lybctx LYB context.
956 * @param[in] snode Schema of the term node.
957 * @param[out] node Created term node.
958 * @return LY_ERR value.
959 */
960static LY_ERR
961lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
962{
963 LY_ERR ret;
964 ly_bool dynamic;
965 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +0200966 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +0200967
968 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
969 LY_CHECK_RET(ret);
970
971 dynamic = 1;
972 /* create node */
973 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
974 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
975 NULL, LYD_HINT_DATA, node);
976 if (dynamic) {
977 free(term_value);
978 }
979 if (ret) {
980 lyd_free_tree(*node);
981 *node = NULL;
982 }
983
984 return ret;
985}
986
987/**
aPiecek0e2e1052021-09-09 15:48:27 +0200988 * @brief Validate inner node, autodelete default values nad create implicit nodes.
989 *
990 * @param[in,out] lybctx LYB context.
991 * @param[in] snode Schema of the inner node.
992 * @param[in] node Parsed inner node.
993 * @return LY_ERR value.
994 */
995static LY_ERR
996lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
997{
998 LY_ERR ret = LY_SUCCESS;
999 uint32_t impl_opts;
1000
1001 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1002 /* new node validation, autodelete CANNOT occur, all nodes are new */
1003 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
1004 LY_CHECK_RET(ret);
1005
1006 /* add any missing default children */
1007 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
Michal Vaskoaac267d2022-01-17 13:34:48 +01001008 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_types,
1009 impl_opts, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001010 LY_CHECK_RET(ret);
1011 }
1012
1013 return ret;
1014}
1015
1016/**
aPiecek821cf732021-09-09 15:37:28 +02001017 * @brief Parse opaq node.
1018 *
1019 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001020 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001021 * @param[in,out] first_p First top-level sibling.
1022 * @param[out] parsed Set of all successfully parsed nodes.
1023 * @return LY_ERR value.
1024 */
1025static LY_ERR
1026lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1027{
1028 LY_ERR ret;
1029 struct lyd_node *node = NULL;
1030 struct lyd_attr *attr = NULL;
1031 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1032 ly_bool dynamic = 0;
1033 LY_VALUE_FORMAT format = 0;
1034 void *val_prefix_data = NULL;
1035 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1036 uint32_t flags;
1037
aPiecek821cf732021-09-09 15:37:28 +02001038 /* parse opaq node attributes */
1039 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1040 LY_CHECK_GOTO(ret, cleanup);
1041
1042 /* read flags */
1043 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1044
1045 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001046 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001047 LY_CHECK_GOTO(ret, cleanup);
1048
1049 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001050 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001051 LY_CHECK_GOTO(ret, cleanup);
1052
1053 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001054 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001055 LY_CHECK_GOTO(ret, cleanup);
1056
1057 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001058 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001059 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1060 dynamic = 1;
1061
1062 /* parse format */
1063 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1064
1065 /* parse value prefixes */
1066 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1067 LY_CHECK_GOTO(ret, cleanup);
1068
aPiecek570d7ed2021-09-10 07:15:35 +02001069 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001070 /* skip children */
1071 ret = lyb_read_start_siblings(lybctx->lybctx);
1072 LY_CHECK_GOTO(ret, cleanup);
1073 lyb_skip_siblings(lybctx->lybctx);
1074 ret = lyb_read_stop_siblings(lybctx->lybctx);
1075 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001076 goto cleanup;
1077 }
1078
aPiecek821cf732021-09-09 15:37:28 +02001079 /* create node */
1080 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1081 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1082 LY_CHECK_GOTO(ret, cleanup);
1083
1084 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001085 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1086 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001087
1088 /* register parsed opaq node */
1089 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1090 assert(!attr && !node);
1091
1092cleanup:
1093 free(prefix);
1094 free(module_key);
1095 free(name);
1096 if (dynamic) {
1097 free(value);
1098 }
1099 lyd_free_attr_siblings(ctx, attr);
1100 lyd_free_tree(node);
1101
1102 return ret;
1103}
1104
aPiecek18457d72021-09-09 15:52:20 +02001105/**
1106 * @brief Parse anydata or anyxml node.
1107 *
1108 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001109 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001110 * @param[in] snode Schema of the node to be parsed.
1111 * @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_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1117 struct lyd_node **first_p, struct ly_set *parsed)
1118{
1119 LY_ERR ret;
1120 struct lyd_node *node = NULL, *tree;
1121 struct lyd_meta *meta = NULL;
1122 LYD_ANYDATA_VALUETYPE value_type;
1123 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001124 uint32_t flags;
1125 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1126
1127 /* read necessary basic data */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001128 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001129 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001130
1131 /* parse value type */
1132 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1133 if (value_type == LYD_ANYDATA_DATATREE) {
1134 /* invalid situation */
1135 LOGINT(ctx);
1136 ret = LY_EINT;
1137 goto error;
1138 }
1139
1140 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001141 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001142 LY_CHECK_GOTO(ret, error);
1143
1144 if (value_type == LYD_ANYDATA_LYB) {
1145 /* try to parse LYB into a data tree */
1146 if (!lyb_parse_any_content(ctx, value, &tree)) {
1147 /* successfully parsed */
1148 free(value);
1149 value = (char *)tree;
1150 value_type = LYD_ANYDATA_DATATREE;
1151 }
1152 }
1153
1154 /* create the node */
1155 switch (value_type) {
1156 case LYD_ANYDATA_LYB:
1157 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001158 case LYD_ANYDATA_STRING:
1159 case LYD_ANYDATA_XML:
1160 case LYD_ANYDATA_JSON:
Michal Vasko46f7ac92022-02-24 16:07:27 +01001161 /* use the value directly */
1162 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001163 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001164 break;
1165 default:
1166 LOGINT(ctx);
1167 ret = LY_EINT;
1168 goto error;
1169 }
1170
1171 /* register parsed anydata node */
1172 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1173
1174 return LY_SUCCESS;
1175
1176error:
1177 free(value);
1178 lyd_free_meta_siblings(meta);
1179 lyd_free_tree(node);
1180 return ret;
1181}
aPiecek821cf732021-09-09 15:37:28 +02001182
1183/**
aPiecek5777f122021-09-10 10:26:23 +02001184 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001185 *
1186 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001187 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1188 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001189 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1190 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001191 * @return LY_ERR value.
1192 */
1193static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001194lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001195 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001196{
1197 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001198 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001199 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001200 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001201
aPiecek307f0772021-09-10 09:09:47 +02001202 /* read necessary basic data */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001203 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001204 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001205
aPiecek5777f122021-09-10 10:26:23 +02001206 /* create node */
1207 ret = lyd_create_inner(snode, &node);
1208 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001209
aPiecek5777f122021-09-10 10:26:23 +02001210 /* process children */
1211 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1212 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001213
aPiecek5777f122021-09-10 10:26:23 +02001214 /* additional procedure for inner node */
1215 ret = lyb_validate_node_inner(lybctx, snode, node);
1216 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001217
aPiecek5777f122021-09-10 10:26:23 +02001218 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1219 /* rememeber the RPC/action/notification */
1220 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001221 }
1222
aPiecek307f0772021-09-10 09:09:47 +02001223 /* register parsed node */
1224 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1225
1226 return LY_SUCCESS;
1227
1228error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001229 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001230 lyd_free_tree(node);
1231 return ret;
1232}
1233
1234/**
aPiecek8a555d72021-09-10 10:15:26 +02001235 * @brief Parse leaf node.
1236 *
1237 * @param[in] lybctx LYB context.
1238 * @param[in] parent Data parent of the sibling.
1239 * @param[in] snode Schema of the node to be parsed.
1240 * @param[in,out] first_p First top-level sibling.
1241 * @param[out] parsed Set of all successfully parsed nodes.
1242 * @return LY_ERR value.
1243 */
1244static LY_ERR
1245lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1246 struct lyd_node **first_p, struct ly_set *parsed)
1247{
1248 LY_ERR ret;
1249 struct lyd_node *node = NULL;
1250 struct lyd_meta *meta = NULL;
1251 uint32_t flags;
1252
1253 /* read necessary basic data */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001254 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001255 LY_CHECK_GOTO(ret, error);
1256
1257 /* read value of term node and create it */
1258 ret = lyb_create_term(lybctx, snode, &node);
1259 LY_CHECK_GOTO(ret, error);
1260
1261 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1262
1263 return LY_SUCCESS;
1264
1265error:
1266 lyd_free_meta_siblings(meta);
1267 lyd_free_tree(node);
1268 return ret;
1269}
1270
1271/**
aPiecek5777f122021-09-10 10:26:23 +02001272 * @brief Parse all leaflist nodes which belong to same schema.
1273 *
1274 * @param[in] lybctx LYB context.
1275 * @param[in] parent Data parent of the sibling.
1276 * @param[in] snode Schema of the nodes to be parsed.
1277 * @param[in,out] first_p First top-level sibling.
1278 * @param[out] parsed Set of all successfully parsed nodes.
1279 * @return LY_ERR value.
1280 */
1281static LY_ERR
1282lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1283 struct lyd_node **first_p, struct ly_set *parsed)
1284{
1285 LY_ERR ret;
1286
1287 /* register a new sibling */
1288 ret = lyb_read_start_siblings(lybctx->lybctx);
1289 LY_CHECK_RET(ret);
1290
1291 /* process all siblings */
1292 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1293 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1294 LY_CHECK_RET(ret);
1295 }
1296
1297 /* end the sibling */
1298 ret = lyb_read_stop_siblings(lybctx->lybctx);
1299 LY_CHECK_RET(ret);
1300
1301 return ret;
1302}
1303
1304/**
aPiecek77d3e962021-09-10 10:33:59 +02001305 * @brief Parse all list nodes which belong to same schema.
1306 *
1307 * @param[in] lybctx LYB context.
1308 * @param[in] parent Data parent of the sibling.
1309 * @param[in] snode Schema of the nodes to be parsed.
1310 * @param[in,out] first_p First top-level sibling.
1311 * @param[out] parsed Set of all successfully parsed nodes.
1312 * @return LY_ERR value.
1313 */
1314static LY_ERR
1315lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1316 struct lyd_node **first_p, struct ly_set *parsed)
1317{
1318 LY_ERR ret;
1319 struct lyd_node *node = NULL;
1320 struct lyd_meta *meta = NULL;
1321 uint32_t flags;
1322
1323 /* register a new sibling */
1324 ret = lyb_read_start_siblings(lybctx->lybctx);
1325 LY_CHECK_RET(ret);
1326
1327 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1328 /* read necessary basic data */
Michal Vaskoaac267d2022-01-17 13:34:48 +01001329 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001330 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001331
1332 /* create list node */
1333 ret = lyd_create_inner(snode, &node);
1334 LY_CHECK_GOTO(ret, error);
1335
1336 /* process children */
1337 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1338 LY_CHECK_GOTO(ret, error);
1339
1340 /* additional procedure for inner node */
1341 ret = lyb_validate_node_inner(lybctx, snode, node);
1342 LY_CHECK_GOTO(ret, error);
1343
1344 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1345 /* rememeber the RPC/action/notification */
1346 lybctx->op_node = node;
1347 }
1348
1349 /* register parsed list node */
1350 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1351 }
1352
1353 /* end the sibling */
1354 ret = lyb_read_stop_siblings(lybctx->lybctx);
1355 LY_CHECK_RET(ret);
1356
1357 return LY_SUCCESS;
1358
1359error:
1360 lyd_free_meta_siblings(meta);
1361 lyd_free_tree(node);
1362 return ret;
1363}
1364
1365/**
aPiecek17737b52021-09-21 12:29:52 +02001366 * @brief Parse node.
1367 *
1368 * @param[in] out Out structure.
1369 * @param[in,out] printed_node Current data node to print. Sets to the last printed node.
1370 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
1371 * @param[in] lybctx LYB context.
1372 * @return LY_ERR value.
1373 */
1374static LY_ERR
1375lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1376 struct ly_set *parsed)
1377{
1378 LY_ERR ret;
1379 const struct lysc_node *snode;
1380 const struct lys_module *mod;
1381
1382 if (!parent || !parent->schema) {
1383 /* top-level or opaque, read module name */
1384 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
1385 LY_CHECK_RET(ret);
1386
1387 /* read hash, find the schema node starting from mod */
1388 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
1389 } else {
1390 /* read hash, find the schema node starting from parent schema */
1391 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
1392 }
1393 LY_CHECK_RET(ret);
1394
1395 if (!snode) {
1396 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1397 } else if (snode->nodetype & LYS_LEAFLIST) {
1398 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1399 } else if (snode->nodetype == LYS_LIST) {
1400 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1401 } else if (snode->nodetype & LYD_NODE_ANY) {
1402 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1403 } else if (snode->nodetype & LYD_NODE_INNER) {
1404 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1405 } else {
1406 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1407 }
1408 LY_CHECK_RET(ret);
1409
1410 return ret;
1411}
1412
1413/**
aPiecek570d7ed2021-09-10 07:15:35 +02001414 * @brief Parse siblings (@ref lyb_print_siblings()).
1415 *
1416 * @param[in] lybctx LYB context.
1417 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1418 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1419 * @param[out] parsed Set of all successfully parsed nodes.
1420 * @return LY_ERR value.
1421 */
1422static LY_ERR
1423lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1424 struct ly_set *parsed)
1425{
1426 LY_ERR ret;
aPiecek570d7ed2021-09-10 07:15:35 +02001427 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001428
1429 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1430
1431 /* register a new siblings */
1432 ret = lyb_read_start_siblings(lybctx->lybctx);
1433 LY_CHECK_RET(ret);
1434
1435 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
aPiecek17737b52021-09-21 12:29:52 +02001436 ret = lyb_parse_node(lybctx, parent, first_p, parsed);
aPiecek570d7ed2021-09-10 07:15:35 +02001437 LY_CHECK_RET(ret);
1438
1439 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1440 break;
1441 }
1442 }
1443
1444 /* end the siblings */
1445 ret = lyb_read_stop_siblings(lybctx->lybctx);
1446 LY_CHECK_RET(ret);
1447
1448 return ret;
1449}
1450
1451/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001452 * @brief Parse used YANG data models.
1453 *
1454 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001455 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001456 * @return LY_ERR value.
1457 */
1458static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001459lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001460{
1461 LY_ERR ret;
1462 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001463 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001464
1465 /* read model count */
1466 lyb_read_number(&count, sizeof count, 2, lybctx);
1467
1468 if (count) {
1469 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1470
1471 /* read modules */
1472 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001473 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001474 LY_CHECK_RET(ret);
1475 LY_ARRAY_INCREMENT(lybctx->models);
1476 }
1477 }
1478
1479 return LY_SUCCESS;
1480}
1481
1482/**
1483 * @brief Parse LYB magic number.
1484 *
1485 * @param[in] lybctx LYB context.
1486 * @return LY_ERR value.
1487 */
1488static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001489lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001490{
1491 char magic_byte = 0;
1492
1493 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1494 if (magic_byte != 'l') {
1495 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1496 return LY_EINVAL;
1497 }
1498
1499 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1500 if (magic_byte != 'y') {
1501 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1502 return LY_EINVAL;
1503 }
1504
1505 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1506 if (magic_byte != 'b') {
1507 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1508 return LY_EINVAL;
1509 }
1510
1511 return LY_SUCCESS;
1512}
1513
1514/**
1515 * @brief Parse LYB header.
1516 *
1517 * @param[in] lybctx LYB context.
1518 * @return LY_ERR value.
1519 */
1520static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001521lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001522{
1523 uint8_t byte = 0;
1524
1525 /* version, future flags */
1526 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1527
1528 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1529 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001530 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001531 return LY_EINVAL;
1532 }
1533
1534 return LY_SUCCESS;
1535}
1536
Michal Vasko02ed9d82021-07-15 14:58:04 +02001537static LY_ERR
1538_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1539 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
Radek Krejcif16e2542021-02-17 15:39:23 +01001540 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001541{
Michal Vaskoe0665742021-02-11 11:08:44 +01001542 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001543 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001544
Michal Vaskoe0665742021-02-11 11:08:44 +01001545 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1546 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001547
Radek Krejci1798aae2020-07-14 13:26:06 +02001548 lybctx = calloc(1, sizeof *lybctx);
1549 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1550 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001551 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001552
Radek Krejci1798aae2020-07-14 13:26:06 +02001553 lybctx->lybctx->in = in;
1554 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001555 lybctx->parse_opts = parse_opts;
1556 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001557 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001558 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001559 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001560
1561 /* find the operation node if it exists already */
1562 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1563
Michal Vasko60ea6352020-06-29 13:39:39 +02001564 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001565 rc = lyb_parse_magic_number(lybctx->lybctx);
1566 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001567
1568 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001569 rc = lyb_parse_header(lybctx->lybctx);
1570 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001571
1572 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001573 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1574 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001575
aPiecek570d7ed2021-09-10 07:15:35 +02001576 /* read sibling(s) */
1577 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1578 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001579
1580 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1581 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1582 rc = LY_EVALID;
1583 goto cleanup;
1584 }
1585 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1586 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1587 rc = LY_EVALID;
1588 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001589 }
1590
1591 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001592 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001593
Michal Vasko60ea6352020-06-29 13:39:39 +02001594cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001595 /* there should be no unres stored if validation should be skipped */
1596 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1597 !lybctx->node_when.count));
1598
1599 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001600 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001601 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001602 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001603 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001604 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001605}
1606
Michal Vasko02ed9d82021-07-15 14:58:04 +02001607LY_ERR
1608lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1609 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoaac267d2022-01-17 13:34:48 +01001610 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko02ed9d82021-07-15 14:58:04 +02001611{
1612 uint32_t int_opts;
1613
1614 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1615 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1616
Michal Vaskoaac267d2022-01-17 13:34:48 +01001617 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1618
Michal Vasko02ed9d82021-07-15 14:58:04 +02001619 switch (data_type) {
1620 case LYD_TYPE_DATA_YANG:
1621 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1622 break;
1623 case LYD_TYPE_RPC_YANG:
1624 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1625 break;
1626 case LYD_TYPE_NOTIF_YANG:
1627 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1628 break;
1629 case LYD_TYPE_REPLY_YANG:
1630 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1631 break;
1632 default:
1633 LOGINT(ctx);
1634 return LY_EINT;
1635 }
1636
Michal Vaskoaac267d2022-01-17 13:34:48 +01001637 if (subtree_sibling) {
1638 *subtree_sibling = 0;
1639 }
Michal Vasko02ed9d82021-07-15 14:58:04 +02001640 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1641}
1642
Jan Kundrátc6e39de2021-12-09 16:01:19 +01001643LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001644lyd_lyb_data_length(const char *data)
1645{
1646 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001647 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001648 int count, i;
1649 size_t len;
1650 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001651 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001652
1653 if (!data) {
1654 return -1;
1655 }
1656
Radek Krejci1798aae2020-07-14 13:26:06 +02001657 lybctx = calloc(1, sizeof *lybctx);
1658 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1659 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001660 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001661
1662 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001663 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001664 LY_CHECK_GOTO(ret, cleanup);
1665
1666 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001667 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001668 LY_CHECK_GOTO(ret, cleanup);
1669
1670 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001671 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001672
1673 /* read all models */
1674 for (i = 0; i < count; ++i) {
1675 /* module name length */
1676 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001677 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001678
1679 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001680 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001681
1682 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001683 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001684 }
1685
aPiecek6828a312021-09-17 15:53:18 +02001686 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001687 /* register a new sibling */
1688 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001689 LY_CHECK_GOTO(ret, cleanup);
1690
1691 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001692 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001693
aPiecek570d7ed2021-09-10 07:15:35 +02001694 /* sibling finished */
1695 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001696 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001697 } else {
aPiecek6828a312021-09-17 15:53:18 +02001698 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001699 }
1700
1701 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001702 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001703
1704cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001705 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001706
Radek Krejci1798aae2020-07-14 13:26:06 +02001707 ly_in_free(lybctx->in, 0);
1708 lylyb_ctx_free(lybctx);
1709
Michal Vasko63f3d842020-07-08 10:10:14 +02001710 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001711}