blob: ddfb5c059424cc7940df2f8de72078ed8b1486cd [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
995 /* store for ext instance node validation, if needed */
996 (void)lyd_validate_node_ext(*node, &lybctx->ext_node);
997
aPiecek37c493b2021-09-09 12:52:30 +0200998 *node = NULL;
999}
1000
1001/**
aPiecek6fdac2f2021-09-10 11:21:10 +02001002 * @brief Parse header for non-opaq node.
1003 *
1004 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +01001005 * @param[in] sparent Schema parent node of the metadata.
aPiecek6fdac2f2021-09-10 11:21:10 +02001006 * @param[out] flags Parsed node flags.
1007 * @param[out] meta Parsed metadata of the node.
1008 * @return LY_ERR value.
1009 */
1010static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001011lyb_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 +02001012{
1013 LY_ERR ret;
1014
1015 /* create and read metadata */
Michal Vaskoddd76592022-01-17 13:34:48 +01001016 ret = lyb_parse_metadata(lybctx, sparent, meta);
aPiecek6fdac2f2021-09-10 11:21:10 +02001017 LY_CHECK_RET(ret);
1018
1019 /* read flags */
1020 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1021
1022 return ret;
1023}
1024
1025/**
aPiecek33fc6b02021-09-09 15:45:37 +02001026 * @brief Create term node and fill it with value.
1027 *
1028 * @param[in] lybctx LYB context.
1029 * @param[in] snode Schema of the term node.
1030 * @param[out] node Created term node.
1031 * @return LY_ERR value.
1032 */
1033static LY_ERR
1034lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
1035{
1036 LY_ERR ret;
1037 ly_bool dynamic;
1038 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +02001039 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +02001040
1041 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
1042 LY_CHECK_RET(ret);
1043
1044 dynamic = 1;
1045 /* create node */
1046 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
1047 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
1048 NULL, LYD_HINT_DATA, node);
1049 if (dynamic) {
1050 free(term_value);
1051 }
1052 if (ret) {
1053 lyd_free_tree(*node);
1054 *node = NULL;
1055 }
1056
1057 return ret;
1058}
1059
1060/**
aPiecek0e2e1052021-09-09 15:48:27 +02001061 * @brief Validate inner node, autodelete default values nad create implicit nodes.
1062 *
1063 * @param[in,out] lybctx LYB context.
1064 * @param[in] snode Schema of the inner node.
1065 * @param[in] node Parsed inner node.
1066 * @return LY_ERR value.
1067 */
1068static LY_ERR
1069lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
1070{
1071 LY_ERR ret = LY_SUCCESS;
1072 uint32_t impl_opts;
1073
1074 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1075 /* new node validation, autodelete CANNOT occur, all nodes are new */
1076 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
1077 LY_CHECK_RET(ret);
1078
1079 /* add any missing default children */
1080 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01001081 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 +02001082 &lybctx->ext_node, impl_opts, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001083 LY_CHECK_RET(ret);
1084 }
1085
1086 return ret;
1087}
1088
1089/**
aPiecek821cf732021-09-09 15:37:28 +02001090 * @brief Parse opaq node.
1091 *
1092 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001093 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001094 * @param[in,out] first_p First top-level sibling.
1095 * @param[out] parsed Set of all successfully parsed nodes.
1096 * @return LY_ERR value.
1097 */
1098static LY_ERR
1099lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1100{
1101 LY_ERR ret;
1102 struct lyd_node *node = NULL;
1103 struct lyd_attr *attr = NULL;
1104 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1105 ly_bool dynamic = 0;
1106 LY_VALUE_FORMAT format = 0;
1107 void *val_prefix_data = NULL;
1108 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1109 uint32_t flags;
1110
aPiecek821cf732021-09-09 15:37:28 +02001111 /* parse opaq node attributes */
1112 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1113 LY_CHECK_GOTO(ret, cleanup);
1114
1115 /* read flags */
1116 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1117
1118 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001119 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001120 LY_CHECK_GOTO(ret, cleanup);
1121
1122 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001123 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001124 LY_CHECK_GOTO(ret, cleanup);
1125
1126 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001127 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001128 LY_CHECK_GOTO(ret, cleanup);
1129
1130 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001131 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001132 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1133 dynamic = 1;
1134
1135 /* parse format */
1136 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1137
1138 /* parse value prefixes */
1139 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1140 LY_CHECK_GOTO(ret, cleanup);
1141
aPiecek570d7ed2021-09-10 07:15:35 +02001142 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001143 /* skip children */
1144 ret = lyb_read_start_siblings(lybctx->lybctx);
1145 LY_CHECK_GOTO(ret, cleanup);
1146 lyb_skip_siblings(lybctx->lybctx);
1147 ret = lyb_read_stop_siblings(lybctx->lybctx);
1148 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001149 goto cleanup;
1150 }
1151
aPiecek821cf732021-09-09 15:37:28 +02001152 /* create node */
1153 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1154 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1155 LY_CHECK_GOTO(ret, cleanup);
1156
1157 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001158 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1159 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001160
1161 /* register parsed opaq node */
1162 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1163 assert(!attr && !node);
1164
1165cleanup:
1166 free(prefix);
1167 free(module_key);
1168 free(name);
1169 if (dynamic) {
1170 free(value);
1171 }
1172 lyd_free_attr_siblings(ctx, attr);
1173 lyd_free_tree(node);
1174
1175 return ret;
1176}
1177
aPiecek18457d72021-09-09 15:52:20 +02001178/**
1179 * @brief Parse anydata or anyxml node.
1180 *
1181 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001182 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001183 * @param[in] snode Schema of the node to be parsed.
1184 * @param[in,out] first_p First top-level sibling.
1185 * @param[out] parsed Set of all successfully parsed nodes.
1186 * @return LY_ERR value.
1187 */
1188static LY_ERR
1189lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1190 struct lyd_node **first_p, struct ly_set *parsed)
1191{
1192 LY_ERR ret;
1193 struct lyd_node *node = NULL, *tree;
1194 struct lyd_meta *meta = NULL;
1195 LYD_ANYDATA_VALUETYPE value_type;
1196 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001197 uint32_t flags;
1198 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1199
1200 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001201 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001202 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001203
1204 /* parse value type */
1205 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1206 if (value_type == LYD_ANYDATA_DATATREE) {
1207 /* invalid situation */
1208 LOGINT(ctx);
1209 ret = LY_EINT;
1210 goto error;
1211 }
1212
1213 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001214 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001215 LY_CHECK_GOTO(ret, error);
1216
1217 if (value_type == LYD_ANYDATA_LYB) {
1218 /* try to parse LYB into a data tree */
1219 if (!lyb_parse_any_content(ctx, value, &tree)) {
1220 /* successfully parsed */
1221 free(value);
1222 value = (char *)tree;
1223 value_type = LYD_ANYDATA_DATATREE;
1224 }
1225 }
1226
1227 /* create the node */
1228 switch (value_type) {
1229 case LYD_ANYDATA_LYB:
1230 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001231 case LYD_ANYDATA_STRING:
1232 case LYD_ANYDATA_XML:
1233 case LYD_ANYDATA_JSON:
Michal Vasko742a5b12022-02-24 16:07:27 +01001234 /* use the value directly */
1235 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001236 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001237 break;
1238 default:
1239 LOGINT(ctx);
1240 ret = LY_EINT;
1241 goto error;
1242 }
1243
1244 /* register parsed anydata node */
1245 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1246
1247 return LY_SUCCESS;
1248
1249error:
1250 free(value);
1251 lyd_free_meta_siblings(meta);
1252 lyd_free_tree(node);
1253 return ret;
1254}
aPiecek821cf732021-09-09 15:37:28 +02001255
1256/**
aPiecek5777f122021-09-10 10:26:23 +02001257 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001258 *
1259 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001260 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1261 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001262 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1263 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001264 * @return LY_ERR value.
1265 */
1266static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001267lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001268 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001269{
1270 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001271 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001272 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001273 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001274
aPiecek307f0772021-09-10 09:09:47 +02001275 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001276 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001277 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001278
aPiecek5777f122021-09-10 10:26:23 +02001279 /* create node */
1280 ret = lyd_create_inner(snode, &node);
1281 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001282
aPiecek5777f122021-09-10 10:26:23 +02001283 /* process children */
1284 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1285 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001286
aPiecek5777f122021-09-10 10:26:23 +02001287 /* additional procedure for inner node */
1288 ret = lyb_validate_node_inner(lybctx, snode, node);
1289 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001290
aPiecek5777f122021-09-10 10:26:23 +02001291 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1292 /* rememeber the RPC/action/notification */
1293 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001294 }
1295
aPiecek307f0772021-09-10 09:09:47 +02001296 /* register parsed node */
1297 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1298
1299 return LY_SUCCESS;
1300
1301error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001302 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001303 lyd_free_tree(node);
1304 return ret;
1305}
1306
1307/**
aPiecek8a555d72021-09-10 10:15:26 +02001308 * @brief Parse leaf node.
1309 *
1310 * @param[in] lybctx LYB context.
1311 * @param[in] parent Data parent of the sibling.
1312 * @param[in] snode Schema of the node to be parsed.
1313 * @param[in,out] first_p First top-level sibling.
1314 * @param[out] parsed Set of all successfully parsed nodes.
1315 * @return LY_ERR value.
1316 */
1317static LY_ERR
1318lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1319 struct lyd_node **first_p, struct ly_set *parsed)
1320{
1321 LY_ERR ret;
1322 struct lyd_node *node = NULL;
1323 struct lyd_meta *meta = NULL;
1324 uint32_t flags;
1325
1326 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001327 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001328 LY_CHECK_GOTO(ret, error);
1329
1330 /* read value of term node and create it */
1331 ret = lyb_create_term(lybctx, snode, &node);
1332 LY_CHECK_GOTO(ret, error);
1333
1334 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1335
1336 return LY_SUCCESS;
1337
1338error:
1339 lyd_free_meta_siblings(meta);
1340 lyd_free_tree(node);
1341 return ret;
1342}
1343
1344/**
aPiecek5777f122021-09-10 10:26:23 +02001345 * @brief Parse all leaflist nodes which belong to same schema.
1346 *
1347 * @param[in] lybctx LYB context.
1348 * @param[in] parent Data parent of the sibling.
1349 * @param[in] snode Schema of the nodes to be parsed.
1350 * @param[in,out] first_p First top-level sibling.
1351 * @param[out] parsed Set of all successfully parsed nodes.
1352 * @return LY_ERR value.
1353 */
1354static LY_ERR
1355lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1356 struct lyd_node **first_p, struct ly_set *parsed)
1357{
1358 LY_ERR ret;
1359
1360 /* register a new sibling */
1361 ret = lyb_read_start_siblings(lybctx->lybctx);
1362 LY_CHECK_RET(ret);
1363
1364 /* process all siblings */
1365 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1366 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1367 LY_CHECK_RET(ret);
1368 }
1369
1370 /* end the sibling */
1371 ret = lyb_read_stop_siblings(lybctx->lybctx);
1372 LY_CHECK_RET(ret);
1373
1374 return ret;
1375}
1376
1377/**
aPiecek77d3e962021-09-10 10:33:59 +02001378 * @brief Parse all list nodes which belong to same schema.
1379 *
1380 * @param[in] lybctx LYB context.
1381 * @param[in] parent Data parent of the sibling.
1382 * @param[in] snode Schema of the nodes to be parsed.
1383 * @param[in,out] first_p First top-level sibling.
1384 * @param[out] parsed Set of all successfully parsed nodes.
1385 * @return LY_ERR value.
1386 */
1387static LY_ERR
1388lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1389 struct lyd_node **first_p, struct ly_set *parsed)
1390{
1391 LY_ERR ret;
1392 struct lyd_node *node = NULL;
1393 struct lyd_meta *meta = NULL;
1394 uint32_t flags;
1395
1396 /* register a new sibling */
1397 ret = lyb_read_start_siblings(lybctx->lybctx);
1398 LY_CHECK_RET(ret);
1399
1400 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1401 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001402 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001403 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001404
1405 /* create list node */
1406 ret = lyd_create_inner(snode, &node);
1407 LY_CHECK_GOTO(ret, error);
1408
1409 /* process children */
1410 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1411 LY_CHECK_GOTO(ret, error);
1412
1413 /* additional procedure for inner node */
1414 ret = lyb_validate_node_inner(lybctx, snode, node);
1415 LY_CHECK_GOTO(ret, error);
1416
1417 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1418 /* rememeber the RPC/action/notification */
1419 lybctx->op_node = node;
1420 }
1421
1422 /* register parsed list node */
1423 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1424 }
1425
1426 /* end the sibling */
1427 ret = lyb_read_stop_siblings(lybctx->lybctx);
1428 LY_CHECK_RET(ret);
1429
1430 return LY_SUCCESS;
1431
1432error:
1433 lyd_free_meta_siblings(meta);
1434 lyd_free_tree(node);
1435 return ret;
1436}
1437
1438/**
Michal Vasko9883a3e2022-03-31 12:16:00 +02001439 * @brief Parse a node.
aPiecek17737b52021-09-21 12:29:52 +02001440 *
aPiecek17737b52021-09-21 12:29:52 +02001441 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +02001442 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1443 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1444 * @param[in,out] parsed Set of all successfully parsed nodes to add to.
aPiecek17737b52021-09-21 12:29:52 +02001445 * @return LY_ERR value.
1446 */
1447static LY_ERR
1448lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1449 struct ly_set *parsed)
1450{
1451 LY_ERR ret;
1452 const struct lysc_node *snode;
1453 const struct lys_module *mod;
Michal Vasko9883a3e2022-03-31 12:16:00 +02001454 enum lylyb_node_type lyb_type;
1455 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek17737b52021-09-21 12:29:52 +02001456
Michal Vasko9883a3e2022-03-31 12:16:00 +02001457 /* read node type */
1458 lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx);
1459
1460 switch (lyb_type) {
1461 case LYB_NODE_TOP:
1462 /* top-level, read module name */
1463 LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod), cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001464
1465 /* read hash, find the schema node starting from mod */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001466 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup);
1467 break;
1468 case LYB_NODE_CHILD:
1469 case LYB_NODE_OPAQ:
1470 /* read hash, find the schema node starting from parent schema, if any */
1471 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, parent ? parent->schema : NULL, NULL, &snode), cleanup);
1472 break;
1473 case LYB_NODE_EXT:
1474 /* ext, read module name */
1475 LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev), cleanup);
1476
1477 /* read schema node name, find the nexted ext schema node */
1478 LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup);
1479 break;
aPiecek17737b52021-09-21 12:29:52 +02001480 }
aPiecek17737b52021-09-21 12:29:52 +02001481
1482 if (!snode) {
1483 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1484 } else if (snode->nodetype & LYS_LEAFLIST) {
1485 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1486 } else if (snode->nodetype == LYS_LIST) {
1487 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1488 } else if (snode->nodetype & LYD_NODE_ANY) {
1489 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1490 } else if (snode->nodetype & LYD_NODE_INNER) {
1491 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1492 } else {
1493 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1494 }
Michal Vasko9883a3e2022-03-31 12:16:00 +02001495 LY_CHECK_GOTO(ret, cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001496
Michal Vasko9883a3e2022-03-31 12:16:00 +02001497cleanup:
1498 free(mod_name);
aPiecek17737b52021-09-21 12:29:52 +02001499 return ret;
1500}
1501
1502/**
aPiecek570d7ed2021-09-10 07:15:35 +02001503 * @brief Parse siblings (@ref lyb_print_siblings()).
1504 *
1505 * @param[in] lybctx LYB context.
1506 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1507 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1508 * @param[out] parsed Set of all successfully parsed nodes.
1509 * @return LY_ERR value.
1510 */
1511static LY_ERR
1512lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1513 struct ly_set *parsed)
1514{
aPiecek570d7ed2021-09-10 07:15:35 +02001515 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001516
1517 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1518
1519 /* register a new siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001520 LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001521
1522 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
Michal Vasko9883a3e2022-03-31 12:16:00 +02001523 LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed));
aPiecek570d7ed2021-09-10 07:15:35 +02001524
1525 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1526 break;
1527 }
1528 }
1529
1530 /* end the siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001531 LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001532
Michal Vasko9883a3e2022-03-31 12:16:00 +02001533 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +02001534}
1535
1536/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001537 * @brief Parse used YANG data models.
1538 *
1539 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001540 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001541 * @return LY_ERR value.
1542 */
1543static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001544lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001545{
1546 LY_ERR ret;
1547 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001548 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001549
1550 /* read model count */
1551 lyb_read_number(&count, sizeof count, 2, lybctx);
1552
1553 if (count) {
1554 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1555
1556 /* read modules */
1557 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001558 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001559 LY_CHECK_RET(ret);
1560 LY_ARRAY_INCREMENT(lybctx->models);
1561 }
1562 }
1563
1564 return LY_SUCCESS;
1565}
1566
1567/**
1568 * @brief Parse LYB magic number.
1569 *
1570 * @param[in] lybctx LYB context.
1571 * @return LY_ERR value.
1572 */
1573static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001574lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001575{
1576 char magic_byte = 0;
1577
1578 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1579 if (magic_byte != 'l') {
1580 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1581 return LY_EINVAL;
1582 }
1583
1584 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1585 if (magic_byte != 'y') {
1586 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1587 return LY_EINVAL;
1588 }
1589
1590 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1591 if (magic_byte != 'b') {
1592 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1593 return LY_EINVAL;
1594 }
1595
1596 return LY_SUCCESS;
1597}
1598
1599/**
1600 * @brief Parse LYB header.
1601 *
1602 * @param[in] lybctx LYB context.
1603 * @return LY_ERR value.
1604 */
1605static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001606lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001607{
1608 uint8_t byte = 0;
1609
1610 /* version, future flags */
1611 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1612
1613 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1614 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001615 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001616 return LY_EINVAL;
1617 }
1618
1619 return LY_SUCCESS;
1620}
1621
Michal Vasko02ed9d82021-07-15 14:58:04 +02001622static LY_ERR
1623_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1624 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 +01001625 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001626{
Michal Vaskoe0665742021-02-11 11:08:44 +01001627 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001628 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001629
Michal Vaskoe0665742021-02-11 11:08:44 +01001630 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1631 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001632
Radek Krejci1798aae2020-07-14 13:26:06 +02001633 lybctx = calloc(1, sizeof *lybctx);
1634 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1635 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001636 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001637
Radek Krejci1798aae2020-07-14 13:26:06 +02001638 lybctx->lybctx->in = in;
1639 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001640 lybctx->parse_opts = parse_opts;
1641 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001642 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001643 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001644 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001645
1646 /* find the operation node if it exists already */
1647 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1648
Michal Vasko60ea6352020-06-29 13:39:39 +02001649 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001650 rc = lyb_parse_magic_number(lybctx->lybctx);
1651 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001652
1653 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001654 rc = lyb_parse_header(lybctx->lybctx);
1655 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001656
1657 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001658 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1659 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001660
aPiecek570d7ed2021-09-10 07:15:35 +02001661 /* read sibling(s) */
1662 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1663 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001664
1665 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1666 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1667 rc = LY_EVALID;
1668 goto cleanup;
1669 }
1670 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1671 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1672 rc = LY_EVALID;
1673 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001674 }
1675
1676 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001677 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001678
Michal Vasko60ea6352020-06-29 13:39:39 +02001679cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001680 /* there should be no unres stored if validation should be skipped */
1681 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1682 !lybctx->node_when.count));
1683
1684 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001685 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001686 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001687 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001688 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001689 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001690}
1691
Michal Vasko02ed9d82021-07-15 14:58:04 +02001692LY_ERR
1693lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1694 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 +01001695 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko02ed9d82021-07-15 14:58:04 +02001696{
1697 uint32_t int_opts;
1698
1699 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1700 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1701
Michal Vaskoddd76592022-01-17 13:34:48 +01001702 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1703
Michal Vasko02ed9d82021-07-15 14:58:04 +02001704 switch (data_type) {
1705 case LYD_TYPE_DATA_YANG:
1706 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1707 break;
1708 case LYD_TYPE_RPC_YANG:
1709 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1710 break;
1711 case LYD_TYPE_NOTIF_YANG:
1712 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1713 break;
1714 case LYD_TYPE_REPLY_YANG:
1715 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1716 break;
1717 default:
1718 LOGINT(ctx);
1719 return LY_EINT;
1720 }
1721
Michal Vaskoddd76592022-01-17 13:34:48 +01001722 if (subtree_sibling) {
1723 *subtree_sibling = 0;
1724 }
Michal Vasko02ed9d82021-07-15 14:58:04 +02001725 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1726}
1727
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001728LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001729lyd_lyb_data_length(const char *data)
1730{
1731 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001732 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001733 int count, i;
1734 size_t len;
1735 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001736 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001737
1738 if (!data) {
1739 return -1;
1740 }
1741
Radek Krejci1798aae2020-07-14 13:26:06 +02001742 lybctx = calloc(1, sizeof *lybctx);
1743 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1744 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001745 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001746
1747 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001748 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001749 LY_CHECK_GOTO(ret, cleanup);
1750
1751 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001752 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001753 LY_CHECK_GOTO(ret, cleanup);
1754
1755 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001756 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001757
1758 /* read all models */
1759 for (i = 0; i < count; ++i) {
1760 /* module name length */
1761 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001762 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001763
1764 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001765 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001766
1767 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001768 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001769 }
1770
aPiecek6828a312021-09-17 15:53:18 +02001771 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001772 /* register a new sibling */
1773 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001774 LY_CHECK_GOTO(ret, cleanup);
1775
1776 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001777 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001778
aPiecek570d7ed2021-09-10 07:15:35 +02001779 /* sibling finished */
1780 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001781 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001782 } else {
aPiecek6828a312021-09-17 15:53:18 +02001783 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001784 }
1785
1786 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001787 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001788
1789cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001790 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001791
Radek Krejci1798aae2020-07-14 13:26:06 +02001792 ly_in_free(lybctx->in, 0);
1793 lylyb_ctx_free(lybctx);
1794
Michal Vasko63f3d842020-07-08 10:10:14 +02001795 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001796}