blob: 9cebd2df7c31aebad3055ade0c42cc3a88ee9884 [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
Radek Krejci1798aae2020-07-14 13:26:06 +020042void
43lylyb_ctx_free(struct lylyb_ctx *ctx)
44{
45 LY_ARRAY_COUNT_TYPE u;
46
47 LY_ARRAY_FREE(ctx->subtrees);
48 LY_ARRAY_FREE(ctx->models);
49
50 LY_ARRAY_FOR(ctx->sib_hts, u) {
51 lyht_free(ctx->sib_hts[u].ht);
52 }
53 LY_ARRAY_FREE(ctx->sib_hts);
54
55 free(ctx);
56}
57
58void
59lyd_lyb_ctx_free(struct lyd_ctx *lydctx)
60{
61 struct lyd_lyb_ctx *ctx = (struct lyd_lyb_ctx *)lydctx;
62
63 lyd_ctx_free(lydctx);
64 lylyb_ctx_free(ctx->lybctx);
65 free(ctx);
66}
67
Michal Vasko60ea6352020-06-29 13:39:39 +020068/**
69 * @brief Read YANG data from LYB input. Metadata are handled transparently and not returned.
70 *
71 * @param[in] buf Destination buffer.
72 * @param[in] count Number of bytes to read.
73 * @param[in] lybctx LYB context.
74 */
75static void
Radek Krejci1798aae2020-07-14 13:26:06 +020076lyb_read(uint8_t *buf, size_t count, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +020077{
Michal Vaskofd69e1d2020-07-03 11:57:17 +020078 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +020079 struct lyd_lyb_subtree *empty;
80 size_t to_read;
81 uint8_t meta_buf[LYB_META_BYTES];
82
83 assert(lybctx);
84
85 while (1) {
86 /* check for fully-read (empty) data chunks */
87 to_read = count;
88 empty = NULL;
89 LY_ARRAY_FOR(lybctx->subtrees, u) {
90 /* we want the innermost chunks resolved first, so replace previous empty chunks,
91 * also ignore chunks that are completely finished, there is nothing for us to do */
92 if ((lybctx->subtrees[u].written <= to_read) && lybctx->subtrees[u].position) {
93 /* empty chunk, do not read more */
94 to_read = lybctx->subtrees[u].written;
95 empty = &lybctx->subtrees[u];
96 }
97 }
98
99 if (!empty && !count) {
100 break;
101 }
102
103 /* we are actually reading some data, not just finishing another chunk */
104 if (to_read) {
105 if (buf) {
Michal Vasko63f3d842020-07-08 10:10:14 +0200106 ly_in_read(lybctx->in, buf, to_read);
107 } else {
108 ly_in_skip(lybctx->in, to_read);
Michal Vasko60ea6352020-06-29 13:39:39 +0200109 }
110
111 LY_ARRAY_FOR(lybctx->subtrees, u) {
112 /* decrease all written counters */
113 lybctx->subtrees[u].written -= to_read;
114 assert(lybctx->subtrees[u].written <= LYB_SIZE_MAX);
115 }
116 /* decrease count/buf */
117 count -= to_read;
118 if (buf) {
119 buf += to_read;
120 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200121 }
122
123 if (empty) {
124 /* read the next chunk meta information */
Michal Vasko63f3d842020-07-08 10:10:14 +0200125 ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200126 empty->written = meta_buf[0];
127 empty->inner_chunks = meta_buf[1];
128
129 /* remember whether there is a following chunk or not */
130 empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200131 }
132 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200133}
134
135/**
136 * @brief Read a number.
137 *
138 * @param[in] num Destination buffer.
139 * @param[in] num_size Size of @p num.
140 * @param[in] bytes Number of bytes to read.
141 * @param[in] lybctx LYB context.
142 */
143static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200144lyb_read_number(void *num, size_t num_size, size_t bytes, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200145{
146 uint64_t buf = 0;
147
148 lyb_read((uint8_t *)&buf, bytes, lybctx);
149
150 /* correct byte order */
151 buf = le64toh(buf);
152
153 switch (num_size) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100154 case sizeof(uint8_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200155 *((uint8_t *)num) = buf;
156 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100157 case sizeof(uint16_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200158 *((uint16_t *)num) = buf;
159 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100160 case sizeof(uint32_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200161 *((uint32_t *)num) = buf;
162 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100163 case sizeof(uint64_t):
Michal Vasko60ea6352020-06-29 13:39:39 +0200164 *((uint64_t *)num) = buf;
165 break;
166 default:
167 LOGINT(lybctx->ctx);
168 }
169}
170
171/**
172 * @brief Read a string.
173 *
174 * @param[in] str Destination buffer, is allocated.
175 * @param[in] with_length Whether the string is preceded with its length or it ends at the end of this subtree.
176 * @param[in] lybctx LYB context.
177 * @return LY_ERR value.
178 */
179static LY_ERR
Radek Krejci857189e2020-09-01 13:26:36 +0200180lyb_read_string(char **str, ly_bool with_length, struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200181{
Radek Krejci857189e2020-09-01 13:26:36 +0200182 ly_bool next_chunk = 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200183 size_t len = 0, cur_len;
184
185 *str = NULL;
186
187 if (with_length) {
188 lyb_read_number(&len, sizeof len, 2, lybctx);
189 } else {
190 /* read until the end of this subtree */
191 len = LYB_LAST_SUBTREE(lybctx).written;
192 if (LYB_LAST_SUBTREE(lybctx).position) {
193 next_chunk = 1;
194 }
195 }
196
197 *str = malloc((len + 1) * sizeof **str);
198 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
199
200 lyb_read((uint8_t *)*str, len, lybctx);
201
202 while (next_chunk) {
203 cur_len = LYB_LAST_SUBTREE(lybctx).written;
204 if (LYB_LAST_SUBTREE(lybctx).position) {
205 next_chunk = 1;
206 } else {
207 next_chunk = 0;
208 }
209
210 *str = ly_realloc(*str, (len + cur_len + 1) * sizeof **str);
211 LY_CHECK_ERR_RET(!*str, LOGMEM(lybctx->ctx), LY_EMEM);
212
213 lyb_read(((uint8_t *)*str) + len, cur_len, lybctx);
214
215 len += cur_len;
216 }
217
218 ((char *)*str)[len] = '\0';
219 return LY_SUCCESS;
220}
221
222/**
223 * @brief Stop the current subtree - change LYB context state.
224 *
225 * @param[in] lybctx LYB context.
226 * @return LY_ERR value.
227 */
228static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200229lyb_read_stop_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200230{
231 if (LYB_LAST_SUBTREE(lybctx).written) {
232 LOGINT_RET(lybctx->ctx);
233 }
234
235 LY_ARRAY_DECREMENT(lybctx->subtrees);
236 return LY_SUCCESS;
237}
238
239/**
240 * @brief Start a new subtree - change LYB context state but also read the expected metadata.
241 *
242 * @param[in] lybctx LYB context.
243 * @return LY_ERR value.
244 */
245static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200246lyb_read_start_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200247{
248 uint8_t meta_buf[LYB_META_BYTES];
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200249 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200250
Radek Krejcic7d13e32020-12-09 12:32:24 +0100251 u = LY_ARRAY_COUNT(lybctx->subtrees);
Michal Vasko60ea6352020-06-29 13:39:39 +0200252 if (u == lybctx->subtree_size) {
253 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
254 lybctx->subtree_size = u + LYB_SUBTREE_STEP;
255 }
256
Michal Vasko63f3d842020-07-08 10:10:14 +0200257 LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
Michal Vasko60ea6352020-06-29 13:39:39 +0200258
259 LY_ARRAY_INCREMENT(lybctx->subtrees);
260 LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
261 LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
262 LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
263
Michal Vasko60ea6352020-06-29 13:39:39 +0200264 return LY_SUCCESS;
265}
266
267/**
268 * @brief Parse YANG model info.
269 *
270 * @param[in] lybctx LYB context.
271 * @param[out] mod Parsed module.
272 * @return LY_ERR value.
273 */
274static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200275lyb_parse_model(struct lylyb_ctx *lybctx, uint32_t parse_options, const struct lys_module **mod)
Michal Vasko60ea6352020-06-29 13:39:39 +0200276{
277 LY_ERR ret = LY_SUCCESS;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100278 char *mod_name = NULL, mod_rev[LY_REV_SIZE];
Michal Vasko60ea6352020-06-29 13:39:39 +0200279 uint16_t rev;
280
281 /* model name */
282 ret = lyb_read_string(&mod_name, 1, lybctx);
283 LY_CHECK_GOTO(ret, cleanup);
284
285 /* revision */
286 lyb_read_number(&rev, sizeof rev, 2, lybctx);
287
288 if (!mod_name[0]) {
289 /* opaq node, no module */
290 *mod = NULL;
291 goto cleanup;
292 }
293
294 if (rev) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100295 sprintf(mod_rev, "%04u-%02u-%02u", ((rev & LYB_REV_YEAR_MASK) >> LYB_REV_YEAR_SHIFT) + LYB_REV_YEAR_OFFSET,
296 (rev & LYB_REV_MONTH_MASK) >> LYB_REV_MONTH_SHIFT, rev & LYB_REV_DAY_MASK);
Michal Vasko60ea6352020-06-29 13:39:39 +0200297 *mod = ly_ctx_get_module(lybctx->ctx, mod_name, mod_rev);
Radek Krejci1798aae2020-07-14 13:26:06 +0200298 if ((parse_options & LYD_PARSE_LYB_MOD_UPDATE) && !(*mod)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200299 /* try to use an updated module */
300 *mod = ly_ctx_get_module_implemented(lybctx->ctx, mod_name);
301 if (*mod && (!(*mod)->revision || (strcmp((*mod)->revision, mod_rev) < 0))) {
302 /* not an implemented module in a newer revision */
303 *mod = NULL;
304 }
305 }
306 } else {
307 *mod = ly_ctx_get_module_latest(lybctx->ctx, mod_name);
308 }
309 /* TODO data_clb supported?
310 if (lybctx->ctx->data_clb) {
311 if (!*mod) {
312 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, NULL, 0, lybctx->ctx->data_clb_data);
313 } else if (!(*mod)->implemented) {
314 *mod = lybctx->ctx->data_clb(lybctx->ctx, mod_name, (*mod)->ns, LY_MODCLB_NOT_IMPLEMENTED, lybctx->ctx->data_clb_data);
315 }
316 }*/
317
318 if (!*mod || !(*mod)->implemented) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200319 if (parse_options & LYD_PARSE_STRICT) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200320 if (!*mod) {
321 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, missing module \"%s%s%s\".",
Michal Vasko69730152020-10-09 16:30:07 +0200322 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200323 } else if (!(*mod)->implemented) {
324 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid context for LYB data parsing, module \"%s%s%s\" not implemented.",
Michal Vasko69730152020-10-09 16:30:07 +0200325 mod_name, rev ? "@" : "", rev ? mod_rev : "");
Michal Vasko60ea6352020-06-29 13:39:39 +0200326 }
327 ret = LY_EINVAL;
328 goto cleanup;
329 }
330
331 }
332
333cleanup:
334 free(mod_name);
335 return ret;
336}
337
338/**
339 * @brief Parse YANG node metadata.
340 *
341 * @param[in] lybctx LYB context.
Michal Vasko60ea6352020-06-29 13:39:39 +0200342 * @param[out] meta Parsed metadata.
343 * @return LY_ERR value.
344 */
345static LY_ERR
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200346lyb_parse_metadata(struct lyd_lyb_ctx *lybctx, struct lyd_meta **meta)
Michal Vasko60ea6352020-06-29 13:39:39 +0200347{
348 LY_ERR ret = LY_SUCCESS;
Radek Krejci857189e2020-09-01 13:26:36 +0200349 ly_bool dynamic;
Michal Vasko60ea6352020-06-29 13:39:39 +0200350 uint8_t i, count = 0;
Michal Vasko1e5d5612020-07-03 13:29:26 +0200351 char *meta_name = NULL, *meta_value;
Michal Vasko60ea6352020-06-29 13:39:39 +0200352 const struct lys_module *mod;
353
354 /* read number of attributes stored */
Radek Krejci1798aae2020-07-14 13:26:06 +0200355 lyb_read(&count, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200356
357 /* read attributes */
358 for (i = 0; i < count; ++i) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200359 ret = lyb_read_start_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200360 LY_CHECK_GOTO(ret, cleanup);
361
362 /* find model */
Michal Vaskoe0665742021-02-11 11:08:44 +0100363 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200364 LY_CHECK_GOTO(ret, cleanup);
365
366 if (!mod) {
367 /* skip it */
368 do {
Radek Krejci1798aae2020-07-14 13:26:06 +0200369 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx->lybctx).written, lybctx->lybctx);
370 } while (LYB_LAST_SUBTREE(lybctx->lybctx).written);
Michal Vasko60ea6352020-06-29 13:39:39 +0200371 goto stop_subtree;
372 }
373
374 /* meta name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200375 ret = lyb_read_string(&meta_name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200376 LY_CHECK_GOTO(ret, cleanup);
377
378 /* meta value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200379 ret = lyb_read_string(&meta_value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200380 LY_CHECK_GOTO(ret, cleanup);
381 dynamic = 1;
382
383 /* create metadata */
Michal Vaskoc8a230d2020-08-14 12:17:10 +0200384 ret = lyd_parser_create_meta((struct lyd_ctx *)lybctx, NULL, meta, mod, meta_name, strlen(meta_name), meta_value,
Michal Vasko69730152020-10-09 16:30:07 +0200385 ly_strlen(meta_value), &dynamic, LY_PREF_JSON, NULL, LYD_HINT_DATA);
Michal Vasko60ea6352020-06-29 13:39:39 +0200386
387 /* free strings */
388 free(meta_name);
389 meta_name = NULL;
390 if (dynamic) {
391 free(meta_value);
392 dynamic = 0;
393 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200394
Radek Krejci1798aae2020-07-14 13:26:06 +0200395 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200396
397stop_subtree:
Radek Krejci1798aae2020-07-14 13:26:06 +0200398 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200399 LY_CHECK_GOTO(ret, cleanup);
400 }
401
402cleanup:
403 free(meta_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200404 if (ret) {
Michal Vasko3a41dff2020-07-15 14:30:28 +0200405 lyd_free_meta_siblings(*meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200406 *meta = NULL;
407 }
408 return ret;
409}
410
411/**
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100412 * @brief Parse format-specific prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200413 *
414 * @param[in] lybctx LYB context.
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100415 * @param[in] format Prefix data format.
416 * @param[out] prefix_data Parsed prefix data.
Michal Vasko60ea6352020-06-29 13:39:39 +0200417 * @return LY_ERR value.
418 */
419static LY_ERR
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100420lyb_parse_prefix_data(struct lylyb_ctx *lybctx, LY_PREFIX_FORMAT format, void **prefix_data)
Michal Vasko60ea6352020-06-29 13:39:39 +0200421{
422 LY_ERR ret = LY_SUCCESS;
423 uint8_t count, i;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100424 struct ly_set *set = NULL;
425 struct lyxml_ns *ns = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200426
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100427 switch (format) {
428 case LY_PREF_XML:
429 /* read count */
430 lyb_read(&count, 1, lybctx);
431 if (!count) {
432 return LY_SUCCESS;
433 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200434
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100435 /* read all NS elements */
436 LY_CHECK_GOTO(ret = ly_set_new(&set), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200437
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100438 for (i = 0; i < count; ++i) {
439 ns = calloc(1, sizeof *ns);
Michal Vasko60ea6352020-06-29 13:39:39 +0200440
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100441 /* prefix */
442 LY_CHECK_GOTO(ret = lyb_read_string(&ns->prefix, 1, lybctx), cleanup);
443
444 /* namespace */
445 LY_CHECK_GOTO(ret = lyb_read_string(&ns->uri, 1, lybctx), cleanup);
446
447 LY_CHECK_GOTO(ret = ly_set_add(set, ns, 1, NULL), cleanup);
448 ns = NULL;
449 }
450
451 *prefix_data = set;
452 break;
453 case LY_PREF_JSON:
454 /* nothing stored */
455 break;
456 default:
457 LOGINT(lybctx->ctx);
458 ret = LY_EINT;
459 break;
Michal Vasko60ea6352020-06-29 13:39:39 +0200460 }
461
462cleanup:
463 if (ret) {
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100464 ly_free_prefix_data(format, set);
465 if (ns) {
466 free(ns->prefix);
467 free(ns->uri);
468 free(ns);
469 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200470 }
471 return ret;
472}
473
474/**
475 * @brief Parse opaque attributes.
476 *
477 * @param[in] lybctx LYB context.
478 * @param[out] attr Parsed attributes.
479 * @return LY_ERR value.
480 */
481static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200482lyb_parse_attributes(struct lylyb_ctx *lybctx, struct lyd_attr **attr)
Michal Vasko60ea6352020-06-29 13:39:39 +0200483{
484 LY_ERR ret = LY_SUCCESS;
485 uint8_t count, i;
Radek Krejci1798aae2020-07-14 13:26:06 +0200486 struct lyd_attr *attr2;
487 char *prefix = NULL, *module_name = NULL, *name = NULL, *value = NULL;
Radek Krejci857189e2020-09-01 13:26:36 +0200488 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100489 LY_PREFIX_FORMAT format = 0;
490 void *val_prefix_data = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200491
492 /* read count */
493 lyb_read(&count, 1, lybctx);
494
495 /* read attributes */
496 for (i = 0; i < count; ++i) {
497 ret = lyb_read_start_subtree(lybctx);
498 LY_CHECK_GOTO(ret, cleanup);
499
Michal Vasko0fdcd242020-11-11 19:12:30 +0100500 /* prefix, may be empty */
Michal Vasko60ea6352020-06-29 13:39:39 +0200501 ret = lyb_read_string(&prefix, 1, lybctx);
502 LY_CHECK_GOTO(ret, cleanup);
503 if (!prefix[0]) {
504 free(prefix);
505 prefix = NULL;
506 }
507
508 /* namespace, may be empty */
Radek Krejci1798aae2020-07-14 13:26:06 +0200509 ret = lyb_read_string(&module_name, 1, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200510 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci1798aae2020-07-14 13:26:06 +0200511 if (!module_name[0]) {
512 free(module_name);
513 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200514 }
515
516 /* name */
517 ret = lyb_read_string(&name, 1, lybctx);
518 LY_CHECK_GOTO(ret, cleanup);
519
Michal Vasko60ea6352020-06-29 13:39:39 +0200520 /* format */
521 lyb_read((uint8_t *)&format, 1, lybctx);
522
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100523 /* value prefixes */
524 ret = lyb_parse_prefix_data(lybctx, format, &val_prefix_data);
525 LY_CHECK_GOTO(ret, cleanup);
526
Michal Vasko60ea6352020-06-29 13:39:39 +0200527 /* value */
528 ret = lyb_read_string(&value, 0, lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100529 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200530 dynamic = 1;
531
532 /* attr2 is always changed to the created attribute */
Michal Vasko501af032020-11-11 20:27:44 +0100533 ret = lyd_create_attr(NULL, &attr2, lybctx->ctx, name, strlen(name), prefix, ly_strlen(prefix), module_name,
534 ly_strlen(module_name), value, ly_strlen(value), &dynamic, format, val_prefix_data, 0);
Michal Vasko60ea6352020-06-29 13:39:39 +0200535 LY_CHECK_GOTO(ret, cleanup);
536
537 free(prefix);
538 prefix = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200539 free(module_name);
540 module_name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200541 free(name);
542 name = NULL;
Michal Vasko60ea6352020-06-29 13:39:39 +0200543 assert(!dynamic);
544 value = NULL;
545
546 if (!*attr) {
547 *attr = attr2;
548 }
549
550 ret = lyb_read_stop_subtree(lybctx);
551 LY_CHECK_GOTO(ret, cleanup);
552 }
553
554cleanup:
555 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200556 free(module_name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200557 free(name);
558 if (dynamic) {
559 free(value);
560 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200561 if (ret) {
Radek Krejci011e4aa2020-09-04 15:22:31 +0200562 lyd_free_attr_siblings(lybctx->ctx, *attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200563 *attr = NULL;
564 }
565 return ret;
566}
567
568/**
569 * @brief Check whether a schema node matches a hash(es).
570 *
571 * @param[in] sibling Schema node to check.
572 * @param[in] hash Hash array to check.
573 * @param[in] hash_count Number of hashes in @p hash.
574 * @return non-zero if matches,
575 * @return 0 if not.
576 */
577static int
578lyb_is_schema_hash_match(struct lysc_node *sibling, LYB_HASH *hash, uint8_t hash_count)
579{
580 LYB_HASH sibling_hash;
581 uint8_t i;
582
583 /* compare all the hashes starting from collision ID 0 */
584 for (i = 0; i < hash_count; ++i) {
585 sibling_hash = lyb_hash(sibling, i);
586 if (sibling_hash != hash[i]) {
587 return 0;
588 }
589 }
590
591 return 1;
592}
593
594/**
Michal Vasko60ea6352020-06-29 13:39:39 +0200595 * @brief Parse schema node hash.
596 *
597 * @param[in] lybctx LYB context.
598 * @param[in] sparent Schema parent, must be set if @p mod is not.
599 * @param[in] mod Module of the top-level node, must be set if @p sparent is not.
600 * @param[out] snode Parsed found schema node, may be NULL if opaque.
601 * @return LY_ERR value.
602 */
603static LY_ERR
604lyb_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 +0200605 const struct lysc_node **snode)
Michal Vasko60ea6352020-06-29 13:39:39 +0200606{
607 LY_ERR ret;
608 uint8_t i, j;
609 const struct lysc_node *sibling;
610 LYB_HASH hash[LYB_HASH_BITS - 1];
Radek Krejci1deb5be2020-08-26 16:43:36 +0200611 uint32_t getnext_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +0200612
613 *snode = NULL;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100614 getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
Michal Vasko60ea6352020-06-29 13:39:39 +0200615
616 /* read the first hash */
Radek Krejci1798aae2020-07-14 13:26:06 +0200617 lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200618
619 if (!hash[0]) {
620 /* opaque node */
621 return LY_SUCCESS;
622 }
623
624 /* based on the first hash read all the other ones, if any */
625 for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
626 if (i > LYB_HASH_BITS) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200627 LOGINT_RET(lybctx->lybctx->ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200628 }
629 }
630
631 /* move the first hash on its accurate position */
632 hash[i] = hash[0];
633
634 /* read the rest of hashes */
635 for (j = i; j; --j) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200636 lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200637
638 /* correct collision ID */
639 assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
640 /* preceded with zeros */
641 assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
642 }
643
644 /* find our node with matching hashes */
645 sibling = NULL;
Radek Krejcif16e2542021-02-17 15:39:23 +0100646 while (1) {
647 if (!sparent && lybctx->ext) {
648 sibling = lys_getnext_ext(sibling, sparent, lybctx->ext, getnext_opts);
649 } else {
650 sibling = lys_getnext(sibling, sparent, mod ? mod->compiled : NULL, getnext_opts);
651 }
652 if (!sibling) {
653 break;
654 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200655 /* skip schema nodes from models not present during printing */
Michal Vasko69730152020-10-09 16:30:07 +0200656 if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
657 lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200658 /* match found */
659 break;
660 }
661 }
662
Michal Vaskoe0665742021-02-11 11:08:44 +0100663 if (!sibling && (lybctx->parse_opts & LYD_PARSE_STRICT)) {
Radek Krejcif16e2542021-02-17 15:39:23 +0100664 if (lybctx->ext) {
665 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a node from \"%s\" extension instance node.",
666 lybctx->ext->def->name);
667 } else if (mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100668 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a top-level node"
Michal Vasko69730152020-10-09 16:30:07 +0200669 " from \"%s\".", mod->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200670 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100671 LOGVAL(lybctx->lybctx->ctx, LYVE_REFERENCE, "Failed to find matching hash for a child node"
Michal Vasko69730152020-10-09 16:30:07 +0200672 " of \"%s\".", sparent->name);
Michal Vasko60ea6352020-06-29 13:39:39 +0200673 }
674 return LY_EVALID;
Radek Krejci1798aae2020-07-14 13:26:06 +0200675 } else if (sibling && (ret = lyd_parser_check_schema((struct lyd_ctx *)lybctx, sibling))) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200676 return ret;
677 }
678
679 *snode = sibling;
680 return LY_SUCCESS;
681}
682
683/**
684 * @brief Read until the end of the current subtree.
685 *
686 * @param[in] lybctx LYB context.
687 */
688static void
Radek Krejci1798aae2020-07-14 13:26:06 +0200689lyb_skip_subtree(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200690{
Michal Vasko60ea6352020-06-29 13:39:39 +0200691 do {
692 /* first skip any meta information inside */
Michal Vasko63f3d842020-07-08 10:10:14 +0200693 ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
Michal Vasko60ea6352020-06-29 13:39:39 +0200694
695 /* then read data */
696 lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
697 } while (LYB_LAST_SUBTREE(lybctx).written);
698}
699
700/**
701 * @brief Parse LYB subtree.
702 *
703 * @param[in] lybctx LYB context.
704 * @param[in] parent Data parent of the subtree, must be set if @p first is not.
705 * @param[in,out] first First top-level sibling, must be set if @p parent is not.
706 * @return LY_ERR value.
707 */
708static LY_ERR
Michal Vaskoe0665742021-02-11 11:08:44 +0100709lyb_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 +0200710{
711 LY_ERR ret = LY_SUCCESS;
712 struct lyd_node *node = NULL, *tree;
713 const struct lys_module *mod;
714 const struct lysc_node *snode = NULL;
715 struct lyd_meta *meta = NULL, *m;
Radek Krejci1798aae2020-07-14 13:26:06 +0200716 struct lyd_attr *attr = NULL, *a;
Michal Vasko60ea6352020-06-29 13:39:39 +0200717 LYD_ANYDATA_VALUETYPE value_type;
Radek Krejci1798aae2020-07-14 13:26:06 +0200718 char *value = NULL, *name = NULL, *prefix = NULL, *module_key = NULL;
Michal Vasko9afe3df2021-02-05 16:33:50 +0100719 const char *val_dict;
Radek Krejci857189e2020-09-01 13:26:36 +0200720 ly_bool dynamic = 0;
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100721 LY_PREFIX_FORMAT format = 0;
722 void *val_prefix_data = NULL;
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100723 uint32_t prev_lo, flags;
Radek Krejci1798aae2020-07-14 13:26:06 +0200724 const struct ly_ctx *ctx = lybctx->lybctx->ctx;
Michal Vasko60ea6352020-06-29 13:39:39 +0200725
726 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200727 LY_CHECK_GOTO(ret = lyb_read_start_subtree(lybctx->lybctx), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200728
729 if (!parent) {
730 /* top-level, read module name */
Michal Vaskoe0665742021-02-11 11:08:44 +0100731 ret = lyb_parse_model(lybctx->lybctx, lybctx->parse_opts, &mod);
Michal Vasko60ea6352020-06-29 13:39:39 +0200732 LY_CHECK_GOTO(ret, cleanup);
733
734 /* read hash, find the schema node starting from mod */
735 ret = lyb_parse_schema_hash(lybctx, NULL, mod, &snode);
736 LY_CHECK_GOTO(ret, cleanup);
737 } else {
738 /* read hash, find the schema node starting from parent schema */
739 ret = lyb_parse_schema_hash(lybctx, parent->schema, NULL, &snode);
740 LY_CHECK_GOTO(ret, cleanup);
741 }
742
Michal Vaskoe0665742021-02-11 11:08:44 +0100743 if (!snode && !(lybctx->parse_opts & LYD_PARSE_OPAQ)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200744 /* unknown data, skip them */
Radek Krejci1798aae2020-07-14 13:26:06 +0200745 lyb_skip_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200746 goto stop_subtree;
Radek Krejci0f969882020-08-21 16:56:47 +0200747 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200748
749 /* create metadata/attributes */
750 if (snode) {
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200751 ret = lyb_parse_metadata(lybctx, &meta);
Michal Vasko60ea6352020-06-29 13:39:39 +0200752 LY_CHECK_GOTO(ret, cleanup);
753 } else {
Radek Krejci1798aae2020-07-14 13:26:06 +0200754 ret = lyb_parse_attributes(lybctx->lybctx, &attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200755 LY_CHECK_GOTO(ret, cleanup);
756 }
757
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100758 /* read flags */
759 lyb_read_number(&flags, sizeof flags, sizeof flags, lybctx->lybctx);
760
Michal Vasko60ea6352020-06-29 13:39:39 +0200761 if (!snode) {
762 /* parse prefix */
Radek Krejci1798aae2020-07-14 13:26:06 +0200763 ret = lyb_read_string(&prefix, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200764 LY_CHECK_GOTO(ret, cleanup);
765
Radek Krejci1798aae2020-07-14 13:26:06 +0200766 /* parse module key */
767 ret = lyb_read_string(&module_key, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200768 LY_CHECK_GOTO(ret, cleanup);
769
770 /* parse name */
Radek Krejci1798aae2020-07-14 13:26:06 +0200771 ret = lyb_read_string(&name, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200772 LY_CHECK_GOTO(ret, cleanup);
773
Michal Vasko60ea6352020-06-29 13:39:39 +0200774 /* parse format */
Radek Krejci1798aae2020-07-14 13:26:06 +0200775 lyb_read((uint8_t *)&format, 1, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200776
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100777 /* parse value prefixes */
778 ret = lyb_parse_prefix_data(lybctx->lybctx, format, &val_prefix_data);
779 LY_CHECK_GOTO(ret, cleanup);
780
Michal Vasko60ea6352020-06-29 13:39:39 +0200781 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200782 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko6b5cb2a2020-11-11 19:11:21 +0100783 LY_CHECK_ERR_GOTO(ret, ly_free_prefix_data(format, val_prefix_data), cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200784 dynamic = 1;
785
786 /* create node */
Michal Vasko501af032020-11-11 20:27:44 +0100787 ret = lyd_create_opaq(ctx, name, strlen(name), prefix, ly_strlen(prefix), module_key, ly_strlen(module_key),
788 value, strlen(value), &dynamic, format, val_prefix_data, 0, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200789 LY_CHECK_GOTO(ret, cleanup);
790
791 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200792 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100793 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200794 LY_CHECK_GOTO(ret, cleanup);
795 }
796 } else if (snode->nodetype & LYD_NODE_TERM) {
797 /* parse value */
Radek Krejci1798aae2020-07-14 13:26:06 +0200798 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200799 LY_CHECK_GOTO(ret, cleanup);
800 dynamic = 1;
801
802 /* create node */
Michal Vaskofeca4fb2020-10-05 08:58:40 +0200803 ret = lyd_parser_create_term((struct lyd_ctx *)lybctx, snode, value, ly_strlen(value), &dynamic, LY_PREF_JSON,
Michal Vasko69730152020-10-09 16:30:07 +0200804 NULL, LYD_HINT_DATA, &node);
Michal Vasko60ea6352020-06-29 13:39:39 +0200805 if (dynamic) {
806 free(value);
807 dynamic = 0;
808 }
809 value = NULL;
Radek Krejci1798aae2020-07-14 13:26:06 +0200810 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200811 } else if (snode->nodetype & LYD_NODE_INNER) {
812 /* create node */
813 ret = lyd_create_inner(snode, &node);
814 LY_CHECK_GOTO(ret, cleanup);
815
816 /* process children */
Radek Krejci1798aae2020-07-14 13:26:06 +0200817 while (LYB_LAST_SUBTREE(lybctx->lybctx).written) {
Michal Vaskoe0665742021-02-11 11:08:44 +0100818 ret = lyb_parse_subtree_r(lybctx, node, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200819 LY_CHECK_GOTO(ret, cleanup);
820 }
821
Michal Vaskoe0665742021-02-11 11:08:44 +0100822 if (!(lybctx->parse_opts & LYD_PARSE_ONLY)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200823 /* new node validation, autodelete CANNOT occur, all nodes are new */
Michal Vaskoe0665742021-02-11 11:08:44 +0100824 ret = lyd_validate_new(lyd_node_child_p(node), snode, NULL, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200825 LY_CHECK_GOTO(ret, cleanup);
826
827 /* add any missing default children */
Radek Krejci4f2e3e52021-03-30 14:20:28 +0200828 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 +0100829 &lybctx->node_types, (lybctx->val_opts & LYD_VALIDATE_NO_STATE) ? LYD_IMPLICIT_NO_STATE : 0, NULL);
Michal Vasko60ea6352020-06-29 13:39:39 +0200830 LY_CHECK_GOTO(ret, cleanup);
831 }
832
Michal Vasko751cb4d2020-07-14 12:25:28 +0200833 if (snode->nodetype & (LYS_RPC | LYS_ACTION | LYS_NOTIF)) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200834 /* rememeber the RPC/action/notification */
Radek Krejci1798aae2020-07-14 13:26:06 +0200835 lybctx->op_node = node;
Michal Vasko60ea6352020-06-29 13:39:39 +0200836 }
837 } else if (snode->nodetype & LYD_NODE_ANY) {
838 /* parse value type */
Radek Krejci1798aae2020-07-14 13:26:06 +0200839 lyb_read((uint8_t *)&value_type, sizeof value_type, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200840 if (value_type == LYD_ANYDATA_DATATREE) {
841 /* invalid situation */
Radek Krejci1798aae2020-07-14 13:26:06 +0200842 LOGINT(ctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200843 goto cleanup;
844 }
845
846 /* read anydata content */
Radek Krejci1798aae2020-07-14 13:26:06 +0200847 ret = lyb_read_string(&value, 0, lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200848 LY_CHECK_GOTO(ret, cleanup);
849 dynamic = 1;
850
851 if (value_type == LYD_ANYDATA_LYB) {
852 /* turn logging off */
853 prev_lo = ly_log_options(0);
854
855 /* try to parse LYB into a data tree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200856 if (lyd_parse_data_mem(ctx, value, LYD_LYB, LYD_PARSE_ONLY | LYD_PARSE_OPAQ | LYD_PARSE_STRICT, 0, &tree) == LY_SUCCESS) {
Michal Vasko60ea6352020-06-29 13:39:39 +0200857 /* successfully parsed */
858 free(value);
859 value = (char *)tree;
860 value_type = LYD_ANYDATA_DATATREE;
861 }
Radek Krejci7931b192020-06-25 17:05:03 +0200862
863 /* turn logging on again */
864 ly_log_options(prev_lo);
Michal Vasko60ea6352020-06-29 13:39:39 +0200865 }
866
Michal Vasko9afe3df2021-02-05 16:33:50 +0100867 /* create the node */
868 switch (value_type) {
869 case LYD_ANYDATA_LYB:
870 case LYD_ANYDATA_DATATREE:
871 /* use the value directly */
872 ret = lyd_create_any(snode, value, value_type, 1, &node);
873 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +0200874
Michal Vasko9afe3df2021-02-05 16:33:50 +0100875 dynamic = 0;
876 value = NULL;
877 break;
878 case LYD_ANYDATA_STRING:
879 case LYD_ANYDATA_XML:
880 case LYD_ANYDATA_JSON:
881 /* value is expected to be in the dictionary */
882 ret = lydict_insert_zc(ctx, value, &val_dict);
883 LY_CHECK_GOTO(ret, cleanup);
884 dynamic = 0;
885 value = NULL;
886
887 /* use the value in the dictionary */
888 ret = lyd_create_any(snode, val_dict, value_type, 1, &node);
889 if (ret) {
890 lydict_remove(ctx, val_dict);
891 goto cleanup;
892 }
893 break;
894 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200895 }
896 assert(node);
897
Michal Vaskoc5e866a2020-11-04 17:09:26 +0100898 /* set flags */
899 node->flags = flags;
Michal Vasko60ea6352020-06-29 13:39:39 +0200900
901 /* add metadata/attributes */
902 if (snode) {
903 LY_LIST_FOR(meta, m) {
904 m->parent = node;
905 }
906 node->meta = meta;
907 meta = NULL;
908 } else {
909 assert(!node->schema);
910 LY_LIST_FOR(attr, a) {
911 a->parent = (struct lyd_node_opaq *)node;
912 }
913 ((struct lyd_node_opaq *)node)->attr = attr;
914 attr = NULL;
915 }
916
Michal Vaskob104f112020-07-17 09:54:54 +0200917 /* insert, keep first pointer correct */
Michal Vaskoe0665742021-02-11 11:08:44 +0100918 lyd_insert_node(parent, first_p, node);
919 while (!parent && (*first_p)->prev->next) {
920 *first_p = (*first_p)->prev;
921 }
922
923 /* rememeber a successfully parsed node */
924 if (parsed) {
925 ly_set_add(parsed, node, 1, NULL);
Michal Vaskob104f112020-07-17 09:54:54 +0200926 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200927 node = NULL;
928
929stop_subtree:
930 /* end the subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +0200931 ret = lyb_read_stop_subtree(lybctx->lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +0200932 LY_CHECK_GOTO(ret, cleanup);
933
934cleanup:
935 free(prefix);
Radek Krejci1798aae2020-07-14 13:26:06 +0200936 free(module_key);
Michal Vasko60ea6352020-06-29 13:39:39 +0200937 free(name);
938 if (dynamic) {
939 free(value);
940 }
Michal Vasko60ea6352020-06-29 13:39:39 +0200941
Michal Vasko3a41dff2020-07-15 14:30:28 +0200942 lyd_free_meta_siblings(meta);
Radek Krejci011e4aa2020-09-04 15:22:31 +0200943 lyd_free_attr_siblings(ctx, attr);
Michal Vasko60ea6352020-06-29 13:39:39 +0200944 lyd_free_tree(node);
945 return ret;
946}
947
948/**
949 * @brief Parse used YANG data models.
950 *
951 * @param[in] lybctx LYB context.
952 * @return LY_ERR value.
953 */
954static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200955lyb_parse_data_models(struct lylyb_ctx *lybctx, uint32_t parse_options)
Michal Vasko60ea6352020-06-29 13:39:39 +0200956{
957 LY_ERR ret;
958 uint32_t count;
Michal Vaskofd69e1d2020-07-03 11:57:17 +0200959 LY_ARRAY_COUNT_TYPE u;
Michal Vasko60ea6352020-06-29 13:39:39 +0200960
961 /* read model count */
962 lyb_read_number(&count, sizeof count, 2, lybctx);
963
964 if (count) {
965 LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->models, count, LY_EMEM);
966
967 /* read modules */
968 for (u = 0; u < count; ++u) {
Radek Krejci1798aae2020-07-14 13:26:06 +0200969 ret = lyb_parse_model(lybctx, parse_options, &lybctx->models[u]);
Michal Vasko60ea6352020-06-29 13:39:39 +0200970 LY_CHECK_RET(ret);
971 LY_ARRAY_INCREMENT(lybctx->models);
972 }
973 }
974
975 return LY_SUCCESS;
976}
977
978/**
979 * @brief Parse LYB magic number.
980 *
981 * @param[in] lybctx LYB context.
982 * @return LY_ERR value.
983 */
984static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +0200985lyb_parse_magic_number(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +0200986{
987 char magic_byte = 0;
988
989 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
990 if (magic_byte != 'l') {
991 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid first magic number byte \"0x%02x\".", magic_byte);
992 return LY_EINVAL;
993 }
994
995 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
996 if (magic_byte != 'y') {
997 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid second magic number byte \"0x%02x\".", magic_byte);
998 return LY_EINVAL;
999 }
1000
1001 lyb_read((uint8_t *)&magic_byte, 1, lybctx);
1002 if (magic_byte != 'b') {
1003 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid third magic number byte \"0x%02x\".", magic_byte);
1004 return LY_EINVAL;
1005 }
1006
1007 return LY_SUCCESS;
1008}
1009
1010/**
1011 * @brief Parse LYB header.
1012 *
1013 * @param[in] lybctx LYB context.
1014 * @return LY_ERR value.
1015 */
1016static LY_ERR
Radek Krejci1798aae2020-07-14 13:26:06 +02001017lyb_parse_header(struct lylyb_ctx *lybctx)
Michal Vasko60ea6352020-06-29 13:39:39 +02001018{
1019 uint8_t byte = 0;
1020
1021 /* version, future flags */
1022 lyb_read((uint8_t *)&byte, sizeof byte, lybctx);
1023
1024 if ((byte & LYB_VERSION_MASK) != LYB_VERSION_NUM) {
1025 LOGERR(lybctx->ctx, LY_EINVAL, "Invalid LYB format version \"0x%02x\", expected \"0x%02x\".",
Michal Vasko69730152020-10-09 16:30:07 +02001026 byte & LYB_VERSION_MASK, LYB_VERSION_NUM);
Michal Vasko60ea6352020-06-29 13:39:39 +02001027 return LY_EINVAL;
1028 }
1029
1030 return LY_SUCCESS;
1031}
1032
Michal Vaskoe0665742021-02-11 11:08:44 +01001033LY_ERR
Radek Krejcif16e2542021-02-17 15:39:23 +01001034lyd_parse_lyb(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, struct lyd_node *parent,
1035 struct lyd_node **first_p, struct ly_in *in, uint32_t parse_opts, uint32_t val_opts, enum lyd_type data_type,
1036 struct ly_set *parsed, struct lyd_ctx **lydctx_p)
Michal Vasko60ea6352020-06-29 13:39:39 +02001037{
Michal Vaskoe0665742021-02-11 11:08:44 +01001038 LY_ERR rc = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001039 struct lyd_lyb_ctx *lybctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001040 uint32_t int_opts;
Michal Vasko60ea6352020-06-29 13:39:39 +02001041
Michal Vaskoe0665742021-02-11 11:08:44 +01001042 assert(!(parse_opts & ~LYD_PARSE_OPTS_MASK));
1043 assert(!(val_opts & ~LYD_VALIDATE_OPTS_MASK));
Radek Krejci7931b192020-06-25 17:05:03 +02001044
Radek Krejci1798aae2020-07-14 13:26:06 +02001045 lybctx = calloc(1, sizeof *lybctx);
1046 LY_CHECK_ERR_RET(!lybctx, LOGMEM(ctx), LY_EMEM);
1047 lybctx->lybctx = calloc(1, sizeof *lybctx->lybctx);
Michal Vaskoe0665742021-02-11 11:08:44 +01001048 LY_CHECK_ERR_GOTO(!lybctx->lybctx, LOGMEM(ctx); rc = LY_EMEM, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001049
Radek Krejci1798aae2020-07-14 13:26:06 +02001050 lybctx->lybctx->in = in;
1051 lybctx->lybctx->ctx = ctx;
Michal Vaskoe0665742021-02-11 11:08:44 +01001052 lybctx->parse_opts = parse_opts;
1053 lybctx->val_opts = val_opts;
Radek Krejci1798aae2020-07-14 13:26:06 +02001054 lybctx->free = lyd_lyb_ctx_free;
Michal Vasko60ea6352020-06-29 13:39:39 +02001055
Michal Vaskoe0665742021-02-11 11:08:44 +01001056 switch (data_type) {
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001057 case LYD_TYPE_DATA_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001058 int_opts = LYD_INTOPT_WITH_SIBLINGS;
1059 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001060 case LYD_TYPE_RPC_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001061 int_opts = LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NO_SIBLINGS;
1062 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001063 case LYD_TYPE_NOTIF_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001064 int_opts = LYD_INTOPT_NOTIF | LYD_INTOPT_NO_SIBLINGS;
1065 break;
Michal Vasko1e4c68e2021-02-18 15:03:01 +01001066 case LYD_TYPE_REPLY_YANG:
Michal Vaskoe0665742021-02-11 11:08:44 +01001067 int_opts = LYD_INTOPT_REPLY | LYD_INTOPT_NO_SIBLINGS;
1068 break;
1069 default:
1070 LOGINT(ctx);
1071 rc = LY_EINT;
1072 goto cleanup;
1073 }
1074 lybctx->int_opts = int_opts;
Radek Krejcif16e2542021-02-17 15:39:23 +01001075 lybctx->ext = ext;
Michal Vaskoe0665742021-02-11 11:08:44 +01001076
1077 /* find the operation node if it exists already */
1078 LY_CHECK_GOTO(rc = lyd_parser_find_operation(parent, int_opts, &lybctx->op_node), cleanup);
1079
Michal Vasko60ea6352020-06-29 13:39:39 +02001080 /* read magic number */
Michal Vaskoe0665742021-02-11 11:08:44 +01001081 rc = lyb_parse_magic_number(lybctx->lybctx);
1082 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001083
1084 /* read header */
Michal Vaskoe0665742021-02-11 11:08:44 +01001085 rc = lyb_parse_header(lybctx->lybctx);
1086 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001087
1088 /* read used models */
Michal Vaskoe0665742021-02-11 11:08:44 +01001089 rc = lyb_parse_data_models(lybctx->lybctx, lybctx->parse_opts);
1090 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001091
1092 /* read subtree(s) */
Radek Krejci1798aae2020-07-14 13:26:06 +02001093 while (lybctx->lybctx->in->current[0]) {
Michal Vaskoe0665742021-02-11 11:08:44 +01001094 rc = lyb_parse_subtree_r(lybctx, parent, first_p, parsed);
1095 LY_CHECK_GOTO(rc, cleanup);
1096
1097 if (!(int_opts & LYD_INTOPT_WITH_SIBLINGS)) {
1098 break;
1099 }
1100 }
1101
1102 if ((int_opts & LYD_INTOPT_NO_SIBLINGS) && lybctx->lybctx->in->current[0]) {
1103 LOGVAL(ctx, LYVE_SYNTAX, "Unexpected sibling node.");
1104 rc = LY_EVALID;
1105 goto cleanup;
1106 }
1107 if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY)) && !lybctx->op_node) {
1108 LOGVAL(ctx, LYVE_DATA, "Missing the operation node.");
1109 rc = LY_EVALID;
1110 goto cleanup;
Michal Vasko60ea6352020-06-29 13:39:39 +02001111 }
1112
1113 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001114 ly_in_skip(lybctx->lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001115
Michal Vasko60ea6352020-06-29 13:39:39 +02001116cleanup:
Michal Vaskoe0665742021-02-11 11:08:44 +01001117 /* there should be no unres stored if validation should be skipped */
1118 assert(!(parse_opts & LYD_PARSE_ONLY) || (!lybctx->node_types.count && !lybctx->meta_types.count &&
1119 !lybctx->node_when.count));
1120
1121 if (rc) {
Radek Krejci1798aae2020-07-14 13:26:06 +02001122 lyd_lyb_ctx_free((struct lyd_ctx *)lybctx);
Radek Krejci1798aae2020-07-14 13:26:06 +02001123 } else {
Michal Vaskoe0665742021-02-11 11:08:44 +01001124 *lydctx_p = (struct lyd_ctx *)lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001125 }
Michal Vaskoe0665742021-02-11 11:08:44 +01001126 return rc;
Michal Vasko60ea6352020-06-29 13:39:39 +02001127}
1128
1129API int
1130lyd_lyb_data_length(const char *data)
1131{
1132 LY_ERR ret = LY_SUCCESS;
Radek Krejci1798aae2020-07-14 13:26:06 +02001133 struct lylyb_ctx *lybctx;
Michal Vasko60ea6352020-06-29 13:39:39 +02001134 int count, i;
1135 size_t len;
1136 uint8_t buf[LYB_SIZE_MAX];
1137
1138 if (!data) {
1139 return -1;
1140 }
1141
Radek Krejci1798aae2020-07-14 13:26:06 +02001142 lybctx = calloc(1, sizeof *lybctx);
1143 LY_CHECK_ERR_RET(!lybctx, LOGMEM(NULL), LY_EMEM);
1144 ret = ly_in_new_memory(data, &lybctx->in);
Michal Vasko63f3d842020-07-08 10:10:14 +02001145 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko60ea6352020-06-29 13:39:39 +02001146
1147 /* read magic number */
Radek Krejci1798aae2020-07-14 13:26:06 +02001148 ret = lyb_parse_magic_number(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001149 LY_CHECK_GOTO(ret, cleanup);
1150
1151 /* read header */
Radek Krejci1798aae2020-07-14 13:26:06 +02001152 ret = lyb_parse_header(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001153 LY_CHECK_GOTO(ret, cleanup);
1154
1155 /* read model count */
Radek Krejci1798aae2020-07-14 13:26:06 +02001156 lyb_read_number(&count, sizeof count, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001157
1158 /* read all models */
1159 for (i = 0; i < count; ++i) {
1160 /* module name length */
1161 len = 0;
Radek Krejci1798aae2020-07-14 13:26:06 +02001162 lyb_read_number(&len, sizeof len, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001163
1164 /* model name */
Radek Krejci1798aae2020-07-14 13:26:06 +02001165 lyb_read(buf, len, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001166
1167 /* revision */
Radek Krejci1798aae2020-07-14 13:26:06 +02001168 lyb_read(buf, 2, lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001169 }
1170
Radek Krejci1798aae2020-07-14 13:26:06 +02001171 while (lybctx->in->current[0]) {
Michal Vasko60ea6352020-06-29 13:39:39 +02001172 /* register a new subtree */
Radek Krejci1798aae2020-07-14 13:26:06 +02001173 ret = lyb_read_start_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001174 LY_CHECK_GOTO(ret, cleanup);
1175
1176 /* skip it */
Radek Krejci1798aae2020-07-14 13:26:06 +02001177 lyb_skip_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001178
1179 /* subtree finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001180 ret = lyb_read_stop_subtree(lybctx);
Michal Vasko60ea6352020-06-29 13:39:39 +02001181 LY_CHECK_GOTO(ret, cleanup);
1182 }
1183
1184 /* read the last zero, parsing finished */
Radek Krejci1798aae2020-07-14 13:26:06 +02001185 ly_in_skip(lybctx->in, 1);
Michal Vasko60ea6352020-06-29 13:39:39 +02001186
1187cleanup:
Radek Krejci1798aae2020-07-14 13:26:06 +02001188 count = lybctx->in->current - lybctx->in->start;
Michal Vasko63f3d842020-07-08 10:10:14 +02001189
Radek Krejci1798aae2020-07-14 13:26:06 +02001190 ly_in_free(lybctx->in, 0);
1191 lylyb_ctx_free(lybctx);
1192
Michal Vasko63f3d842020-07-08 10:10:14 +02001193 return ret ? -1 : count;
Michal Vasko60ea6352020-06-29 13:39:39 +02001194}