blob: 0e6bf9c0f2e46591bf4e8336b658dde188791c5d [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"
Michal Vasko9883a3e2022-03-31 12:16:00 +020033#include "plugins_exts.h"
Radek Krejci77114102021-03-10 15:21:57 +010034#include "set.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020035#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010036#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020037#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010038#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020039#include "tree_schema.h"
40#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010041#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020042
Michal Vasko02ed9d82021-07-15 14:58:04 +020043static LY_ERR _lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
44 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
45 struct ly_set *parsed, struct lyd_ctx **lydctx_p);
46
aPiecek570d7ed2021-09-10 07:15:35 +020047static LY_ERR lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed);
aPiecek821cf732021-09-09 15:37:28 +020048
Radek Krejci1798aae2020-07-14 13:26:06 +020049void
50lylyb_ctx_free(struct lylyb_ctx *ctx)
51{
52 LY_ARRAY_COUNT_TYPE u;
53
aPiecek570d7ed2021-09-10 07:15:35 +020054 LY_ARRAY_FREE(ctx->siblings);
Radek Krejci1798aae2020-07-14 13:26:06 +020055 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/**
aPiecek6828a312021-09-17 15:53:18 +020076 * @brief Read metadata about siblings.
77 *
78 * @param[out] sib Structure in which the metadata will be stored.
79 * @param[in] lybctx LYB context.
80 */
81static void
82lyb_read_sibling_meta(struct lyd_lyb_sibling *sib, struct lylyb_ctx *lybctx)
83{
84 uint8_t meta_buf[LYB_META_BYTES];
85 uint64_t num = 0;
86
87 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
88
89 memcpy(&num, meta_buf, LYB_SIZE_BYTES);
90 sib->written = le64toh(num);
91 memcpy(&num, meta_buf + LYB_SIZE_BYTES, LYB_INCHUNK_BYTES);
92 sib->inner_chunks = le64toh(num);
93
94 /* remember whether there is a following chunk or not */
95 sib->position = (sib->written == LYB_SIZE_MAX ? 1 : 0);
96}
97
98/**
Michal Vasko60ea6352020-06-29 13:39:39 +020099 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
100 *
101 * @param[in] buf Destination buffer.
102 * @param[in] count Number of bytes to read.
103 * @param[in] lybctx LYB context.
104 */
105static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200106lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200107{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200108 LY_ARRAY_COUNT_TYPE u;
aPiecek570d7ed2021-09-10 07:15:35 +0200109 struct lyd_lyb_sibling *empty;
Michal Vasko60ea6352020-06-29 13:39:39 +0200110 size_t to_read;
Michal Vasko60ea6352020-06-29 13:39:39 +0200111
112 assert(lybctx);
113
114 while (1) {
115 /* check for fully-read (empty) data chunks */
116 to_read = count;
117 empty = NULL;
aPiecek570d7ed2021-09-10 07:15:35 +0200118 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200119 /* we want the innermost chunks resolved first, so replace previous empty chunks,
120 * also ignore chunks that are completely finished, there is nothing for us to do */
aPiecek570d7ed2021-09-10 07:15:35 +0200121 if ((lybctx->siblings[u].written <= to_read) && lybctx->siblings[u].position) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200122 /* empty chunk, do not read more */
aPiecek570d7ed2021-09-10 07:15:35 +0200123 to_read = lybctx->siblings[u].written;
124 empty = &lybctx->siblings[u];
Michal Vasko60ea6352020-06-29 13:39:39 +0200125 }
126 }
127
128 if (!empty && !count) {
129 break;
130 }
131
132 /* we are actually reading some data, not just finishing another chunk */
133 if (to_read) {
134 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200135 ly_in_read(lybctx->in, buf, to_read);
136 } else {
137 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200138 }
139
aPiecek570d7ed2021-09-10 07:15:35 +0200140 LY_ARRAY_FOR(lybctx->siblings, u) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200141 /* decrease all written counters */
aPiecek570d7ed2021-09-10 07:15:35 +0200142 lybctx->siblings[u].written -= to_read;
143 assert(lybctx->siblings[u].written <= LYB_SIZE_MAX);
Michal Vasko60ea6352020-06-29 13:39:39 +0200144 }
145 /* decrease count/buf */
146 count -= to_read;
147 if (buf) {
148 buf += to_read;
149 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200150 }
151
152 if (empty) {
153 /* read the next chunk meta information */
aPiecek6828a312021-09-17 15:53:18 +0200154 lyb_read_sibling_meta(empty, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200155 }
156 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200157}
158
159/**
160 * @brief Read a number.
161 *
162 * @param[in] num Destination buffer.
163 * @param[in] num_size Size of @p num.
164 * @param[in] bytes Number of bytes to read.
165 * @param[in] lybctx LYB context.
166 */
167static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200168lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200169{
170 uint64_t buf = 0;
171
172 lyb_read((uint8_t *)&buf, bytes, lybctx);
173
174 /* correct byte order */
175 buf = le64toh(buf);
176
177 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100178 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200179 *((uint8_t *)num) = buf;
180 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100181 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200182 *((uint16_t *)num) = buf;
183 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100184 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200185 *((uint32_t *)num) = buf;
186 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100187 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200188 *((uint64_t *)num) = buf;
189 break;
190 default:
191 LOGINT(lybctx->ctx);
192 }
193}
194
195/**
196 * @brief Read a string.
197 *
198 * @param[in] str Destination buffer, is allocated.
aPieceke99345d2021-09-30 12:49:59 +0200199 * @param[in] len_size Number of bytes on which the length of the string is written.
Michal Vasko60ea6352020-06-29 13:39:39 +0200200 * @param[in] lybctx LYB context.
201 * @return LY_ERR value.
202 */
203static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200204lyb_read_string(char **str, uint8_t len_size, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200205{
aPieceke99345d2021-09-30 12:49:59 +0200206 uint64_t len = 0;
207
208 assert((len_size == 1) || (len_size == 2) || (len_size == 4) || (len_size == 8));
Michal Vasko60ea6352020-06-29 13:39:39 +0200209
210 *str = NULL;
211
aPieceke99345d2021-09-30 12:49:59 +0200212 lyb_read_number(&len, sizeof len, len_size, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200213
214 *str = malloc((len + 1) * sizeof **str);
215 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
216
217 lyb_read((uint8_t *)*str, len, lybctx);
218
Michal Vaskocebbae52021-05-31 11:11:36 +0200219 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200220 return LY_SUCCESS;
221}
222
223/**
aPiecekd5796f92021-09-30 13:39:37 +0200224 * @brief Skip string.
225 *
226 * @param[in] len_size Number of bytes on which the length of the string is written.
227 * @param[in] lybctx LYB context.
228 */
229static void
230lyb_skip_string(uint8_t len_size, struct lylyb_ctx *lybctx)
231{
232 size_t len = 0;
233
234 lyb_read_number(&len, sizeof len, len_size, lybctx);
235
236 lyb_read(NULL, len, lybctx);
237}
238
239/**
aPiecek91eec232021-09-09 15:42:37 +0200240 * @brief Read value of term node.
aPiecekea304e32021-08-18 09:13:47 +0200241 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200242 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200243 * @param[out] term_value Set to term node value in dynamically
244 * allocated memory. The caller must release it.
245 * @param[out] term_value_len Value length in bytes. The zero byte is
246 * always included and is not counted.
247 * @param[in,out] lybctx LYB context.
248 * @return LY_ERR value.
249 */
250static LY_ERR
aPieceke99345d2021-09-30 12:49:59 +0200251lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint64_t *term_value_len,
aPiecek91eec232021-09-09 15:42:37 +0200252 struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200253{
254 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200255 int32_t lyb_data_len;
256 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200257
aPiecekaa5b70a2021-08-30 08:33:25 +0200258 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200259
aPiecekaa5b70a2021-08-30 08:33:25 +0200260 /* Find out the size from @ref howtoDataLYB. */
261 if (term->type->basetype == LY_TYPE_LEAFREF) {
262 /* Leafref itself is ignored, the target is loaded directly. */
263 type_lf = (struct lysc_type_leafref *)term->type;
264 lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
265 } else {
266 lyb_data_len = term->type->plugin->lyb_data_len;
267 }
268
269 if (lyb_data_len < 0) {
270 /* Parse value size. */
271 lyb_read_number(term_value_len, sizeof *term_value_len,
272 sizeof *term_value_len, lybctx);
273 } else {
274 /* Data size is fixed. */
275 *term_value_len = lyb_data_len;
276 }
aPiecekea304e32021-08-18 09:13:47 +0200277
278 /* Allocate memory. */
279 allocated_size = *term_value_len + 1;
280 *term_value = malloc(allocated_size * sizeof **term_value);
281 LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM);
282
283 if (*term_value_len > 0) {
284 /* Parse value. */
285 lyb_read(*term_value, *term_value_len, lybctx);
286 }
287
288 /* Add extra zero byte regardless of whether it is string or not. */
289 (*term_value)[allocated_size - 1] = 0;
290
291 return LY_SUCCESS;
292}
293
294/**
aPiecek570d7ed2021-09-10 07:15:35 +0200295 * @brief Stop the current "siblings" - change LYB context state.
Michal Vasko60ea6352020-06-29 13:39:39 +0200296 *
297 * @param[in] lybctx LYB context.
298 * @return LY_ERR value.
299 */
300static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200301lyb_read_stop_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200302{
aPiecek570d7ed2021-09-10 07:15:35 +0200303 if (LYB_LAST_SIBLING(lybctx).written) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200304 LOGINT_RET(lybctx->ctx);
305 }
306
aPiecek570d7ed2021-09-10 07:15:35 +0200307 LY_ARRAY_DECREMENT(lybctx->siblings);
Michal Vasko60ea6352020-06-29 13:39:39 +0200308 return LY_SUCCESS;
309}
310
311/**
aPiecek570d7ed2021-09-10 07:15:35 +0200312 * @brief Start a new "siblings" - change LYB context state but also read the expected metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200313 *
314 * @param[in] lybctx LYB context.
315 * @return LY_ERR value.
316 */
317static LY_ERR
aPiecek570d7ed2021-09-10 07:15:35 +0200318lyb_read_start_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200319{
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200320 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200321
aPiecek570d7ed2021-09-10 07:15:35 +0200322 u = LY_ARRAY_COUNT(lybctx->siblings);
323 if (u == lybctx->sibling_size) {
324 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->siblings, u + LYB_SIBLING_STEP, LY_EMEM);
325 lybctx->sibling_size = u + LYB_SIBLING_STEP;
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 }
327
aPiecek570d7ed2021-09-10 07:15:35 +0200328 LY_ARRAY_INCREMENT(lybctx->siblings);
aPiecek6828a312021-09-17 15:53:18 +0200329 lyb_read_sibling_meta(&LYB_LAST_SIBLING(lybctx), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200330
Michal Vasko60ea6352020-06-29 13:39:39 +0200331 return LY_SUCCESS;
332}
333
334/**
Michal Vasko9883a3e2022-03-31 12:16:00 +0200335 * @brief Read YANG model info.
Michal Vasko60ea6352020-06-29 13:39:39 +0200336 *
337 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +0200338 * @param[out] mod_name Module name, if any.
339 * @param[out] mod_rev Module revision, "" if none.
Michal Vasko60ea6352020-06-29 13:39:39 +0200340 * @return LY_ERR value.
341 */
342static LY_ERR
Michal Vasko9883a3e2022-03-31 12:16:00 +0200343lyb_read_model(struct lylyb_ctx *lybctx, char **mod_name, char mod_rev[])
Michal Vasko60ea6352020-06-29 13:39:39 +0200344{
aPiecek570d7ed2021-09-10 07:15:35 +0200345 uint16_t rev, length;
Michal Vasko60ea6352020-06-29 13:39:39 +0200346
Michal Vasko9883a3e2022-03-31 12:16:00 +0200347 *mod_name = NULL;
348 mod_rev[0] = '\0';
aPiecek339bdc32021-09-10 08:42:36 +0200349
Michal Vasko9883a3e2022-03-31 12:16:00 +0200350 lyb_read_number(&length, 2, 2, lybctx);
351 if (!length) {
352 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +0200353 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200354
Michal Vasko9883a3e2022-03-31 12:16:00 +0200355 /* module name */
Michal Vasko0d156b92022-04-01 12:27:04 +0200356 *mod_name = malloc(length + 1);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200357 LY_CHECK_ERR_RET(!*mod_name, LOGMEM(lybctx->ctx), LY_EMEM);
358 lyb_read(((uint8_t *)*mod_name), length, lybctx);
359 (*mod_name)[length] = '\0';
360
361 /* module revision */
Michal Vasko60ea6352020-06-29 13:39:39 +0200362 lyb_read_number(&rev, sizeof rev, 2, lybctx);
363
Michal Vasko60ea6352020-06-29 13:39:39 +0200364 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100365 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
366 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko9883a3e2022-03-31 12:16:00 +0200367 }
368
369 return LY_SUCCESS;
370}
371
372/**
373 * @brief Parse YANG model info.
374 *
375 * @param[in] lybctx LYB context.
376 * @param[in] parse_options Flag with options for parsing.
377 * @param[out] mod Parsed module.
378 * @return LY_ERR value.
379 */
380static LY_ERR
381lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
382{
383 LY_ERR ret = LY_SUCCESS;
384 const struct lys_module *m = NULL;
385 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
386
387 /* read module info */
388 if ((ret = lyb_read_model(lybctx, &mod_name, mod_rev))) {
389 goto cleanup;
390 }
391
392 /* get the module */
393 if (mod_rev[0]) {
394 m = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
395 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !m) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200396 /* try to use an updated module */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200397 m = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
398 if (m && (!m->revision || (strcmp(m->revision, mod_rev) < 0))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200399 /* not an implemented module in a newer revision */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200400 m = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200401 }
402 }
403 } else {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200404 m = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200405 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200406
Michal Vasko9883a3e2022-03-31 12:16:00 +0200407 if (!m || !m->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200408 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko9883a3e2022-03-31 12:16:00 +0200409 if (!m) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200410 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko9883a3e2022-03-31 12:16:00 +0200411 mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : "");
412 } else if (!m->implemented) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200413 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko9883a3e2022-03-31 12:16:00 +0200414 mod_name, mod_rev[0] ? "@" : "", mod_rev[0] ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 }
416 ret = LY_EINVAL;
417 goto cleanup;
418 }
419
420 }
421
Michal Vasko9883a3e2022-03-31 12:16:00 +0200422 if (m) {
Michal Vasko85d9edc2021-04-22 09:15:05 +0200423 /* fill cached hashes, if not already */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200424 lyb_cache_module_hash(m);
Michal Vasko85d9edc2021-04-22 09:15:05 +0200425 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200426
Michal Vasko60ea6352020-06-29 13:39:39 +0200427cleanup:
Michal Vasko9883a3e2022-03-31 12:16:00 +0200428 *mod = m;
Michal Vasko60ea6352020-06-29 13:39:39 +0200429 free(mod_name);
430 return ret;
431}
432
433/**
434 * @brief Parse YANG node metadata.
435 *
436 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +0100437 * @param[in] sparent Schema parent node of the metadata.
Michal Vasko60ea6352020-06-29 13:39:39 +0200438 * @param[out] meta Parsed metadata.
439 * @return LY_ERR value.
440 */
441static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +0100442lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200443{
444 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200445 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200446 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200447 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200448 const struct lys_module *mod;
449
450 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200451 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200452
453 /* read attributes */
454 for (i = 0; i < count; ++i) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200455 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100456 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200457 LY_CHECK_GOTO(ret, cleanup);
458
459 if (!mod) {
aPiecekd5796f92021-09-30 13:39:37 +0200460 /* skip meta name */
461 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
462
463 /* skip meta value */
464 lyb_skip_string(sizeof(uint16_t), lybctx->lybctx);
465 continue;
Michal Vasko60ea6352020-06-29 13:39:39 +0200466 }
467
468 /* meta name */
aPieceke99345d2021-09-30 12:49:59 +0200469 ret = lyb_read_string(&meta_name, sizeof(uint16_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200470 LY_CHECK_GOTO(ret, cleanup);
471
472 /* meta value */
aPieceke99345d2021-09-30 12:49:59 +0200473 ret = lyb_read_string(&meta_value, sizeof(uint64_t), lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200474 LY_CHECK_GOTO(ret, cleanup);
475 dynamic = 1;
476
477 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200478 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Michal Vaskoddd76592022-01-17 13:34:48 +0100479 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA, sparent);
Michal Vasko60ea6352020-06-29 13:39:39 +0200480
481 /* free strings */
482 free(meta_name);
483 meta_name = NULL;
484 if (dynamic) {
485 free(meta_value);
486 dynamic = 0;
487 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200488
Radek Krejci1798aae2020-07-14 13:26:06 +0200489 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200490 }
491
492cleanup:
493 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200494 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200495 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200496 *meta = NULL;
497 }
498 return ret;
499}
500
501/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100502 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200503 *
504 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100505 * @param[in] format Prefix data format.
506 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200507 * @return LY_ERR value.
508 */
509static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200510lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200511{
512 LY_ERR ret = LY_SUCCESS;
513 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100514 struct ly_set *set = NULL;
515 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200516
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100517 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200518 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100519 /* read count */
520 lyb_read(&count, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200521
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100522 /* read all NS elements */
523 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200524
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100525 for (i = 0; i < count; ++i) {
526 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200527
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100528 /* prefix */
aPieceke99345d2021-09-30 12:49:59 +0200529 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, sizeof(uint16_t), lybctx), cleanup);
Michal Vaskoa3494b42022-02-28 13:58:34 +0100530 if (!strlen(ns->prefix)) {
531 free(ns->prefix);
532 ns->prefix = NULL;
533 }
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100534
535 /* namespace */
aPieceke99345d2021-09-30 12:49:59 +0200536 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, sizeof(uint16_t), lybctx), cleanup);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100537
538 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
539 ns = NULL;
540 }
541
542 *prefix_data = set;
543 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200544 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200545 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100546 /* nothing stored */
547 break;
548 default:
549 LOGINT(lybctx->ctx);
550 ret = LY_EINT;
551 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200552 }
553
554cleanup:
555 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100556 ly_free_prefix_data(format, set);
557 if (ns) {
558 free(ns->prefix);
559 free(ns->uri);
560 free(ns);
561 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200562 }
563 return ret;
564}
565
566/**
567 * @brief Parse opaque attributes.
568 *
569 * @param[in] lybctx LYB context.
570 * @param[out] attr Parsed attributes.
571 * @return LY_ERR value.
572 */
573static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200574lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200575{
576 LY_ERR ret = LY_SUCCESS;
577 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200578 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200579 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200580 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200581 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100582 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200583
584 /* read count */
585 lyb_read(&count, 1, lybctx);
586
587 /* read attributes */
588 for (i = 0; i < count; ++i) {
Michal Vasko0fdcd242020-11-11 19:12:30 +0100589 /* prefix, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200590 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200591 LY_CHECK_GOTO(ret, cleanup);
592 if (!prefix[0]) {
593 free(prefix);
594 prefix = NULL;
595 }
596
597 /* namespace, may be empty */
aPieceke99345d2021-09-30 12:49:59 +0200598 ret = lyb_read_string(&module_name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200599 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200600 if (!module_name[0]) {
601 free(module_name);
602 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200603 }
604
605 /* name */
aPieceke99345d2021-09-30 12:49:59 +0200606 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200607 LY_CHECK_GOTO(ret, cleanup);
608
Michal Vasko60ea6352020-06-29 13:39:39 +0200609 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200610 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200611
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100612 /* value prefixes */
613 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
614 LY_CHECK_GOTO(ret, cleanup);
615
Michal Vasko60ea6352020-06-29 13:39:39 +0200616 /* value */
aPieceke99345d2021-09-30 12:49:59 +0200617 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100618 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200619 dynamic = 1;
620
621 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100622 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200623 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200624 LY_CHECK_GOTO(ret, cleanup);
625
626 free(prefix);
627 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200628 free(module_name);
629 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200630 free(name);
631 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200632 assert(!dynamic);
633 value = NULL;
634
635 if (!*attr) {
636 *attr = attr2;
637 }
638
Michal Vasko60ea6352020-06-29 13:39:39 +0200639 LY_CHECK_GOTO(ret, cleanup);
640 }
641
642cleanup:
643 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200644 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200645 free(name);
646 if (dynamic) {
647 free(value);
648 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200649 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200650 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200651 *attr = NULL;
652 }
653 return ret;
654}
655
656/**
aPiecek619350d2021-09-09 16:06:59 +0200657 * @brief Fill @p hash with hash values.
658 *
659 * @param[in] lybctx LYB context.
660 * @param[in,out] hash Pointer to the array in which the hash values are to be written.
661 * @param[out] hash_count Number of hashes in @p hash.
662 * @return LY_ERR value.
663 */
664static LY_ERR
665lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
666{
667 uint8_t i = 0, j;
668
669 /* read the first hash */
670 lyb_read(&hash[0], sizeof *hash, lybctx);
671
672 if (!hash[0]) {
673 *hash_count = i + 1;
674 return LY_SUCCESS;
675 }
676
677 /* based on the first hash read all the other ones, if any */
678 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
679 if (i > LYB_HASH_BITS) {
680 LOGINT_RET(lybctx->ctx);
681 }
682 }
683
684 /* move the first hash on its accurate position */
685 hash[i] = hash[0];
686
687 /* read the rest of hashes */
688 for (j = i; j; --j) {
689 lyb_read(&hash[j - 1], sizeof *hash, lybctx);
690
691 /* correct collision ID */
692 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
693 /* preceded with zeros */
694 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
695 }
696
697 *hash_count = i + 1;
698
699 return LY_SUCCESS;
700}
701
702/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200703 * @brief Check whether a schema node matches a hash(es).
704 *
705 * @param[in] sibling Schema node to check.
706 * @param[in] hash Hash array to check.
707 * @param[in] hash_count Number of hashes in @p hash.
708 * @return non-zero if matches,
709 * @return 0 if not.
710 */
711static int
712lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
713{
714 LYB_HASH sibling_hash;
715 uint8_t i;
716
717 /* compare all the hashes starting from collision ID 0 */
718 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200719 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200720 if (sibling_hash != hash[i]) {
721 return 0;
722 }
723 }
724
725 return 1;
726}
727
728/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200729 * @brief Parse schema node hash.
730 *
731 * @param[in] lybctx LYB context.
732 * @param[in] sparent Schema parent, must be set if @p mod is not.
733 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
734 * @param[out] snode Parsed found schema node, may be NULL if opaque.
735 * @return LY_ERR value.
736 */
737static LY_ERR
738lyb_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 +0200739 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200740{
741 LY_ERR ret;
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 const struct lysc_node *sibling;
743 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200744 uint32_t getnext_opts;
aPiecek619350d2021-09-09 16:06:59 +0200745 uint8_t hash_count;
Michal Vasko60ea6352020-06-29 13:39:39 +0200746
aPiecek570d7ed2021-09-10 07:15:35 +0200747 *snode = NULL;
748
aPiecek619350d2021-09-09 16:06:59 +0200749 ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
750 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200751
752 if (!hash[0]) {
753 /* opaque node */
754 return LY_SUCCESS;
755 }
756
aPiecek619350d2021-09-09 16:06:59 +0200757 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200758
759 /* find our node with matching hashes */
760 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100761 while (1) {
762 if (!sparent && lybctx->ext) {
763 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
764 } else {
765 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
766 }
767 if (!sibling) {
768 break;
769 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200770 /* skip schema nodes from models not present during printing */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200771 if (((sibling->module->ctx != lybctx->lybctx->ctx) || lyb_has_schema_model(sibling, lybctx->lybctx->models)) &&
aPiecek619350d2021-09-09 16:06:59 +0200772 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200773 /* match found */
774 break;
775 }
776 }
777
Michal Vaskoe0665742021-02-11 11:08:44 +0100778 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100779 if (lybctx->ext) {
780 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
781 lybctx->ext->def->name);
782 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100783 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200784 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200785 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100786 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200787 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200788 }
789 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200790 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200791 return ret;
792 }
793
794 *snode = sibling;
795 return LY_SUCCESS;
796}
797
798/**
Michal Vasko9883a3e2022-03-31 12:16:00 +0200799 * @brief Parse schema node name of a nested extension data node.
800 *
801 * @param[in] lybctx LYB context.
802 * @param[in] parent Data parent.
803 * @param[in] mod_name Module name of the node.
804 * @param[out] snode Parsed found schema node of a nested extension.
805 * @return LY_ERR value.
806 */
807static LY_ERR
808lyb_parse_schema_nested_ext(struct lyd_lyb_ctx *lybctx, const struct lyd_node *parent, const char *mod_name,
809 const struct lysc_node **snode)
810{
811 LY_ERR rc = LY_SUCCESS, r;
812 char *name = NULL;
813 struct lysc_ext_instance *ext;
814
815 assert(parent);
816
817 /* read schema node name */
818 LY_CHECK_GOTO(rc = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx), cleanup);
819
820 /* check for extension data */
821 r = ly_nested_ext_schema(parent, NULL, mod_name, mod_name ? strlen(mod_name) : 0, LY_VALUE_JSON, NULL, name,
822 strlen(name), snode, &ext);
823 if (r == LY_ENOT) {
824 /* failed to parse */
825 LOGERR(lybctx->lybctx->ctx, LY_EINVAL, "Failed to parse node \"%s\" as nested extension instance data.", name);
826 rc = LY_EINVAL;
827 goto cleanup;
828 } else if (r) {
829 /* error */
830 rc = r;
831 goto cleanup;
832 }
833
834 /* fill cached hashes in the module, it may be from a different context */
835 lyb_cache_module_hash((*snode)->module);
836
837cleanup:
838 free(name);
839 return rc;
840}
841
842/**
aPiecek570d7ed2021-09-10 07:15:35 +0200843 * @brief Read until the end of the current siblings.
Michal Vasko60ea6352020-06-29 13:39:39 +0200844 *
845 * @param[in] lybctx LYB context.
846 */
847static void
aPiecek570d7ed2021-09-10 07:15:35 +0200848lyb_skip_siblings(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200849{
Michal Vasko60ea6352020-06-29 13:39:39 +0200850 do {
851 /* first skip any meta information inside */
aPiecek570d7ed2021-09-10 07:15:35 +0200852 ly_in_skip(lybctx->in, LYB_LAST_SIBLING(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200853
854 /* then read data */
aPiecek570d7ed2021-09-10 07:15:35 +0200855 lyb_read(NULL, LYB_LAST_SIBLING(lybctx).written, lybctx);
856 } while (LYB_LAST_SIBLING(lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200857}
858
859/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200860 * @brief Parse the context of anydata/anyxml node.
861 *
862 * @param[in] ctx libyang context.
863 * @param[in] data LYB data to parse.
864 * @param[out] tree Parsed tree.
865 * @return LY_ERR value.
866 */
867static LY_ERR
868lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
869{
870 LY_ERR ret;
871 uint32_t prev_lo;
872 struct ly_in *in;
873 struct lyd_ctx *lydctx = NULL;
874
875 *tree = NULL;
876
877 LY_CHECK_RET(ly_in_new_memory(data, &in));
878
879 /* turn logging off */
880 prev_lo = ly_log_options(0);
881
Michal Vasko56d88602021-07-15 16:37:59 +0200882 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 +0200883 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
884
885 /* turn logging on again */
886 ly_log_options(prev_lo);
887
888 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200889 if (lydctx) {
890 lydctx->free(lydctx);
891 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200892 if (ret) {
893 lyd_free_siblings(*tree);
894 *tree = NULL;
895 }
896 return ret;
897}
898
899/**
aPiecek37c493b2021-09-09 12:52:30 +0200900 * @brief Insert new node to @p parsed set.
901 *
902 * Also if needed, correct @p first_p.
903 *
904 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200905 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200906 * @param[in,out] node Parsed node to insertion.
907 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
908 * @param[out] parsed Set of all successfully parsed nodes.
909 * @return LY_ERR value.
910 */
911static void
912lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
913 struct ly_set *parsed)
914{
915 /* insert, keep first pointer correct */
Michal Vasko9883a3e2022-03-31 12:16:00 +0200916 if (parent && (LYD_CTX(parent) != LYD_CTX(node))) {
917 lyd_insert_ext(parent, node);
918 } else {
919 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
920 }
aPiecek37c493b2021-09-09 12:52:30 +0200921 while (!parent && (*first_p)->prev->next) {
922 *first_p = (*first_p)->prev;
923 }
924
925 /* rememeber a successfully parsed node */
926 if (parsed) {
927 ly_set_add(parsed, node, 1, NULL);
928 }
929}
930
931/**
932 * @brief Finish parsing the opaq node.
933 *
934 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200935 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200936 * @param[in] flags Node flags to set.
937 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
938 * @param[in,out] node Parsed opaq node to finish.
939 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
940 * @param[out] parsed Set of all successfully parsed nodes.
941 * @return LY_ERR value.
942 */
943static void
944lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
945 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
946{
947 struct lyd_attr *iter;
948
949 /* set flags */
950 (*node)->flags = flags;
951
952 /* add attributes */
953 assert(!(*node)->schema);
954 LY_LIST_FOR(*attr, iter) {
955 iter->parent = (struct lyd_node_opaq *)*node;
956 }
957 ((struct lyd_node_opaq *)*node)->attr = *attr;
958 *attr = NULL;
959
960 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
961 *node = NULL;
962}
963
964/**
965 * @brief Finish parsing the node.
966 *
967 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +0200968 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
aPiecek37c493b2021-09-09 12:52:30 +0200969 * @param[in] flags Node flags to set.
970 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
971 * @param[in,out] node Parsed node to finish.
972 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
973 * @param[out] parsed Set of all successfully parsed nodes.
974 * @return LY_ERR value.
975 */
976static void
977lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
978 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
979{
980 struct lyd_meta *m;
981
982 /* set flags */
983 (*node)->flags = flags;
984
985 /* add metadata */
986 LY_LIST_FOR(*meta, m) {
987 m->parent = *node;
988 }
989 (*node)->meta = *meta;
990 *meta = NULL;
991
Michal Vasko135719f2022-08-25 12:18:17 +0200992 /* insert into parent */
aPiecek37c493b2021-09-09 12:52:30 +0200993 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
Michal Vasko135719f2022-08-25 12:18:17 +0200994
Michal Vaskoeba23112022-08-26 08:35:41 +0200995 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
996 /* store for ext instance node validation, if needed */
997 (void)lyd_validate_node_ext(*node, &lybctx->ext_node);
998 }
Michal Vasko135719f2022-08-25 12:18:17 +0200999
aPiecek37c493b2021-09-09 12:52:30 +02001000 *node = NULL;
1001}
1002
1003/**
aPiecek6fdac2f2021-09-10 11:21:10 +02001004 * @brief Parse header for non-opaq node.
1005 *
1006 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +01001007 * @param[in] sparent Schema parent node of the metadata.
aPiecek6fdac2f2021-09-10 11:21:10 +02001008 * @param[out] flags Parsed node flags.
1009 * @param[out] meta Parsed metadata of the node.
1010 * @return LY_ERR value.
1011 */
1012static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001013lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparent, uint32_t *flags, struct lyd_meta **meta)
aPiecek6fdac2f2021-09-10 11:21:10 +02001014{
1015 LY_ERR ret;
1016
1017 /* create and read metadata */
Michal Vaskoddd76592022-01-17 13:34:48 +01001018 ret = lyb_parse_metadata(lybctx, sparent, meta);
aPiecek6fdac2f2021-09-10 11:21:10 +02001019 LY_CHECK_RET(ret);
1020
1021 /* read flags */
1022 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1023
1024 return ret;
1025}
1026
1027/**
aPiecek33fc6b02021-09-09 15:45:37 +02001028 * @brief Create term node and fill it with value.
1029 *
1030 * @param[in] lybctx LYB context.
1031 * @param[in] snode Schema of the term node.
1032 * @param[out] node Created term node.
1033 * @return LY_ERR value.
1034 */
1035static LY_ERR
1036lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
1037{
1038 LY_ERR ret;
1039 ly_bool dynamic;
1040 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +02001041 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +02001042
1043 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
1044 LY_CHECK_RET(ret);
1045
1046 dynamic = 1;
1047 /* create node */
1048 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
1049 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
1050 NULL, LYD_HINT_DATA, node);
1051 if (dynamic) {
1052 free(term_value);
1053 }
1054 if (ret) {
1055 lyd_free_tree(*node);
1056 *node = NULL;
1057 }
1058
1059 return ret;
1060}
1061
1062/**
aPiecek0e2e1052021-09-09 15:48:27 +02001063 * @brief Validate inner node, autodelete default values nad create implicit nodes.
1064 *
1065 * @param[in,out] lybctx LYB context.
1066 * @param[in] snode Schema of the inner node.
1067 * @param[in] node Parsed inner node.
1068 * @return LY_ERR value.
1069 */
1070static LY_ERR
1071lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
1072{
1073 LY_ERR ret = LY_SUCCESS;
1074 uint32_t impl_opts;
1075
1076 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1077 /* new node validation, autodelete CANNOT occur, all nodes are new */
1078 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
1079 LY_CHECK_RET(ret);
1080
1081 /* add any missing default children */
1082 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01001083 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_types,
Michal Vaskofcbd78f2022-08-26 08:34:15 +02001084 &lybctx->ext_node, impl_opts, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001085 LY_CHECK_RET(ret);
1086 }
1087
1088 return ret;
1089}
1090
1091/**
aPiecek821cf732021-09-09 15:37:28 +02001092 * @brief Parse opaq node.
1093 *
1094 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001095 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001096 * @param[in,out] first_p First top-level sibling.
1097 * @param[out] parsed Set of all successfully parsed nodes.
1098 * @return LY_ERR value.
1099 */
1100static LY_ERR
1101lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1102{
1103 LY_ERR ret;
1104 struct lyd_node *node = NULL;
1105 struct lyd_attr *attr = NULL;
1106 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1107 ly_bool dynamic = 0;
1108 LY_VALUE_FORMAT format = 0;
1109 void *val_prefix_data = NULL;
1110 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1111 uint32_t flags;
1112
aPiecek821cf732021-09-09 15:37:28 +02001113 /* parse opaq node attributes */
1114 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1115 LY_CHECK_GOTO(ret, cleanup);
1116
1117 /* read flags */
1118 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1119
1120 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001121 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001122 LY_CHECK_GOTO(ret, cleanup);
1123
1124 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001125 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001126 LY_CHECK_GOTO(ret, cleanup);
1127
1128 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001129 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001130 LY_CHECK_GOTO(ret, cleanup);
1131
1132 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001133 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001134 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1135 dynamic = 1;
1136
1137 /* parse format */
1138 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1139
1140 /* parse value prefixes */
1141 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1142 LY_CHECK_GOTO(ret, cleanup);
1143
aPiecek570d7ed2021-09-10 07:15:35 +02001144 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001145 /* skip children */
1146 ret = lyb_read_start_siblings(lybctx->lybctx);
1147 LY_CHECK_GOTO(ret, cleanup);
1148 lyb_skip_siblings(lybctx->lybctx);
1149 ret = lyb_read_stop_siblings(lybctx->lybctx);
1150 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001151 goto cleanup;
1152 }
1153
aPiecek821cf732021-09-09 15:37:28 +02001154 /* create node */
1155 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1156 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1157 LY_CHECK_GOTO(ret, cleanup);
1158
1159 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001160 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1161 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001162
1163 /* register parsed opaq node */
1164 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1165 assert(!attr && !node);
1166
1167cleanup:
1168 free(prefix);
1169 free(module_key);
1170 free(name);
1171 if (dynamic) {
1172 free(value);
1173 }
1174 lyd_free_attr_siblings(ctx, attr);
1175 lyd_free_tree(node);
1176
1177 return ret;
1178}
1179
aPiecek18457d72021-09-09 15:52:20 +02001180/**
1181 * @brief Parse anydata or anyxml node.
1182 *
1183 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001184 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001185 * @param[in] snode Schema of the node to be parsed.
1186 * @param[in,out] first_p First top-level sibling.
1187 * @param[out] parsed Set of all successfully parsed nodes.
1188 * @return LY_ERR value.
1189 */
1190static LY_ERR
1191lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1192 struct lyd_node **first_p, struct ly_set *parsed)
1193{
1194 LY_ERR ret;
1195 struct lyd_node *node = NULL, *tree;
1196 struct lyd_meta *meta = NULL;
1197 LYD_ANYDATA_VALUETYPE value_type;
1198 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001199 uint32_t flags;
1200 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1201
1202 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001203 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001204 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001205
1206 /* parse value type */
1207 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1208 if (value_type == LYD_ANYDATA_DATATREE) {
1209 /* invalid situation */
1210 LOGINT(ctx);
1211 ret = LY_EINT;
1212 goto error;
1213 }
1214
1215 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001216 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001217 LY_CHECK_GOTO(ret, error);
1218
1219 if (value_type == LYD_ANYDATA_LYB) {
1220 /* try to parse LYB into a data tree */
1221 if (!lyb_parse_any_content(ctx, value, &tree)) {
1222 /* successfully parsed */
1223 free(value);
1224 value = (char *)tree;
1225 value_type = LYD_ANYDATA_DATATREE;
1226 }
1227 }
1228
1229 /* create the node */
1230 switch (value_type) {
1231 case LYD_ANYDATA_LYB:
1232 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001233 case LYD_ANYDATA_STRING:
1234 case LYD_ANYDATA_XML:
1235 case LYD_ANYDATA_JSON:
Michal Vasko742a5b12022-02-24 16:07:27 +01001236 /* use the value directly */
1237 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001238 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001239 break;
1240 default:
1241 LOGINT(ctx);
1242 ret = LY_EINT;
1243 goto error;
1244 }
1245
1246 /* register parsed anydata node */
1247 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1248
1249 return LY_SUCCESS;
1250
1251error:
1252 free(value);
1253 lyd_free_meta_siblings(meta);
1254 lyd_free_tree(node);
1255 return ret;
1256}
aPiecek821cf732021-09-09 15:37:28 +02001257
1258/**
aPiecek5777f122021-09-10 10:26:23 +02001259 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001260 *
1261 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001262 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1263 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001264 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1265 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001266 * @return LY_ERR value.
1267 */
1268static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001269lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001270 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001271{
1272 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001273 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001274 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001275 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001276
aPiecek307f0772021-09-10 09:09:47 +02001277 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001278 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001279 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001280
aPiecek5777f122021-09-10 10:26:23 +02001281 /* create node */
1282 ret = lyd_create_inner(snode, &node);
1283 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001284
aPiecek5777f122021-09-10 10:26:23 +02001285 /* process children */
1286 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1287 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001288
aPiecek5777f122021-09-10 10:26:23 +02001289 /* additional procedure for inner node */
1290 ret = lyb_validate_node_inner(lybctx, snode, node);
1291 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001292
aPiecek5777f122021-09-10 10:26:23 +02001293 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1294 /* rememeber the RPC/action/notification */
1295 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001296 }
1297
aPiecek307f0772021-09-10 09:09:47 +02001298 /* register parsed node */
1299 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1300
1301 return LY_SUCCESS;
1302
1303error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001304 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001305 lyd_free_tree(node);
1306 return ret;
1307}
1308
1309/**
aPiecek8a555d72021-09-10 10:15:26 +02001310 * @brief Parse leaf node.
1311 *
1312 * @param[in] lybctx LYB context.
1313 * @param[in] parent Data parent of the sibling.
1314 * @param[in] snode Schema of the node to be parsed.
1315 * @param[in,out] first_p First top-level sibling.
1316 * @param[out] parsed Set of all successfully parsed nodes.
1317 * @return LY_ERR value.
1318 */
1319static LY_ERR
1320lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1321 struct lyd_node **first_p, struct ly_set *parsed)
1322{
1323 LY_ERR ret;
1324 struct lyd_node *node = NULL;
1325 struct lyd_meta *meta = NULL;
1326 uint32_t flags;
1327
1328 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001329 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001330 LY_CHECK_GOTO(ret, error);
1331
1332 /* read value of term node and create it */
1333 ret = lyb_create_term(lybctx, snode, &node);
1334 LY_CHECK_GOTO(ret, error);
1335
1336 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1337
1338 return LY_SUCCESS;
1339
1340error:
1341 lyd_free_meta_siblings(meta);
1342 lyd_free_tree(node);
1343 return ret;
1344}
1345
1346/**
aPiecek5777f122021-09-10 10:26:23 +02001347 * @brief Parse all leaflist nodes which belong to same schema.
1348 *
1349 * @param[in] lybctx LYB context.
1350 * @param[in] parent Data parent of the sibling.
1351 * @param[in] snode Schema of the nodes to be parsed.
1352 * @param[in,out] first_p First top-level sibling.
1353 * @param[out] parsed Set of all successfully parsed nodes.
1354 * @return LY_ERR value.
1355 */
1356static LY_ERR
1357lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1358 struct lyd_node **first_p, struct ly_set *parsed)
1359{
1360 LY_ERR ret;
1361
1362 /* register a new sibling */
1363 ret = lyb_read_start_siblings(lybctx->lybctx);
1364 LY_CHECK_RET(ret);
1365
1366 /* process all siblings */
1367 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1368 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1369 LY_CHECK_RET(ret);
1370 }
1371
1372 /* end the sibling */
1373 ret = lyb_read_stop_siblings(lybctx->lybctx);
1374 LY_CHECK_RET(ret);
1375
1376 return ret;
1377}
1378
1379/**
aPiecek77d3e962021-09-10 10:33:59 +02001380 * @brief Parse all list nodes which belong to same schema.
1381 *
1382 * @param[in] lybctx LYB context.
1383 * @param[in] parent Data parent of the sibling.
1384 * @param[in] snode Schema of the nodes to be parsed.
1385 * @param[in,out] first_p First top-level sibling.
1386 * @param[out] parsed Set of all successfully parsed nodes.
1387 * @return LY_ERR value.
1388 */
1389static LY_ERR
1390lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1391 struct lyd_node **first_p, struct ly_set *parsed)
1392{
1393 LY_ERR ret;
1394 struct lyd_node *node = NULL;
1395 struct lyd_meta *meta = NULL;
1396 uint32_t flags;
1397
1398 /* register a new sibling */
1399 ret = lyb_read_start_siblings(lybctx->lybctx);
1400 LY_CHECK_RET(ret);
1401
1402 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1403 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001404 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001405 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001406
1407 /* create list node */
1408 ret = lyd_create_inner(snode, &node);
1409 LY_CHECK_GOTO(ret, error);
1410
1411 /* process children */
1412 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1413 LY_CHECK_GOTO(ret, error);
1414
1415 /* additional procedure for inner node */
1416 ret = lyb_validate_node_inner(lybctx, snode, node);
1417 LY_CHECK_GOTO(ret, error);
1418
1419 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1420 /* rememeber the RPC/action/notification */
1421 lybctx->op_node = node;
1422 }
1423
1424 /* register parsed list node */
1425 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1426 }
1427
1428 /* end the sibling */
1429 ret = lyb_read_stop_siblings(lybctx->lybctx);
1430 LY_CHECK_RET(ret);
1431
1432 return LY_SUCCESS;
1433
1434error:
1435 lyd_free_meta_siblings(meta);
1436 lyd_free_tree(node);
1437 return ret;
1438}
1439
1440/**
Michal Vasko9883a3e2022-03-31 12:16:00 +02001441 * @brief Parse a node.
aPiecek17737b52021-09-21 12:29:52 +02001442 *
aPiecek17737b52021-09-21 12:29:52 +02001443 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +02001444 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1445 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1446 * @param[in,out] parsed Set of all successfully parsed nodes to add to.
aPiecek17737b52021-09-21 12:29:52 +02001447 * @return LY_ERR value.
1448 */
1449static LY_ERR
1450lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1451 struct ly_set *parsed)
1452{
1453 LY_ERR ret;
1454 const struct lysc_node *snode;
1455 const struct lys_module *mod;
Michal Vasko9883a3e2022-03-31 12:16:00 +02001456 enum lylyb_node_type lyb_type;
1457 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek17737b52021-09-21 12:29:52 +02001458
Michal Vasko9883a3e2022-03-31 12:16:00 +02001459 /* read node type */
1460 lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx);
1461
1462 switch (lyb_type) {
1463 case LYB_NODE_TOP:
1464 /* top-level, read module name */
1465 LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod), cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001466
1467 /* read hash, find the schema node starting from mod */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001468 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup);
1469 break;
1470 case LYB_NODE_CHILD:
1471 case LYB_NODE_OPAQ:
1472 /* read hash, find the schema node starting from parent schema, if any */
1473 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, parent ? parent->schema : NULL, NULL, &snode), cleanup);
1474 break;
1475 case LYB_NODE_EXT:
1476 /* ext, read module name */
1477 LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev), cleanup);
1478
1479 /* read schema node name, find the nexted ext schema node */
1480 LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup);
1481 break;
aPiecek17737b52021-09-21 12:29:52 +02001482 }
aPiecek17737b52021-09-21 12:29:52 +02001483
1484 if (!snode) {
1485 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1486 } else if (snode->nodetype & LYS_LEAFLIST) {
1487 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1488 } else if (snode->nodetype == LYS_LIST) {
1489 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1490 } else if (snode->nodetype & LYD_NODE_ANY) {
1491 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1492 } else if (snode->nodetype & LYD_NODE_INNER) {
1493 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1494 } else {
1495 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1496 }
Michal Vasko9883a3e2022-03-31 12:16:00 +02001497 LY_CHECK_GOTO(ret, cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001498
Michal Vasko9883a3e2022-03-31 12:16:00 +02001499cleanup:
1500 free(mod_name);
aPiecek17737b52021-09-21 12:29:52 +02001501 return ret;
1502}
1503
1504/**
aPiecek570d7ed2021-09-10 07:15:35 +02001505 * @brief Parse siblings (@ref lyb_print_siblings()).
1506 *
1507 * @param[in] lybctx LYB context.
1508 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1509 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1510 * @param[out] parsed Set of all successfully parsed nodes.
1511 * @return LY_ERR value.
1512 */
1513static LY_ERR
1514lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1515 struct ly_set *parsed)
1516{
aPiecek570d7ed2021-09-10 07:15:35 +02001517 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001518
1519 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1520
1521 /* register a new siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001522 LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001523
1524 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
Michal Vasko9883a3e2022-03-31 12:16:00 +02001525 LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed));
aPiecek570d7ed2021-09-10 07:15:35 +02001526
1527 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1528 break;
1529 }
1530 }
1531
1532 /* end the siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001533 LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001534
Michal Vasko9883a3e2022-03-31 12:16:00 +02001535 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +02001536}
1537
1538/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001539 * @brief Parse used YANG data models.
1540 *
1541 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001542 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001543 * @return LY_ERR value.
1544 */
1545static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001546lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001547{
1548 LY_ERR ret;
1549 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001550 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001551
1552 /* read model count */
1553 lyb_read_number(&count, sizeof count, 2, lybctx);
1554
1555 if (count) {
1556 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1557
1558 /* read modules */
1559 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001560 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001561 LY_CHECK_RET(ret);
1562 LY_ARRAY_INCREMENT(lybctx->models);
1563 }
1564 }
1565
1566 return LY_SUCCESS;
1567}
1568
1569/**
1570 * @brief Parse LYB magic number.
1571 *
1572 * @param[in] lybctx LYB context.
1573 * @return LY_ERR value.
1574 */
1575static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001576lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001577{
1578 char magic_byte = 0;
1579
1580 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1581 if (magic_byte != 'l') {
1582 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1583 return LY_EINVAL;
1584 }
1585
1586 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1587 if (magic_byte != 'y') {
1588 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1589 return LY_EINVAL;
1590 }
1591
1592 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1593 if (magic_byte != 'b') {
1594 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1595 return LY_EINVAL;
1596 }
1597
1598 return LY_SUCCESS;
1599}
1600
1601/**
1602 * @brief Parse LYB header.
1603 *
1604 * @param[in] lybctx LYB context.
1605 * @return LY_ERR value.
1606 */
1607static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001608lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001609{
1610 uint8_t byte = 0;
1611
1612 /* version, future flags */
1613 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1614
1615 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1616 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001617 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001618 return LY_EINVAL;
1619 }
1620
1621 return LY_SUCCESS;
1622}
1623
Michal Vasko02ed9d82021-07-15 14:58:04 +02001624static LY_ERR
1625_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1626 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 +01001627 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001628{
Michal Vaskoe0665742021-02-11 11:08:44 +01001629 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001630 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001631
Michal Vaskoe0665742021-02-11 11:08:44 +01001632 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1633 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001634
Radek Krejci1798aae2020-07-14 13:26:06 +02001635 lybctx = calloc(1, sizeof *lybctx);
1636 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1637 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001638 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001639
Radek Krejci1798aae2020-07-14 13:26:06 +02001640 lybctx->lybctx->in = in;
1641 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001642 lybctx->parse_opts = parse_opts;
1643 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001644 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001645 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001646 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001647
1648 /* find the operation node if it exists already */
1649 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1650
Michal Vasko60ea6352020-06-29 13:39:39 +02001651 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001652 rc = lyb_parse_magic_number(lybctx->lybctx);
1653 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001654
1655 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001656 rc = lyb_parse_header(lybctx->lybctx);
1657 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001658
1659 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001660 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1661 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001662
aPiecek570d7ed2021-09-10 07:15:35 +02001663 /* read sibling(s) */
1664 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1665 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001666
1667 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1668 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1669 rc = LY_EVALID;
1670 goto cleanup;
1671 }
1672 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1673 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1674 rc = LY_EVALID;
1675 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001676 }
1677
1678 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001679 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001680
Michal Vasko60ea6352020-06-29 13:39:39 +02001681cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001682 /* there should be no unres stored if validation should be skipped */
1683 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1684 !lybctx->node_when.count));
1685
1686 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001687 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001688 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001689 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001690 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001691 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001692}
1693
Michal Vasko02ed9d82021-07-15 14:58:04 +02001694LY_ERR
1695lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1696 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
Michal Vaskoddd76592022-01-17 13:34:48 +01001697 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko02ed9d82021-07-15 14:58:04 +02001698{
1699 uint32_t int_opts;
1700
1701 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1702 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1703
Michal Vaskoddd76592022-01-17 13:34:48 +01001704 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1705
Michal Vasko02ed9d82021-07-15 14:58:04 +02001706 switch (data_type) {
1707 case LYD_TYPE_DATA_YANG:
1708 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1709 break;
1710 case LYD_TYPE_RPC_YANG:
1711 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1712 break;
1713 case LYD_TYPE_NOTIF_YANG:
1714 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1715 break;
1716 case LYD_TYPE_REPLY_YANG:
1717 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1718 break;
1719 default:
1720 LOGINT(ctx);
1721 return LY_EINT;
1722 }
1723
Michal Vaskoddd76592022-01-17 13:34:48 +01001724 if (subtree_sibling) {
1725 *subtree_sibling = 0;
1726 }
Michal Vasko02ed9d82021-07-15 14:58:04 +02001727 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1728}
1729
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001730LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001731lyd_lyb_data_length(const char *data)
1732{
1733 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001734 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001735 int count, i;
1736 size_t len;
1737 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001738 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001739
1740 if (!data) {
1741 return -1;
1742 }
1743
Radek Krejci1798aae2020-07-14 13:26:06 +02001744 lybctx = calloc(1, sizeof *lybctx);
1745 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1746 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001747 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001748
1749 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001750 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001751 LY_CHECK_GOTO(ret, cleanup);
1752
1753 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001754 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001755 LY_CHECK_GOTO(ret, cleanup);
1756
1757 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001758 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001759
1760 /* read all models */
1761 for (i = 0; i < count; ++i) {
1762 /* module name length */
1763 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001764 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001765
1766 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001767 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001768
1769 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001770 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001771 }
1772
aPiecek6828a312021-09-17 15:53:18 +02001773 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001774 /* register a new sibling */
1775 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001776 LY_CHECK_GOTO(ret, cleanup);
1777
1778 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001779 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001780
aPiecek570d7ed2021-09-10 07:15:35 +02001781 /* sibling finished */
1782 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001783 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001784 } else {
aPiecek6828a312021-09-17 15:53:18 +02001785 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001786 }
1787
1788 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001789 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001790
1791cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001792 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001793
Radek Krejci1798aae2020-07-14 13:26:06 +02001794 ly_in_free(lybctx->in, 0);
1795 lylyb_ctx_free(lybctx);
1796
Michal Vasko63f3d842020-07-08 10:10:14 +02001797 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001798}