blob: 9ede71d288365d2c89e2dc1c0b64fb10d988a65b [file] [log] [blame]
Michal Vasko60ea6352020-06-29 13:39:39 +02001/**
2 * @file parser_lyb.c
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief LYB data parser for libyang
5 *
6 * Copyright (c) 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "lyb.h"
16
17#include <assert.h>
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <stdint.h>
Michal Vasko60ea6352020-06-29 13:39:39 +020019#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22
23#include "common.h"
24#include "compat.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020025#include "context.h"
26#include "dict.h"
Radek Krejci47fab892020-11-05 17:02:41 +010027#include "hash_table.h"
28#include "in.h"
Michal Vaskoafac7822020-10-20 14:22:26 +020029#include "in_internal.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020030#include "log.h"
Radek Krejci7931b192020-06-25 17:05:03 +020031#include "parser_data.h"
32#include "parser_internal.h"
Radek Krejci77114102021-03-10 15:21:57 +010033#include "set.h"
Radek Krejciad97c5f2020-06-30 09:19:28 +020034#include "tree.h"
Radek Krejci47fab892020-11-05 17:02:41 +010035#include "tree_data.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020036#include "tree_data_internal.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010037#include "tree_edit.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020038#include "tree_schema.h"
39#include "validation.h"
Michal Vasko6b5cb2a2020-11-11 19:11:21 +010040#include "xml.h"
Michal Vasko60ea6352020-06-29 13:39:39 +020041
Michal Vasko02ed9d82021-07-15 14:58:04 +020042static LY_ERR _lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
43 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, uint32_t int_opts,
44 struct ly_set *parsed, struct lyd_ctx **lydctx_p);
45
aPiecek821cf732021-09-09 15:37:28 +020046static LY_ERR lyb_parse_subtree_r(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed);
aPiecek18457d72021-09-09 15:52:20 +020047static LY_ERR lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta);
aPiecek821cf732021-09-09 15:37:28 +020048
Radek Krejci1798aae2020-07-14 13:26:06 +020049void
50lylyb_ctx_free(struct lylyb_ctx *ctx)
51{
52 LY_ARRAY_COUNT_TYPE u;
53
54 LY_ARRAY_FREE(ctx->subtrees);
55 LY_ARRAY_FREE(ctx->models);
56
57 LY_ARRAY_FOR(ctx->sib_hts, u) {
58 lyht_free(ctx->sib_hts[u].ht);
59 }
60 LY_ARRAY_FREE(ctx->sib_hts);
61
62 free(ctx);
63}
64
65void
66lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
67{
68 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
69
70 lyd_ctx_free(lydctx);
71 lylyb_ctx_free(ctx->lybctx);
72 free(ctx);
73}
74
Michal Vasko60ea6352020-06-29 13:39:39 +020075/**
76 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
77 *
78 * @param[in] buf Destination buffer.
79 * @param[in] count Number of bytes to read.
80 * @param[in] lybctx LYB context.
81 */
82static void
Radek Krejci1798aae2020-07-14 13:26:06 +020083lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020084{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020085 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020086 struct lyd_lyb_subtree *empty;
87 size_t to_read;
88 uint8_t meta_buf[LYB_META_BYTES];
89
90 assert(lybctx);
91
92 while (1) {
93 /* check for fully-read (empty) data chunks */
94 to_read = count;
95 empty = NULL;
96 LY_ARRAY_FOR(lybctx->subtrees, u) {
97 /* we want the innermost chunks resolved first, so replace previous empty chunks,
98 * also ignore chunks that are completely finished, there is nothing for us to do */
99 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
100 /* empty chunk, do not read more */
101 to_read = lybctx->subtrees[u].written;
102 empty = &lybctx->subtrees[u];
103 }
104 }
105
106 if (!empty && !count) {
107 break;
108 }
109
110 /* we are actually reading some data, not just finishing another chunk */
111 if (to_read) {
112 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200113 ly_in_read(lybctx->in, buf, to_read);
114 } else {
115 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200116 }
117
118 LY_ARRAY_FOR(lybctx->subtrees, u) {
119 /* decrease all written counters */
120 lybctx->subtrees[u].written -= to_read;
121 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
122 }
123 /* decrease count/buf */
124 count -= to_read;
125 if (buf) {
126 buf += to_read;
127 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200128 }
129
130 if (empty) {
131 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200132 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200133 empty->written = meta_buf[0];
134 empty->inner_chunks = meta_buf[1];
135
136 /* remember whether there is a following chunk or not */
137 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200138 }
139 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200140}
141
142/**
143 * @brief Read a number.
144 *
145 * @param[in] num Destination buffer.
146 * @param[in] num_size Size of @p num.
147 * @param[in] bytes Number of bytes to read.
148 * @param[in] lybctx LYB context.
149 */
150static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200151lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200152{
153 uint64_t buf = 0;
154
155 lyb_read((uint8_t *)&buf, bytes, lybctx);
156
157 /* correct byte order */
158 buf = le64toh(buf);
159
160 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100161 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200162 *((uint8_t *)num) = buf;
163 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100164 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200165 *((uint16_t *)num) = buf;
166 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100167 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200168 *((uint32_t *)num) = buf;
169 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100170 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200171 *((uint64_t *)num) = buf;
172 break;
173 default:
174 LOGINT(lybctx->ctx);
175 }
176}
177
178/**
179 * @brief Read a string.
180 *
181 * @param[in] str Destination buffer, is allocated.
182 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
183 * @param[in] lybctx LYB context.
184 * @return LY_ERR value.
185 */
186static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200187lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200188{
Radek Krejci857189e2020-09-01 13:26:36 +0200189 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200190 size_t len = 0, cur_len;
191
192 *str = NULL;
193
194 if (with_length) {
195 lyb_read_number(&len, sizeof len, 2, lybctx);
196 } else {
197 /* read until the end of this subtree */
198 len = LYB_LAST_SUBTREE(lybctx).written;
199 if (LYB_LAST_SUBTREE(lybctx).position) {
200 next_chunk = 1;
201 }
202 }
203
204 *str = malloc((len + 1) * sizeof **str);
205 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
206
207 lyb_read((uint8_t *)*str, len, lybctx);
208
209 while (next_chunk) {
210 cur_len = LYB_LAST_SUBTREE(lybctx).written;
211 if (LYB_LAST_SUBTREE(lybctx).position) {
212 next_chunk = 1;
213 } else {
214 next_chunk = 0;
215 }
216
217 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
218 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
219
220 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
221
222 len += cur_len;
223 }
224
Michal Vaskocebbae52021-05-31 11:11:36 +0200225 (*str)[len] = '\0';
Michal Vasko60ea6352020-06-29 13:39:39 +0200226 return LY_SUCCESS;
227}
228
229/**
aPiecek91eec232021-09-09 15:42:37 +0200230 * @brief Read value of term node.
aPiecekea304e32021-08-18 09:13:47 +0200231 *
aPiecekaa5b70a2021-08-30 08:33:25 +0200232 * @param[in] term Compiled term node.
aPiecekea304e32021-08-18 09:13:47 +0200233 * @param[out] term_value Set to term node value in dynamically
234 * allocated memory. The caller must release it.
235 * @param[out] term_value_len Value length in bytes. The zero byte is
236 * always included and is not counted.
237 * @param[in,out] lybctx LYB context.
238 * @return LY_ERR value.
239 */
240static LY_ERR
aPiecek91eec232021-09-09 15:42:37 +0200241lyb_read_term_value(const struct lysc_node_leaf *term, uint8_t **term_value, uint32_t *term_value_len,
242 struct lylyb_ctx *lybctx)
aPiecekea304e32021-08-18 09:13:47 +0200243{
244 uint32_t allocated_size;
aPiecekaa5b70a2021-08-30 08:33:25 +0200245 int32_t lyb_data_len;
246 struct lysc_type_leafref *type_lf;
aPiecekea304e32021-08-18 09:13:47 +0200247
aPiecekaa5b70a2021-08-30 08:33:25 +0200248 assert(term && term_value && term_value_len && lybctx);
aPiecekea304e32021-08-18 09:13:47 +0200249
aPiecekaa5b70a2021-08-30 08:33:25 +0200250 /* Find out the size from @ref howtoDataLYB. */
251 if (term->type->basetype == LY_TYPE_LEAFREF) {
252 /* Leafref itself is ignored, the target is loaded directly. */
253 type_lf = (struct lysc_type_leafref *)term->type;
254 lyb_data_len = type_lf->realtype->plugin->lyb_data_len;
255 } else {
256 lyb_data_len = term->type->plugin->lyb_data_len;
257 }
258
259 if (lyb_data_len < 0) {
260 /* Parse value size. */
261 lyb_read_number(term_value_len, sizeof *term_value_len,
262 sizeof *term_value_len, lybctx);
263 } else {
264 /* Data size is fixed. */
265 *term_value_len = lyb_data_len;
266 }
aPiecekea304e32021-08-18 09:13:47 +0200267
268 /* Allocate memory. */
269 allocated_size = *term_value_len + 1;
270 *term_value = malloc(allocated_size * sizeof **term_value);
271 LY_CHECK_ERR_RET(!*term_value, LOGMEM(lybctx->ctx), LY_EMEM);
272
273 if (*term_value_len > 0) {
274 /* Parse value. */
275 lyb_read(*term_value, *term_value_len, lybctx);
276 }
277
278 /* Add extra zero byte regardless of whether it is string or not. */
279 (*term_value)[allocated_size - 1] = 0;
280
281 return LY_SUCCESS;
282}
283
284/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200285 * @brief Stop the current subtree - change LYB context state.
286 *
287 * @param[in] lybctx LYB context.
288 * @return LY_ERR value.
289 */
290static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200291lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200292{
293 if (LYB_LAST_SUBTREE(lybctx).written) {
294 LOGINT_RET(lybctx->ctx);
295 }
296
297 LY_ARRAY_DECREMENT(lybctx->subtrees);
298 return LY_SUCCESS;
299}
300
301/**
302 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
303 *
304 * @param[in] lybctx LYB context.
305 * @return LY_ERR value.
306 */
307static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200308lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200309{
310 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200311 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200312
Radek Krejcic7d13e32020-12-09 12:32:24 +0100313 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200314 if (u == lybctx->subtree_size) {
315 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
316 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
317 }
318
Michal Vasko63f3d842020-07-08 10:10:14 +0200319 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200320
321 LY_ARRAY_INCREMENT(lybctx->subtrees);
322 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
323 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
324 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
325
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 return LY_SUCCESS;
327}
328
329/**
330 * @brief Parse YANG model info.
331 *
332 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +0200333 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +0200334 * @param[out] mod Parsed module.
335 * @return LY_ERR value.
336 */
337static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200338lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200339{
340 LY_ERR ret = LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100341 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200342 uint16_t rev;
343
344 /* model name */
345 ret = lyb_read_string(&mod_name, 1, lybctx);
346 LY_CHECK_GOTO(ret, cleanup);
347
348 /* revision */
349 lyb_read_number(&rev, sizeof rev, 2, lybctx);
350
351 if (!mod_name[0]) {
352 /* opaq node, no module */
353 *mod = NULL;
354 goto cleanup;
355 }
356
357 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100358 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
359 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko60ea6352020-06-29 13:39:39 +0200360 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200361 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200362 /* try to use an updated module */
363 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
364 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
365 /* not an implemented module in a newer revision */
366 *mod = NULL;
367 }
368 }
369 } else {
370 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
371 }
372 /* TODO data_clb supported?
373 if (lybctx->ctx->data_clb) {
374 if (!*mod) {
375 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
376 } else if (!(*mod)->implemented) {
377 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
378 }
379 }*/
380
381 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200382 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200383 if (!*mod) {
384 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200385 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200386 } else if (!(*mod)->implemented) {
387 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200388 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200389 }
390 ret = LY_EINVAL;
391 goto cleanup;
392 }
393
394 }
395
Michal Vasko85d9edc2021-04-22 09:15:05 +0200396 if (*mod) {
397 /* fill cached hashes, if not already */
398 lyb_cache_module_hash(*mod);
399 }
Michal Vasko11f76c82021-04-15 14:36:14 +0200400
Michal Vasko60ea6352020-06-29 13:39:39 +0200401cleanup:
402 free(mod_name);
403 return ret;
404}
405
406/**
407 * @brief Parse YANG node metadata.
408 *
409 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200410 * @param[out] meta Parsed metadata.
411 * @return LY_ERR value.
412 */
413static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200414lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200415{
416 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200417 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200418 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200419 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200420 const struct lys_module *mod;
421
422 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200423 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200424
425 /* read attributes */
426 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200427 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200428 LY_CHECK_GOTO(ret, cleanup);
429
430 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100431 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200432 LY_CHECK_GOTO(ret, cleanup);
433
434 if (!mod) {
435 /* skip it */
436 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200437 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
438 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200439 goto stop_subtree;
440 }
441
442 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200443 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200444 LY_CHECK_GOTO(ret, cleanup);
445
446 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200447 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200448 LY_CHECK_GOTO(ret, cleanup);
449 dynamic = 1;
450
451 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200452 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Radek Krejci8df109d2021-04-23 12:19:08 +0200453 ly_strlen(meta_value), &dynamic, LY_VALUE_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200454
455 /* free strings */
456 free(meta_name);
457 meta_name = NULL;
458 if (dynamic) {
459 free(meta_value);
460 dynamic = 0;
461 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200462
Radek Krejci1798aae2020-07-14 13:26:06 +0200463 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200464
465stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200466 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200467 LY_CHECK_GOTO(ret, cleanup);
468 }
469
470cleanup:
471 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200472 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200473 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200474 *meta = NULL;
475 }
476 return ret;
477}
478
479/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100480 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200481 *
482 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100483 * @param[in] format Prefix data format.
484 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200485 * @return LY_ERR value.
486 */
487static LY_ERR
Radek Krejci8df109d2021-04-23 12:19:08 +0200488lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_VALUE_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200489{
490 LY_ERR ret = LY_SUCCESS;
491 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100492 struct ly_set *set = NULL;
493 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200494
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100495 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +0200496 case LY_VALUE_XML:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100497 /* read count */
498 lyb_read(&count, 1, lybctx);
499 if (!count) {
500 return LY_SUCCESS;
501 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200502
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100503 /* read all NS elements */
504 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200505
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100506 for (i = 0; i < count; ++i) {
507 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200508
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100509 /* prefix */
510 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
511
512 /* namespace */
513 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
514
515 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
516 ns = NULL;
517 }
518
519 *prefix_data = set;
520 break;
Radek Krejci8df109d2021-04-23 12:19:08 +0200521 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +0200522 case LY_VALUE_LYB:
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100523 /* nothing stored */
524 break;
525 default:
526 LOGINT(lybctx->ctx);
527 ret = LY_EINT;
528 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200529 }
530
531cleanup:
532 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100533 ly_free_prefix_data(format, set);
534 if (ns) {
535 free(ns->prefix);
536 free(ns->uri);
537 free(ns);
538 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200539 }
540 return ret;
541}
542
543/**
544 * @brief Parse opaque attributes.
545 *
546 * @param[in] lybctx LYB context.
547 * @param[out] attr Parsed attributes.
548 * @return LY_ERR value.
549 */
550static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200551lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200552{
553 LY_ERR ret = LY_SUCCESS;
554 uint8_t count, i;
Michal Vasko486e4f92021-07-01 13:12:32 +0200555 struct lyd_attr *attr2 = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200556 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200557 ly_bool dynamic = 0;
Radek Krejci8df109d2021-04-23 12:19:08 +0200558 LY_VALUE_FORMAT format = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100559 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200560
561 /* read count */
562 lyb_read(&count, 1, lybctx);
563
564 /* read attributes */
565 for (i = 0; i < count; ++i) {
566 ret = lyb_read_start_subtree(lybctx);
567 LY_CHECK_GOTO(ret, cleanup);
568
Michal Vasko0fdcd242020-11-11 19:12:30 +0100569 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200570 ret = lyb_read_string(&prefix, 1, lybctx);
571 LY_CHECK_GOTO(ret, cleanup);
572 if (!prefix[0]) {
573 free(prefix);
574 prefix = NULL;
575 }
576
577 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200578 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200579 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200580 if (!module_name[0]) {
581 free(module_name);
582 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200583 }
584
585 /* name */
586 ret = lyb_read_string(&name, 1, lybctx);
587 LY_CHECK_GOTO(ret, cleanup);
588
Michal Vasko60ea6352020-06-29 13:39:39 +0200589 /* format */
Michal Vasko403beac2021-08-24 08:27:52 +0200590 lyb_read_number(&format, sizeof format, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200591
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100592 /* value prefixes */
593 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
594 LY_CHECK_GOTO(ret, cleanup);
595
Michal Vasko60ea6352020-06-29 13:39:39 +0200596 /* value */
597 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100598 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200599 dynamic = 1;
600
601 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100602 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
Michal Vaskobb512792021-07-01 13:12:49 +0200603 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200604 LY_CHECK_GOTO(ret, cleanup);
605
606 free(prefix);
607 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200608 free(module_name);
609 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200610 free(name);
611 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200612 assert(!dynamic);
613 value = NULL;
614
615 if (!*attr) {
616 *attr = attr2;
617 }
618
619 ret = lyb_read_stop_subtree(lybctx);
620 LY_CHECK_GOTO(ret, cleanup);
621 }
622
623cleanup:
624 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200625 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200626 free(name);
627 if (dynamic) {
628 free(value);
629 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200630 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200631 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200632 *attr = NULL;
633 }
634 return ret;
635}
636
637/**
638 * @brief Check whether a schema node matches a hash(es).
639 *
640 * @param[in] sibling Schema node to check.
641 * @param[in] hash Hash array to check.
642 * @param[in] hash_count Number of hashes in @p hash.
643 * @return non-zero if matches,
644 * @return 0 if not.
645 */
646static int
647lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
648{
649 LYB_HASH sibling_hash;
650 uint8_t i;
651
652 /* compare all the hashes starting from collision ID 0 */
653 for (i = 0; i < hash_count; ++i) {
Michal Vasko11f76c82021-04-15 14:36:14 +0200654 sibling_hash = lyb_get_hash(sibling, i);
Michal Vasko60ea6352020-06-29 13:39:39 +0200655 if (sibling_hash != hash[i]) {
656 return 0;
657 }
658 }
659
660 return 1;
661}
662
663/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200664 * @brief Parse schema node hash.
665 *
666 * @param[in] lybctx LYB context.
667 * @param[in] sparent Schema parent, must be set if @p mod is not.
668 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
669 * @param[out] snode Parsed found schema node, may be NULL if opaque.
670 * @return LY_ERR value.
671 */
672static LY_ERR
673lyb_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 +0200674 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200675{
676 LY_ERR ret;
677 uint8_t i, j;
678 const struct lysc_node *sibling;
679 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200680 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200681
682 *snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100683 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200684
685 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200686 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200687
688 if (!hash[0]) {
689 /* opaque node */
690 return LY_SUCCESS;
691 }
692
693 /* based on the first hash read all the other ones, if any */
694 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
695 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200696 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200697 }
698 }
699
700 /* move the first hash on its accurate position */
701 hash[i] = hash[0];
702
703 /* read the rest of hashes */
704 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200705 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200706
707 /* correct collision ID */
708 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
709 /* preceded with zeros */
710 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
711 }
712
713 /* find our node with matching hashes */
714 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100715 while (1) {
716 if (!sparent && lybctx->ext) {
717 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
718 } else {
719 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
720 }
721 if (!sibling) {
722 break;
723 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200724 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200725 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
726 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200727 /* match found */
728 break;
729 }
730 }
731
Michal Vaskoe0665742021-02-11 11:08:44 +0100732 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100733 if (lybctx->ext) {
734 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
735 lybctx->ext->def->name);
736 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100737 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200738 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200739 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100740 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200741 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200742 }
743 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200744 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200745 return ret;
746 }
747
748 *snode = sibling;
749 return LY_SUCCESS;
750}
751
752/**
753 * @brief Read until the end of the current subtree.
754 *
755 * @param[in] lybctx LYB context.
756 */
757static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200758lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200759{
Michal Vasko60ea6352020-06-29 13:39:39 +0200760 do {
761 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200762 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200763
764 /* then read data */
765 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
766 } while (LYB_LAST_SUBTREE(lybctx).written);
767}
768
769/**
Michal Vasko02ed9d82021-07-15 14:58:04 +0200770 * @brief Parse the context of anydata/anyxml node.
771 *
772 * @param[in] ctx libyang context.
773 * @param[in] data LYB data to parse.
774 * @param[out] tree Parsed tree.
775 * @return LY_ERR value.
776 */
777static LY_ERR
778lyb_parse_any_content(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree)
779{
780 LY_ERR ret;
781 uint32_t prev_lo;
782 struct ly_in *in;
783 struct lyd_ctx *lydctx = NULL;
784
785 *tree = NULL;
786
787 LY_CHECK_RET(ly_in_new_memory(data, &in));
788
789 /* turn logging off */
790 prev_lo = ly_log_options(0);
791
Michal Vasko56d88602021-07-15 16:37:59 +0200792 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 +0200793 LYD_INTOPT_ANY | LYD_INTOPT_WITH_SIBLINGS, NULL, &lydctx);
794
795 /* turn logging on again */
796 ly_log_options(prev_lo);
797
798 ly_in_free(in, 0);
Michal Vasko1391e792021-08-23 12:15:44 +0200799 if (lydctx) {
800 lydctx->free(lydctx);
801 }
Michal Vasko02ed9d82021-07-15 14:58:04 +0200802 if (ret) {
803 lyd_free_siblings(*tree);
804 *tree = NULL;
805 }
806 return ret;
807}
808
809/**
aPiecek37c493b2021-09-09 12:52:30 +0200810 * @brief Insert new node to @p parsed set.
811 *
812 * Also if needed, correct @p first_p.
813 *
814 * @param[in] lybctx LYB context.
815 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
816 * @param[in,out] node Parsed node to insertion.
817 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
818 * @param[out] parsed Set of all successfully parsed nodes.
819 * @return LY_ERR value.
820 */
821static void
822lyb_insert_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node *node, struct lyd_node **first_p,
823 struct ly_set *parsed)
824{
825 /* insert, keep first pointer correct */
826 lyd_insert_node(parent, first_p, node, lybctx->parse_opts & LYD_PARSE_ORDERED ? 1 : 0);
827 while (!parent && (*first_p)->prev->next) {
828 *first_p = (*first_p)->prev;
829 }
830
831 /* rememeber a successfully parsed node */
832 if (parsed) {
833 ly_set_add(parsed, node, 1, NULL);
834 }
835}
836
837/**
838 * @brief Finish parsing the opaq node.
839 *
840 * @param[in] lybctx LYB context.
841 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
842 * @param[in] flags Node flags to set.
843 * @param[in,out] attr Attributes to be attached. Finally set to NULL.
844 * @param[in,out] node Parsed opaq node to finish.
845 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
846 * @param[out] parsed Set of all successfully parsed nodes.
847 * @return LY_ERR value.
848 */
849static void
850lyb_finish_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_attr **attr,
851 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
852{
853 struct lyd_attr *iter;
854
855 /* set flags */
856 (*node)->flags = flags;
857
858 /* add attributes */
859 assert(!(*node)->schema);
860 LY_LIST_FOR(*attr, iter) {
861 iter->parent = (struct lyd_node_opaq *)*node;
862 }
863 ((struct lyd_node_opaq *)*node)->attr = *attr;
864 *attr = NULL;
865
866 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
867 *node = NULL;
868}
869
870/**
871 * @brief Finish parsing the node.
872 *
873 * @param[in] lybctx LYB context.
874 * @param[in] parent Data parent of the subtree, must be set if @p first_p is not.
875 * @param[in] flags Node flags to set.
876 * @param[in,out] meta Metadata to be attached. Finally set to NULL.
877 * @param[in,out] node Parsed node to finish.
878 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
879 * @param[out] parsed Set of all successfully parsed nodes.
880 * @return LY_ERR value.
881 */
882static void
883lyb_finish_node(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, uint32_t flags, struct lyd_meta **meta,
884 struct lyd_node **node, struct lyd_node **first_p, struct ly_set *parsed)
885{
886 struct lyd_meta *m;
887
888 /* set flags */
889 (*node)->flags = flags;
890
891 /* add metadata */
892 LY_LIST_FOR(*meta, m) {
893 m->parent = *node;
894 }
895 (*node)->meta = *meta;
896 *meta = NULL;
897
898 lyb_insert_node(lybctx, parent, *node, first_p, parsed);
899 *node = NULL;
900}
901
902/**
aPiecek33fc6b02021-09-09 15:45:37 +0200903 * @brief Create term node and fill it with value.
904 *
905 * @param[in] lybctx LYB context.
906 * @param[in] snode Schema of the term node.
907 * @param[out] node Created term node.
908 * @return LY_ERR value.
909 */
910static LY_ERR
911lyb_create_term(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node **node)
912{
913 LY_ERR ret;
914 ly_bool dynamic;
915 uint8_t *term_value;
916 uint32_t term_value_len;
917
918 ret = lyb_read_term_value((struct lysc_node_leaf *)snode, &term_value, &term_value_len, lybctx->lybctx);
919 LY_CHECK_RET(ret);
920
921 dynamic = 1;
922 /* create node */
923 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode,
924 term_value, term_value_len, &dynamic, LY_VALUE_LYB,
925 NULL, LYD_HINT_DATA, node);
926 if (dynamic) {
927 free(term_value);
928 }
929 if (ret) {
930 lyd_free_tree(*node);
931 *node = NULL;
932 }
933
934 return ret;
935}
936
937/**
aPiecek0e2e1052021-09-09 15:48:27 +0200938 * @brief Validate inner node, autodelete default values nad create implicit nodes.
939 *
940 * @param[in,out] lybctx LYB context.
941 * @param[in] snode Schema of the inner node.
942 * @param[in] node Parsed inner node.
943 * @return LY_ERR value.
944 */
945static LY_ERR
946lyb_validate_node_inner(struct lyd_lyb_ctx *lybctx, const struct lysc_node *snode, struct lyd_node *node)
947{
948 LY_ERR ret = LY_SUCCESS;
949 uint32_t impl_opts;
950
951 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
952 /* new node validation, autodelete CANNOT occur, all nodes are new */
953 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
954 LY_CHECK_RET(ret);
955
956 /* add any missing default children */
957 impl_opts = (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0;
958 ret = lyd_new_implicit_r(node, lyd_node_child_p(node), NULL,
959 NULL, &lybctx->node_when, &lybctx->node_exts,
960 &lybctx->node_types, impl_opts, NULL);
961 LY_CHECK_RET(ret);
962 }
963
964 return ret;
965}
966
967/**
aPiecek821cf732021-09-09 15:37:28 +0200968 * @brief Parse opaq node.
969 *
970 * @param[in] lybctx LYB context.
971 * @param[in] parent Data parent of the subtree.
972 * @param[in,out] first_p First top-level sibling.
973 * @param[out] parsed Set of all successfully parsed nodes.
974 * @return LY_ERR value.
975 */
976static LY_ERR
977lyb_parse_node_opaq(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, struct lyd_node **first_p, struct ly_set *parsed)
978{
979 LY_ERR ret;
980 struct lyd_node *node = NULL;
981 struct lyd_attr *attr = NULL;
982 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
983 ly_bool dynamic = 0;
984 LY_VALUE_FORMAT format = 0;
985 void *val_prefix_data = NULL;
986 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
987 uint32_t flags;
988
989 if (!(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
990 /* unknown data, skip them */
991 lyb_skip_subtree(lybctx->lybctx);
992 return LY_SUCCESS;
993 }
994
995 /* parse opaq node attributes */
996 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
997 LY_CHECK_GOTO(ret, cleanup);
998
999 /* read flags */
1000 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
1001
1002 /* parse prefix */
1003 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
1004 LY_CHECK_GOTO(ret, cleanup);
1005
1006 /* parse module key */
1007 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
1008 LY_CHECK_GOTO(ret, cleanup);
1009
1010 /* parse name */
1011 ret = lyb_read_string(&name, 1, lybctx->lybctx);
1012 LY_CHECK_GOTO(ret, cleanup);
1013
1014 /* parse value */
1015 ret = lyb_read_string(&value, 1, lybctx->lybctx);
1016 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
1017 dynamic = 1;
1018
1019 /* parse format */
1020 lyb_read_number(&format, sizeof format, 1, lybctx->lybctx);
1021
1022 /* parse value prefixes */
1023 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
1024 LY_CHECK_GOTO(ret, cleanup);
1025
1026 /* create node */
1027 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
1028 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
1029 LY_CHECK_GOTO(ret, cleanup);
1030
1031 /* process children */
1032 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
1033 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
1034 LY_CHECK_GOTO(ret, cleanup);
1035 }
1036
1037 /* register parsed opaq node */
1038 lyb_finish_opaq(lybctx, parent, flags, &attr, &node, first_p, parsed);
1039 assert(!attr && !node);
1040
1041cleanup:
1042 free(prefix);
1043 free(module_key);
1044 free(name);
1045 if (dynamic) {
1046 free(value);
1047 }
1048 lyd_free_attr_siblings(ctx, attr);
1049 lyd_free_tree(node);
1050
1051 return ret;
1052}
1053
aPiecek18457d72021-09-09 15:52:20 +02001054/**
1055 * @brief Parse anydata or anyxml node.
1056 *
1057 * @param[in] lybctx LYB context.
1058 * @param[in] parent Data parent of the subtree.
1059 * @param[in] snode Schema of the node to be parsed.
1060 * @param[in,out] first_p First top-level sibling.
1061 * @param[out] parsed Set of all successfully parsed nodes.
1062 * @return LY_ERR value.
1063 */
1064static LY_ERR
1065lyb_parse_node_any(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node *snode,
1066 struct lyd_node **first_p, struct ly_set *parsed)
1067{
1068 LY_ERR ret;
1069 struct lyd_node *node = NULL, *tree;
1070 struct lyd_meta *meta = NULL;
1071 LYD_ANYDATA_VALUETYPE value_type;
1072 char *value = NULL;
1073 const char *val_dict;
1074 uint32_t flags;
1075 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
1076
1077 /* read necessary basic data */
1078 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1079 LY_CHECK_GOTO(ret, error);
1080
1081 /* parse value type */
1082 lyb_read_number(&value_type, sizeof value_type, sizeof value_type, lybctx->lybctx);
1083 if (value_type == LYD_ANYDATA_DATATREE) {
1084 /* invalid situation */
1085 LOGINT(ctx);
1086 ret = LY_EINT;
1087 goto error;
1088 }
1089
1090 /* read anydata content */
1091 ret = lyb_read_string(&value, 0, lybctx->lybctx);
1092 LY_CHECK_GOTO(ret, error);
1093
1094 if (value_type == LYD_ANYDATA_LYB) {
1095 /* try to parse LYB into a data tree */
1096 if (!lyb_parse_any_content(ctx, value, &tree)) {
1097 /* successfully parsed */
1098 free(value);
1099 value = (char *)tree;
1100 value_type = LYD_ANYDATA_DATATREE;
1101 }
1102 }
1103
1104 /* create the node */
1105 switch (value_type) {
1106 case LYD_ANYDATA_LYB:
1107 case LYD_ANYDATA_DATATREE:
1108 /* use the value directly */
1109 ret = lyd_create_any(snode, value, value_type, 1, &node);
1110 LY_CHECK_GOTO(ret, error);
1111
1112 break;
1113 case LYD_ANYDATA_STRING:
1114 case LYD_ANYDATA_XML:
1115 case LYD_ANYDATA_JSON:
1116 /* value is expected to be in the dictionary */
1117 ret = lydict_insert_zc(ctx, value, &val_dict);
1118 LY_CHECK_GOTO(ret, error);
1119
1120 /* use the value in the dictionary */
1121 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
1122 if (ret) {
1123 lydict_remove(ctx, val_dict);
1124 goto error;
1125 }
1126 break;
1127 default:
1128 LOGINT(ctx);
1129 ret = LY_EINT;
1130 goto error;
1131 }
1132
1133 /* register parsed anydata node */
1134 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
1135
1136 return LY_SUCCESS;
1137
1138error:
1139 free(value);
1140 lyd_free_meta_siblings(meta);
1141 lyd_free_tree(node);
1142 return ret;
1143}
aPiecek821cf732021-09-09 15:37:28 +02001144
1145/**
aPiecek37c493b2021-09-09 12:52:30 +02001146 * @brief Parse header for non-opaq node.
1147 *
1148 * @param[in] lybctx LYB context.
1149 * @param[out] flags Parsed node flags.
1150 * @param[out] meta Parsed metadata of the node.
1151 * @return LY_ERR value.
1152 */
1153static LY_ERR
1154lyb_parse_node_header(struct lyd_lyb_ctx *lybctx, uint32_t *flags, struct lyd_meta **meta)
1155{
1156 LY_ERR ret;
1157
1158 /* create and read metadata */
1159 ret = lyb_parse_metadata(lybctx, meta);
1160 LY_CHECK_RET(ret);
1161
1162 /* read flags */
1163 lyb_read_number(flags, sizeof *flags, sizeof *flags, lybctx->lybctx);
1164
1165 return ret;
1166}
1167
1168/**
1169 * @brief Parse model and hash.
1170 *
1171 * @param[in] lybctx LYB context.
1172 * @param[in] parent Data parent of the subtree.
1173 * @param[out] snode Schema of the node to be further parsed. Can be NULL for the opaq node.
1174 * @return LY_ERR value.
1175 */
1176static LY_ERR
1177lyb_print_model_and_hash(struct lyd_lyb_ctx *lybctx, struct lyd_node *parent, const struct lysc_node **snode)
1178{
1179 LY_ERR ret;
1180 const struct lys_module *mod;
1181
1182 if (!parent || !parent->schema) {
1183 /* top-level or opaque, read module name */
1184 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
1185 LY_CHECK_RET(ret);
1186
1187 /* read hash, find the schema node starting from mod */
1188 ret = lyb_parse_schema_hash(lybctx, NULL, mod, snode);
1189 LY_CHECK_RET(ret);
1190 } else {
1191 /* read hash, find the schema node starting from parent schema */
1192 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, snode);
1193 LY_CHECK_RET(ret);
1194 }
1195
1196 return ret;
1197}
1198
1199/**
Michal Vasko60ea6352020-06-29 13:39:39 +02001200 * @brief Parse LYB subtree.
1201 *
1202 * @param[in] lybctx LYB context.
1203 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001204 * @param[in,out] first_p First top-level sibling, must be set if @p parent is not.
1205 * @param[out] parsed Set of all successfully parsed nodes.
Michal Vasko60ea6352020-06-29 13:39:39 +02001206 * @return LY_ERR value.
1207 */
1208static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +01001209lyb_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 +02001210{
1211 LY_ERR ret = LY_SUCCESS;
aPiecek18457d72021-09-09 15:52:20 +02001212 struct lyd_node *node = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +02001213 const struct lysc_node *snode = NULL;
aPiecek37c493b2021-09-09 12:52:30 +02001214 struct lyd_meta *meta = NULL;
aPiecek33fc6b02021-09-09 15:45:37 +02001215 uint32_t flags;
Radek Krejci1798aae2020-07-14 13:26:06 +02001216 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001217
1218 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001219 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001220
aPiecek37c493b2021-09-09 12:52:30 +02001221 ret = lyb_print_model_and_hash(lybctx, parent, &snode);
1222 LY_CHECK_RET(ret);
Michal Vasko60ea6352020-06-29 13:39:39 +02001223
aPiecek37c493b2021-09-09 12:52:30 +02001224 if (!snode) {
aPiecek821cf732021-09-09 15:37:28 +02001225 ret = lyb_parse_node_opaq(lybctx, parent, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001226 LY_CHECK_GOTO(ret, cleanup);
aPiecek821cf732021-09-09 15:37:28 +02001227 goto stop_subtree;
Michal Vasko60ea6352020-06-29 13:39:39 +02001228 } else if (snode->nodetype & LYD_NODE_TERM) {
aPiecek37c493b2021-09-09 12:52:30 +02001229 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1230 LY_CHECK_GOTO(ret, cleanup);
1231
aPiecek33fc6b02021-09-09 15:45:37 +02001232 /* read value of term node and create it */
1233 ret = lyb_create_term(lybctx, snode, &node);
Radek Krejci1798aae2020-07-14 13:26:06 +02001234 LY_CHECK_GOTO(ret, cleanup);
aPiecek37c493b2021-09-09 12:52:30 +02001235
1236 /* complete the node processing */
1237 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001238 } else if (snode->nodetype & LYD_NODE_INNER) {
aPiecek37c493b2021-09-09 12:52:30 +02001239 ret = lyb_parse_node_header(lybctx, &flags, &meta);
1240 LY_CHECK_GOTO(ret, cleanup);
1241
Michal Vasko60ea6352020-06-29 13:39:39 +02001242 /* create node */
1243 ret = lyd_create_inner(snode, &node);
1244 LY_CHECK_GOTO(ret, cleanup);
1245
1246 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +02001247 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001248 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +02001249 LY_CHECK_GOTO(ret, cleanup);
1250 }
1251
aPiecek0e2e1052021-09-09 15:48:27 +02001252 /* additional procedure for inner node */
1253 ret = lyb_validate_node_inner(lybctx, snode, node);
1254 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001255
Michal Vasko751cb4d2020-07-14 12:25:28 +02001256 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001257 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +02001258 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +02001259 }
aPiecek37c493b2021-09-09 12:52:30 +02001260
1261 /* complete the node processing */
1262 lyb_finish_node(lybctx, parent, flags, &meta, &node, first_p, parsed);
Michal Vasko60ea6352020-06-29 13:39:39 +02001263 } else if (snode->nodetype & LYD_NODE_ANY) {
aPiecek18457d72021-09-09 15:52:20 +02001264 ret = lyb_parse_node_any(lybctx, parent, snode, first_p, parsed);
aPiecek37c493b2021-09-09 12:52:30 +02001265 LY_CHECK_GOTO(ret, cleanup);
aPiecek18457d72021-09-09 15:52:20 +02001266 goto stop_subtree;
Michal Vasko60ea6352020-06-29 13:39:39 +02001267 } else {
aPiecek37c493b2021-09-09 12:52:30 +02001268 LOGINT(ctx);
1269 ret = LY_EINT;
1270 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001271 }
1272
Michal Vasko60ea6352020-06-29 13:39:39 +02001273stop_subtree:
1274 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001275 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001276 LY_CHECK_GOTO(ret, cleanup);
1277
1278cleanup:
Michal Vasko3a41dff2020-07-15 14:30:28 +02001279 lyd_free_meta_siblings(meta);
Michal Vasko60ea6352020-06-29 13:39:39 +02001280 lyd_free_tree(node);
1281 return ret;
1282}
1283
1284/**
1285 * @brief Parse used YANG data models.
1286 *
1287 * @param[in] lybctx LYB context.
aPiecekfc7cf7e2021-09-09 11:20:27 +02001288 * @param[in] parse_options Flag with options for parsing.
Michal Vasko60ea6352020-06-29 13:39:39 +02001289 * @return LY_ERR value.
1290 */
1291static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001292lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +02001293{
1294 LY_ERR ret;
1295 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +02001296 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +02001297
1298 /* read model count */
1299 lyb_read_number(&count, sizeof count, 2, lybctx);
1300
1301 if (count) {
1302 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
1303
1304 /* read modules */
1305 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001306 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +02001307 LY_CHECK_RET(ret);
1308 LY_ARRAY_INCREMENT(lybctx->models);
1309 }
1310 }
1311
1312 return LY_SUCCESS;
1313}
1314
1315/**
1316 * @brief Parse LYB magic number.
1317 *
1318 * @param[in] lybctx LYB context.
1319 * @return LY_ERR value.
1320 */
1321static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001322lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001323{
1324 char magic_byte = 0;
1325
1326 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1327 if (magic_byte != 'l') {
1328 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
1329 return LY_EINVAL;
1330 }
1331
1332 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1333 if (magic_byte != 'y') {
1334 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
1335 return LY_EINVAL;
1336 }
1337
1338 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1339 if (magic_byte != 'b') {
1340 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1341 return LY_EINVAL;
1342 }
1343
1344 return LY_SUCCESS;
1345}
1346
1347/**
1348 * @brief Parse LYB header.
1349 *
1350 * @param[in] lybctx LYB context.
1351 * @return LY_ERR value.
1352 */
1353static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001354lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001355{
1356 uint8_t byte = 0;
1357
1358 /* version, future flags */
1359 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1360
1361 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1362 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001363 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001364 return LY_EINVAL;
1365 }
1366
1367 return LY_SUCCESS;
1368}
1369
Michal Vasko02ed9d82021-07-15 14:58:04 +02001370static LY_ERR
1371_lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1372 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 +01001373 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001374{
Michal Vaskoe0665742021-02-11 11:08:44 +01001375 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001376 struct lyd_lyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001377
Michal Vaskoe0665742021-02-11 11:08:44 +01001378 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1379 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001380
Radek Krejci1798aae2020-07-14 13:26:06 +02001381 lybctx = calloc(1, sizeof *lybctx);
1382 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1383 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001384 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001385
Radek Krejci1798aae2020-07-14 13:26:06 +02001386 lybctx->lybctx->in = in;
1387 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001388 lybctx->parse_opts = parse_opts;
1389 lybctx->val_opts = val_opts;
Michal Vaskoe0665742021-02-11 11:08:44 +01001390 lybctx->int_opts = int_opts;
Michal Vasko02ed9d82021-07-15 14:58:04 +02001391 lybctx->free = lyd_lyb_ctx_free;
Radek Krejcif16e2542021-02-17 15:39:23 +01001392 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001393
1394 /* find the operation node if it exists already */
1395 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1396
Michal Vasko60ea6352020-06-29 13:39:39 +02001397 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001398 rc = lyb_parse_magic_number(lybctx->lybctx);
1399 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001400
1401 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001402 rc = lyb_parse_header(lybctx->lybctx);
1403 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001404
1405 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001406 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1407 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001408
1409 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001410 while (lybctx->lybctx->in->current[0]) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001411 rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed);
1412 LY_CHECK_GOTO(rc, cleanup);
1413
1414 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1415 break;
1416 }
1417 }
1418
1419 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1420 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1421 rc = LY_EVALID;
1422 goto cleanup;
1423 }
1424 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1425 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1426 rc = LY_EVALID;
1427 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001428 }
1429
1430 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001431 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001432
Michal Vasko60ea6352020-06-29 13:39:39 +02001433cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001434 /* there should be no unres stored if validation should be skipped */
1435 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1436 !lybctx->node_when.count));
1437
1438 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001439 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001440 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001441 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001442 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001443 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001444}
1445
Michal Vasko02ed9d82021-07-15 14:58:04 +02001446LY_ERR
1447lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1448 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1449 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
1450{
1451 uint32_t int_opts;
1452
1453 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1454 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
1455
1456 switch (data_type) {
1457 case LYD_TYPE_DATA_YANG:
1458 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1459 break;
1460 case LYD_TYPE_RPC_YANG:
1461 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1462 break;
1463 case LYD_TYPE_NOTIF_YANG:
1464 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1465 break;
1466 case LYD_TYPE_REPLY_YANG:
1467 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1468 break;
1469 default:
1470 LOGINT(ctx);
1471 return LY_EINT;
1472 }
1473
1474 return _lyd_parse_lyb(ctx, ext, parent, first_p, in, parse_opts, val_opts, int_opts, parsed, lydctx_p);
1475}
1476
Michal Vasko60ea6352020-06-29 13:39:39 +02001477API int
1478lyd_lyb_data_length(const char *data)
1479{
1480 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001481 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001482 int count, i;
1483 size_t len;
1484 uint8_t buf[LYB_SIZE_MAX];
1485
1486 if (!data) {
1487 return -1;
1488 }
1489
Radek Krejci1798aae2020-07-14 13:26:06 +02001490 lybctx = calloc(1, sizeof *lybctx);
1491 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1492 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001493 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001494
1495 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001496 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001497 LY_CHECK_GOTO(ret, cleanup);
1498
1499 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001500 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001501 LY_CHECK_GOTO(ret, cleanup);
1502
1503 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001504 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001505
1506 /* read all models */
1507 for (i = 0; i < count; ++i) {
1508 /* module name length */
1509 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001510 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001511
1512 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001513 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001514
1515 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001516 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001517 }
1518
Radek Krejci1798aae2020-07-14 13:26:06 +02001519 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001520 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001521 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001522 LY_CHECK_GOTO(ret, cleanup);
1523
1524 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001525 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001526
1527 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001528 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001529 LY_CHECK_GOTO(ret, cleanup);
1530 }
1531
1532 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001533 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001534
1535cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001536 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001537
Radek Krejci1798aae2020-07-14 13:26:06 +02001538 ly_in_free(lybctx->in, 0);
1539 lylyb_ctx_free(lybctx);
1540
Michal Vasko63f3d842020-07-08 10:10:14 +02001541 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001542}