blob: d8c0ed6b750a9a4deda41e365572af25df87d7d3 [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
aPiecek821cf732021-09-09 15:37:28 +020046static LY_ERR lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed);
aPiecek18457d72021-09-09 15:52:20 +020047static LY_ERR lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta);
aPiecek821cf732021-09-09 15:37:28 +020048
Radek Krejci1798aae2020-07-14 13:26:06 +020049void
50lylyb_ctx_free(struct lylyb_ctx *ctx)
51{
52 LY_ARRAY_COUNT_TYPE u;
53
54 LY_ARRAY_FREE(ctx->subtrees);
55 LY_ARRAY_FREE(ctx->models);
56
57 LY_ARRAY_FOR(ctx->sib_hts, u) {
58 lyht_free(ctx->sib_hts[u].ht);
59 }
60 LY_ARRAY_FREE(ctx->sib_hts);
61
62 free(ctx);
63}
64
65void
66lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
67{
68 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
69
70 lyd_ctx_free(lydctx);
71 lylyb_ctx_free(ctx->lybctx);
72 free(ctx);
73}
74
Michal Vasko60ea6352020-06-29 13:39:39 +020075/**
76 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
77 *
78 * @param[in] buf Destination buffer.
79 * @param[in] count Number of bytes to read.
80 * @param[in] lybctx LYB context.
81 */
82static void
Radek Krejci1798aae2020-07-14 13:26:06 +020083lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020084{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020085 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020086 struct lyd_lyb_subtree *empty;
87 size_t to_read;
88 uint8_t meta_buf[LYB_META_BYTES];
89
90 assert(lybctx);
91
92 while (1) {
93 /* check for fully-read (empty) data chunks */
94 to_read = count;
95 empty = NULL;
96 LY_ARRAY_FOR(lybctx->subtrees, u) {
97 /* we want the innermost chunks resolved first, so replace previous empty chunks,
98 * also ignore chunks that are completely finished, there is nothing for us to do */
99 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
100 /* empty chunk, do not read more */
101 to_read = lybctx->subtrees[u].written;
102 empty = &lybctx->subtrees[u];
103 }
104 }
105
106 if (!empty && !count) {
107 break;
108 }
109
110 /* we are actually reading some data, not just finishing another chunk */
111 if (to_read) {
112 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 ly_in_read(lybctx->in, buf, to_read);
114 } else {
115 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200116 }
117
118 LY_ARRAY_FOR(lybctx->subtrees, u) {
119 /* decrease all written counters */
120 lybctx->subtrees[u].written -= to_read;
121 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
122 }
123 /* decrease count/buf */
124 count -= to_read;
125 if (buf) {
126 buf += to_read;
127 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200128 }
129
130 if (empty) {
131 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200133 empty->written = meta_buf[0];
134 empty->inner_chunks = meta_buf[1];
135
136 /* remember whether there is a following chunk or not */
137 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200138 }
139 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200140}
141
142/**
143 * @brief Read a number.
144 *
145 * @param[in] num Destination buffer.
146 * @param[in] num_size Size of @p num.
147 * @param[in] bytes Number of bytes to read.
148 * @param[in] lybctx LYB context.
149 */
150static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200151lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200152{
153 uint64_t buf = 0;
154
155 lyb_read((uint8_t *)&buf, bytes, lybctx);
156
157 /* correct byte order */
158 buf = le64toh(buf);
159
160 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100161 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200162 *((uint8_t *)num) = buf;
163 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100164 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200165 *((uint16_t *)num) = buf;
166 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100167 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200168 *((uint32_t *)num) = buf;
169 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100170 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200171 *((uint64_t *)num) = buf;
172 break;
173 default:
174 LOGINT(lybctx->ctx);
175 }
176}
177
178/**
179 * @brief Read a string.
180 *
181 * @param[in] str Destination buffer, is allocated.
182 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
183 * @param[in] lybctx LYB context.
184 * @return LY_ERR value.
185 */
186static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200187lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200188{
Radek Krejci857189e2020-09-01 13:26:36 +0200189 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200190 size_t len = 0, cur_len;
191
192 *str = NULL;
193
194 if (with_length) {
195 lyb_read_number(&len, sizeof len, 2, lybctx);
196 } else {
197 /* read until the end of this subtree */
198 len = LYB_LAST_SUBTREE(lybctx).written;
199 if (LYB_LAST_SUBTREE(lybctx).position) {
200 next_chunk = 1;
201 }
202 }
203
204 *str = malloc((len + 1) * sizeof **str);
205 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
206
207 lyb_read((uint8_t *)*str, len, lybctx);
208
209 while (next_chunk) {
210 cur_len = LYB_LAST_SUBTREE(lybctx).written;
211 if (LYB_LAST_SUBTREE(lybctx).position) {
212 next_chunk = 1;
213 } else {
214 next_chunk = 0;
215 }
216
217 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
218 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
219
220 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
221
222 len += cur_len;
223 }
224
Michal Vaskocebbae52021-05-31 11:11:36 +0200225 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200226 return LY_SUCCESS;
227}
228
229/**
aPiecek91eec232021-09-09 15:42:37 +0200230 * @brief Read value of term node.
aPiecekea304e32021-08-18 09:13:47 +0200231 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200232 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200233 * @param[out] term_value Set to term node value in dynamically
234 * allocated memory. The caller must release it.
235 * @param[out] term_value_len Value length in bytes. The zero byte is
236 * always included and is not counted.
237 * @param[in,out] lybctx LYB context.
238 * @return LY_ERR value.
239 */
240static LY_ERR
aPiecek91eec232021-09-09 15:42:37 +0200241lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint32_t *term_value_len,
242 struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200243{
244 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200245 int32_t lyb_data_len;
246 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200247
aPiecekaa5b70a2021-08-30 08:33:25 +0200248 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200249
aPiecekaa5b70a2021-08-30 08:33:25 +0200250 /* Find out the size from @ref howtoDataLYB. */
251 if (term->type->basetype == LY_TYPE_LEAFREF) {
252 /* Leafref itself is ignored, the target is loaded directly. */
253 type_lf = (struct lysc_type_leafref *)term->type;
254 lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
255 } else {
256 lyb_data_len = term->type->plugin->lyb_data_len;
257 }
258
259 if (lyb_data_len < 0) {
260 /* Parse value size. */
261 lyb_read_number(term_value_len, sizeof *term_value_len,
262 sizeof *term_value_len, lybctx);
263 } else {
264 /* Data size is fixed. */
265 *term_value_len = lyb_data_len;
266 }
aPiecekea304e32021-08-18 09:13:47 +0200267
268 /* Allocate memory. */
269 allocated_size = *term_value_len + 1;
270 *term_value = malloc(allocated_size * sizeof **term_value);
271 LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM);
272
273 if (*term_value_len > 0) {
274 /* Parse value. */
275 lyb_read(*term_value, *term_value_len, lybctx);
276 }
277
278 /* Add extra zero byte regardless of whether it is string or not. */
279 (*term_value)[allocated_size - 1] = 0;
280
281 return LY_SUCCESS;
282}
283
284/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200285 * @brief Stop the current subtree - change LYB context state.
286 *
287 * @param[in] lybctx LYB context.
288 * @return LY_ERR value.
289 */
290static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200291lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200292{
293 if (LYB_LAST_SUBTREE(lybctx).written) {
294 LOGINT_RET(lybctx->ctx);
295 }
296
297 LY_ARRAY_DECREMENT(lybctx->subtrees);
298 return LY_SUCCESS;
299}
300
301/**
302 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
303 *
304 * @param[in] lybctx LYB context.
305 * @return LY_ERR value.
306 */
307static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200308lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200309{
310 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200311 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200312
Radek Krejcic7d13e32020-12-09 12:32:24 +0100313 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200314 if (u == lybctx->subtree_size) {
315 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
316 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
317 }
318
Michal Vasko63f3d842020-07-08 10:10:14 +0200319 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200320
321 LY_ARRAY_INCREMENT(lybctx->subtrees);
322 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
323 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
324 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
325
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 return LY_SUCCESS;
327}
328
329/**
330 * @brief Parse YANG model info.
331 *
332 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +0200333 * @param[in] parse_options Flag with options for parsing.
aPiecek339bdc32021-09-10 08:42:36 +0200334 * @param[out] model Parsed module.
Michal Vasko60ea6352020-06-29 13:39:39 +0200335 * @return LY_ERR value.
336 */
337static LY_ERR
aPiecek339bdc32021-09-10 08:42:36 +0200338lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **model)
Michal Vasko60ea6352020-06-29 13:39:39 +0200339{
340 LY_ERR ret = LY_SUCCESS;
aPiecek339bdc32021-09-10 08:42:36 +0200341 const struct lys_module *mod;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100342 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200343 uint16_t rev;
344
aPiecek339bdc32021-09-10 08:42:36 +0200345 *model = NULL;
346
Michal Vasko60ea6352020-06-29 13:39:39 +0200347 /* model name */
348 ret = lyb_read_string(&mod_name, 1, lybctx);
349 LY_CHECK_GOTO(ret, cleanup);
350
351 /* revision */
352 lyb_read_number(&rev, sizeof rev, 2, lybctx);
353
354 if (!mod_name[0]) {
355 /* opaq node, no module */
Michal Vasko60ea6352020-06-29 13:39:39 +0200356 goto cleanup;
357 }
358
359 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100360 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
361 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
aPiecek339bdc32021-09-10 08:42:36 +0200362 mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
363 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !mod) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200364 /* try to use an updated module */
aPiecek339bdc32021-09-10 08:42:36 +0200365 mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
366 if (mod && (!mod->revision || (strcmp(mod->revision, mod_rev) < 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200367 /* not an implemented module in a newer revision */
aPiecek339bdc32021-09-10 08:42:36 +0200368 mod = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200369 }
370 }
371 } else {
aPiecek339bdc32021-09-10 08:42:36 +0200372 mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200373 }
374 /* TODO data_clb supported?
375 if (lybctx->ctx->data_clb) {
376 if (!*mod) {
377 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
378 } else if (!(*mod)->implemented) {
379 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
380 }
381 }*/
382
aPiecek339bdc32021-09-10 08:42:36 +0200383 if (!mod || !mod->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200384 if (parse_options & LYD_PARSE_STRICT) {
aPiecek339bdc32021-09-10 08:42:36 +0200385 if (!mod) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200386 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200387 mod_name, rev ? "@" : "", rev ? mod_rev : "");
aPiecek339bdc32021-09-10 08:42:36 +0200388 } else if (!mod->implemented) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200389 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200390 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200391 }
392 ret = LY_EINVAL;
393 goto cleanup;
394 }
395
396 }
397
aPiecek339bdc32021-09-10 08:42:36 +0200398 if (mod) {
Michal Vasko85d9edc2021-04-22 09:15:05 +0200399 /* fill cached hashes, if not already */
aPiecek339bdc32021-09-10 08:42:36 +0200400 lyb_cache_module_hash(mod);
Michal Vasko85d9edc2021-04-22 09:15:05 +0200401 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200402
aPiecek339bdc32021-09-10 08:42:36 +0200403 *model = mod;
404
Michal Vasko60ea6352020-06-29 13:39:39 +0200405cleanup:
406 free(mod_name);
407 return ret;
408}
409
410/**
411 * @brief Parse YANG node metadata.
412 *
413 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200414 * @param[out] meta Parsed metadata.
415 * @return LY_ERR value.
416 */
417static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200418lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200419{
420 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200421 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200422 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200423 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200424 const struct lys_module *mod;
425
426 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200427 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200428
429 /* read attributes */
430 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200431 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200432 LY_CHECK_GOTO(ret, cleanup);
433
434 /* 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) {
439 /* skip it */
440 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200441 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
442 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200443 goto stop_subtree;
444 }
445
446 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200447 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200448 LY_CHECK_GOTO(ret, cleanup);
449
450 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200451 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200452 LY_CHECK_GOTO(ret, cleanup);
453 dynamic = 1;
454
455 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200456 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 +0200457 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200458
459 /* free strings */
460 free(meta_name);
461 meta_name = NULL;
462 if (dynamic) {
463 free(meta_value);
464 dynamic = 0;
465 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200466
Radek Krejci1798aae2020-07-14 13:26:06 +0200467 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200468
469stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200470 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200471 LY_CHECK_GOTO(ret, cleanup);
472 }
473
474cleanup:
475 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200476 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200477 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200478 *meta = NULL;
479 }
480 return ret;
481}
482
483/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100484 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200485 *
486 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100487 * @param[in] format Prefix data format.
488 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200489 * @return LY_ERR value.
490 */
491static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200492lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200493{
494 LY_ERR ret = LY_SUCCESS;
495 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100496 struct ly_set *set = NULL;
497 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200498
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100499 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200500 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100501 /* read count */
502 lyb_read(&count, 1, lybctx);
503 if (!count) {
504 return LY_SUCCESS;
505 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200506
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100507 /* read all NS elements */
508 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200509
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100510 for (i = 0; i < count; ++i) {
511 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200512
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100513 /* prefix */
514 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
515
516 /* namespace */
517 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
518
519 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
520 ns = NULL;
521 }
522
523 *prefix_data = set;
524 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200525 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200526 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100527 /* nothing stored */
528 break;
529 default:
530 LOGINT(lybctx->ctx);
531 ret = LY_EINT;
532 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200533 }
534
535cleanup:
536 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100537 ly_free_prefix_data(format, set);
538 if (ns) {
539 free(ns->prefix);
540 free(ns->uri);
541 free(ns);
542 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200543 }
544 return ret;
545}
546
547/**
548 * @brief Parse opaque attributes.
549 *
550 * @param[in] lybctx LYB context.
551 * @param[out] attr Parsed attributes.
552 * @return LY_ERR value.
553 */
554static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200555lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200556{
557 LY_ERR ret = LY_SUCCESS;
558 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200559 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200560 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200561 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200562 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100563 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200564
565 /* read count */
566 lyb_read(&count, 1, lybctx);
567
568 /* read attributes */
569 for (i = 0; i < count; ++i) {
570 ret = lyb_read_start_subtree(lybctx);
571 LY_CHECK_GOTO(ret, cleanup);
572
Michal Vasko0fdcd242020-11-11 19:12:30 +0100573 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200574 ret = lyb_read_string(&prefix, 1, lybctx);
575 LY_CHECK_GOTO(ret, cleanup);
576 if (!prefix[0]) {
577 free(prefix);
578 prefix = NULL;
579 }
580
581 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200582 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200583 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200584 if (!module_name[0]) {
585 free(module_name);
586 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200587 }
588
589 /* name */
590 ret = lyb_read_string(&name, 1, lybctx);
591 LY_CHECK_GOTO(ret, cleanup);
592
Michal Vasko60ea6352020-06-29 13:39:39 +0200593 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200594 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200595
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100596 /* value prefixes */
597 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
598 LY_CHECK_GOTO(ret, cleanup);
599
Michal Vasko60ea6352020-06-29 13:39:39 +0200600 /* value */
601 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100602 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200603 dynamic = 1;
604
605 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100606 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200607 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200608 LY_CHECK_GOTO(ret, cleanup);
609
610 free(prefix);
611 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200612 free(module_name);
613 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200614 free(name);
615 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200616 assert(!dynamic);
617 value = NULL;
618
619 if (!*attr) {
620 *attr = attr2;
621 }
622
623 ret = lyb_read_stop_subtree(lybctx);
624 LY_CHECK_GOTO(ret, cleanup);
625 }
626
627cleanup:
628 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200629 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200630 free(name);
631 if (dynamic) {
632 free(value);
633 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200634 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200635 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200636 *attr = NULL;
637 }
638 return ret;
639}
640
641/**
aPiecek619350d2021-09-09 16:06:59 +0200642 * @brief Fill @p hash with hash values.
643 *
644 * @param[in] lybctx LYB context.
645 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
646 * @param[out] hash_count Number of hashes in @p hash.
647 * @return LY_ERR value.
648 */
649static LY_ERR
650lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
651{
652 uint8_t i = 0, j;
653
654 /* read the first hash */
655 lyb_read(&hash[0], sizeof *hash, lybctx);
656
657 if (!hash[0]) {
658 *hash_count = i + 1;
659 return LY_SUCCESS;
660 }
661
662 /* based on the first hash read all the other ones, if any */
663 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
664 if (i > LYB_HASH_BITS) {
665 LOGINT_RET(lybctx->ctx);
666 }
667 }
668
669 /* move the first hash on its accurate position */
670 hash[i] = hash[0];
671
672 /* read the rest of hashes */
673 for (j = i; j; --j) {
674 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
675
676 /* correct collision ID */
677 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
678 /* preceded with zeros */
679 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
680 }
681
682 *hash_count = i + 1;
683
684 return LY_SUCCESS;
685}
686
687/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200688 * @brief Check whether a schema node matches a hash(es).
689 *
690 * @param[in] sibling Schema node to check.
691 * @param[in] hash Hash array to check.
692 * @param[in] hash_count Number of hashes in @p hash.
693 * @return non-zero if matches,
694 * @return 0 if not.
695 */
696static int
697lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
698{
699 LYB_HASH sibling_hash;
700 uint8_t i;
701
702 /* compare all the hashes starting from collision ID 0 */
703 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200704 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200705 if (sibling_hash != hash[i]) {
706 return 0;
707 }
708 }
709
710 return 1;
711}
712
713/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200714 * @brief Parse schema node hash.
715 *
716 * @param[in] lybctx LYB context.
717 * @param[in] sparent Schema parent, must be set if @p mod is not.
718 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
719 * @param[out] snode Parsed found schema node, may be NULL if opaque.
720 * @return LY_ERR value.
721 */
722static LY_ERR
723lyb_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 +0200724 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200725{
726 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200727 const struct lysc_node *sibling;
728 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200729 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200730 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200731
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 *snode = NULL;
741 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200742
743 /* find our node with matching hashes */
744 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100745 while (1) {
746 if (!sparent && lybctx->ext) {
747 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
748 } else {
749 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
750 }
751 if (!sibling) {
752 break;
753 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200754 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200755 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
aPiecek619350d2021-09-09 16:06:59 +0200756 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200757 /* match found */
758 break;
759 }
760 }
761
Michal Vaskoe0665742021-02-11 11:08:44 +0100762 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100763 if (lybctx->ext) {
764 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
765 lybctx->ext->def->name);
766 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100767 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200768 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200769 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100770 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200771 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200772 }
773 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200774 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200775 return ret;
776 }
777
778 *snode = sibling;
779 return LY_SUCCESS;
780}
781
782/**
783 * @brief Read until the end of the current subtree.
784 *
785 * @param[in] lybctx LYB context.
786 */
787static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200788lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200789{
Michal Vasko60ea6352020-06-29 13:39:39 +0200790 do {
791 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200792 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200793
794 /* then read data */
795 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
796 } while (LYB_LAST_SUBTREE(lybctx).written);
797}
798
799/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200800 * @brief Parse the context of anydata/anyxml node.
801 *
802 * @param[in] ctx libyang context.
803 * @param[in] data LYB data to parse.
804 * @param[out] tree Parsed tree.
805 * @return LY_ERR value.
806 */
807static LY_ERR
808lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
809{
810 LY_ERR ret;
811 uint32_t prev_lo;
812 struct ly_in *in;
813 struct lyd_ctx *lydctx = NULL;
814
815 *tree = NULL;
816
817 LY_CHECK_RET(ly_in_new_memory(data, &in));
818
819 /* turn logging off */
820 prev_lo = ly_log_options(0);
821
Michal Vasko56d88602021-07-15 16:37:59 +0200822 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 +0200823 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
824
825 /* turn logging on again */
826 ly_log_options(prev_lo);
827
828 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200829 if (lydctx) {
830 lydctx->free(lydctx);
831 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200832 if (ret) {
833 lyd_free_siblings(*tree);
834 *tree = NULL;
835 }
836 return ret;
837}
838
839/**
aPiecek37c493b2021-09-09 12:52:30 +0200840 * @brief Insert new node to @p parsed set.
841 *
842 * Also if needed, correct @p first_p.
843 *
844 * @param[in] lybctx LYB context.
845 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
846 * @param[in,out] node Parsed node to insertion.
847 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
848 * @param[out] parsed Set of all successfully parsed nodes.
849 * @return LY_ERR value.
850 */
851static void
852lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
853 struct ly_set *parsed)
854{
855 /* insert, keep first pointer correct */
856 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
857 while (!parent && (*first_p)->prev->next) {
858 *first_p = (*first_p)->prev;
859 }
860
861 /* rememeber a successfully parsed node */
862 if (parsed) {
863 ly_set_add(parsed, node, 1, NULL);
864 }
865}
866
867/**
868 * @brief Finish parsing the opaq node.
869 *
870 * @param[in] lybctx LYB context.
871 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
872 * @param[in] flags Node flags to set.
873 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
874 * @param[in,out] node Parsed opaq node to finish.
875 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
876 * @param[out] parsed Set of all successfully parsed nodes.
877 * @return LY_ERR value.
878 */
879static void
880lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
881 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
882{
883 struct lyd_attr *iter;
884
885 /* set flags */
886 (*node)->flags = flags;
887
888 /* add attributes */
889 assert(!(*node)->schema);
890 LY_LIST_FOR(*attr, iter) {
891 iter->parent = (struct lyd_node_opaq *)*node;
892 }
893 ((struct lyd_node_opaq *)*node)->attr = *attr;
894 *attr = NULL;
895
896 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
897 *node = NULL;
898}
899
900/**
901 * @brief Finish parsing the node.
902 *
903 * @param[in] lybctx LYB context.
904 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
905 * @param[in] flags Node flags to set.
906 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
907 * @param[in,out] node Parsed node to finish.
908 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
909 * @param[out] parsed Set of all successfully parsed nodes.
910 * @return LY_ERR value.
911 */
912static void
913lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
914 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
915{
916 struct lyd_meta *m;
917
918 /* set flags */
919 (*node)->flags = flags;
920
921 /* add metadata */
922 LY_LIST_FOR(*meta, m) {
923 m->parent = *node;
924 }
925 (*node)->meta = *meta;
926 *meta = NULL;
927
928 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
929 *node = NULL;
930}
931
932/**
aPiecek33fc6b02021-09-09 15:45:37 +0200933 * @brief Create term node and fill it with value.
934 *
935 * @param[in] lybctx LYB context.
936 * @param[in] snode Schema of the term node.
937 * @param[out] node Created term node.
938 * @return LY_ERR value.
939 */
940static LY_ERR
941lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
942{
943 LY_ERR ret;
944 ly_bool dynamic;
945 uint8_t *term_value;
946 uint32_t term_value_len;
947
948 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
949 LY_CHECK_RET(ret);
950
951 dynamic = 1;
952 /* create node */
953 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
954 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
955 NULL, LYD_HINT_DATA, node);
956 if (dynamic) {
957 free(term_value);
958 }
959 if (ret) {
960 lyd_free_tree(*node);
961 *node = NULL;
962 }
963
964 return ret;
965}
966
967/**
aPiecek0e2e1052021-09-09 15:48:27 +0200968 * @brief Validate inner node, autodelete default values nad create implicit nodes.
969 *
970 * @param[in,out] lybctx LYB context.
971 * @param[in] snode Schema of the inner node.
972 * @param[in] node Parsed inner node.
973 * @return LY_ERR value.
974 */
975static LY_ERR
976lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
977{
978 LY_ERR ret = LY_SUCCESS;
979 uint32_t impl_opts;
980
981 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
982 /* new node validation, autodelete CANNOT occur, all nodes are new */
983 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
984 LY_CHECK_RET(ret);
985
986 /* add any missing default children */
987 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
988 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL,
989 NULL, &lybctx->node_when, &lybctx->node_exts,
990 &lybctx->node_types, impl_opts, NULL);
991 LY_CHECK_RET(ret);
992 }
993
994 return ret;
995}
996
997/**
aPiecek821cf732021-09-09 15:37:28 +0200998 * @brief Parse opaq node.
999 *
1000 * @param[in] lybctx LYB context.
1001 * @param[in] parent Data parent of the subtree.
1002 * @param[in,out] first_p First top-level sibling.
1003 * @param[out] parsed Set of all successfully parsed nodes.
1004 * @return LY_ERR value.
1005 */
1006static LY_ERR
1007lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1008{
1009 LY_ERR ret;
1010 struct lyd_node *node = NULL;
1011 struct lyd_attr *attr = NULL;
1012 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1013 ly_bool dynamic = 0;
1014 LY_VALUE_FORMAT format = 0;
1015 void *val_prefix_data = NULL;
1016 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1017 uint32_t flags;
1018
1019 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
1020 /* unknown data, skip them */
1021 lyb_skip_subtree(lybctx->lybctx);
1022 return LY_SUCCESS;
1023 }
1024
1025 /* parse opaq node attributes */
1026 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1027 LY_CHECK_GOTO(ret, cleanup);
1028
1029 /* read flags */
1030 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1031
1032 /* parse prefix */
1033 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
1034 LY_CHECK_GOTO(ret, cleanup);
1035
1036 /* parse module key */
1037 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
1038 LY_CHECK_GOTO(ret, cleanup);
1039
1040 /* parse name */
1041 ret = lyb_read_string(&name, 1, lybctx->lybctx);
1042 LY_CHECK_GOTO(ret, cleanup);
1043
1044 /* parse value */
1045 ret = lyb_read_string(&value, 1, lybctx->lybctx);
1046 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1047 dynamic = 1;
1048
1049 /* parse format */
1050 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1051
1052 /* parse value prefixes */
1053 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1054 LY_CHECK_GOTO(ret, cleanup);
1055
1056 /* create node */
1057 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1058 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1059 LY_CHECK_GOTO(ret, cleanup);
1060
1061 /* process children */
1062 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
1063 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
1064 LY_CHECK_GOTO(ret, cleanup);
1065 }
1066
1067 /* register parsed opaq node */
1068 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1069 assert(!attr && !node);
1070
1071cleanup:
1072 free(prefix);
1073 free(module_key);
1074 free(name);
1075 if (dynamic) {
1076 free(value);
1077 }
1078 lyd_free_attr_siblings(ctx, attr);
1079 lyd_free_tree(node);
1080
1081 return ret;
1082}
1083
aPiecek18457d72021-09-09 15:52:20 +02001084/**
1085 * @brief Parse anydata or anyxml node.
1086 *
1087 * @param[in] lybctx LYB context.
1088 * @param[in] parent Data parent of the subtree.
1089 * @param[in] snode Schema of the node to be parsed.
1090 * @param[in,out] first_p First top-level sibling.
1091 * @param[out] parsed Set of all successfully parsed nodes.
1092 * @return LY_ERR value.
1093 */
1094static LY_ERR
1095lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1096 struct lyd_node **first_p, struct ly_set *parsed)
1097{
1098 LY_ERR ret;
1099 struct lyd_node *node = NULL, *tree;
1100 struct lyd_meta *meta = NULL;
1101 LYD_ANYDATA_VALUETYPE value_type;
1102 char *value = NULL;
1103 const char *val_dict;
1104 uint32_t flags;
1105 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1106
1107 /* read necessary basic data */
1108 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1109 LY_CHECK_GOTO(ret, error);
1110
1111 /* parse value type */
1112 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1113 if (value_type == LYD_ANYDATA_DATATREE) {
1114 /* invalid situation */
1115 LOGINT(ctx);
1116 ret = LY_EINT;
1117 goto error;
1118 }
1119
1120 /* read anydata content */
1121 ret = lyb_read_string(&value, 0, lybctx->lybctx);
1122 LY_CHECK_GOTO(ret, error);
1123
1124 if (value_type == LYD_ANYDATA_LYB) {
1125 /* try to parse LYB into a data tree */
1126 if (!lyb_parse_any_content(ctx, value, &tree)) {
1127 /* successfully parsed */
1128 free(value);
1129 value = (char *)tree;
1130 value_type = LYD_ANYDATA_DATATREE;
1131 }
1132 }
1133
1134 /* create the node */
1135 switch (value_type) {
1136 case LYD_ANYDATA_LYB:
1137 case LYD_ANYDATA_DATATREE:
1138 /* use the value directly */
1139 ret = lyd_create_any(snode, value, value_type, 1, &node);
1140 LY_CHECK_GOTO(ret, error);
1141
1142 break;
1143 case LYD_ANYDATA_STRING:
1144 case LYD_ANYDATA_XML:
1145 case LYD_ANYDATA_JSON:
1146 /* value is expected to be in the dictionary */
1147 ret = lydict_insert_zc(ctx, value, &val_dict);
1148 LY_CHECK_GOTO(ret, error);
1149
1150 /* use the value in the dictionary */
1151 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
1152 if (ret) {
1153 lydict_remove(ctx, val_dict);
1154 goto error;
1155 }
1156 break;
1157 default:
1158 LOGINT(ctx);
1159 ret = LY_EINT;
1160 goto error;
1161 }
1162
1163 /* register parsed anydata node */
1164 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1165
1166 return LY_SUCCESS;
1167
1168error:
1169 free(value);
1170 lyd_free_meta_siblings(meta);
1171 lyd_free_tree(node);
1172 return ret;
1173}
aPiecek821cf732021-09-09 15:37:28 +02001174
1175/**
aPiecek37c493b2021-09-09 12:52:30 +02001176 * @brief Parse header for non-opaq node.
1177 *
1178 * @param[in] lybctx LYB context.
1179 * @param[out] flags Parsed node flags.
1180 * @param[out] meta Parsed metadata of the node.
1181 * @return LY_ERR value.
1182 */
1183static LY_ERR
1184lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta)
1185{
1186 LY_ERR ret;
1187
1188 /* create and read metadata */
1189 ret = lyb_parse_metadata(lybctx, meta);
1190 LY_CHECK_RET(ret);
1191
1192 /* read flags */
1193 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1194
1195 return ret;
1196}
1197
1198/**
1199 * @brief Parse model and hash.
1200 *
1201 * @param[in] lybctx LYB context.
1202 * @param[in] parent Data parent of the subtree.
1203 * @param[out] snode Schema of the node to be further parsed. Can be NULL for the opaq node.
1204 * @return LY_ERR value.
1205 */
1206static LY_ERR
1207lyb_print_model_and_hash(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node **snode)
1208{
1209 LY_ERR ret;
1210 const struct lys_module *mod;
1211
1212 if (!parent || !parent->schema) {
1213 /* top-level or opaque, read module name */
1214 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
1215 LY_CHECK_RET(ret);
1216
1217 /* read hash, find the schema node starting from mod */
1218 ret = lyb_parse_schema_hash(lybctx, NULL, mod, snode);
1219 LY_CHECK_RET(ret);
1220 } else {
1221 /* read hash, find the schema node starting from parent schema */
1222 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, snode);
1223 LY_CHECK_RET(ret);
1224 }
1225
1226 return ret;
1227}
1228
1229/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001230 * @brief Parse LYB subtree.
1231 *
1232 * @param[in] lybctx LYB context.
1233 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001234 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1235 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001236 * @return LY_ERR value.
1237 */
1238static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001239lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001240{
1241 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001242 struct lyd_node *node = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001243 const struct lysc_node *snode = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001244 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001245 uint32_t flags;
Radek Krejci1798aae2020-07-14 13:26:06 +02001246 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001247
1248 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001249 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001250
aPiecek37c493b2021-09-09 12:52:30 +02001251 ret = lyb_print_model_and_hash(lybctx, parent, &snode);
1252 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +02001253
aPiecek37c493b2021-09-09 12:52:30 +02001254 if (!snode) {
aPiecek821cf732021-09-09 15:37:28 +02001255 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001256 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001257 goto stop_subtree;
Michal Vasko60ea6352020-06-29 13:39:39 +02001258 } else if (snode->nodetype & LYD_NODE_TERM) {
aPiecek37c493b2021-09-09 12:52:30 +02001259 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1260 LY_CHECK_GOTO(ret, cleanup);
1261
aPiecek33fc6b02021-09-09 15:45:37 +02001262 /* read value of term node and create it */
1263 ret = lyb_create_term(lybctx, snode, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001264 LY_CHECK_GOTO(ret, cleanup);
aPiecek37c493b2021-09-09 12:52:30 +02001265
1266 /* complete the node processing */
1267 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001268 } else if (snode->nodetype & LYD_NODE_INNER) {
aPiecek37c493b2021-09-09 12:52:30 +02001269 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1270 LY_CHECK_GOTO(ret, cleanup);
1271
Michal Vasko60ea6352020-06-29 13:39:39 +02001272 /* create node */
1273 ret = lyd_create_inner(snode, &node);
1274 LY_CHECK_GOTO(ret, cleanup);
1275
1276 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +02001277 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001278 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001279 LY_CHECK_GOTO(ret, cleanup);
1280 }
1281
aPiecek0e2e1052021-09-09 15:48:27 +02001282 /* additional procedure for inner node */
1283 ret = lyb_validate_node_inner(lybctx, snode, node);
1284 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001285
Michal Vasko751cb4d2020-07-14 12:25:28 +02001286 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001287 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +02001288 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001289 }
aPiecek37c493b2021-09-09 12:52:30 +02001290
1291 /* complete the node processing */
1292 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001293 } else if (snode->nodetype & LYD_NODE_ANY) {
aPiecek18457d72021-09-09 15:52:20 +02001294 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
aPiecek37c493b2021-09-09 12:52:30 +02001295 LY_CHECK_GOTO(ret, cleanup);
aPiecek18457d72021-09-09 15:52:20 +02001296 goto stop_subtree;
Michal Vasko60ea6352020-06-29 13:39:39 +02001297 } else {
aPiecek37c493b2021-09-09 12:52:30 +02001298 LOGINT(ctx);
1299 ret = LY_EINT;
1300 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001301 }
1302
Michal Vasko60ea6352020-06-29 13:39:39 +02001303stop_subtree:
1304 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001305 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001306 LY_CHECK_GOTO(ret, cleanup);
1307
1308cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001309 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001310 lyd_free_tree(node);
1311 return ret;
1312}
1313
1314/**
1315 * @brief Parse used YANG data models.
1316 *
1317 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001318 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001319 * @return LY_ERR value.
1320 */
1321static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001322lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001323{
1324 LY_ERR ret;
1325 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001326 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001327
1328 /* read model count */
1329 lyb_read_number(&count, sizeof count, 2, lybctx);
1330
1331 if (count) {
1332 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1333
1334 /* read modules */
1335 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001336 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001337 LY_CHECK_RET(ret);
1338 LY_ARRAY_INCREMENT(lybctx->models);
1339 }
1340 }
1341
1342 return LY_SUCCESS;
1343}
1344
1345/**
1346 * @brief Parse LYB magic number.
1347 *
1348 * @param[in] lybctx LYB context.
1349 * @return LY_ERR value.
1350 */
1351static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001352lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001353{
1354 char magic_byte = 0;
1355
1356 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1357 if (magic_byte != 'l') {
1358 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1359 return LY_EINVAL;
1360 }
1361
1362 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1363 if (magic_byte != 'y') {
1364 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1365 return LY_EINVAL;
1366 }
1367
1368 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1369 if (magic_byte != 'b') {
1370 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1371 return LY_EINVAL;
1372 }
1373
1374 return LY_SUCCESS;
1375}
1376
1377/**
1378 * @brief Parse LYB header.
1379 *
1380 * @param[in] lybctx LYB context.
1381 * @return LY_ERR value.
1382 */
1383static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001384lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001385{
1386 uint8_t byte = 0;
1387
1388 /* version, future flags */
1389 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1390
1391 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1392 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001393 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001394 return LY_EINVAL;
1395 }
1396
1397 return LY_SUCCESS;
1398}
1399
Michal Vasko02ed9d82021-07-15 14:58:04 +02001400static LY_ERR
1401_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1402 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 +01001403 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001404{
Michal Vaskoe0665742021-02-11 11:08:44 +01001405 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001406 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001407
Michal Vaskoe0665742021-02-11 11:08:44 +01001408 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1409 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001410
Radek Krejci1798aae2020-07-14 13:26:06 +02001411 lybctx = calloc(1, sizeof *lybctx);
1412 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1413 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001414 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001415
Radek Krejci1798aae2020-07-14 13:26:06 +02001416 lybctx->lybctx->in = in;
1417 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001418 lybctx->parse_opts = parse_opts;
1419 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001420 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001421 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001422 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001423
1424 /* find the operation node if it exists already */
1425 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1426
Michal Vasko60ea6352020-06-29 13:39:39 +02001427 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001428 rc = lyb_parse_magic_number(lybctx->lybctx);
1429 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001430
1431 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001432 rc = lyb_parse_header(lybctx->lybctx);
1433 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001434
1435 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001436 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1437 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001438
1439 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001440 while (lybctx->lybctx->in->current[0]) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001441 rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed);
1442 LY_CHECK_GOTO(rc, cleanup);
1443
1444 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1445 break;
1446 }
1447 }
1448
1449 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1450 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1451 rc = LY_EVALID;
1452 goto cleanup;
1453 }
1454 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1455 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1456 rc = LY_EVALID;
1457 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001458 }
1459
1460 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001461 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001462
Michal Vasko60ea6352020-06-29 13:39:39 +02001463cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001464 /* there should be no unres stored if validation should be skipped */
1465 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1466 !lybctx->node_when.count));
1467
1468 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001469 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001470 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001471 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001472 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001473 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001474}
1475
Michal Vasko02ed9d82021-07-15 14:58:04 +02001476LY_ERR
1477lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1478 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1479 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
1480{
1481 uint32_t int_opts;
1482
1483 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1484 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1485
1486 switch (data_type) {
1487 case LYD_TYPE_DATA_YANG:
1488 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1489 break;
1490 case LYD_TYPE_RPC_YANG:
1491 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1492 break;
1493 case LYD_TYPE_NOTIF_YANG:
1494 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1495 break;
1496 case LYD_TYPE_REPLY_YANG:
1497 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1498 break;
1499 default:
1500 LOGINT(ctx);
1501 return LY_EINT;
1502 }
1503
1504 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1505}
1506
Michal Vasko60ea6352020-06-29 13:39:39 +02001507API int
1508lyd_lyb_data_length(const char *data)
1509{
1510 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001511 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001512 int count, i;
1513 size_t len;
1514 uint8_t buf[LYB_SIZE_MAX];
1515
1516 if (!data) {
1517 return -1;
1518 }
1519
Radek Krejci1798aae2020-07-14 13:26:06 +02001520 lybctx = calloc(1, sizeof *lybctx);
1521 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1522 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001523 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001524
1525 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001526 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001527 LY_CHECK_GOTO(ret, cleanup);
1528
1529 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001530 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001531 LY_CHECK_GOTO(ret, cleanup);
1532
1533 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001534 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001535
1536 /* read all models */
1537 for (i = 0; i < count; ++i) {
1538 /* module name length */
1539 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001540 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001541
1542 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001543 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001544
1545 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001546 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001547 }
1548
Radek Krejci1798aae2020-07-14 13:26:06 +02001549 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001550 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001551 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001552 LY_CHECK_GOTO(ret, cleanup);
1553
1554 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001555 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001556
1557 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001558 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001559 LY_CHECK_GOTO(ret, cleanup);
1560 }
1561
1562 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001563 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001564
1565cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001566 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001567
Radek Krejci1798aae2020-07-14 13:26:06 +02001568 ly_in_free(lybctx->in, 0);
1569 lylyb_ctx_free(lybctx);
1570
Michal Vasko63f3d842020-07-08 10:10:14 +02001571 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001572}