blob: d94d132356c8510b79be2f5416a3c9eb62474790 [file] [log] [blame]
Michal Vasko60ea6352020-06-29 13:39:39 +02001/**
2 * @file parser_lyb.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief LYB data parser for libyang
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "lyb.h"
16
17#include <assert.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <stdint.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "common.h"
24#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "context.h"
26#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "hash_table.h"
28#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
32#include "parser_internal.h"
Radek Krejci77114102021-03-10 15:21:57 +010033#include "set.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020034#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020036#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010037#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020038#include "tree_schema.h"
39#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010040#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020041
Michal Vasko02ed9d82021-07-15 14:58:04 +020042static LY_ERR _lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
43 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
44 struct ly_set *parsed, struct lyd_ctx **lydctx_p);
45
Radek Krejci1798aae2020-07-14 13:26:06 +020046void
47lylyb_ctx_free(struct lylyb_ctx *ctx)
48{
49 LY_ARRAY_COUNT_TYPE u;
50
51 LY_ARRAY_FREE(ctx->subtrees);
52 LY_ARRAY_FREE(ctx->models);
53
54 LY_ARRAY_FOR(ctx->sib_hts, u) {
55 lyht_free(ctx->sib_hts[u].ht);
56 }
57 LY_ARRAY_FREE(ctx->sib_hts);
58
59 free(ctx);
60}
61
62void
63lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
64{
65 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
66
67 lyd_ctx_free(lydctx);
68 lylyb_ctx_free(ctx->lybctx);
69 free(ctx);
70}
71
Michal Vasko60ea6352020-06-29 13:39:39 +020072/**
73 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
74 *
75 * @param[in] buf Destination buffer.
76 * @param[in] count Number of bytes to read.
77 * @param[in] lybctx LYB context.
78 */
79static void
Radek Krejci1798aae2020-07-14 13:26:06 +020080lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020081{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020082 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020083 struct lyd_lyb_subtree *empty;
84 size_t to_read;
85 uint8_t meta_buf[LYB_META_BYTES];
86
87 assert(lybctx);
88
89 while (1) {
90 /* check for fully-read (empty) data chunks */
91 to_read = count;
92 empty = NULL;
93 LY_ARRAY_FOR(lybctx->subtrees, u) {
94 /* we want the innermost chunks resolved first, so replace previous empty chunks,
95 * also ignore chunks that are completely finished, there is nothing for us to do */
96 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
97 /* empty chunk, do not read more */
98 to_read = lybctx->subtrees[u].written;
99 empty = &lybctx->subtrees[u];
100 }
101 }
102
103 if (!empty && !count) {
104 break;
105 }
106
107 /* we are actually reading some data, not just finishing another chunk */
108 if (to_read) {
109 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200110 ly_in_read(lybctx->in, buf, to_read);
111 } else {
112 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200113 }
114
115 LY_ARRAY_FOR(lybctx->subtrees, u) {
116 /* decrease all written counters */
117 lybctx->subtrees[u].written -= to_read;
118 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
119 }
120 /* decrease count/buf */
121 count -= to_read;
122 if (buf) {
123 buf += to_read;
124 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200125 }
126
127 if (empty) {
128 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200129 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200130 empty->written = meta_buf[0];
131 empty->inner_chunks = meta_buf[1];
132
133 /* remember whether there is a following chunk or not */
134 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200135 }
136 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200137}
138
139/**
140 * @brief Read a number.
141 *
142 * @param[in] num Destination buffer.
143 * @param[in] num_size Size of @p num.
144 * @param[in] bytes Number of bytes to read.
145 * @param[in] lybctx LYB context.
146 */
147static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200148lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200149{
150 uint64_t buf = 0;
151
152 lyb_read((uint8_t *)&buf, bytes, lybctx);
153
154 /* correct byte order */
155 buf = le64toh(buf);
156
157 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100158 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200159 *((uint8_t *)num) = buf;
160 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100161 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200162 *((uint16_t *)num) = buf;
163 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100164 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200165 *((uint32_t *)num) = buf;
166 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100167 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200168 *((uint64_t *)num) = buf;
169 break;
170 default:
171 LOGINT(lybctx->ctx);
172 }
173}
174
175/**
176 * @brief Read a string.
177 *
178 * @param[in] str Destination buffer, is allocated.
179 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
180 * @param[in] lybctx LYB context.
181 * @return LY_ERR value.
182 */
183static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200184lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200185{
Radek Krejci857189e2020-09-01 13:26:36 +0200186 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200187 size_t len = 0, cur_len;
188
189 *str = NULL;
190
191 if (with_length) {
192 lyb_read_number(&len, sizeof len, 2, lybctx);
193 } else {
194 /* read until the end of this subtree */
195 len = LYB_LAST_SUBTREE(lybctx).written;
196 if (LYB_LAST_SUBTREE(lybctx).position) {
197 next_chunk = 1;
198 }
199 }
200
201 *str = malloc((len + 1) * sizeof **str);
202 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
203
204 lyb_read((uint8_t *)*str, len, lybctx);
205
206 while (next_chunk) {
207 cur_len = LYB_LAST_SUBTREE(lybctx).written;
208 if (LYB_LAST_SUBTREE(lybctx).position) {
209 next_chunk = 1;
210 } else {
211 next_chunk = 0;
212 }
213
214 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
215 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
216
217 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
218
219 len += cur_len;
220 }
221
Michal Vaskocebbae52021-05-31 11:11:36 +0200222 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200223 return LY_SUCCESS;
224}
225
226/**
aPiecekea304e32021-08-18 09:13:47 +0200227 * @brief Read the term node.
228 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200229 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200230 * @param[out] term_value Set to term node value in dynamically
231 * allocated memory. The caller must release it.
232 * @param[out] term_value_len Value length in bytes. The zero byte is
233 * always included and is not counted.
234 * @param[in,out] lybctx LYB context.
235 * @return LY_ERR value.
236 */
237static LY_ERR
aPiecekaa5b70a2021-08-30 08:33:25 +0200238lyb_read_term(const struct lysc_node_leaf *term, uint8_t **term_value,
239 uint32_t *term_value_len, struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200240{
241 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200242 int32_t lyb_data_len;
243 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200244
aPiecekaa5b70a2021-08-30 08:33:25 +0200245 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200246
aPiecekaa5b70a2021-08-30 08:33:25 +0200247 /* Find out the size from @ref howtoDataLYB. */
248 if (term->type->basetype == LY_TYPE_LEAFREF) {
249 /* Leafref itself is ignored, the target is loaded directly. */
250 type_lf = (struct lysc_type_leafref *)term->type;
251 lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
252 } else {
253 lyb_data_len = term->type->plugin->lyb_data_len;
254 }
255
256 if (lyb_data_len < 0) {
257 /* Parse value size. */
258 lyb_read_number(term_value_len, sizeof *term_value_len,
259 sizeof *term_value_len, lybctx);
260 } else {
261 /* Data size is fixed. */
262 *term_value_len = lyb_data_len;
263 }
aPiecekea304e32021-08-18 09:13:47 +0200264
265 /* Allocate memory. */
266 allocated_size = *term_value_len + 1;
267 *term_value = malloc(allocated_size * sizeof **term_value);
268 LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM);
269
270 if (*term_value_len > 0) {
271 /* Parse value. */
272 lyb_read(*term_value, *term_value_len, lybctx);
273 }
274
275 /* Add extra zero byte regardless of whether it is string or not. */
276 (*term_value)[allocated_size - 1] = 0;
277
278 return LY_SUCCESS;
279}
280
281/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200282 * @brief Stop the current subtree - change LYB context state.
283 *
284 * @param[in] lybctx LYB context.
285 * @return LY_ERR value.
286 */
287static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200288lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200289{
290 if (LYB_LAST_SUBTREE(lybctx).written) {
291 LOGINT_RET(lybctx->ctx);
292 }
293
294 LY_ARRAY_DECREMENT(lybctx->subtrees);
295 return LY_SUCCESS;
296}
297
298/**
299 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
300 *
301 * @param[in] lybctx LYB context.
302 * @return LY_ERR value.
303 */
304static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200305lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200306{
307 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200308 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200309
Radek Krejcic7d13e32020-12-09 12:32:24 +0100310 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200311 if (u == lybctx->subtree_size) {
312 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
313 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
314 }
315
Michal Vasko63f3d842020-07-08 10:10:14 +0200316 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200317
318 LY_ARRAY_INCREMENT(lybctx->subtrees);
319 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
320 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
321 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
322
Michal Vasko60ea6352020-06-29 13:39:39 +0200323 return LY_SUCCESS;
324}
325
326/**
327 * @brief Parse YANG model info.
328 *
329 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +0200330 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +0200331 * @param[out] mod Parsed module.
332 * @return LY_ERR value.
333 */
334static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200335lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200336{
337 LY_ERR ret = LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100338 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200339 uint16_t rev;
340
341 /* model name */
342 ret = lyb_read_string(&mod_name, 1, lybctx);
343 LY_CHECK_GOTO(ret, cleanup);
344
345 /* revision */
346 lyb_read_number(&rev, sizeof rev, 2, lybctx);
347
348 if (!mod_name[0]) {
349 /* opaq node, no module */
350 *mod = NULL;
351 goto cleanup;
352 }
353
354 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100355 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
356 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko60ea6352020-06-29 13:39:39 +0200357 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200358 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200359 /* try to use an updated module */
360 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
361 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
362 /* not an implemented module in a newer revision */
363 *mod = NULL;
364 }
365 }
366 } else {
367 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
368 }
369 /* TODO data_clb supported?
370 if (lybctx->ctx->data_clb) {
371 if (!*mod) {
372 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
373 } else if (!(*mod)->implemented) {
374 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
375 }
376 }*/
377
378 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200379 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200380 if (!*mod) {
381 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200382 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200383 } else if (!(*mod)->implemented) {
384 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200385 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200386 }
387 ret = LY_EINVAL;
388 goto cleanup;
389 }
390
391 }
392
Michal Vasko85d9edc2021-04-22 09:15:05 +0200393 if (*mod) {
394 /* fill cached hashes, if not already */
395 lyb_cache_module_hash(*mod);
396 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200397
Michal Vasko60ea6352020-06-29 13:39:39 +0200398cleanup:
399 free(mod_name);
400 return ret;
401}
402
403/**
404 * @brief Parse YANG node metadata.
405 *
406 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200407 * @param[out] meta Parsed metadata.
408 * @return LY_ERR value.
409 */
410static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200411lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200412{
413 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200414 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200415 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200416 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 const struct lys_module *mod;
418
419 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200420 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200421
422 /* read attributes */
423 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200424 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200425 LY_CHECK_GOTO(ret, cleanup);
426
427 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100428 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200429 LY_CHECK_GOTO(ret, cleanup);
430
431 if (!mod) {
432 /* skip it */
433 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200434 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
435 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200436 goto stop_subtree;
437 }
438
439 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200440 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200441 LY_CHECK_GOTO(ret, cleanup);
442
443 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200444 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200445 LY_CHECK_GOTO(ret, cleanup);
446 dynamic = 1;
447
448 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200449 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200450 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200451
452 /* free strings */
453 free(meta_name);
454 meta_name = NULL;
455 if (dynamic) {
456 free(meta_value);
457 dynamic = 0;
458 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200459
Radek Krejci1798aae2020-07-14 13:26:06 +0200460 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200461
462stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200464 LY_CHECK_GOTO(ret, cleanup);
465 }
466
467cleanup:
468 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200469 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200470 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200471 *meta = NULL;
472 }
473 return ret;
474}
475
476/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100477 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200478 *
479 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100480 * @param[in] format Prefix data format.
481 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200482 * @return LY_ERR value.
483 */
484static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200485lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200486{
487 LY_ERR ret = LY_SUCCESS;
488 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100489 struct ly_set *set = NULL;
490 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200491
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100492 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200493 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100494 /* read count */
495 lyb_read(&count, 1, lybctx);
496 if (!count) {
497 return LY_SUCCESS;
498 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200499
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100500 /* read all NS elements */
501 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200502
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100503 for (i = 0; i < count; ++i) {
504 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200505
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100506 /* prefix */
507 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
508
509 /* namespace */
510 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
511
512 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
513 ns = NULL;
514 }
515
516 *prefix_data = set;
517 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200518 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200519 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100520 /* nothing stored */
521 break;
522 default:
523 LOGINT(lybctx->ctx);
524 ret = LY_EINT;
525 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200526 }
527
528cleanup:
529 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100530 ly_free_prefix_data(format, set);
531 if (ns) {
532 free(ns->prefix);
533 free(ns->uri);
534 free(ns);
535 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200536 }
537 return ret;
538}
539
540/**
541 * @brief Parse opaque attributes.
542 *
543 * @param[in] lybctx LYB context.
544 * @param[out] attr Parsed attributes.
545 * @return LY_ERR value.
546 */
547static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200548lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200549{
550 LY_ERR ret = LY_SUCCESS;
551 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200552 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200553 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200554 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200555 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100556 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200557
558 /* read count */
559 lyb_read(&count, 1, lybctx);
560
561 /* read attributes */
562 for (i = 0; i < count; ++i) {
563 ret = lyb_read_start_subtree(lybctx);
564 LY_CHECK_GOTO(ret, cleanup);
565
Michal Vasko0fdcd242020-11-11 19:12:30 +0100566 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200567 ret = lyb_read_string(&prefix, 1, lybctx);
568 LY_CHECK_GOTO(ret, cleanup);
569 if (!prefix[0]) {
570 free(prefix);
571 prefix = NULL;
572 }
573
574 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200575 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200576 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200577 if (!module_name[0]) {
578 free(module_name);
579 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200580 }
581
582 /* name */
583 ret = lyb_read_string(&name, 1, lybctx);
584 LY_CHECK_GOTO(ret, cleanup);
585
Michal Vasko60ea6352020-06-29 13:39:39 +0200586 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200587 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200588
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100589 /* value prefixes */
590 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
591 LY_CHECK_GOTO(ret, cleanup);
592
Michal Vasko60ea6352020-06-29 13:39:39 +0200593 /* value */
594 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100595 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200596 dynamic = 1;
597
598 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100599 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200600 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200601 LY_CHECK_GOTO(ret, cleanup);
602
603 free(prefix);
604 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200605 free(module_name);
606 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200607 free(name);
608 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200609 assert(!dynamic);
610 value = NULL;
611
612 if (!*attr) {
613 *attr = attr2;
614 }
615
616 ret = lyb_read_stop_subtree(lybctx);
617 LY_CHECK_GOTO(ret, cleanup);
618 }
619
620cleanup:
621 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200622 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200623 free(name);
624 if (dynamic) {
625 free(value);
626 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200627 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200628 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200629 *attr = NULL;
630 }
631 return ret;
632}
633
634/**
635 * @brief Check whether a schema node matches a hash(es).
636 *
637 * @param[in] sibling Schema node to check.
638 * @param[in] hash Hash array to check.
639 * @param[in] hash_count Number of hashes in @p hash.
640 * @return non-zero if matches,
641 * @return 0 if not.
642 */
643static int
644lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
645{
646 LYB_HASH sibling_hash;
647 uint8_t i;
648
649 /* compare all the hashes starting from collision ID 0 */
650 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200651 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200652 if (sibling_hash != hash[i]) {
653 return 0;
654 }
655 }
656
657 return 1;
658}
659
660/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200661 * @brief Parse schema node hash.
662 *
663 * @param[in] lybctx LYB context.
664 * @param[in] sparent Schema parent, must be set if @p mod is not.
665 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
666 * @param[out] snode Parsed found schema node, may be NULL if opaque.
667 * @return LY_ERR value.
668 */
669static LY_ERR
670lyb_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 +0200671 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200672{
673 LY_ERR ret;
674 uint8_t i, j;
675 const struct lysc_node *sibling;
676 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200677 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200678
679 *snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100680 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200681
682 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200683 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200684
685 if (!hash[0]) {
686 /* opaque node */
687 return LY_SUCCESS;
688 }
689
690 /* based on the first hash read all the other ones, if any */
691 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
692 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200693 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200694 }
695 }
696
697 /* move the first hash on its accurate position */
698 hash[i] = hash[0];
699
700 /* read the rest of hashes */
701 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200702 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200703
704 /* correct collision ID */
705 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
706 /* preceded with zeros */
707 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
708 }
709
710 /* find our node with matching hashes */
711 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100712 while (1) {
713 if (!sparent && lybctx->ext) {
714 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
715 } else {
716 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
717 }
718 if (!sibling) {
719 break;
720 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200721 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200722 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
723 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200724 /* match found */
725 break;
726 }
727 }
728
Michal Vaskoe0665742021-02-11 11:08:44 +0100729 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100730 if (lybctx->ext) {
731 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
732 lybctx->ext->def->name);
733 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100734 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200735 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200736 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100737 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200738 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200739 }
740 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200741 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 return ret;
743 }
744
745 *snode = sibling;
746 return LY_SUCCESS;
747}
748
749/**
750 * @brief Read until the end of the current subtree.
751 *
752 * @param[in] lybctx LYB context.
753 */
754static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200755lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200756{
Michal Vasko60ea6352020-06-29 13:39:39 +0200757 do {
758 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200759 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200760
761 /* then read data */
762 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
763 } while (LYB_LAST_SUBTREE(lybctx).written);
764}
765
766/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200767 * @brief Parse the context of anydata/anyxml node.
768 *
769 * @param[in] ctx libyang context.
770 * @param[in] data LYB data to parse.
771 * @param[out] tree Parsed tree.
772 * @return LY_ERR value.
773 */
774static LY_ERR
775lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
776{
777 LY_ERR ret;
778 uint32_t prev_lo;
779 struct ly_in *in;
780 struct lyd_ctx *lydctx = NULL;
781
782 *tree = NULL;
783
784 LY_CHECK_RET(ly_in_new_memory(data, &in));
785
786 /* turn logging off */
787 prev_lo = ly_log_options(0);
788
Michal Vasko56d88602021-07-15 16:37:59 +0200789 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 +0200790 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
791
792 /* turn logging on again */
793 ly_log_options(prev_lo);
794
795 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200796 if (lydctx) {
797 lydctx->free(lydctx);
798 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200799 if (ret) {
800 lyd_free_siblings(*tree);
801 *tree = NULL;
802 }
803 return ret;
804}
805
806/**
aPiecek37c493b2021-09-09 12:52:30 +0200807 * @brief Insert new node to @p parsed set.
808 *
809 * Also if needed, correct @p first_p.
810 *
811 * @param[in] lybctx LYB context.
812 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
813 * @param[in,out] node Parsed node to insertion.
814 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
815 * @param[out] parsed Set of all successfully parsed nodes.
816 * @return LY_ERR value.
817 */
818static void
819lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
820 struct ly_set *parsed)
821{
822 /* insert, keep first pointer correct */
823 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
824 while (!parent && (*first_p)->prev->next) {
825 *first_p = (*first_p)->prev;
826 }
827
828 /* rememeber a successfully parsed node */
829 if (parsed) {
830 ly_set_add(parsed, node, 1, NULL);
831 }
832}
833
834/**
835 * @brief Finish parsing the opaq node.
836 *
837 * @param[in] lybctx LYB context.
838 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
839 * @param[in] flags Node flags to set.
840 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
841 * @param[in,out] node Parsed opaq node to finish.
842 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
843 * @param[out] parsed Set of all successfully parsed nodes.
844 * @return LY_ERR value.
845 */
846static void
847lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
848 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
849{
850 struct lyd_attr *iter;
851
852 /* set flags */
853 (*node)->flags = flags;
854
855 /* add attributes */
856 assert(!(*node)->schema);
857 LY_LIST_FOR(*attr, iter) {
858 iter->parent = (struct lyd_node_opaq *)*node;
859 }
860 ((struct lyd_node_opaq *)*node)->attr = *attr;
861 *attr = NULL;
862
863 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
864 *node = NULL;
865}
866
867/**
868 * @brief Finish parsing the node.
869 *
870 * @param[in] lybctx LYB context.
871 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
872 * @param[in] flags Node flags to set.
873 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
874 * @param[in,out] node Parsed node to finish.
875 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
876 * @param[out] parsed Set of all successfully parsed nodes.
877 * @return LY_ERR value.
878 */
879static void
880lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
881 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
882{
883 struct lyd_meta *m;
884
885 /* set flags */
886 (*node)->flags = flags;
887
888 /* add metadata */
889 LY_LIST_FOR(*meta, m) {
890 m->parent = *node;
891 }
892 (*node)->meta = *meta;
893 *meta = NULL;
894
895 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
896 *node = NULL;
897}
898
899/**
900 * @brief Parse header for non-opaq node.
901 *
902 * @param[in] lybctx LYB context.
903 * @param[out] flags Parsed node flags.
904 * @param[out] meta Parsed metadata of the node.
905 * @return LY_ERR value.
906 */
907static LY_ERR
908lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta)
909{
910 LY_ERR ret;
911
912 /* create and read metadata */
913 ret = lyb_parse_metadata(lybctx, meta);
914 LY_CHECK_RET(ret);
915
916 /* read flags */
917 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
918
919 return ret;
920}
921
922/**
923 * @brief Parse model and hash.
924 *
925 * @param[in] lybctx LYB context.
926 * @param[in] parent Data parent of the subtree.
927 * @param[out] snode Schema of the node to be further parsed. Can be NULL for the opaq node.
928 * @return LY_ERR value.
929 */
930static LY_ERR
931lyb_print_model_and_hash(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node **snode)
932{
933 LY_ERR ret;
934 const struct lys_module *mod;
935
936 if (!parent || !parent->schema) {
937 /* top-level or opaque, read module name */
938 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
939 LY_CHECK_RET(ret);
940
941 /* read hash, find the schema node starting from mod */
942 ret = lyb_parse_schema_hash(lybctx, NULL, mod, snode);
943 LY_CHECK_RET(ret);
944 } else {
945 /* read hash, find the schema node starting from parent schema */
946 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, snode);
947 LY_CHECK_RET(ret);
948 }
949
950 return ret;
951}
952
953/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200954 * @brief Parse LYB subtree.
955 *
956 * @param[in] lybctx LYB context.
957 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
aPiecekfc7cf7e2021-09-09 11:20:27 +0200958 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
959 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +0200960 * @return LY_ERR value.
961 */
962static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100963lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
Michal Vasko60ea6352020-06-29 13:39:39 +0200964{
965 LY_ERR ret = LY_SUCCESS;
966 struct lyd_node *node = NULL, *tree;
Michal Vasko60ea6352020-06-29 13:39:39 +0200967 const struct lysc_node *snode = NULL;
aPiecek37c493b2021-09-09 12:52:30 +0200968 struct lyd_meta *meta = NULL;
969 struct lyd_attr *attr = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200970 LYD_ANYDATA_VALUETYPE value_type;
Radek Krejci1798aae2020-07-14 13:26:06 +0200971 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
aPiecekea304e32021-08-18 09:13:47 +0200972 uint8_t *term_value = NULL;
Michal Vasko9afe3df2021-02-05 16:33:50 +0100973 const char *val_dict;
Radek Krejci857189e2020-09-01 13:26:36 +0200974 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200975 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100976 void *val_prefix_data = NULL;
aPiecekea304e32021-08-18 09:13:47 +0200977 uint32_t flags, term_value_len;
Radek Krejci1798aae2020-07-14 13:26:06 +0200978 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200979
980 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200981 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200982
aPiecek37c493b2021-09-09 12:52:30 +0200983 ret = lyb_print_model_and_hash(lybctx, parent, &snode);
984 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +0200985
Michal Vaskoe0665742021-02-11 11:08:44 +0100986 if (!snode && !(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200987 /* unknown data, skip them */
Radek Krejci1798aae2020-07-14 13:26:06 +0200988 lyb_skip_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200989 goto stop_subtree;
Radek Krejci0f969882020-08-21 16:56:47 +0200990 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200991
aPiecek37c493b2021-09-09 12:52:30 +0200992 if (!snode) {
993 /* create attributes */
Radek Krejci1798aae2020-07-14 13:26:06 +0200994 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200995 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200996
aPiecek37c493b2021-09-09 12:52:30 +0200997 /* read flags */
998 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100999
Michal Vasko60ea6352020-06-29 13:39:39 +02001000 /* parse prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +02001001 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001002 LY_CHECK_GOTO(ret, cleanup);
1003
Radek Krejci1798aae2020-07-14 13:26:06 +02001004 /* parse module key */
1005 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001006 LY_CHECK_GOTO(ret, cleanup);
1007
1008 /* parse name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001009 ret = lyb_read_string(&name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001010 LY_CHECK_GOTO(ret, cleanup);
1011
Michal Vaskoffc92bf2021-06-21 08:15:14 +02001012 /* parse value */
1013 ret = lyb_read_string(&value, 1, lybctx->lybctx);
1014 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1015 dynamic = 1;
1016
Michal Vasko60ea6352020-06-29 13:39:39 +02001017 /* parse format */
Michal Vasko403beac2021-08-24 08:27:52 +02001018 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001019
Michal Vasko6b5cb2a2020-11-11 19:11:21 +01001020 /* parse value prefixes */
1021 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1022 LY_CHECK_GOTO(ret, cleanup);
1023
Michal Vasko60ea6352020-06-29 13:39:39 +02001024 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +01001025 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1026 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +02001027 LY_CHECK_GOTO(ret, cleanup);
1028
1029 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +02001030 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001031 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001032 LY_CHECK_GOTO(ret, cleanup);
1033 }
aPiecek37c493b2021-09-09 12:52:30 +02001034
1035 /* complete the node processing */
1036 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001037 } else if (snode->nodetype & LYD_NODE_TERM) {
aPiecek37c493b2021-09-09 12:52:30 +02001038 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1039 LY_CHECK_GOTO(ret, cleanup);
1040
Michal Vasko60ea6352020-06-29 13:39:39 +02001041 /* parse value */
aPiecekaa5b70a2021-08-30 08:33:25 +02001042 ret = lyb_read_term((struct lysc_node_leaf *)snode, &term_value,
1043 &term_value_len, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001044 LY_CHECK_GOTO(ret, cleanup);
1045 dynamic = 1;
1046
1047 /* create node */
aPiecekea304e32021-08-18 09:13:47 +02001048 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
1049 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
Michal Vasko69730152020-10-09 16:30:07 +02001050 NULL, LYD_HINT_DATA, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +02001051 if (dynamic) {
aPiecekea304e32021-08-18 09:13:47 +02001052 free(term_value);
Michal Vasko60ea6352020-06-29 13:39:39 +02001053 dynamic = 0;
1054 }
aPiecekea304e32021-08-18 09:13:47 +02001055 term_value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +02001056 LY_CHECK_GOTO(ret, cleanup);
aPiecek37c493b2021-09-09 12:52:30 +02001057
1058 /* complete the node processing */
1059 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001060 } else if (snode->nodetype & LYD_NODE_INNER) {
aPiecek37c493b2021-09-09 12:52:30 +02001061 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1062 LY_CHECK_GOTO(ret, cleanup);
1063
Michal Vasko60ea6352020-06-29 13:39:39 +02001064 /* create node */
1065 ret = lyd_create_inner(snode, &node);
1066 LY_CHECK_GOTO(ret, cleanup);
1067
1068 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +02001069 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001070 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001071 LY_CHECK_GOTO(ret, cleanup);
1072 }
1073
Michal Vaskoe0665742021-02-11 11:08:44 +01001074 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001075 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +01001076 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001077 LY_CHECK_GOTO(ret, cleanup);
1078
1079 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001080 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL, NULL, &lybctx->node_when, &lybctx->node_exts,
Michal Vaskoc43c8ab2021-03-05 13:32:44 +01001081 &lybctx->node_types, (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001082 LY_CHECK_GOTO(ret, cleanup);
1083 }
1084
Michal Vasko751cb4d2020-07-14 12:25:28 +02001085 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001086 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +02001087 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001088 }
aPiecek37c493b2021-09-09 12:52:30 +02001089
1090 /* complete the node processing */
1091 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001092 } else if (snode->nodetype & LYD_NODE_ANY) {
aPiecek37c493b2021-09-09 12:52:30 +02001093 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1094 LY_CHECK_GOTO(ret, cleanup);
1095
Michal Vasko60ea6352020-06-29 13:39:39 +02001096 /* parse value type */
Michal Vasko403beac2021-08-24 08:27:52 +02001097 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001098 if (value_type == LYD_ANYDATA_DATATREE) {
1099 /* invalid situation */
Radek Krejci1798aae2020-07-14 13:26:06 +02001100 LOGINT(ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001101 goto cleanup;
1102 }
1103
1104 /* read anydata content */
Radek Krejci1798aae2020-07-14 13:26:06 +02001105 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001106 LY_CHECK_GOTO(ret, cleanup);
1107 dynamic = 1;
1108
1109 if (value_type == LYD_ANYDATA_LYB) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001110 /* try to parse LYB into a data tree */
Michal Vasko02ed9d82021-07-15 14:58:04 +02001111 if (!lyb_parse_any_content(ctx, value, &tree)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001112 /* successfully parsed */
1113 free(value);
1114 value = (char *)tree;
1115 value_type = LYD_ANYDATA_DATATREE;
1116 }
1117 }
1118
Michal Vasko9afe3df2021-02-05 16:33:50 +01001119 /* create the node */
1120 switch (value_type) {
1121 case LYD_ANYDATA_LYB:
1122 case LYD_ANYDATA_DATATREE:
1123 /* use the value directly */
1124 ret = lyd_create_any(snode, value, value_type, 1, &node);
1125 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001126
Michal Vasko9afe3df2021-02-05 16:33:50 +01001127 dynamic = 0;
1128 value = NULL;
1129 break;
1130 case LYD_ANYDATA_STRING:
1131 case LYD_ANYDATA_XML:
1132 case LYD_ANYDATA_JSON:
1133 /* value is expected to be in the dictionary */
1134 ret = lydict_insert_zc(ctx, value, &val_dict);
1135 LY_CHECK_GOTO(ret, cleanup);
1136 dynamic = 0;
1137 value = NULL;
1138
1139 /* use the value in the dictionary */
1140 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
1141 if (ret) {
1142 lydict_remove(ctx, val_dict);
1143 goto cleanup;
1144 }
1145 break;
1146 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001147
aPiecek37c493b2021-09-09 12:52:30 +02001148 /* complete the node processing */
1149 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001150 } else {
aPiecek37c493b2021-09-09 12:52:30 +02001151 LOGINT(ctx);
1152 ret = LY_EINT;
1153 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001154 }
1155
Michal Vasko60ea6352020-06-29 13:39:39 +02001156stop_subtree:
1157 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001158 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001159 LY_CHECK_GOTO(ret, cleanup);
1160
1161cleanup:
1162 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +02001163 free(module_key);
Michal Vasko60ea6352020-06-29 13:39:39 +02001164 free(name);
1165 if (dynamic) {
1166 free(value);
1167 }
Michal Vasko60ea6352020-06-29 13:39:39 +02001168
Michal Vasko3a41dff2020-07-15 14:30:28 +02001169 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +02001170 lyd_free_attr_siblings(ctx, attr);
Michal Vasko60ea6352020-06-29 13:39:39 +02001171 lyd_free_tree(node);
1172 return ret;
1173}
1174
1175/**
1176 * @brief Parse used YANG data models.
1177 *
1178 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001179 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001180 * @return LY_ERR value.
1181 */
1182static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001183lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001184{
1185 LY_ERR ret;
1186 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001187 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001188
1189 /* read model count */
1190 lyb_read_number(&count, sizeof count, 2, lybctx);
1191
1192 if (count) {
1193 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1194
1195 /* read modules */
1196 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001197 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001198 LY_CHECK_RET(ret);
1199 LY_ARRAY_INCREMENT(lybctx->models);
1200 }
1201 }
1202
1203 return LY_SUCCESS;
1204}
1205
1206/**
1207 * @brief Parse LYB magic number.
1208 *
1209 * @param[in] lybctx LYB context.
1210 * @return LY_ERR value.
1211 */
1212static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001213lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001214{
1215 char magic_byte = 0;
1216
1217 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1218 if (magic_byte != 'l') {
1219 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1220 return LY_EINVAL;
1221 }
1222
1223 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1224 if (magic_byte != 'y') {
1225 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1226 return LY_EINVAL;
1227 }
1228
1229 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1230 if (magic_byte != 'b') {
1231 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1232 return LY_EINVAL;
1233 }
1234
1235 return LY_SUCCESS;
1236}
1237
1238/**
1239 * @brief Parse LYB header.
1240 *
1241 * @param[in] lybctx LYB context.
1242 * @return LY_ERR value.
1243 */
1244static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001245lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001246{
1247 uint8_t byte = 0;
1248
1249 /* version, future flags */
1250 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1251
1252 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1253 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001254 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001255 return LY_EINVAL;
1256 }
1257
1258 return LY_SUCCESS;
1259}
1260
Michal Vasko02ed9d82021-07-15 14:58:04 +02001261static LY_ERR
1262_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1263 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 +01001264 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001265{
Michal Vaskoe0665742021-02-11 11:08:44 +01001266 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001267 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001268
Michal Vaskoe0665742021-02-11 11:08:44 +01001269 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1270 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001271
Radek Krejci1798aae2020-07-14 13:26:06 +02001272 lybctx = calloc(1, sizeof *lybctx);
1273 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1274 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001275 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001276
Radek Krejci1798aae2020-07-14 13:26:06 +02001277 lybctx->lybctx->in = in;
1278 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001279 lybctx->parse_opts = parse_opts;
1280 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001281 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001282 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001283 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001284
1285 /* find the operation node if it exists already */
1286 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1287
Michal Vasko60ea6352020-06-29 13:39:39 +02001288 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001289 rc = lyb_parse_magic_number(lybctx->lybctx);
1290 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001291
1292 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001293 rc = lyb_parse_header(lybctx->lybctx);
1294 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001295
1296 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001297 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1298 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001299
1300 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001301 while (lybctx->lybctx->in->current[0]) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001302 rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed);
1303 LY_CHECK_GOTO(rc, cleanup);
1304
1305 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1306 break;
1307 }
1308 }
1309
1310 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1311 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1312 rc = LY_EVALID;
1313 goto cleanup;
1314 }
1315 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1316 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1317 rc = LY_EVALID;
1318 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001319 }
1320
1321 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001322 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001323
Michal Vasko60ea6352020-06-29 13:39:39 +02001324cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001325 /* there should be no unres stored if validation should be skipped */
1326 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1327 !lybctx->node_when.count));
1328
1329 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001330 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001331 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001332 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001333 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001334 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001335}
1336
Michal Vasko02ed9d82021-07-15 14:58:04 +02001337LY_ERR
1338lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1339 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1340 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
1341{
1342 uint32_t int_opts;
1343
1344 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1345 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1346
1347 switch (data_type) {
1348 case LYD_TYPE_DATA_YANG:
1349 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1350 break;
1351 case LYD_TYPE_RPC_YANG:
1352 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1353 break;
1354 case LYD_TYPE_NOTIF_YANG:
1355 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1356 break;
1357 case LYD_TYPE_REPLY_YANG:
1358 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1359 break;
1360 default:
1361 LOGINT(ctx);
1362 return LY_EINT;
1363 }
1364
1365 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1366}
1367
Michal Vasko60ea6352020-06-29 13:39:39 +02001368API int
1369lyd_lyb_data_length(const char *data)
1370{
1371 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001372 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001373 int count, i;
1374 size_t len;
1375 uint8_t buf[LYB_SIZE_MAX];
1376
1377 if (!data) {
1378 return -1;
1379 }
1380
Radek Krejci1798aae2020-07-14 13:26:06 +02001381 lybctx = calloc(1, sizeof *lybctx);
1382 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1383 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001384 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001385
1386 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001387 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001388 LY_CHECK_GOTO(ret, cleanup);
1389
1390 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001391 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001392 LY_CHECK_GOTO(ret, cleanup);
1393
1394 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001395 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001396
1397 /* read all models */
1398 for (i = 0; i < count; ++i) {
1399 /* module name length */
1400 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001401 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001402
1403 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001404 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001405
1406 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001407 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001408 }
1409
Radek Krejci1798aae2020-07-14 13:26:06 +02001410 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001411 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001412 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001413 LY_CHECK_GOTO(ret, cleanup);
1414
1415 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001416 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001417
1418 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001419 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001420 LY_CHECK_GOTO(ret, cleanup);
1421 }
1422
1423 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001424 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001425
1426cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001427 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001428
Radek Krejci1798aae2020-07-14 13:26:06 +02001429 ly_in_free(lybctx->in, 0);
1430 lylyb_ctx_free(lybctx);
1431
Michal Vasko63f3d842020-07-08 10:10:14 +02001432 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001433}