blob: 596e48cbf6e7a1a3cb391e4089a754f775188427 [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 Vasko60ea6352020-06-29 13:39:39 +0200417 * @param[out] meta Parsed metadata.
418 * @return LY_ERR value.
419 */
420static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200421lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200422{
423 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200424 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200425 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200426 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200427 const struct lys_module *mod;
428
429 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200430 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200431
432 /* read attributes */
433 for (i = 0; i < count; ++i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200434 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100435 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200436 LY_CHECK_GOTO(ret, cleanup);
437
438 if (!mod) {
aPiecekd5796f92021-09-30 13:39:37 +0200439 /* skip meta name */
440 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
441
442 /* skip meta value */
443 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
444 continue;
Michal Vasko60ea6352020-06-29 13:39:39 +0200445 }
446
447 /* meta name */
aPieceke99345d2021-09-30 12:49:59 +0200448 ret = lyb_read_string(&meta_name, sizeof(uint16_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200449 LY_CHECK_GOTO(ret, cleanup);
450
451 /* meta value */
aPieceke99345d2021-09-30 12:49:59 +0200452 ret = lyb_read_string(&meta_value, sizeof(uint64_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200453 LY_CHECK_GOTO(ret, cleanup);
454 dynamic = 1;
455
456 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200457 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200458 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200459
460 /* free strings */
461 free(meta_name);
462 meta_name = NULL;
463 if (dynamic) {
464 free(meta_value);
465 dynamic = 0;
466 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200467
Radek Krejci1798aae2020-07-14 13:26:06 +0200468 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200469 }
470
471cleanup:
472 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200473 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200474 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200475 *meta = NULL;
476 }
477 return ret;
478}
479
480/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100481 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200482 *
483 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100484 * @param[in] format Prefix data format.
485 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200486 * @return LY_ERR value.
487 */
488static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200489lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200490{
491 LY_ERR ret = LY_SUCCESS;
492 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100493 struct ly_set *set = NULL;
494 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200495
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100496 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200497 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100498 /* read count */
499 lyb_read(&count, 1, lybctx);
500 if (!count) {
501 return LY_SUCCESS;
502 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200503
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100504 /* read all NS elements */
505 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200506
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100507 for (i = 0; i < count; ++i) {
508 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200509
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100510 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200511 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, sizeof(uint16_t), lybctx), cleanup);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100512
513 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200514 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, sizeof(uint16_t), lybctx), cleanup);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100515
516 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
517 ns = NULL;
518 }
519
520 *prefix_data = set;
521 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200522 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200523 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100524 /* nothing stored */
525 break;
526 default:
527 LOGINT(lybctx->ctx);
528 ret = LY_EINT;
529 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200530 }
531
532cleanup:
533 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100534 ly_free_prefix_data(format, set);
535 if (ns) {
536 free(ns->prefix);
537 free(ns->uri);
538 free(ns);
539 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200540 }
541 return ret;
542}
543
544/**
545 * @brief Parse opaque attributes.
546 *
547 * @param[in] lybctx LYB context.
548 * @param[out] attr Parsed attributes.
549 * @return LY_ERR value.
550 */
551static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200552lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200553{
554 LY_ERR ret = LY_SUCCESS;
555 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200556 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200557 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200558 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200559 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100560 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200561
562 /* read count */
563 lyb_read(&count, 1, lybctx);
564
565 /* read attributes */
566 for (i = 0; i < count; ++i) {
Michal Vasko0fdcd242020-11-11 19:12:30 +0100567 /* prefix, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200568 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200569 LY_CHECK_GOTO(ret, cleanup);
570 if (!prefix[0]) {
571 free(prefix);
572 prefix = NULL;
573 }
574
575 /* namespace, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200576 ret = lyb_read_string(&module_name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200577 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 if (!module_name[0]) {
579 free(module_name);
580 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200581 }
582
583 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200584 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200585 LY_CHECK_GOTO(ret, cleanup);
586
Michal Vasko60ea6352020-06-29 13:39:39 +0200587 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200588 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200589
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100590 /* value prefixes */
591 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
592 LY_CHECK_GOTO(ret, cleanup);
593
Michal Vasko60ea6352020-06-29 13:39:39 +0200594 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200595 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100596 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200597 dynamic = 1;
598
599 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100600 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200601 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200602 LY_CHECK_GOTO(ret, cleanup);
603
604 free(prefix);
605 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200606 free(module_name);
607 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200608 free(name);
609 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200610 assert(!dynamic);
611 value = NULL;
612
613 if (!*attr) {
614 *attr = attr2;
615 }
616
Michal Vasko60ea6352020-06-29 13:39:39 +0200617 LY_CHECK_GOTO(ret, cleanup);
618 }
619
620cleanup:
621 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200622 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200623 free(name);
624 if (dynamic) {
625 free(value);
626 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200627 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200628 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200629 *attr = NULL;
630 }
631 return ret;
632}
633
634/**
aPiecek619350d2021-09-09 16:06:59 +0200635 * @brief Fill @p hash with hash values.
636 *
637 * @param[in] lybctx LYB context.
638 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
639 * @param[out] hash_count Number of hashes in @p hash.
640 * @return LY_ERR value.
641 */
642static LY_ERR
643lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
644{
645 uint8_t i = 0, j;
646
647 /* read the first hash */
648 lyb_read(&hash[0], sizeof *hash, lybctx);
649
650 if (!hash[0]) {
651 *hash_count = i + 1;
652 return LY_SUCCESS;
653 }
654
655 /* based on the first hash read all the other ones, if any */
656 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
657 if (i > LYB_HASH_BITS) {
658 LOGINT_RET(lybctx->ctx);
659 }
660 }
661
662 /* move the first hash on its accurate position */
663 hash[i] = hash[0];
664
665 /* read the rest of hashes */
666 for (j = i; j; --j) {
667 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
668
669 /* correct collision ID */
670 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
671 /* preceded with zeros */
672 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
673 }
674
675 *hash_count = i + 1;
676
677 return LY_SUCCESS;
678}
679
680/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200681 * @brief Check whether a schema node matches a hash(es).
682 *
683 * @param[in] sibling Schema node to check.
684 * @param[in] hash Hash array to check.
685 * @param[in] hash_count Number of hashes in @p hash.
686 * @return non-zero if matches,
687 * @return 0 if not.
688 */
689static int
690lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
691{
692 LYB_HASH sibling_hash;
693 uint8_t i;
694
695 /* compare all the hashes starting from collision ID 0 */
696 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200697 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200698 if (sibling_hash != hash[i]) {
699 return 0;
700 }
701 }
702
703 return 1;
704}
705
706/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200707 * @brief Parse schema node hash.
708 *
709 * @param[in] lybctx LYB context.
710 * @param[in] sparent Schema parent, must be set if @p mod is not.
711 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
712 * @param[out] snode Parsed found schema node, may be NULL if opaque.
713 * @return LY_ERR value.
714 */
715static LY_ERR
716lyb_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 +0200717 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200718{
719 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200720 const struct lysc_node *sibling;
721 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200722 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200723 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200724
aPiecek570d7ed2021-09-10 07:15:35 +0200725 *snode = NULL;
726
aPiecek619350d2021-09-09 16:06:59 +0200727 ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
728 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200729
730 if (!hash[0]) {
731 /* opaque node */
732 return LY_SUCCESS;
733 }
734
aPiecek619350d2021-09-09 16:06:59 +0200735 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200736
737 /* find our node with matching hashes */
738 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100739 while (1) {
740 if (!sparent && lybctx->ext) {
741 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
742 } else {
743 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
744 }
745 if (!sibling) {
746 break;
747 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200748 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200749 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
aPiecek619350d2021-09-09 16:06:59 +0200750 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200751 /* match found */
752 break;
753 }
754 }
755
Michal Vaskoe0665742021-02-11 11:08:44 +0100756 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100757 if (lybctx->ext) {
758 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
759 lybctx->ext->def->name);
760 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100761 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200762 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200763 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100764 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200765 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200766 }
767 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200768 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200769 return ret;
770 }
771
772 *snode = sibling;
773 return LY_SUCCESS;
774}
775
776/**
aPiecek570d7ed2021-09-10 07:15:35 +0200777 * @brief Read until the end of the current siblings.
Michal Vasko60ea6352020-06-29 13:39:39 +0200778 *
779 * @param[in] lybctx LYB context.
780 */
781static void
aPiecek570d7ed2021-09-10 07:15:35 +0200782lyb_skip_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200783{
Michal Vasko60ea6352020-06-29 13:39:39 +0200784 do {
785 /* first skip any meta information inside */
aPiecek570d7ed2021-09-10 07:15:35 +0200786 ly_in_skip(lybctx->in, LYB_LAST_SIBLING(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200787
788 /* then read data */
aPiecek570d7ed2021-09-10 07:15:35 +0200789 lyb_read(NULL, LYB_LAST_SIBLING(lybctx).written, lybctx);
790 } while (LYB_LAST_SIBLING(lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200791}
792
793/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200794 * @brief Parse the context of anydata/anyxml node.
795 *
796 * @param[in] ctx libyang context.
797 * @param[in] data LYB data to parse.
798 * @param[out] tree Parsed tree.
799 * @return LY_ERR value.
800 */
801static LY_ERR
802lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
803{
804 LY_ERR ret;
805 uint32_t prev_lo;
806 struct ly_in *in;
807 struct lyd_ctx *lydctx = NULL;
808
809 *tree = NULL;
810
811 LY_CHECK_RET(ly_in_new_memory(data, &in));
812
813 /* turn logging off */
814 prev_lo = ly_log_options(0);
815
Michal Vasko56d88602021-07-15 16:37:59 +0200816 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 +0200817 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
818
819 /* turn logging on again */
820 ly_log_options(prev_lo);
821
822 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200823 if (lydctx) {
824 lydctx->free(lydctx);
825 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200826 if (ret) {
827 lyd_free_siblings(*tree);
828 *tree = NULL;
829 }
830 return ret;
831}
832
833/**
aPiecek37c493b2021-09-09 12:52:30 +0200834 * @brief Insert new node to @p parsed set.
835 *
836 * Also if needed, correct @p first_p.
837 *
838 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200839 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200840 * @param[in,out] node Parsed node to insertion.
841 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
842 * @param[out] parsed Set of all successfully parsed nodes.
843 * @return LY_ERR value.
844 */
845static void
846lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
847 struct ly_set *parsed)
848{
849 /* insert, keep first pointer correct */
850 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
851 while (!parent && (*first_p)->prev->next) {
852 *first_p = (*first_p)->prev;
853 }
854
855 /* rememeber a successfully parsed node */
856 if (parsed) {
857 ly_set_add(parsed, node, 1, NULL);
858 }
859}
860
861/**
862 * @brief Finish parsing the opaq node.
863 *
864 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200865 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200866 * @param[in] flags Node flags to set.
867 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
868 * @param[in,out] node Parsed opaq node to finish.
869 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
870 * @param[out] parsed Set of all successfully parsed nodes.
871 * @return LY_ERR value.
872 */
873static void
874lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
875 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
876{
877 struct lyd_attr *iter;
878
879 /* set flags */
880 (*node)->flags = flags;
881
882 /* add attributes */
883 assert(!(*node)->schema);
884 LY_LIST_FOR(*attr, iter) {
885 iter->parent = (struct lyd_node_opaq *)*node;
886 }
887 ((struct lyd_node_opaq *)*node)->attr = *attr;
888 *attr = NULL;
889
890 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
891 *node = NULL;
892}
893
894/**
895 * @brief Finish parsing the node.
896 *
897 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200898 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200899 * @param[in] flags Node flags to set.
900 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
901 * @param[in,out] node Parsed node to finish.
902 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
903 * @param[out] parsed Set of all successfully parsed nodes.
904 * @return LY_ERR value.
905 */
906static void
907lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
908 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
909{
910 struct lyd_meta *m;
911
912 /* set flags */
913 (*node)->flags = flags;
914
915 /* add metadata */
916 LY_LIST_FOR(*meta, m) {
917 m->parent = *node;
918 }
919 (*node)->meta = *meta;
920 *meta = NULL;
921
922 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
923 *node = NULL;
924}
925
926/**
aPiecek6fdac2f2021-09-10 11:21:10 +0200927 * @brief Parse header for non-opaq node.
928 *
929 * @param[in] lybctx LYB context.
930 * @param[out] flags Parsed node flags.
931 * @param[out] meta Parsed metadata of the node.
932 * @return LY_ERR value.
933 */
934static LY_ERR
935lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta)
936{
937 LY_ERR ret;
938
939 /* create and read metadata */
940 ret = lyb_parse_metadata(lybctx, meta);
941 LY_CHECK_RET(ret);
942
943 /* read flags */
944 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
945
946 return ret;
947}
948
949/**
aPiecek33fc6b02021-09-09 15:45:37 +0200950 * @brief Create term node and fill it with value.
951 *
952 * @param[in] lybctx LYB context.
953 * @param[in] snode Schema of the term node.
954 * @param[out] node Created term node.
955 * @return LY_ERR value.
956 */
957static LY_ERR
958lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
959{
960 LY_ERR ret;
961 ly_bool dynamic;
962 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +0200963 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +0200964
965 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
966 LY_CHECK_RET(ret);
967
968 dynamic = 1;
969 /* create node */
970 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
971 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
972 NULL, LYD_HINT_DATA, node);
973 if (dynamic) {
974 free(term_value);
975 }
976 if (ret) {
977 lyd_free_tree(*node);
978 *node = NULL;
979 }
980
981 return ret;
982}
983
984/**
aPiecek0e2e1052021-09-09 15:48:27 +0200985 * @brief Validate inner node, autodelete default values nad create implicit nodes.
986 *
987 * @param[in,out] lybctx LYB context.
988 * @param[in] snode Schema of the inner node.
989 * @param[in] node Parsed inner node.
990 * @return LY_ERR value.
991 */
992static LY_ERR
993lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
994{
995 LY_ERR ret = LY_SUCCESS;
996 uint32_t impl_opts;
997
998 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
999 /* new node validation, autodelete CANNOT occur, all nodes are new */
1000 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
1001 LY_CHECK_RET(ret);
1002
1003 /* add any missing default children */
1004 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
1005 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL,
1006 NULL, &lybctx->node_when, &lybctx->node_exts,
1007 &lybctx->node_types, impl_opts, NULL);
1008 LY_CHECK_RET(ret);
1009 }
1010
1011 return ret;
1012}
1013
1014/**
aPiecek821cf732021-09-09 15:37:28 +02001015 * @brief Parse opaq node.
1016 *
1017 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001018 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001019 * @param[in,out] first_p First top-level sibling.
1020 * @param[out] parsed Set of all successfully parsed nodes.
1021 * @return LY_ERR value.
1022 */
1023static LY_ERR
1024lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1025{
1026 LY_ERR ret;
1027 struct lyd_node *node = NULL;
1028 struct lyd_attr *attr = NULL;
1029 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1030 ly_bool dynamic = 0;
1031 LY_VALUE_FORMAT format = 0;
1032 void *val_prefix_data = NULL;
1033 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1034 uint32_t flags;
1035
aPiecek821cf732021-09-09 15:37:28 +02001036 /* parse opaq node attributes */
1037 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1038 LY_CHECK_GOTO(ret, cleanup);
1039
1040 /* read flags */
1041 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1042
1043 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001044 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001045 LY_CHECK_GOTO(ret, cleanup);
1046
1047 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001048 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001049 LY_CHECK_GOTO(ret, cleanup);
1050
1051 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001052 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001053 LY_CHECK_GOTO(ret, cleanup);
1054
1055 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001056 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001057 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1058 dynamic = 1;
1059
1060 /* parse format */
1061 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1062
1063 /* parse value prefixes */
1064 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1065 LY_CHECK_GOTO(ret, cleanup);
1066
aPiecek570d7ed2021-09-10 07:15:35 +02001067 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001068 /* skip children */
1069 ret = lyb_read_start_siblings(lybctx->lybctx);
1070 LY_CHECK_GOTO(ret, cleanup);
1071 lyb_skip_siblings(lybctx->lybctx);
1072 ret = lyb_read_stop_siblings(lybctx->lybctx);
1073 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001074 goto cleanup;
1075 }
1076
aPiecek821cf732021-09-09 15:37:28 +02001077 /* create node */
1078 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1079 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1080 LY_CHECK_GOTO(ret, cleanup);
1081
1082 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001083 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1084 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001085
1086 /* register parsed opaq node */
1087 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1088 assert(!attr && !node);
1089
1090cleanup:
1091 free(prefix);
1092 free(module_key);
1093 free(name);
1094 if (dynamic) {
1095 free(value);
1096 }
1097 lyd_free_attr_siblings(ctx, attr);
1098 lyd_free_tree(node);
1099
1100 return ret;
1101}
1102
aPiecek18457d72021-09-09 15:52:20 +02001103/**
1104 * @brief Parse anydata or anyxml node.
1105 *
1106 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001107 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001108 * @param[in] snode Schema of the node to be parsed.
1109 * @param[in,out] first_p First top-level sibling.
1110 * @param[out] parsed Set of all successfully parsed nodes.
1111 * @return LY_ERR value.
1112 */
1113static LY_ERR
1114lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1115 struct lyd_node **first_p, struct ly_set *parsed)
1116{
1117 LY_ERR ret;
1118 struct lyd_node *node = NULL, *tree;
1119 struct lyd_meta *meta = NULL;
1120 LYD_ANYDATA_VALUETYPE value_type;
1121 char *value = NULL;
1122 const char *val_dict;
1123 uint32_t flags;
1124 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1125
1126 /* read necessary basic data */
Michal Vaskoedd4f252021-09-23 08:37:40 +02001127 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1128 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001129
1130 /* parse value type */
1131 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1132 if (value_type == LYD_ANYDATA_DATATREE) {
1133 /* invalid situation */
1134 LOGINT(ctx);
1135 ret = LY_EINT;
1136 goto error;
1137 }
1138
1139 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001140 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001141 LY_CHECK_GOTO(ret, error);
1142
1143 if (value_type == LYD_ANYDATA_LYB) {
1144 /* try to parse LYB into a data tree */
1145 if (!lyb_parse_any_content(ctx, value, &tree)) {
1146 /* successfully parsed */
1147 free(value);
1148 value = (char *)tree;
1149 value_type = LYD_ANYDATA_DATATREE;
1150 }
1151 }
1152
1153 /* create the node */
1154 switch (value_type) {
1155 case LYD_ANYDATA_LYB:
1156 case LYD_ANYDATA_DATATREE:
1157 /* use the value directly */
1158 ret = lyd_create_any(snode, value, value_type, 1, &node);
1159 LY_CHECK_GOTO(ret, error);
1160
1161 break;
1162 case LYD_ANYDATA_STRING:
1163 case LYD_ANYDATA_XML:
1164 case LYD_ANYDATA_JSON:
1165 /* value is expected to be in the dictionary */
1166 ret = lydict_insert_zc(ctx, value, &val_dict);
Michal Vaskoa5e41632021-11-02 12:19:40 +01001167 value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001168 LY_CHECK_GOTO(ret, error);
1169
1170 /* use the value in the dictionary */
1171 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
1172 if (ret) {
1173 lydict_remove(ctx, val_dict);
1174 goto error;
1175 }
1176 break;
1177 default:
1178 LOGINT(ctx);
1179 ret = LY_EINT;
1180 goto error;
1181 }
1182
1183 /* register parsed anydata node */
1184 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1185
1186 return LY_SUCCESS;
1187
1188error:
1189 free(value);
1190 lyd_free_meta_siblings(meta);
1191 lyd_free_tree(node);
1192 return ret;
1193}
aPiecek821cf732021-09-09 15:37:28 +02001194
1195/**
aPiecek5777f122021-09-10 10:26:23 +02001196 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001197 *
1198 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001199 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1200 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001201 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1202 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001203 * @return LY_ERR value.
1204 */
1205static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001206lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001207 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001208{
1209 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001210 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001211 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001212 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001213
aPiecek307f0772021-09-10 09:09:47 +02001214 /* read necessary basic data */
1215 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1216 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001217
aPiecek5777f122021-09-10 10:26:23 +02001218 /* create node */
1219 ret = lyd_create_inner(snode, &node);
1220 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001221
aPiecek5777f122021-09-10 10:26:23 +02001222 /* process children */
1223 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1224 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001225
aPiecek5777f122021-09-10 10:26:23 +02001226 /* additional procedure for inner node */
1227 ret = lyb_validate_node_inner(lybctx, snode, node);
1228 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001229
aPiecek5777f122021-09-10 10:26:23 +02001230 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1231 /* rememeber the RPC/action/notification */
1232 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001233 }
1234
aPiecek307f0772021-09-10 09:09:47 +02001235 /* register parsed node */
1236 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1237
1238 return LY_SUCCESS;
1239
1240error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001241 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001242 lyd_free_tree(node);
1243 return ret;
1244}
1245
1246/**
aPiecek8a555d72021-09-10 10:15:26 +02001247 * @brief Parse leaf node.
1248 *
1249 * @param[in] lybctx LYB context.
1250 * @param[in] parent Data parent of the sibling.
1251 * @param[in] snode Schema of the node to be parsed.
1252 * @param[in,out] first_p First top-level sibling.
1253 * @param[out] parsed Set of all successfully parsed nodes.
1254 * @return LY_ERR value.
1255 */
1256static LY_ERR
1257lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1258 struct lyd_node **first_p, struct ly_set *parsed)
1259{
1260 LY_ERR ret;
1261 struct lyd_node *node = NULL;
1262 struct lyd_meta *meta = NULL;
1263 uint32_t flags;
1264
1265 /* read necessary basic data */
1266 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1267 LY_CHECK_GOTO(ret, error);
1268
1269 /* read value of term node and create it */
1270 ret = lyb_create_term(lybctx, snode, &node);
1271 LY_CHECK_GOTO(ret, error);
1272
1273 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1274
1275 return LY_SUCCESS;
1276
1277error:
1278 lyd_free_meta_siblings(meta);
1279 lyd_free_tree(node);
1280 return ret;
1281}
1282
1283/**
aPiecek5777f122021-09-10 10:26:23 +02001284 * @brief Parse all leaflist nodes which belong to same schema.
1285 *
1286 * @param[in] lybctx LYB context.
1287 * @param[in] parent Data parent of the sibling.
1288 * @param[in] snode Schema of the nodes to be parsed.
1289 * @param[in,out] first_p First top-level sibling.
1290 * @param[out] parsed Set of all successfully parsed nodes.
1291 * @return LY_ERR value.
1292 */
1293static LY_ERR
1294lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1295 struct lyd_node **first_p, struct ly_set *parsed)
1296{
1297 LY_ERR ret;
1298
1299 /* register a new sibling */
1300 ret = lyb_read_start_siblings(lybctx->lybctx);
1301 LY_CHECK_RET(ret);
1302
1303 /* process all siblings */
1304 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1305 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1306 LY_CHECK_RET(ret);
1307 }
1308
1309 /* end the sibling */
1310 ret = lyb_read_stop_siblings(lybctx->lybctx);
1311 LY_CHECK_RET(ret);
1312
1313 return ret;
1314}
1315
1316/**
aPiecek77d3e962021-09-10 10:33:59 +02001317 * @brief Parse all list nodes which belong to same schema.
1318 *
1319 * @param[in] lybctx LYB context.
1320 * @param[in] parent Data parent of the sibling.
1321 * @param[in] snode Schema of the nodes to be parsed.
1322 * @param[in,out] first_p First top-level sibling.
1323 * @param[out] parsed Set of all successfully parsed nodes.
1324 * @return LY_ERR value.
1325 */
1326static LY_ERR
1327lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1328 struct lyd_node **first_p, struct ly_set *parsed)
1329{
1330 LY_ERR ret;
1331 struct lyd_node *node = NULL;
1332 struct lyd_meta *meta = NULL;
1333 uint32_t flags;
1334
1335 /* register a new sibling */
1336 ret = lyb_read_start_siblings(lybctx->lybctx);
1337 LY_CHECK_RET(ret);
1338
1339 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1340 /* read necessary basic data */
Michal Vaskoedd4f252021-09-23 08:37:40 +02001341 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1342 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001343
1344 /* create list node */
1345 ret = lyd_create_inner(snode, &node);
1346 LY_CHECK_GOTO(ret, error);
1347
1348 /* process children */
1349 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1350 LY_CHECK_GOTO(ret, error);
1351
1352 /* additional procedure for inner node */
1353 ret = lyb_validate_node_inner(lybctx, snode, node);
1354 LY_CHECK_GOTO(ret, error);
1355
1356 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1357 /* rememeber the RPC/action/notification */
1358 lybctx->op_node = node;
1359 }
1360
1361 /* register parsed list node */
1362 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1363 }
1364
1365 /* end the sibling */
1366 ret = lyb_read_stop_siblings(lybctx->lybctx);
1367 LY_CHECK_RET(ret);
1368
1369 return LY_SUCCESS;
1370
1371error:
1372 lyd_free_meta_siblings(meta);
1373 lyd_free_tree(node);
1374 return ret;
1375}
1376
1377/**
aPiecek17737b52021-09-21 12:29:52 +02001378 * @brief Parse node.
1379 *
1380 * @param[in] out Out structure.
1381 * @param[in,out] printed_node Current data node to print. Sets to the last printed node.
1382 * @param[in,out] sibling_ht Cached hash table for these siblings, created if NULL.
1383 * @param[in] lybctx LYB context.
1384 * @return LY_ERR value.
1385 */
1386static LY_ERR
1387lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1388 struct ly_set *parsed)
1389{
1390 LY_ERR ret;
1391 const struct lysc_node *snode;
1392 const struct lys_module *mod;
1393
1394 if (!parent || !parent->schema) {
1395 /* top-level or opaque, read module name */
1396 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
1397 LY_CHECK_RET(ret);
1398
1399 /* read hash, find the schema node starting from mod */
1400 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
1401 } else {
1402 /* read hash, find the schema node starting from parent schema */
1403 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
1404 }
1405 LY_CHECK_RET(ret);
1406
1407 if (!snode) {
1408 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1409 } else if (snode->nodetype & LYS_LEAFLIST) {
1410 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1411 } else if (snode->nodetype == LYS_LIST) {
1412 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1413 } else if (snode->nodetype & LYD_NODE_ANY) {
1414 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1415 } else if (snode->nodetype & LYD_NODE_INNER) {
1416 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1417 } else {
1418 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1419 }
1420 LY_CHECK_RET(ret);
1421
1422 return ret;
1423}
1424
1425/**
aPiecek570d7ed2021-09-10 07:15:35 +02001426 * @brief Parse siblings (@ref lyb_print_siblings()).
1427 *
1428 * @param[in] lybctx LYB context.
1429 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1430 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1431 * @param[out] parsed Set of all successfully parsed nodes.
1432 * @return LY_ERR value.
1433 */
1434static LY_ERR
1435lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1436 struct ly_set *parsed)
1437{
1438 LY_ERR ret;
aPiecek570d7ed2021-09-10 07:15:35 +02001439 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001440
1441 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1442
1443 /* register a new siblings */
1444 ret = lyb_read_start_siblings(lybctx->lybctx);
1445 LY_CHECK_RET(ret);
1446
1447 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
aPiecek17737b52021-09-21 12:29:52 +02001448 ret = lyb_parse_node(lybctx, parent, first_p, parsed);
aPiecek570d7ed2021-09-10 07:15:35 +02001449 LY_CHECK_RET(ret);
1450
1451 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1452 break;
1453 }
1454 }
1455
1456 /* end the siblings */
1457 ret = lyb_read_stop_siblings(lybctx->lybctx);
1458 LY_CHECK_RET(ret);
1459
1460 return ret;
1461}
1462
1463/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001464 * @brief Parse used YANG data models.
1465 *
1466 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001467 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001468 * @return LY_ERR value.
1469 */
1470static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001471lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001472{
1473 LY_ERR ret;
1474 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001475 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001476
1477 /* read model count */
1478 lyb_read_number(&count, sizeof count, 2, lybctx);
1479
1480 if (count) {
1481 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1482
1483 /* read modules */
1484 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001485 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001486 LY_CHECK_RET(ret);
1487 LY_ARRAY_INCREMENT(lybctx->models);
1488 }
1489 }
1490
1491 return LY_SUCCESS;
1492}
1493
1494/**
1495 * @brief Parse LYB magic number.
1496 *
1497 * @param[in] lybctx LYB context.
1498 * @return LY_ERR value.
1499 */
1500static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001501lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001502{
1503 char magic_byte = 0;
1504
1505 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1506 if (magic_byte != 'l') {
1507 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1508 return LY_EINVAL;
1509 }
1510
1511 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1512 if (magic_byte != 'y') {
1513 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1514 return LY_EINVAL;
1515 }
1516
1517 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1518 if (magic_byte != 'b') {
1519 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1520 return LY_EINVAL;
1521 }
1522
1523 return LY_SUCCESS;
1524}
1525
1526/**
1527 * @brief Parse LYB header.
1528 *
1529 * @param[in] lybctx LYB context.
1530 * @return LY_ERR value.
1531 */
1532static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001533lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001534{
1535 uint8_t byte = 0;
1536
1537 /* version, future flags */
1538 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1539
1540 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1541 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001542 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001543 return LY_EINVAL;
1544 }
1545
1546 return LY_SUCCESS;
1547}
1548
Michal Vasko02ed9d82021-07-15 14:58:04 +02001549static LY_ERR
1550_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1551 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 +01001552 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001553{
Michal Vaskoe0665742021-02-11 11:08:44 +01001554 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001555 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001556
Michal Vaskoe0665742021-02-11 11:08:44 +01001557 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1558 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001559
Radek Krejci1798aae2020-07-14 13:26:06 +02001560 lybctx = calloc(1, sizeof *lybctx);
1561 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1562 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001563 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001564
Radek Krejci1798aae2020-07-14 13:26:06 +02001565 lybctx->lybctx->in = in;
1566 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001567 lybctx->parse_opts = parse_opts;
1568 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001569 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001570 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001571 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001572
1573 /* find the operation node if it exists already */
1574 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1575
Michal Vasko60ea6352020-06-29 13:39:39 +02001576 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001577 rc = lyb_parse_magic_number(lybctx->lybctx);
1578 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001579
1580 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001581 rc = lyb_parse_header(lybctx->lybctx);
1582 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001583
1584 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001585 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1586 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001587
aPiecek570d7ed2021-09-10 07:15:35 +02001588 /* read sibling(s) */
1589 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1590 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001591
1592 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1593 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1594 rc = LY_EVALID;
1595 goto cleanup;
1596 }
1597 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1598 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1599 rc = LY_EVALID;
1600 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001601 }
1602
1603 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001604 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001605
Michal Vasko60ea6352020-06-29 13:39:39 +02001606cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001607 /* there should be no unres stored if validation should be skipped */
1608 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1609 !lybctx->node_when.count));
1610
1611 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001612 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001613 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001614 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001615 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001616 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001617}
1618
Michal Vasko02ed9d82021-07-15 14:58:04 +02001619LY_ERR
1620lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1621 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1622 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
1623{
1624 uint32_t int_opts;
1625
1626 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1627 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1628
1629 switch (data_type) {
1630 case LYD_TYPE_DATA_YANG:
1631 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1632 break;
1633 case LYD_TYPE_RPC_YANG:
1634 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1635 break;
1636 case LYD_TYPE_NOTIF_YANG:
1637 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1638 break;
1639 case LYD_TYPE_REPLY_YANG:
1640 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1641 break;
1642 default:
1643 LOGINT(ctx);
1644 return LY_EINT;
1645 }
1646
1647 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1648}
1649
Michal Vasko60ea6352020-06-29 13:39:39 +02001650API int
1651lyd_lyb_data_length(const char *data)
1652{
1653 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001654 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001655 int count, i;
1656 size_t len;
1657 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001658 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001659
1660 if (!data) {
1661 return -1;
1662 }
1663
Radek Krejci1798aae2020-07-14 13:26:06 +02001664 lybctx = calloc(1, sizeof *lybctx);
1665 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1666 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001667 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001668
1669 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001670 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001671 LY_CHECK_GOTO(ret, cleanup);
1672
1673 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001674 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001675 LY_CHECK_GOTO(ret, cleanup);
1676
1677 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001678 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001679
1680 /* read all models */
1681 for (i = 0; i < count; ++i) {
1682 /* module name length */
1683 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001684 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001685
1686 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001687 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001688
1689 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001690 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001691 }
1692
aPiecek6828a312021-09-17 15:53:18 +02001693 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001694 /* register a new sibling */
1695 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001696 LY_CHECK_GOTO(ret, cleanup);
1697
1698 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001699 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001700
aPiecek570d7ed2021-09-10 07:15:35 +02001701 /* sibling finished */
1702 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001703 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001704 } else {
aPiecek6828a312021-09-17 15:53:18 +02001705 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001706 }
1707
1708 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001709 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001710
1711cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001712 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001713
Radek Krejci1798aae2020-07-14 13:26:06 +02001714 ly_in_free(lybctx->in, 0);
1715 lylyb_ctx_free(lybctx);
1716
Michal Vasko63f3d842020-07-08 10:10:14 +02001717 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001718}