blob: d84bb1f3486771f4e4156289d666828d292431ab [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/**
75 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
76 *
77 * @param[in] buf Destination buffer.
78 * @param[in] count Number of bytes to read.
79 * @param[in] lybctx LYB context.
80 */
81static void
Radek Krejci1798aae2020-07-14 13:26:06 +020082lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020083{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020084 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +020085 struct lyd_lyb_sibling *empty;
Michal Vasko60ea6352020-06-29 13:39:39 +020086 size_t to_read;
87 uint8_t meta_buf[LYB_META_BYTES];
88
89 assert(lybctx);
90
91 while (1) {
92 /* check for fully-read (empty) data chunks */
93 to_read = count;
94 empty = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +020095 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +020096 /* we want the innermost chunks resolved first, so replace previous empty chunks,
97 * also ignore chunks that are completely finished, there is nothing for us to do */
aPiecek570d7ed2021-09-10 07:15:35 +020098 if ((lybctx->siblings[u].written <= to_read) && lybctx->siblings[u].position) {
Michal Vasko60ea6352020-06-29 13:39:39 +020099 /* empty chunk, do not read more */
aPiecek570d7ed2021-09-10 07:15:35 +0200100 to_read = lybctx->siblings[u].written;
101 empty = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200102 }
103 }
104
105 if (!empty && !count) {
106 break;
107 }
108
109 /* we are actually reading some data, not just finishing another chunk */
110 if (to_read) {
111 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200112 ly_in_read(lybctx->in, buf, to_read);
113 } else {
114 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200115 }
116
aPiecek570d7ed2021-09-10 07:15:35 +0200117 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200118 /* decrease all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200119 lybctx->siblings[u].written -= to_read;
120 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200121 }
122 /* decrease count/buf */
123 count -= to_read;
124 if (buf) {
125 buf += to_read;
126 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200127 }
128
129 if (empty) {
130 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200131 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200132 empty->written = meta_buf[0];
133 empty->inner_chunks = meta_buf[1];
134
135 /* remember whether there is a following chunk or not */
136 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200137 }
138 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200139}
140
141/**
142 * @brief Read a number.
143 *
144 * @param[in] num Destination buffer.
145 * @param[in] num_size Size of @p num.
146 * @param[in] bytes Number of bytes to read.
147 * @param[in] lybctx LYB context.
148 */
149static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200150lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200151{
152 uint64_t buf = 0;
153
154 lyb_read((uint8_t *)&buf, bytes, lybctx);
155
156 /* correct byte order */
157 buf = le64toh(buf);
158
159 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100160 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200161 *((uint8_t *)num) = buf;
162 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100163 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200164 *((uint16_t *)num) = buf;
165 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100166 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200167 *((uint32_t *)num) = buf;
168 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100169 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200170 *((uint64_t *)num) = buf;
171 break;
172 default:
173 LOGINT(lybctx->ctx);
174 }
175}
176
177/**
178 * @brief Read a string.
179 *
180 * @param[in] str Destination buffer, is allocated.
aPiecek570d7ed2021-09-10 07:15:35 +0200181 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this "sibling".
Michal Vasko60ea6352020-06-29 13:39:39 +0200182 * @param[in] lybctx LYB context.
183 * @return LY_ERR value.
184 */
185static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200186lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200187{
Radek Krejci857189e2020-09-01 13:26:36 +0200188 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200189 size_t len = 0, cur_len;
190
191 *str = NULL;
192
193 if (with_length) {
194 lyb_read_number(&len, sizeof len, 2, lybctx);
195 } else {
aPiecek570d7ed2021-09-10 07:15:35 +0200196 /* read until the end of this "sibling" */
197 len = LYB_LAST_SIBLING(lybctx).written;
198 if (LYB_LAST_SIBLING(lybctx).position) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200199 next_chunk = 1;
200 }
201 }
202
203 *str = malloc((len + 1) * sizeof **str);
204 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
205
206 lyb_read((uint8_t *)*str, len, lybctx);
207
208 while (next_chunk) {
aPiecek570d7ed2021-09-10 07:15:35 +0200209 cur_len = LYB_LAST_SIBLING(lybctx).written;
210 if (LYB_LAST_SIBLING(lybctx).position) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200211 next_chunk = 1;
212 } else {
213 next_chunk = 0;
214 }
215
216 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
217 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
218
219 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
220
221 len += cur_len;
222 }
223
Michal Vaskocebbae52021-05-31 11:11:36 +0200224 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200225 return LY_SUCCESS;
226}
227
228/**
aPiecek91eec232021-09-09 15:42:37 +0200229 * @brief Read value of term node.
aPiecekea304e32021-08-18 09:13:47 +0200230 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200231 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200232 * @param[out] term_value Set to term node value in dynamically
233 * allocated memory. The caller must release it.
234 * @param[out] term_value_len Value length in bytes. The zero byte is
235 * always included and is not counted.
236 * @param[in,out] lybctx LYB context.
237 * @return LY_ERR value.
238 */
239static LY_ERR
aPiecek91eec232021-09-09 15:42:37 +0200240lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint32_t *term_value_len,
241 struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200242{
243 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200244 int32_t lyb_data_len;
245 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200246
aPiecekaa5b70a2021-08-30 08:33:25 +0200247 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200248
aPiecekaa5b70a2021-08-30 08:33:25 +0200249 /* Find out the size from @ref howtoDataLYB. */
250 if (term->type->basetype == LY_TYPE_LEAFREF) {
251 /* Leafref itself is ignored, the target is loaded directly. */
252 type_lf = (struct lysc_type_leafref *)term->type;
253 lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
254 } else {
255 lyb_data_len = term->type->plugin->lyb_data_len;
256 }
257
258 if (lyb_data_len < 0) {
259 /* Parse value size. */
260 lyb_read_number(term_value_len, sizeof *term_value_len,
261 sizeof *term_value_len, lybctx);
262 } else {
263 /* Data size is fixed. */
264 *term_value_len = lyb_data_len;
265 }
aPiecekea304e32021-08-18 09:13:47 +0200266
267 /* Allocate memory. */
268 allocated_size = *term_value_len + 1;
269 *term_value = malloc(allocated_size * sizeof **term_value);
270 LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM);
271
272 if (*term_value_len > 0) {
273 /* Parse value. */
274 lyb_read(*term_value, *term_value_len, lybctx);
275 }
276
277 /* Add extra zero byte regardless of whether it is string or not. */
278 (*term_value)[allocated_size - 1] = 0;
279
280 return LY_SUCCESS;
281}
282
283/**
aPiecek570d7ed2021-09-10 07:15:35 +0200284 * @brief Stop the current "siblings" - change LYB context state.
Michal Vasko60ea6352020-06-29 13:39:39 +0200285 *
286 * @param[in] lybctx LYB context.
287 * @return LY_ERR value.
288 */
289static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200290lyb_read_stop_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200291{
aPiecek570d7ed2021-09-10 07:15:35 +0200292 if (LYB_LAST_SIBLING(lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200293 LOGINT_RET(lybctx->ctx);
294 }
295
aPiecek570d7ed2021-09-10 07:15:35 +0200296 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200297 return LY_SUCCESS;
298}
299
300/**
aPiecek570d7ed2021-09-10 07:15:35 +0200301 * @brief Start a new "siblings" - change LYB context state but also read the expected metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200302 *
303 * @param[in] lybctx LYB context.
304 * @return LY_ERR value.
305 */
306static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200307lyb_read_start_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200308{
309 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200310 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200311
aPiecek570d7ed2021-09-10 07:15:35 +0200312 u = LY_ARRAY_COUNT(lybctx->siblings);
313 if (u == lybctx->sibling_size) {
314 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
315 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200316 }
317
Michal Vasko63f3d842020-07-08 10:10:14 +0200318 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200319
aPiecek570d7ed2021-09-10 07:15:35 +0200320 LY_ARRAY_INCREMENT(lybctx->siblings);
321 LYB_LAST_SIBLING(lybctx).written = meta_buf[0];
322 LYB_LAST_SIBLING(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
323 LYB_LAST_SIBLING(lybctx).position = (LYB_LAST_SIBLING(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200324
Michal Vasko60ea6352020-06-29 13:39:39 +0200325 return LY_SUCCESS;
326}
327
328/**
329 * @brief Parse YANG model info.
330 *
331 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +0200332 * @param[in] parse_options Flag with options for parsing.
aPiecek339bdc32021-09-10 08:42:36 +0200333 * @param[out] model Parsed module.
Michal Vasko60ea6352020-06-29 13:39:39 +0200334 * @return LY_ERR value.
335 */
336static LY_ERR
aPiecek339bdc32021-09-10 08:42:36 +0200337lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **model)
Michal Vasko60ea6352020-06-29 13:39:39 +0200338{
339 LY_ERR ret = LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +0200340 const struct lys_module *mod = NULL;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek570d7ed2021-09-10 07:15:35 +0200342 uint16_t rev, length;
Michal Vasko60ea6352020-06-29 13:39:39 +0200343
aPiecek570d7ed2021-09-10 07:15:35 +0200344 lyb_read_number(&length, 2, 2, lybctx);
aPiecek339bdc32021-09-10 08:42:36 +0200345
aPiecek570d7ed2021-09-10 07:15:35 +0200346 if (length) {
347 mod_name = malloc((length + 1) * sizeof *mod_name);
348 LY_CHECK_ERR_RET(!mod_name, LOGMEM(lybctx->ctx), LY_EMEM);
349 lyb_read(((uint8_t *)mod_name), length, lybctx);
350 mod_name[length] = '\0';
351 } else {
352 goto cleanup;
353 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200354
355 /* revision */
356 lyb_read_number(&rev, sizeof rev, 2, lybctx);
357
Michal Vasko60ea6352020-06-29 13:39:39 +0200358 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100359 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
360 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
aPiecek339bdc32021-09-10 08:42:36 +0200361 mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
362 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !mod) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200363 /* try to use an updated module */
aPiecek339bdc32021-09-10 08:42:36 +0200364 mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
365 if (mod && (!mod->revision || (strcmp(mod->revision, mod_rev) < 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200366 /* not an implemented module in a newer revision */
aPiecek339bdc32021-09-10 08:42:36 +0200367 mod = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200368 }
369 }
370 } else {
aPiecek339bdc32021-09-10 08:42:36 +0200371 mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200372 }
373 /* TODO data_clb supported?
374 if (lybctx->ctx->data_clb) {
375 if (!*mod) {
376 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
377 } else if (!(*mod)->implemented) {
378 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
379 }
380 }*/
381
aPiecek339bdc32021-09-10 08:42:36 +0200382 if (!mod || !mod->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200383 if (parse_options & LYD_PARSE_STRICT) {
aPiecek339bdc32021-09-10 08:42:36 +0200384 if (!mod) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200385 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200386 mod_name, rev ? "@" : "", rev ? mod_rev : "");
aPiecek339bdc32021-09-10 08:42:36 +0200387 } else if (!mod->implemented) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200388 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200389 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200390 }
391 ret = LY_EINVAL;
392 goto cleanup;
393 }
394
395 }
396
aPiecek339bdc32021-09-10 08:42:36 +0200397 if (mod) {
Michal Vasko85d9edc2021-04-22 09:15:05 +0200398 /* fill cached hashes, if not already */
aPiecek339bdc32021-09-10 08:42:36 +0200399 lyb_cache_module_hash(mod);
Michal Vasko85d9edc2021-04-22 09:15:05 +0200400 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200401
Michal Vasko60ea6352020-06-29 13:39:39 +0200402cleanup:
aPiecek570d7ed2021-09-10 07:15:35 +0200403 *model = mod;
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 free(mod_name);
405 return ret;
406}
407
408/**
409 * @brief Parse YANG node metadata.
410 *
411 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200412 * @param[out] meta Parsed metadata.
413 * @return LY_ERR value.
414 */
415static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200416lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200417{
418 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200419 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200421 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200422 const struct lys_module *mod;
423
424 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200425 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200426
427 /* read attributes */
428 for (i = 0; i < count; ++i) {
aPiecek570d7ed2021-09-10 07:15:35 +0200429 ret = lyb_read_start_siblings(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200430 LY_CHECK_GOTO(ret, cleanup);
431
432 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100433 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200434 LY_CHECK_GOTO(ret, cleanup);
435
436 if (!mod) {
437 /* skip it */
438 do {
aPiecek570d7ed2021-09-10 07:15:35 +0200439 lyb_read(NULL, LYB_LAST_SIBLING(lybctx->lybctx).written, lybctx->lybctx);
440 } while (LYB_LAST_SIBLING(lybctx->lybctx).written);
441 goto stop_sibling;
Michal Vasko60ea6352020-06-29 13:39:39 +0200442 }
443
444 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200445 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200446 LY_CHECK_GOTO(ret, cleanup);
447
448 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200449 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200450 LY_CHECK_GOTO(ret, cleanup);
451 dynamic = 1;
452
453 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200454 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 +0200455 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200456
457 /* free strings */
458 free(meta_name);
459 meta_name = NULL;
460 if (dynamic) {
461 free(meta_value);
462 dynamic = 0;
463 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200464
Radek Krejci1798aae2020-07-14 13:26:06 +0200465 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200466
aPiecek570d7ed2021-09-10 07:15:35 +0200467stop_sibling:
468 ret = lyb_read_stop_siblings(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200469 LY_CHECK_GOTO(ret, cleanup);
470 }
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);
501 if (!count) {
502 return LY_SUCCESS;
503 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200504
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100505 /* read all NS elements */
506 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200507
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100508 for (i = 0; i < count; ++i) {
509 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200510
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100511 /* prefix */
512 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
513
514 /* namespace */
515 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
516
517 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
518 ns = NULL;
519 }
520
521 *prefix_data = set;
522 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200523 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200524 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100525 /* nothing stored */
526 break;
527 default:
528 LOGINT(lybctx->ctx);
529 ret = LY_EINT;
530 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200531 }
532
533cleanup:
534 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100535 ly_free_prefix_data(format, set);
536 if (ns) {
537 free(ns->prefix);
538 free(ns->uri);
539 free(ns);
540 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200541 }
542 return ret;
543}
544
545/**
546 * @brief Parse opaque attributes.
547 *
548 * @param[in] lybctx LYB context.
549 * @param[out] attr Parsed attributes.
550 * @return LY_ERR value.
551 */
552static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200553lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200554{
555 LY_ERR ret = LY_SUCCESS;
556 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200557 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200558 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200559 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200560 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100561 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200562
563 /* read count */
564 lyb_read(&count, 1, lybctx);
565
566 /* read attributes */
567 for (i = 0; i < count; ++i) {
aPiecek570d7ed2021-09-10 07:15:35 +0200568 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200569 LY_CHECK_GOTO(ret, cleanup);
570
Michal Vasko0fdcd242020-11-11 19:12:30 +0100571 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200572 ret = lyb_read_string(&prefix, 1, lybctx);
573 LY_CHECK_GOTO(ret, cleanup);
574 if (!prefix[0]) {
575 free(prefix);
576 prefix = NULL;
577 }
578
579 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200580 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200581 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200582 if (!module_name[0]) {
583 free(module_name);
584 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200585 }
586
587 /* name */
588 ret = lyb_read_string(&name, 1, lybctx);
589 LY_CHECK_GOTO(ret, cleanup);
590
Michal Vasko60ea6352020-06-29 13:39:39 +0200591 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200592 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200593
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100594 /* value prefixes */
595 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
596 LY_CHECK_GOTO(ret, cleanup);
597
Michal Vasko60ea6352020-06-29 13:39:39 +0200598 /* value */
599 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100600 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200601 dynamic = 1;
602
603 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100604 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200605 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200606 LY_CHECK_GOTO(ret, cleanup);
607
608 free(prefix);
609 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200610 free(module_name);
611 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200612 free(name);
613 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200614 assert(!dynamic);
615 value = NULL;
616
617 if (!*attr) {
618 *attr = attr2;
619 }
620
aPiecek570d7ed2021-09-10 07:15:35 +0200621 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200622 LY_CHECK_GOTO(ret, cleanup);
623 }
624
625cleanup:
626 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200627 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200628 free(name);
629 if (dynamic) {
630 free(value);
631 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200632 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200633 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200634 *attr = NULL;
635 }
636 return ret;
637}
638
639/**
aPiecek619350d2021-09-09 16:06:59 +0200640 * @brief Fill @p hash with hash values.
641 *
642 * @param[in] lybctx LYB context.
643 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
644 * @param[out] hash_count Number of hashes in @p hash.
645 * @return LY_ERR value.
646 */
647static LY_ERR
648lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
649{
650 uint8_t i = 0, j;
651
652 /* read the first hash */
653 lyb_read(&hash[0], sizeof *hash, lybctx);
654
655 if (!hash[0]) {
656 *hash_count = i + 1;
657 return LY_SUCCESS;
658 }
659
660 /* based on the first hash read all the other ones, if any */
661 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
662 if (i > LYB_HASH_BITS) {
663 LOGINT_RET(lybctx->ctx);
664 }
665 }
666
667 /* move the first hash on its accurate position */
668 hash[i] = hash[0];
669
670 /* read the rest of hashes */
671 for (j = i; j; --j) {
672 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
673
674 /* correct collision ID */
675 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
676 /* preceded with zeros */
677 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
678 }
679
680 *hash_count = i + 1;
681
682 return LY_SUCCESS;
683}
684
685/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200686 * @brief Check whether a schema node matches a hash(es).
687 *
688 * @param[in] sibling Schema node to check.
689 * @param[in] hash Hash array to check.
690 * @param[in] hash_count Number of hashes in @p hash.
691 * @return non-zero if matches,
692 * @return 0 if not.
693 */
694static int
695lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
696{
697 LYB_HASH sibling_hash;
698 uint8_t i;
699
700 /* compare all the hashes starting from collision ID 0 */
701 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200702 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200703 if (sibling_hash != hash[i]) {
704 return 0;
705 }
706 }
707
708 return 1;
709}
710
711/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200712 * @brief Parse schema node hash.
713 *
714 * @param[in] lybctx LYB context.
715 * @param[in] sparent Schema parent, must be set if @p mod is not.
716 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
717 * @param[out] snode Parsed found schema node, may be NULL if opaque.
718 * @return LY_ERR value.
719 */
720static LY_ERR
721lyb_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 +0200722 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200723{
724 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200725 const struct lysc_node *sibling;
726 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200727 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200728 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200729
aPiecek570d7ed2021-09-10 07:15:35 +0200730 *snode = NULL;
731
aPiecek619350d2021-09-09 16:06:59 +0200732 ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
733 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200734
735 if (!hash[0]) {
736 /* opaque node */
737 return LY_SUCCESS;
738 }
739
aPiecek619350d2021-09-09 16:06:59 +0200740 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200741
742 /* find our node with matching hashes */
743 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100744 while (1) {
745 if (!sparent && lybctx->ext) {
746 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
747 } else {
748 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
749 }
750 if (!sibling) {
751 break;
752 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200753 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200754 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
aPiecek619350d2021-09-09 16:06:59 +0200755 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200756 /* match found */
757 break;
758 }
759 }
760
Michal Vaskoe0665742021-02-11 11:08:44 +0100761 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100762 if (lybctx->ext) {
763 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
764 lybctx->ext->def->name);
765 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100766 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200767 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200768 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100769 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200770 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200771 }
772 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200773 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200774 return ret;
775 }
776
777 *snode = sibling;
778 return LY_SUCCESS;
779}
780
781/**
aPiecek570d7ed2021-09-10 07:15:35 +0200782 * @brief Read until the end of the current siblings.
Michal Vasko60ea6352020-06-29 13:39:39 +0200783 *
784 * @param[in] lybctx LYB context.
785 */
786static void
aPiecek570d7ed2021-09-10 07:15:35 +0200787lyb_skip_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200788{
Michal Vasko60ea6352020-06-29 13:39:39 +0200789 do {
790 /* first skip any meta information inside */
aPiecek570d7ed2021-09-10 07:15:35 +0200791 ly_in_skip(lybctx->in, LYB_LAST_SIBLING(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200792
793 /* then read data */
aPiecek570d7ed2021-09-10 07:15:35 +0200794 lyb_read(NULL, LYB_LAST_SIBLING(lybctx).written, lybctx);
795 } while (LYB_LAST_SIBLING(lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200796}
797
798/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200799 * @brief Parse the context of anydata/anyxml node.
800 *
801 * @param[in] ctx libyang context.
802 * @param[in] data LYB data to parse.
803 * @param[out] tree Parsed tree.
804 * @return LY_ERR value.
805 */
806static LY_ERR
807lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
808{
809 LY_ERR ret;
810 uint32_t prev_lo;
811 struct ly_in *in;
812 struct lyd_ctx *lydctx = NULL;
813
814 *tree = NULL;
815
816 LY_CHECK_RET(ly_in_new_memory(data, &in));
817
818 /* turn logging off */
819 prev_lo = ly_log_options(0);
820
Michal Vasko56d88602021-07-15 16:37:59 +0200821 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 +0200822 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
823
824 /* turn logging on again */
825 ly_log_options(prev_lo);
826
827 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200828 if (lydctx) {
829 lydctx->free(lydctx);
830 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200831 if (ret) {
832 lyd_free_siblings(*tree);
833 *tree = NULL;
834 }
835 return ret;
836}
837
838/**
aPiecek37c493b2021-09-09 12:52:30 +0200839 * @brief Insert new node to @p parsed set.
840 *
841 * Also if needed, correct @p first_p.
842 *
843 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200844 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200845 * @param[in,out] node Parsed node to insertion.
846 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
847 * @param[out] parsed Set of all successfully parsed nodes.
848 * @return LY_ERR value.
849 */
850static void
851lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
852 struct ly_set *parsed)
853{
854 /* insert, keep first pointer correct */
855 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
856 while (!parent && (*first_p)->prev->next) {
857 *first_p = (*first_p)->prev;
858 }
859
860 /* rememeber a successfully parsed node */
861 if (parsed) {
862 ly_set_add(parsed, node, 1, NULL);
863 }
864}
865
866/**
867 * @brief Finish parsing the opaq node.
868 *
869 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200870 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200871 * @param[in] flags Node flags to set.
872 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
873 * @param[in,out] node Parsed opaq node to finish.
874 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
875 * @param[out] parsed Set of all successfully parsed nodes.
876 * @return LY_ERR value.
877 */
878static void
879lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
880 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
881{
882 struct lyd_attr *iter;
883
884 /* set flags */
885 (*node)->flags = flags;
886
887 /* add attributes */
888 assert(!(*node)->schema);
889 LY_LIST_FOR(*attr, iter) {
890 iter->parent = (struct lyd_node_opaq *)*node;
891 }
892 ((struct lyd_node_opaq *)*node)->attr = *attr;
893 *attr = NULL;
894
895 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
896 *node = NULL;
897}
898
899/**
900 * @brief Finish parsing the node.
901 *
902 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200903 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200904 * @param[in] flags Node flags to set.
905 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
906 * @param[in,out] node Parsed node to finish.
907 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
908 * @param[out] parsed Set of all successfully parsed nodes.
909 * @return LY_ERR value.
910 */
911static void
912lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
913 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
914{
915 struct lyd_meta *m;
916
917 /* set flags */
918 (*node)->flags = flags;
919
920 /* add metadata */
921 LY_LIST_FOR(*meta, m) {
922 m->parent = *node;
923 }
924 (*node)->meta = *meta;
925 *meta = NULL;
926
927 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
928 *node = NULL;
929}
930
931/**
aPiecek6fdac2f2021-09-10 11:21:10 +0200932 * @brief Parse header for non-opaq node.
933 *
934 * @param[in] lybctx LYB context.
935 * @param[out] flags Parsed node flags.
936 * @param[out] meta Parsed metadata of the node.
937 * @return LY_ERR value.
938 */
939static LY_ERR
940lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta)
941{
942 LY_ERR ret;
943
944 /* create and read metadata */
945 ret = lyb_parse_metadata(lybctx, meta);
946 LY_CHECK_RET(ret);
947
948 /* read flags */
949 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
950
951 return ret;
952}
953
954/**
aPiecek33fc6b02021-09-09 15:45:37 +0200955 * @brief Create term node and fill it with value.
956 *
957 * @param[in] lybctx LYB context.
958 * @param[in] snode Schema of the term node.
959 * @param[out] node Created term node.
960 * @return LY_ERR value.
961 */
962static LY_ERR
963lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
964{
965 LY_ERR ret;
966 ly_bool dynamic;
967 uint8_t *term_value;
968 uint32_t term_value_len;
969
970 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
971 LY_CHECK_RET(ret);
972
973 dynamic = 1;
974 /* create node */
975 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
976 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
977 NULL, LYD_HINT_DATA, node);
978 if (dynamic) {
979 free(term_value);
980 }
981 if (ret) {
982 lyd_free_tree(*node);
983 *node = NULL;
984 }
985
986 return ret;
987}
988
989/**
aPiecek0e2e1052021-09-09 15:48:27 +0200990 * @brief Validate inner node, autodelete default values nad create implicit nodes.
991 *
992 * @param[in,out] lybctx LYB context.
993 * @param[in] snode Schema of the inner node.
994 * @param[in] node Parsed inner node.
995 * @return LY_ERR value.
996 */
997static LY_ERR
998lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
999{
1000 LY_ERR ret = LY_SUCCESS;
1001 uint32_t impl_opts;
1002
1003 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1004 /* new node validation, autodelete CANNOT occur, all nodes are new */
1005 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
1006 LY_CHECK_RET(ret);
1007
1008 /* add any missing default children */
1009 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
1010 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL,
1011 NULL, &lybctx->node_when, &lybctx->node_exts,
1012 &lybctx->node_types, impl_opts, NULL);
1013 LY_CHECK_RET(ret);
1014 }
1015
1016 return ret;
1017}
1018
1019/**
aPiecek821cf732021-09-09 15:37:28 +02001020 * @brief Parse opaq node.
1021 *
1022 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001023 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001024 * @param[in,out] first_p First top-level sibling.
1025 * @param[out] parsed Set of all successfully parsed nodes.
1026 * @return LY_ERR value.
1027 */
1028static LY_ERR
1029lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1030{
1031 LY_ERR ret;
1032 struct lyd_node *node = NULL;
1033 struct lyd_attr *attr = NULL;
1034 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1035 ly_bool dynamic = 0;
1036 LY_VALUE_FORMAT format = 0;
1037 void *val_prefix_data = NULL;
1038 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1039 uint32_t flags;
1040
aPiecek821cf732021-09-09 15:37:28 +02001041 /* parse opaq node attributes */
1042 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1043 LY_CHECK_GOTO(ret, cleanup);
1044
1045 /* read flags */
1046 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1047
1048 /* parse prefix */
1049 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
1050 LY_CHECK_GOTO(ret, cleanup);
1051
1052 /* parse module key */
1053 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
1054 LY_CHECK_GOTO(ret, cleanup);
1055
1056 /* parse name */
1057 ret = lyb_read_string(&name, 1, lybctx->lybctx);
1058 LY_CHECK_GOTO(ret, cleanup);
1059
1060 /* parse value */
1061 ret = lyb_read_string(&value, 1, lybctx->lybctx);
1062 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1063 dynamic = 1;
1064
1065 /* parse format */
1066 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1067
1068 /* parse value prefixes */
1069 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1070 LY_CHECK_GOTO(ret, cleanup);
1071
aPiecek570d7ed2021-09-10 07:15:35 +02001072 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
1073 if (lybctx->lybctx->in->current[0] == 0) {
1074 /* opaq node has no children */
1075 lyb_read(NULL, 1, lybctx->lybctx);
1076 } else {
1077 /* skip children */
1078 ret = lyb_read_start_siblings(lybctx->lybctx);
1079 LY_CHECK_RET(ret);
1080 lyb_skip_siblings(lybctx->lybctx);
1081 ret = lyb_read_stop_siblings(lybctx->lybctx);
1082 LY_CHECK_RET(ret);
1083 }
1084
1085 goto cleanup;
1086 }
1087
aPiecek821cf732021-09-09 15:37:28 +02001088 /* create node */
1089 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1090 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1091 LY_CHECK_GOTO(ret, cleanup);
1092
1093 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001094 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1095 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001096
1097 /* register parsed opaq node */
1098 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1099 assert(!attr && !node);
1100
1101cleanup:
1102 free(prefix);
1103 free(module_key);
1104 free(name);
1105 if (dynamic) {
1106 free(value);
1107 }
1108 lyd_free_attr_siblings(ctx, attr);
1109 lyd_free_tree(node);
1110
1111 return ret;
1112}
1113
aPiecek18457d72021-09-09 15:52:20 +02001114/**
1115 * @brief Parse anydata or anyxml node.
1116 *
1117 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001118 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001119 * @param[in] snode Schema of the node to be parsed.
1120 * @param[in,out] first_p First top-level sibling.
1121 * @param[out] parsed Set of all successfully parsed nodes.
1122 * @return LY_ERR value.
1123 */
1124static LY_ERR
1125lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1126 struct lyd_node **first_p, struct ly_set *parsed)
1127{
1128 LY_ERR ret;
1129 struct lyd_node *node = NULL, *tree;
1130 struct lyd_meta *meta = NULL;
1131 LYD_ANYDATA_VALUETYPE value_type;
1132 char *value = NULL;
1133 const char *val_dict;
1134 uint32_t flags;
1135 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1136
1137 /* read necessary basic data */
aPiecek570d7ed2021-09-10 07:15:35 +02001138 lyb_parse_node_header(lybctx, &flags, &meta);
aPiecek18457d72021-09-09 15:52:20 +02001139
1140 /* parse value type */
1141 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1142 if (value_type == LYD_ANYDATA_DATATREE) {
1143 /* invalid situation */
1144 LOGINT(ctx);
1145 ret = LY_EINT;
1146 goto error;
1147 }
1148
1149 /* read anydata content */
aPiecek570d7ed2021-09-10 07:15:35 +02001150 ret = lyb_read_string(&value, 1, lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001151 LY_CHECK_GOTO(ret, error);
1152
1153 if (value_type == LYD_ANYDATA_LYB) {
1154 /* try to parse LYB into a data tree */
1155 if (!lyb_parse_any_content(ctx, value, &tree)) {
1156 /* successfully parsed */
1157 free(value);
1158 value = (char *)tree;
1159 value_type = LYD_ANYDATA_DATATREE;
1160 }
1161 }
1162
1163 /* create the node */
1164 switch (value_type) {
1165 case LYD_ANYDATA_LYB:
1166 case LYD_ANYDATA_DATATREE:
1167 /* use the value directly */
1168 ret = lyd_create_any(snode, value, value_type, 1, &node);
1169 LY_CHECK_GOTO(ret, error);
1170
1171 break;
1172 case LYD_ANYDATA_STRING:
1173 case LYD_ANYDATA_XML:
1174 case LYD_ANYDATA_JSON:
1175 /* value is expected to be in the dictionary */
1176 ret = lydict_insert_zc(ctx, value, &val_dict);
1177 LY_CHECK_GOTO(ret, error);
1178
1179 /* use the value in the dictionary */
1180 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
1181 if (ret) {
1182 lydict_remove(ctx, val_dict);
1183 goto error;
1184 }
1185 break;
1186 default:
1187 LOGINT(ctx);
1188 ret = LY_EINT;
1189 goto error;
1190 }
1191
1192 /* register parsed anydata node */
1193 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1194
1195 return LY_SUCCESS;
1196
1197error:
1198 free(value);
1199 lyd_free_meta_siblings(meta);
1200 lyd_free_tree(node);
1201 return ret;
1202}
aPiecek821cf732021-09-09 15:37:28 +02001203
1204/**
aPiecek5777f122021-09-10 10:26:23 +02001205 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001206 *
1207 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001208 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1209 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001210 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1211 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001212 * @return LY_ERR value.
1213 */
1214static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001215lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001216 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001217{
1218 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001219 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001220 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001221 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001222
aPiecek307f0772021-09-10 09:09:47 +02001223 /* read necessary basic data */
1224 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1225 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001226
aPiecek5777f122021-09-10 10:26:23 +02001227 /* create node */
1228 ret = lyd_create_inner(snode, &node);
1229 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001230
aPiecek5777f122021-09-10 10:26:23 +02001231 /* process children */
1232 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1233 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001234
aPiecek5777f122021-09-10 10:26:23 +02001235 /* additional procedure for inner node */
1236 ret = lyb_validate_node_inner(lybctx, snode, node);
1237 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001238
aPiecek5777f122021-09-10 10:26:23 +02001239 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1240 /* rememeber the RPC/action/notification */
1241 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001242 }
1243
aPiecek307f0772021-09-10 09:09:47 +02001244 /* register parsed node */
1245 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1246
1247 return LY_SUCCESS;
1248
1249error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001250 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001251 lyd_free_tree(node);
1252 return ret;
1253}
1254
1255/**
aPiecek8a555d72021-09-10 10:15:26 +02001256 * @brief Parse leaf node.
1257 *
1258 * @param[in] lybctx LYB context.
1259 * @param[in] parent Data parent of the sibling.
1260 * @param[in] snode Schema of the node to be parsed.
1261 * @param[in,out] first_p First top-level sibling.
1262 * @param[out] parsed Set of all successfully parsed nodes.
1263 * @return LY_ERR value.
1264 */
1265static LY_ERR
1266lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1267 struct lyd_node **first_p, struct ly_set *parsed)
1268{
1269 LY_ERR ret;
1270 struct lyd_node *node = NULL;
1271 struct lyd_meta *meta = NULL;
1272 uint32_t flags;
1273
1274 /* read necessary basic data */
1275 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1276 LY_CHECK_GOTO(ret, error);
1277
1278 /* read value of term node and create it */
1279 ret = lyb_create_term(lybctx, snode, &node);
1280 LY_CHECK_GOTO(ret, error);
1281
1282 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1283
1284 return LY_SUCCESS;
1285
1286error:
1287 lyd_free_meta_siblings(meta);
1288 lyd_free_tree(node);
1289 return ret;
1290}
1291
1292/**
aPiecek5777f122021-09-10 10:26:23 +02001293 * @brief Parse all leaflist nodes which belong to same schema.
1294 *
1295 * @param[in] lybctx LYB context.
1296 * @param[in] parent Data parent of the sibling.
1297 * @param[in] snode Schema of the nodes to be parsed.
1298 * @param[in,out] first_p First top-level sibling.
1299 * @param[out] parsed Set of all successfully parsed nodes.
1300 * @return LY_ERR value.
1301 */
1302static LY_ERR
1303lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1304 struct lyd_node **first_p, struct ly_set *parsed)
1305{
1306 LY_ERR ret;
1307
1308 /* register a new sibling */
1309 ret = lyb_read_start_siblings(lybctx->lybctx);
1310 LY_CHECK_RET(ret);
1311
1312 /* process all siblings */
1313 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1314 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1315 LY_CHECK_RET(ret);
1316 }
1317
1318 /* end the sibling */
1319 ret = lyb_read_stop_siblings(lybctx->lybctx);
1320 LY_CHECK_RET(ret);
1321
1322 return ret;
1323}
1324
1325/**
aPiecek77d3e962021-09-10 10:33:59 +02001326 * @brief Parse all list nodes which belong to same schema.
1327 *
1328 * @param[in] lybctx LYB context.
1329 * @param[in] parent Data parent of the sibling.
1330 * @param[in] snode Schema of the nodes to be parsed.
1331 * @param[in,out] first_p First top-level sibling.
1332 * @param[out] parsed Set of all successfully parsed nodes.
1333 * @return LY_ERR value.
1334 */
1335static LY_ERR
1336lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1337 struct lyd_node **first_p, struct ly_set *parsed)
1338{
1339 LY_ERR ret;
1340 struct lyd_node *node = NULL;
1341 struct lyd_meta *meta = NULL;
1342 uint32_t flags;
1343
1344 /* register a new sibling */
1345 ret = lyb_read_start_siblings(lybctx->lybctx);
1346 LY_CHECK_RET(ret);
1347
1348 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1349 /* read necessary basic data */
1350 lyb_parse_node_header(lybctx, &flags, &meta);
1351
1352 /* create list node */
1353 ret = lyd_create_inner(snode, &node);
1354 LY_CHECK_GOTO(ret, error);
1355
1356 /* process children */
1357 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1358 LY_CHECK_GOTO(ret, error);
1359
1360 /* additional procedure for inner node */
1361 ret = lyb_validate_node_inner(lybctx, snode, node);
1362 LY_CHECK_GOTO(ret, error);
1363
1364 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1365 /* rememeber the RPC/action/notification */
1366 lybctx->op_node = node;
1367 }
1368
1369 /* register parsed list node */
1370 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1371 }
1372
1373 /* end the sibling */
1374 ret = lyb_read_stop_siblings(lybctx->lybctx);
1375 LY_CHECK_RET(ret);
1376
1377 return LY_SUCCESS;
1378
1379error:
1380 lyd_free_meta_siblings(meta);
1381 lyd_free_tree(node);
1382 return ret;
1383}
1384
1385/**
aPiecek570d7ed2021-09-10 07:15:35 +02001386 * @brief Parse siblings (@ref lyb_print_siblings()).
1387 *
1388 * @param[in] lybctx LYB context.
1389 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1390 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1391 * @param[out] parsed Set of all successfully parsed nodes.
1392 * @return LY_ERR value.
1393 */
1394static LY_ERR
1395lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1396 struct ly_set *parsed)
1397{
1398 LY_ERR ret;
1399 const struct lysc_node *snode;
1400 const struct lys_module *mod;
1401 ly_bool top_level;
1402
1403 if (lybctx->lybctx->in->current[0] == 0) {
1404 lyb_read(NULL, 1, lybctx->lybctx);
1405 return LY_SUCCESS;
1406 }
1407
1408 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1409
1410 /* register a new siblings */
1411 ret = lyb_read_start_siblings(lybctx->lybctx);
1412 LY_CHECK_RET(ret);
1413
1414 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1415 if (!parent || !parent->schema) {
1416 /* top-level or opaque, read module name */
1417 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
1418 LY_CHECK_RET(ret);
1419
1420 /* read hash, find the schema node starting from mod */
1421 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
1422 } else {
1423 /* read hash, find the schema node starting from parent schema */
1424 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
1425 }
1426 LY_CHECK_RET(ret);
1427
aPiecek307f0772021-09-10 09:09:47 +02001428 if (!snode) {
1429 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
aPiecek5777f122021-09-10 10:26:23 +02001430 } else if (snode->nodetype & LYS_LEAFLIST) {
1431 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
aPiecek77d3e962021-09-10 10:33:59 +02001432 } else if (snode->nodetype == LYS_LIST) {
1433 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
aPiecek307f0772021-09-10 09:09:47 +02001434 } else if (snode->nodetype & LYD_NODE_ANY) {
1435 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
aPiecek5777f122021-09-10 10:26:23 +02001436 } else if (snode->nodetype & LYD_NODE_INNER) {
1437 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
aPiecek307f0772021-09-10 09:09:47 +02001438 } else {
aPiecek5777f122021-09-10 10:26:23 +02001439 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
aPiecek307f0772021-09-10 09:09:47 +02001440 }
aPiecek570d7ed2021-09-10 07:15:35 +02001441 LY_CHECK_RET(ret);
1442
1443 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1444 break;
1445 }
1446 }
1447
1448 /* end the siblings */
1449 ret = lyb_read_stop_siblings(lybctx->lybctx);
1450 LY_CHECK_RET(ret);
1451
1452 return ret;
1453}
1454
1455/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001456 * @brief Parse used YANG data models.
1457 *
1458 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001459 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001460 * @return LY_ERR value.
1461 */
1462static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001463lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001464{
1465 LY_ERR ret;
1466 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001467 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001468
1469 /* read model count */
1470 lyb_read_number(&count, sizeof count, 2, lybctx);
1471
1472 if (count) {
1473 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1474
1475 /* read modules */
1476 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001477 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001478 LY_CHECK_RET(ret);
1479 LY_ARRAY_INCREMENT(lybctx->models);
1480 }
1481 }
1482
1483 return LY_SUCCESS;
1484}
1485
1486/**
1487 * @brief Parse LYB magic number.
1488 *
1489 * @param[in] lybctx LYB context.
1490 * @return LY_ERR value.
1491 */
1492static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001493lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001494{
1495 char magic_byte = 0;
1496
1497 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1498 if (magic_byte != 'l') {
1499 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1500 return LY_EINVAL;
1501 }
1502
1503 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1504 if (magic_byte != 'y') {
1505 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1506 return LY_EINVAL;
1507 }
1508
1509 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1510 if (magic_byte != 'b') {
1511 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1512 return LY_EINVAL;
1513 }
1514
1515 return LY_SUCCESS;
1516}
1517
1518/**
1519 * @brief Parse LYB header.
1520 *
1521 * @param[in] lybctx LYB context.
1522 * @return LY_ERR value.
1523 */
1524static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001525lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001526{
1527 uint8_t byte = 0;
1528
1529 /* version, future flags */
1530 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1531
1532 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1533 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001534 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001535 return LY_EINVAL;
1536 }
1537
1538 return LY_SUCCESS;
1539}
1540
Michal Vasko02ed9d82021-07-15 14:58:04 +02001541static LY_ERR
1542_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1543 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 +01001544 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001545{
Michal Vaskoe0665742021-02-11 11:08:44 +01001546 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001547 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001548
Michal Vaskoe0665742021-02-11 11:08:44 +01001549 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1550 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001551
Radek Krejci1798aae2020-07-14 13:26:06 +02001552 lybctx = calloc(1, sizeof *lybctx);
1553 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1554 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001555 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001556
Radek Krejci1798aae2020-07-14 13:26:06 +02001557 lybctx->lybctx->in = in;
1558 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001559 lybctx->parse_opts = parse_opts;
1560 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001561 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001562 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001563 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001564
1565 /* find the operation node if it exists already */
1566 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1567
Michal Vasko60ea6352020-06-29 13:39:39 +02001568 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001569 rc = lyb_parse_magic_number(lybctx->lybctx);
1570 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001571
1572 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001573 rc = lyb_parse_header(lybctx->lybctx);
1574 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001575
1576 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001577 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1578 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001579
aPiecek570d7ed2021-09-10 07:15:35 +02001580 /* read sibling(s) */
1581 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1582 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001583
1584 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1585 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1586 rc = LY_EVALID;
1587 goto cleanup;
1588 }
1589 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1590 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1591 rc = LY_EVALID;
1592 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001593 }
1594
1595 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001596 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001597
Michal Vasko60ea6352020-06-29 13:39:39 +02001598cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001599 /* there should be no unres stored if validation should be skipped */
1600 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1601 !lybctx->node_when.count));
1602
1603 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001604 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001605 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001606 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001607 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001608 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001609}
1610
Michal Vasko02ed9d82021-07-15 14:58:04 +02001611LY_ERR
1612lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1613 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1614 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
1615{
1616 uint32_t int_opts;
1617
1618 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1619 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1620
1621 switch (data_type) {
1622 case LYD_TYPE_DATA_YANG:
1623 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1624 break;
1625 case LYD_TYPE_RPC_YANG:
1626 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1627 break;
1628 case LYD_TYPE_NOTIF_YANG:
1629 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1630 break;
1631 case LYD_TYPE_REPLY_YANG:
1632 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1633 break;
1634 default:
1635 LOGINT(ctx);
1636 return LY_EINT;
1637 }
1638
1639 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1640}
1641
Michal Vasko60ea6352020-06-29 13:39:39 +02001642API int
1643lyd_lyb_data_length(const char *data)
1644{
1645 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001646 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001647 int count, i;
1648 size_t len;
1649 uint8_t buf[LYB_SIZE_MAX];
1650
1651 if (!data) {
1652 return -1;
1653 }
1654
Radek Krejci1798aae2020-07-14 13:26:06 +02001655 lybctx = calloc(1, sizeof *lybctx);
1656 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1657 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001658 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001659
1660 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001661 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001662 LY_CHECK_GOTO(ret, cleanup);
1663
1664 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001665 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001666 LY_CHECK_GOTO(ret, cleanup);
1667
1668 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001669 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001670
1671 /* read all models */
1672 for (i = 0; i < count; ++i) {
1673 /* module name length */
1674 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001675 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001676
1677 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001678 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001679
1680 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001681 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001682 }
1683
aPiecek570d7ed2021-09-10 07:15:35 +02001684 if (lybctx->in->current[0]) {
1685 /* register a new sibling */
1686 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001687 LY_CHECK_GOTO(ret, cleanup);
1688
1689 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001690 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001691
aPiecek570d7ed2021-09-10 07:15:35 +02001692 /* sibling finished */
1693 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001694 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001695 } else {
1696 lyb_read(NULL, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001697 }
1698
1699 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001700 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001701
1702cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001703 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001704
Radek Krejci1798aae2020-07-14 13:26:06 +02001705 ly_in_free(lybctx->in, 0);
1706 lylyb_ctx_free(lybctx);
1707
Michal Vasko63f3d842020-07-08 10:10:14 +02001708 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001709}