blob: db68205dcd32865f6fdb4f1826739d0a3a24cbad [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
992 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
993 *node = NULL;
994}
995
996/**
aPiecek6fdac2f2021-09-10 11:21:10 +0200997 * @brief Parse header for non-opaq node.
998 *
999 * @param[in] lybctx LYB context.
Michal Vaskoddd76592022-01-17 13:34:48 +01001000 * @param[in] sparent Schema parent node of the metadata.
aPiecek6fdac2f2021-09-10 11:21:10 +02001001 * @param[out] flags Parsed node flags.
1002 * @param[out] meta Parsed metadata of the node.
1003 * @return LY_ERR value.
1004 */
1005static LY_ERR
Michal Vaskoddd76592022-01-17 13:34:48 +01001006lyb_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 +02001007{
1008 LY_ERR ret;
1009
1010 /* create and read metadata */
Michal Vaskoddd76592022-01-17 13:34:48 +01001011 ret = lyb_parse_metadata(lybctx, sparent, meta);
aPiecek6fdac2f2021-09-10 11:21:10 +02001012 LY_CHECK_RET(ret);
1013
1014 /* read flags */
1015 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1016
1017 return ret;
1018}
1019
1020/**
aPiecek33fc6b02021-09-09 15:45:37 +02001021 * @brief Create term node and fill it with value.
1022 *
1023 * @param[in] lybctx LYB context.
1024 * @param[in] snode Schema of the term node.
1025 * @param[out] node Created term node.
1026 * @return LY_ERR value.
1027 */
1028static LY_ERR
1029lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
1030{
1031 LY_ERR ret;
1032 ly_bool dynamic;
1033 uint8_t *term_value;
aPieceke99345d2021-09-30 12:49:59 +02001034 uint64_t term_value_len;
aPiecek33fc6b02021-09-09 15:45:37 +02001035
1036 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
1037 LY_CHECK_RET(ret);
1038
1039 dynamic = 1;
1040 /* create node */
1041 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
1042 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
1043 NULL, LYD_HINT_DATA, node);
1044 if (dynamic) {
1045 free(term_value);
1046 }
1047 if (ret) {
1048 lyd_free_tree(*node);
1049 *node = NULL;
1050 }
1051
1052 return ret;
1053}
1054
1055/**
aPiecek0e2e1052021-09-09 15:48:27 +02001056 * @brief Validate inner node, autodelete default values nad create implicit nodes.
1057 *
1058 * @param[in,out] lybctx LYB context.
1059 * @param[in] snode Schema of the inner node.
1060 * @param[in] node Parsed inner node.
1061 * @return LY_ERR value.
1062 */
1063static LY_ERR
1064lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
1065{
1066 LY_ERR ret = LY_SUCCESS;
1067 uint32_t impl_opts;
1068
1069 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
1070 /* new node validation, autodelete CANNOT occur, all nodes are new */
1071 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
1072 LY_CHECK_RET(ret);
1073
1074 /* add any missing default children */
1075 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
Michal Vaskoddd76592022-01-17 13:34:48 +01001076 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_types,
1077 impl_opts, NULL);
aPiecek0e2e1052021-09-09 15:48:27 +02001078 LY_CHECK_RET(ret);
1079 }
1080
1081 return ret;
1082}
1083
1084/**
aPiecek821cf732021-09-09 15:37:28 +02001085 * @brief Parse opaq node.
1086 *
1087 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001088 * @param[in] parent Data parent of the sibling.
aPiecek821cf732021-09-09 15:37:28 +02001089 * @param[in,out] first_p First top-level sibling.
1090 * @param[out] parsed Set of all successfully parsed nodes.
1091 * @return LY_ERR value.
1092 */
1093static LY_ERR
1094lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
1095{
1096 LY_ERR ret;
1097 struct lyd_node *node = NULL;
1098 struct lyd_attr *attr = NULL;
1099 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
1100 ly_bool dynamic = 0;
1101 LY_VALUE_FORMAT format = 0;
1102 void *val_prefix_data = NULL;
1103 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1104 uint32_t flags;
1105
aPiecek821cf732021-09-09 15:37:28 +02001106 /* parse opaq node attributes */
1107 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
1108 LY_CHECK_GOTO(ret, cleanup);
1109
1110 /* read flags */
1111 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1112
1113 /* parse prefix */
aPieceke99345d2021-09-30 12:49:59 +02001114 ret = lyb_read_string(&prefix, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001115 LY_CHECK_GOTO(ret, cleanup);
1116
1117 /* parse module key */
aPieceke99345d2021-09-30 12:49:59 +02001118 ret = lyb_read_string(&module_key, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001119 LY_CHECK_GOTO(ret, cleanup);
1120
1121 /* parse name */
aPieceke99345d2021-09-30 12:49:59 +02001122 ret = lyb_read_string(&name, sizeof(uint16_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001123 LY_CHECK_GOTO(ret, cleanup);
1124
1125 /* parse value */
aPieceke99345d2021-09-30 12:49:59 +02001126 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek821cf732021-09-09 15:37:28 +02001127 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1128 dynamic = 1;
1129
1130 /* parse format */
1131 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1132
1133 /* parse value prefixes */
1134 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1135 LY_CHECK_GOTO(ret, cleanup);
1136
aPiecek570d7ed2021-09-10 07:15:35 +02001137 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vaskoefa96e62021-11-02 12:19:14 +01001138 /* skip children */
1139 ret = lyb_read_start_siblings(lybctx->lybctx);
1140 LY_CHECK_GOTO(ret, cleanup);
1141 lyb_skip_siblings(lybctx->lybctx);
1142 ret = lyb_read_stop_siblings(lybctx->lybctx);
1143 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001144 goto cleanup;
1145 }
1146
aPiecek821cf732021-09-09 15:37:28 +02001147 /* create node */
1148 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1149 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1150 LY_CHECK_GOTO(ret, cleanup);
1151
1152 /* process children */
aPiecek570d7ed2021-09-10 07:15:35 +02001153 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1154 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001155
1156 /* register parsed opaq node */
1157 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1158 assert(!attr && !node);
1159
1160cleanup:
1161 free(prefix);
1162 free(module_key);
1163 free(name);
1164 if (dynamic) {
1165 free(value);
1166 }
1167 lyd_free_attr_siblings(ctx, attr);
1168 lyd_free_tree(node);
1169
1170 return ret;
1171}
1172
aPiecek18457d72021-09-09 15:52:20 +02001173/**
1174 * @brief Parse anydata or anyxml node.
1175 *
1176 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001177 * @param[in] parent Data parent of the sibling.
aPiecek18457d72021-09-09 15:52:20 +02001178 * @param[in] snode Schema of the node to be parsed.
1179 * @param[in,out] first_p First top-level sibling.
1180 * @param[out] parsed Set of all successfully parsed nodes.
1181 * @return LY_ERR value.
1182 */
1183static LY_ERR
1184lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1185 struct lyd_node **first_p, struct ly_set *parsed)
1186{
1187 LY_ERR ret;
1188 struct lyd_node *node = NULL, *tree;
1189 struct lyd_meta *meta = NULL;
1190 LYD_ANYDATA_VALUETYPE value_type;
1191 char *value = NULL;
aPiecek18457d72021-09-09 15:52:20 +02001192 uint32_t flags;
1193 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1194
1195 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001196 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001197 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001198
1199 /* parse value type */
1200 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1201 if (value_type == LYD_ANYDATA_DATATREE) {
1202 /* invalid situation */
1203 LOGINT(ctx);
1204 ret = LY_EINT;
1205 goto error;
1206 }
1207
1208 /* read anydata content */
aPieceke99345d2021-09-30 12:49:59 +02001209 ret = lyb_read_string(&value, sizeof(uint64_t), lybctx->lybctx);
aPiecek18457d72021-09-09 15:52:20 +02001210 LY_CHECK_GOTO(ret, error);
1211
1212 if (value_type == LYD_ANYDATA_LYB) {
1213 /* try to parse LYB into a data tree */
1214 if (!lyb_parse_any_content(ctx, value, &tree)) {
1215 /* successfully parsed */
1216 free(value);
1217 value = (char *)tree;
1218 value_type = LYD_ANYDATA_DATATREE;
1219 }
1220 }
1221
1222 /* create the node */
1223 switch (value_type) {
1224 case LYD_ANYDATA_LYB:
1225 case LYD_ANYDATA_DATATREE:
aPiecek18457d72021-09-09 15:52:20 +02001226 case LYD_ANYDATA_STRING:
1227 case LYD_ANYDATA_XML:
1228 case LYD_ANYDATA_JSON:
Michal Vasko742a5b12022-02-24 16:07:27 +01001229 /* use the value directly */
1230 ret = lyd_create_any(snode, value, value_type, 1, &node);
aPiecek18457d72021-09-09 15:52:20 +02001231 LY_CHECK_GOTO(ret, error);
aPiecek18457d72021-09-09 15:52:20 +02001232 break;
1233 default:
1234 LOGINT(ctx);
1235 ret = LY_EINT;
1236 goto error;
1237 }
1238
1239 /* register parsed anydata node */
1240 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1241
1242 return LY_SUCCESS;
1243
1244error:
1245 free(value);
1246 lyd_free_meta_siblings(meta);
1247 lyd_free_tree(node);
1248 return ret;
1249}
aPiecek821cf732021-09-09 15:37:28 +02001250
1251/**
aPiecek5777f122021-09-10 10:26:23 +02001252 * @brief Parse inner node.
aPiecek37c493b2021-09-09 12:52:30 +02001253 *
1254 * @param[in] lybctx LYB context.
aPiecek570d7ed2021-09-10 07:15:35 +02001255 * @param[in] parent Data parent of the sibling, must be set if @p first is not.
1256 * @param[in] snode Schema of the node to be parsed.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001257 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1258 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001259 * @return LY_ERR value.
1260 */
1261static LY_ERR
aPiecek5777f122021-09-10 10:26:23 +02001262lyb_parse_node_inner(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
aPiecek570d7ed2021-09-10 07:15:35 +02001263 struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +02001264{
1265 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001266 struct lyd_node *node = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001267 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001268 uint32_t flags;
Michal Vasko60ea6352020-06-29 13:39:39 +02001269
aPiecek307f0772021-09-10 09:09:47 +02001270 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001271 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek307f0772021-09-10 09:09:47 +02001272 LY_CHECK_GOTO(ret, error);
aPiecek37c493b2021-09-09 12:52:30 +02001273
aPiecek5777f122021-09-10 10:26:23 +02001274 /* create node */
1275 ret = lyd_create_inner(snode, &node);
1276 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001277
aPiecek5777f122021-09-10 10:26:23 +02001278 /* process children */
1279 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1280 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001281
aPiecek5777f122021-09-10 10:26:23 +02001282 /* additional procedure for inner node */
1283 ret = lyb_validate_node_inner(lybctx, snode, node);
1284 LY_CHECK_GOTO(ret, error);
Michal Vasko60ea6352020-06-29 13:39:39 +02001285
aPiecek5777f122021-09-10 10:26:23 +02001286 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1287 /* rememeber the RPC/action/notification */
1288 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001289 }
1290
aPiecek307f0772021-09-10 09:09:47 +02001291 /* register parsed node */
1292 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1293
1294 return LY_SUCCESS;
1295
1296error:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001297 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001298 lyd_free_tree(node);
1299 return ret;
1300}
1301
1302/**
aPiecek8a555d72021-09-10 10:15:26 +02001303 * @brief Parse leaf node.
1304 *
1305 * @param[in] lybctx LYB context.
1306 * @param[in] parent Data parent of the sibling.
1307 * @param[in] snode Schema of the node to be parsed.
1308 * @param[in,out] first_p First top-level sibling.
1309 * @param[out] parsed Set of all successfully parsed nodes.
1310 * @return LY_ERR value.
1311 */
1312static LY_ERR
1313lyb_parse_node_leaf(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1314 struct lyd_node **first_p, struct ly_set *parsed)
1315{
1316 LY_ERR ret;
1317 struct lyd_node *node = NULL;
1318 struct lyd_meta *meta = NULL;
1319 uint32_t flags;
1320
1321 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001322 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
aPiecek8a555d72021-09-10 10:15:26 +02001323 LY_CHECK_GOTO(ret, error);
1324
1325 /* read value of term node and create it */
1326 ret = lyb_create_term(lybctx, snode, &node);
1327 LY_CHECK_GOTO(ret, error);
1328
1329 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1330
1331 return LY_SUCCESS;
1332
1333error:
1334 lyd_free_meta_siblings(meta);
1335 lyd_free_tree(node);
1336 return ret;
1337}
1338
1339/**
aPiecek5777f122021-09-10 10:26:23 +02001340 * @brief Parse all leaflist nodes which belong to same schema.
1341 *
1342 * @param[in] lybctx LYB context.
1343 * @param[in] parent Data parent of the sibling.
1344 * @param[in] snode Schema of the nodes to be parsed.
1345 * @param[in,out] first_p First top-level sibling.
1346 * @param[out] parsed Set of all successfully parsed nodes.
1347 * @return LY_ERR value.
1348 */
1349static LY_ERR
1350lyb_parse_node_leaflist(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1351 struct lyd_node **first_p, struct ly_set *parsed)
1352{
1353 LY_ERR ret;
1354
1355 /* register a new sibling */
1356 ret = lyb_read_start_siblings(lybctx->lybctx);
1357 LY_CHECK_RET(ret);
1358
1359 /* process all siblings */
1360 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1361 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1362 LY_CHECK_RET(ret);
1363 }
1364
1365 /* end the sibling */
1366 ret = lyb_read_stop_siblings(lybctx->lybctx);
1367 LY_CHECK_RET(ret);
1368
1369 return ret;
1370}
1371
1372/**
aPiecek77d3e962021-09-10 10:33:59 +02001373 * @brief Parse all list nodes which belong to same schema.
1374 *
1375 * @param[in] lybctx LYB context.
1376 * @param[in] parent Data parent of the sibling.
1377 * @param[in] snode Schema of the nodes to be parsed.
1378 * @param[in,out] first_p First top-level sibling.
1379 * @param[out] parsed Set of all successfully parsed nodes.
1380 * @return LY_ERR value.
1381 */
1382static LY_ERR
1383lyb_parse_node_list(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1384 struct lyd_node **first_p, struct ly_set *parsed)
1385{
1386 LY_ERR ret;
1387 struct lyd_node *node = NULL;
1388 struct lyd_meta *meta = NULL;
1389 uint32_t flags;
1390
1391 /* register a new sibling */
1392 ret = lyb_read_start_siblings(lybctx->lybctx);
1393 LY_CHECK_RET(ret);
1394
1395 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
1396 /* read necessary basic data */
Michal Vaskoddd76592022-01-17 13:34:48 +01001397 ret = lyb_parse_node_header(lybctx, snode, &flags, &meta);
Michal Vaskoedd4f252021-09-23 08:37:40 +02001398 LY_CHECK_GOTO(ret, error);
aPiecek77d3e962021-09-10 10:33:59 +02001399
1400 /* create list node */
1401 ret = lyd_create_inner(snode, &node);
1402 LY_CHECK_GOTO(ret, error);
1403
1404 /* process children */
1405 ret = lyb_parse_siblings(lybctx, node, NULL, NULL);
1406 LY_CHECK_GOTO(ret, error);
1407
1408 /* additional procedure for inner node */
1409 ret = lyb_validate_node_inner(lybctx, snode, node);
1410 LY_CHECK_GOTO(ret, error);
1411
1412 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
1413 /* rememeber the RPC/action/notification */
1414 lybctx->op_node = node;
1415 }
1416
1417 /* register parsed list node */
1418 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1419 }
1420
1421 /* end the sibling */
1422 ret = lyb_read_stop_siblings(lybctx->lybctx);
1423 LY_CHECK_RET(ret);
1424
1425 return LY_SUCCESS;
1426
1427error:
1428 lyd_free_meta_siblings(meta);
1429 lyd_free_tree(node);
1430 return ret;
1431}
1432
1433/**
Michal Vasko9883a3e2022-03-31 12:16:00 +02001434 * @brief Parse a node.
aPiecek17737b52021-09-21 12:29:52 +02001435 *
aPiecek17737b52021-09-21 12:29:52 +02001436 * @param[in] lybctx LYB context.
Michal Vasko9883a3e2022-03-31 12:16:00 +02001437 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1438 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1439 * @param[in,out] parsed Set of all successfully parsed nodes to add to.
aPiecek17737b52021-09-21 12:29:52 +02001440 * @return LY_ERR value.
1441 */
1442static LY_ERR
1443lyb_parse_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1444 struct ly_set *parsed)
1445{
1446 LY_ERR ret;
1447 const struct lysc_node *snode;
1448 const struct lys_module *mod;
Michal Vasko9883a3e2022-03-31 12:16:00 +02001449 enum lylyb_node_type lyb_type;
1450 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
aPiecek17737b52021-09-21 12:29:52 +02001451
Michal Vasko9883a3e2022-03-31 12:16:00 +02001452 /* read node type */
1453 lyb_read_number(&lyb_type, sizeof lyb_type, 1, lybctx->lybctx);
1454
1455 switch (lyb_type) {
1456 case LYB_NODE_TOP:
1457 /* top-level, read module name */
1458 LY_CHECK_GOTO(ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod), cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001459
1460 /* read hash, find the schema node starting from mod */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001461 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode), cleanup);
1462 break;
1463 case LYB_NODE_CHILD:
1464 case LYB_NODE_OPAQ:
1465 /* read hash, find the schema node starting from parent schema, if any */
1466 LY_CHECK_GOTO(ret = lyb_parse_schema_hash(lybctx, parent ? parent->schema : NULL, NULL, &snode), cleanup);
1467 break;
1468 case LYB_NODE_EXT:
1469 /* ext, read module name */
1470 LY_CHECK_GOTO(ret = lyb_read_model(lybctx->lybctx, &mod_name, mod_rev), cleanup);
1471
1472 /* read schema node name, find the nexted ext schema node */
1473 LY_CHECK_GOTO(ret = lyb_parse_schema_nested_ext(lybctx, parent, mod_name, &snode), cleanup);
1474 break;
aPiecek17737b52021-09-21 12:29:52 +02001475 }
aPiecek17737b52021-09-21 12:29:52 +02001476
1477 if (!snode) {
1478 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
1479 } else if (snode->nodetype & LYS_LEAFLIST) {
1480 ret = lyb_parse_node_leaflist(lybctx, parent, snode, first_p, parsed);
1481 } else if (snode->nodetype == LYS_LIST) {
1482 ret = lyb_parse_node_list(lybctx, parent, snode, first_p, parsed);
1483 } else if (snode->nodetype & LYD_NODE_ANY) {
1484 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
1485 } else if (snode->nodetype & LYD_NODE_INNER) {
1486 ret = lyb_parse_node_inner(lybctx, parent, snode, first_p, parsed);
1487 } else {
1488 ret = lyb_parse_node_leaf(lybctx, parent, snode, first_p, parsed);
1489 }
Michal Vasko9883a3e2022-03-31 12:16:00 +02001490 LY_CHECK_GOTO(ret, cleanup);
aPiecek17737b52021-09-21 12:29:52 +02001491
Michal Vasko9883a3e2022-03-31 12:16:00 +02001492cleanup:
1493 free(mod_name);
aPiecek17737b52021-09-21 12:29:52 +02001494 return ret;
1495}
1496
1497/**
aPiecek570d7ed2021-09-10 07:15:35 +02001498 * @brief Parse siblings (@ref lyb_print_siblings()).
1499 *
1500 * @param[in] lybctx LYB context.
1501 * @param[in] parent Data parent of the sibling, must be set if @p first_p is not.
1502 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1503 * @param[out] parsed Set of all successfully parsed nodes.
1504 * @return LY_ERR value.
1505 */
1506static LY_ERR
1507lyb_parse_siblings(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p,
1508 struct ly_set *parsed)
1509{
aPiecek570d7ed2021-09-10 07:15:35 +02001510 ly_bool top_level;
aPiecek570d7ed2021-09-10 07:15:35 +02001511
1512 top_level = !LY_ARRAY_COUNT(lybctx->lybctx->siblings);
1513
1514 /* register a new siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001515 LY_CHECK_RET(lyb_read_start_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001516
1517 while (LYB_LAST_SIBLING(lybctx->lybctx).written) {
Michal Vasko9883a3e2022-03-31 12:16:00 +02001518 LY_CHECK_RET(lyb_parse_node(lybctx, parent, first_p, parsed));
aPiecek570d7ed2021-09-10 07:15:35 +02001519
1520 if (top_level && !(lybctx->int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1521 break;
1522 }
1523 }
1524
1525 /* end the siblings */
Michal Vasko9883a3e2022-03-31 12:16:00 +02001526 LY_CHECK_RET(lyb_read_stop_siblings(lybctx->lybctx));
aPiecek570d7ed2021-09-10 07:15:35 +02001527
Michal Vasko9883a3e2022-03-31 12:16:00 +02001528 return LY_SUCCESS;
aPiecek570d7ed2021-09-10 07:15:35 +02001529}
1530
1531/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001532 * @brief Parse used YANG data models.
1533 *
1534 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001535 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001536 * @return LY_ERR value.
1537 */
1538static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001539lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001540{
1541 LY_ERR ret;
1542 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001543 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001544
1545 /* read model count */
1546 lyb_read_number(&count, sizeof count, 2, lybctx);
1547
1548 if (count) {
1549 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1550
1551 /* read modules */
1552 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001553 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001554 LY_CHECK_RET(ret);
1555 LY_ARRAY_INCREMENT(lybctx->models);
1556 }
1557 }
1558
1559 return LY_SUCCESS;
1560}
1561
1562/**
1563 * @brief Parse LYB magic number.
1564 *
1565 * @param[in] lybctx LYB context.
1566 * @return LY_ERR value.
1567 */
1568static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001569lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001570{
1571 char magic_byte = 0;
1572
1573 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1574 if (magic_byte != 'l') {
1575 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1576 return LY_EINVAL;
1577 }
1578
1579 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1580 if (magic_byte != 'y') {
1581 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1582 return LY_EINVAL;
1583 }
1584
1585 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1586 if (magic_byte != 'b') {
1587 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1588 return LY_EINVAL;
1589 }
1590
1591 return LY_SUCCESS;
1592}
1593
1594/**
1595 * @brief Parse LYB header.
1596 *
1597 * @param[in] lybctx LYB context.
1598 * @return LY_ERR value.
1599 */
1600static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001601lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001602{
1603 uint8_t byte = 0;
1604
1605 /* version, future flags */
1606 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1607
1608 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1609 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001610 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001611 return LY_EINVAL;
1612 }
1613
1614 return LY_SUCCESS;
1615}
1616
Michal Vasko02ed9d82021-07-15 14:58:04 +02001617static LY_ERR
1618_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1619 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 +01001620 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001621{
Michal Vaskoe0665742021-02-11 11:08:44 +01001622 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001623 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001624
Michal Vaskoe0665742021-02-11 11:08:44 +01001625 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1626 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001627
Radek Krejci1798aae2020-07-14 13:26:06 +02001628 lybctx = calloc(1, sizeof *lybctx);
1629 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1630 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001631 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001632
Radek Krejci1798aae2020-07-14 13:26:06 +02001633 lybctx->lybctx->in = in;
1634 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001635 lybctx->parse_opts = parse_opts;
1636 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001637 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001638 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001639 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001640
1641 /* find the operation node if it exists already */
1642 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1643
Michal Vasko60ea6352020-06-29 13:39:39 +02001644 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001645 rc = lyb_parse_magic_number(lybctx->lybctx);
1646 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001647
1648 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001649 rc = lyb_parse_header(lybctx->lybctx);
1650 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001651
1652 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001653 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1654 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001655
aPiecek570d7ed2021-09-10 07:15:35 +02001656 /* read sibling(s) */
1657 rc = lyb_parse_siblings(lybctx, parent, first_p, parsed);
1658 LY_CHECK_GOTO(rc, cleanup);
Michal Vaskoe0665742021-02-11 11:08:44 +01001659
1660 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1661 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1662 rc = LY_EVALID;
1663 goto cleanup;
1664 }
1665 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1666 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1667 rc = LY_EVALID;
1668 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001669 }
1670
1671 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001672 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001673
Michal Vasko60ea6352020-06-29 13:39:39 +02001674cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001675 /* there should be no unres stored if validation should be skipped */
1676 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1677 !lybctx->node_when.count));
1678
1679 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001680 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001681 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001682 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001683 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001684 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001685}
1686
Michal Vasko02ed9d82021-07-15 14:58:04 +02001687LY_ERR
1688lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1689 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 +01001690 struct ly_set *parsed, ly_bool *subtree_sibling, struct lyd_ctx **lydctx_p)
Michal Vasko02ed9d82021-07-15 14:58:04 +02001691{
1692 uint32_t int_opts;
1693
1694 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1695 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1696
Michal Vaskoddd76592022-01-17 13:34:48 +01001697 LY_CHECK_ARG_RET(ctx, !(parse_opts & LYD_PARSE_SUBTREE), LY_EINVAL);
1698
Michal Vasko02ed9d82021-07-15 14:58:04 +02001699 switch (data_type) {
1700 case LYD_TYPE_DATA_YANG:
1701 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1702 break;
1703 case LYD_TYPE_RPC_YANG:
1704 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1705 break;
1706 case LYD_TYPE_NOTIF_YANG:
1707 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1708 break;
1709 case LYD_TYPE_REPLY_YANG:
1710 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1711 break;
1712 default:
1713 LOGINT(ctx);
1714 return LY_EINT;
1715 }
1716
Michal Vaskoddd76592022-01-17 13:34:48 +01001717 if (subtree_sibling) {
1718 *subtree_sibling = 0;
1719 }
Michal Vasko02ed9d82021-07-15 14:58:04 +02001720 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1721}
1722
Jan Kundrátc53a7ec2021-12-09 16:01:19 +01001723LIBYANG_API_DEF int
Michal Vasko60ea6352020-06-29 13:39:39 +02001724lyd_lyb_data_length(const char *data)
1725{
1726 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001727 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001728 int count, i;
1729 size_t len;
1730 uint8_t buf[LYB_SIZE_MAX];
aPiecek6828a312021-09-17 15:53:18 +02001731 uint8_t zero[LYB_SIZE_BYTES] = {0};
Michal Vasko60ea6352020-06-29 13:39:39 +02001732
1733 if (!data) {
1734 return -1;
1735 }
1736
Radek Krejci1798aae2020-07-14 13:26:06 +02001737 lybctx = calloc(1, sizeof *lybctx);
1738 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1739 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001740 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001741
1742 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001743 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001744 LY_CHECK_GOTO(ret, cleanup);
1745
1746 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001747 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001748 LY_CHECK_GOTO(ret, cleanup);
1749
1750 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001751 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001752
1753 /* read all models */
1754 for (i = 0; i < count; ++i) {
1755 /* module name length */
1756 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001757 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001758
1759 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001760 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001761
1762 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001763 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001764 }
1765
aPiecek6828a312021-09-17 15:53:18 +02001766 if (memcmp(zero, lybctx->in->current, LYB_SIZE_BYTES)) {
aPiecek570d7ed2021-09-10 07:15:35 +02001767 /* register a new sibling */
1768 ret = lyb_read_start_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001769 LY_CHECK_GOTO(ret, cleanup);
1770
1771 /* skip it */
aPiecek570d7ed2021-09-10 07:15:35 +02001772 lyb_skip_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001773
aPiecek570d7ed2021-09-10 07:15:35 +02001774 /* sibling finished */
1775 ret = lyb_read_stop_siblings(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001776 LY_CHECK_GOTO(ret, cleanup);
aPiecek570d7ed2021-09-10 07:15:35 +02001777 } else {
aPiecek6828a312021-09-17 15:53:18 +02001778 lyb_read(NULL, LYB_SIZE_BYTES, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001779 }
1780
1781 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001782 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001783
1784cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001785 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001786
Radek Krejci1798aae2020-07-14 13:26:06 +02001787 ly_in_free(lybctx->in, 0);
1788 lylyb_ctx_free(lybctx);
1789
Michal Vasko63f3d842020-07-08 10:10:14 +02001790 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001791}