blob: ef47fedd2cac348016ac8b49c55141ee6352ef63 [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.
Michal Vasko60ea6352020-06-29 13:39:39 +0200334 * @param[out] mod Parsed module.
335 * @return LY_ERR value.
336 */
337static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200338lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200339{
340 LY_ERR ret = LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200342 uint16_t rev;
343
344 /* model name */
345 ret = lyb_read_string(&mod_name, 1, lybctx);
346 LY_CHECK_GOTO(ret, cleanup);
347
348 /* revision */
349 lyb_read_number(&rev, sizeof rev, 2, lybctx);
350
351 if (!mod_name[0]) {
352 /* opaq node, no module */
353 *mod = NULL;
354 goto cleanup;
355 }
356
357 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100358 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
359 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko60ea6352020-06-29 13:39:39 +0200360 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200361 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200362 /* try to use an updated module */
363 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
364 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
365 /* not an implemented module in a newer revision */
366 *mod = NULL;
367 }
368 }
369 } else {
370 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
371 }
372 /* TODO data_clb supported?
373 if (lybctx->ctx->data_clb) {
374 if (!*mod) {
375 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
376 } else if (!(*mod)->implemented) {
377 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
378 }
379 }*/
380
381 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200382 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200383 if (!*mod) {
384 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200385 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200386 } else if (!(*mod)->implemented) {
387 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200388 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200389 }
390 ret = LY_EINVAL;
391 goto cleanup;
392 }
393
394 }
395
Michal Vasko85d9edc2021-04-22 09:15:05 +0200396 if (*mod) {
397 /* fill cached hashes, if not already */
398 lyb_cache_module_hash(*mod);
399 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200400
Michal Vasko60ea6352020-06-29 13:39:39 +0200401cleanup:
402 free(mod_name);
403 return ret;
404}
405
406/**
407 * @brief Parse YANG node metadata.
408 *
409 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200410 * @param[out] meta Parsed metadata.
411 * @return LY_ERR value.
412 */
413static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200414lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200415{
416 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200417 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200419 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 const struct lys_module *mod;
421
422 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200423 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200424
425 /* read attributes */
426 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200427 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200428 LY_CHECK_GOTO(ret, cleanup);
429
430 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100431 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200432 LY_CHECK_GOTO(ret, cleanup);
433
434 if (!mod) {
435 /* skip it */
436 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200437 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
438 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200439 goto stop_subtree;
440 }
441
442 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200443 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200444 LY_CHECK_GOTO(ret, cleanup);
445
446 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200447 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200448 LY_CHECK_GOTO(ret, cleanup);
449 dynamic = 1;
450
451 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200452 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 +0200453 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200454
455 /* free strings */
456 free(meta_name);
457 meta_name = NULL;
458 if (dynamic) {
459 free(meta_value);
460 dynamic = 0;
461 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200462
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200464
465stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200466 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200467 LY_CHECK_GOTO(ret, cleanup);
468 }
469
470cleanup:
471 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200472 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200473 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200474 *meta = NULL;
475 }
476 return ret;
477}
478
479/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100480 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200481 *
482 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100483 * @param[in] format Prefix data format.
484 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200485 * @return LY_ERR value.
486 */
487static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200488lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200489{
490 LY_ERR ret = LY_SUCCESS;
491 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100492 struct ly_set *set = NULL;
493 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200494
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100495 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200496 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100497 /* read count */
498 lyb_read(&count, 1, lybctx);
499 if (!count) {
500 return LY_SUCCESS;
501 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200502
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100503 /* read all NS elements */
504 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200505
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100506 for (i = 0; i < count; ++i) {
507 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200508
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100509 /* prefix */
510 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
511
512 /* namespace */
513 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
514
515 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
516 ns = NULL;
517 }
518
519 *prefix_data = set;
520 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200521 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200522 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100523 /* nothing stored */
524 break;
525 default:
526 LOGINT(lybctx->ctx);
527 ret = LY_EINT;
528 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200529 }
530
531cleanup:
532 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100533 ly_free_prefix_data(format, set);
534 if (ns) {
535 free(ns->prefix);
536 free(ns->uri);
537 free(ns);
538 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200539 }
540 return ret;
541}
542
543/**
544 * @brief Parse opaque attributes.
545 *
546 * @param[in] lybctx LYB context.
547 * @param[out] attr Parsed attributes.
548 * @return LY_ERR value.
549 */
550static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200551lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200552{
553 LY_ERR ret = LY_SUCCESS;
554 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200555 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200556 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200557 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200558 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100559 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200560
561 /* read count */
562 lyb_read(&count, 1, lybctx);
563
564 /* read attributes */
565 for (i = 0; i < count; ++i) {
566 ret = lyb_read_start_subtree(lybctx);
567 LY_CHECK_GOTO(ret, cleanup);
568
Michal Vasko0fdcd242020-11-11 19:12:30 +0100569 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200570 ret = lyb_read_string(&prefix, 1, lybctx);
571 LY_CHECK_GOTO(ret, cleanup);
572 if (!prefix[0]) {
573 free(prefix);
574 prefix = NULL;
575 }
576
577 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200579 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200580 if (!module_name[0]) {
581 free(module_name);
582 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200583 }
584
585 /* name */
586 ret = lyb_read_string(&name, 1, lybctx);
587 LY_CHECK_GOTO(ret, cleanup);
588
Michal Vasko60ea6352020-06-29 13:39:39 +0200589 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200590 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200591
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100592 /* value prefixes */
593 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
594 LY_CHECK_GOTO(ret, cleanup);
595
Michal Vasko60ea6352020-06-29 13:39:39 +0200596 /* value */
597 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100598 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200599 dynamic = 1;
600
601 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100602 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200603 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200604 LY_CHECK_GOTO(ret, cleanup);
605
606 free(prefix);
607 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200608 free(module_name);
609 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200610 free(name);
611 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200612 assert(!dynamic);
613 value = NULL;
614
615 if (!*attr) {
616 *attr = attr2;
617 }
618
619 ret = lyb_read_stop_subtree(lybctx);
620 LY_CHECK_GOTO(ret, cleanup);
621 }
622
623cleanup:
624 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200625 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200626 free(name);
627 if (dynamic) {
628 free(value);
629 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200630 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200631 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200632 *attr = NULL;
633 }
634 return ret;
635}
636
637/**
aPiecek619350d2021-09-09 16:06:59 +0200638 * @brief Fill @p hash with hash values.
639 *
640 * @param[in] lybctx LYB context.
641 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
642 * @param[out] hash_count Number of hashes in @p hash.
643 * @return LY_ERR value.
644 */
645static LY_ERR
646lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
647{
648 uint8_t i = 0, j;
649
650 /* read the first hash */
651 lyb_read(&hash[0], sizeof *hash, lybctx);
652
653 if (!hash[0]) {
654 *hash_count = i + 1;
655 return LY_SUCCESS;
656 }
657
658 /* based on the first hash read all the other ones, if any */
659 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
660 if (i > LYB_HASH_BITS) {
661 LOGINT_RET(lybctx->ctx);
662 }
663 }
664
665 /* move the first hash on its accurate position */
666 hash[i] = hash[0];
667
668 /* read the rest of hashes */
669 for (j = i; j; --j) {
670 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
671
672 /* correct collision ID */
673 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
674 /* preceded with zeros */
675 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
676 }
677
678 *hash_count = i + 1;
679
680 return LY_SUCCESS;
681}
682
683/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200684 * @brief Check whether a schema node matches a hash(es).
685 *
686 * @param[in] sibling Schema node to check.
687 * @param[in] hash Hash array to check.
688 * @param[in] hash_count Number of hashes in @p hash.
689 * @return non-zero if matches,
690 * @return 0 if not.
691 */
692static int
693lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
694{
695 LYB_HASH sibling_hash;
696 uint8_t i;
697
698 /* compare all the hashes starting from collision ID 0 */
699 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200700 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200701 if (sibling_hash != hash[i]) {
702 return 0;
703 }
704 }
705
706 return 1;
707}
708
709/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200710 * @brief Parse schema node hash.
711 *
712 * @param[in] lybctx LYB context.
713 * @param[in] sparent Schema parent, must be set if @p mod is not.
714 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
715 * @param[out] snode Parsed found schema node, may be NULL if opaque.
716 * @return LY_ERR value.
717 */
718static LY_ERR
719lyb_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 +0200720 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200721{
722 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200723 const struct lysc_node *sibling;
724 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200725 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200726 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200727
aPiecek619350d2021-09-09 16:06:59 +0200728 ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
729 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200730
731 if (!hash[0]) {
732 /* opaque node */
733 return LY_SUCCESS;
734 }
735
aPiecek619350d2021-09-09 16:06:59 +0200736 *snode = NULL;
737 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200738
739 /* find our node with matching hashes */
740 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100741 while (1) {
742 if (!sparent && lybctx->ext) {
743 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
744 } else {
745 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
746 }
747 if (!sibling) {
748 break;
749 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200750 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200751 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
aPiecek619350d2021-09-09 16:06:59 +0200752 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200753 /* match found */
754 break;
755 }
756 }
757
Michal Vaskoe0665742021-02-11 11:08:44 +0100758 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100759 if (lybctx->ext) {
760 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
761 lybctx->ext->def->name);
762 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100763 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200764 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200765 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100766 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200767 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200768 }
769 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200770 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200771 return ret;
772 }
773
774 *snode = sibling;
775 return LY_SUCCESS;
776}
777
778/**
779 * @brief Read until the end of the current subtree.
780 *
781 * @param[in] lybctx LYB context.
782 */
783static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200784lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200785{
Michal Vasko60ea6352020-06-29 13:39:39 +0200786 do {
787 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200788 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200789
790 /* then read data */
791 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
792 } while (LYB_LAST_SUBTREE(lybctx).written);
793}
794
795/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200796 * @brief Parse the context of anydata/anyxml node.
797 *
798 * @param[in] ctx libyang context.
799 * @param[in] data LYB data to parse.
800 * @param[out] tree Parsed tree.
801 * @return LY_ERR value.
802 */
803static LY_ERR
804lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
805{
806 LY_ERR ret;
807 uint32_t prev_lo;
808 struct ly_in *in;
809 struct lyd_ctx *lydctx = NULL;
810
811 *tree = NULL;
812
813 LY_CHECK_RET(ly_in_new_memory(data, &in));
814
815 /* turn logging off */
816 prev_lo = ly_log_options(0);
817
Michal Vasko56d88602021-07-15 16:37:59 +0200818 ret = _lyd_parse_lyb(ctx, NULL, NULL, tree, in, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0,
Michal Vasko02ed9d82021-07-15 14:58:04 +0200819 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
820
821 /* turn logging on again */
822 ly_log_options(prev_lo);
823
824 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200825 if (lydctx) {
826 lydctx->free(lydctx);
827 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200828 if (ret) {
829 lyd_free_siblings(*tree);
830 *tree = NULL;
831 }
832 return ret;
833}
834
835/**
aPiecek37c493b2021-09-09 12:52:30 +0200836 * @brief Insert new node to @p parsed set.
837 *
838 * Also if needed, correct @p first_p.
839 *
840 * @param[in] lybctx LYB context.
841 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
842 * @param[in,out] node Parsed node to insertion.
843 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
844 * @param[out] parsed Set of all successfully parsed nodes.
845 * @return LY_ERR value.
846 */
847static void
848lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
849 struct ly_set *parsed)
850{
851 /* insert, keep first pointer correct */
852 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
853 while (!parent && (*first_p)->prev->next) {
854 *first_p = (*first_p)->prev;
855 }
856
857 /* rememeber a successfully parsed node */
858 if (parsed) {
859 ly_set_add(parsed, node, 1, NULL);
860 }
861}
862
863/**
864 * @brief Finish parsing the opaq node.
865 *
866 * @param[in] lybctx LYB context.
867 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
868 * @param[in] flags Node flags to set.
869 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
870 * @param[in,out] node Parsed opaq node to finish.
871 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
872 * @param[out] parsed Set of all successfully parsed nodes.
873 * @return LY_ERR value.
874 */
875static void
876lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
877 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
878{
879 struct lyd_attr *iter;
880
881 /* set flags */
882 (*node)->flags = flags;
883
884 /* add attributes */
885 assert(!(*node)->schema);
886 LY_LIST_FOR(*attr, iter) {
887 iter->parent = (struct lyd_node_opaq *)*node;
888 }
889 ((struct lyd_node_opaq *)*node)->attr = *attr;
890 *attr = NULL;
891
892 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
893 *node = NULL;
894}
895
896/**
897 * @brief Finish parsing the node.
898 *
899 * @param[in] lybctx LYB context.
900 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
901 * @param[in] flags Node flags to set.
902 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
903 * @param[in,out] node Parsed node to finish.
904 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
905 * @param[out] parsed Set of all successfully parsed nodes.
906 * @return LY_ERR value.
907 */
908static void
909lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
910 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
911{
912 struct lyd_meta *m;
913
914 /* set flags */
915 (*node)->flags = flags;
916
917 /* add metadata */
918 LY_LIST_FOR(*meta, m) {
919 m->parent = *node;
920 }
921 (*node)->meta = *meta;
922 *meta = NULL;
923
924 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
925 *node = NULL;
926}
927
928/**
aPiecek33fc6b02021-09-09 15:45:37 +0200929 * @brief Create term node and fill it with value.
930 *
931 * @param[in] lybctx LYB context.
932 * @param[in] snode Schema of the term node.
933 * @param[out] node Created term node.
934 * @return LY_ERR value.
935 */
936static LY_ERR
937lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
938{
939 LY_ERR ret;
940 ly_bool dynamic;
941 uint8_t *term_value;
942 uint32_t term_value_len;
943
944 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
945 LY_CHECK_RET(ret);
946
947 dynamic = 1;
948 /* create node */
949 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
950 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
951 NULL, LYD_HINT_DATA, node);
952 if (dynamic) {
953 free(term_value);
954 }
955 if (ret) {
956 lyd_free_tree(*node);
957 *node = NULL;
958 }
959
960 return ret;
961}
962
963/**
aPiecek0e2e1052021-09-09 15:48:27 +0200964 * @brief Validate inner node, autodelete default values nad create implicit nodes.
965 *
966 * @param[in,out] lybctx LYB context.
967 * @param[in] snode Schema of the inner node.
968 * @param[in] node Parsed inner node.
969 * @return LY_ERR value.
970 */
971static LY_ERR
972lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
973{
974 LY_ERR ret = LY_SUCCESS;
975 uint32_t impl_opts;
976
977 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
978 /* new node validation, autodelete CANNOT occur, all nodes are new */
979 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
980 LY_CHECK_RET(ret);
981
982 /* add any missing default children */
983 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
984 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL,
985 NULL, &lybctx->node_when, &lybctx->node_exts,
986 &lybctx->node_types, impl_opts, NULL);
987 LY_CHECK_RET(ret);
988 }
989
990 return ret;
991}
992
993/**
aPiecek821cf732021-09-09 15:37:28 +0200994 * @brief Parse opaq node.
995 *
996 * @param[in] lybctx LYB context.
997 * @param[in] parent Data parent of the subtree.
998 * @param[in,out] first_p First top-level sibling.
999 * @param[out] parsed Set of all successfully parsed nodes.
1000 * @return LY_ERR value.
1001 */
1002static LY_ERR
1003lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1004{
1005 LY_ERR ret;
1006 struct lyd_node *node = NULL;
1007 struct lyd_attr *attr = NULL;
1008 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1009 ly_bool dynamic = 0;
1010 LY_VALUE_FORMAT format = 0;
1011 void *val_prefix_data = NULL;
1012 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1013 uint32_t flags;
1014
1015 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
1016 /* unknown data, skip them */
1017 lyb_skip_subtree(lybctx->lybctx);
1018 return LY_SUCCESS;
1019 }
1020
1021 /* parse opaq node attributes */
1022 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1023 LY_CHECK_GOTO(ret, cleanup);
1024
1025 /* read flags */
1026 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1027
1028 /* parse prefix */
1029 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
1030 LY_CHECK_GOTO(ret, cleanup);
1031
1032 /* parse module key */
1033 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
1034 LY_CHECK_GOTO(ret, cleanup);
1035
1036 /* parse name */
1037 ret = lyb_read_string(&name, 1, lybctx->lybctx);
1038 LY_CHECK_GOTO(ret, cleanup);
1039
1040 /* parse value */
1041 ret = lyb_read_string(&value, 1, lybctx->lybctx);
1042 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1043 dynamic = 1;
1044
1045 /* parse format */
1046 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1047
1048 /* parse value prefixes */
1049 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1050 LY_CHECK_GOTO(ret, cleanup);
1051
1052 /* create node */
1053 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1054 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1055 LY_CHECK_GOTO(ret, cleanup);
1056
1057 /* process children */
1058 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
1059 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
1060 LY_CHECK_GOTO(ret, cleanup);
1061 }
1062
1063 /* register parsed opaq node */
1064 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1065 assert(!attr && !node);
1066
1067cleanup:
1068 free(prefix);
1069 free(module_key);
1070 free(name);
1071 if (dynamic) {
1072 free(value);
1073 }
1074 lyd_free_attr_siblings(ctx, attr);
1075 lyd_free_tree(node);
1076
1077 return ret;
1078}
1079
aPiecek18457d72021-09-09 15:52:20 +02001080/**
1081 * @brief Parse anydata or anyxml node.
1082 *
1083 * @param[in] lybctx LYB context.
1084 * @param[in] parent Data parent of the subtree.
1085 * @param[in] snode Schema of the node to be parsed.
1086 * @param[in,out] first_p First top-level sibling.
1087 * @param[out] parsed Set of all successfully parsed nodes.
1088 * @return LY_ERR value.
1089 */
1090static LY_ERR
1091lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1092 struct lyd_node **first_p, struct ly_set *parsed)
1093{
1094 LY_ERR ret;
1095 struct lyd_node *node = NULL, *tree;
1096 struct lyd_meta *meta = NULL;
1097 LYD_ANYDATA_VALUETYPE value_type;
1098 char *value = NULL;
1099 const char *val_dict;
1100 uint32_t flags;
1101 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1102
1103 /* read necessary basic data */
1104 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1105 LY_CHECK_GOTO(ret, error);
1106
1107 /* parse value type */
1108 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1109 if (value_type == LYD_ANYDATA_DATATREE) {
1110 /* invalid situation */
1111 LOGINT(ctx);
1112 ret = LY_EINT;
1113 goto error;
1114 }
1115
1116 /* read anydata content */
1117 ret = lyb_read_string(&value, 0, lybctx->lybctx);
1118 LY_CHECK_GOTO(ret, error);
1119
1120 if (value_type == LYD_ANYDATA_LYB) {
1121 /* try to parse LYB into a data tree */
1122 if (!lyb_parse_any_content(ctx, value, &tree)) {
1123 /* successfully parsed */
1124 free(value);
1125 value = (char *)tree;
1126 value_type = LYD_ANYDATA_DATATREE;
1127 }
1128 }
1129
1130 /* create the node */
1131 switch (value_type) {
1132 case LYD_ANYDATA_LYB:
1133 case LYD_ANYDATA_DATATREE:
1134 /* use the value directly */
1135 ret = lyd_create_any(snode, value, value_type, 1, &node);
1136 LY_CHECK_GOTO(ret, error);
1137
1138 break;
1139 case LYD_ANYDATA_STRING:
1140 case LYD_ANYDATA_XML:
1141 case LYD_ANYDATA_JSON:
1142 /* value is expected to be in the dictionary */
1143 ret = lydict_insert_zc(ctx, value, &val_dict);
1144 LY_CHECK_GOTO(ret, error);
1145
1146 /* use the value in the dictionary */
1147 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
1148 if (ret) {
1149 lydict_remove(ctx, val_dict);
1150 goto error;
1151 }
1152 break;
1153 default:
1154 LOGINT(ctx);
1155 ret = LY_EINT;
1156 goto error;
1157 }
1158
1159 /* register parsed anydata node */
1160 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1161
1162 return LY_SUCCESS;
1163
1164error:
1165 free(value);
1166 lyd_free_meta_siblings(meta);
1167 lyd_free_tree(node);
1168 return ret;
1169}
aPiecek821cf732021-09-09 15:37:28 +02001170
1171/**
aPiecek37c493b2021-09-09 12:52:30 +02001172 * @brief Parse header for non-opaq node.
1173 *
1174 * @param[in] lybctx LYB context.
1175 * @param[out] flags Parsed node flags.
1176 * @param[out] meta Parsed metadata of the node.
1177 * @return LY_ERR value.
1178 */
1179static LY_ERR
1180lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta)
1181{
1182 LY_ERR ret;
1183
1184 /* create and read metadata */
1185 ret = lyb_parse_metadata(lybctx, meta);
1186 LY_CHECK_RET(ret);
1187
1188 /* read flags */
1189 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1190
1191 return ret;
1192}
1193
1194/**
1195 * @brief Parse model and hash.
1196 *
1197 * @param[in] lybctx LYB context.
1198 * @param[in] parent Data parent of the subtree.
1199 * @param[out] snode Schema of the node to be further parsed. Can be NULL for the opaq node.
1200 * @return LY_ERR value.
1201 */
1202static LY_ERR
1203lyb_print_model_and_hash(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node **snode)
1204{
1205 LY_ERR ret;
1206 const struct lys_module *mod;
1207
1208 if (!parent || !parent->schema) {
1209 /* top-level or opaque, read module name */
1210 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
1211 LY_CHECK_RET(ret);
1212
1213 /* read hash, find the schema node starting from mod */
1214 ret = lyb_parse_schema_hash(lybctx, NULL, mod, snode);
1215 LY_CHECK_RET(ret);
1216 } else {
1217 /* read hash, find the schema node starting from parent schema */
1218 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, snode);
1219 LY_CHECK_RET(ret);
1220 }
1221
1222 return ret;
1223}
1224
1225/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001226 * @brief Parse LYB subtree.
1227 *
1228 * @param[in] lybctx LYB context.
1229 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001230 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1231 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001232 * @return LY_ERR value.
1233 */
1234static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001235lyb_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 +02001236{
1237 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001238 struct lyd_node *node = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001239 const struct lysc_node *snode = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001240 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001241 uint32_t flags;
Radek Krejci1798aae2020-07-14 13:26:06 +02001242 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001243
1244 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001245 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001246
aPiecek37c493b2021-09-09 12:52:30 +02001247 ret = lyb_print_model_and_hash(lybctx, parent, &snode);
1248 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +02001249
aPiecek37c493b2021-09-09 12:52:30 +02001250 if (!snode) {
aPiecek821cf732021-09-09 15:37:28 +02001251 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001252 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001253 goto stop_subtree;
Michal Vasko60ea6352020-06-29 13:39:39 +02001254 } else if (snode->nodetype & LYD_NODE_TERM) {
aPiecek37c493b2021-09-09 12:52:30 +02001255 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1256 LY_CHECK_GOTO(ret, cleanup);
1257
aPiecek33fc6b02021-09-09 15:45:37 +02001258 /* read value of term node and create it */
1259 ret = lyb_create_term(lybctx, snode, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001260 LY_CHECK_GOTO(ret, cleanup);
aPiecek37c493b2021-09-09 12:52:30 +02001261
1262 /* complete the node processing */
1263 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001264 } else if (snode->nodetype & LYD_NODE_INNER) {
aPiecek37c493b2021-09-09 12:52:30 +02001265 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1266 LY_CHECK_GOTO(ret, cleanup);
1267
Michal Vasko60ea6352020-06-29 13:39:39 +02001268 /* create node */
1269 ret = lyd_create_inner(snode, &node);
1270 LY_CHECK_GOTO(ret, cleanup);
1271
1272 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +02001273 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001274 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001275 LY_CHECK_GOTO(ret, cleanup);
1276 }
1277
aPiecek0e2e1052021-09-09 15:48:27 +02001278 /* additional procedure for inner node */
1279 ret = lyb_validate_node_inner(lybctx, snode, node);
1280 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001281
Michal Vasko751cb4d2020-07-14 12:25:28 +02001282 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001283 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +02001284 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001285 }
aPiecek37c493b2021-09-09 12:52:30 +02001286
1287 /* complete the node processing */
1288 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001289 } else if (snode->nodetype & LYD_NODE_ANY) {
aPiecek18457d72021-09-09 15:52:20 +02001290 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
aPiecek37c493b2021-09-09 12:52:30 +02001291 LY_CHECK_GOTO(ret, cleanup);
aPiecek18457d72021-09-09 15:52:20 +02001292 goto stop_subtree;
Michal Vasko60ea6352020-06-29 13:39:39 +02001293 } else {
aPiecek37c493b2021-09-09 12:52:30 +02001294 LOGINT(ctx);
1295 ret = LY_EINT;
1296 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001297 }
1298
Michal Vasko60ea6352020-06-29 13:39:39 +02001299stop_subtree:
1300 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001301 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001302 LY_CHECK_GOTO(ret, cleanup);
1303
1304cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001305 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001306 lyd_free_tree(node);
1307 return ret;
1308}
1309
1310/**
1311 * @brief Parse used YANG data models.
1312 *
1313 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001314 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001315 * @return LY_ERR value.
1316 */
1317static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001318lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001319{
1320 LY_ERR ret;
1321 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001322 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001323
1324 /* read model count */
1325 lyb_read_number(&count, sizeof count, 2, lybctx);
1326
1327 if (count) {
1328 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1329
1330 /* read modules */
1331 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001332 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001333 LY_CHECK_RET(ret);
1334 LY_ARRAY_INCREMENT(lybctx->models);
1335 }
1336 }
1337
1338 return LY_SUCCESS;
1339}
1340
1341/**
1342 * @brief Parse LYB magic number.
1343 *
1344 * @param[in] lybctx LYB context.
1345 * @return LY_ERR value.
1346 */
1347static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001348lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001349{
1350 char magic_byte = 0;
1351
1352 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1353 if (magic_byte != 'l') {
1354 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1355 return LY_EINVAL;
1356 }
1357
1358 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1359 if (magic_byte != 'y') {
1360 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1361 return LY_EINVAL;
1362 }
1363
1364 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1365 if (magic_byte != 'b') {
1366 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1367 return LY_EINVAL;
1368 }
1369
1370 return LY_SUCCESS;
1371}
1372
1373/**
1374 * @brief Parse LYB header.
1375 *
1376 * @param[in] lybctx LYB context.
1377 * @return LY_ERR value.
1378 */
1379static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001380lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001381{
1382 uint8_t byte = 0;
1383
1384 /* version, future flags */
1385 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1386
1387 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1388 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001389 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001390 return LY_EINVAL;
1391 }
1392
1393 return LY_SUCCESS;
1394}
1395
Michal Vasko02ed9d82021-07-15 14:58:04 +02001396static LY_ERR
1397_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1398 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 +01001399 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001400{
Michal Vaskoe0665742021-02-11 11:08:44 +01001401 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001402 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001403
Michal Vaskoe0665742021-02-11 11:08:44 +01001404 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1405 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001406
Radek Krejci1798aae2020-07-14 13:26:06 +02001407 lybctx = calloc(1, sizeof *lybctx);
1408 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1409 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001410 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001411
Radek Krejci1798aae2020-07-14 13:26:06 +02001412 lybctx->lybctx->in = in;
1413 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001414 lybctx->parse_opts = parse_opts;
1415 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001416 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001417 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001418 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001419
1420 /* find the operation node if it exists already */
1421 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1422
Michal Vasko60ea6352020-06-29 13:39:39 +02001423 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001424 rc = lyb_parse_magic_number(lybctx->lybctx);
1425 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001426
1427 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001428 rc = lyb_parse_header(lybctx->lybctx);
1429 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001430
1431 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001432 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1433 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001434
1435 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001436 while (lybctx->lybctx->in->current[0]) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001437 rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed);
1438 LY_CHECK_GOTO(rc, cleanup);
1439
1440 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1441 break;
1442 }
1443 }
1444
1445 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1446 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1447 rc = LY_EVALID;
1448 goto cleanup;
1449 }
1450 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1451 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1452 rc = LY_EVALID;
1453 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001454 }
1455
1456 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001457 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001458
Michal Vasko60ea6352020-06-29 13:39:39 +02001459cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001460 /* there should be no unres stored if validation should be skipped */
1461 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1462 !lybctx->node_when.count));
1463
1464 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001465 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001466 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001467 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001468 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001469 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001470}
1471
Michal Vasko02ed9d82021-07-15 14:58:04 +02001472LY_ERR
1473lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1474 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1475 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
1476{
1477 uint32_t int_opts;
1478
1479 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1480 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1481
1482 switch (data_type) {
1483 case LYD_TYPE_DATA_YANG:
1484 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1485 break;
1486 case LYD_TYPE_RPC_YANG:
1487 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1488 break;
1489 case LYD_TYPE_NOTIF_YANG:
1490 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1491 break;
1492 case LYD_TYPE_REPLY_YANG:
1493 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1494 break;
1495 default:
1496 LOGINT(ctx);
1497 return LY_EINT;
1498 }
1499
1500 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1501}
1502
Michal Vasko60ea6352020-06-29 13:39:39 +02001503API int
1504lyd_lyb_data_length(const char *data)
1505{
1506 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001507 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001508 int count, i;
1509 size_t len;
1510 uint8_t buf[LYB_SIZE_MAX];
1511
1512 if (!data) {
1513 return -1;
1514 }
1515
Radek Krejci1798aae2020-07-14 13:26:06 +02001516 lybctx = calloc(1, sizeof *lybctx);
1517 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1518 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001519 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001520
1521 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001522 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001523 LY_CHECK_GOTO(ret, cleanup);
1524
1525 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001526 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001527 LY_CHECK_GOTO(ret, cleanup);
1528
1529 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001530 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001531
1532 /* read all models */
1533 for (i = 0; i < count; ++i) {
1534 /* module name length */
1535 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001536 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001537
1538 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001539 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001540
1541 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001542 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001543 }
1544
Radek Krejci1798aae2020-07-14 13:26:06 +02001545 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001546 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001547 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001548 LY_CHECK_GOTO(ret, cleanup);
1549
1550 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001551 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001552
1553 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001554 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001555 LY_CHECK_GOTO(ret, cleanup);
1556 }
1557
1558 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001559 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001560
1561cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001562 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001563
Radek Krejci1798aae2020-07-14 13:26:06 +02001564 ly_in_free(lybctx->in, 0);
1565 lylyb_ctx_free(lybctx);
1566
Michal Vasko63f3d842020-07-08 10:10:14 +02001567 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001568}